From 0c4bedc42d8a700cf74994545de0c93aa1ee1532 Mon Sep 17 00:00:00 2001 From: msweet Date: Wed, 17 Jul 2013 14:10:21 +0000 Subject: [PATCH] Reconnect as needed prior to sending a GET, HEAD, or PUT request. git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@11153 a1ca3aef-8c08-0410-bb20-df032aa958be --- CHANGES.txt | 5 +- cups/getputfile.c | 20 ++++++ cups/testhttp.c | 175 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 188 insertions(+), 12 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 6c3a5044b..8f1728d2c 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,7 +3,10 @@ CHANGES.txt - 1.7.0 - 2013-07-16 CHANGES IN CUPS V1.7.0 - - Fix a compile error in libcups () + - httpGetFd, httpGetFile, httpPutFd, and httpPutFile did not + automatically reconnect if the server closed the connecion after the + previous response. + - Fixed a compile error in libcups () - The scheduler incorrectly did not pass a FINAL_CONTENT_TYPE environment variable to the filters or backend () diff --git a/cups/getputfile.c b/cups/getputfile.c index 4a09099ee..b4be78ae5 100644 --- a/cups/getputfile.c +++ b/cups/getputfile.c @@ -84,6 +84,16 @@ cupsGetFd(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFA 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); @@ -296,6 +306,16 @@ cupsPutFd(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFA do { + 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)); diff --git a/cups/testhttp.c b/cups/testhttp.c index 4d0ee474a..a1fbc37aa 100644 --- a/cups/testhttp.c +++ b/cups/testhttp.c @@ -23,8 +23,7 @@ * Include necessary headers... */ -#include "string-private.h" -#include "http-private.h" +#include "cups-private.h" /* @@ -623,10 +622,87 @@ main(int argc, /* I - Number of command-line arguments */ continue; } printf("Checking file \"%s\"...\n", resource); - httpClearFields(http); - httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); - httpHead(http, resource); - while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); + + 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, httpGetAuthString(http)); + httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); + if (httpHead(http, resource)) + { + if (httpReconnect2(http, 30000, NULL)) + { + status = HTTP_STATUS_ERROR; + break; + } + else + { + status = HTTP_STATUS_UNAUTHORIZED; + continue; + } + } + + while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); + + if (status == HTTP_STATUS_UNAUTHORIZED) + { + /* + * Flush any error message... + */ + + httpFlush(http); + + /* + * See if we can do authentication... + */ + + if (cupsDoAuthentication(http, "GET", resource)) + { + status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; + break; + } + + if (httpReconnect2(http, 30000, NULL)) + { + status = HTTP_STATUS_ERROR; + break; + } + + continue; + } +#ifdef HAVE_SSL + else if (status == HTTP_STATUS_UPGRADE_REQUIRED) + { + /* Flush any error message... */ + httpFlush(http); + + /* Reconnect... */ + if (httpReconnect2(http, 30000, NULL)) + { + status = HTTP_STATUS_ERROR; + break; + } + + /* Upgrade with encryption... */ + httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); + + /* Try again, this time with encryption enabled... */ + continue; + } +#endif /* HAVE_SSL */ + } + while (status == HTTP_STATUS_UNAUTHORIZED || + status == HTTP_STATUS_UPGRADE_REQUIRED); if (status == HTTP_STATUS_OK) puts("HEAD OK:"); @@ -637,11 +713,88 @@ main(int argc, /* I - Number of command-line arguments */ printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource, encoding ? encoding : "identity"); - httpClearFields(http); - httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); - httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding); - httpGet(http, resource); - while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); + + 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, httpGetAuthString(http)); + httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); + httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding); + + if (httpGet(http, resource)) + { + if (httpReconnect2(http, 30000, NULL)) + { + status = HTTP_STATUS_ERROR; + break; + } + else + { + status = HTTP_STATUS_UNAUTHORIZED; + continue; + } + } + + while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); + + if (status == HTTP_STATUS_UNAUTHORIZED) + { + /* + * Flush any error message... + */ + + httpFlush(http); + + /* + * See if we can do authentication... + */ + + if (cupsDoAuthentication(http, "GET", resource)) + { + status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; + break; + } + + if (httpReconnect2(http, 30000, NULL)) + { + status = HTTP_STATUS_ERROR; + break; + } + + continue; + } +#ifdef HAVE_SSL + else if (status == HTTP_STATUS_UPGRADE_REQUIRED) + { + /* Flush any error message... */ + httpFlush(http); + + /* Reconnect... */ + if (httpReconnect2(http, 30000, NULL)) + { + status = HTTP_STATUS_ERROR; + break; + } + + /* Upgrade with encryption... */ + httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); + + /* Try again, this time with encryption enabled... */ + continue; + } +#endif /* HAVE_SSL */ + } + while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED); if (status == HTTP_STATUS_OK) puts("GET OK:"); -- 2.39.5