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