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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
14 * Include necessary headers...
17 #include "cups-private.h"
24 # include <sys/time.h>
25 # include <sys/resource.h>
29 #endif /* HAVE_POLL */
37 static void http_content_coding_finish(http_t
*http
);
38 static void http_content_coding_start(http_t
*http
,
40 #endif /* HAVE_LIBZ */
41 static http_t
*http_create(const char *host
, int port
,
42 http_addrlist_t
*addrlist
, int family
,
43 http_encryption_t encryption
,
44 int blocking
, _http_mode_t mode
);
46 static void http_debug_hex(const char *prefix
, const char *buffer
,
49 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
50 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
51 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
52 static int http_send(http_t
*http
, http_state_t request
,
54 static ssize_t
http_write(http_t
*http
, const char *buffer
,
56 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
58 static off_t
http_set_length(http_t
*http
);
59 static void http_set_timeout(int fd
, double timeout
);
60 static void http_set_wait(http_t
*http
);
63 static int http_tls_upgrade(http_t
*http
);
71 static const char * const http_fields
[] =
88 "If-Unmodified-since",
107 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
108 * specified listening socket.
110 * @since CUPS 1.7/macOS 10.9@
113 http_t
* /* O - HTTP connection or @code NULL@ */
114 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
115 int blocking
) /* I - 1 if the connection should be
116 blocking, 0 otherwise */
118 http_t
*http
; /* HTTP connection */
119 http_addrlist_t addrlist
; /* Dummy address list */
120 socklen_t addrlen
; /* Length of address */
121 int val
; /* Socket option value */
125 * Range check input...
132 * Create the client connection...
135 memset(&addrlist
, 0, sizeof(addrlist
));
137 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
138 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
139 _HTTP_MODE_SERVER
)) == NULL
)
143 * Accept the client and get the remote address...
146 addrlen
= sizeof(http_addr_t
);
148 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
151 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
157 http
->hostaddr
= &(http
->addrlist
->addr
);
159 if (httpAddrLocalhost(http
->hostaddr
))
160 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
162 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
166 * Disable SIGPIPE for this socket.
170 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
171 #endif /* SO_NOSIGPIPE */
174 * Using TCP_NODELAY improves responsiveness, especially on systems
175 * with a slow loopback interface. Since we write large buffers
176 * when sending print files and requests, there shouldn't be any
177 * performance penalty for this...
181 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
185 * Close this socket when starting another process...
188 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
189 #endif /* FD_CLOEXEC */
196 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
198 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
200 * @since CUPS 1.5/macOS 10.7@
203 int /* O - 0 on success, -1 on error */
205 cups_array_t
*credentials
, /* I - Credentials array */
206 const void *data
, /* I - PEM-encoded X.509 data */
207 size_t datalen
) /* I - Length of data */
209 http_credential_t
*credential
; /* Credential data */
212 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
214 credential
->datalen
= datalen
;
216 if ((credential
->data
= malloc(datalen
)) != NULL
)
218 memcpy(credential
->data
, data
, datalen
);
219 cupsArrayAdd(credentials
, credential
);
231 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
235 httpBlocking(http_t
*http
, /* I - HTTP connection */
236 int b
) /* I - 1 = blocking, 0 = non-blocking */
247 * 'httpCheck()' - Check to see if there is a pending response from the server.
250 int /* O - 0 = no data, 1 = data available */
251 httpCheck(http_t
*http
) /* I - HTTP connection */
253 return (httpWait(http
, 0));
258 * 'httpClearCookie()' - Clear the cookie value(s).
260 * @since CUPS 1.1.19/macOS 10.3@
264 httpClearCookie(http_t
*http
) /* I - HTTP connection */
278 * 'httpClearFields()' - Clear HTTP request fields.
282 httpClearFields(http_t
*http
) /* I - HTTP connection */
284 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
288 memset(http
->fields
, 0, sizeof(http
->fields
));
290 if (http
->mode
== _HTTP_MODE_CLIENT
)
292 if (http
->hostname
[0] == '/')
293 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
295 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
298 if (http
->field_authorization
)
300 free(http
->field_authorization
);
301 http
->field_authorization
= NULL
;
304 if (http
->accept_encoding
)
306 _cupsStrFree(http
->accept_encoding
);
307 http
->accept_encoding
= NULL
;
312 _cupsStrFree(http
->allow
);
318 _cupsStrFree(http
->server
);
322 if (http
->www_authenticate
)
324 free(http
->www_authenticate
);
325 http
->www_authenticate
= NULL
;
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 Authorization: as its contents can be
977 * longer than HTTP_MAX_VALUE...
980 return (http
->field_authorization
);
983 return (http
->fields
[field
]);
985 case HTTP_FIELD_WWW_AUTHENTICATE
:
986 if (http
->www_authenticate
)
989 * Special case for WWW-Authenticate: as its contents can be
990 * longer than HTTP_MAX_VALUE...
993 return (http
->www_authenticate
);
996 return (http
->fields
[field
]);
999 return (http
->fields
[field
]);
1005 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
1007 * @since CUPS 2.0/OS 10.10@
1010 http_keepalive_t
/* O - Keep-Alive state */
1011 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
1013 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1018 * 'httpGetLength()' - Get the amount of data remaining from the
1019 * content-length or transfer-encoding fields.
1021 * This function is deprecated and will not return lengths larger than
1022 * 2^31 - 1; use httpGetLength2() instead.
1024 * @deprecated@ @exclude all@
1027 int /* O - Content length */
1028 httpGetLength(http_t
*http
) /* I - HTTP connection */
1031 * Get the read content length and return the 32-bit value.
1036 httpGetLength2(http
);
1038 return (http
->_data_remaining
);
1046 * 'httpGetLength2()' - Get the amount of data remaining from the
1047 * content-length or transfer-encoding fields.
1049 * This function returns the complete content length, even for
1050 * content larger than 2^31 - 1.
1052 * @since CUPS 1.2/macOS 10.5@
1055 off_t
/* O - Content length */
1056 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1058 off_t remaining
; /* Remaining length */
1061 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1066 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1068 DEBUG_puts("4httpGetLength2: chunked request!");
1074 * The following is a hack for HTTP servers that don't send a
1075 * Content-Length or Transfer-Encoding field...
1077 * If there is no Content-Length then the connection must close
1078 * after the transfer is complete...
1081 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1084 * Default content length is 0 for errors and certain types of operations,
1085 * and 2^31-1 for other successful requests...
1088 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1089 http
->state
== HTTP_STATE_OPTIONS
||
1090 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1091 http
->state
== HTTP_STATE_HEAD
||
1092 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1093 http
->state
== HTTP_STATE_DELETE
||
1094 http
->state
== HTTP_STATE_TRACE
||
1095 http
->state
== HTTP_STATE_CONNECT
)
1098 remaining
= 2147483647;
1100 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1104 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1105 CUPS_LLCAST remaining
));
1113 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1115 * @since CUPS 2.0/OS 10.10@
1118 size_t /* O - Number of bytes buffered */
1119 httpGetPending(http_t
*http
) /* I - HTTP connection */
1121 return (http
? (size_t)http
->wused
: 0);
1126 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1128 * @since CUPS 2.0/OS 10.10@
1131 size_t /* O - Number of bytes available */
1132 httpGetReady(http_t
*http
) /* I - HTTP connection */
1136 else if (http
->used
> 0)
1137 return ((size_t)http
->used
);
1140 return (_httpTLSPending(http
));
1141 #endif /* HAVE_SSL */
1148 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1149 * body or current chunk.
1151 * The @link httpIsChunked@ function can be used to determine whether the
1152 * message body is chunked or fixed-length.
1154 * @since CUPS 2.0/OS 10.10@
1157 size_t /* O - Remaining bytes */
1158 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1160 return (http
? (size_t)http
->data_remaining
: 0);
1165 * 'httpGets()' - Get a line of text from a HTTP connection.
1168 char * /* O - Line or @code NULL@ */
1169 httpGets(char *line
, /* I - Line to read into */
1170 int length
, /* I - Max length of buffer */
1171 http_t
*http
) /* I - HTTP connection */
1173 char *lineptr
, /* Pointer into line */
1174 *lineend
, /* End of line */
1175 *bufptr
, /* Pointer into input buffer */
1176 *bufend
; /* Pointer to end of buffer */
1177 ssize_t bytes
; /* Number of bytes read */
1178 int eol
; /* End-of-line? */
1181 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1183 if (!http
|| !line
|| length
<= 1)
1187 * Read a line from the buffer...
1192 lineend
= line
+ length
- 1;
1195 while (lineptr
< lineend
)
1198 * Pre-load the buffer as needed...
1207 while (http
->used
== 0)
1210 * No newline; see if there is more data to be read...
1213 while (!_httpWait(http
, http
->wait_value
, 1))
1215 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1218 DEBUG_puts("3httpGets: Timed out!");
1220 http
->error
= WSAETIMEDOUT
;
1222 http
->error
= ETIMEDOUT
;
1227 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1229 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1234 * Nope, can't get a line this time...
1238 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1240 if (WSAGetLastError() == WSAEINTR
)
1242 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1244 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1247 http
->error
= WSAGetLastError();
1249 else if (WSAGetLastError() != http
->error
)
1251 http
->error
= WSAGetLastError();
1256 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1260 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1262 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1264 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1267 http
->error
= errno
;
1269 else if (errno
!= http
->error
)
1271 http
->error
= errno
;
1278 else if (bytes
== 0)
1280 http
->error
= EPIPE
;
1286 * Yup, update the amount used...
1289 http
->used
+= (int)bytes
;
1293 * Now copy as much of the current line as possible...
1296 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1297 lineptr
< lineend
&& bufptr
< bufend
;)
1299 if (*bufptr
== 0x0a)
1305 else if (*bufptr
== 0x0d)
1308 *lineptr
++ = *bufptr
++;
1311 http
->used
-= (int)(bufptr
- http
->buffer
);
1313 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1321 http
->activity
= time(NULL
);
1325 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1331 DEBUG_puts("3httpGets: No new line available!");
1338 * 'httpGetState()' - Get the current state of the HTTP request.
1341 http_state_t
/* O - HTTP state */
1342 httpGetState(http_t
*http
) /* I - HTTP connection */
1344 return (http
? http
->state
: HTTP_STATE_ERROR
);
1349 * 'httpGetStatus()' - Get the status of the last HTTP request.
1351 * @since CUPS 1.2/macOS 10.5@
1354 http_status_t
/* O - HTTP status */
1355 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1357 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1362 * 'httpGetSubField()' - Get a sub-field value.
1364 * @deprecated@ @exclude all@
1367 char * /* O - Value or @code NULL@ */
1368 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1369 http_field_t field
, /* I - Field index */
1370 const char *name
, /* I - Name of sub-field */
1371 char *value
) /* O - Value string */
1373 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1378 * 'httpGetSubField2()' - Get a sub-field value.
1380 * @since CUPS 1.2/macOS 10.5@
1383 char * /* O - Value or @code NULL@ */
1384 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1385 http_field_t field
, /* I - Field index */
1386 const char *name
, /* I - Name of sub-field */
1387 char *value
, /* O - Value string */
1388 int valuelen
) /* I - Size of value buffer */
1390 const char *fptr
; /* Pointer into field */
1391 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1392 *ptr
, /* Pointer into string buffer */
1393 *end
; /* End of value buffer */
1395 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1397 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1398 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1401 end
= value
+ valuelen
- 1;
1403 for (fptr
= http
->fields
[field
]; *fptr
;)
1406 * Skip leading whitespace...
1409 while (_cups_isspace(*fptr
))
1419 * Get the sub-field name...
1423 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1424 ptr
< (temp
+ sizeof(temp
) - 1);
1429 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1432 * Skip trailing chars up to the '='...
1435 while (_cups_isspace(*fptr
))
1445 * Skip = and leading whitespace...
1450 while (_cups_isspace(*fptr
))
1456 * Read quoted string...
1459 for (ptr
= value
, fptr
++;
1460 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1465 while (*fptr
&& *fptr
!= '\"')
1474 * Read unquoted string...
1478 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1483 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1487 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1490 * See if this is the one...
1493 if (!strcmp(name
, temp
))
1495 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1502 DEBUG_puts("3httpGetSubField2: Returning NULL");
1509 * 'httpGetVersion()' - Get the HTTP version at the other end.
1512 http_version_t
/* O - Version number */
1513 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1515 return (http
? http
->version
: HTTP_VERSION_1_0
);
1520 * 'httpHead()' - Send a HEAD request to the server.
1523 int /* O - Status of call (0 = success) */
1524 httpHead(http_t
*http
, /* I - HTTP connection */
1525 const char *uri
) /* I - URI for head */
1527 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1528 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1533 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1534 * default HTTP proxy (if any).
1538 httpInitialize(void)
1540 static int initialized
= 0; /* Have we been called before? */
1542 WSADATA winsockdata
; /* WinSock data */
1549 _cupsGlobalUnlock();
1554 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1556 #elif !defined(SO_NOSIGPIPE)
1558 * Ignore SIGPIPE signals...
1562 sigset(SIGPIPE
, SIG_IGN
);
1564 # elif defined(HAVE_SIGACTION)
1565 struct sigaction action
; /* POSIX sigaction data */
1568 memset(&action
, 0, sizeof(action
));
1569 action
.sa_handler
= SIG_IGN
;
1570 sigaction(SIGPIPE
, &action
, NULL
);
1573 signal(SIGPIPE
, SIG_IGN
);
1574 # endif /* !SO_NOSIGPIPE */
1578 _httpTLSInitialize();
1579 # endif /* HAVE_SSL */
1582 _cupsGlobalUnlock();
1587 * 'httpIsChunked()' - Report whether a message body is chunked.
1589 * This function returns non-zero if the message body is composed of
1590 * variable-length chunks.
1592 * @since CUPS 2.0/OS 10.10@
1595 int /* O - 1 if chunked, 0 if not */
1596 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1598 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1603 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1605 * This function returns non-zero if the connection is currently encrypted.
1607 * @since CUPS 2.0/OS 10.10@
1610 int /* O - 1 if encrypted, 0 if not */
1611 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1613 return (http
? http
->tls
!= NULL
: 0);
1618 * 'httpOptions()' - Send an OPTIONS request to the server.
1621 int /* O - Status of call (0 = success) */
1622 httpOptions(http_t
*http
, /* I - HTTP connection */
1623 const char *uri
) /* I - URI for options */
1625 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1630 * 'httpPeek()' - Peek at data from a HTTP connection.
1632 * This function copies available data from the given HTTP connection, reading
1633 * a buffer as needed. The data is still available for reading using
1636 * For non-blocking connections the usual timeouts apply.
1638 * @since CUPS 1.7/macOS 10.9@
1641 ssize_t
/* O - Number of bytes copied */
1642 httpPeek(http_t
*http
, /* I - HTTP connection */
1643 char *buffer
, /* I - Buffer for data */
1644 size_t length
) /* I - Maximum number of bytes */
1646 ssize_t bytes
; /* Bytes read */
1647 char len
[32]; /* Length string */
1650 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1652 if (http
== NULL
|| buffer
== NULL
)
1655 http
->activity
= time(NULL
);
1661 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1662 http
->data_remaining
<= 0)
1664 DEBUG_puts("2httpPeek: Getting chunk length...");
1666 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1668 DEBUG_puts("1httpPeek: Could not get length!");
1674 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1675 if (!httpGets(len
, sizeof(len
), http
))
1677 DEBUG_puts("1httpPeek: Could not get chunk length.");
1682 http
->data_remaining
= strtoll(len
, NULL
, 16);
1684 if (http
->data_remaining
< 0)
1686 DEBUG_puts("1httpPeek: Negative chunk length!");
1691 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1692 CUPS_LLCAST http
->data_remaining
));
1694 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1697 * A zero-length chunk ends a transfer; unless we are reading POST
1702 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1703 http_content_coding_finish(http
);
1704 #endif /* HAVE_LIBZ */
1706 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1707 httpGets(len
, sizeof(len
), http
);
1709 if (http
->state
== HTTP_STATE_POST_RECV
)
1712 http
->state
= HTTP_STATE_STATUS
;
1714 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1715 httpStateString(http
->state
)));
1718 * Prevent future reads for this request...
1721 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1725 else if (length
> (size_t)http
->data_remaining
)
1726 length
= (size_t)http
->data_remaining
;
1729 if (http
->used
== 0 &&
1730 (http
->coding
== _HTTP_CODING_IDENTITY
||
1731 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1733 if (http
->used
== 0)
1734 #endif /* HAVE_LIBZ */
1737 * Buffer small reads for better performance...
1740 ssize_t buflen
; /* Length of read for buffer */
1742 if (!http
->blocking
)
1744 while (!httpWait(http
, http
->wait_value
))
1746 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1753 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1754 buflen
= sizeof(http
->buffer
);
1756 buflen
= (ssize_t
)http
->data_remaining
;
1758 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1759 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1761 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1762 CUPS_LLCAST bytes
));
1766 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1769 http
->used
= (int)bytes
;
1774 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1776 # ifdef HAVE_INFLATECOPY
1777 int zerr
; /* Decompressor error */
1778 z_stream stream
; /* Copy of decompressor stream */
1780 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1782 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1783 /* Number of bytes to copy */
1785 if (http
->stream
.avail_in
> 0 &&
1786 http
->stream
.next_in
> http
->sbuffer
)
1787 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1789 http
->stream
.next_in
= http
->sbuffer
;
1791 if (buflen
> (size_t)http
->data_remaining
)
1792 buflen
= (size_t)http
->data_remaining
;
1794 if (buflen
> (size_t)http
->used
)
1795 buflen
= (size_t)http
->used
;
1797 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1798 "decompression buffer.", (int)buflen
));
1800 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1801 http
->stream
.avail_in
+= buflen
;
1802 http
->used
-= (int)buflen
;
1803 http
->data_remaining
-= (off_t
)buflen
;
1806 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1809 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1810 (int)http
->stream
.avail_in
));
1812 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1814 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1815 http
->error
= ENOMEM
;
1819 stream
.next_out
= (Bytef
*)buffer
;
1820 stream
.avail_out
= (uInt
)length
;
1822 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1823 inflateEnd(&stream
);
1827 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1829 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1836 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1839 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1840 "work with compressed streams.");
1842 # endif /* HAVE_INFLATECOPY */
1845 #endif /* HAVE_LIBZ */
1848 if (length
> (size_t)http
->used
)
1849 length
= (size_t)http
->used
;
1851 bytes
= (ssize_t
)length
;
1853 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1856 memcpy(buffer
, http
->buffer
, length
);
1864 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1867 http
->error
= WSAGetLastError();
1869 if (errno
== EINTR
|| errno
== EAGAIN
)
1872 http
->error
= errno
;
1875 else if (bytes
== 0)
1877 http
->error
= EPIPE
;
1886 * 'httpPost()' - Send a POST request to the server.
1889 int /* O - Status of call (0 = success) */
1890 httpPost(http_t
*http
, /* I - HTTP connection */
1891 const char *uri
) /* I - URI for post */
1893 return (http_send(http
, HTTP_STATE_POST
, uri
));
1898 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1903 int /* O - Number of bytes written */
1904 httpPrintf(http_t
*http
, /* I - HTTP connection */
1905 const char *format
, /* I - printf-style format string */
1906 ...) /* I - Additional args as needed */
1908 ssize_t bytes
; /* Number of bytes to write */
1909 char buf
[16384]; /* Buffer for formatted string */
1910 va_list ap
; /* Variable argument pointer */
1913 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1915 va_start(ap
, format
);
1916 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1919 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1921 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1922 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1927 DEBUG_puts("4httpPrintf: flushing existing data...");
1929 if (httpFlushWrite(http
) < 0)
1933 return ((int)http_write(http
, buf
, (size_t)bytes
));
1939 * 'httpPut()' - Send a PUT request to the server.
1942 int /* O - Status of call (0 = success) */
1943 httpPut(http_t
*http
, /* I - HTTP connection */
1944 const char *uri
) /* I - URI to put */
1946 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1947 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1952 * 'httpRead()' - Read data from a HTTP connection.
1954 * This function is deprecated. Use the httpRead2() function which can
1955 * read more than 2GB of data.
1957 * @deprecated@ @exclude all@
1960 int /* O - Number of bytes read */
1961 httpRead(http_t
*http
, /* I - HTTP connection */
1962 char *buffer
, /* I - Buffer for data */
1963 int length
) /* I - Maximum number of bytes */
1965 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1970 * 'httpRead2()' - Read data from a HTTP connection.
1972 * @since CUPS 1.2/macOS 10.5@
1975 ssize_t
/* O - Number of bytes read */
1976 httpRead2(http_t
*http
, /* I - HTTP connection */
1977 char *buffer
, /* I - Buffer for data */
1978 size_t length
) /* I - Maximum number of bytes */
1980 ssize_t bytes
; /* Bytes read */
1984 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
));
1986 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
));
1987 #endif /* HAVE_LIBZ */
1989 if (http
== NULL
|| buffer
== NULL
)
1992 http
->activity
= time(NULL
);
1999 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2003 if (http
->stream
.avail_in
> 0)
2005 int zerr
; /* Decompressor error */
2007 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
2008 (int)http
->stream
.avail_in
, (int)length
));
2010 http
->stream
.next_out
= (Bytef
*)buffer
;
2011 http
->stream
.avail_out
= (uInt
)length
;
2013 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2015 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2017 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2024 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2026 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2027 http
->stream
.avail_in
, http
->stream
.avail_out
,
2035 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2036 /* Additional bytes for buffer */
2040 if (http
->stream
.avail_in
> 0 &&
2041 http
->stream
.next_in
> http
->sbuffer
)
2042 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2044 http
->stream
.next_in
= http
->sbuffer
;
2046 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2047 "decompression buffer.", (int)buflen
));
2049 if (http
->data_remaining
> 0)
2051 if (buflen
> http
->data_remaining
)
2052 buflen
= (ssize_t
)http
->data_remaining
;
2054 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2056 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2057 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2063 else if (bytes
== 0)
2066 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2067 "decompression buffer.", CUPS_LLCAST bytes
));
2069 http
->data_remaining
-= bytes
;
2070 http
->stream
.avail_in
+= (uInt
)bytes
;
2072 if (http
->data_remaining
<= 0 &&
2073 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2076 * Read the trailing blank line now...
2079 char len
[32]; /* Length string */
2081 httpGets(len
, sizeof(len
), http
);
2093 #endif /* HAVE_LIBZ */
2094 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2096 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2098 http
->data_remaining
-= bytes
;
2100 if (http
->data_remaining
<= 0)
2103 * Read the trailing blank line now...
2106 char len
[32]; /* Length string */
2108 httpGets(len
, sizeof(len
), http
);
2112 else if (http
->data_remaining
<= 0)
2115 * No more data to read...
2122 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2125 if (length
> (size_t)http
->data_remaining
)
2126 length
= (size_t)http
->data_remaining
;
2128 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2130 http
->data_remaining
-= bytes
;
2132 if (http
->data_remaining
<= 0 &&
2133 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2136 * Read the trailing blank line now...
2139 char len
[32]; /* Length string */
2141 httpGets(len
, sizeof(len
), http
);
2148 (http
->coding
== _HTTP_CODING_IDENTITY
||
2149 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2150 #endif /* HAVE_LIBZ */
2151 ((http
->data_remaining
<= 0 &&
2152 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2153 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2156 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2157 http_content_coding_finish(http
);
2158 #endif /* HAVE_LIBZ */
2160 if (http
->state
== HTTP_STATE_POST_RECV
)
2162 else if (http
->state
== HTTP_STATE_GET_SEND
||
2163 http
->state
== HTTP_STATE_POST_SEND
)
2164 http
->state
= HTTP_STATE_WAITING
;
2166 http
->state
= HTTP_STATE_STATUS
;
2168 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2169 httpStateString(http
->state
)));
2177 * 'httpReadRequest()' - Read a HTTP request from a connection.
2179 * @since CUPS 1.7/macOS 10.9@
2182 http_state_t
/* O - New state of connection */
2183 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2184 char *uri
, /* I - URI buffer */
2185 size_t urilen
) /* I - Size of URI buffer */
2187 char line
[4096], /* HTTP request line */
2188 *req_method
, /* HTTP request method */
2189 *req_uri
, /* HTTP request URI */
2190 *req_version
; /* HTTP request version number string */
2194 * Range check input...
2197 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2202 if (!http
|| !uri
|| urilen
< 1)
2204 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2205 return (HTTP_STATE_ERROR
);
2207 else if (http
->state
!= HTTP_STATE_WAITING
)
2209 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2210 httpStateString(http
->state
)));
2211 return (HTTP_STATE_ERROR
);
2218 httpClearFields(http
);
2220 http
->activity
= time(NULL
);
2221 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2222 http
->data_remaining
= 0;
2223 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2224 http
->status
= HTTP_STATUS_OK
;
2225 http
->version
= HTTP_VERSION_1_1
;
2228 * Read a line from the socket...
2231 if (!httpGets(line
, sizeof(line
), http
))
2233 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2234 return (HTTP_STATE_ERROR
);
2239 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2240 return (HTTP_STATE_WAITING
);
2243 DEBUG_printf(("1httpReadRequest: %s", line
));
2252 while (*req_uri
&& !isspace(*req_uri
& 255))
2257 DEBUG_puts("1httpReadRequest: No request URI.");
2258 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2259 return (HTTP_STATE_ERROR
);
2264 while (*req_uri
&& isspace(*req_uri
& 255))
2267 req_version
= req_uri
;
2269 while (*req_version
&& !isspace(*req_version
& 255))
2274 DEBUG_puts("1httpReadRequest: No request protocol version.");
2275 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2276 return (HTTP_STATE_ERROR
);
2279 *req_version
++ = '\0';
2281 while (*req_version
&& isspace(*req_version
& 255))
2288 if (!strcmp(req_method
, "OPTIONS"))
2289 http
->state
= HTTP_STATE_OPTIONS
;
2290 else if (!strcmp(req_method
, "GET"))
2291 http
->state
= HTTP_STATE_GET
;
2292 else if (!strcmp(req_method
, "HEAD"))
2293 http
->state
= HTTP_STATE_HEAD
;
2294 else if (!strcmp(req_method
, "POST"))
2295 http
->state
= HTTP_STATE_POST
;
2296 else if (!strcmp(req_method
, "PUT"))
2297 http
->state
= HTTP_STATE_PUT
;
2298 else if (!strcmp(req_method
, "DELETE"))
2299 http
->state
= HTTP_STATE_DELETE
;
2300 else if (!strcmp(req_method
, "TRACE"))
2301 http
->state
= HTTP_STATE_TRACE
;
2302 else if (!strcmp(req_method
, "CONNECT"))
2303 http
->state
= HTTP_STATE_CONNECT
;
2306 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2307 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2308 return (HTTP_STATE_UNKNOWN_METHOD
);
2311 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2312 httpStateString(http
->state
)));
2314 if (!strcmp(req_version
, "HTTP/1.0"))
2316 http
->version
= HTTP_VERSION_1_0
;
2317 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2319 else if (!strcmp(req_version
, "HTTP/1.1"))
2321 http
->version
= HTTP_VERSION_1_1
;
2322 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2326 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2327 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2328 return (HTTP_STATE_UNKNOWN_VERSION
);
2331 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2332 strlcpy(uri
, req_uri
, urilen
);
2334 return (http
->state
);
2339 * 'httpReconnect()' - Reconnect to a HTTP server.
2341 * This function is deprecated. Please use the @link httpReconnect2@ function
2344 * @deprecated@ @exclude all@
2347 int /* O - 0 on success, non-zero on failure */
2348 httpReconnect(http_t
*http
) /* I - HTTP connection */
2350 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2352 return (httpReconnect2(http
, 30000, NULL
));
2357 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2361 int /* O - 0 on success, non-zero on failure */
2362 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2363 int msec
, /* I - Timeout in milliseconds */
2364 int *cancel
) /* I - Pointer to "cancel" variable */
2366 http_addrlist_t
*addr
; /* Connected address */
2368 http_addrlist_t
*current
; /* Current address */
2369 char temp
[256]; /* Temporary address string */
2373 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2377 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2384 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2387 #endif /* HAVE_SSL */
2390 * Close any previously open socket...
2395 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2397 httpAddrClose(NULL
, http
->fd
);
2403 * Reset all state (except fields, which may be reused)...
2406 http
->state
= HTTP_STATE_WAITING
;
2407 http
->version
= HTTP_VERSION_1_1
;
2408 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2409 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2410 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2411 http
->_data_remaining
= 0;
2413 http
->data_remaining
= 0;
2414 http
->hostaddr
= NULL
;
2418 * Connect to the server...
2422 for (current
= http
->addrlist
; current
; current
= current
->next
)
2423 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2424 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2425 httpAddrPort(&(current
->addr
))));
2428 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2431 * Unable to connect...
2435 http
->error
= WSAGetLastError();
2437 http
->error
= errno
;
2439 http
->status
= HTTP_STATUS_ERROR
;
2441 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2442 strerror(http
->error
)));
2447 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2449 if (http
->timeout_value
> 0)
2450 http_set_timeout(http
->fd
, http
->timeout_value
);
2452 http
->hostaddr
= &(addr
->addr
);
2456 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2459 * Always do encryption via SSL.
2462 if (_httpTLSStart(http
) != 0)
2464 httpAddrClose(NULL
, http
->fd
);
2469 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2470 return (http_tls_upgrade(http
));
2471 #endif /* HAVE_SSL */
2473 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2474 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2475 httpAddrPort(http
->hostaddr
)));
2482 * 'httpSetAuthString()' - Set the current authorization string.
2484 * This function just stores a copy of the current authorization string in
2485 * the HTTP connection object. You must still call @link httpSetField@ to set
2486 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2487 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2490 * @since CUPS 1.3/macOS 10.5@
2494 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2495 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2496 const char *data
) /* I - Auth data (NULL for none) */
2499 * Range check input...
2505 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2506 free(http
->authstring
);
2508 http
->authstring
= http
->_authstring
;
2513 * Set the current authorization string...
2516 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2519 if (len
> sizeof(http
->_authstring
))
2521 if ((temp
= malloc(len
)) == NULL
)
2522 len
= sizeof(http
->_authstring
);
2524 http
->authstring
= temp
;
2528 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2530 strlcpy(http
->authstring
, scheme
, len
);
2535 * Clear the current authorization string...
2538 http
->_authstring
[0] = '\0';
2544 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2547 * @since CUPS 1.5/macOS 10.7@
2550 int /* O - Status of call (0 = success) */
2551 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2552 cups_array_t
*credentials
) /* I - Array of credentials */
2554 if (!http
|| cupsArrayCount(credentials
) < 1)
2558 _httpFreeCredentials(http
->tls_credentials
);
2560 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2561 #endif /* HAVE_SSL */
2563 return (http
->tls_credentials
? 0 : -1);
2568 * 'httpSetCookie()' - Set the cookie value(s).
2570 * @since CUPS 1.1.19/macOS 10.3@
2574 httpSetCookie(http_t
*http
, /* I - Connection */
2575 const char *cookie
) /* I - Cookie string */
2584 http
->cookie
= strdup(cookie
);
2586 http
->cookie
= NULL
;
2591 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2593 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2594 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2596 * @since CUPS 1.7/macOS 10.9@
2600 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2601 http_field_t field
, /* I - Field index */
2602 const char *value
)/* I - Value */
2604 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2611 case HTTP_FIELD_ACCEPT_ENCODING
:
2612 if (http
->default_accept_encoding
)
2613 _cupsStrFree(http
->default_accept_encoding
);
2615 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2618 case HTTP_FIELD_SERVER
:
2619 if (http
->default_server
)
2620 _cupsStrFree(http
->default_server
);
2622 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2625 case HTTP_FIELD_USER_AGENT
:
2626 if (http
->default_user_agent
)
2627 _cupsStrFree(http
->default_user_agent
);
2629 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2633 DEBUG_puts("1httpSetDefaultField: Ignored.");
2640 * 'httpSetExpect()' - Set the Expect: header in a request.
2642 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2645 * @since CUPS 1.2/macOS 10.5@
2649 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2650 http_status_t expect
) /* I - HTTP status to expect
2651 (@code HTTP_STATUS_CONTINUE@) */
2653 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2656 http
->expect
= expect
;
2661 * 'httpSetField()' - Set the value of an HTTP header.
2665 httpSetField(http_t
*http
, /* I - HTTP connection */
2666 http_field_t field
, /* I - Field index */
2667 const char *value
) /* I - Value */
2669 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2672 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2673 field
>= HTTP_FIELD_MAX
||
2679 case HTTP_FIELD_ACCEPT_ENCODING
:
2680 if (http
->accept_encoding
)
2681 _cupsStrFree(http
->accept_encoding
);
2683 http
->accept_encoding
= _cupsStrAlloc(value
);
2686 case HTTP_FIELD_ALLOW
:
2688 _cupsStrFree(http
->allow
);
2690 http
->allow
= _cupsStrAlloc(value
);
2693 case HTTP_FIELD_SERVER
:
2695 _cupsStrFree(http
->server
);
2697 http
->server
= _cupsStrAlloc(value
);
2700 case HTTP_FIELD_WWW_AUTHENTICATE
:
2701 if (!http
->www_authenticate
)
2704 * First WWW-Authenticate seen, just copy it over...
2707 http
->www_authenticate
= strdup(value
);
2708 strlcpy(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], value
, HTTP_MAX_VALUE
);
2713 * Nth WWW-Authenticate seen, append to existing string...
2716 size_t len
= strlen(http
->www_authenticate
) + 2 + strlen(value
) + 1;
2717 char *temp
= realloc(http
->www_authenticate
, len
);
2722 http
->www_authenticate
= temp
;
2723 strlcat(http
->www_authenticate
, ", ", len
);
2724 strlcat(http
->www_authenticate
, value
, len
);
2726 /* Probably more efficient than two more strlcat's */
2727 strlcpy(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], http
->www_authenticate
, HTTP_MAX_VALUE
);
2732 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2736 if (field
== HTTP_FIELD_AUTHORIZATION
)
2739 * Special case for Authorization: as its contents can be
2740 * longer than HTTP_MAX_VALUE
2743 if (http
->field_authorization
)
2744 free(http
->field_authorization
);
2746 http
->field_authorization
= strdup(value
);
2748 else if (field
== HTTP_FIELD_HOST
)
2751 * Special-case for Host: as we don't want a trailing "." on the hostname and
2752 * need to bracket IPv6 numeric addresses.
2755 char *ptr
= strchr(value
, ':');
2757 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2760 * Bracket IPv6 numeric addresses...
2762 * This is slightly inefficient (basically copying twice), but is an edge
2763 * case and not worth optimizing...
2766 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2767 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2772 * Check for a trailing dot on the hostname...
2775 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2779 ptr
+= strlen(ptr
) - 1;
2787 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2788 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
2790 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
2791 http_content_coding_start(http
, value
);
2793 #endif /* HAVE_LIBZ */
2798 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2800 * @since CUPS 2.0/OS 10.10@
2805 http_t
*http
, /* I - HTTP connection */
2806 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2809 http
->keep_alive
= keep_alive
;
2814 * 'httpSetLength()' - Set the content-length and content-encoding.
2816 * @since CUPS 1.2/macOS 10.5@
2820 httpSetLength(http_t
*http
, /* I - HTTP connection */
2821 size_t length
) /* I - Length (0 for chunked) */
2823 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2830 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2832 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2836 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2837 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2838 CUPS_LLFMT
, CUPS_LLCAST length
);
2844 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2846 * The optional timeout callback receives both the HTTP connection and a user
2847 * data pointer and must return 1 to continue or 0 to error (time) out.
2849 * @since CUPS 1.5/macOS 10.7@
2854 http_t
*http
, /* I - HTTP connection */
2855 double timeout
, /* I - Number of seconds for timeout,
2856 must be greater than 0 */
2857 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2858 void *user_data
) /* I - User data pointer */
2860 if (!http
|| timeout
<= 0.0)
2863 http
->timeout_cb
= cb
;
2864 http
->timeout_data
= user_data
;
2865 http
->timeout_value
= timeout
;
2868 http_set_timeout(http
->fd
, timeout
);
2870 http_set_wait(http
);
2875 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2877 * @since CUPS 2.0/OS 10.10@
2881 httpShutdown(http_t
*http
) /* I - HTTP connection */
2883 if (!http
|| http
->fd
< 0)
2889 #endif /* HAVE_SSL */
2892 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2894 shutdown(http
->fd
, SHUT_RD
);
2900 * 'httpTrace()' - Send an TRACE request to the server.
2905 int /* O - Status of call (0 = success) */
2906 httpTrace(http_t
*http
, /* I - HTTP connection */
2907 const char *uri
) /* I - URI for trace */
2909 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2914 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2916 * Note: Unlike httpUpdate(), this function does not flush pending write data
2917 * and only retrieves a single status line from the HTTP connection.
2920 int /* O - 1 to continue, 0 to stop */
2921 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2922 http_status_t
*status
) /* O - Current HTTP status */
2924 char line
[32768], /* Line from connection... */
2925 *value
; /* Pointer to value on line */
2926 http_field_t field
; /* Field index */
2927 int major
, minor
; /* HTTP version numbers */
2930 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2933 * Grab a single line from the connection...
2936 if (!httpGets(line
, sizeof(line
), http
))
2938 *status
= HTTP_STATUS_ERROR
;
2942 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2944 if (line
[0] == '\0')
2947 * Blank line means the start of the data section (if any). Return
2948 * the result code, too...
2950 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2951 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2955 if (http
->status
== HTTP_STATUS_CONTINUE
)
2957 *status
= http
->status
;
2961 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2962 http
->digest_tries
= 0;
2965 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2967 if (_httpTLSStart(http
) != 0)
2969 httpAddrClose(NULL
, http
->fd
);
2971 *status
= http
->status
= HTTP_STATUS_ERROR
;
2975 *status
= HTTP_STATUS_CONTINUE
;
2978 #endif /* HAVE_SSL */
2980 if (http_set_length(http
) < 0)
2982 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2983 http
->error
= EINVAL
;
2984 http
->status
= *status
= HTTP_STATUS_ERROR
;
2988 switch (http
->state
)
2990 case HTTP_STATE_GET
:
2991 case HTTP_STATE_POST
:
2992 case HTTP_STATE_POST_RECV
:
2993 case HTTP_STATE_PUT
:
2996 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2997 httpStateString(http
->state
)));
2999 case HTTP_STATE_POST_SEND
:
3000 case HTTP_STATE_HEAD
:
3004 http
->state
= HTTP_STATE_WAITING
;
3006 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
3011 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
3012 http_content_coding_start(http
,
3013 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3014 #endif /* HAVE_LIBZ */
3016 *status
= http
->status
;
3019 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
3022 * Got the beginning of a response...
3025 int intstatus
; /* Status value as an integer */
3027 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
3029 *status
= http
->status
= HTTP_STATUS_ERROR
;
3033 httpClearFields(http
);
3035 http
->version
= (http_version_t
)(major
* 100 + minor
);
3036 *status
= http
->status
= (http_status_t
)intstatus
;
3038 else if ((value
= strchr(line
, ':')) != NULL
)
3045 while (_cups_isspace(*value
))
3048 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3051 * Be tolerants of servers that send unknown attribute fields...
3054 if (!_cups_strcasecmp(line
, "expect"))
3057 * "Expect: 100-continue" or similar...
3060 http
->expect
= (http_status_t
)atoi(value
);
3062 else if (!_cups_strcasecmp(line
, "cookie"))
3065 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3068 httpSetCookie(http
, value
);
3070 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3071 httpSetField(http
, field
, value
);
3074 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3079 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3080 http
->error
= EINVAL
;
3081 http
->status
= *status
= HTTP_STATUS_ERROR
;
3090 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3093 http_status_t
/* O - HTTP status */
3094 httpUpdate(http_t
*http
) /* I - HTTP connection */
3096 http_status_t status
; /* Request status */
3099 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
3102 * Flush pending data, if any...
3107 DEBUG_puts("2httpUpdate: flushing buffer...");
3109 if (httpFlushWrite(http
) < 0)
3110 return (HTTP_STATUS_ERROR
);
3114 * If we haven't issued any commands, then there is nothing to "update"...
3117 if (http
->state
== HTTP_STATE_WAITING
)
3118 return (HTTP_STATUS_CONTINUE
);
3121 * Grab all of the lines we can from the connection...
3124 while (_httpUpdate(http
, &status
));
3127 * See if there was an error...
3130 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3132 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3133 return (http
->status
);
3138 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3139 strerror(http
->error
)));
3140 http
->status
= HTTP_STATUS_ERROR
;
3141 return (HTTP_STATUS_ERROR
);
3145 * Return the current status...
3153 * '_httpWait()' - Wait for data available on a connection (no flush).
3156 int /* O - 1 if data is available, 0 otherwise */
3157 _httpWait(http_t
*http
, /* I - HTTP connection */
3158 int msec
, /* I - Milliseconds to wait */
3159 int usessl
) /* I - Use SSL context? */
3162 struct pollfd pfd
; /* Polled file descriptor */
3164 fd_set input_set
; /* select() input set */
3165 struct timeval timeout
; /* Timeout */
3166 #endif /* HAVE_POLL */
3167 int nfds
; /* Result from select()/poll() */
3170 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3174 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3179 * Check the SSL/TLS buffers for data first...
3183 if (http
->tls
&& _httpTLSPending(http
))
3185 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3188 #endif /* HAVE_SSL */
3191 * Then try doing a select() or poll() to poll the socket...
3196 pfd
.events
= POLLIN
;
3200 nfds
= poll(&pfd
, 1, msec
);
3202 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3207 FD_ZERO(&input_set
);
3208 FD_SET(http
->fd
, &input_set
);
3210 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3214 timeout
.tv_sec
= msec
/ 1000;
3215 timeout
.tv_usec
= (msec
% 1000) * 1000;
3217 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3220 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3222 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3225 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3226 WSAGetLastError() == WSAEWOULDBLOCK
));
3228 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3230 #endif /* HAVE_POLL */
3232 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3240 * 'httpWait()' - Wait for data available on a connection.
3242 * @since CUPS 1.1.19/macOS 10.3@
3245 int /* O - 1 if data is available, 0 otherwise */
3246 httpWait(http_t
*http
, /* I - HTTP connection */
3247 int msec
) /* I - Milliseconds to wait */
3250 * First see if there is data in the buffer...
3253 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3260 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3265 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3267 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3270 #endif /* HAVE_LIBZ */
3273 * Flush pending data, if any...
3278 DEBUG_puts("3httpWait: Flushing write buffer.");
3280 if (httpFlushWrite(http
) < 0)
3285 * If not, check the SSL/TLS buffers and do a select() on the connection...
3288 return (_httpWait(http
, msec
, 1));
3293 * 'httpWrite()' - Write data to a HTTP connection.
3295 * This function is deprecated. Use the httpWrite2() function which can
3296 * write more than 2GB of data.
3298 * @deprecated@ @exclude all@
3301 int /* O - Number of bytes written */
3302 httpWrite(http_t
*http
, /* I - HTTP connection */
3303 const char *buffer
, /* I - Buffer for data */
3304 int length
) /* I - Number of bytes to write */
3306 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3311 * 'httpWrite2()' - Write data to a HTTP connection.
3313 * @since CUPS 1.2/macOS 10.5@
3316 ssize_t
/* O - Number of bytes written */
3317 httpWrite2(http_t
*http
, /* I - HTTP connection */
3318 const char *buffer
, /* I - Buffer for data */
3319 size_t length
) /* I - Number of bytes to write */
3321 ssize_t bytes
; /* Bytes written */
3324 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3327 * Range check input...
3330 if (!http
|| !buffer
)
3332 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3337 * Mark activity on the connection...
3340 http
->activity
= time(NULL
);
3343 * Buffer small writes for better performance...
3347 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3349 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3353 http_content_coding_finish(http
);
3358 size_t slen
; /* Bytes to write */
3359 ssize_t sret
; /* Bytes written */
3361 http
->stream
.next_in
= (Bytef
*)buffer
;
3362 http
->stream
.avail_in
= (uInt
)length
;
3364 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3366 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3368 if (http
->stream
.avail_out
> 0)
3371 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3373 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3375 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3376 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3378 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3384 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3388 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3389 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3392 bytes
= (ssize_t
)length
;
3396 #endif /* HAVE_LIBZ */
3399 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3401 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3402 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3404 httpFlushWrite(http
);
3407 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3410 * Write to buffer...
3413 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3414 CUPS_LLCAST length
));
3416 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3417 http
->wused
+= (int)length
;
3418 bytes
= (ssize_t
)length
;
3423 * Otherwise write the data directly...
3426 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3427 CUPS_LLCAST length
));
3429 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3430 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3432 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3434 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3435 CUPS_LLCAST bytes
));
3438 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3439 http
->data_remaining
-= bytes
;
3445 * Handle end-of-request processing...
3448 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3449 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3452 * Finished with the transfer; unless we are sending POST or PUT
3457 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3458 http_content_coding_finish(http
);
3459 #endif /* HAVE_LIBZ */
3463 if (httpFlushWrite(http
) < 0)
3467 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3470 * Send a 0-length chunk at the end of the request...
3473 http_write(http
, "0\r\n\r\n", 5);
3476 * Reset the data state...
3479 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3480 http
->data_remaining
= 0;
3483 if (http
->state
== HTTP_STATE_POST_RECV
)
3485 else if (http
->state
== HTTP_STATE_POST_SEND
||
3486 http
->state
== HTTP_STATE_GET_SEND
)
3487 http
->state
= HTTP_STATE_WAITING
;
3489 http
->state
= HTTP_STATE_STATUS
;
3491 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3492 httpStateString(http
->state
)));
3495 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3502 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3504 * @since CUPS 1.7/macOS 10.9@
3507 int /* O - 0 on success, -1 on error */
3508 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3509 http_status_t status
) /* I - Status code */
3511 http_encoding_t old_encoding
; /* Old data_encoding value */
3512 off_t old_remaining
; /* Old data_remaining value */
3516 * Range check input...
3519 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3521 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3523 DEBUG_puts("1httpWriteResponse: Bad input.");
3528 * Set the various standard fields if they aren't already...
3531 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3532 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3534 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3536 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3537 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3540 if (http
->version
== HTTP_VERSION_1_1
)
3542 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3544 if (http
->keep_alive
)
3545 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3547 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3550 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3551 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3555 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3556 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3558 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3559 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3561 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3562 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3564 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3565 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3567 #endif /* HAVE_SSL */
3570 httpSetField(http
, HTTP_FIELD_SERVER
,
3571 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3574 * Set the Accept-Encoding field if it isn't already...
3577 if (!http
->accept_encoding
)
3578 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3579 http
->default_accept_encoding
? http
->default_accept_encoding
:
3581 "gzip, deflate, identity");
3584 #endif /* HAVE_LIBZ */
3587 * Send the response header...
3590 old_encoding
= http
->data_encoding
;
3591 old_remaining
= http
->data_remaining
;
3592 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3594 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3595 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3597 http
->status
= HTTP_STATUS_ERROR
;
3601 if (status
!= HTTP_STATUS_CONTINUE
)
3604 * 100 Continue doesn't have the rest of the response headers...
3607 int i
; /* Looping var */
3608 const char *value
; /* Field value */
3610 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3612 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3614 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3616 http
->status
= HTTP_STATUS_ERROR
;
3624 if (strchr(http
->cookie
, ';'))
3626 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3628 http
->status
= HTTP_STATUS_ERROR
;
3632 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3634 http
->status
= HTTP_STATUS_ERROR
;
3640 * "Click-jacking" defense (STR #4492)...
3643 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3644 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3646 http
->status
= HTTP_STATUS_ERROR
;
3651 if (httpWrite2(http
, "\r\n", 2) < 2)
3653 http
->status
= HTTP_STATUS_ERROR
;
3657 if (httpFlushWrite(http
) < 0)
3659 http
->status
= HTTP_STATUS_ERROR
;
3663 if (status
== HTTP_STATUS_CONTINUE
||
3664 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3667 * Restore the old data_encoding and data_length values...
3670 http
->data_encoding
= old_encoding
;
3671 http
->data_remaining
= old_remaining
;
3673 if (old_remaining
<= INT_MAX
)
3674 http
->_data_remaining
= (int)old_remaining
;
3676 http
->_data_remaining
= INT_MAX
;
3678 else if (http
->state
== HTTP_STATE_OPTIONS
||
3679 http
->state
== HTTP_STATE_HEAD
||
3680 http
->state
== HTTP_STATE_PUT
||
3681 http
->state
== HTTP_STATE_TRACE
||
3682 http
->state
== HTTP_STATE_CONNECT
||
3683 http
->state
== HTTP_STATE_STATUS
)
3685 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3686 "was %s.", httpStateString(http
->state
)));
3687 http
->state
= HTTP_STATE_WAITING
;
3692 * Force data_encoding and data_length to be set according to the response
3696 http_set_length(http
);
3698 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3700 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3701 "was %s.", httpStateString(http
->state
)));
3702 http
->state
= HTTP_STATE_WAITING
;
3706 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3711 * Then start any content encoding...
3714 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3715 http_content_coding_start(http
,
3716 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3717 #endif /* HAVE_LIBZ */
3727 * 'http_content_coding_finish()' - Finish doing any content encoding.
3731 http_content_coding_finish(
3732 http_t
*http
) /* I - HTTP connection */
3734 int zerr
; /* Compression status */
3735 Byte dummy
[1]; /* Dummy read buffer */
3736 size_t bytes
; /* Number of bytes to write */
3739 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3740 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3742 switch (http
->coding
)
3744 case _HTTP_CODING_DEFLATE
:
3745 case _HTTP_CODING_GZIP
:
3746 http
->stream
.next_in
= dummy
;
3747 http
->stream
.avail_in
= 0;
3751 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3752 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3756 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3758 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3759 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3761 http_write(http
, (char *)http
->sbuffer
, bytes
);
3764 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3765 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3767 while (zerr
== Z_OK
);
3769 deflateEnd(&(http
->stream
));
3771 free(http
->sbuffer
);
3772 http
->sbuffer
= NULL
;
3775 httpFlushWrite(http
);
3778 case _HTTP_CODING_INFLATE
:
3779 case _HTTP_CODING_GUNZIP
:
3780 inflateEnd(&(http
->stream
));
3781 free(http
->sbuffer
);
3782 http
->sbuffer
= NULL
;
3789 http
->coding
= _HTTP_CODING_IDENTITY
;
3794 * 'http_content_coding_start()' - Start doing content encoding.
3798 http_content_coding_start(
3799 http_t
*http
, /* I - HTTP connection */
3800 const char *value
) /* I - Value of Content-Encoding */
3802 int zerr
; /* Error/status */
3803 _http_coding_t coding
; /* Content coding value */
3806 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3808 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3810 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3814 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3816 if (http
->state
== HTTP_STATE_GET_SEND
||
3817 http
->state
== HTTP_STATE_POST_SEND
)
3818 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3819 _HTTP_CODING_GUNZIP
;
3820 else if (http
->state
== HTTP_STATE_POST_RECV
||
3821 http
->state
== HTTP_STATE_PUT_RECV
)
3822 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3823 _HTTP_CODING_GUNZIP
;
3826 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3830 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3832 if (http
->state
== HTTP_STATE_GET_SEND
||
3833 http
->state
== HTTP_STATE_POST_SEND
)
3834 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3835 _HTTP_CODING_INFLATE
;
3836 else if (http
->state
== HTTP_STATE_POST_RECV
||
3837 http
->state
== HTTP_STATE_PUT_RECV
)
3838 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3839 _HTTP_CODING_INFLATE
;
3842 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3848 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3852 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3856 case _HTTP_CODING_DEFLATE
:
3857 case _HTTP_CODING_GZIP
:
3859 httpFlushWrite(http
);
3861 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3863 http
->status
= HTTP_STATUS_ERROR
;
3864 http
->error
= errno
;
3869 * Window size for compression is 11 bits - optimal based on PWG Raster
3870 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3874 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3876 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3877 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3879 http
->status
= HTTP_STATUS_ERROR
;
3880 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3884 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3885 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3888 case _HTTP_CODING_INFLATE
:
3889 case _HTTP_CODING_GUNZIP
:
3890 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3892 http
->status
= HTTP_STATUS_ERROR
;
3893 http
->error
= errno
;
3898 * Window size for decompression is up to 15 bits (maximum supported).
3899 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3902 if ((zerr
= inflateInit2(&(http
->stream
),
3903 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3906 free(http
->sbuffer
);
3907 http
->sbuffer
= NULL
;
3908 http
->status
= HTTP_STATUS_ERROR
;
3909 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3913 http
->stream
.avail_in
= 0;
3914 http
->stream
.next_in
= http
->sbuffer
;
3921 http
->coding
= coding
;
3923 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3926 #endif /* HAVE_LIBZ */
3930 * 'http_create()' - Create an unconnected HTTP connection.
3933 static http_t
* /* O - HTTP connection */
3935 const char *host
, /* I - Hostname */
3936 int port
, /* I - Port number */
3937 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3938 int family
, /* I - Address family or AF_UNSPEC */
3939 http_encryption_t encryption
, /* I - Encryption to use */
3940 int blocking
, /* I - 1 for blocking mode */
3941 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3943 http_t
*http
; /* New HTTP connection */
3944 char service
[255]; /* Service name */
3945 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3948 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
));
3950 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3956 * Lookup the host...
3961 myaddrlist
= httpAddrCopyList(addrlist
);
3965 snprintf(service
, sizeof(service
), "%d", port
);
3967 myaddrlist
= httpAddrGetList(host
, family
, service
);
3974 * Allocate memory for the structure...
3977 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3979 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3980 httpAddrFreeList(addrlist
);
3985 * Initialize the HTTP data...
3989 http
->activity
= time(NULL
);
3990 http
->addrlist
= myaddrlist
;
3991 http
->blocking
= blocking
;
3994 http
->gssctx
= GSS_C_NO_CONTEXT
;
3995 http
->gssname
= GSS_C_NO_NAME
;
3996 #endif /* HAVE_GSSAPI */
3997 http
->status
= HTTP_STATUS_CONTINUE
;
3998 http
->version
= HTTP_VERSION_1_1
;
4001 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
4003 if (port
== 443) /* Always use encryption for https */
4004 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
4006 http
->encryption
= encryption
;
4008 http_set_wait(http
);
4011 * Return the new structure...
4020 * 'http_debug_hex()' - Do a hex dump of a buffer.
4024 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4025 const char *buffer
, /* I - Buffer to dump */
4026 int bytes
) /* I - Bytes to dump */
4028 int i
, j
, /* Looping vars */
4029 ch
; /* Current character */
4030 char line
[255], /* Line buffer */
4031 *start
, /* Start of line after prefix */
4032 *ptr
; /* Pointer into line */
4035 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4038 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4040 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4041 start
= line
+ strlen(line
);
4043 for (i
= 0; i
< bytes
; i
+= 16)
4045 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4046 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4050 memcpy(ptr
, " ", 3);
4055 memcpy(ptr
, " ", 3);
4058 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4060 ch
= buffer
[i
+ j
] & 255;
4062 if (ch
< ' ' || ch
>= 127)
4076 * 'http_read()' - Read a buffer from a HTTP connection.
4078 * This function does the low-level read from the socket, retrying and timing
4082 static ssize_t
/* O - Number of bytes read or -1 on error */
4083 http_read(http_t
*http
, /* I - HTTP connection */
4084 char *buffer
, /* I - Buffer */
4085 size_t length
) /* I - Maximum bytes to read */
4087 ssize_t bytes
; /* Bytes read */
4090 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4092 if (!http
->blocking
)
4094 while (!httpWait(http
, http
->wait_value
))
4096 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4099 DEBUG_puts("2http_read: Timeout.");
4104 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4110 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4112 #endif /* HAVE_SSL */
4113 bytes
= recv(http
->fd
, buffer
, length
, 0);
4118 if (WSAGetLastError() != WSAEINTR
)
4120 http
->error
= WSAGetLastError();
4123 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4125 if (!http
->timeout_cb
||
4126 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4128 http
->error
= WSAEWOULDBLOCK
;
4133 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4135 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4137 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4139 http
->error
= errno
;
4142 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4144 http
->error
= errno
;
4148 else if (errno
!= EINTR
)
4150 http
->error
= errno
;
4158 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4159 CUPS_LLCAST bytes
));
4162 http_debug_hex("http_read", buffer
, (int)bytes
);
4168 if (WSAGetLastError() == WSAEINTR
)
4171 http
->error
= WSAGetLastError();
4173 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4176 http
->error
= errno
;
4179 else if (bytes
== 0)
4181 http
->error
= EPIPE
;
4190 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4192 * This function reads data from the HTTP buffer or from the socket, as needed.
4195 static ssize_t
/* O - Number of bytes read or -1 on error */
4196 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4197 char *buffer
, /* I - Buffer */
4198 size_t length
) /* I - Maximum bytes to read */
4200 ssize_t bytes
; /* Bytes read */
4203 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4207 if (length
> (size_t)http
->used
)
4208 bytes
= (ssize_t
)http
->used
;
4210 bytes
= (ssize_t
)length
;
4212 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4215 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4216 http
->used
-= (int)bytes
;
4219 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4222 bytes
= http_read(http
, buffer
, length
);
4229 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4231 * This function reads and validates the chunk length, then does a buffered read
4232 * returning the number of bytes placed in the buffer.
4235 static ssize_t
/* O - Number of bytes read or -1 on error */
4236 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4237 char *buffer
, /* I - Buffer */
4238 size_t length
) /* I - Maximum bytes to read */
4240 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4242 if (http
->data_remaining
<= 0)
4244 char len
[32]; /* Length string */
4246 if (!httpGets(len
, sizeof(len
), http
))
4248 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4254 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4255 if (!httpGets(len
, sizeof(len
), http
))
4257 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4262 http
->data_remaining
= strtoll(len
, NULL
, 16);
4264 if (http
->data_remaining
< 0)
4266 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4267 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4271 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4272 len
, CUPS_LLCAST http
->data_remaining
));
4274 if (http
->data_remaining
== 0)
4277 * 0-length chunk, grab trailing blank line...
4280 httpGets(len
, sizeof(len
), http
);
4284 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4285 CUPS_LLCAST http
->data_remaining
));
4287 if (http
->data_remaining
<= 0)
4289 else if (length
> (size_t)http
->data_remaining
)
4290 length
= (size_t)http
->data_remaining
;
4292 return (http_read_buffered(http
, buffer
, length
));
4297 * 'http_send()' - Send a request with all fields and the trailing blank line.
4300 static int /* O - 0 on success, non-zero on error */
4301 http_send(http_t
*http
, /* I - HTTP connection */
4302 http_state_t request
, /* I - Request code */
4303 const char *uri
) /* I - URI */
4305 int i
; /* Looping var */
4306 char buf
[1024]; /* Encoded URI buffer */
4307 const char *value
; /* Field value */
4308 static const char * const codes
[] = /* Request code strings */
4328 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4330 if (http
== NULL
|| uri
== NULL
)
4334 * Set the User-Agent field if it isn't already...
4337 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4339 if (http
->default_user_agent
)
4340 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4342 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4346 * Set the Accept-Encoding field if it isn't already...
4349 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4350 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4351 http
->default_accept_encoding
);
4354 * Encode the URI as needed...
4357 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4360 * See if we had an error the last time around; if so, reconnect...
4363 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4364 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4366 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4367 http
->fd
, http
->status
, http
->tls_upgrade
));
4369 if (httpReconnect2(http
, 30000, NULL
))
4374 * Flush any written data that is pending...
4379 if (httpFlushWrite(http
) < 0)
4380 if (httpReconnect2(http
, 30000, NULL
))
4385 * Send the request header...
4388 http
->state
= request
;
4389 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4391 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4394 http
->status
= HTTP_STATUS_CONTINUE
;
4397 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4399 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4400 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4402 #endif /* HAVE_SSL */
4404 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4406 http
->status
= HTTP_STATUS_ERROR
;
4410 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4411 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4413 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4415 if (i
== HTTP_FIELD_HOST
)
4417 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4418 httpAddrPort(http
->hostaddr
)) < 1)
4420 http
->status
= HTTP_STATUS_ERROR
;
4424 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4426 http
->status
= HTTP_STATUS_ERROR
;
4432 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4434 http
->status
= HTTP_STATUS_ERROR
;
4438 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4439 http
->mode
, http
->state
));
4441 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4442 (http
->state
== HTTP_STATE_POST_RECV
||
4443 http
->state
== HTTP_STATE_PUT_RECV
))
4444 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4446 http
->status
= HTTP_STATUS_ERROR
;
4450 if (httpPrintf(http
, "\r\n") < 1)
4452 http
->status
= HTTP_STATUS_ERROR
;
4456 if (httpFlushWrite(http
) < 0)
4459 http_set_length(http
);
4460 httpClearFields(http
);
4463 * The Kerberos and AuthRef authentication strings can only be used once...
4466 if (http
->field_authorization
&& http
->authstring
&&
4467 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4468 !strncmp(http
->authstring
, "AuthRef", 7)))
4470 http
->_authstring
[0] = '\0';
4472 if (http
->authstring
!= http
->_authstring
)
4473 free(http
->authstring
);
4475 http
->authstring
= http
->_authstring
;
4483 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4486 static off_t
/* O - Remainder or -1 on error */
4487 http_set_length(http_t
*http
) /* I - Connection */
4489 off_t remaining
; /* Remainder */
4492 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4494 if ((remaining
= httpGetLength2(http
)) >= 0)
4496 if (http
->mode
== _HTTP_MODE_SERVER
&&
4497 http
->state
!= HTTP_STATE_GET_SEND
&&
4498 http
->state
!= HTTP_STATE_PUT
&&
4499 http
->state
!= HTTP_STATE_POST
&&
4500 http
->state
!= HTTP_STATE_POST_SEND
)
4502 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4506 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4509 DEBUG_puts("1http_set_length: Setting data_encoding to "
4510 "HTTP_ENCODING_CHUNKED.");
4511 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4515 DEBUG_puts("1http_set_length: Setting data_encoding to "
4516 "HTTP_ENCODING_LENGTH.");
4517 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4520 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4521 CUPS_LLCAST remaining
));
4522 http
->data_remaining
= remaining
;
4524 if (remaining
<= INT_MAX
)
4525 http
->_data_remaining
= (int)remaining
;
4527 http
->_data_remaining
= INT_MAX
;
4534 * 'http_set_timeout()' - Set the socket timeout values.
4538 http_set_timeout(int fd
, /* I - File descriptor */
4539 double timeout
) /* I - Timeout in seconds */
4542 DWORD tv
= (DWORD
)(timeout
* 1000);
4543 /* Timeout in milliseconds */
4545 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4546 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4549 struct timeval tv
; /* Timeout in secs and usecs */
4551 tv
.tv_sec
= (int)timeout
;
4552 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4554 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4555 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4561 * 'http_set_wait()' - Set the default wait value for reads.
4565 http_set_wait(http_t
*http
) /* I - HTTP connection */
4569 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4571 if (http
->wait_value
<= 0)
4572 http
->wait_value
= 60000;
4575 http
->wait_value
= 10000;
4581 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4584 static int /* O - Status of connection */
4585 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4587 int ret
; /* Return value */
4588 http_t myhttp
; /* Local copy of HTTP data */
4591 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4594 * Flush the connection to make sure any previous "Upgrade" message
4601 * Copy the HTTP data to a local variable so we can do the OPTIONS
4602 * request without interfering with the existing request data...
4605 memcpy(&myhttp
, http
, sizeof(myhttp
));
4608 * Send an OPTIONS request to the server, requiring SSL or TLS
4609 * encryption on the link...
4612 http
->tls_upgrade
= 1;
4613 http
->field_authorization
= NULL
; /* Don't free the auth string */
4615 httpClearFields(http
);
4616 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4617 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4619 if ((ret
= httpOptions(http
, "*")) == 0)
4622 * Wait for the secure connection...
4625 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4629 * Restore the HTTP request data...
4632 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4633 http
->data_encoding
= myhttp
.data_encoding
;
4634 http
->data_remaining
= myhttp
.data_remaining
;
4635 http
->_data_remaining
= myhttp
._data_remaining
;
4636 http
->expect
= myhttp
.expect
;
4637 http
->field_authorization
= myhttp
.field_authorization
;
4638 http
->digest_tries
= myhttp
.digest_tries
;
4639 http
->tls_upgrade
= 0;
4642 * See if we actually went secure...
4648 * Server does not support HTTP upgrade...
4651 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4653 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4654 httpAddrClose(NULL
, http
->fd
);
4663 #endif /* HAVE_SSL */
4667 * 'http_write()' - Write a buffer to a HTTP connection.
4670 static ssize_t
/* O - Number of bytes written */
4671 http_write(http_t
*http
, /* I - HTTP connection */
4672 const char *buffer
, /* I - Buffer for data */
4673 size_t length
) /* I - Number of bytes to write */
4675 ssize_t tbytes
, /* Total bytes sent */
4676 bytes
; /* Bytes sent */
4679 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4685 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4687 if (http
->timeout_cb
)
4690 struct pollfd pfd
; /* Polled file descriptor */
4692 fd_set output_set
; /* Output ready for write? */
4693 struct timeval timeout
; /* Timeout value */
4694 #endif /* HAVE_POLL */
4695 int nfds
; /* Result from select()/poll() */
4701 pfd
.events
= POLLOUT
;
4703 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4704 (errno
== EINTR
|| errno
== EAGAIN
))
4710 FD_ZERO(&output_set
);
4711 FD_SET(http
->fd
, &output_set
);
4713 timeout
.tv_sec
= http
->wait_value
/ 1000;
4714 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4716 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4719 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4720 WSAGetLastError() == WSAEWOULDBLOCK
));
4722 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4724 #endif /* HAVE_POLL */
4728 http
->error
= errno
;
4731 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4734 http
->error
= WSAEWOULDBLOCK
;
4736 http
->error
= EWOULDBLOCK
;
4746 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4748 #endif /* HAVE_SSL */
4749 bytes
= send(http
->fd
, buffer
, length
, 0);
4751 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4752 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4757 if (WSAGetLastError() == WSAEINTR
)
4759 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4761 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4764 http
->error
= WSAGetLastError();
4766 else if (WSAGetLastError() != http
->error
&&
4767 WSAGetLastError() != WSAECONNRESET
)
4769 http
->error
= WSAGetLastError();
4776 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4778 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4780 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4783 http
->error
= errno
;
4785 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4787 http
->error
= errno
;
4792 DEBUG_printf(("3http_write: error writing data (%s).",
4793 strerror(http
->error
)));
4800 length
-= (size_t)bytes
;
4804 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4807 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4814 * 'http_write_chunk()' - Write a chunked buffer.
4817 static ssize_t
/* O - Number bytes written */
4818 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4819 const char *buffer
, /* I - Buffer to write */
4820 size_t length
) /* I - Length of buffer */
4822 char header
[16]; /* Chunk header */
4823 ssize_t bytes
; /* Bytes written */
4826 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4829 * Write the chunk header, data, and trailer.
4832 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4833 if (http_write(http
, header
, strlen(header
)) < 0)
4835 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4839 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4841 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4845 if (http_write(http
, "\r\n", 2) < 0)
4847 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");