|
| 1 | +// substitute underscore (dcode) |
| 2 | +var _ = {}; _.extend = function(obj) { |
| 3 | + Array.prototype.slice.call(arguments, 1).forEach(function(source) { |
| 4 | + if (source) |
| 5 | + for (var prop in source) { |
| 6 | + obj[prop] = source[prop]; |
| 7 | + } |
| 8 | + }); |
| 9 | + return obj; |
| 10 | +}; |
| 11 | + |
| 12 | +/* |
| 13 | + follow-redirects Copyright © 2014 Olivier Lalonde <[email protected]> |
| 14 | +
|
| 15 | + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 16 | + ocumentation files (the “Software”), to deal in the Software without restriction, including without limitation the |
| 17 | + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 18 | + persons to whom the Software is furnished to do so, subject to the following conditions: |
| 19 | +
|
| 20 | + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 21 | +
|
| 22 | + THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 23 | + WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 24 | + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 25 | + OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + */ |
| 27 | +// ref: https://github.com/olalonde/follow-redirects |
| 28 | +var nativeHttps = require('https'), |
| 29 | + nativeHttp = require('http'), |
| 30 | + url = require('url'); |
| 31 | + |
| 32 | +var maxRedirects = module.exports.maxRedirects = 5; |
| 33 | + |
| 34 | +var protocols = { |
| 35 | + https: nativeHttps, |
| 36 | + http: nativeHttp |
| 37 | +}; |
| 38 | + |
| 39 | +// Only use GETs on redirects |
| 40 | +for (var protocol in protocols) { |
| 41 | + // h is either our cloned http or https object |
| 42 | + var h = function() {}; |
| 43 | + h.prototype = protocols[protocol]; |
| 44 | + h = new h(); |
| 45 | + |
| 46 | + module.exports[protocol] = h; |
| 47 | + |
| 48 | + h.request = function (h) { |
| 49 | + return function (options, callback, redirectOptions) { |
| 50 | + |
| 51 | + redirectOptions = redirectOptions || {}; |
| 52 | + |
| 53 | + var max = (typeof options === 'object' && 'maxRedirects' in options) ? options.maxRedirects : exports.maxRedirects; |
| 54 | + |
| 55 | + var redirect = _.extend({ |
| 56 | + count: 0, |
| 57 | + max: max, |
| 58 | + clientRequest: null, |
| 59 | + userCallback: callback |
| 60 | + }, redirectOptions); |
| 61 | + |
| 62 | + /** |
| 63 | + * Emit error if too many redirects |
| 64 | + */ |
| 65 | + if (redirect.count > redirect.max) { |
| 66 | + var err = new Error('Max redirects exceeded. To allow more redirects, pass options.maxRedirects property.'); |
| 67 | + redirect.clientRequest.emit('error', err); |
| 68 | + return redirect.clientRequest; |
| 69 | + } |
| 70 | + |
| 71 | + redirect.count++; |
| 72 | + |
| 73 | + /** |
| 74 | + * Parse URL from options |
| 75 | + */ |
| 76 | + var reqUrl; |
| 77 | + if (typeof options === 'string') { |
| 78 | + reqUrl = options; |
| 79 | + } |
| 80 | + else { |
| 81 | + reqUrl = url.format(_.extend({ protocol: protocol }, options)); |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + * Build client request |
| 86 | + */ |
| 87 | + var clientRequest = h.__proto__.request(options, redirectCallback(reqUrl, redirect)); |
| 88 | + |
| 89 | + // Save user's clientRequest so we can emit errors later |
| 90 | + if (!redirect.clientRequest) redirect.clientRequest = clientRequest; |
| 91 | + |
| 92 | + /** |
| 93 | + * ClientRequest callback for redirects |
| 94 | + */ |
| 95 | + function redirectCallback (reqUrl, redirect) { |
| 96 | + return function (res) { |
| 97 | + // status must be 300-399 for redirects |
| 98 | + if (res.statusCode < 300 || res.statusCode > 399) { |
| 99 | + return redirect.userCallback(res); |
| 100 | + } |
| 101 | + |
| 102 | + // no `Location:` header => nowhere to redirect |
| 103 | + if (!('location' in res.headers)) { |
| 104 | + return redirect.userCallback(res); |
| 105 | + } |
| 106 | + |
| 107 | + // we are going to follow the redirect, but in node 0.10 we must first attach a data listener |
| 108 | + // to consume the stream and send the 'end' event |
| 109 | + res.on('data', function() {}); |
| 110 | + |
| 111 | + // save the original clientRequest to our redirectOptions so we can emit errors later |
| 112 | + |
| 113 | + // need to use url.resolve() in case location is a relative URL |
| 114 | + var redirectUrl = url.resolve(reqUrl, res.headers['location']); |
| 115 | + // we need to call the right api (http vs https) depending on protocol |
| 116 | + var proto = url.parse(redirectUrl).protocol; |
| 117 | + proto = proto.substr(0, proto.length - 1); |
| 118 | + |
| 119 | + // Make a new option object for next request from old options object |
| 120 | + // Break url in parts |
| 121 | + var searchname = url.parse(redirectUrl).search; |
| 122 | + var hostname = url.parse(redirectUrl).hostname; |
| 123 | + var pathname = url.parse(redirectUrl).pathname; |
| 124 | + |
| 125 | + var redirectOptions = options; |
| 126 | + redirectOptions.reqUrl = redirectUrl; |
| 127 | + redirectOptions.hostname = hostname; |
| 128 | + redirectOptions.path = pathname + searchname; |
| 129 | + |
| 130 | + var out = module.exports[proto].get(redirectOptions, redirectCallback(reqUrl, redirect), redirect); |
| 131 | + |
| 132 | + // bubble errors that occur on the redirect back up to the initiating client request |
| 133 | + // object, otherwise they wind up killing the process. |
| 134 | + out.on("error", function(err) { clientRequest.emit("error", err) }); |
| 135 | + |
| 136 | + return out; |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + return clientRequest; |
| 141 | + } |
| 142 | + }(h); |
| 143 | + |
| 144 | + // see https://github.com/joyent/node/blob/master/lib/http.js#L1623 |
| 145 | + h.get = function (h) { |
| 146 | + return function (options, cb, redirectOptions) { |
| 147 | + var req = h.request(options, cb, redirectOptions); |
| 148 | + req.end(); |
| 149 | + return req; |
| 150 | + }; |
| 151 | + }(h); |
| 152 | +} |
0 commit comments