]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/request.c
Merge changes from CUPS 1.6svn-r9968.
[thirdparty/cups.git] / cups / request.c
index 722dcdf0f8b0d6ed22106471c9b0aaded723bb19..a38ec19604917975398dc40571fea4fbb48f0210 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-2009 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__)
@@ -153,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...
@@ -245,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;
         }
 
@@ -263,45 +270,44 @@ 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_BAD_REQUEST &&
-       status != HTTP_UNAUTHORIZED &&
-       status != HTTP_UPGRADE_REQUIRED)
+    if (status == HTTP_ERROR ||
+        (status >= HTTP_BAD_REQUEST && status != HTTP_UNAUTHORIZED &&
+        status != HTTP_UPGRADE_REQUIRED))
     {
-      httpFlush(http);
       _cupsSetHTTPError(status);
       break;
     }
 
-    if (response)
+    if (response && outfile >= 0)
     {
-      if (outfile >= 0)
-      {
-       /*
-        * Write trailing data to file...
-       */
+     /*
+      * Write trailing data to file...
+      */
 
-       while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
-         if (write(outfile, buffer, bytes) < bytes)
-           break;
-      }
-      else
-      {
-       /*
-        * Flush any remaining data...
-        */
+      while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
+       if (write(outfile, buffer, bytes) < bytes)
+         break;
+    }
 
-        httpFlush(http);
-      }
+    if (http->state != HTTP_WAITING)
+    {
+     /*
+      * Flush any remaining data...
+      */
+
+      httpFlush(http);
     }
   }
 
  /*
   * Delete the original request and return the response...
   */
-  
+
   ippDelete(request);
 
   return (response);
@@ -335,7 +341,8 @@ cupsDoRequest(http_t     *http,             /* I - Connection to server or @code CUPS_HTTP_
  *
  * 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.
+ * additional data, use httpRead() after getting a successful response,
+ * otherwise call httpFlush() to complete the response processing.
  *
  * @since CUPS 1.4/Mac OS X 10.6@
  */
@@ -384,9 +391,11 @@ cupsGetResponse(http_t     *http,  /* I - Connection to server or @code CUPS_HTTP
   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(("2cupsGetResponse: status=%d", status));
 
@@ -405,15 +414,19 @@ 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("1cupsGetResponse: IPP read error!");
 
+      httpFlush(http);
+
       ippDelete(response);
       response = NULL;
 
       _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+      http->status = status = HTTP_ERROR;
+      http->error  = EIO;
     }
   }
   else if (status != HTTP_ERROR)
@@ -437,9 +450,9 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
       DEBUG_puts("2cupsGetResponse: Need authorization...");
 
       if (!cupsDoAuthentication(http, "POST", resource))
-       httpReconnect(http);
+        httpReconnect(http);
       else
-        status = HTTP_AUTHORIZATION_CANCELED;
+        http->status = status = HTTP_AUTHORIZATION_CANCELED;
     }
 
 #ifdef HAVE_SSL
@@ -479,6 +492,60 @@ 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.
  *
@@ -526,7 +593,7 @@ 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
@@ -571,6 +638,20 @@ cupsSendRequest(http_t     *http,  /* I - Connection to server or @code CUPS_HTTP
     if ((http = _cupsConnect()) == NULL)
       return (HTTP_SERVICE_UNAVAILABLE);
 
+ /*
+  * If the prior request was not flushed out, do so now...
+  */
+
+  if (http->state == HTTP_GET_SEND ||
+      http->state == HTTP_POST_SEND)
+    httpFlush(http);
+  else if (http->state != HTTP_WAITING)
+  {
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
+
+    return (HTTP_ERROR);
+  }
+
 #ifdef HAVE_SSL
  /*
   * See if we have an auth-info attribute and are communicating over
@@ -591,7 +672,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);
@@ -613,10 +694,23 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
     */
 
     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(("2cupsSendRequest: authstring=\"%s\"", http->authstring));
 
@@ -654,23 +748,35 @@ 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("2cupsSendRequest: Waiting for 100-continue...");
+      if (expect == HTTP_CONTINUE)
+      {
+       DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
 
-      if (httpWait(http, 1000))
-        status = httpUpdate(http);
+       if (httpWait(http, 1000))
+         _httpUpdate(http, &status);
+      }
+      else if (httpCheck(http))
+       _httpUpdate(http, &status);
     }
-    else if (httpCheck(http))
-      status = httpUpdate(http);
 
     DEBUG_printf(("2cupsSendRequest: status=%d", status));
 
@@ -678,6 +784,9 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
     * Process the current HTTP status...
     */
 
+    if (status >= HTTP_MULTIPLE_CHOICES)
+      httpFlush(http);
+
     switch (status)
     {
       case HTTP_ERROR :
@@ -686,18 +795,15 @@ cupsSendRequest(http_t     *http, /* I - Connection to server or @code CUPS_HTTP
           return (status);
 
       case HTTP_UNAUTHORIZED :
-          if (!cupsDoAuthentication(http, "POST", resource))
+          if (cupsDoAuthentication(http, "POST", resource))
+           return (HTTP_AUTHORIZATION_CANCELED);
+
+         if (httpReconnect(http))
          {
-           if (httpReconnect(http))
-           {
-             _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
-             return (HTTP_SERVICE_UNAVAILABLE);
-           }
+           _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+           return (HTTP_SERVICE_UNAVAILABLE);
          }
-         else
-           status = HTTP_AUTHORIZATION_CANCELED;
-
-          return (status);
+         break;
 
 #ifdef HAVE_SSL
       case HTTP_UPGRADE_REQUIRED :
@@ -712,9 +818,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 :
@@ -775,6 +884,7 @@ cupsWriteRequestData(
     if ((http = cg->http) == NULL)
     {
       _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
+      DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
       return (HTTP_ERROR);
     }
   }
@@ -786,7 +896,11 @@ cupsWriteRequestData(
   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...
@@ -801,13 +915,87 @@ cupsWriteRequestData(
     */
 
     if (_httpWait(http, 0, 1))
-      return (httpUpdate(http));
+    {
+      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);
+}
+
+
 /*
  * '_cupsSetError()' - Set the last IPP status code and status-message.
  */
@@ -873,8 +1061,11 @@ _cupsSetHTTPError(http_status_t status)   /* I - HTTP status code */
        break;
 
     case HTTP_UNAUTHORIZED :
+       _cupsSetError(IPP_NOT_AUTHENTICATED, httpStatus(status), 0);
+       break;
+
     case HTTP_AUTHORIZATION_CANCELED :
-       _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0);
+       _cupsSetError(IPP_AUTHENTICATION_CANCELED, httpStatus(status), 0);
        break;
 
     case HTTP_FORBIDDEN :
@@ -897,6 +1088,18 @@ _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(("4_cupsSetHTTPError: HTTP error %d mapped to "
                      "IPP_SERVICE_UNAVAILABLE!", status));