2 * HTTP routines for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Include necessary headers...
23 #include "cups-private.h"
30 # include <sys/time.h>
31 # include <sys/resource.h>
35 #endif /* HAVE_POLL */
43 static void http_content_coding_finish(http_t
*http
);
44 static void http_content_coding_start(http_t
*http
,
46 #endif /* HAVE_LIBZ */
47 static http_t
*http_create(const char *host
, int port
,
48 http_addrlist_t
*addrlist
, int family
,
49 http_encryption_t encryption
,
50 int blocking
, _http_mode_t mode
);
52 static void http_debug_hex(const char *prefix
, const char *buffer
,
55 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
56 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
57 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
58 static int http_send(http_t
*http
, http_state_t request
,
60 static ssize_t
http_write(http_t
*http
, const char *buffer
,
62 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
64 static off_t
http_set_length(http_t
*http
);
65 static void http_set_timeout(int fd
, double timeout
);
66 static void http_set_wait(http_t
*http
);
69 static int http_tls_upgrade(http_t
*http
);
77 static const char * const http_fields
[] =
94 "If-Unmodified-since",
113 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
114 * specified listening socket.
116 * @since CUPS 1.7/macOS 10.9@
119 http_t
* /* O - HTTP connection or @code NULL@ */
120 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
121 int blocking
) /* I - 1 if the connection should be
122 blocking, 0 otherwise */
124 http_t
*http
; /* HTTP connection */
125 http_addrlist_t addrlist
; /* Dummy address list */
126 socklen_t addrlen
; /* Length of address */
127 int val
; /* Socket option value */
131 * Range check input...
138 * Create the client connection...
141 memset(&addrlist
, 0, sizeof(addrlist
));
143 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
144 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
145 _HTTP_MODE_SERVER
)) == NULL
)
149 * Accept the client and get the remote address...
152 addrlen
= sizeof(http_addr_t
);
154 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
157 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
163 http
->hostaddr
= &(http
->addrlist
->addr
);
165 if (httpAddrLocalhost(http
->hostaddr
))
166 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
168 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
172 * Disable SIGPIPE for this socket.
176 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
177 #endif /* SO_NOSIGPIPE */
180 * Using TCP_NODELAY improves responsiveness, especially on systems
181 * with a slow loopback interface. Since we write large buffers
182 * when sending print files and requests, there shouldn't be any
183 * performance penalty for this...
187 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
191 * Close this socket when starting another process...
194 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
195 #endif /* FD_CLOEXEC */
202 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
204 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
206 * @since CUPS 1.5/macOS 10.7@
209 int /* O - 0 on success, -1 on error */
211 cups_array_t
*credentials
, /* I - Credentials array */
212 const void *data
, /* I - PEM-encoded X.509 data */
213 size_t datalen
) /* I - Length of data */
215 http_credential_t
*credential
; /* Credential data */
218 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
220 credential
->datalen
= datalen
;
222 if ((credential
->data
= malloc(datalen
)) != NULL
)
224 memcpy(credential
->data
, data
, datalen
);
225 cupsArrayAdd(credentials
, credential
);
237 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
241 httpBlocking(http_t
*http
, /* I - HTTP connection */
242 int b
) /* I - 1 = blocking, 0 = non-blocking */
253 * 'httpCheck()' - Check to see if there is a pending response from the server.
256 int /* O - 0 = no data, 1 = data available */
257 httpCheck(http_t
*http
) /* I - HTTP connection */
259 return (httpWait(http
, 0));
264 * 'httpClearCookie()' - Clear the cookie value(s).
266 * @since CUPS 1.1.19/macOS 10.3@
270 httpClearCookie(http_t
*http
) /* I - HTTP connection */
284 * 'httpClearFields()' - Clear HTTP request fields.
288 httpClearFields(http_t
*http
) /* I - HTTP connection */
290 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
294 memset(http
->fields
, 0, sizeof(http
->fields
));
296 if (http
->mode
== _HTTP_MODE_CLIENT
)
298 if (http
->hostname
[0] == '/')
299 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
301 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
304 if (http
->field_authorization
)
306 free(http
->field_authorization
);
307 http
->field_authorization
= NULL
;
310 if (http
->accept_encoding
)
312 _cupsStrFree(http
->accept_encoding
);
313 http
->accept_encoding
= NULL
;
318 _cupsStrFree(http
->allow
);
324 _cupsStrFree(http
->server
);
328 http
->expect
= (http_status_t
)0;
334 * 'httpClose()' - Close an HTTP connection.
338 httpClose(http_t
*http
) /* I - HTTP connection */
341 OM_uint32 minor_status
; /* Minor status code */
342 #endif /* HAVE_GSSAPI */
345 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
348 * Range check input...
355 * Close any open connection...
358 _httpDisconnect(http
);
361 * Free memory used...
364 httpAddrFreeList(http
->addrlist
);
370 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
371 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
373 if (http
->gssname
!= GSS_C_NO_NAME
)
374 gss_release_name(&minor_status
, &http
->gssname
);
375 #endif /* HAVE_GSSAPI */
377 #ifdef HAVE_AUTHORIZATION_H
379 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
380 #endif /* HAVE_AUTHORIZATION_H */
382 httpClearFields(http
);
384 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
385 free(http
->authstring
);
392 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
394 * @since CUPS 2.0/OS 10.10@
397 int /* O - 1 if they match, 0 if they do not */
398 httpCompareCredentials(
399 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
400 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
402 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
405 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
))
406 if (temp1
->datalen
!= temp2
->datalen
)
408 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
411 return (temp1
== temp2
);
416 * 'httpConnect()' - Connect to a HTTP server.
418 * This function is deprecated - use @link httpConnect2@ instead.
420 * @deprecated@ @exclude all@
423 http_t
* /* O - New HTTP connection */
424 httpConnect(const char *host
, /* I - Host to connect to */
425 int port
) /* I - Port number */
427 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
428 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
433 * 'httpConnect2()' - Connect to a HTTP server.
435 * @since CUPS 1.7/macOS 10.9@
438 http_t
* /* O - New HTTP connection */
440 const char *host
, /* I - Host to connect to */
441 int port
, /* I - Port number */
442 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
443 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
444 http_encryption_t encryption
, /* I - Type of encryption to use */
445 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
446 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
447 int *cancel
) /* I - Pointer to "cancel" variable */
449 http_t
*http
; /* New HTTP connection */
452 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
));
455 * Create the HTTP structure...
458 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
459 _HTTP_MODE_CLIENT
)) == NULL
)
463 * Optionally connect to the remote system...
466 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
470 * Could not connect to any known address - bail out!
480 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
482 * This function is now deprecated. Please use the @link httpConnect2@ function
485 * @deprecated@ @exclude all@
488 http_t
* /* O - New HTTP connection */
490 const char *host
, /* I - Host to connect to */
491 int port
, /* I - Port number */
492 http_encryption_t encryption
) /* I - Type of encryption to use */
494 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
495 host
, port
, encryption
));
497 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
503 * 'httpDelete()' - Send a DELETE request to the server.
506 int /* O - Status of call (0 = success) */
507 httpDelete(http_t
*http
, /* I - HTTP connection */
508 const char *uri
) /* I - URI to delete */
510 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
515 * '_httpDisconnect()' - Disconnect a HTTP connection.
519 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
524 #endif /* HAVE_SSL */
526 httpAddrClose(NULL
, http
->fd
);
533 * 'httpEncryption()' - Set the required encryption on the link.
536 int /* O - -1 on error, 0 on success */
537 httpEncryption(http_t
*http
, /* I - HTTP connection */
538 http_encryption_t e
) /* I - New encryption preference */
540 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
546 if (http
->mode
== _HTTP_MODE_CLIENT
)
548 http
->encryption
= e
;
550 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
551 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
552 return (httpReconnect2(http
, 30000, NULL
));
553 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
554 return (http_tls_upgrade(http
));
560 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
563 http
->encryption
= e
;
564 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
565 return (_httpTLSStart(http
));
570 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
574 #endif /* HAVE_SSL */
579 * 'httpError()' - Get the last error on a connection.
582 int /* O - Error code (errno) value */
583 httpError(http_t
*http
) /* I - HTTP connection */
586 return (http
->error
);
593 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
597 http_field_t
/* O - Field index */
598 httpFieldValue(const char *name
) /* I - String name */
600 int i
; /* Looping var */
603 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
604 if (!_cups_strcasecmp(name
, http_fields
[i
]))
605 return ((http_field_t
)i
);
607 return (HTTP_FIELD_UNKNOWN
);
612 * 'httpFlush()' - Flush data read from a HTTP connection.
616 httpFlush(http_t
*http
) /* I - HTTP connection */
618 char buffer
[8192]; /* Junk buffer */
619 int blocking
; /* To block or not to block */
620 http_state_t oldstate
; /* Old state */
623 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
626 * Nothing to do if we are in the "waiting" state...
629 if (http
->state
== HTTP_STATE_WAITING
)
633 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
636 blocking
= http
->blocking
;
640 * Read any data we can...
643 oldstate
= http
->state
;
644 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
647 * Restore blocking and reset the connection if we didn't get all of
648 * the remaining data...
651 http
->blocking
= blocking
;
653 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
657 * Didn't get the data back, so close the current connection.
662 http_content_coding_finish(http
);
663 #endif /* HAVE_LIBZ */
665 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
667 http
->state
= HTTP_STATE_WAITING
;
672 #endif /* HAVE_SSL */
674 httpAddrClose(NULL
, http
->fd
);
682 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
684 * @since CUPS 1.2/macOS 10.5@
687 int /* O - Bytes written or -1 on error */
688 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
690 ssize_t bytes
; /* Bytes written */
693 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
695 if (!http
|| !http
->wused
)
697 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
698 "1httpFlushWrite: No connection.");
702 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
703 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
705 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
709 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
716 * 'httpFreeCredentials()' - Free an array of credentials.
721 cups_array_t
*credentials
) /* I - Array of credentials */
723 http_credential_t
*credential
; /* Credential */
726 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
728 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
730 cupsArrayRemove(credentials
, credential
);
731 free((void *)credential
->data
);
735 cupsArrayDelete(credentials
);
740 * 'httpGet()' - Send a GET request to the server.
743 int /* O - Status of call (0 = success) */
744 httpGet(http_t
*http
, /* I - HTTP connection */
745 const char *uri
) /* I - URI to get */
747 return (http_send(http
, HTTP_STATE_GET
, uri
));
752 * 'httpGetActivity()' - Get the most recent activity for a connection.
754 * The return value is the time in seconds of the last read or write.
756 * @since CUPS 2.0/OS 10.10@
759 time_t /* O - Time of last read or write */
760 httpGetActivity(http_t
*http
) /* I - HTTP connection */
762 return (http
? http
->activity
: 0);
767 * 'httpGetAuthString()' - Get the current authorization string.
769 * The authorization string is set by @link cupsDoAuthentication@ and
770 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
771 * string to use with @link httpSetField@ for the
772 * @code HTTP_FIELD_AUTHORIZATION@ value.
774 * @since CUPS 1.3/macOS 10.5@
777 char * /* O - Authorization string */
778 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
781 return (http
->authstring
);
788 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
790 * @since CUPS 1.2/macOS 10.5@
793 int /* O - 1 if blocking, 0 if non-blocking */
794 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
796 return (http
? http
->blocking
: 0);
801 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
802 * the client and server.
804 * This function uses the value of the Accepts-Encoding HTTP header and must be
805 * called after receiving a response from the server or a request from the
806 * client. The value returned can be use in subsequent requests (for clients)
807 * or in the response (for servers) in order to compress the content stream.
809 * @since CUPS 1.7/macOS 10.9@
812 const char * /* O - Content-Coding value or
813 @code NULL@ for the identity
815 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
818 if (http
&& http
->accept_encoding
)
820 int i
; /* Looping var */
821 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
822 *start
, /* Start of coding value */
823 *end
; /* End of coding value */
824 double qvalue
; /* "qvalue" for coding */
825 struct lconv
*loc
= localeconv(); /* Locale data */
826 static const char * const codings
[] =
827 { /* Supported content codings */
834 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
836 for (start
= temp
; *start
; start
= end
)
839 * Find the end of the coding name...
844 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
850 * Grab the qvalue as needed...
853 if (!strncmp(end
, ";q=", 3))
854 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
857 * Skip past all attributes...
861 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
867 while (*end
&& isspace(*end
& 255))
871 * Check value if it matches something we support...
877 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
878 if (!strcmp(start
, codings
[i
]))
882 #endif /* HAVE_LIBZ */
889 * 'httpGetCookie()' - Get any cookie data from the response.
891 * @since CUPS 1.1.19/macOS 10.3@
894 const char * /* O - Cookie data or @code NULL@ */
895 httpGetCookie(http_t
*http
) /* I - HTTP connection */
897 return (http
? http
->cookie
: NULL
);
902 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
904 * This function returns the encryption mode for the connection. Use the
905 * @link httpIsEncrypted@ function to determine whether a TLS session has
908 * @since CUPS 2.0/OS 10.10@
911 http_encryption_t
/* O - Current encryption mode */
912 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
914 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
919 * 'httpGetExpect()' - Get the value of the Expect header, if any.
921 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
922 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
924 * @since CUPS 1.7/macOS 10.9@
927 http_status_t
/* O - Expect: status, if any */
928 httpGetExpect(http_t
*http
) /* I - HTTP connection */
931 return (HTTP_STATUS_ERROR
);
933 return (http
->expect
);
938 * 'httpGetFd()' - Get the file descriptor associated with a connection.
940 * @since CUPS 1.2/macOS 10.5@
943 int /* O - File descriptor or -1 if none */
944 httpGetFd(http_t
*http
) /* I - HTTP connection */
946 return (http
? http
->fd
: -1);
951 * 'httpGetField()' - Get a field value from a request/response.
954 const char * /* O - Field value */
955 httpGetField(http_t
*http
, /* I - HTTP connection */
956 http_field_t field
) /* I - Field to get */
958 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
963 case HTTP_FIELD_ACCEPT_ENCODING
:
964 return (http
->accept_encoding
);
966 case HTTP_FIELD_ALLOW
:
967 return (http
->allow
);
969 case HTTP_FIELD_SERVER
:
970 return (http
->server
);
972 case HTTP_FIELD_AUTHORIZATION
:
973 if (http
->field_authorization
)
976 * Special case for WWW-Authenticate: as its contents can be
977 * longer than HTTP_MAX_VALUE...
980 return (http
->field_authorization
);
984 return (http
->fields
[field
]);
990 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
992 * @since CUPS 2.0/OS 10.10@
995 http_keepalive_t
/* O - Keep-Alive state */
996 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
998 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1003 * 'httpGetLength()' - Get the amount of data remaining from the
1004 * content-length or transfer-encoding fields.
1006 * This function is deprecated and will not return lengths larger than
1007 * 2^31 - 1; use httpGetLength2() instead.
1009 * @deprecated@ @exclude all@
1012 int /* O - Content length */
1013 httpGetLength(http_t
*http
) /* I - HTTP connection */
1016 * Get the read content length and return the 32-bit value.
1021 httpGetLength2(http
);
1023 return (http
->_data_remaining
);
1031 * 'httpGetLength2()' - Get the amount of data remaining from the
1032 * content-length or transfer-encoding fields.
1034 * This function returns the complete content length, even for
1035 * content larger than 2^31 - 1.
1037 * @since CUPS 1.2/macOS 10.5@
1040 off_t
/* O - Content length */
1041 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1043 off_t remaining
; /* Remaining length */
1046 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1051 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1053 DEBUG_puts("4httpGetLength2: chunked request!");
1059 * The following is a hack for HTTP servers that don't send a
1060 * Content-Length or Transfer-Encoding field...
1062 * If there is no Content-Length then the connection must close
1063 * after the transfer is complete...
1066 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1069 * Default content length is 0 for errors and certain types of operations,
1070 * and 2^31-1 for other successful requests...
1073 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1074 http
->state
== HTTP_STATE_OPTIONS
||
1075 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1076 http
->state
== HTTP_STATE_HEAD
||
1077 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1078 http
->state
== HTTP_STATE_DELETE
||
1079 http
->state
== HTTP_STATE_TRACE
||
1080 http
->state
== HTTP_STATE_CONNECT
)
1083 remaining
= 2147483647;
1085 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1089 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1090 CUPS_LLCAST remaining
));
1098 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1100 * @since CUPS 2.0/OS 10.10@
1103 size_t /* O - Number of bytes buffered */
1104 httpGetPending(http_t
*http
) /* I - HTTP connection */
1106 return (http
? (size_t)http
->wused
: 0);
1111 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1113 * @since CUPS 2.0/OS 10.10@
1116 size_t /* O - Number of bytes available */
1117 httpGetReady(http_t
*http
) /* I - HTTP connection */
1121 else if (http
->used
> 0)
1122 return ((size_t)http
->used
);
1125 return (_httpTLSPending(http
));
1126 #endif /* HAVE_SSL */
1133 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1134 * body or current chunk.
1136 * The @link httpIsChunked@ function can be used to determine whether the
1137 * message body is chunked or fixed-length.
1139 * @since CUPS 2.0/OS 10.10@
1142 size_t /* O - Remaining bytes */
1143 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1145 return (http
? (size_t)http
->data_remaining
: 0);
1150 * 'httpGets()' - Get a line of text from a HTTP connection.
1153 char * /* O - Line or @code NULL@ */
1154 httpGets(char *line
, /* I - Line to read into */
1155 int length
, /* I - Max length of buffer */
1156 http_t
*http
) /* I - HTTP connection */
1158 char *lineptr
, /* Pointer into line */
1159 *lineend
, /* End of line */
1160 *bufptr
, /* Pointer into input buffer */
1161 *bufend
; /* Pointer to end of buffer */
1162 ssize_t bytes
; /* Number of bytes read */
1163 int eol
; /* End-of-line? */
1166 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1168 if (!http
|| !line
|| length
<= 1)
1172 * Read a line from the buffer...
1177 lineend
= line
+ length
- 1;
1180 while (lineptr
< lineend
)
1183 * Pre-load the buffer as needed...
1192 while (http
->used
== 0)
1195 * No newline; see if there is more data to be read...
1198 while (!_httpWait(http
, http
->wait_value
, 1))
1200 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1203 DEBUG_puts("3httpGets: Timed out!");
1205 http
->error
= WSAETIMEDOUT
;
1207 http
->error
= ETIMEDOUT
;
1212 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1214 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1219 * Nope, can't get a line this time...
1223 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1225 if (WSAGetLastError() == WSAEINTR
)
1227 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1229 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1232 http
->error
= WSAGetLastError();
1234 else if (WSAGetLastError() != http
->error
)
1236 http
->error
= WSAGetLastError();
1241 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1245 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1247 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1249 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1252 http
->error
= errno
;
1254 else if (errno
!= http
->error
)
1256 http
->error
= errno
;
1263 else if (bytes
== 0)
1265 http
->error
= EPIPE
;
1271 * Yup, update the amount used...
1274 http
->used
+= (int)bytes
;
1278 * Now copy as much of the current line as possible...
1281 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1282 lineptr
< lineend
&& bufptr
< bufend
;)
1284 if (*bufptr
== 0x0a)
1290 else if (*bufptr
== 0x0d)
1293 *lineptr
++ = *bufptr
++;
1296 http
->used
-= (int)(bufptr
- http
->buffer
);
1298 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1306 http
->activity
= time(NULL
);
1310 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1316 DEBUG_puts("3httpGets: No new line available!");
1323 * 'httpGetState()' - Get the current state of the HTTP request.
1326 http_state_t
/* O - HTTP state */
1327 httpGetState(http_t
*http
) /* I - HTTP connection */
1329 return (http
? http
->state
: HTTP_STATE_ERROR
);
1334 * 'httpGetStatus()' - Get the status of the last HTTP request.
1336 * @since CUPS 1.2/macOS 10.5@
1339 http_status_t
/* O - HTTP status */
1340 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1342 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1347 * 'httpGetSubField()' - Get a sub-field value.
1349 * @deprecated@ @exclude all@
1352 char * /* O - Value or @code NULL@ */
1353 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1354 http_field_t field
, /* I - Field index */
1355 const char *name
, /* I - Name of sub-field */
1356 char *value
) /* O - Value string */
1358 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1363 * 'httpGetSubField2()' - Get a sub-field value.
1365 * @since CUPS 1.2/macOS 10.5@
1368 char * /* O - Value or @code NULL@ */
1369 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1370 http_field_t field
, /* I - Field index */
1371 const char *name
, /* I - Name of sub-field */
1372 char *value
, /* O - Value string */
1373 int valuelen
) /* I - Size of value buffer */
1375 const char *fptr
; /* Pointer into field */
1376 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1377 *ptr
, /* Pointer into string buffer */
1378 *end
; /* End of value buffer */
1380 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1382 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1383 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1386 end
= value
+ valuelen
- 1;
1388 for (fptr
= http
->fields
[field
]; *fptr
;)
1391 * Skip leading whitespace...
1394 while (_cups_isspace(*fptr
))
1404 * Get the sub-field name...
1408 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1409 ptr
< (temp
+ sizeof(temp
) - 1);
1414 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1417 * Skip trailing chars up to the '='...
1420 while (_cups_isspace(*fptr
))
1430 * Skip = and leading whitespace...
1435 while (_cups_isspace(*fptr
))
1441 * Read quoted string...
1444 for (ptr
= value
, fptr
++;
1445 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1450 while (*fptr
&& *fptr
!= '\"')
1459 * Read unquoted string...
1463 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1468 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1472 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1475 * See if this is the one...
1478 if (!strcmp(name
, temp
))
1480 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1487 DEBUG_puts("3httpGetSubField2: Returning NULL");
1494 * 'httpGetVersion()' - Get the HTTP version at the other end.
1497 http_version_t
/* O - Version number */
1498 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1500 return (http
? http
->version
: HTTP_VERSION_1_0
);
1505 * 'httpHead()' - Send a HEAD request to the server.
1508 int /* O - Status of call (0 = success) */
1509 httpHead(http_t
*http
, /* I - HTTP connection */
1510 const char *uri
) /* I - URI for head */
1512 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1513 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1518 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1519 * default HTTP proxy (if any).
1523 httpInitialize(void)
1525 static int initialized
= 0; /* Have we been called before? */
1527 WSADATA winsockdata
; /* WinSock data */
1534 _cupsGlobalUnlock();
1539 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1541 #elif !defined(SO_NOSIGPIPE)
1543 * Ignore SIGPIPE signals...
1547 sigset(SIGPIPE
, SIG_IGN
);
1549 # elif defined(HAVE_SIGACTION)
1550 struct sigaction action
; /* POSIX sigaction data */
1553 memset(&action
, 0, sizeof(action
));
1554 action
.sa_handler
= SIG_IGN
;
1555 sigaction(SIGPIPE
, &action
, NULL
);
1558 signal(SIGPIPE
, SIG_IGN
);
1559 # endif /* !SO_NOSIGPIPE */
1563 _httpTLSInitialize();
1564 # endif /* HAVE_SSL */
1567 _cupsGlobalUnlock();
1572 * 'httpIsChunked()' - Report whether a message body is chunked.
1574 * This function returns non-zero if the message body is composed of
1575 * variable-length chunks.
1577 * @since CUPS 2.0/OS 10.10@
1580 int /* O - 1 if chunked, 0 if not */
1581 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1583 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1588 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1590 * This function returns non-zero if the connection is currently encrypted.
1592 * @since CUPS 2.0/OS 10.10@
1595 int /* O - 1 if encrypted, 0 if not */
1596 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1598 return (http
? http
->tls
!= NULL
: 0);
1603 * 'httpOptions()' - Send an OPTIONS request to the server.
1606 int /* O - Status of call (0 = success) */
1607 httpOptions(http_t
*http
, /* I - HTTP connection */
1608 const char *uri
) /* I - URI for options */
1610 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1615 * 'httpPeek()' - Peek at data from a HTTP connection.
1617 * This function copies available data from the given HTTP connection, reading
1618 * a buffer as needed. The data is still available for reading using
1621 * For non-blocking connections the usual timeouts apply.
1623 * @since CUPS 1.7/macOS 10.9@
1626 ssize_t
/* O - Number of bytes copied */
1627 httpPeek(http_t
*http
, /* I - HTTP connection */
1628 char *buffer
, /* I - Buffer for data */
1629 size_t length
) /* I - Maximum number of bytes */
1631 ssize_t bytes
; /* Bytes read */
1632 char len
[32]; /* Length string */
1635 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1637 if (http
== NULL
|| buffer
== NULL
)
1640 http
->activity
= time(NULL
);
1646 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1647 http
->data_remaining
<= 0)
1649 DEBUG_puts("2httpPeek: Getting chunk length...");
1651 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1653 DEBUG_puts("1httpPeek: Could not get length!");
1659 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1660 if (!httpGets(len
, sizeof(len
), http
))
1662 DEBUG_puts("1httpPeek: Could not get chunk length.");
1667 http
->data_remaining
= strtoll(len
, NULL
, 16);
1669 if (http
->data_remaining
< 0)
1671 DEBUG_puts("1httpPeek: Negative chunk length!");
1676 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1677 CUPS_LLCAST http
->data_remaining
));
1679 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1682 * A zero-length chunk ends a transfer; unless we are reading POST
1687 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1688 http_content_coding_finish(http
);
1689 #endif /* HAVE_LIBZ */
1691 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1692 httpGets(len
, sizeof(len
), http
);
1694 if (http
->state
== HTTP_STATE_POST_RECV
)
1697 http
->state
= HTTP_STATE_STATUS
;
1699 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1700 httpStateString(http
->state
)));
1703 * Prevent future reads for this request...
1706 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1710 else if (length
> (size_t)http
->data_remaining
)
1711 length
= (size_t)http
->data_remaining
;
1714 if (http
->used
== 0 &&
1715 (http
->coding
== _HTTP_CODING_IDENTITY
||
1716 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1718 if (http
->used
== 0)
1719 #endif /* HAVE_LIBZ */
1722 * Buffer small reads for better performance...
1725 ssize_t buflen
; /* Length of read for buffer */
1727 if (!http
->blocking
)
1729 while (!httpWait(http
, http
->wait_value
))
1731 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1738 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1739 buflen
= sizeof(http
->buffer
);
1741 buflen
= (ssize_t
)http
->data_remaining
;
1743 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1744 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1746 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1747 CUPS_LLCAST bytes
));
1751 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1754 http
->used
= (int)bytes
;
1759 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1761 # ifdef HAVE_INFLATECOPY
1762 int zerr
; /* Decompressor error */
1763 z_stream stream
; /* Copy of decompressor stream */
1765 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1767 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1768 /* Number of bytes to copy */
1770 if (http
->stream
.avail_in
> 0 &&
1771 http
->stream
.next_in
> http
->sbuffer
)
1772 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1774 http
->stream
.next_in
= http
->sbuffer
;
1776 if (buflen
> (size_t)http
->data_remaining
)
1777 buflen
= (size_t)http
->data_remaining
;
1779 if (buflen
> (size_t)http
->used
)
1780 buflen
= (size_t)http
->used
;
1782 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1783 "decompression buffer.", (int)buflen
));
1785 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1786 http
->stream
.avail_in
+= buflen
;
1787 http
->used
-= (int)buflen
;
1788 http
->data_remaining
-= (off_t
)buflen
;
1791 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1794 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1795 (int)http
->stream
.avail_in
));
1797 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1799 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1800 http
->error
= ENOMEM
;
1804 stream
.next_out
= (Bytef
*)buffer
;
1805 stream
.avail_out
= (uInt
)length
;
1807 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1808 inflateEnd(&stream
);
1812 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1814 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1821 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1824 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1825 "work with compressed streams.");
1827 # endif /* HAVE_INFLATECOPY */
1830 #endif /* HAVE_LIBZ */
1833 if (length
> (size_t)http
->used
)
1834 length
= (size_t)http
->used
;
1836 bytes
= (ssize_t
)length
;
1838 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1841 memcpy(buffer
, http
->buffer
, length
);
1849 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1852 http
->error
= WSAGetLastError();
1854 if (errno
== EINTR
|| errno
== EAGAIN
)
1857 http
->error
= errno
;
1860 else if (bytes
== 0)
1862 http
->error
= EPIPE
;
1871 * 'httpPost()' - Send a POST request to the server.
1874 int /* O - Status of call (0 = success) */
1875 httpPost(http_t
*http
, /* I - HTTP connection */
1876 const char *uri
) /* I - URI for post */
1878 return (http_send(http
, HTTP_STATE_POST
, uri
));
1883 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1888 int /* O - Number of bytes written */
1889 httpPrintf(http_t
*http
, /* I - HTTP connection */
1890 const char *format
, /* I - printf-style format string */
1891 ...) /* I - Additional args as needed */
1893 ssize_t bytes
; /* Number of bytes to write */
1894 char buf
[16384]; /* Buffer for formatted string */
1895 va_list ap
; /* Variable argument pointer */
1898 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1900 va_start(ap
, format
);
1901 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1904 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1906 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1907 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1912 DEBUG_puts("4httpPrintf: flushing existing data...");
1914 if (httpFlushWrite(http
) < 0)
1918 return ((int)http_write(http
, buf
, (size_t)bytes
));
1924 * 'httpPut()' - Send a PUT request to the server.
1927 int /* O - Status of call (0 = success) */
1928 httpPut(http_t
*http
, /* I - HTTP connection */
1929 const char *uri
) /* I - URI to put */
1931 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1932 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1937 * 'httpRead()' - Read data from a HTTP connection.
1939 * This function is deprecated. Use the httpRead2() function which can
1940 * read more than 2GB of data.
1942 * @deprecated@ @exclude all@
1945 int /* O - Number of bytes read */
1946 httpRead(http_t
*http
, /* I - HTTP connection */
1947 char *buffer
, /* I - Buffer for data */
1948 int length
) /* I - Maximum number of bytes */
1950 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1955 * 'httpRead2()' - Read data from a HTTP connection.
1957 * @since CUPS 1.2/macOS 10.5@
1960 ssize_t
/* O - Number of bytes read */
1961 httpRead2(http_t
*http
, /* I - HTTP connection */
1962 char *buffer
, /* I - Buffer for data */
1963 size_t length
) /* I - Maximum number of bytes */
1965 ssize_t bytes
; /* Bytes read */
1969 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
));
1971 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
));
1972 #endif /* HAVE_LIBZ */
1974 if (http
== NULL
|| buffer
== NULL
)
1977 http
->activity
= time(NULL
);
1984 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1988 if (http
->stream
.avail_in
> 0)
1990 int zerr
; /* Decompressor error */
1992 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1993 (int)http
->stream
.avail_in
, (int)length
));
1995 http
->stream
.next_out
= (Bytef
*)buffer
;
1996 http
->stream
.avail_out
= (uInt
)length
;
1998 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2000 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2002 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2009 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2011 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2012 http
->stream
.avail_in
, http
->stream
.avail_out
,
2020 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2021 /* Additional bytes for buffer */
2025 if (http
->stream
.avail_in
> 0 &&
2026 http
->stream
.next_in
> http
->sbuffer
)
2027 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2029 http
->stream
.next_in
= http
->sbuffer
;
2031 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2032 "decompression buffer.", (int)buflen
));
2034 if (http
->data_remaining
> 0)
2036 if (buflen
> http
->data_remaining
)
2037 buflen
= (ssize_t
)http
->data_remaining
;
2039 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2041 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2042 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2048 else if (bytes
== 0)
2051 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2052 "decompression buffer.", CUPS_LLCAST bytes
));
2054 http
->data_remaining
-= bytes
;
2055 http
->stream
.avail_in
+= (uInt
)bytes
;
2057 if (http
->data_remaining
<= 0 &&
2058 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2061 * Read the trailing blank line now...
2064 char len
[32]; /* Length string */
2066 httpGets(len
, sizeof(len
), http
);
2078 #endif /* HAVE_LIBZ */
2079 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2081 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2083 http
->data_remaining
-= bytes
;
2085 if (http
->data_remaining
<= 0)
2088 * Read the trailing blank line now...
2091 char len
[32]; /* Length string */
2093 httpGets(len
, sizeof(len
), http
);
2097 else if (http
->data_remaining
<= 0)
2100 * No more data to read...
2107 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2110 if (length
> (size_t)http
->data_remaining
)
2111 length
= (size_t)http
->data_remaining
;
2113 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2115 http
->data_remaining
-= bytes
;
2117 if (http
->data_remaining
<= 0 &&
2118 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2121 * Read the trailing blank line now...
2124 char len
[32]; /* Length string */
2126 httpGets(len
, sizeof(len
), http
);
2133 (http
->coding
== _HTTP_CODING_IDENTITY
||
2134 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2135 #endif /* HAVE_LIBZ */
2136 ((http
->data_remaining
<= 0 &&
2137 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2138 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2141 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2142 http_content_coding_finish(http
);
2143 #endif /* HAVE_LIBZ */
2145 if (http
->state
== HTTP_STATE_POST_RECV
)
2147 else if (http
->state
== HTTP_STATE_GET_SEND
||
2148 http
->state
== HTTP_STATE_POST_SEND
)
2149 http
->state
= HTTP_STATE_WAITING
;
2151 http
->state
= HTTP_STATE_STATUS
;
2153 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2154 httpStateString(http
->state
)));
2162 * 'httpReadRequest()' - Read a HTTP request from a connection.
2164 * @since CUPS 1.7/macOS 10.9@
2167 http_state_t
/* O - New state of connection */
2168 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2169 char *uri
, /* I - URI buffer */
2170 size_t urilen
) /* I - Size of URI buffer */
2172 char line
[4096], /* HTTP request line */
2173 *req_method
, /* HTTP request method */
2174 *req_uri
, /* HTTP request URI */
2175 *req_version
; /* HTTP request version number string */
2179 * Range check input...
2182 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2187 if (!http
|| !uri
|| urilen
< 1)
2189 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2190 return (HTTP_STATE_ERROR
);
2192 else if (http
->state
!= HTTP_STATE_WAITING
)
2194 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2195 httpStateString(http
->state
)));
2196 return (HTTP_STATE_ERROR
);
2203 httpClearFields(http
);
2205 http
->activity
= time(NULL
);
2206 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2207 http
->data_remaining
= 0;
2208 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2209 http
->status
= HTTP_STATUS_OK
;
2210 http
->version
= HTTP_VERSION_1_1
;
2213 * Read a line from the socket...
2216 if (!httpGets(line
, sizeof(line
), http
))
2218 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2219 return (HTTP_STATE_ERROR
);
2224 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2225 return (HTTP_STATE_WAITING
);
2228 DEBUG_printf(("1httpReadRequest: %s", line
));
2237 while (*req_uri
&& !isspace(*req_uri
& 255))
2242 DEBUG_puts("1httpReadRequest: No request URI.");
2243 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2244 return (HTTP_STATE_ERROR
);
2249 while (*req_uri
&& isspace(*req_uri
& 255))
2252 req_version
= req_uri
;
2254 while (*req_version
&& !isspace(*req_version
& 255))
2259 DEBUG_puts("1httpReadRequest: No request protocol version.");
2260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2261 return (HTTP_STATE_ERROR
);
2264 *req_version
++ = '\0';
2266 while (*req_version
&& isspace(*req_version
& 255))
2273 if (!strcmp(req_method
, "OPTIONS"))
2274 http
->state
= HTTP_STATE_OPTIONS
;
2275 else if (!strcmp(req_method
, "GET"))
2276 http
->state
= HTTP_STATE_GET
;
2277 else if (!strcmp(req_method
, "HEAD"))
2278 http
->state
= HTTP_STATE_HEAD
;
2279 else if (!strcmp(req_method
, "POST"))
2280 http
->state
= HTTP_STATE_POST
;
2281 else if (!strcmp(req_method
, "PUT"))
2282 http
->state
= HTTP_STATE_PUT
;
2283 else if (!strcmp(req_method
, "DELETE"))
2284 http
->state
= HTTP_STATE_DELETE
;
2285 else if (!strcmp(req_method
, "TRACE"))
2286 http
->state
= HTTP_STATE_TRACE
;
2287 else if (!strcmp(req_method
, "CONNECT"))
2288 http
->state
= HTTP_STATE_CONNECT
;
2291 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2292 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2293 return (HTTP_STATE_UNKNOWN_METHOD
);
2296 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2297 httpStateString(http
->state
)));
2299 if (!strcmp(req_version
, "HTTP/1.0"))
2301 http
->version
= HTTP_VERSION_1_0
;
2302 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2304 else if (!strcmp(req_version
, "HTTP/1.1"))
2306 http
->version
= HTTP_VERSION_1_1
;
2307 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2311 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2312 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2313 return (HTTP_STATE_UNKNOWN_VERSION
);
2316 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2317 strlcpy(uri
, req_uri
, urilen
);
2319 return (http
->state
);
2324 * 'httpReconnect()' - Reconnect to a HTTP server.
2326 * This function is deprecated. Please use the @link httpReconnect2@ function
2329 * @deprecated@ @exclude all@
2332 int /* O - 0 on success, non-zero on failure */
2333 httpReconnect(http_t
*http
) /* I - HTTP connection */
2335 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2337 return (httpReconnect2(http
, 30000, NULL
));
2342 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2346 int /* O - 0 on success, non-zero on failure */
2347 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2348 int msec
, /* I - Timeout in milliseconds */
2349 int *cancel
) /* I - Pointer to "cancel" variable */
2351 http_addrlist_t
*addr
; /* Connected address */
2353 http_addrlist_t
*current
; /* Current address */
2354 char temp
[256]; /* Temporary address string */
2358 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2362 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2369 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2372 #endif /* HAVE_SSL */
2375 * Close any previously open socket...
2380 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2382 httpAddrClose(NULL
, http
->fd
);
2388 * Reset all state (except fields, which may be reused)...
2391 http
->state
= HTTP_STATE_WAITING
;
2392 http
->version
= HTTP_VERSION_1_1
;
2393 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2394 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2395 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2396 http
->_data_remaining
= 0;
2398 http
->data_remaining
= 0;
2399 http
->hostaddr
= NULL
;
2403 * Connect to the server...
2407 for (current
= http
->addrlist
; current
; current
= current
->next
)
2408 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2409 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2410 httpAddrPort(&(current
->addr
))));
2413 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2416 * Unable to connect...
2420 http
->error
= WSAGetLastError();
2422 http
->error
= errno
;
2424 http
->status
= HTTP_STATUS_ERROR
;
2426 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2427 strerror(http
->error
)));
2432 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2434 if (http
->timeout_value
> 0)
2435 http_set_timeout(http
->fd
, http
->timeout_value
);
2437 http
->hostaddr
= &(addr
->addr
);
2441 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2444 * Always do encryption via SSL.
2447 if (_httpTLSStart(http
) != 0)
2449 httpAddrClose(NULL
, http
->fd
);
2454 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2455 return (http_tls_upgrade(http
));
2456 #endif /* HAVE_SSL */
2458 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2459 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2460 httpAddrPort(http
->hostaddr
)));
2467 * 'httpSetAuthString()' - Set the current authorization string.
2469 * This function just stores a copy of the current authorization string in
2470 * the HTTP connection object. You must still call @link httpSetField@ to set
2471 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2472 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2475 * @since CUPS 1.3/macOS 10.5@
2479 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2480 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2481 const char *data
) /* I - Auth data (NULL for none) */
2484 * Range check input...
2490 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2491 free(http
->authstring
);
2493 http
->authstring
= http
->_authstring
;
2498 * Set the current authorization string...
2501 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2504 if (len
> sizeof(http
->_authstring
))
2506 if ((temp
= malloc(len
)) == NULL
)
2507 len
= sizeof(http
->_authstring
);
2509 http
->authstring
= temp
;
2513 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2515 strlcpy(http
->authstring
, scheme
, len
);
2520 * Clear the current authorization string...
2523 http
->_authstring
[0] = '\0';
2529 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2532 * @since CUPS 1.5/macOS 10.7@
2535 int /* O - Status of call (0 = success) */
2536 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2537 cups_array_t
*credentials
) /* I - Array of credentials */
2539 if (!http
|| cupsArrayCount(credentials
) < 1)
2543 _httpFreeCredentials(http
->tls_credentials
);
2545 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2546 #endif /* HAVE_SSL */
2548 return (http
->tls_credentials
? 0 : -1);
2553 * 'httpSetCookie()' - Set the cookie value(s).
2555 * @since CUPS 1.1.19/macOS 10.3@
2559 httpSetCookie(http_t
*http
, /* I - Connection */
2560 const char *cookie
) /* I - Cookie string */
2569 http
->cookie
= strdup(cookie
);
2571 http
->cookie
= NULL
;
2576 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2578 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2579 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2581 * @since CUPS 1.7/macOS 10.9@
2585 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2586 http_field_t field
, /* I - Field index */
2587 const char *value
)/* I - Value */
2589 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2596 case HTTP_FIELD_ACCEPT_ENCODING
:
2597 if (http
->default_accept_encoding
)
2598 _cupsStrFree(http
->default_accept_encoding
);
2600 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2603 case HTTP_FIELD_SERVER
:
2604 if (http
->default_server
)
2605 _cupsStrFree(http
->default_server
);
2607 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2610 case HTTP_FIELD_USER_AGENT
:
2611 if (http
->default_user_agent
)
2612 _cupsStrFree(http
->default_user_agent
);
2614 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2618 DEBUG_puts("1httpSetDefaultField: Ignored.");
2625 * 'httpSetExpect()' - Set the Expect: header in a request.
2627 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2630 * @since CUPS 1.2/macOS 10.5@
2634 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2635 http_status_t expect
) /* I - HTTP status to expect
2636 (@code HTTP_STATUS_CONTINUE@) */
2638 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2641 http
->expect
= expect
;
2646 * 'httpSetField()' - Set the value of an HTTP header.
2650 httpSetField(http_t
*http
, /* I - HTTP connection */
2651 http_field_t field
, /* I - Field index */
2652 const char *value
) /* I - Value */
2654 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2657 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2658 field
>= HTTP_FIELD_MAX
||
2664 case HTTP_FIELD_ACCEPT_ENCODING
:
2665 if (http
->accept_encoding
)
2666 _cupsStrFree(http
->accept_encoding
);
2668 http
->accept_encoding
= _cupsStrAlloc(value
);
2671 case HTTP_FIELD_ALLOW
:
2673 _cupsStrFree(http
->allow
);
2675 http
->allow
= _cupsStrAlloc(value
);
2678 case HTTP_FIELD_SERVER
:
2680 _cupsStrFree(http
->server
);
2682 http
->server
= _cupsStrAlloc(value
);
2685 case HTTP_FIELD_WWW_AUTHENTICATE
:
2686 /* CUPS STR #4503 - don't override WWW-Authenticate for unknown auth schemes */
2687 if (http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
][0] &&
2688 _cups_strncasecmp(value
, "Basic ", 6) &&
2689 _cups_strncasecmp(value
, "Digest ", 7) &&
2690 _cups_strncasecmp(value
, "Negotiate ", 10))
2692 DEBUG_printf(("1httpSetField: Ignoring unknown auth scheme in \"%s\".", value
));
2696 /* Fall through to copy */
2699 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2703 if (field
== HTTP_FIELD_AUTHORIZATION
)
2706 * Special case for Authorization: as its contents can be
2707 * longer than HTTP_MAX_VALUE
2710 if (http
->field_authorization
)
2711 free(http
->field_authorization
);
2713 http
->field_authorization
= strdup(value
);
2715 else if (field
== HTTP_FIELD_HOST
)
2718 * Special-case for Host: as we don't want a trailing "." on the hostname and
2719 * need to bracket IPv6 numeric addresses.
2722 char *ptr
= strchr(value
, ':');
2724 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2727 * Bracket IPv6 numeric addresses...
2729 * This is slightly inefficient (basically copying twice), but is an edge
2730 * case and not worth optimizing...
2733 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2734 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2739 * Check for a trailing dot on the hostname...
2742 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2746 ptr
+= strlen(ptr
) - 1;
2754 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2755 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
2757 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
2758 http_content_coding_start(http
, value
);
2760 #endif /* HAVE_LIBZ */
2765 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2767 * @since CUPS 2.0/OS 10.10@
2772 http_t
*http
, /* I - HTTP connection */
2773 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2776 http
->keep_alive
= keep_alive
;
2781 * 'httpSetLength()' - Set the content-length and content-encoding.
2783 * @since CUPS 1.2/macOS 10.5@
2787 httpSetLength(http_t
*http
, /* I - HTTP connection */
2788 size_t length
) /* I - Length (0 for chunked) */
2790 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2797 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2799 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2803 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2804 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2805 CUPS_LLFMT
, CUPS_LLCAST length
);
2811 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2813 * The optional timeout callback receives both the HTTP connection and a user
2814 * data pointer and must return 1 to continue or 0 to error (time) out.
2816 * @since CUPS 1.5/macOS 10.7@
2821 http_t
*http
, /* I - HTTP connection */
2822 double timeout
, /* I - Number of seconds for timeout,
2823 must be greater than 0 */
2824 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2825 void *user_data
) /* I - User data pointer */
2827 if (!http
|| timeout
<= 0.0)
2830 http
->timeout_cb
= cb
;
2831 http
->timeout_data
= user_data
;
2832 http
->timeout_value
= timeout
;
2835 http_set_timeout(http
->fd
, timeout
);
2837 http_set_wait(http
);
2842 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2844 * @since CUPS 2.0/OS 10.10@
2848 httpShutdown(http_t
*http
) /* I - HTTP connection */
2850 if (!http
|| http
->fd
< 0)
2856 #endif /* HAVE_SSL */
2859 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2861 shutdown(http
->fd
, SHUT_RD
);
2867 * 'httpTrace()' - Send an TRACE request to the server.
2872 int /* O - Status of call (0 = success) */
2873 httpTrace(http_t
*http
, /* I - HTTP connection */
2874 const char *uri
) /* I - URI for trace */
2876 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2881 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2883 * Note: Unlike httpUpdate(), this function does not flush pending write data
2884 * and only retrieves a single status line from the HTTP connection.
2887 int /* O - 1 to continue, 0 to stop */
2888 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2889 http_status_t
*status
) /* O - Current HTTP status */
2891 char line
[32768], /* Line from connection... */
2892 *value
; /* Pointer to value on line */
2893 http_field_t field
; /* Field index */
2894 int major
, minor
; /* HTTP version numbers */
2897 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2900 * Grab a single line from the connection...
2903 if (!httpGets(line
, sizeof(line
), http
))
2905 *status
= HTTP_STATUS_ERROR
;
2909 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2911 if (line
[0] == '\0')
2914 * Blank line means the start of the data section (if any). Return
2915 * the result code, too...
2917 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2918 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2922 if (http
->status
== HTTP_STATUS_CONTINUE
)
2924 *status
= http
->status
;
2928 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2929 http
->digest_tries
= 0;
2932 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2934 if (_httpTLSStart(http
) != 0)
2936 httpAddrClose(NULL
, http
->fd
);
2938 *status
= http
->status
= HTTP_STATUS_ERROR
;
2942 *status
= HTTP_STATUS_CONTINUE
;
2945 #endif /* HAVE_SSL */
2947 if (http_set_length(http
) < 0)
2949 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2950 http
->error
= EINVAL
;
2951 http
->status
= *status
= HTTP_STATUS_ERROR
;
2955 switch (http
->state
)
2957 case HTTP_STATE_GET
:
2958 case HTTP_STATE_POST
:
2959 case HTTP_STATE_POST_RECV
:
2960 case HTTP_STATE_PUT
:
2963 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2964 httpStateString(http
->state
)));
2966 case HTTP_STATE_POST_SEND
:
2967 case HTTP_STATE_HEAD
:
2971 http
->state
= HTTP_STATE_WAITING
;
2973 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2978 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2979 http_content_coding_start(http
,
2980 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2981 #endif /* HAVE_LIBZ */
2983 *status
= http
->status
;
2986 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2989 * Got the beginning of a response...
2992 int intstatus
; /* Status value as an integer */
2994 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2996 *status
= http
->status
= HTTP_STATUS_ERROR
;
3000 httpClearFields(http
);
3002 http
->version
= (http_version_t
)(major
* 100 + minor
);
3003 *status
= http
->status
= (http_status_t
)intstatus
;
3005 else if ((value
= strchr(line
, ':')) != NULL
)
3012 while (_cups_isspace(*value
))
3015 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3018 * Be tolerants of servers that send unknown attribute fields...
3021 if (!_cups_strcasecmp(line
, "expect"))
3024 * "Expect: 100-continue" or similar...
3027 http
->expect
= (http_status_t
)atoi(value
);
3029 else if (!_cups_strcasecmp(line
, "cookie"))
3032 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3035 httpSetCookie(http
, value
);
3037 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3038 httpSetField(http
, field
, value
);
3041 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3046 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3047 http
->error
= EINVAL
;
3048 http
->status
= *status
= HTTP_STATUS_ERROR
;
3057 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3060 http_status_t
/* O - HTTP status */
3061 httpUpdate(http_t
*http
) /* I - HTTP connection */
3063 http_status_t status
; /* Request status */
3066 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
3069 * Flush pending data, if any...
3074 DEBUG_puts("2httpUpdate: flushing buffer...");
3076 if (httpFlushWrite(http
) < 0)
3077 return (HTTP_STATUS_ERROR
);
3081 * If we haven't issued any commands, then there is nothing to "update"...
3084 if (http
->state
== HTTP_STATE_WAITING
)
3085 return (HTTP_STATUS_CONTINUE
);
3088 * Grab all of the lines we can from the connection...
3091 while (_httpUpdate(http
, &status
));
3094 * See if there was an error...
3097 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3099 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3100 return (http
->status
);
3105 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3106 strerror(http
->error
)));
3107 http
->status
= HTTP_STATUS_ERROR
;
3108 return (HTTP_STATUS_ERROR
);
3112 * Return the current status...
3120 * '_httpWait()' - Wait for data available on a connection (no flush).
3123 int /* O - 1 if data is available, 0 otherwise */
3124 _httpWait(http_t
*http
, /* I - HTTP connection */
3125 int msec
, /* I - Milliseconds to wait */
3126 int usessl
) /* I - Use SSL context? */
3129 struct pollfd pfd
; /* Polled file descriptor */
3131 fd_set input_set
; /* select() input set */
3132 struct timeval timeout
; /* Timeout */
3133 #endif /* HAVE_POLL */
3134 int nfds
; /* Result from select()/poll() */
3137 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3141 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3146 * Check the SSL/TLS buffers for data first...
3150 if (http
->tls
&& _httpTLSPending(http
))
3152 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3155 #endif /* HAVE_SSL */
3158 * Then try doing a select() or poll() to poll the socket...
3163 pfd
.events
= POLLIN
;
3167 nfds
= poll(&pfd
, 1, msec
);
3169 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3174 FD_ZERO(&input_set
);
3175 FD_SET(http
->fd
, &input_set
);
3177 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3181 timeout
.tv_sec
= msec
/ 1000;
3182 timeout
.tv_usec
= (msec
% 1000) * 1000;
3184 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3187 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3189 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3192 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3193 WSAGetLastError() == WSAEWOULDBLOCK
));
3195 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3197 #endif /* HAVE_POLL */
3199 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3207 * 'httpWait()' - Wait for data available on a connection.
3209 * @since CUPS 1.1.19/macOS 10.3@
3212 int /* O - 1 if data is available, 0 otherwise */
3213 httpWait(http_t
*http
, /* I - HTTP connection */
3214 int msec
) /* I - Milliseconds to wait */
3217 * First see if there is data in the buffer...
3220 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3227 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3232 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3234 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3237 #endif /* HAVE_LIBZ */
3240 * Flush pending data, if any...
3245 DEBUG_puts("3httpWait: Flushing write buffer.");
3247 if (httpFlushWrite(http
) < 0)
3252 * If not, check the SSL/TLS buffers and do a select() on the connection...
3255 return (_httpWait(http
, msec
, 1));
3260 * 'httpWrite()' - Write data to a HTTP connection.
3262 * This function is deprecated. Use the httpWrite2() function which can
3263 * write more than 2GB of data.
3265 * @deprecated@ @exclude all@
3268 int /* O - Number of bytes written */
3269 httpWrite(http_t
*http
, /* I - HTTP connection */
3270 const char *buffer
, /* I - Buffer for data */
3271 int length
) /* I - Number of bytes to write */
3273 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3278 * 'httpWrite2()' - Write data to a HTTP connection.
3280 * @since CUPS 1.2/macOS 10.5@
3283 ssize_t
/* O - Number of bytes written */
3284 httpWrite2(http_t
*http
, /* I - HTTP connection */
3285 const char *buffer
, /* I - Buffer for data */
3286 size_t length
) /* I - Number of bytes to write */
3288 ssize_t bytes
; /* Bytes written */
3291 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3294 * Range check input...
3297 if (!http
|| !buffer
)
3299 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3304 * Mark activity on the connection...
3307 http
->activity
= time(NULL
);
3310 * Buffer small writes for better performance...
3314 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3316 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3320 http_content_coding_finish(http
);
3325 size_t slen
; /* Bytes to write */
3326 ssize_t sret
; /* Bytes written */
3328 http
->stream
.next_in
= (Bytef
*)buffer
;
3329 http
->stream
.avail_in
= (uInt
)length
;
3331 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3333 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3335 if (http
->stream
.avail_out
> 0)
3338 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3340 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3342 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3343 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3345 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3351 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3355 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3356 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3359 bytes
= (ssize_t
)length
;
3363 #endif /* HAVE_LIBZ */
3366 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3368 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3369 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3371 httpFlushWrite(http
);
3374 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3377 * Write to buffer...
3380 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3381 CUPS_LLCAST length
));
3383 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3384 http
->wused
+= (int)length
;
3385 bytes
= (ssize_t
)length
;
3390 * Otherwise write the data directly...
3393 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3394 CUPS_LLCAST length
));
3396 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3397 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3399 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3401 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3402 CUPS_LLCAST bytes
));
3405 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3406 http
->data_remaining
-= bytes
;
3412 * Handle end-of-request processing...
3415 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3416 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3419 * Finished with the transfer; unless we are sending POST or PUT
3424 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3425 http_content_coding_finish(http
);
3426 #endif /* HAVE_LIBZ */
3430 if (httpFlushWrite(http
) < 0)
3434 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3437 * Send a 0-length chunk at the end of the request...
3440 http_write(http
, "0\r\n\r\n", 5);
3443 * Reset the data state...
3446 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3447 http
->data_remaining
= 0;
3450 if (http
->state
== HTTP_STATE_POST_RECV
)
3452 else if (http
->state
== HTTP_STATE_POST_SEND
||
3453 http
->state
== HTTP_STATE_GET_SEND
)
3454 http
->state
= HTTP_STATE_WAITING
;
3456 http
->state
= HTTP_STATE_STATUS
;
3458 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3459 httpStateString(http
->state
)));
3462 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3469 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3471 * @since CUPS 1.7/macOS 10.9@
3474 int /* O - 0 on success, -1 on error */
3475 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3476 http_status_t status
) /* I - Status code */
3478 http_encoding_t old_encoding
; /* Old data_encoding value */
3479 off_t old_remaining
; /* Old data_remaining value */
3483 * Range check input...
3486 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3488 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3490 DEBUG_puts("1httpWriteResponse: Bad input.");
3495 * Set the various standard fields if they aren't already...
3498 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3499 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3501 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3503 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3504 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3507 if (http
->version
== HTTP_VERSION_1_1
)
3509 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3511 if (http
->keep_alive
)
3512 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3514 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3517 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3518 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3522 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3523 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3525 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3526 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3528 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3529 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3531 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3532 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3534 #endif /* HAVE_SSL */
3537 httpSetField(http
, HTTP_FIELD_SERVER
,
3538 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3541 * Set the Accept-Encoding field if it isn't already...
3544 if (!http
->accept_encoding
)
3545 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3546 http
->default_accept_encoding
? http
->default_accept_encoding
:
3548 "gzip, deflate, identity");
3551 #endif /* HAVE_LIBZ */
3554 * Send the response header...
3557 old_encoding
= http
->data_encoding
;
3558 old_remaining
= http
->data_remaining
;
3559 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3561 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3562 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3564 http
->status
= HTTP_STATUS_ERROR
;
3568 if (status
!= HTTP_STATUS_CONTINUE
)
3571 * 100 Continue doesn't have the rest of the response headers...
3574 int i
; /* Looping var */
3575 const char *value
; /* Field value */
3577 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3579 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3581 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3583 http
->status
= HTTP_STATUS_ERROR
;
3591 if (strchr(http
->cookie
, ';'))
3593 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3595 http
->status
= HTTP_STATUS_ERROR
;
3599 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3601 http
->status
= HTTP_STATUS_ERROR
;
3607 * "Click-jacking" defense (STR #4492)...
3610 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3611 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3613 http
->status
= HTTP_STATUS_ERROR
;
3618 if (httpWrite2(http
, "\r\n", 2) < 2)
3620 http
->status
= HTTP_STATUS_ERROR
;
3624 if (httpFlushWrite(http
) < 0)
3626 http
->status
= HTTP_STATUS_ERROR
;
3630 if (status
== HTTP_STATUS_CONTINUE
||
3631 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3634 * Restore the old data_encoding and data_length values...
3637 http
->data_encoding
= old_encoding
;
3638 http
->data_remaining
= old_remaining
;
3640 if (old_remaining
<= INT_MAX
)
3641 http
->_data_remaining
= (int)old_remaining
;
3643 http
->_data_remaining
= INT_MAX
;
3645 else if (http
->state
== HTTP_STATE_OPTIONS
||
3646 http
->state
== HTTP_STATE_HEAD
||
3647 http
->state
== HTTP_STATE_PUT
||
3648 http
->state
== HTTP_STATE_TRACE
||
3649 http
->state
== HTTP_STATE_CONNECT
||
3650 http
->state
== HTTP_STATE_STATUS
)
3652 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3653 "was %s.", httpStateString(http
->state
)));
3654 http
->state
= HTTP_STATE_WAITING
;
3659 * Force data_encoding and data_length to be set according to the response
3663 http_set_length(http
);
3665 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3667 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3668 "was %s.", httpStateString(http
->state
)));
3669 http
->state
= HTTP_STATE_WAITING
;
3673 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3678 * Then start any content encoding...
3681 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3682 http_content_coding_start(http
,
3683 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3684 #endif /* HAVE_LIBZ */
3694 * 'http_content_coding_finish()' - Finish doing any content encoding.
3698 http_content_coding_finish(
3699 http_t
*http
) /* I - HTTP connection */
3701 int zerr
; /* Compression status */
3702 Byte dummy
[1]; /* Dummy read buffer */
3703 size_t bytes
; /* Number of bytes to write */
3706 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3707 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3709 switch (http
->coding
)
3711 case _HTTP_CODING_DEFLATE
:
3712 case _HTTP_CODING_GZIP
:
3713 http
->stream
.next_in
= dummy
;
3714 http
->stream
.avail_in
= 0;
3718 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3719 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3723 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3725 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3726 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3728 http_write(http
, (char *)http
->sbuffer
, bytes
);
3731 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3732 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3734 while (zerr
== Z_OK
);
3736 deflateEnd(&(http
->stream
));
3738 free(http
->sbuffer
);
3739 http
->sbuffer
= NULL
;
3742 httpFlushWrite(http
);
3745 case _HTTP_CODING_INFLATE
:
3746 case _HTTP_CODING_GUNZIP
:
3747 inflateEnd(&(http
->stream
));
3748 free(http
->sbuffer
);
3749 http
->sbuffer
= NULL
;
3756 http
->coding
= _HTTP_CODING_IDENTITY
;
3761 * 'http_content_coding_start()' - Start doing content encoding.
3765 http_content_coding_start(
3766 http_t
*http
, /* I - HTTP connection */
3767 const char *value
) /* I - Value of Content-Encoding */
3769 int zerr
; /* Error/status */
3770 _http_coding_t coding
; /* Content coding value */
3773 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3775 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3777 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3781 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3783 if (http
->state
== HTTP_STATE_GET_SEND
||
3784 http
->state
== HTTP_STATE_POST_SEND
)
3785 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3786 _HTTP_CODING_GUNZIP
;
3787 else if (http
->state
== HTTP_STATE_POST_RECV
||
3788 http
->state
== HTTP_STATE_PUT_RECV
)
3789 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3790 _HTTP_CODING_GUNZIP
;
3793 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3797 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3799 if (http
->state
== HTTP_STATE_GET_SEND
||
3800 http
->state
== HTTP_STATE_POST_SEND
)
3801 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3802 _HTTP_CODING_INFLATE
;
3803 else if (http
->state
== HTTP_STATE_POST_RECV
||
3804 http
->state
== HTTP_STATE_PUT_RECV
)
3805 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3806 _HTTP_CODING_INFLATE
;
3809 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3815 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3819 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3823 case _HTTP_CODING_DEFLATE
:
3824 case _HTTP_CODING_GZIP
:
3826 httpFlushWrite(http
);
3828 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3830 http
->status
= HTTP_STATUS_ERROR
;
3831 http
->error
= errno
;
3836 * Window size for compression is 11 bits - optimal based on PWG Raster
3837 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3841 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3843 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3844 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3846 http
->status
= HTTP_STATUS_ERROR
;
3847 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3851 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3852 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3855 case _HTTP_CODING_INFLATE
:
3856 case _HTTP_CODING_GUNZIP
:
3857 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3859 http
->status
= HTTP_STATUS_ERROR
;
3860 http
->error
= errno
;
3865 * Window size for decompression is up to 15 bits (maximum supported).
3866 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3869 if ((zerr
= inflateInit2(&(http
->stream
),
3870 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3873 free(http
->sbuffer
);
3874 http
->sbuffer
= NULL
;
3875 http
->status
= HTTP_STATUS_ERROR
;
3876 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3880 http
->stream
.avail_in
= 0;
3881 http
->stream
.next_in
= http
->sbuffer
;
3888 http
->coding
= coding
;
3890 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3893 #endif /* HAVE_LIBZ */
3897 * 'http_create()' - Create an unconnected HTTP connection.
3900 static http_t
* /* O - HTTP connection */
3902 const char *host
, /* I - Hostname */
3903 int port
, /* I - Port number */
3904 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3905 int family
, /* I - Address family or AF_UNSPEC */
3906 http_encryption_t encryption
, /* I - Encryption to use */
3907 int blocking
, /* I - 1 for blocking mode */
3908 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3910 http_t
*http
; /* New HTTP connection */
3911 char service
[255]; /* Service name */
3912 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3915 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
));
3917 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3923 * Lookup the host...
3928 myaddrlist
= httpAddrCopyList(addrlist
);
3932 snprintf(service
, sizeof(service
), "%d", port
);
3934 myaddrlist
= httpAddrGetList(host
, family
, service
);
3941 * Allocate memory for the structure...
3944 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3946 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3947 httpAddrFreeList(addrlist
);
3952 * Initialize the HTTP data...
3956 http
->activity
= time(NULL
);
3957 http
->addrlist
= myaddrlist
;
3958 http
->blocking
= blocking
;
3961 http
->gssctx
= GSS_C_NO_CONTEXT
;
3962 http
->gssname
= GSS_C_NO_NAME
;
3963 #endif /* HAVE_GSSAPI */
3964 http
->status
= HTTP_STATUS_CONTINUE
;
3965 http
->version
= HTTP_VERSION_1_1
;
3968 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3970 if (port
== 443) /* Always use encryption for https */
3971 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3973 http
->encryption
= encryption
;
3975 http_set_wait(http
);
3978 * Return the new structure...
3987 * 'http_debug_hex()' - Do a hex dump of a buffer.
3991 http_debug_hex(const char *prefix
, /* I - Prefix for line */
3992 const char *buffer
, /* I - Buffer to dump */
3993 int bytes
) /* I - Bytes to dump */
3995 int i
, j
, /* Looping vars */
3996 ch
; /* Current character */
3997 char line
[255], /* Line buffer */
3998 *start
, /* Start of line after prefix */
3999 *ptr
; /* Pointer into line */
4002 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4005 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4007 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4008 start
= line
+ strlen(line
);
4010 for (i
= 0; i
< bytes
; i
+= 16)
4012 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4013 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4017 memcpy(ptr
, " ", 3);
4022 memcpy(ptr
, " ", 3);
4025 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4027 ch
= buffer
[i
+ j
] & 255;
4029 if (ch
< ' ' || ch
>= 127)
4043 * 'http_read()' - Read a buffer from a HTTP connection.
4045 * This function does the low-level read from the socket, retrying and timing
4049 static ssize_t
/* O - Number of bytes read or -1 on error */
4050 http_read(http_t
*http
, /* I - HTTP connection */
4051 char *buffer
, /* I - Buffer */
4052 size_t length
) /* I - Maximum bytes to read */
4054 ssize_t bytes
; /* Bytes read */
4057 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4059 if (!http
->blocking
)
4061 while (!httpWait(http
, http
->wait_value
))
4063 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4066 DEBUG_puts("2http_read: Timeout.");
4071 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4077 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4079 #endif /* HAVE_SSL */
4080 bytes
= recv(http
->fd
, buffer
, length
, 0);
4085 if (WSAGetLastError() != WSAEINTR
)
4087 http
->error
= WSAGetLastError();
4090 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4092 if (!http
->timeout_cb
||
4093 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4095 http
->error
= WSAEWOULDBLOCK
;
4100 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4102 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4104 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4106 http
->error
= errno
;
4109 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4111 http
->error
= errno
;
4115 else if (errno
!= EINTR
)
4117 http
->error
= errno
;
4125 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4126 CUPS_LLCAST bytes
));
4129 http_debug_hex("http_read", buffer
, (int)bytes
);
4135 if (WSAGetLastError() == WSAEINTR
)
4138 http
->error
= WSAGetLastError();
4140 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4143 http
->error
= errno
;
4146 else if (bytes
== 0)
4148 http
->error
= EPIPE
;
4157 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4159 * This function reads data from the HTTP buffer or from the socket, as needed.
4162 static ssize_t
/* O - Number of bytes read or -1 on error */
4163 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4164 char *buffer
, /* I - Buffer */
4165 size_t length
) /* I - Maximum bytes to read */
4167 ssize_t bytes
; /* Bytes read */
4170 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4174 if (length
> (size_t)http
->used
)
4175 bytes
= (ssize_t
)http
->used
;
4177 bytes
= (ssize_t
)length
;
4179 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4182 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4183 http
->used
-= (int)bytes
;
4186 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4189 bytes
= http_read(http
, buffer
, length
);
4196 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4198 * This function reads and validates the chunk length, then does a buffered read
4199 * returning the number of bytes placed in the buffer.
4202 static ssize_t
/* O - Number of bytes read or -1 on error */
4203 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4204 char *buffer
, /* I - Buffer */
4205 size_t length
) /* I - Maximum bytes to read */
4207 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4209 if (http
->data_remaining
<= 0)
4211 char len
[32]; /* Length string */
4213 if (!httpGets(len
, sizeof(len
), http
))
4215 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4221 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4222 if (!httpGets(len
, sizeof(len
), http
))
4224 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4229 http
->data_remaining
= strtoll(len
, NULL
, 16);
4231 if (http
->data_remaining
< 0)
4233 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4234 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4238 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4239 len
, CUPS_LLCAST http
->data_remaining
));
4241 if (http
->data_remaining
== 0)
4244 * 0-length chunk, grab trailing blank line...
4247 httpGets(len
, sizeof(len
), http
);
4251 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4252 CUPS_LLCAST http
->data_remaining
));
4254 if (http
->data_remaining
<= 0)
4256 else if (length
> (size_t)http
->data_remaining
)
4257 length
= (size_t)http
->data_remaining
;
4259 return (http_read_buffered(http
, buffer
, length
));
4264 * 'http_send()' - Send a request with all fields and the trailing blank line.
4267 static int /* O - 0 on success, non-zero on error */
4268 http_send(http_t
*http
, /* I - HTTP connection */
4269 http_state_t request
, /* I - Request code */
4270 const char *uri
) /* I - URI */
4272 int i
; /* Looping var */
4273 char buf
[1024]; /* Encoded URI buffer */
4274 const char *value
; /* Field value */
4275 static const char * const codes
[] = /* Request code strings */
4295 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4297 if (http
== NULL
|| uri
== NULL
)
4301 * Set the User-Agent field if it isn't already...
4304 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4306 if (http
->default_user_agent
)
4307 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4309 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4313 * Set the Accept-Encoding field if it isn't already...
4316 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4317 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4318 http
->default_accept_encoding
);
4321 * Encode the URI as needed...
4324 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4327 * See if we had an error the last time around; if so, reconnect...
4330 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4331 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4333 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4334 http
->fd
, http
->status
, http
->tls_upgrade
));
4336 if (httpReconnect2(http
, 30000, NULL
))
4341 * Flush any written data that is pending...
4346 if (httpFlushWrite(http
) < 0)
4347 if (httpReconnect2(http
, 30000, NULL
))
4352 * Send the request header...
4355 http
->state
= request
;
4356 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4358 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4361 http
->status
= HTTP_STATUS_CONTINUE
;
4364 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4366 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4367 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4369 #endif /* HAVE_SSL */
4371 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4373 http
->status
= HTTP_STATUS_ERROR
;
4377 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4378 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4380 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4382 if (i
== HTTP_FIELD_HOST
)
4384 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4385 httpAddrPort(http
->hostaddr
)) < 1)
4387 http
->status
= HTTP_STATUS_ERROR
;
4391 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4393 http
->status
= HTTP_STATUS_ERROR
;
4399 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4401 http
->status
= HTTP_STATUS_ERROR
;
4405 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4406 http
->mode
, http
->state
));
4408 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4409 (http
->state
== HTTP_STATE_POST_RECV
||
4410 http
->state
== HTTP_STATE_PUT_RECV
))
4411 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4413 http
->status
= HTTP_STATUS_ERROR
;
4417 if (httpPrintf(http
, "\r\n") < 1)
4419 http
->status
= HTTP_STATUS_ERROR
;
4423 if (httpFlushWrite(http
) < 0)
4426 http_set_length(http
);
4427 httpClearFields(http
);
4430 * The Kerberos and AuthRef authentication strings can only be used once...
4433 if (http
->field_authorization
&& http
->authstring
&&
4434 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4435 !strncmp(http
->authstring
, "AuthRef", 7)))
4437 http
->_authstring
[0] = '\0';
4439 if (http
->authstring
!= http
->_authstring
)
4440 free(http
->authstring
);
4442 http
->authstring
= http
->_authstring
;
4450 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4453 static off_t
/* O - Remainder or -1 on error */
4454 http_set_length(http_t
*http
) /* I - Connection */
4456 off_t remaining
; /* Remainder */
4459 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4461 if ((remaining
= httpGetLength2(http
)) >= 0)
4463 if (http
->mode
== _HTTP_MODE_SERVER
&&
4464 http
->state
!= HTTP_STATE_GET_SEND
&&
4465 http
->state
!= HTTP_STATE_PUT
&&
4466 http
->state
!= HTTP_STATE_POST
&&
4467 http
->state
!= HTTP_STATE_POST_SEND
)
4469 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4473 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4476 DEBUG_puts("1http_set_length: Setting data_encoding to "
4477 "HTTP_ENCODING_CHUNKED.");
4478 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4482 DEBUG_puts("1http_set_length: Setting data_encoding to "
4483 "HTTP_ENCODING_LENGTH.");
4484 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4487 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4488 CUPS_LLCAST remaining
));
4489 http
->data_remaining
= remaining
;
4491 if (remaining
<= INT_MAX
)
4492 http
->_data_remaining
= (int)remaining
;
4494 http
->_data_remaining
= INT_MAX
;
4501 * 'http_set_timeout()' - Set the socket timeout values.
4505 http_set_timeout(int fd
, /* I - File descriptor */
4506 double timeout
) /* I - Timeout in seconds */
4509 DWORD tv
= (DWORD
)(timeout
* 1000);
4510 /* Timeout in milliseconds */
4512 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4513 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4516 struct timeval tv
; /* Timeout in secs and usecs */
4518 tv
.tv_sec
= (int)timeout
;
4519 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4521 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4522 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4528 * 'http_set_wait()' - Set the default wait value for reads.
4532 http_set_wait(http_t
*http
) /* I - HTTP connection */
4536 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4538 if (http
->wait_value
<= 0)
4539 http
->wait_value
= 60000;
4542 http
->wait_value
= 10000;
4548 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4551 static int /* O - Status of connection */
4552 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4554 int ret
; /* Return value */
4555 http_t myhttp
; /* Local copy of HTTP data */
4558 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4561 * Flush the connection to make sure any previous "Upgrade" message
4568 * Copy the HTTP data to a local variable so we can do the OPTIONS
4569 * request without interfering with the existing request data...
4572 memcpy(&myhttp
, http
, sizeof(myhttp
));
4575 * Send an OPTIONS request to the server, requiring SSL or TLS
4576 * encryption on the link...
4579 http
->tls_upgrade
= 1;
4580 http
->field_authorization
= NULL
; /* Don't free the auth string */
4582 httpClearFields(http
);
4583 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4584 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4586 if ((ret
= httpOptions(http
, "*")) == 0)
4589 * Wait for the secure connection...
4592 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4596 * Restore the HTTP request data...
4599 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4600 http
->data_encoding
= myhttp
.data_encoding
;
4601 http
->data_remaining
= myhttp
.data_remaining
;
4602 http
->_data_remaining
= myhttp
._data_remaining
;
4603 http
->expect
= myhttp
.expect
;
4604 http
->field_authorization
= myhttp
.field_authorization
;
4605 http
->digest_tries
= myhttp
.digest_tries
;
4606 http
->tls_upgrade
= 0;
4609 * See if we actually went secure...
4615 * Server does not support HTTP upgrade...
4618 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4620 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4621 httpAddrClose(NULL
, http
->fd
);
4630 #endif /* HAVE_SSL */
4634 * 'http_write()' - Write a buffer to a HTTP connection.
4637 static ssize_t
/* O - Number of bytes written */
4638 http_write(http_t
*http
, /* I - HTTP connection */
4639 const char *buffer
, /* I - Buffer for data */
4640 size_t length
) /* I - Number of bytes to write */
4642 ssize_t tbytes
, /* Total bytes sent */
4643 bytes
; /* Bytes sent */
4646 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4652 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4654 if (http
->timeout_cb
)
4657 struct pollfd pfd
; /* Polled file descriptor */
4659 fd_set output_set
; /* Output ready for write? */
4660 struct timeval timeout
; /* Timeout value */
4661 #endif /* HAVE_POLL */
4662 int nfds
; /* Result from select()/poll() */
4668 pfd
.events
= POLLOUT
;
4670 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4671 (errno
== EINTR
|| errno
== EAGAIN
))
4677 FD_ZERO(&output_set
);
4678 FD_SET(http
->fd
, &output_set
);
4680 timeout
.tv_sec
= http
->wait_value
/ 1000;
4681 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4683 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4686 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4687 WSAGetLastError() == WSAEWOULDBLOCK
));
4689 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4691 #endif /* HAVE_POLL */
4695 http
->error
= errno
;
4698 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4701 http
->error
= WSAEWOULDBLOCK
;
4703 http
->error
= EWOULDBLOCK
;
4713 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4715 #endif /* HAVE_SSL */
4716 bytes
= send(http
->fd
, buffer
, length
, 0);
4718 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4719 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4724 if (WSAGetLastError() == WSAEINTR
)
4726 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4728 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4731 http
->error
= WSAGetLastError();
4733 else if (WSAGetLastError() != http
->error
&&
4734 WSAGetLastError() != WSAECONNRESET
)
4736 http
->error
= WSAGetLastError();
4743 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4745 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4747 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4750 http
->error
= errno
;
4752 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4754 http
->error
= errno
;
4759 DEBUG_printf(("3http_write: error writing data (%s).",
4760 strerror(http
->error
)));
4767 length
-= (size_t)bytes
;
4771 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4774 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4781 * 'http_write_chunk()' - Write a chunked buffer.
4784 static ssize_t
/* O - Number bytes written */
4785 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4786 const char *buffer
, /* I - Buffer to write */
4787 size_t length
) /* I - Length of buffer */
4789 char header
[16]; /* Chunk header */
4790 ssize_t bytes
; /* Bytes written */
4793 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4796 * Write the chunk header, data, and trailer.
4799 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4800 if (http_write(http
, header
, strlen(header
)) < 0)
4802 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4806 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4808 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4812 if (http_write(http
, "\r\n", 2) < 0)
4814 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");