pax_global_header00006660000000000000000000000064131556005340014514gustar00rootroot0000000000000052 comment=d22f8ac6c407789c906bd6fed137efde8f772b09 content-type-1.0.4/000077500000000000000000000000001315560053400141475ustar00rootroot00000000000000content-type-1.0.4/.eslintignore000066400000000000000000000000261315560053400166500ustar00rootroot00000000000000coverage node_modules content-type-1.0.4/.eslintrc000066400000000000000000000001171315560053400157720ustar00rootroot00000000000000{ "extends": "standard", "rules": { "no-param-reassign": "error" } } content-type-1.0.4/.gitignore000066400000000000000000000000701315560053400161340ustar00rootroot00000000000000coverage/ node_modules/ npm-debug.log package-lock.json content-type-1.0.4/.travis.yml000066400000000000000000000021221315560053400162550ustar00rootroot00000000000000language: node_js node_js: - "0.6" - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.8" - "5.12" - "6.11" - "7.10" - "8.4" sudo: false dist: precise cache: directories: - node_modules before_install: # Skip updating shrinkwrap / lock - "npm config set shrinkwrap false" # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul" - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" - "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)" # Update Node.js modules - "test ! -d node_modules || npm prune" - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on istanbul install - "test ! -z $(npm -ps ls istanbul) || npm test" - "test -z $(npm -ps ls istanbul) || npm run-script test-ci" - "test -z $(npm -ps ls eslint ) || npm run-script lint" after_script: - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" content-type-1.0.4/HISTORY.md000066400000000000000000000006641315560053400156400ustar00rootroot000000000000001.0.4 / 2017-09-11 ================== * perf: skip parameter parsing when no parameters 1.0.3 / 2017-09-10 ================== * perf: remove argument reassignment 1.0.2 / 2016-05-09 ================== * perf: enable strict mode 1.0.1 / 2015-02-13 ================== * Improve missing `Content-Type` header error message 1.0.0 / 2015-02-01 ================== * Initial implementation, derived from `media-typer@0.3.0` content-type-1.0.4/LICENSE000066400000000000000000000021011315560053400151460ustar00rootroot00000000000000(The MIT License) Copyright (c) 2015 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. content-type-1.0.4/README.md000066400000000000000000000053541315560053400154350ustar00rootroot00000000000000# content-type [![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 and parse HTTP Content-Type header according to RFC 7231 ## Installation ```sh $ npm install content-type ``` ## API ```js var contentType = require('content-type') ``` ### contentType.parse(string) ```js var obj = contentType.parse('image/svg+xml; charset=utf-8') ``` Parse a content type string. This will return an object with the following properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`): - `type`: The media type (the type and subtype, always lower case). Example: `'image/svg+xml'` - `parameters`: An object of the parameters in the media type (name of parameter always lower case). Example: `{charset: 'utf-8'}` Throws a `TypeError` if the string is missing or invalid. ### contentType.parse(req) ```js var obj = contentType.parse(req) ``` Parse the `content-type` header from the given `req`. Short-cut for `contentType.parse(req.headers['content-type'])`. Throws a `TypeError` if the `Content-Type` header is missing or invalid. ### contentType.parse(res) ```js var obj = contentType.parse(res) ``` Parse the `content-type` header set on the given `res`. Short-cut for `contentType.parse(res.getHeader('content-type'))`. Throws a `TypeError` if the `Content-Type` header is missing or invalid. ### contentType.format(obj) ```js var str = contentType.format({type: 'image/svg+xml'}) ``` Format an object into a content type string. This will return a string of the content type for the given object with the following properties (examples are shown that produce the string `'image/svg+xml; charset=utf-8'`): - `type`: The media type (will be lower-cased). Example: `'image/svg+xml'` - `parameters`: An object of the parameters in the media type (name of the parameter will be lower-cased). Example: `{charset: 'utf-8'}` Throws a `TypeError` if the object contains an invalid type or parameter names. ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/content-type.svg [npm-url]: https://npmjs.org/package/content-type [node-version-image]: https://img.shields.io/node/v/content-type.svg [node-version-url]: http://nodejs.org/download/ [travis-image]: https://img.shields.io/travis/jshttp/content-type/master.svg [travis-url]: https://travis-ci.org/jshttp/content-type [coveralls-image]: https://img.shields.io/coveralls/jshttp/content-type/master.svg [coveralls-url]: https://coveralls.io/r/jshttp/content-type [downloads-image]: https://img.shields.io/npm/dm/content-type.svg [downloads-url]: https://npmjs.org/package/content-type content-type-1.0.4/index.js000066400000000000000000000113111315560053400156110ustar00rootroot00000000000000/*! * content-type * Copyright(c) 2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1 * * parameter = token "=" ( token / quoted-string ) * token = 1*tchar * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" * / DIGIT / ALPHA * ; any VCHAR, except delimiters * quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE * qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text * obs-text = %x80-FF * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text ) */ var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/ var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/ /** * RegExp to match quoted-pair in RFC 7230 sec 3.2.6 * * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text ) * obs-text = %x80-FF */ var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g /** * RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6 */ var QUOTE_REGEXP = /([\\"])/g /** * RegExp to match type in RFC 7231 sec 3.1.1.1 * * media-type = type "/" subtype * type = token * subtype = token */ var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/ /** * Module exports. * @public */ exports.format = format exports.parse = parse /** * Format object to media type. * * @param {object} obj * @return {string} * @public */ function format (obj) { if (!obj || typeof obj !== 'object') { throw new TypeError('argument obj is required') } var parameters = obj.parameters var type = obj.type if (!type || !TYPE_REGEXP.test(type)) { throw new TypeError('invalid type') } var string = type // append parameters if (parameters && typeof parameters === 'object') { var param var params = Object.keys(parameters).sort() for (var i = 0; i < params.length; i++) { param = params[i] if (!TOKEN_REGEXP.test(param)) { throw new TypeError('invalid parameter name') } string += '; ' + param + '=' + qstring(parameters[param]) } } return string } /** * Parse media type to object. * * @param {string|object} string * @return {Object} * @public */ function parse (string) { if (!string) { throw new TypeError('argument string is required') } // support req/res-like objects as argument var header = typeof string === 'object' ? getcontenttype(string) : string if (typeof header !== 'string') { throw new TypeError('argument string is required to be a string') } var index = header.indexOf(';') var type = index !== -1 ? header.substr(0, index).trim() : header.trim() if (!TYPE_REGEXP.test(type)) { throw new TypeError('invalid media type') } var obj = new ContentType(type.toLowerCase()) // parse parameters if (index !== -1) { var key var match var value PARAM_REGEXP.lastIndex = index while ((match = PARAM_REGEXP.exec(header))) { if (match.index !== index) { throw new TypeError('invalid parameter format') } index += match[0].length key = match[1].toLowerCase() value = match[2] if (value[0] === '"') { // remove quotes and escapes value = value .substr(1, value.length - 2) .replace(QESC_REGEXP, '$1') } obj.parameters[key] = value } if (index !== header.length) { throw new TypeError('invalid parameter format') } } return obj } /** * Get content-type from req/res objects. * * @param {object} * @return {Object} * @private */ function getcontenttype (obj) { var header if (typeof obj.getHeader === 'function') { // res-like header = obj.getHeader('content-type') } else if (typeof obj.headers === 'object') { // req-like header = obj.headers && obj.headers['content-type'] } if (typeof header !== 'string') { throw new TypeError('content-type header is missing from object') } return header } /** * Quote a string if necessary. * * @param {string} val * @return {string} * @private */ function qstring (val) { var str = String(val) // no need to quote tokens if (TOKEN_REGEXP.test(str)) { return str } if (str.length > 0 && !TEXT_REGEXP.test(str)) { throw new TypeError('invalid parameter value') } return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"' } /** * Class to represent a content type. * @private */ function ContentType (type) { this.parameters = Object.create(null) this.type = type } content-type-1.0.4/package.json000066400000000000000000000020561315560053400164400ustar00rootroot00000000000000{ "name": "content-type", "description": "Create and parse HTTP Content-Type header", "version": "1.0.4", "author": "Douglas Christopher Wilson ", "license": "MIT", "keywords": [ "content-type", "http", "req", "res", "rfc7231" ], "repository": "jshttp/content-type", "devDependencies": { "eslint": "3.19.0", "eslint-config-standard": "10.2.1", "eslint-plugin-import": "2.7.0", "eslint-plugin-node": "5.1.1", "eslint-plugin-promise": "3.5.0", "eslint-plugin-standard": "3.0.1", "istanbul": "0.4.5", "mocha": "~1.21.5" }, "files": [ "LICENSE", "HISTORY.md", "README.md", "index.js" ], "engines": { "node": ">= 0.6" }, "scripts": { "lint": "eslint .", "test": "mocha --reporter spec --check-leaks --bail test/", "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/" } } content-type-1.0.4/test/000077500000000000000000000000001315560053400151265ustar00rootroot00000000000000content-type-1.0.4/test/.eslintrc000066400000000000000000000000451315560053400167510ustar00rootroot00000000000000{ "env": { "mocha": true } } content-type-1.0.4/test/contentType_format.js000066400000000000000000000047131315560053400213550ustar00rootroot00000000000000 var assert = require('assert') var contentType = require('..') describe('contentType.format(obj)', function () { it('should format basic type', function () { var str = contentType.format({type: 'text/html'}) assert.equal(str, 'text/html') }) it('should format type with suffix', function () { var str = contentType.format({type: 'image/svg+xml'}) assert.equal(str, 'image/svg+xml') }) it('should format type with parameter', function () { var str = contentType.format({ type: 'text/html', parameters: {charset: 'utf-8'} }) assert.equal(str, 'text/html; charset=utf-8') }) it('should format type with parameter that needs quotes', function () { var str = contentType.format({ type: 'text/html', parameters: {foo: 'bar or "baz"'} }) assert.equal(str, 'text/html; foo="bar or \\"baz\\""') }) it('should format type with parameter with empty value', function () { var str = contentType.format({ type: 'text/html', parameters: {foo: ''} }) assert.equal(str, 'text/html; foo=""') }) it('should format type with multiple parameters', function () { var str = contentType.format({ type: 'text/html', parameters: {charset: 'utf-8', foo: 'bar', bar: 'baz'} }) assert.equal(str, 'text/html; bar=baz; charset=utf-8; foo=bar') }) it('should require argument', function () { assert.throws(contentType.format.bind(null), /argument obj is required/) }) it('should reject non-objects', function () { assert.throws(contentType.format.bind(null, 7), /argument obj is required/) }) it('should require type', function () { var obj = {} assert.throws(contentType.format.bind(null, obj), /invalid type/) }) it('should reject invalid type', function () { var obj = {type: 'text/'} assert.throws(contentType.format.bind(null, obj), /invalid type/) }) it('should reject invalid type with LWS', function () { var obj = {type: ' text/html'} assert.throws(contentType.format.bind(null, obj), /invalid type/) }) it('should reject invalid parameter name', function () { var obj = {type: 'image/svg', parameters: {'foo/': 'bar'}} assert.throws(contentType.format.bind(null, obj), /invalid parameter name/) }) it('should reject invalid parameter value', function () { var obj = {type: 'image/svg', parameters: {'foo': 'bar\u0000'}} assert.throws(contentType.format.bind(null, obj), /invalid parameter value/) }) }) content-type-1.0.4/test/contentType_parse.js000066400000000000000000000106231315560053400211740ustar00rootroot00000000000000 var assert = require('assert') var contentType = require('..') var invalidTypes = [ ' ', 'null', 'undefined', '/', 'text / plain', 'text/;plain', 'text/"plain"', 'text/p£ain', 'text/(plain)', 'text/@plain', 'text/plain,wrong' ] describe('contentType.parse(string)', function () { it('should parse basic type', function () { var type = contentType.parse('text/html') assert.equal(type.type, 'text/html') }) it('should parse with suffix', function () { var type = contentType.parse('image/svg+xml') assert.equal(type.type, 'image/svg+xml') }) it('should parse basic type with surrounding OWS', function () { var type = contentType.parse(' text/html ') assert.equal(type.type, 'text/html') }) it('should parse parameters', function () { var type = contentType.parse('text/html; charset=utf-8; foo=bar') assert.equal(type.type, 'text/html') assert.deepEqual(type.parameters, { charset: 'utf-8', foo: 'bar' }) }) it('should parse parameters with extra LWS', function () { var type = contentType.parse('text/html ; charset=utf-8 ; foo=bar') assert.equal(type.type, 'text/html') assert.deepEqual(type.parameters, { charset: 'utf-8', foo: 'bar' }) }) it('should lower-case type', function () { var type = contentType.parse('IMAGE/SVG+XML') assert.equal(type.type, 'image/svg+xml') }) it('should lower-case parameter names', function () { var type = contentType.parse('text/html; Charset=UTF-8') assert.equal(type.type, 'text/html') assert.deepEqual(type.parameters, { charset: 'UTF-8' }) }) it('should unquote parameter values', function () { var type = contentType.parse('text/html; charset="UTF-8"') assert.equal(type.type, 'text/html') assert.deepEqual(type.parameters, { charset: 'UTF-8' }) }) it('should unquote parameter values with escapes', function () { var type = contentType.parse('text/html; charset = "UT\\F-\\\\\\"8\\""') assert.equal(type.type, 'text/html') assert.deepEqual(type.parameters, { charset: 'UTF-\\"8"' }) }) it('should handle balanced quotes', function () { var type = contentType.parse('text/html; param="charset=\\"utf-8\\"; foo=bar"; bar=foo') assert.equal(type.type, 'text/html') assert.deepEqual(type.parameters, { param: 'charset="utf-8"; foo=bar', bar: 'foo' }) }) invalidTypes.forEach(function (type) { it('should throw on invalid media type ' + type, function () { assert.throws(contentType.parse.bind(null, type), /invalid media type/) }) }) it('should throw on invalid parameter format', function () { assert.throws(contentType.parse.bind(null, 'text/plain; foo="bar'), /invalid parameter format/) assert.throws(contentType.parse.bind(null, 'text/plain; profile=http://localhost; foo=bar'), /invalid parameter format/) assert.throws(contentType.parse.bind(null, 'text/plain; profile=http://localhost'), /invalid parameter format/) }) it('should require argument', function () { assert.throws(contentType.parse.bind(null), /string.*required/) }) it('should reject non-strings', function () { assert.throws(contentType.parse.bind(null, 7), /string.*required/) }) }) describe('contentType.parse(req)', function () { it('should parse content-type header', function () { var req = {headers: {'content-type': 'text/html'}} var type = contentType.parse(req) assert.equal(type.type, 'text/html') }) it('should reject objects without headers property', function () { assert.throws(contentType.parse.bind(null, {}), /content-type header is missing/) }) it('should reject missing content-type', function () { var req = {headers: {}} assert.throws(contentType.parse.bind(null, req), /content-type header is missing/) }) }) describe('contentType.parse(res)', function () { it('should parse content-type header', function () { var res = {getHeader: function () { return 'text/html' }} var type = contentType.parse(res) assert.equal(type.type, 'text/html') }) it('should reject objects without getHeader method', function () { assert.throws(contentType.parse.bind(null, {}), /content-type header is missing/) }) it('should reject missing content-type', function () { var res = {getHeader: function () {}} assert.throws(contentType.parse.bind(null, res), /content-type header is missing/) }) })