]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/request.c
Merge changes from CUPS 1.4svn-r7696.
[thirdparty/cups.git] / cups / request.c
index 6544c3448fb5dcc6fcc6d2d842a9e608ac2697a9..36b02b912380abcee500ec36da174fba5c3d7c9a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: request.c 6879 2007-08-29 20:26:50Z mike $"
+ * "$Id: request.c 7460 2008-04-16 02:19:54Z mike $"
  *
  *   IPP utilities for the Common UNIX Printing System (CUPS).
  *
  * '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 ippDelete()
+ * and authenticating as necessary.  The request is freed with @link ippDelete@
  * after receiving a valid IPP response.
  */
 
 ipp_t *                                        /* O - Response data */
-cupsDoFileRequest(http_t     *http,    /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+cupsDoFileRequest(http_t     *http,    /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
                   ipp_t      *request, /* I - IPP request */
                   const char *resource,        /* I - HTTP resource for POST */
-                 const char *filename) /* I - File to send or NULL for none */
+                 const char *filename) /* I - File to send or @code NULL@ for none */
 {
   ipp_t                *response;              /* IPP response data */
   int          infile;                 /* Input file */
 
 
+  DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
+                "filename=\"%s\")\n", http, request,
+               request ? ippOpString(request->request.op.operation_id) : "?",
+               resource ? resource : "(null)",
+               filename ? filename : "(null)"));
+
   if (filename)
   {
     if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
@@ -110,7 +116,7 @@ cupsDoFileRequest(http_t     *http, /* I - HTTP connection or CUPS_HTTP_DEFAULT
  */
 
 ipp_t *                                        /* O - Response data */
-cupsDoIORequest(http_t     *http,      /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+cupsDoIORequest(http_t     *http,      /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
                 ipp_t      *request,   /* I - IPP request */
                 const char *resource,  /* I - HTTP resource for POST */
                int        infile,      /* I - File to read from or -1 for none */
@@ -124,8 +130,9 @@ cupsDoIORequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
   char         buffer[32768];          /* Output buffer */
 
 
-  DEBUG_printf(("cupsDoIORequest(http=%p, request=%p, resource=\"%s\""
+  DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
                 "infile=%d, outfile=%d)\n", http, request,
+               request ? ippOpString(request->request.op.operation_id) : "?",
                resource ? resource : "(null)", infile, outfile));
 
  /*
@@ -146,7 +153,8 @@ cupsDoIORequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
   */
 
   if (!http)
-    http = _cupsConnect();
+    if ((http = _cupsConnect()) == NULL)
+      return (NULL);
 
  /*
   * See if we have a file to send...
@@ -247,6 +255,9 @@ cupsDoIORequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
       status   = http->status;
     }
 
+    if (status == HTTP_FORBIDDEN)
+      break;
+
     if (response)
     {
       if (outfile >= 0)
@@ -289,10 +300,15 @@ cupsDoIORequest(http_t     *http, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
  */
 
 ipp_t *                                        /* O - Response data */
-cupsDoRequest(http_t     *http,                /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+cupsDoRequest(http_t     *http,                /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
               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",
+                http, request,
+               request ? ippOpString(request->request.op.operation_id) : "?",
+               resource ? resource : "(null)"));
+
   return (cupsDoFileRequest(http, request, resource, NULL));
 }
 
@@ -307,8 +323,8 @@ cupsDoRequest(http_t     *http,             /* I - HTTP connection or CUPS_HTTP_DEFAULT */
  * @since CUPS 1.4@
  */
 
-ipp_t *                                        /* O - Response or NULL on HTTP error */
-cupsGetResponse(http_t     *http,      /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+ipp_t *                                        /* O - Response or @code NULL@ on HTTP error */
+cupsGetResponse(http_t     *http,      /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
                 const char *resource)  /* I - HTTP resource for POST */
 {
   http_status_t        status;                 /* HTTP status */
@@ -316,7 +332,8 @@ cupsGetResponse(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
   ipp_t                *response = NULL;       /* IPP response */
 
 
-  DEBUG_printf(("cupsGetReponse(http=%p)\n", http));
+  DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http,
+                resource ? resource : "(null)"));
 
  /*
   * Connect to the default server as needed...
@@ -348,12 +365,12 @@ cupsGetResponse(http_t     *http, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
   * Wait for a response from the server...
   */
 
-  DEBUG_puts("cupsGetResponse: update...");
+  DEBUG_puts("cupsGetResponse: Update loop...");
 
   while ((status = httpUpdate(http)) == HTTP_CONTINUE)
     /* Do nothing but update */;
 
-  DEBUG_printf(("cupsGetResponse: status = %d\n", status));
+  DEBUG_printf(("cupsGetResponse: status=%d\n", status));
 
   if (status == HTTP_OK)
   {
@@ -373,7 +390,7 @@ cupsGetResponse(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
       * Delete the response...
       */
 
-      DEBUG_puts("IPP read error!");
+      DEBUG_puts("cupsGetResponse: IPP read error!");
 
       ippDelete(response);
       response = NULL;
@@ -399,10 +416,14 @@ cupsGetResponse(http_t     *http, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
       * See if we can do authentication...
       */
 
+      int auth_result;
+
       DEBUG_puts("cupsGetResponse: Need authorization...");
 
-      if (!cupsDoAuthentication(http, "POST", resource))
+      if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0)
        httpReconnect(http);
+      else if (auth_result < 0)
+        http->status = status = HTTP_FORBIDDEN;
     }
 
 #ifdef HAVE_SSL
@@ -427,6 +448,10 @@ cupsGetResponse(http_t     *http,  /* I - HTTP connection or CUPS_HTTP_DEFAULT */
 
     attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
 
+    DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n",
+                  ippErrorString(response->request.status.status_code),
+                  attr ? attr->values[0].string.text : ""));
+
     _cupsSetError(response->request.status.status_code,
                    attr ? attr->values[0].string.text :
                       ippErrorString(response->request.status.status_code));
@@ -449,7 +474,7 @@ cupsGetResponse(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
 
 ssize_t                                        /* O - Bytes read, 0 on EOF, -1 on error */
 cupsReadResponseData(
-    http_t *http,                      /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+    http_t *http,                      /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     char   *buffer,                    /* I - Buffer to use */
     size_t length)                     /* I - Number of bytes to read */
 {
@@ -457,6 +482,9 @@ cupsReadResponseData(
   * Get the default connection as needed...
   */
 
+  DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
+                "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
+
   if (!http)
   {
     _cups_globals_t *cg = _cupsGlobals();
@@ -492,10 +520,10 @@ cupsReadResponseData(
  */
 
 http_status_t                          /* O - Initial HTTP status */
-cupsSendRequest(http_t     *http,      /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+cupsSendRequest(http_t     *http,      /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
                 ipp_t      *request,   /* I - IPP request */
                 const char *resource,  /* I - Resource path */
-               size_t     length)      /* I - Length of data to follow or CUPS_LENGTH_VARIABLE */
+               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? */
@@ -503,8 +531,9 @@ cupsSendRequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
   http_status_t        expect;                 /* Expect: header to use */
 
 
-  DEBUG_printf(("cupsSendRequest(http=%p, request=%p, resource=\"%s\", "
+  DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
                 "length=" CUPS_LLFMT ")\n", http, request,
+               request ? ippOpString(request->request.op.operation_id) : "?",
                resource ? resource : "(null)", CUPS_LLCAST length));
 
  /*
@@ -523,7 +552,8 @@ cupsSendRequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
   */
 
   if (!http)
-    http = _cupsConnect();
+    if ((http = _cupsConnect()) == NULL)
+      return (HTTP_SERVICE_UNAVAILABLE);
 
 #ifdef HAVE_SSL
  /*
@@ -547,7 +577,7 @@ cupsSendRequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
 
   for (;;)
   {
-    DEBUG_puts("cupsSendRequest: setup...");
+    DEBUG_puts("cupsSendRequest: Setup...");
 
    /*
     * Setup the HTTP variables needed...
@@ -565,7 +595,7 @@ cupsSendRequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
     * Try the request...
     */
 
-    DEBUG_puts("cupsSendRequest: post...");
+    DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
 
     if (httpPost(http, resource))
     {
@@ -579,7 +609,7 @@ cupsSendRequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
     * Send the IPP data...
     */
 
-    DEBUG_puts("cupsSendRequest: ipp write...");
+    DEBUG_puts("cupsSendRequest: Writing IPP request...");
 
     request->state = IPP_IDLE;
     status         = HTTP_CONTINUE;
@@ -602,12 +632,18 @@ cupsSendRequest(http_t     *http, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
 
     if (!got_status && expect == HTTP_CONTINUE)
     {
+      DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
+
       if (httpWait(http, 1000))
         status = httpUpdate(http);
+      else
+        status = HTTP_EXPECTATION_FAILED;
     }
     else if (httpCheck(http))
       status = httpUpdate(http);
 
+    DEBUG_printf(("cupsSendRequest: status=%d\n", status));
+
    /*
     * Process the current HTTP status...
     */
@@ -647,6 +683,7 @@ cupsSendRequest(http_t     *http,   /* I - HTTP connection or CUPS_HTTP_DEFAULT */
          */
 
          expect = (http_status_t)0;
+         break;
 
       default :
          /*
@@ -662,15 +699,15 @@ cupsSendRequest(http_t     *http, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
 /*
  * 'cupsWriteRequestData()' - Write additional data after an IPP request.
  *
- * This function is used after cupsSendRequest() or cupsStartDocument()
- * to provide a PPD or document file as needed.
+ * This function is used after @link cupsSendRequest@ to provide a PPD and
+ * after @link cupsStartDocument@ to provide a document file.
  *
  * @since CUPS 1.4@
  */
 
-http_status_t                          /* O - HTTP_CONTINUE if OK or HTTP status on error */
+http_status_t                          /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
 cupsWriteRequestData(
-    http_t     *http,                  /* I - HTTP connection or CUPS_HTTP_DEFAULT */
+    http_t     *http,                  /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     const char *buffer,                        /* I - Bytes to write */
     size_t     length)                 /* I - Number of bytes to write */
 {
@@ -678,6 +715,9 @@ cupsWriteRequestData(
   * Get the default connection as needed...
   */
 
+  DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
+                "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
+
   if (!http)
   {
     _cups_globals_t *cg = _cupsGlobals();
@@ -731,6 +771,9 @@ _cupsSetError(ipp_status_t status,  /* I - IPP status code */
 
   if (message)
     cg->last_status_message = strdup(message);
+
+  DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
+                ippErrorString(cg->last_error), message ? message : ""));
 }
 
 
@@ -781,5 +824,5 @@ _cupsSetHTTPError(http_status_t status)     /* I - HTTP status code */
 
 
 /*
- * End of "$Id: request.c 6879 2007-08-29 20:26:50Z mike $".
+ * End of "$Id: request.c 7460 2008-04-16 02:19:54Z mike $".
  */