Skip to content
Myr

request.js

This file

Path

lib/request.js

364 lines · 7.5 KiB · JavaScript

Last changed by

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

History of this file

Take it

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 25–35why, one step back
25
26/**27 * Return request header or optional default.28 *29 * The `Referrer` header field is special-cased,30 * both `Referrer` and `Referer` will yield are31 * interchangeable.32 *33 * Examples:34 *35 * req.header('Content-Type');

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 37–39why, one step back
37 * // => "text/plain"
38 * 39 * req.header('content-type');

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 41–42why, one step back
41 * // => "text/plain"
42 *

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

line 43why, one step back
43 * req.header('Something');

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 44–50why, one step back
44 * // => undefined
45 * 46 * @param {String} name47 * @return {String} 48 * @api public49 */50

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

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;

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 58why, one step back
58 default:

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

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

mergedTj Holowaychukbefore adoption547d18c

lines 64–65why, one step back
64 * Check if the given `type` is acceptable,
65 * otherwise you should respond with 406 "Not Acceptable".

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 66–89why, one step back
66 *
67 * Examples:68 * 69 * // Accept: text/html70 * req.accepts('html');71 * // => true72 *73 * // Accept: text/*; application/json74 * req.accepts('html');75 * req.accepts('text/html');76 * req.accepts('text/plain');77 * req.accepts('application/json');78 * // => true79 *80 * req.accepts('image/png');81 * req.accepts('png');82 * // => false83 *84 * @param {String} type85 * @return {Boolean}86 * @api public87 */8889req.accepts = function(type){

mergedTj Holowaychukbefore adoption547d18c

lines 90–91why, one step back
90 return utils.accepts(type, this.header('Accept'));
91};

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 92why, one step back
92

mergedTj Holowaychukbefore adoption547d18c

lines 93–100why, one step back
93/**
94 * Check if the given `charset` is acceptable,95 * otherwise you should respond with 406 "Not Acceptable".96 *97 * @param {String} charset98 * @return {Boolean}99 * @api public100 */

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 101why, one step back
101

mergedTj Holowaychukbefore adoption547d18c

lines 102–107why, one step back
102req.acceptsCharset = function(charset){
103 var accepted = this.acceptedCharsets;104 return accepted.length105 ? ~accepted.indexOf(charset)106 : true;107};

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 108why, one step back
108

mergedTj Holowaychukbefore adoption547d18c

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 117why, one step back
117

mergedTj Holowaychukbefore adoption547d18c

lines 118–122why, one step back
118req.acceptsLanguage = function(lang){
119 var accepted = this.acceptedLanguages;120 return accepted.length121 ? ~accepted.indexOf(lang)122 : true;

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 123why, one step back
123};

mergedTj Holowaychukbefore adoption547d18c

lines 124–197why, one step back
124
125/**126 * Return an array of Accepted media types127 * ordered from highest quality to lowest.128 *129 * Examples:130 *131 * [ { value: 'application/json',132 * quality: 1,133 * type: 'application',134 * subtype: 'json' },135 * { value: 'text/html',136 * quality: 0.5,137 * type: 'text',138 * subtype: 'html' } ]139 *140 * @return {Array}141 * @api public142 */143144req.__defineGetter__('accepted', function(){145 var accept = this.header('Accept');146 return accept147 ? utils.parseAccept(accept)148 : [];149});150151/**152 * Return an array of Accepted languages153 * ordered from highest quality to lowest.154 *155 * Examples:156 *157 * Accept-Language: en;q=.5, en-us158 * ['en-us', 'en']159 *160 * @return {Array}161 * @api public162 */163164req.__defineGetter__('acceptedLanguages', function(){165 var accept = this.header('Accept-Language');166 return accept167 ? utils168 .parseQuality(accept)169 .map(function(obj){170 return obj.value;171 })172 : [];173});174175/**176 * Return an array of Accepted charsets177 * ordered from highest quality to lowest.178 *179 * Examples:180 *181 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8182 * ['unicode-1-1', 'iso-8859-5']183 *184 * @return {Array}185 * @api public186 */187188req.__defineGetter__('acceptedCharsets', function(){189 var accept = this.header('Accept-Charset');190 return accept191 ? utils192 .parseQuality(accept)193 .map(function(obj){194 return obj.value;195 })196 : [];197});

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 198–201why, one step back
198
199/**200 * Return the value of param `name` when present or `defaultValue`.201 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 203–205why, one step back
203 * - Checks route placeholders, ex: _/user/:id_
204 * - Checks query string params, ex: ?id=12205 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 209–216why, one step back
209 *
210 * @param {String} name211 * @param {Mixed} defaultValue212 * @return {String}213 * @api public214 */215216req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

lines 217–220why, one step back
217 // req.body
218 if (this.body && undefined !== this.body[name]) return this.body[name];219220 // route params

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 221–226why, one step back
221 if (this.params
222 && this.params.hasOwnProperty(name)223 && undefined !== this.params[name]) {224 return this.params[name]; 225 }226

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 229–241why, one step back
229
230 return defaultValue;231};232233/**234 * Check if the incoming request contains the "Content-Type" 235 * header field, and it contains the give mime `type`.236 *237 * Examples:238 *239 * // With Content-Type: text/html; charset=utf-8240 * req.is('html');241 * req.is('text/html');

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 243–247why, one step back
243 * // => true
244 * 245 * // When Content-Type is application/json246 * req.is('json');247 * req.is('application/json');

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 249–257why, one step back
249 * // => true
250 * 251 * req.is('html');252 * // => false253 * 254 * Now within our route callbacks, we can use to to assert content types255 * such as "image/jpeg", "image/png", etc.256 * 257 * app.post('/image/upload', function(req, res, next){

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 259–270why, one step back
259 * // do something
260 * } else {261 * next();262 * }263 * });264 * 265 * @param {String} type266 * @return {Boolean}267 * @api public268 */269270req.is = function(type){

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

lines 271–273why, one step back
271 var ct = this.headers['content-type'];
272 if (!ct) return false;273 ct = ct.split(';')[0];

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

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

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

lines 277–280why, one step back
277 ct = ct.split('/');
278 if ('*' == type[0] && type[1] == ct[1]) return true;279 if ('*' == type[1] && type[0] == ct[0]) return true;280 return false;

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

line 281why, one step back
281 }

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 283–285why, one step back
283};
284285/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

lines 286–291why, one step back
286 * Return the protocol string "http" or "https"
287 * when requested with TLS. When the "trust proxy" 288 * setting is enabled the "X-Forwarded-Proto" header289 * field will be trusted. If you're running behind290 * a reverse proxy that supplies https for you this291 * may be enabled.

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 292–296why, one step back
292 *
293 * @return {String}294 * @api public295 */296

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 299–301why, one step back
299 return this.secure
300 ? 'https'301 : trustProxy

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 304why, one step back
304});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 305–306why, one step back
305
306/**

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 307–313why, one step back
307 * Short-hand for `req.connection.encrypted`.
308 *309 * @return {Boolean}310 * @api public311 */312313req.__defineGetter__('secure', function(){

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

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 315–342why, one step back
315});
316317/**318 * Short-hand for `require('url').parse(req.url).pathname`.319 *320 * @return {String}321 * @api public322 */323324req.__defineGetter__('path', function(){325 return parse(this.url).pathname;326});327328/**329 * Check if the request is fresh, aka330 * Last-Modified and/or the ETag331 * still match.332 *333 * @return {Boolean}334 * @api public335 */336337req.__defineGetter__('fresh', function(){338 return ! this.stale;339});340341/**342 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

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

generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae

lines 344–360why, one step back
344 * resource has changed.
345 *346 * @return {Boolean}347 * @api public348 */349350req.__defineGetter__('stale', function(){351 return connect.utils.modified(this, this.res);352});353354/**355 * Check if the request was an _XMLHttpRequest_.356 *357 * @return {Boolean}358 * @api public359 */360

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

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

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

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

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

line 364why, one step back
364});