httpd: extend -p PORT to -p [IP[v6]:]PORT

httpd: comment out strange code which "closes connection properly"
(isnt exit(0) good enough?)

handle_incoming_and_exit                               -    2246   +2246
httpd_main                                          1116    1141     +25
getLine                                               75      77      +2
packed_usage                                       22827   22811     -16
parse_conf                                          1303    1284     -19
shutdown                                              32       -     -32
handleIncoming                                      2578       -   -2578
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 2/2 up/down: 2273/-2645)       Total: -372 bytes
   text    data     bss     dec     hex filename
 774704    1058   11092  786854   c01a6 busybox_old
 774335    1058   11092  786485   c0035 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-08-14 16:50:01 +00:00
parent 3d17d2b430
commit 0372f0f199
4 changed files with 108 additions and 137 deletions

View File

@ -606,7 +606,6 @@ CONFIG_APP_UDHCPD=y
CONFIG_APP_DHCPRELAY=y CONFIG_APP_DHCPRELAY=y
CONFIG_APP_DUMPLEASES=y CONFIG_APP_DUMPLEASES=y
# CONFIG_APP_UDHCPC is not set # CONFIG_APP_UDHCPC is not set
CONFIG_FEATURE_UDHCP_SYSLOG=y
# CONFIG_FEATURE_UDHCP_DEBUG is not set # CONFIG_FEATURE_UDHCP_DEBUG is not set
CONFIG_FEATURE_RFC3397=y CONFIG_FEATURE_RFC3397=y
CONFIG_VCONFIG=y CONFIG_VCONFIG=y

View File

@ -1352,21 +1352,21 @@
" [-h home]" \ " [-h home]" \
" [-d/-e string]" " [-d/-e string]"
#define httpd_full_usage \ #define httpd_full_usage \
"Listen for incoming http server requests" \ "Listen for incoming HTTP requests" \
"\n\nOptions:\n" \ "\n\nOptions:" \
" -c FILE Specifies configuration file. (default httpd.conf)\n" \ "\n -c FILE Configuration file (default httpd.conf)" \
" -p PORT Server port (default 80)\n" \ "\n -p PORT Server port (default 80)" \
" -i Assume that we are started from inetd\n" \ "\n -i Inetd mode" \
" -f Do not daemonize\n" \ "\n -f Do not daemonize" \
USE_FEATURE_HTTPD_SETUID( \ USE_FEATURE_HTTPD_SETUID( \
" -u USER[:GRP] Set uid/gid after binding to port\n") \ "\n -u USER[:GRP] Set uid/gid after binding to port") \
USE_FEATURE_HTTPD_BASIC_AUTH( \ USE_FEATURE_HTTPD_BASIC_AUTH( \
" -r REALM Authentication Realm for Basic Authentication\n") \ "\n -r REALM Authentication Realm for Basic Authentication") \
USE_FEATURE_HTTPD_AUTH_MD5( \ USE_FEATURE_HTTPD_AUTH_MD5( \
" -m PASS Crypt PASS with md5 algorithm\n") \ "\n -m PASS Crypt PASS with md5 algorithm") \
" -h HOME Specifies http HOME directory (default ./)\n" \ "\n -h HOME Home directory (default .)" \
" -e STRING HTML encode STRING\n" \ "\n -e STRING HTML encode STRING" \
" -d STRING URL decode STRING" "\n -d STRING URL decode STRING"
#define hwclock_trivial_usage \ #define hwclock_trivial_usage \
"[-r|--show] [-s|--hctosys] [-w|--systohc]" \ "[-r|--show] [-s|--hctosys] [-w|--systohc]" \

View File

@ -114,19 +114,19 @@ static const char httpd_conf[] ALIGN1 = "httpd.conf";
//#define DEBUG 1 //#define DEBUG 1
#define DEBUG 0 #define DEBUG 0
#define MAX_MEMORY_BUFF 8192 /* IO buffer */ #define MAX_MEMORY_BUF 8192 /* IO buffer */
typedef struct HT_ACCESS { typedef struct Htaccess {
char *after_colon; char *after_colon;
struct HT_ACCESS *next; struct Htaccess *next;
char before_colon[1]; /* really bigger, must last */ char before_colon[1]; /* really bigger, must be last */
} Htaccess; } Htaccess;
typedef struct HT_ACCESS_IP { typedef struct Htaccess_IP {
unsigned ip; unsigned ip;
unsigned mask; unsigned mask;
int allow_deny; int allow_deny;
struct HT_ACCESS_IP *next; struct Htaccess_IP *next;
} Htaccess_IP; } Htaccess_IP;
struct globals { struct globals {
@ -153,8 +153,9 @@ struct globals {
#if ENABLE_FEATURE_HTTPD_CGI || DEBUG #if ENABLE_FEATURE_HTTPD_CGI || DEBUG
char *rmt_ip_str; /* for set env REMOTE_ADDR */ char *rmt_ip_str; /* for set env REMOTE_ADDR */
#endif #endif
unsigned tcp_port; /* server initial port and for const char *bind_addr_or_port;
set env REMOTE_PORT */ unsigned tcp_port; /* for set env REMOTE_PORT */
#if ENABLE_FEATURE_HTTPD_BASIC_AUTH #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Htaccess *g_auth; /* config user:password lines */ Htaccess *g_auth; /* config user:password lines */
#endif #endif
@ -164,7 +165,7 @@ struct globals {
#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Htaccess *script_i; /* config script interpreters */ Htaccess *script_i; /* config script interpreters */
#endif #endif
char iobuf[MAX_MEMORY_BUFF]; char iobuf[MAX_MEMORY_BUF];
}; };
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
#define server_socket (G.server_socket ) #define server_socket (G.server_socket )
@ -187,6 +188,7 @@ struct globals {
#if ENABLE_FEATURE_HTTPD_CGI || DEBUG #if ENABLE_FEATURE_HTTPD_CGI || DEBUG
#define rmt_ip_str (G.rmt_ip_str ) #define rmt_ip_str (G.rmt_ip_str )
#endif #endif
#define bind_addr_or_port (G.bind_addr_or_port)
#define tcp_port (G.tcp_port ) #define tcp_port (G.tcp_port )
#if ENABLE_FEATURE_HTTPD_BASIC_AUTH #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
#define g_auth (G.g_auth ) #define g_auth (G.g_auth )
@ -201,7 +203,7 @@ struct globals {
#define INIT_G() do { \ #define INIT_G() do { \
PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
USE_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \ USE_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
tcp_port = 80; \ bind_addr_or_port = "80"; \
ContentLength = -1; \ ContentLength = -1; \
} while (0) } while (0)
@ -771,7 +773,7 @@ static void setenv1(const char *name, const char *value)
} }
static void setenv_long(const char *name, long value) static void setenv_long(const char *name, long value)
{ {
char buf[sizeof(value)*3 + 1]; char buf[sizeof(value)*3 + 2];
sprintf(buf, "%ld", value); sprintf(buf, "%ld", value);
setenv(name, buf, 1); setenv(name, buf, 1);
} }
@ -849,33 +851,27 @@ static void decodeBase64(char *Data)
#if BB_MMU #if BB_MMU
static int openServer(void) static int openServer(void)
{ {
int fd; int n = bb_strtou(bind_addr_or_port, NULL, 10);
if (!errno && n && n <= 0xffff)
/* create the socket right now */ n = create_and_bind_stream_or_die(NULL, n);
fd = create_and_bind_stream_or_die(NULL, tcp_port); else
xlisten(fd, 9); n = create_and_bind_stream_or_die(bind_addr_or_port, 80);
return fd; xlisten(n, 9);
return n;
} }
#endif #endif
/**************************************************************************** /*
* * Create and send HTTP response headers.
> $Function: sendHeaders() * The arguments are combined and sent as one write operation. Note that
* * IE will puke big-time if the headers are not sent in one packet and the
* $Description: Create and send HTTP response headers. * second packet is delayed for any reason.
* The arguments are combined and sent as one write operation. Note that * responseNum - the result code to send.
* IE will puke big-time if the headers are not sent in one packet and the * Return result of write().
* second packet is delayed for any reason. */
*
* $Parameter:
* (HttpResponseNum) responseNum . . . The result code to send.
*
* $Return: (int) . . . . writing errors
*
****************************************************************************/
static int sendHeaders(HttpResponseNum responseNum) static int sendHeaders(HttpResponseNum responseNum)
{ {
char *buf = iobuf; char *const buf = iobuf; // dont really need it?
const char *responseString = ""; const char *responseString = "";
const char *infoString = 0; const char *infoString = 0;
const char *mime_type; const char *mime_type;
@ -937,33 +933,29 @@ static int sendHeaders(HttpResponseNum responseNum)
return full_write(i, buf, len); return full_write(i, buf, len);
} }
/**************************************************************************** /*
* * Read from the socket until '\n' or EOF. '\r' chars are removed.
> $Function: getLine() * Return number of characters read or -1 if nothing is read.
* * Data is returned in iobuf.
* $Description: Read from the socket until an end of line char found. */
*
* Characters are read one at a time until an eol sequence is found.
*
* $Return: (int) . . . . number of characters read. -1 if error.
*
****************************************************************************/
static int getLine(void) static int getLine(void)
{ {
int count = 0; int count = 0;
char *buf = iobuf;
while (read(accepted_socket, buf + count, 1) == 1) { /* We must not read extra chars. Reading byte-by-byte... */
if (buf[count] == '\r') continue; while (read(accepted_socket, iobuf + count, 1) == 1) {
if (buf[count] == '\n') { if (iobuf[count] == '\r')
buf[count] = 0; continue;
if (iobuf[count] == '\n') {
iobuf[count] = '\0';
return count; return count;
} }
if (count < (MAX_MEMORY_BUFF-1)) /* check overflow */ if (count < (MAX_MEMORY_BUF - 1)) /* check overflow */
count++; count++;
} }
if (count) return count; if (count)
else return -1; return count;
return -1;
} }
#if ENABLE_FEATURE_HTTPD_CGI #if ENABLE_FEATURE_HTTPD_CGI
@ -1255,8 +1247,8 @@ static int sendCgi(const char *url,
} }
#define PIPESIZE PIPE_BUF #define PIPESIZE PIPE_BUF
#if PIPESIZE >= MAX_MEMORY_BUFF #if PIPESIZE >= MAX_MEMORY_BUF
# error "PIPESIZE >= MAX_MEMORY_BUFF" # error "PIPESIZE >= MAX_MEMORY_BUF"
#endif #endif
if (FD_ISSET(inFd, &readSet)) { if (FD_ISSET(inFd, &readSet)) {
/* There is something to read from CGI */ /* There is something to read from CGI */
@ -1408,7 +1400,7 @@ static int sendFile(const char *url)
fallback: fallback:
#endif #endif
while ((count = full_read(f, iobuf, MAX_MEMORY_BUFF)) > 0) { while ((count = full_read(f, iobuf, MAX_MEMORY_BUF)) > 0) {
if (full_write(fd, iobuf, count) != count) if (full_write(fd, iobuf, count) != count)
break; break;
} }
@ -1532,33 +1524,25 @@ set_remoteuser_var:
#endif /* FEATURE_HTTPD_BASIC_AUTH */ #endif /* FEATURE_HTTPD_BASIC_AUTH */
/**************************************************************************** /*
* * Handle timeouts
> $Function: handle_sigalrm() */
*
* $Description: Handle timeouts
*
****************************************************************************/
static void handle_sigalrm(int sig) static void handle_sigalrm(int sig)
{ {
sendHeaders(HTTP_REQUEST_TIMEOUT); sendHeaders(HTTP_REQUEST_TIMEOUT);
alarm_signaled = 1; alarm_signaled = 1;
} }
/**************************************************************************** /*
* * Handle an incoming http request and exit.
> $Function: handleIncoming() */
* static void handle_incoming_and_exit(void) ATTRIBUTE_NORETURN;
* $Description: Handle an incoming http request. static void handle_incoming_and_exit(void)
*
****************************************************************************/
static void handleIncoming(void)
{ {
char *buf = iobuf; char *buf = iobuf;
char *url; char *url;
char *purl; char *purl;
int blank = -1; int blank = -1;
char *test; char *test;
struct stat sb; struct stat sb;
int ip_allowed; int ip_allowed;
@ -1568,9 +1552,6 @@ static void handleIncoming(void)
char *cookie = 0; char *cookie = 0;
char *content_type = 0; char *content_type = 0;
#endif #endif
fd_set s_fd;
struct timeval tv;
int retval;
struct sigaction sa; struct sigaction sa;
#if ENABLE_FEATURE_HTTPD_BASIC_AUTH #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
@ -1582,10 +1563,12 @@ static void handleIncoming(void)
sa.sa_flags = 0; /* no SA_RESTART */ sa.sa_flags = 0; /* no SA_RESTART */
sigaction(SIGALRM, &sa, NULL); sigaction(SIGALRM, &sa, NULL);
/* It's not a real loop (it ends with while(0)).
* Break from this "loop" jumps to exit(0) */
do { do {
int count; int count;
(void) alarm(TIMEOUT); alarm(TIMEOUT);
if (getLine() <= 0) if (getLine() <= 0)
break; /* closed */ break; /* closed */
@ -1696,8 +1679,7 @@ static void handleIncoming(void)
/* read until blank line for HTTP version specified, else parse immediate */ /* read until blank line for HTTP version specified, else parse immediate */
while (1) { while (1) {
alarm(TIMEOUT); alarm(TIMEOUT);
count = getLine(); if (getLine() <= 0)
if (count <= 0)
break; break;
if (DEBUG) if (DEBUG)
@ -1831,52 +1813,48 @@ static void handleIncoming(void)
bail_out: bail_out:
#endif #endif
exit(0);
#if 0 /* Is this needed? Why? */
if (DEBUG) if (DEBUG)
fprintf(stderr, "closing socket\n\n"); fprintf(stderr, "closing socket\n");
#if ENABLE_FEATURE_HTTPD_CGI #if ENABLE_FEATURE_HTTPD_CGI
free(cookie); free(cookie);
free(content_type); free(content_type);
free(referer); free(referer);
referer = NULL; referer = NULL;
# if ENABLE_FEATURE_HTTPD_BASIC_AUTH #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
free(remoteuser); free(remoteuser);
remoteuser = NULL; remoteuser = NULL;
# endif
#endif #endif
shutdown(accepted_socket, SHUT_WR); #endif
/* Properly wait for remote to closed */ /* Properly wait for remote to closed */
FD_ZERO(&s_fd); int retval;
FD_SET(accepted_socket, &s_fd); shutdown(accepted_socket, SHUT_WR);
do { do {
fd_set s_fd;
struct timeval tv;
FD_ZERO(&s_fd);
FD_SET(accepted_socket, &s_fd);
tv.tv_sec = 2; tv.tv_sec = 2;
tv.tv_usec = 0; tv.tv_usec = 0;
retval = select(accepted_socket + 1, &s_fd, NULL, NULL, &tv); retval = select(accepted_socket + 1, &s_fd, NULL, NULL, &tv);
} while (retval > 0 && read(accepted_socket, buf, sizeof(iobuf) > 0)); } while (retval > 0 && read(accepted_socket, buf, sizeof(iobuf) > 0));
shutdown(accepted_socket, SHUT_RD); shutdown(accepted_socket, SHUT_RD);
/* In inetd case, we close fd 1 (stdout) here. We will exit soon anyway */
close(accepted_socket); close(accepted_socket);
exit(0);
#endif
} }
#if BB_MMU #if BB_MMU
/**************************************************************************** /*
* * The main http server function.
> $Function: miniHttpd() * Given an open socket, listen for new connections and farm out
* * the processing as a forked process.
* $Description: The main http server function. * Never returns.
* */
* Given an open socket fildes, listen for new connections and farm out static void mini_httpd(int server) ATTRIBUTE_NORETURN;
* the processing as a forked process. static void mini_httpd(int server)
*
* $Parameters:
* (int) server. . . The server socket fildes.
*
* $Return: (int) . . . . Always 0.
*
****************************************************************************/
static int miniHttpd(int server)
{ {
fd_set readfd, portfd; fd_set readfd, portfd;
@ -1926,24 +1904,23 @@ static int miniHttpd(int server)
/* set the KEEPALIVE option to cull dead connections */ /* set the KEEPALIVE option to cull dead connections */
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
if (DEBUG || fork() == 0) { if (fork() == 0) {
/* child */ /* child */
#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP #if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
/* protect reload config, may be confuse checking */ /* Do not reload config on HUP */
signal(SIGHUP, SIG_IGN); signal(SIGHUP, SIG_IGN);
#endif #endif
handleIncoming(); handle_incoming_and_exit();
if (!DEBUG)
exit(0);
} }
close(s); close(s);
} /* while (1) */ } /* while (1) */
return 0; /* return 0; - never reached */
} }
#endif #endif
/* from inetd */ /* from inetd */
static int miniHttpd_inetd(void) static void mini_httpd_inetd(void) ATTRIBUTE_NORETURN;
static void mini_httpd_inetd(void)
{ {
union { union {
struct sockaddr sa; struct sockaddr sa;
@ -1969,8 +1946,7 @@ static int miniHttpd_inetd(void)
tcp_port = ntohs(fromAddr.sin6.sin6_port); tcp_port = ntohs(fromAddr.sin6.sin6_port);
} }
#endif #endif
handleIncoming(); handle_incoming_and_exit();
return 0;
} }
#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP #if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
@ -2017,7 +1993,6 @@ int httpd_main(int argc, char **argv)
unsigned opt; unsigned opt;
char *url_for_decode; char *url_for_decode;
USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;) USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
const char *s_port;
USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;) USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;) USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;) USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
@ -2043,7 +2018,7 @@ int httpd_main(int argc, char **argv)
USE_FEATURE_HTTPD_BASIC_AUTH(, &g_realm) USE_FEATURE_HTTPD_BASIC_AUTH(, &g_realm)
USE_FEATURE_HTTPD_AUTH_MD5(, &pass) USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
USE_FEATURE_HTTPD_SETUID(, &s_ugid) USE_FEATURE_HTTPD_SETUID(, &s_ugid)
, &s_port , &bind_addr_or_port
); );
if (opt & OPT_DECODE_URL) { if (opt & OPT_DECODE_URL) {
printf("%s", decodeString(url_for_decode, 1)); printf("%s", decodeString(url_for_decode, 1));
@ -2061,8 +2036,6 @@ int httpd_main(int argc, char **argv)
return 0; return 0;
} }
#endif #endif
if (opt & OPT_PORT)
tcp_port = xatou16(s_port);
#if ENABLE_FEATURE_HTTPD_SETUID #if ENABLE_FEATURE_HTTPD_SETUID
if (opt & OPT_SETUID) { if (opt & OPT_SETUID) {
@ -2111,14 +2084,14 @@ int httpd_main(int argc, char **argv)
parse_conf(default_path_httpd_conf, FIRST_PARSE); parse_conf(default_path_httpd_conf, FIRST_PARSE);
#endif #endif
if (opt & OPT_INETD)
return miniHttpd_inetd();
#if BB_MMU #if BB_MMU
if (opt & OPT_INETD)
mini_httpd_inetd();
if (!(opt & OPT_FOREGROUND)) if (!(opt & OPT_FOREGROUND))
bb_daemonize(0); /* don't change current directory */ bb_daemonize(0); /* don't change current directory */
return miniHttpd(server_socket); mini_httpd(server_socket); /* never returns */
#else #else
return 0; /* not reached */ mini_httpd_inetd(); /* never returns */
/* return 0; */
#endif #endif
} }

View File

@ -619,7 +619,6 @@ CONFIG_APP_UDHCPD=y
CONFIG_APP_DHCPRELAY=y CONFIG_APP_DHCPRELAY=y
CONFIG_APP_DUMPLEASES=y CONFIG_APP_DUMPLEASES=y
CONFIG_APP_UDHCPC=y CONFIG_APP_UDHCPC=y
CONFIG_FEATURE_UDHCP_SYSLOG=y
# CONFIG_FEATURE_UDHCP_DEBUG is not set # CONFIG_FEATURE_UDHCP_DEBUG is not set
CONFIG_FEATURE_RFC3397=y CONFIG_FEATURE_RFC3397=y
CONFIG_VCONFIG=y CONFIG_VCONFIG=y