-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresponse.c
362 lines (331 loc) · 11.3 KB
/
response.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Boa, an http server
* Copyright (C) 1995 Paul Phillips <[email protected]>
* Some changes Copyright (C) 1996 Larry Doolittle <[email protected]>
* Some changes Copyright (C) 1996-99 Jon Nelson <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* $Id: response.c,v 1.41.2.1 2002/06/06 05:08:54 jnelson Exp $*/
#include "boa.h"
#define HTML "text/html; charset=ISO-8859-1"
#define CRLF "\r\n"
void print_content_type(request * req)
{
req_write(req, "Content-Type: ");
req_write(req, get_mime_type(req->request_uri));
req_write(req, "\r\n");
}
void print_content_length(request * req)
{
req_write(req, "Content-Length: ");
req_write(req, simple_itoa(req->filesize));
req_write(req, "\r\n");
}
void print_last_modified(request * req)
{
static char lm[] = "Last-Modified: "
" " "\r\n";
rfc822_time_buf(lm + 15, req->last_modified);
req_write(req, lm);
}
void print_ka_phrase(request * req)
{
if (req->kacount > 0 &&
req->keepalive == KA_ACTIVE && req->response_status < 500) {
req_write(req, "Connection: Keep-Alive\r\nKeep-Alive: timeout=");
req_write(req, simple_itoa(ka_timeout));
req_write(req, ", max=");
req_write(req, simple_itoa(req->kacount));
req_write(req, "\r\n");
} else
req_write(req, "Connection: close\r\n");
}
void print_http_headers(request * req)
{
static char stuff[] = "Date: "
" "
"\r\nServer: " SERVER_VERSION "\r\n";
rfc822_time_buf(stuff + 6, 0);
req_write(req, stuff);
print_ka_phrase(req);
}
/* The routines above are only called by the routines below.
* The rest of Boa only enters through the routines below.
*/
/* R_REQUEST_OK: 200 */
void send_r_request_ok(request * req)
{
req->response_status = R_REQUEST_OK;
if (req->simple)
return;
req_write(req, "HTTP/1.0 200 OK\r\n");
print_http_headers(req);
if (!req->is_cgi) {
print_content_length(req);
print_last_modified(req);
print_content_type(req);
req_write(req, "\r\n");
}
}
/* R_MOVED_PERM: 301 */
void send_r_moved_perm(request * req, char *url)
{
SQUASH_KA(req);
req->response_status = R_MOVED_PERM;
if (!req->simple) {
req_write(req, "HTTP/1.0 301 Moved Permanently\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n");
req_write(req, "Location: ");
req_write_escape_http(req, url);
req_write(req, "\r\n\r\n");
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>301 Moved Permanently</TITLE></HEAD>\n"
"<BODY>\n<H1>301 Moved</H1>The document has moved\n"
"<A HREF=\"");
req_write_escape_html(req, url);
req_write(req, "\">here</A>.\n</BODY></HTML>\n");
}
req_flush(req);
}
/* R_MOVED_TEMP: 302 */
void send_r_moved_temp(request * req, char *url, char *more_hdr)
{
SQUASH_KA(req);
req->response_status = R_MOVED_TEMP;
if (!req->simple) {
req_write(req, "HTTP/1.0 302 Moved Temporarily\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n");
req_write(req, "Location: ");
req_write_escape_http(req, url);
req_write(req, "\r\n");
req_write(req, more_hdr);
req_write(req, "\r\n\r\n");
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>302 Moved Temporarily</TITLE></HEAD>\n"
"<BODY>\n<H1>302 Moved</H1>The document has moved\n"
"<A HREF=\"");
req_write_escape_html(req, url);
req_write(req, "\">here</A>.\n</BODY></HTML>\n");
}
req_flush(req);
}
/* R_NOT_MODIFIED: 304 */
void send_r_not_modified(request * req)
{
SQUASH_KA(req);
req->response_status = R_NOT_MODIFIED;
req_write(req, "HTTP/1.0 304 Not Modified\r\n");
print_http_headers(req);
print_content_type(req);
req_write(req, "\r\n");
req_flush(req);
}
/* R_BAD_REQUEST: 400 */
void send_r_bad_request(request * req)
{
SQUASH_KA(req);
req->response_status = R_BAD_REQUEST;
if (!req->simple) {
req_write(req, "HTTP/1.0 400 Bad Request\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD)
req_write(req,
"<HTML><HEAD><TITLE>400 Bad Request</TITLE></HEAD>\n"
"<BODY><H1>400 Bad Request</H1>\nYour client has issued "
"a malformed or illegal request.\n</BODY></HTML>\n");
req_flush(req);
}
/* R_UNAUTHORIZED: 401 */
void send_r_unauthorized(request * req, char *realm_name)
{
SQUASH_KA(req);
req->response_status = R_UNAUTHORIZED;
if (!req->simple) {
req_write(req, "HTTP/1.0 401 Unauthorized\r\n");
print_http_headers(req);
req_write(req, "WWW-Authenticate: Basic realm=\"");
req_write(req, realm_name);
req_write(req, "\"\r\n");
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>\n"
"<BODY><H1>401 Unauthorized</H1>\nYour client does not "
"have permission to get URL ");
req_write_escape_html(req, req->request_uri);
req_write(req, " from this server.\n</BODY></HTML>\n");
}
req_flush(req);
}
/* R_FORBIDDEN: 403 */
void send_r_forbidden(request * req)
{
SQUASH_KA(req);
req->response_status = R_FORBIDDEN;
if (!req->simple) {
req_write(req, "HTTP/1.0 403 Forbidden\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req, "<HTML><HEAD><TITLE>403 Forbidden</TITLE></HEAD>\n"
"<BODY><H1>403 Forbidden</H1>\nYour client does not "
"have permission to get URL ");
req_write_escape_html(req, req->request_uri);
req_write(req, " from this server.\n</BODY></HTML>\n");
}
req_flush(req);
}
/* R_NOT_FOUND: 404 */
void send_r_not_found(request * req)
{
SQUASH_KA(req);
req->response_status = R_NOT_FOUND;
if (!req->simple) {
req_write(req, "HTTP/1.0 404 Not Found\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req, "<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>\n"
"<BODY><H1>404 Not Found</H1>\nThe requested URL ");
req_write_escape_html(req, req->request_uri);
req_write(req, " was not found on this server.\n</BODY></HTML>\n");
}
req_flush(req);
}
/* R_ERROR: 500 */
void send_r_error(request * req)
{
SQUASH_KA(req);
req->response_status = R_ERROR;
if (!req->simple) {
req_write(req, "HTTP/1.0 500 Server Error\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>500 Server Error</TITLE></HEAD>\n"
"<BODY><H1>500 Server Error</H1>\nThe server encountered "
"an internal error and could not complete your request.\n"
"</BODY></HTML>\n");
}
req_flush(req);
}
/* R_NOT_IMP: 501 */
void send_r_not_implemented(request * req)
{
SQUASH_KA(req);
req->response_status = R_NOT_IMP;
if (!req->simple) {
req_write(req, "HTTP/1.0 501 Not Implemented\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>501 Not Implemented</TITLE></HEAD>\n"
"<BODY><H1>501 Not Implemented</H1>\nPOST to non-script "
"is not supported in Boa.\n</BODY></HTML>\n");
}
req_flush(req);
}
/* R_BAD_GATEWAY: 502 */
void send_r_bad_gateway(request * req)
{
SQUASH_KA(req);
req->response_status = R_BAD_GATEWAY;
if (!req->simple) {
req_write(req, "HTTP/1.0 502 Bad Gateway" CRLF);
print_http_headers(req);
req_write(req, "Content-Type: " HTML CRLF CRLF); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>502 Bad Gateway</TITLE></HEAD>\n"
"<BODY><H1>502 Bad Gateway</H1>\nThe CGI was "
"not CGI/1.1 compliant.\n" "</BODY></HTML>\n");
}
req_flush(req);
}
/* R_SERVICE_UNAVAILABLE: 503 */
void send_r_service_unavailable(request * req) /* 503 */
{
static char body[] =
"<HTML><HEAD><TITLE>503 Service Unavailable</TITLE></HEAD>\n"
"<BODY><H1>503 Service Unavailable</H1>\n"
"There are too many connections in use right now.\r\n"
"Please try again later.\r\n</BODY></HTML>\n";
static int _body_len;
static char *body_len;
if (!_body_len)
_body_len = strlen(body);
if (!body_len)
body_len = strdup(simple_itoa(_body_len));
if (!body_len) {
log_error_time();
perror("strdup of _body_len from simple_itoa");
}
SQUASH_KA(req);
req->response_status = R_SERVICE_UNAV;
if (!req->simple) {
req_write(req, "HTTP/1.0 503 Service Unavailable\r\n");
print_http_headers(req);
if (body_len) {
req_write(req, "Content-Length: ");
req_write(req, body_len);
req_write(req, "\r\n");
}
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header
*/
}
if (req->method != M_HEAD) {
req_write(req, body);
}
req_flush(req);
}
/* R_NOT_IMP: 505 */
void send_r_bad_version(request * req)
{
SQUASH_KA(req);
req->response_status = R_BAD_VERSION;
if (!req->simple) {
req_write(req, "HTTP/1.0 505 HTTP Version Not Supported\r\n");
print_http_headers(req);
req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */
}
if (req->method != M_HEAD) {
req_write(req,
"<HTML><HEAD><TITLE>505 HTTP Version Not Supported</TITLE></HEAD>\n"
"<BODY><H1>505 HTTP Version Not Supported</H1>\nHTTP versions "
"other than 0.9 and 1.0 "
"are not supported in Boa.\n<p><p>Version encountered: ");
req_write(req, req->http_version);
req_write(req, "<p><p></BODY></HTML>\n");
}
req_flush(req);
}