httpbase: minor.
This commit is contained in:
parent
8b74d95280
commit
65aefbd99c
@ -286,10 +286,11 @@ HTTPBase.prototype.bodyParser = function bodyParser(options) {
|
|||||||
* Parse request body.
|
* Parse request body.
|
||||||
* @private
|
* @private
|
||||||
* @param {ServerRequest} req
|
* @param {ServerRequest} req
|
||||||
|
* @param {Object} options
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HTTPBase.prototype.parseBody = async function parseBody(req, opt) {
|
HTTPBase.prototype.parseBody = async function parseBody(req, options) {
|
||||||
var body = Object.create(null);
|
var body = Object.create(null);
|
||||||
var type = req.contentType;
|
var type = req.contentType;
|
||||||
var data;
|
var data;
|
||||||
@ -297,20 +298,20 @@ HTTPBase.prototype.parseBody = async function parseBody(req, opt) {
|
|||||||
if (req.method === 'GET')
|
if (req.method === 'GET')
|
||||||
return body;
|
return body;
|
||||||
|
|
||||||
data = await this.readBody(req, 'utf8', opt);
|
data = await this.readBody(req, 'utf8', options);
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return body;
|
return body;
|
||||||
|
|
||||||
if (opt.contentType)
|
if (options.contentType)
|
||||||
type = opt.contentType;
|
type = options.contentType;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'json':
|
case 'json':
|
||||||
body = JSON.parse(data);
|
body = JSON.parse(data);
|
||||||
break;
|
break;
|
||||||
case 'form':
|
case 'form':
|
||||||
body = parsePairs(data, opt.keyLimit);
|
body = parsePairs(data, options.keyLimit);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -323,13 +324,14 @@ HTTPBase.prototype.parseBody = async function parseBody(req, opt) {
|
|||||||
* Read and buffer request body.
|
* Read and buffer request body.
|
||||||
* @param {ServerRequest} req
|
* @param {ServerRequest} req
|
||||||
* @param {String} enc
|
* @param {String} enc
|
||||||
|
* @param {Object} options
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HTTPBase.prototype.readBody = function readBody(req, enc, opt) {
|
HTTPBase.prototype.readBody = function readBody(req, enc, options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
return self._readBody(req, enc, opt, resolve, reject);
|
return self._readBody(req, enc, options, resolve, reject);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -338,11 +340,12 @@ HTTPBase.prototype.readBody = function readBody(req, enc, opt) {
|
|||||||
* @private
|
* @private
|
||||||
* @param {ServerRequest} req
|
* @param {ServerRequest} req
|
||||||
* @param {String} enc
|
* @param {String} enc
|
||||||
|
* @param {Object} options
|
||||||
* @param {Function} resolve
|
* @param {Function} resolve
|
||||||
* @param {Function} reject
|
* @param {Function} reject
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HTTPBase.prototype._readBody = function _readBody(req, enc, opt, resolve, reject) {
|
HTTPBase.prototype._readBody = function _readBody(req, enc, options, resolve, reject) {
|
||||||
var decode = new StringDecoder(enc);
|
var decode = new StringDecoder(enc);
|
||||||
var hasData = false;
|
var hasData = false;
|
||||||
var total = 0;
|
var total = 0;
|
||||||
@ -353,7 +356,7 @@ HTTPBase.prototype._readBody = function _readBody(req, enc, opt, resolve, reject
|
|||||||
timer = null;
|
timer = null;
|
||||||
cleanup();
|
cleanup();
|
||||||
reject(new Error('Request body timed out.'));
|
reject(new Error('Request body timed out.'));
|
||||||
}, opt.timeout);
|
}, options.timeout);
|
||||||
|
|
||||||
function cleanup() {
|
function cleanup() {
|
||||||
req.removeListener('data', onData);
|
req.removeListener('data', onData);
|
||||||
@ -370,7 +373,7 @@ HTTPBase.prototype._readBody = function _readBody(req, enc, opt, resolve, reject
|
|||||||
total += data.length;
|
total += data.length;
|
||||||
hasData = true;
|
hasData = true;
|
||||||
|
|
||||||
if (total > opt.bodyLimit) {
|
if (total > options.bodyLimit) {
|
||||||
reject(new Error('Request body overflow.'));
|
reject(new Error('Request body overflow.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user