Skip to content
Myr

request.js

This file

Path

lib/request.js

435 lines · 9.8 KiB · JavaScript

Last changed by

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

History of this file

Take it

The walk stopped after 200 commits: lines older than 33eaa83 point at it.

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 6–7why, one step back
6var accepts = require('accepts');
7var typeis = require('type-is');

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 8–33why, one step back
8var http = require('http')
9 , utils = require('./utils')10 , connect = require('connect')11 , fresh = require('fresh')12 , parseRange = require('range-parser')13 , parse = connect.utils.parseUrl14 , mime = connect.mime;1516/**17 * Request prototype.18 */1920var req = exports = module.exports = {21 __proto__: http.IncomingMessage.prototype22};2324/**25 * Return request header.26 *27 * The `Referrer` header field is special-cased,28 * both `Referrer` and `Referer` are interchangeable.29 *30 * Examples:31 *32 * req.get('Content-Type');33 * // => "text/plain"

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 34why, one step back
34 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 35–36why, one step back
35 * req.get('content-type');
36 * // => "text/plain"

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 37why, one step back
37 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 38–43why, one step back
38 * req.get('Something');
39 * // => undefined40 *41 * Aliased as `req.header()`.42 *43 * @param {String} name

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

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

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 45–47why, one step back
45 * @api public
46 */47

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 48why, one step back
48req.get =

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 49–60why, one step back
49req.header = function(name){
50 switch (name = name.toLowerCase()) {51 case 'referer':52 case 'referrer':53 return this.headers.referrer54 || this.headers.referer;55 default:56 return this.headers[name];57 }58};5960/**

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 61–62why, one step back
61 * To do: update docs.
62 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 63–69why, one step back
63 * Check if the given `type(s)` is acceptable, returning
64 * the best match when true, otherwise `undefined`, in which65 * case you should respond with 406 "Not Acceptable".66 *67 * The `type` value may be a single mime type string68 * such as "application/json", the extension name69 * such as "json", a comma-delimted list such as "json, html, text/plain",

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption752b5f7

line 70why, one step back
70 * an argument list such as `"json", "html", "text/plain"`,

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 71–74why, one step back
71 * or an array `["json", "html", "text/plain"]`. When a list
72 * or array is given the _best_ match, if any is returned.73 *74 * Examples:

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 75why, one step back
75 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 76–96why, one step back
76 * // Accept: text/html
77 * req.accepts('html');78 * // => "html"79 *80 * // Accept: text/*, application/json81 * req.accepts('html');82 * // => "html"83 * req.accepts('text/html');84 * // => "text/html"85 * req.accepts('json, text');86 * // => "json"87 * req.accepts('application/json');88 * // => "application/json"89 *90 * // Accept: text/*, application/json91 * req.accepts('image/png');92 * req.accepts('png');93 * // => undefined94 *95 * // Accept: text/*;q=.5, application/json96 * req.accepts(['html', 'json']);

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption752b5f7

line 97why, one step back
97 * req.accepts('html', 'json');

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 98–105why, one step back
98 * req.accepts('html, json');
99 * // => "json"100 *101 * @param {String|Array} type(s)102 * @return {String}103 * @api public104 */105

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 106–108why, one step back
106req.accepts = function(){
107 var accept = accepts(this);108 return accept.types.apply(accept, arguments);

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 109–111why, one step back
109};
110111/**

add req.acceptsEncoding(name)TJ Holowaychukbefore adoption2e53cb7

lines 112–118why, one step back
112 * Check if the given `encoding` is accepted.
113 *114 * @param {String} encoding115 * @return {Boolean}116 * @api public117 */118

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 119–122why, one step back
119req.acceptsEncoding = // backwards compatibility
120req.acceptsEncodings = function(){121 var accept = accepts(this);122 return accept.encodings.apply(accept, arguments);

add req.acceptsEncoding(name)TJ Holowaychukbefore adoption2e53cb7

lines 123–125why, one step back
123};
124125/**

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 126–127why, one step back
126 * To do: update docs.
127 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 128–135why, one step back
128 * Check if the given `charset` is acceptable,
129 * otherwise you should respond with 406 "Not Acceptable".130 *131 * @param {String} charset132 * @return {Boolean}133 * @api public134 */135

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 136–139why, one step back
136req.acceptsCharset = // backwards compatibility
137req.acceptsCharsets = function(){138 var accept = accepts(this);139 return accept.charsets.apply(accept, arguments);

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 140–142why, one step back
140};
141142/**

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 143–144why, one step back
143 * To do: update docs.
144 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 145–152why, one step back
145 * Check if the given `lang` is acceptable,
146 * otherwise you should respond with 406 "Not Acceptable".147 *148 * @param {String} lang149 * @return {Boolean}150 * @api public151 */152

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 153–156why, one step back
153req.acceptsLanguage = // backwards compatibility
154req.acceptsLanguages = function(lang){155 var accept = accepts(this);156 return accept.languages.apply(accept, arguments);

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 157–196why, one step back
157};
158159/**160 * Parse Range header field,161 * capping to the given `size`.162 *163 * Unspecified ranges such as "0-" require164 * knowledge of your resource length. In165 * the case of a byte range this is of course166 * the total number of bytes. If the Range167 * header field is not given `null` is returned,168 * `-1` when unsatisfiable, `-2` when syntactically invalid.169 *170 * NOTE: remember that ranges are inclusive, so171 * for example "Range: users=0-3" should respond172 * with 4 users when available, not 3.173 *174 * @param {Number} size175 * @return {Array}176 * @api public177 */178179req.range = function(size){180 var range = this.get('Range');181 if (!range) return;182 return parseRange(size, range);183};184185/**186 * Return the value of param `name` when present or `defaultValue`.187 *188 * - Checks route placeholders, ex: _/user/:id_189 * - Checks body params, ex: id=12, {"id":12}190 * - Checks query string params, ex: ?id=12191 *192 * To utilize request bodies, `req.body`193 * should be an object. This can be done by using194 * the `connect.bodyParser()` middleware.195 *196 * @param {String} name

docsTJ Holowaychukbefore adoptiond1bafa0

line 197why, one step back
197 * @param {Mixed} [defaultValue]

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 198–212why, one step back
198 * @return {String}
199 * @api public200 */201202req.param = function(name, defaultValue){203 var params = this.params || {};204 var body = this.body || {};205 var query = this.query || {};206 if (null != params[name] && params.hasOwnProperty(name)) return params[name];207 if (null != body[name]) return body[name];208 if (null != query[name]) return query[name];209 return defaultValue;210};211212/**

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 213why, one step back
213 * Check if the incoming request contains the "Content-Type"

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 214–222why, one step back
214 * header field, and it contains the give mime `type`.
215 *216 * Examples:217 *218 * // With Content-Type: text/html; charset=utf-8219 * req.is('html');220 * req.is('text/html');221 * req.is('text/*');222 * // => true

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 223why, one step back
223 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 224–228why, one step back
224 * // When Content-Type is application/json
225 * req.is('json');226 * req.is('application/json');227 * req.is('application/*');228 * // => true

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 229why, one step back
229 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 230–231why, one step back
230 * req.is('html');
231 * // => false

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 232why, one step back
232 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 233–237why, one step back
233 * @param {String} type
234 * @return {Boolean}235 * @api public236 */237

refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06

lines 238–240why, one step back
238req.is = function(types){
239 if (!Array.isArray(types)) types = [].slice.call(arguments);240 return typeis(this, types);

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 241–244why, one step back
241};
242243/**244 * Return the protocol string "http" or "https"

move "subdomain offset" defaulting to configTJ Holowaychukbefore adoptioncd54faa

line 245why, one step back
245 * when requested with TLS. When the "trust proxy"

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 246–256why, one step back
246 * setting is enabled the "X-Forwarded-Proto" header
247 * field will be trusted. If you're running behind248 * a reverse proxy that supplies https for you this249 * may be enabled.250 *251 * @return {String}252 * @api public253 */254255req.__defineGetter__('protocol', function(){256 var trustProxy = this.app.get('trust proxy');

add support for multiple X-Forwarded-Proto values. Closes #1646TJ Holowaychukbefore adoption8ab4408

lines 257–260why, one step back
257 if (this.connection.encrypted) return 'https';
258 if (!trustProxy) return 'http';259 var proto = this.get('X-Forwarded-Proto') || 'http';260 return proto.split(/\s*,\s*/)[0];

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 261–333why, one step back
261});
262263/**264 * Short-hand for:265 *266 * req.protocol == 'https'267 *268 * @return {Boolean}269 * @api public270 */271272req.__defineGetter__('secure', function(){273 return 'https' == this.protocol;274});275276/**277 * Return the remote address, or when278 * "trust proxy" is `true` return279 * the upstream addr.280 *281 * @return {String}282 * @api public283 */284285req.__defineGetter__('ip', function(){286 return this.ips[0] || this.connection.remoteAddress;287});288289/**290 * When "trust proxy" is `true`, parse291 * the "X-Forwarded-For" ip address list.292 *293 * For example if the value were "client, proxy1, proxy2"294 * you would receive the array `["client", "proxy1", "proxy2"]`295 * where "proxy2" is the furthest down-stream.296 *297 * @return {Array}298 * @api public299 */300301req.__defineGetter__('ips', function(){302 var trustProxy = this.app.get('trust proxy');303 var val = this.get('X-Forwarded-For');304 return trustProxy && val305 ? val.split(/ *, */)306 : [];307});308309/**310 * Return basic auth credentials.311 *312 * Examples:313 *314 * // http://tobi:hello@example.com315 * req.auth316 * // => { username: 'tobi', password: 'hello' }317 *318 * @return {Object} or undefined319 * @api public320 */321322req.__defineGetter__('auth', function(){323 // missing324 var auth = this.get('Authorization');325 if (!auth) return;326327 // malformed328 var parts = auth.split(' ');329 if ('basic' != parts[0].toLowerCase()) return;330 if (!parts[1]) return;331 auth = parts[1];332333 // credentials

Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006

lines 334–336why, one step back
334 auth = new Buffer(auth, 'base64').toString().match(/^([^:]*):(.*)$/);
335 if (!auth) return;336 return { username: auth[1], password: auth[2] };

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 337–341why, one step back
337});
338339/**340 * Return subdomains as an array.341 *

Merge pull request #1475 from gmethvin/subdomain_offsetTJ Holowaychukbefore adoption5beb1c4

lines 342–344why, one step back
342 * Subdomains are the dot-separated parts of the host before the main domain of
343 * the app. By default, the domain of the app is assumed to be the last two344 * parts of the host. This can be changed by setting "subdomain offset".

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

line 345why, one step back
345 *

Merge pull request #1475 from gmethvin/subdomain_offsetTJ Holowaychukbefore adoption5beb1c4

lines 346–349why, one step back
346 * For example, if the domain is "tobi.ferrets.example.com":
347 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.348 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.349 *

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 350–354why, one step back
350 * @return {Array}
351 * @api public352 */353354req.__defineGetter__('subdomains', function(){

Merge pull request #1475 from gmethvin/subdomain_offsetTJ Holowaychukbefore adoption5beb1c4

line 355why, one step back
355 var offset = this.app.get('subdomain offset');

fix `req.subdomains` when no Host is presentTJ Holowaychukbefore adoption8559c0e

line 356why, one step back
356 return (this.host || '')

change req.subdomain styling backTJ Holowaychukbefore adoptione3ac2c5

lines 357–359why, one step back
357 .split('.')
358 .reverse()359 .slice(offset);

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 360–380why, one step back
360});
361362/**363 * Short-hand for `url.parse(req.url).pathname`.364 *365 * @return {String}366 * @api public367 */368369req.__defineGetter__('path', function(){370 return parse(this).pathname;371});372373/**374 * Parse the "Host" header field hostname.375 *376 * @return {String}377 * @api public378 */379380req.__defineGetter__('host', function(){

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption6654b71

line 381why, one step back
381 var trustProxy = this.app.get('trust proxy');

refactor req.hostTJ Holowaychukbefore adoption4a1fa58

lines 382–383why, one step back
382 var host = trustProxy && this.get('X-Forwarded-Host');
383 host = host || this.get('Host');

fix `req.host` when no Host is present, return undefinedTJ Holowaychukbefore adoption06ead58

line 384why, one step back
384 if (!host) return;

refactor req.hostTJ Holowaychukbefore adoption4a1fa58

line 385why, one step back
385 return host.split(':')[0];

Release 3.0.5TJ Holowaychukbefore adoption33eaa83

lines 386–435why, one step back
386});
387388/**389 * Check if the request is fresh, aka390 * Last-Modified and/or the ETag391 * still match.392 *393 * @return {Boolean}394 * @api public395 */396397req.__defineGetter__('fresh', function(){398 var method = this.method;399 var s = this.res.statusCode;400401 // GET or HEAD for weak freshness validation only402 if ('GET' != method && 'HEAD' != method) return false;403404 // 2xx or 304 as per rfc2616 14.26405 if ((s >= 200 && s < 300) || 304 == s) {406 return fresh(this.headers, this.res._headers);407 }408409 return false;410});411412/**413 * Check if the request is stale, aka414 * "Last-Modified" and / or the "ETag" for the415 * resource has changed.416 *417 * @return {Boolean}418 * @api public419 */420421req.__defineGetter__('stale', function(){422 return !this.fresh;423});424425/**426 * Check if the request was an _XMLHttpRequest_.427 *428 * @return {Boolean}429 * @api public430 */431432req.__defineGetter__('xhr', function(){433 var val = this.get('X-Requested-With') || '';434 return 'xmlhttprequest' == val.toLowerCase();435});