xref: /plan9/sys/src/libhttpd/fail.c (revision ec59a3ddbfceee0efe34584c2c9981a5e5ff1ec4)
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];
54 	va_list arg;
55 	int n;
56 
57 	hout = &c->hout;
58 	va_start(arg, reason);
59 	vseprint(makeup, makeup+HBufSize, errormsg[reason].verbose, arg);
60 	va_end(arg);
61 	n = snprint(c->xferbuf, HBufSize, "<head><title>%s</title></head>\n<body><h1>%s</h1>\n%s</body>\n",
62 		errormsg[reason].concise, errormsg[reason].concise, makeup);
63 
64 	hprint(hout, "%s %s\r\n", hversion, errormsg[reason].num);
65 	hprint(hout, "Date: %D\r\n", time(nil));
66 	hprint(hout, "Server: Plan9\r\n");
67 	hprint(hout, "Content-Type: text/html\r\n");
68 	hprint(hout, "Content-Length: %d\r\n", n);
69 	if(c->head.closeit)
70 		hprint(hout, "Connection: close\r\n");
71 	else if(!http11(c))
72 		hprint(hout, "Connection: Keep-Alive\r\n");
73 	hprint(hout, "\r\n");
74 
75 	if(c->req.meth == nil || strcmp(c->req.meth, "HEAD") != 0)
76 		hwrite(hout, c->xferbuf, n);
77 
78 	if(c->replog)
79 		c->replog(c, "Reply: %s\nReason: %s\n", errormsg[reason].num, errormsg[reason].concise);
80 	return hflush(hout);
81 }
82