Skip to content

Commit

Permalink
httpConnectAgain from libcups v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Jun 18, 2024
1 parent f369610 commit e185038
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 94 deletions.
6 changes: 3 additions & 3 deletions cups/cups-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
199 changes: 108 additions & 91 deletions cups/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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.
//
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions cups/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e185038

Please sign in to comment.