Skip to content
Myr

request.js

This file

Path

lib/request.js

391 lines · 8.1 KiB · JavaScript

Last changed by

more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8

History of this file

Take it

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 1–16why, 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');

no longer manipulate the req/res protos directlyTj Holowaychukbefore adoptiond249868

lines 17–24why, one step back
17
18/**19 * Request prototype.20 */2122var req = exports = module.exports = {23 __proto__: http.IncomingMessage.prototype24};

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 25–26why, one step back
25
26/**

doc typoTJ Holowaychukbefore adoptionfe27989

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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){

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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;

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 55why, one step back
55 default:

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

line 61why, one step back
61 * Check if the given `type(s)` is acceptable,

mergedTj Holowaychukbefore adoption547d18c

line 62why, one step back
62 * otherwise you should respond with 406 "Not Acceptable".

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 63–76why, one step back
63 *
64 * Examples:65 * 66 * // Accept: text/html67 * req.accepts('html');68 * // => true69 *70 * // Accept: text/*; application/json71 * req.accepts('html');72 * req.accepts('text/html');73 * req.accepts('text/plain');74 * req.accepts('application/json');75 * // => true76 *

more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 78–81why, one step back
78 * req.accepts('image/png');
79 * req.accepts('png');80 * // => false81 *

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8

lines 88–92why, one step back
88 * // => "application/json"
89 *90 * // Accept: text/plain91 * req.accepts(['text/html', 'application/json']);92 * // => undefined

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 96–99why, one step back
96 * @api public
97 */9899req.accepts = function(type){

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

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

mergedTj Holowaychukbefore adoption547d18c

line 101why, one step back
101};

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 102why, one step back
102

mergedTj Holowaychukbefore adoption547d18c

lines 103–110why, one step back
103/**
104 * Check if the given `charset` is acceptable,105 * otherwise you should respond with 406 "Not Acceptable".106 *107 * @param {String} charset108 * @return {Boolean}109 * @api public110 */

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 111why, one step back
111

mergedTj Holowaychukbefore adoption547d18c

lines 112–117why, one step back
112req.acceptsCharset = function(charset){
113 var accepted = this.acceptedCharsets;114 return accepted.length115 ? ~accepted.indexOf(charset)116 : true;117};

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 118why, one step back
118

mergedTj Holowaychukbefore adoption547d18c

lines 119–126why, one step back
119/**
120 * Check if the given `lang` is acceptable,121 * otherwise you should respond with 406 "Not Acceptable".122 *123 * @param {String} lang124 * @return {Boolean}125 * @api public126 */

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 127why, one step back
127

mergedTj Holowaychukbefore adoption547d18c

lines 128–132why, one step back
128req.acceptsLanguage = function(lang){
129 var accepted = this.acceptedLanguages;130 return accepted.length131 ? ~accepted.indexOf(lang)132 : true;

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 133why, one step back
133};

mergedTj Holowaychukbefore adoption547d18c

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

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 156–174why, one step back
156 return accept
157 ? utils.parseAccept(accept)158 : [];159});160161/**162 * Return an array of Accepted languages163 * ordered from highest quality to lowest.164 *165 * Examples:166 *167 * Accept-Language: en;q=.5, en-us168 * ['en-us', 'en']169 *170 * @return {Array}171 * @api public172 */173174req.__defineGetter__('acceptedLanguages', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 176–198why, one step back
176 return accept
177 ? utils178 .parseQuality(accept)179 .map(function(obj){180 return obj.value;181 })182 : [];183});184185/**186 * Return an array of Accepted charsets187 * ordered from highest quality to lowest.188 *189 * Examples:190 *191 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8192 * ['unicode-1-1', 'iso-8859-5']193 *194 * @return {Array}195 * @api public196 */197198req.__defineGetter__('acceptedCharsets', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 200–207why, one step back
200 return accept
201 ? utils202 .parseQuality(accept)203 .map(function(obj){204 return obj.value;205 })206 : [];207});

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 208–211why, one step back
208
209/**210 * Return the value of param `name` when present or `defaultValue`.211 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 213–215why, one step back
213 * - Checks route placeholders, ex: _/user/:id_
214 * - Checks query string params, ex: ?id=12215 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 219–226why, one step back
219 *
220 * @param {String} name221 * @param {Mixed} defaultValue222 * @return {String}223 * @api public224 */225226req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 239–251why, one step back
239
240 return defaultValue;241};242243/**244 * Check if the incoming request contains the "Content-Type" 245 * header field, and it contains the give mime `type`.246 *247 * Examples:248 *249 * // With Content-Type: text/html; charset=utf-8250 * req.is('html');251 * req.is('text/html');

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 253–257why, one step back
253 * // => true
254 * 255 * // When Content-Type is application/json256 * req.is('json');257 * req.is('application/json');

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 269–280why, one step back
269 * // do something
270 * } else {271 * next();272 * }273 * });274 * 275 * @param {String} type276 * @return {Boolean}277 * @api public278 */279280req.is = function(type){

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

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

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

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

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

line 291why, one step back
291 }

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

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 293–295why, one step back
293};
294295/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

lines 296–301why, one step back
296 * Return the protocol string "http" or "https"
297 * when requested with TLS. When the "trust proxy" 298 * setting is enabled the "X-Forwarded-Proto" header299 * field will be trusted. If you're running behind300 * a reverse proxy that supplies https for you this301 * may be enabled.

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 302–306why, one step back
302 *
303 * @return {String}304 * @api public305 */306

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 309–311why, one step back
309 return this.secure
310 ? 'https'311 : trustProxy

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 314why, one step back
314});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 315–316why, one step back
315
316/**

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 317–323why, one step back
317 * Short-hand for `req.connection.encrypted`.
318 *319 * @return {Boolean}320 * @api public321 */322323req.__defineGetter__('secure', function(){

Added X-Forwarded-Proto support to `res.redirect()`. Closes #927Tj Holowaychukbefore adoption5757f87

line 324why, one step back
324 return this.connection.encrypted;

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 325–327why, one step back
325});
326327/**

Added `req.subdomains`TJ Holowaychukbefore adoption4e33245

lines 328–344why, one step back
328 * Return subdomains as an array.
329 *330 * For example "tobi.ferrets.example.com"331 * would provide `["ferrets", "tobi"]`.332 *333 * @return {Array}334 * @api public335 */336337req.__defineGetter__('subdomains', function(){338 return this.get('Host')339 .split('.')340 .slice(0, -2)341 .reverse();342});343344/**

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 345–369why, one step back
345 * Short-hand for `require('url').parse(req.url).pathname`.
346 *347 * @return {String}348 * @api public349 */350351req.__defineGetter__('path', function(){352 return parse(this.url).pathname;353});354355/**356 * Check if the request is fresh, aka357 * Last-Modified and/or the ETag358 * still match.359 *360 * @return {Boolean}361 * @api public362 */363364req.__defineGetter__('fresh', function(){365 return ! this.stale;366});367368/**369 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

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

removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c

lines 371–387why, one step back
371 * resource has changed.
372 *373 * @return {Boolean}374 * @api public375 */376377req.__defineGetter__('stale', function(){378 return connect.utils.modified(this, this.res);379});380381/**382 * Check if the request was an _XMLHttpRequest_.383 *384 * @return {Boolean}385 * @api public386 */387

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

line 388why, one step back
388req.__defineGetter__('xhr', function(){

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

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

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

line 391why, one step back
391});