lib/_provisioning/management-service.js
function httpGet (description, currentRedirects, requestOptions, verbosity,
                  responseCallback) {
  var requestUrl = toUrlString(requestOptions)
  var responseBody = ''

  if (verbosity > 1) {
    console.log('HTTP request to: ' + requestUrl)
  }

  var request = https.get(requestOptions, function (response) {
    if (verbosity > 1) {
      console.log('HTTP response status code: ' + response.statusCode)
    }
    if (response.statusCode === 302) {
      if (currentRedirects === HTTP_MAX_REDIRECTS) {
        responseCallback(provisionUtil.createFormattedDxlError(
          'Reached maximum redirects for request to ' + requestUrl, description)
        )
      } else {
        var redirectLocation = response.headers.location
        if (!redirectLocation) {
          responseCallback(provisionUtil.createFormattedDxlError(
            'Redirect with no location specified for ' +
            requestUrl, description))
          return
        }
        if (verbosity > 1) {
          console.log('HTTP response redirect to: ' + redirectLocation)
        }

        var parsedRedirectLocation = url.parse(redirectLocation)
        if ((parsedRedirectLocation.hostname &&
            (parsedRedirectLocation.hostname !== requestOptions.hostname)) ||
          (parsedRedirectLocation.port &&
            (parsedRedirectLocation.port !== requestOptions.port))) {
          responseCallback(provisionUtil.createFormattedDxlError(
              'Redirect to different host or port not supported. ' +
              'Original URL: ' + requestUrl +
              '. Redirect URL: ' + redirectLocation + '.', description))
          return
        }

        if (response.headers['set-cookie']) {
          if (!requestOptions.headers) {
            requestOptions.headers = {}
          }
          requestOptions.headers.cookie = response.headers['set-cookie']
        }

        requestOptions.path = response.headers.location
        httpGet(description, currentRedirects + 1, requestOptions, verbosity,
          responseCallback)
        response.resume()
        return
      }
    }
    response.on('data', function (chunk) {
      responseBody += chunk
    })
    response.on('end', function () {
      if (response.statusCode >= 200 && response.statusCode <= 299) {
        responseCallback(null, responseBody)
      } else {
        responseCallback(provisionUtil.createFormattedDxlError(
            'Request to ' + requestUrl +
            ' failed with HTTP error code: ' + response.statusCode +
            '. Reason: ' + response.statusMessage + '.', description))
      }
    })
  })
  request.on('error', function (error) {
    responseCallback(provisionUtil.createFormattedDxlError(
        'Error processing request to ' + requestUrl + ': ' + error,
        description))
  })
}
function httpGet (description, currentRedirects, requestOptions, verbosity,
                  responseCallback) {
  var requestUrl = toUrlString(requestOptions)
  var responseBody = ''

  if (verbosity > 1) {
    console.log('HTTP request to: ' + requestUrl)
  }

  var request = https.get(requestOptions, function (response) {
    if (verbosity > 1) {
      console.log('HTTP response status code: ' + response.statusCode)
    }
    if (response.statusCode === 302) {
      if (currentRedirects === HTTP_MAX_REDIRECTS) {
        responseCallback(provisionUtil.createFormattedDxlError(
          'Reached maximum redirects for request to ' + requestUrl, description)
        )
      } else {
        var redirectLocation = response.headers.location
        if (!redirectLocation) {
          responseCallback(provisionUtil.createFormattedDxlError(
            'Redirect with no location specified for ' +
            requestUrl, description))
          return
        }
        if (verbosity > 1) {
          console.log('HTTP response redirect to: ' + redirectLocation)
        }

        var parsedRedirectLocation = url.parse(redirectLocation)
        if ((parsedRedirectLocation.hostname &&
            (parsedRedirectLocation.hostname !== requestOptions.hostname)) ||
          (parsedRedirectLocation.port &&
            (parsedRedirectLocation.port !== requestOptions.port))) {
          responseCallback(provisionUtil.createFormattedDxlError(
              'Redirect to different host or port not supported. ' +
              'Original URL: ' + requestUrl +
              '. Redirect URL: ' + redirectLocation + '.', description))
          return
        }

        if (response.headers['set-cookie']) {
          if (!requestOptions.headers) {
            requestOptions.headers = {}
          }
          requestOptions.headers.cookie = response.headers['set-cookie']
        }

        requestOptions.path = response.headers.location
        httpGet(description, currentRedirects + 1, requestOptions, verbosity,
          responseCallback)
        response.resume()
        return
      }
    }
    response.on('data', function (chunk) {
      responseBody += chunk
    })
    response.on('end', function () {
      if (response.statusCode >= 200 && response.statusCode <= 299) {
        responseCallback(null, responseBody)
      } else {
        responseCallback(provisionUtil.createFormattedDxlError(
            'Request to ' + requestUrl +
            ' failed with HTTP error code: ' + response.statusCode +
            '. Reason: ' + response.statusMessage + '.', description))
      }
    })
  })
  request.on('error', function (error) {
    responseCallback(provisionUtil.createFormattedDxlError(
        'Error processing request to ' + requestUrl + ': ' + error,
        description))
  })
}
function httpGet ( description , currentRedirects , requestOptions , verbosity , responseCallback ) { var requestUrl = toUrlString ( requestOptions ) var responseBody = '' if ( verbosity > 1 ) { console . log ( 'HTTP request to: ' + requestUrl ) } var request = https . get ( requestOptions , function ( response ) { if ( verbosity > 1 ) { console . log ( 'HTTP response status code: ' + response . statusCode ) } if ( response . statusCode === 302 ) { if ( currentRedirects === HTTP_MAX_REDIRECTS ) { responseCallback ( provisionUtil . createFormattedDxlError ( 'Reached maximum redirects for request to ' + requestUrl , description ) ) } else { var redirectLocation = response . headers . location if ( ! redirectLocation ) { responseCallback ( provisionUtil . createFormattedDxlError ( 'Redirect with no location specified for ' + requestUrl , description ) ) return } if ( verbosity > 1 ) { console . log ( 'HTTP response redirect to: ' + redirectLocation ) } var parsedRedirectLocation = url . parse ( redirectLocation ) if ( ( parsedRedirectLocation . hostname && ( parsedRedirectLocation . hostname !== requestOptions . hostname ) ) || ( parsedRedirectLocation . port && ( parsedRedirectLocation . port !== requestOptions . port ) ) ) { responseCallback ( provisionUtil . createFormattedDxlError ( 'Redirect to different host or port not supported. ' + 'Original URL: ' + requestUrl + '. Redirect URL: ' + redirectLocation + '.' , description ) ) return } if ( response . headers [ 'set-cookie' ] ) { if ( ! requestOptions . headers ) { requestOptions . headers = { } } requestOptions . headers . cookie = response . headers [ 'set-cookie' ] } requestOptions . path = response . headers . location httpGet ( description , currentRedirects + 1 , requestOptions , verbosity , responseCallback ) response . resume ( ) return } } response . on ( 'data' , function ( chunk ) { responseBody += chunk } ) response . on ( 'end' , function ( ) { if ( response . statusCode >= 200 && response . statusCode <= 299 ) { responseCallback ( null , responseBody ) } else { responseCallback ( provisionUtil . createFormattedDxlError ( 'Request to ' + requestUrl + ' failed with HTTP error code: ' + response . statusCode + '. Reason: ' + response . statusMessage + '.' , description ) ) } } ) } ) request . on ( 'error' , function ( error ) { responseCallback ( provisionUtil . createFormattedDxlError ( 'Error processing request to ' + requestUrl + ': ' + error , description ) ) } ) }
Performs an HTTP get to the management service. The response received from
the service is supplied as an argument to the responseCallback function.
@param {String} description - Textual description of the HTTP request (used
in log and error messages).
@param {Number} currentRedirects - Number of HTTP redirects which have been
followed thus far for this request.
@param {Object} requestOptions - Request options to pass along to the
underlying HTTP request library.
@param {Number} verbosity - Level of verbosity at which to log any error
or trace messages.
@param {Function} responseCallback - Function to invoke with the response
body received from the management service. If an error occurs during
request processing, the first parameter delivered to the callback
includes the associated `Error` instance. If the request is successful,
the response body is provided in the second parameter (as an object
parsed from the response JSON).
@private
Performs an HTTP get to the management service . The response received from the service is supplied as an argument to the responseCallback function .