aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-08-17 19:20:39 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-08-17 19:20:39 +0000
commitfeac3ce8c00cde26cf081ec7a8566498364d834b (patch)
tree992719fd299aacfff92d7db88ca07897ae5f8917
parent073214f89409a8b2a01bae70f7ce699944b2a3be (diff)
downloadbusybox-w32-feac3ce8c00cde26cf081ec7a8566498364d834b.tar.gz
busybox-w32-feac3ce8c00cde26cf081ec7a8566498364d834b.tar.bz2
busybox-w32-feac3ce8c00cde26cf081ec7a8566498364d834b.zip
httpd shrink and logging update, part 6 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.c75
1 files changed, 46 insertions, 29 deletions
diff --git a/networking/httpd.c b/networking/httpd.c
index 81660b2e6..32ecde0b8 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -299,7 +299,7 @@ static int scan_ip(const char **ep, unsigned *ip, unsigned char endc)
299 for (j = 0; j < 4; j++) { 299 for (j = 0; j < 4; j++) {
300 unsigned octet; 300 unsigned octet;
301 301
302 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0) 302 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != '\0')
303 return -auto_mask; 303 return -auto_mask;
304 octet = 0; 304 octet = 0;
305 while (*p >= '0' && *p <= '9') { 305 while (*p >= '0' && *p <= '9') {
@@ -311,15 +311,15 @@ static int scan_ip(const char **ep, unsigned *ip, unsigned char endc)
311 } 311 }
312 if (*p == '.') 312 if (*p == '.')
313 p++; 313 p++;
314 if (*p != '/' && *p != 0) 314 if (*p != '/' && *p != '\0')
315 auto_mask += 8; 315 auto_mask += 8;
316 *ip = ((*ip) << 8) | octet; 316 *ip = ((*ip) << 8) | octet;
317 } 317 }
318 if (*p != 0) { 318 if (*p != '\0') {
319 if (*p != endc) 319 if (*p != endc)
320 return -auto_mask; 320 return -auto_mask;
321 p++; 321 p++;
322 if (*p == 0) 322 if (*p == '\0')
323 return -auto_mask; 323 return -auto_mask;
324 } 324 }
325 *ep = p; 325 *ep = p;
@@ -782,6 +782,17 @@ static int openServer(void)
782#endif 782#endif
783 783
784/* 784/*
785 * Log the connection closure and exit.
786 */
787static void log_and_exit(void) ATTRIBUTE_NORETURN;
788static void log_and_exit(void)
789{
790 if (verbose > 2)
791 bb_error_msg("closed");
792 _exit(xfunc_error_retval);
793}
794
795/*
785 * Create and send HTTP response headers. 796 * Create and send HTTP response headers.
786 * The arguments are combined and sent as one write operation. Note that 797 * The arguments are combined and sent as one write operation. Note that
787 * IE will puke big-time if the headers are not sent in one packet and the 798 * IE will puke big-time if the headers are not sent in one packet and the
@@ -789,7 +800,7 @@ static int openServer(void)
789 * responseNum - the result code to send. 800 * responseNum - the result code to send.
790 * Return result of write(). 801 * Return result of write().
791 */ 802 */
792static int sendHeaders(HttpResponseNum responseNum) 803static void send_headers(HttpResponseNum responseNum)
793{ 804{
794 static const char RFC1123FMT[] ALIGN1 = "%a, %d %b %Y %H:%M:%S GMT"; 805 static const char RFC1123FMT[] ALIGN1 = "%a, %d %b %Y %H:%M:%S GMT";
795 806
@@ -841,8 +852,8 @@ static int sendHeaders(HttpResponseNum responseNum)
841 len += sprintf(iobuf + len, "Last-Modified: %s\r\n%s %"OFF_FMT"d\r\n", 852 len += sprintf(iobuf + len, "Last-Modified: %s\r\n%s %"OFF_FMT"d\r\n",
842 tmp_str, "Content-length:", ContentLength); 853 tmp_str, "Content-length:", ContentLength);
843 } 854 }
844 strcat(iobuf, "\r\n"); 855 iobuf[len++] = '\r';
845 len += 2; 856 iobuf[len++] = '\n';
846 if (infoString) { 857 if (infoString) {
847 len += sprintf(iobuf + len, 858 len += sprintf(iobuf + len,
848 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\n" 859 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\n"
@@ -855,14 +866,18 @@ static int sendHeaders(HttpResponseNum responseNum)
855 i = accepted_socket; 866 i = accepted_socket;
856 if (i == 0) 867 if (i == 0)
857 i++; /* write to fd #1 in inetd mode */ 868 i++; /* write to fd #1 in inetd mode */
858 return full_write(i, iobuf, len); 869 if (full_write(i, iobuf, len) != len) {
870 if (verbose > 1)
871 bb_perror_msg("error");
872 log_and_exit();
873 }
859} 874}
860 875
861static void send_headers_and_exit(HttpResponseNum responseNum) ATTRIBUTE_NORETURN; 876static void send_headers_and_exit(HttpResponseNum responseNum) ATTRIBUTE_NORETURN;
862static void send_headers_and_exit(HttpResponseNum responseNum) 877static void send_headers_and_exit(HttpResponseNum responseNum)
863{ 878{
864 sendHeaders(responseNum); 879 send_headers(responseNum);
865 _exit(0); 880 log_and_exit();
866} 881}
867 882
868/* 883/*
@@ -945,8 +960,10 @@ static void send_cgi_and_exit(
945#else 960#else
946 pid = fork(); 961 pid = fork();
947#endif 962#endif
948 if (pid < 0) 963 if (pid < 0) {
949 _exit(0); 964 /* TODO: log perror? */
965 log_and_exit();
966 }
950 967
951 if (!pid) { 968 if (!pid) {
952 /* child process */ 969 /* child process */
@@ -1054,9 +1071,6 @@ static void send_cgi_and_exit(
1054 *script = '\0'; 1071 *script = '\0';
1055 /* chdiring to script's dir */ 1072 /* chdiring to script's dir */
1056 if (chdir(fullpath) == 0) { 1073 if (chdir(fullpath) == 0) {
1057 /* Now run the program. If it fails,
1058 * use _exit() so no destructors
1059 * get called and make a mess. */
1060#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR 1074#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1061 char *interpr = NULL; 1075 char *interpr = NULL;
1062 char *suffix = strrchr(purl, '.'); 1076 char *suffix = strrchr(purl, '.');
@@ -1083,8 +1097,7 @@ static void send_cgi_and_exit(
1083 /* send to stdout 1097 /* send to stdout
1084 * (we are CGI here, our stdout is pumped to the net) */ 1098 * (we are CGI here, our stdout is pumped to the net) */
1085 accepted_socket = 1; 1099 accepted_socket = 1;
1086 sendHeaders(HTTP_NOT_FOUND); 1100 send_headers_and_exit(HTTP_NOT_FOUND);
1087 _exit(242);
1088 } /* end child */ 1101 } /* end child */
1089 1102
1090 /* parent process */ 1103 /* parent process */
@@ -1248,7 +1261,7 @@ static void send_cgi_and_exit(
1248 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf); 1261 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf);
1249 } /* if (FD_ISSET(fromCgi.rd)) */ 1262 } /* if (FD_ISSET(fromCgi.rd)) */
1250 } /* while (1) */ 1263 } /* while (1) */
1251 _exit(0); 1264 log_and_exit();
1252} 1265}
1253#endif /* FEATURE_HTTPD_CGI */ 1266#endif /* FEATURE_HTTPD_CGI */
1254 1267
@@ -1329,14 +1342,14 @@ static void send_file_and_exit(const char *url)
1329 send_headers_and_exit(HTTP_NOT_FOUND); 1342 send_headers_and_exit(HTTP_NOT_FOUND);
1330 } 1343 }
1331 1344
1332 sendHeaders(HTTP_OK); 1345 send_headers(HTTP_OK);
1333 fd = accepted_socket; 1346 fd = accepted_socket;
1334 if (fd == 0) 1347 if (fd == 0)
1335 fd++; /* write to fd #1 in inetd mode */ 1348 fd++; /* write to fd #1 in inetd mode */
1336 1349
1337 /* If you want to know about EPIPE below 1350 /* If you want to know about EPIPE below
1338 * (happens if you abort downloads from local httpd): */ 1351 * (happens if you abort downloads from local httpd): */
1339 /* signal(SIGPIPE, SIG_IGN); */ 1352 signal(SIGPIPE, SIG_IGN);
1340 1353
1341#if ENABLE_FEATURE_HTTPD_USE_SENDFILE 1354#if ENABLE_FEATURE_HTTPD_USE_SENDFILE
1342 do { 1355 do {
@@ -1348,7 +1361,7 @@ static void send_file_and_exit(const char *url)
1348 goto fin; 1361 goto fin;
1349 } 1362 }
1350 } while (count > 0); 1363 } while (count > 0);
1351 _exit(0); 1364 log_and_exit();
1352 1365
1353 fallback: 1366 fallback:
1354#endif 1367#endif
@@ -1363,12 +1376,12 @@ static void send_file_and_exit(const char *url)
1363#endif 1376#endif
1364 if (count < 0 && verbose > 1) 1377 if (count < 0 && verbose > 1)
1365 bb_perror_msg("error"); 1378 bb_perror_msg("error");
1366 _exit(0); 1379 log_and_exit();
1367} 1380}
1368 1381
1369static int checkPermIP(void) 1382static int checkPermIP(void)
1370{ 1383{
1371 Htaccess_IP * cur; 1384 Htaccess_IP *cur;
1372 1385
1373 /* This could stand some work */ 1386 /* This could stand some work */
1374 for (cur = ip_a_d; cur; cur = cur->next) { 1387 for (cur = ip_a_d; cur; cur = cur->next) {
@@ -1517,8 +1530,10 @@ static void handle_incoming_and_exit(void)
1517 sigaction(SIGALRM, &sa, NULL); 1530 sigaction(SIGALRM, &sa, NULL);
1518 alarm(HEADER_READ_TIMEOUT); 1531 alarm(HEADER_READ_TIMEOUT);
1519 1532
1520 if (!get_line()) 1533 if (!get_line()) {
1521 _exit(0); /* EOF or error or empty line */ 1534 /* EOF or error or empty line */
1535 send_headers_and_exit(HTTP_BAD_REQUEST);
1536 }
1522 1537
1523 /* Determine type of request (GET/POST) */ 1538 /* Determine type of request (GET/POST) */
1524 purl = strpbrk(iobuf, " \t"); 1539 purl = strpbrk(iobuf, " \t");
@@ -1644,7 +1659,7 @@ static void handle_incoming_and_exit(void)
1644 if (prequest != request_GET) { 1659 if (prequest != request_GET) {
1645 test = iobuf + sizeof("Content-length:") - 1; 1660 test = iobuf + sizeof("Content-length:") - 1;
1646 if (!test[0]) 1661 if (!test[0])
1647 _exit(0); 1662 send_headers_and_exit(HTTP_BAD_REQUEST);
1648 errno = 0; 1663 errno = 0;
1649 /* not using strtoul: it ignores leading minus! */ 1664 /* not using strtoul: it ignores leading minus! */
1650 length = strtol(test, &test, 10); 1665 length = strtol(test, &test, 10);
@@ -1652,7 +1667,7 @@ static void handle_incoming_and_exit(void)
1652 /* so we check for negative or too large values in one go: */ 1667 /* so we check for negative or too large values in one go: */
1653 /* (long -> ulong conv caused negatives to be seen as > INT_MAX) */ 1668 /* (long -> ulong conv caused negatives to be seen as > INT_MAX) */
1654 if (test[0] || errno || length > INT_MAX) 1669 if (test[0] || errno || length > INT_MAX)
1655 _exit(0); 1670 send_headers_and_exit(HTTP_BAD_REQUEST);
1656 } 1671 }
1657 } else if (STRNCASECMP(iobuf, "Cookie:") == 0) { 1672 } else if (STRNCASECMP(iobuf, "Cookie:") == 0) {
1658 cookie = strdup(skip_whitespace(iobuf + sizeof("Cookie:")-1)); 1673 cookie = strdup(skip_whitespace(iobuf + sizeof("Cookie:")-1));
@@ -1682,7 +1697,7 @@ static void handle_incoming_and_exit(void)
1682 } /* while extra header reading */ 1697 } /* while extra header reading */
1683 } 1698 }
1684 1699
1685 /* We read everything, disable peer timeout */ 1700 /* We read headers, disable peer timeout */
1686 alarm(0); 1701 alarm(0);
1687 1702
1688 if (strcmp(bb_basename(url), httpd_conf) == 0 || ip_allowed == 0) { 1703 if (strcmp(bb_basename(url), httpd_conf) == 0 || ip_allowed == 0) {
@@ -1779,7 +1794,7 @@ static void handle_incoming_and_exit(void)
1779 } while (retval > 0 && read(accepted_socket, iobuf, sizeof(iobuf) > 0)); 1794 } while (retval > 0 && read(accepted_socket, iobuf, sizeof(iobuf) > 0));
1780 shutdown(accepted_socket, SHUT_RD); 1795 shutdown(accepted_socket, SHUT_RD);
1781 close(accepted_socket); 1796 close(accepted_socket);
1782 _exit(0); 1797 log_and_exit();
1783#endif 1798#endif
1784} 1799}
1785 1800
@@ -2003,12 +2018,14 @@ int httpd_main(int argc, char **argv)
2003#else 2018#else
2004 parse_conf(default_path_httpd_conf, FIRST_PARSE); 2019 parse_conf(default_path_httpd_conf, FIRST_PARSE);
2005#endif 2020#endif
2021 xfunc_error_retval = 0;
2006 if (opt & OPT_INETD) 2022 if (opt & OPT_INETD)
2007 mini_httpd_inetd(); 2023 mini_httpd_inetd();
2008 if (!(opt & OPT_FOREGROUND)) 2024 if (!(opt & OPT_FOREGROUND))
2009 bb_daemonize(0); /* don't change current directory */ 2025 bb_daemonize(0); /* don't change current directory */
2010 mini_httpd(server_socket); /* never returns */ 2026 mini_httpd(server_socket); /* never returns */
2011#else 2027#else
2028 xfunc_error_retval = 0;
2012 mini_httpd_inetd(); /* never returns */ 2029 mini_httpd_inetd(); /* never returns */
2013 /* return 0; */ 2030 /* return 0; */
2014#endif 2031#endif