2 * "$Id: http.c 12034 2014-07-16 19:37:34Z msweet $"
4 * HTTP routines for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * Include necessary headers...
25 #include "cups-private.h"
32 # include <sys/time.h>
33 # include <sys/resource.h>
37 #endif /* HAVE_POLL */
45 static void http_content_coding_finish(http_t
*http
);
46 static void http_content_coding_start(http_t
*http
,
48 #endif /* HAVE_LIBZ */
49 static http_t
*http_create(const char *host
, int port
,
50 http_addrlist_t
*addrlist
, int family
,
51 http_encryption_t encryption
,
52 int blocking
, _http_mode_t mode
);
54 static void http_debug_hex(const char *prefix
, const char *buffer
,
57 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
58 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
59 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
60 static int http_send(http_t
*http
, http_state_t request
,
62 static ssize_t
http_write(http_t
*http
, const char *buffer
,
64 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
66 static off_t
http_set_length(http_t
*http
);
67 static void http_set_timeout(int fd
, double timeout
);
68 static void http_set_wait(http_t
*http
);
71 static int http_tls_upgrade(http_t
*http
);
79 static const char * const http_fields
[] =
96 "If-Unmodified-since",
115 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
116 * specified listening socket.
118 * @since CUPS 1.7/OS X 10.9@
121 http_t
* /* O - HTTP connection or @code NULL@ */
122 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
123 int blocking
) /* I - 1 if the connection should be
124 blocking, 0 otherwise */
126 http_t
*http
; /* HTTP connection */
127 http_addrlist_t addrlist
; /* Dummy address list */
128 socklen_t addrlen
; /* Length of address */
129 int val
; /* Socket option value */
133 * Range check input...
140 * Create the client connection...
143 memset(&addrlist
, 0, sizeof(addrlist
));
145 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
146 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
147 _HTTP_MODE_SERVER
)) == NULL
)
151 * Accept the client and get the remote address...
154 addrlen
= sizeof(http_addr_t
);
156 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
159 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
165 http
->hostaddr
= &(http
->addrlist
->addr
);
167 if (httpAddrLocalhost(http
->hostaddr
))
168 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
170 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
174 * Disable SIGPIPE for this socket.
178 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
179 #endif /* SO_NOSIGPIPE */
182 * Using TCP_NODELAY improves responsiveness, especially on systems
183 * with a slow loopback interface. Since we write large buffers
184 * when sending print files and requests, there shouldn't be any
185 * performance penalty for this...
189 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
193 * Close this socket when starting another process...
196 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
197 #endif /* FD_CLOEXEC */
204 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
206 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
208 * @since CUPS 1.5/OS X 10.7@
211 int /* O - 0 on success, -1 on error */
213 cups_array_t
*credentials
, /* I - Credentials array */
214 const void *data
, /* I - PEM-encoded X.509 data */
215 size_t datalen
) /* I - Length of data */
217 http_credential_t
*credential
; /* Credential data */
220 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
222 credential
->datalen
= datalen
;
224 if ((credential
->data
= malloc(datalen
)) != NULL
)
226 memcpy(credential
->data
, data
, datalen
);
227 cupsArrayAdd(credentials
, credential
);
239 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
243 httpBlocking(http_t
*http
, /* I - HTTP connection */
244 int b
) /* I - 1 = blocking, 0 = non-blocking */
255 * 'httpCheck()' - Check to see if there is a pending response from the server.
258 int /* O - 0 = no data, 1 = data available */
259 httpCheck(http_t
*http
) /* I - HTTP connection */
261 return (httpWait(http
, 0));
266 * 'httpClearCookie()' - Clear the cookie value(s).
268 * @since CUPS 1.1.19/OS X 10.3@
272 httpClearCookie(http_t
*http
) /* I - HTTP connection */
286 * 'httpClearFields()' - Clear HTTP request fields.
290 httpClearFields(http_t
*http
) /* I - HTTP connection */
292 DEBUG_printf(("httpClearFields(http=%p)", http
));
296 memset(http
->fields
, 0, sizeof(http
->fields
));
298 if (http
->mode
== _HTTP_MODE_CLIENT
)
300 if (http
->hostname
[0] == '/')
301 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
303 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
306 if (http
->field_authorization
)
308 free(http
->field_authorization
);
309 http
->field_authorization
= NULL
;
312 if (http
->accept_encoding
)
314 _cupsStrFree(http
->accept_encoding
);
315 http
->accept_encoding
= NULL
;
320 _cupsStrFree(http
->allow
);
326 _cupsStrFree(http
->server
);
330 http
->expect
= (http_status_t
)0;
336 * 'httpClose()' - Close an HTTP connection.
340 httpClose(http_t
*http
) /* I - HTTP connection */
343 OM_uint32 minor_status
; /* Minor status code */
344 #endif /* HAVE_GSSAPI */
347 DEBUG_printf(("httpClose(http=%p)", http
));
350 * Range check input...
357 * Close any open connection...
360 _httpDisconnect(http
);
363 * Free memory used...
366 httpAddrFreeList(http
->addrlist
);
372 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
373 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
375 if (http
->gssname
!= GSS_C_NO_NAME
)
376 gss_release_name(&minor_status
, &http
->gssname
);
377 #endif /* HAVE_GSSAPI */
379 #ifdef HAVE_AUTHORIZATION_H
381 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
382 #endif /* HAVE_AUTHORIZATION_H */
384 httpClearFields(http
);
386 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
387 free(http
->authstring
);
394 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
399 int /* O - 1 if they match, 0 if they do not */
400 httpCompareCredentials(
401 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
402 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
404 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
407 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
))
408 if (temp1
->datalen
!= temp2
->datalen
)
410 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
413 return (temp1
== temp2
);
418 * 'httpConnect()' - Connect to a HTTP server.
420 * This function is deprecated - use @link httpConnect2@ instead.
425 http_t
* /* O - New HTTP connection */
426 httpConnect(const char *host
, /* I - Host to connect to */
427 int port
) /* I - Port number */
429 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
430 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
435 * 'httpConnect2()' - Connect to a HTTP server.
437 * @since CUPS 1.7/OS X 10.9@
440 http_t
* /* O - New HTTP connection */
442 const char *host
, /* I - Host to connect to */
443 int port
, /* I - Port number */
444 http_addrlist_t
*addrlist
, /* I - List of addresses or NULL to lookup */
445 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
446 http_encryption_t encryption
, /* I - Type of encryption to use */
447 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
448 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
449 int *cancel
) /* I - Pointer to "cancel" variable */
451 http_t
*http
; /* New HTTP connection */
454 DEBUG_printf(("httpConnect2(host=\"%s\", port=%d, addrlist=%p, family=%d, "
455 "encryption=%d, blocking=%d, msec=%d, cancel=%p)", host
, port
,
456 addrlist
, family
, encryption
, blocking
, msec
, cancel
));
459 * Create the HTTP structure...
462 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
463 _HTTP_MODE_CLIENT
)) == NULL
)
467 * Optionally connect to the remote system...
470 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
474 * Could not connect to any known address - bail out!
484 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
486 * This function is now deprecated. Please use the @link httpConnect2@ function
492 http_t
* /* O - New HTTP connection */
494 const char *host
, /* I - Host to connect to */
495 int port
, /* I - Port number */
496 http_encryption_t encryption
) /* I - Type of encryption to use */
498 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
499 host
, port
, encryption
));
501 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
507 * 'httpDelete()' - Send a DELETE request to the server.
510 int /* O - Status of call (0 = success) */
511 httpDelete(http_t
*http
, /* I - HTTP connection */
512 const char *uri
) /* I - URI to delete */
514 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
519 * '_httpDisconnect()' - Disconnect a HTTP connection.
523 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
528 #endif /* HAVE_SSL */
530 httpAddrClose(NULL
, http
->fd
);
537 * 'httpEncryption()' - Set the required encryption on the link.
540 int /* O - -1 on error, 0 on success */
541 httpEncryption(http_t
*http
, /* I - HTTP connection */
542 http_encryption_t e
) /* I - New encryption preference */
544 DEBUG_printf(("httpEncryption(http=%p, e=%d)", http
, e
));
550 if (http
->mode
== _HTTP_MODE_CLIENT
)
552 http
->encryption
= e
;
554 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
555 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
556 return (httpReconnect2(http
, 30000, NULL
));
557 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
558 return (http_tls_upgrade(http
));
564 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
567 http
->encryption
= e
;
568 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
569 return (_httpTLSStart(http
));
574 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
578 #endif /* HAVE_SSL */
583 * 'httpError()' - Get the last error on a connection.
586 int /* O - Error code (errno) value */
587 httpError(http_t
*http
) /* I - HTTP connection */
590 return (http
->error
);
597 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
601 http_field_t
/* O - Field index */
602 httpFieldValue(const char *name
) /* I - String name */
604 int i
; /* Looping var */
607 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
608 if (!_cups_strcasecmp(name
, http_fields
[i
]))
609 return ((http_field_t
)i
);
611 return (HTTP_FIELD_UNKNOWN
);
616 * 'httpFlush()' - Flush data from a HTTP connection.
620 httpFlush(http_t
*http
) /* I - HTTP connection */
622 char buffer
[8192]; /* Junk buffer */
623 int blocking
; /* To block or not to block */
624 http_state_t oldstate
; /* Old state */
627 DEBUG_printf(("httpFlush(http=%p), state=%s", http
,
628 httpStateString(http
->state
)));
631 * Nothing to do if we are in the "waiting" state...
634 if (http
->state
== HTTP_STATE_WAITING
)
638 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
641 blocking
= http
->blocking
;
645 * Read any data we can...
648 oldstate
= http
->state
;
649 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
652 * Restore blocking and reset the connection if we didn't get all of
653 * the remaining data...
656 http
->blocking
= blocking
;
658 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
662 * Didn't get the data back, so close the current connection.
667 http_content_coding_finish(http
);
668 #endif /* HAVE_LIBZ */
670 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
672 http
->state
= HTTP_STATE_WAITING
;
677 #endif /* HAVE_SSL */
679 httpAddrClose(NULL
, http
->fd
);
687 * 'httpFlushWrite()' - Flush data in write buffer.
689 * @since CUPS 1.2/OS X 10.5@
692 int /* O - Bytes written or -1 on error */
693 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
695 ssize_t bytes
; /* Bytes written */
698 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", http
,
699 http
? http
->data_encoding
: 100));
701 if (!http
|| !http
->wused
)
703 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
704 "1httpFlushWrite: No connection.");
708 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
709 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
711 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
715 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
722 * 'httpFreeCredentials()' - Free an array of credentials.
727 cups_array_t
*credentials
) /* I - Array of credentials */
729 http_credential_t
*credential
; /* Credential */
732 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
734 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
736 cupsArrayRemove(credentials
, credential
);
737 free((void *)credential
->data
);
741 cupsArrayDelete(credentials
);
746 * 'httpGet()' - Send a GET request to the server.
749 int /* O - Status of call (0 = success) */
750 httpGet(http_t
*http
, /* I - HTTP connection */
751 const char *uri
) /* I - URI to get */
753 return (http_send(http
, HTTP_STATE_GET
, uri
));
758 * 'httpGetActivity()' - Get the most recent activity for a connection.
760 * The return value is the UNIX time of the last read or write.
765 time_t /* O - Time of last read or write */
766 httpGetActivity(http_t
*http
) /* I - HTTP connection */
768 return (http
? http
->activity
: 0);
773 * 'httpGetAuthString()' - Get the current authorization string.
775 * The authorization string is set by cupsDoAuthentication() and
776 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
777 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
780 * @since CUPS 1.3/OS X 10.5@
783 char * /* O - Authorization string */
784 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
787 return (http
->authstring
);
794 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
796 * @since CUPS 1.2/OS X 10.5@
799 int /* O - 1 if blocking, 0 if non-blocking */
800 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
802 return (http
? http
->blocking
: 0);
807 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
808 * the client and server.
810 * This function uses the value of the Accepts-Encoding HTTP header and must be
811 * called after receiving a response from the server or a request from the
812 * client. The value returned can be use in subsequent requests (for clients)
813 * or in the response (for servers) in order to compress the content stream.
815 * @since CUPS 1.7/OS X 10.9@
818 const char * /* O - Content-Coding value or
819 @code NULL@ for the identity
821 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
824 if (http
&& http
->accept_encoding
)
826 int i
; /* Looping var */
827 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
828 *start
, /* Start of coding value */
829 *end
; /* End of coding value */
830 double qvalue
; /* "qvalue" for coding */
831 struct lconv
*loc
= localeconv(); /* Locale data */
832 static const char * const codings
[] =
833 { /* Supported content codings */
840 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
842 for (start
= temp
; *start
; start
= end
)
845 * Find the end of the coding name...
850 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
856 * Grab the qvalue as needed...
859 if (!strncmp(end
, ";q=", 3))
860 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
863 * Skip past all attributes...
867 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
873 while (*end
&& isspace(*end
& 255))
877 * Check value if it matches something we support...
883 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
884 if (!strcmp(start
, codings
[i
]))
888 #endif /* HAVE_LIBZ */
895 * 'httpGetCookie()' - Get any cookie data from the response.
897 * @since CUPS 1.1.19/OS X 10.3@
900 const char * /* O - Cookie data or NULL */
901 httpGetCookie(http_t
*http
) /* I - HTTP connecion */
903 return (http
? http
->cookie
: NULL
);
908 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
910 * This function returns the encryption mode for the connection. Use the
911 * @link httpIsEncrypted@ function to determine whether a TLS session has
917 http_encryption_t
/* O - Current encryption mode */
918 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
920 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
925 * 'httpGetExpect()' - Get the value of the Expect header, if any.
927 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
928 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
930 * @since CUPS 1.7/OS X 10.9@
933 http_status_t
/* O - Expect: status, if any */
934 httpGetExpect(http_t
*http
) /* I - HTTP connection */
937 return (HTTP_STATUS_ERROR
);
939 return (http
->expect
);
944 * 'httpGetFd()' - Get the file descriptor associated with a connection.
946 * @since CUPS 1.2/OS X 10.5@
949 int /* O - File descriptor or -1 if none */
950 httpGetFd(http_t
*http
) /* I - HTTP connection */
952 return (http
? http
->fd
: -1);
957 * 'httpGetField()' - Get a field value from a request/response.
960 const char * /* O - Field value */
961 httpGetField(http_t
*http
, /* I - HTTP connection */
962 http_field_t field
) /* I - Field to get */
964 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
969 case HTTP_FIELD_ACCEPT_ENCODING
:
970 return (http
->accept_encoding
);
972 case HTTP_FIELD_ALLOW
:
973 return (http
->allow
);
975 case HTTP_FIELD_SERVER
:
976 return (http
->server
);
978 case HTTP_FIELD_AUTHORIZATION
:
979 if (http
->field_authorization
)
982 * Special case for WWW-Authenticate: as its contents can be
983 * longer than HTTP_MAX_VALUE...
986 return (http
->field_authorization
);
990 return (http
->fields
[field
]);
996 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
1001 http_keepalive_t
/* O - Keep-Alive state */
1002 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
1004 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1009 * 'httpGetLength()' - Get the amount of data remaining from the
1010 * content-length or transfer-encoding fields.
1012 * This function is deprecated and will not return lengths larger than
1013 * 2^31 - 1; use httpGetLength2() instead.
1018 int /* O - Content length */
1019 httpGetLength(http_t
*http
) /* I - HTTP connection */
1022 * Get the read content length and return the 32-bit value.
1027 httpGetLength2(http
);
1029 return (http
->_data_remaining
);
1037 * 'httpGetLength2()' - Get the amount of data remaining from the
1038 * content-length or transfer-encoding fields.
1040 * This function returns the complete content length, even for
1041 * content larger than 2^31 - 1.
1043 * @since CUPS 1.2/OS X 10.5@
1046 off_t
/* O - Content length */
1047 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1049 off_t remaining
; /* Remaining length */
1052 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http
,
1053 httpStateString(http
->state
)));
1058 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1060 DEBUG_puts("4httpGetLength2: chunked request!");
1066 * The following is a hack for HTTP servers that don't send a
1067 * Content-Length or Transfer-Encoding field...
1069 * If there is no Content-Length then the connection must close
1070 * after the transfer is complete...
1073 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1076 * Default content length is 0 for errors and certain types of operations,
1077 * and 2^31-1 for other successful requests...
1080 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1081 http
->state
== HTTP_STATE_OPTIONS
||
1082 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1083 http
->state
== HTTP_STATE_HEAD
||
1084 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1085 http
->state
== HTTP_STATE_DELETE
||
1086 http
->state
== HTTP_STATE_TRACE
||
1087 http
->state
== HTTP_STATE_CONNECT
)
1090 remaining
= 2147483647;
1092 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1096 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1097 CUPS_LLCAST remaining
));
1105 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1110 size_t /* O - Number of bytes buffered */
1111 httpGetPending(http_t
*http
) /* I - HTTP connection */
1113 return (http
? (size_t)http
->wused
: 0);
1118 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1123 size_t /* O - Number of bytes available */
1124 httpGetReady(http_t
*http
) /* I - HTTP connection */
1128 else if (http
->used
> 0)
1129 return ((size_t)http
->used
);
1132 return (_httpTLSPending(http
));
1133 #endif /* HAVE_SSL */
1140 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1141 * body or current chunk.
1143 * The @link httpIsChunked@ function can be used to determine whether the
1144 * message body is chunked or fixed-length.
1149 size_t /* O - Remaining bytes */
1150 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1152 return (http
? (size_t)http
->data_remaining
: 0);
1157 * 'httpGets()' - Get a line of text from a HTTP connection.
1160 char * /* O - Line or NULL */
1161 httpGets(char *line
, /* I - Line to read into */
1162 int length
, /* I - Max length of buffer */
1163 http_t
*http
) /* I - HTTP connection */
1165 char *lineptr
, /* Pointer into line */
1166 *lineend
, /* End of line */
1167 *bufptr
, /* Pointer into input buffer */
1168 *bufend
; /* Pointer to end of buffer */
1169 ssize_t bytes
; /* Number of bytes read */
1170 int eol
; /* End-of-line? */
1173 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", line
, length
, http
));
1175 if (!http
|| !line
|| length
<= 1)
1179 * Read a line from the buffer...
1184 lineend
= line
+ length
- 1;
1187 while (lineptr
< lineend
)
1190 * Pre-load the buffer as needed...
1199 while (http
->used
== 0)
1202 * No newline; see if there is more data to be read...
1205 while (!_httpWait(http
, http
->wait_value
, 1))
1207 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1210 DEBUG_puts("3httpGets: Timed out!");
1212 http
->error
= WSAETIMEDOUT
;
1214 http
->error
= ETIMEDOUT
;
1219 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1221 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1226 * Nope, can't get a line this time...
1230 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1232 if (WSAGetLastError() == WSAEINTR
)
1234 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1236 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1239 http
->error
= WSAGetLastError();
1241 else if (WSAGetLastError() != http
->error
)
1243 http
->error
= WSAGetLastError();
1248 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1252 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1254 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1256 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1259 http
->error
= errno
;
1261 else if (errno
!= http
->error
)
1263 http
->error
= errno
;
1270 else if (bytes
== 0)
1272 http
->error
= EPIPE
;
1278 * Yup, update the amount used...
1281 http
->used
+= (int)bytes
;
1285 * Now copy as much of the current line as possible...
1288 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1289 lineptr
< lineend
&& bufptr
< bufend
;)
1291 if (*bufptr
== 0x0a)
1297 else if (*bufptr
== 0x0d)
1300 *lineptr
++ = *bufptr
++;
1303 http
->used
-= (int)(bufptr
- http
->buffer
);
1305 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1313 http
->activity
= time(NULL
);
1317 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1323 DEBUG_puts("3httpGets: No new line available!");
1330 * 'httpGetState()' - Get the current state of the HTTP request.
1333 http_state_t
/* O - HTTP state */
1334 httpGetState(http_t
*http
) /* I - HTTP connection */
1336 return (http
? http
->state
: HTTP_STATE_ERROR
);
1341 * 'httpGetStatus()' - Get the status of the last HTTP request.
1343 * @since CUPS 1.2/OS X 10.5@
1346 http_status_t
/* O - HTTP status */
1347 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1349 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1354 * 'httpGetSubField()' - Get a sub-field value.
1359 char * /* O - Value or NULL */
1360 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1361 http_field_t field
, /* I - Field index */
1362 const char *name
, /* I - Name of sub-field */
1363 char *value
) /* O - Value string */
1365 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1370 * 'httpGetSubField2()' - Get a sub-field value.
1372 * @since CUPS 1.2/OS X 10.5@
1375 char * /* O - Value or NULL */
1376 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1377 http_field_t field
, /* I - Field index */
1378 const char *name
, /* I - Name of sub-field */
1379 char *value
, /* O - Value string */
1380 int valuelen
) /* I - Size of value buffer */
1382 const char *fptr
; /* Pointer into field */
1383 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1384 *ptr
, /* Pointer into string buffer */
1385 *end
; /* End of value buffer */
1387 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, "
1388 "valuelen=%d)", http
, field
, name
, value
, valuelen
));
1390 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1391 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1394 end
= value
+ valuelen
- 1;
1396 for (fptr
= http
->fields
[field
]; *fptr
;)
1399 * Skip leading whitespace...
1402 while (_cups_isspace(*fptr
))
1412 * Get the sub-field name...
1416 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1417 ptr
< (temp
+ sizeof(temp
) - 1);
1422 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1425 * Skip trailing chars up to the '='...
1428 while (_cups_isspace(*fptr
))
1438 * Skip = and leading whitespace...
1443 while (_cups_isspace(*fptr
))
1449 * Read quoted string...
1452 for (ptr
= value
, fptr
++;
1453 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1458 while (*fptr
&& *fptr
!= '\"')
1467 * Read unquoted string...
1471 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1476 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1480 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1483 * See if this is the one...
1486 if (!strcmp(name
, temp
))
1488 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1495 DEBUG_puts("3httpGetSubField2: Returning NULL");
1502 * 'httpGetVersion()' - Get the HTTP version at the other end.
1505 http_version_t
/* O - Version number */
1506 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1508 return (http
? http
->version
: HTTP_VERSION_1_0
);
1513 * 'httpHead()' - Send a HEAD request to the server.
1516 int /* O - Status of call (0 = success) */
1517 httpHead(http_t
*http
, /* I - HTTP connection */
1518 const char *uri
) /* I - URI for head */
1520 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http
, uri
));
1521 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1526 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1527 * default HTTP proxy (if any).
1531 httpInitialize(void)
1533 static int initialized
= 0; /* Have we been called before? */
1535 WSADATA winsockdata
; /* WinSock data */
1542 _cupsGlobalUnlock();
1547 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1549 #elif !defined(SO_NOSIGPIPE)
1551 * Ignore SIGPIPE signals...
1555 sigset(SIGPIPE
, SIG_IGN
);
1557 # elif defined(HAVE_SIGACTION)
1558 struct sigaction action
; /* POSIX sigaction data */
1561 memset(&action
, 0, sizeof(action
));
1562 action
.sa_handler
= SIG_IGN
;
1563 sigaction(SIGPIPE
, &action
, NULL
);
1566 signal(SIGPIPE
, SIG_IGN
);
1567 # endif /* !SO_NOSIGPIPE */
1571 _httpTLSInitialize();
1572 # endif /* HAVE_SSL */
1575 _cupsGlobalUnlock();
1580 * 'httpIsChunked()' - Report whether a message body is chunked.
1582 * This function returns non-zero if the message body is composed of
1583 * variable-length chunks.
1588 int /* O - 1 if chunked, 0 if not */
1589 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1591 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1596 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1598 * This function returns non-zero if the connection is currently encrypted.
1603 int /* O - 1 if encrypted, 0 if not */
1604 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1606 return (http
? http
->tls
!= NULL
: 0);
1611 * 'httpOptions()' - Send an OPTIONS request to the server.
1614 int /* O - Status of call (0 = success) */
1615 httpOptions(http_t
*http
, /* I - HTTP connection */
1616 const char *uri
) /* I - URI for options */
1618 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1623 * 'httpPeek()' - Peek at data from a HTTP connection.
1625 * This function copies available data from the given HTTP connection, reading
1626 * a buffer as needed. The data is still available for reading using
1627 * @link httpRead@ or @link httpRead2@.
1629 * For non-blocking connections the usual timeouts apply.
1631 * @since CUPS 1.7/OS X 10.9@
1634 ssize_t
/* O - Number of bytes copied */
1635 httpPeek(http_t
*http
, /* I - HTTP connection */
1636 char *buffer
, /* I - Buffer for data */
1637 size_t length
) /* I - Maximum number of bytes */
1639 ssize_t bytes
; /* Bytes read */
1640 char len
[32]; /* Length string */
1643 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
1644 http
, buffer
, CUPS_LLCAST length
));
1646 if (http
== NULL
|| buffer
== NULL
)
1649 http
->activity
= time(NULL
);
1655 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1656 http
->data_remaining
<= 0)
1658 DEBUG_puts("2httpPeek: Getting chunk length...");
1660 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1662 DEBUG_puts("1httpPeek: Could not get length!");
1668 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1669 if (!httpGets(len
, sizeof(len
), http
))
1671 DEBUG_puts("1httpPeek: Could not get chunk length.");
1676 http
->data_remaining
= strtoll(len
, NULL
, 16);
1678 if (http
->data_remaining
< 0)
1680 DEBUG_puts("1httpPeek: Negative chunk length!");
1685 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1686 CUPS_LLCAST http
->data_remaining
));
1688 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1691 * A zero-length chunk ends a transfer; unless we are reading POST
1696 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1697 http_content_coding_finish(http
);
1698 #endif /* HAVE_LIBZ */
1700 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1701 httpGets(len
, sizeof(len
), http
);
1703 if (http
->state
== HTTP_STATE_POST_RECV
)
1706 http
->state
= HTTP_STATE_STATUS
;
1708 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1709 httpStateString(http
->state
)));
1712 * Prevent future reads for this request...
1715 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1719 else if (length
> (size_t)http
->data_remaining
)
1720 length
= (size_t)http
->data_remaining
;
1723 if (http
->used
== 0 &&
1724 (http
->coding
== _HTTP_CODING_IDENTITY
||
1725 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1727 if (http
->used
== 0)
1728 #endif /* HAVE_LIBZ */
1731 * Buffer small reads for better performance...
1734 ssize_t buflen
; /* Length of read for buffer */
1736 if (!http
->blocking
)
1738 while (!httpWait(http
, http
->wait_value
))
1740 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1747 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1748 buflen
= sizeof(http
->buffer
);
1750 buflen
= (ssize_t
)http
->data_remaining
;
1752 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1753 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1755 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1756 CUPS_LLCAST bytes
));
1760 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1763 http
->used
= (int)bytes
;
1768 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1770 # ifdef HAVE_INFLATECOPY
1771 int zerr
; /* Decompressor error */
1772 z_stream stream
; /* Copy of decompressor stream */
1774 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1776 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1777 /* Number of bytes to copy */
1779 if (http
->stream
.avail_in
> 0 &&
1780 http
->stream
.next_in
> http
->sbuffer
)
1781 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1783 http
->stream
.next_in
= http
->sbuffer
;
1785 if (buflen
> (size_t)http
->data_remaining
)
1786 buflen
= (size_t)http
->data_remaining
;
1788 if (buflen
> (size_t)http
->used
)
1789 buflen
= (size_t)http
->used
;
1791 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1792 "decompression buffer.", (int)buflen
));
1794 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1795 http
->stream
.avail_in
+= buflen
;
1796 http
->used
-= buflen
;
1797 http
->data_remaining
-= buflen
;
1800 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1803 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1804 (int)http
->stream
.avail_in
));
1806 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1808 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1809 http
->error
= ENOMEM
;
1813 stream
.next_out
= (Bytef
*)buffer
;
1814 stream
.avail_out
= (uInt
)length
;
1816 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1817 inflateEnd(&stream
);
1821 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1823 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1830 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1833 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1834 "work with compressed streams.");
1836 # endif /* HAVE_INFLATECOPY */
1839 #endif /* HAVE_LIBZ */
1842 if (length
> (size_t)http
->used
)
1843 length
= (size_t)http
->used
;
1845 bytes
= (ssize_t
)length
;
1847 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1850 memcpy(buffer
, http
->buffer
, length
);
1858 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1861 http
->error
= WSAGetLastError();
1863 if (errno
== EINTR
|| errno
== EAGAIN
)
1866 http
->error
= errno
;
1869 else if (bytes
== 0)
1871 http
->error
= EPIPE
;
1880 * 'httpPost()' - Send a POST request to the server.
1883 int /* O - Status of call (0 = success) */
1884 httpPost(http_t
*http
, /* I - HTTP connection */
1885 const char *uri
) /* I - URI for post */
1887 return (http_send(http
, HTTP_STATE_POST
, uri
));
1892 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1897 int /* O - Number of bytes written */
1898 httpPrintf(http_t
*http
, /* I - HTTP connection */
1899 const char *format
, /* I - printf-style format string */
1900 ...) /* I - Additional args as needed */
1902 ssize_t bytes
; /* Number of bytes to write */
1903 char buf
[16384]; /* Buffer for formatted string */
1904 va_list ap
; /* Variable argument pointer */
1907 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", http
, format
));
1909 va_start(ap
, format
);
1910 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1913 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1915 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1916 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1921 DEBUG_puts("4httpPrintf: flushing existing data...");
1923 if (httpFlushWrite(http
) < 0)
1927 return ((int)http_write(http
, buf
, (size_t)bytes
));
1933 * 'httpPut()' - Send a PUT request to the server.
1936 int /* O - Status of call (0 = success) */
1937 httpPut(http_t
*http
, /* I - HTTP connection */
1938 const char *uri
) /* I - URI to put */
1940 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http
, uri
));
1941 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1946 * 'httpRead()' - Read data from a HTTP connection.
1948 * This function is deprecated. Use the httpRead2() function which can
1949 * read more than 2GB of data.
1954 int /* O - Number of bytes read */
1955 httpRead(http_t
*http
, /* I - HTTP connection */
1956 char *buffer
, /* I - Buffer for data */
1957 int length
) /* I - Maximum number of bytes */
1959 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1964 * 'httpRead2()' - Read data from a HTTP connection.
1966 * @since CUPS 1.2/OS X 10.5@
1969 ssize_t
/* O - Number of bytes read */
1970 httpRead2(http_t
*http
, /* I - HTTP connection */
1971 char *buffer
, /* I - Buffer for data */
1972 size_t length
) /* I - Maximum number of bytes */
1974 ssize_t bytes
; /* Bytes read */
1978 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
1979 ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT
,
1980 http
, buffer
, CUPS_LLCAST length
,
1982 http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1984 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
1985 ") data_encoding=%d data_remaining=" CUPS_LLFMT
,
1986 http
, buffer
, CUPS_LLCAST length
,
1987 http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1988 #endif /* HAVE_LIBZ */
1990 if (http
== NULL
|| buffer
== NULL
)
1993 http
->activity
= time(NULL
);
2000 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2004 if (http
->stream
.avail_in
> 0)
2006 int zerr
; /* Decompressor error */
2008 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
2009 (int)http
->stream
.avail_in
, (int)length
));
2011 http
->stream
.next_out
= (Bytef
*)buffer
;
2012 http
->stream
.avail_out
= (uInt
)length
;
2014 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2016 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2018 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2025 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2027 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2028 http
->stream
.avail_in
, http
->stream
.avail_out
,
2036 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2037 /* Additional bytes for buffer */
2041 if (http
->stream
.avail_in
> 0 &&
2042 http
->stream
.next_in
> http
->sbuffer
)
2043 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2045 http
->stream
.next_in
= http
->sbuffer
;
2047 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2048 "decompression buffer.", (int)buflen
));
2050 if (http
->data_remaining
> 0)
2052 if (buflen
> http
->data_remaining
)
2053 buflen
= (ssize_t
)http
->data_remaining
;
2055 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2057 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2058 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2064 else if (bytes
== 0)
2067 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2068 "decompression buffer.", CUPS_LLCAST bytes
));
2070 http
->data_remaining
-= bytes
;
2071 http
->stream
.avail_in
+= (uInt
)bytes
;
2073 if (http
->data_remaining
<= 0 &&
2074 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2077 * Read the trailing blank line now...
2080 char len
[32]; /* Length string */
2082 httpGets(len
, sizeof(len
), http
);
2094 #endif /* HAVE_LIBZ */
2095 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2097 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2099 http
->data_remaining
-= bytes
;
2101 if (http
->data_remaining
<= 0)
2104 * Read the trailing blank line now...
2107 char len
[32]; /* Length string */
2109 httpGets(len
, sizeof(len
), http
);
2113 else if (http
->data_remaining
<= 0)
2116 * No more data to read...
2123 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2126 if (length
> (size_t)http
->data_remaining
)
2127 length
= (size_t)http
->data_remaining
;
2129 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2131 http
->data_remaining
-= bytes
;
2133 if (http
->data_remaining
<= 0 &&
2134 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2137 * Read the trailing blank line now...
2140 char len
[32]; /* Length string */
2142 httpGets(len
, sizeof(len
), http
);
2149 (http
->coding
== _HTTP_CODING_IDENTITY
||
2150 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2151 #endif /* HAVE_LIBZ */
2152 ((http
->data_remaining
<= 0 &&
2153 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2154 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2157 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2158 http_content_coding_finish(http
);
2159 #endif /* HAVE_LIBZ */
2161 if (http
->state
== HTTP_STATE_POST_RECV
)
2163 else if (http
->state
== HTTP_STATE_GET_SEND
||
2164 http
->state
== HTTP_STATE_POST_SEND
)
2165 http
->state
= HTTP_STATE_WAITING
;
2167 http
->state
= HTTP_STATE_STATUS
;
2169 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2170 httpStateString(http
->state
)));
2178 * 'httpReadRequest()' - Read a HTTP request from a connection.
2180 * @since CUPS 1.7/OS X 10.9@
2183 http_state_t
/* O - New state of connection */
2184 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2185 char *uri
, /* I - URI buffer */
2186 size_t urilen
) /* I - Size of URI buffer */
2188 char line
[4096], /* HTTP request line */
2189 *req_method
, /* HTTP request method */
2190 *req_uri
, /* HTTP request URI */
2191 *req_version
; /* HTTP request version number string */
2195 * Range check input...
2198 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")",
2199 http
, uri
, CUPS_LLCAST urilen
));
2204 if (!http
|| !uri
|| urilen
< 1)
2206 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2207 return (HTTP_STATE_ERROR
);
2209 else if (http
->state
!= HTTP_STATE_WAITING
)
2211 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2212 httpStateString(http
->state
)));
2213 return (HTTP_STATE_ERROR
);
2220 httpClearFields(http
);
2222 http
->activity
= time(NULL
);
2223 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2224 http
->data_remaining
= 0;
2225 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2226 http
->status
= HTTP_STATUS_OK
;
2227 http
->version
= HTTP_VERSION_1_1
;
2230 * Read a line from the socket...
2233 if (!httpGets(line
, sizeof(line
), http
))
2235 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2236 return (HTTP_STATE_ERROR
);
2241 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2242 return (HTTP_STATE_WAITING
);
2245 DEBUG_printf(("1httpReadRequest: %s", line
));
2254 while (*req_uri
&& !isspace(*req_uri
& 255))
2259 DEBUG_puts("1httpReadRequest: No request URI.");
2260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2261 return (HTTP_STATE_ERROR
);
2266 while (*req_uri
&& isspace(*req_uri
& 255))
2269 req_version
= req_uri
;
2271 while (*req_version
&& !isspace(*req_version
& 255))
2276 DEBUG_puts("1httpReadRequest: No request protocol version.");
2277 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2278 return (HTTP_STATE_ERROR
);
2281 *req_version
++ = '\0';
2283 while (*req_version
&& isspace(*req_version
& 255))
2290 if (!strcmp(req_method
, "OPTIONS"))
2291 http
->state
= HTTP_STATE_OPTIONS
;
2292 else if (!strcmp(req_method
, "GET"))
2293 http
->state
= HTTP_STATE_GET
;
2294 else if (!strcmp(req_method
, "HEAD"))
2295 http
->state
= HTTP_STATE_HEAD
;
2296 else if (!strcmp(req_method
, "POST"))
2297 http
->state
= HTTP_STATE_POST
;
2298 else if (!strcmp(req_method
, "PUT"))
2299 http
->state
= HTTP_STATE_PUT
;
2300 else if (!strcmp(req_method
, "DELETE"))
2301 http
->state
= HTTP_STATE_DELETE
;
2302 else if (!strcmp(req_method
, "TRACE"))
2303 http
->state
= HTTP_STATE_TRACE
;
2304 else if (!strcmp(req_method
, "CONNECT"))
2305 http
->state
= HTTP_STATE_CONNECT
;
2308 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2309 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2310 return (HTTP_STATE_UNKNOWN_METHOD
);
2313 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2314 httpStateString(http
->state
)));
2316 if (!strcmp(req_version
, "HTTP/1.0"))
2318 http
->version
= HTTP_VERSION_1_0
;
2319 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2321 else if (!strcmp(req_version
, "HTTP/1.1"))
2323 http
->version
= HTTP_VERSION_1_1
;
2324 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2328 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2329 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2330 return (HTTP_STATE_UNKNOWN_VERSION
);
2333 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2334 strlcpy(uri
, req_uri
, urilen
);
2336 return (http
->state
);
2341 * 'httpReconnect()' - Reconnect to a HTTP server.
2343 * This function is deprecated. Please use the @link httpReconnect2@ function
2349 int /* O - 0 on success, non-zero on failure */
2350 httpReconnect(http_t
*http
) /* I - HTTP connection */
2352 DEBUG_printf(("httpReconnect(http=%p)", http
));
2354 return (httpReconnect2(http
, 30000, NULL
));
2359 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2363 int /* O - 0 on success, non-zero on failure */
2364 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2365 int msec
, /* I - Timeout in milliseconds */
2366 int *cancel
) /* I - Pointer to "cancel" variable */
2368 http_addrlist_t
*addr
; /* Connected address */
2370 http_addrlist_t
*current
; /* Current address */
2371 char temp
[256]; /* Temporary address string */
2375 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", http
, msec
,
2380 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2387 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2390 #endif /* HAVE_SSL */
2393 * Close any previously open socket...
2398 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2400 httpAddrClose(NULL
, http
->fd
);
2406 * Reset all state (except fields, which may be reused)...
2409 http
->state
= HTTP_STATE_WAITING
;
2410 http
->version
= HTTP_VERSION_1_1
;
2411 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2412 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2413 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2414 http
->_data_remaining
= 0;
2416 http
->data_remaining
= 0;
2417 http
->hostaddr
= NULL
;
2421 * Connect to the server...
2425 for (current
= http
->addrlist
; current
; current
= current
->next
)
2426 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2427 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2428 httpAddrPort(&(current
->addr
))));
2431 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
,
2435 * Unable to connect...
2439 http
->error
= WSAGetLastError();
2441 http
->error
= errno
;
2443 http
->status
= HTTP_STATUS_ERROR
;
2445 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2446 strerror(http
->error
)));
2451 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2453 if (http
->timeout_value
> 0)
2454 http_set_timeout(http
->fd
, http
->timeout_value
);
2456 http
->hostaddr
= &(addr
->addr
);
2460 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2463 * Always do encryption via SSL.
2466 if (_httpTLSStart(http
) != 0)
2468 httpAddrClose(NULL
, http
->fd
);
2473 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2474 return (http_tls_upgrade(http
));
2475 #endif /* HAVE_SSL */
2477 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2478 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2479 httpAddrPort(http
->hostaddr
)));
2486 * 'httpSetAuthString()' - Set the current authorization string.
2488 * This function just stores a copy of the current authorization string in
2489 * the HTTP connection object. You must still call httpSetField() to set
2490 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
2491 * httpHead(), httpOptions(), httpPost, or httpPut().
2493 * @since CUPS 1.3/OS X 10.5@
2497 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2498 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2499 const char *data
) /* I - Auth data (NULL for none) */
2502 * Range check input...
2508 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2509 free(http
->authstring
);
2511 http
->authstring
= http
->_authstring
;
2516 * Set the current authorization string...
2519 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2522 if (len
> sizeof(http
->_authstring
))
2524 if ((temp
= malloc(len
)) == NULL
)
2525 len
= sizeof(http
->_authstring
);
2527 http
->authstring
= temp
;
2531 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2533 strlcpy(http
->authstring
, scheme
, len
);
2538 * Clear the current authorization string...
2541 http
->_authstring
[0] = '\0';
2547 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2550 * @since CUPS 1.5/OS X 10.7@
2553 int /* O - Status of call (0 = success) */
2554 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2555 cups_array_t
*credentials
) /* I - Array of credentials */
2557 if (!http
|| cupsArrayCount(credentials
) < 1)
2560 _httpFreeCredentials(http
->tls_credentials
);
2562 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2564 return (http
->tls_credentials
? 0 : -1);
2569 * 'httpSetCookie()' - Set the cookie value(s).
2571 * @since CUPS 1.1.19/OS X 10.3@
2575 httpSetCookie(http_t
*http
, /* I - Connection */
2576 const char *cookie
) /* I - Cookie string */
2585 http
->cookie
= strdup(cookie
);
2587 http
->cookie
= NULL
;
2592 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2594 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2595 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2597 * @since CUPS 1.7/OS X 10.9@
2601 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2602 http_field_t field
, /* I - Field index */
2603 const char *value
)/* I - Value */
2605 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")",
2606 http
, field
, http_fields
[field
], value
));
2613 case HTTP_FIELD_ACCEPT_ENCODING
:
2614 if (http
->default_accept_encoding
)
2615 _cupsStrFree(http
->default_accept_encoding
);
2617 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2620 case HTTP_FIELD_SERVER
:
2621 if (http
->default_server
)
2622 _cupsStrFree(http
->default_server
);
2624 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2627 case HTTP_FIELD_USER_AGENT
:
2628 if (http
->default_user_agent
)
2629 _cupsStrFree(http
->default_user_agent
);
2631 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2635 DEBUG_puts("1httpSetDefaultField: Ignored.");
2642 * 'httpSetExpect()' - Set the Expect: header in a request.
2644 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2647 * @since CUPS 1.2/OS X 10.5@
2651 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2652 http_status_t expect
) /* I - HTTP status to expect
2653 (@code HTTP_STATUS_CONTINUE@) */
2655 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", http
, expect
));
2658 http
->expect
= expect
;
2663 * 'httpSetField()' - Set the value of an HTTP header.
2667 httpSetField(http_t
*http
, /* I - HTTP connection */
2668 http_field_t field
, /* I - Field index */
2669 const char *value
) /* I - Value */
2671 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", http
,
2672 field
, http_fields
[field
], value
));
2675 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2676 field
>= HTTP_FIELD_MAX
||
2682 case HTTP_FIELD_ACCEPT_ENCODING
:
2683 if (http
->accept_encoding
)
2684 _cupsStrFree(http
->accept_encoding
);
2686 http
->accept_encoding
= _cupsStrAlloc(value
);
2689 case HTTP_FIELD_ALLOW
:
2691 _cupsStrFree(http
->allow
);
2693 http
->allow
= _cupsStrAlloc(value
);
2696 case HTTP_FIELD_SERVER
:
2698 _cupsStrFree(http
->server
);
2700 http
->server
= _cupsStrAlloc(value
);
2704 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2708 if (field
== HTTP_FIELD_AUTHORIZATION
)
2711 * Special case for Authorization: as its contents can be
2712 * longer than HTTP_MAX_VALUE
2715 if (http
->field_authorization
)
2716 free(http
->field_authorization
);
2718 http
->field_authorization
= strdup(value
);
2720 else if (field
== HTTP_FIELD_HOST
)
2723 * Special-case for Host: as we don't want a trailing "." on the hostname and
2724 * need to bracket IPv6 numeric addresses.
2727 char *ptr
= strchr(value
, ':');
2729 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2732 * Bracket IPv6 numeric addresses...
2734 * This is slightly inefficient (basically copying twice), but is an edge
2735 * case and not worth optimizing...
2738 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2739 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2744 * Check for a trailing dot on the hostname...
2747 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2751 ptr
+= strlen(ptr
) - 1;
2759 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2760 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
2762 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
2763 http_content_coding_start(http
, value
);
2765 #endif /* HAVE_LIBZ */
2770 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2777 http_t
*http
, /* I - HTTP connection */
2778 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2781 http
->keep_alive
= keep_alive
;
2786 * 'httpSetLength()' - Set the content-length and content-encoding.
2788 * @since CUPS 1.2/OS X 10.5@
2792 httpSetLength(http_t
*http
, /* I - HTTP connection */
2793 size_t length
) /* I - Length (0 for chunked) */
2795 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", http
,
2796 CUPS_LLCAST length
));
2803 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2805 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2809 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2810 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2811 CUPS_LLFMT
, CUPS_LLCAST length
);
2817 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2819 * The optional timeout callback receives both the HTTP connection and a user
2820 * data pointer and must return 1 to continue or 0 to error (time) out.
2822 * @since CUPS 1.5/OS X 10.7@
2827 http_t
*http
, /* I - HTTP connection */
2828 double timeout
, /* I - Number of seconds for timeout,
2829 must be greater than 0 */
2830 http_timeout_cb_t cb
, /* I - Callback function or NULL */
2831 void *user_data
) /* I - User data pointer */
2833 if (!http
|| timeout
<= 0.0)
2836 http
->timeout_cb
= cb
;
2837 http
->timeout_data
= user_data
;
2838 http
->timeout_value
= timeout
;
2841 http_set_timeout(http
->fd
, timeout
);
2843 http_set_wait(http
);
2848 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2854 httpShutdown(http_t
*http
) /* I - HTTP connection */
2856 if (!http
|| http
->fd
< 0)
2863 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2865 shutdown(http
->fd
, SHUT_RD
);
2871 * 'httpTrace()' - Send an TRACE request to the server.
2874 int /* O - Status of call (0 = success) */
2875 httpTrace(http_t
*http
, /* I - HTTP connection */
2876 const char *uri
) /* I - URI for trace */
2878 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2883 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2885 * Note: Unlike httpUpdate(), this function does not flush pending write data
2886 * and only retrieves a single status line from the HTTP connection.
2889 int /* O - 1 to continue, 0 to stop */
2890 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2891 http_status_t
*status
) /* O - Current HTTP status */
2893 char line
[32768], /* Line from connection... */
2894 *value
; /* Pointer to value on line */
2895 http_field_t field
; /* Field index */
2896 int major
, minor
; /* HTTP version numbers */
2899 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http
, status
,
2900 httpStateString(http
->state
)));
2903 * Grab a single line from the connection...
2906 if (!httpGets(line
, sizeof(line
), http
))
2908 *status
= HTTP_STATUS_ERROR
;
2912 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2914 if (line
[0] == '\0')
2917 * Blank line means the start of the data section (if any). Return
2918 * the result code, too...
2920 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2921 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2925 if (http
->status
== HTTP_STATUS_CONTINUE
)
2927 *status
= http
->status
;
2931 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2932 http
->digest_tries
= 0;
2935 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2937 if (_httpTLSStart(http
) != 0)
2939 httpAddrClose(NULL
, http
->fd
);
2941 *status
= http
->status
= HTTP_STATUS_ERROR
;
2945 *status
= HTTP_STATUS_CONTINUE
;
2948 #endif /* HAVE_SSL */
2950 if (http_set_length(http
) < 0)
2952 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2953 http
->error
= EINVAL
;
2954 http
->status
= *status
= HTTP_STATUS_ERROR
;
2958 switch (http
->state
)
2960 case HTTP_STATE_GET
:
2961 case HTTP_STATE_POST
:
2962 case HTTP_STATE_POST_RECV
:
2963 case HTTP_STATE_PUT
:
2966 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2967 httpStateString(http
->state
)));
2969 case HTTP_STATE_POST_SEND
:
2970 case HTTP_STATE_HEAD
:
2974 http
->state
= HTTP_STATE_WAITING
;
2976 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2981 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2982 http_content_coding_start(http
,
2983 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2984 #endif /* HAVE_LIBZ */
2986 *status
= http
->status
;
2989 else if (!strncmp(line
, "HTTP/", 5))
2992 * Got the beginning of a response...
2995 int intstatus
; /* Status value as an integer */
2997 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2999 *status
= http
->status
= HTTP_STATUS_ERROR
;
3003 httpClearFields(http
);
3005 http
->version
= (http_version_t
)(major
* 100 + minor
);
3006 *status
= http
->status
= (http_status_t
)intstatus
;
3008 else if ((value
= strchr(line
, ':')) != NULL
)
3015 while (_cups_isspace(*value
))
3018 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3021 * Be tolerants of servers that send unknown attribute fields...
3024 if (!_cups_strcasecmp(line
, "expect"))
3027 * "Expect: 100-continue" or similar...
3030 http
->expect
= (http_status_t
)atoi(value
);
3032 else if (!_cups_strcasecmp(line
, "cookie"))
3035 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3038 httpSetCookie(http
, value
);
3040 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3041 httpSetField(http
, field
, value
);
3044 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3049 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3050 http
->error
= EINVAL
;
3051 http
->status
= *status
= HTTP_STATUS_ERROR
;
3060 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3063 http_status_t
/* O - HTTP status */
3064 httpUpdate(http_t
*http
) /* I - HTTP connection */
3066 http_status_t status
; /* Request status */
3069 DEBUG_printf(("httpUpdate(http=%p), state=%s", http
,
3070 httpStateString(http
->state
)));
3073 * Flush pending data, if any...
3078 DEBUG_puts("2httpUpdate: flushing buffer...");
3080 if (httpFlushWrite(http
) < 0)
3081 return (HTTP_STATUS_ERROR
);
3085 * If we haven't issued any commands, then there is nothing to "update"...
3088 if (http
->state
== HTTP_STATE_WAITING
)
3089 return (HTTP_STATUS_CONTINUE
);
3092 * Grab all of the lines we can from the connection...
3095 while (_httpUpdate(http
, &status
));
3098 * See if there was an error...
3101 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3103 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3104 return (http
->status
);
3109 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3110 strerror(http
->error
)));
3111 http
->status
= HTTP_STATUS_ERROR
;
3112 return (HTTP_STATUS_ERROR
);
3116 * Return the current status...
3124 * '_httpWait()' - Wait for data available on a connection (no flush).
3127 int /* O - 1 if data is available, 0 otherwise */
3128 _httpWait(http_t
*http
, /* I - HTTP connection */
3129 int msec
, /* I - Milliseconds to wait */
3130 int usessl
) /* I - Use SSL context? */
3133 struct pollfd pfd
; /* Polled file descriptor */
3135 fd_set input_set
; /* select() input set */
3136 struct timeval timeout
; /* Timeout */
3137 #endif /* HAVE_POLL */
3138 int nfds
; /* Result from select()/poll() */
3141 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", http
, msec
, usessl
));
3145 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3150 * Check the SSL/TLS buffers for data first...
3154 if (http
->tls
&& _httpTLSPending(http
))
3156 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3159 #endif /* HAVE_SSL */
3162 * Then try doing a select() or poll() to poll the socket...
3167 pfd
.events
= POLLIN
;
3171 nfds
= poll(&pfd
, 1, msec
);
3173 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3178 FD_ZERO(&input_set
);
3179 FD_SET(http
->fd
, &input_set
);
3181 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3185 timeout
.tv_sec
= msec
/ 1000;
3186 timeout
.tv_usec
= (msec
% 1000) * 1000;
3188 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3191 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3193 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3196 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3197 WSAGetLastError() == WSAEWOULDBLOCK
));
3199 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3201 #endif /* HAVE_POLL */
3203 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3211 * 'httpWait()' - Wait for data available on a connection.
3213 * @since CUPS 1.1.19/OS X 10.3@
3216 int /* O - 1 if data is available, 0 otherwise */
3217 httpWait(http_t
*http
, /* I - HTTP connection */
3218 int msec
) /* I - Milliseconds to wait */
3221 * First see if there is data in the buffer...
3224 DEBUG_printf(("2httpWait(http=%p, msec=%d)", http
, msec
));
3231 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3236 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3238 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3241 #endif /* HAVE_LIBZ */
3244 * Flush pending data, if any...
3249 DEBUG_puts("3httpWait: Flushing write buffer.");
3251 if (httpFlushWrite(http
) < 0)
3256 * If not, check the SSL/TLS buffers and do a select() on the connection...
3259 return (_httpWait(http
, msec
, 1));
3264 * 'httpWrite()' - Write data to a HTTP connection.
3266 * This function is deprecated. Use the httpWrite2() function which can
3267 * write more than 2GB of data.
3272 int /* O - Number of bytes written */
3273 httpWrite(http_t
*http
, /* I - HTTP connection */
3274 const char *buffer
, /* I - Buffer for data */
3275 int length
) /* I - Number of bytes to write */
3277 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3282 * 'httpWrite2()' - Write data to a HTTP connection.
3284 * @since CUPS 1.2/OS X 10.5@
3287 ssize_t
/* O - Number of bytes written */
3288 httpWrite2(http_t
*http
, /* I - HTTP connection */
3289 const char *buffer
, /* I - Buffer for data */
3290 size_t length
) /* I - Number of bytes to write */
3292 ssize_t bytes
; /* Bytes written */
3295 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
3296 buffer
, CUPS_LLCAST length
));
3299 * Range check input...
3302 if (!http
|| !buffer
)
3304 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3309 * Mark activity on the connection...
3312 http
->activity
= time(NULL
);
3315 * Buffer small writes for better performance...
3319 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3321 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3325 http_content_coding_finish(http
);
3330 size_t slen
; /* Bytes to write */
3331 ssize_t sret
; /* Bytes written */
3333 http
->stream
.next_in
= (Bytef
*)buffer
;
3334 http
->stream
.avail_in
= (uInt
)length
;
3336 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3338 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3340 if (http
->stream
.avail_out
> 0)
3343 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3345 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3347 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3348 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3350 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3356 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3360 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3361 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3364 bytes
= (ssize_t
)length
;
3368 #endif /* HAVE_LIBZ */
3371 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3373 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3374 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3376 httpFlushWrite(http
);
3379 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3382 * Write to buffer...
3385 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3386 CUPS_LLCAST length
));
3388 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3389 http
->wused
+= (int)length
;
3390 bytes
= (ssize_t
)length
;
3395 * Otherwise write the data directly...
3398 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3399 CUPS_LLCAST length
));
3401 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3402 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3404 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3406 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3407 CUPS_LLCAST bytes
));
3410 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3411 http
->data_remaining
-= bytes
;
3417 * Handle end-of-request processing...
3420 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3421 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3424 * Finished with the transfer; unless we are sending POST or PUT
3429 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3430 http_content_coding_finish(http
);
3431 #endif /* HAVE_LIBZ */
3435 if (httpFlushWrite(http
) < 0)
3439 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3442 * Send a 0-length chunk at the end of the request...
3445 http_write(http
, "0\r\n\r\n", 5);
3448 * Reset the data state...
3451 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3452 http
->data_remaining
= 0;
3455 if (http
->state
== HTTP_STATE_POST_RECV
)
3457 else if (http
->state
== HTTP_STATE_POST_SEND
||
3458 http
->state
== HTTP_STATE_GET_SEND
)
3459 http
->state
= HTTP_STATE_WAITING
;
3461 http
->state
= HTTP_STATE_STATUS
;
3463 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3464 httpStateString(http
->state
)));
3467 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3474 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3476 * @since CUPS 1.7/OS X 10.9@
3479 int /* O - 0 on success, -1 on error */
3480 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3481 http_status_t status
) /* I - Status code */
3483 http_encoding_t old_encoding
; /* Old data_encoding value */
3484 off_t old_remaining
; /* Old data_remaining value */
3488 * Range check input...
3491 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", http
, status
));
3493 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3495 DEBUG_puts("1httpWriteResponse: Bad input.");
3500 * Set the various standard fields if they aren't already...
3503 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3504 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3506 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3508 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3509 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3512 if (http
->version
== HTTP_VERSION_1_1
)
3514 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3516 if (http
->keep_alive
)
3517 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3519 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3522 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3523 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3527 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3528 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3530 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3531 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3533 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3534 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3536 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3537 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3539 #endif /* HAVE_SSL */
3542 httpSetField(http
, HTTP_FIELD_SERVER
,
3543 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3546 * Set the Accept-Encoding field if it isn't already...
3549 if (!http
->accept_encoding
)
3550 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3551 http
->default_accept_encoding
? http
->default_accept_encoding
:
3553 "gzip, deflate, identity");
3556 #endif /* HAVE_LIBZ */
3559 * Send the response header...
3562 old_encoding
= http
->data_encoding
;
3563 old_remaining
= http
->data_remaining
;
3564 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3566 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3567 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3569 http
->status
= HTTP_STATUS_ERROR
;
3573 if (status
!= HTTP_STATUS_CONTINUE
)
3576 * 100 Continue doesn't have the rest of the response headers...
3579 int i
; /* Looping var */
3580 const char *value
; /* Field value */
3582 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3584 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3586 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3588 http
->status
= HTTP_STATUS_ERROR
;
3596 if (strchr(http
->cookie
, ';'))
3598 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3600 http
->status
= HTTP_STATUS_ERROR
;
3604 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3606 http
->status
= HTTP_STATUS_ERROR
;
3612 if (httpWrite2(http
, "\r\n", 2) < 2)
3614 http
->status
= HTTP_STATUS_ERROR
;
3618 if (httpFlushWrite(http
) < 0)
3620 http
->status
= HTTP_STATUS_ERROR
;
3624 if (status
== HTTP_STATUS_CONTINUE
||
3625 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3628 * Restore the old data_encoding and data_length values...
3631 http
->data_encoding
= old_encoding
;
3632 http
->data_remaining
= old_remaining
;
3634 if (old_remaining
<= INT_MAX
)
3635 http
->_data_remaining
= (int)old_remaining
;
3637 http
->_data_remaining
= INT_MAX
;
3639 else if (http
->state
== HTTP_STATE_OPTIONS
||
3640 http
->state
== HTTP_STATE_HEAD
||
3641 http
->state
== HTTP_STATE_PUT
||
3642 http
->state
== HTTP_STATE_TRACE
||
3643 http
->state
== HTTP_STATE_CONNECT
||
3644 http
->state
== HTTP_STATE_STATUS
)
3646 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3647 "was %s.", httpStateString(http
->state
)));
3648 http
->state
= HTTP_STATE_WAITING
;
3653 * Force data_encoding and data_length to be set according to the response
3657 http_set_length(http
);
3659 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3661 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3662 "was %s.", httpStateString(http
->state
)));
3663 http
->state
= HTTP_STATE_WAITING
;
3669 * Then start any content encoding...
3672 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3673 http_content_coding_start(http
,
3674 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3675 #endif /* HAVE_LIBZ */
3685 * 'http_content_coding_finish()' - Finish doing any content encoding.
3689 http_content_coding_finish(
3690 http_t
*http
) /* I - HTTP connection */
3692 int zerr
; /* Compression status */
3693 Byte dummy
[1]; /* Dummy read buffer */
3694 size_t bytes
; /* Number of bytes to write */
3697 DEBUG_printf(("http_content_coding_finish(http=%p)", http
));
3698 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3700 switch (http
->coding
)
3702 case _HTTP_CODING_DEFLATE
:
3703 case _HTTP_CODING_GZIP
:
3704 http
->stream
.next_in
= dummy
;
3705 http
->stream
.avail_in
= 0;
3709 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3710 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3714 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3716 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3717 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3719 http_write(http
, (char *)http
->sbuffer
, bytes
);
3722 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3723 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3725 while (zerr
== Z_OK
);
3727 deflateEnd(&(http
->stream
));
3729 free(http
->sbuffer
);
3730 http
->sbuffer
= NULL
;
3733 httpFlushWrite(http
);
3736 case _HTTP_CODING_INFLATE
:
3737 case _HTTP_CODING_GUNZIP
:
3738 inflateEnd(&(http
->stream
));
3739 free(http
->sbuffer
);
3740 http
->sbuffer
= NULL
;
3747 http
->coding
= _HTTP_CODING_IDENTITY
;
3752 * 'http_content_coding_start()' - Start doing content encoding.
3756 http_content_coding_start(
3757 http_t
*http
, /* I - HTTP connection */
3758 const char *value
) /* I - Value of Content-Encoding */
3760 int zerr
; /* Error/status */
3761 _http_coding_t coding
; /* Content coding value */
3764 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", http
,
3767 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3769 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3773 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3775 if (http
->state
== HTTP_STATE_GET_SEND
||
3776 http
->state
== HTTP_STATE_POST_SEND
)
3777 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3778 _HTTP_CODING_GUNZIP
;
3779 else if (http
->state
== HTTP_STATE_POST_RECV
||
3780 http
->state
== HTTP_STATE_PUT_RECV
)
3781 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3782 _HTTP_CODING_GUNZIP
;
3785 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3789 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3791 if (http
->state
== HTTP_STATE_GET_SEND
||
3792 http
->state
== HTTP_STATE_POST_SEND
)
3793 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3794 _HTTP_CODING_INFLATE
;
3795 else if (http
->state
== HTTP_STATE_POST_RECV
||
3796 http
->state
== HTTP_STATE_PUT_RECV
)
3797 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3798 _HTTP_CODING_INFLATE
;
3801 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3807 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3811 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3815 case _HTTP_CODING_DEFLATE
:
3816 case _HTTP_CODING_GZIP
:
3818 httpFlushWrite(http
);
3820 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3822 http
->status
= HTTP_STATUS_ERROR
;
3823 http
->error
= errno
;
3828 * Window size for compression is 11 bits - optimal based on PWG Raster
3829 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3833 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3835 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3836 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3838 http
->status
= HTTP_STATUS_ERROR
;
3839 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3843 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3844 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3847 case _HTTP_CODING_INFLATE
:
3848 case _HTTP_CODING_GUNZIP
:
3849 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3851 http
->status
= HTTP_STATUS_ERROR
;
3852 http
->error
= errno
;
3857 * Window size for decompression is up to 15 bits (maximum supported).
3858 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3861 if ((zerr
= inflateInit2(&(http
->stream
),
3862 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3865 free(http
->sbuffer
);
3866 http
->sbuffer
= NULL
;
3867 http
->status
= HTTP_STATUS_ERROR
;
3868 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3872 http
->stream
.avail_in
= 0;
3873 http
->stream
.next_in
= http
->sbuffer
;
3880 http
->coding
= coding
;
3882 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3885 #endif /* HAVE_LIBZ */
3889 * 'http_create()' - Create an unconnected HTTP connection.
3892 static http_t
* /* O - HTTP connection */
3894 const char *host
, /* I - Hostname */
3895 int port
, /* I - Port number */
3896 http_addrlist_t
*addrlist
, /* I - Address list or NULL */
3897 int family
, /* I - Address family or AF_UNSPEC */
3898 http_encryption_t encryption
, /* I - Encryption to use */
3899 int blocking
, /* I - 1 for blocking mode */
3900 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3902 http_t
*http
; /* New HTTP connection */
3903 char service
[255]; /* Service name */
3904 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3907 DEBUG_printf(("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, "
3908 "encryption=%d, blocking=%d, mode=%d)", host
, port
, addrlist
,
3909 family
, encryption
, blocking
, mode
));
3911 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3917 * Lookup the host...
3922 myaddrlist
= httpAddrCopyList(addrlist
);
3926 snprintf(service
, sizeof(service
), "%d", port
);
3928 myaddrlist
= httpAddrGetList(host
, family
, service
);
3935 * Allocate memory for the structure...
3938 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3940 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3941 httpAddrFreeList(addrlist
);
3946 * Initialize the HTTP data...
3950 http
->activity
= time(NULL
);
3951 http
->addrlist
= myaddrlist
;
3952 http
->blocking
= blocking
;
3955 http
->gssctx
= GSS_C_NO_CONTEXT
;
3956 http
->gssname
= GSS_C_NO_NAME
;
3957 #endif /* HAVE_GSSAPI */
3958 http
->status
= HTTP_STATUS_CONTINUE
;
3959 http
->version
= HTTP_VERSION_1_1
;
3962 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3964 if (port
== 443) /* Always use encryption for https */
3965 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3967 http
->encryption
= encryption
;
3969 http_set_wait(http
);
3972 * Return the new structure...
3981 * 'http_debug_hex()' - Do a hex dump of a buffer.
3985 http_debug_hex(const char *prefix
, /* I - Prefix for line */
3986 const char *buffer
, /* I - Buffer to dump */
3987 int bytes
) /* I - Bytes to dump */
3989 int i
, j
, /* Looping vars */
3990 ch
; /* Current character */
3991 char line
[255], /* Line buffer */
3992 *start
, /* Start of line after prefix */
3993 *ptr
; /* Pointer into line */
3996 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
3999 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4001 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4002 start
= line
+ strlen(line
);
4004 for (i
= 0; i
< bytes
; i
+= 16)
4006 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4007 sprintf(ptr
, "%02X", buffer
[i
+ j
] & 255);
4011 memcpy(ptr
, " ", 3);
4016 memcpy(ptr
, " ", 3);
4019 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4021 ch
= buffer
[i
+ j
] & 255;
4023 if (ch
< ' ' || ch
>= 127)
4037 * 'http_read()' - Read a buffer from a HTTP connection.
4039 * This function does the low-level read from the socket, retrying and timing
4043 static ssize_t
/* O - Number of bytes read or -1 on error */
4044 http_read(http_t
*http
, /* I - HTTP connection */
4045 char *buffer
, /* I - Buffer */
4046 size_t length
) /* I - Maximum bytes to read */
4048 ssize_t bytes
; /* Bytes read */
4051 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
4052 buffer
, CUPS_LLCAST length
));
4054 if (!http
->blocking
)
4056 while (!httpWait(http
, http
->wait_value
))
4058 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4061 DEBUG_puts("2http_read: Timeout.");
4066 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4072 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4074 #endif /* HAVE_SSL */
4075 bytes
= recv(http
->fd
, buffer
, length
, 0);
4080 if (WSAGetLastError() != WSAEINTR
)
4082 http
->error
= WSAGetLastError();
4085 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4087 if (!http
->timeout_cb
||
4088 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4090 http
->error
= WSAEWOULDBLOCK
;
4095 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4097 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4099 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4101 http
->error
= errno
;
4104 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4106 http
->error
= errno
;
4110 else if (errno
!= EINTR
)
4112 http
->error
= errno
;
4120 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4121 CUPS_LLCAST bytes
));
4124 http_debug_hex("http_read", buffer
, (int)bytes
);
4130 if (WSAGetLastError() == WSAEINTR
)
4133 http
->error
= WSAGetLastError();
4135 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4138 http
->error
= errno
;
4141 else if (bytes
== 0)
4143 http
->error
= EPIPE
;
4152 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4154 * This function reads data from the HTTP buffer or from the socket, as needed.
4157 static ssize_t
/* O - Number of bytes read or -1 on error */
4158 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4159 char *buffer
, /* I - Buffer */
4160 size_t length
) /* I - Maximum bytes to read */
4162 ssize_t bytes
; /* Bytes read */
4165 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
4167 http
, buffer
, CUPS_LLCAST length
, http
->used
));
4171 if (length
> (size_t)http
->used
)
4172 bytes
= (ssize_t
)http
->used
;
4174 bytes
= (ssize_t
)length
;
4176 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4179 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4180 http
->used
-= (int)bytes
;
4183 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4186 bytes
= http_read(http
, buffer
, length
);
4193 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4195 * This function reads and validates the chunk length, then does a buffered read
4196 * returning the number of bytes placed in the buffer.
4199 static ssize_t
/* O - Number of bytes read or -1 on error */
4200 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4201 char *buffer
, /* I - Buffer */
4202 size_t length
) /* I - Maximum bytes to read */
4204 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
4205 http
, buffer
, CUPS_LLCAST length
));
4207 if (http
->data_remaining
<= 0)
4209 char len
[32]; /* Length string */
4211 if (!httpGets(len
, sizeof(len
), http
))
4213 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4219 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4220 if (!httpGets(len
, sizeof(len
), http
))
4222 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4227 http
->data_remaining
= strtoll(len
, NULL
, 16);
4229 if (http
->data_remaining
< 0)
4231 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4232 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4236 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4237 len
, CUPS_LLCAST http
->data_remaining
));
4239 if (http
->data_remaining
== 0)
4242 * 0-length chunk, grab trailing blank line...
4245 httpGets(len
, sizeof(len
), http
);
4249 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4250 CUPS_LLCAST http
->data_remaining
));
4252 if (http
->data_remaining
<= 0)
4254 else if (length
> (size_t)http
->data_remaining
)
4255 length
= (size_t)http
->data_remaining
;
4257 return (http_read_buffered(http
, buffer
, length
));
4262 * 'http_send()' - Send a request with all fields and the trailing blank line.
4265 static int /* O - 0 on success, non-zero on error */
4266 http_send(http_t
*http
, /* I - HTTP connection */
4267 http_state_t request
, /* I - Request code */
4268 const char *uri
) /* I - URI */
4270 int i
; /* Looping var */
4271 char buf
[1024]; /* Encoded URI buffer */
4272 const char *value
; /* Field value */
4273 static const char * const codes
[] = /* Request code strings */
4293 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")",
4294 http
, codes
[request
], uri
));
4296 if (http
== NULL
|| uri
== NULL
)
4300 * Set the User-Agent field if it isn't already...
4303 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4305 if (http
->default_user_agent
)
4306 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4308 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4312 * Set the Accept-Encoding field if it isn't already...
4315 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4316 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4317 http
->default_accept_encoding
);
4320 * Encode the URI as needed...
4323 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4326 * See if we had an error the last time around; if so, reconnect...
4329 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4330 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4332 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4333 http
->fd
, http
->status
, http
->tls_upgrade
));
4335 if (httpReconnect2(http
, 30000, NULL
))
4340 * Flush any written data that is pending...
4345 if (httpFlushWrite(http
) < 0)
4346 if (httpReconnect2(http
, 30000, NULL
))
4351 * Send the request header...
4354 http
->state
= request
;
4355 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4357 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4360 http
->status
= HTTP_STATUS_CONTINUE
;
4363 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4365 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4366 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4368 #endif /* HAVE_SSL */
4370 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4372 http
->status
= HTTP_STATUS_ERROR
;
4376 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4377 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4379 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4381 if (i
== HTTP_FIELD_HOST
)
4383 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4384 httpAddrPort(http
->hostaddr
)) < 1)
4386 http
->status
= HTTP_STATUS_ERROR
;
4390 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4392 http
->status
= HTTP_STATUS_ERROR
;
4398 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4400 http
->status
= HTTP_STATUS_ERROR
;
4404 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4405 http
->mode
, http
->state
));
4407 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4408 (http
->state
== HTTP_STATE_POST_RECV
||
4409 http
->state
== HTTP_STATE_PUT_RECV
))
4410 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4412 http
->status
= HTTP_STATUS_ERROR
;
4416 if (httpPrintf(http
, "\r\n") < 1)
4418 http
->status
= HTTP_STATUS_ERROR
;
4422 if (httpFlushWrite(http
) < 0)
4425 http_set_length(http
);
4426 httpClearFields(http
);
4429 * The Kerberos and AuthRef authentication strings can only be used once...
4432 if (http
->field_authorization
&& http
->authstring
&&
4433 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4434 !strncmp(http
->authstring
, "AuthRef", 7)))
4436 http
->_authstring
[0] = '\0';
4438 if (http
->authstring
!= http
->_authstring
)
4439 free(http
->authstring
);
4441 http
->authstring
= http
->_authstring
;
4449 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4452 static off_t
/* O - Remainder or -1 on error */
4453 http_set_length(http_t
*http
) /* I - Connection */
4455 off_t remaining
; /* Remainder */
4458 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", http
, http
->mode
,
4459 httpStateString(http
->state
)));
4461 if ((remaining
= httpGetLength2(http
)) >= 0)
4463 if (http
->mode
== _HTTP_MODE_SERVER
&&
4464 http
->state
!= HTTP_STATE_GET_SEND
&&
4465 http
->state
!= HTTP_STATE_PUT
&&
4466 http
->state
!= HTTP_STATE_POST
&&
4467 http
->state
!= HTTP_STATE_POST_SEND
)
4469 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4473 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4476 DEBUG_puts("1http_set_length: Setting data_encoding to "
4477 "HTTP_ENCODING_CHUNKED.");
4478 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4482 DEBUG_puts("1http_set_length: Setting data_encoding to "
4483 "HTTP_ENCODING_LENGTH.");
4484 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4487 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4488 CUPS_LLCAST remaining
));
4489 http
->data_remaining
= remaining
;
4491 if (remaining
<= INT_MAX
)
4492 http
->_data_remaining
= (int)remaining
;
4494 http
->_data_remaining
= INT_MAX
;
4501 * 'http_set_timeout()' - Set the socket timeout values.
4505 http_set_timeout(int fd
, /* I - File descriptor */
4506 double timeout
) /* I - Timeout in seconds */
4509 DWORD tv
= (DWORD
)(timeout
* 1000);
4510 /* Timeout in milliseconds */
4512 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4513 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4516 struct timeval tv
; /* Timeout in secs and usecs */
4518 tv
.tv_sec
= (int)timeout
;
4519 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4521 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4522 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4528 * 'http_set_wait()' - Set the default wait value for reads.
4532 http_set_wait(http_t
*http
) /* I - HTTP connection */
4536 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4538 if (http
->wait_value
<= 0)
4539 http
->wait_value
= 60000;
4542 http
->wait_value
= 10000;
4548 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4551 static int /* O - Status of connection */
4552 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4554 int ret
; /* Return value */
4555 http_t myhttp
; /* Local copy of HTTP data */
4558 DEBUG_printf(("7http_tls_upgrade(%p)", http
));
4561 * Flush the connection to make sure any previous "Upgrade" message
4568 * Copy the HTTP data to a local variable so we can do the OPTIONS
4569 * request without interfering with the existing request data...
4572 memcpy(&myhttp
, http
, sizeof(myhttp
));
4575 * Send an OPTIONS request to the server, requiring SSL or TLS
4576 * encryption on the link...
4579 http
->tls_upgrade
= 1;
4580 http
->field_authorization
= NULL
; /* Don't free the auth string */
4582 httpClearFields(http
);
4583 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4584 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4586 if ((ret
= httpOptions(http
, "*")) == 0)
4589 * Wait for the secure connection...
4592 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4596 * Restore the HTTP request data...
4599 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4600 http
->data_encoding
= myhttp
.data_encoding
;
4601 http
->data_remaining
= myhttp
.data_remaining
;
4602 http
->_data_remaining
= myhttp
._data_remaining
;
4603 http
->expect
= myhttp
.expect
;
4604 http
->field_authorization
= myhttp
.field_authorization
;
4605 http
->digest_tries
= myhttp
.digest_tries
;
4606 http
->tls_upgrade
= 0;
4609 * See if we actually went secure...
4615 * Server does not support HTTP upgrade...
4618 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4620 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4621 httpAddrClose(NULL
, http
->fd
);
4630 #endif /* HAVE_SSL */
4634 * 'http_write()' - Write a buffer to a HTTP connection.
4637 static ssize_t
/* O - Number of bytes written */
4638 http_write(http_t
*http
, /* I - HTTP connection */
4639 const char *buffer
, /* I - Buffer for data */
4640 size_t length
) /* I - Number of bytes to write */
4642 ssize_t tbytes
, /* Total bytes sent */
4643 bytes
; /* Bytes sent */
4646 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
4647 buffer
, CUPS_LLCAST length
));
4653 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4655 if (http
->timeout_cb
)
4658 struct pollfd pfd
; /* Polled file descriptor */
4660 fd_set output_set
; /* Output ready for write? */
4661 struct timeval timeout
; /* Timeout value */
4662 #endif /* HAVE_POLL */
4663 int nfds
; /* Result from select()/poll() */
4669 pfd
.events
= POLLOUT
;
4671 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4672 (errno
== EINTR
|| errno
== EAGAIN
))
4678 FD_ZERO(&output_set
);
4679 FD_SET(http
->fd
, &output_set
);
4681 timeout
.tv_sec
= http
->wait_value
/ 1000;
4682 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4684 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4687 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4688 WSAGetLastError() == WSAEWOULDBLOCK
));
4690 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4692 #endif /* HAVE_POLL */
4696 http
->error
= errno
;
4699 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4702 http
->error
= WSAEWOULDBLOCK
;
4704 http
->error
= EWOULDBLOCK
;
4714 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4716 #endif /* HAVE_SSL */
4717 bytes
= send(http
->fd
, buffer
, length
, 0);
4719 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4720 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4725 if (WSAGetLastError() == WSAEINTR
)
4727 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4729 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4732 http
->error
= WSAGetLastError();
4734 else if (WSAGetLastError() != http
->error
&&
4735 WSAGetLastError() != WSAECONNRESET
)
4737 http
->error
= WSAGetLastError();
4744 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4746 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4748 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4751 http
->error
= errno
;
4753 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4755 http
->error
= errno
;
4760 DEBUG_printf(("3http_write: error writing data (%s).",
4761 strerror(http
->error
)));
4768 length
-= (size_t)bytes
;
4772 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4775 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4782 * 'http_write_chunk()' - Write a chunked buffer.
4785 static ssize_t
/* O - Number bytes written */
4786 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4787 const char *buffer
, /* I - Buffer to write */
4788 size_t length
) /* I - Length of buffer */
4790 char header
[16]; /* Chunk header */
4791 ssize_t bytes
; /* Bytes written */
4794 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
4795 http
, buffer
, CUPS_LLCAST length
));
4798 * Write the chunk header, data, and trailer.
4801 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4802 if (http_write(http
, header
, strlen(header
)) < 0)
4804 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4808 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4810 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4814 if (http_write(http
, "\r\n", 2) < 0)
4816 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");
4825 * End of "$Id: http.c 12034 2014-07-16 19:37:34Z msweet $".