* close sockets on error, and abort execution
authorWilfried Göesgens <willi@citadel.org>
Tue, 2 Sep 2008 10:50:50 +0000 (10:50 +0000)
committerWilfried Göesgens <willi@citadel.org>
Tue, 2 Sep 2008 10:50:50 +0000 (10:50 +0000)
webcit/context_loop.c
webcit/webcit.c
webcit/webserver.c
webcit/webserver.h

index 41d39bd1e37169381e58a3c659f49d1a7e9d1bec..b00852c179b252aed9c8f2da81e2a3cd428ffef4 100644 (file)
@@ -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
index ebaf0c82e1fc440a4c7faa8551197ef488c01cf7..7e3080bcec5fce19606bdf5d71b33c86639d975c 100644 (file)
@@ -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<hr />\n", WC->wc_session);
        wprintf("Command: <br /><PRE>\n");
-       escputs(WC->UrlFragment1);
+       StrEscPuts(WC->UrlFragment1);
        wprintf("<br />\n");
-       escputs(WC->UrlFragment2);
+       StrEscPuts(WC->UrlFragment2);
        wprintf("</PRE><hr />\n");
        wprintf("Variables: <br /><PRE>\n");
        dump_vars();
index 36c64a5f4155863c1dc82c815361cb49216de463..aa2b0d0012e48e4586a9628c087fccb39c32514d 100644 (file)
@@ -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);
                        }
 
                }
index f190ddd94208404ff3d74ce891f0c2312b87a5ca..37b899cf6ea4c70cd423d409efb5b2301a4fe573 100644 (file)
@@ -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);