response.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var logging = require("./logging");
  2. var config = require("../config");
  3. var crc = require("crc").crc32;
  4. var human_status = {
  5. "-2": "user error", // e.g. invalid size
  6. "-1": "server error", // e.g. mojang/network issues
  7. 0: "none", // cached as null (user has no skin)
  8. 1: "cached", // found on disk
  9. 2: "downloaded", // profile downloaded, skin downloaded from mojang servers
  10. 3: "checked", // profile re-downloaded (was too old), has no skin or skin cached
  11. };
  12. // print these, but without stacktrace
  13. var silent_errors = ["ETIMEDOUT", "ESOCKETTIMEDOUT", "ECONNRESET", "EHOSTUNREACH", "ECONNREFUSED", "HTTPERROR"];
  14. // handles HTTP responses
  15. // +request+ a http.IncomingMessage
  16. // +response+ a http.ServerResponse
  17. // +result+ an object with:
  18. // * status: see human_status, required for images without err
  19. // * redirect: redirect URL
  20. // * body: file or message, required unless redirect is present or status is < 0
  21. // * type: a valid Content-Type for the body, defaults to "text/plain"
  22. // * hash: image hash, required when body is an image
  23. // * err: a possible Error
  24. // * code: override HTTP response code when status is < 0
  25. module.exports = function(request, response, result) {
  26. // These headers are the same for every response
  27. var headers = {
  28. "Content-Type": result.body && result.type || "text/plain",
  29. "Cache-Control": "max-age=" + config.caching.browser,
  30. "Response-Time": Date.now() - request.start,
  31. "X-Request-ID": request.id,
  32. "Access-Control-Allow-Origin": "*",
  33. };
  34. response.on("close", function() {
  35. logging.warn(request.id, "Connection closed");
  36. });
  37. response.on("finish", function() {
  38. logging.log(request.id, request.method, request.url.href, response.statusCode, headers["Response-Time"] + "ms", "(" + (human_status[result.status] || "-") + ")");
  39. });
  40. response.on("error", function(err) {
  41. logging.error(request.id, err);
  42. });
  43. if (result.err) {
  44. var silent = silent_errors.indexOf(result.err.code) !== -1;
  45. if (result.err.stack && !silent) {
  46. logging.error(request.id, result.err.stack);
  47. } else if (silent) {
  48. logging.warn(request.id, result.err);
  49. } else {
  50. logging.error(request.id, result.err);
  51. }
  52. result.status = -1;
  53. }
  54. if (result.status !== undefined && result.status !== null) {
  55. headers["X-Storage-Type"] = human_status[result.status];
  56. }
  57. // use crc32 as a hash function for Etag
  58. var etag = "\"" + crc(result.body || "") + "\"";
  59. // handle etag caching
  60. var incoming_etag = request.headers["if-none-match"];
  61. // also respond with 304 on server error (use client's version)
  62. // don't respond with 304 when debugging is enabled
  63. if (incoming_etag && (incoming_etag === etag || result.status === -1 && !config.server.debug_enabled)) {
  64. response.writeHead(304, headers);
  65. response.end();
  66. return;
  67. }
  68. if (result.redirect) {
  69. headers.Location = result.redirect;
  70. response.writeHead(307, headers);
  71. response.end();
  72. return;
  73. }
  74. if (result.status === -2) {
  75. response.writeHead(result.code || 422, headers);
  76. } else if (result.status === -1) {
  77. // 500 responses shouldn't be cached
  78. headers["Cache-Control"] = "private, max-age=0, no-cache";
  79. response.writeHead(result.code || 500, headers);
  80. } else {
  81. if (result.body) {
  82. headers.Etag = etag;
  83. response.writeHead(result.status === 2 ? 201 : 200, headers);
  84. } else {
  85. response.writeHead(404, headers);
  86. }
  87. }
  88. response.end(result.body);
  89. };