123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*!
  2. * content-disposition
  3. * Copyright(c) 2014 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * Module exports.
  9. */
  10. module.exports = contentDisposition
  11. module.exports.parse = parse
  12. /**
  13. * Module dependencies.
  14. */
  15. var basename = require('path').basename
  16. /**
  17. * RegExp to match non attr-char, *after* encodeURIComponent (i.e. not including "%")
  18. */
  19. var ENCODE_URL_ATTR_CHAR_REGEXP = /[\x00-\x20"'()*,/:;<=>?@[\\\]{}\x7f]/g // eslint-disable-line no-control-regex
  20. /**
  21. * RegExp to match percent encoding escape.
  22. */
  23. var HEX_ESCAPE_REGEXP = /%[0-9A-Fa-f]{2}/
  24. var HEX_ESCAPE_REPLACE_REGEXP = /%([0-9A-Fa-f]{2})/g
  25. /**
  26. * RegExp to match non-latin1 characters.
  27. */
  28. var NON_LATIN1_REGEXP = /[^\x20-\x7e\xa0-\xff]/g
  29. /**
  30. * RegExp to match quoted-pair in RFC 2616
  31. *
  32. * quoted-pair = "\" CHAR
  33. * CHAR = <any US-ASCII character (octets 0 - 127)>
  34. */
  35. var QESC_REGEXP = /\\([\u0000-\u007f])/g
  36. /**
  37. * RegExp to match chars that must be quoted-pair in RFC 2616
  38. */
  39. var QUOTE_REGEXP = /([\\"])/g
  40. /**
  41. * RegExp for various RFC 2616 grammar
  42. *
  43. * parameter = token "=" ( token | quoted-string )
  44. * token = 1*<any CHAR except CTLs or separators>
  45. * separators = "(" | ")" | "<" | ">" | "@"
  46. * | "," | ";" | ":" | "\" | <">
  47. * | "/" | "[" | "]" | "?" | "="
  48. * | "{" | "}" | SP | HT
  49. * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
  50. * qdtext = <any TEXT except <">>
  51. * quoted-pair = "\" CHAR
  52. * CHAR = <any US-ASCII character (octets 0 - 127)>
  53. * TEXT = <any OCTET except CTLs, but including LWS>
  54. * LWS = [CRLF] 1*( SP | HT )
  55. * CRLF = CR LF
  56. * CR = <US-ASCII CR, carriage return (13)>
  57. * LF = <US-ASCII LF, linefeed (10)>
  58. * SP = <US-ASCII SP, space (32)>
  59. * HT = <US-ASCII HT, horizontal-tab (9)>
  60. * CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
  61. * OCTET = <any 8-bit sequence of data>
  62. */
  63. var PARAM_REGEXP = /;[\x09\x20]*([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*=[\x09\x20]*("(?:[\x20!\x23-\x5b\x5d-\x7e\x80-\xff]|\\[\x20-\x7e])*"|[!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*/g // eslint-disable-line no-control-regex
  64. var TEXT_REGEXP = /^[\x20-\x7e\x80-\xff]+$/
  65. var TOKEN_REGEXP = /^[!#$%&'*+.0-9A-Z^_`a-z|~-]+$/
  66. /**
  67. * RegExp for various RFC 5987 grammar
  68. *
  69. * ext-value = charset "'" [ language ] "'" value-chars
  70. * charset = "UTF-8" / "ISO-8859-1" / mime-charset
  71. * mime-charset = 1*mime-charsetc
  72. * mime-charsetc = ALPHA / DIGIT
  73. * / "!" / "#" / "$" / "%" / "&"
  74. * / "+" / "-" / "^" / "_" / "`"
  75. * / "{" / "}" / "~"
  76. * language = ( 2*3ALPHA [ extlang ] )
  77. * / 4ALPHA
  78. * / 5*8ALPHA
  79. * extlang = *3( "-" 3ALPHA )
  80. * value-chars = *( pct-encoded / attr-char )
  81. * pct-encoded = "%" HEXDIG HEXDIG
  82. * attr-char = ALPHA / DIGIT
  83. * / "!" / "#" / "$" / "&" / "+" / "-" / "."
  84. * / "^" / "_" / "`" / "|" / "~"
  85. */
  86. var EXT_VALUE_REGEXP = /^([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[A-Za-z]{2,3}(?:-[A-Za-z]{3}){0,3}|[A-Za-z]{4,8}|)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/
  87. /**
  88. * RegExp for various RFC 6266 grammar
  89. *
  90. * disposition-type = "inline" | "attachment" | disp-ext-type
  91. * disp-ext-type = token
  92. * disposition-parm = filename-parm | disp-ext-parm
  93. * filename-parm = "filename" "=" value
  94. * | "filename*" "=" ext-value
  95. * disp-ext-parm = token "=" value
  96. * | ext-token "=" ext-value
  97. * ext-token = <the characters in token, followed by "*">
  98. */
  99. var DISPOSITION_TYPE_REGEXP = /^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/ // eslint-disable-line no-control-regex
  100. /**
  101. * Create an attachment Content-Disposition header.
  102. *
  103. * @param {string} [filename]
  104. * @param {object} [options]
  105. * @param {string} [options.type=attachment]
  106. * @param {string|boolean} [options.fallback=true]
  107. * @return {string}
  108. * @api public
  109. */
  110. function contentDisposition (filename, options) {
  111. var opts = options || {}
  112. // get type
  113. var type = opts.type || 'attachment'
  114. // get parameters
  115. var params = createparams(filename, opts.fallback)
  116. // format into string
  117. return format(new ContentDisposition(type, params))
  118. }
  119. /**
  120. * Create parameters object from filename and fallback.
  121. *
  122. * @param {string} [filename]
  123. * @param {string|boolean} [fallback=true]
  124. * @return {object}
  125. * @api private
  126. */
  127. function createparams (filename, fallback) {
  128. if (filename === undefined) {
  129. return
  130. }
  131. var params = {}
  132. if (typeof filename !== 'string') {
  133. throw new TypeError('filename must be a string')
  134. }
  135. // fallback defaults to true
  136. if (fallback === undefined) {
  137. fallback = true
  138. }
  139. if (typeof fallback !== 'string' && typeof fallback !== 'boolean') {
  140. throw new TypeError('fallback must be a string or boolean')
  141. }
  142. if (typeof fallback === 'string' && NON_LATIN1_REGEXP.test(fallback)) {
  143. throw new TypeError('fallback must be ISO-8859-1 string')
  144. }
  145. // restrict to file base name
  146. var name = basename(filename)
  147. // determine if name is suitable for quoted string
  148. var isQuotedString = TEXT_REGEXP.test(name)
  149. // generate fallback name
  150. var fallbackName = typeof fallback !== 'string'
  151. ? fallback && getlatin1(name)
  152. : basename(fallback)
  153. var hasFallback = typeof fallbackName === 'string' && fallbackName !== name
  154. // set extended filename parameter
  155. if (hasFallback || !isQuotedString || HEX_ESCAPE_REGEXP.test(name)) {
  156. params['filename*'] = name
  157. }
  158. // set filename parameter
  159. if (isQuotedString || hasFallback) {
  160. params.filename = hasFallback
  161. ? fallbackName
  162. : name
  163. }
  164. return params
  165. }
  166. /**
  167. * Format object to Content-Disposition header.
  168. *
  169. * @param {object} obj
  170. * @param {string} obj.type
  171. * @param {object} [obj.parameters]
  172. * @return {string}
  173. * @api private
  174. */
  175. function format (obj) {
  176. var parameters = obj.parameters
  177. var type = obj.type
  178. if (!type || typeof type !== 'string' || !TOKEN_REGEXP.test(type)) {
  179. throw new TypeError('invalid type')
  180. }
  181. // start with normalized type
  182. var string = String(type).toLowerCase()
  183. // append parameters
  184. if (parameters && typeof parameters === 'object') {
  185. var param
  186. var params = Object.keys(parameters).sort()
  187. for (var i = 0; i < params.length; i++) {
  188. param = params[i]
  189. var val = param.substr(-1) === '*'
  190. ? ustring(parameters[param])
  191. : qstring(parameters[param])
  192. string += '; ' + param + '=' + val
  193. }
  194. }
  195. return string
  196. }
  197. /**
  198. * Decode a RFC 6987 field value (gracefully).
  199. *
  200. * @param {string} str
  201. * @return {string}
  202. * @api private
  203. */
  204. function decodefield (str) {
  205. var match = EXT_VALUE_REGEXP.exec(str)
  206. if (!match) {
  207. throw new TypeError('invalid extended field value')
  208. }
  209. var charset = match[1].toLowerCase()
  210. var encoded = match[2]
  211. var value
  212. // to binary string
  213. var binary = encoded.replace(HEX_ESCAPE_REPLACE_REGEXP, pdecode)
  214. switch (charset) {
  215. case 'iso-8859-1':
  216. value = getlatin1(binary)
  217. break
  218. case 'utf-8':
  219. value = new Buffer(binary, 'binary').toString('utf8')
  220. break
  221. default:
  222. throw new TypeError('unsupported charset in extended field')
  223. }
  224. return value
  225. }
  226. /**
  227. * Get ISO-8859-1 version of string.
  228. *
  229. * @param {string} val
  230. * @return {string}
  231. * @api private
  232. */
  233. function getlatin1 (val) {
  234. // simple Unicode -> ISO-8859-1 transformation
  235. return String(val).replace(NON_LATIN1_REGEXP, '?')
  236. }
  237. /**
  238. * Parse Content-Disposition header string.
  239. *
  240. * @param {string} string
  241. * @return {object}
  242. * @api private
  243. */
  244. function parse (string) {
  245. if (!string || typeof string !== 'string') {
  246. throw new TypeError('argument string is required')
  247. }
  248. var match = DISPOSITION_TYPE_REGEXP.exec(string)
  249. if (!match) {
  250. throw new TypeError('invalid type format')
  251. }
  252. // normalize type
  253. var index = match[0].length
  254. var type = match[1].toLowerCase()
  255. var key
  256. var names = []
  257. var params = {}
  258. var value
  259. // calculate index to start at
  260. index = PARAM_REGEXP.lastIndex = match[0].substr(-1) === ';'
  261. ? index - 1
  262. : index
  263. // match parameters
  264. while ((match = PARAM_REGEXP.exec(string))) {
  265. if (match.index !== index) {
  266. throw new TypeError('invalid parameter format')
  267. }
  268. index += match[0].length
  269. key = match[1].toLowerCase()
  270. value = match[2]
  271. if (names.indexOf(key) !== -1) {
  272. throw new TypeError('invalid duplicate parameter')
  273. }
  274. names.push(key)
  275. if (key.indexOf('*') + 1 === key.length) {
  276. // decode extended value
  277. key = key.slice(0, -1)
  278. value = decodefield(value)
  279. // overwrite existing value
  280. params[key] = value
  281. continue
  282. }
  283. if (typeof params[key] === 'string') {
  284. continue
  285. }
  286. if (value[0] === '"') {
  287. // remove quotes and escapes
  288. value = value
  289. .substr(1, value.length - 2)
  290. .replace(QESC_REGEXP, '$1')
  291. }
  292. params[key] = value
  293. }
  294. if (index !== -1 && index !== string.length) {
  295. throw new TypeError('invalid parameter format')
  296. }
  297. return new ContentDisposition(type, params)
  298. }
  299. /**
  300. * Percent decode a single character.
  301. *
  302. * @param {string} str
  303. * @param {string} hex
  304. * @return {string}
  305. * @api private
  306. */
  307. function pdecode (str, hex) {
  308. return String.fromCharCode(parseInt(hex, 16))
  309. }
  310. /**
  311. * Percent encode a single character.
  312. *
  313. * @param {string} char
  314. * @return {string}
  315. * @api private
  316. */
  317. function pencode (char) {
  318. var hex = String(char)
  319. .charCodeAt(0)
  320. .toString(16)
  321. .toUpperCase()
  322. return hex.length === 1
  323. ? '%0' + hex
  324. : '%' + hex
  325. }
  326. /**
  327. * Quote a string for HTTP.
  328. *
  329. * @param {string} val
  330. * @return {string}
  331. * @api private
  332. */
  333. function qstring (val) {
  334. var str = String(val)
  335. return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'
  336. }
  337. /**
  338. * Encode a Unicode string for HTTP (RFC 5987).
  339. *
  340. * @param {string} val
  341. * @return {string}
  342. * @api private
  343. */
  344. function ustring (val) {
  345. var str = String(val)
  346. // percent encode as UTF-8
  347. var encoded = encodeURIComponent(str)
  348. .replace(ENCODE_URL_ATTR_CHAR_REGEXP, pencode)
  349. return 'UTF-8\'\'' + encoded
  350. }
  351. /**
  352. * Class for parsed Content-Disposition header for v8 optimization
  353. */
  354. function ContentDisposition (type, parameters) {
  355. this.type = type
  356. this.parameters = parameters
  357. }