response.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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. // handles HTTP responses
  13. // +request+ a http.IncomingMessage
  14. // +response+ a http.ServerResponse
  15. // +result+ an object with:
  16. // * status: see human_status, required for images without err
  17. // * redirect: redirect URL
  18. // * body: file or message, required unless redirect is present or status is < 0
  19. // * type: a valid Content-Type for the body, defaults to "text/plain"
  20. // * hash: image hash, required when body is an image
  21. // * err: a possible Error
  22. module.exports = function(request, response, result) {
  23. response.on("close", function() {
  24. logging.warn(request.id, "Connection closed");
  25. });
  26. response.on("finish", function() {
  27. logging.log(request.id, response.statusCode, "(" + (human_status[result.status] || "-") + ")");
  28. });
  29. response.on("error", function(err) {
  30. logging.error(request.id, err);
  31. });
  32. // These headers are the same for every response
  33. var headers = {
  34. "Content-Type": (result.body && result.type) || "text/plain",
  35. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  36. "Response-Time": Date.now() - request.start,
  37. "X-Request-ID": request.id,
  38. "Access-Control-Allow-Origin": "*"
  39. };
  40. if (result.err) {
  41. logging.error(result.err);
  42. logging.error(result.err.stack);
  43. result.status = -1;
  44. }
  45. if (result.status !== undefined && result.status !== null) {
  46. headers["X-Storage-Type"] = human_status[result.status];
  47. }
  48. if (result.body) {
  49. // use Mojang's image hash if available
  50. // use crc32 as a hash function otherwise
  51. var etag = result.body && result.hash && result.hash.substr(0, 10) || crc(result.body);
  52. headers.Etag = "\"" + etag + "\"";
  53. // handle etag caching
  54. var incoming_etag = request.headers["if-none-match"];
  55. if (incoming_etag && incoming_etag === headers.Etag) {
  56. response.writeHead(304, headers);
  57. response.end();
  58. return;
  59. }
  60. }
  61. if (result.redirect) {
  62. headers.Location = result.redirect;
  63. response.writeHead(307, headers);
  64. response.end();
  65. return;
  66. }
  67. if (result.status === -2) {
  68. response.writeHead(422, headers);
  69. response.end(result.body);
  70. } else if (result.status === -1) {
  71. response.writeHead(500, headers);
  72. response.end(result.body);
  73. } else {
  74. response.writeHead(result.body ? 200 : 404, headers);
  75. response.end(result.body);
  76. }
  77. };