]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
httpConnectAgain from libcups v3.
authorMichael R Sweet <msweet@msweet.org>
Tue, 18 Jun 2024 15:22:20 +0000 (11:22 -0400)
committerMichael R Sweet <msweet@msweet.org>
Tue, 18 Jun 2024 15:22:20 +0000 (11:22 -0400)
cups/cups-private.h
cups/http.c
cups/http.h

index b3275217c72d3fd62a985de99061447de4d6eba4..ab8f5bc1725c56d48ece456ad2ec39d9f21dfc94 100644 (file)
@@ -70,9 +70,9 @@ typedef struct _cups_globals_s                // CUPS global state data
   const char           *cups_datadir,  // CUPS_DATADIR environment var
                        *cups_serverbin,// CUPS_SERVERBIN environment var
                        *sysconfig,     // System configuration directory (influenced by CUPS_SERVERROOT environment var)
-                       *cups_statedir, // CUPS_STATEDIR environment var
-                       *userconfig,    // User configuration directory (influenced by various environment vars)
-                       *localedir;     // LOCALDIR environment var
+                       *cups_statedir; // CUPS_STATEDIR environment var
+  char                 *userconfig;    // User configuration directory (influenced by various environment vars)
+  const char           *localedir;     // LOCALDIR environment var
 
   // adminutil.c
   time_t               cupsd_update;   // Last time we got or set cupsd.conf
index 66ca8eefe74ee9270687eb676e9a2411540e4aa3..1a9337788e9a6e7394655b77223b105970def232 100644 (file)
@@ -421,7 +421,7 @@ httpConnect2(
     return (NULL);
 
   // Optionally connect to the remote system...
-  if (msec == 0 || !httpReconnect2(http, msec, cancel))
+  if (msec == 0 || httpConnectAgain(http, msec, cancel))
     return (http);
 
   // Could not connect to any known address - bail out!
@@ -431,6 +431,107 @@ httpConnect2(
 }
 
 
+//
+// 'httpConnectAgain()' - Reconnect to a HTTP server with timeout and optional cancel variable.
+//
+
+bool                                   // O - `true` on success, `false` on failure
+httpConnectAgain(http_t *http,         // I - HTTP connection
+                int    msec,           // I - Timeout in milliseconds
+                int    *cancel)        // I - Pointer to "cancel" variable
+{
+  http_addrlist_t      *addr;          // Connected address
+#ifdef DEBUG
+  http_addrlist_t      *current;       // Current address
+  char                 temp[256];      // Temporary address string
+#endif // DEBUG
+
+
+  DEBUG_printf("httpConnectAgain(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel);
+
+  if (!http)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (false);
+  }
+
+  if (http->tls)
+  {
+    DEBUG_puts("1httpConnectAgain: Shutting down TLS...");
+    _httpTLSStop(http);
+  }
+
+  // Close any previously open socket...
+  if (http->fd >= 0)
+  {
+    DEBUG_printf("1httpConnectAgain: Closing socket %d...", http->fd);
+
+    httpAddrClose(NULL, http->fd);
+
+    http->fd = -1;
+  }
+
+  // Reset all state (except fields, which may be reused)...
+  http->state           = HTTP_STATE_WAITING;
+  http->version         = HTTP_VERSION_1_1;
+  http->keep_alive      = HTTP_KEEPALIVE_OFF;
+  memset(&http->_hostaddr, 0, sizeof(http->_hostaddr));
+  http->data_encoding   = HTTP_ENCODING_FIELDS;
+  http->_data_remaining = 0;
+  http->used            = 0;
+  http->data_remaining  = 0;
+  http->hostaddr        = NULL;
+  http->wused           = 0;
+
+  // Connect to the server...
+#ifdef DEBUG
+  for (current = http->addrlist; current; current = current->next)
+    DEBUG_printf("1httpConnectAgain: Address %s:%d", httpAddrString(&(current->addr), temp, sizeof(temp)), httpAddrPort(&(current->addr)));
+#endif // DEBUG
+
+  if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, cancel)) == NULL)
+  {
+    // Unable to connect...
+#ifdef _WIN32
+    http->error  = WSAGetLastError();
+#else
+    http->error  = errno;
+#endif // _WIN32
+    http->status = HTTP_STATUS_ERROR;
+
+    DEBUG_printf("1httpConnectAgain: httpAddrConnect failed: %s", strerror(http->error));
+
+    return (false);
+  }
+
+  DEBUG_printf("1httpConnectAgain: New socket=%d", http->fd);
+
+  if (http->timeout_value > 0)
+    http_set_timeout(http->fd, http->timeout_value);
+
+  http->hostaddr = &(addr->addr);
+  http->error    = 0;
+
+  if (http->encryption == HTTP_ENCRYPTION_ALWAYS)
+  {
+    // Always do encryption via TLS.
+    if (!_httpTLSStart(http))
+    {
+      httpAddrClose(NULL, http->fd);
+      http->fd = -1;
+
+      return (false);
+    }
+  }
+  else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls_upgrade)
+    return (http_tls_upgrade(http));
+
+  DEBUG_printf("1httpConnectAgain: Connected to %s:%d...", httpAddrGetString(http->hostaddr, temp, sizeof(temp)), httpAddrGetPort(http->hostaddr));
+
+  return (true);
+}
+
+
 //
 // 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
 //
@@ -2117,101 +2218,17 @@ httpReconnect(http_t *http)            // I - HTTP connection
 // 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
 //                      cancel.
 //
+// @deprecated@ @exclude all@
+//
 
 int                                    // O - 0 on success, non-zero on failure
 httpReconnect2(http_t *http,           // I - HTTP connection
               int    msec,             // I - Timeout in milliseconds
               int    *cancel)          // I - Pointer to "cancel" variable
 {
-  http_addrlist_t      *addr;          // Connected address
-#ifdef DEBUG
-  http_addrlist_t      *current;       // Current address
-  char                 temp[256];      // Temporary address string
-#endif // DEBUG
-
-
   DEBUG_printf("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel);
 
-  if (!http)
-  {
-    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
-    return (-1);
-  }
-
-  if (http->tls)
-  {
-    DEBUG_puts("2httpReconnect2: Shutting down TLS...");
-    _httpTLSStop(http);
-  }
-
-  // Close any previously open socket...
-  if (http->fd >= 0)
-  {
-    DEBUG_printf("2httpReconnect2: Closing socket %d...", http->fd);
-
-    httpAddrClose(NULL, http->fd);
-
-    http->fd = -1;
-  }
-
-  // Reset all state (except fields, which may be reused)...
-  http->state           = HTTP_STATE_WAITING;
-  http->version         = HTTP_VERSION_1_1;
-  http->keep_alive      = HTTP_KEEPALIVE_OFF;
-  memset(&http->_hostaddr, 0, sizeof(http->_hostaddr));
-  http->data_encoding   = HTTP_ENCODING_FIELDS;
-  http->_data_remaining = 0;
-  http->used            = 0;
-  http->data_remaining  = 0;
-  http->hostaddr        = NULL;
-  http->wused           = 0;
-
-  // Connect to the server...
-#ifdef DEBUG
-  for (current = http->addrlist; current; current = current->next)
-    DEBUG_printf("2httpReconnect2: Address %s:%d", httpAddrString(&(current->addr), temp, sizeof(temp)), httpAddrPort(&(current->addr)));
-#endif // DEBUG
-
-  if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, cancel)) == NULL)
-  {
-    // Unable to connect...
-#ifdef _WIN32
-    http->error  = WSAGetLastError();
-#else
-    http->error  = errno;
-#endif // _WIN32
-    http->status = HTTP_STATUS_ERROR;
-
-    DEBUG_printf("1httpReconnect2: httpAddrConnect failed: %s", strerror(http->error));
-
-    return (-1);
-  }
-
-  DEBUG_printf("2httpReconnect2: New socket=%d", http->fd);
-
-  if (http->timeout_value > 0)
-    http_set_timeout(http->fd, http->timeout_value);
-
-  http->hostaddr = &(addr->addr);
-  http->error    = 0;
-
-  if (http->encryption == HTTP_ENCRYPTION_ALWAYS)
-  {
-    // Always do encryption via TLS.
-    if (!_httpTLSStart(http))
-    {
-      httpAddrClose(NULL, http->fd);
-      http->fd = -1;
-
-      return (-1);
-    }
-  }
-  else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls_upgrade)
-    return (http_tls_upgrade(http) ? 0 : -1);
-
-  DEBUG_printf("1httpReconnect2: Connected to %s:%d...", httpAddrGetString(http->hostaddr, temp, sizeof(temp)), httpAddrGetPort(http->hostaddr));
-
-  return (0);
+  return (httpConnectAgain(http, msec, cancel) ? 0 : -1);
 }
 
 
@@ -2370,7 +2387,7 @@ httpSetEncryption(
     http->encryption = e;
 
     if ((http->encryption == HTTP_ENCRYPTION_ALWAYS && !http->tls) || (http->encryption == HTTP_ENCRYPTION_NEVER && http->tls))
-      return (!httpReconnect2(http, 30000, NULL));
+      return (httpConnectAgain(http, 30000, NULL));
     else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls)
       return (http_tls_upgrade(http));
     else
@@ -3976,7 +3993,7 @@ http_send(http_t       *http,             // I - HTTP connection
   {
     DEBUG_printf("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d", http->fd, http->status, http->tls_upgrade);
 
-    if (httpReconnect2(http, 30000, NULL))
+    if (!httpConnectAgain(http, 30000, NULL))
       return (false);
   }
 
@@ -3985,7 +4002,7 @@ http_send(http_t       *http,             // I - HTTP connection
   {
     if (httpFlushWrite(http) < 0)
     {
-      if (httpReconnect2(http, 30000, NULL))
+      if (!httpConnectAgain(http, 30000, NULL))
         return (false);
     }
   }
index bff23d8bf5c01e3a8eed74d82863792fec0eaaaa..8453e602121e37a1552b794bea91b5392fe1d64d 100644 (file)
@@ -415,6 +415,7 @@ extern void         httpClose(http_t *http) _CUPS_PUBLIC;
 extern int             httpCompareCredentials(cups_array_t *cred1, cups_array_t *cred2) _CUPS_DEPRECATED;
 extern http_t          *httpConnect(const char *host, int port) _CUPS_DEPRECATED_MSG("Use httpConnect2 instead.");
 extern http_t          *httpConnect2(const char *host, int port, http_addrlist_t *addrlist, int family, http_encryption_t encryption, int blocking, int msec, int *cancel) _CUPS_PUBLIC;
+extern bool            httpConnectAgain(http_t *http, int msec, int *cancel) _CUPS_PUBLIC;
 extern http_t          *httpConnectEncrypt(const char *host, int port, http_encryption_t encryption) _CUPS_DEPRECATED_MSG("Use httpConnect2 instead.");
 extern int             httpCopyCredentials(http_t *http, cups_array_t **credentials) _CUPS_DEPRECATED_MSG("Use httpCopyPeerCredentials instead.");
 extern char            *httpCopyPeerCredentials(http_t *http) _CUPS_PUBLIC;