1 |
|
2 |
|
3 |
|
4 |
|
5 | var http = require('http');
|
6 | var path = require('path');
|
7 | var mixin = require('utils-merge');
|
8 | var escapeHtml = require('escape-html');
|
9 | var sign = require('cookie-signature').sign;
|
10 | var normalizeType = require('./utils').normalizeType;
|
11 | var normalizeTypes = require('./utils').normalizeTypes;
|
12 | var setCharset = require('./utils').setCharset;
|
13 | var contentDisposition = require('./utils').contentDisposition;
|
14 | var deprecate = require('./utils').deprecate;
|
15 | var etag = require('./utils').etag;
|
16 | var statusCodes = http.STATUS_CODES;
|
17 | var cookie = require('cookie');
|
18 | var send = require('send');
|
19 | var basename = path.basename;
|
20 | var extname = path.extname;
|
21 | var mime = send.mime;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var res = module.exports = {
|
28 | __proto__: http.ServerResponse.prototype
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | res.status = function(code){
|
40 | this.statusCode = code;
|
41 | return this;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | res.links = function(links){
|
60 | var link = this.get('Link') || '';
|
61 | if (link) link += ', ';
|
62 | return this.set('Link', link + Object.keys(links).map(function(rel){
|
63 | return '<' + links[rel] + '>; rel="' + rel + '"';
|
64 | }).join(', '));
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | res.send = function(body){
|
85 | var req = this.req;
|
86 | var head = 'HEAD' == req.method;
|
87 | var type;
|
88 | var encoding;
|
89 | var len;
|
90 |
|
91 |
|
92 | var app = this.app;
|
93 |
|
94 |
|
95 | if (2 == arguments.length) {
|
96 |
|
97 | if ('number' != typeof body && 'number' == typeof arguments[1]) {
|
98 | this.statusCode = arguments[1];
|
99 | } else {
|
100 | this.statusCode = body;
|
101 | body = arguments[1];
|
102 | }
|
103 | }
|
104 |
|
105 | switch (typeof body) {
|
106 |
|
107 | case 'number':
|
108 | this.get('Content-Type') || this.type('txt');
|
109 | this.statusCode = body;
|
110 | body = http.STATUS_CODES[body];
|
111 | break;
|
112 |
|
113 | case 'string':
|
114 | if (!this.get('Content-Type')) this.type('html');
|
115 | break;
|
116 | case 'boolean':
|
117 | case 'object':
|
118 | if (null == body) {
|
119 | body = '';
|
120 | } else if (Buffer.isBuffer(body)) {
|
121 | this.get('Content-Type') || this.type('bin');
|
122 | } else {
|
123 | return this.json(body);
|
124 | }
|
125 | break;
|
126 | }
|
127 |
|
128 |
|
129 | if (undefined !== body && !this.get('Content-Length')) {
|
130 | this.set('Content-Length', len = Buffer.isBuffer(body)
|
131 | ? body.length
|
132 | : Buffer.byteLength(body));
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 | if (app.settings.etag && len && ('GET' == req.method || 'HEAD' == req.method)) {
|
138 | if (!this.get('ETag')) {
|
139 | this.set('ETag', etag(body));
|
140 | }
|
141 | }
|
142 |
|
143 |
|
144 | if ('string' === typeof body) {
|
145 | encoding = 'utf8';
|
146 | type = this.get('Content-Type');
|
147 |
|
148 |
|
149 | if ('string' === typeof type) {
|
150 | this.set('Content-Type', setCharset(type, 'utf-8'));
|
151 | }
|
152 | }
|
153 |
|
154 |
|
155 | if (req.fresh) this.statusCode = 304;
|
156 |
|
157 |
|
158 | if (204 == this.statusCode || 304 == this.statusCode) {
|
159 | this.removeHeader('Content-Type');
|
160 | this.removeHeader('Content-Length');
|
161 | this.removeHeader('Transfer-Encoding');
|
162 | body = '';
|
163 | }
|
164 |
|
165 |
|
166 | this.end((head ? null : body), encoding);
|
167 |
|
168 | return this;
|
169 | };
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | res.json = function(obj){
|
188 |
|
189 | if (2 == arguments.length) {
|
190 |
|
191 | if ('number' == typeof arguments[1]) {
|
192 | this.statusCode = arguments[1];
|
193 | return 'number' === typeof obj
|
194 | ? jsonNumDeprecated.call(this, obj)
|
195 | : jsonDeprecated.call(this, obj);
|
196 | } else {
|
197 | this.statusCode = obj;
|
198 | obj = arguments[1];
|
199 | }
|
200 | }
|
201 |
|
202 |
|
203 | var app = this.app;
|
204 | var replacer = app.get('json replacer');
|
205 | var spaces = app.get('json spaces');
|
206 | var body = JSON.stringify(obj, replacer, spaces);
|
207 |
|
208 |
|
209 | this.get('Content-Type') || this.set('Content-Type', 'application/json');
|
210 |
|
211 | return this.send(body);
|
212 | };
|
213 |
|
214 | var jsonDeprecated = deprecate(res.json,
|
215 | 'res.json(obj, status): Use res.json(status, obj) instead');
|
216 |
|
217 | var jsonNumDeprecated = deprecate(res.json,
|
218 | 'res.json(num, status): Use res.status(status).json(num) instead');
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | res.jsonp = function(obj){
|
237 |
|
238 | if (2 == arguments.length) {
|
239 |
|
240 | if ('number' == typeof arguments[1]) {
|
241 | this.statusCode = arguments[1];
|
242 | return 'number' === typeof obj
|
243 | ? jsonpNumDeprecated.call(this, obj)
|
244 | : jsonpDeprecated.call(this, obj);
|
245 | } else {
|
246 | this.statusCode = obj;
|
247 | obj = arguments[1];
|
248 | }
|
249 | }
|
250 |
|
251 |
|
252 | var app = this.app;
|
253 | var replacer = app.get('json replacer');
|
254 | var spaces = app.get('json spaces');
|
255 | var body = JSON.stringify(obj, replacer, spaces)
|
256 | .replace(/\u2028/g, '\\u2028')
|
257 | .replace(/\u2029/g, '\\u2029');
|
258 | var callback = this.req.query[app.get('jsonp callback name')];
|
259 |
|
260 |
|
261 | this.get('Content-Type') || this.set('Content-Type', 'application/json');
|
262 |
|
263 |
|
264 | if (Array.isArray(callback)) {
|
265 | callback = callback[0];
|
266 | }
|
267 |
|
268 |
|
269 | if (callback && 'string' === typeof callback) {
|
270 | this.set('Content-Type', 'text/javascript');
|
271 | var cb = callback.replace(/[^\[\]\w$.]/g, '');
|
272 | body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';
|
273 | }
|
274 |
|
275 | return this.send(body);
|
276 | };
|
277 |
|
278 | var jsonpDeprecated = deprecate(res.json,
|
279 | 'res.jsonp(obj, status): Use res.jsonp(status, obj) instead');
|
280 |
|
281 | var jsonpNumDeprecated = deprecate(res.json,
|
282 | 'res.jsonp(num, status): Use res.status(status).jsonp(num) instead');
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | res.sendfile = function(path, options, fn){
|
328 | options = options || {};
|
329 | var self = this;
|
330 | var req = self.req;
|
331 | var next = this.req.next;
|
332 | var done;
|
333 |
|
334 |
|
335 |
|
336 | if ('function' == typeof options) {
|
337 | fn = options;
|
338 | options = {};
|
339 | }
|
340 |
|
341 |
|
342 | req.socket.on('error', error);
|
343 |
|
344 |
|
345 | function error(err) {
|
346 | if (done) return;
|
347 | done = true;
|
348 |
|
349 |
|
350 | cleanup();
|
351 | if (!self.headersSent) self.removeHeader('Content-Disposition');
|
352 |
|
353 |
|
354 | if (fn) return fn(err);
|
355 |
|
356 |
|
357 | if (self.headersSent) return;
|
358 |
|
359 |
|
360 | next(err);
|
361 | }
|
362 |
|
363 |
|
364 | function stream(stream) {
|
365 | if (done) return;
|
366 | cleanup();
|
367 | if (fn) stream.on('end', fn);
|
368 | }
|
369 |
|
370 |
|
371 | function cleanup() {
|
372 | req.socket.removeListener('error', error);
|
373 | }
|
374 |
|
375 |
|
376 | options.maxage = options.maxage || options.maxAge || 0;
|
377 |
|
378 |
|
379 | var file = send(req, path, options);
|
380 | file.on('error', error);
|
381 | file.on('directory', next);
|
382 | file.on('stream', stream);
|
383 | file.pipe(this);
|
384 | this.on('finish', cleanup);
|
385 | };
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | res.download = function(path, filename, fn){
|
404 |
|
405 | if ('function' == typeof filename) {
|
406 | fn = filename;
|
407 | filename = null;
|
408 | }
|
409 |
|
410 | filename = filename || path;
|
411 | this.set('Content-Disposition', contentDisposition(filename));
|
412 | return this.sendfile(path, fn);
|
413 | };
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 | res.contentType =
|
433 | res.type = function(type){
|
434 | return this.set('Content-Type', ~type.indexOf('/')
|
435 | ? type
|
436 | : mime.lookup(type));
|
437 | };
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | res.format = function(obj){
|
497 | var req = this.req;
|
498 | var next = req.next;
|
499 |
|
500 | var fn = obj.default;
|
501 | if (fn) delete obj.default;
|
502 | var keys = Object.keys(obj);
|
503 |
|
504 | var key = req.accepts(keys);
|
505 |
|
506 | this.vary("Accept");
|
507 |
|
508 | if (key) {
|
509 | this.set('Content-Type', normalizeType(key).value);
|
510 | obj[key](req, this, next);
|
511 | } else if (fn) {
|
512 | fn();
|
513 | } else {
|
514 | var err = new Error('Not Acceptable');
|
515 | err.status = 406;
|
516 | err.types = normalizeTypes(keys).map(function(o){ return o.value });
|
517 | next(err);
|
518 | }
|
519 |
|
520 | return this;
|
521 | };
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 | res.attachment = function(filename){
|
532 | if (filename) this.type(extname(filename));
|
533 | this.set('Content-Disposition', contentDisposition(filename));
|
534 | return this;
|
535 | };
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 | res.set =
|
556 | res.header = function(field, val){
|
557 | if (2 == arguments.length) {
|
558 | if (Array.isArray(val)) val = val.map(String);
|
559 | else val = String(val);
|
560 | if ('content-type' == field.toLowerCase() && !/;\s*charset\s*=/.test(val)) {
|
561 | var charset = mime.charsets.lookup(val.split(';')[0]);
|
562 | if (charset) val += '; charset=' + charset.toLowerCase();
|
563 | }
|
564 | this.setHeader(field, val);
|
565 | } else {
|
566 | for (var key in field) {
|
567 | this.set(key, field[key]);
|
568 | }
|
569 | }
|
570 | return this;
|
571 | };
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 | res.get = function(field){
|
582 | return this.getHeader(field);
|
583 | };
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 | res.clearCookie = function(name, options){
|
595 | var opts = { expires: new Date(1), path: '/' };
|
596 | return this.cookie(name, '', options
|
597 | ? mixin(opts, options)
|
598 | : opts);
|
599 | };
|
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 | res.cookie = function(name, val, options){
|
625 | options = mixin({}, options);
|
626 | var secret = this.req.secret;
|
627 | var signed = options.signed;
|
628 | if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies');
|
629 | if ('number' == typeof val) val = val.toString();
|
630 | if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
|
631 | if (signed) val = 's:' + sign(val, secret);
|
632 | if ('maxAge' in options) {
|
633 | options.expires = new Date(Date.now() + options.maxAge);
|
634 | options.maxAge /= 1000;
|
635 | }
|
636 | if (null == options.path) options.path = '/';
|
637 | var headerVal = cookie.serialize(name, String(val), options);
|
638 |
|
639 |
|
640 | var prev = this.get('Set-Cookie');
|
641 | if (prev) {
|
642 | if (Array.isArray(prev)) {
|
643 | headerVal = prev.concat(headerVal);
|
644 | } else {
|
645 | headerVal = [prev, headerVal];
|
646 | }
|
647 | }
|
648 | this.set('Set-Cookie', headerVal);
|
649 | return this;
|
650 | };
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 | res.location = function(url){
|
670 | var req = this.req;
|
671 |
|
672 |
|
673 | if ('back' == url) url = req.get('Referrer') || '/';
|
674 |
|
675 |
|
676 | this.set('Location', url);
|
677 | return this;
|
678 | };
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 | res.redirect = function(url){
|
702 | var head = 'HEAD' == this.req.method;
|
703 | var status = 302;
|
704 | var body;
|
705 |
|
706 |
|
707 | if (2 == arguments.length) {
|
708 | if ('number' == typeof url) {
|
709 | status = url;
|
710 | url = arguments[1];
|
711 | } else {
|
712 | status = arguments[1];
|
713 | }
|
714 | }
|
715 |
|
716 |
|
717 | this.location(url);
|
718 | url = this.get('Location');
|
719 |
|
720 |
|
721 | this.format({
|
722 | text: function(){
|
723 | body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);
|
724 | },
|
725 |
|
726 | html: function(){
|
727 | var u = escapeHtml(url);
|
728 | body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
|
729 | },
|
730 |
|
731 | default: function(){
|
732 | body = '';
|
733 | }
|
734 | });
|
735 |
|
736 |
|
737 | this.statusCode = status;
|
738 | this.set('Content-Length', Buffer.byteLength(body));
|
739 | this.end(head ? null : body);
|
740 | };
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 | res.vary = function(field){
|
752 | var self = this;
|
753 |
|
754 |
|
755 | if (!field) return this;
|
756 |
|
757 |
|
758 | if (Array.isArray(field)) {
|
759 | field.forEach(function(field){
|
760 | self.vary(field);
|
761 | });
|
762 | return;
|
763 | }
|
764 |
|
765 | var vary = this.get('Vary');
|
766 |
|
767 |
|
768 | if (vary) {
|
769 | vary = vary.split(/ *, */);
|
770 | if (!~vary.indexOf(field)) vary.push(field);
|
771 | this.set('Vary', vary.join(', '));
|
772 | return this;
|
773 | }
|
774 |
|
775 |
|
776 | this.set('Vary', field);
|
777 | return this;
|
778 | };
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 | res.render = function(view, options, fn){
|
797 | options = options || {};
|
798 | var self = this;
|
799 | var req = this.req;
|
800 | var app = req.app;
|
801 |
|
802 |
|
803 | if ('function' == typeof options) {
|
804 | fn = options, options = {};
|
805 | }
|
806 |
|
807 |
|
808 | options._locals = self.locals;
|
809 |
|
810 |
|
811 | fn = fn || function(err, str){
|
812 | if (err) return req.next(err);
|
813 | self.send(str);
|
814 | };
|
815 |
|
816 |
|
817 | app.render(view, options, fn);
|
818 | };
|