-
Notifications
You must be signed in to change notification settings - Fork 172
/
httpd.c
429 lines (385 loc) · 10.1 KB
/
httpd.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (c) 2005-2007 Jilles Tjoelker et al.
* Rights to this code are as documented in doc/LICENSE.
*
* Generic HTTPd
*
*/
#include "atheme.h"
#include "httpd.h"
#include "datastream.h"
#define REQUEST_MAX 65536 /* maximum size of one call */
DECLARE_MODULE_V1
(
"misc/httpd", false, _modinit, _moddeinit,
PACKAGE_STRING,
"Atheme Development Group <http://www.atheme.org>"
);
connection_t *listener;
mowgli_list_t httpd_path_handlers;
/* conf stuff */
mowgli_list_t conf_httpd_table;
struct httpd_configuration
{
char *host;
char *www_root;
unsigned int port;
} httpd_config;
static void clear_httpddata(struct httpddata *hd)
{
hd->method[0] = '\0';
hd->filename[0] = '\0';
if (hd->requestbuf != NULL)
{
free(hd->requestbuf);
hd->requestbuf = NULL;
}
if (hd->replybuf != NULL)
{
free(hd->replybuf);
hd->replybuf = NULL;
}
hd->length = 0;
hd->lengthdone = 0;
hd->correct_content_type = false;
hd->expect_100_continue = false;
hd->sent_reply = false;
}
static int open_file(const char *filename)
{
char fname[256];
if (strstr(filename, ".."))
return -1;
if (!strcmp(filename, "/"))
filename = "/index.html";
snprintf(fname, sizeof fname, "%s/%s", httpd_config.www_root, filename);
return open(fname, O_RDONLY);
}
static void process_header(connection_t *cptr, char *line)
{
struct httpddata *hd;
char *p;
hd = cptr->userdata;
p = strchr(line, ':');
if (p == NULL)
return;
*p = '\0';
p++;
while (*p == ' ')
p++;
if (!strcasecmp(line, "Connection"))
{
p = strtok(p, ", \t");
while (p != NULL)
{
if (!strcasecmp(p, "close"))
{
slog(LG_DEBUG, "process_header(): Connection: close requested by fd %d", cptr->fd);
hd->connection_close = true;
}
p = strtok(NULL, ", \t");
}
}
else if (!strcasecmp(line, "Content-Length"))
{
hd->length = atoi(p);
}
else if (!strcasecmp(line, "Content-Type"))
{
p = strtok(p, "; \t");
hd->correct_content_type = p != NULL && (!strcasecmp(p, "text/xml") || !strcasecmp(p, "application/json"));
}
else if (!strcasecmp(line, "Expect"))
{
hd->expect_100_continue = !strcasecmp(p, "100-continue");
}
}
static void check_close(connection_t *cptr)
{
struct httpddata *hd;
hd = cptr->userdata;
if (hd->connection_close)
sendq_add_eof(cptr);
}
static void send_error(connection_t *cptr, int errorcode, const char *text, bool sendentity)
{
char buf1[300];
char buf2[700];
if (errorcode < 100 || errorcode > 999)
errorcode = 500;
snprintf(buf2, sizeof buf2, "HTTP/1.1 %d %s\r\n", errorcode, text);
snprintf(buf1, sizeof buf1, "HTTP/1.1 %d %s\r\n"
"Server: Atheme/%s\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %lu\r\n\r\n%s",
errorcode, text, PACKAGE_VERSION, (unsigned long)strlen(buf2),
sendentity ? buf2 : "");
sendq_add(cptr, buf1, strlen(buf1));
}
static const char *content_type(const char *filename)
{
const char *p;
if (!strcmp(filename, "/"))
return "text/html";
p = strrchr(filename, '.');
if (p == NULL)
return "text/plain";
p++;
if (!strcasecmp(p, "html") || !strcasecmp(p, "htm"))
return "text/html";
else if (!strcasecmp(p, "txt"))
return "text/plain";
else if (!strcasecmp(p, "jpg") || !strcasecmp(p, "jpeg"))
return "image/jpeg";
else if (!strcasecmp(p, "gif"))
return "image/gif";
else if (!strcasecmp(p, "png"))
return "image/png";
return "application/octet-stream";
}
static void httpd_recvqhandler(connection_t *cptr)
{
char buf[BUFSIZE * 2];
char outbuf[BUFSIZE * 2];
int count;
struct httpddata *hd;
char *p;
int in;
struct stat sb;
off_t count1;
mowgli_node_t *n;
path_handler_t *ph = NULL;
bool is_get, is_post, handling_done = false;
hd = cptr->userdata;
MOWGLI_ITER_FOREACH(n, httpd_path_handlers.head)
{
ph = (path_handler_t *)n->data;
handling_done = !strcmp(hd->filename, ph->path);
if (handling_done)
break;
}
if (handling_done)
{
if (hd->requestbuf != NULL)
{
count = recvq_get(cptr, hd->requestbuf + hd->lengthdone, hd->length - hd->lengthdone);
if (count <= 0)
return;
hd->lengthdone += count;
if (hd->lengthdone != hd->length)
return;
hd->requestbuf[hd->length] = '\0';
ph->handler(cptr, hd->requestbuf);
clear_httpddata(hd);
return;
}
}
count = recvq_getline(cptr, buf, sizeof buf - 1);
if (count <= 0)
return;
if (cptr->flags & CF_NONEWLINE)
{
slog(LG_INFO, "httpd_recvqhandler(): throwing out fd %d (%s) for excessive line length", cptr->fd, cptr->hbuf);
send_error(cptr, 400, "Bad request", true);
sendq_add_eof(cptr);
return;
}
cnt.bin += count;
if (buf[count - 1] == '\n')
count--;
if (count > 0 && buf[count - 1] == '\r')
count--;
buf[count] = '\0';
if (hd->method[0] == '\0')
{
/* make sure they're not sending more requests after
* declaring they're not sending any more */
if (hd->connection_close)
return;
p = strtok(buf, " ");
if (p == NULL)
return;
mowgli_strlcpy(hd->method, p, sizeof hd->method);
p = strtok(NULL, " ");
if (p == NULL)
return;
mowgli_strlcpy(hd->filename, p, sizeof hd->filename);
p = strtok(NULL, "");
if (p == NULL || !strcmp(p, "HTTP/1.0"))
hd->connection_close = true;
slog(LG_DEBUG, "httpd_recvqhandler(): request %s for %s", hd->method, hd->filename);
}
else if (count == 0)
{
is_get = !strcmp(hd->method, "GET");
is_post = !strcmp(hd->method, "POST");
if (!is_post && !is_get)
{
send_error(cptr, 501, "Method Not Implemented", true);
sendq_add_eof(cptr);
return;
}
hd->method[0] = '\0';
if (!handling_done)
{
in = open_file(hd->filename);
if (in == -1 || fstat(in, &sb) == -1 || !S_ISREG(sb.st_mode))
{
if (in != -1)
close(in);
slog(LG_DEBUG, "httpd_recvqhandler(): 404 for \2%s\2", hd->filename);
send_error(cptr, 404, "Not Found", is_get);
check_close(cptr);
return;
}
slog(LG_INFO, "httpd_recvqhandler(): 200 for %s", hd->filename);
snprintf(outbuf, sizeof outbuf,
"HTTP/1.1 200 OK\r\nServer: Atheme/%s\r\nContent-Type: %s\r\nContent-Length: %lu\r\n\r\n",
PACKAGE_VERSION,
content_type(hd->filename),
(unsigned long)sb.st_size);
sendq_add(cptr, outbuf, strlen(outbuf));
count1 = is_get ? sb.st_size : 0;
while (count1 > 0)
{
count = sizeof outbuf;
if (count > count1)
count = count1;
count = read(in, outbuf, count);
if (count <= 0)
break;
sendq_add(cptr, outbuf, count);
count1 -= count;
}
close(in);
if (count1 > 0)
{
slog(LG_INFO, "httpd_recvqhandler(): disconnecting fd %d (%s), read failed on %s", cptr->fd, cptr->hbuf, hd->filename);
cptr->flags |= CF_DEAD;
}
else
check_close(cptr);
}
else
{
if (hd->length <= 0)
{
send_error(cptr, 411, "Length Required", true);
sendq_add_eof(cptr);
return;
}
if (hd->length > REQUEST_MAX)
{
send_error(cptr, 413, "Request Entity Too Large", true);
sendq_add_eof(cptr);
return;
}
if (!hd->correct_content_type)
{
send_error(cptr, 415, "Unsupported Media Type", true);
sendq_add_eof(cptr);
return;
}
if (hd->expect_100_continue)
{
snprintf(outbuf, sizeof outbuf,
"HTTP/1.1 100 Continue\r\nServer: Atheme/%s\r\n\r\n",
PACKAGE_VERSION);
sendq_add(cptr, outbuf, strlen(outbuf));
}
hd->requestbuf = smalloc(hd->length + 1);
}
}
else
process_header(cptr, buf);
}
static void httpd_closehandler(connection_t *cptr)
{
struct httpddata *hd;
slog(LG_DEBUG, "httpd_closehandler(): fd %d (%s) closed", cptr->fd, cptr->hbuf);
hd = cptr->userdata;
if (hd != NULL)
{
free(hd->requestbuf);
free(hd);
}
cptr->userdata = NULL;
}
static void do_listen(connection_t *cptr)
{
connection_t *newptr;
struct httpddata *hd;
newptr = connection_accept_tcp(cptr, recvq_put, NULL);
slog(LG_DEBUG, "do_listen(): accepted httpd from %s fd %d", newptr->hbuf, newptr->fd);
hd = smalloc(sizeof(*hd));
hd->requestbuf = NULL;
hd->replybuf = NULL;
hd->connection_close = false;
clear_httpddata(hd);
newptr->userdata = hd;
newptr->recvq_handler = httpd_recvqhandler;
newptr->close_handler = httpd_closehandler;
}
static void httpd_checkidle(void *arg)
{
mowgli_node_t *n, *tn;
connection_t *cptr;
(void)arg;
if (listener == NULL)
return;
MOWGLI_ITER_FOREACH_SAFE(n, tn, connection_list.head)
{
cptr = n->data;
if (cptr->listener == listener && cptr->last_recv + 300 < CURRTIME)
{
if (sendq_nonempty(cptr))
cptr->last_recv = CURRTIME;
else
/* from a timeout function,
* connection_close_soon() may take quite
* a while, and connection_close() is safe
* -- jilles */
connection_close(cptr);
}
}
}
static void httpd_config_ready(void *vptr)
{
if (httpd_config.host != NULL && httpd_config.port != 0)
{
/* Some code depends on connection_t.listener == listener. */
if (listener != NULL)
return;
listener = connection_open_listener_tcp(httpd_config.host,
httpd_config.port, do_listen);
if (listener == NULL)
slog(LG_ERROR, "httpd_config_ready(): failed to open listener on host %s port %d", httpd_config.host, httpd_config.port);
}
else
slog(LG_ERROR, "httpd_config_ready(): httpd {} block missing or invalid");
}
static mowgli_eventloop_timer_t *httpd_checkidle_timer = NULL;
void _modinit(module_t *m)
{
httpd_checkidle_timer = mowgli_timer_add(base_eventloop, "httpd_checkidle", httpd_checkidle, NULL, 60);
/* This module needs a rehash to initialize fully if loaded
* at run time */
hook_add_event("config_ready");
hook_add_config_ready(httpd_config_ready);
add_subblock_top_conf("HTTPD", &conf_httpd_table);
add_dupstr_conf_item("HOST", &conf_httpd_table, 0, &httpd_config.host, NULL);
add_dupstr_conf_item("WWW_ROOT", &conf_httpd_table, 0, &httpd_config.www_root, NULL);
add_uint_conf_item("PORT", &conf_httpd_table, 0, &httpd_config.port, 1, 65535, 0);
}
void _moddeinit(module_unload_intent_t intent)
{
mowgli_timer_destroy(base_eventloop, httpd_checkidle_timer);
hook_del_config_ready(httpd_config_ready);
connection_close_soon_children(listener);
del_conf_item("HOST", &conf_httpd_table);
del_conf_item("WWW_ROOT", &conf_httpd_table);
del_conf_item("PORT", &conf_httpd_table);
del_top_conf("HTTPD");
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs ts=8 sw=8 noexpandtab
*/