123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- 'use strict'
- var debug = require('debug')('finalhandler')
- var encodeUrl = require('encodeurl')
- var escapeHtml = require('escape-html')
- var onFinished = require('on-finished')
- var parseUrl = require('parseurl')
- var statuses = require('statuses')
- var unpipe = require('unpipe')
- var DOUBLE_SPACE_REGEXP = /\x20{2}/g
- var NEWLINE_REGEXP = /\n/g
- var defer = typeof setImmediate === 'function'
- ? setImmediate
- : function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) }
- var isFinished = onFinished.isFinished
- function createHtmlDocument (message) {
- var body = escapeHtml(message)
- .replace(NEWLINE_REGEXP, '<br>')
- .replace(DOUBLE_SPACE_REGEXP, ' ')
- return '<!DOCTYPE html>\n' +
- '<html lang="en">\n' +
- '<head>\n' +
- '<meta charset="utf-8">\n' +
- '<title>Error</title>\n' +
- '</head>\n' +
- '<body>\n' +
- '<pre>' + body + '</pre>\n' +
- '</body>\n' +
- '</html>\n'
- }
- module.exports = finalhandler
- function finalhandler (req, res, options) {
- var opts = options || {}
-
- var env = opts.env || process.env.NODE_ENV || 'development'
-
- var onerror = opts.onerror
- return function (err) {
- var headers
- var msg
- var status
-
- if (!err && headersSent(res)) {
- debug('cannot 404 after headers sent')
- return
- }
-
- if (err) {
-
- status = getErrorStatusCode(err)
- if (status === undefined) {
-
- status = getResponseStatusCode(res)
- } else {
-
- headers = getErrorHeaders(err)
- }
-
- msg = getErrorMessage(err, status, env)
- } else {
-
- status = 404
- msg = 'Cannot ' + req.method + ' ' + encodeUrl(getResourceName(req))
- }
- debug('default %s', status)
-
- if (err && onerror) {
- defer(onerror, err, req, res)
- }
-
- if (headersSent(res)) {
- debug('cannot %d after headers sent', status)
- if (req.socket) {
- req.socket.destroy()
- }
- return
- }
-
- send(req, res, status, headers, msg)
- }
- }
- function getErrorHeaders (err) {
- if (!err.headers || typeof err.headers !== 'object') {
- return undefined
- }
- var headers = Object.create(null)
- var keys = Object.keys(err.headers)
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i]
- headers[key] = err.headers[key]
- }
- return headers
- }
- function getErrorMessage (err, status, env) {
- var msg
- if (env !== 'production') {
-
- msg = err.stack
-
- if (!msg && typeof err.toString === 'function') {
- msg = err.toString()
- }
- }
- return msg || statuses.message[status]
- }
- function getErrorStatusCode (err) {
-
- if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) {
- return err.status
- }
-
- if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) {
- return err.statusCode
- }
- return undefined
- }
- function getResourceName (req) {
- try {
- return parseUrl.original(req).pathname
- } catch (e) {
- return 'resource'
- }
- }
- function getResponseStatusCode (res) {
- var status = res.statusCode
-
- if (typeof status !== 'number' || status < 400 || status > 599) {
- status = 500
- }
- return status
- }
- function headersSent (res) {
- return typeof res.headersSent !== 'boolean'
- ? Boolean(res._header)
- : res.headersSent
- }
- function send (req, res, status, headers, message) {
- function write () {
-
- var body = createHtmlDocument(message)
-
- res.statusCode = status
- if (req.httpVersionMajor < 2) {
- res.statusMessage = statuses.message[status]
- }
-
- res.removeHeader('Content-Encoding')
- res.removeHeader('Content-Language')
- res.removeHeader('Content-Range')
-
- setHeaders(res, headers)
-
- res.setHeader('Content-Security-Policy', "default-src 'none'")
- res.setHeader('X-Content-Type-Options', 'nosniff')
-
- res.setHeader('Content-Type', 'text/html; charset=utf-8')
- res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'))
- if (req.method === 'HEAD') {
- res.end()
- return
- }
- res.end(body, 'utf8')
- }
- if (isFinished(req)) {
- write()
- return
- }
-
- unpipe(req)
-
- onFinished(req, write)
- req.resume()
- }
- function setHeaders (res, headers) {
- if (!headers) {
- return
- }
- var keys = Object.keys(headers)
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i]
- res.setHeader(key, headers[key])
- }
- }
|