http-proxy-agent.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Module dependencies.
  3. */
  4. var net = require('net');
  5. var tls = require('tls');
  6. var url = require('url');
  7. var extend = require('extend');
  8. var Agent = require('agent-base');
  9. var inherits = require('util').inherits;
  10. var debug = require('debug')('http-proxy-agent');
  11. /**
  12. * Module exports.
  13. */
  14. module.exports = HttpProxyAgent;
  15. /**
  16. * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the
  17. * specified "HTTP proxy server" in order to proxy HTTP requests.
  18. *
  19. * @api public
  20. */
  21. function HttpProxyAgent (opts) {
  22. if (!(this instanceof HttpProxyAgent)) return new HttpProxyAgent(opts);
  23. if ('string' == typeof opts) opts = url.parse(opts);
  24. if (!opts) throw new Error('an HTTP(S) proxy server `host` and `port` must be specified!');
  25. debug('creating new HttpProxyAgent instance: %o', opts);
  26. Agent.call(this, connect);
  27. var proxy = extend({}, opts);
  28. // if `true`, then connect to the proxy server over TLS. defaults to `false`.
  29. this.secureProxy = proxy.protocol ? /^https:?$/i.test(proxy.protocol) : false;
  30. // prefer `hostname` over `host`, and set the `port` if needed
  31. proxy.host = proxy.hostname || proxy.host;
  32. proxy.port = +proxy.port || (this.secureProxy ? 443 : 80);
  33. if (proxy.host && proxy.path) {
  34. // if both a `host` and `path` are specified then it's most likely the
  35. // result of a `url.parse()` call... we need to remove the `path` portion so
  36. // that `net.connect()` doesn't attempt to open that as a unix socket file.
  37. delete proxy.path;
  38. delete proxy.pathname;
  39. }
  40. this.proxy = proxy;
  41. }
  42. inherits(HttpProxyAgent, Agent);
  43. /**
  44. * Called when the node-core HTTP client library is creating a new HTTP request.
  45. *
  46. * @api public
  47. */
  48. function connect (req, opts, fn) {
  49. var proxy = this.proxy;
  50. // change the `http.ClientRequest` instance's "path" field
  51. // to the absolute path of the URL that will be requested
  52. var parsed = url.parse(req.path);
  53. if (null == parsed.protocol) parsed.protocol = 'http:';
  54. if (null == parsed.hostname) parsed.hostname = opts.hostname || opts.host;
  55. if (null == parsed.port) parsed.port = opts.port;
  56. if (parsed.port == 80) {
  57. // if port is 80, then we can remove the port so that the
  58. // ":80" portion is not on the produced URL
  59. delete parsed.port;
  60. }
  61. var absolute = url.format(parsed);
  62. req.path = absolute;
  63. // inject the `Proxy-Authorization` header if necessary
  64. var auth = proxy.auth;
  65. if (auth) {
  66. req.setHeader('Proxy-Authorization', 'Basic ' + new Buffer(auth).toString('base64'));
  67. }
  68. // create a socket connection to the proxy server
  69. var socket;
  70. if (this.secureProxy) {
  71. socket = tls.connect(proxy);
  72. } else {
  73. socket = net.connect(proxy);
  74. }
  75. // at this point, the http ClientRequest's internal `_header` field might have
  76. // already been set. If this is the case then we'll need to re-generate the
  77. // string since we just changed the `req.path`
  78. if (req._header) {
  79. debug('regenerating stored HTTP header string for request');
  80. req._header = null;
  81. req._implicitHeader();
  82. if (req.output && req.output.length > 0) {
  83. debug('patching connection write() output buffer with updated header');
  84. // the _header has already been queued to be written to the socket
  85. var first = req.output[0];
  86. var endOfHeaders = first.indexOf('\r\n\r\n') + 4;
  87. req.output[0] = req._header + first.substring(endOfHeaders);
  88. debug('output buffer: %o', req.output);
  89. }
  90. }
  91. fn(null, socket);
  92. };