Lines Matching defs:httpd

36  * bozohttpd.c:  minimal httpd; provides only these features:
193 size_arrays(bozohttpd_t *httpd, bozoprefs_t *bozoprefs, size_t needed)
199 bozoprefs->name = bozomalloc(httpd, len);
200 bozoprefs->value = bozomalloc(httpd, len);
203 bozoprefs->name = bozorealloc(httpd, bozoprefs->name, len);
204 bozoprefs->value = bozorealloc(httpd, bozoprefs->value, len);
223 bozo_set_pref(bozohttpd_t *httpd, bozoprefs_t *bozoprefs,
230 if (!size_arrays(httpd, bozoprefs, bozoprefs->size + 15))
233 bozoprefs->name[i] = bozostrdup(httpd, NULL, name);
238 bozoprefs->value[i] = bozostrdup(httpd, NULL, value);
287 parse_request(bozohttpd_t *httpd, char *in, char **method, char **file,
293 USE_ARG(httpd);
294 debug((httpd, DEBUG_EXPLODING, "parse in: %s", in));
327 *file = bozostrdup(httpd, NULL, *file);
329 *query = bozostrdup(httpd, NULL, *query);
331 debug((httpd, DEBUG_FAT,
402 bozo_set_timeout(bozohttpd_t *httpd, bozoprefs_t *prefs,
418 bozo_set_pref(httpd, prefs, *cur, val);
438 struct bozohttpd_t *httpd = request->hr_httpd;
459 bozoasprintf(httpd, &nval, "%s, %s", hdr->h_value, str);
465 hdr = bozomalloc(httpd, sizeof *hdr);
466 hdr->h_header = bozostrdup(httpd, request, val);
468 hdr->h_value = bozostrdup(httpd, request, str);
470 hdr->h_value = bozostrdup(httpd, request, " ");
503 struct bozohttpd_t *httpd = request->hr_httpd;
509 request->hr_proto = httpd->consts.http_09;
510 debug((httpd, DEBUG_FAT, "request %s is http/0.9",
534 request->hr_proto = httpd->consts.http_10;
536 request->hr_proto = httpd->consts.http_11;
540 debug((httpd, DEBUG_FAT, "request %s is %s",
547 return bozo_http_error(httpd, 404, NULL, "unknown prototype");
574 struct bozohttpd_t *httpd = request->hr_httpd;
577 if (request->hr_proto == httpd->consts.http_11)
587 return bozo_http_error(httpd, 404, request, "unknown method");
609 bozo_read_request(bozohttpd_t *httpd)
628 if (bozo_daemon_fork(httpd))
631 request = bozomalloc(httpd, sizeof(*request));
633 request->hr_httpd = httpd;
655 if (httpd->numeric == 0 &&
663 request->hr_remotehost = bozostrdup(httpd, request, host);
665 request->hr_remoteaddr = bozostrdup(httpd, request, addr);
673 if (httpd->bindport) {
674 if (strcmp(httpd->bindport, BOZO_HTTP_PORT) != 0)
675 port = httpd->bindport;
681 request->hr_serverport = bozostrdup(httpd, request, port);
693 bozo_http_error(httpd, 500, NULL, "clock_gettime failed");
702 if (bozo_ssl_accept(httpd))
705 alarm(httpd->initial_timeout);
706 while ((str = bozodgetln(httpd, STDIN_FILENO, &len, bozo_read)) != NULL) {
710 bozo_http_error(httpd, 500, NULL, "clock_gettime failed");
725 ts.tv_sec > httpd->request_timeout &&
726 ts.tv_sec - httpd->request_timeout > ots.tv_sec)
730 bozo_http_error(httpd, 408, NULL, "request timed out");
737 bozo_http_error(httpd, 404, NULL, "null method");
740 bozowarn(httpd,
747 parse_request(httpd, str, &method, &file, &query, &proto);
751 bozo_http_error(httpd, 404, NULL, "null method");
755 bozo_http_error(httpd, 404, NULL, "null file");
769 debug((httpd, DEBUG_FAT, "got file \"%s\" query \"%s\"",
774 if (request->hr_proto == httpd->consts.http_09)
783 debug((httpd, DEBUG_EXPLODING, "read_req2: after "
787 bozo_http_error(httpd, 404, request, "no header");
791 bozo_http_error(httpd, 404, request,
815 bozo_http_error(httpd, 400, request,
819 request->hr_host = bozostrdup(httpd, request,
824 bozo_http_error(httpd, 417, request,
840 debug((httpd, DEBUG_FAT, "adding header %s: %s",
844 alarm(httpd->header_timeout);
847 bozo_http_error(httpd, 413, request, "request too large");
858 bozo_http_error(httpd, 400, request, "missing content length");
863 if (request->hr_proto == httpd->consts.http_11 &&
866 bozo_http_error(httpd, 400, request, "missing Host header");
871 debug((httpd, DEBUG_FAT, "hr_range: %s", request->hr_range));
898 debug((httpd, DEBUG_FAT, "bozo_read_request returns url %s in request",
909 mmap_and_write_part(bozohttpd_t *httpd, int fd, off_t first_byte_pos, size_t sz)
930 mappedoffset = first_byte_pos & ~((off_t)httpd->page_size - 1);
932 (first_byte_pos - mappedoffset + sz + httpd->page_size - 1) &
933 ~(httpd->page_size - 1);
938 bozowarn(httpd, "mmap failed: %s", strerror(errno));
947 if (bozo_write(httpd, STDOUT_FILENO, addr + wroffset,
949 bozowarn(httpd, "write failed: %s", strerror(errno));
952 debug((httpd, DEBUG_OBESE, "wrote %d bytes", BOZO_WRSZ));
956 if (sz && (size_t)bozo_write(httpd, STDOUT_FILENO, addr + wroffset,
958 bozowarn(httpd, "final write failed: %s", strerror(errno));
961 debug((httpd, DEBUG_OBESE, "wrote %d bytes", (int)sz));
964 bozowarn(httpd, "munmap failed");
996 bozo_escape_rfc3986(bozohttpd_t *httpd, const char *url, int absolute)
1007 buf = bozorealloc(httpd, buf, buflen);
1061 bozohttpd_t *httpd = request->hr_httpd;
1068 const char *hostname = BOZOHOST(httpd, request);
1072 bozoasprintf(httpd, &urlbuf, "/%s/", request->hr_file);
1079 bozoasprintf(httpd, &userbuf, "/~%s%s", request->hr_user, url);
1116 scheme = absproto ? "" : httpd->sslinfo ? "https://" : "http://";
1122 const char *defport = httpd->sslinfo ? BOZO_HTTPS_PORT : BOZO_HTTP_PORT;
1132 url = bozo_escape_rfc3986(httpd, url, absolute);
1141 bozoasprintf(httpd, &finalurl, "%s%s%s%s%s%s",
1144 bozowarn(httpd, "redirecting %s", finalurl);
1145 debug((httpd, DEBUG_FAT, "redirecting %s", finalurl));
1147 bozo_printf(httpd, "%s 301 Document Moved\r\n", request->hr_proto);
1148 if (request->hr_proto != httpd->consts.http_09)
1150 if (request->hr_proto != httpd->consts.http_09)
1151 bozo_printf(httpd, "Location: %s\r\n", finalurl);
1152 bozo_printf(httpd, "\r\n");
1155 bozo_printf(httpd, "<html><head><title>Document Moved</title></head>\n");
1156 bozo_printf(httpd, "<body><h1>Document Moved</h1>\n");
1157 bozo_printf(httpd, "This document had moved <a href=\"%s\">here</a>\n",
1159 bozo_printf(httpd, "</body></html>\n");
1161 bozo_flush(httpd, stdout);
1204 bozohttpd_t *httpd = request->hr_httpd;
1216 debug((httpd, DEBUG_FAT, "remap file found"));
1218 bozowarn(httpd, "could not stat " REMAP_FILE ", errno: %d",
1225 bozowarn(httpd, "could not mmap " REMAP_FILE ", error %d",
1302 newfile = bozomalloc(httpd, strlen(file) + rlen - len + 1);
1305 debug((httpd, DEBUG_NORMAL, "remapping found '%s'",
1318 * if we have a virtual path root (httpd->virtbase), and we are given a
1320 * directory exists under httpd->virtbase. if it does, use this as the
1326 bozohttpd_t *httpd = request->hr_httpd;
1333 debug((httpd, DEBUG_OBESE,
1340 request->hr_host = bozostrdup(httpd, request, file);
1346 bozostrdup(httpd, request, s ? s : "/");
1347 debug((httpd, DEBUG_OBESE, "got host '%s' file is now '%s'",
1362 if (!httpd->virtbase) {
1376 request->hr_virthostname = bozostrdup(httpd, request,
1391 debug((httpd, DEBUG_OBESE,
1392 "check_virtual: checking host `%s' under httpd->virtbase `%s' "
1394 request->hr_host, httpd->virtbase, request->hr_file));
1395 if (strncasecmp(httpd->virthostname, request->hr_host, len) != 0) {
1400 if ((dirp = opendir(httpd->virtbase)) != NULL) {
1406 debug((httpd, DEBUG_OBESE, "looking at dir '%s'",
1410 debug((httpd, DEBUG_OBESE, "found it punch it"));
1412 bozostrdup(httpd, request, d->d_name);
1413 bozoasprintf(httpd, &s, "%s/%s",
1414 httpd->virtbase,
1422 debug((httpd, DEBUG_FAT, "opendir %s failed: %s",
1423 httpd->virtbase, strerror(errno)));
1426 if (httpd->unknown_slash)
1428 return bozo_http_error(httpd, 404, request,
1433 s = httpd->slashdir;
1439 return bozo_http_error(httpd, 404, request,
1458 bozohttpd_t *httpd = request->hr_httpd;
1471 bozo_http_error(httpd, 404, request, "file path too long");
1474 debug((httpd, DEBUG_FAT, "check_bzredirect: dir %s", dir));
1492 debug((httpd, DEBUG_FAT, "check_bzredirect: path %s", path));
1496 return bozo_http_error(httpd, 404, request,
1506 bozo_http_error(httpd, 404, request,
1514 debug((httpd, DEBUG_FAT, "check_bzredirect: calling readlink"));
1517 debug((httpd, DEBUG_FAT, "readlink failed"));
1521 debug((httpd, DEBUG_FAT, "readlink returned \"%s\"", redirpath));
1532 bozo_http_error(httpd, 404, request,
1539 debug((httpd, DEBUG_FAT, "check_bzredirect: new redir %s", finalredir));
1548 bozohttpd_t *httpd = request->hr_httpd;
1561 debug((httpd, DEBUG_EXPLODING,
1568 debug((httpd, DEBUG_EXPLODING,
1572 debug((httpd, DEBUG_EXPLODING,
1576 return bozo_http_error(httpd, 400, request,
1579 return bozo_http_error(httpd, 404, request,
1582 return bozo_http_error(httpd, 404, request,
1590 debug((httpd, DEBUG_EXPLODING,
1593 return bozo_http_error(httpd, 400, request,
1604 debug((httpd, DEBUG_FAT, "bozo_decode_url_percent returns `%s'",
1628 bozohttpd_t *httpd = request->hr_httpd;
1634 debug((httpd, DEBUG_FAT, "tf_req: file %s", request->hr_file));
1643 bozo_http_error(httpd, 404, request, "unknown URL");
1658 if (len > 1 && httpd->enable_users && file[1] == '~') {
1660 bozo_http_error(httpd, 404, request,
1667 bozoasprintf(httpd, &userredirecturl, "%s/", file);
1672 debug((httpd, DEBUG_FAT, "calling bozo_user_transform"));
1693 debug((httpd, DEBUG_FAT, "file[len-1] == %c", file[len-1]));
1696 debug((httpd, DEBUG_FAT, "appending index.html"));
1697 newfile = bozomalloc(httpd,
1698 len + strlen(httpd->index_html) + 1);
1700 strcat(newfile, httpd->index_html);
1702 newfile = bozostrdup(httpd, request, file + 1);
1704 debug((httpd, DEBUG_EXPLODING, "tf_req: len == 1"));
1705 newfile = bozostrdup(httpd, request, httpd->index_html);
1708 bozo_http_error(httpd, 500, request, "request->hr_file is nul");
1713 bozo_http_error(httpd, 500, request, "internal failure");
1724 debug((httpd, DEBUG_FAT, "newfile: %s", newfile));
1728 bozo_http_error(httpd, 403, request, "illegal request");
1744 debug((httpd, DEBUG_FAT, "transform_request set: %s", newfile));
1748 debug((httpd, DEBUG_FAT, "transform_request returning: 0"));
1810 bozohttpd_t *httpd = request->hr_httpd;
1828 bozoasprintf(httpd, &file, "%s.gz", request->hr_file);
1841 debug((httpd, DEBUG_FAT, "open failed: %s", strerror(errno)));
1845 bozo_http_error(httpd, 403, request,
1852 bozo_http_error(httpd, 404, request, "no file");
1855 bozo_http_error(httpd, 500, request, "open file");
1860 bozo_http_error(httpd, 500, request, "can't fstat");
1872 bozo_printf(httpd, "%s 304 Not Modified\r\n",
1874 bozo_printf(httpd, "\r\n");
1875 bozo_flush(httpd, stdout);
1889 debug((httpd, DEBUG_FAT, "have_range %d first_pos %lld last_pos %lld",
1894 bozo_printf(httpd, "%s 206 Partial Content\r\n",
1897 bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto);
1899 if (request->hr_proto != httpd->consts.http_09) {
1905 bozo_printf(httpd, "\r\n");
1907 bozo_flush(httpd, stdout);
1920 if ((off_t)httpd->mmapsz < szleft)
1921 sz = httpd->mmapsz;
1924 if (mmap_and_write_part(httpd, fd, cur_byte_pos, sz)) {
1926 httpd->mmapsz /= 2;
1927 if (httpd->mmapsz >= httpd->page_size)
1950 bozohttpd_t *httpd = request->hr_httpd;
1957 error = bozo_http_error(httpd, 403, request,
1973 bozohttpd_t *httpd = request->hr_httpd;
1979 bozo_printf(httpd, "%s: %s\r\n", hdr->h_header,
1983 bozo_printf(httpd, "Date: %s\r\n", bozo_http_date(date, sizeof(date)));
1984 bozo_printf(httpd, "Server: %s\r\n", httpd->server_software);
1985 bozo_printf(httpd, "Accept-Ranges: bytes\r\n");
1993 bozo_printf(httpd, "Last-Modified: %s\r\n", filedate);
1996 bozo_printf(httpd, "Content-Type: %s\r\n", type);
1998 bozo_printf(httpd, "Content-Encoding: %s\r\n", encoding);
2003 bozo_printf(httpd,
2010 bozo_printf(httpd, "Content-Length: %qd\r\n", (long long)len);
2012 if (request->hr_proto == httpd->consts.http_11)
2013 bozo_printf(httpd, "Connection: close\r\n");
2014 bozo_flush(httpd, stdout);
2019 debug__(bozohttpd_t *httpd, int level, const char *fmt, ...)
2025 if (httpd->debug < level)
2030 if (!httpd->nolog) {
2031 if (httpd->logstderr) {
2044 bozowarn(bozohttpd_t *httpd, const char *fmt, ...)
2049 if (!httpd->nolog) {
2050 if (httpd->logstderr || isatty(STDERR_FILENO)) {
2061 bozoerr(bozohttpd_t *httpd, int code, const char *fmt, ...)
2066 if (!httpd->nolog) {
2067 if (httpd->logstderr || isatty(STDERR_FILENO)) {
2079 bozoasprintf(bozohttpd_t *httpd, char **str, const char *fmt, ...)
2089 bozoerr(httpd, EXIT_FAILURE, "asprintf");
2096 * call with NULL httpd in error paths, to avoid recursive
2097 * malloc failure. call with valid httpd in normal paths
2101 bozo_escape_html(bozohttpd_t *httpd, const char *url)
2130 if (httpd)
2131 tmp = bozomalloc(httpd, len);
2225 bozo_http_error(bozohttpd_t *httpd, int code, bozo_httpreq_t *request,
2232 request->hr_proto : httpd->consts.http_11;
2238 debug((httpd, DEBUG_FAT, "bozo_http_error %d: %s", code, msg));
2240 bozoerr(httpd, 1,
2256 const char *hostname = BOZOHOST(httpd, request);
2273 bozoasprintf(httpd, &user, "~%s/", user_escaped);
2279 size = snprintf(httpd->errorbuf, BOZO_MINBUFSIZE,
2290 bozowarn(httpd,
2300 bozo_printf(httpd, "%s %s\r\n", proto, header);
2305 bozo_printf(httpd, "%s: %s\r\n", hdr->h_header,
2310 bozo_printf(httpd, "Content-Type: text/html\r\n");
2311 bozo_printf(httpd, "Content-Length: %d\r\n", size);
2312 bozo_printf(httpd, "Server: %s\r\n", httpd->server_software);
2314 bozo_printf(httpd, "Allow: %s\r\n", request->hr_allow);
2317 request->hr_proto == httpd->consts.http_11)
2318 bozo_printf(httpd, "Connection: close\r\n");
2319 bozo_printf(httpd, "\r\n");
2323 bozo_printf(httpd, "%s", httpd->errorbuf);
2324 bozo_flush(httpd, stdout);
2387 bozodgetln(bozohttpd_t *httpd, int fd, ssize_t *lenp,
2395 if (httpd->getln_buflen == 0) {
2397 httpd->getln_buflen = 128;
2398 httpd->getln_buffer =
2399 bozomalloc(httpd, (size_t)httpd->getln_buflen);
2412 for (; readfn(httpd, fd, &c, 1) == 1; ) {
2413 debug((httpd, DEBUG_EXPLODING, "bozodgetln read %c", c));
2415 if (httpd->getln_buflen > BOZO_HEADERS_MAX_SIZE)
2418 if (len >= httpd->getln_buflen - 1) {
2419 httpd->getln_buflen *= 2;
2420 debug((httpd, DEBUG_EXPLODING, "bozodgetln: "
2422 httpd->getln_buflen));
2423 nbuffer = bozorealloc(httpd, httpd->getln_buffer,
2424 (size_t)httpd->getln_buflen);
2425 httpd->getln_buffer = nbuffer;
2428 httpd->getln_buffer[len++] = c;
2448 httpd->getln_buffer[len] = '\0';
2449 debug((httpd, DEBUG_OBESE, "bozodgetln returns: '%s' with len %zd",
2450 httpd->getln_buffer, len));
2452 return httpd->getln_buffer;
2458 * note that these may access members of the httpd and/or request.
2461 bozorealloc(bozohttpd_t *httpd, void *ptr, size_t size)
2469 bozo_http_error(httpd, 500, NULL, "memory allocation failure");
2474 bozomalloc(bozohttpd_t *httpd, size_t size)
2482 bozo_http_error(httpd, 500, NULL, "memory allocation failure");
2487 bozostrdup(bozohttpd_t *httpd, bozo_httpreq_t *request, const char *str)
2496 bozoerr(httpd, EXIT_FAILURE, "strdup");
2498 bozo_http_error(httpd, 500, request, "memory allocation failure");
2504 bozo_init_httpd(bozohttpd_t *httpd)
2507 (void) memset(httpd, 0x0, sizeof(*httpd));
2510 httpd->consts.http_09 = "HTTP/0.9";
2511 httpd->consts.http_10 = "HTTP/1.0";
2512 httpd->consts.http_11 = "HTTP/1.1";
2513 httpd->consts.text_plain = "text/plain";
2516 httpd->mmapsz = BOZO_MMAPSZ;
2519 if ((httpd->errorbuf = malloc(BOZO_MINBUFSIZE)) == NULL) {
2525 SIMPLEQ_INIT(&httpd->lua_states);
2532 bozo_init_prefs(bozohttpd_t *httpd, bozoprefs_t *prefs)
2540 if (!bozo_set_pref(httpd, prefs, "server software", SERVER_SOFTWARE))
2542 if (!bozo_set_pref(httpd, prefs, "index.html", INDEX_HTML))
2544 if (!bozo_set_pref(httpd, prefs, "public_html", PUBLIC_HTML))
2546 if (!bozo_set_pref(httpd, prefs, "ssl timeout", SSL_TIMEOUT))
2548 if (!bozo_set_pref(httpd, prefs, "initial timeout", INITIAL_TIMEOUT))
2550 if (!bozo_set_pref(httpd, prefs, "header timeout", HEADER_WAIT_TIME))
2552 if (!bozo_set_pref(httpd, prefs, "request timeout", TOTAL_MAX_REQ_TIME))
2560 bozo_set_defaults(bozohttpd_t *httpd, bozoprefs_t *prefs)
2562 return bozo_init_httpd(httpd) && bozo_init_prefs(httpd, prefs);
2567 bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
2584 httpd->virthostname = bozomalloc(httpd, MAXHOSTNAMELEN+1);
2585 if (gethostname(httpd->virthostname, MAXHOSTNAMELEN+1) < 0)
2586 bozoerr(httpd, 1, "gethostname");
2587 httpd->virthostname[MAXHOSTNAMELEN] = '\0';
2589 httpd->virthostname = bozostrdup(httpd, NULL, vhost);
2591 httpd->slashdir = bozostrdup(httpd, NULL, root);
2593 httpd->bindport = bozostrdup(httpd, NULL, portnum);
2599 httpd->numeric = 1;
2603 httpd->logstderr = 1;
2607 httpd->nolog = 1;
2610 httpd->bindaddress = bozostrdup(httpd, NULL, cp);
2613 httpd->background = atoi(cp);
2617 httpd->foreground = 1;
2620 httpd->pidfile = bozostrdup(httpd, NULL, cp);
2624 httpd->unknown_slash = 1;
2627 httpd->virtbase = bozostrdup(httpd, NULL, cp);
2631 httpd->enable_users = 1;
2635 httpd->enable_cgi_users = 1;
2643 httpd->hide_dots = 1;
2647 httpd->dir_indexing = 1;
2650 httpd->dir_readme = bozostrdup(httpd, NULL, cp);
2653 httpd->public_html = bozostrdup(httpd, NULL, cp);
2656 httpd->ssl_timeout = atoi(cp);
2659 httpd->initial_timeout = atoi(cp);
2662 httpd->header_timeout = atoi(cp);
2665 httpd->request_timeout = atoi(cp);
2667 httpd->server_software =
2668 bozostrdup(httpd, NULL, bozo_get_pref(prefs, "server software"));
2669 httpd->index_html =
2670 bozostrdup(httpd, NULL, bozo_get_pref(prefs, "index.html"));
2675 bozo_ssl_init(httpd);
2676 bozo_daemon_init(httpd);
2681 bozoerr(httpd, 1, "getpwnam(%s): %s", username,
2684 bozoerr(httpd, 1, "initgroups: %s", strerror(errno));
2686 bozoerr(httpd, 1, "setgid(%u): %s", pw->pw_gid,
2695 httpd->rootdir = bozostrdup(httpd, NULL, chrootdir);
2696 if (chdir(httpd->rootdir) == -1)
2697 bozoerr(httpd, 1, "chdir(%s): %s", httpd->rootdir,
2699 if (chroot(httpd->rootdir) == -1)
2700 bozoerr(httpd, 1, "chroot(%s): %s", httpd->rootdir,
2705 bozoerr(httpd, 1, "setuid(%d): %s", uid, strerror(errno));
2717 httpd->page_size = (long)sysconf(_SC_PAGESIZE);
2719 httpd->page_size = 4096;
2721 debug((httpd, DEBUG_OBESE, "myname is %s, slashdir is %s",
2722 httpd->virthostname, httpd->slashdir));
2728 bozo_cleanup(bozohttpd_t *httpd, bozoprefs_t *prefs)
2732 free(httpd->virthostname);
2733 free(httpd->errorbuf);
2734 free(httpd->getln_buffer);
2735 free(httpd->slashdir);
2736 free(httpd->bindport);
2737 free(httpd->pidfile);
2738 free(httpd->cgibin);
2739 free(httpd->virtbase);
2740 free(httpd->dynamic_content_map);
2742 free(bozo_unconst(httpd->server_software));
2743 free(bozo_unconst(httpd->index_html));
2744 free(bozo_unconst(httpd->dir_readme));
2745 free(bozo_unconst(httpd->public_html));