Skip to content
Myr

request.js

This file

Path

lib/request.js

361 lines · 7.4 KiB · JavaScript

Last changed by

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

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/**

mergedTj Holowaychukbefore adoption547d18c

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

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

lines 63–86why, 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 *77 * req.accepts('image/png');78 * req.accepts('png');79 * // => false80 *81 * @param {String} type82 * @return {Boolean}83 * @api public84 */8586req.accepts = function(type){

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

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

mergedTj Holowaychukbefore adoption547d18c

line 88why, one step back
88};

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

line 89why, one step back
89

mergedTj Holowaychukbefore adoption547d18c

lines 90–97why, one step back
90/**
91 * Check if the given `charset` is acceptable,92 * otherwise you should respond with 406 "Not Acceptable".93 *94 * @param {String} charset95 * @return {Boolean}96 * @api public97 */

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

line 98why, one step back
98

mergedTj Holowaychukbefore adoption547d18c

lines 99–104why, one step back
99req.acceptsCharset = function(charset){
100 var accepted = this.acceptedCharsets;101 return accepted.length102 ? ~accepted.indexOf(charset)103 : true;104};

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

line 105why, one step back
105

mergedTj Holowaychukbefore adoption547d18c

lines 106–113why, one step back
106/**
107 * Check if the given `lang` is acceptable,108 * otherwise you should respond with 406 "Not Acceptable".109 *110 * @param {String} lang111 * @return {Boolean}112 * @api public113 */

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

line 114why, one step back
114

mergedTj Holowaychukbefore adoption547d18c

lines 115–119why, one step back
115req.acceptsLanguage = function(lang){
116 var accepted = this.acceptedLanguages;117 return accepted.length118 ? ~accepted.indexOf(lang)119 : true;

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

line 120why, one step back
120};

mergedTj Holowaychukbefore adoption547d18c

lines 121–141why, one step back
121
122/**123 * Return an array of Accepted media types124 * ordered from highest quality to lowest.125 *126 * Examples:127 *128 * [ { value: 'application/json',129 * quality: 1,130 * type: 'application',131 * subtype: 'json' },132 * { value: 'text/html',133 * quality: 0.5,134 * type: 'text',135 * subtype: 'html' } ]136 *137 * @return {Array}138 * @api public139 */140141req.__defineGetter__('accepted', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 143–161why, one step back
143 return accept
144 ? utils.parseAccept(accept)145 : [];146});147148/**149 * Return an array of Accepted languages150 * ordered from highest quality to lowest.151 *152 * Examples:153 *154 * Accept-Language: en;q=.5, en-us155 * ['en-us', 'en']156 *157 * @return {Array}158 * @api public159 */160161req.__defineGetter__('acceptedLanguages', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 163–185why, one step back
163 return accept
164 ? utils165 .parseQuality(accept)166 .map(function(obj){167 return obj.value;168 })169 : [];170});171172/**173 * Return an array of Accepted charsets174 * ordered from highest quality to lowest.175 *176 * Examples:177 *178 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8179 * ['unicode-1-1', 'iso-8859-5']180 *181 * @return {Array}182 * @api public183 */184185req.__defineGetter__('acceptedCharsets', function(){

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

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

mergedTj Holowaychukbefore adoption547d18c

lines 187–194why, one step back
187 return accept
188 ? utils189 .parseQuality(accept)190 .map(function(obj){191 return obj.value;192 })193 : [];194});

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

lines 195–198why, one step back
195
196/**197 * Return the value of param `name` when present or `defaultValue`.198 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

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

lines 200–202why, one step back
200 * - Checks route placeholders, ex: _/user/:id_
201 * - Checks query string params, ex: ?id=12202 *

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

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

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

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

lines 206–213why, one step back
206 *
207 * @param {String} name208 * @param {Mixed} defaultValue209 * @return {String}210 * @api public211 */212213req.param = function(name, defaultValue){

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

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

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

req.body takes precedenceTj Holowaychukbefore adoptione49c482

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

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

lines 226–238why, one step back
226
227 return defaultValue;228};229230/**231 * Check if the incoming request contains the "Content-Type" 232 * header field, and it contains the give mime `type`.233 *234 * Examples:235 *236 * // With Content-Type: text/html; charset=utf-8237 * req.is('html');238 * req.is('text/html');

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

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

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

lines 240–244why, one step back
240 * // => true
241 * 242 * // When Content-Type is application/json243 * req.is('json');244 * req.is('application/json');

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

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

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

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

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

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

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

lines 256–267why, one step back
256 * // do something
257 * } else {258 * next();259 * }260 * });261 * 262 * @param {String} type263 * @return {Boolean}264 * @api public265 */266267req.is = function(type){

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

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

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

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

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

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

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

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

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

line 278why, one step back
278 }

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

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

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

lines 280–282why, one step back
280};
281282/**

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

lines 283–288why, one step back
283 * Return the protocol string "http" or "https"
284 * when requested with TLS. When the "trust proxy" 285 * setting is enabled the "X-Forwarded-Proto" header286 * field will be trusted. If you're running behind287 * a reverse proxy that supplies https for you this288 * may be enabled.

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 289–293why, one step back
289 *
290 * @return {String}291 * @api public292 */293

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 296–298why, one step back
296 return this.secure
297 ? 'https'298 : trustProxy

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

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

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

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

Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc

line 301why, one step back
301});

Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d

lines 302–303why, one step back
302
303/**

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

lines 304–310why, one step back
304 * Short-hand for `req.connection.encrypted`.
305 *306 * @return {Boolean}307 * @api public308 */309310req.__defineGetter__('secure', function(){

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

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

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

lines 312–339why, one step back
312});
313314/**315 * Short-hand for `require('url').parse(req.url).pathname`.316 *317 * @return {String}318 * @api public319 */320321req.__defineGetter__('path', function(){322 return parse(this.url).pathname;323});324325/**326 * Check if the request is fresh, aka327 * Last-Modified and/or the ETag328 * still match.329 *330 * @return {Boolean}331 * @api public332 */333334req.__defineGetter__('fresh', function(){335 return ! this.stale;336});337338/**339 * Check if the request is stale, aka

docsTJ Holowaychukbefore adoption6ab61d0

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

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

lines 341–357why, one step back
341 * resource has changed.
342 *343 * @return {Boolean}344 * @api public345 */346347req.__defineGetter__('stale', function(){348 return connect.utils.modified(this, this.res);349});350351/**352 * Check if the request was an _XMLHttpRequest_.353 *354 * @return {Boolean}355 * @api public356 */357

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

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

refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee

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

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

line 361why, one step back
361});