]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http.c
Migrate Windows conditional code to _WIN32 define.
[thirdparty/cups.git] / cups / http.c
index 36729b122bd19be077de10cee9da91d1c0442d84..794a213b2f030de9a386f65a0e72781ff16d30d4 100644 (file)
@@ -1,21 +1,14 @@
 /*
- * "$Id$"
- *
  * HTTP routines for CUPS.
  *
- * Copyright 2007-2013 by Apple Inc.
+ * Copyright 2007-2018 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  * This file contains Kerberos support code, copyright 2006 by
  * Jelmer Vernooij.
  *
- * 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/".
- *
- * This file is subject to the Apple OS-Developed Software exception.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
 #include "cups-private.h"
 #include <fcntl.h>
 #include <math.h>
-#ifdef WIN32
+#ifdef _WIN32
 #  include <tchar.h>
 #else
 #  include <signal.h>
 #  include <sys/time.h>
 #  include <sys/resource.h>
-#endif /* WIN32 */
+#endif /* _WIN32 */
 #ifdef HAVE_POLL
 #  include <poll.h>
 #endif /* HAVE_POLL */
@@ -41,6 +34,7 @@
  * Local functions...
  */
 
+static void            http_add_field(http_t *http, http_field_t field, const char *value, int append);
 #ifdef HAVE_LIBZ
 static void            http_content_coding_finish(http_t *http);
 static void            http_content_coding_start(http_t *http,
@@ -68,22 +62,7 @@ static void          http_set_timeout(int fd, double timeout);
 static void            http_set_wait(http_t *http);
 
 #ifdef HAVE_SSL
-static size_t          http_tls_pending(http_t *http);
-static int             http_tls_read(http_t *http, char *buf, int len);
-static int             http_tls_set_credentials(http_t *http);
-static int             http_tls_start(http_t *http);
-static void            http_tls_stop(http_t *http);
 static int             http_tls_upgrade(http_t *http);
-static int             http_tls_write(http_t *http, const char *buf, int len);
-#  ifdef HAVE_LIBSSL
-#    include "tls-openssl.c"
-#  elif defined(HAVE_GNUTLS)
-#    include "tls-gnutls.c"
-#  elif defined(HAVE_CDSASSL)
-#    include "tls-darwin.c"
-#  else
-#    include "tls-sspi.c"
-#  endif /* HAVE_LIBSSL */
 #endif /* HAVE_SSL */
 
 
@@ -122,7 +101,8 @@ static const char * const http_fields[] =
                          "WWW-Authenticate",
                          "Accept-Encoding",
                          "Allow",
-                         "Server"
+                         "Server",
+                         "Authentication-Info"
                        };
 
 
@@ -130,7 +110,7 @@ static const char * const http_fields[] =
  * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
  *                            specified listening socket.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 http_t *                               /* O - HTTP connection or @code NULL@ */
@@ -220,7 +200,7 @@ httpAcceptConnection(int fd,                /* I - Listen socket file descriptor */
  *
  * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -280,7 +260,7 @@ httpCheck(http_t *http)                     /* I - HTTP connection */
 /*
  * 'httpClearCookie()' - Clear the cookie value(s).
  *
- * @since CUPS 1.1.19/OS X 10.3@
+ * @since CUPS 1.1.19/macOS 10.3@
  */
 
 void
@@ -304,11 +284,22 @@ httpClearCookie(http_t *http)             /* I - HTTP connection */
 void
 httpClearFields(http_t *http)          /* I - HTTP connection */
 {
-  DEBUG_printf(("httpClearFields(http=%p)", http));
+  http_field_t field;                  /* Current field */
+
+
+  DEBUG_printf(("httpClearFields(http=%p)", (void *)http));
 
   if (http)
   {
-    memset(http->fields, 0, sizeof(http->fields));
+    memset(http->_fields, 0, sizeof(http->fields));
+
+    for (field = HTTP_FIELD_ACCEPT_LANGUAGE; field < HTTP_FIELD_MAX; field ++)
+    {
+      if (http->fields[field] && http->fields[field] != http->_fields[field])
+        free(http->fields[field]);
+
+      http->fields[field] = NULL;
+    }
 
     if (http->mode == _HTTP_MODE_CLIENT)
     {
@@ -318,30 +309,6 @@ httpClearFields(http_t *http)              /* I - HTTP connection */
        httpSetField(http, HTTP_FIELD_HOST, http->hostname);
     }
 
-    if (http->field_authorization)
-    {
-      free(http->field_authorization);
-      http->field_authorization = NULL;
-    }
-
-    if (http->accept_encoding)
-    {
-      _cupsStrFree(http->accept_encoding);
-      http->accept_encoding = NULL;
-    }
-
-    if (http->allow)
-    {
-      _cupsStrFree(http->allow);
-      http->allow = NULL;
-    }
-
-    if (http->server)
-    {
-      _cupsStrFree(http->server);
-      http->server = NULL;
-    }
-
     http->expect = (http_status_t)0;
   }
 }
@@ -359,7 +326,7 @@ httpClose(http_t *http)                     /* I - HTTP connection */
 #endif /* HAVE_GSSAPI */
 
 
-  DEBUG_printf(("httpClose(http=%p)", http));
+  DEBUG_printf(("httpClose(http=%p)", (void *)http));
 
  /*
   * Range check input...
@@ -405,12 +372,36 @@ httpClose(http_t *http)                   /* I - HTTP connection */
 }
 
 
+/*
+ * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
+ *
+ * @since CUPS 2.0/OS 10.10@
+ */
+
+int                                    /* O - 1 if they match, 0 if they do not */
+httpCompareCredentials(
+    cups_array_t *cred1,               /* I - First set of X.509 credentials */
+    cups_array_t *cred2)               /* I - Second set of X.509 credentials */
+{
+  http_credential_t    *temp1, *temp2; /* Temporary credentials */
+
+
+  for (temp1 = (http_credential_t *)cupsArrayFirst(cred1), temp2 = (http_credential_t *)cupsArrayFirst(cred2); temp1 && temp2; temp1 = (http_credential_t *)cupsArrayNext(cred1), temp2 = (http_credential_t *)cupsArrayNext(cred2))
+    if (temp1->datalen != temp2->datalen)
+      return (0);
+    else if (memcmp(temp1->data, temp2->data, temp1->datalen))
+      return (0);
+
+  return (temp1 == temp2);
+}
+
+
 /*
  * 'httpConnect()' - Connect to a HTTP server.
  *
  * This function is deprecated - use @link httpConnect2@ instead.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
 http_t *                               /* O - New HTTP connection */
@@ -425,14 +416,14 @@ httpConnect(const char *host,             /* I - Host to connect to */
 /*
  * 'httpConnect2()' - Connect to a HTTP server.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 http_t *                               /* O - New HTTP connection */
 httpConnect2(
     const char        *host,           /* I - Host to connect to */
     int               port,            /* I - Port number */
-    http_addrlist_t   *addrlist,       /* I - List of addresses or NULL to lookup */
+    http_addrlist_t   *addrlist,       /* I - List of addresses or @code NULL@ to lookup */
     int               family,          /* I - Address family to use or @code AF_UNSPEC@ for any */
     http_encryption_t encryption,      /* I - Type of encryption to use */
     int               blocking,                /* I - 1 for blocking connection, 0 for non-blocking */
@@ -442,9 +433,7 @@ httpConnect2(
   http_t       *http;                  /* New HTTP connection */
 
 
-  DEBUG_printf(("httpConnect2(host=\"%s\", port=%d, addrlist=%p, family=%d, "
-                "encryption=%d, blocking=%d, msec=%d, cancel=%p)", host, port,
-                addrlist, family, encryption, blocking, msec, cancel));
+  DEBUG_printf(("httpConnect2(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%d, msec=%d, cancel=%p)", host, port, (void *)addrlist, family, encryption, blocking, msec, (void *)cancel));
 
  /*
   * Create the HTTP structure...
@@ -477,7 +466,7 @@ httpConnect2(
  * This function is now deprecated. Please use the @link httpConnect2@ function
  * instead.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
 http_t *                               /* O - New HTTP connection */
@@ -515,7 +504,7 @@ _httpDisconnect(http_t *http)               /* I - HTTP connection */
 {
 #ifdef HAVE_SSL
   if (http->tls)
-    http_tls_stop(http);
+    _httpTLSStop(http);
 #endif /* HAVE_SSL */
 
   httpAddrClose(NULL, http->fd);
@@ -532,7 +521,7 @@ int                                 /* O - -1 on error, 0 on success */
 httpEncryption(http_t            *http,        /* I - HTTP connection */
                http_encryption_t e)    /* I - New encryption preference */
 {
-  DEBUG_printf(("httpEncryption(http=%p, e=%d)", http, e));
+  DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http, e));
 
 #ifdef HAVE_SSL
   if (!http)
@@ -557,7 +546,7 @@ httpEncryption(http_t            *http,     /* I - HTTP connection */
 
     http->encryption = e;
     if (e != HTTP_ENCRYPTION_IF_REQUESTED && !http->tls)
-      return (http_tls_start(http));
+      return (_httpTLSStart(http));
     else
       return (0);
   }
@@ -604,7 +593,7 @@ httpFieldValue(const char *name)    /* I - String name */
 
 
 /*
- * 'httpFlush()' - Flush data from a HTTP connection.
+ * 'httpFlush()' - Flush data read from a HTTP connection.
  */
 
 void
@@ -615,8 +604,7 @@ httpFlush(http_t *http)                     /* I - HTTP connection */
   http_state_t oldstate;               /* Old state */
 
 
-  DEBUG_printf(("httpFlush(http=%p), state=%s", http,
-                httpStateString(http->state)));
+  DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http, httpStateString(http->state)));
 
  /*
   * Nothing to do if we are in the "waiting" state...
@@ -664,7 +652,7 @@ httpFlush(http_t *http)                     /* I - HTTP connection */
 
 #ifdef HAVE_SSL
     if (http->tls)
-      http_tls_stop(http);
+      _httpTLSStop(http);
 #endif /* HAVE_SSL */
 
     httpAddrClose(NULL, http->fd);
@@ -675,19 +663,18 @@ httpFlush(http_t *http)                   /* I - HTTP connection */
 
 
 /*
- * 'httpFlushWrite()' - Flush data in write buffer.
+ * 'httpFlushWrite()' - Flush data written to a HTTP connection.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 int                                    /* O - Bytes written or -1 on error */
 httpFlushWrite(http_t *http)           /* I - HTTP connection */
 {
-  int  bytes;                          /* Bytes written */
+  ssize_t      bytes;                  /* Bytes written */
 
 
-  DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", http,
-                http ? http->data_encoding : -1));
+  DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http, http ? http->data_encoding : 100));
 
   if (!http || !http->wused)
   {
@@ -697,15 +684,15 @@ httpFlushWrite(http_t *http)              /* I - HTTP connection */
   }
 
   if (http->data_encoding == HTTP_ENCODING_CHUNKED)
-    bytes = http_write_chunk(http, http->wbuffer, http->wused);
+    bytes = http_write_chunk(http, http->wbuffer, (size_t)http->wused);
   else
-    bytes = http_write(http, http->wbuffer, http->wused);
+    bytes = http_write(http, http->wbuffer, (size_t)http->wused);
 
   http->wused = 0;
 
-  DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", bytes, errno));
+  DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes, errno));
 
-  return (bytes);
+  return ((int)bytes);
 }
 
 
@@ -748,9 +735,9 @@ httpGet(http_t     *http,           /* I - HTTP connection */
 /*
  * 'httpGetActivity()' - Get the most recent activity for a connection.
  *
- * The return value is the UNIX time of the last read or write.
+ * The return value is the time in seconds of the last read or write.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 time_t                                 /* O - Time of last read or write */
@@ -763,12 +750,12 @@ httpGetActivity(http_t *http)             /* I - HTTP connection */
 /*
  * 'httpGetAuthString()' - Get the current authorization string.
  *
- * The authorization string is set by cupsDoAuthentication() and
- * httpSetAuthString().  Use httpGetAuthString() to retrieve the
- * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
- * value.
+ * The authorization string is set by @link cupsDoAuthentication@ and
+ * @link httpSetAuthString@.  Use @link httpGetAuthString@ to retrieve the
+ * string to use with @link httpSetField@ for the
+ * @code HTTP_FIELD_AUTHORIZATION@ value.
  *
- * @since CUPS 1.3/OS X 10.5@
+ * @since CUPS 1.3/macOS 10.5@
  */
 
 char *                                 /* O - Authorization string */
@@ -784,7 +771,7 @@ httpGetAuthString(http_t *http)             /* I - HTTP connection */
 /*
  * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 int                                    /* O - 1 if blocking, 0 if non-blocking */
@@ -803,7 +790,7 @@ httpGetBlocking(http_t *http)               /* I - HTTP connection */
  * client.  The value returned can be use in subsequent requests (for clients)
  * or in the response (for servers) in order to compress the content stream.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 const char *                           /* O - Content-Coding value or
@@ -812,7 +799,7 @@ const char *                                /* O - Content-Coding value or
 httpGetContentEncoding(http_t *http)   /* I - HTTP connection */
 {
 #ifdef HAVE_LIBZ
-  if (http && http->accept_encoding)
+  if (http && http->fields[HTTP_FIELD_ACCEPT_ENCODING])
   {
     int                i;                      /* Looping var */
     char       temp[HTTP_MAX_VALUE],   /* Copy of Accepts-Encoding value */
@@ -828,7 +815,7 @@ httpGetContentEncoding(http_t *http)        /* I - HTTP connection */
       "x-gzip"
     };
 
-    strlcpy(temp, http->accept_encoding, sizeof(temp));
+    strlcpy(temp, http->fields[HTTP_FIELD_ACCEPT_ENCODING], sizeof(temp));
 
     for (start = temp; *start; start = end)
     {
@@ -885,11 +872,11 @@ httpGetContentEncoding(http_t *http)      /* I - HTTP connection */
 /*
  * 'httpGetCookie()' - Get any cookie data from the response.
  *
- * @since CUPS 1.1.19/OS X 10.3@
+ * @since CUPS 1.1.19/macOS 10.3@
  */
 
-const char *                           /* O - Cookie data or NULL */
-httpGetCookie(http_t *http)            /* I - HTTP connecion */
+const char *                           /* O - Cookie data or @code NULL@ */
+httpGetCookie(http_t *http)            /* I - HTTP connection */
 {
   return (http ? http->cookie : NULL);
 }
@@ -902,7 +889,7 @@ httpGetCookie(http_t *http)         /* I - HTTP connecion */
  * @link httpIsEncrypted@ function to determine whether a TLS session has
  * been established.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 http_encryption_t                      /* O - Current encryption mode */
@@ -918,7 +905,7 @@ httpGetEncryption(http_t *http)             /* I - HTTP connection */
  * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
  * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 http_status_t                          /* O - Expect: status, if any */
@@ -934,7 +921,7 @@ httpGetExpect(http_t *http)         /* I - HTTP connection */
 /*
  * 'httpGetFd()' - Get the file descriptor associated with a connection.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 int                                    /* O - File descriptor or -1 if none */
@@ -954,39 +941,17 @@ httpGetField(http_t       *http,  /* I - HTTP connection */
 {
   if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
     return (NULL);
-
-  switch (field)
-  {
-    case HTTP_FIELD_ACCEPT_ENCODING :
-        return (http->accept_encoding);
-
-    case HTTP_FIELD_ALLOW :
-        return (http->allow);
-
-    case HTTP_FIELD_SERVER :
-        return (http->server);
-
-    case HTTP_FIELD_AUTHORIZATION :
-        if (http->field_authorization)
-       {
-        /*
-         * Special case for WWW-Authenticate: as its contents can be
-         * longer than HTTP_MAX_VALUE...
-         */
-
-         return (http->field_authorization);
-       }
-
-    default :
-        return (http->fields[field]);
-  }
+  else if (http->fields[field])
+    return (http->fields[field]);
+  else
+    return ("");
 }
 
 
 /*
  * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 http_keepalive_t                       /* O - Keep-Alive state */
@@ -1003,7 +968,7 @@ httpGetKeepAlive(http_t *http)             /* I - HTTP connection */
  * This function is deprecated and will not return lengths larger than
  * 2^31 - 1; use httpGetLength2() instead.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
 int                                    /* O - Content length */
@@ -1031,7 +996,7 @@ httpGetLength(http_t *http)                /* I - HTTP connection */
  * This function returns the complete content length, even for
  * content larger than 2^31 - 1.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 off_t                                  /* O - Content length */
@@ -1040,13 +1005,12 @@ httpGetLength2(http_t *http)            /* I - HTTP connection */
   off_t                        remaining;      /* Remaining length */
 
 
-  DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http,
-                httpStateString(http->state)));
+  DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http, httpStateString(http->state)));
 
   if (!http)
     return (-1);
 
-  if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"))
+  if (http->fields[HTTP_FIELD_TRANSFER_ENCODING] && !_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"))
   {
     DEBUG_puts("4httpGetLength2: chunked request!");
     remaining = 0;
@@ -1061,7 +1025,7 @@ httpGetLength2(http_t *http)              /* I - HTTP connection */
     * after the transfer is complete...
     */
 
-    if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
+    if (!http->fields[HTTP_FIELD_CONTENT_LENGTH] || !http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
     {
      /*
       * Default content length is 0 for errors and certain types of operations,
@@ -1095,20 +1059,20 @@ httpGetLength2(http_t *http)            /* I - HTTP connection */
 /*
  * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 size_t                                 /* O - Number of bytes buffered */
 httpGetPending(http_t *http)           /* I - HTTP connection */
 {
-  return (http ? http->wused : 0);
+  return (http ? (size_t)http->wused : 0);
 }
 
 
 /*
  * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 size_t                                 /* O - Number of bytes available */
@@ -1117,23 +1081,10 @@ httpGetReady(http_t *http)              /* I - HTTP connection */
   if (!http)
     return (0);
   else if (http->used > 0)
-    return (http->used);
+    return ((size_t)http->used);
 #ifdef HAVE_SSL
   else if (http->tls)
-  {
-    size_t     ready;                  /* Ready bytes */
-
-#  ifdef HAVE_LIBSSL
-    if ((ready = SSL_pending((SSL *)(http->tls))) > 0)
-      return (ready);
-#  elif defined(HAVE_GNUTLS)
-    if ((ready = gnutls_record_check_pending(http->tls)) > 0)
-      return (ready);
-#  elif defined(HAVE_CDSASSL)
-    if (!SSLGetBufferedReadSize(http->tls, &ready) && ready > 0)
-      return (ready);
-#  endif /* HAVE_LIBSSL */
-  }
+    return (_httpTLSPending(http));
 #endif /* HAVE_SSL */
 
   return (0);
@@ -1147,13 +1098,13 @@ httpGetReady(http_t *http)              /* I - HTTP connection */
  * The @link httpIsChunked@ function can be used to determine whether the
  * message body is chunked or fixed-length.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 size_t                                 /* O - Remaining bytes */
 httpGetRemaining(http_t *http)         /* I - HTTP connection */
 {
-  return (http ? http->data_remaining : 0);
+  return (http ? (size_t)http->data_remaining : 0);
 }
 
 
@@ -1161,20 +1112,20 @@ httpGetRemaining(http_t *http)          /* I - HTTP connection */
  * 'httpGets()' - Get a line of text from a HTTP connection.
  */
 
-char *                                 /* O - Line or NULL */
+char *                                 /* O - Line or @code NULL@ */
 httpGets(char   *line,                 /* I - Line to read into */
          int    length,                        /* I - Max length of buffer */
         http_t *http)                  /* I - HTTP connection */
 {
-  char *lineptr,                       /* Pointer into line */
-       *lineend,                       /* End of line */
-       *bufptr,                        /* Pointer into input buffer */
-       *bufend;                        /* Pointer to end of buffer */
-  int  bytes,                          /* Number of bytes read */
-       eol;                            /* End-of-line? */
+  char         *lineptr,               /* Pointer into line */
+               *lineend,               /* End of line */
+               *bufptr,                /* Pointer into input buffer */
+               *bufend;                /* Pointer to end of buffer */
+  ssize_t      bytes;                  /* Number of bytes read */
+  int          eol;                    /* End-of-line? */
 
 
-  DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", line, length, http));
+  DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line, length, (void *)http));
 
   if (!http || !line || length <= 1)
     return (NULL);
@@ -1194,11 +1145,11 @@ httpGets(char   *line,                  /* I - Line to read into */
     * Pre-load the buffer as needed...
     */
 
-#ifdef WIN32
+#ifdef _WIN32
     WSASetLastError(0);
 #else
     errno = 0;
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
     while (http->used == 0)
     {
@@ -1212,18 +1163,17 @@ httpGets(char   *line,                  /* I - Line to read into */
          continue;
 
         DEBUG_puts("3httpGets: Timed out!");
-#ifdef WIN32
+#ifdef _WIN32
         http->error = WSAETIMEDOUT;
 #else
         http->error = ETIMEDOUT;
-#endif /* WIN32 */
+#endif /* _WIN32 */
         return (NULL);
       }
 
-      bytes = http_read(http, http->buffer + http->used,
-                        HTTP_MAX_BUFFER - http->used);
+      bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used));
 
-      DEBUG_printf(("4httpGets: read %d bytes.", bytes));
+      DEBUG_printf(("4httpGets: read " CUPS_LLFMT " bytes.", CUPS_LLCAST bytes));
 
       if (bytes < 0)
       {
@@ -1231,7 +1181,7 @@ httpGets(char   *line,                    /* I - Line to read into */
        * Nope, can't get a line this time...
        */
 
-#ifdef WIN32
+#ifdef _WIN32
         DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
 
         if (WSAGetLastError() == WSAEINTR)
@@ -1268,7 +1218,7 @@ httpGets(char   *line,                    /* I - Line to read into */
          http->error = errno;
          continue;
        }
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
         return (NULL);
       }
@@ -1283,7 +1233,7 @@ httpGets(char   *line,                    /* I - Line to read into */
       * Yup, update the amount used...
       */
 
-      http->used += bytes;
+      http->used += (int)bytes;
     }
 
    /*
@@ -1307,7 +1257,7 @@ httpGets(char   *line,                    /* I - Line to read into */
 
     http->used -= (int)(bufptr - http->buffer);
     if (http->used > 0)
-      memmove(http->buffer, bufptr, http->used);
+      memmove(http->buffer, bufptr, (size_t)http->used);
 
     if (eol)
     {
@@ -1345,7 +1295,7 @@ httpGetState(http_t *http)                /* I - HTTP connection */
 /*
  * 'httpGetStatus()' - Get the status of the last HTTP request.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 http_status_t                          /* O - HTTP status */
@@ -1358,10 +1308,10 @@ httpGetStatus(http_t *http)             /* I - HTTP connection */
 /*
  * 'httpGetSubField()' - Get a sub-field value.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
-char *                                 /* O - Value or NULL */
+char *                                 /* O - Value or @code NULL@ */
 httpGetSubField(http_t       *http,    /* I - HTTP connection */
                 http_field_t field,    /* I - Field index */
                 const char   *name,    /* I - Name of sub-field */
@@ -1374,10 +1324,10 @@ httpGetSubField(http_t       *http,     /* I - HTTP connection */
 /*
  * 'httpGetSubField2()' - Get a sub-field value.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
-char *                                 /* O - Value or NULL */
+char *                                 /* O - Value or @code NULL@ */
 httpGetSubField2(http_t       *http,   /* I - HTTP connection */
                  http_field_t field,   /* I - Field index */
                  const char   *name,   /* I - Name of sub-field */
@@ -1389,8 +1339,7 @@ httpGetSubField2(http_t       *http,      /* I - HTTP connection */
                *ptr,                   /* Pointer into string buffer */
                *end;                   /* End of value buffer */
 
-  DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, "
-                "valuelen=%d)", http, field, name, value, valuelen));
+  DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http, field, name, (void *)value, valuelen));
 
   if (!http || !name || !value || valuelen < 2 ||
       field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
@@ -1522,7 +1471,7 @@ int                                       /* O - Status of call (0 = success) */
 httpHead(http_t     *http,             /* I - HTTP connection */
          const char *uri)              /* I - URI for head */
 {
-  DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http, uri));
+  DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http, uri));
   return (http_send(http, HTTP_STATE_HEAD, uri));
 }
 
@@ -1536,13 +1485,9 @@ void
 httpInitialize(void)
 {
   static int   initialized = 0;        /* Have we been called before? */
-#ifdef WIN32
+#ifdef _WIN32
   WSADATA      winsockdata;            /* WinSock data */
-#endif /* WIN32 */
-#ifdef HAVE_LIBSSL
-  int          i;                      /* Looping var */
-  unsigned char        data[1024];             /* Seed data */
-#endif /* HAVE_LIBSSL */
+#endif /* _WIN32 */
 
 
   _cupsGlobalLock();
@@ -1552,7 +1497,7 @@ httpInitialize(void)
     return;
   }
 
-#ifdef WIN32
+#ifdef _WIN32
   WSAStartup(MAKEWORD(2,2), &winsockdata);
 
 #elif !defined(SO_NOSIGPIPE)
@@ -1574,10 +1519,10 @@ httpInitialize(void)
 #  else
   signal(SIGPIPE, SIG_IGN);
 #  endif /* !SO_NOSIGPIPE */
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
 #  ifdef HAVE_SSL
-  http_tls_initialize();
+  _httpTLSInitialize();
 #  endif /* HAVE_SSL */
 
   initialized = 1;
@@ -1591,7 +1536,7 @@ httpInitialize(void)
  * This function returns non-zero if the message body is composed of
  * variable-length chunks.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 int                                    /* O - 1 if chunked, 0 if not */
@@ -1606,7 +1551,7 @@ httpIsChunked(http_t *http)               /* I - HTTP connection */
  *
  * This function returns non-zero if the connection is currently encrypted.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 int                                    /* O - 1 if encrypted, 0 if not */
@@ -1633,11 +1578,11 @@ httpOptions(http_t     *http,           /* I - HTTP connection */
  *
  * This function copies available data from the given HTTP connection, reading
  * a buffer as needed.  The data is still available for reading using
- * @link httpRead@ or @link httpRead2@.
+ * @link httpRead2@.
  *
  * For non-blocking connections the usual timeouts apply.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 ssize_t                                        /* O - Number of bytes copied */
@@ -1649,8 +1594,7 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
   char         len[32];                /* Length string */
 
 
-  DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")",
-                http, buffer, CUPS_LLCAST length));
+  DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
   if (http == NULL || buffer == NULL)
     return (-1);
@@ -1702,7 +1646,7 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
     */
 
 #ifdef HAVE_LIBZ
-    if (http->coding)
+    if (http->coding >= _HTTP_CODING_GUNZIP)
       http_content_coding_finish(http);
 #endif /* HAVE_LIBZ */
 
@@ -1730,7 +1674,8 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
 
 #ifdef HAVE_LIBZ
   if (http->used == 0 &&
-      (http->coding == _HTTP_CODING_IDENTITY || http->stream.avail_in == 0))
+      (http->coding == _HTTP_CODING_IDENTITY ||
+       (http->coding >= _HTTP_CODING_GUNZIP && http->stream.avail_in == 0)))
 #else
   if (http->used == 0)
 #endif /* HAVE_LIBZ */
@@ -1752,13 +1697,13 @@ httpPeek(http_t *http,                  /* I - HTTP connection */
       }
     }
 
-    if (http->data_remaining > sizeof(http->buffer))
+    if ((size_t)http->data_remaining > sizeof(http->buffer))
       buflen = sizeof(http->buffer);
     else
-      buflen = http->data_remaining;
+      buflen = (ssize_t)http->data_remaining;
 
     DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen));
-    bytes = http_read(http, http->buffer, buflen);
+    bytes = http_read(http, http->buffer, (size_t)buflen);
 
     DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT " bytes into buffer.",
                   CUPS_LLCAST bytes));
@@ -1768,12 +1713,12 @@ httpPeek(http_t *http,                  /* I - HTTP connection */
       http_debug_hex("httpPeek", http->buffer, (int)bytes);
 #endif /* DEBUG */
 
-      http->used = bytes;
+      http->used = (int)bytes;
     }
   }
 
 #ifdef HAVE_LIBZ
-  if (http->coding)
+  if (http->coding >= _HTTP_CODING_GUNZIP)
   {
 #  ifdef HAVE_INFLATECOPY
     int                zerr;                   /* Decompressor error */
@@ -1785,27 +1730,27 @@ httpPeek(http_t *http,                  /* I - HTTP connection */
                                        /* Number of bytes to copy */
 
       if (http->stream.avail_in > 0 &&
-         http->stream.next_in > http->dbuffer)
-        memmove(http->dbuffer, http->stream.next_in, http->stream.avail_in);
+         http->stream.next_in > http->sbuffer)
+        memmove(http->sbuffer, http->stream.next_in, http->stream.avail_in);
 
-      http->stream.next_in = http->dbuffer;
+      http->stream.next_in = http->sbuffer;
 
-      if (buflen > http->data_remaining)
-        buflen = http->data_remaining;
+      if (buflen > (size_t)http->data_remaining)
+        buflen = (size_t)http->data_remaining;
 
-      if (buflen > http->used)
-        buflen = http->used;
+      if (buflen > (size_t)http->used)
+        buflen = (size_t)http->used;
 
       DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
                    "decompression buffer.", (int)buflen));
 
-      memcpy(http->dbuffer + http->stream.avail_in, http->buffer, buflen);
+      memcpy(http->sbuffer + http->stream.avail_in, http->buffer, buflen);
       http->stream.avail_in += buflen;
-      http->used            -= buflen;
-      http->data_remaining  -= buflen;
+      http->used            -= (int)buflen;
+      http->data_remaining  -= (off_t)buflen;
 
       if (http->used > 0)
-        memmove(http->buffer, http->buffer + buflen, http->used);
+        memmove(http->buffer, http->buffer + buflen, (size_t)http->used);
     }
 
     DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length,
@@ -1819,7 +1764,7 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
     }
 
     stream.next_out  = (Bytef *)buffer;
-    stream.avail_out = length;
+    stream.avail_out = (uInt)length;
 
     zerr = inflate(&stream, Z_SYNC_FLUSH);
     inflateEnd(&stream);
@@ -1828,15 +1773,14 @@ httpPeek(http_t *http,                  /* I - HTTP connection */
     {
       DEBUG_printf(("2httpPeek: zerr=%d", zerr));
 #ifdef DEBUG
-      http_debug_hex("2httpPeek", (char *)http->dbuffer,
-                    http->stream.avail_in);
+      http_debug_hex("2httpPeek", (char *)http->sbuffer, (int)http->stream.avail_in);
 #endif /* DEBUG */
 
       http->error = EIO;
       return (-1);
     }
 
-    bytes = length - http->stream.avail_out;
+    bytes = (ssize_t)(length - http->stream.avail_out);
 
 #  else
     DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
@@ -1863,7 +1807,7 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
 
   if (bytes < 0)
   {
-#ifdef WIN32
+#ifdef _WIN32
     if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK)
       bytes = 0;
     else
@@ -1873,7 +1817,7 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
       bytes = 0;
     else
       http->error = errno;
-#endif /* WIN32 */
+#endif /* _WIN32 */
   }
   else if (bytes == 0)
   {
@@ -1884,10 +1828,6 @@ httpPeek(http_t *http,                   /* I - HTTP connection */
   return (bytes);
 }
 
-/* For OS X 10.8 and earlier */
-ssize_t _httpPeek(http_t *http, char *buffer, size_t length)
-{ return (httpPeek(http, buffer, length)); }
-
 
 /*
  * 'httpPost()' - Send a POST request to the server.
@@ -1912,21 +1852,21 @@ httpPrintf(http_t     *http,            /* I - HTTP connection */
            const char *format,         /* I - printf-style format string */
           ...)                         /* I - Additional args as needed */
 {
-  int          bytes;                  /* Number of bytes to write */
+  ssize_t      bytes;                  /* Number of bytes to write */
   char         buf[16384];             /* Buffer for formatted string */
   va_list      ap;                     /* Variable argument pointer */
 
 
-  DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", http, format));
+  DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http, format));
 
   va_start(ap, format);
   bytes = vsnprintf(buf, sizeof(buf), format, ap);
   va_end(ap);
 
-  DEBUG_printf(("3httpPrintf: (%d bytes) %s", bytes, buf));
+  DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT " bytes) %s", CUPS_LLCAST bytes, buf));
 
   if (http->data_encoding == HTTP_ENCODING_FIELDS)
-    return (httpWrite2(http, buf, bytes));
+    return ((int)httpWrite2(http, buf, (size_t)bytes));
   else
   {
     if (http->wused)
@@ -1937,7 +1877,7 @@ httpPrintf(http_t     *http,              /* I - HTTP connection */
        return (-1);
     }
 
-    return (http_write(http, buf, bytes));
+    return ((int)http_write(http, buf, (size_t)bytes));
   }
 }
 
@@ -1950,7 +1890,7 @@ int                                       /* O - Status of call (0 = success) */
 httpPut(http_t     *http,              /* I - HTTP connection */
         const char *uri)               /* I - URI to put */
 {
-  DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http, uri));
+  DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http, uri));
   return (http_send(http, HTTP_STATE_PUT, uri));
 }
 
@@ -1961,7 +1901,7 @@ httpPut(http_t     *http,         /* I - HTTP connection */
  * This function is deprecated. Use the httpRead2() function which can
  * read more than 2GB of data.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
 int                                    /* O - Number of bytes read */
@@ -1969,14 +1909,14 @@ httpRead(http_t *http,                  /* I - HTTP connection */
          char   *buffer,               /* I - Buffer for data */
         int    length)                 /* I - Maximum number of bytes */
 {
-  return ((int)httpRead2(http, buffer, length));
+  return ((int)httpRead2(http, buffer, (size_t)length));
 }
 
 
 /*
  * 'httpRead2()' - Read data from a HTTP connection.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 ssize_t                                        /* O - Number of bytes read */
@@ -1988,16 +1928,9 @@ httpRead2(http_t *http,                  /* I - HTTP connection */
 
 
 #ifdef HAVE_LIBZ
-  DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
-                ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT,
-                http, buffer, CUPS_LLCAST length,
-                http->coding,
-                http->data_encoding, CUPS_LLCAST http->data_remaining));
+  DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)http, (void *)buffer, CUPS_LLCAST length, http->coding, http->data_encoding, CUPS_LLCAST http->data_remaining));
 #else
-  DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
-                ") data_encoding=%d data_remaining=" CUPS_LLFMT,
-                http, buffer, CUPS_LLCAST length,
-                http->data_encoding, CUPS_LLCAST http->data_remaining));
+  DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT ") data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)http, (void *)buffer, CUPS_LLCAST length, http->data_encoding, CUPS_LLCAST http->data_remaining));
 #endif /* HAVE_LIBZ */
 
   if (http == NULL || buffer == NULL)
@@ -2010,7 +1943,7 @@ httpRead2(http_t *http,                   /* I - HTTP connection */
     return (0);
 
 #ifdef HAVE_LIBZ
-  if (http->coding)
+  if (http->coding >= _HTTP_CODING_GUNZIP)
   {
     do
     {
@@ -2022,21 +1955,20 @@ httpRead2(http_t *http,                 /* I - HTTP connection */
                      (int)http->stream.avail_in, (int)length));
 
        http->stream.next_out  = (Bytef *)buffer;
-       http->stream.avail_out = length;
+       http->stream.avail_out = (uInt)length;
 
        if ((zerr = inflate(&(http->stream), Z_SYNC_FLUSH)) < Z_OK)
        {
          DEBUG_printf(("2httpRead2: zerr=%d", zerr));
 #ifdef DEBUG
-          http_debug_hex("2httpRead2", (char *)http->dbuffer,
-                         http->stream.avail_in);
+          http_debug_hex("2httpRead2", (char *)http->sbuffer, (int)http->stream.avail_in);
 #endif /* DEBUG */
 
          http->error = EIO;
          return (-1);
        }
 
-       bytes = length - http->stream.avail_out;
+       bytes = (ssize_t)(length - http->stream.avail_out);
 
        DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
                      http->stream.avail_in, http->stream.avail_out,
@@ -2047,16 +1979,16 @@ httpRead2(http_t *http,                 /* I - HTTP connection */
 
       if (bytes == 0)
       {
-        ssize_t buflen = HTTP_MAX_BUFFER - http->stream.avail_in;
+        ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)http->stream.avail_in;
                                        /* Additional bytes for buffer */
 
         if (buflen > 0)
         {
           if (http->stream.avail_in > 0 &&
-              http->stream.next_in > http->dbuffer)
-            memmove(http->dbuffer, http->stream.next_in, http->stream.avail_in);
+              http->stream.next_in > http->sbuffer)
+            memmove(http->sbuffer, http->stream.next_in, http->stream.avail_in);
 
-         http->stream.next_in = http->dbuffer;
+         http->stream.next_in = http->sbuffer;
 
           DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
                         "decompression buffer.", (int)buflen));
@@ -2064,16 +1996,12 @@ httpRead2(http_t *http,                 /* I - HTTP connection */
           if (http->data_remaining > 0)
           {
            if (buflen > http->data_remaining)
-             buflen = http->data_remaining;
+             buflen = (ssize_t)http->data_remaining;
 
-           bytes = http_read_buffered(http,
-                                      (char *)http->dbuffer +
-                                              http->stream.avail_in, buflen);
+           bytes = http_read_buffered(http, (char *)http->sbuffer + http->stream.avail_in, (size_t)buflen);
           }
           else if (http->data_encoding == HTTP_ENCODING_CHUNKED)
-            bytes = http_read_chunk(http,
-                                   (char *)http->dbuffer +
-                                       http->stream.avail_in, buflen);
+            bytes = http_read_chunk(http, (char *)http->sbuffer + http->stream.avail_in, (size_t)buflen);
           else
             bytes = 0;
 
@@ -2086,7 +2014,7 @@ httpRead2(http_t *http,                   /* I - HTTP connection */
                         "decompression buffer.", CUPS_LLCAST bytes));
 
           http->data_remaining  -= bytes;
-          http->stream.avail_in += bytes;
+          http->stream.avail_in += (uInt)bytes;
 
          if (http->data_remaining <= 0 &&
              http->data_encoding == HTTP_ENCODING_CHUNKED)
@@ -2164,14 +2092,15 @@ httpRead2(http_t *http,                 /* I - HTTP connection */
 
   if (
 #ifdef HAVE_LIBZ
-      (http->coding == _HTTP_CODING_IDENTITY || http->stream.avail_in == 0) &&
+      (http->coding == _HTTP_CODING_IDENTITY ||
+       (http->coding >= _HTTP_CODING_GUNZIP && http->stream.avail_in == 0)) &&
 #endif /* HAVE_LIBZ */
       ((http->data_remaining <= 0 &&
         http->data_encoding == HTTP_ENCODING_LENGTH) ||
        (http->data_encoding == HTTP_ENCODING_CHUNKED && bytes == 0)))
   {
 #ifdef HAVE_LIBZ
-    if (http->coding)
+    if (http->coding >= _HTTP_CODING_GUNZIP)
       http_content_coding_finish(http);
 #endif /* HAVE_LIBZ */
 
@@ -2194,7 +2123,7 @@ httpRead2(http_t *http,                   /* I - HTTP connection */
 /*
  * 'httpReadRequest()' - Read a HTTP request from a connection.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 http_state_t                           /* O - New state of connection */
@@ -2212,8 +2141,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   * Range check input...
   */
 
-  DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT ")",
-                http, uri, CUPS_LLCAST urilen));
+  DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT ")", (void *)http, (void *)uri, CUPS_LLCAST urilen));
 
   if (uri)
     *uri = '\0';
@@ -2360,13 +2288,13 @@ httpReadRequest(http_t *http,           /* I - HTTP connection */
  * This function is deprecated. Please use the @link httpReconnect2@ function
  * instead.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
 int                                    /* O - 0 on success, non-zero on failure */
 httpReconnect(http_t *http)            /* I - HTTP connection */
 {
-  DEBUG_printf(("httpReconnect(http=%p)", http));
+  DEBUG_printf(("httpReconnect(http=%p)", (void *)http));
 
   return (httpReconnect2(http, 30000, NULL));
 }
@@ -2389,8 +2317,7 @@ httpReconnect2(http_t *http,              /* I - HTTP connection */
 #endif /* DEBUG */
 
 
-  DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", http, msec,
-                cancel));
+  DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel));
 
   if (!http)
   {
@@ -2402,7 +2329,7 @@ httpReconnect2(http_t *http,              /* I - HTTP connection */
   if (http->tls)
   {
     DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
-    http_tls_stop(http);
+    _httpTLSStop(http);
   }
 #endif /* HAVE_SSL */
 
@@ -2445,18 +2372,17 @@ httpReconnect2(http_t *http,            /* I - HTTP connection */
                   httpAddrPort(&(current->addr))));
 #endif /* DEBUG */
 
-  if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec,
-                               cancel)) == NULL)
+  if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, cancel)) == NULL)
   {
    /*
     * Unable to connect...
     */
 
-#ifdef WIN32
+#ifdef _WIN32
     http->error  = WSAGetLastError();
 #else
     http->error  = errno;
-#endif /* WIN32 */
+#endif /* _WIN32 */
     http->status = HTTP_STATUS_ERROR;
 
     DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
@@ -2480,7 +2406,7 @@ httpReconnect2(http_t *http,              /* I - HTTP connection */
     * Always do encryption via SSL.
     */
 
-    if (http_tls_start(http) != 0)
+    if (_httpTLSStart(http) != 0)
     {
       httpAddrClose(NULL, http->fd);
 
@@ -2503,11 +2429,12 @@ httpReconnect2(http_t *http,            /* I - HTTP connection */
  * 'httpSetAuthString()' - Set the current authorization string.
  *
  * This function just stores a copy of the current authorization string in
- * the HTTP connection object.  You must still call httpSetField() to set
- * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
- * httpHead(), httpOptions(), httpPost, or httpPut().
+ * the HTTP connection object.  You must still call @link httpSetField@ to set
+ * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
+ * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
+ * @link httpPut@.
  *
- * @since CUPS 1.3/OS X 10.5@
+ * @since CUPS 1.3/macOS 10.5@
  */
 
 void
@@ -2533,10 +2460,10 @@ httpSetAuthString(http_t     *http,     /* I - HTTP connection */
     * Set the current authorization string...
     */
 
-    int len = (int)strlen(scheme) + (data ? (int)strlen(data) + 1 : 0) + 1;
+    size_t len = strlen(scheme) + (data ? strlen(data) + 1 : 0) + 1;
     char *temp;
 
-    if (len > (int)sizeof(http->_authstring))
+    if (len > sizeof(http->_authstring))
     {
       if ((temp = malloc(len)) == NULL)
         len = sizeof(http->_authstring);
@@ -2564,7 +2491,7 @@ httpSetAuthString(http_t     *http,       /* I - HTTP connection */
  * 'httpSetCredentials()' - Set the credentials associated with an encrypted
  *                         connection.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 int                                            /* O - Status of call (0 = success) */
@@ -2574,9 +2501,11 @@ httpSetCredentials(http_t        *http,          /* I - HTTP connection */
   if (!http || cupsArrayCount(credentials) < 1)
     return (-1);
 
+#ifdef HAVE_SSL
   _httpFreeCredentials(http->tls_credentials);
 
   http->tls_credentials = _httpCreateCredentials(credentials);
+#endif /* HAVE_SSL */
 
   return (http->tls_credentials ? 0 : -1);
 }
@@ -2585,7 +2514,7 @@ httpSetCredentials(http_t *http,          /* I - HTTP connection */
 /*
  * 'httpSetCookie()' - Set the cookie value(s).
  *
- * @since CUPS 1.1.19/OS X 10.3@
+ * @since CUPS 1.1.19/macOS 10.3@
  */
 
 void
@@ -2611,7 +2540,7 @@ httpSetCookie(http_t     *http,           /* I - Connection */
  * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
  * and @code HTTP_FIELD_USER_AGENT@ can be set.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 void
@@ -2619,39 +2548,15 @@ httpSetDefaultField(http_t       *http, /* I - HTTP connection */
                     http_field_t field,        /* I - Field index */
                    const char   *value)/* I - Value */
 {
-  DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")",
-                http, field, http_fields[field], value));
+  DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, http_fields[field], value));
 
-  if (!http)
+  if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
     return;
 
-  switch (field)
-  {
-    case HTTP_FIELD_ACCEPT_ENCODING :
-        if (http->default_accept_encoding)
-          _cupsStrFree(http->default_accept_encoding);
-
-        http->default_accept_encoding = value ? _cupsStrAlloc(value) : NULL;
-        break;
+  if (http->default_fields[field])
+    free(http->default_fields[field]);
 
-    case HTTP_FIELD_SERVER :
-        if (http->default_server)
-          _cupsStrFree(http->default_server);
-
-        http->default_server = value ? _cupsStrAlloc(value) : NULL;
-        break;
-
-    case HTTP_FIELD_USER_AGENT :
-        if (http->default_user_agent)
-          _cupsStrFree(http->default_user_agent);
-
-        http->default_user_agent = value ? _cupsStrAlloc(value) : NULL;
-        break;
-
-    default :
-        DEBUG_puts("1httpSetDefaultField: Ignored.");
-       break;
-  }
+  http->default_fields[field] = value ? strdup(value) : NULL;
 }
 
 
@@ -2661,7 +2566,7 @@ httpSetDefaultField(http_t       *http,   /* I - HTTP connection */
  * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
  * argument.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 void
@@ -2669,7 +2574,7 @@ httpSetExpect(http_t        *http,        /* I - HTTP connection */
               http_status_t expect)    /* I - HTTP status to expect
                                               (@code HTTP_STATUS_CONTINUE@) */
 {
-  DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", http, expect));
+  DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http, expect));
 
   if (http)
     http->expect = expect;
@@ -2685,108 +2590,19 @@ httpSetField(http_t       *http,       /* I - HTTP connection */
              http_field_t field,       /* I - Field index */
             const char   *value)       /* I - Value */
 {
-  DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", http,
-                field, http_fields[field], value));
+  DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, http_fields[field], value));
 
-  if (http == NULL ||
-      field < HTTP_FIELD_ACCEPT_LANGUAGE ||
-      field >= HTTP_FIELD_MAX ||
-      value == NULL)
+  if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX || !value)
     return;
 
-  switch (field)
-  {
-    case HTTP_FIELD_ACCEPT_ENCODING :
-        if (http->accept_encoding)
-          _cupsStrFree(http->accept_encoding);
-
-        http->accept_encoding = _cupsStrAlloc(value);
-        break;
-
-    case HTTP_FIELD_ALLOW :
-        if (http->allow)
-          _cupsStrFree(http->allow);
-
-        http->allow = _cupsStrAlloc(value);
-        break;
-
-    case HTTP_FIELD_SERVER :
-        if (http->server)
-          _cupsStrFree(http->server);
-
-        http->server = _cupsStrAlloc(value);
-        break;
-
-    default :
-       strlcpy(http->fields[field], value, HTTP_MAX_VALUE);
-       break;
-  }
-
-  if (field == HTTP_FIELD_AUTHORIZATION)
-  {
-   /*
-    * Special case for Authorization: as its contents can be
-    * longer than HTTP_MAX_VALUE
-    */
-
-    if (http->field_authorization)
-      free(http->field_authorization);
-
-    http->field_authorization = strdup(value);
-  }
-  else if (field == HTTP_FIELD_HOST)
-  {
-   /*
-    * Special-case for Host: as we don't want a trailing "." on the hostname and
-    * need to bracket IPv6 numeric addresses.
-    */
-
-    char *ptr = strchr(value, ':');
-
-    if (value[0] != '[' && ptr && strchr(ptr + 1, ':'))
-    {
-     /*
-      * Bracket IPv6 numeric addresses...
-      *
-      * This is slightly inefficient (basically copying twice), but is an edge
-      * case and not worth optimizing...
-      */
-
-      snprintf(http->fields[HTTP_FIELD_HOST],
-               sizeof(http->fields[HTTP_FIELD_HOST]), "[%s]", value);
-    }
-    else
-    {
-     /*
-      * Check for a trailing dot on the hostname...
-      */
-
-      ptr = http->fields[HTTP_FIELD_HOST];
-
-      if (*ptr)
-      {
-       ptr += strlen(ptr) - 1;
-
-       if (*ptr == '.')
-         *ptr = '\0';
-      }
-    }
-  }
-#ifdef HAVE_LIBZ
-  else if (field == HTTP_FIELD_CONTENT_ENCODING &&
-           http->data_encoding != HTTP_ENCODING_FIELDS)
-  {
-    DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
-    http_content_coding_start(http, value);
-  }
-#endif /* HAVE_LIBZ */
+  http_add_field(http, field, value, 0);
 }
 
 
 /*
  * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 void
@@ -2802,30 +2618,31 @@ httpSetKeepAlive(
 /*
  * 'httpSetLength()' - Set the content-length and content-encoding.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 void
 httpSetLength(http_t *http,            /* I - HTTP connection */
               size_t length)           /* I - Length (0 for chunked) */
 {
-  DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT ")", http,
-                CUPS_LLCAST length));
+  DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT ")", (void *)http, CUPS_LLCAST length));
 
   if (!http)
     return;
 
   if (!length)
   {
-    strlcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked",
-            HTTP_MAX_VALUE);
-    http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0';
+    httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked");
+    httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "");
   }
   else
   {
-    http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0';
-    snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE,
-             CUPS_LLFMT, CUPS_LLCAST length);
+    char len[32];                      /* Length string */
+
+
+    snprintf(len, sizeof(len), CUPS_LLFMT, CUPS_LLCAST length);
+    httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "");
+    httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, len);
   }
 }
 
@@ -2836,7 +2653,7 @@ httpSetLength(http_t *http,               /* I - HTTP connection */
  * The optional timeout callback receives both the HTTP connection and a user
  * data pointer and must return 1 to continue or 0 to error (time) out.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 void
@@ -2844,7 +2661,7 @@ httpSetTimeout(
     http_t            *http,           /* I - HTTP connection */
     double            timeout,         /* I - Number of seconds for timeout,
                                                must be greater than 0 */
-    http_timeout_cb_t cb,              /* I - Callback function or NULL */
+    http_timeout_cb_t cb,              /* I - Callback function or @code NULL@ */
     void              *user_data)      /* I - User data pointer */
 {
   if (!http || timeout <= 0.0)
@@ -2864,7 +2681,7 @@ httpSetTimeout(
 /*
  * 'httpShutdown()' - Shutdown one side of an HTTP connection.
  *
- * @since CUPS 2.0@
+ * @since CUPS 2.0/OS 10.10@
  */
 
 void
@@ -2873,15 +2690,23 @@ httpShutdown(http_t *http)              /* I - HTTP connection */
   if (!http || http->fd < 0)
     return;
 
+#ifdef HAVE_SSL
   if (http->tls)
-    http_tls_stop(http);
+    _httpTLSStop(http);
+#endif /* HAVE_SSL */
 
+#ifdef _WIN32
+  shutdown(http->fd, SD_RECEIVE);      /* Microsoft-ism... */
+#else
   shutdown(http->fd, SHUT_RD);
+#endif /* _WIN32 */
 }
 
 
 /*
  * 'httpTrace()' - Send an TRACE request to the server.
+ *
+ * @exclude all@
  */
 
 int                                    /* O - Status of call (0 = success) */
@@ -2909,8 +2734,7 @@ _httpUpdate(http_t        *http,  /* I - HTTP connection */
   int          major, minor;           /* HTTP version numbers */
 
 
-  DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http, status,
-                httpStateString(http->state)));
+  DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpStateString(http->state)));
 
  /*
   * Grab a single line from the connection...
@@ -2947,7 +2771,7 @@ _httpUpdate(http_t        *http,  /* I - HTTP connection */
 #ifdef HAVE_SSL
     if (http->status == HTTP_STATUS_SWITCHING_PROTOCOLS && !http->tls)
     {
-      if (http_tls_start(http) != 0)
+      if (_httpTLSStart(http) != 0)
       {
         httpAddrClose(NULL, http->fd);
 
@@ -2999,7 +2823,7 @@ _httpUpdate(http_t        *http,  /* I - HTTP connection */
     *status = http->status;
     return (0);
   }
-  else if (!strncmp(line, "HTTP/", 5))
+  else if (!strncmp(line, "HTTP/", 5) && http->mode == _HTTP_MODE_CLIENT)
   {
    /*
     * Got the beginning of a response...
@@ -3051,7 +2875,12 @@ _httpUpdate(http_t        *http, /* I - HTTP connection */
       httpSetCookie(http, value);
     }
     else if ((field = httpFieldValue(line)) != HTTP_FIELD_UNKNOWN)
-      httpSetField(http, field, value);
+    {
+      http_add_field(http, field, value, 1);
+
+      if (field == HTTP_FIELD_AUTHENTICATION_INFO)
+        httpGetSubField2(http, HTTP_FIELD_AUTHENTICATION_INFO, "nextnonce", http->nextnonce, (int)sizeof(http->nextnonce));
+    }
 #ifdef DEBUG
     else
       DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line));
@@ -3079,8 +2908,7 @@ httpUpdate(http_t *http)          /* I - HTTP connection */
   http_status_t        status;                 /* Request status */
 
 
-  DEBUG_printf(("httpUpdate(http=%p), state=%s", http,
-                httpStateString(http->state)));
+  DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http, httpStateString(http->state)));
 
  /*
   * Flush pending data, if any...
@@ -3151,7 +2979,7 @@ _httpWait(http_t *http,                   /* I - HTTP connection */
   int                  nfds;           /* Result from select()/poll() */
 
 
-  DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", http, msec, usessl));
+  DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http, msec, usessl));
 
   if (http->fd < 0)
   {
@@ -3164,7 +2992,7 @@ _httpWait(http_t *http,                   /* I - HTTP connection */
   */
 
 #ifdef HAVE_SSL
-  if (http_tls_pending(http))
+  if (http->tls && _httpTLSPending(http))
   {
     DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
     return (1);
@@ -3205,12 +3033,12 @@ _httpWait(http_t *http,                 /* I - HTTP connection */
 
     DEBUG_printf(("6_httpWait: select() returned %d...", nfds));
   }
-#  ifdef WIN32
+#  ifdef _WIN32
   while (nfds < 0 && (WSAGetLastError() == WSAEINTR ||
                       WSAGetLastError() == WSAEWOULDBLOCK));
 #  else
   while (nfds < 0 && (errno == EINTR || errno == EAGAIN));
-#  endif /* WIN32 */
+#  endif /* _WIN32 */
 #endif /* HAVE_POLL */
 
   DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds,
@@ -3223,7 +3051,7 @@ _httpWait(http_t *http,                   /* I - HTTP connection */
 /*
  * 'httpWait()' - Wait for data available on a connection.
  *
- * @since CUPS 1.1.19/OS X 10.3@
+ * @since CUPS 1.1.19/macOS 10.3@
  */
 
 int                                    /* O - 1 if data is available, 0 otherwise */
@@ -3234,7 +3062,7 @@ httpWait(http_t *http,                    /* I - HTTP connection */
   * First see if there is data in the buffer...
   */
 
-  DEBUG_printf(("2httpWait(http=%p, msec=%d)", http, msec));
+  DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http, msec));
 
   if (http == NULL)
     return (0);
@@ -3279,7 +3107,7 @@ httpWait(http_t *http,                    /* I - HTTP connection */
  * This function is deprecated. Use the httpWrite2() function which can
  * write more than 2GB of data.
  *
- * @deprecated@
+ * @deprecated@ @exclude all@
  */
 
 int                                    /* O - Number of bytes written */
@@ -3287,14 +3115,14 @@ httpWrite(http_t     *http,             /* I - HTTP connection */
           const char *buffer,          /* I - Buffer for data */
          int        length)            /* I - Number of bytes to write */
 {
-  return ((int)httpWrite2(http, buffer, length));
+  return ((int)httpWrite2(http, buffer, (size_t)length));
 }
 
 
 /*
  * 'httpWrite2()' - Write data to a HTTP connection.
  *
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 ssize_t                                        /* O - Number of bytes written */
@@ -3305,8 +3133,7 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
   ssize_t      bytes;                  /* Bytes written */
 
 
-  DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", http,
-                buffer, CUPS_LLCAST length));
+  DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
  /*
   * Range check input...
@@ -3329,7 +3156,7 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
   */
 
 #ifdef HAVE_LIBZ
-  if (http->coding)
+  if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE)
   {
     DEBUG_printf(("1httpWrite2: http->coding=%d", http->coding));
 
@@ -3340,37 +3167,48 @@ httpWrite2(http_t     *http,            /* I - HTTP connection */
     }
     else
     {
+      size_t   slen;                   /* Bytes to write */
+      ssize_t  sret;                   /* Bytes written */
+
       http->stream.next_in   = (Bytef *)buffer;
-      http->stream.avail_in  = length;
-      http->stream.next_out  = (Bytef *)http->wbuffer + http->wused;
-      http->stream.avail_out = sizeof(http->wbuffer) - http->wused;
+      http->stream.avail_in  = (uInt)length;
 
       while (deflate(&(http->stream), Z_NO_FLUSH) == Z_OK)
       {
-       http->wused = sizeof(http->wbuffer) - http->stream.avail_out;
+        DEBUG_printf(("1httpWrite2: avail_out=%d", http->stream.avail_out));
 
-        if (http->stream.avail_out == 0)
-        {
-         if (httpFlushWrite(http) < 0)
-         {
-           DEBUG_puts("1httpWrite2: Unable to flush, returning -1.");
-           return (-1);
-         }
+        if (http->stream.avail_out > 0)
+         continue;
+
+       slen = _HTTP_MAX_SBUFFER - http->stream.avail_out;
+
+        DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen));
+
+       if (slen > 0 && http->data_encoding == HTTP_ENCODING_CHUNKED)
+         sret = http_write_chunk(http, (char *)http->sbuffer, slen);
+       else if (slen > 0)
+         sret = http_write(http, (char *)http->sbuffer, slen);
+       else
+         sret = 0;
 
-         http->stream.next_out  = (Bytef *)http->wbuffer;
-         http->stream.avail_out = sizeof(http->wbuffer);
+        if (sret < 0)
+       {
+         DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
+         return (-1);
        }
+
+       http->stream.next_out  = (Bytef *)http->sbuffer;
+       http->stream.avail_out = (uInt)_HTTP_MAX_SBUFFER;
       }
 
-      http->wused = sizeof(http->wbuffer) - http->stream.avail_out;
-      bytes       = length;
+      bytes = (ssize_t)length;
     }
   }
   else
 #endif /* HAVE_LIBZ */
   if (length > 0)
   {
-    if (http->wused && (length + http->wused) > sizeof(http->wbuffer))
+    if (http->wused && (length + (size_t)http->wused) > sizeof(http->wbuffer))
     {
       DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
                     CUPS_LLFMT ")", http->wused, CUPS_LLCAST length));
@@ -3378,8 +3216,7 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
       httpFlushWrite(http);
     }
 
-    if ((length + http->wused) <= sizeof(http->wbuffer) &&
-        length < sizeof(http->wbuffer))
+    if ((length + (size_t)http->wused) <= sizeof(http->wbuffer) && length < sizeof(http->wbuffer))
     {
      /*
       * Write to buffer...
@@ -3402,9 +3239,9 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
                     CUPS_LLCAST length));
 
       if (http->data_encoding == HTTP_ENCODING_CHUNKED)
-       bytes = (ssize_t)http_write_chunk(http, buffer, (int)length);
+       bytes = (ssize_t)http_write_chunk(http, buffer, length);
       else
-       bytes = (ssize_t)http_write(http, buffer, (int)length);
+       bytes = (ssize_t)http_write(http, buffer, length);
 
       DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT " bytes...",
                     CUPS_LLCAST bytes));
@@ -3429,7 +3266,7 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
     */
 
 #ifdef HAVE_LIBZ
-    if (http->coding)
+    if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE)
       http_content_coding_finish(http);
 #endif /* HAVE_LIBZ */
 
@@ -3457,7 +3294,8 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
 
     if (http->state == HTTP_STATE_POST_RECV)
       http->state ++;
-    else if (http->state == HTTP_STATE_POST_SEND)
+    else if (http->state == HTTP_STATE_POST_SEND ||
+             http->state == HTTP_STATE_GET_SEND)
       http->state = HTTP_STATE_WAITING;
     else
       http->state = HTTP_STATE_STATUS;
@@ -3475,7 +3313,7 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
 /*
  * 'httpWriteResponse()' - Write a HTTP response to a client connection.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -3490,7 +3328,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
   * Range check input...
   */
 
-  DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", http, status));
+  DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http, status));
 
   if (!http || status < HTTP_STATUS_CONTINUE)
   {
@@ -3502,7 +3340,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
   * Set the various standard fields if they aren't already...
   */
 
-  if (!http->fields[HTTP_FIELD_DATE][0])
+  if (!http->fields[HTTP_FIELD_DATE])
     httpSetField(http, HTTP_FIELD_DATE, httpGetDateString(time(NULL)));
 
   if (status >= HTTP_STATUS_BAD_REQUEST && http->keep_alive)
@@ -3513,7 +3351,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
 
   if (http->version == HTTP_VERSION_1_1)
   {
-    if (!http->fields[HTTP_FIELD_CONNECTION][0])
+    if (!http->fields[HTTP_FIELD_CONNECTION])
     {
       if (http->keep_alive)
        httpSetField(http, HTTP_FIELD_CONNECTION, "Keep-Alive");
@@ -3521,7 +3359,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
        httpSetField(http, HTTP_FIELD_CONNECTION, "close");
     }
 
-    if (http->keep_alive && !http->fields[HTTP_FIELD_KEEP_ALIVE][0])
+    if (http->keep_alive && !http->fields[HTTP_FIELD_KEEP_ALIVE])
       httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "timeout=10");
   }
 
@@ -3529,28 +3367,26 @@ httpWriteResponse(http_t        *http,  /* I - HTTP connection */
   if (status == HTTP_STATUS_UPGRADE_REQUIRED ||
       status == HTTP_STATUS_SWITCHING_PROTOCOLS)
   {
-    if (!http->fields[HTTP_FIELD_CONNECTION][0])
+    if (!http->fields[HTTP_FIELD_CONNECTION])
       httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade");
 
-    if (!http->fields[HTTP_FIELD_UPGRADE][0])
+    if (!http->fields[HTTP_FIELD_UPGRADE])
       httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0");
 
-    if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
+    if (!http->fields[HTTP_FIELD_CONTENT_LENGTH])
       httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "0");
   }
 #endif /* HAVE_SSL */
 
-  if (!http->server)
-    httpSetField(http, HTTP_FIELD_SERVER,
-                 http->default_server ? http->default_server : CUPS_MINIMAL);
+  if (!http->fields[HTTP_FIELD_SERVER])
+    httpSetField(http, HTTP_FIELD_SERVER, http->default_fields[HTTP_FIELD_SERVER] ? http->default_fields[HTTP_FIELD_SERVER] : CUPS_MINIMAL);
 
  /*
   * Set the Accept-Encoding field if it isn't already...
   */
 
-  if (!http->accept_encoding)
-    httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING,
-                 http->default_accept_encoding ? http->default_accept_encoding :
+  if (!http->fields[HTTP_FIELD_ACCEPT_ENCODING])
+    httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, http->default_fields[HTTP_FIELD_ACCEPT_ENCODING] ? http->default_fields[HTTP_FIELD_ACCEPT_ENCODING] :
 #ifdef HAVE_LIBZ
                                                  "gzip, deflate, identity");
 #else
@@ -3565,8 +3401,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
   old_remaining       = http->data_remaining;
   http->data_encoding = HTTP_ENCODING_FIELDS;
 
-  if (httpPrintf(http, "HTTP/%d.%d %d %s\r\n", http->version / 100,
-                 http->version % 100, (int)status, httpStatus(status)) < 0)
+  if (httpPrintf(http, "HTTP/%d.%d %d %s\r\n", http->version / 100, http->version % 100, (int)status, httpStatus(status)) < 0)
   {
     http->status = HTTP_STATUS_ERROR;
     return (-1);
@@ -3595,13 +3430,31 @@ httpWriteResponse(http_t        *http,  /* I - HTTP connection */
 
     if (http->cookie)
     {
-      if (httpPrintf(http, "Set-Cookie: %s path=/ httponly%s\r\n",
-                    http->cookie, http->tls ? " secure" : "") < 1)
+      if (strchr(http->cookie, ';'))
+      {
+        if (httpPrintf(http, "Set-Cookie: %s\r\n", http->cookie) < 1)
+       {
+         http->status = HTTP_STATUS_ERROR;
+         return (-1);
+       }
+      }
+      else if (httpPrintf(http, "Set-Cookie: %s; path=/; httponly;%s\r\n", http->cookie, http->tls ? " secure;" : "") < 1)
       {
        http->status = HTTP_STATUS_ERROR;
        return (-1);
       }
     }
+
+   /*
+    * "Click-jacking" defense (STR #4492)...
+    */
+
+    if (httpPrintf(http, "X-Frame-Options: DENY\r\n"
+                         "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
+    {
+      http->status = HTTP_STATUS_ERROR;
+      return (-1);
+    }
   }
 
   if (httpWrite2(http, "\r\n", 2) < 2)
@@ -3651,6 +3504,17 @@ httpWriteResponse(http_t        *http,   /* I - HTTP connection */
 
     http_set_length(http);
 
+    if (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0)
+    {
+      DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
+                    "was %s.", httpStateString(http->state)));
+      http->state = HTTP_STATE_WAITING;
+      return (0);
+    }
+
+    if (http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_GET)
+      http->state ++;
+
 #ifdef HAVE_LIBZ
    /*
     * Then start any content encoding...
@@ -3660,12 +3524,148 @@ httpWriteResponse(http_t        *http, /* I - HTTP connection */
     http_content_coding_start(http,
                              httpGetField(http, HTTP_FIELD_CONTENT_ENCODING));
 #endif /* HAVE_LIBZ */
+
   }
 
   return (0);
 }
 
 
+/*
+ * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
+ */
+
+static void
+http_add_field(http_t       *http,     /* I - HTTP connection */
+               http_field_t field,     /* I - HTTP field */
+               const char   *value,    /* I - Value string */
+               int          append)    /* I - Append value? */
+{
+  char         temp[1024];             /* Temporary value string */
+  size_t       fieldlen,               /* Length of existing value */
+               valuelen,               /* Length of value string */
+               total;                  /* Total length of string */
+
+
+  if (field == HTTP_FIELD_HOST)
+  {
+   /*
+    * Special-case for Host: as we don't want a trailing "." on the hostname and
+    * need to bracket IPv6 numeric addresses.
+    */
+
+    char *ptr = strchr(value, ':');
+
+    if (value[0] != '[' && ptr && strchr(ptr + 1, ':'))
+    {
+     /*
+      * Bracket IPv6 numeric addresses...
+      *
+      * This is slightly inefficient (basically copying twice), but is an edge
+      * case and not worth optimizing...
+      */
+
+      snprintf(temp, sizeof(temp), "[%s]", value);
+      value = temp;
+    }
+    else if (*value)
+    {
+     /*
+      * Check for a trailing dot on the hostname...
+      */
+
+      strlcpy(temp, value, sizeof(temp));
+      value = temp;
+      ptr   = temp + strlen(temp) - 1;
+
+      if (*ptr == '.')
+       *ptr = '\0';
+    }
+  }
+
+  if (append && field != HTTP_FIELD_ACCEPT_ENCODING && field != HTTP_FIELD_ACCEPT_LANGUAGE && field != HTTP_FIELD_ACCEPT_RANGES && field != HTTP_FIELD_ALLOW && field != HTTP_FIELD_LINK && field != HTTP_FIELD_TRANSFER_ENCODING && field != HTTP_FIELD_UPGRADE && field != HTTP_FIELD_WWW_AUTHENTICATE)
+    append = 0;
+
+  if (!append && http->fields[field])
+  {
+    if (http->fields[field] != http->_fields[field])
+      free(http->fields[field]);
+
+    http->fields[field] = NULL;
+  }
+
+  valuelen = strlen(value);
+
+  if (!valuelen)
+  {
+    http->_fields[field][0] = '\0';
+    return;
+  }
+
+  if (http->fields[field])
+  {
+    fieldlen = strlen(http->fields[field]);
+    total    = fieldlen + 2 + valuelen;
+  }
+  else
+  {
+    fieldlen = 0;
+    total    = valuelen;
+  }
+
+  if (total < HTTP_MAX_VALUE && field < HTTP_FIELD_ACCEPT_ENCODING)
+  {
+   /*
+    * Copy short values to legacy char arrays (maintained for binary
+    * compatibility with CUPS 1.2.x and earlier applications...)
+    */
+
+    if (fieldlen)
+    {
+      char     combined[HTTP_MAX_VALUE];
+                                       /* Combined value string */
+
+      snprintf(combined, sizeof(combined), "%s, %s", http->_fields[field], value);
+      value = combined;
+    }
+
+    strlcpy(http->_fields[field], value, sizeof(http->_fields[field]));
+    http->fields[field] = http->_fields[field];
+  }
+  else if (fieldlen)
+  {
+   /*
+    * Expand the field value...
+    */
+
+    char       *combined;              /* New value string */
+
+    if ((combined = realloc(http->fields[field], total + 1)) != NULL)
+    {
+      http->fields[field] = combined;
+      strlcat(combined, ", ", total + 1);
+      strlcat(combined, value, total + 1);
+    }
+  }
+  else
+  {
+   /*
+    * Allocate the field value...
+    */
+
+    http->fields[field] = strdup(value);
+  }
+
+#ifdef HAVE_LIBZ
+  if (field == HTTP_FIELD_CONTENT_ENCODING && http->data_encoding != HTTP_ENCODING_FIELDS)
+  {
+    DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
+    http_content_coding_start(http, value);
+  }
+#endif /* HAVE_LIBZ */
+}
+
+
 #ifdef HAVE_LIBZ
 /*
  * 'http_content_coding_finish()' - Finish doing any content encoding.
@@ -3675,28 +3675,46 @@ static void
 http_content_coding_finish(
     http_t *http)                      /* I - HTTP connection */
 {
-  int  zerr;                           /* Compression status */
+  int          zerr;                   /* Compression status */
+  Byte         dummy[1];               /* Dummy read buffer */
+  size_t       bytes;                  /* Number of bytes to write */
+
 
+  DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http));
+  DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http->coding));
 
   switch (http->coding)
   {
     case _HTTP_CODING_DEFLATE :
     case _HTTP_CODING_GZIP :
+        http->stream.next_in  = dummy;
+        http->stream.avail_in = 0;
+
         do
         {
-          http->stream.next_out  = (Bytef *)http->wbuffer + http->wused;
-          http->stream.avail_out = sizeof(http->wbuffer) - http->wused;
+          zerr  = deflate(&(http->stream), Z_FINISH);
+         bytes = _HTTP_MAX_SBUFFER - http->stream.avail_out;
 
-          zerr = deflate(&(http->stream), Z_FINISH);
+          if (bytes > 0)
+         {
+           DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes));
 
-          http->wused = sizeof(http->wbuffer) - http->stream.avail_out;
-          if (http->wused == sizeof(http->wbuffer))
-            httpFlushWrite(http);
-        }
+           if (http->data_encoding == HTTP_ENCODING_CHUNKED)
+             http_write_chunk(http, (char *)http->sbuffer, bytes);
+           else
+             http_write(http, (char *)http->sbuffer, bytes);
+          }
+
+          http->stream.next_out  = (Bytef *)http->sbuffer;
+          http->stream.avail_out = (uInt)_HTTP_MAX_SBUFFER;
+       }
         while (zerr == Z_OK);
 
         deflateEnd(&(http->stream));
 
+        free(http->sbuffer);
+        http->sbuffer = NULL;
+
         if (http->wused)
           httpFlushWrite(http);
         break;
@@ -3704,8 +3722,8 @@ http_content_coding_finish(
     case _HTTP_CODING_INFLATE :
     case _HTTP_CODING_GUNZIP :
         inflateEnd(&(http->stream));
-        free(http->dbuffer);
-        http->dbuffer = NULL;
+        free(http->sbuffer);
+        http->sbuffer = NULL;
         break;
 
     default :
@@ -3729,8 +3747,7 @@ http_content_coding_start(
   _http_coding_t       coding;         /* Content coding value */
 
 
-  DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", http,
-                value));
+  DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http, value));
 
   if (http->coding != _HTTP_CODING_IDENTITY)
   {
@@ -3785,6 +3802,13 @@ http_content_coding_start(
         if (http->wused)
           httpFlushWrite(http);
 
+        if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL)
+        {
+          http->status = HTTP_STATUS_ERROR;
+          http->error  = errno;
+          return;
+        }
+
        /*
         * Window size for compression is 11 bits - optimal based on PWG Raster
         * sample files on pwg.org.  -11 is raw deflate, 27 is gzip, per ZLIB
@@ -3800,11 +3824,14 @@ http_content_coding_start(
           http->error  = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL;
           return;
         }
+
+       http->stream.next_out  = (Bytef *)http->sbuffer;
+       http->stream.avail_out = (uInt)_HTTP_MAX_SBUFFER;
         break;
 
     case _HTTP_CODING_INFLATE :
     case _HTTP_CODING_GUNZIP :
-        if ((http->dbuffer = malloc(HTTP_MAX_BUFFER)) == NULL)
+        if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL)
         {
           http->status = HTTP_STATUS_ERROR;
           http->error  = errno;
@@ -3820,15 +3847,15 @@ http_content_coding_start(
                                  coding == _HTTP_CODING_INFLATE ? -15 : 31))
                < Z_OK)
         {
-          free(http->dbuffer);
-          http->dbuffer = NULL;
+          free(http->sbuffer);
+          http->sbuffer = NULL;
           http->status  = HTTP_STATUS_ERROR;
           http->error   = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL;
           return;
         }
 
         http->stream.avail_in = 0;
-        http->stream.next_in  = http->dbuffer;
+        http->stream.next_in  = http->sbuffer;
         break;
 
     default :
@@ -3851,7 +3878,7 @@ static http_t *                           /* O - HTTP connection */
 http_create(
     const char        *host,           /* I - Hostname */
     int               port,            /* I - Port number */
-    http_addrlist_t   *addrlist,       /* I - Address list or NULL */
+    http_addrlist_t   *addrlist,       /* I - Address list or @code NULL@ */
     int               family,          /* I - Address family or AF_UNSPEC */
     http_encryption_t encryption,      /* I - Encryption to use */
     int               blocking,                /* I - 1 for blocking mode */
@@ -3862,9 +3889,7 @@ http_create(
   http_addrlist_t *myaddrlist = NULL;  /* My address list */
 
 
-  DEBUG_printf(("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, "
-                "encryption=%d, blocking=%d, mode=%d)", host, port, addrlist,
-                family, encryption, blocking, mode));
+  DEBUG_printf(("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%d, mode=%d)", host, port, (void *)addrlist, family, encryption, blocking, mode));
 
   if (!host && mode == _HTTP_MODE_CLIENT)
     return (NULL);
@@ -3896,7 +3921,7 @@ http_create(
   if ((http = calloc(sizeof(http_t), 1)) == NULL)
   {
     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
-    httpAddrFreeList(addrlist);
+    httpAddrFreeList(myaddrlist);
     return (NULL);
   }
 
@@ -3933,12 +3958,6 @@ http_create(
   return (http);
 }
 
-/* For OS X 10.8 and earlier */
-http_t *_httpCreate(const char *host, int port, http_addrlist_t *addrlist,
-                   http_encryption_t encryption, int family)
-{ return (http_create(host, port, addrlist, family, encryption, 1,
-                      _HTTP_MODE_CLIENT)); }
-
 
 #ifdef DEBUG
 /*
@@ -3968,7 +3987,7 @@ http_debug_hex(const char *prefix,        /* I - Prefix for line */
   for (i = 0; i < bytes; i += 16)
   {
     for (j = 0, ptr = start; j < 16 && (i + j) < bytes; j ++, ptr += 2)
-      sprintf(ptr, "%02X", buffer[i + j] & 255);
+      snprintf(ptr, 3, "%02X", buffer[i + j] & 255);
 
     while (j < 16)
     {
@@ -3987,7 +4006,7 @@ http_debug_hex(const char *prefix,        /* I - Prefix for line */
       if (ch < ' ' || ch >= 127)
        ch = '.';
 
-      *ptr++ = ch;
+      *ptr++ = (char)ch;
     }
 
     *ptr = '\0';
@@ -4012,10 +4031,9 @@ http_read(http_t *http,                  /* I - HTTP connection */
   ssize_t      bytes;                  /* Bytes read */
 
 
-  DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", http,
-                buffer, CUPS_LLCAST length));
+  DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
-  if (!http->blocking)
+  if (!http->blocking || http->timeout_value > 0.0)
   {
     while (!httpWait(http, http->wait_value))
     {
@@ -4033,14 +4051,14 @@ http_read(http_t *http,                 /* I - HTTP connection */
   {
 #ifdef HAVE_SSL
     if (http->tls)
-      bytes = http_tls_read(http, buffer, length);
+      bytes = _httpTLSRead(http, buffer, (int)length);
     else
 #endif /* HAVE_SSL */
     bytes = recv(http->fd, buffer, length, 0);
 
     if (bytes < 0)
     {
-#ifdef WIN32
+#ifdef _WIN32
       if (WSAGetLastError() != WSAEINTR)
       {
        http->error = WSAGetLastError();
@@ -4076,7 +4094,7 @@ http_read(http_t *http,                   /* I - HTTP connection */
        http->error = errno;
        return (-1);
       }
-#endif /* WIN32 */
+#endif /* _WIN32 */
     }
   }
   while (bytes < 0);
@@ -4090,7 +4108,7 @@ http_read(http_t *http,                   /* I - HTTP connection */
 
   if (bytes < 0)
   {
-#ifdef WIN32
+#ifdef _WIN32
     if (WSAGetLastError() == WSAEINTR)
       bytes = 0;
     else
@@ -4100,7 +4118,7 @@ http_read(http_t *http,                   /* I - HTTP connection */
       bytes = 0;
     else
       http->error = errno;
-#endif /* WIN32 */
+#endif /* _WIN32 */
   }
   else if (bytes == 0)
   {
@@ -4126,25 +4144,23 @@ http_read_buffered(http_t *http,        /* I - HTTP connection */
   ssize_t      bytes;                  /* Bytes read */
 
 
-  DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
-                ") used=%d",
-                http, buffer, CUPS_LLCAST length, http->used));
+  DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT ") used=%d", (void *)http, (void *)buffer, CUPS_LLCAST length, http->used));
 
   if (http->used > 0)
   {
     if (length > (size_t)http->used)
-      bytes = (size_t)http->used;
+      bytes = (ssize_t)http->used;
     else
-      bytes = length;
+      bytes = (ssize_t)length;
 
     DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
                   (int)bytes));
 
-    memcpy(buffer, http->buffer, bytes);
+    memcpy(buffer, http->buffer, (size_t)bytes);
     http->used -= (int)bytes;
 
     if (http->used > 0)
-      memmove(http->buffer, http->buffer + bytes, http->used);
+      memmove(http->buffer, http->buffer + bytes, (size_t)http->used);
   }
   else
     bytes = http_read(http, buffer, length);
@@ -4165,8 +4181,7 @@ http_read_chunk(http_t *http,             /* I - HTTP connection */
                char   *buffer,         /* I - Buffer */
                size_t length)          /* I - Maximum bytes to read */
 {
-  DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")",
-                http, buffer, CUPS_LLCAST length));
+  DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
   if (http->data_remaining <= 0)
   {
@@ -4254,8 +4269,7 @@ http_send(http_t       *http,             /* I - HTTP connection */
                };
 
 
-  DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")",
-                http, codes[request], uri));
+  DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http, codes[request], uri));
 
   if (http == NULL || uri == NULL)
     return (-1);
@@ -4264,10 +4278,10 @@ http_send(http_t       *http,           /* I - HTTP connection */
   * Set the User-Agent field if it isn't already...
   */
 
-  if (!http->fields[HTTP_FIELD_USER_AGENT][0])
+  if (!http->fields[HTTP_FIELD_USER_AGENT])
   {
-    if (http->default_user_agent)
-      httpSetField(http, HTTP_FIELD_USER_AGENT, http->default_user_agent);
+    if (http->default_fields[HTTP_FIELD_USER_AGENT])
+      httpSetField(http, HTTP_FIELD_USER_AGENT, http->default_fields[HTTP_FIELD_USER_AGENT]);
     else
       httpSetField(http, HTTP_FIELD_USER_AGENT, cupsUserAgent());
   }
@@ -4276,9 +4290,8 @@ http_send(http_t       *http,             /* I - HTTP connection */
   * Set the Accept-Encoding field if it isn't already...
   */
 
-  if (!http->accept_encoding && http->default_accept_encoding)
-    httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING,
-                 http->default_accept_encoding);
+  if (!http->fields[HTTP_FIELD_ACCEPT_ENCODING] && http->default_fields[HTTP_FIELD_ACCEPT_ENCODING])
+    httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, http->default_fields[HTTP_FIELD_ACCEPT_ENCODING]);
 
  /*
   * Encode the URI as needed...
@@ -4393,7 +4406,7 @@ http_send(http_t       *http,             /* I - HTTP connection */
   * The Kerberos and AuthRef authentication strings can only be used once...
   */
 
-  if (http->field_authorization && http->authstring &&
+  if (http->fields[HTTP_FIELD_AUTHORIZATION] && http->authstring &&
       (!strncmp(http->authstring, "Negotiate", 9) ||
        !strncmp(http->authstring, "AuthRef", 7)))
   {
@@ -4419,8 +4432,7 @@ http_set_length(http_t *http)             /* I - Connection */
   off_t        remaining;                      /* Remainder */
 
 
-  DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", http, http->mode,
-                httpStateString(http->state)));
+  DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http, http->mode, httpStateString(http->state)));
 
   if ((remaining = httpGetLength2(http)) >= 0)
   {
@@ -4434,8 +4446,7 @@ http_set_length(http_t *http)             /* I - Connection */
       return (remaining);
     }
 
-    if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING],
-                          "chunked"))
+    if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_TRANSFER_ENCODING), "chunked"))
     {
       DEBUG_puts("1http_set_length: Setting data_encoding to "
                  "HTTP_ENCODING_CHUNKED.");
@@ -4453,7 +4464,7 @@ http_set_length(http_t *http)             /* I - Connection */
     http->data_remaining = remaining;
 
     if (remaining <= INT_MAX)
-      http->_data_remaining = remaining;
+      http->_data_remaining = (int)remaining;
     else
       http->_data_remaining = INT_MAX;
   }
@@ -4469,7 +4480,7 @@ static void
 http_set_timeout(int    fd,            /* I - File descriptor */
                  double timeout)       /* I - Timeout in seconds */
 {
-#ifdef WIN32
+#ifdef _WIN32
   DWORD tv = (DWORD)(timeout * 1000);
                                      /* Timeout in milliseconds */
 
@@ -4484,7 +4495,7 @@ http_set_timeout(int    fd,               /* I - File descriptor */
 
   setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, CUPS_SOCAST &tv, sizeof(tv));
   setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, CUPS_SOCAST &tv, sizeof(tv));
-#endif /* WIN32 */
+#endif /* _WIN32 */
 }
 
 
@@ -4519,7 +4530,7 @@ http_tls_upgrade(http_t *http)            /* I - HTTP connection */
   http_t       myhttp;                 /* Local copy of HTTP data */
 
 
-  DEBUG_printf(("7http_tls_upgrade(%p)", http));
+  DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http));
 
  /*
   * Flush the connection to make sure any previous "Upgrade" message
@@ -4540,10 +4551,15 @@ http_tls_upgrade(http_t *http)          /* I - HTTP connection */
   * encryption on the link...
   */
 
-  http->tls_upgrade         = 1;
-  http->field_authorization = NULL;    /* Don't free the auth string */
+  http->tls_upgrade = 1;
+  memset(http->fields, 0, sizeof(http->fields));
+  http->expect = (http_status_t)0;
+
+  if (http->hostname[0] == '/')
+    httpSetField(http, HTTP_FIELD_HOST, "localhost");
+  else
+    httpSetField(http, HTTP_FIELD_HOST, http->hostname);
 
-  httpClearFields(http);
   httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade");
   httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0");
 
@@ -4560,14 +4576,15 @@ http_tls_upgrade(http_t *http)          /* I - HTTP connection */
   * Restore the HTTP request data...
   */
 
+  memcpy(http->_fields, myhttp._fields, sizeof(http->_fields));
   memcpy(http->fields, myhttp.fields, sizeof(http->fields));
-  http->data_encoding       = myhttp.data_encoding;
-  http->data_remaining      = myhttp.data_remaining;
-  http->_data_remaining     = myhttp._data_remaining;
-  http->expect              = myhttp.expect;
-  http->field_authorization = myhttp.field_authorization;
-  http->digest_tries        = myhttp.digest_tries;
-  http->tls_upgrade         = 0;
+
+  http->data_encoding   = myhttp.data_encoding;
+  http->data_remaining  = myhttp.data_remaining;
+  http->_data_remaining = myhttp._data_remaining;
+  http->expect          = myhttp.expect;
+  http->digest_tries    = myhttp.digest_tries;
+  http->tls_upgrade     = 0;
 
  /*
   * See if we actually went secure...
@@ -4581,6 +4598,7 @@ http_tls_upgrade(http_t *http)            /* I - HTTP connection */
 
     DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
 
+    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Encryption is not supported."), 1);
     httpAddrClose(NULL, http->fd);
 
     http->fd = -1;
@@ -4606,8 +4624,7 @@ http_write(http_t     *http,              /* I - HTTP connection */
                bytes;                  /* Bytes sent */
 
 
-  DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT ")", http,
-                buffer, CUPS_LLCAST length));
+  DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
   http->error = 0;
   tbytes      = 0;
 
@@ -4615,7 +4632,7 @@ http_write(http_t     *http,              /* I - HTTP connection */
   {
     DEBUG_printf(("3http_write: About to write %d bytes.", (int)length));
 
-    if (http->timeout_cb)
+    if (http->timeout_value > 0.0)
     {
 #ifdef HAVE_POLL
       struct pollfd    pfd;            /* Polled file descriptor */
@@ -4646,12 +4663,12 @@ http_write(http_t     *http,            /* I - HTTP connection */
 
          nfds = select(http->fd + 1, NULL, &output_set, NULL, &timeout);
        }
-#  ifdef WIN32
+#  ifdef _WIN32
        while (nfds < 0 && (WSAGetLastError() == WSAEINTR ||
                            WSAGetLastError() == WSAEWOULDBLOCK));
 #  else
        while (nfds < 0 && (errno == EINTR || errno == EAGAIN));
-#  endif /* WIN32 */
+#  endif /* _WIN32 */
 #endif /* HAVE_POLL */
 
         if (nfds < 0)
@@ -4659,13 +4676,13 @@ http_write(http_t     *http,            /* I - HTTP connection */
          http->error = errno;
          return (-1);
        }
-       else if (nfds == 0 && !(*http->timeout_cb)(http, http->timeout_data))
+       else if (nfds == 0 && (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data)))
        {
-#ifdef WIN32
+#ifdef _WIN32
          http->error = WSAEWOULDBLOCK;
 #else
          http->error = EWOULDBLOCK;
-#endif /* WIN32 */
+#endif /* _WIN32 */
          return (-1);
        }
       }
@@ -4674,7 +4691,7 @@ http_write(http_t     *http,              /* I - HTTP connection */
 
 #ifdef HAVE_SSL
     if (http->tls)
-      bytes = http_tls_write(http, buffer, length);
+      bytes = _httpTLSWrite(http, buffer, (int)length);
     else
 #endif /* HAVE_SSL */
     bytes = send(http->fd, buffer, length, 0);
@@ -4684,7 +4701,7 @@ http_write(http_t     *http,              /* I - HTTP connection */
 
     if (bytes < 0)
     {
-#ifdef WIN32
+#ifdef _WIN32
       if (WSAGetLastError() == WSAEINTR)
         continue;
       else if (WSAGetLastError() == WSAEWOULDBLOCK)
@@ -4718,7 +4735,7 @@ http_write(http_t     *http,              /* I - HTTP connection */
         http->error = errno;
        continue;
       }
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
       DEBUG_printf(("3http_write: error writing data (%s).",
                     strerror(http->error)));
@@ -4728,11 +4745,11 @@ http_write(http_t     *http,            /* I - HTTP connection */
 
     buffer += bytes;
     tbytes += bytes;
-    length -= bytes;
+    length -= (size_t)bytes;
   }
 
 #ifdef DEBUG
-  http_debug_hex("http_write", buffer - tbytes, tbytes);
+  http_debug_hex("http_write", buffer - tbytes, (int)tbytes);
 #endif /* DEBUG */
 
   DEBUG_printf(("3http_write: Returning " CUPS_LLFMT ".", CUPS_LLCAST tbytes));
@@ -4754,8 +4771,7 @@ http_write_chunk(http_t     *http,        /* I - HTTP connection */
   ssize_t      bytes;                  /* Bytes written */
 
 
-  DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")",
-                http, buffer, CUPS_LLCAST length));
+  DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
 
  /*
   * Write the chunk header, data, and trailer.
@@ -4782,8 +4798,3 @@ http_write_chunk(http_t     *http,        /* I - HTTP connection */
 
   return (bytes);
 }
-
-
-/*
- * End of "$Id$".
- */