common.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var common = exports,
  2. url = require('url'),
  3. extend = require('util')._extend,
  4. required = require('requires-port');
  5. var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
  6. isSSL = /^https|wss/;
  7. /**
  8. * Simple Regex for testing if protocol is https
  9. */
  10. common.isSSL = isSSL;
  11. /**
  12. * Copies the right headers from `options` and `req` to
  13. * `outgoing` which is then used to fire the proxied
  14. * request.
  15. *
  16. * Examples:
  17. *
  18. * common.setupOutgoing(outgoing, options, req)
  19. * // => { host: ..., hostname: ...}
  20. *
  21. * @param {Object} Outgoing Base object to be filled with required properties
  22. * @param {Object} Options Config object passed to the proxy
  23. * @param {ClientRequest} Req Request Object
  24. * @param {String} Forward String to select forward or target
  25. * @return {Object} Outgoing Object with all required properties set
  26. *
  27. * @api private
  28. */
  29. common.setupOutgoing = function(outgoing, options, req, forward) {
  30. outgoing.port = options[forward || 'target'].port ||
  31. (isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
  32. ['host', 'hostname', 'socketPath', 'pfx', 'key',
  33. 'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
  34. function(e) { outgoing[e] = options[forward || 'target'][e]; }
  35. );
  36. outgoing.method = options.method || req.method;
  37. outgoing.headers = extend({}, req.headers);
  38. if (options.headers){
  39. extend(outgoing.headers, options.headers);
  40. }
  41. if (options.auth) {
  42. outgoing.auth = options.auth;
  43. }
  44. if (options.ca) {
  45. outgoing.ca = options.ca;
  46. }
  47. if (isSSL.test(options[forward || 'target'].protocol)) {
  48. outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
  49. }
  50. outgoing.agent = options.agent || false;
  51. outgoing.localAddress = options.localAddress;
  52. //
  53. // Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
  54. // as node core doesn't handle this COMPLETELY properly yet.
  55. //
  56. if (!outgoing.agent) {
  57. outgoing.headers = outgoing.headers || {};
  58. if (typeof outgoing.headers.connection !== 'string'
  59. || !upgradeHeader.test(outgoing.headers.connection)
  60. ) { outgoing.headers.connection = 'close'; }
  61. }
  62. // the final path is target path + relative path requested by user:
  63. var target = options[forward || 'target'];
  64. var targetPath = target && options.prependPath !== false
  65. ? (target.path || '')
  66. : '';
  67. //
  68. // Remark: Can we somehow not use url.parse as a perf optimization?
  69. //
  70. var outgoingPath = !options.toProxy
  71. ? (url.parse(req.url).path || '')
  72. : req.url;
  73. //
  74. // Remark: ignorePath will just straight up ignore whatever the request's
  75. // path is. This can be labeled as FOOT-GUN material if you do not know what
  76. // you are doing and are using conflicting options.
  77. //
  78. outgoingPath = !options.ignorePath ? outgoingPath : '';
  79. outgoing.path = common.urlJoin(targetPath, outgoingPath);
  80. if (options.changeOrigin) {
  81. outgoing.headers.host =
  82. required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
  83. ? outgoing.host + ':' + outgoing.port
  84. : outgoing.host;
  85. }
  86. return outgoing;
  87. };
  88. /**
  89. * Set the proper configuration for sockets,
  90. * set no delay and set keep alive, also set
  91. * the timeout to 0.
  92. *
  93. * Examples:
  94. *
  95. * common.setupSocket(socket)
  96. * // => Socket
  97. *
  98. * @param {Socket} Socket instance to setup
  99. * @return {Socket} Return the configured socket.
  100. *
  101. * @api private
  102. */
  103. common.setupSocket = function(socket) {
  104. socket.setTimeout(0);
  105. socket.setNoDelay(true);
  106. socket.setKeepAlive(true, 0);
  107. return socket;
  108. };
  109. /**
  110. * Get the port number from the host. Or guess it based on the connection type.
  111. *
  112. * @param {Request} req Incoming HTTP request.
  113. *
  114. * @return {String} The port number.
  115. *
  116. * @api private
  117. */
  118. common.getPort = function(req) {
  119. var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
  120. return res ?
  121. res[1] :
  122. common.hasEncryptedConnection(req) ? '443' : '80';
  123. };
  124. /**
  125. * Check if the request has an encrypted connection.
  126. *
  127. * @param {Request} req Incoming HTTP request.
  128. *
  129. * @return {Boolean} Whether the connection is encrypted or not.
  130. *
  131. * @api private
  132. */
  133. common.hasEncryptedConnection = function(req) {
  134. return Boolean(req.connection.encrypted || req.connection.pair);
  135. };
  136. /**
  137. * OS-agnostic join (doesn't break on URLs like path.join does on Windows)>
  138. *
  139. * @return {String} The generated path.
  140. *
  141. * @api private
  142. */
  143. common.urlJoin = function() {
  144. //
  145. // We do not want to mess with the query string. All we want to touch is the path.
  146. //
  147. var args = Array.prototype.slice.call(arguments),
  148. lastIndex = args.length - 1,
  149. last = args[lastIndex],
  150. lastSegs = last.split('?'),
  151. retSegs;
  152. args[lastIndex] = lastSegs.shift();
  153. //
  154. // Join all strings, but remove empty strings so we don't get extra slashes from
  155. // joining e.g. ['', 'am']
  156. //
  157. retSegs = [
  158. args.filter(Boolean).join('/')
  159. .replace(/\/+/g, '/')
  160. .replace('http:/', 'http://')
  161. .replace('https:/', 'https://')
  162. ];
  163. // Only join the query string if it exists so we don't have trailing a '?'
  164. // on every request
  165. // Handle case where there could be multiple ? in the URL.
  166. retSegs.push.apply(retSegs, lastSegs);
  167. return retSegs.join('?')
  168. };
  169. /**
  170. * Rewrites or removes the domain of a cookie header
  171. *
  172. * @param {String|Array} Header
  173. * @param {Object} Config, mapping of domain to rewritten domain.
  174. * '*' key to match any domain, null value to remove the domain.
  175. *
  176. * @api private
  177. */
  178. common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
  179. if (Array.isArray(header)) {
  180. return header.map(function (headerElement) {
  181. return rewriteCookieProperty(headerElement, config, property);
  182. });
  183. }
  184. return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
  185. var newValue;
  186. if (previousValue in config) {
  187. newValue = config[previousValue];
  188. } else if ('*' in config) {
  189. newValue = config['*'];
  190. } else {
  191. //no match, return previous value
  192. return match;
  193. }
  194. if (newValue) {
  195. //replace value
  196. return prefix + newValue;
  197. } else {
  198. //remove value
  199. return '';
  200. }
  201. });
  202. };
  203. /**
  204. * Check the host and see if it potentially has a port in it (keep it simple)
  205. *
  206. * @returns {Boolean} Whether we have one or not
  207. *
  208. * @api private
  209. */
  210. function hasPort(host) {
  211. return !!~host.indexOf(':');
  212. };