diff --git a/index.js b/index.js index 145e027..e03114c 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,9 @@ /** - * {description of this file} * @author yutent * @date 2020/09/16 14:52:58 */ -import STATUS_TEXT from './lib/http-code-msg.json' - -const CHARSET_REGEXP = /;\s*charset\s*=/ +import STATUS_TEXT from './lib/http-code.js' export default class Response { constructor(req, res) { @@ -45,16 +42,20 @@ export default class Response { if (this.rendered) { return } - let prev = this.get(key) - let value = val + var prev = this.get(key) + var value + + if (Array.isArray(val)) { + value = val + } else { + value = [val] + } if (prev) { if (Array.isArray(prev)) { value = prev.concat(val) - } else if (Array.isArray(val)) { - value = [prev].concat(val) } else { - value = [prev, val] + value = [prev].concat(val) } } return this.set(key, value) @@ -185,9 +186,13 @@ export default class Response { return this } 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' } diff --git a/lib/http-code-msg.json b/lib/http-code-msg.json deleted file mode 100644 index aece7af..0000000 --- a/lib/http-code-msg.json +++ /dev/null @@ -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" -} \ No newline at end of file diff --git a/lib/http-code.js b/lib/http-code.js new file mode 100644 index 0000000..d293f16 --- /dev/null +++ b/lib/http-code.js @@ -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' +}