aboutsummaryrefslogtreecommitdiff
path: root/networking/httpd.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-08-18 14:18:43 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-08-18 14:18:43 +0000
commit9611cb1215fa52200ca5259e3b226df34d0a8045 (patch)
treee2e6ec870fd2ca179bda80fe93afb16ca3eec3df /networking/httpd.c
parenta27a11bb2c033d3e0312dc1d62430a002314686f (diff)
downloadbusybox-w32-9611cb1215fa52200ca5259e3b226df34d0a8045.tar.gz
busybox-w32-9611cb1215fa52200ca5259e3b226df34d0a8045.tar.bz2
busybox-w32-9611cb1215fa52200ca5259e3b226df34d0a8045.zip
get rid of server_ and accepted_socket globals.
get_line 74 73 -1 log_and_exit 33 32 -1 send_headers 457 449 -8 httpd_main 906 892 -14 handle_incoming_and_exit 2136 2113 -23 send_cgi_and_exit 1743 1583 -160
Diffstat (limited to 'networking/httpd.c')
-rw-r--r--networking/httpd.c95
1 files changed, 38 insertions, 57 deletions
diff --git a/networking/httpd.c b/networking/httpd.c
index a57504bac..d68dc06e9 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -131,13 +131,11 @@ typedef struct Htaccess_IP {
131} Htaccess_IP; 131} Htaccess_IP;
132 132
133struct globals { 133struct globals {
134 int server_socket;
135 int accepted_socket;
136 int verbose; 134 int verbose;
137 smallint flg_deny_all; 135 smallint flg_deny_all;
138 136
139 unsigned rmt_ip; 137 unsigned rmt_ip;
140 unsigned tcp_port; /* for set env REMOTE_PORT */ 138 unsigned rmt_port; /* for set env REMOTE_PORT */
141 const char *bind_addr_or_port; 139 const char *bind_addr_or_port;
142 140
143 const char *g_query; 141 const char *g_query;
@@ -169,12 +167,10 @@ struct globals {
169 char iobuf[MAX_MEMORY_BUF]; 167 char iobuf[MAX_MEMORY_BUF];
170}; 168};
171#define G (*ptr_to_globals) 169#define G (*ptr_to_globals)
172#define server_socket (G.server_socket )
173#define accepted_socket (G.accepted_socket )
174#define verbose (G.verbose ) 170#define verbose (G.verbose )
175#define flg_deny_all (G.flg_deny_all ) 171#define flg_deny_all (G.flg_deny_all )
176#define rmt_ip (G.rmt_ip ) 172#define rmt_ip (G.rmt_ip )
177#define tcp_port (G.tcp_port ) 173#define rmt_port (G.rmt_port )
178#define bind_addr_or_port (G.bind_addr_or_port) 174#define bind_addr_or_port (G.bind_addr_or_port)
179#define g_query (G.g_query ) 175#define g_query (G.g_query )
180#define configFile (G.configFile ) 176#define configFile (G.configFile )
@@ -863,10 +859,7 @@ static void send_headers(HttpResponseNum responseNum)
863 } 859 }
864 if (DEBUG) 860 if (DEBUG)
865 fprintf(stderr, "headers: '%s'\n", iobuf); 861 fprintf(stderr, "headers: '%s'\n", iobuf);
866 i = accepted_socket; 862 if (full_write(1, iobuf, len) != len) {
867 if (i == 0)
868 i++; /* write to fd #1 in inetd mode */
869 if (full_write(i, iobuf, len) != len) {
870 if (verbose > 1) 863 if (verbose > 1)
871 bb_perror_msg("error"); 864 bb_perror_msg("error");
872 log_and_exit(); 865 log_and_exit();
@@ -892,7 +885,7 @@ static int get_line(void)
892 int count = 0; 885 int count = 0;
893 886
894 /* We must not read extra chars. Reading byte-by-byte... */ 887 /* We must not read extra chars. Reading byte-by-byte... */
895 while (read(accepted_socket, iobuf + count, 1) == 1) { 888 while (read(0, iobuf + count, 1) == 1) {
896 if (iobuf[count] == '\r') 889 if (iobuf[count] == '\r')
897 continue; 890 continue;
898 if (iobuf[count] == '\n') { 891 if (iobuf[count] == '\n') {
@@ -941,7 +934,6 @@ static void send_cgi_and_exit(
941 int buf_count; 934 int buf_count;
942 int status; 935 int status;
943 int pid = 0; 936 int pid = 0;
944 int sv_accepted_socket = accepted_socket;
945 937
946 /* 938 /*
947 * We are mucking with environment _first_ and then vfork/exec, 939 * We are mucking with environment _first_ and then vfork/exec,
@@ -1008,7 +1000,7 @@ static void send_cgi_and_exit(
1008 } 1000 }
1009 setenv1("HTTP_USER_AGENT", user_agent); 1001 setenv1("HTTP_USER_AGENT", user_agent);
1010#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV 1002#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
1011 setenv_long("REMOTE_PORT", tcp_port); 1003 setenv_long("REMOTE_PORT", rmt_port);
1012#endif 1004#endif
1013 if (bodyLen) 1005 if (bodyLen)
1014 setenv_long("CONTENT_LENGTH", bodyLen); 1006 setenv_long("CONTENT_LENGTH", bodyLen);
@@ -1038,11 +1030,6 @@ static void send_cgi_and_exit(
1038 /* Child process */ 1030 /* Child process */
1039 xfunc_error_retval = 242; 1031 xfunc_error_retval = 242;
1040 1032
1041 if (accepted_socket > 1)
1042 close(accepted_socket);
1043 if (server_socket > 1)
1044 close(server_socket);
1045
1046 xmove_fd(toCgi.rd, 0); /* replace stdin with the pipe */ 1033 xmove_fd(toCgi.rd, 0); /* replace stdin with the pipe */
1047 xmove_fd(fromCgi.wr, 1); /* replace stdout with the pipe */ 1034 xmove_fd(fromCgi.wr, 1); /* replace stdout with the pipe */
1048 close(fromCgi.rd); 1035 close(fromCgi.rd);
@@ -1087,7 +1074,6 @@ static void send_cgi_and_exit(
1087 error_execing_cgi: 1074 error_execing_cgi:
1088 /* send to stdout 1075 /* send to stdout
1089 * (we are CGI here, our stdout is pumped to the net) */ 1076 * (we are CGI here, our stdout is pumped to the net) */
1090 accepted_socket = 1;
1091 send_headers_and_exit(HTTP_NOT_FOUND); 1077 send_headers_and_exit(HTTP_NOT_FOUND);
1092 } /* end child */ 1078 } /* end child */
1093 1079
@@ -1095,7 +1081,6 @@ static void send_cgi_and_exit(
1095 1081
1096 /* First, restore variables possibly changed by child */ 1082 /* First, restore variables possibly changed by child */
1097 xfunc_error_retval = 0; 1083 xfunc_error_retval = 0;
1098 accepted_socket = sv_accepted_socket;
1099 1084
1100 /* Prepare for pumping data */ 1085 /* Prepare for pumping data */
1101 buf_count = 0; 1086 buf_count = 0;
@@ -1126,11 +1111,8 @@ static void send_cgi_and_exit(
1126 if (bodyLen > 0 || post_read_size > 0) { 1111 if (bodyLen > 0 || post_read_size > 0) {
1127 FD_SET(toCgi.wr, &writeSet); 1112 FD_SET(toCgi.wr, &writeSet);
1128 nfound = toCgi.wr > fromCgi.rd ? toCgi.wr : fromCgi.rd; 1113 nfound = toCgi.wr > fromCgi.rd ? toCgi.wr : fromCgi.rd;
1129 if (post_read_size == 0) { 1114 if (post_read_size == 0)
1130 FD_SET(accepted_socket, &readSet); 1115 FD_SET(0, &readSet);
1131 if (nfound < accepted_socket)
1132 nfound = accepted_socket;
1133 }
1134 /* Now wait on the set of sockets! */ 1116 /* Now wait on the set of sockets! */
1135 nfound = select(nfound + 1, &readSet, &writeSet, NULL, NULL); 1117 nfound = select(nfound + 1, &readSet, &writeSet, NULL, NULL);
1136 } else { 1118 } else {
@@ -1169,13 +1151,13 @@ static void send_cgi_and_exit(
1169 post_read_size = bodyLen = 0; /* EOF/broken pipe to CGI */ 1151 post_read_size = bodyLen = 0; /* EOF/broken pipe to CGI */
1170 } 1152 }
1171 } else if (bodyLen > 0 && post_read_size == 0 1153 } else if (bodyLen > 0 && post_read_size == 0
1172 && FD_ISSET(accepted_socket, &readSet) 1154 && FD_ISSET(0, &readSet)
1173 ) { 1155 ) {
1174 /* We expect data, prev data portion is eaten by CGI 1156 /* We expect data, prev data portion is eaten by CGI
1175 * and there *is* data to read from the peer 1157 * and there *is* data to read from the peer
1176 * (POSTDATA?) */ 1158 * (POSTDATA?) */
1177 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen; 1159 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
1178 count = safe_read(accepted_socket, wbuf, count); 1160 count = safe_read(0, wbuf, count);
1179 if (count > 0) { 1161 if (count > 0) {
1180 post_read_size = count; 1162 post_read_size = count;
1181 post_read_idx = 0; 1163 post_read_idx = 0;
@@ -1191,7 +1173,6 @@ static void send_cgi_and_exit(
1191#endif 1173#endif
1192 if (FD_ISSET(fromCgi.rd, &readSet)) { 1174 if (FD_ISSET(fromCgi.rd, &readSet)) {
1193 /* There is something to read from CGI */ 1175 /* There is something to read from CGI */
1194 int s = accepted_socket;
1195 char *rbuf = iobuf; 1176 char *rbuf = iobuf;
1196 1177
1197 /* Are we still buffering CGI output? */ 1178 /* Are we still buffering CGI output? */
@@ -1213,8 +1194,8 @@ static void send_cgi_and_exit(
1213 /* eof (or error) and there was no "HTTP", 1194 /* eof (or error) and there was no "HTTP",
1214 * so write it, then write received data */ 1195 * so write it, then write received data */
1215 if (buf_count) { 1196 if (buf_count) {
1216 full_write(s, HTTP_200, sizeof(HTTP_200)-1); 1197 full_write(1, HTTP_200, sizeof(HTTP_200)-1);
1217 full_write(s, rbuf, buf_count); 1198 full_write(1, rbuf, buf_count);
1218 } 1199 }
1219 break; /* closed */ 1200 break; /* closed */
1220 } 1201 }
@@ -1223,7 +1204,7 @@ static void send_cgi_and_exit(
1223 /* "Status" header format is: "Status: 302 Redirected\r\n" */ 1204 /* "Status" header format is: "Status: 302 Redirected\r\n" */
1224 if (buf_count >= 8 && memcmp(rbuf, "Status: ", 8) == 0) { 1205 if (buf_count >= 8 && memcmp(rbuf, "Status: ", 8) == 0) {
1225 /* send "HTTP/1.0 " */ 1206 /* send "HTTP/1.0 " */
1226 if (full_write(s, HTTP_200, 9) != 9) 1207 if (full_write(1, HTTP_200, 9) != 9)
1227 break; 1208 break;
1228 rbuf += 8; /* skip "Status: " */ 1209 rbuf += 8; /* skip "Status: " */
1229 count = buf_count - 8; 1210 count = buf_count - 8;
@@ -1232,7 +1213,7 @@ static void send_cgi_and_exit(
1232 /* Did CGI add "HTTP"? */ 1213 /* Did CGI add "HTTP"? */
1233 if (memcmp(rbuf, HTTP_200, 4) != 0) { 1214 if (memcmp(rbuf, HTTP_200, 4) != 0) {
1234 /* there is no "HTTP", do it ourself */ 1215 /* there is no "HTTP", do it ourself */
1235 if (full_write(s, HTTP_200, sizeof(HTTP_200)-1) != sizeof(HTTP_200)-1) 1216 if (full_write(1, HTTP_200, sizeof(HTTP_200)-1) != sizeof(HTTP_200)-1)
1236 break; 1217 break;
1237 } 1218 }
1238 /* Commented out: 1219 /* Commented out:
@@ -1252,7 +1233,7 @@ static void send_cgi_and_exit(
1252 if (count <= 0) 1233 if (count <= 0)
1253 break; /* eof (or error) */ 1234 break; /* eof (or error) */
1254 } 1235 }
1255 if (full_write(s, rbuf, count) != count) 1236 if (full_write(1, rbuf, count) != count)
1256 break; 1237 break;
1257 if (DEBUG) 1238 if (DEBUG)
1258 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf); 1239 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf);
@@ -1292,7 +1273,6 @@ static void send_file_and_exit(const char *url)
1292 1273
1293 char *suffix; 1274 char *suffix;
1294 int f; 1275 int f;
1295 int fd;
1296 const char *const *table; 1276 const char *const *table;
1297 const char *try_suffix; 1277 const char *try_suffix;
1298 ssize_t count; 1278 ssize_t count;
@@ -1340,9 +1320,6 @@ static void send_file_and_exit(const char *url)
1340 } 1320 }
1341 1321
1342 send_headers(HTTP_OK); 1322 send_headers(HTTP_OK);
1343 fd = accepted_socket;
1344 if (fd == 0)
1345 fd++; /* write to fd #1 in inetd mode */
1346 1323
1347 /* If you want to know about EPIPE below 1324 /* If you want to know about EPIPE below
1348 * (happens if you abort downloads from local httpd): */ 1325 * (happens if you abort downloads from local httpd): */
@@ -1351,7 +1328,7 @@ static void send_file_and_exit(const char *url)
1351#if ENABLE_FEATURE_HTTPD_USE_SENDFILE 1328#if ENABLE_FEATURE_HTTPD_USE_SENDFILE
1352 do { 1329 do {
1353 /* byte count (3rd arg) is rounded down to 64k */ 1330 /* byte count (3rd arg) is rounded down to 64k */
1354 count = sendfile(fd, f, &offset, MAXINT(ssize_t) - 0xffff); 1331 count = sendfile(1, f, &offset, MAXINT(ssize_t) - 0xffff);
1355 if (count < 0) { 1332 if (count < 0) {
1356 if (offset == 0) 1333 if (offset == 0)
1357 goto fallback; 1334 goto fallback;
@@ -1364,7 +1341,7 @@ static void send_file_and_exit(const char *url)
1364#endif 1341#endif
1365 while ((count = safe_read(f, iobuf, MAX_MEMORY_BUF)) > 0) { 1342 while ((count = safe_read(f, iobuf, MAX_MEMORY_BUF)) > 0) {
1366 ssize_t n = count; 1343 ssize_t n = count;
1367 count = full_write(fd, iobuf, count); 1344 count = full_write(1, iobuf, count);
1368 if (count != n) 1345 if (count != n)
1369 break; 1346 break;
1370 } 1347 }
@@ -1521,9 +1498,10 @@ static void handle_incoming_and_exit(void)
1521#endif 1498#endif
1522 1499
1523 /* Install timeout handler */ 1500 /* Install timeout handler */
1501 memset(&sa, 0, sizeof(sa));
1524 sa.sa_handler = exit_on_signal; 1502 sa.sa_handler = exit_on_signal;
1525 sigemptyset(&sa.sa_mask); 1503 /* sigemptyset(&sa.sa_mask); - memset should be enough */
1526 sa.sa_flags = 0; /* no SA_RESTART */ 1504 /*sa.sa_flags = 0; - no SA_RESTART */
1527 sigaction(SIGALRM, &sa, NULL); 1505 sigaction(SIGALRM, &sa, NULL);
1528 alarm(HEADER_READ_TIMEOUT); 1506 alarm(HEADER_READ_TIMEOUT);
1529 1507
@@ -1779,18 +1757,17 @@ static void handle_incoming_and_exit(void)
1779#endif 1757#endif
1780 /* Properly wait for remote to closed */ 1758 /* Properly wait for remote to closed */
1781 int retval; 1759 int retval;
1782 shutdown(accepted_socket, SHUT_WR); 1760 shutdown(1, SHUT_WR);
1783 do { 1761 do {
1784 fd_set s_fd; 1762 fd_set s_fd;
1785 struct timeval tv; 1763 struct timeval tv;
1786 FD_ZERO(&s_fd); 1764 FD_ZERO(&s_fd);
1787 FD_SET(accepted_socket, &s_fd); 1765 FD_SET(0, &s_fd);
1788 tv.tv_sec = 2; 1766 tv.tv_sec = 2;
1789 tv.tv_usec = 0; 1767 tv.tv_usec = 0;
1790 retval = select(accepted_socket + 1, &s_fd, NULL, NULL, &tv); 1768 retval = select(1, &s_fd, NULL, NULL, &tv);
1791 } while (retval > 0 && read(accepted_socket, iobuf, sizeof(iobuf) > 0)); 1769 } while (retval > 0 && read(0, iobuf, sizeof(iobuf) > 0));
1792 shutdown(accepted_socket, SHUT_RD); 1770 shutdown(0, SHUT_RD);
1793 close(accepted_socket);
1794 log_and_exit(); 1771 log_and_exit();
1795#endif 1772#endif
1796} 1773}
@@ -1802,8 +1779,8 @@ static void handle_incoming_and_exit(void)
1802 * the processing as a forked process. 1779 * the processing as a forked process.
1803 * Never returns. 1780 * Never returns.
1804 */ 1781 */
1805static void mini_httpd(int server) ATTRIBUTE_NORETURN; 1782static void mini_httpd(int server_socket) ATTRIBUTE_NORETURN;
1806static void mini_httpd(int server) 1783static void mini_httpd(int server_socket)
1807{ 1784{
1808 /* NB: it's best to not use xfuncs in this loop before fork(). 1785 /* NB: it's best to not use xfuncs in this loop before fork().
1809 * Otherwise server may die on transient errors (temporary 1786 * Otherwise server may die on transient errors (temporary
@@ -1816,7 +1793,7 @@ static void mini_httpd(int server)
1816 1793
1817 /* Wait for connections... */ 1794 /* Wait for connections... */
1818 fromAddr.len = LSA_SIZEOF_SA; 1795 fromAddr.len = LSA_SIZEOF_SA;
1819 n = accept(server, &fromAddr.sa, &fromAddr.len); 1796 n = accept(server_socket, &fromAddr.sa, &fromAddr.len);
1820 1797
1821 if (n < 0) 1798 if (n < 0)
1822 continue; 1799 continue;
@@ -1829,15 +1806,18 @@ static void mini_httpd(int server)
1829 /* Do not reload config on HUP */ 1806 /* Do not reload config on HUP */
1830 signal(SIGHUP, SIG_IGN); 1807 signal(SIGHUP, SIG_IGN);
1831#endif 1808#endif
1832 accepted_socket = n; 1809 close(server_socket);
1810 xmove_fd(n, 0);
1811 xdup2(0, 1);
1812
1833 n = get_nport(&fromAddr.sa); 1813 n = get_nport(&fromAddr.sa);
1834 tcp_port = ntohs(n); 1814 rmt_port = ntohs(n);
1835 rmt_ip = 0; 1815 rmt_ip = 0;
1836 if (fromAddr.sa.sa_family == AF_INET) { 1816 if (fromAddr.sa.sa_family == AF_INET) {
1837 rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); 1817 rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr);
1838 } 1818 }
1839 if (ENABLE_FEATURE_HTTPD_CGI || DEBUG || verbose) { 1819 if (ENABLE_FEATURE_HTTPD_CGI || DEBUG || verbose) {
1840 rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddr.len); 1820 rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa);
1841 } 1821 }
1842 if (verbose) { 1822 if (verbose) {
1843 /* this trick makes -v logging much simpler */ 1823 /* this trick makes -v logging much simpler */
@@ -1864,13 +1844,13 @@ static void mini_httpd_inetd(void)
1864 fromAddr.len = LSA_SIZEOF_SA; 1844 fromAddr.len = LSA_SIZEOF_SA;
1865 getpeername(0, &fromAddr.sa, &fromAddr.len); 1845 getpeername(0, &fromAddr.sa, &fromAddr.len);
1866 n = get_nport(&fromAddr.sa); 1846 n = get_nport(&fromAddr.sa);
1867 tcp_port = ntohs(n); 1847 rmt_port = ntohs(n);
1868 rmt_ip = 0; 1848 rmt_ip = 0;
1869 if (fromAddr.sa.sa_family == AF_INET) { 1849 if (fromAddr.sa.sa_family == AF_INET) {
1870 rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); 1850 rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr);
1871 } 1851 }
1872 if (ENABLE_FEATURE_HTTPD_CGI || DEBUG || verbose) { 1852 if (ENABLE_FEATURE_HTTPD_CGI || DEBUG || verbose) {
1873 rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddr.len); 1853 rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa);
1874 } 1854 }
1875 handle_incoming_and_exit(); 1855 handle_incoming_and_exit();
1876} 1856}
@@ -1919,6 +1899,7 @@ enum {
1919int httpd_main(int argc, char **argv); 1899int httpd_main(int argc, char **argv);
1920int httpd_main(int argc, char **argv) 1900int httpd_main(int argc, char **argv)
1921{ 1901{
1902 int server_socket = server_socket; /* for gcc */
1922 unsigned opt; 1903 unsigned opt;
1923 char *url_for_decode; 1904 char *url_for_decode;
1924 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;) 1905 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
@@ -2004,8 +1985,8 @@ int httpd_main(int argc, char **argv)
2004 clearenv(); 1985 clearenv();
2005 if (p) 1986 if (p)
2006 putenv(p - 5); 1987 putenv(p - 5);
2007 if (!(opt & OPT_INETD)) 1988// if (!(opt & OPT_INETD))
2008 setenv_long("SERVER_PORT", tcp_port); 1989// setenv_long("SERVER_PORT", ???);
2009 } 1990 }
2010#endif 1991#endif
2011 1992