| 1 |
/* |
| 2 |
* Copyright (c) 2005-2007 Jilles Tjoelker et al. |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Generic HTTPd |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
#include "httpd.h" |
| 11 |
#include "datastream.h" |
| 12 |
|
| 13 |
#define REQUEST_MAX 65536 /* maximum size of one call */ |
| 14 |
|
| 15 |
DECLARE_MODULE_V1 |
| 16 |
( |
| 17 |
"misc/httpd", false, _modinit, _moddeinit, |
| 18 |
PACKAGE_STRING, |
| 19 |
VENDOR_STRING |
| 20 |
); |
| 21 |
|
| 22 |
connection_t *listener; |
| 23 |
mowgli_list_t httpd_path_handlers; |
| 24 |
|
| 25 |
/* conf stuff */ |
| 26 |
mowgli_list_t conf_httpd_table; |
| 27 |
struct httpd_configuration |
| 28 |
{ |
| 29 |
char *host; |
| 30 |
char *www_root; |
| 31 |
unsigned int port; |
| 32 |
} httpd_config; |
| 33 |
|
| 34 |
static void clear_httpddata(struct httpddata *hd) |
| 35 |
{ |
| 36 |
hd->method[0] = '\0'; |
| 37 |
hd->filename[0] = '\0'; |
| 38 |
if (hd->requestbuf != NULL) |
| 39 |
{ |
| 40 |
free(hd->requestbuf); |
| 41 |
hd->requestbuf = NULL; |
| 42 |
} |
| 43 |
if (hd->replybuf != NULL) |
| 44 |
{ |
| 45 |
free(hd->replybuf); |
| 46 |
hd->replybuf = NULL; |
| 47 |
} |
| 48 |
hd->length = 0; |
| 49 |
hd->lengthdone = 0; |
| 50 |
hd->correct_content_type = false; |
| 51 |
hd->expect_100_continue = false; |
| 52 |
hd->sent_reply = false; |
| 53 |
} |
| 54 |
|
| 55 |
static int open_file(const char *filename) |
| 56 |
{ |
| 57 |
char fname[256]; |
| 58 |
|
| 59 |
if (strstr(filename, "..")) |
| 60 |
return -1; |
| 61 |
if (!strcmp(filename, "/")) |
| 62 |
filename = "/index.html"; |
| 63 |
snprintf(fname, sizeof fname, "%s/%s", httpd_config.www_root, filename); |
| 64 |
return open(fname, O_RDONLY); |
| 65 |
} |
| 66 |
|
| 67 |
static void process_header(connection_t *cptr, char *line) |
| 68 |
{ |
| 69 |
struct httpddata *hd; |
| 70 |
char *p; |
| 71 |
|
| 72 |
hd = cptr->userdata; |
| 73 |
p = strchr(line, ':'); |
| 74 |
if (p == NULL) |
| 75 |
return; |
| 76 |
*p = '\0'; |
| 77 |
p++; |
| 78 |
while (*p == ' ') |
| 79 |
p++; |
| 80 |
if (!strcasecmp(line, "Connection")) |
| 81 |
{ |
| 82 |
p = strtok(p, ", \t"); |
| 83 |
while (p != NULL) |
| 84 |
{ |
| 85 |
if (!strcasecmp(p, "close")) |
| 86 |
{ |
| 87 |
slog(LG_DEBUG, "process_header(): Connection: close requested by fd %d", cptr->fd); |
| 88 |
hd->connection_close = true; |
| 89 |
} |
| 90 |
p = strtok(NULL, ", \t"); |
| 91 |
} |
| 92 |
} |
| 93 |
else if (!strcasecmp(line, "Content-Length")) |
| 94 |
{ |
| 95 |
hd->length = atoi(p); |
| 96 |
} |
| 97 |
else if (!strcasecmp(line, "Content-Type")) |
| 98 |
{ |
| 99 |
p = strtok(p, "; \t"); |
| 100 |
hd->correct_content_type = p != NULL && (!strcasecmp(p, "text/xml") || !strcasecmp(p, "application/json")); |
| 101 |
} |
| 102 |
else if (!strcasecmp(line, "Expect")) |
| 103 |
{ |
| 104 |
hd->expect_100_continue = !strcasecmp(p, "100-continue"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static void check_close(connection_t *cptr) |
| 109 |
{ |
| 110 |
struct httpddata *hd; |
| 111 |
|
| 112 |
hd = cptr->userdata; |
| 113 |
if (hd->connection_close) |
| 114 |
sendq_add_eof(cptr); |
| 115 |
} |
| 116 |
|
| 117 |
static void send_error(connection_t *cptr, int errorcode, const char *text, bool sendentity) |
| 118 |
{ |
| 119 |
char buf1[300]; |
| 120 |
char buf2[700]; |
| 121 |
|
| 122 |
if (errorcode < 100 || errorcode > 999) |
| 123 |
errorcode = 500; |
| 124 |
snprintf(buf2, sizeof buf2, "HTTP/1.1 %d %s\r\n", errorcode, text); |
| 125 |
snprintf(buf1, sizeof buf1, "HTTP/1.1 %d %s\r\n" |
| 126 |
"Server: Atheme/%s\r\n" |
| 127 |
"Content-Type: text/plain\r\n" |
| 128 |
"Content-Length: %lu\r\n\r\n%s", |
| 129 |
errorcode, text, PACKAGE_VERSION, (unsigned long)strlen(buf2), |
| 130 |
sendentity ? buf2 : ""); |
| 131 |
sendq_add(cptr, buf1, strlen(buf1)); |
| 132 |
} |
| 133 |
|
| 134 |
static const char *content_type(const char *filename) |
| 135 |
{ |
| 136 |
const char *p; |
| 137 |
|
| 138 |
if (!strcmp(filename, "/")) |
| 139 |
return "text/html"; |
| 140 |
|
| 141 |
p = strrchr(filename, '.'); |
| 142 |
if (p == NULL) |
| 143 |
return "text/plain"; |
| 144 |
p++; |
| 145 |
if (!strcasecmp(p, "html") || !strcasecmp(p, "htm")) |
| 146 |
return "text/html"; |
| 147 |
else if (!strcasecmp(p, "txt")) |
| 148 |
return "text/plain"; |
| 149 |
else if (!strcasecmp(p, "jpg") || !strcasecmp(p, "jpeg")) |
| 150 |
return "image/jpeg"; |
| 151 |
else if (!strcasecmp(p, "gif")) |
| 152 |
return "image/gif"; |
| 153 |
else if (!strcasecmp(p, "png")) |
| 154 |
return "image/png"; |
| 155 |
return "application/octet-stream"; |
| 156 |
} |
| 157 |
|
| 158 |
static void httpd_recvqhandler(connection_t *cptr) |
| 159 |
{ |
| 160 |
char buf[BUFSIZE * 2]; |
| 161 |
char outbuf[BUFSIZE * 2]; |
| 162 |
int count; |
| 163 |
struct httpddata *hd; |
| 164 |
char *p; |
| 165 |
int in; |
| 166 |
struct stat sb; |
| 167 |
off_t count1; |
| 168 |
mowgli_node_t *n; |
| 169 |
path_handler_t *ph = NULL; |
| 170 |
bool is_get, is_post, handling_done = false; |
| 171 |
|
| 172 |
hd = cptr->userdata; |
| 173 |
|
| 174 |
MOWGLI_ITER_FOREACH(n, httpd_path_handlers.head) |
| 175 |
{ |
| 176 |
ph = (path_handler_t *)n->data; |
| 177 |
handling_done = !strcmp(hd->filename, ph->path); |
| 178 |
|
| 179 |
if (handling_done) |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
if (handling_done) |
| 184 |
{ |
| 185 |
if (hd->requestbuf != NULL) |
| 186 |
{ |
| 187 |
count = recvq_get(cptr, hd->requestbuf + hd->lengthdone, hd->length - hd->lengthdone); |
| 188 |
if (count <= 0) |
| 189 |
return; |
| 190 |
hd->lengthdone += count; |
| 191 |
if (hd->lengthdone != hd->length) |
| 192 |
return; |
| 193 |
hd->requestbuf[hd->length] = '\0'; |
| 194 |
|
| 195 |
ph->handler(cptr, hd->requestbuf); |
| 196 |
|
| 197 |
clear_httpddata(hd); |
| 198 |
return; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
count = recvq_getline(cptr, buf, sizeof buf - 1); |
| 203 |
if (count <= 0) |
| 204 |
return; |
| 205 |
if (cptr->flags & CF_NONEWLINE) |
| 206 |
{ |
| 207 |
slog(LG_INFO, "httpd_recvqhandler(): throwing out fd %d (%s) for excessive line length", cptr->fd, cptr->hbuf); |
| 208 |
send_error(cptr, 400, "Bad request", true); |
| 209 |
sendq_add_eof(cptr); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
cnt.bin += count; |
| 214 |
if (buf[count - 1] == '\n') |
| 215 |
count--; |
| 216 |
if (count > 0 && buf[count - 1] == '\r') |
| 217 |
count--; |
| 218 |
buf[count] = '\0'; |
| 219 |
|
| 220 |
if (hd->method[0] == '\0') |
| 221 |
{ |
| 222 |
/* make sure they're not sending more requests after |
| 223 |
* declaring they're not sending any more */ |
| 224 |
if (hd->connection_close) |
| 225 |
return; |
| 226 |
p = strtok(buf, " "); |
| 227 |
if (p == NULL) |
| 228 |
return; |
| 229 |
mowgli_strlcpy(hd->method, p, sizeof hd->method); |
| 230 |
p = strtok(NULL, " "); |
| 231 |
if (p == NULL) |
| 232 |
return; |
| 233 |
mowgli_strlcpy(hd->filename, p, sizeof hd->filename); |
| 234 |
p = strtok(NULL, ""); |
| 235 |
if (p == NULL || !strcmp(p, "HTTP/1.0")) |
| 236 |
hd->connection_close = true; |
| 237 |
slog(LG_DEBUG, "httpd_recvqhandler(): request %s for %s", hd->method, hd->filename); |
| 238 |
} |
| 239 |
else if (count == 0) |
| 240 |
{ |
| 241 |
is_get = !strcmp(hd->method, "GET"); |
| 242 |
is_post = !strcmp(hd->method, "POST"); |
| 243 |
|
| 244 |
if (!is_post && !is_get) |
| 245 |
{ |
| 246 |
send_error(cptr, 501, "Method Not Implemented", true); |
| 247 |
sendq_add_eof(cptr); |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
hd->method[0] = '\0'; |
| 252 |
|
| 253 |
if (!handling_done) |
| 254 |
{ |
| 255 |
in = open_file(hd->filename); |
| 256 |
if (in == -1 || fstat(in, &sb) == -1 || !S_ISREG(sb.st_mode)) |
| 257 |
{ |
| 258 |
if (in != -1) |
| 259 |
close(in); |
| 260 |
slog(LG_DEBUG, "httpd_recvqhandler(): 404 for \2%s\2", hd->filename); |
| 261 |
send_error(cptr, 404, "Not Found", is_get); |
| 262 |
check_close(cptr); |
| 263 |
return; |
| 264 |
} |
| 265 |
slog(LG_INFO, "httpd_recvqhandler(): 200 for %s", hd->filename); |
| 266 |
snprintf(outbuf, sizeof outbuf, |
| 267 |
"HTTP/1.1 200 OK\r\nServer: Atheme/%s\r\nContent-Type: %s\r\nContent-Length: %lu\r\n\r\n", |
| 268 |
PACKAGE_VERSION, |
| 269 |
content_type(hd->filename), |
| 270 |
(unsigned long)sb.st_size); |
| 271 |
sendq_add(cptr, outbuf, strlen(outbuf)); |
| 272 |
count1 = is_get ? sb.st_size : 0; |
| 273 |
while (count1 > 0) |
| 274 |
{ |
| 275 |
count = sizeof outbuf; |
| 276 |
if (count > count1) |
| 277 |
count = count1; |
| 278 |
count = read(in, outbuf, count); |
| 279 |
if (count <= 0) |
| 280 |
break; |
| 281 |
sendq_add(cptr, outbuf, count); |
| 282 |
count1 -= count; |
| 283 |
} |
| 284 |
close(in); |
| 285 |
if (count1 > 0) |
| 286 |
{ |
| 287 |
slog(LG_INFO, "httpd_recvqhandler(): disconnecting fd %d (%s), read failed on %s", cptr->fd, cptr->hbuf, hd->filename); |
| 288 |
cptr->flags |= CF_DEAD; |
| 289 |
} |
| 290 |
else |
| 291 |
check_close(cptr); |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
if (hd->length <= 0) |
| 296 |
{ |
| 297 |
send_error(cptr, 411, "Length Required", true); |
| 298 |
sendq_add_eof(cptr); |
| 299 |
return; |
| 300 |
} |
| 301 |
if (hd->length > REQUEST_MAX) |
| 302 |
{ |
| 303 |
send_error(cptr, 413, "Request Entity Too Large", true); |
| 304 |
sendq_add_eof(cptr); |
| 305 |
return; |
| 306 |
} |
| 307 |
if (!hd->correct_content_type) |
| 308 |
{ |
| 309 |
send_error(cptr, 415, "Unsupported Media Type", true); |
| 310 |
sendq_add_eof(cptr); |
| 311 |
return; |
| 312 |
} |
| 313 |
if (hd->expect_100_continue) |
| 314 |
{ |
| 315 |
snprintf(outbuf, sizeof outbuf, |
| 316 |
"HTTP/1.1 100 Continue\r\nServer: Atheme/%s\r\n\r\n", |
| 317 |
PACKAGE_VERSION); |
| 318 |
sendq_add(cptr, outbuf, strlen(outbuf)); |
| 319 |
} |
| 320 |
hd->requestbuf = smalloc(hd->length + 1); |
| 321 |
} |
| 322 |
} |
| 323 |
else |
| 324 |
process_header(cptr, buf); |
| 325 |
} |
| 326 |
|
| 327 |
static void httpd_closehandler(connection_t *cptr) |
| 328 |
{ |
| 329 |
struct httpddata *hd; |
| 330 |
|
| 331 |
slog(LG_DEBUG, "httpd_closehandler(): fd %d (%s) closed", cptr->fd, cptr->hbuf); |
| 332 |
hd = cptr->userdata; |
| 333 |
if (hd != NULL) |
| 334 |
{ |
| 335 |
free(hd->requestbuf); |
| 336 |
free(hd); |
| 337 |
} |
| 338 |
cptr->userdata = NULL; |
| 339 |
} |
| 340 |
|
| 341 |
static void do_listen(connection_t *cptr) |
| 342 |
{ |
| 343 |
connection_t *newptr; |
| 344 |
struct httpddata *hd; |
| 345 |
|
| 346 |
newptr = connection_accept_tcp(cptr, recvq_put, NULL); |
| 347 |
slog(LG_DEBUG, "do_listen(): accepted httpd from %s fd %d", newptr->hbuf, newptr->fd); |
| 348 |
hd = smalloc(sizeof(*hd)); |
| 349 |
hd->requestbuf = NULL; |
| 350 |
hd->replybuf = NULL; |
| 351 |
hd->connection_close = false; |
| 352 |
clear_httpddata(hd); |
| 353 |
newptr->userdata = hd; |
| 354 |
newptr->recvq_handler = httpd_recvqhandler; |
| 355 |
newptr->close_handler = httpd_closehandler; |
| 356 |
} |
| 357 |
|
| 358 |
static void httpd_checkidle(void *arg) |
| 359 |
{ |
| 360 |
mowgli_node_t *n, *tn; |
| 361 |
connection_t *cptr; |
| 362 |
|
| 363 |
(void)arg; |
| 364 |
if (listener == NULL) |
| 365 |
return; |
| 366 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, connection_list.head) |
| 367 |
{ |
| 368 |
cptr = n->data; |
| 369 |
if (cptr->listener == listener && cptr->last_recv + 300 < CURRTIME) |
| 370 |
{ |
| 371 |
if (sendq_nonempty(cptr)) |
| 372 |
cptr->last_recv = CURRTIME; |
| 373 |
else |
| 374 |
/* from a timeout function, |
| 375 |
* connection_close_soon() may take quite |
| 376 |
* a while, and connection_close() is safe |
| 377 |
* -- jilles */ |
| 378 |
connection_close(cptr); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
static void httpd_config_ready(void *vptr) |
| 384 |
{ |
| 385 |
if (httpd_config.host != NULL && httpd_config.port != 0) |
| 386 |
{ |
| 387 |
/* Some code depends on connection_t.listener == listener. */ |
| 388 |
if (listener != NULL) |
| 389 |
return; |
| 390 |
listener = connection_open_listener_tcp(httpd_config.host, |
| 391 |
httpd_config.port, do_listen); |
| 392 |
if (listener == NULL) |
| 393 |
slog(LG_ERROR, "httpd_config_ready(): failed to open listener on host %s port %d", httpd_config.host, httpd_config.port); |
| 394 |
} |
| 395 |
else |
| 396 |
slog(LG_ERROR, "httpd_config_ready(): httpd {} block missing or invalid"); |
| 397 |
} |
| 398 |
|
| 399 |
static mowgli_eventloop_timer_t *httpd_checkidle_timer = NULL; |
| 400 |
|
| 401 |
void _modinit(module_t *m) |
| 402 |
{ |
| 403 |
httpd_checkidle_timer = mowgli_timer_add(base_eventloop, "httpd_checkidle", httpd_checkidle, NULL, 60); |
| 404 |
|
| 405 |
/* This module needs a rehash to initialize fully if loaded |
| 406 |
* at run time */ |
| 407 |
hook_add_event("config_ready"); |
| 408 |
hook_add_config_ready(httpd_config_ready); |
| 409 |
|
| 410 |
add_subblock_top_conf("HTTPD", &conf_httpd_table); |
| 411 |
add_dupstr_conf_item("HOST", &conf_httpd_table, 0, &httpd_config.host, NULL); |
| 412 |
add_dupstr_conf_item("WWW_ROOT", &conf_httpd_table, 0, &httpd_config.www_root, NULL); |
| 413 |
add_uint_conf_item("PORT", &conf_httpd_table, 0, &httpd_config.port, 1, 65535, 0); |
| 414 |
} |
| 415 |
|
| 416 |
void _moddeinit(module_unload_intent_t intent) |
| 417 |
{ |
| 418 |
mowgli_timer_destroy(base_eventloop, httpd_checkidle_timer); |
| 419 |
|
| 420 |
hook_del_config_ready(httpd_config_ready); |
| 421 |
connection_close_children(listener); |
| 422 |
del_conf_item("HOST", &conf_httpd_table); |
| 423 |
del_conf_item("WWW_ROOT", &conf_httpd_table); |
| 424 |
del_conf_item("PORT", &conf_httpd_table); |
| 425 |
del_top_conf("HTTPD"); |
| 426 |
} |
| 427 |
|
| 428 |
/* 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 |
| 429 |
*/ |