diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-17 19:18:47 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-17 19:18:47 +0000 |
commit | 241b1567adfa481e9c160f24d4ea7ca29ca7dd3e (patch) | |
tree | df6d3e3990ecf58be377b35f9809c2b8ae56a400 | |
parent | 1b97efd66aecdd79fa6501bc90aefe9998464347 (diff) | |
download | busybox-w32-241b1567adfa481e9c160f24d4ea7ca29ca7dd3e.tar.gz busybox-w32-241b1567adfa481e9c160f24d4ea7ca29ca7dd3e.tar.bz2 busybox-w32-241b1567adfa481e9c160f24d4ea7ca29ca7dd3e.zip |
httpd shrink and logging update, part 2 of 7
text data bss dec hex filename
9836 0 0 9836 266c busybox.t1/networking/httpd.o.orig
9724 0 0 9724 25fc busybox.t2/networking/httpd.o
9657 0 0 9657 25b9 busybox.t3/networking/httpd.o
9342 0 0 9342 247e busybox.t4/networking/httpd.o
9342 0 0 9342 247e busybox.t5/networking/httpd.o
9262 0 0 9262 242e busybox.t6/networking/httpd.o
9283 0 0 9283 2443 busybox.t7/networking/httpd.o
9334 0 0 9334 2476 busybox.t8/networking/httpd.o
-rw-r--r-- | networking/httpd.c | 174 |
1 files changed, 82 insertions, 92 deletions
diff --git a/networking/httpd.c b/networking/httpd.c index b44beaa8a..2ac19cf47 100644 --- a/networking/httpd.c +++ b/networking/httpd.c | |||
@@ -107,9 +107,9 @@ static const char httpd_conf[] ALIGN1 = "httpd.conf"; | |||
107 | #define TIMEOUT 60 | 107 | #define TIMEOUT 60 |
108 | 108 | ||
109 | // Note: busybox xfuncs are not used because we want the server to keep running | 109 | // Note: busybox xfuncs are not used because we want the server to keep running |
110 | // if something bad happens due to a malformed user request. | 110 | // if something bad happens due to a malformed user request. |
111 | // As a result, all memory allocation after daemonize | 111 | // As a result, all memory allocations after daemonize |
112 | // is checked rigorously | 112 | // are checked rigorously |
113 | 113 | ||
114 | //#define DEBUG 1 | 114 | //#define DEBUG 1 |
115 | #define DEBUG 0 | 115 | #define DEBUG 0 |
@@ -875,7 +875,7 @@ static int sendHeaders(HttpResponseNum responseNum) | |||
875 | found_mime_type : "text/html"; | 875 | found_mime_type : "text/html"; |
876 | 876 | ||
877 | if (verbose) | 877 | if (verbose) |
878 | write(2, iobuf, sprintf(iobuf, "%s response:%u\n", rmt_ip_str, responseNum)); | 878 | bb_error_msg("response:%u", responseNum); |
879 | 879 | ||
880 | /* emit the current date */ | 880 | /* emit the current date */ |
881 | strftime(tmp_str, sizeof(tmp_str), RFC1123FMT, gmtime(&timer)); | 881 | strftime(tmp_str, sizeof(tmp_str), RFC1123FMT, gmtime(&timer)); |
@@ -946,34 +946,34 @@ static int get_line(void) | |||
946 | } | 946 | } |
947 | 947 | ||
948 | #if ENABLE_FEATURE_HTTPD_CGI | 948 | #if ENABLE_FEATURE_HTTPD_CGI |
949 | /**************************************************************************** | 949 | /* |
950 | * | 950 | * Spawn CGI script, forward CGI's stdin/out <=> network |
951 | > $Function: sendCgi() | ||
952 | * | ||
953 | * $Description: Execute a CGI script and send it's stdout back | ||
954 | * | ||
955 | * Environment variables are set up and the script is invoked with pipes | ||
956 | * for stdin/stdout. If a post is being done the script is fed the POST | ||
957 | * data in addition to setting the QUERY_STRING variable (for GETs or POSTs). | ||
958 | * | ||
959 | * $Parameters: | ||
960 | * (const char *) url . . . . . . The requested URL (with leading /). | ||
961 | * (int bodyLen) . . . . . . . . Length of the post body. | ||
962 | * (const char *cookie) . . . . . For set HTTP_COOKIE. | ||
963 | * (const char *content_type) . . For set CONTENT_TYPE. | ||
964 | * | ||
965 | * $Return: (char *) . . . . A pointer to the decoded string (same as input). | ||
966 | * | 951 | * |
967 | * $Errors: None | 952 | * Environment variables are set up and the script is invoked with pipes |
953 | * for stdin/stdout. If a post is being done the script is fed the POST | ||
954 | * data in addition to setting the QUERY_STRING variable (for GETs or POSTs). | ||
968 | * | 955 | * |
969 | ****************************************************************************/ | 956 | * Parameters: |
970 | static int sendCgi(const char *url, | 957 | * const char *url The requested URL (with leading /). |
971 | const char *request, int bodyLen, const char *cookie, | 958 | * int bodyLen Length of the post body. |
959 | * const char *cookie For set HTTP_COOKIE. | ||
960 | * const char *content_type For set CONTENT_TYPE. | ||
961 | */ | ||
962 | static void send_cgi_and_exit( | ||
963 | const char *url, | ||
964 | const char *request, | ||
965 | int bodyLen, | ||
966 | const char *cookie, | ||
967 | const char *content_type) ATTRIBUTE_NORETURN; | ||
968 | static void send_cgi_and_exit( | ||
969 | const char *url, | ||
970 | const char *request, | ||
971 | int bodyLen, | ||
972 | const char *cookie, | ||
972 | const char *content_type) | 973 | const char *content_type) |
973 | { | 974 | { |
974 | struct { int rd; int wr; } fromCgi; /* CGI -> httpd pipe */ | 975 | struct { int rd; int wr; } fromCgi; /* CGI -> httpd pipe */ |
975 | struct { int rd; int wr; } toCgi; /* httpd -> CGI pipe */ | 976 | struct { int rd; int wr; } toCgi; /* httpd -> CGI pipe */ |
976 | char *fullpath; | ||
977 | char *argp[] = { NULL, NULL }; | 977 | char *argp[] = { NULL, NULL }; |
978 | int pid = 0; | 978 | int pid = 0; |
979 | int inFd; | 979 | int inFd; |
@@ -982,10 +982,8 @@ static int sendCgi(const char *url, | |||
982 | int status; | 982 | int status; |
983 | size_t post_read_size, post_read_idx; | 983 | size_t post_read_size, post_read_idx; |
984 | 984 | ||
985 | if (pipe(&fromCgi.rd) != 0) | 985 | xpipe(&fromCgi.rd); |
986 | return 0; | 986 | xpipe(&toCgi.rd); |
987 | if (pipe(&toCgi.rd) != 0) | ||
988 | return 0; | ||
989 | 987 | ||
990 | /* | 988 | /* |
991 | * Note: We can use vfork() here in the no-mmu case, although | 989 | * Note: We can use vfork() here in the no-mmu case, although |
@@ -995,27 +993,26 @@ static int sendCgi(const char *url, | |||
995 | * exits. This happens instantly after the child finishes, | 993 | * exits. This happens instantly after the child finishes, |
996 | * since httpd is run from inetd (and it can't run standalone | 994 | * since httpd is run from inetd (and it can't run standalone |
997 | * in uClinux). | 995 | * in uClinux). |
996 | * TODO: we can muck with environment _first_ and then fork/exec, | ||
997 | * that will be more understandable, and safer wrt vfork! | ||
998 | */ | 998 | */ |
999 | 999 | ||
1000 | // FIXME: setenv leaks memory! (old values of env vars are leaked) | ||
1001 | // Thus we have a bug on NOMMU. | ||
1002 | // Need to use this instead: | ||
1003 | // [malloc +] putenv + (in child: exec) + (in parent: unsetenv [+ free]) | ||
1004 | |||
1005 | #if !BB_MMU | 1000 | #if !BB_MMU |
1006 | fullpath = NULL; | ||
1007 | pid = vfork(); | 1001 | pid = vfork(); |
1008 | #else | 1002 | #else |
1009 | pid = fork(); | 1003 | pid = fork(); |
1010 | #endif | 1004 | #endif |
1011 | if (pid < 0) | 1005 | if (pid < 0) |
1012 | return 0; | 1006 | _exit(0); |
1013 | 1007 | ||
1014 | if (!pid) { | 1008 | if (!pid) { |
1015 | /* child process */ | 1009 | /* child process */ |
1010 | char *fullpath; | ||
1016 | char *script; | 1011 | char *script; |
1017 | char *purl; | 1012 | char *purl; |
1018 | 1013 | ||
1014 | xfunc_error_retval = 242; | ||
1015 | |||
1019 | if (accepted_socket > 1) | 1016 | if (accepted_socket > 1) |
1020 | close(accepted_socket); | 1017 | close(accepted_socket); |
1021 | if (server_socket > 1) | 1018 | if (server_socket > 1) |
@@ -1026,13 +1023,12 @@ static int sendCgi(const char *url, | |||
1026 | close(fromCgi.rd); | 1023 | close(fromCgi.rd); |
1027 | close(toCgi.wr); | 1024 | close(toCgi.wr); |
1028 | /* Huh? User seeing stderr can be a security problem. | 1025 | /* Huh? User seeing stderr can be a security problem. |
1029 | * If CGI really wants that, it can always dup2(1,2). */ | 1026 | * If CGI really wants that, it can always do dup itself. */ |
1030 | /* dup2(1, 2); */ | 1027 | /* dup2(1, 2); */ |
1031 | 1028 | ||
1032 | /* | 1029 | /* |
1033 | * Find PATH_INFO. | 1030 | * Find PATH_INFO. |
1034 | */ | 1031 | */ |
1035 | xfunc_error_retval = 242; | ||
1036 | purl = xstrdup(url); | 1032 | purl = xstrdup(url); |
1037 | script = purl; | 1033 | script = purl; |
1038 | while ((script = strchr(script + 1, '/')) != NULL) { | 1034 | while ((script = strchr(script + 1, '/')) != NULL) { |
@@ -1040,7 +1036,7 @@ static int sendCgi(const char *url, | |||
1040 | struct stat sb; | 1036 | struct stat sb; |
1041 | 1037 | ||
1042 | *script = '\0'; | 1038 | *script = '\0'; |
1043 | if (is_directory(purl + 1, 1, &sb) == 0) { | 1039 | if (!is_directory(purl + 1, 1, &sb)) { |
1044 | /* not directory, found script.cgi/PATH_INFO */ | 1040 | /* not directory, found script.cgi/PATH_INFO */ |
1045 | *script = '/'; | 1041 | *script = '/'; |
1046 | break; | 1042 | break; |
@@ -1050,10 +1046,7 @@ static int sendCgi(const char *url, | |||
1050 | setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */ | 1046 | setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */ |
1051 | setenv1("REQUEST_METHOD", request); | 1047 | setenv1("REQUEST_METHOD", request); |
1052 | if (g_query) { | 1048 | if (g_query) { |
1053 | char *uri = alloca(strlen(purl) + 2 + strlen(g_query)); | 1049 | putenv(xasprintf("%s=%s?%s", "REQUEST_URI", purl, g_query)); |
1054 | if (uri) | ||
1055 | sprintf(uri, "%s?%s", purl, g_query); | ||
1056 | setenv1("REQUEST_URI", uri); | ||
1057 | } else { | 1050 | } else { |
1058 | setenv1("REQUEST_URI", purl); | 1051 | setenv1("REQUEST_URI", purl); |
1059 | } | 1052 | } |
@@ -1082,7 +1075,7 @@ static int sendCgi(const char *url, | |||
1082 | * IOW - REMOTE_PEER="1.2.3.4:56" makes much more sense. | 1075 | * IOW - REMOTE_PEER="1.2.3.4:56" makes much more sense. |
1083 | * Oh well... */ | 1076 | * Oh well... */ |
1084 | { | 1077 | { |
1085 | char *p = rmt_ip_str ? : (char*)""; | 1078 | char *p = rmt_ip_str ? rmt_ip_str : (char*)""; |
1086 | char *cp = strrchr(p, ':'); | 1079 | char *cp = strrchr(p, ':'); |
1087 | if (ENABLE_FEATURE_IPV6 && cp && strchr(cp, ']')) | 1080 | if (ENABLE_FEATURE_IPV6 && cp && strchr(cp, ']')) |
1088 | cp = NULL; | 1081 | cp = NULL; |
@@ -1150,10 +1143,6 @@ static int sendCgi(const char *url, | |||
1150 | } /* end child */ | 1143 | } /* end child */ |
1151 | 1144 | ||
1152 | /* parent process */ | 1145 | /* parent process */ |
1153 | #if !BB_MMU | ||
1154 | free(fullpath); | ||
1155 | #endif | ||
1156 | |||
1157 | buf_count = 0; | 1146 | buf_count = 0; |
1158 | post_read_size = 0; | 1147 | post_read_size = 0; |
1159 | post_read_idx = 0; /* for gcc */ | 1148 | post_read_idx = 0; /* for gcc */ |
@@ -1306,23 +1295,15 @@ static int sendCgi(const char *url, | |||
1306 | fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf); | 1295 | fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf); |
1307 | } /* if (FD_ISSET(inFd)) */ | 1296 | } /* if (FD_ISSET(inFd)) */ |
1308 | } /* while (1) */ | 1297 | } /* while (1) */ |
1309 | return 0; | 1298 | _exit(0); |
1310 | } | 1299 | } |
1311 | #endif /* FEATURE_HTTPD_CGI */ | 1300 | #endif /* FEATURE_HTTPD_CGI */ |
1312 | 1301 | ||
1313 | /**************************************************************************** | 1302 | /* |
1314 | * | 1303 | * Send a file response to a HTTP request, and exit |
1315 | > $Function: sendFile() | 1304 | */ |
1316 | * | 1305 | static void send_file_and_exit(const char *url) ATTRIBUTE_NORETURN; |
1317 | * $Description: Send a file response to a HTTP request | 1306 | static void send_file_and_exit(const char *url) |
1318 | * | ||
1319 | * $Parameter: | ||
1320 | * (const char *) url . . The URL requested. | ||
1321 | * | ||
1322 | * $Return: (int) . . . . . . Always 0. | ||
1323 | * | ||
1324 | ****************************************************************************/ | ||
1325 | static int sendFile(const char *url) | ||
1326 | { | 1307 | { |
1327 | char *suffix; | 1308 | char *suffix; |
1328 | int f; | 1309 | int f; |
@@ -1365,13 +1346,18 @@ static int sendFile(const char *url) | |||
1365 | if (DEBUG) | 1346 | if (DEBUG) |
1366 | bb_perror_msg("cannot open '%s'", url); | 1347 | bb_perror_msg("cannot open '%s'", url); |
1367 | sendHeaders(HTTP_NOT_FOUND); | 1348 | sendHeaders(HTTP_NOT_FOUND); |
1368 | return 0; | 1349 | _exit(0); |
1369 | } | 1350 | } |
1370 | 1351 | ||
1371 | sendHeaders(HTTP_OK); | 1352 | sendHeaders(HTTP_OK); |
1372 | fd = accepted_socket; | 1353 | fd = accepted_socket; |
1373 | if (fd == 0) | 1354 | if (fd == 0) |
1374 | fd++; /* write to fd #1 in inetd mode */ | 1355 | fd++; /* write to fd #1 in inetd mode */ |
1356 | |||
1357 | /* If you want to know about EPIPEs below | ||
1358 | * (happen if you abort downloads from local httpd) */ | ||
1359 | /*signal(SIGPIPE, SIG_IGN);*/ | ||
1360 | |||
1375 | #if ENABLE_FEATURE_HTTPD_USE_SENDFILE | 1361 | #if ENABLE_FEATURE_HTTPD_USE_SENDFILE |
1376 | do { | 1362 | do { |
1377 | /* byte count is rounded down to 64k */ | 1363 | /* byte count is rounded down to 64k */ |
@@ -1379,20 +1365,26 @@ static int sendFile(const char *url) | |||
1379 | if (count < 0) { | 1365 | if (count < 0) { |
1380 | if (offset == 0) | 1366 | if (offset == 0) |
1381 | goto fallback; | 1367 | goto fallback; |
1382 | bb_perror_msg("sendfile '%s'", url); | 1368 | goto fin; |
1383 | } | 1369 | } |
1384 | } while (count > 0); | 1370 | } while (count > 0); |
1385 | close(f); | 1371 | _exit(0); |
1386 | return 0; | ||
1387 | 1372 | ||
1388 | fallback: | 1373 | fallback: |
1389 | #endif | 1374 | #endif |
1375 | /* TODO: why full_read? safe_read maybe? */ | ||
1390 | while ((count = full_read(f, iobuf, MAX_MEMORY_BUF)) > 0) { | 1376 | while ((count = full_read(f, iobuf, MAX_MEMORY_BUF)) > 0) { |
1391 | if (full_write(fd, iobuf, count) != count) | 1377 | ssize_t n = count; |
1378 | count = full_write(fd, iobuf, count); | ||
1379 | if (count != n) | ||
1392 | break; | 1380 | break; |
1393 | } | 1381 | } |
1394 | close(f); | 1382 | #if ENABLE_FEATURE_HTTPD_USE_SENDFILE |
1395 | return 0; | 1383 | fin: |
1384 | #endif | ||
1385 | if (count < 0) | ||
1386 | bb_perror_msg("error:%u", errno); | ||
1387 | _exit(0); | ||
1396 | } | 1388 | } |
1397 | 1389 | ||
1398 | static int checkPermIP(void) | 1390 | static int checkPermIP(void) |
@@ -1645,11 +1637,8 @@ static void handle_incoming_and_exit(void) | |||
1645 | found_moved_temporarily = url; | 1637 | found_moved_temporarily = url; |
1646 | } | 1638 | } |
1647 | } | 1639 | } |
1648 | if (verbose > 1) { | 1640 | if (verbose > 1) |
1649 | /* Hopefully does it with single write[v] */ | 1641 | bb_error_msg("url:%s", url); |
1650 | /* (uclibc does, glibc: ?) */ | ||
1651 | fdprintf(2, "%s url:%s\n", rmt_ip_str, url); | ||
1652 | } | ||
1653 | 1642 | ||
1654 | test = url; | 1643 | test = url; |
1655 | ip_allowed = checkPermIP(); | 1644 | ip_allowed = checkPermIP(); |
@@ -1671,8 +1660,7 @@ static void handle_incoming_and_exit(void) | |||
1671 | break; /* EOF or error or empty line */ | 1660 | break; /* EOF or error or empty line */ |
1672 | 1661 | ||
1673 | if (DEBUG) | 1662 | if (DEBUG) |
1674 | fprintf(stderr, "header: '%s'\n", iobuf); | 1663 | bb_error_msg("header: '%s'", iobuf); |
1675 | |||
1676 | #if ENABLE_FEATURE_HTTPD_CGI | 1664 | #if ENABLE_FEATURE_HTTPD_CGI |
1677 | /* try and do our best to parse more lines */ | 1665 | /* try and do our best to parse more lines */ |
1678 | if ((STRNCASECMP(iobuf, "Content-length:") == 0)) { | 1666 | if ((STRNCASECMP(iobuf, "Content-length:") == 0)) { |
@@ -1700,7 +1688,6 @@ static void handle_incoming_and_exit(void) | |||
1700 | user_agent = strdup(skip_whitespace(iobuf + sizeof("User-Agent:")-1)); | 1688 | user_agent = strdup(skip_whitespace(iobuf + sizeof("User-Agent:")-1)); |
1701 | } | 1689 | } |
1702 | #endif | 1690 | #endif |
1703 | |||
1704 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH | 1691 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
1705 | if (STRNCASECMP(iobuf, "Authorization:") == 0) { | 1692 | if (STRNCASECMP(iobuf, "Authorization:") == 0) { |
1706 | /* We only allow Basic credentials. | 1693 | /* We only allow Basic credentials. |
@@ -1716,7 +1703,6 @@ static void handle_incoming_and_exit(void) | |||
1716 | credentials = checkPerm(url, test); | 1703 | credentials = checkPerm(url, test); |
1717 | } | 1704 | } |
1718 | #endif /* FEATURE_HTTPD_BASIC_AUTH */ | 1705 | #endif /* FEATURE_HTTPD_BASIC_AUTH */ |
1719 | |||
1720 | } /* while extra header reading */ | 1706 | } /* while extra header reading */ |
1721 | } | 1707 | } |
1722 | alarm(0); | 1708 | alarm(0); |
@@ -1752,8 +1738,7 @@ static void handle_incoming_and_exit(void) | |||
1752 | if (strncmp(test, "cgi-bin", 7) == 0) { | 1738 | if (strncmp(test, "cgi-bin", 7) == 0) { |
1753 | if (test[7] == '/' && test[8] == '\0') | 1739 | if (test[7] == '/' && test[8] == '\0') |
1754 | goto FORBIDDEN; /* protect listing cgi-bin/ */ | 1740 | goto FORBIDDEN; /* protect listing cgi-bin/ */ |
1755 | sendCgi(url, prequest, length, cookie, content_type); | 1741 | send_cgi_and_exit(url, prequest, length, cookie, content_type); |
1756 | break; | ||
1757 | } | 1742 | } |
1758 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR | 1743 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
1759 | { | 1744 | { |
@@ -1762,8 +1747,7 @@ static void handle_incoming_and_exit(void) | |||
1762 | Htaccess *cur; | 1747 | Htaccess *cur; |
1763 | for (cur = script_i; cur; cur = cur->next) { | 1748 | for (cur = script_i; cur; cur = cur->next) { |
1764 | if (strcmp(cur->before_colon + 1, suffix) == 0) { | 1749 | if (strcmp(cur->before_colon + 1, suffix) == 0) { |
1765 | sendCgi(url, prequest, length, cookie, content_type); | 1750 | send_cgi_and_exit(url, prequest, length, cookie, content_type); |
1766 | goto bail_out; | ||
1767 | } | 1751 | } |
1768 | } | 1752 | } |
1769 | } | 1753 | } |
@@ -1788,20 +1772,18 @@ static void handle_incoming_and_exit(void) | |||
1788 | if (access("/cgi-bin/index.cgi"+1, X_OK) == 0) { | 1772 | if (access("/cgi-bin/index.cgi"+1, X_OK) == 0) { |
1789 | purl[0] = '\0'; | 1773 | purl[0] = '\0'; |
1790 | g_query = url; | 1774 | g_query = url; |
1791 | sendCgi("/cgi-bin/index.cgi", prequest, length, cookie, content_type); | 1775 | send_cgi_and_exit("/cgi-bin/index.cgi", prequest, length, cookie, content_type); |
1792 | break; | ||
1793 | } | 1776 | } |
1794 | } | 1777 | } |
1795 | #endif /* FEATURE_HTTPD_CGI */ | 1778 | #endif /* FEATURE_HTTPD_CGI */ |
1796 | sendFile(test); | 1779 | send_file_and_exit(test); |
1797 | ContentLength = -1; | ||
1798 | } while (0); | 1780 | } while (0); |
1799 | 1781 | ||
1800 | #if ENABLE_FEATURE_HTTPD_CGI | 1782 | #if ENABLE_FEATURE_HTTPD_CGI |
1801 | bail_out: | 1783 | bail_out: |
1802 | #endif | 1784 | #endif |
1803 | 1785 | ||
1804 | exit(0); | 1786 | _exit(0); |
1805 | 1787 | ||
1806 | #if 0 /* Is this needed? Why? */ | 1788 | #if 0 /* Is this needed? Why? */ |
1807 | if (DEBUG) | 1789 | if (DEBUG) |
@@ -1830,7 +1812,7 @@ static void handle_incoming_and_exit(void) | |||
1830 | } while (retval > 0 && read(accepted_socket, iobuf, sizeof(iobuf) > 0)); | 1812 | } while (retval > 0 && read(accepted_socket, iobuf, sizeof(iobuf) > 0)); |
1831 | shutdown(accepted_socket, SHUT_RD); | 1813 | shutdown(accepted_socket, SHUT_RD); |
1832 | close(accepted_socket); | 1814 | close(accepted_socket); |
1833 | exit(0); | 1815 | _exit(0); |
1834 | #endif | 1816 | #endif |
1835 | } | 1817 | } |
1836 | 1818 | ||
@@ -1844,6 +1826,7 @@ static void handle_incoming_and_exit(void) | |||
1844 | static void mini_httpd(int server) ATTRIBUTE_NORETURN; | 1826 | static void mini_httpd(int server) ATTRIBUTE_NORETURN; |
1845 | static void mini_httpd(int server) | 1827 | static void mini_httpd(int server) |
1846 | { | 1828 | { |
1829 | // TODO: use accept WITHOUT select, it will just block there | ||
1847 | fd_set readfd, portfd; | 1830 | fd_set readfd, portfd; |
1848 | 1831 | ||
1849 | FD_ZERO(&portfd); | 1832 | FD_ZERO(&portfd); |
@@ -1857,9 +1840,10 @@ static void mini_httpd(int server) | |||
1857 | struct sockaddr_in sin; | 1840 | struct sockaddr_in sin; |
1858 | USE_FEATURE_IPV6(struct sockaddr_in6 sin6;) | 1841 | USE_FEATURE_IPV6(struct sockaddr_in6 sin6;) |
1859 | } fromAddr; | 1842 | } fromAddr; |
1843 | // TODO: this looks like lsa to me | ||
1860 | socklen_t fromAddrLen = sizeof(fromAddr); | 1844 | socklen_t fromAddrLen = sizeof(fromAddr); |
1861 | 1845 | ||
1862 | /* Now wait INDEFINITELY on the set of sockets! */ | 1846 | /* Now wait INDEFINITELY on the set of sockets */ |
1863 | readfd = portfd; | 1847 | readfd = portfd; |
1864 | if (select(server + 1, &readfd, 0, 0, 0) <= 0) | 1848 | if (select(server + 1, &readfd, 0, 0, 0) <= 0) |
1865 | continue; | 1849 | continue; |
@@ -1871,7 +1855,7 @@ static void mini_httpd(int server) | |||
1871 | accepted_socket = s; | 1855 | accepted_socket = s; |
1872 | rmt_ip = 0; | 1856 | rmt_ip = 0; |
1873 | tcp_port = 0; | 1857 | tcp_port = 0; |
1874 | if (verbose || ENABLE_FEATURE_HTTPD_CGI || DEBUG) { | 1858 | if (ENABLE_FEATURE_HTTPD_CGI || DEBUG || verbose) { |
1875 | free(rmt_ip_str); | 1859 | free(rmt_ip_str); |
1876 | rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddrLen); | 1860 | rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddrLen); |
1877 | if (DEBUG) | 1861 | if (DEBUG) |
@@ -1879,6 +1863,7 @@ static void mini_httpd(int server) | |||
1879 | } | 1863 | } |
1880 | if (fromAddr.sa.sa_family == AF_INET) { | 1864 | if (fromAddr.sa.sa_family == AF_INET) { |
1881 | rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); | 1865 | rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); |
1866 | // TODO: use get_nport? | ||
1882 | tcp_port = ntohs(fromAddr.sin.sin_port); | 1867 | tcp_port = ntohs(fromAddr.sin.sin_port); |
1883 | } | 1868 | } |
1884 | #if ENABLE_FEATURE_IPV6 | 1869 | #if ENABLE_FEATURE_IPV6 |
@@ -1897,11 +1882,15 @@ static void mini_httpd(int server) | |||
1897 | /* Do not reload config on HUP */ | 1882 | /* Do not reload config on HUP */ |
1898 | signal(SIGHUP, SIG_IGN); | 1883 | signal(SIGHUP, SIG_IGN); |
1899 | #endif | 1884 | #endif |
1885 | if (verbose) { | ||
1886 | /* this trick makes -v logging much simpler */ | ||
1887 | applet_name = rmt_ip_str; | ||
1888 | } | ||
1900 | handle_incoming_and_exit(); | 1889 | handle_incoming_and_exit(); |
1901 | } | 1890 | } |
1902 | close(s); | 1891 | close(s); |
1903 | } /* while (1) */ | 1892 | } /* while (1) */ |
1904 | /* return 0; - never reached */ | 1893 | /* never reached */ |
1905 | } | 1894 | } |
1906 | #endif | 1895 | #endif |
1907 | 1896 | ||
@@ -1914,6 +1903,7 @@ static void mini_httpd_inetd(void) | |||
1914 | struct sockaddr_in sin; | 1903 | struct sockaddr_in sin; |
1915 | USE_FEATURE_IPV6(struct sockaddr_in6 sin6;) | 1904 | USE_FEATURE_IPV6(struct sockaddr_in6 sin6;) |
1916 | } fromAddr; | 1905 | } fromAddr; |
1906 | // TODO: this looks like lsa to me | ||
1917 | socklen_t fromAddrLen = sizeof(fromAddr); | 1907 | socklen_t fromAddrLen = sizeof(fromAddr); |
1918 | 1908 | ||
1919 | getpeername(0, &fromAddr.sa, &fromAddrLen); | 1909 | getpeername(0, &fromAddr.sa, &fromAddrLen); |