3 * Copyright(c) 2012 Isaac Z. Schlueter
4 * Copyright(c) 2014 Federico Romero
5 * Copyright(c) 2014-2015 Douglas Christopher Wilson
16 module.exports = preferredEncodings;
17 module.exports.preferredEncodings = preferredEncodings;
24 var simpleEncodingRegExp = /^\s*([^\s;]+)\s*(?:;(.*))?$/;
27 * Parse the Accept-Encoding header.
31 function parseAcceptEncoding(accept) {
32 var accepts = accept.split(',');
33 var hasIdentity = false;
36 for (var i = 0, j = 0; i < accepts.length; i++) {
37 var encoding = parseEncoding(accepts[i].trim(), i);
40 accepts[j++] = encoding;
41 hasIdentity = hasIdentity || specify('identity', encoding);
42 minQuality = Math.min(minQuality, encoding.q || 1);
48 * If identity doesn't explicitly appear in the accept-encoding header,
49 * it's added to the list of acceptable encoding with the lowest q
65 * Parse an encoding from the Accept-Encoding header.
69 function parseEncoding(str, i) {
70 var match = simpleEncodingRegExp.exec(str);
71 if (!match) return null;
73 var encoding = match[1];
76 var params = match[2].split(';');
77 for (var j = 0; j < params.length; j++) {
78 var p = params[j].trim().split('=');
94 * Get the priority of an encoding.
98 function getEncodingPriority(encoding, accepted, index) {
99 var priority = {o: -1, q: 0, s: 0};
101 for (var i = 0; i < accepted.length; i++) {
102 var spec = specify(encoding, accepted[i], index);
104 if (spec && (priority.s - spec.s || priority.q - spec.q || priority.o - spec.o) < 0) {
113 * Get the specificity of the encoding.
117 function specify(encoding, spec, index) {
119 if(spec.encoding.toLowerCase() === encoding.toLowerCase()){
121 } else if (spec.encoding !== '*' ) {
134 * Get the preferred encodings from an Accept-Encoding header.
138 function preferredEncodings(accept, provided) {
139 var accepts = parseAcceptEncoding(accept || '');
142 // sorted list of all encodings
146 .map(getFullEncoding);
149 var priorities = provided.map(function getPriority(type, index) {
150 return getEncodingPriority(type, accepts, index);
153 // sorted list of accepted encodings
154 return priorities.filter(isQuality).sort(compareSpecs).map(function getEncoding(priority) {
155 return provided[priorities.indexOf(priority)];
164 function compareSpecs(a, b) {
165 return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i) || 0;
169 * Get full encoding string.
173 function getFullEncoding(spec) {
174 return spec.encoding;
178 * Check if a spec has any quality.
182 function isQuality(spec) {