123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- var http = require('http')
- , utils = require('./utils')
- , connect = require('connect')
- , fresh = require('fresh')
- , parseRange = require('range-parser')
- , parse = connect.utils.parseUrl
- , mime = connect.mime;
- var req = exports = module.exports = {
- __proto__: http.IncomingMessage.prototype
- };
- 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];
- }
- };
- req.accepts = function(type){
- return utils.accepts(type, this.get('Accept'));
- };
- req.acceptsEncoding = function(encoding){
- return !! ~this.acceptedEncodings.indexOf(encoding);
- };
- req.acceptsCharset = function(charset){
- var accepted = this.acceptedCharsets;
- return accepted.length
- ? !! ~accepted.indexOf(charset)
- : true;
- };
- req.acceptsLanguage = function(lang){
- var accepted = this.acceptedLanguages;
- return accepted.length
- ? !! ~accepted.indexOf(lang)
- : true;
- };
- req.range = function(size){
- var range = this.get('Range');
- if (!range) return;
- return parseRange(size, range);
- };
- req.__defineGetter__('acceptedEncodings', function(){
- var accept = this.get('Accept-Encoding');
- return accept
- ? accept.trim().split(/ *, */)
- : [];
- });
- req.__defineGetter__('accepted', function(){
- var accept = this.get('Accept');
- return accept
- ? utils.parseAccept(accept)
- : [];
- });
- req.__defineGetter__('acceptedLanguages', function(){
- var accept = this.get('Accept-Language');
- return accept
- ? utils
- .parseParams(accept)
- .map(function(obj){
- return obj.value;
- })
- : [];
- });
- req.__defineGetter__('acceptedCharsets', function(){
- var accept = this.get('Accept-Charset');
- return accept
- ? utils
- .parseParams(accept)
- .map(function(obj){
- return obj.value;
- })
- : [];
- });
- req.param = function(name, defaultValue){
- var params = this.params || {};
- var body = this.body || {};
- var query = this.query || {};
- if (null != params[name] && params.hasOwnProperty(name)) return params[name];
- if (null != body[name]) return body[name];
- if (null != query[name]) return query[name];
- return defaultValue;
- };
- req.is = function(type){
- var ct = this.get('Content-Type');
- if (!ct) return false;
- ct = ct.split(';')[0];
- if (!~type.indexOf('/')) type = mime.lookup(type);
- if (~type.indexOf('*')) {
- type = type.split('/');
- ct = ct.split('/');
- if ('*' == type[0] && type[1] == ct[1]) return true;
- if ('*' == type[1] && type[0] == ct[0]) return true;
- return false;
- }
- return !! ~ct.indexOf(type);
- };
- req.__defineGetter__('protocol', function(){
- var trustProxy = this.app.get('trust proxy');
- if (this.connection.encrypted) return 'https';
- if (!trustProxy) return 'http';
- var proto = this.get('X-Forwarded-Proto') || 'http';
- return proto.split(/\s*,\s*/)[0];
- });
- req.__defineGetter__('secure', function(){
- return 'https' == this.protocol;
- });
- req.__defineGetter__('ip', function(){
- return this.ips[0] || this.connection.remoteAddress;
- });
- req.__defineGetter__('ips', function(){
- var trustProxy = this.app.get('trust proxy');
- var val = this.get('X-Forwarded-For');
- return trustProxy && val
- ? val.split(/ *, */)
- : [];
- });
- req.__defineGetter__('auth', function(){
-
- var auth = this.get('Authorization');
- if (!auth) return;
-
- var parts = auth.split(' ');
- if ('basic' != parts[0].toLowerCase()) return;
- if (!parts[1]) return;
- auth = parts[1];
-
- auth = new Buffer(auth, 'base64').toString().match(/^([^:]*):(.*)$/);
- if (!auth) return;
- return { username: auth[1], password: auth[2] };
- });
- req.__defineGetter__('subdomains', function(){
- var offset = this.app.get('subdomain offset');
- return (this.host || '')
- .split('.')
- .reverse()
- .slice(offset);
- });
- req.__defineGetter__('path', function(){
- return parse(this).pathname;
- });
- req.__defineGetter__('host', function(){
- var trustProxy = this.app.get('trust proxy');
- var host = trustProxy && this.get('X-Forwarded-Host');
- host = host || this.get('Host');
- if (!host) return;
- return host.split(':')[0];
- });
- req.__defineGetter__('fresh', function(){
- var method = this.method;
- var s = this.res.statusCode;
-
- if ('GET' != method && 'HEAD' != method) return false;
-
- if ((s >= 200 && s < 300) || 304 == s) {
- return fresh(this.headers, this.res._headers);
- }
- return false;
- });
- req.__defineGetter__('stale', function(){
- return !this.fresh;
- });
- req.__defineGetter__('xhr', function(){
- var val = this.get('X-Requested-With') || '';
- return 'xmlhttprequest' == val.toLowerCase();
- });
|