4 * HTTP routines for CUPS.
6 * Copyright 2007-2015 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)", (void *)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)", (void *)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.
396 * @since CUPS 2.0/OS 10.10@
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, encryption=%d, blocking=%d, msec=%d, cancel=%p)", host
, port
, (void *)addrlist
, family
, encryption
, blocking
, msec
, (void *)cancel
));
457 * Create the HTTP structure...
460 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
461 _HTTP_MODE_CLIENT
)) == NULL
)
465 * Optionally connect to the remote system...
468 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
472 * Could not connect to any known address - bail out!
482 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
484 * This function is now deprecated. Please use the @link httpConnect2@ function
490 http_t
* /* O - New HTTP connection */
492 const char *host
, /* I - Host to connect to */
493 int port
, /* I - Port number */
494 http_encryption_t encryption
) /* I - Type of encryption to use */
496 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
497 host
, port
, encryption
));
499 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
505 * 'httpDelete()' - Send a DELETE request to the server.
508 int /* O - Status of call (0 = success) */
509 httpDelete(http_t
*http
, /* I - HTTP connection */
510 const char *uri
) /* I - URI to delete */
512 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
517 * '_httpDisconnect()' - Disconnect a HTTP connection.
521 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
526 #endif /* HAVE_SSL */
528 httpAddrClose(NULL
, http
->fd
);
535 * 'httpEncryption()' - Set the required encryption on the link.
538 int /* O - -1 on error, 0 on success */
539 httpEncryption(http_t
*http
, /* I - HTTP connection */
540 http_encryption_t e
) /* I - New encryption preference */
542 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
548 if (http
->mode
== _HTTP_MODE_CLIENT
)
550 http
->encryption
= e
;
552 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
553 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
554 return (httpReconnect2(http
, 30000, NULL
));
555 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
556 return (http_tls_upgrade(http
));
562 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
565 http
->encryption
= e
;
566 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
567 return (_httpTLSStart(http
));
572 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
576 #endif /* HAVE_SSL */
581 * 'httpError()' - Get the last error on a connection.
584 int /* O - Error code (errno) value */
585 httpError(http_t
*http
) /* I - HTTP connection */
588 return (http
->error
);
595 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
599 http_field_t
/* O - Field index */
600 httpFieldValue(const char *name
) /* I - String name */
602 int i
; /* Looping var */
605 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
606 if (!_cups_strcasecmp(name
, http_fields
[i
]))
607 return ((http_field_t
)i
);
609 return (HTTP_FIELD_UNKNOWN
);
614 * 'httpFlush()' - Flush data from a HTTP connection.
618 httpFlush(http_t
*http
) /* I - HTTP connection */
620 char buffer
[8192]; /* Junk buffer */
621 int blocking
; /* To block or not to block */
622 http_state_t oldstate
; /* Old state */
625 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
628 * Nothing to do if we are in the "waiting" state...
631 if (http
->state
== HTTP_STATE_WAITING
)
635 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
638 blocking
= http
->blocking
;
642 * Read any data we can...
645 oldstate
= http
->state
;
646 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
649 * Restore blocking and reset the connection if we didn't get all of
650 * the remaining data...
653 http
->blocking
= blocking
;
655 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
659 * Didn't get the data back, so close the current connection.
664 http_content_coding_finish(http
);
665 #endif /* HAVE_LIBZ */
667 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
669 http
->state
= HTTP_STATE_WAITING
;
674 #endif /* HAVE_SSL */
676 httpAddrClose(NULL
, http
->fd
);
684 * 'httpFlushWrite()' - Flush data in write buffer.
686 * @since CUPS 1.2/OS X 10.5@
689 int /* O - Bytes written or -1 on error */
690 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
692 ssize_t bytes
; /* Bytes written */
695 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
697 if (!http
|| !http
->wused
)
699 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
700 "1httpFlushWrite: No connection.");
704 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
705 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
707 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
711 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
718 * 'httpFreeCredentials()' - Free an array of credentials.
723 cups_array_t
*credentials
) /* I - Array of credentials */
725 http_credential_t
*credential
; /* Credential */
728 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
730 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
732 cupsArrayRemove(credentials
, credential
);
733 free((void *)credential
->data
);
737 cupsArrayDelete(credentials
);
742 * 'httpGet()' - Send a GET request to the server.
745 int /* O - Status of call (0 = success) */
746 httpGet(http_t
*http
, /* I - HTTP connection */
747 const char *uri
) /* I - URI to get */
749 return (http_send(http
, HTTP_STATE_GET
, uri
));
754 * 'httpGetActivity()' - Get the most recent activity for a connection.
756 * The return value is the UNIX time of the last read or write.
758 * @since CUPS 2.0/OS 10.10@
761 time_t /* O - Time of last read or write */
762 httpGetActivity(http_t
*http
) /* I - HTTP connection */
764 return (http
? http
->activity
: 0);
769 * 'httpGetAuthString()' - Get the current authorization string.
771 * The authorization string is set by cupsDoAuthentication() and
772 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
773 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
776 * @since CUPS 1.3/OS X 10.5@
779 char * /* O - Authorization string */
780 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
783 return (http
->authstring
);
790 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
792 * @since CUPS 1.2/OS X 10.5@
795 int /* O - 1 if blocking, 0 if non-blocking */
796 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
798 return (http
? http
->blocking
: 0);
803 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
804 * the client and server.
806 * This function uses the value of the Accepts-Encoding HTTP header and must be
807 * called after receiving a response from the server or a request from the
808 * client. The value returned can be use in subsequent requests (for clients)
809 * or in the response (for servers) in order to compress the content stream.
811 * @since CUPS 1.7/OS X 10.9@
814 const char * /* O - Content-Coding value or
815 @code NULL@ for the identity
817 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
820 if (http
&& http
->accept_encoding
)
822 int i
; /* Looping var */
823 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
824 *start
, /* Start of coding value */
825 *end
; /* End of coding value */
826 double qvalue
; /* "qvalue" for coding */
827 struct lconv
*loc
= localeconv(); /* Locale data */
828 static const char * const codings
[] =
829 { /* Supported content codings */
836 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
838 for (start
= temp
; *start
; start
= end
)
841 * Find the end of the coding name...
846 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
852 * Grab the qvalue as needed...
855 if (!strncmp(end
, ";q=", 3))
856 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
859 * Skip past all attributes...
863 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
869 while (*end
&& isspace(*end
& 255))
873 * Check value if it matches something we support...
879 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
880 if (!strcmp(start
, codings
[i
]))
884 #endif /* HAVE_LIBZ */
891 * 'httpGetCookie()' - Get any cookie data from the response.
893 * @since CUPS 1.1.19/OS X 10.3@
896 const char * /* O - Cookie data or NULL */
897 httpGetCookie(http_t
*http
) /* I - HTTP connection */
899 return (http
? http
->cookie
: NULL
);
904 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
906 * This function returns the encryption mode for the connection. Use the
907 * @link httpIsEncrypted@ function to determine whether a TLS session has
910 * @since CUPS 2.0/OS 10.10@
913 http_encryption_t
/* O - Current encryption mode */
914 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
916 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
921 * 'httpGetExpect()' - Get the value of the Expect header, if any.
923 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
924 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
926 * @since CUPS 1.7/OS X 10.9@
929 http_status_t
/* O - Expect: status, if any */
930 httpGetExpect(http_t
*http
) /* I - HTTP connection */
933 return (HTTP_STATUS_ERROR
);
935 return (http
->expect
);
940 * 'httpGetFd()' - Get the file descriptor associated with a connection.
942 * @since CUPS 1.2/OS X 10.5@
945 int /* O - File descriptor or -1 if none */
946 httpGetFd(http_t
*http
) /* I - HTTP connection */
948 return (http
? http
->fd
: -1);
953 * 'httpGetField()' - Get a field value from a request/response.
956 const char * /* O - Field value */
957 httpGetField(http_t
*http
, /* I - HTTP connection */
958 http_field_t field
) /* I - Field to get */
960 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
965 case HTTP_FIELD_ACCEPT_ENCODING
:
966 return (http
->accept_encoding
);
968 case HTTP_FIELD_ALLOW
:
969 return (http
->allow
);
971 case HTTP_FIELD_SERVER
:
972 return (http
->server
);
974 case HTTP_FIELD_AUTHORIZATION
:
975 if (http
->field_authorization
)
978 * Special case for WWW-Authenticate: as its contents can be
979 * longer than HTTP_MAX_VALUE...
982 return (http
->field_authorization
);
986 return (http
->fields
[field
]);
992 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
994 * @since CUPS 2.0/OS 10.10@
997 http_keepalive_t
/* O - Keep-Alive state */
998 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
1000 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1005 * 'httpGetLength()' - Get the amount of data remaining from the
1006 * content-length or transfer-encoding fields.
1008 * This function is deprecated and will not return lengths larger than
1009 * 2^31 - 1; use httpGetLength2() instead.
1014 int /* O - Content length */
1015 httpGetLength(http_t
*http
) /* I - HTTP connection */
1018 * Get the read content length and return the 32-bit value.
1023 httpGetLength2(http
);
1025 return (http
->_data_remaining
);
1033 * 'httpGetLength2()' - Get the amount of data remaining from the
1034 * content-length or transfer-encoding fields.
1036 * This function returns the complete content length, even for
1037 * content larger than 2^31 - 1.
1039 * @since CUPS 1.2/OS X 10.5@
1042 off_t
/* O - Content length */
1043 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1045 off_t remaining
; /* Remaining length */
1048 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1053 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1055 DEBUG_puts("4httpGetLength2: chunked request!");
1061 * The following is a hack for HTTP servers that don't send a
1062 * Content-Length or Transfer-Encoding field...
1064 * If there is no Content-Length then the connection must close
1065 * after the transfer is complete...
1068 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1071 * Default content length is 0 for errors and certain types of operations,
1072 * and 2^31-1 for other successful requests...
1075 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1076 http
->state
== HTTP_STATE_OPTIONS
||
1077 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1078 http
->state
== HTTP_STATE_HEAD
||
1079 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1080 http
->state
== HTTP_STATE_DELETE
||
1081 http
->state
== HTTP_STATE_TRACE
||
1082 http
->state
== HTTP_STATE_CONNECT
)
1085 remaining
= 2147483647;
1087 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1091 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1092 CUPS_LLCAST remaining
));
1100 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1102 * @since CUPS 2.0/OS 10.10@
1105 size_t /* O - Number of bytes buffered */
1106 httpGetPending(http_t
*http
) /* I - HTTP connection */
1108 return (http
? (size_t)http
->wused
: 0);
1113 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1115 * @since CUPS 2.0/OS 10.10@
1118 size_t /* O - Number of bytes available */
1119 httpGetReady(http_t
*http
) /* I - HTTP connection */
1123 else if (http
->used
> 0)
1124 return ((size_t)http
->used
);
1127 return (_httpTLSPending(http
));
1128 #endif /* HAVE_SSL */
1135 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1136 * body or current chunk.
1138 * The @link httpIsChunked@ function can be used to determine whether the
1139 * message body is chunked or fixed-length.
1141 * @since CUPS 2.0/OS 10.10@
1144 size_t /* O - Remaining bytes */
1145 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1147 return (http
? (size_t)http
->data_remaining
: 0);
1152 * 'httpGets()' - Get a line of text from a HTTP connection.
1155 char * /* O - Line or NULL */
1156 httpGets(char *line
, /* I - Line to read into */
1157 int length
, /* I - Max length of buffer */
1158 http_t
*http
) /* I - HTTP connection */
1160 char *lineptr
, /* Pointer into line */
1161 *lineend
, /* End of line */
1162 *bufptr
, /* Pointer into input buffer */
1163 *bufend
; /* Pointer to end of buffer */
1164 ssize_t bytes
; /* Number of bytes read */
1165 int eol
; /* End-of-line? */
1168 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1170 if (!http
|| !line
|| length
<= 1)
1174 * Read a line from the buffer...
1179 lineend
= line
+ length
- 1;
1182 while (lineptr
< lineend
)
1185 * Pre-load the buffer as needed...
1194 while (http
->used
== 0)
1197 * No newline; see if there is more data to be read...
1200 while (!_httpWait(http
, http
->wait_value
, 1))
1202 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1205 DEBUG_puts("3httpGets: Timed out!");
1207 http
->error
= WSAETIMEDOUT
;
1209 http
->error
= ETIMEDOUT
;
1214 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1216 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1221 * Nope, can't get a line this time...
1225 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1227 if (WSAGetLastError() == WSAEINTR
)
1229 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1231 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1234 http
->error
= WSAGetLastError();
1236 else if (WSAGetLastError() != http
->error
)
1238 http
->error
= WSAGetLastError();
1243 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1247 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1249 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1251 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1254 http
->error
= errno
;
1256 else if (errno
!= http
->error
)
1258 http
->error
= errno
;
1265 else if (bytes
== 0)
1267 http
->error
= EPIPE
;
1273 * Yup, update the amount used...
1276 http
->used
+= (int)bytes
;
1280 * Now copy as much of the current line as possible...
1283 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1284 lineptr
< lineend
&& bufptr
< bufend
;)
1286 if (*bufptr
== 0x0a)
1292 else if (*bufptr
== 0x0d)
1295 *lineptr
++ = *bufptr
++;
1298 http
->used
-= (int)(bufptr
- http
->buffer
);
1300 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1308 http
->activity
= time(NULL
);
1312 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1318 DEBUG_puts("3httpGets: No new line available!");
1325 * 'httpGetState()' - Get the current state of the HTTP request.
1328 http_state_t
/* O - HTTP state */
1329 httpGetState(http_t
*http
) /* I - HTTP connection */
1331 return (http
? http
->state
: HTTP_STATE_ERROR
);
1336 * 'httpGetStatus()' - Get the status of the last HTTP request.
1338 * @since CUPS 1.2/OS X 10.5@
1341 http_status_t
/* O - HTTP status */
1342 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1344 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1349 * 'httpGetSubField()' - Get a sub-field value.
1354 char * /* O - Value or NULL */
1355 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1356 http_field_t field
, /* I - Field index */
1357 const char *name
, /* I - Name of sub-field */
1358 char *value
) /* O - Value string */
1360 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1365 * 'httpGetSubField2()' - Get a sub-field value.
1367 * @since CUPS 1.2/OS X 10.5@
1370 char * /* O - Value or NULL */
1371 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1372 http_field_t field
, /* I - Field index */
1373 const char *name
, /* I - Name of sub-field */
1374 char *value
, /* O - Value string */
1375 int valuelen
) /* I - Size of value buffer */
1377 const char *fptr
; /* Pointer into field */
1378 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1379 *ptr
, /* Pointer into string buffer */
1380 *end
; /* End of value buffer */
1382 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1384 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1385 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1388 end
= value
+ valuelen
- 1;
1390 for (fptr
= http
->fields
[field
]; *fptr
;)
1393 * Skip leading whitespace...
1396 while (_cups_isspace(*fptr
))
1406 * Get the sub-field name...
1410 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1411 ptr
< (temp
+ sizeof(temp
) - 1);
1416 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1419 * Skip trailing chars up to the '='...
1422 while (_cups_isspace(*fptr
))
1432 * Skip = and leading whitespace...
1437 while (_cups_isspace(*fptr
))
1443 * Read quoted string...
1446 for (ptr
= value
, fptr
++;
1447 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1452 while (*fptr
&& *fptr
!= '\"')
1461 * Read unquoted string...
1465 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1470 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1474 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1477 * See if this is the one...
1480 if (!strcmp(name
, temp
))
1482 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1489 DEBUG_puts("3httpGetSubField2: Returning NULL");
1496 * 'httpGetVersion()' - Get the HTTP version at the other end.
1499 http_version_t
/* O - Version number */
1500 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1502 return (http
? http
->version
: HTTP_VERSION_1_0
);
1507 * 'httpHead()' - Send a HEAD request to the server.
1510 int /* O - Status of call (0 = success) */
1511 httpHead(http_t
*http
, /* I - HTTP connection */
1512 const char *uri
) /* I - URI for head */
1514 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1515 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1520 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1521 * default HTTP proxy (if any).
1525 httpInitialize(void)
1527 static int initialized
= 0; /* Have we been called before? */
1529 WSADATA winsockdata
; /* WinSock data */
1536 _cupsGlobalUnlock();
1541 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1543 #elif !defined(SO_NOSIGPIPE)
1545 * Ignore SIGPIPE signals...
1549 sigset(SIGPIPE
, SIG_IGN
);
1551 # elif defined(HAVE_SIGACTION)
1552 struct sigaction action
; /* POSIX sigaction data */
1555 memset(&action
, 0, sizeof(action
));
1556 action
.sa_handler
= SIG_IGN
;
1557 sigaction(SIGPIPE
, &action
, NULL
);
1560 signal(SIGPIPE
, SIG_IGN
);
1561 # endif /* !SO_NOSIGPIPE */
1565 _httpTLSInitialize();
1566 # endif /* HAVE_SSL */
1569 _cupsGlobalUnlock();
1574 * 'httpIsChunked()' - Report whether a message body is chunked.
1576 * This function returns non-zero if the message body is composed of
1577 * variable-length chunks.
1579 * @since CUPS 2.0/OS 10.10@
1582 int /* O - 1 if chunked, 0 if not */
1583 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1585 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1590 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1592 * This function returns non-zero if the connection is currently encrypted.
1594 * @since CUPS 2.0/OS 10.10@
1597 int /* O - 1 if encrypted, 0 if not */
1598 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1600 return (http
? http
->tls
!= NULL
: 0);
1605 * 'httpOptions()' - Send an OPTIONS request to the server.
1608 int /* O - Status of call (0 = success) */
1609 httpOptions(http_t
*http
, /* I - HTTP connection */
1610 const char *uri
) /* I - URI for options */
1612 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1617 * 'httpPeek()' - Peek at data from a HTTP connection.
1619 * This function copies available data from the given HTTP connection, reading
1620 * a buffer as needed. The data is still available for reading using
1621 * @link httpRead@ or @link httpRead2@.
1623 * For non-blocking connections the usual timeouts apply.
1625 * @since CUPS 1.7/OS X 10.9@
1628 ssize_t
/* O - Number of bytes copied */
1629 httpPeek(http_t
*http
, /* I - HTTP connection */
1630 char *buffer
, /* I - Buffer for data */
1631 size_t length
) /* I - Maximum number of bytes */
1633 ssize_t bytes
; /* Bytes read */
1634 char len
[32]; /* Length string */
1637 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1639 if (http
== NULL
|| buffer
== NULL
)
1642 http
->activity
= time(NULL
);
1648 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1649 http
->data_remaining
<= 0)
1651 DEBUG_puts("2httpPeek: Getting chunk length...");
1653 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1655 DEBUG_puts("1httpPeek: Could not get length!");
1661 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1662 if (!httpGets(len
, sizeof(len
), http
))
1664 DEBUG_puts("1httpPeek: Could not get chunk length.");
1669 http
->data_remaining
= strtoll(len
, NULL
, 16);
1671 if (http
->data_remaining
< 0)
1673 DEBUG_puts("1httpPeek: Negative chunk length!");
1678 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1679 CUPS_LLCAST http
->data_remaining
));
1681 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1684 * A zero-length chunk ends a transfer; unless we are reading POST
1689 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1690 http_content_coding_finish(http
);
1691 #endif /* HAVE_LIBZ */
1693 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1694 httpGets(len
, sizeof(len
), http
);
1696 if (http
->state
== HTTP_STATE_POST_RECV
)
1699 http
->state
= HTTP_STATE_STATUS
;
1701 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1702 httpStateString(http
->state
)));
1705 * Prevent future reads for this request...
1708 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1712 else if (length
> (size_t)http
->data_remaining
)
1713 length
= (size_t)http
->data_remaining
;
1716 if (http
->used
== 0 &&
1717 (http
->coding
== _HTTP_CODING_IDENTITY
||
1718 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1720 if (http
->used
== 0)
1721 #endif /* HAVE_LIBZ */
1724 * Buffer small reads for better performance...
1727 ssize_t buflen
; /* Length of read for buffer */
1729 if (!http
->blocking
)
1731 while (!httpWait(http
, http
->wait_value
))
1733 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1740 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1741 buflen
= sizeof(http
->buffer
);
1743 buflen
= (ssize_t
)http
->data_remaining
;
1745 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1746 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1748 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1749 CUPS_LLCAST bytes
));
1753 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1756 http
->used
= (int)bytes
;
1761 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1763 # ifdef HAVE_INFLATECOPY
1764 int zerr
; /* Decompressor error */
1765 z_stream stream
; /* Copy of decompressor stream */
1767 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1769 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1770 /* Number of bytes to copy */
1772 if (http
->stream
.avail_in
> 0 &&
1773 http
->stream
.next_in
> http
->sbuffer
)
1774 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1776 http
->stream
.next_in
= http
->sbuffer
;
1778 if (buflen
> (size_t)http
->data_remaining
)
1779 buflen
= (size_t)http
->data_remaining
;
1781 if (buflen
> (size_t)http
->used
)
1782 buflen
= (size_t)http
->used
;
1784 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1785 "decompression buffer.", (int)buflen
));
1787 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1788 http
->stream
.avail_in
+= buflen
;
1789 http
->used
-= (int)buflen
;
1790 http
->data_remaining
-= (off_t
)buflen
;
1793 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1796 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1797 (int)http
->stream
.avail_in
));
1799 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1801 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1802 http
->error
= ENOMEM
;
1806 stream
.next_out
= (Bytef
*)buffer
;
1807 stream
.avail_out
= (uInt
)length
;
1809 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1810 inflateEnd(&stream
);
1814 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1816 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1823 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1826 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1827 "work with compressed streams.");
1829 # endif /* HAVE_INFLATECOPY */
1832 #endif /* HAVE_LIBZ */
1835 if (length
> (size_t)http
->used
)
1836 length
= (size_t)http
->used
;
1838 bytes
= (ssize_t
)length
;
1840 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1843 memcpy(buffer
, http
->buffer
, length
);
1851 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1854 http
->error
= WSAGetLastError();
1856 if (errno
== EINTR
|| errno
== EAGAIN
)
1859 http
->error
= errno
;
1862 else if (bytes
== 0)
1864 http
->error
= EPIPE
;
1873 * 'httpPost()' - Send a POST request to the server.
1876 int /* O - Status of call (0 = success) */
1877 httpPost(http_t
*http
, /* I - HTTP connection */
1878 const char *uri
) /* I - URI for post */
1880 return (http_send(http
, HTTP_STATE_POST
, uri
));
1885 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1890 int /* O - Number of bytes written */
1891 httpPrintf(http_t
*http
, /* I - HTTP connection */
1892 const char *format
, /* I - printf-style format string */
1893 ...) /* I - Additional args as needed */
1895 ssize_t bytes
; /* Number of bytes to write */
1896 char buf
[16384]; /* Buffer for formatted string */
1897 va_list ap
; /* Variable argument pointer */
1900 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1902 va_start(ap
, format
);
1903 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1906 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1908 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1909 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1914 DEBUG_puts("4httpPrintf: flushing existing data...");
1916 if (httpFlushWrite(http
) < 0)
1920 return ((int)http_write(http
, buf
, (size_t)bytes
));
1926 * 'httpPut()' - Send a PUT request to the server.
1929 int /* O - Status of call (0 = success) */
1930 httpPut(http_t
*http
, /* I - HTTP connection */
1931 const char *uri
) /* I - URI to put */
1933 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1934 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1939 * 'httpRead()' - Read data from a HTTP connection.
1941 * This function is deprecated. Use the httpRead2() function which can
1942 * read more than 2GB of data.
1947 int /* O - Number of bytes read */
1948 httpRead(http_t
*http
, /* I - HTTP connection */
1949 char *buffer
, /* I - Buffer for data */
1950 int length
) /* I - Maximum number of bytes */
1952 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1957 * 'httpRead2()' - Read data from a HTTP connection.
1959 * @since CUPS 1.2/OS X 10.5@
1962 ssize_t
/* O - Number of bytes read */
1963 httpRead2(http_t
*http
, /* I - HTTP connection */
1964 char *buffer
, /* I - Buffer for data */
1965 size_t length
) /* I - Maximum number of bytes */
1967 ssize_t bytes
; /* Bytes read */
1971 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT
, (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->coding
, http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1973 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
") data_encoding=%d data_remaining=" CUPS_LLFMT
, (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1974 #endif /* HAVE_LIBZ */
1976 if (http
== NULL
|| buffer
== NULL
)
1979 http
->activity
= time(NULL
);
1986 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1990 if (http
->stream
.avail_in
> 0)
1992 int zerr
; /* Decompressor error */
1994 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1995 (int)http
->stream
.avail_in
, (int)length
));
1997 http
->stream
.next_out
= (Bytef
*)buffer
;
1998 http
->stream
.avail_out
= (uInt
)length
;
2000 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2002 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2004 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2011 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2013 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2014 http
->stream
.avail_in
, http
->stream
.avail_out
,
2022 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2023 /* Additional bytes for buffer */
2027 if (http
->stream
.avail_in
> 0 &&
2028 http
->stream
.next_in
> http
->sbuffer
)
2029 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2031 http
->stream
.next_in
= http
->sbuffer
;
2033 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2034 "decompression buffer.", (int)buflen
));
2036 if (http
->data_remaining
> 0)
2038 if (buflen
> http
->data_remaining
)
2039 buflen
= (ssize_t
)http
->data_remaining
;
2041 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2043 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2044 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2050 else if (bytes
== 0)
2053 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2054 "decompression buffer.", CUPS_LLCAST bytes
));
2056 http
->data_remaining
-= bytes
;
2057 http
->stream
.avail_in
+= (uInt
)bytes
;
2059 if (http
->data_remaining
<= 0 &&
2060 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2063 * Read the trailing blank line now...
2066 char len
[32]; /* Length string */
2068 httpGets(len
, sizeof(len
), http
);
2080 #endif /* HAVE_LIBZ */
2081 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2083 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2085 http
->data_remaining
-= bytes
;
2087 if (http
->data_remaining
<= 0)
2090 * Read the trailing blank line now...
2093 char len
[32]; /* Length string */
2095 httpGets(len
, sizeof(len
), http
);
2099 else if (http
->data_remaining
<= 0)
2102 * No more data to read...
2109 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2112 if (length
> (size_t)http
->data_remaining
)
2113 length
= (size_t)http
->data_remaining
;
2115 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2117 http
->data_remaining
-= bytes
;
2119 if (http
->data_remaining
<= 0 &&
2120 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2123 * Read the trailing blank line now...
2126 char len
[32]; /* Length string */
2128 httpGets(len
, sizeof(len
), http
);
2135 (http
->coding
== _HTTP_CODING_IDENTITY
||
2136 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2137 #endif /* HAVE_LIBZ */
2138 ((http
->data_remaining
<= 0 &&
2139 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2140 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2143 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2144 http_content_coding_finish(http
);
2145 #endif /* HAVE_LIBZ */
2147 if (http
->state
== HTTP_STATE_POST_RECV
)
2149 else if (http
->state
== HTTP_STATE_GET_SEND
||
2150 http
->state
== HTTP_STATE_POST_SEND
)
2151 http
->state
= HTTP_STATE_WAITING
;
2153 http
->state
= HTTP_STATE_STATUS
;
2155 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2156 httpStateString(http
->state
)));
2164 * 'httpReadRequest()' - Read a HTTP request from a connection.
2166 * @since CUPS 1.7/OS X 10.9@
2169 http_state_t
/* O - New state of connection */
2170 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2171 char *uri
, /* I - URI buffer */
2172 size_t urilen
) /* I - Size of URI buffer */
2174 char line
[4096], /* HTTP request line */
2175 *req_method
, /* HTTP request method */
2176 *req_uri
, /* HTTP request URI */
2177 *req_version
; /* HTTP request version number string */
2181 * Range check input...
2184 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2189 if (!http
|| !uri
|| urilen
< 1)
2191 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2192 return (HTTP_STATE_ERROR
);
2194 else if (http
->state
!= HTTP_STATE_WAITING
)
2196 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2197 httpStateString(http
->state
)));
2198 return (HTTP_STATE_ERROR
);
2205 httpClearFields(http
);
2207 http
->activity
= time(NULL
);
2208 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2209 http
->data_remaining
= 0;
2210 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2211 http
->status
= HTTP_STATUS_OK
;
2212 http
->version
= HTTP_VERSION_1_1
;
2215 * Read a line from the socket...
2218 if (!httpGets(line
, sizeof(line
), http
))
2220 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2221 return (HTTP_STATE_ERROR
);
2226 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2227 return (HTTP_STATE_WAITING
);
2230 DEBUG_printf(("1httpReadRequest: %s", line
));
2239 while (*req_uri
&& !isspace(*req_uri
& 255))
2244 DEBUG_puts("1httpReadRequest: No request URI.");
2245 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2246 return (HTTP_STATE_ERROR
);
2251 while (*req_uri
&& isspace(*req_uri
& 255))
2254 req_version
= req_uri
;
2256 while (*req_version
&& !isspace(*req_version
& 255))
2261 DEBUG_puts("1httpReadRequest: No request protocol version.");
2262 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2263 return (HTTP_STATE_ERROR
);
2266 *req_version
++ = '\0';
2268 while (*req_version
&& isspace(*req_version
& 255))
2275 if (!strcmp(req_method
, "OPTIONS"))
2276 http
->state
= HTTP_STATE_OPTIONS
;
2277 else if (!strcmp(req_method
, "GET"))
2278 http
->state
= HTTP_STATE_GET
;
2279 else if (!strcmp(req_method
, "HEAD"))
2280 http
->state
= HTTP_STATE_HEAD
;
2281 else if (!strcmp(req_method
, "POST"))
2282 http
->state
= HTTP_STATE_POST
;
2283 else if (!strcmp(req_method
, "PUT"))
2284 http
->state
= HTTP_STATE_PUT
;
2285 else if (!strcmp(req_method
, "DELETE"))
2286 http
->state
= HTTP_STATE_DELETE
;
2287 else if (!strcmp(req_method
, "TRACE"))
2288 http
->state
= HTTP_STATE_TRACE
;
2289 else if (!strcmp(req_method
, "CONNECT"))
2290 http
->state
= HTTP_STATE_CONNECT
;
2293 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2294 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2295 return (HTTP_STATE_UNKNOWN_METHOD
);
2298 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2299 httpStateString(http
->state
)));
2301 if (!strcmp(req_version
, "HTTP/1.0"))
2303 http
->version
= HTTP_VERSION_1_0
;
2304 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2306 else if (!strcmp(req_version
, "HTTP/1.1"))
2308 http
->version
= HTTP_VERSION_1_1
;
2309 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2313 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2314 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2315 return (HTTP_STATE_UNKNOWN_VERSION
);
2318 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2319 strlcpy(uri
, req_uri
, urilen
);
2321 return (http
->state
);
2326 * 'httpReconnect()' - Reconnect to a HTTP server.
2328 * This function is deprecated. Please use the @link httpReconnect2@ function
2334 int /* O - 0 on success, non-zero on failure */
2335 httpReconnect(http_t
*http
) /* I - HTTP connection */
2337 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2339 return (httpReconnect2(http
, 30000, NULL
));
2344 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2348 int /* O - 0 on success, non-zero on failure */
2349 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2350 int msec
, /* I - Timeout in milliseconds */
2351 int *cancel
) /* I - Pointer to "cancel" variable */
2353 http_addrlist_t
*addr
; /* Connected address */
2355 http_addrlist_t
*current
; /* Current address */
2356 char temp
[256]; /* Temporary address string */
2360 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2364 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2371 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2374 #endif /* HAVE_SSL */
2377 * Close any previously open socket...
2382 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2384 httpAddrClose(NULL
, http
->fd
);
2390 * Reset all state (except fields, which may be reused)...
2393 http
->state
= HTTP_STATE_WAITING
;
2394 http
->version
= HTTP_VERSION_1_1
;
2395 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2396 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2397 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2398 http
->_data_remaining
= 0;
2400 http
->data_remaining
= 0;
2401 http
->hostaddr
= NULL
;
2405 * Connect to the server...
2409 for (current
= http
->addrlist
; current
; current
= current
->next
)
2410 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2411 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2412 httpAddrPort(&(current
->addr
))));
2415 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2418 * Unable to connect...
2422 http
->error
= WSAGetLastError();
2424 http
->error
= errno
;
2426 http
->status
= HTTP_STATUS_ERROR
;
2428 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2429 strerror(http
->error
)));
2434 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2436 if (http
->timeout_value
> 0)
2437 http_set_timeout(http
->fd
, http
->timeout_value
);
2439 http
->hostaddr
= &(addr
->addr
);
2443 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2446 * Always do encryption via SSL.
2449 if (_httpTLSStart(http
) != 0)
2451 httpAddrClose(NULL
, http
->fd
);
2456 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2457 return (http_tls_upgrade(http
));
2458 #endif /* HAVE_SSL */
2460 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2461 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2462 httpAddrPort(http
->hostaddr
)));
2469 * 'httpSetAuthString()' - Set the current authorization string.
2471 * This function just stores a copy of the current authorization string in
2472 * the HTTP connection object. You must still call httpSetField() to set
2473 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
2474 * httpHead(), httpOptions(), httpPost, or httpPut().
2476 * @since CUPS 1.3/OS X 10.5@
2480 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2481 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2482 const char *data
) /* I - Auth data (NULL for none) */
2485 * Range check input...
2491 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2492 free(http
->authstring
);
2494 http
->authstring
= http
->_authstring
;
2499 * Set the current authorization string...
2502 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2505 if (len
> sizeof(http
->_authstring
))
2507 if ((temp
= malloc(len
)) == NULL
)
2508 len
= sizeof(http
->_authstring
);
2510 http
->authstring
= temp
;
2514 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2516 strlcpy(http
->authstring
, scheme
, len
);
2521 * Clear the current authorization string...
2524 http
->_authstring
[0] = '\0';
2530 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2533 * @since CUPS 1.5/OS X 10.7@
2536 int /* O - Status of call (0 = success) */
2537 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2538 cups_array_t
*credentials
) /* I - Array of credentials */
2540 if (!http
|| cupsArrayCount(credentials
) < 1)
2544 _httpFreeCredentials(http
->tls_credentials
);
2546 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2547 #endif /* HAVE_SSL */
2549 return (http
->tls_credentials
? 0 : -1);
2554 * 'httpSetCookie()' - Set the cookie value(s).
2556 * @since CUPS 1.1.19/OS X 10.3@
2560 httpSetCookie(http_t
*http
, /* I - Connection */
2561 const char *cookie
) /* I - Cookie string */
2570 http
->cookie
= strdup(cookie
);
2572 http
->cookie
= NULL
;
2577 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2579 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2580 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2582 * @since CUPS 1.7/OS X 10.9@
2586 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2587 http_field_t field
, /* I - Field index */
2588 const char *value
)/* I - Value */
2590 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2597 case HTTP_FIELD_ACCEPT_ENCODING
:
2598 if (http
->default_accept_encoding
)
2599 _cupsStrFree(http
->default_accept_encoding
);
2601 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2604 case HTTP_FIELD_SERVER
:
2605 if (http
->default_server
)
2606 _cupsStrFree(http
->default_server
);
2608 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2611 case HTTP_FIELD_USER_AGENT
:
2612 if (http
->default_user_agent
)
2613 _cupsStrFree(http
->default_user_agent
);
2615 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2619 DEBUG_puts("1httpSetDefaultField: Ignored.");
2626 * 'httpSetExpect()' - Set the Expect: header in a request.
2628 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2631 * @since CUPS 1.2/OS X 10.5@
2635 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2636 http_status_t expect
) /* I - HTTP status to expect
2637 (@code HTTP_STATUS_CONTINUE@) */
2639 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2642 http
->expect
= expect
;
2647 * 'httpSetField()' - Set the value of an HTTP header.
2651 httpSetField(http_t
*http
, /* I - HTTP connection */
2652 http_field_t field
, /* I - Field index */
2653 const char *value
) /* I - Value */
2655 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2658 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2659 field
>= HTTP_FIELD_MAX
||
2665 case HTTP_FIELD_ACCEPT_ENCODING
:
2666 if (http
->accept_encoding
)
2667 _cupsStrFree(http
->accept_encoding
);
2669 http
->accept_encoding
= _cupsStrAlloc(value
);
2672 case HTTP_FIELD_ALLOW
:
2674 _cupsStrFree(http
->allow
);
2676 http
->allow
= _cupsStrAlloc(value
);
2679 case HTTP_FIELD_SERVER
:
2681 _cupsStrFree(http
->server
);
2683 http
->server
= _cupsStrAlloc(value
);
2686 case HTTP_FIELD_WWW_AUTHENTICATE
:
2687 /* CUPS STR #4503 - don't override WWW-Authenticate for unknown auth schemes */
2688 if (http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
][0] &&
2689 _cups_strncasecmp(value
, "Basic ", 6) &&
2690 _cups_strncasecmp(value
, "Digest ", 7) &&
2691 _cups_strncasecmp(value
, "Negotiate ", 10))
2693 DEBUG_printf(("1httpSetField: Ignoring unknown auth scheme in \"%s\".", value
));
2697 /* Fall through to copy */
2700 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2704 if (field
== HTTP_FIELD_AUTHORIZATION
)
2707 * Special case for Authorization: as its contents can be
2708 * longer than HTTP_MAX_VALUE
2711 if (http
->field_authorization
)
2712 free(http
->field_authorization
);
2714 http
->field_authorization
= strdup(value
);
2716 else if (field
== HTTP_FIELD_HOST
)
2719 * Special-case for Host: as we don't want a trailing "." on the hostname and
2720 * need to bracket IPv6 numeric addresses.
2723 char *ptr
= strchr(value
, ':');
2725 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2728 * Bracket IPv6 numeric addresses...
2730 * This is slightly inefficient (basically copying twice), but is an edge
2731 * case and not worth optimizing...
2734 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2735 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2740 * Check for a trailing dot on the hostname...
2743 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2747 ptr
+= strlen(ptr
) - 1;
2755 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2756 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
2758 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
2759 http_content_coding_start(http
, value
);
2761 #endif /* HAVE_LIBZ */
2766 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2768 * @since CUPS 2.0/OS 10.10@
2773 http_t
*http
, /* I - HTTP connection */
2774 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2777 http
->keep_alive
= keep_alive
;
2782 * 'httpSetLength()' - Set the content-length and content-encoding.
2784 * @since CUPS 1.2/OS X 10.5@
2788 httpSetLength(http_t
*http
, /* I - HTTP connection */
2789 size_t length
) /* I - Length (0 for chunked) */
2791 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2798 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2800 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2804 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2805 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2806 CUPS_LLFMT
, CUPS_LLCAST length
);
2812 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2814 * The optional timeout callback receives both the HTTP connection and a user
2815 * data pointer and must return 1 to continue or 0 to error (time) out.
2817 * @since CUPS 1.5/OS X 10.7@
2822 http_t
*http
, /* I - HTTP connection */
2823 double timeout
, /* I - Number of seconds for timeout,
2824 must be greater than 0 */
2825 http_timeout_cb_t cb
, /* I - Callback function or NULL */
2826 void *user_data
) /* I - User data pointer */
2828 if (!http
|| timeout
<= 0.0)
2831 http
->timeout_cb
= cb
;
2832 http
->timeout_data
= user_data
;
2833 http
->timeout_value
= timeout
;
2836 http_set_timeout(http
->fd
, timeout
);
2838 http_set_wait(http
);
2843 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2845 * @since CUPS 2.0/OS 10.10@
2849 httpShutdown(http_t
*http
) /* I - HTTP connection */
2851 if (!http
|| http
->fd
< 0)
2857 #endif /* HAVE_SSL */
2860 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2862 shutdown(http
->fd
, SHUT_RD
);
2868 * 'httpTrace()' - Send an TRACE request to the server.
2871 int /* O - Status of call (0 = success) */
2872 httpTrace(http_t
*http
, /* I - HTTP connection */
2873 const char *uri
) /* I - URI for trace */
2875 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2880 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2882 * Note: Unlike httpUpdate(), this function does not flush pending write data
2883 * and only retrieves a single status line from the HTTP connection.
2886 int /* O - 1 to continue, 0 to stop */
2887 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2888 http_status_t
*status
) /* O - Current HTTP status */
2890 char line
[32768], /* Line from connection... */
2891 *value
; /* Pointer to value on line */
2892 http_field_t field
; /* Field index */
2893 int major
, minor
; /* HTTP version numbers */
2896 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2899 * Grab a single line from the connection...
2902 if (!httpGets(line
, sizeof(line
), http
))
2904 *status
= HTTP_STATUS_ERROR
;
2908 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2910 if (line
[0] == '\0')
2913 * Blank line means the start of the data section (if any). Return
2914 * the result code, too...
2916 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2917 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2921 if (http
->status
== HTTP_STATUS_CONTINUE
)
2923 *status
= http
->status
;
2927 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2928 http
->digest_tries
= 0;
2931 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2933 if (_httpTLSStart(http
) != 0)
2935 httpAddrClose(NULL
, http
->fd
);
2937 *status
= http
->status
= HTTP_STATUS_ERROR
;
2941 *status
= HTTP_STATUS_CONTINUE
;
2944 #endif /* HAVE_SSL */
2946 if (http_set_length(http
) < 0)
2948 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2949 http
->error
= EINVAL
;
2950 http
->status
= *status
= HTTP_STATUS_ERROR
;
2954 switch (http
->state
)
2956 case HTTP_STATE_GET
:
2957 case HTTP_STATE_POST
:
2958 case HTTP_STATE_POST_RECV
:
2959 case HTTP_STATE_PUT
:
2962 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2963 httpStateString(http
->state
)));
2965 case HTTP_STATE_POST_SEND
:
2966 case HTTP_STATE_HEAD
:
2970 http
->state
= HTTP_STATE_WAITING
;
2972 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2977 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2978 http_content_coding_start(http
,
2979 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2980 #endif /* HAVE_LIBZ */
2982 *status
= http
->status
;
2985 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2988 * Got the beginning of a response...
2991 int intstatus
; /* Status value as an integer */
2993 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2995 *status
= http
->status
= HTTP_STATUS_ERROR
;
2999 httpClearFields(http
);
3001 http
->version
= (http_version_t
)(major
* 100 + minor
);
3002 *status
= http
->status
= (http_status_t
)intstatus
;
3004 else if ((value
= strchr(line
, ':')) != NULL
)
3011 while (_cups_isspace(*value
))
3014 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3017 * Be tolerants of servers that send unknown attribute fields...
3020 if (!_cups_strcasecmp(line
, "expect"))
3023 * "Expect: 100-continue" or similar...
3026 http
->expect
= (http_status_t
)atoi(value
);
3028 else if (!_cups_strcasecmp(line
, "cookie"))
3031 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3034 httpSetCookie(http
, value
);
3036 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3037 httpSetField(http
, field
, value
);
3040 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3045 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3046 http
->error
= EINVAL
;
3047 http
->status
= *status
= HTTP_STATUS_ERROR
;
3056 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3059 http_status_t
/* O - HTTP status */
3060 httpUpdate(http_t
*http
) /* I - HTTP connection */
3062 http_status_t status
; /* Request status */
3065 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
3068 * Flush pending data, if any...
3073 DEBUG_puts("2httpUpdate: flushing buffer...");
3075 if (httpFlushWrite(http
) < 0)
3076 return (HTTP_STATUS_ERROR
);
3080 * If we haven't issued any commands, then there is nothing to "update"...
3083 if (http
->state
== HTTP_STATE_WAITING
)
3084 return (HTTP_STATUS_CONTINUE
);
3087 * Grab all of the lines we can from the connection...
3090 while (_httpUpdate(http
, &status
));
3093 * See if there was an error...
3096 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3098 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3099 return (http
->status
);
3104 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3105 strerror(http
->error
)));
3106 http
->status
= HTTP_STATUS_ERROR
;
3107 return (HTTP_STATUS_ERROR
);
3111 * Return the current status...
3119 * '_httpWait()' - Wait for data available on a connection (no flush).
3122 int /* O - 1 if data is available, 0 otherwise */
3123 _httpWait(http_t
*http
, /* I - HTTP connection */
3124 int msec
, /* I - Milliseconds to wait */
3125 int usessl
) /* I - Use SSL context? */
3128 struct pollfd pfd
; /* Polled file descriptor */
3130 fd_set input_set
; /* select() input set */
3131 struct timeval timeout
; /* Timeout */
3132 #endif /* HAVE_POLL */
3133 int nfds
; /* Result from select()/poll() */
3136 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3140 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3145 * Check the SSL/TLS buffers for data first...
3149 if (http
->tls
&& _httpTLSPending(http
))
3151 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3154 #endif /* HAVE_SSL */
3157 * Then try doing a select() or poll() to poll the socket...
3162 pfd
.events
= POLLIN
;
3166 nfds
= poll(&pfd
, 1, msec
);
3168 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3173 FD_ZERO(&input_set
);
3174 FD_SET(http
->fd
, &input_set
);
3176 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3180 timeout
.tv_sec
= msec
/ 1000;
3181 timeout
.tv_usec
= (msec
% 1000) * 1000;
3183 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3186 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3188 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3191 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3192 WSAGetLastError() == WSAEWOULDBLOCK
));
3194 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3196 #endif /* HAVE_POLL */
3198 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3206 * 'httpWait()' - Wait for data available on a connection.
3208 * @since CUPS 1.1.19/OS X 10.3@
3211 int /* O - 1 if data is available, 0 otherwise */
3212 httpWait(http_t
*http
, /* I - HTTP connection */
3213 int msec
) /* I - Milliseconds to wait */
3216 * First see if there is data in the buffer...
3219 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3226 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3231 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3233 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3236 #endif /* HAVE_LIBZ */
3239 * Flush pending data, if any...
3244 DEBUG_puts("3httpWait: Flushing write buffer.");
3246 if (httpFlushWrite(http
) < 0)
3251 * If not, check the SSL/TLS buffers and do a select() on the connection...
3254 return (_httpWait(http
, msec
, 1));
3259 * 'httpWrite()' - Write data to a HTTP connection.
3261 * This function is deprecated. Use the httpWrite2() function which can
3262 * write more than 2GB of data.
3267 int /* O - Number of bytes written */
3268 httpWrite(http_t
*http
, /* I - HTTP connection */
3269 const char *buffer
, /* I - Buffer for data */
3270 int length
) /* I - Number of bytes to write */
3272 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3277 * 'httpWrite2()' - Write data to a HTTP connection.
3279 * @since CUPS 1.2/OS X 10.5@
3282 ssize_t
/* O - Number of bytes written */
3283 httpWrite2(http_t
*http
, /* I - HTTP connection */
3284 const char *buffer
, /* I - Buffer for data */
3285 size_t length
) /* I - Number of bytes to write */
3287 ssize_t bytes
; /* Bytes written */
3290 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3293 * Range check input...
3296 if (!http
|| !buffer
)
3298 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3303 * Mark activity on the connection...
3306 http
->activity
= time(NULL
);
3309 * Buffer small writes for better performance...
3313 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3315 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3319 http_content_coding_finish(http
);
3324 size_t slen
; /* Bytes to write */
3325 ssize_t sret
; /* Bytes written */
3327 http
->stream
.next_in
= (Bytef
*)buffer
;
3328 http
->stream
.avail_in
= (uInt
)length
;
3330 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3332 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3334 if (http
->stream
.avail_out
> 0)
3337 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3339 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3341 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3342 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3344 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3350 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3354 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3355 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3358 bytes
= (ssize_t
)length
;
3362 #endif /* HAVE_LIBZ */
3365 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3367 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3368 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3370 httpFlushWrite(http
);
3373 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3376 * Write to buffer...
3379 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3380 CUPS_LLCAST length
));
3382 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3383 http
->wused
+= (int)length
;
3384 bytes
= (ssize_t
)length
;
3389 * Otherwise write the data directly...
3392 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3393 CUPS_LLCAST length
));
3395 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3396 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3398 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3400 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3401 CUPS_LLCAST bytes
));
3404 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3405 http
->data_remaining
-= bytes
;
3411 * Handle end-of-request processing...
3414 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3415 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3418 * Finished with the transfer; unless we are sending POST or PUT
3423 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3424 http_content_coding_finish(http
);
3425 #endif /* HAVE_LIBZ */
3429 if (httpFlushWrite(http
) < 0)
3433 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3436 * Send a 0-length chunk at the end of the request...
3439 http_write(http
, "0\r\n\r\n", 5);
3442 * Reset the data state...
3445 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3446 http
->data_remaining
= 0;
3449 if (http
->state
== HTTP_STATE_POST_RECV
)
3451 else if (http
->state
== HTTP_STATE_POST_SEND
||
3452 http
->state
== HTTP_STATE_GET_SEND
)
3453 http
->state
= HTTP_STATE_WAITING
;
3455 http
->state
= HTTP_STATE_STATUS
;
3457 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3458 httpStateString(http
->state
)));
3461 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3468 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3470 * @since CUPS 1.7/OS X 10.9@
3473 int /* O - 0 on success, -1 on error */
3474 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3475 http_status_t status
) /* I - Status code */
3477 http_encoding_t old_encoding
; /* Old data_encoding value */
3478 off_t old_remaining
; /* Old data_remaining value */
3482 * Range check input...
3485 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3487 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3489 DEBUG_puts("1httpWriteResponse: Bad input.");
3494 * Set the various standard fields if they aren't already...
3497 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3498 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3500 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3502 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3503 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3506 if (http
->version
== HTTP_VERSION_1_1
)
3508 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3510 if (http
->keep_alive
)
3511 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3513 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3516 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3517 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3521 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3522 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3524 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3525 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3527 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3528 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3530 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3531 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3533 #endif /* HAVE_SSL */
3536 httpSetField(http
, HTTP_FIELD_SERVER
,
3537 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3540 * Set the Accept-Encoding field if it isn't already...
3543 if (!http
->accept_encoding
)
3544 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3545 http
->default_accept_encoding
? http
->default_accept_encoding
:
3547 "gzip, deflate, identity");
3550 #endif /* HAVE_LIBZ */
3553 * Send the response header...
3556 old_encoding
= http
->data_encoding
;
3557 old_remaining
= http
->data_remaining
;
3558 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3560 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3561 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3563 http
->status
= HTTP_STATUS_ERROR
;
3567 if (status
!= HTTP_STATUS_CONTINUE
)
3570 * 100 Continue doesn't have the rest of the response headers...
3573 int i
; /* Looping var */
3574 const char *value
; /* Field value */
3576 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3578 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3580 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3582 http
->status
= HTTP_STATUS_ERROR
;
3590 if (strchr(http
->cookie
, ';'))
3592 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3594 http
->status
= HTTP_STATUS_ERROR
;
3598 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3600 http
->status
= HTTP_STATUS_ERROR
;
3606 * "Click-jacking" defense (STR #4492)...
3609 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3610 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3612 http
->status
= HTTP_STATUS_ERROR
;
3617 if (httpWrite2(http
, "\r\n", 2) < 2)
3619 http
->status
= HTTP_STATUS_ERROR
;
3623 if (httpFlushWrite(http
) < 0)
3625 http
->status
= HTTP_STATUS_ERROR
;
3629 if (status
== HTTP_STATUS_CONTINUE
||
3630 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3633 * Restore the old data_encoding and data_length values...
3636 http
->data_encoding
= old_encoding
;
3637 http
->data_remaining
= old_remaining
;
3639 if (old_remaining
<= INT_MAX
)
3640 http
->_data_remaining
= (int)old_remaining
;
3642 http
->_data_remaining
= INT_MAX
;
3644 else if (http
->state
== HTTP_STATE_OPTIONS
||
3645 http
->state
== HTTP_STATE_HEAD
||
3646 http
->state
== HTTP_STATE_PUT
||
3647 http
->state
== HTTP_STATE_TRACE
||
3648 http
->state
== HTTP_STATE_CONNECT
||
3649 http
->state
== HTTP_STATE_STATUS
)
3651 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3652 "was %s.", httpStateString(http
->state
)));
3653 http
->state
= HTTP_STATE_WAITING
;
3658 * Force data_encoding and data_length to be set according to the response
3662 http_set_length(http
);
3664 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3666 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3667 "was %s.", httpStateString(http
->state
)));
3668 http
->state
= HTTP_STATE_WAITING
;
3674 * Then start any content encoding...
3677 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3678 http_content_coding_start(http
,
3679 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3680 #endif /* HAVE_LIBZ */
3690 * 'http_content_coding_finish()' - Finish doing any content encoding.
3694 http_content_coding_finish(
3695 http_t
*http
) /* I - HTTP connection */
3697 int zerr
; /* Compression status */
3698 Byte dummy
[1]; /* Dummy read buffer */
3699 size_t bytes
; /* Number of bytes to write */
3702 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3703 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3705 switch (http
->coding
)
3707 case _HTTP_CODING_DEFLATE
:
3708 case _HTTP_CODING_GZIP
:
3709 http
->stream
.next_in
= dummy
;
3710 http
->stream
.avail_in
= 0;
3714 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3715 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3719 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3721 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3722 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3724 http_write(http
, (char *)http
->sbuffer
, bytes
);
3727 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3728 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3730 while (zerr
== Z_OK
);
3732 deflateEnd(&(http
->stream
));
3734 free(http
->sbuffer
);
3735 http
->sbuffer
= NULL
;
3738 httpFlushWrite(http
);
3741 case _HTTP_CODING_INFLATE
:
3742 case _HTTP_CODING_GUNZIP
:
3743 inflateEnd(&(http
->stream
));
3744 free(http
->sbuffer
);
3745 http
->sbuffer
= NULL
;
3752 http
->coding
= _HTTP_CODING_IDENTITY
;
3757 * 'http_content_coding_start()' - Start doing content encoding.
3761 http_content_coding_start(
3762 http_t
*http
, /* I - HTTP connection */
3763 const char *value
) /* I - Value of Content-Encoding */
3765 int zerr
; /* Error/status */
3766 _http_coding_t coding
; /* Content coding value */
3769 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3771 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3773 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3777 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3779 if (http
->state
== HTTP_STATE_GET_SEND
||
3780 http
->state
== HTTP_STATE_POST_SEND
)
3781 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3782 _HTTP_CODING_GUNZIP
;
3783 else if (http
->state
== HTTP_STATE_POST_RECV
||
3784 http
->state
== HTTP_STATE_PUT_RECV
)
3785 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3786 _HTTP_CODING_GUNZIP
;
3789 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3793 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3795 if (http
->state
== HTTP_STATE_GET_SEND
||
3796 http
->state
== HTTP_STATE_POST_SEND
)
3797 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3798 _HTTP_CODING_INFLATE
;
3799 else if (http
->state
== HTTP_STATE_POST_RECV
||
3800 http
->state
== HTTP_STATE_PUT_RECV
)
3801 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3802 _HTTP_CODING_INFLATE
;
3805 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3811 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3815 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3819 case _HTTP_CODING_DEFLATE
:
3820 case _HTTP_CODING_GZIP
:
3822 httpFlushWrite(http
);
3824 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3826 http
->status
= HTTP_STATUS_ERROR
;
3827 http
->error
= errno
;
3832 * Window size for compression is 11 bits - optimal based on PWG Raster
3833 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3837 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3839 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3840 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3842 http
->status
= HTTP_STATUS_ERROR
;
3843 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3847 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3848 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3851 case _HTTP_CODING_INFLATE
:
3852 case _HTTP_CODING_GUNZIP
:
3853 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3855 http
->status
= HTTP_STATUS_ERROR
;
3856 http
->error
= errno
;
3861 * Window size for decompression is up to 15 bits (maximum supported).
3862 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3865 if ((zerr
= inflateInit2(&(http
->stream
),
3866 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3869 free(http
->sbuffer
);
3870 http
->sbuffer
= NULL
;
3871 http
->status
= HTTP_STATUS_ERROR
;
3872 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3876 http
->stream
.avail_in
= 0;
3877 http
->stream
.next_in
= http
->sbuffer
;
3884 http
->coding
= coding
;
3886 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3889 #endif /* HAVE_LIBZ */
3893 * 'http_create()' - Create an unconnected HTTP connection.
3896 static http_t
* /* O - HTTP connection */
3898 const char *host
, /* I - Hostname */
3899 int port
, /* I - Port number */
3900 http_addrlist_t
*addrlist
, /* I - Address list or NULL */
3901 int family
, /* I - Address family or AF_UNSPEC */
3902 http_encryption_t encryption
, /* I - Encryption to use */
3903 int blocking
, /* I - 1 for blocking mode */
3904 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3906 http_t
*http
; /* New HTTP connection */
3907 char service
[255]; /* Service name */
3908 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3911 DEBUG_printf(("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%d, mode=%d)", host
, port
, (void *)addrlist
, family
, encryption
, blocking
, mode
));
3913 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3919 * Lookup the host...
3924 myaddrlist
= httpAddrCopyList(addrlist
);
3928 snprintf(service
, sizeof(service
), "%d", port
);
3930 myaddrlist
= httpAddrGetList(host
, family
, service
);
3937 * Allocate memory for the structure...
3940 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3942 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3943 httpAddrFreeList(addrlist
);
3948 * Initialize the HTTP data...
3952 http
->activity
= time(NULL
);
3953 http
->addrlist
= myaddrlist
;
3954 http
->blocking
= blocking
;
3957 http
->gssctx
= GSS_C_NO_CONTEXT
;
3958 http
->gssname
= GSS_C_NO_NAME
;
3959 #endif /* HAVE_GSSAPI */
3960 http
->status
= HTTP_STATUS_CONTINUE
;
3961 http
->version
= HTTP_VERSION_1_1
;
3964 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3966 if (port
== 443) /* Always use encryption for https */
3967 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3969 http
->encryption
= encryption
;
3971 http_set_wait(http
);
3974 * Return the new structure...
3983 * 'http_debug_hex()' - Do a hex dump of a buffer.
3987 http_debug_hex(const char *prefix
, /* I - Prefix for line */
3988 const char *buffer
, /* I - Buffer to dump */
3989 int bytes
) /* I - Bytes to dump */
3991 int i
, j
, /* Looping vars */
3992 ch
; /* Current character */
3993 char line
[255], /* Line buffer */
3994 *start
, /* Start of line after prefix */
3995 *ptr
; /* Pointer into line */
3998 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4001 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4003 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4004 start
= line
+ strlen(line
);
4006 for (i
= 0; i
< bytes
; i
+= 16)
4008 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4009 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4013 memcpy(ptr
, " ", 3);
4018 memcpy(ptr
, " ", 3);
4021 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4023 ch
= buffer
[i
+ j
] & 255;
4025 if (ch
< ' ' || ch
>= 127)
4039 * 'http_read()' - Read a buffer from a HTTP connection.
4041 * This function does the low-level read from the socket, retrying and timing
4045 static ssize_t
/* O - Number of bytes read or -1 on error */
4046 http_read(http_t
*http
, /* I - HTTP connection */
4047 char *buffer
, /* I - Buffer */
4048 size_t length
) /* I - Maximum bytes to read */
4050 ssize_t bytes
; /* Bytes read */
4053 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4055 if (!http
->blocking
)
4057 while (!httpWait(http
, http
->wait_value
))
4059 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4062 DEBUG_puts("2http_read: Timeout.");
4067 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4073 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4075 #endif /* HAVE_SSL */
4076 bytes
= recv(http
->fd
, buffer
, length
, 0);
4081 if (WSAGetLastError() != WSAEINTR
)
4083 http
->error
= WSAGetLastError();
4086 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4088 if (!http
->timeout_cb
||
4089 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4091 http
->error
= WSAEWOULDBLOCK
;
4096 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4098 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4100 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4102 http
->error
= errno
;
4105 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4107 http
->error
= errno
;
4111 else if (errno
!= EINTR
)
4113 http
->error
= errno
;
4121 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4122 CUPS_LLCAST bytes
));
4125 http_debug_hex("http_read", buffer
, (int)bytes
);
4131 if (WSAGetLastError() == WSAEINTR
)
4134 http
->error
= WSAGetLastError();
4136 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4139 http
->error
= errno
;
4142 else if (bytes
== 0)
4144 http
->error
= EPIPE
;
4153 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4155 * This function reads data from the HTTP buffer or from the socket, as needed.
4158 static ssize_t
/* O - Number of bytes read or -1 on error */
4159 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4160 char *buffer
, /* I - Buffer */
4161 size_t length
) /* I - Maximum bytes to read */
4163 ssize_t bytes
; /* Bytes read */
4166 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4170 if (length
> (size_t)http
->used
)
4171 bytes
= (ssize_t
)http
->used
;
4173 bytes
= (ssize_t
)length
;
4175 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4178 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4179 http
->used
-= (int)bytes
;
4182 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4185 bytes
= http_read(http
, buffer
, length
);
4192 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4194 * This function reads and validates the chunk length, then does a buffered read
4195 * returning the number of bytes placed in the buffer.
4198 static ssize_t
/* O - Number of bytes read or -1 on error */
4199 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4200 char *buffer
, /* I - Buffer */
4201 size_t length
) /* I - Maximum bytes to read */
4203 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4205 if (http
->data_remaining
<= 0)
4207 char len
[32]; /* Length string */
4209 if (!httpGets(len
, sizeof(len
), http
))
4211 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4217 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4218 if (!httpGets(len
, sizeof(len
), http
))
4220 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4225 http
->data_remaining
= strtoll(len
, NULL
, 16);
4227 if (http
->data_remaining
< 0)
4229 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4230 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4234 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4235 len
, CUPS_LLCAST http
->data_remaining
));
4237 if (http
->data_remaining
== 0)
4240 * 0-length chunk, grab trailing blank line...
4243 httpGets(len
, sizeof(len
), http
);
4247 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4248 CUPS_LLCAST http
->data_remaining
));
4250 if (http
->data_remaining
<= 0)
4252 else if (length
> (size_t)http
->data_remaining
)
4253 length
= (size_t)http
->data_remaining
;
4255 return (http_read_buffered(http
, buffer
, length
));
4260 * 'http_send()' - Send a request with all fields and the trailing blank line.
4263 static int /* O - 0 on success, non-zero on error */
4264 http_send(http_t
*http
, /* I - HTTP connection */
4265 http_state_t request
, /* I - Request code */
4266 const char *uri
) /* I - URI */
4268 int i
; /* Looping var */
4269 char buf
[1024]; /* Encoded URI buffer */
4270 const char *value
; /* Field value */
4271 static const char * const codes
[] = /* Request code strings */
4291 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4293 if (http
== NULL
|| uri
== NULL
)
4297 * Set the User-Agent field if it isn't already...
4300 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4302 if (http
->default_user_agent
)
4303 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4305 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4309 * Set the Accept-Encoding field if it isn't already...
4312 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4313 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4314 http
->default_accept_encoding
);
4317 * Encode the URI as needed...
4320 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4323 * See if we had an error the last time around; if so, reconnect...
4326 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4327 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4329 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4330 http
->fd
, http
->status
, http
->tls_upgrade
));
4332 if (httpReconnect2(http
, 30000, NULL
))
4337 * Flush any written data that is pending...
4342 if (httpFlushWrite(http
) < 0)
4343 if (httpReconnect2(http
, 30000, NULL
))
4348 * Send the request header...
4351 http
->state
= request
;
4352 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4354 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4357 http
->status
= HTTP_STATUS_CONTINUE
;
4360 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4362 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4363 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4365 #endif /* HAVE_SSL */
4367 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4369 http
->status
= HTTP_STATUS_ERROR
;
4373 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4374 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4376 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4378 if (i
== HTTP_FIELD_HOST
)
4380 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4381 httpAddrPort(http
->hostaddr
)) < 1)
4383 http
->status
= HTTP_STATUS_ERROR
;
4387 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4389 http
->status
= HTTP_STATUS_ERROR
;
4395 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4397 http
->status
= HTTP_STATUS_ERROR
;
4401 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4402 http
->mode
, http
->state
));
4404 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4405 (http
->state
== HTTP_STATE_POST_RECV
||
4406 http
->state
== HTTP_STATE_PUT_RECV
))
4407 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4409 http
->status
= HTTP_STATUS_ERROR
;
4413 if (httpPrintf(http
, "\r\n") < 1)
4415 http
->status
= HTTP_STATUS_ERROR
;
4419 if (httpFlushWrite(http
) < 0)
4422 http_set_length(http
);
4423 httpClearFields(http
);
4426 * The Kerberos and AuthRef authentication strings can only be used once...
4429 if (http
->field_authorization
&& http
->authstring
&&
4430 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4431 !strncmp(http
->authstring
, "AuthRef", 7)))
4433 http
->_authstring
[0] = '\0';
4435 if (http
->authstring
!= http
->_authstring
)
4436 free(http
->authstring
);
4438 http
->authstring
= http
->_authstring
;
4446 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4449 static off_t
/* O - Remainder or -1 on error */
4450 http_set_length(http_t
*http
) /* I - Connection */
4452 off_t remaining
; /* Remainder */
4455 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4457 if ((remaining
= httpGetLength2(http
)) >= 0)
4459 if (http
->mode
== _HTTP_MODE_SERVER
&&
4460 http
->state
!= HTTP_STATE_GET_SEND
&&
4461 http
->state
!= HTTP_STATE_PUT
&&
4462 http
->state
!= HTTP_STATE_POST
&&
4463 http
->state
!= HTTP_STATE_POST_SEND
)
4465 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4469 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4472 DEBUG_puts("1http_set_length: Setting data_encoding to "
4473 "HTTP_ENCODING_CHUNKED.");
4474 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4478 DEBUG_puts("1http_set_length: Setting data_encoding to "
4479 "HTTP_ENCODING_LENGTH.");
4480 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4483 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4484 CUPS_LLCAST remaining
));
4485 http
->data_remaining
= remaining
;
4487 if (remaining
<= INT_MAX
)
4488 http
->_data_remaining
= (int)remaining
;
4490 http
->_data_remaining
= INT_MAX
;
4497 * 'http_set_timeout()' - Set the socket timeout values.
4501 http_set_timeout(int fd
, /* I - File descriptor */
4502 double timeout
) /* I - Timeout in seconds */
4505 DWORD tv
= (DWORD
)(timeout
* 1000);
4506 /* Timeout in milliseconds */
4508 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4509 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4512 struct timeval tv
; /* Timeout in secs and usecs */
4514 tv
.tv_sec
= (int)timeout
;
4515 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4517 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4518 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4524 * 'http_set_wait()' - Set the default wait value for reads.
4528 http_set_wait(http_t
*http
) /* I - HTTP connection */
4532 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4534 if (http
->wait_value
<= 0)
4535 http
->wait_value
= 60000;
4538 http
->wait_value
= 10000;
4544 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4547 static int /* O - Status of connection */
4548 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4550 int ret
; /* Return value */
4551 http_t myhttp
; /* Local copy of HTTP data */
4554 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4557 * Flush the connection to make sure any previous "Upgrade" message
4564 * Copy the HTTP data to a local variable so we can do the OPTIONS
4565 * request without interfering with the existing request data...
4568 memcpy(&myhttp
, http
, sizeof(myhttp
));
4571 * Send an OPTIONS request to the server, requiring SSL or TLS
4572 * encryption on the link...
4575 http
->tls_upgrade
= 1;
4576 http
->field_authorization
= NULL
; /* Don't free the auth string */
4578 httpClearFields(http
);
4579 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4580 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4582 if ((ret
= httpOptions(http
, "*")) == 0)
4585 * Wait for the secure connection...
4588 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4592 * Restore the HTTP request data...
4595 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4596 http
->data_encoding
= myhttp
.data_encoding
;
4597 http
->data_remaining
= myhttp
.data_remaining
;
4598 http
->_data_remaining
= myhttp
._data_remaining
;
4599 http
->expect
= myhttp
.expect
;
4600 http
->field_authorization
= myhttp
.field_authorization
;
4601 http
->digest_tries
= myhttp
.digest_tries
;
4602 http
->tls_upgrade
= 0;
4605 * See if we actually went secure...
4611 * Server does not support HTTP upgrade...
4614 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4616 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4617 httpAddrClose(NULL
, http
->fd
);
4626 #endif /* HAVE_SSL */
4630 * 'http_write()' - Write a buffer to a HTTP connection.
4633 static ssize_t
/* O - Number of bytes written */
4634 http_write(http_t
*http
, /* I - HTTP connection */
4635 const char *buffer
, /* I - Buffer for data */
4636 size_t length
) /* I - Number of bytes to write */
4638 ssize_t tbytes
, /* Total bytes sent */
4639 bytes
; /* Bytes sent */
4642 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4648 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4650 if (http
->timeout_cb
)
4653 struct pollfd pfd
; /* Polled file descriptor */
4655 fd_set output_set
; /* Output ready for write? */
4656 struct timeval timeout
; /* Timeout value */
4657 #endif /* HAVE_POLL */
4658 int nfds
; /* Result from select()/poll() */
4664 pfd
.events
= POLLOUT
;
4666 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4667 (errno
== EINTR
|| errno
== EAGAIN
))
4673 FD_ZERO(&output_set
);
4674 FD_SET(http
->fd
, &output_set
);
4676 timeout
.tv_sec
= http
->wait_value
/ 1000;
4677 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4679 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4682 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4683 WSAGetLastError() == WSAEWOULDBLOCK
));
4685 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4687 #endif /* HAVE_POLL */
4691 http
->error
= errno
;
4694 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4697 http
->error
= WSAEWOULDBLOCK
;
4699 http
->error
= EWOULDBLOCK
;
4709 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4711 #endif /* HAVE_SSL */
4712 bytes
= send(http
->fd
, buffer
, length
, 0);
4714 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4715 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4720 if (WSAGetLastError() == WSAEINTR
)
4722 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4724 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4727 http
->error
= WSAGetLastError();
4729 else if (WSAGetLastError() != http
->error
&&
4730 WSAGetLastError() != WSAECONNRESET
)
4732 http
->error
= WSAGetLastError();
4739 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4741 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4743 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4746 http
->error
= errno
;
4748 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4750 http
->error
= errno
;
4755 DEBUG_printf(("3http_write: error writing data (%s).",
4756 strerror(http
->error
)));
4763 length
-= (size_t)bytes
;
4767 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4770 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4777 * 'http_write_chunk()' - Write a chunked buffer.
4780 static ssize_t
/* O - Number bytes written */
4781 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4782 const char *buffer
, /* I - Buffer to write */
4783 size_t length
) /* I - Length of buffer */
4785 char header
[16]; /* Chunk header */
4786 ssize_t bytes
; /* Bytes written */
4789 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4792 * Write the chunk header, data, and trailer.
4795 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4796 if (http_write(http
, header
, strlen(header
)) < 0)
4798 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4802 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4804 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4808 if (http_write(http
, "\r\n", 2) < 0)
4810 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");