| 1 | /* |
| 2 | * httpd.c: implementation of httpd.h. |
| 3 | */ |
| 4 | |
| 5 | #include "agedu.h" |
| 6 | #include "alloc.h" |
| 7 | #include "html.h" |
| 8 | #include "httpd.h" |
| 9 | |
| 10 | /* --- Logic driving what the web server's responses are. --- */ |
| 11 | |
| 12 | enum { /* connctx states */ |
| 13 | READING_REQ_LINE, |
| 14 | READING_HEADERS, |
| 15 | DONE |
| 16 | }; |
| 17 | |
| 18 | struct connctx { |
| 19 | const void *t; |
| 20 | char *data; |
| 21 | int datalen, datasize; |
| 22 | char *method, *url, *headers, *auth; |
| 23 | int state; |
| 24 | }; |
| 25 | |
| 26 | /* |
| 27 | * Called when a new connection arrives on a listening socket. |
| 28 | * Returns a connctx for the new connection. |
| 29 | */ |
| 30 | struct connctx *new_connection(const void *t) |
| 31 | { |
| 32 | struct connctx *cctx = snew(struct connctx); |
| 33 | cctx->t = t; |
| 34 | cctx->data = NULL; |
| 35 | cctx->datalen = cctx->datasize = 0; |
| 36 | cctx->state = READING_REQ_LINE; |
| 37 | cctx->method = cctx->url = cctx->headers = cctx->auth = NULL; |
| 38 | return cctx; |
| 39 | } |
| 40 | |
| 41 | void free_connection(struct connctx *cctx) |
| 42 | { |
| 43 | sfree(cctx->data); |
| 44 | sfree(cctx); |
| 45 | } |
| 46 | |
| 47 | static char *http_error(char *code, char *errmsg, char *extraheader, |
| 48 | char *errtext, ...) |
| 49 | { |
| 50 | return dupfmt("HTTP/1.1 %s %s\r\n" |
| 51 | "Date: %D\r\n" |
| 52 | "Server: " PNAME "\r\n" |
| 53 | "Connection: close\r\n" |
| 54 | "%s" |
| 55 | "Content-Type: text/html; charset=US-ASCII\r\n" |
| 56 | "\r\n" |
| 57 | "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n" |
| 58 | "<HTML><HEAD>\r\n" |
| 59 | "<TITLE>%s %s</TITLE>\r\n" |
| 60 | "</HEAD><BODY>\r\n" |
| 61 | "<H1>%s %s</H1>\r\n" |
| 62 | "<P>%s</P>\r\n" |
| 63 | "</BODY></HTML>\r\n", code, errmsg, |
| 64 | extraheader ? extraheader : "", |
| 65 | code, errmsg, code, errmsg, errtext); |
| 66 | } |
| 67 | |
| 68 | static char *http_success(char *mimetype, int stuff_cr, char *document) |
| 69 | { |
| 70 | return dupfmt("HTTP/1.1 200 OK\r\n" |
| 71 | "Date: %D\r\n" |
| 72 | "Expires: %D\r\n" |
| 73 | "Server: " PNAME "\r\n" |
| 74 | "Connection: close\r\n" |
| 75 | "Content-Type: %s\r\n" |
| 76 | "\r\n" |
| 77 | "%S", mimetype, stuff_cr, document); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Called when data comes in on a connection. |
| 82 | * |
| 83 | * If this function returns NULL, the platform code continues |
| 84 | * reading from the socket. Otherwise, it returns some dynamically |
| 85 | * allocated data which the platform code will then write to the |
| 86 | * socket before closing it. |
| 87 | */ |
| 88 | char *got_data(struct connctx *ctx, char *data, int length, |
| 89 | int magic_access, const char *auth_string, |
| 90 | const struct html_config *cfg) |
| 91 | { |
| 92 | char *line, *p, *q, *r, *z1, *z2, c1, c2; |
| 93 | int auth_provided = 0, auth_correct = 0; |
| 94 | unsigned long index; |
| 95 | char *document, *ret; |
| 96 | |
| 97 | /* |
| 98 | * Add the data we've just received to our buffer. |
| 99 | */ |
| 100 | if (ctx->datasize < ctx->datalen + length) { |
| 101 | ctx->datasize = (ctx->datalen + length) * 3 / 2 + 4096; |
| 102 | ctx->data = sresize(ctx->data, ctx->datasize, char); |
| 103 | } |
| 104 | memcpy(ctx->data + ctx->datalen, data, length); |
| 105 | ctx->datalen += length; |
| 106 | |
| 107 | /* |
| 108 | * Gradually process the HTTP request as we receive it. |
| 109 | */ |
| 110 | if (ctx->state == READING_REQ_LINE) { |
| 111 | /* |
| 112 | * We're waiting for the first line of the input, which |
| 113 | * contains the main HTTP request. See if we've got it |
| 114 | * yet. |
| 115 | */ |
| 116 | |
| 117 | line = ctx->data; |
| 118 | /* |
| 119 | * RFC 2616 section 4.1: `In the interest of robustness, |
| 120 | * [...] if the server is reading the protocol stream at |
| 121 | * the beginning of a message and receives a CRLF first, |
| 122 | * it should ignore the CRLF.' |
| 123 | */ |
| 124 | while (line - ctx->data < ctx->datalen && |
| 125 | (*line == '\r' || *line == '\n')) |
| 126 | line++; |
| 127 | q = line; |
| 128 | while (q - ctx->data < ctx->datalen && *q != '\n') |
| 129 | q++; |
| 130 | if (q - ctx->data >= ctx->datalen) |
| 131 | return NULL; /* not got request line yet */ |
| 132 | |
| 133 | /* |
| 134 | * We've got the first line of the request. Zero-terminate |
| 135 | * and parse it into method, URL and optional HTTP |
| 136 | * version. |
| 137 | */ |
| 138 | *q = '\0'; |
| 139 | ctx->headers = q+1; |
| 140 | if (q > line && q[-1] == '\r') |
| 141 | *--q = '\0'; |
| 142 | z1 = z2 = q; |
| 143 | c1 = c2 = *q; |
| 144 | p = line; |
| 145 | while (*p && !isspace((unsigned char)*p)) p++; |
| 146 | if (*p) { |
| 147 | z1 = p++; |
| 148 | c1 = *z1; |
| 149 | *z1 = '\0'; |
| 150 | } |
| 151 | while (*p && isspace((unsigned char)*p)) p++; |
| 152 | q = p; |
| 153 | while (*q && !isspace((unsigned char)*q)) q++; |
| 154 | z2 = q++; |
| 155 | c2 = *z2; |
| 156 | *z2 = '\0'; |
| 157 | while (*q && isspace((unsigned char)*q)) q++; |
| 158 | |
| 159 | /* |
| 160 | * Now `line' points at the method name; p points at the |
| 161 | * URL, if any; q points at the HTTP version, if any. |
| 162 | */ |
| 163 | |
| 164 | /* |
| 165 | * There should _be_ a URL, on any request type at all. |
| 166 | */ |
| 167 | if (!*p) { |
| 168 | char *ret, *text; |
| 169 | /* Restore the request to the way we received it. */ |
| 170 | *z2 = c2; |
| 171 | *z1 = c1; |
| 172 | text = dupfmt("<code>" PNAME "</code> received the HTTP request" |
| 173 | " \"<code>%h</code>\", which contains no URL.", |
| 174 | line); |
| 175 | ret = http_error("400", "Bad request", NULL, text); |
| 176 | sfree(text); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | ctx->method = line; |
| 181 | ctx->url = p; |
| 182 | |
| 183 | /* |
| 184 | * If there was an HTTP version, we might need to see |
| 185 | * headers. Otherwise, the request is done. |
| 186 | */ |
| 187 | if (*q) { |
| 188 | ctx->state = READING_HEADERS; |
| 189 | } else { |
| 190 | ctx->state = DONE; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (ctx->state == READING_HEADERS) { |
| 195 | /* |
| 196 | * While we're receiving the HTTP request headers, all we |
| 197 | * do is to keep scanning to see if we find two newlines |
| 198 | * next to each other. |
| 199 | */ |
| 200 | q = ctx->data + ctx->datalen; |
| 201 | for (p = ctx->headers; p < q; p++) { |
| 202 | if (*p == '\n' && |
| 203 | ((p+1 < q && p[1] == '\n') || |
| 204 | (p+2 < q && p[1] == '\r' && p[2] == '\n'))) { |
| 205 | p[1] = '\0'; |
| 206 | ctx->state = DONE; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (ctx->state == DONE) { |
| 213 | /* |
| 214 | * Now we have the entire HTTP request. Decide what to do |
| 215 | * with it. |
| 216 | */ |
| 217 | if (auth_string) { |
| 218 | /* |
| 219 | * Search the request headers for Authorization. |
| 220 | */ |
| 221 | q = ctx->data + ctx->datalen; |
| 222 | for (p = ctx->headers; p < q; p++) { |
| 223 | const char *hdr = "Authorization:"; |
| 224 | int i; |
| 225 | for (i = 0; hdr[i]; i++) { |
| 226 | if (p >= q || tolower((unsigned char)*p) != |
| 227 | tolower((unsigned char)hdr[i])) |
| 228 | break; |
| 229 | p++; |
| 230 | } |
| 231 | if (!hdr[i]) |
| 232 | break; /* found our header */ |
| 233 | p = memchr(p, '\n', q - p); |
| 234 | if (!p) |
| 235 | p = q; |
| 236 | } |
| 237 | if (p < q) { |
| 238 | auth_provided = 1; |
| 239 | while (p < q && isspace((unsigned char)*p)) |
| 240 | p++; |
| 241 | r = p; |
| 242 | while (p < q && !isspace((unsigned char)*p)) |
| 243 | p++; |
| 244 | if (p < q) { |
| 245 | *p++ = '\0'; |
| 246 | if (!strcasecmp(r, "Basic")) { |
| 247 | while (p < q && isspace((unsigned char)*p)) |
| 248 | p++; |
| 249 | r = p; |
| 250 | while (p < q && !isspace((unsigned char)*p)) |
| 251 | p++; |
| 252 | if (p < q) { |
| 253 | *p++ = '\0'; |
| 254 | if (!strcmp(r, auth_string)) |
| 255 | auth_correct = 1; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (!magic_access && !auth_correct) { |
| 263 | if (auth_string) { |
| 264 | ret = http_error("401", "Unauthorized", |
| 265 | "WWW-Authenticate: Basic realm=\""PNAME"\"\r", |
| 266 | "\nYou must authenticate to view these pages."); |
| 267 | } else { |
| 268 | ret = http_error("403", "Forbidden", NULL, |
| 269 | "This is a restricted-access set of pages."); |
| 270 | } |
| 271 | } else { |
| 272 | p = ctx->url; |
| 273 | p += strspn(p, "/?"); |
| 274 | index = strtoul(p, NULL, 10); |
| 275 | document = html_query(ctx->t, index, cfg); |
| 276 | if (document) { |
| 277 | ret = http_success("text/html", 1, document); |
| 278 | sfree(document); |
| 279 | } else { |
| 280 | ret = http_error("404", "Not Found", NULL, |
| 281 | "Pathname index out of range."); |
| 282 | } |
| 283 | } |
| 284 | return ret; |
| 285 | } else |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | /* --- Platform support for running a web server. --- */ |
| 290 | |
| 291 | enum { FD_CLIENT, FD_LISTENER, FD_CONNECTION }; |
| 292 | |
| 293 | struct fd { |
| 294 | int fd; |
| 295 | int type; |
| 296 | int deleted; |
| 297 | char *wdata; |
| 298 | int wdatalen, wdatapos; |
| 299 | int magic_access; |
| 300 | struct connctx *cctx; |
| 301 | }; |
| 302 | |
| 303 | struct fd *fds = NULL; |
| 304 | int nfds = 0, fdsize = 0; |
| 305 | |
| 306 | struct fd *new_fdstruct(int fd, int type) |
| 307 | { |
| 308 | struct fd *ret; |
| 309 | |
| 310 | if (nfds >= fdsize) { |
| 311 | fdsize = nfds * 3 / 2 + 32; |
| 312 | fds = sresize(fds, fdsize, struct fd); |
| 313 | } |
| 314 | |
| 315 | ret = &fds[nfds++]; |
| 316 | |
| 317 | ret->fd = fd; |
| 318 | ret->type = type; |
| 319 | ret->wdata = NULL; |
| 320 | ret->wdatalen = ret->wdatapos = 0; |
| 321 | ret->cctx = NULL; |
| 322 | ret->deleted = 0; |
| 323 | ret->magic_access = 0; |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | int check_owning_uid(int fd, int flip) |
| 329 | { |
| 330 | struct sockaddr_in sock, peer; |
| 331 | socklen_t addrlen; |
| 332 | char linebuf[4096], matchbuf[80]; |
| 333 | FILE *fp; |
| 334 | |
| 335 | addrlen = sizeof(sock); |
| 336 | if (getsockname(fd, (struct sockaddr *)&sock, &addrlen)) { |
| 337 | fprintf(stderr, "getsockname: %s\n", strerror(errno)); |
| 338 | exit(1); |
| 339 | } |
| 340 | addrlen = sizeof(peer); |
| 341 | if (getpeername(fd, (struct sockaddr *)&peer, &addrlen)) { |
| 342 | if (errno == ENOTCONN) { |
| 343 | peer.sin_addr.s_addr = htonl(0); |
| 344 | peer.sin_port = htons(0); |
| 345 | } else { |
| 346 | fprintf(stderr, "getpeername: %s\n", strerror(errno)); |
| 347 | exit(1); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (flip) { |
| 352 | struct sockaddr_in tmp = sock; |
| 353 | sock = peer; |
| 354 | peer = tmp; |
| 355 | } |
| 356 | |
| 357 | sprintf(matchbuf, "%08X:%04X %08X:%04X", |
| 358 | peer.sin_addr.s_addr, ntohs(peer.sin_port), |
| 359 | sock.sin_addr.s_addr, ntohs(sock.sin_port)); |
| 360 | fp = fopen("/proc/net/tcp", "r"); |
| 361 | if (fp) { |
| 362 | while (fgets(linebuf, sizeof(linebuf), fp)) { |
| 363 | if (strlen(linebuf) >= 75 && |
| 364 | !strncmp(linebuf+6, matchbuf, strlen(matchbuf))) { |
| 365 | return atoi(linebuf + 75); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | void check_magic_access(struct fd *fd) |
| 374 | { |
| 375 | if (check_owning_uid(fd->fd, 0) == getuid()) |
| 376 | fd->magic_access = 1; |
| 377 | } |
| 378 | |
| 379 | static void base64_encode_atom(unsigned char *data, int n, char *out) |
| 380 | { |
| 381 | static const char base64_chars[] = |
| 382 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 383 | |
| 384 | unsigned word; |
| 385 | |
| 386 | word = data[0] << 16; |
| 387 | if (n > 1) |
| 388 | word |= data[1] << 8; |
| 389 | if (n > 2) |
| 390 | word |= data[2]; |
| 391 | out[0] = base64_chars[(word >> 18) & 0x3F]; |
| 392 | out[1] = base64_chars[(word >> 12) & 0x3F]; |
| 393 | if (n > 1) |
| 394 | out[2] = base64_chars[(word >> 6) & 0x3F]; |
| 395 | else |
| 396 | out[2] = '='; |
| 397 | if (n > 2) |
| 398 | out[3] = base64_chars[word & 0x3F]; |
| 399 | else |
| 400 | out[3] = '='; |
| 401 | } |
| 402 | |
| 403 | void run_httpd(const void *t, int authmask, const struct httpd_config *dcfg, |
| 404 | const struct html_config *incfg) |
| 405 | { |
| 406 | int fd, ret; |
| 407 | int authtype; |
| 408 | char *authstring = NULL; |
| 409 | unsigned long ipaddr; |
| 410 | struct fd *f; |
| 411 | struct sockaddr_in addr; |
| 412 | socklen_t addrlen; |
| 413 | struct html_config cfg = *incfg; |
| 414 | |
| 415 | cfg.format = "%.0lu"; |
| 416 | |
| 417 | /* |
| 418 | * Establish the listening socket and retrieve its port |
| 419 | * number. |
| 420 | */ |
| 421 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 422 | if (fd < 0) { |
| 423 | fprintf(stderr, "socket(PF_INET): %s\n", strerror(errno)); |
| 424 | exit(1); |
| 425 | } |
| 426 | addr.sin_family = AF_INET; |
| 427 | if (!dcfg->address) { |
| 428 | srand(0L); |
| 429 | ipaddr = 0x7f000000; |
| 430 | ipaddr += (1 + rand() % 255) << 16; |
| 431 | ipaddr += (1 + rand() % 255) << 8; |
| 432 | ipaddr += (1 + rand() % 255); |
| 433 | addr.sin_addr.s_addr = htonl(ipaddr); |
| 434 | addr.sin_port = htons(0); |
| 435 | |
| 436 | } else { |
| 437 | addr.sin_addr.s_addr = inet_addr(dcfg->address); |
| 438 | addr.sin_port = dcfg->port ? htons(dcfg->port) : 0; |
| 439 | } |
| 440 | addrlen = sizeof(addr); |
| 441 | ret = bind(fd, (const struct sockaddr *)&addr, addrlen); |
| 442 | if (ret < 0 && errno == EADDRNOTAVAIL && !dcfg->address) { |
| 443 | /* |
| 444 | * Some systems don't like us binding to random weird |
| 445 | * localhost-space addresses. Try again with the official |
| 446 | * INADDR_LOOPBACK. |
| 447 | */ |
| 448 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 449 | addr.sin_port = htons(0); |
| 450 | ret = bind(fd, (const struct sockaddr *)&addr, addrlen); |
| 451 | } |
| 452 | if (ret < 0) { |
| 453 | fprintf(stderr, "bind: %s\n", strerror(errno)); |
| 454 | exit(1); |
| 455 | } |
| 456 | if (listen(fd, 5) < 0) { |
| 457 | fprintf(stderr, "listen: %s\n", strerror(errno)); |
| 458 | exit(1); |
| 459 | } |
| 460 | addrlen = sizeof(addr); |
| 461 | if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) { |
| 462 | fprintf(stderr, "getsockname: %s\n", strerror(errno)); |
| 463 | exit(1); |
| 464 | } |
| 465 | if ((authmask & HTTPD_AUTH_MAGIC) && |
| 466 | (check_owning_uid(fd, 1) == getuid())) { |
| 467 | authtype = HTTPD_AUTH_MAGIC; |
| 468 | if (authmask != HTTPD_AUTH_MAGIC) |
| 469 | printf("Using Linux /proc/net magic authentication\n"); |
| 470 | } else if ((authmask & HTTPD_AUTH_BASIC)) { |
| 471 | char username[128], password[128], userpassbuf[259]; |
| 472 | const char *userpass; |
| 473 | const char *rname; |
| 474 | unsigned char passbuf[10]; |
| 475 | int i, j, k, fd; |
| 476 | |
| 477 | authtype = HTTPD_AUTH_BASIC; |
| 478 | |
| 479 | if (authmask != HTTPD_AUTH_BASIC) |
| 480 | printf("Using HTTP Basic authentication\n"); |
| 481 | |
| 482 | if (dcfg->basicauthdata) { |
| 483 | userpass = dcfg->basicauthdata; |
| 484 | } else { |
| 485 | strcpy(username, PNAME); |
| 486 | rname = "/dev/urandom"; |
| 487 | fd = open(rname, O_RDONLY); |
| 488 | if (fd < 0) { |
| 489 | int err = errno; |
| 490 | rname = "/dev/random"; |
| 491 | fd = open(rname, O_RDONLY); |
| 492 | if (fd < 0) { |
| 493 | int err2 = errno; |
| 494 | fprintf(stderr, "/dev/urandom: open: %s\n", strerror(err)); |
| 495 | fprintf(stderr, "/dev/random: open: %s\n", strerror(err2)); |
| 496 | exit(1); |
| 497 | } |
| 498 | } |
| 499 | for (i = 0; i < 10 ;) { |
| 500 | j = read(fd, passbuf + i, 10 - i); |
| 501 | if (j <= 0) { |
| 502 | fprintf(stderr, "%s: read: %s\n", rname, |
| 503 | j < 0 ? strerror(errno) : "unexpected EOF"); |
| 504 | exit(1); |
| 505 | } |
| 506 | i += j; |
| 507 | } |
| 508 | close(fd); |
| 509 | for (i = 0; i < 16; i++) { |
| 510 | /* |
| 511 | * 32 characters out of the 36 alphanumerics gives |
| 512 | * me the latitude to discard i,l,o for being too |
| 513 | * numeric-looking, and w because it has two too |
| 514 | * many syllables and one too many presidential |
| 515 | * associations. |
| 516 | */ |
| 517 | static const char chars[32] = |
| 518 | "0123456789abcdefghjkmnpqrstuvxyz"; |
| 519 | int v = 0; |
| 520 | |
| 521 | k = i / 8 * 5; |
| 522 | for (j = 0; j < 5; j++) |
| 523 | v |= ((passbuf[k+j] >> (i%8)) & 1) << j; |
| 524 | |
| 525 | password[i] = chars[v]; |
| 526 | } |
| 527 | password[i] = '\0'; |
| 528 | |
| 529 | sprintf(userpassbuf, "%s:%s", username, password); |
| 530 | userpass = userpassbuf; |
| 531 | |
| 532 | printf("Username: %s\nPassword: %s\n", username, password); |
| 533 | } |
| 534 | |
| 535 | k = strlen(userpass); |
| 536 | authstring = snewn(k * 4 / 3 + 16, char); |
| 537 | for (i = j = 0; i < k ;) { |
| 538 | int s = k-i < 3 ? k-i : 3; |
| 539 | base64_encode_atom((unsigned char *)(userpass+i), s, authstring+j); |
| 540 | i += s; |
| 541 | j += 4; |
| 542 | } |
| 543 | authstring[j] = '\0'; |
| 544 | } else if ((authmask & HTTPD_AUTH_NONE)) { |
| 545 | authtype = HTTPD_AUTH_NONE; |
| 546 | if (authmask != HTTPD_AUTH_NONE) |
| 547 | printf("Web server is unauthenticated\n"); |
| 548 | } else { |
| 549 | fprintf(stderr, PNAME ": authentication method not supported\n"); |
| 550 | exit(1); |
| 551 | } |
| 552 | if (ntohs(addr.sin_addr.s_addr) == INADDR_ANY) { |
| 553 | printf("Server port: %d\n", ntohs(addr.sin_port)); |
| 554 | } else if (ntohs(addr.sin_port) == 80) { |
| 555 | printf("URL: http://%s/\n", inet_ntoa(addr.sin_addr)); |
| 556 | } else { |
| 557 | printf("URL: http://%s:%d/\n", |
| 558 | inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Now construct an fd structure to hold it. |
| 563 | */ |
| 564 | f = new_fdstruct(fd, FD_LISTENER); |
| 565 | |
| 566 | /* |
| 567 | * Read from standard input, and treat EOF as a notification |
| 568 | * to exit. |
| 569 | */ |
| 570 | new_fdstruct(0, FD_CLIENT); |
| 571 | |
| 572 | /* |
| 573 | * Now we're ready to run our main loop. Keep looping round on |
| 574 | * select. |
| 575 | */ |
| 576 | while (1) { |
| 577 | fd_set rfds, wfds; |
| 578 | int i, j; |
| 579 | SELECT_TYPE_ARG1 maxfd; |
| 580 | int ret; |
| 581 | |
| 582 | #define FD_SET_MAX(fd, set, max) \ |
| 583 | do { FD_SET((fd),(set)); (max) = ((max)<=(fd)?(fd)+1:(max)); } while(0) |
| 584 | |
| 585 | /* |
| 586 | * Loop round the fd list putting fds into our select |
| 587 | * sets. Also in this loop we remove any that were marked |
| 588 | * as deleted in the previous loop. |
| 589 | */ |
| 590 | FD_ZERO(&rfds); |
| 591 | FD_ZERO(&wfds); |
| 592 | maxfd = 0; |
| 593 | for (i = j = 0; j < nfds; j++) { |
| 594 | |
| 595 | if (fds[j].deleted) { |
| 596 | sfree(fds[j].wdata); |
| 597 | free_connection(fds[j].cctx); |
| 598 | continue; |
| 599 | } |
| 600 | fds[i] = fds[j]; |
| 601 | |
| 602 | switch (fds[i].type) { |
| 603 | case FD_CLIENT: |
| 604 | FD_SET_MAX(fds[i].fd, &rfds, maxfd); |
| 605 | break; |
| 606 | case FD_LISTENER: |
| 607 | FD_SET_MAX(fds[i].fd, &rfds, maxfd); |
| 608 | break; |
| 609 | case FD_CONNECTION: |
| 610 | /* |
| 611 | * Always read from a connection socket. Even |
| 612 | * after we've started writing, the peer might |
| 613 | * still be sending (e.g. because we shamefully |
| 614 | * jumped the gun before waiting for the end of |
| 615 | * the HTTP request) and so we should be prepared |
| 616 | * to read data and throw it away. |
| 617 | */ |
| 618 | FD_SET_MAX(fds[i].fd, &rfds, maxfd); |
| 619 | /* |
| 620 | * Also attempt to write, if we have data to write. |
| 621 | */ |
| 622 | if (fds[i].wdatapos < fds[i].wdatalen) |
| 623 | FD_SET_MAX(fds[i].fd, &wfds, maxfd); |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | i++; |
| 628 | } |
| 629 | nfds = i; |
| 630 | |
| 631 | ret = select(maxfd, SELECT_TYPE_ARG234 &rfds, |
| 632 | SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 NULL, |
| 633 | SELECT_TYPE_ARG5 NULL); |
| 634 | if (ret <= 0) { |
| 635 | if (ret < 0 && (errno != EINTR)) { |
| 636 | fprintf(stderr, "select: %s", strerror(errno)); |
| 637 | exit(1); |
| 638 | } |
| 639 | continue; |
| 640 | } |
| 641 | |
| 642 | for (i = 0; i < nfds; i++) { |
| 643 | switch (fds[i].type) { |
| 644 | case FD_CLIENT: |
| 645 | if (FD_ISSET(fds[i].fd, &rfds)) { |
| 646 | char buf[4096]; |
| 647 | int ret = read(fds[i].fd, buf, sizeof(buf)); |
| 648 | if (ret <= 0) { |
| 649 | if (ret < 0) { |
| 650 | fprintf(stderr, "standard input: read: %s\n", |
| 651 | strerror(errno)); |
| 652 | exit(1); |
| 653 | } |
| 654 | return; |
| 655 | } |
| 656 | } |
| 657 | break; |
| 658 | case FD_LISTENER: |
| 659 | if (FD_ISSET(fds[i].fd, &rfds)) { |
| 660 | /* |
| 661 | * New connection has come in. Accept it. |
| 662 | */ |
| 663 | struct fd *f; |
| 664 | struct sockaddr_in addr; |
| 665 | socklen_t addrlen = sizeof(addr); |
| 666 | int newfd = accept(fds[i].fd, (struct sockaddr *)&addr, |
| 667 | &addrlen); |
| 668 | if (newfd < 0) |
| 669 | break; /* not sure what happened there */ |
| 670 | |
| 671 | f = new_fdstruct(newfd, FD_CONNECTION); |
| 672 | f->cctx = new_connection(t); |
| 673 | if (authtype == HTTPD_AUTH_MAGIC) |
| 674 | check_magic_access(f); |
| 675 | } |
| 676 | break; |
| 677 | case FD_CONNECTION: |
| 678 | if (FD_ISSET(fds[i].fd, &rfds)) { |
| 679 | /* |
| 680 | * There's data to be read. |
| 681 | */ |
| 682 | char readbuf[4096]; |
| 683 | int ret; |
| 684 | |
| 685 | ret = read(fds[i].fd, readbuf, sizeof(readbuf)); |
| 686 | if (ret <= 0) { |
| 687 | /* |
| 688 | * This shouldn't happen in a sensible |
| 689 | * HTTP connection, so we abandon the |
| 690 | * connection if it does. |
| 691 | */ |
| 692 | close(fds[i].fd); |
| 693 | fds[i].deleted = 1; |
| 694 | break; |
| 695 | } else { |
| 696 | if (!fds[i].wdata) { |
| 697 | /* |
| 698 | * If we haven't got an HTTP response |
| 699 | * yet, keep processing data in the |
| 700 | * hope of acquiring one. |
| 701 | */ |
| 702 | fds[i].wdata = got_data |
| 703 | (fds[i].cctx, readbuf, ret, |
| 704 | (authtype == HTTPD_AUTH_NONE || |
| 705 | fds[i].magic_access), authstring, &cfg); |
| 706 | if (fds[i].wdata) { |
| 707 | fds[i].wdatalen = strlen(fds[i].wdata); |
| 708 | fds[i].wdatapos = 0; |
| 709 | } |
| 710 | } else { |
| 711 | /* |
| 712 | * Otherwise, just drop our read data |
| 713 | * on the floor. |
| 714 | */ |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | if (FD_ISSET(fds[i].fd, &wfds) && |
| 719 | fds[i].wdatapos < fds[i].wdatalen) { |
| 720 | /* |
| 721 | * The socket is writable, and we have data to |
| 722 | * write. Write it. |
| 723 | */ |
| 724 | int ret = write(fds[i].fd, fds[i].wdata + fds[i].wdatapos, |
| 725 | fds[i].wdatalen - fds[i].wdatapos); |
| 726 | if (ret <= 0) { |
| 727 | /* |
| 728 | * Shouldn't happen; abandon the connection. |
| 729 | */ |
| 730 | close(fds[i].fd); |
| 731 | fds[i].deleted = 1; |
| 732 | break; |
| 733 | } else { |
| 734 | fds[i].wdatapos += ret; |
| 735 | if (fds[i].wdatapos == fds[i].wdatalen) { |
| 736 | shutdown(fds[i].fd, SHUT_WR); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | } |
| 745 | } |