]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/getputfile.c
Changes to eliminate warnings from new Clang.
[thirdparty/cups.git] / cups / getputfile.c
index 4a78a42df2367309244c4f4057f9d43d1f74e803..478c55cb2f4f122a1be4d0e16241087df416ffad 100644 (file)
@@ -1,48 +1,25 @@
 /*
- * "$Id: getputfile.c 5138 2006-02-21 10:49:06Z mike $"
+ * "$Id$"
  *
- *   Get/put file functions for the Common UNIX Printing System (CUPS).
+ * Get/put file functions for CUPS.
  *
- *   Copyright 1997-2006 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products 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 please contact Easy Software Products
- *   at:
+ * 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/".
  *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   cupsGetFd()   - Get a file from the server.
- *   cupsGetFile() - Get a file from the server.
- *   cupsPutFd()   - Put a file on the server.
- *   cupsPutFile() - Put a file on the server.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include "cups.h"
-#include "ipp.h"
-#include "language.h"
-#include "string.h"
-#include "debug.h"
-#include <stdlib.h>
-#include <ctype.h>
-#include <errno.h>
+#include "cups-private.h"
 #include <fcntl.h>
 #include <sys/stat.h>
 #if defined(WIN32) || defined(__EMX__)
 /*
  * 'cupsGetFd()' - Get a file from the server.
  *
- * This function returns HTTP_OK when the file is successfully retrieved.
+ * This function returns @code HTTP_STATUS_OK@ when the file is successfully retrieved.
  *
- * @since CUPS 1.1.20@
+ * @since CUPS 1.1.20/OS X 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
-cupsGetFd(http_t     *http,            /* I - HTTP connection to server */
+cupsGetFd(http_t     *http,            /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
          const char *resource,         /* I - Resource name */
          int        fd)                /* I - File descriptor */
 {
-  int          bytes;                  /* Number of bytes read */
+  ssize_t      bytes;                  /* Number of bytes read */
   char         buffer[8192];           /* Buffer for file */
   http_status_t        status;                 /* HTTP status from server */
+  char         if_modified_since[HTTP_MAX_VALUE];
+                                       /* If-Modified-Since header */
 
 
  /*
   * Range check input...
   */
 
-  DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)\n", http,
-                resource, fd));
+  DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)", (void *)http, resource, fd));
 
-  if (!http || !resource || fd < 0)
+  if (!resource || fd < 0)
   {
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
+  if (!http)
+    if ((http = _cupsConnect()) == NULL)
+      return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+
  /*
   * Then send GET requests to the HTTP server...
   */
 
+  strlcpy(if_modified_since, httpGetField(http, HTTP_FIELD_IF_MODIFIED_SINCE),
+          sizeof(if_modified_since));
+
   do
   {
+    if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+    {
+      httpClearFields(http);
+      if (httpReconnect2(http, 30000, NULL))
+      {
+       status = HTTP_STATUS_ERROR;
+       break;
+      }
+    }
+
     httpClearFields(http);
     httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
+    httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE, if_modified_since);
 
     if (httpGet(http, resource))
     {
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
        break;
       }
       else
       {
-        status = HTTP_UNAUTHORIZED;
+        status = HTTP_STATUS_UNAUTHORIZED;
         continue;
       }
     }
 
-    while ((status = httpUpdate(http)) == HTTP_CONTINUE);
+    while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
 
-    if (status == HTTP_UNAUTHORIZED)
+    if (status == HTTP_STATUS_UNAUTHORIZED)
     {
      /*
       * Flush any error message...
@@ -123,59 +119,67 @@ cupsGetFd(http_t     *http,               /* I - HTTP connection to server */
       */
 
       if (cupsDoAuthentication(http, "GET", resource))
+      {
+        status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
         break;
+      }
 
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       continue;
     }
 #ifdef HAVE_SSL
-    else if (status == HTTP_UPGRADE_REQUIRED)
+    else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
     {
       /* Flush any error message... */
       httpFlush(http);
 
       /* Reconnect... */
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       /* Upgrade with encryption... */
-      httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+      httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
 
       /* Try again, this time with encryption enabled... */
       continue;
     }
 #endif /* HAVE_SSL */
   }
-  while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
+  while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED);
 
  /*
   * See if we actually got the file or an error...
   */
 
-  if (status == HTTP_OK)
+  if (status == HTTP_STATUS_OK)
   {
    /*
     * Yes, copy the file...
     */
 
     while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
-      write(fd, buffer, bytes);
+      write(fd, buffer, (size_t)bytes);
   }
   else
+  {
+    _cupsSetHTTPError(status);
     httpFlush(http);
+  }
 
  /*
   * Return the request status...
   */
 
+  DEBUG_printf(("1cupsGetFd: Returning %d...", status));
+
   return (status);
 }
 
@@ -183,13 +187,13 @@ cupsGetFd(http_t     *http,               /* I - HTTP connection to server */
 /*
  * 'cupsGetFile()' - Get a file from the server.
  *
- * This function returns HTTP_OK when the file is successfully retrieved.
+ * This function returns @code HTTP_STATUS_OK@ when the file is successfully retrieved.
  *
- * @since CUPS 1.1.20@
+ * @since CUPS 1.1.20/OS X 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
-cupsGetFile(http_t     *http,          /* I - HTTP connection to server */
+cupsGetFile(http_t     *http,          /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
            const char *resource,       /* I - Resource name */
            const char *filename)       /* I - Filename */
 {
@@ -206,7 +210,7 @@ cupsGetFile(http_t     *http,               /* I - HTTP connection to server */
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -221,7 +225,7 @@ cupsGetFile(http_t     *http,               /* I - HTTP connection to server */
 
     http->error = errno;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -236,7 +240,7 @@ cupsGetFile(http_t     *http,               /* I - HTTP connection to server */
 
   close(fd);
 
-  if (status != HTTP_OK)
+  if (status != HTTP_STATUS_OK)
     unlink(filename);
 
  /*
@@ -250,18 +254,19 @@ cupsGetFile(http_t     *http,             /* I - HTTP connection to server */
 /*
  * 'cupsPutFd()' - Put a file on the server.
  *
- * This function returns HTTP_CREATED when the file is stored successfully.
+ * This function returns @code HTTP_STATUS_CREATED@ when the file is stored
+ * successfully.
  *
- * @since CUPS 1.1.20@
+ * @since CUPS 1.1.20/OS X 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
-cupsPutFd(http_t     *http,            /* I - HTTP connection to server */
+cupsPutFd(http_t     *http,            /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
           const char *resource,                /* I - Resource name */
          int        fd)                /* I - File descriptor */
 {
-  int          bytes,                  /* Number of bytes read */
-               retries;                /* Number of retries */
+  ssize_t      bytes;                  /* Number of bytes read */
+  int          retries;                /* Number of retries */
   char         buffer[8192];           /* Buffer for file */
   http_status_t        status;                 /* HTTP status from server */
 
@@ -270,17 +275,20 @@ cupsPutFd(http_t     *http,               /* I - HTTP connection to server */
   * Range check input...
   */
 
-  DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http,
-                resource, fd));
+  DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)", (void *)http, resource, fd));
 
-  if (!http || !resource || fd < 0)
+  if (!resource || fd < 0)
   {
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
+  if (!http)
+    if ((http = _cupsConnect()) == NULL)
+      return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+
  /*
   * Then send PUT requests to the HTTP server...
   */
@@ -289,54 +297,75 @@ cupsPutFd(http_t     *http,               /* I - HTTP connection to server */
 
   do
   {
-    DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n",
+    if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+    {
+      httpClearFields(http);
+      if (httpReconnect2(http, 30000, NULL))
+      {
+       status = HTTP_STATUS_ERROR;
+       break;
+      }
+    }
+
+    DEBUG_printf(("2cupsPutFd: starting attempt, authstring=\"%s\"...",
                   http->authstring));
 
     httpClearFields(http);
     httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
     httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked");
+    httpSetExpect(http, HTTP_STATUS_CONTINUE);
 
     if (httpPut(http, resource))
     {
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
        break;
       }
       else
       {
-        status = HTTP_UNAUTHORIZED;
+        status = HTTP_STATUS_UNAUTHORIZED;
         continue;
       }
     }
 
    /*
-    * Copy the file...
+    * Wait up to 1 second for a 100-continue response...
     */
 
-    lseek(fd, 0, SEEK_SET);
+    if (httpWait(http, 1000))
+      status = httpUpdate(http);
+    else
+      status = HTTP_STATUS_CONTINUE;
 
-    status = HTTP_CONTINUE;
+    if (status == HTTP_STATUS_CONTINUE)
+    {
+     /*
+      * Copy the file...
+      */
 
-    while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
-      if (httpCheck(http))
-      {
-        if ((status = httpUpdate(http)) != HTTP_CONTINUE)
-          break;
-      }
-      else
-        httpWrite2(http, buffer, bytes);
+      lseek(fd, 0, SEEK_SET);
 
-    if (status == HTTP_CONTINUE)
+      while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
+       if (httpCheck(http))
+       {
+          if ((status = httpUpdate(http)) != HTTP_STATUS_CONTINUE)
+            break;
+       }
+       else
+          httpWrite2(http, buffer, (size_t)bytes);
+    }
+
+    if (status == HTTP_STATUS_CONTINUE)
     {
       httpWrite2(http, buffer, 0);
 
-      while ((status = httpUpdate(http)) == HTTP_CONTINUE);
+      while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
     }
 
-    if (status == HTTP_ERROR && !retries)
+    if (status == HTTP_STATUS_ERROR && !retries)
     {
-      DEBUG_printf(("cupsPutFd: retry on status %d\n", status));
+      DEBUG_printf(("2cupsPutFd: retry on status %d", status));
 
       retries ++;
 
@@ -344,9 +373,9 @@ cupsPutFd(http_t     *http,         /* I - HTTP connection to server */
       httpFlush(http);
 
       /* Reconnect... */
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
@@ -354,9 +383,9 @@ cupsPutFd(http_t     *http,         /* I - HTTP connection to server */
       continue;
     }
 
-    DEBUG_printf(("cupsPutFd: status=%d\n", status));
+    DEBUG_printf(("2cupsPutFd: status=%d", status));
 
-    if (status == HTTP_UNAUTHORIZED)
+    if (status == HTTP_STATUS_UNAUTHORIZED)
     {
      /*
       * Flush any error message...
@@ -369,46 +398,54 @@ cupsPutFd(http_t     *http,               /* I - HTTP connection to server */
       */
 
       if (cupsDoAuthentication(http, "PUT", resource))
+      {
+        status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
         break;
+      }
 
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       continue;
     }
 #ifdef HAVE_SSL
-    else if (status == HTTP_UPGRADE_REQUIRED)
+    else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
     {
       /* Flush any error message... */
       httpFlush(http);
 
       /* Reconnect... */
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       /* Upgrade with encryption... */
-      httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+      httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
 
       /* Try again, this time with encryption enabled... */
       continue;
     }
 #endif /* HAVE_SSL */
   }
-  while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED ||
-         (status == HTTP_ERROR && retries < 2));
+  while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED ||
+         (status == HTTP_STATUS_ERROR && retries < 2));
 
  /*
   * See if we actually put the file or an error...
   */
 
-  if (status != HTTP_CREATED)
+  if (status != HTTP_STATUS_CREATED)
+  {
+    _cupsSetHTTPError(status);
     httpFlush(http);
+  }
+
+  DEBUG_printf(("1cupsPutFd: Returning %d...", status));
 
   return (status);
 }
@@ -417,13 +454,14 @@ cupsPutFd(http_t     *http,               /* I - HTTP connection to server */
 /*
  * 'cupsPutFile()' - Put a file on the server.
  *
- * This function returns HTTP_CREATED when the file is stored successfully.
+ * This function returns @code HTTP_CREATED@ when the file is stored
+ * successfully.
  *
- * @since CUPS 1.1.20@
+ * @since CUPS 1.1.20/OS X 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
-cupsPutFile(http_t     *http,          /* I - HTTP connection to server */
+cupsPutFile(http_t     *http,          /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
             const char *resource,      /* I - Resource name */
            const char *filename)       /* I - Filename */
 {
@@ -440,7 +478,7 @@ cupsPutFile(http_t     *http,               /* I - HTTP connection to server */
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -455,7 +493,7 @@ cupsPutFile(http_t     *http,               /* I - HTTP connection to server */
 
     http->error = errno;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -471,5 +509,5 @@ cupsPutFile(http_t     *http,               /* I - HTTP connection to server */
 
 
 /*
- * End of "$Id: getputfile.c 5138 2006-02-21 10:49:06Z mike $".
+ * End of "$Id$".
  */