index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*!
  2. * content-type
  3. * Copyright(c) 2015 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1
  9. *
  10. * parameter = token "=" ( token / quoted-string )
  11. * token = 1*tchar
  12. * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
  13. * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
  14. * / DIGIT / ALPHA
  15. * ; any VCHAR, except delimiters
  16. * quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
  17. * qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
  18. * obs-text = %x80-FF
  19. * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
  20. */
  21. var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g
  22. var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/
  23. var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
  24. /**
  25. * RegExp to match quoted-pair in RFC 7230 sec 3.2.6
  26. *
  27. * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
  28. * obs-text = %x80-FF
  29. */
  30. var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g
  31. /**
  32. * RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6
  33. */
  34. var QUOTE_REGEXP = /([\\"])/g
  35. /**
  36. * RegExp to match type in RFC 7231 sec 3.1.1.1
  37. *
  38. * media-type = type "/" subtype
  39. * type = token
  40. * subtype = token
  41. */
  42. var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
  43. /**
  44. * Module exports.
  45. * @public
  46. */
  47. exports.format = format
  48. exports.parse = parse
  49. /**
  50. * Format object to media type.
  51. *
  52. * @param {object} obj
  53. * @return {string}
  54. * @public
  55. */
  56. function format (obj) {
  57. if (!obj || typeof obj !== 'object') {
  58. throw new TypeError('argument obj is required')
  59. }
  60. var parameters = obj.parameters
  61. var type = obj.type
  62. if (!type || !TYPE_REGEXP.test(type)) {
  63. throw new TypeError('invalid type')
  64. }
  65. var string = type
  66. // append parameters
  67. if (parameters && typeof parameters === 'object') {
  68. var param
  69. var params = Object.keys(parameters).sort()
  70. for (var i = 0; i < params.length; i++) {
  71. param = params[i]
  72. if (!TOKEN_REGEXP.test(param)) {
  73. throw new TypeError('invalid parameter name')
  74. }
  75. string += '; ' + param + '=' + qstring(parameters[param])
  76. }
  77. }
  78. return string
  79. }
  80. /**
  81. * Parse media type to object.
  82. *
  83. * @param {string|object} string
  84. * @return {Object}
  85. * @public
  86. */
  87. function parse (string) {
  88. if (!string) {
  89. throw new TypeError('argument string is required')
  90. }
  91. // support req/res-like objects as argument
  92. var header = typeof string === 'object'
  93. ? getcontenttype(string)
  94. : string
  95. if (typeof header !== 'string') {
  96. throw new TypeError('argument string is required to be a string')
  97. }
  98. var index = header.indexOf(';')
  99. var type = index !== -1
  100. ? header.substr(0, index).trim()
  101. : header.trim()
  102. if (!TYPE_REGEXP.test(type)) {
  103. throw new TypeError('invalid media type')
  104. }
  105. var obj = new ContentType(type.toLowerCase())
  106. // parse parameters
  107. if (index !== -1) {
  108. var key
  109. var match
  110. var value
  111. PARAM_REGEXP.lastIndex = index
  112. while ((match = PARAM_REGEXP.exec(header))) {
  113. if (match.index !== index) {
  114. throw new TypeError('invalid parameter format')
  115. }
  116. index += match[0].length
  117. key = match[1].toLowerCase()
  118. value = match[2]
  119. if (value[0] === '"') {
  120. // remove quotes and escapes
  121. value = value
  122. .substr(1, value.length - 2)
  123. .replace(QESC_REGEXP, '$1')
  124. }
  125. obj.parameters[key] = value
  126. }
  127. if (index !== header.length) {
  128. throw new TypeError('invalid parameter format')
  129. }
  130. }
  131. return obj
  132. }
  133. /**
  134. * Get content-type from req/res objects.
  135. *
  136. * @param {object}
  137. * @return {Object}
  138. * @private
  139. */
  140. function getcontenttype (obj) {
  141. var header
  142. if (typeof obj.getHeader === 'function') {
  143. // res-like
  144. header = obj.getHeader('content-type')
  145. } else if (typeof obj.headers === 'object') {
  146. // req-like
  147. header = obj.headers && obj.headers['content-type']
  148. }
  149. if (typeof header !== 'string') {
  150. throw new TypeError('content-type header is missing from object')
  151. }
  152. return header
  153. }
  154. /**
  155. * Quote a string if necessary.
  156. *
  157. * @param {string} val
  158. * @return {string}
  159. * @private
  160. */
  161. function qstring (val) {
  162. var str = String(val)
  163. // no need to quote tokens
  164. if (TOKEN_REGEXP.test(str)) {
  165. return str
  166. }
  167. if (str.length > 0 && !TEXT_REGEXP.test(str)) {
  168. throw new TypeError('invalid parameter value')
  169. }
  170. return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'
  171. }
  172. /**
  173. * Class to represent a content type.
  174. * @private
  175. */
  176. function ContentType (type) {
  177. this.parameters = Object.create(null)
  178. this.type = type
  179. }