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