123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var os = require('os');
  2. var http = require('http');
  3. var https = require('https');
  4. var owns = {}.hasOwnProperty;
  5. module.exports = function proxyMiddleware(options) {
  6. //enable ability to quickly pass a url for shorthand setup
  7. if(typeof options === 'string'){
  8. options = require('url').parse(options);
  9. }
  10. var httpLib = options.protocol === 'https:' ? https : http;
  11. var request = httpLib.request;
  12. options = options || {};
  13. options.hostname = options.hostname;
  14. options.port = options.port;
  15. options.pathname = options.pathname || '/';
  16. return function (req, resp, next) {
  17. var url = req.url;
  18. // You can pass the route within the options, as well
  19. if (typeof options.route === 'string') {
  20. if (url === options.route) {
  21. url = '';
  22. } else if (url.slice(0, options.route.length) === options.route) {
  23. url = url.slice(options.route.length);
  24. } else {
  25. return next();
  26. }
  27. }
  28. //options for this request
  29. var opts = extend({}, options);
  30. if (url && url.charAt(0) === '?') { // prevent /api/resource/?offset=0
  31. if (options.pathname.length > 1 && options.pathname.charAt(options.pathname.length - 1) === '/') {
  32. opts.path = options.pathname.substring(0, options.pathname.length - 1) + url;
  33. } else {
  34. opts.path = options.pathname + url;
  35. }
  36. } else if (url) {
  37. opts.path = slashJoin(options.pathname, url);
  38. } else {
  39. opts.path = options.pathname;
  40. }
  41. opts.method = req.method;
  42. opts.headers = options.headers ? merge(req.headers, options.headers) : req.headers;
  43. applyViaHeader(req.headers, opts, opts.headers);
  44. if (!options.preserveHost) {
  45. // Forwarding the host breaks dotcloud
  46. delete opts.headers.host;
  47. }
  48. var myReq = request(opts, function (myRes) {
  49. var statusCode = myRes.statusCode
  50. , headers = myRes.headers
  51. , location = headers.location;
  52. // Fix the location
  53. if (((statusCode > 300 && statusCode < 304) || statusCode === 201) && location && location.indexOf(options.href) > -1) {
  54. // absoulte path
  55. headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), '')));
  56. }
  57. applyViaHeader(myRes.headers, opts, myRes.headers);
  58. rewriteCookieHosts(myRes.headers, opts, myRes.headers, req);
  59. resp.writeHead(myRes.statusCode, myRes.headers);
  60. myRes.on('error', function (err) {
  61. next(err);
  62. });
  63. myRes.pipe(resp);
  64. });
  65. myReq.on('error', function (err) {
  66. next(err);
  67. });
  68. if (!req.readable) {
  69. myReq.end();
  70. } else {
  71. req.pipe(myReq);
  72. }
  73. };
  74. };
  75. function applyViaHeader(existingHeaders, opts, applyTo) {
  76. if (!opts.via) return;
  77. var viaName = (true === opts.via) ? os.hostname() : opts.via;
  78. var viaHeader = '1.1 ' + viaName;
  79. if(existingHeaders.via) {
  80. viaHeader = existingHeaders.via + ', ' + viaHeader;
  81. }
  82. applyTo.via = viaHeader;
  83. }
  84. function rewriteCookieHosts(existingHeaders, opts, applyTo, req) {
  85. if (!opts.cookieRewrite || !owns.call(existingHeaders, 'set-cookie')) {
  86. return;
  87. }
  88. var existingCookies = existingHeaders['set-cookie'],
  89. rewrittenCookies = [],
  90. rewriteHostname = (true === opts.cookieRewrite) ? os.hostname() : opts.cookieRewrite;
  91. if (!Array.isArray(existingCookies)) {
  92. existingCookies = [ existingCookies ];
  93. }
  94. for (var i = 0; i < existingCookies.length; i++) {
  95. var rewrittenCookie = existingCookies[i].replace(/(Domain)=[a-z\.-_]*?(;|$)/gi, '$1=' + rewriteHostname + '$2');
  96. if (!req.connection.encrypted) {
  97. rewrittenCookie = rewrittenCookie.replace(/;\s*?(Secure)/i, '');
  98. }
  99. rewrittenCookies.push(rewrittenCookie);
  100. }
  101. applyTo['set-cookie'] = rewrittenCookies;
  102. }
  103. function slashJoin(p1, p2) {
  104. var trailing_slash = false;
  105. if (p1.length && p1[p1.length - 1] === '/') { trailing_slash = true; }
  106. if (trailing_slash && p2.length && p2[0] === '/') {p2 = p2.substring(1); }
  107. return p1 + p2;
  108. }
  109. function extend(obj, src) {
  110. for (var key in src) if (owns.call(src, key)) obj[key] = src[key];
  111. return obj;
  112. }
  113. //merges data without changing state in either argument
  114. function merge(src1, src2) {
  115. var merged = {};
  116. extend(merged, src1);
  117. extend(merged, src2);
  118. return merged;
  119. }