Skip to content
Myr

request.js

This file

Path

lib/request.js

414 lines · 8.6 KiB · JavaScript

Last changed by

Changed `req.param()` to check route firstTJ Holowaychukbefore adoptionebcb1ca

History of this file

Take it

The walk stopped after 200 commits: lines older than 6368ab4 point at it.

ocdTJ Holowaychukbefore adoption6368ab4

lines 1–8why, one step back
1
2/**3 * Module dependencies.4 */56var http = require('http')7 , utils = require('./utils')8 , connect = require('connect')

Perf: memoize parsed urlsTJ Holowaychukbefore adoption73092c9

line 9why, one step back
9 , parse = connect.utils.parseUrl

ocdTJ Holowaychukbefore adoption6368ab4

lines 10–23why, one step back
10 , mime = require('mime');
1112/**13 * Request prototype.14 */1516var req = exports = module.exports = {17 __proto__: http.IncomingMessage.prototype18};1920/**21 * Return request header.22 *23 * The `Referrer` header field is special-cased,

doc typoTJ Holowaychukbefore adoptionb5cee82

line 24why, one step back
24 * both `Referrer` and `Referer` are interchangeable.

ocdTJ Holowaychukbefore adoption6368ab4

lines 25–35why, one step back
25 *
26 * Examples:27 *28 * req.get('Content-Type');29 * // => "text/plain"30 * 31 * req.get('content-type');32 * // => "text/plain"33 * 34 * req.get('Something');35 * // => undefined

docsTJ Holowaychukbefore adoption27b1b5e

lines 36–38why, one step back
36 *
37 * Aliased as `req.header()`.38 *

ocdTJ Holowaychukbefore adoption6368ab4

lines 39–43why, one step back
39 * @param {String} name
40 * @return {String} 41 * @api public42 */43

Added `res.header()` and `req.header()` aliases for BCTJ Holowaychukbefore adoption5fa2f89

lines 44–45why, one step back
44req.get =
45req.header = function(name){

ocdTJ Holowaychukbefore adoption6368ab4

lines 46–56why, one step back
46 switch (name = name.toLowerCase()) {
47 case 'referer':48 case 'referrer':49 return this.headers.referrer50 || this.headers.referer;51 default:52 return this.headers[name];53 }54};5556/**

docsTJ Holowaychukbefore adoption4e2677f

lines 57–59why, one step back
57 * Check if the given `type(s)` is acceptable, returning
58 * the best match when true, otherwise `undefined`, in which59 * case you should respond with 406 "Not Acceptable".

ocdTJ Holowaychukbefore adoption6368ab4

line 60why, one step back
60 *

docsTJ Holowaychukbefore adoption365a98d

lines 61–66why, one step back
61 * The `type` value may be a single mime type string
62 * such as "application/json", the extension name63 * such as "json", a comma-delimted list such as "json, html, text/plain",64 * or an array `["json", "html", "text/plain"]`. When a list65 * or array is given the _best_ match, if any is returned.66 *

ocdTJ Holowaychukbefore adoption6368ab4

lines 67–70why, one step back
67 * Examples:
68 * 69 * // Accept: text/html70 * req.accepts('html');

docsTJ Holowaychukbefore adoption365a98d

line 71why, one step back
71 * // => "html"

ocdTJ Holowaychukbefore adoption6368ab4

line 72why, one step back
72 *

typoTJ Holowaychukbefore adoptionba570a9

line 73why, one step back
73 * // Accept: text/*, application/json

ocdTJ Holowaychukbefore adoption6368ab4

line 74why, one step back
74 * req.accepts('html');

docsTJ Holowaychukbefore adoption365a98d

line 75why, one step back
75 * // => "html"

ocdTJ Holowaychukbefore adoption6368ab4

line 76why, one step back
76 * req.accepts('text/html');

docsTJ Holowaychukbefore adoption365a98d

lines 77–79why, one step back
77 * // => "text/html"
78 * req.accepts('json, text');79 * // => "json"

ocdTJ Holowaychukbefore adoption6368ab4

line 80why, one step back
80 * req.accepts('application/json');

docsTJ Holowaychukbefore adoption365a98d

line 81why, one step back
81 * // => "application/json"

ocdTJ Holowaychukbefore adoption6368ab4

line 82why, one step back
82 *

typoTJ Holowaychukbefore adoptionba570a9

line 83why, one step back
83 * // Accept: text/*, application/json

ocdTJ Holowaychukbefore adoption6368ab4

lines 84–85why, one step back
84 * req.accepts('image/png');
85 * req.accepts('png');

docsTJ Holowaychukbefore adoption365a98d

line 86why, one step back
86 * // => undefined

ocdTJ Holowaychukbefore adoption6368ab4

line 87why, one step back
87 *

typoTJ Holowaychukbefore adoptionba570a9

line 88why, one step back
88 * // Accept: text/*;q=.5, application/json

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

line 89why, one step back
89 * req.accepts(['html', 'json']);

docsTJ Holowaychukbefore adoption365a98d

lines 90–91why, one step back
90 * req.accepts('html, json');
91 * // => "json"

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

lines 92–93why, one step back
92 *
93 * @param {String|Array} type(s)

docsTJ Holowaychukbefore adoption4e2677f

line 94why, one step back
94 * @return {String}

ocdTJ Holowaychukbefore adoption6368ab4

lines 95–211why, one step back
95 * @api public
96 */9798req.accepts = function(type){99 return utils.accepts(type, this.get('Accept'));100};101102/**103 * Check if the given `charset` is acceptable,104 * otherwise you should respond with 406 "Not Acceptable".105 *106 * @param {String} charset107 * @return {Boolean}108 * @api public109 */110111req.acceptsCharset = function(charset){112 var accepted = this.acceptedCharsets;113 return accepted.length114 ? ~accepted.indexOf(charset)115 : true;116};117118/**119 * Check if the given `lang` is acceptable,120 * otherwise you should respond with 406 "Not Acceptable".121 *122 * @param {String} lang123 * @return {Boolean}124 * @api public125 */126127req.acceptsLanguage = function(lang){128 var accepted = this.acceptedLanguages;129 return accepted.length130 ? ~accepted.indexOf(lang)131 : true;132};133134/**135 * Return an array of Accepted media types136 * ordered from highest quality to lowest.137 *138 * Examples:139 *140 * [ { value: 'application/json',141 * quality: 1,142 * type: 'application',143 * subtype: 'json' },144 * { value: 'text/html',145 * quality: 0.5,146 * type: 'text',147 * subtype: 'html' } ]148 *149 * @return {Array}150 * @api public151 */152153req.__defineGetter__('accepted', function(){154 var accept = this.get('Accept');155 return accept156 ? utils.parseAccept(accept)157 : [];158});159160/**161 * Return an array of Accepted languages162 * ordered from highest quality to lowest.163 *164 * Examples:165 *166 * Accept-Language: en;q=.5, en-us167 * ['en-us', 'en']168 *169 * @return {Array}170 * @api public171 */172173req.__defineGetter__('acceptedLanguages', function(){174 var accept = this.get('Accept-Language');175 return accept176 ? utils177 .parseQuality(accept)178 .map(function(obj){179 return obj.value;180 })181 : [];182});183184/**185 * Return an array of Accepted charsets186 * ordered from highest quality to lowest.187 *188 * Examples:189 *190 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8191 * ['unicode-1-1', 'iso-8859-5']192 *193 * @return {Array}194 * @api public195 */196197req.__defineGetter__('acceptedCharsets', function(){198 var accept = this.get('Accept-Charset');199 return accept200 ? utils201 .parseQuality(accept)202 .map(function(obj){203 return obj.value;204 })205 : [];206});207208/**209 * Return the value of param `name` when present or `defaultValue`.210 *211 * - Checks route placeholders, ex: _/user/:id_

Changed `req.param()` to check route firstTJ Holowaychukbefore adoptionebcb1ca

line 212why, one step back
212 * - Checks body params, ex: id=12, {"id":12}

ocdTJ Holowaychukbefore adoption6368ab4

lines 213–231why, one step back
213 * - Checks query string params, ex: ?id=12
214 *215 * To utilize request bodies, `req.body`216 * should be an object. This can be done by using217 * the `connect.bodyParser()` middleware.218 *219 * @param {String} name220 * @param {Mixed} defaultValue221 * @return {String}222 * @api public223 */224225req.param = function(name, defaultValue){226 // route params227 if (this.params228 && this.params.hasOwnProperty(name)229 && undefined !== this.params[name]) {230 return this.params[name]; 231 }

Changed `req.param()` to check route firstTJ Holowaychukbefore adoptionebcb1ca

lines 232–234why, one step back
232
233 // req.body234 if (this.body && undefined !== this.body[name]) return this.body[name];

ocdTJ Holowaychukbefore adoption6368ab4

lines 235–294why, one step back
235
236 // query-string237 if (undefined !== this.query[name]) return this.query[name]; 238239 return defaultValue;240};241242/**243 * Check if the incoming request contains the "Content-Type" 244 * header field, and it contains the give mime `type`.245 *246 * Examples:247 *248 * // With Content-Type: text/html; charset=utf-8249 * req.is('html');250 * req.is('text/html');251 * req.is('text/*');252 * // => true253 * 254 * // When Content-Type is application/json255 * req.is('json');256 * req.is('application/json');257 * req.is('application/*');258 * // => true259 * 260 * req.is('html');261 * // => false262 * 263 * @param {String} type264 * @return {Boolean}265 * @api public266 */267268req.is = function(type){269 var ct = this.get('Content-Type');270 if (!ct) return false;271 ct = ct.split(';')[0];272 if (!~type.indexOf('/')) type = mime.lookup(type);273 if (~type.indexOf('*')) {274 type = type.split('/');275 ct = ct.split('/');276 if ('*' == type[0] && type[1] == ct[1]) return true;277 if ('*' == type[1] && type[0] == ct[0]) return true;278 return false;279 }280 return !! ~ct.indexOf(type);281};282283/**284 * Return the protocol string "http" or "https"285 * when requested with TLS. When the "trust proxy" 286 * setting is enabled the "X-Forwarded-Proto" header287 * field will be trusted. If you're running behind288 * a reverse proxy that supplies https for you this289 * may be enabled.290 *291 * @return {String}292 * @api public293 */294

populate changelogTJ Holowaychukbefore adoptionabe5cc7

line 295why, one step back
295req.__defineGetter__('protocol', function(){

Fixed: use app.get() for all core settingsTJ Holowaychukbefore adoption8f87400

line 296why, one step back
296 var trustProxy = this.app.get('trust proxy');

Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04

line 297why, one step back
297 return this.connection.encrypted

ocdTJ Holowaychukbefore adoption6368ab4

lines 298–304why, one step back
298 ? 'https'
299 : trustProxy300 ? (this.get('X-Forwarded-Proto') || 'http')301 : 'http';302});303304/**

Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04

line 305why, one step back
305 * Short-hand for:

ocdTJ Holowaychukbefore adoption6368ab4

line 306why, one step back
306 *

Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04

lines 307–308why, one step back
307 * req.protocol == 'https'
308 *

ocdTJ Holowaychukbefore adoption6368ab4

lines 309–313why, one step back
309 * @return {Boolean}
310 * @api public311 */312313req.__defineGetter__('secure', function(){

Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04

line 314why, one step back
314 return 'https' == this.protocol;

ocdTJ Holowaychukbefore adoption6368ab4

lines 315–317why, one step back
315});
316317/**

Added `req.ip`TJ Holowaychukbefore adoptiond84d0b6

lines 318–330why, one step back
318 * Return the remote address, or when
319 * "trust proxy" is `true` return320 * the upstream addr.321 *322 * @return {String}323 * @api public324 */325326req.__defineGetter__('ip', function(){327 return this.ips[0] || this.connection.remoteAddress;328});329330/**

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 331–333why, one step back
331 * When "trust proxy" is `true`, parse
332 * the "X-Forwarded-For" ip address list.333 *

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

line 334why, one step back
334 * For example if the value were "client, proxy1, proxy2"

Changed: dont reverse `req.ips`TJ Holowaychukbefore adoptionb694ba2

line 335why, one step back
335 * you would receive the array `["client", "proxy1", "proxy2"]`

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

lines 336–337why, one step back
336 * where "proxy2" is the furthest down-stream.
337 *

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 338–342why, one step back
338 * @return {Array}
339 * @api public340 */341342req.__defineGetter__('ips', function(){

misc refactorTJ Holowaychukbefore adoption910dae1

line 343why, one step back
343 var trustProxy = this.app.get('trust proxy');

req.ipds doesnt need "trust proxy"TJ Holowaychukbefore adoption6d0f9a3

line 344why, one step back
344 var val = this.get('X-Forwarded-For');

Fixed setting check for req.ipsTJ Holowaychukbefore adoptione3cbac2

line 345why, one step back
345 return trustProxy && val

Changed: dont reverse `req.ips`TJ Holowaychukbefore adoptionb694ba2

line 346why, one step back
346 ? val.split(/ *, */)

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 347–350why, one step back
347 : [];
348});349350/**

Added `req.subdomains`TJ Holowaychukbefore adoption4e33245

lines 351–367why, one step back
351 * Return subdomains as an array.
352 *353 * For example "tobi.ferrets.example.com"354 * would provide `["ferrets", "tobi"]`.355 *356 * @return {Array}357 * @api public358 */359360req.__defineGetter__('subdomains', function(){361 return this.get('Host')362 .split('.')363 .slice(0, -2)364 .reverse();365});366367/**

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

line 368why, one step back
368 * Short-hand for `url.parse(req.url).pathname`.

ocdTJ Holowaychukbefore adoption6368ab4

lines 369–374why, one step back
369 *
370 * @return {String}371 * @api public372 */373374req.__defineGetter__('path', function(){

Perf: memoize parsed urlsTJ Holowaychukbefore adoption73092c9

line 375why, one step back
375 return parse(this).pathname;

ocdTJ Holowaychukbefore adoption6368ab4

lines 376–414why, one step back
376});
377378/**379 * Check if the request is fresh, aka380 * Last-Modified and/or the ETag381 * still match.382 *383 * @return {Boolean}384 * @api public385 */386387req.__defineGetter__('fresh', function(){388 return ! this.stale;389});390391/**392 * Check if the request is stale, aka393 * "Last-Modified" and / or the "ETag" for the394 * resource has changed.395 *396 * @return {Boolean}397 * @api public398 */399400req.__defineGetter__('stale', function(){401 return connect.utils.modified(this, this.res);402});403404/**405 * Check if the request was an _XMLHttpRequest_.406 *407 * @return {Boolean}408 * @api public409 */410411req.__defineGetter__('xhr', function(){412 var val = this.get('X-Requested-With') || '';413 return 'xmlhttprequest' == val.toLowerCase();414});