response.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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", "RATELIMIT"];
  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("finish", function() {
  35. logging.log(request.id, request.method, request.url.href, response.statusCode, headers["Response-Time"] + "ms", "(" + (human_status[result.status] || "-") + ")");
  36. });
  37. response.on("error", function(err) {
  38. logging.error(request.id, err);
  39. });
  40. if (result.err) {
  41. var silent = silent_errors.indexOf(result.err.code) !== -1;
  42. if (result.err.stack && !silent) {
  43. logging.error(request.id, result.err.stack);
  44. } else if (silent) {
  45. logging.warn(request.id, result.err);
  46. } else {
  47. logging.error(request.id, result.err);
  48. }
  49. result.status = -1;
  50. }
  51. if (result.status !== undefined && result.status !== null) {
  52. headers["X-Storage-Type"] = human_status[result.status];
  53. }
  54. // use crc32 as a hash function for Etag
  55. var etag = "\"" + crc(result.body || "") + "\"";
  56. // handle etag caching
  57. var incoming_etag = request.headers["if-none-match"];
  58. // also respond with 304 on server error (use client's version)
  59. // don't respond with 304 when debugging is enabled
  60. if (incoming_etag && (incoming_etag === etag || result.status === -1 && !config.server.debug_enabled)) {
  61. response.writeHead(304, headers);
  62. response.end();
  63. return;
  64. }
  65. if (result.redirect) {
  66. headers.Location = result.redirect;
  67. response.writeHead(307, headers);
  68. response.end();
  69. return;
  70. }
  71. if (result.status === -2) {
  72. response.writeHead(result.code || 422, headers);
  73. } else if (result.status === -1) {
  74. // 500 responses shouldn't be cached
  75. headers["Cache-Control"] = "private, max-age=0, no-cache";
  76. response.writeHead(result.code || 500, headers);
  77. } else {
  78. if (result.body) {
  79. headers.Etag = etag;
  80. response.writeHead(result.status === 2 ? 201 : 200, headers);
  81. } else {
  82. response.writeHead(404, headers);
  83. }
  84. }
  85. response.end(result.body);
  86. };