Skip to content
Myr

Added array support. Closes #1053

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent a7a8dcd · Browse files · 5 files+146 added−41 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+146 added−41 removed

  • tests:+67 added−0 removed
  • other:+79 added−41 removed

5 files+146 added−41 removed

modified:lib/request.js+10 added-3 removed
56 unmodified lines
57 }58};5960/**61 * Check if the given `type` is acceptable,61 * Check if the given `type(s)` is acceptable,62 * otherwise you should respond with 406 "Not Acceptable".63 *64 * Examples:65 *
11 unmodified lines
77 * req.accepts('image/png');78 * req.accepts('png');79 * // => false80 *81 * // Accept: text/*; application/json82 * req.accepts(['html', 'json']);83 * // => "html"84 *85 * req.accepts(['text/html', 'application/json']);86 * // => "text/html"87 *81 * @param {String} type82 * @return {Boolean}88 * @param {String|Array} type(s)89 * @return {Boolean|String}90 * @api public91 */9293req.accepts = function(type){
292 unmodified lines
modified:lib/response.js+5 added-35 removed
12 unmodified lines
13 , http = require('http')14 , path = require('path')15 , connect = require('connect')16 , utils = connect.utils17 , accept = require('./utils').accept17 , normalizeType = require('./utils').normalizeType18 , normalizeTypes = require('./utils').normalizeTypes19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21 , mime = require('mime')22 , basename = path.basename
317 unmodified lines
340 */341342res.format = function(obj){343 var keys = Object.keys(obj)343 , types = []344 , req = this.req345 , next = req.next346 , accepted = req.accepted347 , acceptedlen = accepted.length348 , type349 , key;346 , key = req.accepts(keys);347351 // normalize extnames -> mime352 if (acceptedlen) {353 for (var i = 0; i < keys.length; ++i) {354 types.push(~keys[i].indexOf('/')355 ? keys[i]356 : mime.lookup(keys[i]));357 }358 }359360 // determine most acceptable format361 out:362 for (var i = 0; i < acceptedlen; ++i) {363 for (var j = 0, jlen = types.length; j < jlen; ++j) {364 if (accept(types[j].split('/'), accepted[i])) {365 key = keys[j];366 type = types[j];367 break out;368 }369 }370 }371372 // default to the first373 if (!acceptedlen) {374 key = keys[0];375 type = types[0];376 }377348 if (key) {379 this.set('Content-Type', type);349 this.set('Content-Type', normalizeType(key));350 obj[key](req, this, next);351 } else {352 var err = new Error('Not Acceptable');353 err.status = 406;384 err.types = types;354 err.types = normalizeTypes(keys);355 next(err);356 }357358 return this;
277 unmodified lines
modified:lib/utils.js+64 added-3 removed
44 unmodified lines
45 return ret;46};4748/**49 * Check if `type` is acceptable based on50 * the given `str`.49 * Normalize the given `type`, for example "html" becomes "text/html".50 *51 * @param {String} type52 * @return {String}53 * @api private54 */5556exports.normalizeType = function(type){57 return ~type.indexOf('/') ? type : mime.lookup(type);58};5960/**61 * Normalize `types`, for example "html" becomes "text/html".62 *63 * @param {Array} types64 * @return {Array}65 * @api private66 */6768exports.normalizeTypes = function(types){69 var ret = [];7071 for (var i = 0; i < types.length; ++i) {72 ret.push(~types[i].indexOf('/')73 ? types[i]74 : mime.lookup(types[i]));75 }7677 return ret;78};7980/**81 * Return the acceptable type in `types`, if any.82 *83 * @param {Array} types84 * @param {String} str54 * @return {Boolean}85 * @return {String}86 * @api private87 */8889exports.acceptsArray = function(types, str){90 // accept anything when Accept is not present91 if (!str) return types[0];9293 // parse94 var accepted = exports.parseAccept(str)95 , normalized = exports.normalizeTypes(types)96 , len = accepted.length;9798 for (var i = 0; i < len; ++i) {99 for (var j = 0, jlen = types.length; j < jlen; ++j) {100 if (exports.accept(normalized[j].split('/'), accepted[i])) {101 return types[j];102 }103 }104 }105};106107/**108 * Check if `type(s)` are acceptable based on109 * the given `str`.110 *111 * @param {String|Array} type(s)112 * @param {String} str113 * @return {Boolean|String}114 * @api private115 */116117exports.accepts = function(type, str){118 if (Array.isArray(type)) return exports.acceptsArray(type, str);119120 // accept anything when Accept is not present121 if (!str) return true;122123 // resolve mime
113 unmodified lines
modified:test/req.accepts.js+66 added
modified:test/res.format.js+1 added