Skip to content
Myr

request.js

This file

Path

lib/request.js

417 lines · 8.8 KiB · JavaScript

Last changed by

update headersTJ Holowaychukbefore adoptiona376980

History of this file

Take it

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

specTj Holowaychukbefore adoptionb4ce57c

lines 1–3why, one step back
1
2/*!3 * Express - request

update headersTJ Holowaychukbefore adoptiona376980

line 4why, one step back
4 * Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca>

specTj Holowaychukbefore adoptionb4ce57c

lines 5–14why, one step back
5 * MIT Licensed
6 */78/**9 * Module dependencies.10 */1112var http = require('http')13 , utils = require('./utils')14 , connect = require('connect')

Perf: memoize parsed urlsTJ Holowaychukbefore adoption73092c9

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

specTj Holowaychukbefore adoptionb4ce57c

lines 16–26why, one step back
16 , 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.

specTj Holowaychukbefore adoptionb4ce57c

lines 28–29why, one step back
28 *
29 * The `Referrer` header field is special-cased,

doc typoTJ Holowaychukbefore adoptionb5cee82

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

specTj Holowaychukbefore adoptionb4ce57c

lines 31–33why, one step back
31 *
32 * Examples:33 *

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

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

specTj Holowaychukbefore adoptionb4ce57c

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

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

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

specTj Holowaychukbefore adoptionb4ce57c

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

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

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

specTj Holowaychukbefore adoptionb4ce57c

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

docsTJ Holowaychukbefore adoption27b1b5e

lines 42–44why, one step back
42 *
43 * Aliased as `req.header()`.44 *

specTj Holowaychukbefore adoptionb4ce57c

lines 45–49why, one step back
45 * @param {String} name
46 * @return {String} 47 * @api public48 */49

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

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

specTj Holowaychukbefore adoptionb4ce57c

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

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

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

specTj Holowaychukbefore adoptionb4ce57c

line 57why, one step back
57 default:

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 59–62why, one step back
59 }
60};6162/**

docsTJ Holowaychukbefore adoption4e2677f

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

specTj Holowaychukbefore adoptionb4ce57c

line 66why, one step back
66 *

docsTJ Holowaychukbefore adoption365a98d

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

specTj Holowaychukbefore adoptionb4ce57c

lines 73–76why, one step back
73 * Examples:
74 * 75 * // Accept: text/html76 * req.accepts('html');

docsTJ Holowaychukbefore adoption365a98d

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

specTj Holowaychukbefore adoptionb4ce57c

line 78why, one step back
78 *

typoTJ Holowaychukbefore adoptionba570a9

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

specTj Holowaychukbefore adoptionb4ce57c

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

docsTJ Holowaychukbefore adoption365a98d

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

specTj Holowaychukbefore adoptionb4ce57c

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

docsTJ Holowaychukbefore adoption365a98d

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

specTj Holowaychukbefore adoptionb4ce57c

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

docsTJ Holowaychukbefore adoption365a98d

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

specTj Holowaychukbefore adoptionb4ce57c

line 88why, one step back
88 *

typoTJ Holowaychukbefore adoptionba570a9

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

specTj Holowaychukbefore adoptionb4ce57c

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

docsTJ Holowaychukbefore adoption365a98d

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

specTj Holowaychukbefore adoptionb4ce57c

line 93why, one step back
93 *

typoTJ Holowaychukbefore adoptionba570a9

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption365a98d

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption4e2677f

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

specTj Holowaychukbefore adoptionb4ce57c

lines 101–104why, one step back
101 * @api public
102 */103104req.accepts = function(type){

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 106–159why, one step back
106};
107108/**109 * Check if the given `charset` is acceptable,110 * otherwise you should respond with 406 "Not Acceptable".111 *112 * @param {String} charset113 * @return {Boolean}114 * @api public115 */116117req.acceptsCharset = function(charset){118 var accepted = this.acceptedCharsets;119 return accepted.length120 ? ~accepted.indexOf(charset)121 : true;122};123124/**125 * Check if the given `lang` is acceptable,126 * otherwise you should respond with 406 "Not Acceptable".127 *128 * @param {String} lang129 * @return {Boolean}130 * @api public131 */132133req.acceptsLanguage = function(lang){134 var accepted = this.acceptedLanguages;135 return accepted.length136 ? ~accepted.indexOf(lang)137 : true;138};139140/**141 * Return an array of Accepted media types142 * ordered from highest quality to lowest.143 *144 * Examples:145 *146 * [ { value: 'application/json',147 * quality: 1,148 * type: 'application',149 * subtype: 'json' },150 * { value: 'text/html',151 * quality: 0.5,152 * type: 'text',153 * subtype: 'html' } ]154 *155 * @return {Array}156 * @api public157 */158159req.__defineGetter__('accepted', function(){

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 161–179why, one step back
161 return accept
162 ? utils.parseAccept(accept)163 : [];164});165166/**167 * Return an array of Accepted languages168 * ordered from highest quality to lowest.169 *170 * Examples:171 *172 * Accept-Language: en;q=.5, en-us173 * ['en-us', 'en']174 *175 * @return {Array}176 * @api public177 */178179req.__defineGetter__('acceptedLanguages', function(){

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 181–203why, one step back
181 return accept
182 ? utils183 .parseQuality(accept)184 .map(function(obj){185 return obj.value;186 })187 : [];188});189190/**191 * Return an array of Accepted charsets192 * ordered from highest quality to lowest.193 *194 * Examples:195 *196 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8197 * ['unicode-1-1', 'iso-8859-5']198 *199 * @return {Array}200 * @api public201 */202203req.__defineGetter__('acceptedCharsets', function(){

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 205–256why, one step back
205 return accept
206 ? utils207 .parseQuality(accept)208 .map(function(obj){209 return obj.value;210 })211 : [];212});213214/**215 * Return the value of param `name` when present or `defaultValue`.216 *217 * - Checks body params, ex: id=12, {"id":12}218 * - Checks route placeholders, ex: _/user/:id_219 * - Checks query string params, ex: ?id=12220 *221 * To utilize request bodies, `req.body`222 * should be an object. This can be done by using223 * the `connect.bodyParser()` middleware.224 *225 * @param {String} name226 * @param {Mixed} defaultValue227 * @return {String}228 * @api public229 */230231req.param = function(name, defaultValue){232 // req.body233 if (this.body && undefined !== this.body[name]) return this.body[name];234235 // route params236 if (this.params237 && this.params.hasOwnProperty(name)238 && undefined !== this.params[name]) {239 return this.params[name]; 240 }241242 // query-string243 if (undefined !== this.query[name]) return this.query[name]; 244245 return defaultValue;246};247248/**249 * Check if the incoming request contains the "Content-Type" 250 * header field, and it contains the give mime `type`.251 *252 * Examples:253 *254 * // With Content-Type: text/html; charset=utf-8255 * req.is('html');256 * req.is('text/html');

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

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

specTj Holowaychukbefore adoptionb4ce57c

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

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 264–269why, one step back
264 * // => true
265 * 266 * req.is('html');267 * // => false268 * 269 * Now within our route callbacks, we can use to to assert content types

docsTJ Holowaychukbefore adoption18a6bbf

line 270why, one step back
270 * such as "image/jpeg", "image/png" as shown here:

specTj Holowaychukbefore adoptionb4ce57c

lines 271–272why, one step back
271 *
272 * app.post('/image/upload', function(req, res, next){

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 274–285why, one step back
274 * // do something
275 * } else {276 * next();277 * }278 * });279 * 280 * @param {String} type281 * @return {Boolean}282 * @api public283 */284285req.is = function(type){

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

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

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

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

specTj Holowaychukbefore adoptionb4ce57c

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

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

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

specTj Holowaychukbefore adoptionb4ce57c

line 296why, one step back
296 }

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 298–300why, one step back
298};
299300/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

lines 301–306why, one step back
301 * Return the protocol string "http" or "https"
302 * when requested with TLS. When the "trust proxy" 303 * setting is enabled the "X-Forwarded-Proto" header304 * field will be trusted. If you're running behind305 * a reverse proxy that supplies https for you this306 * may be enabled.

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 307–311why, one step back
307 *
308 * @return {String}309 * @api public310 */311

populate changelogTJ Holowaychukbefore adoptionabe5cc7

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

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

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

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

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 315–316why, one step back
315 ? 'https'
316 : trustProxy

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 319why, one step back
319});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 320–321why, one step back
320
321/**

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

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

specTj Holowaychukbefore adoptionb4ce57c

line 323why, one step back
323 *

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

lines 324–325why, one step back
324 * req.protocol == 'https'
325 *

specTj Holowaychukbefore adoptionb4ce57c

lines 326–330why, one step back
326 * @return {Boolean}
327 * @api public328 */329330req.__defineGetter__('secure', function(){

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

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

specTj Holowaychukbefore adoptionb4ce57c

lines 332–334why, one step back
332});
333334/**

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

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

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

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

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 342–346why, one step back
342 * @return {Array}
343 * @api public344 */345346req.__defineGetter__('ips', function(){

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

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

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

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

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 350–353why, one step back
350 : [];
351});352353/**

Added `req.subdomains`TJ Holowaychukbefore adoption4e33245

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

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

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

specTj Holowaychukbefore adoptionb4ce57c

lines 372–377why, one step back
372 *
373 * @return {String}374 * @api public375 */376377req.__defineGetter__('path', function(){

Perf: memoize parsed urlsTJ Holowaychukbefore adoption73092c9

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

specTj Holowaychukbefore adoptionb4ce57c

lines 379–395why, one step back
379});
380381/**382 * Check if the request is fresh, aka383 * Last-Modified and/or the ETag384 * still match.385 *386 * @return {Boolean}387 * @api public388 */389390req.__defineGetter__('fresh', function(){391 return ! this.stale;392});393394/**395 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

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

specTj Holowaychukbefore adoptionb4ce57c

lines 397–414why, one step back
397 * resource has changed.
398 *399 * @return {Boolean}400 * @api public401 */402403req.__defineGetter__('stale', function(){404 return connect.utils.modified(this, this.res);405});406407/**408 * Check if the request was an _XMLHttpRequest_.409 *410 * @return {Boolean}411 * @api public412 */413414req.__defineGetter__('xhr', function(){

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

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

specTj Holowaychukbefore adoptionb4ce57c

line 417why, one step back
417});