1 | |
---|
2 | /** |
---|
3 | * Module dependencies. |
---|
4 | */ |
---|
5 | |
---|
6 | var http = require('http') |
---|
7 | , utils = require('./utils') |
---|
8 | , connect = require('connect') |
---|
9 | , fresh = require('fresh') |
---|
10 | , parseRange = require('range-parser') |
---|
11 | , parse = connect.utils.parseUrl |
---|
12 | , mime = connect.mime; |
---|
13 | |
---|
14 | /** |
---|
15 | * Request prototype. |
---|
16 | */ |
---|
17 | |
---|
18 | var req = exports = module.exports = { |
---|
19 | __proto__: http.IncomingMessage.prototype |
---|
20 | }; |
---|
21 | |
---|
22 | /** |
---|
23 | * Return request header. |
---|
24 | * |
---|
25 | * The `Referrer` header field is special-cased, |
---|
26 | * both `Referrer` and `Referer` are interchangeable. |
---|
27 | * |
---|
28 | * Examples: |
---|
29 | * |
---|
30 | * req.get('Content-Type'); |
---|
31 | * // => "text/plain" |
---|
32 | * |
---|
33 | * req.get('content-type'); |
---|
34 | * // => "text/plain" |
---|
35 | * |
---|
36 | * req.get('Something'); |
---|
37 | * // => undefined |
---|
38 | * |
---|
39 | * Aliased as `req.header()`. |
---|
40 | * |
---|
41 | * @param {String} name |
---|
42 | * @return {String} |
---|
43 | * @api public |
---|
44 | */ |
---|
45 | |
---|
46 | req.get = |
---|
47 | req.header = function(name){ |
---|
48 | switch (name = name.toLowerCase()) { |
---|
49 | case 'referer': |
---|
50 | case 'referrer': |
---|
51 | return this.headers.referrer |
---|
52 | || this.headers.referer; |
---|
53 | default: |
---|
54 | return this.headers[name]; |
---|
55 | } |
---|
56 | }; |
---|
57 | |
---|
58 | /** |
---|
59 | * Check if the given `type(s)` is acceptable, returning |
---|
60 | * the best match when true, otherwise `undefined`, in which |
---|
61 | * case you should respond with 406 "Not Acceptable". |
---|
62 | * |
---|
63 | * The `type` value may be a single mime type string |
---|
64 | * such as "application/json", the extension name |
---|
65 | * such as "json", a comma-delimted list such as "json, html, text/plain", |
---|
66 | * or an array `["json", "html", "text/plain"]`. When a list |
---|
67 | * or array is given the _best_ match, if any is returned. |
---|
68 | * |
---|
69 | * Examples: |
---|
70 | * |
---|
71 | * // Accept: text/html |
---|
72 | * req.accepts('html'); |
---|
73 | * // => "html" |
---|
74 | * |
---|
75 | * // Accept: text/*, application/json |
---|
76 | * req.accepts('html'); |
---|
77 | * // => "html" |
---|
78 | * req.accepts('text/html'); |
---|
79 | * // => "text/html" |
---|
80 | * req.accepts('json, text'); |
---|
81 | * // => "json" |
---|
82 | * req.accepts('application/json'); |
---|
83 | * // => "application/json" |
---|
84 | * |
---|
85 | * // Accept: text/*, application/json |
---|
86 | * req.accepts('image/png'); |
---|
87 | * req.accepts('png'); |
---|
88 | * // => undefined |
---|
89 | * |
---|
90 | * // Accept: text/*;q=.5, application/json |
---|
91 | * req.accepts(['html', 'json']); |
---|
92 | * req.accepts('html, json'); |
---|
93 | * // => "json" |
---|
94 | * |
---|
95 | * @param {String|Array} type(s) |
---|
96 | * @return {String} |
---|
97 | * @api public |
---|
98 | */ |
---|
99 | |
---|
100 | req.accepts = function(type){ |
---|
101 | return utils.accepts(type, this.get('Accept')); |
---|
102 | }; |
---|
103 | |
---|
104 | /** |
---|
105 | * Check if the given `encoding` is accepted. |
---|
106 | * |
---|
107 | * @param {String} encoding |
---|
108 | * @return {Boolean} |
---|
109 | * @api public |
---|
110 | */ |
---|
111 | |
---|
112 | req.acceptsEncoding = function(encoding){ |
---|
113 | return ~this.acceptedEncodings.indexOf(encoding); |
---|
114 | }; |
---|
115 | |
---|
116 | /** |
---|
117 | * Check if the given `charset` is acceptable, |
---|
118 | * otherwise you should respond with 406 "Not Acceptable". |
---|
119 | * |
---|
120 | * @param {String} charset |
---|
121 | * @return {Boolean} |
---|
122 | * @api public |
---|
123 | */ |
---|
124 | |
---|
125 | req.acceptsCharset = function(charset){ |
---|
126 | var accepted = this.acceptedCharsets; |
---|
127 | return accepted.length |
---|
128 | ? ~accepted.indexOf(charset) |
---|
129 | : true; |
---|
130 | }; |
---|
131 | |
---|
132 | /** |
---|
133 | * Check if the given `lang` is acceptable, |
---|
134 | * otherwise you should respond with 406 "Not Acceptable". |
---|
135 | * |
---|
136 | * @param {String} lang |
---|
137 | * @return {Boolean} |
---|
138 | * @api public |
---|
139 | */ |
---|
140 | |
---|
141 | req.acceptsLanguage = function(lang){ |
---|
142 | var accepted = this.acceptedLanguages; |
---|
143 | return accepted.length |
---|
144 | ? ~accepted.indexOf(lang) |
---|
145 | : true; |
---|
146 | }; |
---|
147 | |
---|
148 | /** |
---|
149 | * Parse Range header field, |
---|
150 | * capping to the given `size`. |
---|
151 | * |
---|
152 | * Unspecified ranges such as "0-" require |
---|
153 | * knowledge of your resource length. In |
---|
154 | * the case of a byte range this is of course |
---|
155 | * the total number of bytes. If the Range |
---|
156 | * header field is not given `null` is returned, |
---|
157 | * `-1` when unsatisfiable, `-2` when syntactically invalid. |
---|
158 | * |
---|
159 | * NOTE: remember that ranges are inclusive, so |
---|
160 | * for example "Range: users=0-3" should respond |
---|
161 | * with 4 users when available, not 3. |
---|
162 | * |
---|
163 | * @param {Number} size |
---|
164 | * @return {Array} |
---|
165 | * @api public |
---|
166 | */ |
---|
167 | |
---|
168 | req.range = function(size){ |
---|
169 | var range = this.get('Range'); |
---|
170 | if (!range) return; |
---|
171 | return parseRange(size, range); |
---|
172 | }; |
---|
173 | |
---|
174 | /** |
---|
175 | * Return an array of encodings. |
---|
176 | * |
---|
177 | * Examples: |
---|
178 | * |
---|
179 | * ['gzip', 'deflate'] |
---|
180 | * |
---|
181 | * @return {Array} |
---|
182 | * @api public |
---|
183 | */ |
---|
184 | |
---|
185 | req.__defineGetter__('acceptedEncodings', function(){ |
---|
186 | var accept = this.get('Accept-Encoding'); |
---|
187 | return accept |
---|
188 | ? accept.trim().split(/ *, */) |
---|
189 | : []; |
---|
190 | }); |
---|
191 | |
---|
192 | /** |
---|
193 | * Return an array of Accepted media types |
---|
194 | * ordered from highest quality to lowest. |
---|
195 | * |
---|
196 | * Examples: |
---|
197 | * |
---|
198 | * [ { value: 'application/json', |
---|
199 | * quality: 1, |
---|
200 | * type: 'application', |
---|
201 | * subtype: 'json' }, |
---|
202 | * { value: 'text/html', |
---|
203 | * quality: 0.5, |
---|
204 | * type: 'text', |
---|
205 | * subtype: 'html' } ] |
---|
206 | * |
---|
207 | * @return {Array} |
---|
208 | * @api public |
---|
209 | */ |
---|
210 | |
---|
211 | req.__defineGetter__('accepted', function(){ |
---|
212 | var accept = this.get('Accept'); |
---|
213 | return accept |
---|
214 | ? utils.parseAccept(accept) |
---|
215 | : []; |
---|
216 | }); |
---|
217 | |
---|
218 | /** |
---|
219 | * Return an array of Accepted languages |
---|
220 | * ordered from highest quality to lowest. |
---|
221 | * |
---|
222 | * Examples: |
---|
223 | * |
---|
224 | * Accept-Language: en;q=.5, en-us |
---|
225 | * ['en-us', 'en'] |
---|
226 | * |
---|
227 | * @return {Array} |
---|
228 | * @api public |
---|
229 | */ |
---|
230 | |
---|
231 | req.__defineGetter__('acceptedLanguages', function(){ |
---|
232 | var accept = this.get('Accept-Language'); |
---|
233 | return accept |
---|
234 | ? utils |
---|
235 | .parseParams(accept) |
---|
236 | .map(function(obj){ |
---|
237 | return obj.value; |
---|
238 | }) |
---|
239 | : []; |
---|
240 | }); |
---|
241 | |
---|
242 | /** |
---|
243 | * Return an array of Accepted charsets |
---|
244 | * ordered from highest quality to lowest. |
---|
245 | * |
---|
246 | * Examples: |
---|
247 | * |
---|
248 | * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8 |
---|
249 | * ['unicode-1-1', 'iso-8859-5'] |
---|
250 | * |
---|
251 | * @return {Array} |
---|
252 | * @api public |
---|
253 | */ |
---|
254 | |
---|
255 | req.__defineGetter__('acceptedCharsets', function(){ |
---|
256 | var accept = this.get('Accept-Charset'); |
---|
257 | return accept |
---|
258 | ? utils |
---|
259 | .parseParams(accept) |
---|
260 | .map(function(obj){ |
---|
261 | return obj.value; |
---|
262 | }) |
---|
263 | : []; |
---|
264 | }); |
---|
265 | |
---|
266 | /** |
---|
267 | * Return the value of param `name` when present or `defaultValue`. |
---|
268 | * |
---|
269 | * - Checks route placeholders, ex: _/user/:id_ |
---|
270 | * - Checks body params, ex: id=12, {"id":12} |
---|
271 | * - Checks query string params, ex: ?id=12 |
---|
272 | * |
---|
273 | * To utilize request bodies, `req.body` |
---|
274 | * should be an object. This can be done by using |
---|
275 | * the `connect.bodyParser()` middleware. |
---|
276 | * |
---|
277 | * @param {String} name |
---|
278 | * @param {Mixed} [defaultValue] |
---|
279 | * @return {String} |
---|
280 | * @api public |
---|
281 | */ |
---|
282 | |
---|
283 | req.param = function(name, defaultValue){ |
---|
284 | var params = this.params || {}; |
---|
285 | var body = this.body || {}; |
---|
286 | var query = this.query || {}; |
---|
287 | if (null != params[name] && params.hasOwnProperty(name)) return params[name]; |
---|
288 | if (null != body[name]) return body[name]; |
---|
289 | if (null != query[name]) return query[name]; |
---|
290 | return defaultValue; |
---|
291 | }; |
---|
292 | |
---|
293 | /** |
---|
294 | * Check if the incoming request contains the "Content-Type" |
---|
295 | * header field, and it contains the give mime `type`. |
---|
296 | * |
---|
297 | * Examples: |
---|
298 | * |
---|
299 | * // With Content-Type: text/html; charset=utf-8 |
---|
300 | * req.is('html'); |
---|
301 | * req.is('text/html'); |
---|
302 | * req.is('text/*'); |
---|
303 | * // => true |
---|
304 | * |
---|
305 | * // When Content-Type is application/json |
---|
306 | * req.is('json'); |
---|
307 | * req.is('application/json'); |
---|
308 | * req.is('application/*'); |
---|
309 | * // => true |
---|
310 | * |
---|
311 | * req.is('html'); |
---|
312 | * // => false |
---|
313 | * |
---|
314 | * @param {String} type |
---|
315 | * @return {Boolean} |
---|
316 | * @api public |
---|
317 | */ |
---|
318 | |
---|
319 | req.is = function(type){ |
---|
320 | var ct = this.get('Content-Type'); |
---|
321 | if (!ct) return false; |
---|
322 | ct = ct.split(';')[0]; |
---|
323 | if (!~type.indexOf('/')) type = mime.lookup(type); |
---|
324 | if (~type.indexOf('*')) { |
---|
325 | type = type.split('/'); |
---|
326 | ct = ct.split('/'); |
---|
327 | if ('*' == type[0] && type[1] == ct[1]) return true; |
---|
328 | if ('*' == type[1] && type[0] == ct[0]) return true; |
---|
329 | return false; |
---|
330 | } |
---|
331 | return !! ~ct.indexOf(type); |
---|
332 | }; |
---|
333 | |
---|
334 | /** |
---|
335 | * Return the protocol string "http" or "https" |
---|
336 | * when requested with TLS. When the "trust proxy" |
---|
337 | * setting is enabled the "X-Forwarded-Proto" header |
---|
338 | * field will be trusted. If you're running behind |
---|
339 | * a reverse proxy that supplies https for you this |
---|
340 | * may be enabled. |
---|
341 | * |
---|
342 | * @return {String} |
---|
343 | * @api public |
---|
344 | */ |
---|
345 | |
---|
346 | req.__defineGetter__('protocol', function(){ |
---|
347 | var trustProxy = this.app.get('trust proxy'); |
---|
348 | return this.connection.encrypted |
---|
349 | ? 'https' |
---|
350 | : trustProxy |
---|
351 | ? (this.get('X-Forwarded-Proto') || 'http') |
---|
352 | : 'http'; |
---|
353 | }); |
---|
354 | |
---|
355 | /** |
---|
356 | * Short-hand for: |
---|
357 | * |
---|
358 | * req.protocol == 'https' |
---|
359 | * |
---|
360 | * @return {Boolean} |
---|
361 | * @api public |
---|
362 | */ |
---|
363 | |
---|
364 | req.__defineGetter__('secure', function(){ |
---|
365 | return 'https' == this.protocol; |
---|
366 | }); |
---|
367 | |
---|
368 | /** |
---|
369 | * Return the remote address, or when |
---|
370 | * "trust proxy" is `true` return |
---|
371 | * the upstream addr. |
---|
372 | * |
---|
373 | * @return {String} |
---|
374 | * @api public |
---|
375 | */ |
---|
376 | |
---|
377 | req.__defineGetter__('ip', function(){ |
---|
378 | return this.ips[0] || this.connection.remoteAddress; |
---|
379 | }); |
---|
380 | |
---|
381 | /** |
---|
382 | * When "trust proxy" is `true`, parse |
---|
383 | * the "X-Forwarded-For" ip address list. |
---|
384 | * |
---|
385 | * For example if the value were "client, proxy1, proxy2" |
---|
386 | * you would receive the array `["client", "proxy1", "proxy2"]` |
---|
387 | * where "proxy2" is the furthest down-stream. |
---|
388 | * |
---|
389 | * @return {Array} |
---|
390 | * @api public |
---|
391 | */ |
---|
392 | |
---|
393 | req.__defineGetter__('ips', function(){ |
---|
394 | var trustProxy = this.app.get('trust proxy'); |
---|
395 | var val = this.get('X-Forwarded-For'); |
---|
396 | return trustProxy && val |
---|
397 | ? val.split(/ *, */) |
---|
398 | : []; |
---|
399 | }); |
---|
400 | |
---|
401 | /** |
---|
402 | * Return basic auth credentials. |
---|
403 | * |
---|
404 | * Examples: |
---|
405 | * |
---|
406 | * // http://tobi:hello@example.com |
---|
407 | * req.auth |
---|
408 | * // => { username: 'tobi', password: 'hello' } |
---|
409 | * |
---|
410 | * @return {Object} or undefined |
---|
411 | * @api public |
---|
412 | */ |
---|
413 | |
---|
414 | req.__defineGetter__('auth', function(){ |
---|
415 | // missing |
---|
416 | var auth = this.get('Authorization'); |
---|
417 | if (!auth) return; |
---|
418 | |
---|
419 | // malformed |
---|
420 | var parts = auth.split(' '); |
---|
421 | if ('basic' != parts[0].toLowerCase()) return; |
---|
422 | if (!parts[1]) return; |
---|
423 | auth = parts[1]; |
---|
424 | |
---|
425 | // credentials |
---|
426 | auth = new Buffer(auth, 'base64').toString().match(/^([^:]*):(.*)$/); |
---|
427 | if (!auth) return; |
---|
428 | return { username: auth[1], password: auth[2] }; |
---|
429 | }); |
---|
430 | |
---|
431 | /** |
---|
432 | * Return subdomains as an array. |
---|
433 | * |
---|
434 | * Subdomains are the dot-separated parts of the host before the main domain of |
---|
435 | * the app. By default, the domain of the app is assumed to be the last two |
---|
436 | * parts of the host. This can be changed by setting "subdomain offset". |
---|
437 | * |
---|
438 | * For example, if the domain is "tobi.ferrets.example.com": |
---|
439 | * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`. |
---|
440 | * If "subdomain offset" is 3, req.subdomains is `["tobi"]`. |
---|
441 | * |
---|
442 | * @return {Array} |
---|
443 | * @api public |
---|
444 | */ |
---|
445 | |
---|
446 | req.__defineGetter__('subdomains', function(){ |
---|
447 | var offset = this.app.get('subdomain offset'); |
---|
448 | return (this.host || '') |
---|
449 | .split('.') |
---|
450 | .reverse() |
---|
451 | .slice(offset); |
---|
452 | }); |
---|
453 | |
---|
454 | /** |
---|
455 | * Short-hand for `url.parse(req.url).pathname`. |
---|
456 | * |
---|
457 | * @return {String} |
---|
458 | * @api public |
---|
459 | */ |
---|
460 | |
---|
461 | req.__defineGetter__('path', function(){ |
---|
462 | return parse(this).pathname; |
---|
463 | }); |
---|
464 | |
---|
465 | /** |
---|
466 | * Parse the "Host" header field hostname. |
---|
467 | * |
---|
468 | * @return {String} |
---|
469 | * @api public |
---|
470 | */ |
---|
471 | |
---|
472 | req.__defineGetter__('host', function(){ |
---|
473 | var trustProxy = this.app.get('trust proxy'); |
---|
474 | var host = trustProxy && this.get('X-Forwarded-Host'); |
---|
475 | host = host || this.get('Host'); |
---|
476 | if (!host) return; |
---|
477 | return host.split(':')[0]; |
---|
478 | }); |
---|
479 | |
---|
480 | /** |
---|
481 | * Check if the request is fresh, aka |
---|
482 | * Last-Modified and/or the ETag |
---|
483 | * still match. |
---|
484 | * |
---|
485 | * @return {Boolean} |
---|
486 | * @api public |
---|
487 | */ |
---|
488 | |
---|
489 | req.__defineGetter__('fresh', function(){ |
---|
490 | var method = this.method; |
---|
491 | var s = this.res.statusCode; |
---|
492 | |
---|
493 | // GET or HEAD for weak freshness validation only |
---|
494 | if ('GET' != method && 'HEAD' != method) return false; |
---|
495 | |
---|
496 | // 2xx or 304 as per rfc2616 14.26 |
---|
497 | if ((s >= 200 && s < 300) || 304 == s) { |
---|
498 | return fresh(this.headers, this.res._headers); |
---|
499 | } |
---|
500 | |
---|
501 | return false; |
---|
502 | }); |
---|
503 | |
---|
504 | /** |
---|
505 | * Check if the request is stale, aka |
---|
506 | * "Last-Modified" and / or the "ETag" for the |
---|
507 | * resource has changed. |
---|
508 | * |
---|
509 | * @return {Boolean} |
---|
510 | * @api public |
---|
511 | */ |
---|
512 | |
---|
513 | req.__defineGetter__('stale', function(){ |
---|
514 | return !this.fresh; |
---|
515 | }); |
---|
516 | |
---|
517 | /** |
---|
518 | * Check if the request was an _XMLHttpRequest_. |
---|
519 | * |
---|
520 | * @return {Boolean} |
---|
521 | * @api public |
---|
522 | */ |
---|
523 | |
---|
524 | req.__defineGetter__('xhr', function(){ |
---|
525 | var val = this.get('X-Requested-With') || ''; |
---|
526 | return 'xmlhttprequest' == val.toLowerCase(); |
---|
527 | }); |
---|