Skip to content
Myr

request.js

This file

Path

lib/request.js

332 lines · 8.1 KiB · JavaScript

Last changed by

semiTj Holowaychukbefore adoption82c78ec

History of this file

Take it

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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.

fixed testsTj Holowaychukbefore adoptiond2f963d

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 = {

fixed testsTj Holowaychukbefore adoptiond2f963d

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;

fixed testsTj Holowaychukbefore adoptiond2f963d

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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);

fixed testsTj Holowaychukbefore adoptiond2f963d

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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);

fixed testsTj Holowaychukbefore adoptiond2f963d

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);

fixed testsTj Holowaychukbefore adoptiond2f963d

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;

fixed testsTj Holowaychukbefore adoptiond2f963d

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);

fixed testsTj Holowaychukbefore adoptiond2f963d

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]) {

fixed testsTj Holowaychukbefore adoptiond2f963d

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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 *

fixed testsTj Holowaychukbefore adoptiond2f963d

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');

fixed testsTj Holowaychukbefore adoptiond2f963d

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

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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');

fixed testsTj Holowaychukbefore adoptiond2f963d

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

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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()`

fixed testsTj Holowaychukbefore adoptiond2f963d

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);

fixed testsTj Holowaychukbefore adoptiond2f963d

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:

fixed testsTj Holowaychukbefore adoptiond2f963d

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 *

fixed testsTj Holowaychukbefore adoptiond2f963d

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

lines 228–229why, one step back
228
229 // notification

fixed testsTj Holowaychukbefore adoptiond2f963d

lines 230–232why, one step back
230 if (type && msg) {
231 var i = 2232 , args = arguments

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

lines 233–234why, one step back
233 , formatters = this.app.formatters || {};
234 formatters.__proto__ = defaultFormatters;

fixed testsTj Holowaychukbefore adoptiond2f963d

lines 235–240why, one step back
235 msg = utils.miniMarkdown(utils.escape(msg));
236 msg = msg.replace(/%([a-zA-Z])/g, function(_, format){237 var formatter = formatters[format];238 if (formatter) return formatter(args[i++]);239 });240 return (msgs[type] = msgs[type] || []).push(msg);

refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6

lines 241–244why, one step back
241 }
242243 // flush messages for a specific type244 if (type) {

fixed testsTj Holowaychukbefore adoptiond2f963d

lines 245–248why, one step back
245 var arr = msgs[type];
246 delete msgs[type];247 return arr || [];248 }

refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6

lines 249–250why, one step back
249
250 // flush all messages

Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702

line 251why, one step back
251 sess.notifications = {};

refactored `req.flash()`Tj Holowaychukbefore adoption5982fa6

line 252why, one step back
252 return msgs;

fixed testsTj Holowaychukbefore adoptiond2f963d

lines 253–281why, one step back
253};
254255/**256 * Check if the incoming request contains the "Content-Type" 257 * header field, and it contains the give mime `type`.258 *259 * Examples:260 *261 * // With Content-Type: text/html; charset=utf-8262 * req.is('html');263 * req.is('text/html');264 * // => true265 * 266 * // When Content-Type is application/json267 * req.is('json');268 * req.is('application/json');269 * // => true270 * 271 * req.is('html');272 * // => false273 * 274 * Ad-hoc callbacks can also be registered with Express, to perform275 * assertions again the request, for example if we need an expressive276 * way to check if our incoming request is an image, we can register "an image"277 * callback:278 * 279 * app.is('an image', function(req){280 * return 0 == req.headers['content-type'].indexOf('image');281 * });

another `req.is()` exampleTj Holowaychukbefore adoption4b8e08d

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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

semiTj Holowaychukbefore adoption82c78ec

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

fixed testsTj Holowaychukbefore adoptiond2f963d

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

return bool from req.is()Tj Holowaychukbefore adoption6e633b3

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

fixed testsTj Holowaychukbefore adoptiond2f963d

lines 316–332why, one step back
316};
317318// Callback for isXMLHttpRequest / xhr319320function isxhr() {321 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';322}323324/**325 * Check if the request was an _XMLHttpRequest_.326 *327 * @return {Boolean}328 * @api public329 */330331req.__defineGetter__('isXMLHttpRequest', isxhr);332req.__defineGetter__('xhr', isxhr);