]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http.c
Use httpAddrListen and new httpAddrClose in cupsd and other places that need it.
[thirdparty/cups.git] / cups / http.c
index 046b20c52241b04258c7fc9d00db2f86d416aa43..3db98fca2199eedf0beec728d55ea0223db9a8b5 100644 (file)
 /*
  * "$Id$"
  *
- *   HTTP routines for CUPS.
+ * HTTP routines for CUPS.
  *
- *   Copyright 2007-2013 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2013 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   This file contains Kerberos support code, copyright 2006 by
- *   Jelmer Vernooij.
+ * 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/".
+ * 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.
- *
- * Contents:
- *
- *   httpAcceptConnection()      - Accept a new HTTP client connection from
- *                                 the specified listening socket.
- *   httpAddCredential()         - Allocates and adds a single credential to
- *                                 an array.
- *   _httpBIOMethods()           - Get the OpenSSL BIO methods for HTTP
- *                                 connections.
- *   httpBlocking()              - Set blocking/non-blocking behavior on a
- *                                 connection.
- *   httpCheck()                 - Check to see if there is a pending response
- *                                 from the server.
- *   httpClearCookie()           - Clear the cookie value(s).
- *   httpClearFields()           - Clear HTTP request fields.
- *   httpClose()                 - Close an HTTP connection.
- *   httpConnect()               - Connect to a HTTP server.
- *   httpConnect2()              - Connect to a HTTP server.
- *   httpConnectEncrypt()        - Connect to a HTTP server using encryption.
- *   httpCopyCredentials()       - Copy the credentials associated with an
- *                                 encrypted connection.
- *   _httpCreateCredentials()    - Create credentials in the internal format.
- *   httpDelete()                - Send a DELETE request to the server.
- *   _httpDisconnect()           - Disconnect a HTTP connection.
- *   httpEncryption()            - Set the required encryption on the link.
- *   httpError()                 - Get the last error on a connection.
- *   httpFlush()                 - Flush data from a HTTP connection.
- *   httpFlushWrite()            - Flush data in write buffer.
- *   _httpFreeCredentials()      - Free internal credentials.
- *   httpFreeCredentials()       - Free an array of credentials.
- *   httpGet()                   - Send a GET request to the server.
- *   httpGetAuthString()         - Get the current authorization string.
- *   httpGetBlocking()           - Get the blocking/non-block state of a
- *                                 connection.
- *   httpGetContentEncoding()    - Get a common content encoding, if any,
- *                                 between the client and server.
- *   httpGetCookie()             - Get any cookie data from the response.
- *   httpGetExpect()             - Get the value of the Expect header, if any.
- *   httpGetFd()                 - Get the file descriptor associated with a
- *                                 connection.
- *   httpGetField()              - Get a field value from a request/response.
- *   httpGetLength()             - Get the amount of data remaining from the
- *                                 content-length or transfer-encoding fields.
- *   httpGetLength2()            - Get the amount of data remaining from the
- *                                 content-length or transfer-encoding fields.
- *   httpGets()                  - Get a line of text from a HTTP connection.
- *   httpGetState()              - Get the current state of the HTTP request.
- *   httpGetStatus()             - Get the status of the last HTTP request.
- *   httpGetSubField()           - Get a sub-field value.
- *   httpGetSubField2()          - Get a sub-field value.
- *   httpGetVersion()            - Get the HTTP version at the other end.
- *   httpHead()                  - Send a HEAD request to the server.
- *   httpInitialize()            - Initialize the HTTP interface library and
- *                                 set the default HTTP proxy (if any).
- *   httpOptions()               - Send an OPTIONS request to the server.
- *   httpPeek()                  - Peek at data from a HTTP connection.
- *   httpPost()                  - Send a POST request to the server.
- *   httpPrintf()                - Print a formatted string to a HTTP
- *                                 connection.
- *   httpPut()                   - Send a PUT request to the server.
- *   httpRead()                  - Read data from a HTTP connection.
- *   httpRead2()                 - Read data from a HTTP connection.
- *   _httpReadCDSA()             - Read function for the CDSA library.
- *   _httpReadGNUTLS()           - Read function for the GNU TLS library.
- *   httpReadRequest()           - Read a HTTP request from a connection.
- *   httpReconnect()             - Reconnect to a HTTP server.
- *   httpReconnect2()            - Reconnect to a HTTP server with timeout and
- *                                 optional cancel.
- *   httpSetAuthString()         - Set the current authorization string.
- *   httpSetCredentials()        - Set the credentials associated with an
- *                                 encrypted connection.
- *   httpSetCookie()             - Set the cookie value(s).
- *   httpSetDefaultField()       - Set the default value of an HTTP header.
- *   httpSetExpect()             - Set the Expect: header in a request.
- *   httpSetField()              - Set the value of an HTTP header.
- *   httpSetLength()             - Set the content-length and
- *                                 content-encoding.
- *   httpSetTimeout()            - Set read/write timeouts and an optional
- *                                 callback.
- *   httpTrace()                 - Send an TRACE request to the server.
- *   _httpUpdate()               - Update the current HTTP status for incoming
- *                                 data.
- *   httpUpdate()                - Update the current HTTP state for incoming
- *                                 data.
- *   _httpWait()                 - Wait for data available on a connection (no
- *                                 flush).
- *   httpWait()                  - Wait for data available on a connection.
- *   httpWrite()                 - Write data to a HTTP connection.
- *   httpWrite2()                - Write data to a HTTP connection.
- *   _httpWriteCDSA()            - Write function for the CDSA library.
- *   _httpWriteGNUTLS()          - Write function for the GNU TLS library.
- *   httpWriteResponse()         - Write a HTTP response to a client
- *                                 connection.
- *   http_bio_ctrl()             - Control the HTTP connection.
- *   http_bio_free()             - Free OpenSSL data.
- *   http_bio_new()              - Initialize an OpenSSL BIO structure.
- *   http_bio_puts()             - Send a string for OpenSSL.
- *   http_bio_read()             - Read data for OpenSSL.
- *   http_bio_write()            - Write data for OpenSSL.
- *   http_content_coding_finish() - Finish doing any content encoding.
- *   http_content_coding_start()  - Start doing content encoding.
- *   http_create()               - Create an unconnected HTTP connection.
- *   http_debug_hex()            - Do a hex dump of a buffer.
- *   http_field()                - Return the field index for a field name.
- *   http_read()                 - Read a buffer from a HTTP connection.
- *   http_read_buffered()        - Do a buffered read from a HTTP connection.
- *   http_read_chunk()           - Read a chunk from a HTTP connection.
- *   http_read_ssl()             - Read from a SSL/TLS connection.
- *   http_send()                 - Send a request with all fields and the
- *                                 trailing blank line.
- *   http_set_credentials()      - Set the SSL/TLS credentials.
- *   http_set_length()           - Set the data_encoding and data_remaining
- *                                 values.
- *   http_set_timeout()          - Set the socket timeout values.
- *   http_set_wait()             - Set the default wait value for reads.
- *   http_setup_ssl()            - Set up SSL/TLS support on a connection.
- *   http_shutdown_ssl()         - Shut down SSL/TLS on a connection.
- *   http_state_string()          - Return the string associated with a given
- *                                  HTTP state.
- *   http_upgrade()              - Force upgrade to TLS encryption.
- *   http_write()                - Write a buffer to a HTTP connection.
- *   http_write_chunk()          - Write a chunked buffer.
- *   http_write_ssl()            - Write to a SSL/TLS connection.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
@@ -177,7 +54,6 @@ static http_t                *http_create(const char *host, int port,
 static void            http_debug_hex(const char *prefix, const char *buffer,
                                       int bytes);
 #endif /* DEBUG */
-static http_field_t    http_field(const char *name);
 static ssize_t         http_read(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read_buffered(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read_chunk(http_t *http, char *buffer, size_t length);
@@ -189,16 +65,11 @@ static ssize_t             http_write_chunk(http_t *http, const char *buffer,
                                         size_t length);
 #ifdef HAVE_SSL
 static int             http_read_ssl(http_t *http, char *buf, int len);
-#  ifdef HAVE_CDSASSL
 static int             http_set_credentials(http_t *http);
-#  endif /* HAVE_CDSASSL */
 #endif /* HAVE_SSL */
 static off_t           http_set_length(http_t *http);
 static void            http_set_timeout(int fd, double timeout);
 static void            http_set_wait(http_t *http);
-#ifdef DEBUG
-static const char      *http_state_string(http_state_t state);
-#endif /* DEBUG */
 #ifdef HAVE_SSL
 static int             http_setup_ssl(http_t *http);
 static void            http_shutdown_ssl(http_t *http);
@@ -278,7 +149,7 @@ static BIO_METHOD   http_bio_methods =
  * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
  *                            specified listening socket.
  *
- * @since CUPS 1.7@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 http_t *                               /* O - HTTP connection or @code NULL@ */
@@ -325,8 +196,12 @@ httpAcceptConnection(int fd,               /* I - Listen socket file descriptor */
     return (NULL);
   }
 
-  httpAddrString(&(http->addrlist->addr), http->hostname,
-                sizeof(http->hostname));
+  http->hostaddr = &(http->addrlist->addr);
+
+  if (httpAddrLocalhost(http->hostaddr))
+    strlcpy(http->hostname, "localhost", sizeof(http->hostname));
+  else
+    httpAddrString(http->hostaddr, http->hostname, sizeof(http->hostname));
 
 #ifdef SO_NOSIGPIPE
  /*
@@ -412,7 +287,7 @@ _httpBIOMethods(void)
  */
 
 void
-httpBlocking(http_t *http,             /* I - Connection to server */
+httpBlocking(http_t *http,             /* I - HTTP connection */
              int    b)                 /* I - 1 = blocking, 0 = non-blocking */
 {
   if (http)
@@ -428,7 +303,7 @@ httpBlocking(http_t *http,          /* I - Connection to server */
  */
 
 int                                    /* O - 0 = no data, 1 = data available */
-httpCheck(http_t *http)                        /* I - Connection to server */
+httpCheck(http_t *http)                        /* I - HTTP connection */
 {
   return (httpWait(http, 0));
 }
@@ -441,7 +316,7 @@ httpCheck(http_t *http)                     /* I - Connection to server */
  */
 
 void
-httpClearCookie(http_t *http)          /* I - Connection to server */
+httpClearCookie(http_t *http)          /* I - HTTP connection */
 {
   if (!http)
     return;
@@ -459,7 +334,7 @@ httpClearCookie(http_t *http)               /* I - Connection to server */
  */
 
 void
-httpClearFields(http_t *http)          /* I - Connection to server */
+httpClearFields(http_t *http)          /* I - HTTP connection */
 {
   DEBUG_printf(("httpClearFields(http=%p)", http));
 
@@ -509,7 +384,7 @@ httpClearFields(http_t *http)               /* I - Connection to server */
  */
 
 void
-httpClose(http_t *http)                        /* I - Connection to server */
+httpClose(http_t *http)                        /* I - HTTP connection */
 {
 #ifdef HAVE_GSSAPI
   OM_uint32    minor_status;           /* Minor status code */
@@ -582,7 +457,7 @@ httpConnect(const char *host,               /* I - Host to connect to */
 /*
  * 'httpConnect2()' - Connect to a HTTP server.
  *
- * @since CUPS 1.7@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 http_t *                               /* O - New HTTP connection */
@@ -660,7 +535,7 @@ httpConnectEncrypt(
 
 int                                    /* O - Status of call (0 = success) */
 httpCopyCredentials(
-    http_t      *http,                 /* I - Connection to server */
+    http_t      *http,                 /* I - HTTP connection */
     cups_array_t **credentials)                /* O - Array of credentials */
 {
 #  ifdef HAVE_LIBSSL
@@ -784,7 +659,7 @@ _httpCreateCredentials(
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpDelete(http_t     *http,           /* I - Connection to server */
+httpDelete(http_t     *http,           /* I - HTTP connection */
            const char *uri)            /* I - URI to delete */
 {
   return (http_send(http, HTTP_STATE_DELETE, uri));
@@ -796,18 +671,14 @@ httpDelete(http_t     *http,              /* I - Connection to server */
  */
 
 void
-_httpDisconnect(http_t *http)          /* I - Connection to server */
+_httpDisconnect(http_t *http)          /* I - HTTP connection */
 {
 #ifdef HAVE_SSL
   if (http->tls)
     http_shutdown_ssl(http);
 #endif /* HAVE_SSL */
 
-#ifdef WIN32
-  closesocket(http->fd);
-#else
-  close(http->fd);
-#endif /* WIN32 */
+  httpAddrClose(NULL, http->fd);
 
   http->fd = -1;
 }
@@ -818,7 +689,7 @@ _httpDisconnect(http_t *http)               /* I - Connection to server */
  */
 
 int                                    /* O - -1 on error, 0 on success */
-httpEncryption(http_t            *http,        /* I - Connection to server */
+httpEncryption(http_t            *http,        /* I - HTTP connection */
                http_encryption_t e)    /* I - New encryption preference */
 {
   DEBUG_printf(("httpEncryption(http=%p, e=%d)", http, e));
@@ -850,7 +721,7 @@ httpEncryption(http_t            *http,     /* I - Connection to server */
  */
 
 int                                    /* O - Error code (errno) value */
-httpError(http_t *http)                        /* I - Connection to server */
+httpError(http_t *http)                        /* I - HTTP connection */
 {
   if (http)
     return (http->error);
@@ -859,12 +730,31 @@ httpError(http_t *http)                   /* I - Connection to server */
 }
 
 
+/*
+ * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
+ *                      name.
+ */
+
+http_field_t                           /* O - Field index */
+httpFieldValue(const char *name)       /* I - String name */
+{
+  int  i;                              /* Looping var */
+
+
+  for (i = 0; i < HTTP_FIELD_MAX; i ++)
+    if (!_cups_strcasecmp(name, http_fields[i]))
+      return ((http_field_t)i);
+
+  return (HTTP_FIELD_UNKNOWN);
+}
+
+
 /*
  * 'httpFlush()' - Flush data from a HTTP connection.
  */
 
 void
-httpFlush(http_t *http)                        /* I - Connection to server */
+httpFlush(http_t *http)                        /* I - HTTP connection */
 {
   char         buffer[8192];           /* Junk buffer */
   int          blocking;               /* To block or not to block */
@@ -872,7 +762,7 @@ httpFlush(http_t *http)                     /* I - Connection to server */
 
 
   DEBUG_printf(("httpFlush(http=%p), state=%s", http,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
  /*
   * Nothing to do if we are in the "waiting" state...
@@ -923,11 +813,7 @@ httpFlush(http_t *http)                    /* I - Connection to server */
       http_shutdown_ssl(http);
 #endif /* HAVE_SSL */
 
-#ifdef WIN32
-    closesocket(http->fd);
-#else
-    close(http->fd);
-#endif /* WIN32 */
+    httpAddrClose(NULL, http->fd);
 
     http->fd = -1;
   }
@@ -941,7 +827,7 @@ httpFlush(http_t *http)                     /* I - Connection to server */
  */
 
 int                                    /* O - Bytes written or -1 on error */
-httpFlushWrite(http_t *http)           /* I - Connection to server */
+httpFlushWrite(http_t *http)           /* I - HTTP connection */
 {
   int  bytes;                          /* Bytes written */
 
@@ -959,19 +845,8 @@ httpFlushWrite(http_t *http)               /* I - Connection to server */
   if (http->data_encoding == HTTP_ENCODING_CHUNKED)
     bytes = http_write_chunk(http, http->wbuffer, http->wused);
   else
-  {
     bytes = http_write(http, http->wbuffer, http->wused);
 
-    if (bytes > 0 && http->data_encoding == HTTP_ENCODING_LENGTH)
-    {
-      http->data_remaining -= bytes;
-
-      if (http->data_remaining <= 0)
-      {
-      }
-    }
-  }
-
   http->wused = 0;
 
   DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", bytes, errno));
@@ -1036,13 +911,28 @@ httpFreeCredentials(
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpGet(http_t     *http,              /* I - Connection to server */
+httpGet(http_t     *http,              /* I - HTTP connection */
         const char *uri)               /* I - URI to get */
 {
   return (http_send(http, HTTP_STATE_GET, uri));
 }
 
 
+/*
+ * 'httpGetActivity()' - Get the most recent activity for a connection.
+ *
+ * The return value is the UNIX time of the last read or write.
+ *
+ * @since CUPS 2.0@
+ */
+
+time_t                                 /* O - Time of last read or write */
+httpGetActivity(http_t *http)          /* I - HTTP connection */
+{
+  return (http ? http->activity : 0);
+}
+
+
 /*
  * 'httpGetAuthString()' - Get the current authorization string.
  *
@@ -1055,7 +945,7 @@ httpGet(http_t     *http,          /* I - Connection to server */
  */
 
 char *                                 /* O - Authorization string */
-httpGetAuthString(http_t *http)                /* I - Connection to server */
+httpGetAuthString(http_t *http)                /* I - HTTP connection */
 {
   if (http)
     return (http->authstring);
@@ -1071,7 +961,7 @@ httpGetAuthString(http_t *http)            /* I - Connection to server */
  */
 
 int                                    /* O - 1 if blocking, 0 if non-blocking */
-httpGetBlocking(http_t *http)          /* I - Connection to server */
+httpGetBlocking(http_t *http)          /* I - HTTP connection */
 {
   return (http ? http->blocking : 0);
 }
@@ -1086,7 +976,7 @@ httpGetBlocking(http_t *http)              /* I - Connection to server */
  * 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@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 const char *                           /* O - Content-Coding value or
@@ -1178,13 +1068,30 @@ httpGetCookie(http_t *http)             /* I - HTTP connecion */
 }
 
 
+/*
+ * 'httpGetEncryption()' - Get the current encryption mode of a connection.
+ *
+ * This function returns the encryption mode for the connection. Use the
+ * @link httpIsEncrypted@ function to determine whether a TLS session has
+ * been established.
+ *
+ * @since CUPS 2.0@
+ */
+
+http_encryption_t                      /* O - Current encryption mode */
+httpGetEncryption(http_t *http)                /* I - HTTP connection */
+{
+  return (http ? http->encryption : HTTP_ENCRYPTION_IF_REQUESTED);
+}
+
+
 /*
  * 'httpGetExpect()' - Get the value of the Expect header, if any.
  *
  * 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@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 http_status_t                          /* O - Expect: status, if any */
@@ -1204,7 +1111,7 @@ httpGetExpect(http_t *http)               /* I - Connection to client */
  */
 
 int                                    /* O - File descriptor or -1 if none */
-httpGetFd(http_t *http)                        /* I - Connection to server */
+httpGetFd(http_t *http)                        /* I - HTTP connection */
 {
   return (http ? http->fd : -1);
 }
@@ -1215,7 +1122,7 @@ httpGetFd(http_t *http)                   /* I - Connection to server */
  */
 
 const char *                           /* O - Field value */
-httpGetField(http_t       *http,       /* I - Connection to server */
+httpGetField(http_t       *http,       /* I - HTTP connection */
              http_field_t field)       /* I - Field to get */
 {
   if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
@@ -1249,6 +1156,19 @@ httpGetField(http_t       *http, /* I - Connection to server */
 }
 
 
+/*
+ * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
+ *
+ * @since CUPS 2.0@
+ */
+
+http_keepalive_t                       /* O - Keep-Alive state */
+httpGetKeepAlive(http_t *http)         /* I - HTTP connection */
+{
+  return (http ? http->keep_alive : HTTP_KEEPALIVE_OFF);
+}
+
+
 /*
  * 'httpGetLength()' - Get the amount of data remaining from the
  *                     content-length or transfer-encoding fields.
@@ -1260,7 +1180,7 @@ httpGetField(http_t       *http,  /* I - Connection to server */
  */
 
 int                                    /* O - Content length */
-httpGetLength(http_t *http)            /* I - Connection to server */
+httpGetLength(http_t *http)            /* I - HTTP connection */
 {
  /*
   * Get the read content length and return the 32-bit value.
@@ -1288,13 +1208,13 @@ httpGetLength(http_t *http)             /* I - Connection to server */
  */
 
 off_t                                  /* O - Content length */
-httpGetLength2(http_t *http)           /* I - Connection to server */
+httpGetLength2(http_t *http)           /* I - HTTP connection */
 {
   off_t                        remaining;      /* Remaining length */
 
 
   DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
   if (!http)
     return (-1);
@@ -1345,6 +1265,71 @@ httpGetLength2(http_t *http)             /* I - Connection to server */
 }
 
 
+/*
+ * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
+ *
+ * @since CUPS 2.0@
+ */
+
+size_t                                 /* O - Number of bytes buffered */
+httpGetPending(http_t *http)           /* I - HTTP connection */
+{
+  return (http ? http->wused : 0);
+}
+
+
+/*
+ * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
+ *
+ * @since CUPS 2.0@
+ */
+
+size_t                                 /* O - Number of bytes available */
+httpGetReady(http_t *http)             /* I - HTTP connection */
+{
+  if (!http)
+    return (0);
+  else if (http->used > 0)
+    return (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 */
+  }
+#endif /* HAVE_SSL */
+
+  return (0);
+}
+
+
+/*
+ * 'httpGetRemaining()' - Get the number of remaining bytes in the message
+ *                        body or current chunk.
+ *
+ * The @link httpIsChunked@ function can be used to determine whether the
+ * message body is chunked or fixed-length.
+ *
+ * @since CUPS 2.0@
+ */
+
+size_t                                 /* O - Remaining bytes */
+httpGetRemaining(http_t *http)         /* I - HTTP connection */
+{
+  return (http ? http->data_remaining : 0);
+}
+
+
 /*
  * 'httpGets()' - Get a line of text from a HTTP connection.
  */
@@ -1352,7 +1337,7 @@ httpGetLength2(http_t *http)              /* I - Connection to server */
 char *                                 /* O - Line or NULL */
 httpGets(char   *line,                 /* I - Line to read into */
          int    length,                        /* I - Max length of buffer */
-        http_t *http)                  /* I - Connection to server */
+        http_t *http)                  /* I - HTTP connection */
 {
   char *lineptr,                       /* Pointer into line */
        *lineend,                       /* End of line */
@@ -1524,7 +1509,7 @@ httpGets(char   *line,                    /* I - Line to read into */
  */
 
 http_state_t                           /* O - HTTP state */
-httpGetState(http_t *http)             /* I - Connection to server */
+httpGetState(http_t *http)             /* I - HTTP connection */
 {
   return (http ? http->state : HTTP_STATE_ERROR);
 }
@@ -1537,7 +1522,7 @@ httpGetState(http_t *http)                /* I - Connection to server */
  */
 
 http_status_t                          /* O - HTTP status */
-httpGetStatus(http_t *http)            /* I - Connection to server */
+httpGetStatus(http_t *http)            /* I - HTTP connection */
 {
   return (http ? http->status : HTTP_STATUS_ERROR);
 }
@@ -1550,7 +1535,7 @@ httpGetStatus(http_t *http)               /* I - Connection to server */
  */
 
 char *                                 /* O - Value or NULL */
-httpGetSubField(http_t       *http,    /* I - Connection to server */
+httpGetSubField(http_t       *http,    /* I - HTTP connection */
                 http_field_t field,    /* I - Field index */
                 const char   *name,    /* I - Name of sub-field */
                char         *value)    /* O - Value string */
@@ -1566,7 +1551,7 @@ httpGetSubField(http_t       *http,       /* I - Connection to server */
  */
 
 char *                                 /* O - Value or NULL */
-httpGetSubField2(http_t       *http,   /* I - Connection to server */
+httpGetSubField2(http_t       *http,   /* I - HTTP connection */
                  http_field_t field,   /* I - Field index */
                  const char   *name,   /* I - Name of sub-field */
                 char         *value,   /* O - Value string */
@@ -1696,7 +1681,7 @@ httpGetSubField2(http_t       *http,      /* I - Connection to server */
  */
 
 http_version_t                         /* O - Version number */
-httpGetVersion(http_t *http)           /* I - Connection to server */
+httpGetVersion(http_t *http)           /* I - HTTP connection */
 {
   return (http ? http->version : HTTP_VERSION_1_0);
 }
@@ -1707,7 +1692,7 @@ httpGetVersion(http_t *http)              /* I - Connection to server */
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpHead(http_t     *http,             /* I - Connection to server */
+httpHead(http_t     *http,             /* I - HTTP connection */
          const char *uri)              /* I - URI for head */
 {
   DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http, uri));
@@ -1797,12 +1782,43 @@ httpInitialize(void)
 }
 
 
+/*
+ * 'httpIsChunked()' - Report whether a message body is chunked.
+ *
+ * This function returns non-zero if the message body is composed of
+ * variable-length chunks.
+ *
+ * @since CUPS 2.0@
+ */
+
+int                                    /* O - 1 if chunked, 0 if not */
+httpIsChunked(http_t *http)            /* I - HTTP connection */
+{
+  return (http ? http->data_encoding == HTTP_ENCODING_CHUNKED : 0);
+}
+
+
+/*
+ * 'httpIsEncrypted()' - Report whether a connection is encrypted.
+ *
+ * This function returns non-zero if the connection is currently encrypted.
+ *
+ * @since CUPS 2.0@
+ */
+
+int                                    /* O - 1 if encrypted, 0 if not */
+httpIsEncrypted(http_t *http)          /* I - HTTP connection */
+{
+  return (http ? http->tls != NULL : 0);
+}
+
+
 /*
  * 'httpOptions()' - Send an OPTIONS request to the server.
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpOptions(http_t     *http,          /* I - Connection to server */
+httpOptions(http_t     *http,          /* I - HTTP connection */
             const char *uri)           /* I - URI for options */
 {
   return (http_send(http, HTTP_STATE_OPTIONS, uri));
@@ -1818,11 +1834,11 @@ httpOptions(http_t     *http,           /* I - Connection to server */
  *
  * For non-blocking connections the usual timeouts apply.
  *
- * @since CUPS 1.7@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 ssize_t                                        /* O - Number of bytes copied */
-httpPeek(http_t *http,                 /* I - Connection to server */
+httpPeek(http_t *http,                 /* I - HTTP connection */
          char   *buffer,               /* I - Buffer for data */
         size_t length)                 /* I - Maximum number of bytes */
 {
@@ -1896,7 +1912,7 @@ httpPeek(http_t *http,                    /* I - Connection to server */
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
-                  http_state_string(http->state)));
+                  httpStateString(http->state)));
 
    /*
     * Prevent future reads for this request...
@@ -2075,7 +2091,7 @@ ssize_t _httpPeek(http_t *http, char *buffer, size_t length)
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpPost(http_t     *http,             /* I - Connection to server */
+httpPost(http_t     *http,             /* I - HTTP connection */
          const char *uri)              /* I - URI for post */
 {
   return (http_send(http, HTTP_STATE_POST, uri));
@@ -2089,7 +2105,7 @@ httpPost(http_t     *http,                /* I - Connection to server */
  */
 
 int                                    /* O - Number of bytes written */
-httpPrintf(http_t     *http,           /* I - Connection to server */
+httpPrintf(http_t     *http,           /* I - HTTP connection */
            const char *format,         /* I - printf-style format string */
           ...)                         /* I - Additional args as needed */
 {
@@ -2128,7 +2144,7 @@ httpPrintf(http_t     *http,              /* I - Connection to server */
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpPut(http_t     *http,              /* I - Connection to server */
+httpPut(http_t     *http,              /* I - HTTP connection */
         const char *uri)               /* I - URI to put */
 {
   DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http, uri));
@@ -2146,7 +2162,7 @@ httpPut(http_t     *http,         /* I - Connection to server */
  */
 
 int                                    /* O - Number of bytes read */
-httpRead(http_t *http,                 /* I - Connection to server */
+httpRead(http_t *http,                 /* I - HTTP connection */
          char   *buffer,               /* I - Buffer for data */
         int    length)                 /* I - Maximum number of bytes */
 {
@@ -2161,7 +2177,7 @@ httpRead(http_t *http,                    /* I - Connection to server */
  */
 
 ssize_t                                        /* O - Number of bytes read */
-httpRead2(http_t *http,                        /* I - Connection to server */
+httpRead2(http_t *http,                        /* I - HTTP connection */
           char   *buffer,              /* I - Buffer for data */
          size_t length)                /* I - Maximum number of bytes */
 {
@@ -2365,7 +2381,7 @@ httpRead2(http_t *http,                   /* I - Connection to server */
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("1httpRead2: End of content, set state to %s.",
-                 http_state_string(http->state)));
+                 httpStateString(http->state)));
   }
 
   return (bytes);
@@ -2445,7 +2461,7 @@ _httpReadCDSA(
 
 ssize_t                                        /* O - Number of bytes read or -1 on error */
 _httpReadGNUTLS(
-    gnutls_transport_ptr ptr,          /* I - Connection to server */
+    gnutls_transport_ptr ptr,          /* I - HTTP connection */
     void                 *data,                /* I - Buffer */
     size_t               length)       /* I - Number of bytes to read */
 {
@@ -2483,7 +2499,7 @@ _httpReadGNUTLS(
 /*
  * 'httpReadRequest()' - Read a HTTP request from a connection.
  *
- * @since CUPS 1.7@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 http_state_t                           /* O - New state of connection */
@@ -2515,7 +2531,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   else if (http->state != HTTP_STATE_WAITING)
   {
     DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
-                  http_state_string(http->state)));
+                  httpStateString(http->state)));
     return (HTTP_STATE_ERROR);
   }
 
@@ -2563,6 +2579,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   if (!*req_uri)
   {
     DEBUG_puts("1httpReadRequest: No request URI.");
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request URI."), 1);
     return (HTTP_STATE_ERROR);
   }
 
@@ -2579,6 +2596,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   if (!*req_version)
   {
     DEBUG_puts("1httpReadRequest: No request protocol version.");
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request protocol version."), 1);
     return (HTTP_STATE_ERROR);
   }
 
@@ -2610,11 +2628,12 @@ httpReadRequest(http_t *http,           /* I - HTTP connection */
   else
   {
     DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method));
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown request method."), 1);
     return (HTTP_STATE_UNKNOWN_METHOD);
   }
 
   DEBUG_printf(("1httpReadRequest: Set state to %s.",
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
   if (!strcmp(req_version, "HTTP/1.0"))
   {
@@ -2629,6 +2648,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   else
   {
     DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version));
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown request version."), 1);
     return (HTTP_STATE_UNKNOWN_VERSION);
   }
 
@@ -2649,7 +2669,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
  */
 
 int                                    /* O - 0 on success, non-zero on failure */
-httpReconnect(http_t *http)            /* I - Connection to server */
+httpReconnect(http_t *http)            /* I - HTTP connection */
 {
   DEBUG_printf(("httpReconnect(http=%p)", http));
 
@@ -2663,7 +2683,7 @@ httpReconnect(http_t *http)               /* I - Connection to server */
  */
 
 int                                    /* O - 0 on success, non-zero on failure */
-httpReconnect2(http_t *http,           /* I - Connection to server */
+httpReconnect2(http_t *http,           /* I - HTTP connection */
               int    msec,             /* I - Timeout in milliseconds */
               int    *cancel)          /* I - Pointer to "cancel" variable */
 {
@@ -2699,11 +2719,7 @@ httpReconnect2(http_t *http,             /* I - Connection to server */
   {
     DEBUG_printf(("2httpReconnect2: Closing socket %d...", http->fd));
 
-#ifdef WIN32
-    closesocket(http->fd);
-#else
-    close(http->fd);
-#endif /* WIN32 */
+    httpAddrClose(NULL, http->fd);
 
     http->fd = -1;
   }
@@ -2771,11 +2787,7 @@ httpReconnect2(http_t *http,             /* I - Connection to server */
 
     if (http_setup_ssl(http) != 0)
     {
-#  ifdef WIN32
-      closesocket(http->fd);
-#  else
-      close(http->fd);
-#  endif /* WIN32 */
+      httpAddrClose(NULL, http->fd);
 
       return (-1);
     }
@@ -2804,7 +2816,7 @@ httpReconnect2(http_t *http,              /* I - Connection to server */
  */
 
 void
-httpSetAuthString(http_t     *http,    /* I - Connection to server */
+httpSetAuthString(http_t     *http,    /* I - HTTP connection */
                   const char *scheme,  /* I - Auth scheme (NULL to clear it) */
                  const char *data)     /* I - Auth data (NULL for none) */
 {
@@ -2861,7 +2873,7 @@ httpSetAuthString(http_t     *http,       /* I - Connection to server */
  */
 
 int                                            /* O - Status of call (0 = success) */
-httpSetCredentials(http_t      *http,          /* I - Connection to server */
+httpSetCredentials(http_t      *http,          /* I - HTTP connection */
                   cups_array_t *credentials)   /* I - Array of credentials */
 {
   if (!http || cupsArrayCount(credentials) < 1)
@@ -2904,11 +2916,11 @@ 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@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 void
-httpSetDefaultField(http_t       *http,        /* I - Connection to server */
+httpSetDefaultField(http_t       *http,        /* I - HTTP connection */
                     http_field_t field,        /* I - Field index */
                    const char   *value)/* I - Value */
 {
@@ -2958,7 +2970,7 @@ httpSetDefaultField(http_t       *http,   /* I - Connection to server */
  */
 
 void
-httpSetExpect(http_t        *http,     /* I - Connection to server */
+httpSetExpect(http_t        *http,     /* I - HTTP connection */
               http_status_t expect)    /* I - HTTP status to expect
                                               (@code HTTP_STATUS_CONTINUE@) */
 {
@@ -2974,7 +2986,7 @@ httpSetExpect(http_t        *http,        /* I - Connection to server */
  */
 
 void
-httpSetField(http_t       *http,       /* I - Connection to server */
+httpSetField(http_t       *http,       /* I - HTTP connection */
              http_field_t field,       /* I - Field index */
             const char   *value)       /* I - Value */
 {
@@ -3076,6 +3088,22 @@ httpSetField(http_t       *http, /* I - Connection to server */
 }
 
 
+/*
+ * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
+ *
+ * @since CUPS 2.0@
+ */
+
+void
+httpSetKeepAlive(
+    http_t           *http,            /* I - HTTP connection */
+    http_keepalive_t keep_alive)       /* I - New Keep-Alive value */
+{
+  if (http)
+    http->keep_alive = keep_alive;
+}
+
+
 /*
  * 'httpSetLength()' - Set the content-length and content-encoding.
  *
@@ -3083,7 +3111,7 @@ httpSetField(http_t       *http,  /* I - Connection to server */
  */
 
 void
-httpSetLength(http_t *http,            /* I - Connection to server */
+httpSetLength(http_t *http,            /* I - HTTP connection */
               size_t length)           /* I - Length (0 for chunked) */
 {
   DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT ")", http,
@@ -3118,7 +3146,7 @@ httpSetLength(http_t *http,               /* I - Connection to server */
 
 void
 httpSetTimeout(
-    http_t            *http,           /* I - Connection to server */
+    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 */
@@ -3138,12 +3166,31 @@ httpSetTimeout(
 }
 
 
+/*
+ * 'httpShutdown()' - Shutdown one side of an HTTP connection.
+ *
+ * @since CUPS 2.0@
+ */
+
+void
+httpShutdown(http_t *http)             /* I - HTTP connection */
+{
+  if (!http || http->fd < 0)
+    return;
+
+  if (http->tls)
+    http_shutdown_ssl(http);
+
+  shutdown(http->fd, SHUT_RD);
+}
+
+
 /*
  * 'httpTrace()' - Send an TRACE request to the server.
  */
 
 int                                    /* O - Status of call (0 = success) */
-httpTrace(http_t     *http,            /* I - Connection to server */
+httpTrace(http_t     *http,            /* I - HTTP connection */
           const char *uri)             /* I - URI for trace */
 {
   return (http_send(http, HTTP_STATE_TRACE, uri));
@@ -3158,7 +3205,7 @@ httpTrace(http_t     *http,               /* I - Connection to server */
  */
 
 int                                    /* O - 1 to continue, 0 to stop */
-_httpUpdate(http_t        *http,       /* I - Connection to server */
+_httpUpdate(http_t        *http,       /* I - HTTP connection */
             http_status_t *status)     /* O - Current HTTP status */
 {
   char         line[32768],            /* Line from connection... */
@@ -3168,7 +3215,7 @@ _httpUpdate(http_t        *http,  /* I - Connection to server */
 
 
   DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http, status,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
  /*
   * Grab a single line from the connection...
@@ -3207,11 +3254,7 @@ _httpUpdate(http_t        *http, /* I - Connection to server */
     {
       if (http_setup_ssl(http) != 0)
       {
-#  ifdef WIN32
-       closesocket(http->fd);
-#  else
-       close(http->fd);
-#  endif /* WIN32 */
+        httpAddrClose(NULL, http->fd);
 
        *status = http->status = HTTP_STATUS_ERROR;
        return (0);
@@ -3239,7 +3282,7 @@ _httpUpdate(http_t        *http,  /* I - Connection to server */
          http->state ++;
 
          DEBUG_printf(("1_httpUpdate: Set state to %s.",
-                       http_state_string(http->state)));
+                       httpStateString(http->state)));
 
       case HTTP_STATE_POST_SEND :
       case HTTP_STATE_HEAD :
@@ -3312,7 +3355,7 @@ _httpUpdate(http_t        *http,  /* I - Connection to server */
 
       httpSetCookie(http, value);
     }
-    else if ((field = http_field(line)) != HTTP_FIELD_UNKNOWN)
+    else if ((field = httpFieldValue(line)) != HTTP_FIELD_UNKNOWN)
       httpSetField(http, field, value);
 #ifdef DEBUG
     else
@@ -3336,13 +3379,13 @@ _httpUpdate(http_t        *http,        /* I - Connection to server */
  */
 
 http_status_t                          /* O - HTTP status */
-httpUpdate(http_t *http)               /* I - Connection to server */
+httpUpdate(http_t *http)               /* I - HTTP connection */
 {
   http_status_t        status;                 /* Request status */
 
 
   DEBUG_printf(("httpUpdate(http=%p), state=%s", http,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
  /*
   * Flush pending data, if any...
@@ -3400,7 +3443,7 @@ httpUpdate(http_t *http)          /* I - Connection to server */
  */
 
 int                                    /* O - 1 if data is available, 0 otherwise */
-_httpWait(http_t *http,                        /* I - Connection to server */
+_httpWait(http_t *http,                        /* I - HTTP connection */
           int    msec,                 /* I - Milliseconds to wait */
          int    usessl)                /* I - Use SSL context? */
 {
@@ -3511,7 +3554,7 @@ _httpWait(http_t *http,                   /* I - Connection to server */
  */
 
 int                                    /* O - 1 if data is available, 0 otherwise */
-httpWait(http_t *http,                 /* I - Connection to server */
+httpWait(http_t *http,                 /* I - HTTP connection */
          int    msec)                  /* I - Milliseconds to wait */
 {
  /*
@@ -3567,7 +3610,7 @@ httpWait(http_t *http,                    /* I - Connection to server */
  */
 
 int                                    /* O - Number of bytes written */
-httpWrite(http_t     *http,            /* I - Connection to server */
+httpWrite(http_t     *http,            /* I - HTTP connection */
           const char *buffer,          /* I - Buffer for data */
          int        length)            /* I - Number of bytes to write */
 {
@@ -3582,7 +3625,7 @@ httpWrite(http_t     *http,               /* I - Connection to server */
  */
 
 ssize_t                                        /* O - Number of bytes written */
-httpWrite2(http_t     *http,           /* I - Connection to server */
+httpWrite2(http_t     *http,           /* I - HTTP connection */
            const char *buffer,         /* I - Buffer for data */
           size_t     length)           /* I - Number of bytes to write */
 {
@@ -3741,11 +3784,13 @@ httpWrite2(http_t     *http,            /* I - Connection to server */
 
     if (http->state == HTTP_STATE_POST_RECV)
       http->state ++;
+    else if (http->state == HTTP_STATE_POST_SEND)
+      http->state = HTTP_STATE_WAITING;
     else
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("2httpWrite2: Changed state to %s.",
-                 http_state_string(http->state)));
+                 httpStateString(http->state)));
   }
 
   DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT ".", CUPS_LLCAST bytes));
@@ -3809,7 +3854,7 @@ _httpWriteCDSA(
 
 ssize_t                                        /* O - Number of bytes written or -1 on error */
 _httpWriteGNUTLS(
-    gnutls_transport_ptr ptr,          /* I - Connection to server */
+    gnutls_transport_ptr ptr,          /* I - HTTP connection */
     const void           *data,                /* I - Data buffer */
     size_t               length)       /* I - Number of bytes to write */
 {
@@ -3833,7 +3878,7 @@ _httpWriteGNUTLS(
 /*
  * 'httpWriteResponse()' - Write a HTTP response to a client connection.
  *
- * @since CUPS 1.7@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -3865,7 +3910,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
 
   if (status >= HTTP_STATUS_BAD_REQUEST && http->keep_alive)
   {
-    http->keep_alive = 0;
+    http->keep_alive = HTTP_KEEPALIVE_OFF;
     httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "");
   }
 
@@ -3884,13 +3929,17 @@ httpWriteResponse(http_t        *http,  /* I - HTTP connection */
   }
 
 #ifdef HAVE_SSL
-  if (status == HTTP_STATUS_UPGRADE_REQUIRED)
+  if (status == HTTP_STATUS_UPGRADE_REQUIRED ||
+      status == HTTP_STATUS_SWITCHING_PROTOCOLS)
   {
     if (!http->fields[HTTP_FIELD_CONNECTION][0])
       httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade");
 
     if (!http->fields[HTTP_FIELD_UPGRADE][0])
       httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0");
+
+    if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
+      httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "0");
   }
 #endif /* HAVE_SSL */
 
@@ -3949,8 +3998,8 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
 
     if (http->cookie)
     {
-      if (httpPrintf(http, "Set-Cookie: %s path=/%s\r\n", http->cookie,
-                     http->tls ? " secure" : "") < 1)
+      if (httpPrintf(http, "Set-Cookie: %s path=/ httponly%s\r\n",
+                    http->cookie, http->tls ? " secure" : "") < 1)
       {
        http->status = HTTP_STATUS_ERROR;
        return (-1);
@@ -3970,7 +4019,8 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
     return (-1);
   }
 
-  if (status == HTTP_STATUS_CONTINUE)
+  if (status == HTTP_STATUS_CONTINUE ||
+      status == HTTP_STATUS_SWITCHING_PROTOCOLS)
   {
    /*
     * Restore the old data_encoding and data_length values...
@@ -3992,7 +4042,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
            http->state == HTTP_STATE_STATUS)
   {
     DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
-                  "was %s.", http_state_string(http->state)));
+                  "was %s.", httpStateString(http->state)));
     http->state = HTTP_STATE_WAITING;
   }
   else
@@ -4499,24 +4549,6 @@ http_debug_hex(const char *prefix,       /* I - Prefix for line */
 #endif /* DEBUG */
 
 
-/*
- * 'http_field()' - Return the field index for a field name.
- */
-
-static http_field_t                    /* O - Field index */
-http_field(const char *name)           /* I - String name */
-{
-  int  i;                              /* Looping var */
-
-
-  for (i = 0; i < HTTP_FIELD_MAX; i ++)
-    if (_cups_strcasecmp(name, http_fields[i]) == 0)
-      return ((http_field_t)i);
-
-  return (HTTP_FIELD_UNKNOWN);
-}
-
-
 /*
  * 'http_read()' - Read a buffer from a HTTP connection.
  *
@@ -4525,7 +4557,7 @@ http_field(const char *name)              /* I - String name */
  */
 
 static ssize_t                         /* O - Number of bytes read or -1 on error */
-http_read(http_t *http,                        /* I - Connection to server */
+http_read(http_t *http,                        /* I - HTTP connection */
           char   *buffer,              /* I - Buffer */
           size_t length)               /* I - Maximum bytes to read */
 {
@@ -4639,7 +4671,7 @@ http_read(http_t *http,                   /* I - Connection to server */
  */
 
 static ssize_t                         /* O - Number of bytes read or -1 on error */
-http_read_buffered(http_t *http,       /* I - Connection to server */
+http_read_buffered(http_t *http,       /* I - HTTP connection */
                    char   *buffer,     /* I - Buffer */
                    size_t length)      /* I - Maximum bytes to read */
 {
@@ -4681,7 +4713,7 @@ http_read_buffered(http_t *http,  /* I - Connection to server */
  */
 
 static ssize_t                         /* O - Number of bytes read or -1 on error */
-http_read_chunk(http_t *http,          /* I - Connection to server */
+http_read_chunk(http_t *http,          /* I - HTTP connection */
                char   *buffer,         /* I - Buffer */
                size_t length)          /* I - Maximum bytes to read */
 {
@@ -4748,7 +4780,7 @@ http_read_chunk(http_t *http,             /* I - Connection to server */
  */
 
 static int                             /* O - Bytes read */
-http_read_ssl(http_t *http,            /* I - Connection to server */
+http_read_ssl(http_t *http,            /* I - HTTP connection */
              char   *buf,              /* I - Buffer to store data */
              int    len)               /* I - Length of buffer */
 {
@@ -4838,7 +4870,7 @@ http_read_ssl(http_t *http,               /* I - Connection to server */
  */
 
 static int                             /* O - 0 on success, non-zero on error */
-http_send(http_t       *http,          /* I - Connection to server */
+http_send(http_t       *http,          /* I - HTTP connection */
           http_state_t request,                /* I - Request code */
          const char   *uri)            /* I - URI */
 {
@@ -5027,7 +5059,7 @@ http_send(http_t       *http,             /* I - Connection to server */
  */
 
 static int                             /* O - Status of connection */
-http_set_credentials(http_t *http)     /* I - Connection to server */
+http_set_credentials(http_t *http)     /* I - HTTP connection */
 {
   _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals */
   OSStatus             error = 0;      /* Error code */
@@ -5070,7 +5102,7 @@ http_set_length(http_t *http)             /* I - Connection */
 
 
   DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", http, http->mode,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
   if ((remaining = httpGetLength2(http)) >= 0)
   {
@@ -5143,7 +5175,7 @@ http_set_timeout(int    fd,               /* I - File descriptor */
  */
 
 static void
-http_set_wait(http_t *http)            /* I - Connection to server */
+http_set_wait(http_t *http)            /* I - HTTP connection */
 {
   if (http->blocking)
   {
@@ -5163,7 +5195,7 @@ http_set_wait(http_t *http)               /* I - Connection to server */
  */
 
 static int                             /* O - 0 on success, -1 on failure */
-http_setup_ssl(http_t *http)           /* I - Connection to server */
+http_setup_ssl(http_t *http)           /* I - HTTP connection */
 {
   char                 hostname[256],  /* Hostname */
                        *hostptr;       /* Pointer into hostname */
@@ -5256,7 +5288,7 @@ http_setup_ssl(http_t *http)              /* I - Connection to server */
     if (!message)
       message = _("Unable to establish a secure connection to host.");
 
-    _cupsSetError(IPP_PKI_ERROR, message, 1);
+    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, message, 1);
 
     return (-1);
   }
@@ -5542,7 +5574,7 @@ http_setup_ssl(http_t *http)              /* I - Connection to server */
     http->error  = EIO;
     http->status = HTTP_STATUS_ERROR;
 
-    _cupsSetError(IPP_PKI_ERROR,
+    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI,
                   _("Unable to establish a secure connection to host."), 1);
 
     return (-1);
@@ -5559,7 +5591,7 @@ http_setup_ssl(http_t *http)              /* I - Connection to server */
     http->error  = EIO;
     http->status = HTTP_STATUS_ERROR;
 
-    _cupsSetError(IPP_PKI_ERROR,
+    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI,
                   _("Unable to establish a secure connection to host."), 1);
 
     return (-1);
@@ -5575,7 +5607,7 @@ http_setup_ssl(http_t *http)              /* I - Connection to server */
  */
 
 static void
-http_shutdown_ssl(http_t *http)                /* I - Connection to server */
+http_shutdown_ssl(http_t *http)                /* I - HTTP connection */
 {
 #  ifdef HAVE_LIBSSL
   SSL_CTX      *context;               /* Context for encryption */
@@ -5616,52 +5648,13 @@ http_shutdown_ssl(http_t *http)         /* I - Connection to server */
 #endif /* HAVE_SSL */
 
 
-#ifdef DEBUG
-/*
- * 'http_state_string()' - Return the string associated with a given HTTP state.
- */
-
-static const char *                    /* O - State string */
-http_state_string(http_state_t state)  /* I - HTTP state */
-{
-  static char buffer[255];             /* Unknown value buffer */
-  static const char * const states[] = /* State strings */
-  {
-    "HTTP_STATE_ERROR",
-    "HTTP_STATE_WAITING",
-    "HTTP_STATE_OPTIONS",
-    "HTTP_STATE_GET",
-    "HTTP_STATE_GET_SEND",
-    "HTTP_STATE_HEAD",
-    "HTTP_STATE_POST",
-    "HTTP_STATE_POST_RECV",
-    "HTTP_STATE_POST_SEND",
-    "HTTP_STATE_PUT",
-    "HTTP_STATE_PUT_RECV",
-    "HTTP_STATE_DELETE",
-    "HTTP_STATE_TRACE",
-    "HTTP_STATE_CONNECT",
-    "HTTP_STATE_STATUS",
-    "HTTP_STATE_UNKNOWN_METHOD",
-    "HTTP_STATE_UNKNOWN_VERSION"
-  };
-
-  if (state >= HTTP_STATE_ERROR && state <= HTTP_STATE_UNKNOWN_VERSION)
-    return (states[state - HTTP_STATE_ERROR]);
-
-  snprintf(buffer, sizeof(buffer), "??? %d ???", (int)state);
-  return (buffer);
-}
-#endif /* DEBUG */
-
-
 #ifdef HAVE_SSL
 /*
  * 'http_upgrade()' - Force upgrade to TLS encryption.
  */
 
 static int                             /* O - Status of connection */
-http_upgrade(http_t *http)             /* I - Connection to server */
+http_upgrade(http_t *http)             /* I - HTTP connection */
 {
   int          ret;                    /* Return value */
   http_t       myhttp;                 /* Local copy of HTTP data */
@@ -5729,11 +5722,7 @@ http_upgrade(http_t *http)               /* I - Connection to server */
 
     DEBUG_puts("8http_upgrade: Server does not support HTTP upgrade!");
 
-#  ifdef WIN32
-    closesocket(http->fd);
-#  else
-    close(http->fd);
-#  endif
+    httpAddrClose(NULL, http->fd);
 
     http->fd = -1;
 
@@ -5750,7 +5739,7 @@ http_upgrade(http_t *http)                /* I - Connection to server */
  */
 
 static ssize_t                         /* O - Number of bytes written */
-http_write(http_t     *http,           /* I - Connection to server */
+http_write(http_t     *http,           /* I - HTTP connection */
            const char *buffer,         /* I - Buffer for data */
           size_t     length)           /* I - Number of bytes to write */
 {
@@ -5898,7 +5887,7 @@ http_write(http_t     *http,              /* I - Connection to server */
  */
 
 static ssize_t                         /* O - Number bytes written */
-http_write_chunk(http_t     *http,     /* I - Connection to server */
+http_write_chunk(http_t     *http,     /* I - HTTP connection */
                  const char *buffer,   /* I - Buffer to write */
                 size_t        length)  /* I - Length of buffer */
 {
@@ -5942,7 +5931,7 @@ http_write_chunk(http_t     *http,        /* I - Connection to server */
  */
 
 static int                             /* O - Bytes written */
-http_write_ssl(http_t     *http,       /* I - Connection to server */
+http_write_ssl(http_t     *http,       /* I - HTTP connection */
               const char *buf,         /* I - Buffer holding data */
               int        len)          /* I - Length of buffer */
 {