1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var http = require('http')
|
14 | , crypto = require('crypto')
|
15 | , crc16 = require('crc').crc16
|
16 | , Path = require('path')
|
17 | , fs = require('fs');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | exports.mime = function(req) {
|
29 | var str = req.headers['content-type'] || '';
|
30 | return str.split(';')[0];
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | exports.error = function(code){
|
42 | var err = new Error(http.STATUS_CODES[code]);
|
43 | err.status = code;
|
44 | return err;
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | exports.md5 = function(str, encoding){
|
61 | return crypto
|
62 | .createHash('md5')
|
63 | .update(str)
|
64 | .digest(encoding || 'hex');
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | exports.merge = function(a, b){
|
83 | if (a && b) {
|
84 | for (var key in b) {
|
85 | a[key] = b[key];
|
86 | }
|
87 | }
|
88 | return a;
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | exports.escape = function(html){
|
100 | return String(html)
|
101 | .replace(/&(?!\w+;)/g, '&')
|
102 | .replace(/</g, '<')
|
103 | .replace(/>/g, '>')
|
104 | .replace(/"/g, '"');
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | exports.uid = function(len) {
|
120 | return crypto.randomBytes(Math.ceil(len * 3 / 4))
|
121 | .toString('base64')
|
122 | .slice(0, len);
|
123 | };
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | exports.sign = function(val, secret){
|
135 | return val + '.' + crypto
|
136 | .createHmac('sha256', secret)
|
137 | .update(val)
|
138 | .digest('base64')
|
139 | .replace(/=+$/, '');
|
140 | };
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | exports.unsign = function(val, secret){
|
153 | var str = val.slice(0,val.lastIndexOf('.'));
|
154 | return exports.sign(str, secret) == val
|
155 | ? str
|
156 | : false;
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | exports.parseSignedCookies = function(obj, secret){
|
170 | var ret = {};
|
171 | Object.keys(obj).forEach(function(key){
|
172 | var val = obj[key]
|
173 | , signed = exports.unsign(val, secret);
|
174 |
|
175 | if (signed) {
|
176 | ret[key] = signed;
|
177 | delete obj[key];
|
178 | }
|
179 | });
|
180 | return ret;
|
181 | };
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | exports.parseJSONCookies = function(obj){
|
192 | var hashes = {};
|
193 |
|
194 | Object.keys(obj).forEach(function(key){
|
195 | var val = obj[key];
|
196 | if (0 == val.indexOf('j:')) {
|
197 | try {
|
198 | hashes[key] = crc16(val);
|
199 | obj[key] = JSON.parse(val.slice(2));
|
200 | } catch (err) {
|
201 |
|
202 | }
|
203 | }
|
204 | });
|
205 |
|
206 | return {
|
207 | cookies: obj,
|
208 | hashes: hashes
|
209 | };
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | exports.parseCookie = function(str){
|
221 | var obj = {}
|
222 | , pairs = str.split(/[;,] */);
|
223 | for (var i = 0, len = pairs.length; i < len; ++i) {
|
224 | var pair = pairs[i]
|
225 | , eqlIndex = pair.indexOf('=')
|
226 | , key = pair.substr(0, eqlIndex).trim()
|
227 | , val = pair.substr(++eqlIndex, pair.length).trim();
|
228 |
|
229 |
|
230 | if ('"' == val[0]) val = val.slice(1, -1);
|
231 |
|
232 |
|
233 | if (undefined == obj[key]) {
|
234 | val = val.replace(/\+/g, ' ');
|
235 | try {
|
236 | obj[key] = decodeURIComponent(val);
|
237 | } catch (err) {
|
238 | if (err instanceof URIError) {
|
239 | obj[key] = val;
|
240 | } else {
|
241 | throw err;
|
242 | }
|
243 | }
|
244 | }
|
245 | }
|
246 | return obj;
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | exports.serializeCookie = function(name, val, obj){
|
263 | var pairs = [name + '=' + encodeURIComponent(val)]
|
264 | , obj = obj || {};
|
265 |
|
266 | if (obj.domain) pairs.push('domain=' + obj.domain);
|
267 | if (obj.path) pairs.push('path=' + obj.path);
|
268 | if (obj.expires) pairs.push('expires=' + obj.expires.toUTCString());
|
269 | if (obj.httpOnly) pairs.push('httpOnly');
|
270 | if (obj.secure) pairs.push('secure');
|
271 |
|
272 | return pairs.join('; ');
|
273 | };
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | exports.pause = function(obj){
|
294 | var onData
|
295 | , onEnd
|
296 | , events = [];
|
297 |
|
298 |
|
299 | obj.on('data', onData = function(data, encoding){
|
300 | events.push(['data', data, encoding]);
|
301 | });
|
302 |
|
303 |
|
304 | obj.on('end', onEnd = function(data, encoding){
|
305 | events.push(['end', data, encoding]);
|
306 | });
|
307 |
|
308 | return {
|
309 | end: function(){
|
310 | obj.removeListener('data', onData);
|
311 | obj.removeListener('end', onEnd);
|
312 | },
|
313 | resume: function(){
|
314 | this.end();
|
315 | for (var i = 0, len = events.length; i < len; ++i) {
|
316 | obj.emit.apply(obj, events[i]);
|
317 | }
|
318 | }
|
319 | };
|
320 | };
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | exports.modified = function(req, res, headers) {
|
332 | var headers = headers || res._headers || {}
|
333 | , modifiedSince = req.headers['if-modified-since']
|
334 | , lastModified = headers['last-modified']
|
335 | , noneMatch = req.headers['if-none-match']
|
336 | , etag = headers['etag'];
|
337 |
|
338 | if (noneMatch) noneMatch = noneMatch.split(/ *, */);
|
339 |
|
340 |
|
341 | if (noneMatch && etag && ~noneMatch.indexOf(etag)) {
|
342 | return false;
|
343 | }
|
344 |
|
345 |
|
346 | if (modifiedSince && lastModified) {
|
347 | modifiedSince = new Date(modifiedSince);
|
348 | lastModified = new Date(lastModified);
|
349 |
|
350 | if (!isNaN(modifiedSince.getTime())) {
|
351 | if (lastModified <= modifiedSince) return false;
|
352 | }
|
353 | }
|
354 |
|
355 | return true;
|
356 | };
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 | exports.removeContentHeaders = function(res){
|
366 | Object.keys(res._headers).forEach(function(field){
|
367 | if (0 == field.indexOf('content')) {
|
368 | res.removeHeader(field);
|
369 | }
|
370 | });
|
371 | };
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 | exports.conditionalGET = function(req) {
|
382 | return req.headers['if-modified-since']
|
383 | || req.headers['if-none-match'];
|
384 | };
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | exports.unauthorized = function(res, realm) {
|
395 | res.statusCode = 401;
|
396 | res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
|
397 | res.end('Unauthorized');
|
398 | };
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 | exports.notModified = function(res) {
|
409 | exports.removeContentHeaders(res);
|
410 | res.statusCode = 304;
|
411 | res.end();
|
412 | };
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | exports.etag = function(stat) {
|
424 | return '"' + stat.size + '-' + Number(stat.mtime) + '"';
|
425 | };
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 | exports.parseRange = function(size, str){
|
437 | var valid = true;
|
438 | var arr = str.substr(6).split(',').map(function(range){
|
439 | var range = range.split('-')
|
440 | , start = parseInt(range[0], 10)
|
441 | , end = parseInt(range[1], 10);
|
442 |
|
443 |
|
444 | if (isNaN(start)) {
|
445 | start = size - end;
|
446 | end = size - 1;
|
447 |
|
448 | } else if (isNaN(end)) {
|
449 | end = size - 1;
|
450 | }
|
451 |
|
452 |
|
453 | if (isNaN(start)
|
454 | || isNaN(end)
|
455 | || start > end
|
456 | || start < 0) valid = false;
|
457 |
|
458 | return {
|
459 | start: start,
|
460 | end: end
|
461 | };
|
462 | });
|
463 |
|
464 | return valid ? arr : null;
|
465 | };
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | exports.parseCacheControl = function(str){
|
476 | var directives = str.split(',')
|
477 | , obj = {};
|
478 |
|
479 | for(var i = 0, len = directives.length; i < len; i++) {
|
480 | var parts = directives[i].split('=')
|
481 | , key = parts.shift().trim()
|
482 | , val = parseInt(parts.shift(), 10);
|
483 |
|
484 | obj[key] = isNaN(val) ? true : val;
|
485 | }
|
486 |
|
487 | return obj;
|
488 | };
|