|
|
@ -1,31 +1,47 @@ |
|
|
|
'use strict'; |
|
|
|
var urlLib = require('url'); |
|
|
|
|
|
|
|
var http = require('http'); |
|
|
|
var https = require('https'); |
|
|
|
var urlLib = require('url'); |
|
|
|
var zlib = require('zlib'); |
|
|
|
var PassThrough = require('stream').PassThrough; |
|
|
|
|
|
|
|
var assign = require('object-assign'); |
|
|
|
|
|
|
|
module.exports = function (url, opts, cb) { |
|
|
|
var redirectCount = 0; |
|
|
|
if (typeof opts === 'function') { |
|
|
|
// If `cb` has been specified but `opts` has not.
|
|
|
|
cb = opts; |
|
|
|
opts = {}; |
|
|
|
} else if (!opts) { |
|
|
|
// opts has not been specified.
|
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
|
|
|
|
// extract own options
|
|
|
|
var encoding = opts.encoding; |
|
|
|
delete opts.encoding; |
|
|
|
|
|
|
|
var get = function (url, opts, cb) { |
|
|
|
if (typeof opts === 'function') { |
|
|
|
cb = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
// If no callback has been provided, returns a proxy stream to the
|
|
|
|
// response.
|
|
|
|
var proxy; |
|
|
|
if (!cb) { |
|
|
|
proxy = new PassThrough(); |
|
|
|
|
|
|
|
cb = cb || function () {}; |
|
|
|
opts = opts || {}; |
|
|
|
// Forward errors on the stream.
|
|
|
|
cb = function (err) { |
|
|
|
proxy.emit('error', err); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// Merge additional headers.
|
|
|
|
opts.headers = assign({ |
|
|
|
'user-agent': 'https://github.com/sindresorhus/got', |
|
|
|
'accept-encoding': 'gzip,deflate' |
|
|
|
}, opts.headers || {}); |
|
|
|
|
|
|
|
var redirectCount = 0; |
|
|
|
var get = function (url, opts, cb) { |
|
|
|
var parsedUrl = urlLib.parse(url); |
|
|
|
var fn = parsedUrl.protocol === 'https:' ? https : http; |
|
|
|
var arg = assign({}, parsedUrl, opts); |
|
|
@ -56,6 +72,12 @@ module.exports = function (url, opts, cb) { |
|
|
|
res = unzip; |
|
|
|
} |
|
|
|
|
|
|
|
// If in proxy mode, simply pipe the response to the proxy.
|
|
|
|
if (proxy) { |
|
|
|
res.pipe(proxy); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
res.once('error', cb); |
|
|
|
|
|
|
|
var chunks = []; |
|
|
@ -79,4 +101,5 @@ module.exports = function (url, opts, cb) { |
|
|
|
}; |
|
|
|
|
|
|
|
get(url, opts, cb); |
|
|
|
return proxy; |
|
|
|
}; |
|
|
|