Blame


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