From 28b55339d5eacaa0b2cfe46775e1ed8c7b0b6167 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 1 Mar 2017 20:30:45 -0800 Subject: [PATCH] http: improve request options. --- lib/http/request.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/http/request.js b/lib/http/request.js index 26daba03..727a6dd4 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -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':