Skip to content
Myr

request.js

This file

Path

lib/request.js

418 lines · 8.8 KiB · JavaScript

Last changed by

docsTJ Holowaychukbefore adoption27b1b5e

History of this file

Take it

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

fixed headTj Holowaychukbefore adoptionfc1e849

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.

fixed headTj Holowaychukbefore adoptionfc1e849

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');

fixed headTj Holowaychukbefore adoptionfc1e849

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');

fixed headTj Holowaychukbefore adoptionfc1e849

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');

fixed headTj Holowaychukbefore adoptionfc1e849

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

docsTJ Holowaychukbefore adoption27b1b5e

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 46–50why, one step back
46 * @param {String} name
47 * @return {String} 48 * @api public49 */50

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 58why, one step back
58 default:

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 60–63why, one step back
60 }
61};6263/**

docsTJ Holowaychukbefore adoption4e2677f

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 67why, one step back
67 *

docsTJ Holowaychukbefore adoption365a98d

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

docsTJ Holowaychukbefore adoption365a98d

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 79why, one step back
79 *

typoTJ Holowaychukbefore adoptionba570a9

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

docsTJ Holowaychukbefore adoption365a98d

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

docsTJ Holowaychukbefore adoption365a98d

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

docsTJ Holowaychukbefore adoption365a98d

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 89why, one step back
89 *

typoTJ Holowaychukbefore adoptionba570a9

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

docsTJ Holowaychukbefore adoption365a98d

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 94why, one step back
94 *

typoTJ Holowaychukbefore adoptionba570a9

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption365a98d

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption4e2677f

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 102–105why, one step back
102 * @api public
103 */104105req.accepts = function(type){

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

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

mergedTj Holowaychukbefore adoption547d18c

line 107why, one step back
107};

fixed headTj Holowaychukbefore adoptionfc1e849

line 108why, one step back
108

mergedTj Holowaychukbefore adoption547d18c

lines 109–116why, one step back
109/**
110 * Check if the given `charset` is acceptable,111 * otherwise you should respond with 406 "Not Acceptable".112 *113 * @param {String} charset114 * @return {Boolean}115 * @api public116 */

fixed headTj Holowaychukbefore adoptionfc1e849

line 117why, one step back
117

mergedTj Holowaychukbefore adoption547d18c

lines 118–123why, one step back
118req.acceptsCharset = function(charset){
119 var accepted = this.acceptedCharsets;120 return accepted.length121 ? ~accepted.indexOf(charset)122 : true;123};

fixed headTj Holowaychukbefore adoptionfc1e849

line 124why, one step back
124

mergedTj Holowaychukbefore adoption547d18c

lines 125–132why, one step back
125/**
126 * Check if the given `lang` is acceptable,127 * otherwise you should respond with 406 "Not Acceptable".128 *129 * @param {String} lang130 * @return {Boolean}131 * @api public132 */

fixed headTj Holowaychukbefore adoptionfc1e849

line 133why, one step back
133

mergedTj Holowaychukbefore adoption547d18c

lines 134–138why, one step back
134req.acceptsLanguage = function(lang){
135 var accepted = this.acceptedLanguages;136 return accepted.length137 ? ~accepted.indexOf(lang)138 : true;

fixed headTj Holowaychukbefore adoptionfc1e849

line 139why, one step back
139};

mergedTj Holowaychukbefore adoption547d18c

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

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 162–180why, one step back
162 return accept
163 ? utils.parseAccept(accept)164 : [];165});166167/**168 * Return an array of Accepted languages169 * ordered from highest quality to lowest.170 *171 * Examples:172 *173 * Accept-Language: en;q=.5, en-us174 * ['en-us', 'en']175 *176 * @return {Array}177 * @api public178 */179180req.__defineGetter__('acceptedLanguages', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 182–204why, one step back
182 return accept
183 ? utils184 .parseQuality(accept)185 .map(function(obj){186 return obj.value;187 })188 : [];189});190191/**192 * Return an array of Accepted charsets193 * ordered from highest quality to lowest.194 *195 * Examples:196 *197 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8198 * ['unicode-1-1', 'iso-8859-5']199 *200 * @return {Array}201 * @api public202 */203204req.__defineGetter__('acceptedCharsets', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 206–213why, one step back
206 return accept
207 ? utils208 .parseQuality(accept)209 .map(function(obj){210 return obj.value;211 })212 : [];213});

fixed headTj Holowaychukbefore adoptionfc1e849

lines 214–217why, one step back
214
215/**216 * Return the value of param `name` when present or `defaultValue`.217 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 219–221why, one step back
219 * - Checks route placeholders, ex: _/user/:id_
220 * - Checks query string params, ex: ?id=12221 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 225–232why, one step back
225 *
226 * @param {String} name227 * @param {Mixed} defaultValue228 * @return {String}229 * @api public230 */231232req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 245–257why, one step back
245
246 return defaultValue;247};248249/**250 * Check if the incoming request contains the "Content-Type" 251 * header field, and it contains the give mime `type`.252 *253 * Examples:254 *255 * // With Content-Type: text/html; charset=utf-8256 * req.is('html');257 * req.is('text/html');

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 275–286why, one step back
275 * // do something
276 * } else {277 * next();278 * }279 * });280 * 281 * @param {String} type282 * @return {Boolean}283 * @api public284 */285286req.is = function(type){

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

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

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

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

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 297why, one step back
297 }

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 299–301why, one step back
299};
300301/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 308–312why, one step back
308 *
309 * @return {String}310 * @api public311 */312

populate changelogTJ Holowaychukbefore adoptionabe5cc7

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 314why, one step back
314 var trustProxy = this.app.settings['trust proxy'];

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

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 320why, one step back
320});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 324why, one step back
324 *

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 327–331why, one step back
327 * @return {Boolean}
328 * @api public329 */330331req.__defineGetter__('secure', function(){

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

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 333–335why, one step back
333});
334335/**

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

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

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

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

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 343–347why, one step back
343 * @return {Array}
344 * @api public345 */346347req.__defineGetter__('ips', function(){

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

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

reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c

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

Added `req.ips`TJ Holowaychukbefore adoptiondd468fb

lines 351–354why, one step back
351 : [];
352});353354/**

Added `req.subdomains`TJ Holowaychukbefore adoption4e33245

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

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 373–396why, one step back
373 *
374 * @return {String}375 * @api public376 */377378req.__defineGetter__('path', function(){379 return parse(this.url).pathname;380});381382/**383 * Check if the request is fresh, aka384 * Last-Modified and/or the ETag385 * still match.386 *387 * @return {Boolean}388 * @api public389 */390391req.__defineGetter__('fresh', function(){392 return ! this.stale;393});394395/**396 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

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

fixed headTj Holowaychukbefore adoptionfc1e849

lines 398–415why, one step back
398 * resource has changed.
399 *400 * @return {Boolean}401 * @api public402 */403404req.__defineGetter__('stale', function(){405 return connect.utils.modified(this, this.res);406});407408/**409 * Check if the request was an _XMLHttpRequest_.410 *411 * @return {Boolean}412 * @api public413 */414415req.__defineGetter__('xhr', function(){

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

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

fixed headTj Holowaychukbefore adoptionfc1e849

line 418why, one step back
418});