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.

195 lines
4.2 KiB

11 years ago
'use strict';
10 years ago
var http = require('http');
var https = require('https');
var urlLib = require('url');
var util = require('util');
10 years ago
var zlib = require('zlib');
var objectAssign = require('object-assign');
var agent = require('infinity-agent');
var duplexify = require('duplexify');
10 years ago
var isStream = require('is-stream');
var read = require('read-all-stream');
var timeout = require('timed-out');
var prependHttp = require('prepend-http');
var lowercaseKeys = require('lowercase-keys');
var status = require('statuses');
var NestedError = require('nested-error-stacks');
function GotError(message, nested) {
NestedError.call(this, message, nested);
objectAssign(this, nested);
}
util.inherits(GotError, NestedError);
GotError.prototype.name = 'GotError';
function got(url, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
} else if (!opts) {
opts = {};
}
11 years ago
opts = objectAssign({}, opts);
opts.headers = objectAssign({
'user-agent': 'https://github.com/sindresorhus/got',
'accept-encoding': 'gzip,deflate'
}, lowercaseKeys(opts.headers));
var encoding = opts.encoding;
var body = opts.body;
10 years ago
var json = opts.json;
10 years ago
var proxy;
var redirectCount = 0;
delete opts.encoding;
delete opts.body;
10 years ago
delete opts.json;
if (body) {
opts.method = opts.method || 'POST';
}
// returns a proxy stream to the response
// if no callback has been provided
if (!cb) {
proxy = duplexify();
11 years ago
// forward errors on the stream
cb = function (err) {
proxy.emit('error', err);
};
}
10 years ago
if (proxy && json) {
throw new GotError('got can not be used as stream when options.json is used');
}
10 years ago
function get(url, opts, cb) {
var parsedUrl = urlLib.parse(prependHttp(url));
var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = objectAssign({}, parsedUrl, opts);
// TODO: remove this when Node 0.10 will be deprecated
if (arg.agent === undefined) {
arg.agent = agent(arg);
}
var req = fn.request(arg, function (response) {
var statusCode = response.statusCode;
var res = response;
if (proxy) {
proxy.emit('response', res);
}
11 years ago
// redirect
if (status.redirect[statusCode] && 'location' in res.headers) {
res.resume(); // Discard response
11 years ago
if (++redirectCount > 10) {
cb(new GotError('Redirected 10 times. Aborting.'), undefined, res);
11 years ago
return;
}
get(urlLib.resolve(url, res.headers.location), opts, cb);
11 years ago
return;
}
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
var unzip = zlib.createUnzip();
res.pipe(unzip);
res = unzip;
}
if (statusCode < 200 || statusCode > 299) {
read(res, encoding, function (err, data) {
err = new GotError(url + ' response code is ' + statusCode + ' (' + status[statusCode] + ')', err);
err.code = statusCode;
cb(err, data, response);
});
return;
}
// pipe the response to the proxy if in proxy mode
if (proxy) {
proxy.setReadable(res);
return;
}
read(res, encoding, function (err, data) {
if (err) {
err = new GotError('Reading ' + url + ' response failed', err);
10 years ago
} else if (json) {
try {
data = JSON.parse(data);
} catch (e) {
err = new GotError('Parsing ' + url + ' response failed', err);
}
}
cb(err, data, response);
});
}).once('error', function (err) {
cb(new GotError('Request to ' + url + ' failed', err));
});
if (opts.timeout) {
timeout(req, opts.timeout);
}
if (!proxy) {
10 years ago
if (isStream.readable(body)) {
body.pipe(req);
} else {
req.end(body);
}
return;
}
if (body) {
proxy.write = function () {
throw new Error('got\'s stream is not writable when options.body is used');
};
10 years ago
if (isStream.readable(body)) {
body.pipe(req);
} else {
req.end(body);
}
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
proxy.setWritable(req);
return;
}
req.end();
10 years ago
}
11 years ago
get(url, opts, cb);
return proxy;
}
[
'get',
'post',
'put',
'patch',
'head',
'delete'
].forEach(function (el) {
got[el] = function (url, opts, cb) {
opts = opts || {};
opts.method = el.toUpperCase();
return got(url, opts, cb);
};
});
module.exports = got;