index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*!
  2. * content-disposition
  3. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * Module exports.
  9. * @public
  10. */
  11. module.exports = contentDisposition
  12. module.exports.parse = parse
  13. /**
  14. * TextDecoder instance for UTF-8 decoding when decodeURIComponent fails due to invalid byte sequences.
  15. * @type {TextDecoder}
  16. * @private
  17. */
  18. const utf8Decoder = new TextDecoder('utf-8')
  19. /**
  20. * RegExp to match non attr-char, *after* encodeURIComponent (i.e. not including "%")
  21. * @private
  22. */
  23. var ENCODE_URL_ATTR_CHAR_REGEXP = /[\x00-\x20"'()*,/:;<=>?@[\\\]{}\x7f]/g // eslint-disable-line no-control-regex
  24. /**
  25. * RegExp to match non-latin1 characters.
  26. * @private
  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. * @private
  35. */
  36. var QESC_REGEXP = /\\([\u0000-\u007f])/g // eslint-disable-line no-control-regex
  37. /**
  38. * RegExp to match chars that must be quoted-pair in RFC 2616
  39. * @private
  40. */
  41. var QUOTE_REGEXP = /([\\"])/g
  42. /**
  43. * RegExp for various RFC 2616 grammar
  44. *
  45. * parameter = token "=" ( token | quoted-string )
  46. * token = 1*<any CHAR except CTLs or separators>
  47. * separators = "(" | ")" | "<" | ">" | "@"
  48. * | "," | ";" | ":" | "\" | <">
  49. * | "/" | "[" | "]" | "?" | "="
  50. * | "{" | "}" | SP | HT
  51. * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
  52. * qdtext = <any TEXT except <">>
  53. * quoted-pair = "\" CHAR
  54. * CHAR = <any US-ASCII character (octets 0 - 127)>
  55. * TEXT = <any OCTET except CTLs, but including LWS>
  56. * LWS = [CRLF] 1*( SP | HT )
  57. * CRLF = CR LF
  58. * CR = <US-ASCII CR, carriage return (13)>
  59. * LF = <US-ASCII LF, linefeed (10)>
  60. * SP = <US-ASCII SP, space (32)>
  61. * HT = <US-ASCII HT, horizontal-tab (9)>
  62. * CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
  63. * OCTET = <any 8-bit sequence of data>
  64. * @private
  65. */
  66. 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
  67. var TEXT_REGEXP = /^[\x20-\x7e\x80-\xff]+$/
  68. var TOKEN_REGEXP = /^[!#$%&'*+.0-9A-Z^_`a-z|~-]+$/
  69. /**
  70. * RegExp for various RFC 5987 grammar
  71. *
  72. * ext-value = charset "'" [ language ] "'" value-chars
  73. * charset = "UTF-8" / "ISO-8859-1" / mime-charset
  74. * mime-charset = 1*mime-charsetc
  75. * mime-charsetc = ALPHA / DIGIT
  76. * / "!" / "#" / "$" / "%" / "&"
  77. * / "+" / "-" / "^" / "_" / "`"
  78. * / "{" / "}" / "~"
  79. * language = ( 2*3ALPHA [ extlang ] )
  80. * / 4ALPHA
  81. * / 5*8ALPHA
  82. * extlang = *3( "-" 3ALPHA )
  83. * value-chars = *( pct-encoded / attr-char )
  84. * pct-encoded = "%" HEXDIG HEXDIG
  85. * attr-char = ALPHA / DIGIT
  86. * / "!" / "#" / "$" / "&" / "+" / "-" / "."
  87. * / "^" / "_" / "`" / "|" / "~"
  88. * @private
  89. */
  90. 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!#$&+.^_`|~-])+)$/
  91. /**
  92. * RegExp for various RFC 6266 grammar
  93. *
  94. * disposition-type = "inline" | "attachment" | disp-ext-type
  95. * disp-ext-type = token
  96. * disposition-parm = filename-parm | disp-ext-parm
  97. * filename-parm = "filename" "=" value
  98. * | "filename*" "=" ext-value
  99. * disp-ext-parm = token "=" value
  100. * | ext-token "=" ext-value
  101. * ext-token = <the characters in token, followed by "*">
  102. * @private
  103. */
  104. var DISPOSITION_TYPE_REGEXP = /^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/ // eslint-disable-line no-control-regex
  105. /**
  106. * Create an attachment Content-Disposition header.
  107. *
  108. * @param {string} [filename]
  109. * @param {object} [options]
  110. * @param {string} [options.type=attachment]
  111. * @param {string|boolean} [options.fallback=true]
  112. * @return {string}
  113. * @public
  114. */
  115. function contentDisposition (filename, options) {
  116. var opts = options || {}
  117. // get type
  118. var type = opts.type || 'attachment'
  119. // get parameters
  120. var params = createparams(filename, opts.fallback)
  121. // format into string
  122. return format(new ContentDisposition(type, params))
  123. }
  124. /**
  125. * Create parameters object from filename and fallback.
  126. *
  127. * @param {string} [filename]
  128. * @param {string|boolean} [fallback=true]
  129. * @return {object}
  130. * @private
  131. */
  132. function createparams (filename, fallback) {
  133. if (filename === undefined) {
  134. return
  135. }
  136. var params = {}
  137. if (typeof filename !== 'string') {
  138. throw new TypeError('filename must be a string')
  139. }
  140. // fallback defaults to true
  141. if (fallback === undefined) {
  142. fallback = true
  143. }
  144. if (typeof fallback !== 'string' && typeof fallback !== 'boolean') {
  145. throw new TypeError('fallback must be a string or boolean')
  146. }
  147. if (typeof fallback === 'string' && NON_LATIN1_REGEXP.test(fallback)) {
  148. throw new TypeError('fallback must be ISO-8859-1 string')
  149. }
  150. // restrict to file base name
  151. var name = basename(filename)
  152. // determine if name is suitable for quoted string
  153. var isQuotedString = TEXT_REGEXP.test(name)
  154. // generate fallback name
  155. var fallbackName = typeof fallback !== 'string'
  156. ? fallback && getlatin1(name)
  157. : basename(fallback)
  158. var hasFallback = typeof fallbackName === 'string' && fallbackName !== name
  159. // set extended filename parameter
  160. if (hasFallback || !isQuotedString || hasHexEscape(name)) {
  161. params['filename*'] = name
  162. }
  163. // set filename parameter
  164. if (isQuotedString || hasFallback) {
  165. params.filename = hasFallback
  166. ? fallbackName
  167. : name
  168. }
  169. return params
  170. }
  171. /**
  172. * Format object to Content-Disposition header.
  173. *
  174. * @param {object} obj
  175. * @param {string} obj.type
  176. * @param {object} [obj.parameters]
  177. * @return {string}
  178. * @private
  179. */
  180. function format (obj) {
  181. var parameters = obj.parameters
  182. var type = obj.type
  183. if (!type || typeof type !== 'string' || !TOKEN_REGEXP.test(type)) {
  184. throw new TypeError('invalid type')
  185. }
  186. // start with normalized type
  187. var string = String(type).toLowerCase()
  188. // append parameters
  189. if (parameters && typeof parameters === 'object') {
  190. var param
  191. var params = Object.keys(parameters).sort()
  192. for (var i = 0; i < params.length; i++) {
  193. param = params[i]
  194. var val = param.slice(-1) === '*'
  195. ? ustring(parameters[param])
  196. : qstring(parameters[param])
  197. string += '; ' + param + '=' + val
  198. }
  199. }
  200. return string
  201. }
  202. /**
  203. * Decode a RFC 5987 field value (gracefully).
  204. *
  205. * @param {string} str
  206. * @return {string}
  207. * @private
  208. */
  209. function decodefield (str) {
  210. const match = EXT_VALUE_REGEXP.exec(str)
  211. if (!match) {
  212. throw new TypeError('invalid extended field value')
  213. }
  214. const charset = match[1].toLowerCase()
  215. const encoded = match[2]
  216. switch (charset) {
  217. case 'iso-8859-1':
  218. {
  219. const binary = decodeHexEscapes(encoded)
  220. return getlatin1(binary)
  221. }
  222. case 'utf-8':
  223. case 'utf8':
  224. {
  225. try {
  226. return decodeURIComponent(encoded)
  227. } catch {
  228. // Failed to decode with decodeURIComponent, fallback to lenient decoding which replaces invalid UTF-8 byte sequences with the Unicode replacement character
  229. // TODO: Consider removing in the next major version to be more strict about invalid percent-encodings
  230. const binary = decodeHexEscapes(encoded)
  231. const bytes = new Uint8Array(binary.length)
  232. for (let idx = 0; idx < binary.length; idx++) {
  233. bytes[idx] = binary.charCodeAt(idx)
  234. }
  235. return utf8Decoder.decode(bytes)
  236. }
  237. }
  238. }
  239. throw new TypeError('unsupported charset in extended field')
  240. }
  241. /**
  242. * Get ISO-8859-1 version of string.
  243. *
  244. * @param {string} val
  245. * @return {string}
  246. * @private
  247. */
  248. function getlatin1 (val) {
  249. // simple Unicode -> ISO-8859-1 transformation
  250. return String(val).replace(NON_LATIN1_REGEXP, '?')
  251. }
  252. /**
  253. * Parse Content-Disposition header string.
  254. *
  255. * @param {string} string
  256. * @return {object}
  257. * @public
  258. */
  259. function parse (string) {
  260. if (!string || typeof string !== 'string') {
  261. throw new TypeError('argument string is required')
  262. }
  263. var match = DISPOSITION_TYPE_REGEXP.exec(string)
  264. if (!match) {
  265. throw new TypeError('invalid type format')
  266. }
  267. // normalize type
  268. var index = match[0].length
  269. var type = match[1].toLowerCase()
  270. var key
  271. var names = []
  272. var params = {}
  273. var value
  274. // calculate index to start at
  275. index = PARAM_REGEXP.lastIndex = match[0].slice(-1) === ';'
  276. ? index - 1
  277. : index
  278. // match parameters
  279. while ((match = PARAM_REGEXP.exec(string))) {
  280. if (match.index !== index) {
  281. throw new TypeError('invalid parameter format')
  282. }
  283. index += match[0].length
  284. key = match[1].toLowerCase()
  285. value = match[2]
  286. if (names.indexOf(key) !== -1) {
  287. throw new TypeError('invalid duplicate parameter')
  288. }
  289. names.push(key)
  290. if (key.indexOf('*') + 1 === key.length) {
  291. // decode extended value
  292. key = key.slice(0, -1)
  293. value = decodefield(value)
  294. // overwrite existing value
  295. params[key] = value
  296. continue
  297. }
  298. if (typeof params[key] === 'string') {
  299. continue
  300. }
  301. if (value[0] === '"') {
  302. // remove quotes and escapes
  303. value = value
  304. .slice(1, -1)
  305. .replace(QESC_REGEXP, '$1')
  306. }
  307. params[key] = value
  308. }
  309. if (index !== -1 && index !== string.length) {
  310. throw new TypeError('invalid parameter format')
  311. }
  312. return new ContentDisposition(type, params)
  313. }
  314. /**
  315. * Percent encode a single character.
  316. *
  317. * @param {string} char
  318. * @return {string}
  319. * @private
  320. */
  321. function pencode (char) {
  322. return '%' + String(char)
  323. .charCodeAt(0)
  324. .toString(16)
  325. .toUpperCase()
  326. }
  327. /**
  328. * Quote a string for HTTP.
  329. *
  330. * @param {string} val
  331. * @return {string}
  332. * @private
  333. */
  334. function qstring (val) {
  335. var str = String(val)
  336. return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'
  337. }
  338. /**
  339. * Encode a Unicode string for HTTP (RFC 5987).
  340. *
  341. * @param {string} val
  342. * @return {string}
  343. * @private
  344. */
  345. function ustring (val) {
  346. var str = String(val)
  347. // percent encode as UTF-8
  348. var encoded = encodeURIComponent(str)
  349. .replace(ENCODE_URL_ATTR_CHAR_REGEXP, pencode)
  350. return 'UTF-8\'\'' + encoded
  351. }
  352. /**
  353. * Class for parsed Content-Disposition header for v8 optimization
  354. *
  355. * @public
  356. * @param {string} type
  357. * @param {object} parameters
  358. * @constructor
  359. */
  360. function ContentDisposition (type, parameters) {
  361. this.type = type
  362. this.parameters = parameters
  363. }
  364. /**
  365. * Return the last portion of a path
  366. *
  367. * @param {string} path
  368. * @returns {string}
  369. */
  370. function basename (path) {
  371. const normalized = path.replaceAll('\\', '/')
  372. let end = normalized.length
  373. while (end > 0 && normalized[end - 1] === '/') {
  374. end--
  375. }
  376. if (end === 0) {
  377. return ''
  378. }
  379. let start = end - 1
  380. while (start >= 0 && normalized[start] !== '/') {
  381. start--
  382. }
  383. return normalized.slice(start + 1, end)
  384. }
  385. /**
  386. * Check if a character is a hex digit [0-9A-Fa-f]
  387. *
  388. * @param {string} char
  389. * @return {boolean}
  390. * @private
  391. */
  392. function isHexDigit (char) {
  393. const code = char.charCodeAt(0)
  394. return (
  395. (code >= 48 && code <= 57) || // 0-9
  396. (code >= 65 && code <= 70) || // A-F
  397. (code >= 97 && code <= 102) // a-f
  398. )
  399. }
  400. /**
  401. * Check if a string contains percent encoding escapes.
  402. *
  403. * @param {string} str
  404. * @return {boolean}
  405. * @private
  406. */
  407. function hasHexEscape (str) {
  408. const maxIndex = str.length - 3
  409. let lastIndex = -1
  410. while ((lastIndex = str.indexOf('%', lastIndex + 1)) !== -1 && lastIndex <= maxIndex) {
  411. if (isHexDigit(str[lastIndex + 1]) && isHexDigit(str[lastIndex + 2])) {
  412. return true
  413. }
  414. }
  415. return false
  416. }
  417. /**
  418. * Decode hex escapes in a string (e.g., %20 -> space)
  419. *
  420. * @param {string} str
  421. * @return {string}
  422. * @private
  423. */
  424. function decodeHexEscapes (str) {
  425. const firstEscape = str.indexOf('%')
  426. if (firstEscape === -1) return str
  427. let result = str.slice(0, firstEscape)
  428. for (let idx = firstEscape; idx < str.length; idx++) {
  429. if (
  430. str[idx] === '%' &&
  431. idx + 2 < str.length &&
  432. isHexDigit(str[idx + 1]) &&
  433. isHexDigit(str[idx + 2])
  434. ) {
  435. result += String.fromCharCode(Number.parseInt(str[idx + 1] + str[idx + 2], 16))
  436. idx += 2
  437. } else {
  438. result += str[idx]
  439. }
  440. }
  441. return result
  442. }