fail.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bin.h>
  12. #include <httpd.h>
  13. typedef struct Error Error;
  14. struct Error
  15. {
  16. char *num;
  17. char *concise;
  18. char *verbose;
  19. };
  20. Error errormsg[] =
  21. {
  22. [HInternal] = {"500 Internal Error", "Internal Error",
  23. "This server could not process your request due to an internal error."},
  24. [HTempFail] = {"500 Internal Error", "Temporary Failure",
  25. "The object %s is currently inaccessible.<p>Please try again later."},
  26. [HUnimp] = {"501 Not implemented", "Command not implemented",
  27. "This server does not implement the %s command."},
  28. [HUnkVers] = {"501 Not Implemented", "Unknown http version",
  29. "This server does not know how to respond to http version %s."},
  30. [HBadCont] = {"501 Not Implemented", "Impossible format",
  31. "This server cannot produce %s in any of the formats your client accepts."},
  32. [HBadReq] = {"400 Bad Request", "Strange Request",
  33. "Your client sent a query that this server could not understand."},
  34. [HSyntax] = {"400 Bad Request", "Garbled Syntax",
  35. "Your client sent a query with incoherent syntax."},
  36. [HBadSearch] = {"400 Bad Request", "Inapplicable Search",
  37. "Your client sent a search that cannot be applied to %s."},
  38. [HNotFound] = {"404 Not Found", "Object not found",
  39. "The object %s does not exist on this server."},
  40. [HNoSearch] = {"403 Forbidden", "Search not supported",
  41. "The object %s does not support the search command."},
  42. [HNoData] = {"403 Forbidden", "No data supplied",
  43. "Search or forms data must be supplied to %s."},
  44. [HExpectFail] = {"403 Expectation Failed", "Expectation Failed",
  45. "This server does not support some of your request's expectations."},
  46. [HUnauth] = {"403 Forbidden", "Forbidden",
  47. "You are not allowed to see the object %s."},
  48. [HOK] = {"200 OK", "everything is fine"},
  49. };
  50. /*
  51. * write a failure message to the net and exit
  52. */
  53. int
  54. hfail(HConnect *c, int reason, ...)
  55. {
  56. Hio *hout;
  57. char makeup[HBufSize], err[ERRMAX];
  58. va_list arg;
  59. int n;
  60. rerrstr(err, sizeof err);
  61. hout = &c->hout;
  62. va_start(arg, reason);
  63. vseprint(makeup, makeup+HBufSize, errormsg[reason].verbose, arg);
  64. va_end(arg);
  65. /*
  66. * this additional information has proved useful when debugging
  67. * complex http configuration problems.
  68. */
  69. n = snprint(c->xferbuf, HBufSize, "<head><title>%s</title></head>\n"
  70. "<body><h1>%s</h1>\n%s<p>\n"
  71. "errstr: %s<br>\n"
  72. "uri host: %s<br>\n"
  73. "header host: %s<br>\nactual host: %s\n</body>\n",
  74. errormsg[reason].concise, errormsg[reason].concise, makeup,
  75. err,
  76. (c->req.urihost? c->req.urihost: ""),
  77. c->head.host, hmydomain);
  78. hprint(hout, "%s %s\r\n", hversion, errormsg[reason].num);
  79. hprint(hout, "Date: %D\r\n", time(nil));
  80. hprint(hout, "Server: Plan9\r\n");
  81. hprint(hout, "Content-Type: text/html\r\n");
  82. hprint(hout, "Content-Length: %d\r\n", n);
  83. if(c->head.closeit)
  84. hprint(hout, "Connection: close\r\n");
  85. else if(!http11(c))
  86. hprint(hout, "Connection: Keep-Alive\r\n");
  87. hprint(hout, "\r\n");
  88. if(c->req.meth == nil || strcmp(c->req.meth, "HEAD") != 0)
  89. hwrite(hout, c->xferbuf, n);
  90. if(c->replog)
  91. c->replog(c, "Reply: %s\nReason: %s\n", errormsg[reason].num, errormsg[reason].concise);
  92. return hflush(hout);
  93. }