123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /**
- * negotiator
- * Copyright(c) 2012 Isaac Z. Schlueter
- * Copyright(c) 2014 Federico Romero
- * Copyright(c) 2014-2015 Douglas Christopher Wilson
- * MIT Licensed
- */
- 'use strict';
- /**
- * Module exports.
- * @public
- */
- module.exports = preferredMediaTypes;
- module.exports.preferredMediaTypes = preferredMediaTypes;
- /**
- * Module variables.
- * @private
- */
- var simpleMediaTypeRegExp = /^\s*([^\s\/;]+)\/([^;\s]+)\s*(?:;(.*))?$/;
- /**
- * Parse the Accept header.
- * @private
- */
- function parseAccept(accept) {
- var accepts = splitMediaTypes(accept);
- for (var i = 0, j = 0; i < accepts.length; i++) {
- var mediaType = parseMediaType(accepts[i].trim(), i);
- if (mediaType) {
- accepts[j++] = mediaType;
- }
- }
- // trim accepts
- accepts.length = j;
- return accepts;
- }
- /**
- * Parse a media type from the Accept header.
- * @private
- */
- function parseMediaType(str, i) {
- var match = simpleMediaTypeRegExp.exec(str);
- if (!match) return null;
- var params = Object.create(null);
- var q = 1;
- var subtype = match[2];
- var type = match[1];
- if (match[3]) {
- var kvps = splitParameters(match[3]).map(splitKeyValuePair);
- for (var j = 0; j < kvps.length; j++) {
- var pair = kvps[j];
- var key = pair[0].toLowerCase();
- var val = pair[1];
- // get the value, unwrapping quotes
- var value = val && val[0] === '"' && val[val.length - 1] === '"'
- ? val.substr(1, val.length - 2)
- : val;
- if (key === 'q') {
- q = parseFloat(value);
- break;
- }
- // store parameter
- params[key] = value;
- }
- }
- return {
- type: type,
- subtype: subtype,
- params: params,
- q: q,
- i: i
- };
- }
- /**
- * Get the priority of a media type.
- * @private
- */
- function getMediaTypePriority(type, accepted, index) {
- var priority = {o: -1, q: 0, s: 0};
- for (var i = 0; i < accepted.length; i++) {
- var spec = specify(type, accepted[i], index);
- if (spec && (priority.s - spec.s || priority.q - spec.q || priority.o - spec.o) < 0) {
- priority = spec;
- }
- }
- return priority;
- }
- /**
- * Get the specificity of the media type.
- * @private
- */
- function specify(type, spec, index) {
- var p = parseMediaType(type);
- var s = 0;
- if (!p) {
- return null;
- }
- if(spec.type.toLowerCase() == p.type.toLowerCase()) {
- s |= 4
- } else if(spec.type != '*') {
- return null;
- }
- if(spec.subtype.toLowerCase() == p.subtype.toLowerCase()) {
- s |= 2
- } else if(spec.subtype != '*') {
- return null;
- }
- var keys = Object.keys(spec.params);
- if (keys.length > 0) {
- if (keys.every(function (k) {
- return spec.params[k] == '*' || (spec.params[k] || '').toLowerCase() == (p.params[k] || '').toLowerCase();
- })) {
- s |= 1
- } else {
- return null
- }
- }
- return {
- i: index,
- o: spec.i,
- q: spec.q,
- s: s,
- }
- }
- /**
- * Get the preferred media types from an Accept header.
- * @public
- */
- function preferredMediaTypes(accept, provided) {
- // RFC 2616 sec 14.2: no header = */*
- var accepts = parseAccept(accept === undefined ? '*/*' : accept || '');
- if (!provided) {
- // sorted list of all types
- return accepts
- .filter(isQuality)
- .sort(compareSpecs)
- .map(getFullType);
- }
- var priorities = provided.map(function getPriority(type, index) {
- return getMediaTypePriority(type, accepts, index);
- });
- // sorted list of accepted types
- return priorities.filter(isQuality).sort(compareSpecs).map(function getType(priority) {
- return provided[priorities.indexOf(priority)];
- });
- }
- /**
- * Compare two specs.
- * @private
- */
- function compareSpecs(a, b) {
- return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i) || 0;
- }
- /**
- * Get full type string.
- * @private
- */
- function getFullType(spec) {
- return spec.type + '/' + spec.subtype;
- }
- /**
- * Check if a spec has any quality.
- * @private
- */
- function isQuality(spec) {
- return spec.q > 0;
- }
- /**
- * Count the number of quotes in a string.
- * @private
- */
- function quoteCount(string) {
- var count = 0;
- var index = 0;
- while ((index = string.indexOf('"', index)) !== -1) {
- count++;
- index++;
- }
- return count;
- }
- /**
- * Split a key value pair.
- * @private
- */
- function splitKeyValuePair(str) {
- var index = str.indexOf('=');
- var key;
- var val;
- if (index === -1) {
- key = str;
- } else {
- key = str.substr(0, index);
- val = str.substr(index + 1);
- }
- return [key, val];
- }
- /**
- * Split an Accept header into media types.
- * @private
- */
- function splitMediaTypes(accept) {
- var accepts = accept.split(',');
- for (var i = 1, j = 0; i < accepts.length; i++) {
- if (quoteCount(accepts[j]) % 2 == 0) {
- accepts[++j] = accepts[i];
- } else {
- accepts[j] += ',' + accepts[i];
- }
- }
- // trim accepts
- accepts.length = j + 1;
- return accepts;
- }
- /**
- * Split a string of parameters.
- * @private
- */
- function splitParameters(str) {
- var parameters = str.split(';');
- for (var i = 1, j = 0; i < parameters.length; i++) {
- if (quoteCount(parameters[j]) % 2 == 0) {
- parameters[++j] = parameters[i];
- } else {
- parameters[j] += ';' + parameters[i];
- }
- }
- // trim parameters
- parameters.length = j + 1;
- for (var i = 0; i < parameters.length; i++) {
- parameters[i] = parameters[i].trim();
- }
- return parameters;
- }
|