a7bafd2d0b66fd3fa4f4f21d441fa5e54a3b94f726b15644cf7876adbc822eaad6cff390636bb965ab3f5b9fd155f14652242b72e4a35e81482c91d56bf24e 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict'
  2. var utils = exports
  3. var util = require('util')
  4. function ProtocolError (code, message) {
  5. this.code = code
  6. this.message = message
  7. }
  8. util.inherits(ProtocolError, Error)
  9. utils.ProtocolError = ProtocolError
  10. utils.error = function error (code, message) {
  11. return new ProtocolError(code, message)
  12. }
  13. utils.reverse = function reverse (object) {
  14. var result = []
  15. Object.keys(object).forEach(function (key) {
  16. result[object[key] | 0] = key
  17. })
  18. return result
  19. }
  20. // weight [1, 36] <=> priority [0, 7]
  21. // This way weight=16 is preserved and has priority=3
  22. utils.weightToPriority = function weightToPriority (weight) {
  23. return ((Math.min(35, (weight - 1)) / 35) * 7) | 0
  24. }
  25. utils.priorityToWeight = function priorityToWeight (priority) {
  26. return (((priority / 7) * 35) | 0) + 1
  27. }
  28. // Copy-Paste from node
  29. exports.addHeaderLine = function addHeaderLine (field, value, dest) {
  30. field = field.toLowerCase()
  31. if (/^:/.test(field)) {
  32. dest[field] = value
  33. return
  34. }
  35. switch (field) {
  36. // Array headers:
  37. case 'set-cookie':
  38. if (dest[field] !== undefined) {
  39. dest[field].push(value)
  40. } else {
  41. dest[field] = [ value ]
  42. }
  43. break
  44. /* eslint-disable max-len */
  45. // list is taken from:
  46. /* eslint-enable max-len */
  47. case 'content-type':
  48. case 'content-length':
  49. case 'user-agent':
  50. case 'referer':
  51. case 'host':
  52. case 'authorization':
  53. case 'proxy-authorization':
  54. case 'if-modified-since':
  55. case 'if-unmodified-since':
  56. case 'from':
  57. case 'location':
  58. case 'max-forwards':
  59. // drop duplicates
  60. if (dest[field] === undefined) {
  61. dest[field] = value
  62. }
  63. break
  64. case 'cookie':
  65. // make semicolon-separated list
  66. if (dest[field] !== undefined) {
  67. dest[field] += '; ' + value
  68. } else {
  69. dest[field] = value
  70. }
  71. break
  72. default:
  73. // make comma-separated list
  74. if (dest[field] !== undefined) {
  75. dest[field] += ', ' + value
  76. } else {
  77. dest[field] = value
  78. }
  79. }
  80. }