宇天 2020-09-20 16:27:35 +08:00
parent 7fdcf9562e
commit 0d023e8881
3 changed files with 84 additions and 79 deletions

View File

@ -1,12 +1,9 @@
/** /**
* {description of this file}
* @author yutent<yutent.io@gmail.com> * @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 14:52:58 * @date 2020/09/16 14:52:58
*/ */
import STATUS_TEXT from './lib/http-code-msg.json' import STATUS_TEXT from './lib/http-code.js'
const CHARSET_REGEXP = /;\s*charset\s*=/
export default class Response { export default class Response {
constructor(req, res) { constructor(req, res) {
@ -45,16 +42,20 @@ export default class Response {
if (this.rendered) { if (this.rendered) {
return return
} }
let prev = this.get(key) var prev = this.get(key)
let value = val var value
if (Array.isArray(val)) {
value = val
} else {
value = [val]
}
if (prev) { if (prev) {
if (Array.isArray(prev)) { if (Array.isArray(prev)) {
value = prev.concat(val) value = prev.concat(val)
} else if (Array.isArray(val)) {
value = [prev].concat(val)
} else { } else {
value = [prev, val] value = [prev].concat(val)
} }
} }
return this.set(key, value) return this.set(key, value)
@ -185,9 +186,13 @@ export default class Response {
return this return this
} }
if (arguments.length === 2) { if (arguments.length === 2) {
let value = Array.isArray(val) ? val.map(String) : String(val) var value = Array.isArray(val) ? val.map(String) : String(val)
if (key.toLowerCase() === 'content-type' && !CHARSET_REGEXP.test(value)) { if (
key.toLowerCase() === 'content-type' &&
typeof value === 'string' &&
value.indexOf('charset') < 0
) {
value += '; charset=utf-8' value += '; charset=utf-8'
} }

View File

@ -1,68 +0,0 @@
{
"100": "Continue",
"101": "Switching Protocols",
"102": "Processing",
"200": "OK",
"201": "Created",
"202": "Accepted",
"203": "Non-Authoritative Information",
"204": "No Content",
"205": "Reset Content",
"206": "Partial Conten",
"207": "Multi-Status",
"208": "Already Reported",
"226": "IM Used",
"300": "Multiple Choices",
"301": "Moved Permanently",
"302": "Found",
"303": "See Other",
"304": "Not Modified",
"305": "Use Proxy",
"306": "Switch Proxy",
"307": "Temporary Redirect",
"308": "Permanent Redirect",
"400": "Bad Request",
"401": "Unauthorized",
"402": "Payment Required",
"403": "Forbidden",
"404": "Not Found",
"405": "Method Not Allowed",
"406": "Not Acceptable",
"407": "Proxy Authentication Required",
"408": "Request Timeout",
"409": "Conflict",
"410": "Gone",
"411": "Length Required",
"412": "Precondition Failed",
"413": "Request Entity Too Large",
"414": "Request-URI Too Long",
"415": "Unsupported Media Type",
"416": "Requested Range Not Satisfiable",
"417": "Expectation Failed",
"418": "I'm a teapot",
"420": "Enhance Your Caim",
"421": "Misdirected Request",
"422": "Unprocessable Entity",
"423": "Locked",
"424": "Failed Dependency",
"425": "Unordered Collection",
"426": "Upgrade Required",
"428": "Precondition Required",
"429": "Too Many Requests",
"431": "Request Header Fields Too Large",
"444": "No Response",
"450": "Blocked by Windows Parental Controls",
"451": "Unavailable For Legal Reasons",
"494": "Request Header Too Large",
"500": "Internal Server Error",
"501": "Not Implemented",
"502": "Bad Gateway",
"503": "Service Unavailable",
"504": "Gateway Timeout",
"505": "HTTP Version Not Supported",
"506": "Variant Also Negotiates",
"507": "Insufficient Storage",
"508": "Loop Detected",
"510": "Not Extended",
"511": "Network Authentication Required"
}

68
lib/http-code.js Normal file
View File

@ -0,0 +1,68 @@
export default {
'100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'203': 'Non-Authoritative Information',
'204': 'No Content',
'205': 'Reset Content',
'206': 'Partial Conten',
'207': 'Multi-Status',
'208': 'Already Reported',
'226': 'IM Used',
'300': 'Multiple Choices',
'301': 'Moved Permanently',
'302': 'Found',
'303': 'See Other',
'304': 'Not Modified',
'305': 'Use Proxy',
'306': 'Switch Proxy',
'307': 'Temporary Redirect',
'308': 'Permanent Redirect',
'400': 'Bad Request',
'401': 'Unauthorized',
'402': 'Payment Required',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Acceptable',
'407': 'Proxy Authentication Required',
'408': 'Request Timeout',
'409': 'Conflict',
'410': 'Gone',
'411': 'Length Required',
'412': 'Precondition Failed',
'413': 'Request Entity Too Large',
'414': 'Request-URI Too Long',
'415': 'Unsupported Media Type',
'416': 'Requested Range Not Satisfiable',
'417': 'Expectation Failed',
'418': "I'm a teapot",
'420': 'Enhance Your Caim',
'421': 'Misdirected Request',
'422': 'Unprocessable Entity',
'423': 'Locked',
'424': 'Failed Dependency',
'425': 'Unordered Collection',
'426': 'Upgrade Required',
'428': 'Precondition Required',
'429': 'Too Many Requests',
'431': 'Request Header Fields Too Large',
'444': 'No Response',
'450': 'Blocked by Windows Parental Controls',
'451': 'Unavailable For Legal Reasons',
'494': 'Request Header Too Large',
'500': 'Internal Server Error',
'501': 'Not Implemented',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Timeout',
'505': 'HTTP Version Not Supported',
'506': 'Variant Also Negotiates',
'507': 'Insufficient Storage',
'508': 'Loop Detected',
'510': 'Not Extended',
'511': 'Network Authentication Required'
}