Skip to content
Myr

request.js

This file

Path

lib/request.js

392 lines · 8.1 KiB · JavaScript

Last changed by

docsTJ Holowaychukbefore adoption4e2677f

History of this file

Take it

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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};

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

doc typoTJ Holowaychukbefore adoptionfe27989

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

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 res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 64–77why, one step back
64 *
65 * Examples:66 * 67 * // Accept: text/html68 * req.accepts('html');69 * // => true70 *71 * // Accept: text/*; application/json72 * req.accepts('html');73 * req.accepts('text/html');74 * req.accepts('text/plain');75 * req.accepts('application/json');76 * // => true77 *

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

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

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

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

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

docsTJ Holowaychukbefore adoption4e2677f

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 97–100why, one step back
97 * @api public
98 */99100req.accepts = function(type){

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

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

mergedTj Holowaychukbefore adoption547d18c

line 102why, one step back
102};

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 103why, one step back
103

mergedTj Holowaychukbefore adoption547d18c

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 112why, one step back
112

mergedTj Holowaychukbefore adoption547d18c

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 119why, one step back
119

mergedTj Holowaychukbefore adoption547d18c

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 128why, one step back
128

mergedTj Holowaychukbefore adoption547d18c

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 134why, one step back
134};

mergedTj Holowaychukbefore adoption547d18c

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

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 157–175why, one step back
157 return accept
158 ? utils.parseAccept(accept)159 : [];160});161162/**163 * Return an array of Accepted languages164 * ordered from highest quality to lowest.165 *166 * Examples:167 *168 * Accept-Language: en;q=.5, en-us169 * ['en-us', 'en']170 *171 * @return {Array}172 * @api public173 */174175req.__defineGetter__('acceptedLanguages', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 177–199why, one step back
177 return accept
178 ? utils179 .parseQuality(accept)180 .map(function(obj){181 return obj.value;182 })183 : [];184});185186/**187 * Return an array of Accepted charsets188 * ordered from highest quality to lowest.189 *190 * Examples:191 *192 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8193 * ['unicode-1-1', 'iso-8859-5']194 *195 * @return {Array}196 * @api public197 */198199req.__defineGetter__('acceptedCharsets', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 220–227why, one step back
220 *
221 * @param {String} name222 * @param {Mixed} defaultValue223 * @return {String}224 * @api public225 */226227req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 270–281why, one step back
270 * // do something
271 * } else {272 * next();273 * }274 * });275 * 276 * @param {String} type277 * @return {Boolean}278 * @api public279 */280281req.is = function(type){

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

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

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 292why, one step back
292 }

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 294–296why, one step back
294};
295296/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 303–307why, one step back
303 *
304 * @return {String}305 * @api public306 */307

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 315why, one step back
315});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

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

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

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 326–328why, one step back
326});
327328/**

Added `req.subdomains`TJ Holowaychukbefore adoption4e33245

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 346–370why, one step back
346 * Short-hand for `require('url').parse(req.url).pathname`.
347 *348 * @return {String}349 * @api public350 */351352req.__defineGetter__('path', function(){353 return parse(this.url).pathname;354});355356/**357 * Check if the request is fresh, aka358 * Last-Modified and/or the ETag359 * still match.360 *361 * @return {Boolean}362 * @api public363 */364365req.__defineGetter__('fresh', function(){366 return ! this.stale;367});368369/**370 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

lines 372–389why, one step back
372 * resource has changed.
373 *374 * @return {Boolean}375 * @api public376 */377378req.__defineGetter__('stale', function(){379 return connect.utils.modified(this, this.res);380});381382/**383 * Check if the request was an _XMLHttpRequest_.384 *385 * @return {Boolean}386 * @api public387 */388389req.__defineGetter__('xhr', function(){

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

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

Added res.attachment() testsTj Holowaychukbefore adoption0e39597

line 392why, one step back
392});