-
Notifications
You must be signed in to change notification settings - Fork 0
/
Responder.njs
139 lines (119 loc) · 3.86 KB
/
Responder.njs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module.exports = Responder;
var Promise = require('promise'),
url = require('url'),
debug = require('debug')('jugglypuff:Responder'),
ContGen = require('ContinuousGenerator'),
_ = require('underscore'),
cookie = require('cookie'),
CError = require('ExtendableError').CodedError,
EventEmitter = require('events').EventEmitter;
/**
* Events:
* unhandledMethodError(responder, error)
*/
function Responder(req, res) {
this.req = req;
this.res = res;
this.cookiesToSet_ = {};
this.statusCode_ = null;
this.headersToSet_ = {};
var parsedUrl = url.parse(req.url, true);
this.req.query = parsedUrl.query;
this.req.pathname = parsedUrl.pathname;
this.req.cookies = this.req.headers.cookie ?
cookie.parse(this.req.headers.cookie) : {};
// promise for body
this.req.getBody = function getBody() {
if (!getBody.promise) {
getBody.promise = new Promise(function getBodyP(resolve, reject) {
var body = '';
this.req.on('end', function onEnd() {resolve(body);});
this.req.on('data', function onData(data) {
if (body.length + data.length >= this.POST_DATA_MAX_BYTES) {
this.req.connection.destroy();
reject(new CError('DATA_LENGTH_EXCEEDED', 'post body limit exceeded'));
} else {
body += data;
}
}.bind(this));
}.bind(this));
}
return getBody.promise;
}.bind(this);
}
Responder.prototype = Object.create(EventEmitter.prototype);
Responder.prototype.POST_DATA_MAX_BYTES = 64 * 1024;
Responder.prototype.run = function run() {
var Generator = this.methods[this.req.method];
if (!Generator) {
debug('no method found');
var methods = Object.keys(this.methods);
this.res.writeHead('405', {
Allow: methods.join(', ')
});
return this.res.end('405');
}
this.methodGenerator = Generator;
this.respond();
};
/**
* Respond to the request by invoking the appropriate responder then sending
* the response.
*/
Responder.prototype.respond = function respond() {
ContGen(this.methodGenerator, this)
.done(this.sendResponse.bind(this), function onError(err) {
if (!this.emit('unhandledMethodError', this, err))
throw err;
}.bind(this));
};
Responder.prototype.methods = {};
Responder.prototype.setHeader = function setHeader(name, value, overwrite) {
if (!overwrite && this.headersToSet_[name])
throw new CError('HEADER_OVERWRITE', 'header already set');
this.headersToSet_[name] = value;
};
Responder.prototype.setCookie = function setCookie(name, value, opts,
overwrite) {
if (!overwrite && this.cookiesToSet_[name])
throw new CError('COOKIE_OVERWRITE', 'cookie already set');
opts = _.defaults({}, opts, {path: '/', secure: true, httpOnly: true});
this.cookiesToSet_[name] = {
value: value,
opts: opts,
};
};
Responder.prototype.setStatusCode = function setStatusCode(code, overwrite) {
if (!overwrite && this.statusCode_)
throw new CError('STATUS_OVERWRITE', 'Status code already set');
this.statusCode_ = code;
};
Responder.prototype.redirect = function redirect(code, url) {
// 301:perm
// 307:temp
// 303:modify
this.setResponseCode(code);
this.setHeader('Location', url);
}
Responder.prototype.sendResponse = function sendResponse(body) {
debug('send response');
// set headers
var keys = Object.keys(this.headersToSet_);
for (var i = 0; i < keys.length; ++i) {
var name = keys[i];
this.res.setHeader(name, this.headersToSet_[name]);
}
// set cookies
var keys = Object.keys(this.cookiesToSet_);
var cookieStrs = keys.map(function(key) {
var c = this.cookiesToSet_[key];
return cookie.serialize(key, c.value, c.opts);
}.bind(this));
if (cookieStrs.length > 0) {
this.res.setHeader('Set-Cookie', cookieStrs);
}
// set response code
this.res.writeHead(this.statusCode_ || 200);
// send body
this.res.end(body);
};