add support for custom Accept parameters
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Pavel Brylov · committed by TJ Holowaychuk · parent d1bafa0 · Browse files · 4 files·+72 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+72 added−42 removed
- tests:+23 added−13 removed
- other:+49 added−29 removed
Provenance
- Commit
9e684d4- Tree
9706e2e- Author
Pavel Brylov <pavel@brylov.info>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
d1bafa0
4 files changed+72 added−42 removed
modified:lib/request.js+2 added-2 removed
200 unmodified lines201 req.__defineGetter__('acceptedLanguages', function(){202 var accept = this.get('Accept-Language');203 return accept204 ? utils205- .parseQuality(accept)205+ .parseParams(accept)206 .map(function(obj){207 return obj.value;208 })209 : [];15 unmodified lines225 req.__defineGetter__('acceptedCharsets', function(){226 var accept = this.get('Accept-Charset');227 return accept228 ? utils229- .parseQuality(accept)229+ .parseParams(accept)230 .map(function(obj){231 return obj.value;232 })233 : [];263 unmodified linesmodified:lib/response.js+2 added-2 removed
460 unmodified lines461 462 this.set('Vary', 'Accept');463 464 if (key) {465- this.set('Content-Type', normalizeType(key));465+ this.set('Content-Type', normalizeType(key).value);466 obj[key](req, this, next);467 } else if (fn) {468 fn();469 } else {470 var err = new Error('Not Acceptable');471 err.status = 406;472- err.types = normalizeTypes(keys);472+ err.types = normalizeTypes(keys).map(function(o){ return o.value });473 next(err);474 }475 476 return this;280 unmodified linesmodified:lib/utils.js+45 added-25 removed
73 unmodified lines74 /**75 * Normalize the given `type`, for example "html" becomes "text/html".76 *77 * @param {String} type78- * @return {String}78+ * @return {Object}79 * @api private80 */81 82 exports.normalizeType = function(type){83- return ~type.indexOf('/') ? type : mime.lookup(type);83+ return ~type.indexOf('/')84+ ? acceptParams(type)85+ : { value: mime.lookup(type), params: {} };86 };87 88 /**89 * Normalize `types`, for example "html" becomes "text/html".6 unmodified lines96 exports.normalizeTypes = function(types){97 var ret = [];98 99 for (var i = 0; i < types.length; ++i) {98- ret.push(~types[i].indexOf('/')99- ? types[i]100- : mime.lookup(types[i]));100+ ret.push(exports.normalizeType(types[i]));101 }102 103 return ret;104 };17 unmodified lines122 , len = accepted.length;123 124 for (var i = 0; i < len; ++i) {125 for (var j = 0, jlen = types.length; j < jlen; ++j) {126- if (exports.accept(normalized[j].split('/'), accepted[i])) {126+ if (exports.accept(normalized[j], accepted[i])) {127 return types[j];128 }129 }130 }16 unmodified lines147 148 /**149 * Check if `type` array is acceptable for `other`.150 *151- * @param {Array} type151+ * @param {Object} type152 * @param {Object} other153 * @return {Boolean}154 * @api private155 */156 157 exports.accept = function(type, other){158+ var t = type.value.split('/');158- return (type[0] == other.type || '*' == other.type)159- && (type[1] == other.subtype || '*' == other.subtype);159+ return (t[0] == other.type || '*' == other.type)160+ && (t[1] == other.subtype || '*' == other.subtype)161+ && paramsEqual(type.params, other.params);162 };163 164 /**165+ * Check if accept params are equal.166+ *167+ * @param {Object} a168+ * @param {Object} b169+ * @return {Boolean}170+ */171+function paramsEqual(a, b){172+ return !Object.keys(a).some(function(k) {173+ return a[k] != b[k];174+ });175+}176+177+/**178 * Parse accept `str`, returning179 * an array objects containing180 * `.type` and `.subtype` along181 * with the values provided by5 unmodified lines187 */188 189 exports.parseAccept = function(str){190 return exports176- .parseQuality(str)191+ .parseParams(str)192 .map(function(obj){193 var parts = obj.value.split('/');194 obj.type = parts[0];195 obj.subtype = parts[1];2 unmodified lines198 };199 200 /**201 * Parse quality `str`, returning an187- * array of objects with `.value` and188- * `.quality`.202+ * array of objects with `.value`,203+ * `.quality` and optional `.params`204 *190- * @param {Type} name191- * @return {Type}205+ * @param {String} str206+ * @return {Array}207 * @api private208 */209 195-exports.parseQuality = function(str){210+exports.parseParams = function(str){211 return str212 .split(/ *, */)198- .map(quality)213+ .map(acceptParams)214 .filter(function(obj){215 return obj.quality;216 })217 .sort(function(a, b){1 unmodified line219 });220 };221 222 /**208- * Parse quality `str` returning an209- * object with `.value` and `.quality`.223+ * Parse accept params `str` returning an224+ * object with `.value`, `.quality` and `.params`.225 *226 * @param {String} str227 * @return {Object}228 * @api private229 */230 216-function quality(str) {217- var parts = str.split(/ *; */)218- , val = parts[0];231+function acceptParams(str) {232+ var parts = str.split(/ *; */);233+ var ret = { value: parts[0], quality: 1, params: {} };234 220- var q = parts[1]221- ? parseFloat(parts[1].split(/ *= */)[1])222- : 1;235+ for (var i = 1; i < parts.length; ++i) {236+ var pms = parts[i].split(/ *= */);237+ if ('q' == pms[0]) {238+ ret.quality = parseFloat(pms[1]);239+ } else {240+ ret.params[pms[0]] = pms[1];241+ }242+ }243 224- return { value: val, quality: q };244+ return ret;245 }246 247 /**248 * Escape special characters in the given string of html.52 unmodified linesmodified:test/utils.js+23 added-13 removed
47 unmodified lines48 .should.equal('<script>foo & "bar"')49 })50 })51 52-describe('utils.parseQuality(str)', function(){52+describe('utils.parseParams(str)', function(){53 it('should default quality to 1', function(){54- utils.parseQuality('text/html')55- .should.eql([{ value: 'text/html', quality: 1 }]);54+ utils.parseParams('text/html')55+ .should.eql([{ value: 'text/html', quality: 1, params: {}}]);56 })57 58 it('should parse qvalues', function(){59- utils.parseQuality('text/html; q=0.5')60- .should.eql([{ value: 'text/html', quality: 0.5 }]);59+ utils.parseParams('text/html; q=0.5')60+ .should.eql([{ value: 'text/html', quality: 0.5, params: {}}]);61 62- utils.parseQuality('text/html; q=.2')63- .should.eql([{ value: 'text/html', quality: 0.2 }]);62+ utils.parseParams('text/html; q=.2')63+ .should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);64 })65- 65+66+ it('should parse accept parameters', function(){67+ utils.parseParams('application/json; ver=2.0')68+ .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}}]);69+70+ utils.parseParams('text/html; q=0.5; level=2')71+ .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}}]);72+ utils.parseParams('text/html;q=.2;ver=beta')73+ .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}}]);74+ })75+76 it('should work with messed up whitespace', function(){67- utils.parseQuality('text/html ; q = .2')68- .should.eql([{ value: 'text/html', quality: 0.2 }]);77+ utils.parseParams('text/html ; q = .2')78+ .should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);79 })80 81 it('should work with multiples', function(){82 var str = 'da, en;q=.5, en-gb;q=.8';73- var arr = utils.parseQuality(str);83+ var arr = utils.parseParams(str);84 arr[0].value.should.equal('da');85 arr[1].value.should.equal('en-gb');86 arr[2].value.should.equal('en');87 })88 89 it('should sort by quality', function(){90 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';81- var arr = utils.parseQuality(str);91+ var arr = utils.parseParams(str);92 arr[0].value.should.equal('application/json');93 arr[1].value.should.equal('text/html');94 arr[2].value.should.equal('text/plain');95 })96 97 it('should exclude those with a quality of 0', function(){98 var str = 'text/plain;q=.2, application/json, text/html;q=0';89- var arr = utils.parseQuality(str);99+ var arr = utils.parseParams(str);100 arr.should.have.length(2);101 })102 })103 112 unmodified lines