request.js
This file
Path
lib/request.js
380 lines · 8.2 KiB · JavaScript
Last changed by
Removed `req.notify()`Tj Holowaychukbefore adoption28f32b9
History of this fileTake it
The walk stopped after 200 commits: lines older than 294bc8c point at it.
misc refactoringTj Holowaychukbefore adoption294bc8c
lines 1–13why, one step back12/*!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')Added req.fresh and req.staleTj Holowaychukbefore adoption0e4e050
line 14why, one step back14 , connect = require('connect')misc refactoringTj Holowaychukbefore adoption294bc8c
lines 15–16why, one step back15 , parse = require('url').parse16 , mime = require('mime');no longer manipulate the req/res protos directlyTj Holowaychukbefore adoptiond249868
lines 17–24why, one step back1718/**19 * Request prototype.20 */2122var req = exports = module.exports = {23 __proto__: http.IncomingMessage.prototype24};misc refactoringTj Holowaychukbefore adoption294bc8c
lines 25–82why, one step back2526/**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 back83 // TODO: rename... add tests, make .get() .header()misc refactoringTj Holowaychukbefore adoption294bc8c
lines 84–91why, one step back84 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 back92 * Check if the given `type` is acceptable,93 * otherwise you should respond with 406 "Not Acceptable".misc refactoringTj Holowaychukbefore adoption294bc8c
lines 94–117why, one step back94 *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 back118 return utils.accepts(type, this.header('Accept'));119};misc refactoringTj Holowaychukbefore adoption294bc8c
line 120why, one step back120mergedTj Holowaychukbefore adoption547d18c
lines 121–128why, one step back121/**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 */misc refactoringTj Holowaychukbefore adoption294bc8c
line 129why, one step back129mergedTj Holowaychukbefore adoption547d18c
lines 130–135why, one step back130req.acceptsCharset = function(charset){131 var accepted = this.acceptedCharsets;132 return accepted.length133 ? ~accepted.indexOf(charset)134 : true;135};misc refactoringTj Holowaychukbefore adoption294bc8c
line 136why, one step back136mergedTj Holowaychukbefore adoption547d18c
lines 137–144why, one step back137/**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 */misc refactoringTj Holowaychukbefore adoption294bc8c
line 145why, one step back145mergedTj Holowaychukbefore adoption547d18c
lines 146–150why, one step back146req.acceptsLanguage = function(lang){147 var accepted = this.acceptedLanguages;148 return accepted.length149 ? ~accepted.indexOf(lang)150 : true;misc refactoringTj Holowaychukbefore adoption294bc8c
line 151why, one step back151};mergedTj Holowaychukbefore adoption547d18c
lines 152–225why, one step back152153/**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});misc refactoringTj Holowaychukbefore adoption294bc8c
lines 226–321why, one step back226227/**228 * Return the value of param `name` when present or `defaultValue`.229 *230 * - Checks route placeholders, ex: _/user/:id_231 * - Checks query string params, ex: ?id=12232 * - Checks urlencoded body params, ex: id=12233 *234 * To utilize urlencoded request bodies, `req.body`235 * should be an object. This can be done by using236 * the `connect.bodyParser` middleware.237 *238 * @param {String} name239 * @param {Mixed} defaultValue240 * @return {String}241 * @api public242 */243244req.param = function(name, defaultValue){245 // route params like /user/:id246 if (this.params247 && this.params.hasOwnProperty(name)248 && undefined !== this.params[name]) {249 return this.params[name]; 250 }251 // query string params252 if (undefined !== this.query[name]) return this.query[name]; 253254 // request body params via connect.bodyParser255 if (this.body && undefined !== this.body[name]) return this.body[name];256257 return defaultValue;258};259260/**261 * Check if the incoming request contains the "Content-Type" 262 * header field, and it contains the give mime `type`.263 *264 * Examples:265 *266 * // With Content-Type: text/html; charset=utf-8267 * req.is('html');268 * req.is('text/html');269 * // => true270 * 271 * // When Content-Type is application/json272 * req.is('json');273 * req.is('application/json');274 * // => true275 * 276 * req.is('html');277 * // => false278 * 279 * Ad-hoc callbacks can also be registered with Express, to perform280 * assertions again the request, for example if we need an expressive281 * way to check if our incoming request is an image, we can register "an image"282 * callback:283 * 284 * app.is('an image', function(req){285 * return 0 == req.headers['content-type'].indexOf('image');286 * });287 *288 * app.is('an attachment', function(req){289 * return 0 == req.headers['content-disposition'].indexOf('attachment');290 * });291 *292 * Now within our route callbacks, we can use to to assert content types293 * such as "image/jpeg", "image/png", etc.294 * 295 * app.post('/image/upload', function(req, res, next){296 * if (req.is('an image')) {297 * // do something298 * } else {299 * next();300 * }301 * });302 * 303 * @param {String} type304 * @return {Boolean}305 * @api public306 */307308req.is = function(type){309 var fn = this.app.is(type);310 if (fn) return fn(this);311 var contentType = this.headers['content-type'];312 if (!contentType) return;313 if (!~type.indexOf('/')) type = mime.lookup(type);314 if (~type.indexOf('*')) {315 type = type.split('/');316 contentType = contentType.split('/');317 if ('*' == type[0] && type[1] == contentType[1]) return true;318 if ('*' == type[1] && type[0] == contentType[0]) return true;319 }320 return !! ~contentType.indexOf(type);321};move getters downTj Holowaychukbefore adoptiond6fabb6
lines 322–343why, one step back322323/**324 * Short-hand for `req.connection.encrypted`.325 *326 * @return {Boolean}327 * @api public328 */329330req.__defineGetter__('secure', function(){331 return req.connection.encrypted;332});333334/**335 * Short-hand for `require('url').parse(req.url).pathname`.336 *337 * @return {String}338 * @api public339 */340341req.__defineGetter__('path', function(){342 return parse(this.url).pathname;343});Added req.fresh and req.staleTj Holowaychukbefore adoption0e4e050
lines 344–355why, one step back344345/**346 * Check if the request is fresh, aka347 * Last-Modified and/or the ETag348 * still match.349 *350 * @return {Boolean}351 * @api public352 */353354req.__defineGetter__('fresh', function(){355 return ! this.stale;semicolonsTj Holowaychukbefore adoption1a1ed0a
line 356why, one step back356});Added req.fresh and req.staleTj Holowaychukbefore adoption0e4e050
lines 357–368why, one step back357358/**359 * Check if the request is stale, aka360 * Last-Modified and/or the ETag for the361 * resource has changed.362 *363 * @return {Boolean}364 * @api public365 */366367req.__defineGetter__('stale', function(){368 return connect.utils.modified(this, this.res);semicolonsTj Holowaychukbefore adoption1a1ed0a
line 369why, one step back369});misc refactoringTj Holowaychukbefore adoption294bc8c
lines 370–377why, one step back370371/**372 * Check if the request was an _XMLHttpRequest_.373 *374 * @return {Boolean}375 * @api public376 */377Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 378why, one step back378req.__defineGetter__('xhr', function(){case sensitivity testTj Holowaychukbefore adoption1a5636b
line 379why, one step back379 return 'xmlhttprequest' == this.header('X-Requested-With', '').toLowerCase();Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 380why, one step back380});