webjs.js 8.5 KB

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