Skip to content
Myr

request.js

This file

Path

lib/request.js

331 lines · 8.2 KiB · JavaScript

Last changed by

make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107

History of this file

Take it

The walk stopped after 200 commits: lines older than 0477a53 point at it.

misc refactorTj Holowaychukbefore adoption0477a53

lines 1–17why, 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 , req = http.IncomingMessage.prototype14 , utils = require('./utils')15 , mime = require('mime');1617/**

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 18why, one step back
18 * Default notification formatters.

misc refactorTj Holowaychukbefore adoption0477a53

lines 19–20why, one step back
19 */
20

Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe

line 21why, one step back
21var defaultFormatters = exports.formatters = {

misc refactorTj Holowaychukbefore adoption0477a53

lines 22–23why, one step back
22 s: function(val){
23 return String(val);

Added `%d` flash formatterTj Holowaychukbefore adoptionc7c9606

lines 24–26why, one step back
24 },
25 d: function(val){26 return val | 0;

misc refactorTj Holowaychukbefore adoption0477a53

lines 27–69why, one step back
27 }
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 back
70 * Get `field`'s `param` value, defaulting to ''.
71 *72 * Examples:73 *

docs for req.get()Tj Holowaychukbefore adoption6bb100d

lines 74–75why, one step back
74 * req.get('content-disposition', 'filename');
75 * // => "something.png"

Merge branch 'param'Tj Holowaychukbefore adoption1820ea6

line 76why, one step back
76 *

examplesTj Holowaychukbefore adoption7eec21a

lines 77–79why, one step back
77 * req.get('content-disposition', 'rawr');
78 * // => ""79 *

Merge branch 'param'Tj Holowaychukbefore adoption1820ea6

lines 80–88why, one step back
80 * @param {String} field
81 * @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 back
89 var regexp = new RegExp(param + ' *= *(?:"([^"]+)"|([^;]+))', 'i');

Merge branch 'param'Tj Holowaychukbefore adoption1820ea6

lines 90–94why, one step back
90 if (!regexp.exec(val)) return '';
91 return RegExp.$1 || RegExp.$2;92};9394/**

misc refactorTj Holowaychukbefore adoption0477a53

lines 95–124why, one step back
95 * 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){

Refactored `req.accepts()`Tj Holowaychukbefore adoptionb1d4b71

lines 125–126why, one step back
125 var accept = this.header('Accept')
126 , type = String(type);

misc refactorTj Holowaychukbefore adoption0477a53

line 127why, one step back
127

Refactored `req.accepts()`Tj Holowaychukbefore adoptionb1d4b71

lines 128–130why, one step back
128 // when not present or "*/*" accept anything
129 if (!accept || '*/*' == accept) return true;130

misc refactorTj Holowaychukbefore adoption0477a53

line 131why, one step back
131 // normalize extensions ".json" -> "json"

Refactored `req.accepts()`Tj Holowaychukbefore adoptionb1d4b71

line 132why, one step back
132 if ('.' == type[0]) type = type.substr(1);

misc refactorTj Holowaychukbefore adoption0477a53

line 133why, one step back
133

Refactored `req.accepts()`Tj Holowaychukbefore adoptionb1d4b71

lines 134–135why, one step back
134 // allow "html" vs "text/html" etc
135 if (!~type.indexOf('/')) type = mime.lookup(type);

misc refactorTj Holowaychukbefore adoption0477a53

line 136why, one step back
136

Refactored `req.accepts()`Tj Holowaychukbefore adoptionb1d4b71

lines 137–138why, one step back
137 // check if we have a direct match
138 if (~accept.indexOf(type)) return true;

misc refactorTj Holowaychukbefore adoption0477a53

line 139why, one step back
139

Refactored `req.accepts()`Tj Holowaychukbefore adoptionb1d4b71

lines 140–142why, one step back
140 // check if we have type/*
141 type = type.split('/')[0] + '/*';142 return !! ~accept.indexOf(type);

misc refactorTj Holowaychukbefore adoption0477a53

lines 143–163why, one step back
143};
144145/**146 * Return the value of param `name` when present or `defaultValue`.147 *148 * - Checks route placeholders, ex: _/user/:id_149 * - Checks query string params, ex: ?id=12150 * - Checks urlencoded body params, ex: id=12151 *152 * To utilize urlencoded request bodies, `req.body`153 * should be an object. This can be done by using154 * the `connect.bodyParser` middleware.155 *156 * @param {String} name157 * @param {Mixed} defaultValue158 * @return {String}159 * @api public160 */161162req.param = function(name, defaultValue){163 // route params like /user/:id

refactored `req.param()`Tj Holowaychukbefore adoptiona4621a6

lines 164–166why, one step back
164 if (this.params
165 && this.params.hasOwnProperty(name)166 && undefined !== this.params[name]) {

misc refactorTj Holowaychukbefore adoption0477a53

lines 167–169why, one step back
167 return this.params[name];
168 }169 // query string params

refactored `req.param()`Tj Holowaychukbefore adoptiona4621a6

lines 170–171why, one step back
170 if (undefined !== this.query[name]) return this.query[name];
171

misc refactorTj Holowaychukbefore adoption0477a53

line 172why, one step back
172 // request body params via connect.bodyParser

refactored `req.param()`Tj Holowaychukbefore adoptiona4621a6

lines 173–174why, one step back
173 if (this.body && undefined !== this.body[name]) return this.body[name];
174

misc refactorTj Holowaychukbefore adoption0477a53

lines 175–180why, one step back
175 return defaultValue;
176};177178/**179 * Queue flash `msg` of the given `type`.180 *

Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30

lines 181–182why, one step back
181 * This method is aliased as `req.notify()`.
182 *

misc refactorTj Holowaychukbefore adoption0477a53

lines 183–184why, one step back
183 * Examples:
184 *

Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30

line 185why, one step back
185 * req.notify('info', 'email sent');

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

lines 186–188why, one step back
186 * req.notify('info', 'email sent');
187 * req.notify('error', 'email delivery failed');188 * req.notify('info', 'email re-sent');

misc refactorTj Holowaychukbefore adoption0477a53

lines 189–190why, one step back
189 * // => 2
190 *

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 191why, one step back
191 * req.notify('info');

misc refactorTj Holowaychukbefore adoption0477a53

lines 192–193why, one step back
192 * // => ['email sent', 'email re-sent']
193 *

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 194why, one step back
194 * req.notify('info');

misc refactorTj Holowaychukbefore adoption0477a53

lines 195–196why, one step back
195 * // => []
196 *

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 197why, one step back
197 * req.notify();

misc refactorTj Holowaychukbefore adoption0477a53

lines 198–202why, one step back
198 * // => { error: ['email delivery failed'], info: [] }
199 *200 * Formatting:201 *202 * Flash notifications also support arbitrary formatting support.

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 203why, one step back
203 * For example you may pass variable arguments to `req.notify()`

misc refactorTj Holowaychukbefore adoption0477a53

lines 204–205why, one step back
204 * and use the %s specifier to be replaced by the associated argument:
205 *

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 206why, one step back
206 * req.notify('info', 'email has been sent to %s.', userName);

misc refactorTj Holowaychukbefore adoption0477a53

line 207why, one step back
207 *

Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe

lines 208–210why, one step back
208 * You may add addition formatters by defining `app.formatters`,
209 * for example in the following snippet we define `%u` to uppercase210 * a string:

misc refactorTj Holowaychukbefore adoption0477a53

line 211why, one step back
211 *

Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe

lines 212–217why, one step back
212 * app.formatters = {
213 * u: function(val){214 * return String(val).toUpperCase();215 * }216 * };217 *

misc refactorTj Holowaychukbefore adoption0477a53

lines 218–223why, one step back
218 * @param {String} type
219 * @param {String} msg220 * @return {Array|Object|Number}221 * @api public222 */223

Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30

line 224why, one step back
224req.notify = function(type, msg){

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

lines 225–227why, one step back
225 var sess = this.session;
226 if (null == sess) throw Error('req.notify() requires sessions');227 var msgs = sess.notifications = sess.notifications || {};

refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6

line 228why, one step back
228

make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107

lines 229–250why, one step back
229 switch (arguments.length) {
230 // flush all messages231 case 0:232 sess.notifications = {};233 return msgs234 // flush messages for a specific type235 case 1:236 var arr = msgs[type];237 delete msgs[type];238 return arr || [];239 // set notification message240 default:241 var i = 2242 , args = arguments243 , formatters = this.app.formatters || {};244 formatters.__proto__ = defaultFormatters;245 msg = utils.miniMarkdown(utils.escape(msg));246 msg = msg.replace(/%([a-zA-Z])/g, function(_, format){247 var formatter = formatters[format];248 if (formatter) return formatter(args[i++]);249 });250 return (msgs[type] = msgs[type] || []).push(msg);

refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6

line 251why, one step back
251 }

misc refactorTj Holowaychukbefore adoption0477a53

lines 252–280why, one step back
252};
253254/**255 * Check if the incoming request contains the "Content-Type" 256 * header field, and it contains the give mime `type`.257 *258 * Examples:259 *260 * // With Content-Type: text/html; charset=utf-8261 * req.is('html');262 * req.is('text/html');263 * // => true264 * 265 * // When Content-Type is application/json266 * req.is('json');267 * req.is('application/json');268 * // => true269 * 270 * req.is('html');271 * // => false272 * 273 * Ad-hoc callbacks can also be registered with Express, to perform274 * assertions again the request, for example if we need an expressive275 * way to check if our incoming request is an image, we can register "an image"276 * callback:277 * 278 * app.is('an image', function(req){279 * return 0 == req.headers['content-type'].indexOf('image');280 * });

another `req.is()` exampleTj Holowaychukbefore adoption4b8e08d

lines 281–285why, one step back
281 *
282 * app.is('an attachment', function(req){283 * return 0 == req.headers['content-disposition'].indexOf('attachment');284 * });285 *

misc refactorTj Holowaychukbefore adoption0477a53

lines 286–308why, one step back
286 * Now within our route callbacks, we can use to to assert content types
287 * such as "image/jpeg", "image/png", etc.288 * 289 * app.post('/image/upload', function(req, res, next){290 * if (req.is('an image')) {291 * // do something292 * } else {293 * next();294 * }295 * });296 * 297 * @param {String} type298 * @return {Boolean}299 * @api public300 */301302req.is = function(type){303 var fn = this.app.is(type);304 if (fn) return fn(this);305 var contentType = this.headers['content-type'];306 if (!contentType) return;307 if (!~type.indexOf('/')) type = mime.lookup(type);308 if (~type.indexOf('*')) {

semiTj Holowaychukbefore adoption82c78ec

line 309why, one step back
309 type = type.split('/');

misc refactorTj Holowaychukbefore adoption0477a53

lines 310–313why, one step back
310 contentType = contentType.split('/');
311 if ('*' == type[0] && type[1] == contentType[1]) return true;312 if ('*' == type[1] && type[0] == contentType[0]) return true;313 }

return bool from req.is()Tj Holowaychukbefore adoption6e633b3

line 314why, one step back
314 return !! ~contentType.indexOf(type);

misc refactorTj Holowaychukbefore adoption0477a53

lines 315–331why, one step back
315};
316317// Callback for isXMLHttpRequest / xhr318319function isxhr() {320 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';321}322323/**324 * Check if the request was an _XMLHttpRequest_.325 *326 * @return {Boolean}327 * @api public328 */329330req.__defineGetter__('isXMLHttpRequest', isxhr);331req.__defineGetter__('xhr', isxhr);