]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/request.c
Merge changes from CUPS 1.5b1-r9798.
[thirdparty/cups.git] / cups / request.c
index d0c5e320930848200db45721f50e6d006a4e30b2..7d724b6abec1d2731d0bc453c4ee9de20f12013f 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
  *
- *   IPP utilities for the Common UNIX Printing System (CUPS).
+ *   IPP utilities for CUPS.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2011 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
  *   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.
  */
  * Include necessary headers...
  */
 
-#include "globals.h"
-#include "debug.h"
-#include <stdlib.h>
-#include <errno.h>
+#include "cups-private.h"
 #include <fcntl.h>
 #include <sys/stat.h>
 #if defined(WIN32) || defined(__EMX__)
@@ -66,10 +67,9 @@ cupsDoFileRequest(http_t     *http,  /* I - Connection to server or @code CUPS_HT
 
 
   DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
-                "filename=\"%s\")\n", http, request,
+                "filename=\"%s\")", http, request,
                request ? ippOpString(request->request.op.operation_id) : "?",
-               resource ? resource : "(null)",
-               filename ? filename : "(null)"));
+               resource, filename));
 
   if (filename)
   {
@@ -112,7 +112,7 @@ cupsDoFileRequest(http_t     *http, /* I - Connection to server or @code CUPS_HT
  * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
  * all of the data after the IPP response message to the file.
  *
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 ipp_t *                                        /* O - Response data */
@@ -131,9 +131,9 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
 
   DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
-                "infile=%d, outfile=%d)\n", http, request,
+                "infile=%d, outfile=%d)", http, request,
                request ? ippOpString(request->request.op.operation_id) : "?",
-               resource ? resource : "(null)", infile, outfile));
+               resource, infile, outfile));
 
  /*
   * Range check input...
@@ -154,7 +154,11 @@ cupsDoIORequest(http_t     *http,  /* I - Connection to server or @code CUPS_HTTP
 
   if (!http)
     if ((http = _cupsConnect()) == NULL)
+    {
+      ippDelete(request);
+
       return (NULL);
+    }
 
  /*
   * See if we have a file to send...
@@ -203,13 +207,23 @@ cupsDoIORequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
   else
     length = ippLength(request);
 
+  DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
+                (long)ippLength(request), (long)length));
+
+ /*
+  * Clear any "Local" authentication data since it is probably stale...
+  */
+
+  if (http->authstring && !strncmp(http->authstring, "Local ", 6))
+    httpSetAuthString(http, NULL, NULL);
+
  /*
   * Loop until we can send the request without authorization problems.
   */
 
   while (response == NULL)
   {
-    DEBUG_puts("cupsDoIORequest: setup...");
+    DEBUG_puts("2cupsDoIORequest: setup...");
 
    /*
     * Send the request...
@@ -217,11 +231,11 @@ cupsDoIORequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 
     status = cupsSendRequest(http, request, resource, length);
 
-    DEBUG_printf(("cupsDoIORequest: status=%d\n", status));
+    DEBUG_printf(("2cupsDoIORequest: status=%d", status));
 
     if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0)
     {
-      DEBUG_puts("cupsDoIORequest: file write...");
+      DEBUG_puts("2cupsDoIORequest: file write...");
 
      /*
       * Send the file with the request...
@@ -236,7 +250,9 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
       {
        if (httpCheck(http))
        {
-         if ((status = httpUpdate(http)) != HTTP_CONTINUE)
+         _httpUpdate(http, &status);
+
+         if (status >= HTTP_MULTIPLE_CHOICES)
            break;
         }
 
@@ -254,9 +270,14 @@ cupsDoIORequest(http_t     *http,  /* I - Connection to server or @code CUPS_HTTP
       response = cupsGetResponse(http, resource);
       status   = http->status;
     }
+    else
+      httpFlush(http);
+
+    DEBUG_printf(("2cupsDoIORequest: status=%d", status));
 
-    if (status == HTTP_FORBIDDEN || status == HTTP_ERROR ||
-        status == HTTP_SERVICE_UNAVAILABLE)
+    if (status == HTTP_ERROR ||
+        (status >= HTTP_BAD_REQUEST && status != HTTP_UNAUTHORIZED &&
+        status != HTTP_UPGRADE_REQUIRED))
     {
       _cupsSetHTTPError(status);
       break;
@@ -288,7 +309,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
  /*
   * Delete the original request and return the response...
   */
-  
+
   ippDelete(request);
 
   return (response);
@@ -308,10 +329,10 @@ 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\")\n",
+  DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
                 http, request,
                request ? ippOpString(request->request.op.operation_id) : "?",
-               resource ? resource : "(null)"));
+               resource));
 
   return (cupsDoIORequest(http, request, resource, -1, -1));
 }
@@ -324,7 +345,7 @@ cupsDoRequest(http_t     *http,             /* I - Connection to server or @code CUPS_HTTP_
  * cupsSendDocument() or cupsSendRequest(). For requests that return
  * additional data, use httpRead() after getting a successful response.
  *
- * @since CUPS 1.4@
+ * @since CUPS 1.4/Mac OS X 10.6@
  */
 
 ipp_t *                                        /* O - Response or @code NULL@ on HTTP error */
@@ -336,8 +357,7 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
   ipp_t                *response = NULL;       /* IPP response */
 
 
-  DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http,
-                resource ? resource : "(null)"));
+  DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http, resource));
 
  /*
   * Connect to the default server as needed...
@@ -359,7 +379,7 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
     * Send a 0-length chunk to finish off the request...
     */
 
-    DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
+    DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
 
     if (httpWrite2(http, "", 0) < 0)
       return (NULL);
@@ -369,14 +389,16 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
   * Wait for a response from the server...
   */
 
-  DEBUG_printf(("cupsGetResponse: Update loop, http->status=%d...\n",
+  DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
                 http->status));
 
-  status = http->status;
-  while (status == HTTP_CONTINUE)
+  do
+  {
     status = httpUpdate(http);
+  }
+  while (status != HTTP_ERROR && http->state == HTTP_POST_RECV);
 
-  DEBUG_printf(("cupsGetResponse: status=%d\n", status));
+  DEBUG_printf(("2cupsGetResponse: status=%d", status));
 
   if (status == HTTP_OK)
   {
@@ -393,10 +415,12 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
     if (state == IPP_ERROR)
     {
      /*
-      * Delete the response...
+      * Flush remaining data and delete the response...
       */
 
-      DEBUG_puts("cupsGetResponse: IPP read error!");
+      DEBUG_puts("1cupsGetResponse: IPP read error!");
+
+      httpFlush(http);
 
       ippDelete(response);
       response = NULL;
@@ -422,14 +446,12 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
       * See if we can do authentication...
       */
 
-      int auth_result;
-
-      DEBUG_puts("cupsGetResponse: Need authorization...");
+      DEBUG_puts("2cupsGetResponse: Need authorization...");
 
-      if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0)
-       httpReconnect(http);
-      else if (auth_result < 0)
-        http->status = status = HTTP_FORBIDDEN;
+      if (!cupsDoAuthentication(http, "POST", resource))
+        httpReconnect(http);
+      else
+        status = HTTP_AUTHORIZATION_CANCELED;
     }
 
 #ifdef HAVE_SSL
@@ -439,7 +461,7 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
       * Force a reconnect with encryption...
       */
 
-      DEBUG_puts("cupsGetResponse: Need encryption...");
+      DEBUG_puts("2cupsGetResponse: Need encryption...");
 
       if (!httpReconnect(http))
         httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
@@ -454,7 +476,7 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
     attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
 
-    DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n",
+    DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
                   ippErrorString(response->request.status.status_code),
                   attr ? attr->values[0].string.text : ""));
 
@@ -469,13 +491,67 @@ cupsGetResponse(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 }
 
 
+/*
+ * 'cupsLastError()' - Return the last IPP status code.
+ */
+
+ipp_status_t                           /* O - IPP status code from last request */
+cupsLastError(void)
+{
+  return (_cupsGlobals()->last_error);
+}
+
+
+/*
+ * 'cupsLastErrorString()' - Return the last IPP status-message.
+ *
+ * @since CUPS 1.2/Mac OS X 10.5@
+ */
+
+const char *                           /* O - status-message text from last request */
+cupsLastErrorString(void)
+{
+  return (_cupsGlobals()->last_status_message);
+}
+
+
+/*
+ * '_cupsNextDelay()' - Return the next retry delay value.
+ *
+ * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
+ *
+ * Pass 0 for the current delay value to initialize the sequence.
+ */
+
+int                                    /* O  - Next delay value */
+_cupsNextDelay(int current,            /* I  - Current delay value or 0 */
+               int *previous)          /* IO - Previous delay value */
+{
+  int  next;                           /* Next delay value */
+
+
+  if (current > 0)
+  {
+    next      = (current + *previous) % 12;
+    *previous = next < current ? 0 : current;
+  }
+  else
+  {
+    next      = 1;
+    *previous = 0;
+  }
+
+  return (next);
+}
+
+
 /*
  * '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.
  *
- * @since CUPS 1.4@
+ * @since CUPS 1.4/Mac OS X 10.6@
  */
 
 ssize_t                                        /* O - Bytes read, 0 on EOF, -1 on error */
@@ -489,7 +565,7 @@ cupsReadResponseData(
   */
 
   DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
-                "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
+                "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
 
   if (!http)
   {
@@ -516,13 +592,13 @@ cupsReadResponseData(
  *
  * 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 
+ * to read any additional data following the response. Only one request can be
  * sent/queued at a time.
  *
  * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
  * request is not freed.
  *
- * @since CUPS 1.4@
+ * @since CUPS 1.4/Mac OS X 10.6@
  */
 
 http_status_t                          /* O - Initial HTTP status */
@@ -538,9 +614,9 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
 
   DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
-                "length=" CUPS_LLFMT ")\n", http, request,
+                "length=" CUPS_LLFMT ")", http, request,
                request ? ippOpString(request->request.op.operation_id) : "?",
-               resource ? resource : "(null)", CUPS_LLCAST length));
+               resource, CUPS_LLCAST length));
 
  /*
   * Range check input...
@@ -581,7 +657,7 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
   * Reconnect if the last response had a "Connection: close"...
   */
 
-  if (!strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close"))
+  if (!_cups_strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close"))
     if (httpReconnect(http))
     {
       _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
@@ -596,25 +672,38 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
 
   for (;;)
   {
-    DEBUG_puts("cupsSendRequest: Setup...");
+    DEBUG_puts("2cupsSendRequest: Setup...");
 
    /*
     * Setup the HTTP variables needed...
     */
 
     httpClearFields(http);
-    httpSetLength(http, length);
+    httpSetExpect(http, expect);
     httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
+    httpSetLength(http, length);
+
+#ifdef HAVE_GSSAPI
+    if (http->authstring && !strncmp(http->authstring, "Negotiate", 9))
+    {
+     /*
+      * Do not use cached Kerberos credentials since they will look like a
+      * "replay" attack...
+      */
+
+      _cupsSetNegotiateAuthString(http, "POST", resource);
+    }
+#endif /* HAVE_GSSAPI */
+
     httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
-    httpSetExpect(http, expect);
 
-    DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring));
+    DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring));
 
    /*
     * Try the request...
     */
 
-    DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
+    DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
 
     if (httpPost(http, resource))
     {
@@ -631,7 +720,7 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
     * Send the IPP data...
     */
 
-    DEBUG_puts("cupsSendRequest: Writing IPP request...");
+    DEBUG_puts("2cupsSendRequest: Writing IPP request...");
 
     request->state = IPP_IDLE;
     status         = HTTP_CONTINUE;
@@ -644,32 +733,45 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
       {
         got_status = 1;
 
-       if ((status = httpUpdate(http)) != HTTP_CONTINUE)
+        _httpUpdate(http, &status);
+       if (status >= HTTP_MULTIPLE_CHOICES)
          break;
       }
 
+    if (state == IPP_ERROR)
+    {
+      http->status = HTTP_ERROR;
+      http->state  = HTTP_WAITING;
+
+      return (HTTP_ERROR);
+    }
+
    /*
     * Wait up to 1 second to get the 100-continue response as needed...
     */
 
-    if (!got_status && expect == HTTP_CONTINUE)
+    if (!got_status)
     {
-      DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
+      if (expect == HTTP_CONTINUE)
+      {
+       DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
 
-      if (httpWait(http, 1000))
-        status = httpUpdate(http);
-      else
-        status = HTTP_EXPECTATION_FAILED;
+       if (httpWait(http, 1000))
+         _httpUpdate(http, &status);
+      }
+      else if (httpCheck(http))
+       _httpUpdate(http, &status);
     }
-    else if (httpCheck(http))
-      status = httpUpdate(http);
 
-    DEBUG_printf(("cupsSendRequest: status=%d\n", status));
+    DEBUG_printf(("2cupsSendRequest: status=%d", status));
 
    /*
     * Process the current HTTP status...
     */
 
+    if (status >= HTTP_MULTIPLE_CHOICES)
+      httpFlush(http);
+
     switch (status)
     {
       case HTTP_ERROR :
@@ -678,14 +780,15 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
           return (status);
 
       case HTTP_UNAUTHORIZED :
-          if (!cupsDoAuthentication(http, "POST", resource))
-           if (httpReconnect(http))
-           {
-             _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
-             return (HTTP_SERVICE_UNAVAILABLE);
-           }
+          if (cupsDoAuthentication(http, "POST", resource))
+           return (HTTP_AUTHORIZATION_CANCELED);
 
-          return (status);
+         if (httpReconnect(http))
+         {
+           _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+           return (HTTP_SERVICE_UNAVAILABLE);
+         }
+         break;
 
 #ifdef HAVE_SSL
       case HTTP_UPGRADE_REQUIRED :
@@ -700,9 +803,12 @@ cupsSendRequest(http_t     *http,  /* I - Connection to server or @code CUPS_HTTP
            return (HTTP_SERVICE_UNAVAILABLE);
          }
 
-         httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
-
-          return (status);
+         if (httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
+         {
+           _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+           return (HTTP_SERVICE_UNAVAILABLE);
+         }
+         break;
 #endif /* HAVE_SSL */
 
       case HTTP_EXPECTATION_FAILED :
@@ -711,6 +817,12 @@ cupsSendRequest(http_t     *http,  /* I - Connection to server or @code CUPS_HTTP
          */
 
          expect = (http_status_t)0;
+
+         if (httpReconnect(http))
+         {
+           _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+           return (HTTP_SERVICE_UNAVAILABLE);
+         }
          break;
 
       default :
@@ -730,7 +842,7 @@ 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@
+ * @since CUPS 1.4/Mac OS X 10.6@
  */
 
 http_status_t                          /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
@@ -739,12 +851,15 @@ cupsWriteRequestData(
     const char *buffer,                        /* I - Bytes to write */
     size_t     length)                 /* I - Number of bytes to write */
 {
+  int  wused;                          /* Previous bytes in buffer */
+
+
  /*
   * Get the default connection as needed...
   */
 
   DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
-                "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
+                "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
 
   if (!http)
   {
@@ -754,6 +869,7 @@ cupsWriteRequestData(
     if ((http = cg->http) == NULL)
     {
       _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
+      DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
       return (HTTP_ERROR);
     }
   }
@@ -762,17 +878,106 @@ cupsWriteRequestData(
   * Then write to the HTTP connection...
   */
 
+  wused = http->wused;
+
   if (httpWrite2(http, buffer, length) < 0)
+  {
+    DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(http->error), 0);
     return (HTTP_ERROR);
+  }
 
  /*
   * Finally, check if we have any pending data from the server...
   */
 
-  if (httpCheck(http))
-    return (httpUpdate(http));
-  else
-    return (HTTP_CONTINUE);
+  if (length >= HTTP_MAX_BUFFER ||
+      http->wused < wused ||
+      (wused > 0 && http->wused == length))
+  {
+   /*
+    * We've written something to the server, so check for response data...
+    */
+
+    if (_httpWait(http, 0, 1))
+    {
+      http_status_t    status;         /* Status from _httpUpdate */
+
+      _httpUpdate(http, &status);
+      if (status >= HTTP_MULTIPLE_CHOICES)
+      {
+        _cupsSetHTTPError(status);
+        httpFlush(http);
+      }
+
+      DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status));
+      return (status);
+    }
+  }
+
+  DEBUG_puts("1cupsWriteRequestData: Returning HTTP_CONTINUE.");
+  return (HTTP_CONTINUE);
+}
+
+
+/*
+ * '_cupsConnect()' - Get the default server connection...
+ */
+
+http_t *                               /* O - HTTP connection */
+_cupsConnect(void)
+{
+  _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals */
+
+
+ /*
+  * See if we are connected to the same server...
+  */
+
+  if (cg->http)
+  {
+   /*
+    * Compare the connection hostname, port, and encryption settings to
+    * the cached defaults; these were initialized the first time we
+    * connected...
+    */
+
+    if (strcmp(cg->http->hostname, cg->server) ||
+        cg->ipp_port != _httpAddrPort(cg->http->hostaddr) ||
+        (cg->http->encryption != cg->encryption &&
+        cg->http->encryption == HTTP_ENCRYPT_NEVER))
+    {
+     /*
+      * Need to close the current connection because something has changed...
+      */
+
+      httpClose(cg->http);
+      cg->http = NULL;
+    }
+  }
+
+ /*
+  * (Re)connect as needed...
+  */
+
+  if (!cg->http)
+  {
+    if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(),
+                                       cupsEncryption())) == NULL)
+    {
+      if (errno)
+        _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+      else
+        _cupsSetError(IPP_SERVICE_UNAVAILABLE,
+                     _("Unable to connect to host."), 1);
+    }
+  }
+
+ /*
+  * Return the cached connection...
+  */
+
+  return (cg->http);
 }
 
 
@@ -822,9 +1027,8 @@ _cupsSetError(ipp_status_t status, /* I - IPP status code */
       cg->last_status_message = _cupsStrAlloc(message);
   }
 
-  DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
-                ippErrorString(cg->last_error),
-               cg->last_status_message ? cg->last_status_message : ""));
+  DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
+                ippErrorString(cg->last_error), cg->last_status_message));
 }
 
 
@@ -842,7 +1046,11 @@ _cupsSetHTTPError(http_status_t status)   /* I - HTTP status code */
        break;
 
     case HTTP_UNAUTHORIZED :
-       _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0);
+       _cupsSetError(IPP_NOT_AUTHENTICATED, httpStatus(status), 0);
+       break;
+
+    case HTTP_AUTHORIZATION_CANCELED :
+       _cupsSetError(IPP_AUTHENTICATION_CANCELED, httpStatus(status), 0);
        break;
 
     case HTTP_FORBIDDEN :
@@ -865,9 +1073,21 @@ _cupsSetHTTPError(http_status_t status)   /* I - HTTP status code */
        _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
        break;
 
+    case HTTP_UPGRADE_REQUIRED :
+       _cupsSetError(IPP_UPGRADE_REQUIRED, httpStatus(status), 0);
+        break;
+
+    case HTTP_PKI_ERROR :
+       _cupsSetError(IPP_PKI_ERROR, httpStatus(status), 0);
+        break;
+
+    case HTTP_ERROR :
+       _cupsSetError(IPP_INTERNAL_ERROR, httpStatus(status), 0);
+        break;
+
     default :
-       DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
-                     status));
+       DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
+                     "IPP_SERVICE_UNAVAILABLE!", status));
        _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0);
        break;
   }