-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5ab4436
Showing
8 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
node_modules/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
language: node_js | ||
node_js: | ||
- "0.6" | ||
- "0.8" | ||
- "0.10" | ||
- "0.11" | ||
matrix: | ||
allow_failures: | ||
- node_js: "0.11" | ||
fast_finish: true | ||
script: | ||
- "test $TRAVIS_NODE_VERSION != '0.6' || npm test" | ||
- "test $TRAVIS_NODE_VERSION = '0.6' || npm run-script test-travis" | ||
after_script: | ||
- "test $TRAVIS_NODE_VERSION = '0.10' && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.0.0 / 2014-09-18 | ||
================== | ||
|
||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2014 Douglas Christopher Wilson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# content-disposition | ||
|
||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
[![Node.js Version][node-version-image]][node-version-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Test Coverage][coveralls-image]][coveralls-url] | ||
|
||
Create an attachment Content-Disposition header | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install content-disposition | ||
``` | ||
|
||
## API | ||
|
||
```js | ||
var contentDisposition = require('content-disposition') | ||
``` | ||
|
||
### contentDisposition(filename) | ||
|
||
Create an attachment `Content-Disposition` header value using the given file name. | ||
|
||
```js | ||
res.setHeader('Content-Disposition', contentDisposition('∫ maths.pdf')) | ||
``` | ||
|
||
## Testing | ||
|
||
```sh | ||
$ npm test | ||
``` | ||
|
||
## References | ||
|
||
- [RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1][rfc-2616] | ||
- [RFC 5987: Character Set and Language Encoding for Hypertext Transfer Protocol (HTTP) Header Field Parameters][rfc-5987] | ||
- [RFC 6266: Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)][rfc-6266] | ||
- [Test Cases for HTTP Content-Disposition header field (RFC 6266) and the Encodings defined in RFCs 2047, 2231 and 5987][tc-2231] | ||
|
||
[rfc-2616]: https://tools.ietf.org/html/rfc2616 | ||
[rfc-5987]: https://tools.ietf.org/html/rfc5987 | ||
[rfc-6266]: https://tools.ietf.org/html/rfc6266 | ||
[tc-2231]: http://greenbytes.de/tech/tc2231/ | ||
|
||
## License | ||
|
||
[MIT](LICENSE) | ||
|
||
[npm-image]: https://img.shields.io/npm/v/content-disposition.svg?style=flat | ||
[npm-url]: https://npmjs.org/package/content-disposition | ||
[node-version-image]: https://img.shields.io/node/v/content-disposition.svg?style=flat | ||
[node-version-url]: http://nodejs.org/download/ | ||
[travis-image]: https://img.shields.io/travis/jshttp/content-disposition.svg?style=flat | ||
[travis-url]: https://travis-ci.org/jshttp/content-disposition | ||
[coveralls-image]: https://img.shields.io/coveralls/jshttp/content-disposition.svg?style=flat | ||
[coveralls-url]: https://coveralls.io/r/jshttp/content-disposition?branch=master | ||
[downloads-image]: https://img.shields.io/npm/dm/content-disposition.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/content-disposition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/*! | ||
* content-disposition | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module exports. | ||
*/ | ||
|
||
module.exports = contentDisposition | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var basename = require('path').basename | ||
|
||
/** | ||
* RegExp to match non attr-char not handled by encodeURI. | ||
*/ | ||
|
||
var encodeUriAttrCharRegExp = /[*']/g | ||
|
||
/** | ||
* RegExp to match non-US-ASCII characters. | ||
*/ | ||
|
||
var nonAsciiRegExp = /[^\x00-\x7f]/g | ||
|
||
/** | ||
* RegExp to match chars that must be quoted-pair in RFC 2616 | ||
*/ | ||
|
||
var quoteRegExp = /([\\"])/g | ||
|
||
/** | ||
* RegExp for various RFC 2616 grammar | ||
* | ||
* TEXT = <any OCTET except CTLs, but including LWS> | ||
*/ | ||
|
||
var textRegExp = /^[\u0020-\u007e\u0080-\u00ff]+$/ | ||
|
||
/** | ||
* Create an attachment Content-Disposition header. | ||
* | ||
* @param {string} filename | ||
* @return {string} | ||
* @api public | ||
*/ | ||
|
||
function contentDisposition(filename) { | ||
if (typeof filename !== 'string') { | ||
throw new TypeError('argument filename is required') | ||
} | ||
|
||
// restrict to file base name | ||
var name = basename(filename) | ||
|
||
if (!nonAsciiRegExp.test(name)) { | ||
// simple header | ||
// file name is always quoted and not a token for RFC 2616 compatibility | ||
return 'attachment; filename=' + qstring(name) | ||
} | ||
|
||
// simple Unicode -> US-ASCII transliteration | ||
var asciiFilename = name.replace(nonAsciiRegExp, '?') | ||
|
||
return 'attachment; filename=' + qstring(asciiFilename) | ||
+ '; filename*=' + ustring(name) | ||
} | ||
|
||
/** | ||
* Percent encode a single character. | ||
* | ||
* @param {string} char | ||
* @return {string} | ||
* @api private | ||
*/ | ||
|
||
function pencode(char) { | ||
var hex = String(char) | ||
.charCodeAt(0) | ||
.toString(16) | ||
.toUpperCase() | ||
return hex.length === 1 | ||
? '%0' + hex | ||
: '%' + hex | ||
} | ||
|
||
/** | ||
* Quote a string for HTTP. | ||
* | ||
* @param {string} val | ||
* @return {string} | ||
* @api private | ||
*/ | ||
|
||
function qstring(val) { | ||
var str = String(val) | ||
|
||
if (str.length > 0 && !textRegExp.test(str)) { | ||
throw new TypeError('invalid quoted string value') | ||
} | ||
|
||
return '"' + str.replace(quoteRegExp, '\\$1') + '"' | ||
} | ||
|
||
/** | ||
* Encode a Unicode string for HTTP (RFC 5987). | ||
* | ||
* @param {string} val | ||
* @return {string} | ||
* @api private | ||
*/ | ||
|
||
function ustring(val) { | ||
var str = String(val) | ||
|
||
// percent encode as UTF-8 | ||
var encoded = encodeURI(str) | ||
.replace(encodeUriAttrCharRegExp, pencode) | ||
|
||
return 'UTF-8\'\'' + encoded | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "content-disposition", | ||
"description": "Create an attachment Content-Disposition header", | ||
"version": "0.0.0", | ||
"contributors": [ | ||
"Douglas Christopher Wilson <[email protected]>" | ||
], | ||
"license": "MIT", | ||
"keywords": [ | ||
"content-disposition", | ||
"http", | ||
"rfc6266", | ||
"res" | ||
], | ||
"repository": "jshttp/content-disposition", | ||
"devDependencies": { | ||
"istanbul": "0.3.2", | ||
"mocha": "~1.21.4" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"HISTORY.md", | ||
"README.md", | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dougwilson
Author
Contributor
|
||
"index.js" | ||
], | ||
"engines": { | ||
"node": ">= 0.6" | ||
}, | ||
"scripts": { | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
var assert = require('assert') | ||
var contentDisposition = require('..') | ||
|
||
describe('contentDisposition(filename)', function () { | ||
it('should require a filename', function () { | ||
assert.throws(contentDisposition.bind(), /argument filename is required/) | ||
}) | ||
|
||
it('should create a header', function () { | ||
assert.equal(contentDisposition('plans.pdf'), 'attachment; filename="plans.pdf"') | ||
}) | ||
|
||
it('should use the basename of the string', function () { | ||
assert.equal(contentDisposition('/path/to/plans.pdf'), 'attachment; filename="plans.pdf"') | ||
}) | ||
|
||
it('should not accept filename with NULLs', function () { | ||
assert.throws(contentDisposition.bind(null, 'plans\u0000.pdf'), /invalid.*value/) | ||
}) | ||
|
||
describe('when "filename" is US-ASCII', function () { | ||
it('should only include filename parameter', function () { | ||
assert.equal(contentDisposition('plans.pdf'), 'attachment; filename="plans.pdf"') | ||
}) | ||
|
||
it('should escape quotes', function () { | ||
assert.equal(contentDisposition('the "plans".pdf'), 'attachment; filename="the \\"plans\\".pdf"') | ||
}) | ||
}) | ||
|
||
describe('when "filename" is Unicode', function () { | ||
it('should include filename* parameter', function () { | ||
assert.equal(contentDisposition('планы.pdf'), 'attachment; filename="?????.pdf"; filename*=UTF-8\'\'%D0%BF%D0%BB%D0%B0%D0%BD%D1%8B.pdf') | ||
}) | ||
|
||
it('should encode special characters', function () { | ||
assert.equal(contentDisposition('«\'*%».pdf'), 'attachment; filename="?\'*%?.pdf"; filename*=UTF-8\'\'%C2%AB%27%2A%25%C2%BB.pdf') | ||
}) | ||
}) | ||
}) |
@dougwilson is or isn't this auto-included?