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