2013-11-13 17:01:15 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var debug = require('debug')('koa:context');
|
2013-11-13 17:01:15 +00:00
|
|
|
var Request = require('./request');
|
|
|
|
var Response = require('./response');
|
2013-11-08 01:05:26 +00:00
|
|
|
var Cookies = require('cookies');
|
2013-08-17 07:15:57 +00:00
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prototype.
|
|
|
|
*/
|
|
|
|
|
2013-11-14 02:34:15 +00:00
|
|
|
module.exports = {
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#header.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get header() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.header;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#url.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
get url() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.url;
|
2013-09-14 22:54:17 +00:00
|
|
|
},
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#url=.
|
2013-09-14 22:54:17 +00:00
|
|
|
*/
|
2013-08-28 02:30:35 +00:00
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
set url(val) {
|
2013-11-13 17:01:15 +00:00
|
|
|
this.request.url = val;
|
2013-08-28 02:30:35 +00:00
|
|
|
},
|
2013-08-21 04:24:18 +00:00
|
|
|
|
2013-08-28 02:30:35 +00:00
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#method.
|
2013-08-28 02:30:35 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
get method() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.method;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#method=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
set method(val) {
|
2013-11-13 17:01:15 +00:00
|
|
|
this.request.method = val;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#status.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-24 06:54:07 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get status() {
|
|
|
|
return this.response.status;
|
2013-10-24 06:44:22 +00:00
|
|
|
},
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#status=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
set status(val) {
|
2013-11-13 17:01:15 +00:00
|
|
|
this.response.status = val;
|
2013-10-24 06:44:22 +00:00
|
|
|
},
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#body.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
get body() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.response.body;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#body=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
set body(val) {
|
2013-11-13 17:01:15 +00:00
|
|
|
this.response.body = val;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#path.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get path() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.path;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#path=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
set path(val) {
|
|
|
|
this.request.path = val;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#query.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get query() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.query;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#query=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
set query(val) {
|
|
|
|
this.request.query = val;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#querystring.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get querystring() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.querystring;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#querystring=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
set querystring(val) {
|
|
|
|
this.request.querystring = val;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#host.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get host() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.host;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#fresh.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get fresh() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.fresh;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#stale.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get stale() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.stale;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#idempotent.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get idempotent() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.idempotent;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#socket.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get socket() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.socket;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#length.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
get length() {
|
2013-11-13 17:01:15 +00:00
|
|
|
return this.request.length;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
2013-09-14 22:54:17 +00:00
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#length.
|
2013-09-14 22:54:17 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
set length(val) {
|
|
|
|
this.response.length = val;
|
2013-09-14 22:54:17 +00:00
|
|
|
},
|
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#protocol.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get protocol() {
|
|
|
|
return this.request.protocol;
|
|
|
|
},
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
|
|
|
* Delegate to Request#secure.
|
|
|
|
*/
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get secure() {
|
|
|
|
return this.request.secure;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#ip.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get ip() {
|
|
|
|
return this.request.ip;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#ips.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get ips() {
|
|
|
|
return this.request.ips;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#subdomains.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get subdomains() {
|
|
|
|
return this.request.subdomains;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#headerSent.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get headerSent() {
|
|
|
|
return this.response.headerSent;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#type=.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
set type(val) {
|
|
|
|
this.response.type = val;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#type.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
get type() {
|
|
|
|
return this.request.type;
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#accepts().
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
accepts: function() {
|
|
|
|
return this.request.accepts.apply(this.request, arguments);
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#acceptsCharsets().
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
acceptsCharsets: function() {
|
|
|
|
return this.request.acceptsCharsets.apply(this.request, arguments);
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#acceptsEncodings().
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
acceptsEncodings: function() {
|
|
|
|
return this.request.acceptsEncodings.apply(this.request, arguments);
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Request#acceptsLanguages().
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
acceptsLanguages: function() {
|
|
|
|
return this.request.acceptsLanguages.apply(this.request, arguments);
|
2013-08-17 07:15:57 +00:00
|
|
|
},
|
2013-10-24 06:54:07 +00:00
|
|
|
|
2013-11-08 22:38:35 +00:00
|
|
|
/**
|
2013-11-13 17:01:15 +00:00
|
|
|
* Delegate to Response#vary().
|
2013-11-08 22:38:35 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
vary: function() {
|
|
|
|
return this.response.vary.apply(this.response, arguments);
|
2013-10-24 05:38:06 +00:00
|
|
|
},
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
/**
|
|
|
|
* Delegate to Request#is().
|
|
|
|
*/
|
|
|
|
|
|
|
|
is: function() {
|
|
|
|
return this.request.is.apply(this.request, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to Response#append().
|
|
|
|
*/
|
|
|
|
|
|
|
|
append: function() {
|
|
|
|
return this.response.append.apply(this.response, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to Request#get().
|
|
|
|
*/
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.request.get.apply(this.request, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to Response#set().
|
|
|
|
*/
|
|
|
|
|
|
|
|
set: function() {
|
|
|
|
return this.response.set.apply(this.response, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to Response#redirect().
|
|
|
|
*/
|
|
|
|
|
|
|
|
redirect: function() {
|
|
|
|
return this.response.redirect.apply(this.response, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to Response#attachment().
|
|
|
|
*/
|
|
|
|
|
|
|
|
attachment: function() {
|
|
|
|
return this.response.attachment.apply(this.response, arguments);
|
2013-11-14 02:34:15 +00:00
|
|
|
},
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-08-28 03:54:13 +00:00
|
|
|
/**
|
|
|
|
* Throw an error with `msg` and optional `status`
|
2013-09-12 15:01:40 +00:00
|
|
|
* defaulting to 500. Note that these are user-level
|
|
|
|
* errors, and the message may be exposed to the client.
|
2013-08-28 03:54:13 +00:00
|
|
|
*
|
|
|
|
* this.error(403)
|
|
|
|
* this.error('name required', 400)
|
|
|
|
* this.error('something exploded')
|
|
|
|
*
|
|
|
|
* @param {String|Number} msg
|
|
|
|
* @param {Number} status
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
error: function(msg, status){
|
2013-09-14 04:06:16 +00:00
|
|
|
// TODO: switch order... feels weird now that im used to express
|
2013-08-28 03:54:13 +00:00
|
|
|
if ('number' == typeof msg) {
|
|
|
|
var tmp = msg;
|
|
|
|
msg = http.STATUS_CODES[tmp];
|
|
|
|
status = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
var err = new Error(msg);
|
|
|
|
err.status = status || 500;
|
2013-09-12 15:01:40 +00:00
|
|
|
err.expose = true;
|
2013-08-28 03:54:13 +00:00
|
|
|
throw err;
|
|
|
|
},
|
2013-10-05 19:27:15 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
|
|
|
* Default error handling.
|
|
|
|
*
|
|
|
|
* @param {Error} err
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2013-08-22 02:47:56 +00:00
|
|
|
onerror: function(err){
|
2013-10-09 06:23:14 +00:00
|
|
|
// don't do anything if there is no error.
|
|
|
|
// this allows you to pass `this.onerror`
|
|
|
|
// to node-style callbacks.
|
|
|
|
if (!err) return;
|
|
|
|
|
2013-08-22 02:47:56 +00:00
|
|
|
// nothing we can do here other
|
|
|
|
// than delegate to the app-level
|
|
|
|
// handler and log.
|
2013-08-28 03:24:04 +00:00
|
|
|
if (this.headerSent) {
|
|
|
|
err.headerSent = true;
|
2013-08-31 06:48:54 +00:00
|
|
|
this.app.emit('error', err, this);
|
2013-08-28 03:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delegate
|
2013-08-31 06:48:54 +00:00
|
|
|
this.app.emit('error', err, this);
|
2013-08-22 02:47:56 +00:00
|
|
|
|
2013-09-08 19:11:02 +00:00
|
|
|
// force text/plain
|
|
|
|
this.type = 'text';
|
|
|
|
|
|
|
|
// ENOENT support
|
|
|
|
if ('ENOENT' == err.code) err.status = 404;
|
|
|
|
|
|
|
|
// default to 500
|
|
|
|
err.status = err.status || 500;
|
2013-08-22 02:47:56 +00:00
|
|
|
|
|
|
|
// respond
|
2013-09-12 15:05:50 +00:00
|
|
|
var code = http.STATUS_CODES[err.status];
|
|
|
|
var msg = err.expose ? err.message : code;
|
2013-09-08 19:11:02 +00:00
|
|
|
this.status = err.status;
|
2013-09-12 15:05:50 +00:00
|
|
|
this.res.end(msg);
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
};
|