jshttp / content-disposition

Create and parse HTTP Content-Disposition header
MIT License
224 stars 43 forks source link

Extract filenames from RFC 2047 headers #16

Closed tusbar closed 7 years ago

tusbar commented 7 years ago

Handle both Q(uoted) and B(ase64) encodings.

This allows to extract filenames from headers such as attachment; filename="=?utf-8?B?TGVzIGPDomJsZXMgb3B0aXF1ZXMuemlw?=" that are returned today by Azure Storage.

tusbar commented 7 years ago

0.6 build breaks, and the log (>4MB) is useless. But who uses node 0.6.

dougwilson commented 7 years ago

Hi @tusbar thanks for the pull request! Unfortunately I do not think this is the correct way to approach this use-case. You see, what Azure is doing is encoding the file name as RFC 2047 and then taking that string and setting it as the file name in the Content-Disposition header. To Content-Disposition parsers (like this module) the file name itself is opaque; just like this would not automatically un-base64-encode the filename just because it happened to be a base64 string as the filename field.

Instead, you need to first parse the header, then given the knowledge that you are getting a response from Azure and they pass a RFC 2047 string as the file name, decode the filename.

Here is an example module:

var contentDisposition = require('content-disposition')

module.exports = function getFilename (header) {
  return rfc2047decode(contentDisposition.parse(header).parameters.filename)
}
tusbar commented 7 years ago

Hi @dougwilson, yeah, that makes sense. At least it made me understand how to deal with that particular case. Thanks.