fail.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bin.h>
  4. #include <httpd.h>
  5. typedef struct Error Error;
  6. struct Error
  7. {
  8. char *num;
  9. char *concise;
  10. char *verbose;
  11. };
  12. Error errormsg[] =
  13. {
  14. [HInternal] {"500 Internal Error", "Internal Error",
  15. "This server could not process your request due to an internal error."},
  16. [HTempFail] {"500 Internal Error", "Temporary Failure",
  17. "The object %s is currently inaccessible.<p>Please try again later."},
  18. [HUnimp] {"501 Not implemented", "Command not implemented",
  19. "This server does not implement the %s command."},
  20. [HUnkVers] {"501 Not Implemented", "Unknown http version",
  21. "This server does not know how to respond to http version %s."},
  22. [HBadCont] {"501 Not Implemented", "Impossible format",
  23. "This server cannot produce %s in any of the formats your client accepts."},
  24. [HBadReq] {"400 Bad Request", "Strange Request",
  25. "Your client sent a query that this server could not understand."},
  26. [HSyntax] {"400 Bad Request", "Garbled Syntax",
  27. "Your client sent a query with incoherent syntax."},
  28. [HBadSearch] {"400 Bad Request", "Inapplicable Search",
  29. "Your client sent a search that cannot be applied to %s."},
  30. [HNotFound] {"404 Not Found", "Object not found",
  31. "The object %s does not exist on this server."},
  32. [HNoSearch] {"403 Forbidden", "Search not supported",
  33. "The object %s does not support the search command."},
  34. [HNoData] {"403 Forbidden", "No data supplied",
  35. "Search or forms data must be supplied to %s."},
  36. [HExpectFail] {"403 Expectation Failed", "Expectation Failed",
  37. "This server does not support some of your request's expectations."},
  38. [HUnauth] {"403 Forbidden", "Forbidden",
  39. "You are not allowed to see the object %s."},
  40. [HOK] {"200 OK", "everything is fine"},
  41. };
  42. /*
  43. * write a failure message to the net and exit
  44. */
  45. int
  46. hfail(HConnect *c, int reason, ...)
  47. {
  48. Hio *hout;
  49. char makeup[HBufSize];
  50. va_list arg;
  51. int n;
  52. hout = &c->hout;
  53. va_start(arg, reason);
  54. vseprint(makeup, makeup+HBufSize, errormsg[reason].verbose, arg);
  55. va_end(arg);
  56. n = snprint(c->xferbuf, HBufSize, "<head><title>%s</title></head>\n<body><h1>%s</h1>\n%s</body>\n",
  57. errormsg[reason].concise, errormsg[reason].concise, makeup);
  58. hprint(hout, "%s %s\r\n", hversion, errormsg[reason].num);
  59. hprint(hout, "Date: %D\r\n", time(nil));
  60. hprint(hout, "Server: Plan9\r\n");
  61. hprint(hout, "Content-Type: text/html\r\n");
  62. hprint(hout, "Content-Length: %d\r\n", n);
  63. if(c->head.closeit)
  64. hprint(hout, "Connection: close\r\n");
  65. else if(!http11(c))
  66. hprint(hout, "Connection: Keep-Alive\r\n");
  67. hprint(hout, "\r\n");
  68. if(c->req.meth == nil || strcmp(c->req.meth, "HEAD") != 0)
  69. hwrite(hout, c->xferbuf, n);
  70. if(c->replog)
  71. c->replog(c, "Reply: %s\nReason: %s\n", errormsg[reason].num, errormsg[reason].concise);
  72. return hflush(hout);
  73. }