agent.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Module dependencies.
  3. */
  4. require('./patch-core');
  5. var extend = require('extend');
  6. var inherits = require('util').inherits;
  7. var EventEmitter = require('events').EventEmitter;
  8. /**
  9. * Module exports.
  10. */
  11. module.exports = Agent;
  12. /**
  13. * Base `http.Agent` implementation.
  14. * No pooling/keep-alive is implemented by default.
  15. *
  16. * @param {Function} callback
  17. * @api public
  18. */
  19. function Agent (callback) {
  20. if (!(this instanceof Agent)) return new Agent(callback);
  21. if ('function' != typeof callback) throw new Error('Must pass a "callback function"');
  22. EventEmitter.call(this);
  23. this.callback = callback;
  24. }
  25. inherits(Agent, EventEmitter);
  26. /**
  27. * Called by node-core's "_http_client.js" module when creating
  28. * a new HTTP request with this Agent instance.
  29. *
  30. * @api public
  31. */
  32. Agent.prototype.addRequest = function (req, host, port, localAddress) {
  33. var opts;
  34. if ('object' == typeof host) {
  35. // >= v0.11.x API
  36. opts = extend({}, req._options, host);
  37. } else {
  38. // <= v0.10.x API
  39. opts = extend({}, req._options, { host: host, port: port });
  40. if (null != localAddress) {
  41. opts.localAddress = localAddress;
  42. }
  43. }
  44. if (opts.host && opts.path) {
  45. // if both a `host` and `path` are specified then it's most likely the
  46. // result of a `url.parse()` call... we need to remove the `path` portion so
  47. // that `net.connect()` doesn't attempt to open that as a unix socket file.
  48. delete opts.path;
  49. }
  50. // set default `port` if none was explicitly specified
  51. if (null == opts.port) {
  52. opts.port = opts.secureEndpoint ? 443 : 80;
  53. }
  54. delete opts.agent;
  55. delete opts.hostname;
  56. delete opts._defaultAgent;
  57. delete opts.defaultPort;
  58. delete opts.createConnection;
  59. // hint to use "Connection: close"
  60. // XXX: non-documented `http` module API :(
  61. req._last = true;
  62. req.shouldKeepAlive = false;
  63. // clean up a bit of memory since we're no longer using this
  64. req._options = null;
  65. // create the `net.Socket` instance
  66. var sync = true;
  67. this.callback(req, opts, function (err, socket) {
  68. function emitErr () {
  69. req.emit('error', err);
  70. // For Safety. Some additional errors might fire later on
  71. // and we need to make sure we don't double-fire the error event.
  72. req._hadError = true;
  73. }
  74. if (err) {
  75. if (sync) {
  76. // need to defer the "error" event, when sync, because by now the `req`
  77. // instance hasn't event been passed back to the user yet...
  78. process.nextTick(emitErr);
  79. } else {
  80. emitErr();
  81. }
  82. } else {
  83. req.onSocket(socket);
  84. }
  85. });
  86. sync = false;
  87. };