]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Clean up read buffering code to deal with sporadic errors.
authormike <mike@7a7537e8-13f0-0310-91df-b6672ffda945>
Wed, 7 Sep 2011 23:45:35 +0000 (23:45 +0000)
committermike <mike@7a7537e8-13f0-0310-91df-b6672ffda945>
Wed, 7 Sep 2011 23:45:35 +0000 (23:45 +0000)
git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@9972 7a7537e8-13f0-0310-91df-b6672ffda945

cups/http.c

index 54f25ef184862332d738a4f264b42cc89a71064e..7d801fe0574a93796f48beaf5955ac30e2700b7c 100644 (file)
@@ -1910,6 +1910,8 @@ httpRead2(http_t *http,                   /* I - Connection to server */
     * Buffer small reads for better performance...
     */
 
+    ssize_t    buflen;                 /* Length of read for buffer */
+
     if (!http->blocking)
     {
       while (!httpWait(http, http->wait_value))
@@ -1922,65 +1924,65 @@ httpRead2(http_t *http,                 /* I - Connection to server */
     }
 
     if (http->data_remaining > sizeof(http->buffer))
-      bytes = sizeof(http->buffer);
+      buflen = sizeof(http->buffer);
     else
-      bytes = http->data_remaining;
+      buflen = http->data_remaining;
 
-    DEBUG_printf(("2httpRead2: Reading %d bytes into buffer.", (int)bytes));
+    DEBUG_printf(("2httpRead2: Reading %d bytes into buffer.", (int)buflen));
 
+    do
+    {
 #ifdef HAVE_SSL
-    if (http->tls)
-      bytes = http_read_ssl(http, http->buffer, bytes);
-    else
+      if (http->tls)
+       bytes = http_read_ssl(http, http->buffer, buflen);
+      else
 #endif /* HAVE_SSL */
-    bytes = recv(http->fd, http->buffer, bytes, 0);
+      bytes = recv(http->fd, http->buffer, buflen, 0);
 
-    DEBUG_printf(("2httpRead2: Read %d bytes into buffer.", (int)bytes));
-
-    if (bytes > 0)
-      http->used = bytes;
-    else if (bytes < 0)
-    {
-#ifdef WIN32
-      if (WSAGetLastError() != WSAEINTR)
-      {
-        http->error = WSAGetLastError();
-        return (-1);
-      }
-      else if (WSAGetLastError() == WSAEWOULDBLOCK)
+      if (bytes < 0)
       {
-        if (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data))
+#ifdef WIN32
+       if (WSAGetLastError() != WSAEINTR)
        {
-         http->error = WSAEWOULDBLOCK;
+         http->error = WSAGetLastError();
          return (-1);
        }
-      }
+       else if (WSAGetLastError() == WSAEWOULDBLOCK)
+       {
+         if (!http->timeout_cb ||
+             !(*http->timeout_cb)(http, http->timeout_data))
+         {
+           http->error = WSAEWOULDBLOCK;
+           return (-1);
+         }
+       }
 #else
-      if (errno == EWOULDBLOCK || errno == EAGAIN)
-      {
-        if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data))
+       if (errno == EWOULDBLOCK || errno == EAGAIN)
        {
-         http->error = errno;
-         return (-1);
+         if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data))
+         {
+           http->error = errno;
+           return (-1);
+         }
+         else if (!http->timeout_cb && errno != EAGAIN)
+         {
+           http->error = errno;
+           return (-1);
+         }
        }
-       else if (!http->timeout_cb && errno != EAGAIN)
+       else if (errno != EINTR)
        {
          http->error = errno;
          return (-1);
        }
-      }
-      else if (errno != EINTR)
-      {
-        http->error = errno;
-        return (-1);
-      }
 #endif /* WIN32 */
+      }
     }
-    else
-    {
-      http->error = EPIPE;
-      return (0);
-    }
+    while (bytes < 0);
+
+    DEBUG_printf(("2httpRead2: Read %d bytes into buffer.", (int)bytes));
+
+    http->used = bytes;
   }
 
   if (http->used > 0)
@@ -2013,7 +2015,28 @@ httpRead2(http_t *http,                  /* I - Connection to server */
       }
     }
 
-    bytes = (ssize_t)http_read_ssl(http, buffer, (int)length);
+    while ((bytes = (ssize_t)http_read_ssl(http, buffer, (int)length)) < 0)
+    {
+#ifdef WIN32
+      if (WSAGetLastError() == WSAEWOULDBLOCK)
+      {
+        if (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data))
+         break;
+      }
+      else if (WSAGetLastError() != WSAEINTR)
+        break;
+#else
+      if (errno == EWOULDBLOCK || errno == EAGAIN)
+      {
+        if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data))
+         break;
+        else if (!http->timeout_cb && errno != EAGAIN)
+         break;
+      }
+      else if (errno != EINTR)
+        break;
+#endif /* WIN32 */
+    }
   }
 #endif /* HAVE_SSL */
   else