response.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. 4: "server error;cached" // tried to check but ran into server error, using cached version
  12. };
  13. // print these, but without stacktrace
  14. var silent_errors = ["ETIMEDOUT", "ESOCKETTIMEDOUT", "ECONNRESET", "EHOSTUNREACH", "ECONNREFUSED", "HTTPERROR", "RATELIMIT"];
  15. // handles HTTP responses
  16. // +request+ a http.IncomingMessage
  17. // +response+ a http.ServerResponse
  18. // +result+ an object with:
  19. // * status: see human_status, required for images without err
  20. // * redirect: redirect URL
  21. // * body: file or message, required unless redirect is present or status is < 0
  22. // * type: a valid Content-Type for the body, defaults to "text/plain"
  23. // * hash: image hash, required when body is an image
  24. // * err: a possible Error
  25. // * code: override HTTP response code when status is < 0
  26. module.exports = function(request, response, result) {
  27. // These headers are the same for every response
  28. var headers = {
  29. "Content-Type": result.body && result.type || "text/plain",
  30. "Cache-Control": "max-age=" + config.caching.browser,
  31. "Response-Time": Date.now() - request.start,
  32. "X-Request-ID": request.id,
  33. "Access-Control-Allow-Origin": "*",
  34. };
  35. response.on("finish", function() {
  36. logging.log(request.id, request.method, request.url.href, response.statusCode, headers["Response-Time"] + "ms", "(" + (human_status[result.status] || "-") + ")");
  37. });
  38. response.on("error", function(err) {
  39. logging.error(request.id, err);
  40. });
  41. if (result.err) {
  42. var silent = silent_errors.indexOf(result.err.code) !== -1;
  43. if (result.err.stack && !silent) {
  44. logging.error(request.id, result.err.stack);
  45. } else if (silent) {
  46. logging.warn(request.id, result.err);
  47. } else {
  48. logging.error(request.id, result.err);
  49. }
  50. result.status = -1;
  51. }
  52. if (result.status !== undefined && result.status !== null) {
  53. headers["X-Storage-Type"] = human_status[result.status];
  54. }
  55. // use crc32 as a hash function for Etag
  56. var etag = "\"" + crc(result.body || "") + "\"";
  57. // handle etag caching
  58. var incoming_etag = request.headers["if-none-match"];
  59. // also respond with 304 on server error (use client's version)
  60. // don't respond with 304 when debugging is enabled
  61. if (incoming_etag && (incoming_etag === etag || result.status === -1 && !config.server.debug_enabled)) {
  62. response.writeHead(304, headers);
  63. response.end();
  64. return;
  65. }
  66. if (result.redirect) {
  67. headers.Location = result.redirect;
  68. response.writeHead(307, headers);
  69. response.end();
  70. return;
  71. }
  72. if (result.status === -2) {
  73. response.writeHead(result.code || 422, headers);
  74. } else if (result.status === -1) {
  75. // server errors shouldn't be cached
  76. headers["Cache-Control"] = "no-cache, max-age=0";
  77. if (result.body && result.hash && !result.hash.startsWith("mhf_")) {
  78. headers["Warning"] = '110 Crafatar "Response is Stale"'
  79. headers["Etag"] = etag;
  80. result.code = result.code || 200;
  81. }
  82. if (result.err && result.err.code === "ENOENT") {
  83. result.code = result.code || 500;
  84. }
  85. response.writeHead(result.code || 502, headers);
  86. } else {
  87. if (result.body) {
  88. if (result.status === 4) {
  89. headers["Warning"] = '111 Crafatar "Revalidation Failed"'
  90. }
  91. headers["Etag"] = etag;
  92. response.writeHead(200, headers);
  93. } else {
  94. response.writeHead(404, headers);
  95. }
  96. }
  97. response.end(result.body);
  98. };