]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Fix httpWrite() bug which caused a variety of errors.
authormike <mike@7a7537e8-13f0-0310-91df-b6672ffda945>
Tue, 27 Sep 2005 03:37:52 +0000 (03:37 +0000)
committermike <mike@7a7537e8-13f0-0310-91df-b6672ffda945>
Tue, 27 Sep 2005 03:37:52 +0000 (03:37 +0000)
cups/http.c:
    - httpConnectEncrypt(): Update DEBUG_printf.
    - httpPrintf(): Check return value of httpFlushWrite().
    - httpUpdate(): Check return value of httpFlushWrite().
    - httpWrite(): Save bytes written in "bytes" instead of
      "length" and update DEBUG_printf's.
    - http_write_chunk(): Add DEBUG_printf's.

scheduler/client.c:
    - CloseClient(): Close pipes and files before socket.
    - WriteClient(): Add L_DEBUG2 log messages.

git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@4707 7a7537e8-13f0-0310-91df-b6672ffda945

cups/http.c
scheduler/client.c

index 9180c8d70765cc6cced483e2def2002ce851966c..11fe893cdea1b48aacf5444790598188bcf95f01 100644 (file)
@@ -292,8 +292,8 @@ httpConnectEncrypt(
   struct hostent       *hostaddr;      /* Host address data */
 
 
-  DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encrypt=%d)\n",
-                host ? host : "(null)", port, encrypt));
+  DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n",
+                host ? host : "(null)", port, encryption));
 
   if (!host)
     return (NULL);
@@ -1231,7 +1231,12 @@ httpPrintf(http_t     *http,             /* I - HTTP data */
   DEBUG_printf(("httpPrintf: %s", buf));
 
   if (http->wused)
-    httpFlushWrite(http);
+  {
+    DEBUG_puts("    flushing existing data...");
+
+    if (httpFlushWrite(http) < 0)
+      return (-1);
+  }
 
   return (http_write(http, buf, bytes));
 }
@@ -1693,7 +1698,12 @@ httpUpdate(http_t *http)         /* I - HTTP data */
   */
 
   if (http->wused)
-    httpFlushWrite(http);
+  {
+    DEBUG_puts("    flushing buffer...");
+
+    if (httpFlushWrite(http) < 0)
+      return (HTTP_ERROR);
+  }
 
  /*
   * If we haven't issued any commands, then there is nothing to "update"...
@@ -1882,6 +1892,9 @@ httpWrite(http_t     *http,               /* I - HTTP data */
   int  bytes;                          /* Bytes written */
 
 
+  DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http,
+                buffer, length));
+
  /*
   * Range check input...
   */
@@ -1901,8 +1914,13 @@ httpWrite(http_t     *http,              /* I - HTTP data */
 
   if (length > 0)
   {
-    if ((length + http->wused) > sizeof(http->wbuffer))
+    if (http->wused && (length + http->wused) > sizeof(http->wbuffer))
+    {
+      DEBUG_printf(("    flushing buffer (wused=%d, length=%d)\n",
+                    http->wused, length));
+
       httpFlushWrite(http);
+    }
 
     if ((length + http->wused) <= sizeof(http->wbuffer))
     {
@@ -1910,6 +1928,8 @@ httpWrite(http_t     *http,               /* I - HTTP data */
       * Write to buffer...
       */
 
+      DEBUG_printf(("    copying %d bytes to wbuffer...\n", length));
+
       memcpy(http->wbuffer + http->wused, buffer, length);
       http->wused += length;
       bytes = length;
@@ -1920,10 +1940,14 @@ httpWrite(http_t     *http,             /* I - HTTP data */
       * Otherwise write the data directly...
       */
 
+      DEBUG_printf(("    writing %d bytes to socket...\n", length));
+
       if (http->data_encoding == HTTP_ENCODE_CHUNKED)
-       length = http_write_chunk(http, buffer, length);
+       bytes = http_write_chunk(http, buffer, length);
       else
-       length = http_write(http, buffer, length);
+       bytes = http_write(http, buffer, length);
+
+      DEBUG_printf(("    wrote %d bytes...\n", bytes));
     }
 
     if (http->data_encoding == HTTP_ENCODE_LENGTH)
@@ -2669,13 +2693,22 @@ http_write_chunk(http_t     *http,      /* I - HTTP data */
 
   sprintf(header, "%x\r\n", length);
   if (http_write(http, header, strlen(header)) < 0)
+  {
+    DEBUG_puts("    http_write of length failed!");
     return (-1);
+  }
 
   if ((bytes = http_write(http, buffer, length)) < 0)
+  {
+    DEBUG_puts("    http_write of buffer failed!");
     return (-1);
+  }
 
   if (http_write(http, "\r\n", 2) < 0)
+  {
+    DEBUG_puts("    http_write of CR LF failed!");
     return (-1);
+  }
 
   return (bytes);
 }
index 1d2a4ca76d20808a96d64737e21d9b1aa02a1352..99e3b56f09830f87f47a785013726df67f2c06e9 100644 (file)
@@ -478,6 +478,33 @@ CloseClient(client_t *con) /* I - Client to close */
   }
 #endif /* HAVE_SSL */
 
+  if (con->pipe_pid != 0)
+  {
+   /*
+    * Stop any CGI process...
+    */
+
+    LogMessage(L_DEBUG2, "CloseClient: %d Killing process ID %d...",
+               con->http.fd, con->pipe_pid);
+    cupsdEndProcess(con->pipe_pid, 1);
+  }
+
+  if (con->file >= 0)
+  {
+    if (FD_ISSET(con->file, InputSet))
+    {
+      LogMessage(L_DEBUG2, "CloseClient: %d Removing fd %d from InputSet...",
+                con->http.fd, con->file);
+      FD_CLR(con->file, InputSet);
+    }
+
+    LogMessage(L_DEBUG2, "CloseClient: %d Closing data file %d.",
+               con->http.fd, con->file);
+
+    close(con->file);
+    con->file = -1;
+  }
+
  /*
   * Close the socket and clear the file from the input set for select()...
   */
@@ -510,33 +537,6 @@ CloseClient(client_t *con) /* I - Client to close */
     }
   }
 
-  if (con->pipe_pid != 0)
-  {
-   /*
-    * Stop any CGI process...
-    */
-
-    LogMessage(L_DEBUG2, "CloseClient: %d Killing process ID %d...",
-               con->http.fd, con->pipe_pid);
-    cupsdEndProcess(con->pipe_pid, 1);
-  }
-
-  if (con->file >= 0)
-  {
-    if (FD_ISSET(con->file, InputSet))
-    {
-      LogMessage(L_DEBUG2, "CloseClient: %d Removing fd %d from InputSet...",
-                con->http.fd, con->file);
-      FD_CLR(con->file, InputSet);
-    }
-
-    LogMessage(L_DEBUG2, "CloseClient: %d Closing data file %d.",
-               con->http.fd, con->file);
-
-    close(con->file);
-    con->file = -1;
-  }
-
   if (!partial)
   {
    /*
@@ -2340,10 +2340,8 @@ WriteClient(client_t *con)               /* I - Client connection */
   }
   else if ((bytes = read(con->file, buf, sizeof(buf) - 1)) > 0)
   {
-#ifdef DEBUG
     LogMessage(L_DEBUG2, "WriteClient: Read %d bytes from file %d...",
                bytes, con->file);
-#endif /* DEBUG */
 
     if (con->pipe_pid && !con->got_fields)
     {
@@ -2415,6 +2413,9 @@ WriteClient(client_t *con)                /* I - Client connection */
        else if (*bufptr != '\r')
          con->field_col ++;
 
+      LogMessage(L_DEBUG2, "WriteClient: %d bytes=%d, got_fields=%d",
+                 con->http.fd, bytes, con->got_fields);
+
       if (bytes > 0 && !con->got_fields)
       {
        /*
@@ -2435,6 +2436,9 @@ WriteClient(client_t *con)                /* I - Client connection */
 
     if (httpWrite(HTTP(con), buf, bytes) < 0)
     {
+      LogMessage(L_DEBUG2, "WriteClient: %d Write of %d bytes failed!",
+                 con->http.fd, bytes);
+
       CloseClient(con);
       return (0);
     }
@@ -2444,6 +2448,8 @@ WriteClient(client_t *con)                /* I - Client connection */
 
   if (bytes <= 0)
   {
+    LogMessage(L_DEBUG2, "WriteClient: %d bytes < 0", con->http.fd);
+
     LogRequest(con, HTTP_OK);
 
     httpFlushWrite(HTTP(con));