]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/request.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / cups / request.c
index 1f87e77f650c23753dcc6c8928715198a2ed3ea2..39cbe6ccdf8f796bc356cd871c67a6e7b47fb44f 100644 (file)
@@ -1,34 +1,16 @@
 /*
- * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
+ * IPP utilities for CUPS.
  *
- *   IPP utilities for CUPS.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   Copyright 2007-2011 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * missing or damaged, see the license at "http://www.cups.org/".
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   cupsDoFileRequest()    - Do an IPP request with a file.
- *   cupsDoIORequest()      - Do an IPP request with file descriptors.
- *   cupsDoRequest()        - Do an IPP request.
- *   cupsGetResponse()      - Get a response to an IPP request.
- *   cupsLastError()        - Return the last IPP status code.
- *   cupsLastErrorString()  - Return the last IPP status-message.
- *   _cupsNextDelay()       - Return the next retry delay value.
- *   cupsReadResponseData() - Read additional data after the IPP response.
- *   cupsSendRequest()      - Send an IPP request.
- *   cupsWriteRequestData() - Write additional data after an IPP request.
- *   _cupsConnect()         - Get the default server connection...
- *   _cupsSetError()        - Set the last IPP status code and status-message.
- *   _cupsSetHTTPError()    - Set the last error using the HTTP status.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
 #ifndef O_BINARY
 #  define O_BINARY 0
 #endif /* O_BINARY */
+#ifndef MSG_DONTWAIT
+#  define MSG_DONTWAIT 0
+#endif /* !MSG_DONTWAIT */
 
 
 /*
  * 'cupsDoFileRequest()' - Do an IPP request with a file.
  *
- * This function sends the IPP request to the specified server, retrying
- * and authenticating as necessary.  The request is freed with @link ippDelete@
- * after receiving a valid IPP response.
+ * This function sends the IPP request and attached file to the specified
+ * server, retrying and authenticating as necessary.  The request is freed with
+ * @link ippDelete@.
  */
 
 ipp_t *                                        /* O - Response data */
@@ -66,10 +51,7 @@ cupsDoFileRequest(http_t     *http,  /* I - Connection to server or @code CUPS_HT
   int          infile;                 /* Input file */
 
 
-  DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
-                "filename=\"%s\")", http, request,
-               request ? ippOpString(request->request.op.operation_id) : "?",
-               resource, filename));
+  DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", filename=\"%s\")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource, filename));
 
   if (filename)
   {
@@ -79,7 +61,7 @@ cupsDoFileRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HT
       * Can't get file information!
       */
 
-      _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
+      _cupsSetError(errno == ENOENT ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED,
                     NULL, 0);
 
       ippDelete(request);
@@ -102,17 +84,17 @@ cupsDoFileRequest(http_t     *http,        /* I - Connection to server or @code CUPS_HT
 /*
  * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
  *
- * This function sends the IPP request to the specified server, retrying
- * and authenticating as necessary.  The request is freed with ippDelete()
- * after receiving a valid IPP response.
+ * This function sends the IPP request with the optional input file "infile" to
+ * the specified server, retrying and authenticating as necessary.  The request
+ * is freed with @link ippDelete@.
  *
- * If "infile" is a valid file descriptor, cupsDoIORequest() copies
+ * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
  * all of the data from the file after the IPP request message.
  *
- * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
+ * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
  * all of the data after the IPP response message to the file.
  *
- * @since CUPS 1.3/Mac OS X 10.5@
+ * @since CUPS 1.3/macOS 10.5@
  */
 
 ipp_t *                                        /* O - Response data */
@@ -126,14 +108,11 @@ cupsDoIORequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
   size_t       length = 0;             /* Content-Length value */
   http_status_t        status;                 /* Status of HTTP request */
   struct stat  fileinfo;               /* File information */
-  int          bytes;                  /* Number of bytes read/written */
+  ssize_t      bytes;                  /* Number of bytes read/written */
   char         buffer[32768];          /* Output buffer */
 
 
-  DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
-                "infile=%d, outfile=%d)", http, request,
-               request ? ippOpString(request->request.op.operation_id) : "?",
-               resource, infile, outfile));
+  DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", infile=%d, outfile=%d)", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource, infile, outfile));
 
  /*
   * Range check input...
@@ -143,7 +122,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
   {
     ippDelete(request);
 
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
 
     return (NULL);
   }
@@ -172,7 +151,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
       * Can't get file information!
       */
 
-      _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
+      _cupsSetError(errno == EBADF ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED,
                     NULL, 0);
 
       ippDelete(request);
@@ -192,7 +171,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
       ippDelete(request);
 
-      _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0);
+      _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE, strerror(EISDIR), 0);
 
       return (NULL);
     }
@@ -202,7 +181,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
       length = 0;                      /* Chunk when piping */
     else
 #endif /* !WIN32 */
-    length = ippLength(request) + fileinfo.st_size;
+    length = ippLength(request) + (size_t)fileinfo.st_size;
   }
   else
     length = ippLength(request);
@@ -233,7 +212,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
     DEBUG_printf(("2cupsDoIORequest: status=%d", status));
 
-    if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0)
+    if (status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA && infile >= 0)
     {
       DEBUG_puts("2cupsDoIORequest: file write...");
 
@@ -246,10 +225,10 @@ cupsDoIORequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 #endif /* WIN32 */
       lseek(infile, 0, SEEK_SET);
 
-      while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
+      while ((bytes = read(infile, buffer, sizeof(buffer))) > 0)
       {
-        if ((status = cupsWriteRequestData(http, buffer, bytes))
-                != HTTP_CONTINUE)
+        if ((status = cupsWriteRequestData(http, buffer, (size_t)bytes))
+                != HTTP_STATUS_CONTINUE)
          break;
       }
     }
@@ -258,7 +237,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
     * Get the server's response...
     */
 
-    if (status != HTTP_ERROR)
+    if (status <= HTTP_STATUS_CONTINUE || status == HTTP_STATUS_OK)
     {
       response = cupsGetResponse(http, resource);
       status   = httpGetStatus(http);
@@ -266,9 +245,9 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
     DEBUG_printf(("2cupsDoIORequest: status=%d", status));
 
-    if (status == HTTP_ERROR ||
-        (status >= HTTP_BAD_REQUEST && status != HTTP_UNAUTHORIZED &&
-        status != HTTP_UPGRADE_REQUIRED))
+    if (status == HTTP_STATUS_ERROR ||
+        (status >= HTTP_STATUS_BAD_REQUEST && status != HTTP_STATUS_UNAUTHORIZED &&
+        status != HTTP_STATUS_UPGRADE_REQUIRED))
     {
       _cupsSetHTTPError(status);
       break;
@@ -280,12 +259,12 @@ cupsDoIORequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
       * Write trailing data to file...
       */
 
-      while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
-       if (write(outfile, buffer, bytes) < bytes)
+      while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
+       if (write(outfile, buffer, (size_t)bytes) < bytes)
          break;
     }
 
-    if (http->state != HTTP_WAITING)
+    if (http->state != HTTP_STATE_WAITING)
     {
      /*
       * Flush any remaining data...
@@ -309,8 +288,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
  * 'cupsDoRequest()' - Do an IPP request.
  *
  * This function sends the IPP request to the specified server, retrying
- * and authenticating as necessary.  The request is freed with ippDelete()
- * after receiving a valid IPP response.
+ * and authenticating as necessary.  The request is freed with @link ippDelete@.
  */
 
 ipp_t *                                        /* O - Response data */
@@ -318,10 +296,7 @@ cupsDoRequest(http_t     *http,            /* I - Connection to server or @code CUPS_HTTP_
               ipp_t      *request,     /* I - IPP request */
               const char *resource)    /* I - HTTP resource for POST */
 {
-  DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
-                http, request,
-               request ? ippOpString(request->request.op.operation_id) : "?",
-               resource));
+  DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource));
 
   return (cupsDoIORequest(http, request, resource, -1, -1));
 }
@@ -331,11 +306,11 @@ cupsDoRequest(http_t     *http,           /* I - Connection to server or @code CUPS_HTTP_
  * 'cupsGetResponse()' - Get a response to an IPP request.
  *
  * Use this function to get the response for an IPP request sent using
- * cupsSendDocument() or cupsSendRequest(). For requests that return
- * additional data, use httpRead() after getting a successful response,
- * otherwise call httpFlush() to complete the response processing.
+ * @link cupsSendRequest@. For requests that return additional data, use
+ * @link cupsReadResponseData@ after getting a successful response,
+ * otherwise call @link httpFlush@ to complete the response processing.
  *
- * @since CUPS 1.4/Mac OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
  */
 
 ipp_t *                                        /* O - Response or @code NULL@ on HTTP error */
@@ -347,23 +322,38 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
   ipp_t                *response = NULL;       /* IPP response */
 
 
-  DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http, resource));
+  DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", (void *)http, resource));
+  DEBUG_printf(("1cupsGetResponse: http->state=%d", http ? http->state : HTTP_STATE_ERROR));
 
  /*
   * Connect to the default server as needed...
   */
 
   if (!http)
-    http = _cupsConnect();
+  {
+    _cups_globals_t *cg = _cupsGlobals();
+                                       /* Pointer to library globals */
+
+    if ((http = cg->http) == NULL)
+    {
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection."), 1);
+      DEBUG_puts("1cupsGetResponse: No active connection - returning NULL.");
+      return (NULL);
+    }
+  }
 
-  if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND))
+  if (http->state != HTTP_STATE_POST_RECV && http->state != HTTP_STATE_POST_SEND)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request sent."), 1);
+    DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL.");
     return (NULL);
+  }
 
  /*
   * Check for an unfinished chunked request...
   */
 
-  if (http->data_encoding == HTTP_ENCODE_CHUNKED)
+  if (http->data_encoding == HTTP_ENCODING_CHUNKED)
   {
    /*
     * Send a 0-length chunk to finish off the request...
@@ -386,11 +376,11 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
   {
     status = httpUpdate(http);
   }
-  while (status != HTTP_ERROR && http->state == HTTP_POST_RECV);
+  while (status == HTTP_STATUS_CONTINUE);
 
   DEBUG_printf(("2cupsGetResponse: status=%d", status));
 
-  if (status == HTTP_OK)
+  if (status == HTTP_STATUS_OK)
   {
    /*
     * Get the IPP response...
@@ -398,11 +388,11 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 
     response = ippNew();
 
-    while ((state = ippRead(http, response)) != IPP_DATA)
-      if (state == IPP_ERROR)
+    while ((state = ippRead(http, response)) != IPP_STATE_DATA)
+      if (state == IPP_STATE_ERROR)
        break;
 
-    if (state == IPP_ERROR)
+    if (state == IPP_STATE_ERROR)
     {
      /*
       * Flush remaining data and delete the response...
@@ -415,12 +405,11 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
       ippDelete(response);
       response = NULL;
 
-      _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
-      http->status = status = HTTP_ERROR;
-      http->error  = EIO;
+      http->status = status = HTTP_STATUS_ERROR;
+      http->error  = EINVAL;
     }
   }
-  else if (status != HTTP_ERROR)
+  else if (status != HTTP_STATUS_ERROR)
   {
    /*
     * Flush any error message...
@@ -432,7 +421,7 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
     * Then handle encryption and authentication...
     */
 
-    if (status == HTTP_UNAUTHORIZED)
+    if (status == HTTP_STATUS_UNAUTHORIZED)
     {
      /*
       * See if we can do authentication...
@@ -441,13 +430,13 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
       DEBUG_puts("2cupsGetResponse: Need authorization...");
 
       if (!cupsDoAuthentication(http, "POST", resource))
-        httpReconnect(http);
+        httpReconnect2(http, 30000, NULL);
       else
-        http->status = status = HTTP_AUTHORIZATION_CANCELED;
+        http->status = status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
     }
 
 #ifdef HAVE_SSL
-    else if (status == HTTP_UPGRADE_REQUIRED)
+    else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
     {
      /*
       * Force a reconnect with encryption...
@@ -455,8 +444,8 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
       DEBUG_puts("2cupsGetResponse: Need encryption...");
 
-      if (!httpReconnect(http))
-        httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+      if (!httpReconnect2(http, 30000, NULL))
+        httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
     }
 #endif /* HAVE_SSL */
   }
@@ -476,15 +465,14 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
                   attr ? attr->values[0].string.text :
                      ippErrorString(response->request.status.status_code), 0);
   }
-  else if (status != HTTP_OK)
-    _cupsSetHTTPError(status);
 
   return (response);
 }
 
 
 /*
- * 'cupsLastError()' - Return the last IPP status code.
+ * 'cupsLastError()' - Return the last IPP status code received on the current
+ *                     thread.
  */
 
 ipp_status_t                           /* O - IPP status code from last request */
@@ -495,9 +483,10 @@ cupsLastError(void)
 
 
 /*
- * 'cupsLastErrorString()' - Return the last IPP status-message.
+ * 'cupsLastErrorString()' - Return the last IPP status-message received on the
+ *                           current thread.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 const char *                           /* O - status-message text from last request */
@@ -540,10 +529,11 @@ _cupsNextDelay(int current,               /* I  - Current delay value or 0 */
 /*
  * 'cupsReadResponseData()' - Read additional data after the IPP response.
  *
- * This function is used after cupsGetResponse() to read the PPD or document
- * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
+ * This function is used after @link cupsGetResponse@ to read the PPD or document
+ * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
+ * respectively.
  *
- * @since CUPS 1.4/Mac OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
  */
 
 ssize_t                                        /* O - Bytes read, 0 on EOF, -1 on error */
@@ -556,8 +546,7 @@ cupsReadResponseData(
   * Get the default connection as needed...
   */
 
-  DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
-                "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
+  DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
   if (!http)
   {
@@ -566,7 +555,7 @@ cupsReadResponseData(
 
     if ((http = cg->http) == NULL)
     {
-      _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
       return (-1);
     }
   }
@@ -582,15 +571,19 @@ cupsReadResponseData(
 /*
  * 'cupsSendRequest()' - Send an IPP request.
  *
- * Use httpWrite() to write any additional data (document, PPD file, etc.)
- * for the request, cupsGetResponse() to get the IPP response, and httpRead()
- * to read any additional data following the response. Only one request can be
- * sent/queued at a time.
+ * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
+ * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
+ * and @link cupsReadResponseData@ to read any additional data following the
+ * response. Only one request can be sent/queued at a time per @code http_t@
+ * connection.
+ *
+ * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
+ * on a successful send of the request.
  *
- * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
- * request is not freed.
+ * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
+ * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
  *
- * @since CUPS 1.4/Mac OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
  */
 
 http_status_t                          /* O - Initial HTTP status */
@@ -599,16 +592,13 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
                 const char *resource,  /* I - Resource path */
                size_t     length)      /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
 {
-  http_status_t        status;                 /* Status of HTTP request */
-  int          got_status;             /* Did we get the status? */
-  ipp_state_t  state;                  /* State of IPP processing */
-  http_status_t        expect;                 /* Expect: header to use */
+  http_status_t                status;         /* Status of HTTP request */
+  int                  got_status;     /* Did we get the status? */
+  ipp_state_t          state;          /* State of IPP processing */
+  http_status_t                expect;         /* Expect: header to use */
 
 
-  DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
-                "length=" CUPS_LLFMT ")", http, request,
-               request ? ippOpString(request->request.op.operation_id) : "?",
-               resource, CUPS_LLCAST length));
+  DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", length=" CUPS_LLFMT ")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource, CUPS_LLCAST length));
 
  /*
   * Range check input...
@@ -616,9 +606,9 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
   if (!request || !resource)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -627,25 +617,24 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 
   if (!http)
     if ((http = _cupsConnect()) == NULL)
-      return (HTTP_SERVICE_UNAVAILABLE);
+      return (HTTP_STATUS_SERVICE_UNAVAILABLE);
 
  /*
   * If the prior request was not flushed out, do so now...
   */
 
-  if (http->state == HTTP_GET_SEND ||
-      http->state == HTTP_POST_SEND)
+  if (http->state == HTTP_STATE_GET_SEND ||
+      http->state == HTTP_STATE_POST_SEND)
   {
     DEBUG_puts("2cupsSendRequest: Flush prior response.");
     httpFlush(http);
   }
-  else if (http->state != HTTP_WAITING)
+  else if (http->state != HTTP_STATE_WAITING)
   {
-    DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), bailing.",
-                  http->state));
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
-
-    return (HTTP_ERROR);
+    DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
+                  "reconnecting.", http->state));
+    if (httpReconnect2(http, 30000, NULL))
+      return (HTTP_STATUS_ERROR);
   }
 
 #ifdef HAVE_SSL
@@ -657,10 +646,10 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 
   if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
       !httpAddrLocalhost(http->hostaddr) && !http->tls &&
-      httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
+      httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
   {
     DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
-    return (HTTP_SERVICE_UNAVAILABLE);
+    return (HTTP_STATUS_SERVICE_UNAVAILABLE);
   }
 #endif /* HAVE_SSL */
 
@@ -672,10 +661,10 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
   {
     DEBUG_puts("2cupsSendRequest: Connection: close");
     httpClearFields(http);
-    if (httpReconnect(http))
+    if (httpReconnect2(http, 30000, NULL))
     {
       DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
-      return (HTTP_SERVICE_UNAVAILABLE);
+      return (HTTP_STATUS_SERVICE_UNAVAILABLE);
     }
   }
 
@@ -683,7 +672,7 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
   * Loop until we can send the request without authorization problems.
   */
 
-  expect = HTTP_CONTINUE;
+  expect = HTTP_STATUS_CONTINUE;
 
   for (;;)
   {
@@ -723,10 +712,10 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
     if (httpPost(http, resource))
     {
       DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
         DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
-        return (HTTP_SERVICE_UNAVAILABLE);
+        return (HTTP_STATUS_SERVICE_UNAVAILABLE);
       }
       else
         continue;
@@ -738,30 +727,44 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 
     DEBUG_puts("2cupsSendRequest: Writing IPP request...");
 
-    request->state = IPP_IDLE;
-    status         = HTTP_CONTINUE;
+    request->state = IPP_STATE_IDLE;
+    status         = HTTP_STATUS_CONTINUE;
     got_status     = 0;
 
-    while ((state = ippWrite(http, request)) != IPP_DATA)
-      if (state == IPP_ERROR)
-       break;
-      else if (httpCheck(http))
+    while ((state = ippWrite(http, request)) != IPP_STATE_DATA)
+    {
+      if (httpCheck(http))
       {
         got_status = 1;
 
         _httpUpdate(http, &status);
-       if (status >= HTTP_MULTIPLE_CHOICES)
+       if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
          break;
       }
+      else if (state == IPP_STATE_ERROR)
+       break;
+    }
 
-    if (state == IPP_ERROR)
+    if (state == IPP_STATE_ERROR)
     {
-      DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
+     /*
+      * We weren't able to send the IPP request. But did we already get a HTTP
+      * error status?
+      */
+
+      if (!got_status || status < HTTP_STATUS_MULTIPLE_CHOICES)
+      {
+       /*
+        * No, something else went wrong.
+       */
+
+       DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
 
-      http->status = HTTP_ERROR;
-      http->state  = HTTP_WAITING;
+       http->status = HTTP_STATUS_ERROR;
+       http->state  = HTTP_STATE_WAITING;
 
-      return (HTTP_ERROR);
+       return (HTTP_STATUS_ERROR);
+      }
     }
 
    /*
@@ -770,7 +773,7 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
     if (!got_status)
     {
-      if (expect == HTTP_CONTINUE)
+      if (expect == HTTP_STATUS_CONTINUE)
       {
        DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
 
@@ -787,69 +790,72 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
     * Process the current HTTP status...
     */
 
-    if (status >= HTTP_MULTIPLE_CHOICES)
+    if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
     {
+      int temp_status;                 /* Temporary status */
+
       _cupsSetHTTPError(status);
 
       do
       {
-       status = httpUpdate(http);
+       temp_status = httpUpdate(http);
       }
-      while (status != HTTP_ERROR && http->state == HTTP_POST_RECV);
+      while (temp_status != HTTP_STATUS_ERROR &&
+             http->state == HTTP_STATE_POST_RECV);
 
       httpFlush(http);
     }
 
     switch (status)
     {
-      case HTTP_ERROR :
-      case HTTP_CONTINUE :
-      case HTTP_OK :
+      case HTTP_STATUS_CONTINUE :
+      case HTTP_STATUS_OK :
+      case HTTP_STATUS_ERROR :
           DEBUG_printf(("1cupsSendRequest: Returning %d.", status));
           return (status);
 
-      case HTTP_UNAUTHORIZED :
+      case HTTP_STATUS_UNAUTHORIZED :
           if (cupsDoAuthentication(http, "POST", resource))
          {
-            DEBUG_puts("1cupsSendRequest: Returning HTTP_AUTHORIZATION_CANCELED.");
-           return (HTTP_AUTHORIZATION_CANCELED);
+            DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
+           return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED);
          }
 
-          DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_UNAUTHORIZED.");
+          DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
 
-         if (httpReconnect(http))
+         if (httpReconnect2(http, 30000, NULL))
          {
            DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
-           return (HTTP_SERVICE_UNAVAILABLE);
+           return (HTTP_STATUS_SERVICE_UNAVAILABLE);
          }
          break;
 
 #ifdef HAVE_SSL
-      case HTTP_UPGRADE_REQUIRED :
+      case HTTP_STATUS_UPGRADE_REQUIRED :
         /*
          * Flush any error message, reconnect, and then upgrade with
          * encryption...
          */
 
           DEBUG_puts("2cupsSendRequest: Reconnecting after "
-                    "HTTP_UPGRADE_REQUIRED.");
+                    "HTTP_STATUS_UPGRADE_REQUIRED.");
 
-         if (httpReconnect(http))
+         if (httpReconnect2(http, 30000, NULL))
          {
            DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
-           return (HTTP_SERVICE_UNAVAILABLE);
+           return (HTTP_STATUS_SERVICE_UNAVAILABLE);
          }
 
          DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
-         if (httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
+         if (httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
          {
            DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
-           return (HTTP_SERVICE_UNAVAILABLE);
+           return (HTTP_STATUS_SERVICE_UNAVAILABLE);
          }
          break;
 #endif /* HAVE_SSL */
 
-      case HTTP_EXPECTATION_FAILED :
+      case HTTP_STATUS_EXPECTATION_FAILED :
         /*
          * Don't try using the Expect: header the next time around...
          */
@@ -859,10 +865,10 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
           DEBUG_puts("2cupsSendRequest: Reconnecting after "
                     "HTTP_EXPECTATION_FAILED.");
 
-         if (httpReconnect(http))
+         if (httpReconnect2(http, 30000, NULL))
          {
            DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
-           return (HTTP_SERVICE_UNAVAILABLE);
+           return (HTTP_STATUS_SERVICE_UNAVAILABLE);
          }
          break;
 
@@ -883,10 +889,10 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
  * This function is used after @link cupsSendRequest@ to provide a PPD and
  * after @link cupsStartDocument@ to provide a document file.
  *
- * @since CUPS 1.4/Mac OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
  */
 
-http_status_t                          /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
+http_status_t                          /* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
 cupsWriteRequestData(
     http_t     *http,                  /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     const char *buffer,                        /* I - Bytes to write */
@@ -899,8 +905,7 @@ cupsWriteRequestData(
   * Get the default connection as needed...
   */
 
-  DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
-                "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
+  DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
   if (!http)
   {
@@ -909,9 +914,9 @@ cupsWriteRequestData(
 
     if ((http = cg->http) == NULL)
     {
-      _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
-      DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
-      return (HTTP_ERROR);
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
+      DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
+      return (HTTP_STATUS_ERROR);
     }
   }
 
@@ -923,9 +928,9 @@ cupsWriteRequestData(
 
   if (httpWrite2(http, buffer, length) < 0)
   {
-    DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(http->error), 0);
-    return (HTTP_ERROR);
+    DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -934,7 +939,7 @@ cupsWriteRequestData(
 
   if (length >= HTTP_MAX_BUFFER ||
       http->wused < wused ||
-      (wused > 0 && http->wused == length))
+      (wused > 0 && (size_t)http->wused == length))
   {
    /*
     * We've written something to the server, so check for response data...
@@ -945,7 +950,7 @@ cupsWriteRequestData(
       http_status_t    status;         /* Status from _httpUpdate */
 
       _httpUpdate(http, &status);
-      if (status >= HTTP_MULTIPLE_CHOICES)
+      if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
       {
         _cupsSetHTTPError(status);
 
@@ -953,7 +958,7 @@ cupsWriteRequestData(
        {
          status = httpUpdate(http);
        }
-       while (status != HTTP_ERROR && http->state == HTTP_POST_RECV);
+       while (status != HTTP_STATUS_ERROR && http->state == HTTP_STATE_POST_RECV);
 
         httpFlush(http);
       }
@@ -963,8 +968,8 @@ cupsWriteRequestData(
     }
   }
 
-  DEBUG_puts("1cupsWriteRequestData: Returning HTTP_CONTINUE.");
-  return (HTTP_CONTINUE);
+  DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
+  return (HTTP_STATUS_CONTINUE);
 }
 
 
@@ -991,9 +996,9 @@ _cupsConnect(void)
     */
 
     if (strcmp(cg->http->hostname, cg->server) ||
-        cg->ipp_port != _httpAddrPort(cg->http->hostaddr) ||
+        cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
         (cg->http->encryption != cg->encryption &&
-        cg->http->encryption == HTTP_ENCRYPT_NEVER))
+        cg->http->encryption == HTTP_ENCRYPTION_NEVER))
     {
      /*
       * Need to close the current connection because something has changed...
@@ -1002,6 +1007,31 @@ _cupsConnect(void)
       httpClose(cg->http);
       cg->http = NULL;
     }
+    else
+    {
+     /*
+      * Same server, see if the connection is still established...
+      */
+
+      char     ch;                     /* Connection check byte */
+      ssize_t  n;                      /* Number of bytes */
+
+#ifdef WIN32
+      if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK)) == 0 ||
+          (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK))
+#else
+      if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK | MSG_DONTWAIT)) == 0 ||
+          (n < 0 && errno != EWOULDBLOCK))
+#endif /* WIN32 */
+      {
+       /*
+        * Nope, close the connection...
+        */
+
+       httpClose(cg->http);
+       cg->http = NULL;
+      }
+    }
   }
 
  /*
@@ -1010,13 +1040,13 @@ _cupsConnect(void)
 
   if (!cg->http)
   {
-    if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption())) == NULL)
+    if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
+                                cupsEncryption(), 1, 30000, NULL)) == NULL)
     {
       if (errno)
-        _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+        _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
       else
-        _cupsSetError(IPP_SERVICE_UNAVAILABLE,
+        _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
                      _("Unable to connect to host."), 1);
     }
   }
@@ -1089,59 +1119,54 @@ _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
 {
   switch (status)
   {
-    case HTTP_NOT_FOUND :
-       _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0);
+    case HTTP_STATUS_NOT_FOUND :
+       _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, httpStatus(status), 0);
        break;
 
-    case HTTP_UNAUTHORIZED :
-       _cupsSetError(IPP_NOT_AUTHENTICATED, httpStatus(status), 0);
+    case HTTP_STATUS_UNAUTHORIZED :
+       _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED, httpStatus(status), 0);
        break;
 
-    case HTTP_AUTHORIZATION_CANCELED :
-       _cupsSetError(IPP_AUTHENTICATION_CANCELED, httpStatus(status), 0);
+    case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED :
+       _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED, httpStatus(status), 0);
        break;
 
-    case HTTP_FORBIDDEN :
-       _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0);
+    case HTTP_STATUS_FORBIDDEN :
+       _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN, httpStatus(status), 0);
        break;
 
-    case HTTP_BAD_REQUEST :
-       _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0);
+    case HTTP_STATUS_BAD_REQUEST :
+       _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST, httpStatus(status), 0);
        break;
 
-    case HTTP_REQUEST_TOO_LARGE :
-       _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0);
+    case HTTP_STATUS_REQUEST_TOO_LARGE :
+       _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE, httpStatus(status), 0);
        break;
 
-    case HTTP_NOT_IMPLEMENTED :
-       _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
+    case HTTP_STATUS_NOT_IMPLEMENTED :
+       _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
        break;
 
-    case HTTP_NOT_SUPPORTED :
-       _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
+    case HTTP_STATUS_NOT_SUPPORTED :
+       _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
        break;
 
-    case HTTP_UPGRADE_REQUIRED :
-       _cupsSetError(IPP_UPGRADE_REQUIRED, httpStatus(status), 0);
+    case HTTP_STATUS_UPGRADE_REQUIRED :
+       _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED, httpStatus(status), 0);
         break;
 
-    case HTTP_PKI_ERROR :
-       _cupsSetError(IPP_PKI_ERROR, httpStatus(status), 0);
+    case HTTP_STATUS_CUPS_PKI_ERROR :
+       _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, httpStatus(status), 0);
         break;
 
-    case HTTP_ERROR :
-       _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+    case HTTP_STATUS_ERROR :
+       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
         break;
 
     default :
        DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
-                     "IPP_SERVICE_UNAVAILABLE!", status));
-       _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0);
+                     "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status));
+       _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, httpStatus(status), 0);
        break;
   }
 }
-
-
-/*
- * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".
- */