warning.js 862 B

123456789101112131415161718192021222324
  1. const url = require('url')
  2. module.exports = setWarning
  3. function setWarning (reqOrRes, code, message, replace) {
  4. // Warning = "Warning" ":" 1#warning-value
  5. // warning-value = warn-code SP warn-agent SP warn-text [SP warn-date]
  6. // warn-code = 3DIGIT
  7. // warn-agent = ( host [ ":" port ] ) | pseudonym
  8. // ; the name or pseudonym of the server adding
  9. // ; the Warning header, for use in debugging
  10. // warn-text = quoted-string
  11. // warn-date = <"> HTTP-date <">
  12. // (https://tools.ietf.org/html/rfc2616#section-14.46)
  13. const host = url.parse(reqOrRes.url).host
  14. const jsonMessage = JSON.stringify(message)
  15. const jsonDate = JSON.stringify(new Date().toUTCString())
  16. const header = replace ? 'set' : 'append'
  17. reqOrRes.headers[header](
  18. 'Warning',
  19. `${code} ${host} ${jsonMessage} ${jsonDate}`
  20. )
  21. }