http: improve request options.

This commit is contained in:
Christopher Jeffrey 2017-03-01 20:30:45 -08:00
parent 46a646aebd
commit 28b55339d5
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -49,6 +49,7 @@ function RequestOptions(options) {
this.maxRedirects = 5;
this.timeout = 5000;
this.buffer = false;
this.headers = null;
// Hack
ensureRequires();
@ -191,6 +192,11 @@ RequestOptions.prototype.fromOptions = function fromOptions(options) {
assert(typeof options.buffer === 'boolean');
this.buffer = options.buffer;
}
if (options.headers != null) {
assert(typeof options.headers === 'object');
this.headers = options.headers;
}
};
RequestOptions.prototype.isExpected = function isExpected(type) {
@ -220,20 +226,21 @@ RequestOptions.prototype.getBackend = function getBackend() {
return this.ssl ? https : http;
};
RequestOptions.prototype.toHTTP = function toHTTP() {
var query = '';
var headers = {};
var auth;
RequestOptions.prototype.getHeaders = function getHeaders() {
var headers, auth;
if (this.query)
query = '?' + qs.stringify(this.query);
if (this.headers)
return this.headers;
headers = {};
headers['User-Agent'] = this.agent;
if (this.body) {
if (this.type)
headers['Content-Type'] = getType(this.type);
if (this.body)
headers['Content-Length'] = this.body.length + '';
}
if (this.auth) {
auth = this.auth.username + ':' + this.auth.password;
@ -241,12 +248,21 @@ RequestOptions.prototype.toHTTP = function toHTTP() {
'Basic ' + new Buffer(auth, 'utf8').toString('base64');
}
return headers;
};
RequestOptions.prototype.toHTTP = function toHTTP() {
var query = '';
if (this.query)
query = '?' + qs.stringify(this.query);
return {
method: this.method,
host: this.host,
port: this.port,
path: this.path + query,
headers: headers,
headers: this.getHeaders(),
agent: this.pool ? null : false,
rejectUnauthorized: this.strictSSL
};
@ -561,6 +577,9 @@ function parseType(type) {
case 'text/html':
case 'application/xhtml+xml':
return 'html';
case 'text/xml':
case 'application/xml':
return 'xml';
case 'text/javascript':
case 'application/javascript':
return 'js';
@ -583,6 +602,8 @@ function getType(type) {
return 'application/x-www-form-urlencoded; charset=utf-8';
case 'html':
return 'text/html; charset=utf-8';
case 'xml':
return 'application/xml; charset=utf-8';
case 'js':
return 'application/javascript; charset=utf-8';
case 'css':