SIGN IN SIGN UP
2011-02-03 20:20:42 -08:00
/**
* Module dependencies.
*/
var http = require('http')
, utils = require('./utils')
, connect = require('connect')
2012-04-25 16:12:40 -07:00
, parse = connect.utils.parseUrl
, mime = require('mime');
2011-02-03 20:20:42 -08:00
/**
* Request prototype.
*/
var req = exports = module.exports = {
__proto__: http.IncomingMessage.prototype
};
2011-02-03 20:20:42 -08:00
/**
2012-02-07 08:25:40 -08:00
* Return request header.
2011-02-03 20:20:42 -08:00
*
* The `Referrer` header field is special-cased,
2012-04-18 17:42:13 -07:00
* both `Referrer` and `Referer` are interchangeable.
*
2011-02-03 20:20:42 -08:00
* Examples:
*
* req.get('Content-Type');
2011-02-03 20:20:42 -08:00
* // => "text/plain"
*
* req.get('content-type');
2011-02-03 20:20:42 -08:00
* // => "text/plain"
*
2012-02-07 08:32:08 -08:00
* req.get('Something');
2011-02-03 20:20:42 -08:00
* // => undefined
2012-04-16 08:53:16 -07:00
*
* Aliased as `req.header()`.
*
2011-02-03 20:20:42 -08:00
* @param {String} name
* @return {String}
* @api public
*/
req.get =
req.header = function(name){
switch (name = name.toLowerCase()) {
case 'referer':
case 'referrer':
return this.headers.referrer
|| this.headers.referer;
default:
return this.headers[name];
}
2011-02-03 20:20:42 -08:00
};
/**
2012-03-24 11:37:26 -07:00
* Check if the given `type(s)` is acceptable, returning
* the best match when true, otherwise `undefined`, in which
* case you should respond with 406 "Not Acceptable".
2011-02-03 20:20:42 -08:00
*
2012-03-24 11:39:47 -07:00
* The `type` value may be a single mime type string
* such as "application/json", the extension name
* such as "json", a comma-delimted list such as "json, html, text/plain",
* or an array `["json", "html", "text/plain"]`. When a list
* or array is given the _best_ match, if any is returned.
*
2011-02-03 20:20:42 -08:00
* Examples:
*
* // Accept: text/html
* req.accepts('html');
2012-03-24 11:39:47 -07:00
* // => "html"
2011-02-03 20:20:42 -08:00
*
2012-03-24 11:42:26 -07:00
* // Accept: text/*, application/json
2011-02-03 20:20:42 -08:00
* req.accepts('html');
2012-03-24 11:39:47 -07:00
* // => "html"
2011-02-03 20:20:42 -08:00
* req.accepts('text/html');
2012-03-24 11:39:47 -07:00
* // => "text/html"
* req.accepts('json, text');
* // => "json"
2011-02-03 20:20:42 -08:00
* req.accepts('application/json');
2012-03-24 11:39:47 -07:00
* // => "application/json"
2011-02-03 20:20:42 -08:00
*
2012-03-24 11:42:26 -07:00
* // Accept: text/*, application/json
2011-02-03 20:20:42 -08:00
* req.accepts('image/png');
* req.accepts('png');
2012-03-24 11:39:47 -07:00
* // => undefined
2011-02-03 20:20:42 -08:00
*
2012-03-24 11:42:26 -07:00
* // Accept: text/*;q=.5, application/json
2012-03-23 17:58:15 -07:00
* req.accepts(['html', 'json']);
2012-03-24 11:39:47 -07:00
* req.accepts('html, json');
* // => "json"
2012-03-23 17:58:15 -07:00
*
* @param {String|Array} type(s)
2012-03-24 11:37:26 -07:00
* @return {String}
2011-02-03 20:20:42 -08:00
* @api public
*/
req.accepts = function(type){
2012-02-07 08:32:08 -08:00
return utils.accepts(type, this.get('Accept'));
2011-02-03 20:20:42 -08:00
};
2011-11-20 12:51:27 -08:00
/**
* Check if the given `charset` is acceptable,
* otherwise you should respond with 406 "Not Acceptable".
*
* @param {String} charset
* @return {Boolean}
* @api public
*/
req.acceptsCharset = function(charset){
var accepted = this.acceptedCharsets;
return accepted.length
? ~accepted.indexOf(charset)
: true;
};
2011-11-20 12:53:01 -08:00
/**
* Check if the given `lang` is acceptable,
* otherwise you should respond with 406 "Not Acceptable".
*
* @param {String} lang
* @return {Boolean}
* @api public
*/
req.acceptsLanguage = function(lang){
var accepted = this.acceptedLanguages;
return accepted.length
? ~accepted.indexOf(lang)
: true;
};
2011-11-19 22:08:26 -08:00
/**
* Return an array of Accepted media types
* ordered from highest quality to lowest.
*
* Examples:
*
* [ { value: 'application/json',
* quality: 1,
* type: 'application',
* subtype: 'json' },
* { value: 'text/html',
* quality: 0.5,
* type: 'text',
* subtype: 'html' } ]
*
* @return {Array}
* @api public
*/
req.__defineGetter__('accepted', function(){
2012-02-07 08:32:08 -08:00
var accept = this.get('Accept');
2011-11-19 22:08:26 -08:00
return accept
? utils.parseAccept(accept)
: [];
});
2011-11-19 22:12:09 -08:00
/**
* Return an array of Accepted languages
* ordered from highest quality to lowest.
*
* Examples:
*
* Accept-Language: en;q=.5, en-us
* ['en-us', 'en']
*
* @return {Array}
* @api public
*/
req.__defineGetter__('acceptedLanguages', function(){
2012-02-07 08:32:08 -08:00
var accept = this.get('Accept-Language');
2011-11-19 22:12:09 -08:00
return accept
? utils
.parseQuality(accept)
.map(function(obj){
return obj.value;
})
: [];
});
2011-11-19 22:16:16 -08:00
/**
* Return an array of Accepted charsets
* ordered from highest quality to lowest.
*
* Examples:
*
* Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8
* ['unicode-1-1', 'iso-8859-5']
*
* @return {Array}
* @api public
*/
req.__defineGetter__('acceptedCharsets', function(){
2012-02-07 08:32:08 -08:00
var accept = this.get('Accept-Charset');
2011-11-19 22:16:16 -08:00
return accept
? utils
.parseQuality(accept)
.map(function(obj){
return obj.value;
})
: [];
});
2011-02-03 20:20:42 -08:00
/**
* Return the value of param `name` when present or `defaultValue`.
*
* - Checks route placeholders, ex: _/user/:id_
* - Checks body params, ex: id=12, {"id":12}
2011-02-03 20:20:42 -08:00
* - Checks query string params, ex: ?id=12
*
2011-12-07 08:41:22 -08:00
* To utilize request bodies, `req.body`
2011-02-03 20:20:42 -08:00
* should be an object. This can be done by using
2011-12-07 08:41:22 -08:00
* the `connect.bodyParser()` middleware.
2011-02-03 20:20:42 -08:00
*
* @param {String} name
* @param {Mixed} defaultValue
* @return {String}
* @api public
*/
req.param = function(name, defaultValue){
2012-06-05 18:59:26 -07:00
var params = this.params || {};
var body = this.body || {};
var query = this.query || {};
if (null != params[name] && params.hasOwnProperty(name)) return params[name];
2012-06-05 18:59:26 -07:00
if (null != body[name]) return body[name];
if (null != query[name]) return query[name];
2011-02-03 20:20:42 -08:00
return defaultValue;
};
/**
* Check if the incoming request contains the "Content-Type"
* header field, and it contains the give mime `type`.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* req.is('html');
* req.is('text/html');
2011-12-30 15:10:51 -08:00
* req.is('text/*');
2011-02-03 20:20:42 -08:00
* // => true
*
* // When Content-Type is application/json
* req.is('json');
* req.is('application/json');
2011-12-30 15:10:51 -08:00
* req.is('application/*');
2011-02-03 20:20:42 -08:00
* // => true
*
* req.is('html');
* // => false
*
* @param {String} type
* @return {Boolean}
* @api public
*/
req.is = function(type){
2012-02-07 08:32:08 -08:00
var ct = this.get('Content-Type');
2011-12-30 15:04:41 -08:00
if (!ct) return false;
ct = ct.split(';')[0];
if (!~type.indexOf('/')) type = mime.lookup(type);
2011-02-03 20:20:42 -08:00
if (~type.indexOf('*')) {
2011-07-29 11:02:50 -07:00
type = type.split('/');
2011-12-30 15:04:41 -08:00
ct = ct.split('/');
if ('*' == type[0] && type[1] == ct[1]) return true;
if ('*' == type[1] && type[0] == ct[0]) return true;
return false;
2011-02-03 20:20:42 -08:00
}
2011-12-30 15:04:41 -08:00
return !! ~ct.indexOf(type);
2011-02-03 20:20:42 -08:00
};
2012-02-07 04:39:10 -08:00
/**
2012-02-07 08:19:30 -08:00
* Return the protocol string "http" or "https"
* when requested with TLS. When the "trust proxy"
* setting is enabled the "X-Forwarded-Proto" header
* field will be trusted. If you're running behind
* a reverse proxy that supplies https for you this
* may be enabled.
2012-02-07 04:39:10 -08:00
*
* @return {String}
* @api public
*/
2012-04-15 11:40:31 -07:00
req.__defineGetter__('protocol', function(){
var trustProxy = this.app.get('trust proxy');
return this.connection.encrypted
2012-02-07 04:39:10 -08:00
? 'https'
: trustProxy
2012-02-07 08:19:30 -08:00
? (this.get('X-Forwarded-Proto') || 'http')
2012-02-07 04:39:10 -08:00
: 'http';
2012-02-07 08:19:30 -08:00
});
2012-02-07 04:39:10 -08:00
2011-10-13 09:46:26 -07:00
/**
* Short-hand for:
*
* req.protocol == 'https'
2011-10-13 09:46:26 -07:00
*
* @return {Boolean}
* @api public
*/
req.__defineGetter__('secure', function(){
return 'https' == this.protocol;
2011-10-13 09:46:26 -07:00
});
2012-05-29 18:46:07 -07:00
/**
* Return the remote address, or when
* "trust proxy" is `true` return
* the upstream addr.
*
* @return {String}
* @api public
*/
req.__defineGetter__('ip', function(){
return this.ips[0] || this.connection.remoteAddress;
});
2012-03-29 16:26:59 -07:00
/**
* When "trust proxy" is `true`, parse
* the "X-Forwarded-For" ip address list.
*
2012-03-29 16:39:32 -07:00
* For example if the value were "client, proxy1, proxy2"
* you would receive the array `["client", "proxy1", "proxy2"]`
2012-03-29 16:39:32 -07:00
* where "proxy2" is the furthest down-stream.
*
2012-03-29 16:26:59 -07:00
* @return {Array}
* @api public
*/
req.__defineGetter__('ips', function(){
2012-05-29 18:51:08 -07:00
var trustProxy = this.app.get('trust proxy');
var val = this.get('X-Forwarded-For');
2012-05-29 18:36:46 -07:00
return trustProxy && val
? val.split(/ *, */)
2012-03-29 16:26:59 -07:00
: [];
});
2012-02-22 16:36:13 -08:00
/**
* Return subdomains as an array.
*
* For example "tobi.ferrets.example.com"
* would provide `["ferrets", "tobi"]`.
*
* @return {Array}
* @api public
*/
req.__defineGetter__('subdomains', function(){
return this.get('Host')
.split('.')
.slice(0, -2)
.reverse();
});
2011-10-13 09:46:26 -07:00
/**
2012-04-15 21:01:17 -07:00
* Short-hand for `url.parse(req.url).pathname`.
2011-10-13 09:46:26 -07:00
*
* @return {String}
* @api public
*/
req.__defineGetter__('path', function(){
2012-04-25 16:12:40 -07:00
return parse(this).pathname;
2011-10-13 09:46:26 -07:00
});
2012-06-05 19:24:49 -07:00
/**
* Parse the "Host" header field hostname.
*
* @return {String}
* @api public
*/
req.__defineGetter__('host', function(){
return this.get('Host').split(':')[0];
});
/**
* Check if the request is fresh, aka
* Last-Modified and/or the ETag
* still match.
*
* @return {Boolean}
* @api public
*/
req.__defineGetter__('fresh', function(){
return ! this.stale;
2011-10-13 09:46:48 -07:00
});
/**
* Check if the request is stale, aka
2012-02-07 08:21:32 -08:00
* "Last-Modified" and / or the "ETag" for the
* resource has changed.
*
* @return {Boolean}
* @api public
*/
req.__defineGetter__('stale', function(){
return connect.utils.modified(this, this.res);
2011-10-13 09:46:48 -07:00
});
2011-02-03 20:20:42 -08:00
/**
* Check if the request was an _XMLHttpRequest_.
*
* @return {Boolean}
* @api public
*/
req.__defineGetter__('xhr', function(){
2012-02-07 08:24:58 -08:00
var val = this.get('X-Requested-With') || '';
return 'xmlhttprequest' == val.toLowerCase();
});