Skip to content
Myr

request.js

This file

Path

lib/request.js

288 lines · 7.1 KiB · JavaScript

Last changed by

replaced connect.utils.mime() with mime moduleTj Holowaychukbefore adoptiond37ad85

History of this file

Take it

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 1–12why, 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')

replaced connect.utils.mime() with mime moduleTj Holowaychukbefore adoptiond37ad85

line 13why, one step back
13 , req = http.IncomingMessage.prototype

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

line 14why, one step back
14 , utils = require('./utils')

replaced connect.utils.mime() with mime moduleTj Holowaychukbefore adoptiond37ad85

line 15why, one step back
15 , mime = require('mime');

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 16–31why, one step back
16
17/**18 * Default flash formatters.19 *20 * @type Object21 */2223var flashFormatters = exports.flashFormatters = {24 s: function(val){25 return String(val);26 }27};2829/**30 * Return request header or optional default.31 *

Added req.header() Referrer / Referer special-caseTj Holowaychukbefore adoptionb65b063

lines 32–35why, one step back
32 * The `Referrer` header field is special-cased,
33 * both `Referrer` and `Referer` will yield are34 * interchangeable.35 *

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 36–55why, one step back
36 * Examples:
37 *38 * req.header('Content-Type');39 * // => "text/plain"40 * 41 * req.header('content-type');42 * // => "text/plain"43 * 44 * req.header('Accept');45 * // => undefined46 * 47 * req.header('Accept', 'text/html');48 * // => "text/html"49 *50 * @param {String} name51 * @param {String} defaultValue52 * @return {String} 53 * @api public54 */55

Refactored req/res proto assignmentsTj Holowaychukbefore adoption99820e7

line 56why, one step back
56req.header = function(name, defaultValue){

Added req.header() Referrer / Referer special-caseTj Holowaychukbefore adoptionb65b063

lines 57–63why, one step back
57 switch (name = name.toLowerCase()) {
58 case 'referer':59 case 'referrer':60 return this.headers.referrer || this.headers.referer || defaultValue;61 default:62 return this.headers[name] || defaultValue;63 }

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 64–95why, one step back
64};
6566/**67 * Check if the _Accept_ header is present, and includes the given `type`.68 *69 * When the _Accept_ header is not present `true` is returned. Otherwise70 * the given `type` is matched by an exact match, and then subtypes. You71 * may pass the subtype such as "html" which is then converted internally72 * to "text/html" using the mime lookup table.73 *74 * Examples:75 * 76 * // Accept: text/html77 * req.accepts('html');78 * // => true79 *80 * // Accept: text/*; application/json81 * req.accepts('html');82 * req.accepts('text/html');83 * req.accepts('text/plain');84 * req.accepts('application/json');85 * // => true86 *87 * req.accepts('image/png');88 * req.accepts('png');89 * // => false90 *91 * @param {String} type92 * @return {Boolean}93 * @api public94 */95

Refactored req/res proto assignmentsTj Holowaychukbefore adoption99820e7

line 96why, one step back
96req.accepts = function(type){

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 97–107why, one step back
97 var accept = this.header('Accept');
9899 // Normalize extensions ".json" -> "json"100 if (type && '.' == type[0]) type = type.substr(1);101102 // When Accept does not exist, or is '*/*' return true 103 if (!accept || '*/*' == accept) {104 return true;105 } else if (type) {106 // Allow "html" vs "text/html" etc107 if (type.indexOf('/') < 0) {

replaced connect.utils.mime() with mime moduleTj Holowaychukbefore adoptiond37ad85

line 108why, one step back
108 type = mime.lookup(type);

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 109–138why, one step back
109 }
110111 // Check if we have a direct match112 if (~accept.indexOf(type)) return true;113114 // Check if we have type/*115 type = type.split('/')[0] + '/*';116 return accept.indexOf(type) >= 0;117 } else {118 return false;119 }120};121122/**123 * Return the value of param `name` when present or `defaultValue`.124 *125 * - Checks route placeholders, ex: _/user/:id_126 * - Checks query string params, ex: ?id=12127 * - Checks urlencoded body params, ex: id=12128 *129 * To utilize urlencoded request bodies, `req.body`130 * should be an object. This can be done by using131 * the `connect.bodyDecoder` middleware.132 *133 * @param {String} name134 * @param {Mixed} defaultValue135 * @return {String}136 * @api public137 */138

Refactored req/res proto assignmentsTj Holowaychukbefore adoption99820e7

line 139why, one step back
139req.param = function(name, defaultValue){

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 140–189why, one step back
140 // Route params like /user/:id
141 if (this.params[name] !== undefined) {142 return this.params[name]; 143 }144 // Query string params145 if (this.query[name] !== undefined) {146 return this.query[name]; 147 }148 // Request body params via connect.bodyDecoder149 if (this.body && this.body[name] !== undefined) {150 return this.body[name];151 }152 return defaultValue;153};154155/**156 * Queue flash `msg` of the given `type`.157 *158 * Examples:159 *160 * req.flash('info', 'email sent');161 * req.flash('error', 'email delivery failed');162 * req.flash('info', 'email re-sent');163 * // => 2164 *165 * req.flash('info');166 * // => ['email sent', 'email re-sent']167 *168 * req.flash('info');169 * // => []170 *171 * req.flash();172 * // => { error: ['email delivery failed'], info: [] }173 *174 * Formatting:175 *176 * Flash notifications also support arbitrary formatting support.177 * For example you may pass variable arguments to `req.flash()`178 * and use the %s specifier to be replaced by the associated argument:179 *180 * req.flash('info', 'email has been sent to %s.', userName);181 *182 * To add custom formatters use the `exports.flashFormatters` object.183 *184 * @param {String} type185 * @param {String} msg186 * @return {Array|Object|Number}187 * @api public188 */189

Refactored req/res proto assignmentsTj Holowaychukbefore adoption99820e7

line 190why, one step back
190req.flash = function(type, msg){

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 191–256why, one step back
191 var msgs = this.session.flash = this.session.flash || {};
192 if (type && msg) {193 var i = 2194 , args = arguments195 , formatters = this.app.flashFormatters || {};196 formatters.__proto__ = flashFormatters;197 msg = utils.miniMarkdown(utils.htmlEscape(msg));198 msg = msg.replace(/%([a-zA-Z])/g, function(_, format){199 var formatter = formatters[format];200 if (formatter) return formatter(args[i++]);201 });202 return (msgs[type] = msgs[type] || []).push(msg);203 } else if (type) {204 var arr = msgs[type];205 delete msgs[type];206 return arr || [];207 } else {208 this.session.flash = {};209 return msgs;210 }211};212213/**214 * Check if the incoming request contains the "Content-Type" 215 * header field, and it contains the give mime `type`.216 *217 * Examples:218 *219 * // With Content-Type: text/html; charset=utf-8220 * req.is('html');221 * req.is('text/html');222 * // => true223 * 224 * // When Content-Type is application/json225 * req.is('json');226 * req.is('application/json');227 * // => true228 * 229 * req.is('html');230 * // => false231 * 232 * Ad-hoc callbacks can also be registered with Express, to perform233 * assertions again the request, for example if we need an expressive234 * way to check if our incoming request is an image, we can register "an image"235 * callback:236 * 237 * app.is('an image', function(req){238 * return 0 == req.headers['content-type'].indexOf('image');239 * });240 * 241 * Now within our route callbacks, we can use to to assert content types242 * such as "image/jpeg", "image/png", etc.243 * 244 * app.post('/image/upload', function(req, res, next){245 * if (req.is('an image')) {246 * // do something247 * } else {248 * next();249 * }250 * });251 * 252 * @param {String} type253 * @return {Boolean}254 * @api public255 */256

Refactored req/res proto assignmentsTj Holowaychukbefore adoption99820e7

line 257why, one step back
257req.is = function(type){

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 258–261why, one step back
258 var fn = this.app.is(type);
259 if (fn) return fn(this);260 var contentType = this.headers['content-type'];261 if (!contentType) return;

replaced connect.utils.mime() with mime moduleTj Holowaychukbefore adoptiond37ad85

line 262why, one step back
262 if (!~type.indexOf('/')) type = mime.lookup(type);

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 263–286why, one step back
263 // TODO: remove when connect no longer appends charset...
264 if (~type.indexOf(';')) type = type.split(';')[0];265 if (~type.indexOf('*')) {266 type = type.split('/')267 contentType = contentType.split('/');268 if ('*' == type[0] && type[1] == contentType[1]) return true;269 if ('*' == type[1] && type[0] == contentType[0]) return true;270 }271 return ~contentType.indexOf(type);272};273274// Callback for isXMLHttpRequest / xhr275276function isxhr() {277 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';278}279280/**281 * Check if the request was an _XMLHttpRequest_.282 *283 * @return {Boolean}284 * @api public285 */286

Refactored req/res proto assignmentsTj Holowaychukbefore adoption99820e7

lines 287–288why, one step back
287req.__defineGetter__('isXMLHttpRequest', isxhr);
288req.__defineGetter__('xhr', isxhr);