Skip to content
Myr

request.js

This file

Path

lib/request.js

381 lines · 8.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 25–82why, 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');36 * // => "text/plain"37 * 38 * req.header('content-type');39 * // => "text/plain"40 * 41 * req.header('Accept');42 * // => undefined43 * 44 * req.header('Accept', 'text/html');45 * // => "text/html"46 *47 * @param {String} name48 * @param {String} defaultValue49 * @return {String} 50 * @api public51 */5253req.header = function(name, defaultValue){54 switch (name = name.toLowerCase()) {55 case 'referer':56 case 'referrer':57 return this.headers.referrer58 || this.headers.referer59 || defaultValue;60 default:61 return this.headers[name] || defaultValue;62 }63};6465/**66 * Get `field`'s `param` value, defaulting to ''.67 *68 * Examples:69 *70 * req.get('content-disposition', 'filename');71 * // => "something.png"72 *73 * req.get('content-disposition', 'rawr');74 * // => ""75 *76 * @param {String} field77 * @param {String} param78 * @return {String}79 * @api public80 */8182req.get = function(field, param){

mergedTj Holowaychukbefore adoption547d18c

line 83why, one step back
83 // TODO: rename... add tests, make .get() .header()

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 84–91why, one step back
84 var val = this.header(field);
85 if (!val) return '';86 var regexp = new RegExp(param + ' *= *(?:"([^"]+)"|([^;]+))', 'i');87 if (!regexp.exec(val)) return '';88 return RegExp.$1 || RegExp.$2;89};9091/**

mergedTj Holowaychukbefore adoption547d18c

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 94–117why, one step back
94 *
95 * Examples:96 * 97 * // Accept: text/html98 * req.accepts('html');99 * // => true100 *101 * // Accept: text/*; application/json102 * req.accepts('html');103 * req.accepts('text/html');104 * req.accepts('text/plain');105 * req.accepts('application/json');106 * // => true107 *108 * req.accepts('image/png');109 * req.accepts('png');110 * // => false111 *112 * @param {String} type113 * @return {Boolean}114 * @api public115 */116117req.accepts = function(type){

mergedTj Holowaychukbefore adoption547d18c

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

line 120why, one step back
120

mergedTj Holowaychukbefore adoption547d18c

lines 121–128why, one step back
121/**
122 * Check if the given `charset` is acceptable,123 * otherwise you should respond with 406 "Not Acceptable".124 *125 * @param {String} charset126 * @return {Boolean}127 * @api public128 */

upgrade shouldTj Holowaychukbefore adoption17ccd55

line 129why, one step back
129

mergedTj Holowaychukbefore adoption547d18c

lines 130–135why, one step back
130req.acceptsCharset = function(charset){
131 var accepted = this.acceptedCharsets;132 return accepted.length133 ? ~accepted.indexOf(charset)134 : true;135};

upgrade shouldTj Holowaychukbefore adoption17ccd55

line 136why, one step back
136

mergedTj Holowaychukbefore adoption547d18c

lines 137–144why, one step back
137/**
138 * Check if the given `lang` is acceptable,139 * otherwise you should respond with 406 "Not Acceptable".140 *141 * @param {String} lang142 * @return {Boolean}143 * @api public144 */

upgrade shouldTj Holowaychukbefore adoption17ccd55

line 145why, one step back
145

mergedTj Holowaychukbefore adoption547d18c

lines 146–150why, one step back
146req.acceptsLanguage = function(lang){
147 var accepted = this.acceptedLanguages;148 return accepted.length149 ? ~accepted.indexOf(lang)150 : true;

upgrade shouldTj Holowaychukbefore adoption17ccd55

line 151why, one step back
151};

mergedTj Holowaychukbefore adoption547d18c

lines 152–225why, one step back
152
153/**154 * Return an array of Accepted media types155 * ordered from highest quality to lowest.156 *157 * Examples:158 *159 * [ { value: 'application/json',160 * quality: 1,161 * type: 'application',162 * subtype: 'json' },163 * { value: 'text/html',164 * quality: 0.5,165 * type: 'text',166 * subtype: 'html' } ]167 *168 * @return {Array}169 * @api public170 */171172req.__defineGetter__('accepted', function(){173 var accept = this.header('Accept');174 return accept175 ? utils.parseAccept(accept)176 : [];177});178179/**180 * Return an array of Accepted languages181 * ordered from highest quality to lowest.182 *183 * Examples:184 *185 * Accept-Language: en;q=.5, en-us186 * ['en-us', 'en']187 *188 * @return {Array}189 * @api public190 */191192req.__defineGetter__('acceptedLanguages', function(){193 var accept = this.header('Accept-Language');194 return accept195 ? utils196 .parseQuality(accept)197 .map(function(obj){198 return obj.value;199 })200 : [];201});202203/**204 * Return an array of Accepted charsets205 * ordered from highest quality to lowest.206 *207 * Examples:208 *209 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8210 * ['unicode-1-1', 'iso-8859-5']211 *212 * @return {Array}213 * @api public214 */215216req.__defineGetter__('acceptedCharsets', function(){217 var accept = this.header('Accept-Charset');218 return accept219 ? utils220 .parseQuality(accept)221 .map(function(obj){222 return obj.value;223 })224 : [];225});

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 226–229why, one step back
226
227/**228 * Return the value of param `name` when present or `defaultValue`.229 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 231–233why, one step back
231 * - Checks route placeholders, ex: _/user/:id_
232 * - Checks query string params, ex: ?id=12233 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 237–244why, one step back
237 *
238 * @param {String} name239 * @param {Mixed} defaultValue240 * @return {String}241 * @api public242 */243244req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

lines 245–248why, one step back
245 // req.body
246 if (this.body && undefined !== this.body[name]) return this.body[name];247248 // route params

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 249–254why, one step back
249 if (this.params
250 && this.params.hasOwnProperty(name)251 && undefined !== this.params[name]) {252 return this.params[name]; 253 }254

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 257–331why, one step back
257
258 return defaultValue;259};260261/**262 * Check if the incoming request contains the "Content-Type" 263 * header field, and it contains the give mime `type`.264 *265 * Examples:266 *267 * // With Content-Type: text/html; charset=utf-8268 * req.is('html');269 * req.is('text/html');270 * // => true271 * 272 * // When Content-Type is application/json273 * req.is('json');274 * req.is('application/json');275 * // => true276 * 277 * req.is('html');278 * // => false279 * 280 * Ad-hoc callbacks can also be registered with Express, to perform281 * assertions again the request, for example if we need an expressive282 * way to check if our incoming request is an image, we can register "an image"283 * callback:284 * 285 * app.is('an image', function(req){286 * return 0 == req.headers['content-type'].indexOf('image');287 * });288 *289 * app.is('an attachment', function(req){290 * return 0 == req.headers['content-disposition'].indexOf('attachment');291 * });292 *293 * Now within our route callbacks, we can use to to assert content types294 * such as "image/jpeg", "image/png", etc.295 * 296 * app.post('/image/upload', function(req, res, next){297 * if (req.is('an image')) {298 * // do something299 * } else {300 * next();301 * }302 * });303 * 304 * @param {String} type305 * @return {Boolean}306 * @api public307 */308309req.is = function(type){310 var fn = this.app.is(type);311 if (fn) return fn(this);312 var contentType = this.headers['content-type'];313 if (!contentType) return;314 if (!~type.indexOf('/')) type = mime.lookup(type);315 if (~type.indexOf('*')) {316 type = type.split('/');317 contentType = contentType.split('/');318 if ('*' == type[0] && type[1] == contentType[1]) return true;319 if ('*' == type[1] && type[0] == contentType[0]) return true;320 }321 return !! ~contentType.indexOf(type);322};323324/**325 * Short-hand for `req.connection.encrypted`.326 *327 * @return {Boolean}328 * @api public329 */330331req.__defineGetter__('secure', function(){

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

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

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 333–378why, one step back
333});
334335/**336 * Short-hand for `require('url').parse(req.url).pathname`.337 *338 * @return {String}339 * @api public340 */341342req.__defineGetter__('path', function(){343 return parse(this.url).pathname;344});345346/**347 * Check if the request is fresh, aka348 * Last-Modified and/or the ETag349 * still match.350 *351 * @return {Boolean}352 * @api public353 */354355req.__defineGetter__('fresh', function(){356 return ! this.stale;357});358359/**360 * Check if the request is stale, aka361 * Last-Modified and/or the ETag for the362 * resource has changed.363 *364 * @return {Boolean}365 * @api public366 */367368req.__defineGetter__('stale', function(){369 return connect.utils.modified(this, this.res);370});371372/**373 * Check if the request was an _XMLHttpRequest_.374 *375 * @return {Boolean}376 * @api public377 */378

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

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

case sensitivity testTj Holowaychukbefore adoption1a5636b

line 380why, one step back
380 return 'xmlhttprequest' == this.header('X-Requested-With', '').toLowerCase();

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

line 381why, one step back
381});