1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var bytes = require('bytes');
|
15 | var contentType = require('content-type');
|
16 | var cookieParser = require('cookie-parser');
|
17 | var createError = require('http-errors');
|
18 | var deprecate = require('depd')('connect');
|
19 | var http = require('http')
|
20 | , crypto = require('crypto')
|
21 | , parseurl = require('parseurl')
|
22 | , sep = require('path').sep
|
23 | , signature = require('cookie-signature')
|
24 | , typeis = require('type-is')
|
25 | , nodeVersion = process.versions.node.split('.');
|
26 | var merge = require('utils-merge');
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | exports.brokenPause = parseInt(nodeVersion[0], 10) === 0
|
32 | && parseInt(nodeVersion[1], 10) < 10;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | exports.hasBody = deprecate.function(typeis.hasBody,
|
43 | 'utils.hasBody: use type-is npm module instead');
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | exports.mime = function(req) {
|
55 | var str = req.headers['content-type'] || ''
|
56 | , i = str.indexOf(';');
|
57 | return ~i ? str.slice(0, i) : str;
|
58 | };
|
59 |
|
60 | exports.mime = deprecate.function(exports.mime,
|
61 | 'utils.mime: use type-is npm module instead for mime comparisons');
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | exports.error = function(code, msg){
|
74 | var err = new Error(msg || http.STATUS_CODES[code]);
|
75 | err.status = code;
|
76 | return err;
|
77 | };
|
78 |
|
79 | exports.error = deprecate.function(exports.error,
|
80 | 'utils.error: use http-errors npm module instead');
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | exports.md5 = function(str, encoding){
|
96 | return crypto
|
97 | .createHash('md5')
|
98 | .update(str, 'utf8')
|
99 | .digest(encoding || 'hex');
|
100 | };
|
101 |
|
102 | exports.md5 = deprecate.function(exports.md5,
|
103 | 'utils.md5: use crypto npm module instead for hashing');
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | exports.merge = deprecate.function(merge,
|
121 | 'utils.merge: use utils-merge npm module instead');
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | exports.escape = function(html){
|
132 | return String(html)
|
133 | .replace(/&(?!\w+;)/g, '&')
|
134 | .replace(/</g, '<')
|
135 | .replace(/>/g, '>')
|
136 | .replace(/"/g, '"');
|
137 | };
|
138 |
|
139 | exports.escape = deprecate.function(exports.escape,
|
140 | 'utils.escape: use escape-html npm module instead');
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | exports.sign = deprecate.function(signature.sign,
|
152 | 'utils.sign: use cookie-signature npm module instead');
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | exports.unsign = deprecate.function(signature.unsign,
|
165 | 'utils.unsign: use cookie-signature npm module instead');
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | exports.parseSignedCookies = deprecate.function(cookieParser.signedCookies,
|
178 | 'utils.parseSignedCookies: use cookie-parser npm module instead');
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | exports.parseSignedCookie = deprecate.function(cookieParser.signedCookie,
|
190 | 'utils.parseSignedCookie: use cookie-parser npm module instead');
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | exports.parseJSONCookies = deprecate.function(cookieParser.JSONCookies,
|
201 | 'utils.parseJSONCookies: use cookie-parser npm module instead');
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | exports.parseJSONCookie = deprecate.function(cookieParser.JSONCookie,
|
212 | 'utils.parseJSONCookie: use cookie-parser npm module instead');
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | exports.pause = exports.brokenPause
|
235 | ? require('pause')
|
236 | : function () {
|
237 | return {
|
238 | end: noop,
|
239 | resume: noop
|
240 | }
|
241 | }
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | exports.removeContentHeaders = function(res){
|
251 | if (!res._headers) return;
|
252 | Object.keys(res._headers).forEach(function(field){
|
253 | if (0 == field.indexOf('content')) {
|
254 | res.removeHeader(field);
|
255 | }
|
256 | });
|
257 | };
|
258 |
|
259 | exports.removeContentHeaders = deprecate.function(exports.removeContentHeaders,
|
260 | 'utils.removeContentHeaders: this private api moved with serve-static');
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | exports.conditionalGET = function(req) {
|
271 | return req.headers['if-modified-since']
|
272 | || req.headers['if-none-match'];
|
273 | };
|
274 |
|
275 | exports.conditionalGET = deprecate.function(exports.conditionalGET,
|
276 | 'utils.conditionalGET: use fresh npm module instead');
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | exports.unauthorized = function(res, realm) {
|
287 | res.statusCode = 401;
|
288 | res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
|
289 | res.end('Unauthorized');
|
290 | };
|
291 |
|
292 | exports.unauthorized = deprecate.function(exports.unauthorized,
|
293 | 'utils.unauthorized: this private api moved with basic-auth-connect');
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | exports.notModified = function(res) {
|
304 | exports.removeContentHeaders(res);
|
305 | res.statusCode = 304;
|
306 | res.end();
|
307 | };
|
308 |
|
309 | exports.notModified = deprecate.function(exports.notModified,
|
310 | 'utils.notModified: this private api moved with serve-static');
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | exports.etag = function(stat) {
|
322 | return '"' + stat.size + '-' + Number(stat.mtime) + '"';
|
323 | };
|
324 |
|
325 | exports.etag = deprecate.function(exports.etag,
|
326 | 'utils.etag: this private api moved with serve-static');
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | exports.parseCacheControl = function(str){
|
337 | var directives = str.split(',')
|
338 | , obj = {};
|
339 |
|
340 | for(var i = 0, len = directives.length; i < len; i++) {
|
341 | var parts = directives[i].split('=')
|
342 | , key = parts.shift().trim()
|
343 | , val = parseInt(parts.shift(), 10);
|
344 |
|
345 | obj[key] = isNaN(val) ? true : val;
|
346 | }
|
347 |
|
348 | return obj;
|
349 | };
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | exports.parseUrl = deprecate.function(parseurl,
|
360 | 'utils.parseUrl: use parseurl npm module instead');
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 | exports.parseBytes = deprecate.function(bytes,
|
371 | 'utils.parseBytes: use bytes npm module instead');
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 | exports.normalizeSlashes = function normalizeSlashes(path) {
|
383 | return path.split(sep).join('/');
|
384 | };
|
385 |
|
386 | exports.normalizeSlashes = deprecate.function(exports.normalizeSlashes,
|
387 | 'utils.normalizeSlashes: this private api moved with serve-index');
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | exports.setCharset = function setCharset(type, charset) {
|
399 | if (!type || !charset) return type;
|
400 |
|
401 | var parsed = contentType.parse(type);
|
402 | var exists = parsed.parameters.charset;
|
403 |
|
404 |
|
405 | if (exists) {
|
406 | return type;
|
407 | }
|
408 |
|
409 |
|
410 | parsed.parameters.charset = charset;
|
411 |
|
412 | return contentType.format(parsed);
|
413 | };
|
414 |
|
415 | function noop() {}
|