request.js
This file
Path
lib/request.js
335 lines · 8.1 KiB · JavaScript
Last changed by
Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
History of this fileTake it
The walk stopped after 200 commits: lines older than 1abb674 point at it.
temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 1–17why, 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 , req = http.IncomingMessage.prototype14 , utils = require('./utils')15 , mime = require('mime');1617/**Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 18why, one step back18 * Default notification formatters.temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 19–20why, one step back19 */20Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe
line 21why, one step back21var defaultFormatters = exports.formatters = {temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 22–23why, one step back22 s: function(val){23 return String(val);Added `%d` flash formatterTj Holowaychukbefore adoptionc7c9606
lines 24–26why, one step back24 },25 d: function(val){26 return val | 0;temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 27–69why, one step back27 }28};2930/**31 * Return request header or optional default.32 *33 * The `Referrer` header field is special-cased,34 * both `Referrer` and `Referer` will yield are35 * interchangeable.36 *37 * Examples:38 *39 * req.header('Content-Type');40 * // => "text/plain"41 * 42 * req.header('content-type');43 * // => "text/plain"44 * 45 * req.header('Accept');46 * // => undefined47 * 48 * req.header('Accept', 'text/html');49 * // => "text/html"50 *51 * @param {String} name52 * @param {String} defaultValue53 * @return {String} 54 * @api public55 */5657req.header = function(name, defaultValue){58 switch (name = name.toLowerCase()) {59 case 'referer':60 case 'referrer':61 return this.headers.referrer62 || this.headers.referer63 || defaultValue;64 default:65 return this.headers[name] || defaultValue;66 }67};6869/**Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
lines 70–73why, one step back70 * Get `field`'s `param` value, defaulting to ''.71 *72 * Examples:73 *docs for req.get()Tj Holowaychukbefore adoption6bb100d
lines 74–75why, one step back74 * req.get('content-disposition', 'filename');75 * // => "something.png"Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
line 76why, one step back76 *examplesTj Holowaychukbefore adoption7eec21a
lines 77–79why, one step back77 * req.get('content-disposition', 'rawr');78 * // => ""79 *Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
lines 80–88why, one step back80 * @param {String} field81 * @param {String} param82 * @return {String}83 * @api public84 */8586req.get = function(field, param){87 var val = this.header(field);88 if (!val) return '';typoTj Holowaychukbefore adoptionf13ea34
line 89why, one step back89 var regexp = new RegExp(param + ' *= *(?:"([^"]+)"|([^;]+))', 'i');Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
lines 90–94why, one step back90 if (!regexp.exec(val)) return '';91 return RegExp.$1 || RegExp.$2;92};9394/**temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 95–134why, one step back95 * Check if the _Accept_ header is present, and includes the given `type`.96 *97 * When the _Accept_ header is not present `true` is returned. Otherwise98 * the given `type` is matched by an exact match, and then subtypes. You99 * may pass the subtype such as "html" which is then converted internally100 * to "text/html" using the mime lookup table.101 *102 * Examples:103 * 104 * // Accept: text/html105 * req.accepts('html');106 * // => true107 *108 * // Accept: text/*; application/json109 * req.accepts('html');110 * req.accepts('text/html');111 * req.accepts('text/plain');112 * req.accepts('application/json');113 * // => true114 *115 * req.accepts('image/png');116 * req.accepts('png');117 * // => false118 *119 * @param {String} type120 * @return {Boolean}121 * @api public122 */123124req.accepts = function(type){125 var accept = this.header('Accept');126127 // normalize extensions ".json" -> "json"128 if (type && '.' == type[0]) type = type.substr(1);129130 // when Accept does not exist, or is '*/*' return true 131 if (!accept || '*/*' == accept) {132 return true;133 } else if (type) {134 // allow "html" vs "text/html" etcmisc refactoringTj Holowaychukbefore adoption3dc53e1
line 135why, one step back135 if (!~type.indexOf('/')) type = mime.lookup(type);temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 136–141why, one step back136137 // check if we have a direct match138 if (~accept.indexOf(type)) return true;139140 // check if we have type/*141 type = type.split('/')[0] + '/*';refactored `req.param()`Tj Holowaychukbefore adoptiona4621a6
line 142why, one step back142 return !! ~accept.indexOf(type);temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 143–166why, one step back143 } else {144 return false;145 }146};147148/**149 * Return the value of param `name` when present or `defaultValue`.150 *151 * - Checks route placeholders, ex: _/user/:id_152 * - Checks query string params, ex: ?id=12153 * - Checks urlencoded body params, ex: id=12154 *155 * To utilize urlencoded request bodies, `req.body`156 * should be an object. This can be done by using157 * the `connect.bodyParser` middleware.158 *159 * @param {String} name160 * @param {Mixed} defaultValue161 * @return {String}162 * @api public163 */164165req.param = function(name, defaultValue){166 // route params like /user/:idrefactored `req.param()`Tj Holowaychukbefore adoptiona4621a6
lines 167–169why, one step back167 if (this.params168 && this.params.hasOwnProperty(name)169 && undefined !== this.params[name]) {temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 170–172why, one step back170 return this.params[name]; 171 }172 // query string paramsrefactored `req.param()`Tj Holowaychukbefore adoptiona4621a6
lines 173–174why, one step back173 if (undefined !== this.query[name]) return this.query[name]; 174temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
line 175why, one step back175 // request body params via connect.bodyParserrefactored `req.param()`Tj Holowaychukbefore adoptiona4621a6
lines 176–177why, one step back176 if (this.body && undefined !== this.body[name]) return this.body[name];177temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 178–183why, one step back178 return defaultValue;179};180181/**182 * Queue flash `msg` of the given `type`.183 *Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30
lines 184–185why, one step back184 * This method is aliased as `req.notify()`.185 *temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 186–187why, one step back186 * Examples:187 *Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30
line 188why, one step back188 * req.notify('info', 'email sent');Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 189–191why, one step back189 * req.notify('info', 'email sent');190 * req.notify('error', 'email delivery failed');191 * req.notify('info', 'email re-sent');temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 192–193why, one step back192 * // => 2193 *Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 194why, one step back194 * req.notify('info');temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 195–196why, one step back195 * // => ['email sent', 'email re-sent']196 *Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 197why, one step back197 * req.notify('info');temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 198–199why, one step back198 * // => []199 *Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 200why, one step back200 * req.notify();temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 201–205why, one step back201 * // => { error: ['email delivery failed'], info: [] }202 *203 * Formatting:204 *205 * Flash notifications also support arbitrary formatting support.Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 206why, one step back206 * For example you may pass variable arguments to `req.notify()`temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 207–208why, one step back207 * and use the %s specifier to be replaced by the associated argument:208 *Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 209why, one step back209 * req.notify('info', 'email has been sent to %s.', userName);temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
line 210why, one step back210 *Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe
lines 211–213why, one step back211 * You may add addition formatters by defining `app.formatters`,212 * for example in the following snippet we define `%u` to uppercase213 * a string:temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
line 214why, one step back214 *Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe
lines 215–220why, one step back215 * app.formatters = {216 * u: function(val){217 * return String(val).toUpperCase();218 * }219 * };220 *temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 221–226why, one step back221 * @param {String} type222 * @param {String} msg223 * @return {Array|Object|Number}224 * @api public225 */226Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30
line 227why, one step back227req.notify = function(type, msg){Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 228–230why, one step back228 var sess = this.session;229 if (null == sess) throw Error('req.notify() requires sessions');230 var msgs = sess.notifications = sess.notifications || {};refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6
lines 231–232why, one step back231232 // notificationtemp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 233–235why, one step back233 if (type && msg) {234 var i = 2235 , args = argumentsRenamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe
lines 236–237why, one step back236 , formatters = this.app.formatters || {};237 formatters.__proto__ = defaultFormatters;Merge branch 'feature/router'Tj Holowaychukbefore adoption1d2dd2a
line 238why, one step back238 msg = utils.miniMarkdown(utils.escape(msg));temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 239–243why, one step back239 msg = msg.replace(/%([a-zA-Z])/g, function(_, format){240 var formatter = formatters[format];241 if (formatter) return formatter(args[i++]);242 });243 return (msgs[type] = msgs[type] || []).push(msg);refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6
lines 244–247why, one step back244 }245246 // flush messages for a specific type247 if (type) {temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 248–251why, one step back248 var arr = msgs[type];249 delete msgs[type];250 return arr || [];251 }refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6
lines 252–253why, one step back252253 // flush all messagesRemoved `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 254why, one step back254 sess.notifications = {};refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6
line 255why, one step back255 return msgs;temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 256–284why, one step back256};257258/**259 * Check if the incoming request contains the "Content-Type" 260 * header field, and it contains the give mime `type`.261 *262 * Examples:263 *264 * // With Content-Type: text/html; charset=utf-8265 * req.is('html');266 * req.is('text/html');267 * // => true268 * 269 * // When Content-Type is application/json270 * req.is('json');271 * req.is('application/json');272 * // => true273 * 274 * req.is('html');275 * // => false276 * 277 * Ad-hoc callbacks can also be registered with Express, to perform278 * assertions again the request, for example if we need an expressive279 * way to check if our incoming request is an image, we can register "an image"280 * callback:281 * 282 * app.is('an image', function(req){283 * return 0 == req.headers['content-type'].indexOf('image');284 * });another `req.is()` exampleTj Holowaychukbefore adoption4b8e08d
lines 285–289why, one step back285 *286 * app.is('an attachment', function(req){287 * return 0 == req.headers['content-disposition'].indexOf('attachment');288 * });289 *temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 290–317why, one step back290 * Now within our route callbacks, we can use to to assert content types291 * such as "image/jpeg", "image/png", etc.292 * 293 * app.post('/image/upload', function(req, res, next){294 * if (req.is('an image')) {295 * // do something296 * } else {297 * next();298 * }299 * });300 * 301 * @param {String} type302 * @return {Boolean}303 * @api public304 */305306req.is = function(type){307 var fn = this.app.is(type);308 if (fn) return fn(this);309 var contentType = this.headers['content-type'];310 if (!contentType) return;311 if (!~type.indexOf('/')) type = mime.lookup(type);312 if (~type.indexOf('*')) {313 type = type.split('/')314 contentType = contentType.split('/');315 if ('*' == type[0] && type[1] == contentType[1]) return true;316 if ('*' == type[1] && type[0] == contentType[0]) return true;317 }return bool from req.is()Tj Holowaychukbefore adoption6e633b3
line 318why, one step back318 return !! ~contentType.indexOf(type);temp fix for nodes res.removeHeader() after sent "bug"Tj Holowaychukbefore adoption1abb674
lines 319–335why, one step back319};320321// Callback for isXMLHttpRequest / xhr322323function isxhr() {324 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';325}326327/**328 * Check if the request was an _XMLHttpRequest_.329 *330 * @return {Boolean}331 * @api public332 */333334req.__defineGetter__('isXMLHttpRequest', isxhr);335req.__defineGetter__('xhr', isxhr);