You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.6 KiB

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