Simple image host.
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.

context.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var formidable = require("formidable");
  2. var crypto = require("crypto");
  3. var zlib = require("zlib");
  4. var fs = require("fs");
  5. var preprocess = require("./preprocess.js");
  6. var sessions = {};
  7. var sessionTimeouts = {};
  8. if (fs.existsSync(".sessions")) {
  9. sessions = JSON.parse(fs.readFileSync(".sessions", "utf8"));
  10. //Set appropriate timeouts.
  11. Object.keys(sessions).forEach(function(i) {
  12. sessionTimeouts[i] = setTimeout(function() {
  13. delete sessions[i];
  14. }, 1000*60*10);
  15. });
  16. }
  17. function saveSessions() {
  18. fs.writeFileSync(".sessions", JSON.stringify(sessions));
  19. process.exit();
  20. }
  21. process.on("SIGINT", saveSessions);
  22. process.on("SIGTERM", saveSessions);
  23. function templatify(str, args, ctx, env) {
  24. env.url = ctx.req.url;
  25. var env = {
  26. session: function(key) {
  27. ctx.cachable = false;
  28. return ctx.session[key];
  29. },
  30. arg: function(key) {
  31. return ctx.htmlEntities(args[key]);
  32. },
  33. noescape: args,
  34. env: env,
  35. template: function(key) {
  36. return ctx.template(key, args);
  37. }
  38. }
  39. str = preprocess(str, env);
  40. return str;
  41. }
  42. var cache = {};
  43. module.exports = function(options) {
  44. this.req = options.req;
  45. this.res = options.res;
  46. this.templates = options.templates;
  47. this.views = options.views;
  48. this.db = options.db;
  49. this.conf = options.conf;
  50. this.query = this.req.url.split("?")[1] || "";
  51. this.shouldGzip = /gzip/.test(this.req.headers["accept-encoding"]);
  52. this.cachable = true;
  53. this.statusCode = 200;
  54. this.headers = {};
  55. if (this.conf.debug)
  56. this.startTime = new Date();
  57. //Handle cookies
  58. this.cookies = {};
  59. this.req.headers.cookie = this.req.headers.cookie || "";
  60. this.req.headers.cookie.split(/;\s*/).forEach(function(elem) {
  61. var pair = elem.split("=");
  62. this.cookies[pair[0]] = decodeURIComponent(pair[1]);
  63. }.bind(this));
  64. //Handle sessions
  65. var key;
  66. if (sessions[this.cookies.session]) {
  67. key = this.cookies.session;
  68. } else {
  69. do {
  70. key = crypto.randomBytes(64).toString("hex");
  71. } while (sessions[key]);
  72. sessions[key] = {};
  73. this.res.setHeader("Set-Cookie", "session="+key);
  74. }
  75. this.session = sessions[key];
  76. //Reset session delete timer
  77. if (sessionTimeouts[key])
  78. clearTimeout(sessionTimeouts[key]);
  79. sessionTimeouts[key] = setTimeout(function() {
  80. delete sessions[key];
  81. }, this.conf.session_timeout);
  82. }
  83. module.exports.prototype = {
  84. _end: function(str) {
  85. if (this.conf.debug) {
  86. var ms = (new Date().getTime() - this.startTime.getTime());
  87. console.log(
  88. ms+" millisecond(s)\t"+
  89. (this.statusCode || 200)+"\t"+
  90. this.req.url
  91. );
  92. } else {
  93. console.log(this.req.url);
  94. }
  95. this.res.writeHead(this.statusCode, this.headers);
  96. this.res.end(str);
  97. },
  98. end: function(str, alreadyGzipped) {
  99. if (!this.shouldGzip)
  100. return this._end(str);
  101. this.setHeader("Content-Encoding", "gzip");
  102. if (alreadyGzipped)
  103. return this._end(str);
  104. zlib.gzip(str, function(err, res) {
  105. if (err)
  106. throw err;
  107. this._end(res);
  108. }.bind(this));
  109. },
  110. succeed: function(obj) {
  111. obj = obj || {};
  112. obj.success = true;
  113. this.setHeader("Content-Type", "application/json");
  114. this.end(JSON.stringify(obj));
  115. },
  116. fail: function(err) {
  117. console.log("Sending error to client:");
  118. console.trace(err);
  119. this.setHeader("Content-Type", "application/json");
  120. obj = {};
  121. obj.success = false;
  122. obj.error = err.toString();
  123. this.end(JSON.stringify(obj));
  124. },
  125. err404: function() {
  126. this.setStatus(404);
  127. this.end(this.view("404"));
  128. },
  129. template: function(name, args) {
  130. var str = this.templates[name];
  131. if (!str)
  132. throw new Error("No such template: "+name);
  133. return templatify(str, args, this, {template: name});
  134. },
  135. view: function(name, args) {
  136. var str = this.views[name];
  137. if (!str)
  138. throw new Error("No such view: "+name);
  139. return templatify(str, args, this, {view: name});
  140. },
  141. getPostData: function(cb) {
  142. if (this.postData)
  143. return cb(null, this.postData.data, this.postData.files);
  144. if (this.req.method.toUpperCase() != "POST")
  145. return cb(new Error("Expected POST request, got "+this.req.method));
  146. var form = new formidable.IncomingForm();
  147. form.parse(this.req, function(err, data, files) {
  148. if (err) return cb(err);
  149. this.postData = {
  150. data: data,
  151. files: files
  152. }
  153. cb(null, data, files);
  154. }.bind(this));
  155. },
  156. setStatus: function(code) {
  157. this.statusCode = code;
  158. },
  159. setHeader: function(key, val) {
  160. this.headers[key] = val;
  161. },
  162. login: function(username, id) {
  163. this.session.loggedIn = true;
  164. this.session.username = username;
  165. this.session.userId = id;
  166. },
  167. logout: function() {
  168. this.session.loggedIn = false;
  169. delete this.session.username;
  170. delete this.session.userId;
  171. },
  172. async: function(n, cb) {
  173. if (typeof n !== "number")
  174. throw new Error("Expected number, got "+typeof n);
  175. if (n < 1)
  176. return cb();
  177. var res = {};
  178. var errs = {};
  179. var errnum = 0;
  180. return function(key, val, err) {
  181. if (key)
  182. res[key] = val;
  183. if (err)
  184. errs[key] = err;
  185. if (n === 1)
  186. cb((errnum ? errs : null), res);
  187. else
  188. n -= 1;
  189. }
  190. },
  191. htmlEntities: function(arg) {
  192. if (typeof arg === "string") {
  193. return arg.replace(/&/g, "&amp;")
  194. .replace(/</g, "&lt;")
  195. .replace(/>/g, "&gt;")
  196. .replace(/"/g, "&quot;");
  197. } else {
  198. return arg;
  199. }
  200. }
  201. }