handleRequest: function handleRequest()

in lib/http/node.js [11:63]


  handleRequest: function handleRequest(httpRequest, httpOptions, callback, errCallback) {
    var endpoint = httpRequest.endpoint;
    var pathPrefix = '';
    if (!httpOptions) httpOptions = {};

    var useSSL = endpoint.protocol === 'https:';
    var http = useSSL ? require('https') : require('http');
    if (httpOptions.maxSockets) {
      http.globalAgent.maxSockets = httpOptions.maxSockets;
    } else {
      http.globalAgent.maxSockets = 300;
    }
    var options = {
      host: endpoint.hostname,
      port: endpoint.port,
      method: httpRequest.method,
      headers: httpRequest.headers,
      path: pathPrefix + httpRequest.path
    };

    if (useSSL && !httpOptions.agent) {
      options.agent = this.sslAgent();
    }

    TableStore.util.update(options, httpOptions);
    delete options.proxy; // proxy isn't an HTTP option
    delete options.timeout; // timeout isn't an HTTP option
    delete options.maxSockets; // maxSockets isn't an HTTP option

    var stream = http.request(options, function (httpResp) {
      callback(httpResp);
      httpResp.emit('headers', httpResp.statusCode, httpResp.headers);
    });
    httpRequest.stream = stream; // attach stream to httpRequest

    // timeout support
    stream.setTimeout(httpOptions.timeout || 0);
    stream.once('timeout', function () {
      var msg = 'Connection timed out after ' + httpOptions.timeout + 'ms';
      errCallback(TableStore.util.error(new Error(msg), { code: 'TimeoutError' }));

      // HACK - abort the connection without tripping our error handler
      // since we already raised our TimeoutError. Otherwise the connection
      // comes back with ECONNRESET, which is not a helpful error message
      stream.removeListener('error', errCallback);
      stream.on('error', function () { });
      stream.abort();
    });

    stream.on('error', errCallback);
    this.writeBody(stream, httpRequest);
    return stream;
  },