1
|
// Generated by CoffeeScript 1.12.7
|
2
|
(function() {
|
3
|
var GenericApp, execute_request, fake_response, fs, http, querystring, url, utils;
|
4
|
|
5
|
url = require('url');
|
6
|
|
7
|
querystring = require('querystring');
|
8
|
|
9
|
fs = require('fs');
|
10
|
|
11
|
http = require('http');
|
12
|
|
13
|
utils = require('./utils');
|
14
|
|
15
|
execute_request = function(app, funs, req, res, data) {
|
16
|
var fun, results, x;
|
17
|
try {
|
18
|
results = [];
|
19
|
while (funs.length > 0) {
|
20
|
fun = funs.shift();
|
21
|
req.last_fun = fun;
|
22
|
results.push(data = app[fun](req, res, data, req.next_filter));
|
23
|
}
|
24
|
return results;
|
25
|
} catch (error1) {
|
26
|
x = error1;
|
27
|
if (typeof x === 'object' && 'status' in x) {
|
28
|
if (x.status === 0) {
|
29
|
return;
|
30
|
} else if ('handle_' + x.status in app) {
|
31
|
app['handle_' + x.status](req, res, x);
|
32
|
} else {
|
33
|
app['handle_error'](req, res, x);
|
34
|
}
|
35
|
} else {
|
36
|
app['handle_error'](req, res, x);
|
37
|
}
|
38
|
return app['log_request'](req, res, true);
|
39
|
}
|
40
|
};
|
41
|
|
42
|
fake_response = function(req, res) {
|
43
|
var headers;
|
44
|
headers = {
|
45
|
'Connection': 'close'
|
46
|
};
|
47
|
res.writeHead = function(status, user_headers) {
|
48
|
var k, r, x;
|
49
|
if (user_headers == null) {
|
50
|
user_headers = {};
|
51
|
}
|
52
|
r = [];
|
53
|
r.push('HTTP/' + req.httpVersion + ' ' + status + ' ' + http.STATUS_CODES[status]);
|
54
|
utils.objectExtend(headers, user_headers);
|
55
|
for (k in headers) {
|
56
|
r.push(k + ': ' + headers[k]);
|
57
|
}
|
58
|
r = r.concat(['', '']);
|
59
|
try {
|
60
|
res.write(r.join('\r\n'));
|
61
|
} catch (error1) {
|
62
|
x = error1;
|
63
|
}
|
64
|
try {
|
65
|
return res.end();
|
66
|
} catch (error1) {
|
67
|
x = error1;
|
68
|
}
|
69
|
};
|
70
|
return res.setHeader = function(k, v) {
|
71
|
return headers[k] = v;
|
72
|
};
|
73
|
};
|
74
|
|
75
|
exports.generateHandler = function(app, dispatcher) {
|
76
|
return function(req, res, head) {
|
77
|
var allowed_methods, found, funs, i, j, l, len, m, method, path, ref, row;
|
78
|
if (typeof res.writeHead === "undefined") {
|
79
|
fake_response(req, res);
|
80
|
}
|
81
|
utils.objectExtend(req, url.parse(req.url, true));
|
82
|
req.start_date = new Date();
|
83
|
found = false;
|
84
|
allowed_methods = [];
|
85
|
for (j = 0, len = dispatcher.length; j < len; j++) {
|
86
|
row = dispatcher[j];
|
87
|
method = row[0], path = row[1], funs = row[2];
|
88
|
if (path.constructor !== Array) {
|
89
|
path = [path];
|
90
|
}
|
91
|
m = req.pathname.match(path[0]);
|
92
|
if (!m) {
|
93
|
continue;
|
94
|
}
|
95
|
if (!req.method.match(new RegExp(method))) {
|
96
|
allowed_methods.push(method);
|
97
|
continue;
|
98
|
}
|
99
|
for (i = l = 1, ref = path.length; 1 <= ref ? l < ref : l > ref; i = 1 <= ref ? ++l : --l) {
|
100
|
req[path[i]] = m[i];
|
101
|
}
|
102
|
funs = funs.slice(0);
|
103
|
funs.push('log_request');
|
104
|
req.next_filter = function(data) {
|
105
|
return execute_request(app, funs, req, res, data);
|
106
|
};
|
107
|
req.next_filter(head);
|
108
|
found = true;
|
109
|
break;
|
110
|
}
|
111
|
if (!found) {
|
112
|
if (allowed_methods.length !== 0) {
|
113
|
app['handle_405'](req, res, allowed_methods);
|
114
|
} else {
|
115
|
app['handle_404'](req, res);
|
116
|
}
|
117
|
app['log_request'](req, res, true);
|
118
|
}
|
119
|
};
|
120
|
};
|
121
|
|
122
|
exports.GenericApp = GenericApp = (function() {
|
123
|
function GenericApp() {}
|
124
|
|
125
|
GenericApp.prototype.handle_404 = function(req, res, x) {
|
126
|
if (res.finished) {
|
127
|
return x;
|
128
|
}
|
129
|
res.writeHead(404, {});
|
130
|
res.end();
|
131
|
return true;
|
132
|
};
|
133
|
|
134
|
GenericApp.prototype.handle_405 = function(req, res, methods) {
|
135
|
res.writeHead(405, {
|
136
|
'Allow': methods.join(', ')
|
137
|
});
|
138
|
res.end();
|
139
|
return true;
|
140
|
};
|
141
|
|
142
|
GenericApp.prototype.handle_error = function(req, res, x) {
|
143
|
if (res.finished) {
|
144
|
return x;
|
145
|
}
|
146
|
if (typeof x === 'object' && 'status' in x) {
|
147
|
res.writeHead(x.status, {});
|
148
|
res.end(x.message || "");
|
149
|
} else {
|
150
|
try {
|
151
|
res.writeHead(500, {});
|
152
|
res.end("500 - Internal Server Error");
|
153
|
} catch (error1) {
|
154
|
x = error1;
|
155
|
}
|
156
|
this.log('error', 'Exception on "' + req.method + ' ' + req.href + '" in filter "' + req.last_fun + '":\n' + (x.stack || x));
|
157
|
}
|
158
|
return true;
|
159
|
};
|
160
|
|
161
|
GenericApp.prototype.log_request = function(req, res, data) {
|
162
|
var td;
|
163
|
td = (new Date()) - req.start_date;
|
164
|
this.log('info', req.method + ' ' + req.url + ' ' + td + 'ms ' + (res.finished ? res.statusCode : '(unfinished)'));
|
165
|
return data;
|
166
|
};
|
167
|
|
168
|
GenericApp.prototype.log = function(severity, line) {
|
169
|
return console.log(line);
|
170
|
};
|
171
|
|
172
|
GenericApp.prototype.expose_html = function(req, res, content) {
|
173
|
if (res.finished) {
|
174
|
return content;
|
175
|
}
|
176
|
if (!res.getHeader('Content-Type')) {
|
177
|
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
|
178
|
}
|
179
|
return this.expose(req, res, content);
|
180
|
};
|
181
|
|
182
|
GenericApp.prototype.expose_json = function(req, res, content) {
|
183
|
if (res.finished) {
|
184
|
return content;
|
185
|
}
|
186
|
if (!res.getHeader('Content-Type')) {
|
187
|
res.setHeader('Content-Type', 'application/json');
|
188
|
}
|
189
|
return this.expose(req, res, JSON.stringify(content));
|
190
|
};
|
191
|
|
192
|
GenericApp.prototype.expose = function(req, res, content) {
|
193
|
if (res.finished) {
|
194
|
return content;
|
195
|
}
|
196
|
if (content && !res.getHeader('Content-Type')) {
|
197
|
res.setHeader('Content-Type', 'text/plain');
|
198
|
}
|
199
|
if (content) {
|
200
|
res.setHeader('Content-Length', content.length);
|
201
|
}
|
202
|
res.writeHead(res.statusCode);
|
203
|
res.end(content, 'utf8');
|
204
|
return true;
|
205
|
};
|
206
|
|
207
|
GenericApp.prototype.serve_file = function(req, res, filename, next_filter) {
|
208
|
var a;
|
209
|
a = function(error, content) {
|
210
|
if (error) {
|
211
|
res.writeHead(500);
|
212
|
res.end("can't read file");
|
213
|
} else {
|
214
|
res.setHeader('Content-length', content.length);
|
215
|
res.writeHead(res.statusCode, res.headers);
|
216
|
res.end(content, 'utf8');
|
217
|
}
|
218
|
return next_filter(true);
|
219
|
};
|
220
|
fs.readFile(filename, a);
|
221
|
throw {
|
222
|
status: 0
|
223
|
};
|
224
|
};
|
225
|
|
226
|
GenericApp.prototype.cache_for = function(req, res, content) {
|
227
|
var exp;
|
228
|
res.cache_for = res.cache_for || 365 * 24 * 60 * 60;
|
229
|
res.setHeader('Cache-Control', 'public, max-age=' + res.cache_for);
|
230
|
exp = new Date();
|
231
|
exp.setTime(exp.getTime() + res.cache_for * 1000);
|
232
|
res.setHeader('Expires', exp.toGMTString());
|
233
|
return content;
|
234
|
};
|
235
|
|
236
|
GenericApp.prototype.h_no_cache = function(req, res, content) {
|
237
|
res.setHeader('Cache-Control', 'no-store, no-cache, no-transform, must-revalidate, max-age=0');
|
238
|
return content;
|
239
|
};
|
240
|
|
241
|
GenericApp.prototype.expect_form = function(req, res, _data, next_filter) {
|
242
|
var data;
|
243
|
data = new Buffer(0);
|
244
|
req.on('data', (function(_this) {
|
245
|
return function(d) {
|
246
|
return data = utils.buffer_concat(data, new Buffer(d, 'binary'));
|
247
|
};
|
248
|
})(this));
|
249
|
req.on('end', (function(_this) {
|
250
|
return function() {
|
251
|
var q;
|
252
|
data = data.toString('utf-8');
|
253
|
switch ((req.headers['content-type'] || '').split(';')[0]) {
|
254
|
case 'application/x-www-form-urlencoded':
|
255
|
q = querystring.parse(data);
|
256
|
break;
|
257
|
case 'text/plain':
|
258
|
case '':
|
259
|
q = data;
|
260
|
break;
|
261
|
default:
|
262
|
_this.log('error', "Unsupported content-type " + req.headers['content-type']);
|
263
|
q = void 0;
|
264
|
}
|
265
|
return next_filter(q);
|
266
|
};
|
267
|
})(this));
|
268
|
throw {
|
269
|
status: 0
|
270
|
};
|
271
|
};
|
272
|
|
273
|
GenericApp.prototype.expect_xhr = function(req, res, _data, next_filter) {
|
274
|
var data;
|
275
|
data = new Buffer(0);
|
276
|
req.on('data', (function(_this) {
|
277
|
return function(d) {
|
278
|
return data = utils.buffer_concat(data, new Buffer(d, 'binary'));
|
279
|
};
|
280
|
})(this));
|
281
|
req.on('end', (function(_this) {
|
282
|
return function() {
|
283
|
var q;
|
284
|
data = data.toString('utf-8');
|
285
|
switch ((req.headers['content-type'] || '').split(';')[0]) {
|
286
|
case 'text/plain':
|
287
|
case 'T':
|
288
|
case 'application/json':
|
289
|
case 'application/xml':
|
290
|
case '':
|
291
|
case 'text/xml':
|
292
|
q = data;
|
293
|
break;
|
294
|
default:
|
295
|
_this.log('error', 'Unsupported content-type ' + req.headers['content-type']);
|
296
|
q = void 0;
|
297
|
}
|
298
|
return next_filter(q);
|
299
|
};
|
300
|
})(this));
|
301
|
throw {
|
302
|
status: 0
|
303
|
};
|
304
|
};
|
305
|
|
306
|
return GenericApp;
|
307
|
|
308
|
})();
|
309
|
|
310
|
}).call(this);
|