From b12f7c07a9528027f808009fc7e1b8e4fd48570e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Tue, 2 Sep 2008 10:50:50 +0000 Subject: [PATCH] * close sockets on error, and abort execution --- webcit/context_loop.c | 6 +++--- webcit/webcit.c | 6 +++--- webcit/webserver.c | 30 ++++++++++++++++++------------ webcit/webserver.h | 6 +++--- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/webcit/context_loop.c b/webcit/context_loop.c index 41d39bd1e..b00852c17 100644 --- a/webcit/context_loop.c +++ b/webcit/context_loop.c @@ -163,7 +163,7 @@ int GenerateSessionID(void) /* * Collapse multiple cookies on one line */ -int req_gets(int sock, char *buf, char *hold, size_t hlen) +int req_gets(int *sock, char *buf, char *hold, size_t hlen) { int a, b; @@ -283,7 +283,7 @@ int is_bogus(char *http_cmd) { * transaction. * \param sock the socket we will put our answer to */ -void context_loop(int sock) +void context_loop(int *sock) { struct httprequest *req = NULL; struct httprequest *last = NULL; @@ -509,7 +509,7 @@ void context_loop(int sock) TheSession->urlstrings = NewHash(1,NULL); TheSession->vars = NewHash(1,NULL); - TheSession->http_sock = sock; + TheSession->http_sock = *sock; TheSession->lastreq = time(NULL); /* log */ TheSession->gzip_ok = gzip_ok; #ifdef ENABLE_NLS diff --git a/webcit/webcit.c b/webcit/webcit.c index ebaf0c82e..7e3080bce 100644 --- a/webcit/webcit.c +++ b/webcit/webcit.c @@ -1464,7 +1464,7 @@ void session_loop(struct httprequest *req) body_start = strlen(content); /** Read the entire input data at once. */ - client_read(WCC->http_sock, &content[body_start], ContentLength); + client_read(&WCC->http_sock, &content[body_start], ContentLength); if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) { StrBuf *Content; @@ -1792,9 +1792,9 @@ void diagnostics(void) output_headers(1, 1, 1, 0, 0, 0); wprintf("Session: %d
\n", WC->wc_session); wprintf("Command:
\n");
-	escputs(WC->UrlFragment1);
+	StrEscPuts(WC->UrlFragment1);
 	wprintf("
\n"); - escputs(WC->UrlFragment2); + StrEscPuts(WC->UrlFragment2); wprintf("

\n"); wprintf("Variables:
\n");
 	dump_vars();
diff --git a/webcit/webserver.c b/webcit/webserver.c
index 36c64a5f4..aa2b0d001 100644
--- a/webcit/webserver.c
+++ b/webcit/webserver.c
@@ -24,7 +24,7 @@ int is_https = 0;		/* Nonzero if I am an HTTPS service */
 int follow_xff = 0;		/* Follow X-Forwarded-For: header */
 int home_specified = 0;		/* did the user specify a homedir? */
 int time_to_die = 0;            /* Nonzero if server is shutting down */
-extern void *context_loop(int);
+extern void *context_loop(int*);
 extern void *housekeeping_loop(void);
 extern pthread_mutex_t SessionListMutex;
 extern pthread_key_t MyConKey;
@@ -193,7 +193,7 @@ int ig_uds_server(char *sockpath, int queue_len)
  *      0       Request timed out.
  *	-1   	Connection is broken, or other error.
  */
-int client_read_to(int sock, char *buf, int bytes, int timeout)
+int client_read_to(int *sock, char *buf, int bytes, int timeout)
 {
 	int len, rlen;
 	fd_set rfds;
@@ -208,22 +208,25 @@ int client_read_to(int sock, char *buf, int bytes, int timeout)
 #endif
 
 	len = 0;
-	while (len < bytes) {
+	while ((len < bytes) && (*sock > 0)) {
 		FD_ZERO(&rfds);
-		FD_SET(sock, &rfds);
+		FD_SET(*sock, &rfds);
 		tv.tv_sec = timeout;
 		tv.tv_usec = 0;
 
-		retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
-		if (FD_ISSET(sock, &rfds) == 0) {
+		retval = select((*sock) + 1, &rfds, NULL, NULL, &tv);
+		if (FD_ISSET(*sock, &rfds) == 0) {
 			return (0);
 		}
 
-		rlen = read(sock, &buf[len], bytes - len);
+		rlen = read(*sock, &buf[len], bytes - len);
 
 		if (rlen < 1) {
 			lprintf(2, "client_read() failed: %s\n",
 				strerror(errno));
+			if (*sock > 0)
+				close(*sock);
+			*sock = -1;	
 			return (-1);
 		}
 		len = len + rlen;
@@ -354,7 +357,7 @@ long end_burst(void)
  * \param buf the buffer to write to
  * \param bytes Number of bytes to read
  */
-int client_read(int sock, char *buf, int bytes)
+int client_read(int *sock, char *buf, int bytes)
 {
 	return (client_read_to(sock, buf, bytes, SLEEPING));
 }
@@ -369,13 +372,15 @@ int client_read(int sock, char *buf, int bytes)
  * \param bufsiz how many bytes to read
  * \return  number of bytes read???
  */
-int client_getln(int sock, char *buf, int bufsiz)
+int client_getln(int *sock, char *buf, int bufsiz)
 {
 	int i, retval;
 
 	/* Read one character at a time.*/
-	for (i = 0;; i++) {
+	for (i = 0; *sock > 0; i++) {
 		retval = client_read(sock, &buf[i], 1);
+		if (retval < 0)
+			return retval;
 		if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
 			break;
 		if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
@@ -994,7 +999,7 @@ void worker_entry(void)
 			if (fail_this_transaction == 0) {
 
 				/* Perform an HTTP transaction... */
-				context_loop(ssock);
+				context_loop(&ssock);
 
 				/* Shut down SSL/TLS if required... */
 #ifdef HAVE_OPENSSL
@@ -1004,7 +1009,8 @@ void worker_entry(void)
 #endif
 
 				/* ...and close the socket. */
-				lingering_close(ssock);
+				if (ssock > 0)
+					lingering_close(ssock);
 			}
 
 		}
diff --git a/webcit/webserver.h b/webcit/webserver.h
index f190ddd94..37b899cf6 100644
--- a/webcit/webserver.h
+++ b/webcit/webserver.h
@@ -5,8 +5,8 @@ extern char *static_content_dirs[PATH_MAX];  /**< Disk representation */
 extern int ndirs;
 extern char socket_dir[PATH_MAX];
 
-int client_getln(int sock, char *buf, int bufsiz);
-int client_read(int sock, char *buf, int bytes);
-int client_read_to(int sock, char *buf, int bytes, int timeout);
+int client_getln(int *sock, char *buf, int bufsiz);
+int client_read(int *sock, char *buf, int bytes);
+int client_read_to(int *sock, char *buf, int bytes, int timeout);
 int lprintf(int loglevel, const char *format, ...);
 void wc_backtrace(void);
-- 
2.39.2