request: more content types.

This commit is contained in:
Christopher Jeffrey 2016-12-19 14:49:46 -08:00
parent 37f8d45c25
commit b9d6e7e40d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -192,13 +192,28 @@ RequestOptions.prototype.fromOptions = function fromOptions(options) {
} }
}; };
RequestOptions.prototype.expected = function expected(type) { RequestOptions.prototype.isExpected = function isExpected(type) {
if (!this.expect) if (!this.expect)
return true; return true;
return this.expect === type; return this.expect === type;
}; };
RequestOptions.prototype.isOverflow = function isOverflow(length) {
if (!length)
return false;
if (!this.buffer)
return false;
length = parseInt(length, 10);
if (length !== length)
return true;
return length > this.limit;
};
RequestOptions.prototype.getBackend = function getBackend() { RequestOptions.prototype.getBackend = function getBackend() {
ensureRequires(this.ssl); ensureRequires(this.ssl);
return this.ssl ? https : http; return this.ssl ? https : http;
@ -299,10 +314,6 @@ Request.prototype.cleanup = function cleanup() {
this.response.removeListener('data', this.onData); this.response.removeListener('data', this.onData);
this.response.removeListener('error', this.onEnd); this.response.removeListener('error', this.onEnd);
this.response.removeListener('end', this.onEnd); this.response.removeListener('end', this.onEnd);
if (this.response.socket) {
this.response.socket.removeListener('error', this.onEnd);
this.response.socket.removeListener('end', this.onEnd);
}
} }
}; };
@ -400,8 +411,9 @@ Request.prototype.finish = function finish(err) {
}; };
Request.prototype._onResponse = function _onResponse(response) { Request.prototype._onResponse = function _onResponse(response) {
var location = response.headers['location'];
var type = response.headers['content-type']; var type = response.headers['content-type'];
var length = response.headers['content-length'];
var location = response.headers['location'];
if (location) { if (location) {
if (++this.redirects > this.options.maxRedirects) { if (++this.redirects > this.options.maxRedirects) {
@ -417,16 +429,25 @@ Request.prototype._onResponse = function _onResponse(response) {
type = parseType(type); type = parseType(type);
if (!this.options.expected(type)) { if (!this.options.isExpected(type)) {
this.finish(new Error('Wrong content-type for response.')); this.finish(new Error('Wrong content-type for response.'));
return; return;
} }
if (this.options.isOverflow(length)) {
this.finish(new Error('Response exceeded limit.'));
return;
}
this.response = response; this.response = response;
this.statusCode = response.statusCode; this.statusCode = response.statusCode;
this.headers = response.headers; this.headers = response.headers;
this.type = type; this.type = type;
this.response.on('data', this.onData);
this.response.on('error', this.onEnd);
this.response.on('end', this.onEnd);
this.emit('headers', response.headers); this.emit('headers', response.headers);
this.emit('type', this.type); this.emit('type', this.type);
this.emit('response', response); this.emit('response', response);
@ -439,31 +460,26 @@ Request.prototype._onResponse = function _onResponse(response) {
this.body = []; this.body = [];
} }
} }
this.response.on('data', this.onData);
this.response.on('error', this.onEnd);
this.response.on('end', this.onEnd);
}; };
Request.prototype._onData = function _onData(data) { Request.prototype._onData = function _onData(data) {
this.total += data.length; this.total += data.length;
if (this.options.limit) { this.emit('data', data);
if (this.total > this.options.limit) {
this.finish(new Error('Response exceeded limit.'));
return;
}
}
if (this.options.buffer) { if (this.options.buffer) {
if (this.options.limit) {
if (this.total > this.options.limit) {
this.finish(new Error('Response exceeded limit.'));
return;
}
}
if (this.decoder) { if (this.decoder) {
this.body += this.decoder.write(data); this.body += this.decoder.write(data);
return; return;
} }
this.body.push(data); this.body.push(data);
} }
this.emit('data', data);
}; };
Request.prototype._onEnd = function _onEnd(err) { Request.prototype._onEnd = function _onEnd(err) {
@ -539,19 +555,32 @@ request.promise = function promise(options) {
*/ */
function parseType(type) { function parseType(type) {
if (/\/json/i.test(type)) type = type || '';
return 'json'; type = type.split(';')[0];
type = type.toLowerCase();
type = type.trim();
if (/form-urlencoded/i.test(type)) switch (type) {
return 'form'; case 'text/x-json':
case 'application/json':
if (/\/x?html/i.test(type)) return 'json';
return 'html'; case 'application/x-www-form-urlencoded':
return 'form';
if (/text\/plain/i.test(type)) case 'text/html':
return 'text'; case 'application/xhtml+xml':
return 'html';
return 'binary'; case 'text/javascript':
case 'application/javascript':
return 'js';
case 'text/css':
return 'css';
case 'text/plain':
return 'text';
case 'application/octet-stream':
return 'binary';
default:
return 'binary';
}
} }
function getType(type) { function getType(type) {
@ -562,6 +591,10 @@ 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 'js':
return 'application/javascript; charset=utf-8';
case 'css':
return 'text/css; charset=utf-8';
case 'text': case 'text':
return 'text/plain; charset=utf-8'; return 'text/plain; charset=utf-8';
case 'binary': case 'binary':