2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2019-10-12 00:17:22 +00:00
|
|
|
const ReadStream = require('fs').ReadStream;
|
2021-01-04 14:06:20 +00:00
|
|
|
const contentDisposition = require('./content-disposition');
|
2019-10-12 14:31:02 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const extname = require('path').extname;
|
|
|
|
const util = require('util');
|
2019-10-12 00:17:22 +00:00
|
|
|
const onFinish = require('./onfinish');
|
|
|
|
const isJSON = require('./isjson');
|
2019-10-11 16:40:06 +00:00
|
|
|
const statuses = require('./statuses');
|
2019-10-12 04:30:06 +00:00
|
|
|
const getMimetype = require('./getmimetype');
|
2019-10-12 14:31:02 +00:00
|
|
|
const accepts = require('./accepts');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prototype.
|
|
|
|
*/
|
|
|
|
|
2013-11-14 02:34:15 +00:00
|
|
|
module.exports = {
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-13 21:17:16 +00:00
|
|
|
/**
|
|
|
|
* Return the request socket.
|
|
|
|
*
|
|
|
|
* @return {Connection}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get socket() {
|
2018-04-27 15:51:47 +00:00
|
|
|
return this.res.socket;
|
2013-11-13 21:17:16 +00:00
|
|
|
},
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
|
|
|
* Return response header.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get header() {
|
2017-03-08 06:59:24 +00:00
|
|
|
const { res } = this;
|
|
|
|
return typeof res.getHeaders === 'function'
|
|
|
|
? res.getHeaders()
|
2019-06-26 03:15:22 +00:00
|
|
|
: res._headers || {}; // Node < 7.7
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
2015-04-23 09:28:02 +00:00
|
|
|
/**
|
|
|
|
* Return response header, alias as response.header
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get headers() {
|
|
|
|
return this.header;
|
|
|
|
},
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
|
|
|
* Get response status code.
|
|
|
|
*
|
|
|
|
* @return {Number}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get status() {
|
2014-04-29 04:17:30 +00:00
|
|
|
return this.res.statusCode;
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set response status code.
|
|
|
|
*
|
2014-06-07 23:26:19 +00:00
|
|
|
* @param {Number} code
|
2013-11-13 17:01:15 +00:00
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2014-03-13 01:29:14 +00:00
|
|
|
set status(code) {
|
2018-02-11 08:25:24 +00:00
|
|
|
if (this.headerSent) return;
|
|
|
|
|
2019-01-28 08:50:27 +00:00
|
|
|
assert(Number.isInteger(code), 'status code must be a number');
|
|
|
|
assert(code >= 100 && code <= 999, `invalid status code: ${code}`);
|
2014-04-29 04:17:30 +00:00
|
|
|
this._explicitStatus = true;
|
|
|
|
this.res.statusCode = code;
|
2017-11-06 12:17:43 +00:00
|
|
|
if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code];
|
2014-10-01 12:12:20 +00:00
|
|
|
if (this.body && statuses.empty[code]) this.body = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get response status message
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get message() {
|
|
|
|
return this.res.statusMessage || statuses[this.status];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set response status message
|
|
|
|
*
|
|
|
|
* @param {String} msg
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
set message(msg) {
|
|
|
|
this.res.statusMessage = msg;
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get response body.
|
|
|
|
*
|
|
|
|
* @return {Mixed}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get body() {
|
|
|
|
return this._body;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set response body.
|
|
|
|
*
|
|
|
|
* @param {String|Buffer|Object|Stream} val
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
set body(val) {
|
2015-10-05 18:23:47 +00:00
|
|
|
const original = this._body;
|
2013-11-13 17:01:15 +00:00
|
|
|
this._body = val;
|
|
|
|
|
|
|
|
// no content
|
|
|
|
if (null == val) {
|
2014-10-01 12:12:20 +00:00
|
|
|
if (!statuses.empty[this.status]) this.status = 204;
|
2015-12-20 21:30:54 +00:00
|
|
|
this.remove('Content-Type');
|
|
|
|
this.remove('Content-Length');
|
|
|
|
this.remove('Transfer-Encoding');
|
2013-11-13 17:01:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:26:19 +00:00
|
|
|
// set the status
|
2014-04-29 04:17:30 +00:00
|
|
|
if (!this._explicitStatus) this.status = 200;
|
2013-12-30 06:26:19 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
// set the content-type only if not yet set
|
2015-10-05 18:23:47 +00:00
|
|
|
const setType = !this.header['content-type'];
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
// string
|
|
|
|
if ('string' == typeof val) {
|
2014-02-14 02:35:25 +00:00
|
|
|
if (setType) this.type = /^\s*</.test(val) ? 'html' : 'text';
|
2013-11-13 17:01:15 +00:00
|
|
|
this.length = Buffer.byteLength(val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffer
|
|
|
|
if (Buffer.isBuffer(val)) {
|
|
|
|
if (setType) this.type = 'bin';
|
|
|
|
this.length = val.length;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stream
|
2013-12-20 05:14:44 +00:00
|
|
|
if ('function' == typeof val.pipe) {
|
2019-10-12 00:17:22 +00:00
|
|
|
// On finish, destroy the stream
|
|
|
|
onFinish(this.res, () => {
|
|
|
|
// Functionality taken from destroy
|
|
|
|
if (!(val instanceof ReadStream)) {
|
|
|
|
if (typeof val.destroy === 'function') {
|
|
|
|
val.destroy();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof val.close !== 'function') return;
|
|
|
|
|
|
|
|
// Fix potential bug (?) with node leaving file descriptor open
|
|
|
|
val.on('open', function() {
|
|
|
|
if (typeof this.fd === 'number') {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
val.on('error', err => this.ctx.onerror(err));
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
// overwriting
|
|
|
|
if (null != original && original != val) this.remove('Content-Length');
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
if (setType) this.type = 'bin';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// json
|
2014-04-29 03:34:26 +00:00
|
|
|
this.remove('Content-Length');
|
2013-11-13 17:01:15 +00:00
|
|
|
this.type = 'json';
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Content-Length field to `n`.
|
|
|
|
*
|
|
|
|
* @param {Number} n
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
set length(n) {
|
|
|
|
this.set('Content-Length', n);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return parsed response Content-Length when present.
|
|
|
|
*
|
|
|
|
* @return {Number}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get length() {
|
2015-10-05 18:23:47 +00:00
|
|
|
const len = this.header['content-length'];
|
|
|
|
const body = this.body;
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
if (null == len) {
|
|
|
|
if (!body) return;
|
|
|
|
if ('string' == typeof body) return Buffer.byteLength(body);
|
2014-04-29 03:34:26 +00:00
|
|
|
if (Buffer.isBuffer(body)) return body.length;
|
|
|
|
if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body));
|
|
|
|
return;
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 08:22:35 +00:00
|
|
|
return Math.trunc(len) || 0;
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a header has been written to the socket.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get headerSent() {
|
|
|
|
return this.res.headersSent;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Vary on `field`.
|
|
|
|
*
|
|
|
|
* @param {String} field
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
vary(field) {
|
2018-02-11 08:25:24 +00:00
|
|
|
if (this.headerSent) return;
|
|
|
|
|
2019-10-12 00:17:22 +00:00
|
|
|
// Revert #291, no reason to include full module
|
|
|
|
// that can be accomplished in 4 extra lines of code
|
|
|
|
let list = this.header.vary;
|
|
|
|
if (!list) return this.set('vary', field);
|
|
|
|
|
|
|
|
list = list.split(/ *, */);
|
|
|
|
if (!~list.indexOf(field)) list.push(field);
|
|
|
|
this.set('vary', list.join(', '));
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a 302 redirect to `url`.
|
|
|
|
*
|
|
|
|
* The string "back" is special-cased
|
|
|
|
* to provide Referrer support, when Referrer
|
|
|
|
* is not present `alt` or "/" is used.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* this.redirect('back');
|
|
|
|
* this.redirect('back', '/index.html');
|
|
|
|
* this.redirect('/login');
|
|
|
|
* this.redirect('http://google.com');
|
|
|
|
*
|
|
|
|
* @param {String} url
|
2016-02-22 10:33:08 +00:00
|
|
|
* @param {String} [alt]
|
2013-11-13 17:01:15 +00:00
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
redirect(url, alt) {
|
2014-04-12 17:59:04 +00:00
|
|
|
// location
|
2013-11-13 17:01:15 +00:00
|
|
|
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
|
2019-10-12 00:17:22 +00:00
|
|
|
this.set('Location', encodeURI(url));
|
2014-04-12 17:59:04 +00:00
|
|
|
|
|
|
|
// status
|
2014-10-01 12:12:20 +00:00
|
|
|
if (!statuses.redirect[this.status]) this.status = 302;
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
// html
|
2019-09-29 19:17:14 +00:00
|
|
|
if (this.ctx.headers.accept && this.ctx.headers.accept.indexOf('html') >= 0) {
|
2019-10-12 14:31:02 +00:00
|
|
|
// Sanitize the url in case developer does something silly like:
|
|
|
|
// ctx.redirect(ctx.query.goto) or something without sanitizing himself.
|
2019-10-12 00:17:22 +00:00
|
|
|
url = url.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
2013-11-13 17:01:15 +00:00
|
|
|
this.type = 'text/html; charset=utf-8';
|
2015-10-09 04:01:32 +00:00
|
|
|
this.body = `Redirecting to <a href="${url}">${url}</a>.`;
|
2013-11-13 17:01:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// text
|
2014-11-27 17:01:15 +00:00
|
|
|
this.type = 'text/plain; charset=utf-8';
|
2015-10-09 04:01:32 +00:00
|
|
|
this.body = `Redirecting to ${url}.`;
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Content-Disposition header to "attachment" with optional `filename`.
|
|
|
|
*
|
|
|
|
* @param {String} filename
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2018-10-08 14:43:11 +00:00
|
|
|
attachment(filename, options) {
|
2013-11-13 17:01:15 +00:00
|
|
|
if (filename) this.type = extname(filename);
|
2018-10-08 14:43:11 +00:00
|
|
|
this.set('Content-Disposition', contentDisposition(filename, options));
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Content-Type response header with `type` through `mime.lookup()`
|
2014-04-09 16:34:50 +00:00
|
|
|
* when it does not contain a charset.
|
2013-11-13 17:01:15 +00:00
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* this.type = '.html';
|
|
|
|
* this.type = 'html';
|
|
|
|
* this.type = 'json';
|
|
|
|
* this.type = 'application/json';
|
|
|
|
* this.type = 'png';
|
|
|
|
*
|
|
|
|
* @param {String} type
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2019-09-29 19:17:14 +00:00
|
|
|
set type(orgType) {
|
2019-10-11 17:46:53 +00:00
|
|
|
let type = orgType;
|
2019-10-09 00:32:09 +00:00
|
|
|
if (!type) {
|
|
|
|
this.remove('Content-Type');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-29 19:17:14 +00:00
|
|
|
// If full type is specified, pass it straight on.
|
|
|
|
// Otherwise we do some basic checking for most common
|
|
|
|
// supported mime types.
|
|
|
|
if (type.indexOf('/') > 0 || type.indexOf(';') > 0) {
|
|
|
|
if (type.indexOf(';') === -1 && type.indexOf('text') >= 0) {
|
2019-10-11 17:46:53 +00:00
|
|
|
type += '; charset=utf-8';
|
2019-09-29 19:17:14 +00:00
|
|
|
}
|
2015-10-19 04:15:50 +00:00
|
|
|
this.set('Content-Type', type);
|
|
|
|
} else {
|
2019-10-12 04:30:06 +00:00
|
|
|
let mimetype = getMimetype(type, true);
|
|
|
|
if (mimetype) {
|
|
|
|
this.set('Content-Type', mimetype);
|
|
|
|
} else {
|
|
|
|
this.remove('Content-Type');
|
|
|
|
}
|
2015-10-19 04:15:50 +00:00
|
|
|
}
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
2013-11-14 03:59:49 +00:00
|
|
|
/**
|
|
|
|
* Set the Last-Modified date using a string or a Date.
|
|
|
|
*
|
|
|
|
* this.response.lastModified = new Date();
|
|
|
|
* this.response.lastModified = '2013-09-13';
|
|
|
|
*
|
|
|
|
* @param {String|Date} type
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
set lastModified(val) {
|
|
|
|
if ('string' == typeof val) val = new Date(val);
|
|
|
|
this.set('Last-Modified', val.toUTCString());
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Last-Modified date in Date form, if it exists.
|
|
|
|
*
|
|
|
|
* @return {Date}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get lastModified() {
|
2015-10-05 18:23:47 +00:00
|
|
|
const date = this.get('last-modified');
|
2013-11-14 03:59:49 +00:00
|
|
|
if (date) return new Date(date);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the ETag of a response.
|
|
|
|
* This will normalize the quotes if necessary.
|
|
|
|
*
|
|
|
|
* this.response.etag = 'md5hashsum';
|
|
|
|
* this.response.etag = '"md5hashsum"';
|
|
|
|
* this.response.etag = 'W/"123456789"';
|
|
|
|
*
|
|
|
|
* @param {String} etag
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
set etag(val) {
|
2015-10-09 04:01:32 +00:00
|
|
|
if (!/^(W\/)?"/.test(val)) val = `"${val}"`;
|
2013-11-14 03:59:49 +00:00
|
|
|
this.set('ETag', val);
|
|
|
|
},
|
|
|
|
|
2014-06-05 23:19:29 +00:00
|
|
|
/**
|
|
|
|
* Get the ETag of a response.
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get etag() {
|
|
|
|
return this.get('ETag');
|
|
|
|
},
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
2015-08-25 08:49:05 +00:00
|
|
|
* Return the response mime type void of
|
2013-11-13 17:01:15 +00:00
|
|
|
* parameters such as "charset".
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
get type() {
|
2015-10-05 18:23:47 +00:00
|
|
|
const type = this.get('Content-Type');
|
2015-04-28 17:44:02 +00:00
|
|
|
if (!type) return '';
|
2019-01-03 12:48:31 +00:00
|
|
|
return type.split(';', 1)[0];
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
2014-06-13 07:30:59 +00:00
|
|
|
/**
|
|
|
|
* Check whether the response is one of the listed types.
|
|
|
|
* Pretty much the same as `this.request.is()`.
|
|
|
|
*
|
|
|
|
* @param {String|Array} types...
|
|
|
|
* @return {String|false}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
is(types) {
|
2019-10-12 14:31:02 +00:00
|
|
|
if (!types) return this.type || false;
|
2014-06-13 07:30:59 +00:00
|
|
|
if (!Array.isArray(types)) types = [].slice.call(arguments);
|
2019-10-12 14:31:02 +00:00
|
|
|
return accepts(this, 'content-type', types, false);
|
2014-06-13 07:30:59 +00:00
|
|
|
},
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
|
|
|
* Return response header.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* this.get('Content-Type');
|
|
|
|
* // => "text/plain"
|
|
|
|
*
|
|
|
|
* this.get('content-type');
|
|
|
|
* // => "text/plain"
|
|
|
|
*
|
2013-11-24 13:06:35 +00:00
|
|
|
* @param {String} field
|
2013-11-13 17:01:15 +00:00
|
|
|
* @return {String}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
get(field) {
|
2015-04-28 17:44:02 +00:00
|
|
|
return this.header[field.toLowerCase()] || '';
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set header `field` to `val`, or pass
|
|
|
|
* an object of header fields.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* this.set('Foo', ['bar', 'baz']);
|
|
|
|
* this.set('Accept', 'application/json');
|
|
|
|
* this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
|
|
*
|
|
|
|
* @param {String|Object|Array} field
|
|
|
|
* @param {String} val
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
set(field, val) {
|
2018-02-11 08:25:24 +00:00
|
|
|
if (this.headerSent) return;
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
if (2 == arguments.length) {
|
2018-07-12 06:10:12 +00:00
|
|
|
if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
|
|
|
|
else if (typeof val !== 'string') val = String(val);
|
2013-11-13 17:01:15 +00:00
|
|
|
this.res.setHeader(field, val);
|
|
|
|
} else {
|
2015-10-22 22:46:47 +00:00
|
|
|
for (const key in field) {
|
2013-11-13 17:01:15 +00:00
|
|
|
this.set(key, field[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-01-25 17:53:55 +00:00
|
|
|
/**
|
|
|
|
* Append additional header `field` with value `val`.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
2015-10-28 07:53:49 +00:00
|
|
|
* ```
|
|
|
|
* this.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
|
|
|
|
* this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
|
|
|
|
* this.append('Warning', '199 Miscellaneous warning');
|
|
|
|
* ```
|
2015-01-25 17:53:55 +00:00
|
|
|
*
|
|
|
|
* @param {String} field
|
|
|
|
* @param {String|Array} val
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
append(field, val) {
|
2015-10-05 18:23:47 +00:00
|
|
|
const prev = this.get(field);
|
2015-01-27 18:14:57 +00:00
|
|
|
|
2015-01-25 17:53:55 +00:00
|
|
|
if (prev) {
|
2015-01-27 18:14:57 +00:00
|
|
|
val = Array.isArray(prev)
|
|
|
|
? prev.concat(val)
|
|
|
|
: [prev].concat(val);
|
2015-01-25 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 18:14:57 +00:00
|
|
|
return this.set(field, val);
|
2015-01-25 17:53:55 +00:00
|
|
|
},
|
|
|
|
|
2013-11-24 13:06:35 +00:00
|
|
|
/**
|
|
|
|
* Remove header `field`.
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
remove(field) {
|
2018-02-11 08:25:24 +00:00
|
|
|
if (this.headerSent) return;
|
|
|
|
|
2013-11-24 13:06:35 +00:00
|
|
|
this.res.removeHeader(field);
|
|
|
|
},
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
2014-02-15 10:06:08 +00:00
|
|
|
* Checks if the request is writable.
|
|
|
|
* Tests for the existence of the socket
|
|
|
|
* as node sometimes does not set it.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
|
|
|
get writable() {
|
2016-07-23 17:20:29 +00:00
|
|
|
// can't write any more after response finished
|
|
|
|
if (this.res.finished) return false;
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const socket = this.res.socket;
|
2016-07-23 17:20:29 +00:00
|
|
|
// There are already pending outgoing res, but still writable
|
|
|
|
// https://github.com/nodejs/node/blob/v4.4.7/lib/_http_server.js#L486
|
|
|
|
if (!socket) return true;
|
2014-02-15 10:06:08 +00:00
|
|
|
return socket.writable;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Inspect implementation.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
inspect() {
|
2014-03-11 18:06:57 +00:00
|
|
|
if (!this.res) return;
|
2015-10-05 18:23:47 +00:00
|
|
|
const o = this.toJSON();
|
2013-11-14 19:34:55 +00:00
|
|
|
o.body = this.body;
|
|
|
|
return o;
|
2013-11-13 17:01:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return JSON representation.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-10-12 06:51:31 +00:00
|
|
|
toJSON() {
|
2019-10-09 00:32:09 +00:00
|
|
|
return {
|
|
|
|
status: this.status,
|
|
|
|
message: this.message,
|
2019-10-11 17:46:53 +00:00
|
|
|
header: this.header
|
|
|
|
};
|
2016-03-04 03:57:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush any set headers, and begin the body
|
|
|
|
*/
|
|
|
|
flushHeaders() {
|
2016-08-10 19:15:48 +00:00
|
|
|
this.res.flushHeaders();
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-25 02:34:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom inspection implementation for newer Node.js versions.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
if (util.inspect.custom) {
|
|
|
|
module.exports[util.inspect.custom] = module.exports.inspect;
|
|
|
|
}
|