2
0

response.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. logging.debug("status: " + result.status);
  47. headers["X-Storage-Type"] = human_status[result.status];
  48. }
  49. if (result.body) {
  50. // use Mojang's image hash if available
  51. // use crc32 as a hash function otherwise
  52. var etag = result.body && result.hash && result.hash.substr(0, 10) || crc(result.body);
  53. headers.Etag = "\"" + etag + "\"";
  54. // handle etag caching
  55. var incoming_etag = request.headers["if-none-match"];
  56. if (incoming_etag && incoming_etag === headers.Etag) {
  57. logging.debug(request.id, "Etag matches");
  58. response.writeHead(304, headers);
  59. response.end();
  60. return;
  61. }
  62. }
  63. if (result.redirect) {
  64. headers.Location = result.redirect;
  65. response.writeHead(307, headers);
  66. response.end();
  67. return;
  68. }
  69. if (result.status === -2) {
  70. response.writeHead(422, headers);
  71. response.end(result.body);
  72. } else if (result.status === -1) {
  73. response.writeHead(500, headers);
  74. response.end(result.body);
  75. } else {
  76. response.writeHead(result.body ? 200 : 404, headers);
  77. response.end(result.body);
  78. }
  79. };