1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const Boom = require('boom');
|
6 | const Hoek = require('hoek');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const internals = {};
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | exports.encoding = function (header, preferences) {
|
27 |
|
28 | const encodings = exports.encodings(header, preferences);
|
29 | return encodings.length ? encodings[0] : '';
|
30 | };
|
31 |
|
32 |
|
33 | exports.encodings = function (header, preferences) {
|
34 |
|
35 | Hoek.assert(!preferences || Array.isArray(preferences), 'Preferences must be an array');
|
36 |
|
37 | const scores = internals.parse(header, 'encoding');
|
38 | if (!preferences) {
|
39 | preferences = Object.keys(scores.accept);
|
40 | preferences.push('*');
|
41 | }
|
42 |
|
43 | return internals.map(preferences, scores);
|
44 | };
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | internals.preferenceRegex = /\s*([^;\,]+)(?:\s*;\s*[qQ]\=([01](?:\.\d{0,3})?))?\s*(?:\,|$)/g;
|
61 |
|
62 |
|
63 | internals.equivalents = {
|
64 | encoding: {
|
65 | 'x-compress': 'compress',
|
66 | 'x-gzip': 'gzip'
|
67 | }
|
68 | };
|
69 |
|
70 | internals.parse = function (header, type) {
|
71 |
|
72 | const scores = {
|
73 | accept: {},
|
74 | reject: {},
|
75 | any: 0.0
|
76 | };
|
77 |
|
78 | if (header) {
|
79 | const leftovers = header.replace(internals.preferenceRegex, ($0, $1, $2) => {
|
80 |
|
81 | $1 = $1.toLowerCase();
|
82 | const key = internals.equivalents[type][$1] || $1;
|
83 | const score = $2 ? parseFloat($2) : 1.0;
|
84 | if (key === '*') {
|
85 | scores.any = score;
|
86 | }
|
87 | else if (score > 0) {
|
88 | scores.accept[key] = score;
|
89 | }
|
90 | else {
|
91 | scores.reject[key] = true;
|
92 | }
|
93 |
|
94 | return '';
|
95 | });
|
96 |
|
97 | if (leftovers) {
|
98 | throw Boom.badRequest('Invalid accept-' + type + ' header');
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 | if (!scores.reject.identity &&
|
105 | !scores.accept.identity) {
|
106 |
|
107 | scores.accept.identity = scores.any || 0.001;
|
108 | }
|
109 |
|
110 | return scores;
|
111 | };
|
112 |
|
113 |
|
114 | internals.map = function (preferences, scores) {
|
115 |
|
116 | const scored = [];
|
117 | for (let i = 0; i < preferences.length; ++i) {
|
118 | const key = preferences[i].toLowerCase();
|
119 | if (!scores.reject[key]) {
|
120 | const score = scores.accept[key] || scores.any;
|
121 | if (score > 0) {
|
122 | scored.push({ key, score });
|
123 | }
|
124 | }
|
125 | }
|
126 |
|
127 | scored.sort(internals.sort);
|
128 |
|
129 | const result = [];
|
130 | for (let i = 0; i < scored.length; ++i) {
|
131 | result.push(scored[i].key);
|
132 | }
|
133 |
|
134 | return result;
|
135 | };
|
136 |
|
137 |
|
138 | internals.sort = function (a, b) {
|
139 |
|
140 | return b.score - a.score;
|
141 | };
|