Skip to content
Myr

request.js

This file

Path

lib/request.js

415 lines · 8.8 KiB · JavaScript

Last changed by

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

History of this file

Take it

The walk stopped after 200 commits: lines older than 94d1e94 point at it.

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 1–26why, one step back
1
2/*!3 * Express - request4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var http = require('http')13 , utils = require('./utils')14 , connect = require('connect')15 , parse = require('url').parse16 , mime = require('mime');1718/**19 * Request prototype.20 */2122var req = exports = module.exports = {23 __proto__: http.IncomingMessage.prototype24};2526/**

doc typoTJ Holowaychukbefore adoptionfe27989

line 27why, one step back
27 * Return request header.

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 28–34why, one step back
28 *
29 * The `Referrer` header field is special-cased,30 * both `Referrer` and `Referer` will yield are31 * interchangeable.32 *33 * Examples:34 *

Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b

line 35why, one step back
35 * req.get('Content-Type');

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 36–37why, one step back
36 * // => "text/plain"
37 *

Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b

line 38why, one step back
38 * req.get('content-type');

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 39–40why, one step back
39 * // => "text/plain"
40 *

Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964

line 41why, one step back
41 * req.get('Something');

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 42–48why, one step back
42 * // => undefined
43 * 44 * @param {String} name45 * @return {String} 46 * @api public47 */48

Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b

line 49why, one step back
49req.get = function(name){

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 50–53why, one step back
50 switch (name = name.toLowerCase()) {
51 case 'referer':52 case 'referrer':53 return this.headers.referrer

Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f

line 54why, one step back
54 || this.headers.referer;

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 55why, one step back
55 default:

Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f

line 56why, one step back
56 return this.headers[name];

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 57–60why, one step back
57 }
58};5960/**

docsTJ Holowaychukbefore adoption4e2677f

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 64why, one step back
64 *

docsTJ Holowaychukbefore adoption365a98d

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 71–74why, one step back
71 * Examples:
72 * 73 * // Accept: text/html74 * req.accepts('html');

docsTJ Holowaychukbefore adoption365a98d

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 76why, one step back
76 *

typoTJ Holowaychukbefore adoptionba570a9

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

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

docsTJ Holowaychukbefore adoption365a98d

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

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

docsTJ Holowaychukbefore adoption365a98d

lines 81–83why, one step back
81 * // => "text/html"
82 * req.accepts('json, text');83 * // => "json"

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

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

docsTJ Holowaychukbefore adoption365a98d

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 86why, one step back
86 *

typoTJ Holowaychukbefore adoptionba570a9

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

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

docsTJ Holowaychukbefore adoption365a98d

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 91why, one step back
91 *

typoTJ Holowaychukbefore adoptionba570a9

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption365a98d

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption4e2677f

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 99–102why, one step back
99 * @api public
100 */101102req.accepts = function(type){

Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964

line 103why, one step back
103 return utils.accepts(type, this.get('Accept'));

mergedTj Holowaychukbefore adoption547d18c

line 104why, one step back
104};

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 105why, one step back
105

mergedTj Holowaychukbefore adoption547d18c

lines 106–113why, one step back
106/**
107 * Check if the given `charset` is acceptable,108 * otherwise you should respond with 406 "Not Acceptable".109 *110 * @param {String} charset111 * @return {Boolean}112 * @api public113 */

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 114why, one step back
114

mergedTj Holowaychukbefore adoption547d18c

lines 115–120why, one step back
115req.acceptsCharset = function(charset){
116 var accepted = this.acceptedCharsets;117 return accepted.length118 ? ~accepted.indexOf(charset)119 : true;120};

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 121why, one step back
121

mergedTj Holowaychukbefore adoption547d18c

lines 122–129why, one step back
122/**
123 * Check if the given `lang` is acceptable,124 * otherwise you should respond with 406 "Not Acceptable".125 *126 * @param {String} lang127 * @return {Boolean}128 * @api public129 */

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 130why, one step back
130

mergedTj Holowaychukbefore adoption547d18c

lines 131–135why, one step back
131req.acceptsLanguage = function(lang){
132 var accepted = this.acceptedLanguages;133 return accepted.length134 ? ~accepted.indexOf(lang)135 : true;

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 136why, one step back
136};

mergedTj Holowaychukbefore adoption547d18c

lines 137–157why, one step back
137
138/**139 * Return an array of Accepted media types140 * ordered from highest quality to lowest.141 *142 * Examples:143 *144 * [ { value: 'application/json',145 * quality: 1,146 * type: 'application',147 * subtype: 'json' },148 * { value: 'text/html',149 * quality: 0.5,150 * type: 'text',151 * subtype: 'html' } ]152 *153 * @return {Array}154 * @api public155 */156157req.__defineGetter__('accepted', function(){

Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964

line 158why, one step back
158 var accept = this.get('Accept');

mergedTj Holowaychukbefore adoption547d18c

lines 159–177why, one step back
159 return accept
160 ? utils.parseAccept(accept)161 : [];162});163164/**165 * Return an array of Accepted languages166 * ordered from highest quality to lowest.167 *168 * Examples:169 *170 * Accept-Language: en;q=.5, en-us171 * ['en-us', 'en']172 *173 * @return {Array}174 * @api public175 */176177req.__defineGetter__('acceptedLanguages', function(){

Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964

line 178why, one step back
178 var accept = this.get('Accept-Language');

mergedTj Holowaychukbefore adoption547d18c

lines 179–201why, one step back
179 return accept
180 ? utils181 .parseQuality(accept)182 .map(function(obj){183 return obj.value;184 })185 : [];186});187188/**189 * Return an array of Accepted charsets190 * ordered from highest quality to lowest.191 *192 * Examples:193 *194 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8195 * ['unicode-1-1', 'iso-8859-5']196 *197 * @return {Array}198 * @api public199 */200201req.__defineGetter__('acceptedCharsets', function(){

Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964

line 202why, one step back
202 var accept = this.get('Accept-Charset');

mergedTj Holowaychukbefore adoption547d18c

lines 203–210why, one step back
203 return accept
204 ? utils205 .parseQuality(accept)206 .map(function(obj){207 return obj.value;208 })209 : [];210});

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 211–214why, one step back
211
212/**213 * Return the value of param `name` when present or `defaultValue`.214 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 216–218why, one step back
216 * - Checks route placeholders, ex: _/user/:id_
217 * - Checks query string params, ex: ?id=12218 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

line 219why, one step back
219 * To utilize request bodies, `req.body`

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 220why, one step back
220 * should be an object. This can be done by using

req.body takes precedenceTj Holowaychukbefore adoptione49c482

line 221why, one step back
221 * the `connect.bodyParser()` middleware.

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 222–229why, one step back
222 *
223 * @param {String} name224 * @param {Mixed} defaultValue225 * @return {String}226 * @api public227 */228229req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

lines 230–233why, one step back
230 // req.body
231 if (this.body && undefined !== this.body[name]) return this.body[name];232233 // route params

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 234–239why, one step back
234 if (this.params
235 && this.params.hasOwnProperty(name)236 && undefined !== this.params[name]) {237 return this.params[name]; 238 }239

req.body takes precedenceTj Holowaychukbefore adoptione49c482

lines 240–241why, one step back
240 // query-string
241 if (undefined !== this.query[name]) return this.query[name];

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 242–254why, one step back
242
243 return defaultValue;244};245246/**247 * Check if the incoming request contains the "Content-Type" 248 * header field, and it contains the give mime `type`.249 *250 * Examples:251 *252 * // With Content-Type: text/html; charset=utf-8253 * req.is('html');254 * req.is('text/html');

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

line 255why, one step back
255 * req.is('text/*');

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 256–260why, one step back
256 * // => true
257 * 258 * // When Content-Type is application/json259 * req.is('json');260 * req.is('application/json');

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

line 261why, one step back
261 * req.is('application/*');

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 262–270why, one step back
262 * // => true
263 * 264 * req.is('html');265 * // => false266 * 267 * Now within our route callbacks, we can use to to assert content types268 * such as "image/jpeg", "image/png", etc.269 * 270 * app.post('/image/upload', function(req, res, next){

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

line 271why, one step back
271 * if (req.is('image/*')) {

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 272–283why, one step back
272 * // do something
273 * } else {274 * next();275 * }276 * });277 * 278 * @param {String} type279 * @return {Boolean}280 * @api public281 */282283req.is = function(type){

Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964

line 284why, one step back
284 var ct = this.get('Content-Type');

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

lines 285–286why, one step back
285 if (!ct) return false;
286 ct = ct.split(';')[0];

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 287–289why, one step back
287 if (!~type.indexOf('/')) type = mime.lookup(type);
288 if (~type.indexOf('*')) {289 type = type.split('/');

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

lines 290–293why, one step back
290 ct = ct.split('/');
291 if ('*' == type[0] && type[1] == ct[1]) return true;292 if ('*' == type[1] && type[0] == ct[0]) return true;293 return false;

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 294why, one step back
294 }

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

line 295why, one step back
295 return !! ~ct.indexOf(type);

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 296–298why, one step back
296};
297298/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

lines 299–304why, one step back
299 * Return the protocol string "http" or "https"
300 * when requested with TLS. When the "trust proxy" 301 * setting is enabled the "X-Forwarded-Proto" header302 * field will be trusted. If you're running behind303 * a reverse proxy that supplies https for you this304 * may be enabled.

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 305–309why, one step back
305 *
306 * @return {String}307 * @api public308 */309

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

lines 310–311why, one step back
310req.__defineGetter__('protocol', function(trustProxy){
311 var trustProxy = this.app.settings['trust proxy'];

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

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 313–314why, one step back
313 ? 'https'
314 : trustProxy

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 315why, one step back
315 ? (this.get('X-Forwarded-Proto') || 'http')

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

line 316why, one step back
316 : 'http';

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 317why, one step back
317});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 318–319why, one step back
318
319/**

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

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 321why, one step back
321 *

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

lines 322–323why, one step back
322 * req.protocol == 'https'
323 *

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 324–328why, one step back
324 * @return {Boolean}
325 * @api public326 */327328req.__defineGetter__('secure', function(){

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

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 330–332why, one step back
330});
331332/**

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

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

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

lines 336–339why, one step back
336 * For example if the value were "client, proxy1, proxy2"
337 * you would receive the array `["proxy2", "proxy1", "client"]`338 * where "proxy2" is the furthest down-stream.339 *

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 340–344why, one step back
340 * @return {Array}
341 * @api public342 */343344req.__defineGetter__('ips', function(){

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

lines 345–346why, one step back
345 var val = this.get('X-Forwarded-For');
346 return val

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

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

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 348–351why, one step back
348 : [];
349});350351/**

Added `req.subdomains`TJ Holowaychukbefore adoption4e33245

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

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 369–393why, one step back
369 * Short-hand for `require('url').parse(req.url).pathname`.
370 *371 * @return {String}372 * @api public373 */374375req.__defineGetter__('path', function(){376 return parse(this.url).pathname;377});378379/**380 * Check if the request is fresh, aka381 * Last-Modified and/or the ETag382 * still match.383 *384 * @return {Boolean}385 * @api public386 */387388req.__defineGetter__('fresh', function(){389 return ! this.stale;390});391392/**393 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

line 394why, one step back
394 * "Last-Modified" and / or the "ETag" for the

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

lines 395–412why, one step back
395 * resource has changed.
396 *397 * @return {Boolean}398 * @api public399 */400401req.__defineGetter__('stale', function(){402 return connect.utils.modified(this, this.res);403});404405/**406 * Check if the request was an _XMLHttpRequest_.407 *408 * @return {Boolean}409 * @api public410 */411412req.__defineGetter__('xhr', function(){

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

lines 413–414why, one step back
413 var val = this.get('X-Requested-With') || '';
414 return 'xmlhttprequest' == val.toLowerCase();

Added app.{request,response}Tj Holowaychukbefore adoption94d1e94

line 415why, one step back
415});