|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
-
-
- 'use strict'
-
-
-
- module.exports = contentDisposition
- module.exports.parse = parse
-
-
-
- var basename = require('path').basename
-
-
-
- var ENCODE_URL_ATTR_CHAR_REGEXP = /[\x00-\x20"'()*,/:;<=>?@[\\\]{}\x7f]/g
-
-
-
- var HEX_ESCAPE_REGEXP = /%[0-9A-Fa-f]{2}/
- var HEX_ESCAPE_REPLACE_REGEXP = /%([0-9A-Fa-f]{2})/g
-
-
-
- var NON_LATIN1_REGEXP = /[^\x20-\x7e\xa0-\xff]/g
-
-
-
- var QESC_REGEXP = /\\([\u0000-\u007f])/g
-
-
-
- var QUOTE_REGEXP = /([\\"])/g
-
-
-
- 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
- var TEXT_REGEXP = /^[\x20-\x7e\x80-\xff]+$/
- var TOKEN_REGEXP = /^[!#$%&'*+.0-9A-Z^_`a-z|~-]+$/
-
-
-
- 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!#$&+.^_`|~-])+)$/
-
-
-
- var DISPOSITION_TYPE_REGEXP = /^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/
-
-
-
- function contentDisposition (filename, options) {
- var opts = options || {}
-
-
- var type = opts.type || 'attachment'
-
-
- var params = createparams(filename, opts.fallback)
-
-
- return format(new ContentDisposition(type, params))
- }
-
-
-
- function createparams (filename, fallback) {
- if (filename === undefined) {
- return
- }
-
- var params = {}
-
- if (typeof filename !== 'string') {
- throw new TypeError('filename must be a string')
- }
-
-
- if (fallback === undefined) {
- fallback = true
- }
-
- if (typeof fallback !== 'string' && typeof fallback !== 'boolean') {
- throw new TypeError('fallback must be a string or boolean')
- }
-
- if (typeof fallback === 'string' && NON_LATIN1_REGEXP.test(fallback)) {
- throw new TypeError('fallback must be ISO-8859-1 string')
- }
-
-
- var name = basename(filename)
-
-
- var isQuotedString = TEXT_REGEXP.test(name)
-
-
- var fallbackName = typeof fallback !== 'string'
- ? fallback && getlatin1(name)
- : basename(fallback)
- var hasFallback = typeof fallbackName === 'string' && fallbackName !== name
-
-
- if (hasFallback || !isQuotedString || HEX_ESCAPE_REGEXP.test(name)) {
- params['filename*'] = name
- }
-
-
- if (isQuotedString || hasFallback) {
- params.filename = hasFallback
- ? fallbackName
- : name
- }
-
- return params
- }
-
-
-
- function format (obj) {
- var parameters = obj.parameters
- var type = obj.type
-
- if (!type || typeof type !== 'string' || !TOKEN_REGEXP.test(type)) {
- throw new TypeError('invalid type')
- }
-
-
- var string = String(type).toLowerCase()
-
-
- if (parameters && typeof parameters === 'object') {
- var param
- var params = Object.keys(parameters).sort()
-
- for (var i = 0; i < params.length; i++) {
- param = params[i]
-
- var val = param.substr(-1) === '*'
- ? ustring(parameters[param])
- : qstring(parameters[param])
-
- string += '; ' + param + '=' + val
- }
- }
-
- return string
- }
-
-
-
- function decodefield (str) {
- var match = EXT_VALUE_REGEXP.exec(str)
-
- if (!match) {
- throw new TypeError('invalid extended field value')
- }
-
- var charset = match[1].toLowerCase()
- var encoded = match[2]
- var value
-
-
- var binary = encoded.replace(HEX_ESCAPE_REPLACE_REGEXP, pdecode)
-
- switch (charset) {
- case 'iso-8859-1':
- value = getlatin1(binary)
- break
- case 'utf-8':
- value = new Buffer(binary, 'binary').toString('utf8')
- break
- default:
- throw new TypeError('unsupported charset in extended field')
- }
-
- return value
- }
-
-
-
- function getlatin1 (val) {
-
- return String(val).replace(NON_LATIN1_REGEXP, '?')
- }
-
-
-
- function parse (string) {
- if (!string || typeof string !== 'string') {
- throw new TypeError('argument string is required')
- }
-
- var match = DISPOSITION_TYPE_REGEXP.exec(string)
-
- if (!match) {
- throw new TypeError('invalid type format')
- }
-
-
- var index = match[0].length
- var type = match[1].toLowerCase()
-
- var key
- var names = []
- var params = {}
- var value
-
-
- index = PARAM_REGEXP.lastIndex = match[0].substr(-1) === ';'
- ? index - 1
- : index
-
-
- while ((match = PARAM_REGEXP.exec(string))) {
- if (match.index !== index) {
- throw new TypeError('invalid parameter format')
- }
-
- index += match[0].length
- key = match[1].toLowerCase()
- value = match[2]
-
- if (names.indexOf(key) !== -1) {
- throw new TypeError('invalid duplicate parameter')
- }
-
- names.push(key)
-
- if (key.indexOf('*') + 1 === key.length) {
-
- key = key.slice(0, -1)
- value = decodefield(value)
-
-
- params[key] = value
- continue
- }
-
- if (typeof params[key] === 'string') {
- continue
- }
-
- if (value[0] === '"') {
-
- value = value
- .substr(1, value.length - 2)
- .replace(QESC_REGEXP, '$1')
- }
-
- params[key] = value
- }
-
- if (index !== -1 && index !== string.length) {
- throw new TypeError('invalid parameter format')
- }
-
- return new ContentDisposition(type, params)
- }
-
-
-
- function pdecode (str, hex) {
- return String.fromCharCode(parseInt(hex, 16))
- }
-
-
-
- function pencode (char) {
- var hex = String(char)
- .charCodeAt(0)
- .toString(16)
- .toUpperCase()
- return hex.length === 1
- ? '%0' + hex
- : '%' + hex
- }
-
-
-
- function qstring (val) {
- var str = String(val)
-
- return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'
- }
-
-
-
- function ustring (val) {
- var str = String(val)
-
-
- var encoded = encodeURIComponent(str)
- .replace(ENCODE_URL_ATTR_CHAR_REGEXP, pencode)
-
- return 'UTF-8\'\'' + encoded
- }
-
-
-
- function ContentDisposition (type, parameters) {
- this.type = type
- this.parameters = parameters
- }
|