You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.9 KiB

11 years ago
'use strict';
var urlLib = require('url');
var http = require('http');
var https = require('https');
var zlib = require('zlib');
var assign = require('object-assign');
11 years ago
module.exports = function (url, opts, cb) {
11 years ago
var redirectCount = 0;
// Extract got options.
var encoding = opts.encoding;
delete opts.encoding;
var get = function (url, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
11 years ago
cb = cb || function () {};
opts = opts || {};
opts.headers = assign({
'user-agent': 'https://github.com/sindresorhus/got',
'accept-encoding': 'gzip,deflate'
}, opts.headers || {});
var parsedUrl = urlLib.parse(url);
var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts);
fn.get(arg, function (res) {
11 years ago
// redirect
if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) {
res.destroy();
11 years ago
if (++redirectCount > 10) {
cb(new Error('Redirected 10 times. Aborting.'));
return;
}
get(urlLib.resolve(url, res.headers.location), opts, cb);
11 years ago
return;
}
if (res.statusCode < 200 || res.statusCode > 299) {
res.destroy();
11 years ago
cb(res.statusCode);
return;
}
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
var unzip = zlib.createUnzip();
res.pipe(unzip);
res = unzip;
}
res.once('error', cb);
11 years ago
var chunks = [];
var n = 0;
11 years ago
res.on('data', function (chunk) {
// Add the new chunk to the list.
chunks.push(chunk);
n += chunk.length;
});
res.once('end', function () {
// Concatenate all chunks into a single buffer.
var data = Buffer.concat(chunks, n);
// Unless the encoding has been explicitely set to `null`,
// convert the buffer to a string.
if (encoding !== null) {
data = data.toString(encoding || 'utf8');
}
// Return the result.
cb(null, data, res);
11 years ago
});
}).once('error', cb);
11 years ago
};
get(url, opts, cb);
11 years ago
};