2 // HTTP routines for CUPS.
4 // Copyright © 2022-2025 by OpenPrinting.
5 // Copyright © 2007-2021 by Apple Inc.
6 // Copyright © 1997-2007 by Easy Software Products, all rights reserved.
8 // This file contains Kerberos support code, copyright 2006 by
11 // Licensed under Apache License v2.0. See the file "LICENSE" for more
15 #include "cups-private.h"
23 # include <sys/time.h>
24 # include <sys/resource.h>
33 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
34 static void http_content_coding_finish(http_t
*http
);
35 static void http_content_coding_start(http_t
*http
, const char *value
);
36 static http_t
*http_create(const char *host
, int port
, http_addrlist_t
*addrlist
, int family
, http_encryption_t encryption
, int blocking
, _http_mode_t mode
);
38 static void http_debug_hex(const char *prefix
, const char *buffer
, int bytes
);
40 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
41 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
42 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
43 static bool http_send(http_t
*http
, http_state_t request
, const char *uri
);
44 static ssize_t
http_write(http_t
*http
, const char *buffer
, size_t length
);
45 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
, size_t length
);
46 static off_t
http_set_length(http_t
*http
);
47 static void http_set_timeout(int fd
, double timeout
);
48 static void http_set_wait(http_t
*http
);
49 static bool http_tls_upgrade(http_t
*http
);
56 static const char * const http_fields
[] =
73 "If-Unmodified-since",
88 "Authentication-Info",
89 "Access-Control-Allow-Credentials",
90 "Access-Control-Allow-Headers",
91 "Access-Control-Allow-Methods",
92 "Access-Control-Allow-Origin",
93 "Access-Control-Expose-Headers",
94 "Access-Control-Max-Age",
95 "Access-Control-Request-Headers",
96 "Access-Control-Request-Method",
97 "Optional-WWW-Authenticate",
100 "Strict-Transport-Security",
106 // 'httpAcceptConnection()' - Accept a new HTTP client connection.
108 // This function accepts a new HTTP client connection from the specified
109 // listening socket "fd". The "blocking" argument specifies whether the new
110 // HTTP connection is blocking.
115 http_t
* // O - HTTP connection or `NULL`
116 httpAcceptConnection(int fd
, // I - Listen socket file descriptor
117 int blocking
) // I - 1 if the connection should be blocking, 0 otherwise
119 http_t
*http
; // HTTP connection
120 http_addrlist_t addrlist
; // Dummy address list
121 socklen_t addrlen
; // Length of address
122 int val
; // Socket option value
125 // Range check input...
129 // Create the client connection...
130 memset(&addrlist
, 0, sizeof(addrlist
));
132 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, blocking
, _HTTP_MODE_SERVER
)) == NULL
)
135 // Accept the client and get the remote address...
136 addrlen
= sizeof(http_addr_t
);
138 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
), &addrlen
)) < 0)
140 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
146 http
->hostaddr
= &(http
->addrlist
->addr
);
148 if (httpAddrIsLocalhost(http
->hostaddr
))
149 cupsCopyString(http
->hostname
, "localhost", sizeof(http
->hostname
));
151 httpAddrGetString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
154 // Disable SIGPIPE for this socket.
156 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
157 #endif // SO_NOSIGPIPE
159 // Using TCP_NODELAY improves responsiveness, especially on systems with a
160 // slow loopback interface. Since we write large buffers when sending print
161 // files and requests, there shouldn't be any performance penalty for this...
163 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
166 // Close this socket when starting another process...
167 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
175 // 'httpAddCredential()' - Allocates and adds a single credential to an array.
177 // @deprecated@ @exclude all@
180 int // O - 0 on success, -1 on error
182 cups_array_t
*credentials
, // I - Credentials array
183 const void *data
, // I - PEM-encoded X.509 data
184 size_t datalen
) // I - Length of data
195 // 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
201 httpBlocking(http_t
*http
, // I - HTTP connection
202 int b
) // I - 1 = blocking, 0 = non-blocking
204 httpSetBlocking(http
, b
!= 0);
209 // 'httpCheck()' - Check to see if there is a pending response from the server.
214 int // O - 0 = no data, 1 = data available
215 httpCheck(http_t
*http
) // I - HTTP connection
217 return (httpWait(http
, 0));
222 // 'httpClearCookie()' - Clear the cookie value(s).
224 // @since CUPS 1.1.19@
228 httpClearCookie(http_t
*http
) // I - HTTP connection
239 // 'httpClearFields()' - Clear HTTP request/response fields.
243 httpClearFields(http_t
*http
) // I - HTTP connection
245 http_field_t field
; // Current field
248 DEBUG_printf("httpClearFields(http=%p)", (void *)http
);
252 memset(http
->_fields
, 0, sizeof(http
->_fields
));
254 for (field
= HTTP_FIELD_ACCEPT_LANGUAGE
; field
< HTTP_FIELD_ACCEPT_ENCODING
; field
++)
256 if (!http
->fields
[field
])
259 if (http
->fields
[field
] != http
->_fields
[field
])
260 free(http
->fields
[field
]);
262 http
->fields
[field
] = NULL
;
265 for (; field
< HTTP_FIELD_MAX
; field
++)
267 if (!http
->fields
[field
])
270 free(http
->fields
[field
]);
271 http
->fields
[field
] = NULL
;
274 if (http
->mode
== _HTTP_MODE_CLIENT
)
276 if (http
->hostname
[0] == '/')
277 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
279 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
282 http
->expect
= (http_status_t
)0;
288 // 'httpClose()' - Close a HTTP connection.
292 httpClose(http_t
*http
) // I - HTTP connection
294 http_field_t field
; // Current field
296 OM_uint32 minor_status
; // Minor status code
297 #endif // HAVE_GSSAPI
300 DEBUG_printf("httpClose(http=%p)", (void *)http
);
303 // Range check input...
310 // Close any open connection...
313 _httpDisconnect(http
);
316 // Free memory used...
319 httpAddrFreeList(http
->addrlist
);
324 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
325 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
327 if (http
->gssname
!= GSS_C_NO_NAME
)
328 gss_release_name(&minor_status
, &http
->gssname
);
329 #endif // HAVE_GSSAPI
331 #ifdef HAVE_AUTHORIZATION_H
333 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
334 #endif // HAVE_AUTHORIZATION_H
336 for (field
= HTTP_FIELD_ACCEPT_LANGUAGE
; field
< HTTP_FIELD_MAX
; field
++)
338 free(http
->default_fields
[field
]);
339 if (field
>= HTTP_FIELD_ACCEPT_ENCODING
|| http
->fields
[field
] != http
->_fields
[field
])
340 free(http
->fields
[field
]);
343 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
344 free(http
->authstring
);
346 _httpFreeCredentials(http
->tls_credentials
);
353 // 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
355 // @deprecated@ @exclude all@
358 int // O - 1 if they match, 0 if they do not
359 httpCompareCredentials(
360 cups_array_t
*cred1
, // I - First set of X.509 credentials
361 cups_array_t
*cred2
) // I - Second set of X.509 credentials
371 // 'httpConnect()' - Connect to a HTTP server.
373 // This function is deprecated - use @link httpConnect2@ instead.
375 // @deprecated@ @exclude all@
378 http_t
* // O - New HTTP connection
379 httpConnect(const char *host
, // I - Host to connect to
380 int port
) // I - Port number
382 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
387 // 'httpConnect2()' - Connect to a HTTP server.
389 // This function creates a connection to a HTTP server. The "host" and "port"
390 // arguments specify a hostname or IP address and port number to use while the
391 // "addrlist" argument specifies a list of addresses to use or `NULL` to do a
392 // fresh lookup. The "family" argument specifies the address family to use -
393 // `AF_UNSPEC` to try both IPv4 and IPv6, `AF_INET` for IPv4, or `AF_INET6` for
396 // The "encryption" argument specifies whether to encrypt the connection and the
397 // "blocking" argument specifies whether to use blocking behavior when reading
400 // The "msec" argument specifies how long to try to connect to the server or `0`
401 // to just create an unconnected `http_t` object. The "cancel" argument
402 // specifies an integer variable that can be set to a non-zero value to cancel
403 // the connection process.
408 http_t
* // O - New HTTP connection
410 const char *host
, // I - Host to connect to
411 int port
, // I - Port number
412 http_addrlist_t
*addrlist
, // I - List of addresses or `NULL` to lookup
413 int family
, // I - Address family to use or `AF_UNSPEC` for any
414 http_encryption_t encryption
, // I - Type of encryption to use
415 int blocking
, // I - 1 for blocking connection, 0 for non-blocking
416 int msec
, // I - Connection timeout in milliseconds, 0 means don't connect
417 int *cancel
) // I - Pointer to "cancel" variable
419 http_t
*http
; // New HTTP connection
422 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
);
424 // Create the HTTP structure...
425 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
, _HTTP_MODE_CLIENT
)) == NULL
)
428 // Optionally connect to the remote system...
429 if (msec
== 0 || httpConnectAgain(http
, msec
, cancel
))
432 // Could not connect to any known address - bail out!
440 // 'httpConnectAgain()' - Reconnect to a HTTP server with timeout and optional cancel variable.
443 bool // O - `true` on success, `false` on failure
444 httpConnectAgain(http_t
*http
, // I - HTTP connection
445 int msec
, // I - Timeout in milliseconds
446 int *cancel
) // I - Pointer to "cancel" variable
448 http_addrlist_t
*addr
; // Connected address
449 char *orig_creds
; // Original peer credentials
451 http_addrlist_t
*current
; // Current address
452 char temp
[256]; // Temporary address string
456 DEBUG_printf("httpConnectAgain(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
);
460 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
464 orig_creds
= httpCopyPeerCredentials(http
);
468 DEBUG_puts("1httpConnectAgain: Shutting down TLS...");
472 // Close any previously open socket...
475 DEBUG_printf("1httpConnectAgain: Closing socket %d...", http
->fd
);
477 httpAddrClose(NULL
, http
->fd
);
482 // Reset all state (except fields, which may be reused)...
483 http
->state
= HTTP_STATE_WAITING
;
484 http
->version
= HTTP_VERSION_1_1
;
485 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
486 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
487 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
488 http
->_data_remaining
= 0;
490 http
->data_remaining
= 0;
491 http
->hostaddr
= NULL
;
494 // Connect to the server...
496 for (current
= http
->addrlist
; current
; current
= current
->next
)
497 DEBUG_printf("1httpConnectAgain: Address %s:%d", httpAddrString(&(current
->addr
), temp
, sizeof(temp
)), httpAddrPort(&(current
->addr
)));
500 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
502 // Unable to connect...
504 http
->error
= WSAGetLastError();
508 http
->status
= HTTP_STATUS_ERROR
;
510 DEBUG_printf("1httpConnectAgain: httpAddrConnect failed: %s", strerror(http
->error
));
517 DEBUG_printf("1httpConnectAgain: New socket=%d", http
->fd
);
519 if (http
->timeout_value
> 0)
520 http_set_timeout(http
->fd
, http
->timeout_value
);
522 http
->hostaddr
= &(addr
->addr
);
525 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
527 // Always do encryption via TLS.
528 if (!_httpTLSStart(http
))
530 httpAddrClose(NULL
, http
->fd
);
538 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
540 if (!http_tls_upgrade(http
))
548 DEBUG_printf("1httpConnectAgain: Connected to %s:%d...", httpAddrGetString(http
->hostaddr
, temp
, sizeof(temp
)), httpAddrGetPort(http
->hostaddr
));
552 char *new_creds
= httpCopyPeerCredentials(http
);
553 // New peer credentials
555 if (!new_creds
|| (strcmp(orig_creds
, new_creds
) && cupsGetCredentialsTrust(/*path*/NULL
, http
->hostname
, new_creds
, /*require_ca*/true) != HTTP_TRUST_OK
))
557 // New and old credentials don't match and the new cert doesn't validate...
558 _httpDisconnect(http
);
576 // 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
578 // This function is now deprecated. Please use the @link httpConnect2@ function
581 // @deprecated@ @exclude all@
584 http_t
* // O - New HTTP connection
586 const char *host
, // I - Host to connect to
587 int port
, // I - Port number
588 http_encryption_t encryption
) // I - Type of encryption to use
590 DEBUG_printf("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)", host
, port
, encryption
);
592 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
));
597 // 'httpConnectURI()' - Connect to a HTTP service using a URI.
599 // This function creates a connection to a HTTP server. The "uri" argument
600 // specifies a "http", "https", "ipp", or "ipps" URI for the service.
602 // The resource path for the service is returned in the buffer pointed to by
603 // the "resource" argument of size "rsize".
605 // The "msec" argument specifies how long to try to connect to the server or `0`
606 // to just create an unconnected `http_t` object. The "cancel" argument
607 // specifies an integer variable that can be set to a non-zero value to cancel
608 // the connection process.
610 // The "require_ca" argument specifies whether to verify that the service
611 // connection is using a CA-signed X.509 certificate.
616 http_t
* // O - New HTTP connection
617 httpConnectURI(const char *uri
, // I - Service to connect to
618 char *host
, // I - Host name buffer (`NULL` for don't care)
619 size_t hsize
, // I - Size of host name buffer
620 int *port
, // O - Port number (`NULL` for don't care)
621 char *resource
, // I - Resource path buffer (`NULL` for don't care)
622 size_t rsize
, // I - Size of resource path buffer
623 bool blocking
, // I - `true` for blocking connection, `false` for non-blocking
624 int msec
, // I - Connection timeout in milliseconds, `0` means don't connect
625 int *cancel
, // I - Pointer to "cancel" variable or `NULL` for none
626 bool require_ca
) // I - `true` to require a CA-signed X.509 certificate
628 http_t
*http
; // New HTTP connection
629 char scheme
[32], // URI scheme
630 userpass
[32], // URI username:password
631 lhost
[256], // URI host (local copy)
632 lresource
[256]; // URI resource (local copy)
633 int lport
; // URI port (local copy)
634 http_encryption_t encryption
; // Type of encryption to use
635 http_uri_status_t uri_status
; // URI separation status
638 DEBUG_printf("httpConnectURI(uri=\"%s\", host=%p, hsize=%u, port=%p, resource=%p, rsize=%u, blocking=%d, msec=%d, cancel=%p, require_ca=%s)", uri
, (void *)host
, (unsigned)hsize
, (void *)port
, (void *)resource
, (unsigned)rsize
, blocking
, msec
, (void *)cancel
, require_ca
? "true" : "false");
640 // Range check input...
658 hsize
= sizeof(lhost
);
666 resource
= lresource
;
667 rsize
= sizeof(lresource
);
670 // Get the URI components...
671 if ((uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, hsize
, port
, resource
, rsize
)) < HTTP_URI_STATUS_OK
)
673 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, httpURIStatusString(uri_status
), 0);
677 DEBUG_printf("1httpConnectURI: scheme=\"%s\", host=\"%s\", port=%d, resource=\"%s\"", scheme
, host
, *port
, resource
);
679 if (!strcmp(scheme
, "https") || !strcmp(scheme
, "ipps") || *port
== 443)
680 encryption
= HTTP_ENCRYPTION_ALWAYS
;
682 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
684 http
= httpConnect2(host
, *port
, /*addrlist*/NULL
, AF_UNSPEC
, encryption
, blocking
, msec
, cancel
);
686 if (httpIsEncrypted(http
))
688 // Validate trust with service...
689 char *creds
; // Peer credentials...
690 http_trust_t trust
; // Trust in credentials...
692 creds
= httpCopyPeerCredentials(http
);
693 trust
= cupsGetCredentialsTrust(/*path*/NULL
, host
, creds
, require_ca
);
695 if (trust
== HTTP_TRUST_OK
)
697 // Pin the trusted credentials (for TOFU)...
698 cupsSaveCredentials(/*path*/NULL
, host
, creds
, /*key*/NULL
);
700 else if (trust
!= HTTP_TRUST_RENEWED
)
702 // Don't allow the connection...
715 // 'httpDelete()' - Send a DELETE request to the server.
717 // @deprecated@ @exclude all@
720 int // O - Status of call (0 = success)
721 httpDelete(http_t
*http
, // I - HTTP connection
722 const char *uri
) // I - URI to delete
724 return (http_send(http
, HTTP_STATE_DELETE
, uri
) ? 0 : -1);
729 // '_httpDisconnect()' - Disconnect a HTTP connection.
733 _httpDisconnect(http_t
*http
) // I - HTTP connection
738 httpAddrClose(NULL
, http
->fd
);
745 // 'httpEncryption()' - Set the required encryption on the link.
747 // @deprecated@ @exclude all@
750 int // O - -1 on error, 0 on success
751 httpEncryption(http_t
*http
, // I - HTTP connection
752 http_encryption_t e
) // I - New encryption preference
754 return (httpSetEncryption(http
, e
) ? 0 : -1);
759 // 'httpError()' - Get the last error on a connection.
761 // @deprecated@ @exclude all@
764 int // O - Error code (errno) value
765 httpError(http_t
*http
) // I - HTTP connection
767 return (httpGetError(http
));
772 // 'httpFieldValue()' - Return the HTTP field enumeration value for a field name.
775 http_field_t
// O - Field index
776 httpFieldValue(const char *name
) // I - String name
778 int i
; // Looping var
781 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
783 if (!_cups_strcasecmp(name
, http_fields
[i
]))
784 return ((http_field_t
)i
);
787 return (HTTP_FIELD_UNKNOWN
);
792 // 'httpFlush()' - Flush data read from a HTTP connection.
796 httpFlush(http_t
*http
) // I - HTTP connection
798 char buffer
[8192]; // Junk buffer
799 int blocking
; // To block or not to block
800 http_state_t oldstate
; // Old state
803 DEBUG_printf("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
));
805 // Nothing to do if we are in the "waiting" state...
806 if (http
->state
== HTTP_STATE_WAITING
)
809 // Temporarily set non-blocking mode so we don't get stuck in httpRead()...
810 blocking
= http
->blocking
;
813 // Read any data we can...
814 oldstate
= http
->state
;
815 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
817 // Restore blocking and reset the connection if we didn't get all of
818 // the remaining data...
819 http
->blocking
= blocking
;
821 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
824 // Didn't get the data back, so close the current connection.
826 http_content_coding_finish(http
);
828 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
830 http
->state
= HTTP_STATE_WAITING
;
835 httpAddrClose(NULL
, http
->fd
);
843 // 'httpFlushWrite()' - Flush data written to a HTTP connection.
848 int // O - Bytes written or -1 on error
849 httpFlushWrite(http_t
*http
) // I - HTTP connection
851 ssize_t bytes
; // Bytes written
854 DEBUG_printf("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100);
856 if (!http
|| !http
->wused
)
858 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." : "1httpFlushWrite: No connection.");
862 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
863 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
865 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
869 DEBUG_printf("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
);
876 // 'httpFreeCredentials()' - Free an array of credentials.
878 // @deprecated@ @exclude all@
883 cups_array_t
*credentials
) // I - Array of credentials
890 // 'httpGet()' - Send a GET request to the server.
892 // @deprecated@ @exclude all@
895 int // O - Status of call (0 = success)
896 httpGet(http_t
*http
, // I - HTTP connection
897 const char *uri
) // I - URI to get
899 return (http_send(http
, HTTP_STATE_GET
, uri
) ? 0 : -1);
904 // 'httpGetActivity()' - Get the most recent activity for a connection.
906 // The return value is the time in seconds of the last read or write.
908 // @since CUPS 2.0/OS 10.10@
911 time_t // O - Time of last read or write
912 httpGetActivity(http_t
*http
) // I - HTTP connection
914 return (http
? http
->activity
: 0);
919 // 'httpGetAuthString()' - Get the current authorization string.
921 // The authorization string is set by @link cupsDoAuthentication@ and
922 // @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
923 // string to use with @link httpSetField@ for the
924 // `HTTP_FIELD_AUTHORIZATION` value.
929 char * // O - Authorization string
930 httpGetAuthString(http_t
*http
) // I - HTTP connection
933 return (http
->authstring
);
940 // 'httpGetBlocking()' - Get the blocking/non-blocking state of a connection.
945 int // O - 1 if blocking, 0 if non-blocking
946 httpGetBlocking(http_t
*http
) // I - HTTP connection
948 return (http
? http
->blocking
: 0);
953 // 'httpGetContentEncoding()' - Get a common content encoding, if any, between
954 // the client and server.
956 // This function uses the value of the Accepts-Encoding HTTP header and must be
957 // called after receiving a response from the server or a request from the
958 // client. The value returned can be use in subsequent requests (for clients)
959 // or in the response (for servers) in order to compress the content stream.
964 const char * // O - Content-Coding value or `NULL` for the identity coding.
965 httpGetContentEncoding(http_t
*http
) // I - HTTP connection
967 if (http
&& http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
969 int i
; // Looping var
970 char temp
[HTTP_MAX_VALUE
], // Copy of Accepts-Encoding value
971 *start
, // Start of coding value
972 *end
; // End of coding value
973 double qvalue
; // "qvalue" for coding
974 struct lconv
*loc
= localeconv(); // Locale data
975 static const char * const codings
[] =
976 { // Supported content codings
983 cupsCopyString(temp
, http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
], sizeof(temp
));
985 for (start
= temp
; *start
; start
= end
)
987 // Find the end of the coding name...
990 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
995 // Grab the qvalue as needed...
996 if (!strncmp(end
, ";q=", 3))
997 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
999 // Skip past all attributes...
1001 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
1009 while (*end
&& isspace(*end
& 255))
1012 // Check value if it matches something we support...
1016 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
1018 if (!strcmp(start
, codings
[i
]))
1019 return (codings
[i
]);
1029 // 'httpGetCookie()' - Get cookie data from the HTTP connection.
1031 // This function returns any HTTP "Set-Cookie:" or "Cookie:" header data for the
1032 // given HTTP connection as described in RFC 6265. Use the
1033 // @link httpGetCookieValue@ to get the value of a named "Cookie:" value.
1035 // @since CUPS 1.1.19@
1038 const char * // O - Cookie data or `NULL`
1039 httpGetCookie(http_t
*http
) // I - HTTP connection
1041 return (http
? http
->cookie
: NULL
);
1046 // 'httpGetCookieValue()' - Get the value of a named cookie from the HTTP connection.
1048 // This function copies the value of a named cookie in the HTTP "Cookie:" header
1049 // for the given HTTP connection as described in RFC 6265. Use the
1050 // @link httpGetCookie@ function to get the original "Cookie:" string.
1055 char * // O - Cookie value or `NULL` if not present
1056 httpGetCookieValue(http_t
*http
, // I - HTTP connection
1057 const char *name
, // I - Cookie name
1058 char *buffer
, // I - Value buffer
1059 size_t bufsize
) // I - Size of value buffer
1061 const char *cookie
; // Cookie: header value
1062 char current
[128], // Current name string
1063 *ptr
, // Pointer into name/buffer
1064 *end
; // End of name/buffer
1065 bool match
; // Does the current name match?
1068 // Range check input...
1072 if (!http
|| !http
->cookie
|| !name
|| !buffer
|| bufsize
< 2)
1075 // Loop through the cookie string...
1076 for (cookie
= http
->cookie
; *cookie
;)
1078 // Skip leading whitespace...
1079 while (isspace(*cookie
& 255))
1085 for (ptr
= current
, end
= current
+ sizeof(current
) - 1; *cookie
&& *cookie
!= '=';)
1097 match
= !strcmp(current
, name
);
1100 // Then the value...
1101 if (*cookie
== '\"')
1103 // Copy quoted value...
1104 for (cookie
++, ptr
= buffer
, end
= buffer
+ bufsize
- 1; *cookie
&& *cookie
!= '\"';)
1106 if (match
&& ptr
< end
)
1112 if (*cookie
== '\"')
1119 // Copy unquoted value...
1120 for (ptr
= buffer
, end
= buffer
+ bufsize
- 1; *cookie
&& *cookie
!= ';';)
1122 if (match
&& ptr
< end
)
1131 // Got the value we were looking for, nul-terminate and return...
1136 // Skip over separator...
1141 // If we get here then we never found the cookie...
1147 // 'httpGetEncryption()' - Get the current encryption mode of a connection.
1149 // This function returns the encryption mode for the connection. Use the
1150 // @link httpIsEncrypted@ function to determine whether a TLS session has
1151 // been established.
1156 http_encryption_t
// O - Current encryption mode
1157 httpGetEncryption(http_t
*http
) // I - HTTP connection
1159 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
1164 // 'httpGetError()' - Get the last error on a connection.
1169 int // O - Error code (errno) value
1170 httpGetError(http_t
*http
) // I - HTTP connection
1173 return (http
->error
);
1180 // 'httpGetExpect()' - Get the value of the Expect header, if any.
1182 // Returns `HTTP_STATUS_NONE` if there is no Expect header, otherwise
1183 // returns the expected HTTP status code, typically `HTTP_STATUS_CONTINUE`.
1188 http_status_t
// O - Expect: status, if any
1189 httpGetExpect(http_t
*http
) // I - HTTP connection
1192 return (HTTP_STATUS_ERROR
);
1194 return (http
->expect
);
1199 // 'httpGetFd()' - Get the file descriptor associated with a connection.
1204 int // O - File descriptor or -1 if none
1205 httpGetFd(http_t
*http
) // I - HTTP connection
1207 return (http
? http
->fd
: -1);
1212 // 'httpGetField()' - Get a field value from a request/response.
1215 const char * // O - Field value
1216 httpGetField(http_t
*http
, // I - HTTP connection
1217 http_field_t field
) // I - Field to get
1219 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1221 else if (http
->fields
[field
])
1222 return (http
->fields
[field
]);
1229 // 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
1231 // @since CUPS 2.0/OS 10.10@
1234 http_keepalive_t
// O - Keep-Alive state
1235 httpGetKeepAlive(http_t
*http
) // I - HTTP connection
1237 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1242 // 'httpGetLength()' - Get the amount of data remaining from the Content-Length or Transfer-Encoding fields.
1244 // This function is deprecated and will not return lengths larger than
1245 // 2^31 - 1; use @link httpGetLength2@ instead.
1247 // @deprecated@ @exclude all@
1250 int // O - Content length
1251 httpGetLength(http_t
*http
) // I - HTTP connection
1253 // Get the read content length and return the 32-bit value.
1256 httpGetLength2(http
);
1258 return (http
->_data_remaining
);
1268 // 'httpGetLength2()' - Get the amount of data remaining from the Content-Length or Transfer-Encoding fields.
1270 // This function returns the complete content length, even for
1271 // content larger than 2^31 - 1.
1276 off_t
// O - Content length
1277 httpGetLength2(http_t
*http
) // I - HTTP connection
1279 off_t remaining
; // Remaining length
1282 DEBUG_printf("2httpGetLength2(http=%p), state=%s", (void *)http
, http
? httpStateString(http
->state
) : "NONE");
1287 if (http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
] && !_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1289 DEBUG_puts("4httpGetLength2: chunked request!");
1294 // The following is a hack for HTTP servers that don't send a
1295 // Content-Length or Transfer-Encoding field...
1297 // If there is no Content-Length then the connection must close
1298 // after the transfer is complete...
1299 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
] || !http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1301 // Default content length is 0 for errors and certain types of operations,
1302 // and 2^31-1 for other successful requests...
1303 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
|| http
->state
== HTTP_STATE_OPTIONS
|| (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) || http
->state
== HTTP_STATE_HEAD
|| (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) || http
->state
== HTTP_STATE_DELETE
|| http
->state
== HTTP_STATE_TRACE
|| http
->state
== HTTP_STATE_CONNECT
)
1306 remaining
= 2147483647;
1308 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], NULL
, 10)) < 0)
1313 DEBUG_printf("4httpGetLength2: content_length=" CUPS_LLFMT
, CUPS_LLCAST remaining
);
1321 // 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1323 // @since CUPS 2.0/OS 10.10@
1326 size_t // O - Number of bytes buffered
1327 httpGetPending(http_t
*http
) // I - HTTP connection
1329 return (http
? (size_t)http
->wused
: 0);
1334 // 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1336 // @since CUPS 2.0/OS 10.10@
1339 size_t // O - Number of bytes available
1340 httpGetReady(http_t
*http
) // I - HTTP connection
1344 else if (http
->used
> 0)
1345 return ((size_t)http
->used
);
1347 return (_httpTLSPending(http
));
1354 // 'httpGetRemaining()' - Get the number of remaining bytes in the message body or current chunk.
1356 // The @link httpIsChunked@ function can be used to determine whether the
1357 // message body is chunked or fixed-length.
1359 // @since CUPS 2.0/OS 10.10@
1362 size_t // O - Remaining bytes
1363 httpGetRemaining(http_t
*http
) // I - HTTP connection
1365 return (http
? (size_t)http
->data_remaining
: 0);
1370 // 'httpGets()' - Get a line of text from a HTTP connection.
1372 // @deprecated@ @exclude all@
1375 char * // O - Line or `NULL`
1376 httpGets(char *line
, // I - Line to read into
1377 int length
, // I - Max length of buffer
1378 http_t
*http
) // I - HTTP connection
1380 return (httpGets2(http
, line
, (size_t)length
));
1385 // 'httpGets2()' - Get a line of text from a HTTP connection.
1388 char * // O - Line or `NULL`
1389 httpGets2(http_t
*http
, // I - HTTP connection
1390 char *line
, // I - Line to read into
1391 size_t length
) // I - Max length of buffer
1393 char *lineptr
, // Pointer into line
1394 *lineend
, // End of line
1395 *bufptr
, // Pointer into input buffer
1396 *bufend
; // Pointer to end of buffer
1397 ssize_t bytes
; // Number of bytes read
1398 int eol
; // End-of-line?
1401 DEBUG_printf("2httpGets2(http=%p, line=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)line
, CUPS_LLCAST length
);
1403 if (!http
|| !line
|| length
<= 1)
1406 // Read a line from the buffer...
1409 lineend
= line
+ length
- 1;
1412 while (lineptr
< lineend
)
1414 // Pre-load the buffer as needed...
1421 while (http
->used
== 0)
1423 // No newline; see if there is more data to be read...
1424 while (!_httpWait(http
, http
->wait_value
, 1))
1426 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1429 DEBUG_puts("3httpGets2: Timed out!");
1431 http
->error
= WSAETIMEDOUT
;
1433 http
->error
= ETIMEDOUT
;
1438 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1440 DEBUG_printf("4httpGets2: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
);
1444 // Nope, can't get a line this time...
1446 DEBUG_printf("3httpGets2: recv() error %d!", WSAGetLastError());
1448 if (WSAGetLastError() == WSAEINTR
)
1452 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1454 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1457 http
->error
= WSAGetLastError();
1460 else if (WSAGetLastError() != http
->error
)
1462 http
->error
= WSAGetLastError();
1467 DEBUG_printf("3httpGets2: recv() error %d!", errno
);
1473 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1475 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1477 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1480 http
->error
= errno
;
1483 else if (errno
!= http
->error
)
1485 http
->error
= errno
;
1492 else if (bytes
== 0)
1494 http
->error
= EPIPE
;
1499 // Yup, update the amount used...
1500 http
->used
+= (int)bytes
;
1503 // Now copy as much of the current line as possible...
1504 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
; lineptr
< lineend
&& bufptr
< bufend
;)
1506 if (*bufptr
== 0x0a)
1512 else if (*bufptr
== 0x0d)
1518 *lineptr
++ = *bufptr
++;
1522 http
->used
-= (int)(bufptr
- http
->buffer
);
1524 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1529 http
->activity
= time(NULL
);
1533 DEBUG_printf("3httpGets2: Returning \"%s\"", line
);
1539 DEBUG_puts("3httpGets2: No new line available!");
1546 // 'httpGetState()' - Get the current state of the HTTP request.
1549 http_state_t
// O - HTTP state
1550 httpGetState(http_t
*http
) // I - HTTP connection
1552 return (http
? http
->state
: HTTP_STATE_ERROR
);
1557 // 'httpGetStatus()' - Get the status of the last HTTP request.
1562 http_status_t
// O - HTTP status
1563 httpGetStatus(http_t
*http
) // I - HTTP connection
1565 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1570 // 'httpGetSubField()' - Get a sub-field value.
1572 // @deprecated@ @exclude all@
1575 char * // O - Value or `NULL`
1576 httpGetSubField(http_t
*http
, // I - HTTP connection
1577 http_field_t field
, // I - Field index
1578 const char *name
, // I - Name of sub-field
1579 char *value
) // O - Value string
1581 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1586 // 'httpGetSubField2()' - Get a sub-field value.
1591 char * // O - Value or `NULL`
1592 httpGetSubField2(http_t
*http
, // I - HTTP connection
1593 http_field_t field
, // I - Field index
1594 const char *name
, // I - Name of sub-field
1595 char *value
, // O - Value string
1596 int valuelen
) // I - Size of value buffer
1598 const char *fptr
; // Pointer into field
1599 char temp
[HTTP_MAX_VALUE
], // Temporary buffer for name
1600 *ptr
, // Pointer into string buffer
1601 *end
; // End of value buffer
1603 DEBUG_printf("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
);
1608 if (!http
|| !name
|| !value
|| valuelen
< 2 || field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !http
->fields
[field
])
1611 end
= value
+ valuelen
- 1;
1613 for (fptr
= http
->fields
[field
]; *fptr
;)
1615 // Skip leading whitespace...
1616 while (_cups_isspace(*fptr
))
1625 // Get the sub-field name...
1626 for (ptr
= temp
; *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) && ptr
< (temp
+ sizeof(temp
) - 1); *ptr
++ = *fptr
++)
1627 ; // Copy sub-field name
1631 DEBUG_printf("4httpGetSubField2: name=\"%s\"", temp
);
1633 // Skip trailing chars up to the '='...
1634 while (_cups_isspace(*fptr
))
1643 // Skip = and leading whitespace...
1646 while (_cups_isspace(*fptr
))
1651 // Read quoted string...
1652 for (ptr
= value
, fptr
++; *fptr
&& *fptr
!= '\"' && ptr
< end
; *ptr
++ = *fptr
++)
1653 ; // Copy quoted string
1657 while (*fptr
&& *fptr
!= '\"')
1665 // Read unquoted string...
1666 for (ptr
= value
; *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
; *ptr
++ = *fptr
++)
1667 ; // Copy unquoted string
1671 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1675 DEBUG_printf("4httpGetSubField2: value=\"%s\"", value
);
1677 // See if this is the one...
1678 if (!strcmp(name
, temp
))
1680 DEBUG_printf("3httpGetSubField2: Returning \"%s\"", value
);
1687 DEBUG_puts("3httpGetSubField2: Returning NULL");
1694 // 'httpGetVersion()' - Get the HTTP version at the other end.
1697 http_version_t
// O - Version number
1698 httpGetVersion(http_t
*http
) // I - HTTP connection
1700 return (http
? http
->version
: HTTP_VERSION_1_0
);
1705 // 'httpHead()' - Send a HEAD request to the server.
1707 // @deprecated@ @exclude all@
1710 int // O - Status of call (0 = success)
1711 httpHead(http_t
*http
, // I - HTTP connection
1712 const char *uri
) // I - URI for head
1714 DEBUG_printf("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
);
1715 return (http_send(http
, HTTP_STATE_HEAD
, uri
) ? 0 : -1);
1720 // 'httpInitialize()' - Initialize the HTTP interface library and set the
1721 // default HTTP proxy (if any).
1725 httpInitialize(void)
1727 static int initialized
= 0; // Have we been called before?
1729 WSADATA winsockdata
; // WinSock data
1736 _cupsGlobalUnlock();
1741 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1743 #elif !defined(SO_NOSIGPIPE)
1744 // Ignore SIGPIPE signals...
1745 struct sigaction action
; // POSIX sigaction data
1748 memset(&action
, 0, sizeof(action
));
1749 action
.sa_handler
= SIG_IGN
;
1750 sigaction(SIGPIPE
, &action
, NULL
);
1753 _httpTLSInitialize();
1756 _cupsGlobalUnlock();
1761 // 'httpIsChunked()' - Report whether a message body is chunked.
1763 // This function returns non-zero if the message body is composed of
1764 // variable-length chunks.
1766 // @since CUPS 2.0/OS 10.10@
1769 int // O - 1 if chunked, 0 if not
1770 httpIsChunked(http_t
*http
) // I - HTTP connection
1772 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1777 // 'httpIsEncrypted()' - Report whether a connection is encrypted.
1779 // This function returns non-zero if the connection is currently encrypted.
1781 // @since CUPS 2.0/OS 10.10@
1784 int // O - 1 if encrypted, 0 if not
1785 httpIsEncrypted(http_t
*http
) // I - HTTP connection
1787 return (http
? http
->tls
!= NULL
: 0);
1792 // 'httpLoadCredentials()' - Load credentials.
1794 // @deprecated@ @exclude all@
1797 int // O - -1 on error
1798 httpLoadCredentials(
1799 const char *path
, // I - Path for certs
1800 cups_array_t
**credentials
, // O - Array of credentials
1801 const char *common_name
) // I - Common name
1807 *credentials
= NULL
;
1814 // 'httpOptions()' - Send an OPTIONS request to the server.
1816 // @deprecated@ @exclude all@
1819 int // O - Status of call (0 = success)
1820 httpOptions(http_t
*http
, // I - HTTP connection
1821 const char *uri
) // I - URI for options
1823 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
) ? 0 : -1);
1828 // 'httpPeek()' - Peek at data from a HTTP connection.
1830 // This function copies available data from the given HTTP connection, reading
1831 // a buffer as needed. The data is still available for reading using
1832 // @link httpRead2@.
1834 // For non-blocking connections the usual timeouts apply.
1839 ssize_t
// O - Number of bytes copied
1840 httpPeek(http_t
*http
, // I - HTTP connection
1841 char *buffer
, // I - Buffer for data
1842 size_t length
) // I - Maximum number of bytes
1844 ssize_t bytes
; // Bytes read
1845 char len
[32]; // Length string
1848 DEBUG_printf("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
);
1850 if (http
== NULL
|| buffer
== NULL
)
1853 http
->activity
= time(NULL
);
1859 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& http
->data_remaining
<= 0)
1861 DEBUG_puts("2httpPeek: Getting chunk length...");
1863 if (httpGets2(http
, len
, sizeof(len
)) == NULL
)
1865 DEBUG_puts("1httpPeek: Could not get length!");
1871 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1872 if (!httpGets2(http
, len
, sizeof(len
)))
1874 DEBUG_puts("1httpPeek: Could not get chunk length.");
1879 http
->data_remaining
= strtoll(len
, NULL
, 16);
1881 if (http
->data_remaining
< 0)
1883 DEBUG_puts("1httpPeek: Negative chunk length!");
1888 DEBUG_printf("2httpPeek: data_remaining=" CUPS_LLFMT
, CUPS_LLCAST http
->data_remaining
);
1890 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1892 // A zero-length chunk ends a transfer; unless we are reading POST
1894 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1895 http_content_coding_finish(http
);
1897 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1898 httpGets2(http
, len
, sizeof(len
));
1900 if (http
->state
== HTTP_STATE_POST_RECV
)
1903 http
->state
= HTTP_STATE_STATUS
;
1905 DEBUG_printf("1httpPeek: 0-length chunk, set state to %s.", httpStateString(http
->state
));
1907 // Prevent future reads for this request...
1908 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1912 else if (length
> (size_t)http
->data_remaining
)
1914 length
= (size_t)http
->data_remaining
;
1917 if (http
->used
== 0 && (http
->coding
== _HTTP_CODING_IDENTITY
|| (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)))
1919 // Buffer small reads for better performance...
1920 ssize_t buflen
; // Length of read for buffer
1922 if (!http
->blocking
)
1924 while (!httpWait(http
, http
->wait_value
))
1926 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1933 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1934 buflen
= sizeof(http
->buffer
);
1936 buflen
= (ssize_t
)http
->data_remaining
;
1938 DEBUG_printf("2httpPeek: Reading %d bytes into buffer.", (int)buflen
);
1939 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1941 DEBUG_printf("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.", CUPS_LLCAST bytes
);
1945 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1948 http
->used
= (int)bytes
;
1952 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1954 int zerr
; // Decompressor error
1955 z_stream stream
; // Copy of decompressor stream
1957 if (http
->used
> 0 && ((z_stream
*)http
->stream
)->avail_in
< HTTP_MAX_BUFFER
)
1959 size_t buflen
= HTTP_MAX_BUFFER
- ((z_stream
*)http
->stream
)->avail_in
;
1960 // Number of bytes to copy
1962 if (((z_stream
*)http
->stream
)->avail_in
> 0 && ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
1963 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
1965 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
1967 if (buflen
> (size_t)http
->data_remaining
)
1968 buflen
= (size_t)http
->data_remaining
;
1970 if (buflen
> (size_t)http
->used
)
1971 buflen
= (size_t)http
->used
;
1973 DEBUG_printf("1httpPeek: Copying %d more bytes of data into decompression buffer.", (int)buflen
);
1975 memcpy(http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, http
->buffer
, buflen
);
1976 ((z_stream
*)http
->stream
)->avail_in
+= buflen
;
1977 http
->used
-= (int)buflen
;
1978 http
->data_remaining
-= (off_t
)buflen
;
1981 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1984 DEBUG_printf("2httpPeek: length=%d, avail_in=%d", (int)length
, (int)((z_stream
*)http
->stream
)->avail_in
);
1986 if (inflateCopy(&stream
, (z_stream
*)http
->stream
) != Z_OK
)
1988 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1989 http
->error
= ENOMEM
;
1990 inflateEnd(&stream
);
1994 stream
.next_out
= (Bytef
*)buffer
;
1995 stream
.avail_out
= (uInt
)length
;
1997 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1998 inflateEnd(&stream
);
2002 DEBUG_printf("2httpPeek: zerr=%d", zerr
);
2004 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
2011 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
2013 else if (http
->used
> 0)
2015 if (length
> (size_t)http
->used
)
2016 length
= (size_t)http
->used
;
2018 bytes
= (ssize_t
)length
;
2020 DEBUG_printf("2httpPeek: grabbing %d bytes from input buffer...", (int)bytes
);
2022 memcpy(buffer
, http
->buffer
, length
);
2032 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
2035 http
->error
= WSAGetLastError();
2037 if (errno
== EINTR
|| errno
== EAGAIN
)
2040 http
->error
= errno
;
2043 else if (bytes
== 0)
2045 http
->error
= EPIPE
;
2054 // 'httpPost()' - Send a POST request to the server.
2056 // @deprecated@ @exclude all@
2059 int // O - Status of call (0 = success)
2060 httpPost(http_t
*http
, // I - HTTP connection
2061 const char *uri
) // I - URI for post
2063 return (http_send(http
, HTTP_STATE_POST
, uri
) ? 0 : -1);
2068 // 'httpPrintf()' - Print a formatted string to a HTTP connection.
2073 int // O - Number of bytes written or `-1` on error
2074 httpPrintf(http_t
*http
, // I - HTTP connection
2075 const char *format
, // I - printf-style format string
2076 ...) // I - Additional args as needed
2078 ssize_t bytes
; // Number of bytes to write
2079 char buf
[65536]; // Buffer for formatted string
2080 va_list ap
; // Variable argument pointer
2083 DEBUG_printf("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
);
2085 va_start(ap
, format
);
2086 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
2089 DEBUG_printf("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
);
2091 if (bytes
> (ssize_t
)(sizeof(buf
) - 1))
2093 http
->error
= ENOMEM
;
2096 else if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
2098 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
2104 DEBUG_puts("4httpPrintf: flushing existing data...");
2106 if (httpFlushWrite(http
) < 0)
2110 return ((int)http_write(http
, buf
, (size_t)bytes
));
2116 // 'httpPut()' - Send a PUT request to the server.
2118 // @deprecated@ @exclude all@
2121 int // O - Status of call (0 = success)
2122 httpPut(http_t
*http
, // I - HTTP connection
2123 const char *uri
) // I - URI to put
2125 DEBUG_printf("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
);
2126 return (http_send(http
, HTTP_STATE_PUT
, uri
) ? 0 : -1);
2131 // 'httpRead()' - Read data from a HTTP connection.
2133 // This function is deprecated. Use the httpRead2() function which can
2134 // read more than 2GB of data.
2136 // @deprecated@ @exclude all@
2139 int // O - Number of bytes read
2140 httpRead(http_t
*http
, // I - HTTP connection
2141 char *buffer
, // I - Buffer for data
2142 int length
) // I - Maximum number of bytes
2144 return ((int)httpRead2(http
, buffer
, (size_t)length
));
2149 // 'httpRead2()' - Read data from a HTTP connection.
2154 ssize_t
// O - Number of bytes read
2155 httpRead2(http_t
*http
, // I - HTTP connection
2156 char *buffer
, // I - Buffer for data
2157 size_t length
) // I - Maximum number of bytes
2159 ssize_t bytes
; // Bytes read
2162 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
? http
->coding
: 0, http
? http
->data_encoding
: 0, CUPS_LLCAST (http
? http
->data_remaining
: -1));
2164 if (http
== NULL
|| buffer
== NULL
)
2167 http
->activity
= time(NULL
);
2173 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2177 if (((z_stream
*)http
->stream
)->avail_in
> 0)
2179 int zerr
; // Decompressor error
2181 DEBUG_printf("2httpRead2: avail_in=%d, avail_out=%d", (int)((z_stream
*)http
->stream
)->avail_in
, (int)length
);
2183 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)buffer
;
2184 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)length
;
2186 if ((zerr
= inflate((z_stream
*)http
->stream
, Z_SYNC_FLUSH
)) < Z_OK
)
2188 DEBUG_printf("2httpRead2: zerr=%d", zerr
);
2190 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
2197 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
2199 DEBUG_printf("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d", ((z_stream
*)http
->stream
)->avail_in
, ((z_stream
*)http
->stream
)->avail_out
, (int)bytes
);
2208 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)((z_stream
*)http
->stream
)->avail_in
;
2209 // Additional bytes for buffer
2213 if (((z_stream
*)http
->stream
)->avail_in
> 0 && ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
2214 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
2216 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
2218 DEBUG_printf("1httpRead2: Reading up to %d more bytes of data into decompression buffer.", (int)buflen
);
2220 if (http
->data_remaining
> 0)
2222 if (buflen
> http
->data_remaining
)
2223 buflen
= (ssize_t
)http
->data_remaining
;
2225 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2227 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2229 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2238 else if (bytes
== 0)
2241 DEBUG_printf("1httpRead2: Adding " CUPS_LLFMT
" bytes to decompression buffer.", CUPS_LLCAST bytes
);
2243 http
->data_remaining
-= bytes
;
2244 ((z_stream
*)http
->stream
)->avail_in
+= (uInt
)bytes
;
2246 if (http
->data_remaining
<= 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2248 // Read the trailing blank line now...
2249 char len
[32]; // Length string
2251 httpGets2(http
, len
, sizeof(len
));
2264 else if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2266 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2268 http
->data_remaining
-= bytes
;
2270 if (http
->data_remaining
<= 0)
2272 // Read the trailing blank line now...
2273 char len
[32]; // Length string
2275 httpGets2(http
, len
, sizeof(len
));
2279 else if (http
->data_remaining
<= 0)
2281 // No more data to read...
2286 DEBUG_printf("1httpRead2: Reading up to %d bytes into buffer.", (int)length
);
2288 if (length
> (size_t)http
->data_remaining
)
2289 length
= (size_t)http
->data_remaining
;
2291 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2293 http
->data_remaining
-= bytes
;
2295 if (http
->data_remaining
<= 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2297 // Read the trailing blank line now...
2298 char len
[32]; // Length string
2300 httpGets2(http
, len
, sizeof(len
));
2305 if ((http
->coding
== _HTTP_CODING_IDENTITY
|| (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)) && ((http
->data_remaining
<= 0 && http
->data_encoding
== HTTP_ENCODING_LENGTH
) || (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2307 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2308 http_content_coding_finish(http
);
2310 if (http
->state
== HTTP_STATE_POST_RECV
)
2312 else if (http
->state
== HTTP_STATE_GET_SEND
|| http
->state
== HTTP_STATE_POST_SEND
)
2313 http
->state
= HTTP_STATE_WAITING
;
2315 http
->state
= HTTP_STATE_STATUS
;
2317 DEBUG_printf("1httpRead2: End of content, set state to %s.", httpStateString(http
->state
));
2325 // 'httpReadRequest()' - Read a HTTP request from a connection.
2330 http_state_t
// O - New state of connection
2331 httpReadRequest(http_t
*http
, // I - HTTP connection
2332 char *uri
, // I - URI buffer
2333 size_t urilen
) // I - Size of URI buffer
2335 char line
[4096], // HTTP request line
2336 *req_method
, // HTTP request method
2337 *req_uri
, // HTTP request URI
2338 *req_version
; // HTTP request version number string
2341 // Range check input...
2342 DEBUG_printf("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
);
2347 if (!http
|| !uri
|| urilen
< 1)
2349 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2350 return (HTTP_STATE_ERROR
);
2352 else if (http
->state
!= HTTP_STATE_WAITING
)
2354 DEBUG_printf("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.", httpStateString(http
->state
));
2355 return (HTTP_STATE_ERROR
);
2359 httpClearFields(http
);
2361 http
->activity
= time(NULL
);
2362 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2363 http
->data_remaining
= 0;
2364 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2365 http
->status
= HTTP_STATUS_OK
;
2366 http
->version
= HTTP_VERSION_1_1
;
2368 // Read a line from the socket...
2369 if (!httpGets2(http
, line
, sizeof(line
)))
2371 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2372 return (HTTP_STATE_ERROR
);
2377 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2378 return (HTTP_STATE_WAITING
);
2381 DEBUG_printf("1httpReadRequest: %s", line
);
2387 while (*req_uri
&& !isspace(*req_uri
& 255))
2392 DEBUG_puts("1httpReadRequest: No request URI.");
2393 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2394 return (HTTP_STATE_ERROR
);
2399 while (*req_uri
&& isspace(*req_uri
& 255))
2402 req_version
= req_uri
;
2404 while (*req_version
&& !isspace(*req_version
& 255))
2409 DEBUG_puts("1httpReadRequest: No request protocol version.");
2410 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2411 return (HTTP_STATE_ERROR
);
2414 *req_version
++ = '\0';
2416 while (*req_version
&& isspace(*req_version
& 255))
2420 if (!strcmp(req_method
, "OPTIONS"))
2422 http
->state
= HTTP_STATE_OPTIONS
;
2424 else if (!strcmp(req_method
, "GET"))
2426 http
->state
= HTTP_STATE_GET
;
2428 else if (!strcmp(req_method
, "HEAD"))
2430 http
->state
= HTTP_STATE_HEAD
;
2432 else if (!strcmp(req_method
, "POST"))
2434 http
->state
= HTTP_STATE_POST
;
2436 else if (!strcmp(req_method
, "PUT"))
2438 http
->state
= HTTP_STATE_PUT
;
2440 else if (!strcmp(req_method
, "DELETE"))
2442 http
->state
= HTTP_STATE_DELETE
;
2444 else if (!strcmp(req_method
, "TRACE"))
2446 http
->state
= HTTP_STATE_TRACE
;
2448 else if (!strcmp(req_method
, "CONNECT"))
2450 http
->state
= HTTP_STATE_CONNECT
;
2454 DEBUG_printf("1httpReadRequest: Unknown method \"%s\".", req_method
);
2455 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2456 return (HTTP_STATE_UNKNOWN_METHOD
);
2459 DEBUG_printf("1httpReadRequest: Set state to %s.", httpStateString(http
->state
));
2461 if (!strcmp(req_version
, "HTTP/1.0"))
2463 http
->version
= HTTP_VERSION_1_0
;
2464 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2466 else if (!strcmp(req_version
, "HTTP/1.1"))
2468 http
->version
= HTTP_VERSION_1_1
;
2469 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2473 DEBUG_printf("1httpReadRequest: Unknown version \"%s\".", req_version
);
2474 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2475 return (HTTP_STATE_UNKNOWN_VERSION
);
2478 DEBUG_printf("1httpReadRequest: URI is \"%s\".", req_uri
);
2479 cupsCopyString(uri
, req_uri
, urilen
);
2481 return (http
->state
);
2486 // 'httpReconnect()' - Reconnect to a HTTP server.
2488 // This function is deprecated. Please use the @link httpReconnect2@ function
2491 // @deprecated@ @exclude all@
2494 int // O - 0 on success, non-zero on failure
2495 httpReconnect(http_t
*http
) // I - HTTP connection
2497 DEBUG_printf("httpReconnect(http=%p)", (void *)http
);
2499 return (httpReconnect2(http
, 30000, NULL
));
2504 // 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2507 // @deprecated@ @exclude all@
2510 int // O - 0 on success, non-zero on failure
2511 httpReconnect2(http_t
*http
, // I - HTTP connection
2512 int msec
, // I - Timeout in milliseconds
2513 int *cancel
) // I - Pointer to "cancel" variable
2515 DEBUG_printf("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
);
2517 return (httpConnectAgain(http
, msec
, cancel
) ? 0 : -1);
2522 // 'httpSaveCredentials()' - Save credentials.
2524 // @deprecated@ @exclude all@
2527 int // O - -1 on error
2528 httpSaveCredentials(
2529 const char *path
, // I - Path for certs
2530 cups_array_t
*credentials
, // O - Array of credentials
2531 const char *common_name
) // I - Common name
2542 // 'httpSetAuthString()' - Set the current authorization string.
2544 // This function just stores a copy of the current authorization string in
2545 // the HTTP connection object. You must still call @link httpSetField@ to set
2546 // `HTTP_FIELD_AUTHORIZATION` prior to issuing a HTTP request using
2547 // @link httpWriteRequest@.
2553 httpSetAuthString(http_t
*http
, // I - HTTP connection
2554 const char *scheme
, // I - Auth scheme (NULL to clear it)
2555 const char *data
) // I - Auth data (NULL for none)
2557 // Range check input...
2558 DEBUG_printf("httpSetAuthString(http=%p, scheme=\"%s\", data=\"%s\")", (void *)http
, scheme
, data
);
2563 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2564 free(http
->authstring
);
2566 http
->authstring
= http
->_authstring
;
2570 // Set the current authorization string...
2571 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2574 if (len
> sizeof(http
->_authstring
))
2576 if ((temp
= malloc(len
)) == NULL
)
2577 len
= sizeof(http
->_authstring
);
2579 http
->authstring
= temp
;
2583 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2585 cupsCopyString(http
->authstring
, scheme
, len
);
2589 // Clear the current authorization string...
2590 http
->_authstring
[0] = '\0';
2593 DEBUG_printf("1httpSetAuthString: authstring=\"%s\"", http
->authstring
);
2598 // 'httpSetBlocking()' - Set blocking/non-blocking behavior on a connection.
2602 httpSetBlocking(http_t
*http
, // I - HTTP connection
2603 bool b
) // I - `true` for blocking, `false` for non-blocking
2607 http
->blocking
= b
? 1 : 0;
2608 http_set_wait(http
);
2614 // 'httpSetCredentials()' - Set the credentials associated with an encrypted connection.
2616 // @deprecated@ @exclude all@
2619 int // O - Status of call (0 = success)
2620 httpSetCredentials(http_t
*http
, // I - HTTP connection
2621 cups_array_t
*credentials
) // I - Array of credentials
2631 // 'httpSetCookie()' - Add Set-Cookie value(s).
2633 // This function adds one or more Set-Cookie header values that will be sent to
2634 // the client with the @link httpWriteResponse@ function. Each value conforms
2635 // to the format defined in RFC 6265. Multiple values can be passed in the
2636 // "cookie" string separated by a newline character.
2638 // Call the @link httpClearCookies@ function to clear all Set-Cookie values.
2640 // @since CUPS 1.1.19@
2644 httpSetCookie(http_t
*http
, // I - HTTP cnnection
2645 const char *cookie
) // I - Cookie string
2647 // Range check input...
2648 if (!http
|| !cookie
)
2651 // Set or append the Set-Cookie value....
2654 // Append with a newline between values...
2655 size_t clen
, // Length of cookie string
2656 ctotal
; // Total length of cookies
2657 char *temp
; // Temporary value
2659 clen
= strlen(http
->cookie
);
2660 ctotal
= clen
+ strlen(cookie
) + 2;
2662 if ((temp
= realloc(http
->cookie
, ctotal
)) == NULL
)
2665 http
->cookie
= temp
;
2667 cupsCopyString(temp
+ clen
+ 1, cookie
, ctotal
- clen
- 1);
2671 // Just copy/set this cookie...
2672 http
->cookie
= strdup(cookie
);
2678 // 'httpSetDefaultField()' - Set the default value of an HTTP header.
2680 // Currently only `HTTP_FIELD_ACCEPT_ENCODING`, `HTTP_FIELD_SERVER`,
2681 // and `HTTP_FIELD_USER_AGENT` can be set.
2687 httpSetDefaultField(http_t
*http
, // I - HTTP connection
2688 http_field_t field
, // I - Field index
2689 const char *value
)// I - Value
2691 DEBUG_printf("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
);
2693 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
2696 if (http
->default_fields
[field
])
2697 free(http
->default_fields
[field
]);
2699 http
->default_fields
[field
] = value
? strdup(value
) : NULL
;
2704 // 'httpSetEncryption()' - Set the required encryption on the link.
2709 bool // O - `true` on success, `false` on error
2711 http_t
*http
, // I - HTTP connection
2712 http_encryption_t e
) // I - New encryption preference
2714 DEBUG_printf("httpSetEncryption(http=%p, e=%d)", (void *)http
, e
);
2719 if (http
->mode
== _HTTP_MODE_CLIENT
)
2721 http
->encryption
= e
;
2723 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) || (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
2724 return (httpConnectAgain(http
, 30000, NULL
));
2725 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
2726 return (http_tls_upgrade(http
));
2732 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
2735 http
->encryption
= e
;
2736 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
2737 return (_httpTLSStart(http
));
2745 // 'httpSetExpect()' - Set the Expect: header in a request.
2747 // Currently only `HTTP_STATUS_CONTINUE` is supported for the "expect"
2754 httpSetExpect(http_t
*http
, // I - HTTP connection
2755 http_status_t expect
) // I - HTTP status to expect (`HTTP_STATUS_CONTINUE`)
2757 DEBUG_printf("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
);
2760 http
->expect
= expect
;
2765 // 'httpSetField()' - Set the value of an HTTP header.
2769 httpSetField(http_t
*http
, // I - HTTP connection
2770 http_field_t field
, // I - Field index
2771 const char *value
) // I - Value
2773 DEBUG_printf("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
);
2775 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !value
)
2778 http_add_field(http
, field
, value
, 0);
2783 // 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2785 // @since CUPS 2.0/OS 10.10@
2790 http_t
*http
, // I - HTTP connection
2791 http_keepalive_t keep_alive
) // I - New Keep-Alive value
2794 http
->keep_alive
= keep_alive
;
2799 // 'httpSetLength()' - Set the content-length and content-encoding.
2805 httpSetLength(http_t
*http
, // I - HTTP connection
2806 size_t length
) // I - Length (0 for chunked)
2808 DEBUG_printf("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
);
2815 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
2816 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "");
2820 char len
[32]; // Length string
2822 snprintf(len
, sizeof(len
), CUPS_LLFMT
, CUPS_LLCAST length
);
2823 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "");
2824 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, len
);
2830 // 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2832 // The optional timeout callback receives both the HTTP connection and a user
2833 // data pointer and must return 1 to continue or 0 to error (time) out.
2840 http_t
*http
, // I - HTTP connection
2841 double timeout
, // I - Number of seconds for timeout, must be greater than `0.0`
2842 http_timeout_cb_t cb
, // I - Callback function or `NULL`
2843 void *user_data
) // I - User data pointer
2845 if (!http
|| timeout
<= 0.0)
2848 http
->timeout_cb
= cb
;
2849 http
->timeout_data
= user_data
;
2850 http
->timeout_value
= timeout
;
2853 http_set_timeout(http
->fd
, timeout
);
2855 http_set_wait(http
);
2860 // 'httpShutdown()' - Shutdown one side of an HTTP connection.
2862 // @since CUPS 2.0/OS 10.10@
2866 httpShutdown(http_t
*http
) // I - HTTP connection
2868 if (!http
|| http
->fd
< 0)
2875 shutdown(http
->fd
, SD_RECEIVE
); // Microsoft-ism...
2877 shutdown(http
->fd
, SHUT_RD
);
2883 // 'httpTrace()' - Send an TRACE request to the server.
2885 // @deprecated@ @exclude all@
2888 int // O - Status of call (0 = success)
2889 httpTrace(http_t
*http
, // I - HTTP connection
2890 const char *uri
) // I - URI for trace
2892 return (http_send(http
, HTTP_STATE_TRACE
, uri
) ? 0 : -1);
2897 // '_httpUpdate()' - Update the current HTTP status for incoming data.
2899 // Note: Unlike httpUpdate(), this function does not flush pending write data
2900 // and only retrieves a single status line from the HTTP connection.
2903 int // O - 1 to continue, 0 to stop
2904 _httpUpdate(http_t
*http
, // I - HTTP connection
2905 http_status_t
*status
) // O - Current HTTP status
2907 char line
[32768], // Line from connection...
2908 *value
; // Pointer to value on line
2909 http_field_t field
; // Field index
2910 int major
, minor
; // HTTP version numbers
2913 DEBUG_printf("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
));
2915 // Grab a single line from the connection...
2916 if (!httpGets2(http
, line
, sizeof(line
)))
2918 *status
= HTTP_STATUS_ERROR
;
2922 DEBUG_printf("2_httpUpdate: Got \"%s\"", line
);
2924 if (line
[0] == '\0')
2926 // Blank line means the start of the data section (if any). Return
2927 // the result code, too...
2929 // If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2930 // states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2931 // keep on tryin'...
2932 if (http
->status
== HTTP_STATUS_CONTINUE
)
2934 *status
= http
->status
;
2938 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2939 http
->digest_tries
= 0;
2941 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2943 if (!_httpTLSStart(http
))
2945 httpAddrClose(NULL
, http
->fd
);
2948 *status
= http
->status
= HTTP_STATUS_ERROR
;
2952 *status
= HTTP_STATUS_CONTINUE
;
2956 if (http_set_length(http
) < 0)
2958 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2959 http
->error
= EINVAL
;
2960 http
->status
= *status
= HTTP_STATUS_ERROR
;
2964 switch (http
->state
)
2966 case HTTP_STATE_GET
:
2967 case HTTP_STATE_POST
:
2968 case HTTP_STATE_POST_RECV
:
2969 case HTTP_STATE_PUT
:
2972 DEBUG_printf("1_httpUpdate: Set state to %s.", httpStateString(http
->state
));
2974 case HTTP_STATE_POST_SEND
:
2975 case HTTP_STATE_HEAD
:
2979 http
->state
= HTTP_STATE_WAITING
;
2981 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2985 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2986 http_content_coding_start(http
, httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2988 *status
= http
->status
;
2991 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2993 // Got the beginning of a response...
2994 int intstatus
; // Status value as an integer
2996 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2998 *status
= http
->status
= HTTP_STATUS_ERROR
;
3002 httpClearFields(http
);
3004 http
->version
= (http_version_t
)(major
* 100 + minor
);
3005 *status
= http
->status
= (http_status_t
)intstatus
;
3007 else if ((value
= strchr(line
, ':')) != NULL
)
3011 while (_cups_isspace(*value
))
3014 DEBUG_printf("1_httpUpdate: Header %s: %s", line
, value
);
3016 // Be tolerants of servers that send unknown attribute fields...
3017 if (!_cups_strcasecmp(line
, "expect"))
3019 // "Expect: 100-continue" or similar...
3020 http
->expect
= (http_status_t
)atoi(value
);
3022 else if (!_cups_strcasecmp(line
, "cookie"))
3024 // "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3025 httpSetCookie(http
, value
);
3027 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3029 http_add_field(http
, field
, value
, 1);
3031 if (field
== HTTP_FIELD_AUTHENTICATION_INFO
)
3032 httpGetSubField2(http
, HTTP_FIELD_AUTHENTICATION_INFO
, "nextnonce", http
->nextnonce
, (int)sizeof(http
->nextnonce
));
3037 DEBUG_printf("1_httpUpdate: unknown field %s seen!", line
);
3043 DEBUG_printf("1_httpUpdate: Bad response line \"%s\"!", line
);
3044 http
->error
= EINVAL
;
3045 http
->status
= *status
= HTTP_STATUS_ERROR
;
3054 // 'httpUpdate()' - Update the current HTTP state for incoming data.
3057 http_status_t
// O - HTTP status
3058 httpUpdate(http_t
*http
) // I - HTTP connection
3060 http_status_t status
; // Request status
3063 DEBUG_printf("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
));
3065 // Flush pending data, if any...
3068 DEBUG_puts("2httpUpdate: flushing buffer...");
3070 if (httpFlushWrite(http
) < 0)
3071 return (HTTP_STATUS_ERROR
);
3074 // If we haven't issued any commands, then there is nothing to "update"...
3075 if (http
->state
== HTTP_STATE_WAITING
)
3076 return (HTTP_STATUS_CONTINUE
);
3078 // Grab all of the lines we can from the connection...
3079 while (_httpUpdate(http
, &status
))
3080 ; // Update as needed...
3082 // See if there was an error...
3083 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3085 DEBUG_printf("1httpUpdate: Returning status %d...", http
->status
);
3086 return (http
->status
);
3091 DEBUG_printf("1httpUpdate: socket error %d - %s", http
->error
, strerror(http
->error
));
3092 http
->status
= HTTP_STATUS_ERROR
;
3093 return (HTTP_STATUS_ERROR
);
3096 // Return the current status...
3102 // '_httpWait()' - Wait for data available on a connection (no flush).
3105 int // O - 1 if data is available, 0 otherwise
3106 _httpWait(http_t
*http
, // I - HTTP connection
3107 int msec
, // I - Milliseconds to wait
3108 int usessl
) // I - Use TLS context?
3110 struct pollfd pfd
; // Polled file descriptor
3111 int nfds
; // Result from select()/poll()
3114 DEBUG_printf("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
);
3118 DEBUG_printf("5_httpWait: Returning 0 since fd=%d", http
->fd
);
3122 // Check the TLS buffers for data first...
3123 if (http
->tls
&& _httpTLSPending(http
))
3125 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3129 // Then try doing a select() or poll() to poll the socket...
3131 pfd
.events
= POLLIN
;
3135 nfds
= poll(&pfd
, 1, msec
);
3138 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
));
3140 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3143 DEBUG_printf("5_httpWait: returning with nfds=%d, errno=%d...", nfds
, errno
);
3150 // 'httpWait()' - Wait for data available on a connection.
3152 // @since CUPS 1.1.19@
3155 int // O - 1 if data is available, 0 otherwise
3156 httpWait(http_t
*http
, // I - HTTP connection
3157 int msec
) // I - Milliseconds to wait
3159 // First see if there is data in the buffer...
3160 DEBUG_printf("2httpWait(http=%p, msec=%d)", (void *)http
, msec
);
3167 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3171 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
> 0)
3173 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3177 // Flush pending data, if any...
3180 DEBUG_puts("3httpWait: Flushing write buffer.");
3182 if (httpFlushWrite(http
) < 0)
3186 // If not, check the TLS buffers and do a select() on the connection...
3187 return (_httpWait(http
, msec
, 1));
3192 // 'httpWrite()' - Write data to a HTTP connection.
3194 // This function is deprecated. Use the httpWrite2() function which can
3195 // write more than 2GB of data.
3197 // @deprecated@ @exclude all@
3200 int // O - Number of bytes written
3201 httpWrite(http_t
*http
, // I - HTTP connection
3202 const char *buffer
, // I - Buffer for data
3203 int length
) // I - Number of bytes to write
3205 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3210 // 'httpWrite2()' - Write data to a HTTP connection.
3215 ssize_t
// O - Number of bytes written
3216 httpWrite2(http_t
*http
, // I - HTTP connection
3217 const char *buffer
, // I - Buffer for data
3218 size_t length
) // I - Number of bytes to write
3220 ssize_t bytes
; // Bytes written
3223 DEBUG_printf("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
);
3225 // Range check input...
3226 if (!http
|| !buffer
)
3228 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3232 // Mark activity on the connection...
3233 http
->activity
= time(NULL
);
3235 // Buffer small writes for better performance...
3236 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3238 DEBUG_printf("1httpWrite2: http->coding=%d", http
->coding
);
3242 http_content_coding_finish(http
);
3247 size_t slen
; // Bytes to write
3248 ssize_t sret
; // Bytes written
3250 ((z_stream
*)http
->stream
)->next_in
= (Bytef
*)buffer
;
3251 ((z_stream
*)http
->stream
)->avail_in
= (uInt
)length
;
3253 while (deflate((z_stream
*)http
->stream
, Z_NO_FLUSH
) == Z_OK
)
3255 DEBUG_printf("1httpWrite2: avail_out=%d", ((z_stream
*)http
->stream
)->avail_out
);
3257 if (((z_stream
*)http
->stream
)->avail_out
> 0)
3260 slen
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3262 DEBUG_printf("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
);
3264 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3265 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3267 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3273 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3277 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3278 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3281 bytes
= (ssize_t
)length
;
3284 else if (length
> 0)
3286 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3288 DEBUG_printf("2httpWrite2: Flushing buffer (wused=%d, length=" CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
);
3290 httpFlushWrite(http
);
3293 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3295 // Write to buffer...
3296 DEBUG_printf("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...", CUPS_LLCAST length
);
3298 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3299 http
->wused
+= (int)length
;
3300 bytes
= (ssize_t
)length
;
3304 // Otherwise write the data directly...
3305 DEBUG_printf("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...", CUPS_LLCAST length
);
3307 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3308 bytes
= http_write_chunk(http
, buffer
, length
);
3310 bytes
= http_write(http
, buffer
, length
);
3312 DEBUG_printf("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...", CUPS_LLCAST bytes
);
3315 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3316 http
->data_remaining
-= bytes
;
3323 // Handle end-of-request processing...
3324 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) || (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3326 // Finished with the transfer; unless we are sending POST or PUT data, go idle...
3327 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3328 http_content_coding_finish(http
);
3332 if (httpFlushWrite(http
) < 0)
3336 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3338 // Send a 0-length chunk at the end of the request...
3339 http_write(http
, "0\r\n\r\n", 5);
3341 // Reset the data state...
3342 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3343 http
->data_remaining
= 0;
3346 if (http
->state
== HTTP_STATE_POST_RECV
)
3348 else if (http
->state
== HTTP_STATE_POST_SEND
|| http
->state
== HTTP_STATE_GET_SEND
)
3349 http
->state
= HTTP_STATE_WAITING
;
3351 http
->state
= HTTP_STATE_STATUS
;
3353 DEBUG_printf("2httpWrite2: Changed state to %s.", httpStateString(http
->state
));
3356 DEBUG_printf("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
);
3363 // 'httpWriteRequest()' - Send a HTTP request.
3368 bool // O - `true` on success, `false` on error
3369 httpWriteRequest(http_t
*http
, // I - HTTP connection
3370 const char *method
, // I - Method string ("GET", "POST", etc.)
3371 const char *uri
) // I - URI
3373 if (!strcasecmp(method
, "DELETE"))
3374 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
3375 else if (!strcasecmp(method
, "GET"))
3376 return (http_send(http
, HTTP_STATE_GET
, uri
));
3377 else if (!strcasecmp(method
, "HEAD"))
3378 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
3379 else if (!strcasecmp(method
, "OPTIONS"))
3380 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
3381 else if (!strcasecmp(method
, "POST"))
3382 return (http_send(http
, HTTP_STATE_POST
, uri
));
3383 else if (!strcasecmp(method
, "PUT"))
3384 return (http_send(http
, HTTP_STATE_PUT
, uri
));
3385 else if (!strcasecmp(method
, "TRACE"))
3386 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
3393 // 'httpWriteResponse()' - Write a HTTP response to a client connection.
3398 int // O - 0 on success, -1 on error
3399 httpWriteResponse(http_t
*http
, // I - HTTP connection
3400 http_status_t status
) // I - Status code
3402 http_encoding_t old_encoding
; // Old data_encoding value
3403 off_t old_remaining
; // Old data_remaining value
3404 cups_lang_t
*lang
; // Response language
3407 // Range check input...
3408 DEBUG_printf("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
);
3410 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3412 DEBUG_puts("1httpWriteResponse: Bad input.");
3416 // Set the various standard fields if they aren't already...
3417 if (!http
->fields
[HTTP_FIELD_DATE
])
3418 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3420 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3422 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3423 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3426 if (http
->version
== HTTP_VERSION_1_1
)
3428 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3430 if (http
->keep_alive
)
3431 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3433 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3436 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
])
3437 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3440 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
|| status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3442 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3443 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3445 if (!http
->fields
[HTTP_FIELD_UPGRADE
])
3446 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3448 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
])
3449 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3452 if (!http
->fields
[HTTP_FIELD_SERVER
])
3453 httpSetField(http
, HTTP_FIELD_SERVER
, http
->default_fields
[HTTP_FIELD_SERVER
] ? http
->default_fields
[HTTP_FIELD_SERVER
] : CUPS_MINIMAL
);
3455 // Set the Accept-Encoding field if it isn't already...
3456 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
3457 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] ? http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] : "gzip, deflate, identity");
3459 // Get the response language, if any...
3460 lang
= cupsLangGet(http
->fields
[HTTP_FIELD_CONTENT_LANGUAGE
]);
3462 // Send the response header...
3463 old_encoding
= http
->data_encoding
;
3464 old_remaining
= http
->data_remaining
;
3465 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3467 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100, http
->version
% 100, (int)status
, _httpStatusString(lang
, status
)) < 0)
3469 http
->status
= HTTP_STATUS_ERROR
;
3473 if (status
!= HTTP_STATUS_CONTINUE
)
3475 // 100 Continue doesn't have the rest of the response headers...
3476 int i
; // Looping var
3477 const char *value
; // Field value
3479 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3481 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3483 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3485 http
->status
= HTTP_STATUS_ERROR
;
3493 char *start
, // Start of cookie
3494 *ptr
; // Pointer into cookie
3496 for (start
= http
->cookie
; start
; start
= ptr
)
3498 if ((ptr
= strchr(start
, '\n')) != NULL
)
3501 if (strchr(start
, ';'))
3503 if (httpPrintf(http
, "Set-Cookie: %s\r\n", start
) < 1)
3505 http
->status
= HTTP_STATUS_ERROR
;
3511 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", start
, http
->tls
? " secure;" : "") < 1)
3513 http
->status
= HTTP_STATUS_ERROR
;
3524 // "Click-jacking" defense (STR #4492)...
3525 if (httpPrintf(http
, "X-Frame-Options: DENY\r\nContent-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3527 http
->status
= HTTP_STATUS_ERROR
;
3532 if (httpWrite2(http
, "\r\n", 2) < 2)
3534 http
->status
= HTTP_STATUS_ERROR
;
3538 if (httpFlushWrite(http
) < 0)
3540 http
->status
= HTTP_STATUS_ERROR
;
3544 if (status
== HTTP_STATUS_CONTINUE
|| status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3546 // Restore the old data_encoding and data_length values...
3547 http
->data_encoding
= old_encoding
;
3548 http
->data_remaining
= old_remaining
;
3550 if (old_remaining
<= INT_MAX
)
3551 http
->_data_remaining
= (int)old_remaining
;
3553 http
->_data_remaining
= INT_MAX
;
3555 else if (http
->state
== HTTP_STATE_OPTIONS
|| http
->state
== HTTP_STATE_HEAD
|| http
->state
== HTTP_STATE_PUT
|| http
->state
== HTTP_STATE_TRACE
|| http
->state
== HTTP_STATE_CONNECT
|| http
->state
== HTTP_STATE_STATUS
)
3557 DEBUG_printf("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, was %s.", httpStateString(http
->state
));
3558 http
->state
= HTTP_STATE_WAITING
;
3562 // Force data_encoding and data_length to be set according to the response headers...
3563 http_set_length(http
);
3565 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3567 DEBUG_printf("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, was %s.", httpStateString(http
->state
));
3568 http
->state
= HTTP_STATE_WAITING
;
3572 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3575 // Then start any content encoding...
3576 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3577 http_content_coding_start(http
, httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3585 // 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3589 http_add_field(http_t
*http
, // I - HTTP connection
3590 http_field_t field
, // I - HTTP field
3591 const char *value
, // I - Value string
3592 int append
) // I - Append value?
3594 char temp
[1024], // Temporary value string
3595 combined
[HTTP_MAX_VALUE
];
3596 // Combined value string
3597 size_t fieldlen
, // Length of existing value
3598 valuelen
, // Length of value string
3599 total
; // Total length of string
3602 if (field
== HTTP_FIELD_HOST
)
3604 // Special-case for Host: as we don't want a trailing "." on the hostname
3605 // and need to bracket IPv6 numeric addresses.
3606 char *ptr
= strchr(value
, ':');
3608 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3610 // Bracket IPv6 numeric addresses...
3612 // This is slightly inefficient (basically copying twice), but is an edge
3613 // case and not worth optimizing...
3614 snprintf(temp
, sizeof(temp
), "[%s]", value
);
3619 // Check for a trailing dot on the hostname...
3620 cupsCopyString(temp
, value
, sizeof(temp
));
3622 ptr
= temp
+ strlen(temp
) - 1;
3629 if (append
&& field
!= HTTP_FIELD_ACCEPT_ENCODING
&& field
!= HTTP_FIELD_ACCEPT_LANGUAGE
&& field
!= HTTP_FIELD_ACCEPT_RANGES
&& field
!= HTTP_FIELD_ALLOW
&& field
!= HTTP_FIELD_LINK
&& field
!= HTTP_FIELD_TRANSFER_ENCODING
&& field
!= HTTP_FIELD_UPGRADE
&& field
!= HTTP_FIELD_WWW_AUTHENTICATE
)
3632 if (!append
&& http
->fields
[field
])
3634 if (field
>= HTTP_FIELD_ACCEPT_ENCODING
|| http
->fields
[field
] != http
->_fields
[field
])
3635 free(http
->fields
[field
]);
3637 http
->fields
[field
] = NULL
;
3640 valuelen
= strlen(value
);
3644 if (field
< HTTP_FIELD_ACCEPT_ENCODING
)
3645 http
->_fields
[field
][0] = '\0';
3649 if (http
->fields
[field
])
3651 fieldlen
= strlen(http
->fields
[field
]);
3652 total
= fieldlen
+ 2 + valuelen
;
3660 if (total
< HTTP_MAX_VALUE
&& field
< HTTP_FIELD_ACCEPT_ENCODING
)
3662 // Copy short values to legacy char arrays (maintained for binary
3663 // compatibility with CUPS 1.2.x and earlier applications...)
3666 snprintf(combined
, sizeof(combined
), "%s, %s", http
->_fields
[field
], value
);
3670 cupsCopyString(http
->_fields
[field
], value
, sizeof(http
->_fields
[field
]));
3671 http
->fields
[field
] = http
->_fields
[field
];
3675 // Expand the field value...
3676 char *mcombined
; // New value string
3678 if (field
< HTTP_FIELD_ACCEPT_ENCODING
&& http
->fields
[field
] == http
->_fields
[field
])
3680 if ((mcombined
= malloc(total
+ 1)) != NULL
)
3682 http
->fields
[field
] = mcombined
;
3683 snprintf(mcombined
, total
+ 1, "%s, %s", http
->_fields
[field
], value
);
3686 else if ((mcombined
= realloc(http
->fields
[field
], total
+ 1)) != NULL
)
3688 http
->fields
[field
] = mcombined
;
3689 cupsConcatString(mcombined
, ", ", total
+ 1);
3690 cupsConcatString(mcombined
, value
, total
+ 1);
3695 // Allocate the field value...
3696 http
->fields
[field
] = strdup(value
);
3699 DEBUG_printf("1http_add_field: New value of %s is \"%s\"", http_fields
[field
], http
->fields
[field
]);
3701 if (field
== HTTP_FIELD_CONTENT_ENCODING
&& http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3703 DEBUG_puts("1http_add_field: Calling http_content_coding_start.");
3704 http_content_coding_start(http
, value
);
3710 // 'http_content_coding_finish()' - Finish doing any content encoding.
3714 http_content_coding_finish(
3715 http_t
*http
) // I - HTTP connection
3717 int zerr
; // Compression status
3718 Byte dummy
[1]; // Dummy read buffer
3719 size_t bytes
; // Number of bytes to write
3722 DEBUG_printf("http_content_coding_finish(http=%p)", (void *)http
);
3723 DEBUG_printf("1http_content_coding_finishing: http->coding=%d", http
->coding
);
3725 switch (http
->coding
)
3727 case _HTTP_CODING_DEFLATE
:
3728 case _HTTP_CODING_GZIP
:
3729 ((z_stream
*)http
->stream
)->next_in
= dummy
;
3730 ((z_stream
*)http
->stream
)->avail_in
= 0;
3734 zerr
= deflate((z_stream
*)http
->stream
, Z_FINISH
);
3735 bytes
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3739 DEBUG_printf("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
);
3741 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3742 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3744 http_write(http
, (char *)http
->sbuffer
, bytes
);
3747 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3748 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3750 while (zerr
== Z_OK
);
3752 deflateEnd((z_stream
*)http
->stream
);
3754 free(http
->sbuffer
);
3757 http
->sbuffer
= NULL
;
3758 http
->stream
= NULL
;
3761 httpFlushWrite(http
);
3764 case _HTTP_CODING_INFLATE
:
3765 case _HTTP_CODING_GUNZIP
:
3766 inflateEnd((z_stream
*)http
->stream
);
3768 free(http
->sbuffer
);
3771 http
->sbuffer
= NULL
;
3772 http
->stream
= NULL
;
3779 http
->coding
= _HTTP_CODING_IDENTITY
;
3784 // 'http_content_coding_start()' - Start doing content encoding.
3788 http_content_coding_start(
3789 http_t
*http
, // I - HTTP connection
3790 const char *value
) // I - Value of Content-Encoding
3792 int zerr
; // Error/status
3793 _http_coding_t coding
; // Content coding value
3796 DEBUG_printf("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
);
3798 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3800 DEBUG_printf("1http_content_coding_start: http->coding already %d.", http
->coding
);
3803 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3805 if (http
->state
== HTTP_STATE_GET_SEND
|| http
->state
== HTTP_STATE_POST_SEND
)
3807 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
: _HTTP_CODING_GUNZIP
;
3809 else if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_PUT_RECV
)
3811 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
: _HTTP_CODING_GUNZIP
;
3815 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3819 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3821 if (http
->state
== HTTP_STATE_GET_SEND
|| http
->state
== HTTP_STATE_POST_SEND
)
3823 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
: _HTTP_CODING_INFLATE
;
3825 else if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_PUT_RECV
)
3827 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
: _HTTP_CODING_INFLATE
;
3831 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3837 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3843 case _HTTP_CODING_DEFLATE
:
3844 case _HTTP_CODING_GZIP
:
3846 httpFlushWrite(http
);
3848 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3850 http
->status
= HTTP_STATUS_ERROR
;
3851 http
->error
= errno
;
3855 // Window size for compression is 11 bits - optimal based on PWG Raster
3856 // sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3858 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3860 free(http
->sbuffer
);
3862 http
->sbuffer
= NULL
;
3863 http
->status
= HTTP_STATUS_ERROR
;
3864 http
->error
= errno
;
3868 if ((zerr
= deflateInit2((z_stream
*)http
->stream
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7, Z_DEFAULT_STRATEGY
)) < Z_OK
)
3870 free(http
->sbuffer
);
3873 http
->sbuffer
= NULL
;
3874 http
->stream
= NULL
;
3875 http
->status
= HTTP_STATUS_ERROR
;
3876 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3880 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3881 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3884 case _HTTP_CODING_INFLATE
:
3885 case _HTTP_CODING_GUNZIP
:
3886 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3888 http
->status
= HTTP_STATUS_ERROR
;
3889 http
->error
= errno
;
3893 // Window size for decompression is up to 15 bits (maximum supported).
3894 // -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3895 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3897 free(http
->sbuffer
);
3899 http
->sbuffer
= NULL
;
3900 http
->status
= HTTP_STATUS_ERROR
;
3901 http
->error
= errno
;
3905 if ((zerr
= inflateInit2((z_stream
*)http
->stream
, coding
== _HTTP_CODING_INFLATE
? -15 : 31)) < Z_OK
)
3907 free(http
->sbuffer
);
3910 http
->sbuffer
= NULL
;
3911 http
->stream
= NULL
;
3912 http
->status
= HTTP_STATUS_ERROR
;
3913 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3917 ((z_stream
*)http
->stream
)->avail_in
= 0;
3918 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
3925 http
->coding
= coding
;
3927 DEBUG_printf("1http_content_coding_start: http->coding now %d.", http
->coding
);
3932 // 'http_create()' - Create an unconnected HTTP connection.
3935 static http_t
* // O - HTTP connection
3937 const char *host
, // I - Hostname
3938 int port
, // I - Port number
3939 http_addrlist_t
*addrlist
, // I - Address list or `NULL`
3940 int family
, // I - Address family or AF_UNSPEC
3941 http_encryption_t encryption
, // I - Encryption to use
3942 int blocking
, // I - 1 for blocking mode
3943 _http_mode_t mode
) // I - _HTTP_MODE_CLIENT or _SERVER
3945 http_t
*http
; // New HTTP connection
3946 char service
[255]; // Service name
3947 http_addrlist_t
*myaddrlist
= NULL
; // My address list
3948 _cups_globals_t
*cg
= _cupsGlobals(); // Thread global data
3951 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
);
3953 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3958 // Lookup the host...
3961 myaddrlist
= httpAddrCopyList(addrlist
);
3965 snprintf(service
, sizeof(service
), "%d", port
);
3967 myaddrlist
= httpAddrGetList(host
, family
, service
);
3973 // Allocate memory for the structure...
3974 if ((http
= calloc(1, sizeof(http_t
))) == NULL
)
3976 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3977 httpAddrFreeList(myaddrlist
);
3981 // Initialize the HTTP data...
3983 http
->activity
= time(NULL
);
3984 http
->addrlist
= myaddrlist
;
3985 http
->blocking
= blocking
;
3988 http
->gssctx
= GSS_C_NO_CONTEXT
;
3989 http
->gssname
= GSS_C_NO_NAME
;
3990 #endif // HAVE_GSSAPI
3991 http
->status
= HTTP_STATUS_CONTINUE
;
3992 http
->version
= HTTP_VERSION_1_1
;
3996 DEBUG_printf("5http_create: host=\"%s\"", host
);
3998 if (!strncmp(host
, "fe80::", 6))
4000 // IPv6 link local address, convert to IPvFuture format...
4001 char *zoneid
; // Pointer to zoneid separator
4003 snprintf(http
->hostname
, sizeof(http
->hostname
), "[v1.%s]", host
);
4004 if ((zoneid
= strchr(http
->hostname
, '%')) != NULL
)
4007 else if (isxdigit(host
[0]) && isxdigit(host
[1]) && isxdigit(host
[2]) && isxdigit(host
[3]) && host
[4] == ':')
4009 // IPv6 address, convert to URI format...
4010 snprintf(http
->hostname
, sizeof(http
->hostname
), "[%s]", host
);
4014 // Not an IPv6 numeric address...
4015 cupsCopyString(http
->hostname
, host
, sizeof(http
->hostname
));
4018 DEBUG_printf("5http_create: http->hostname=\"%s\"", http
->hostname
);
4021 if (port
== 443) // Always use encryption for https
4022 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
4024 http
->encryption
= encryption
;
4026 http_set_wait(http
);
4028 // Set client credentials...
4029 http
->tls_credentials
= _httpUseCredentials(cg
->credentials
);
4031 // Return the new structure...
4038 // 'http_debug_hex()' - Do a hex dump of a buffer.
4042 http_debug_hex(const char *prefix
, // I - Prefix for line
4043 const char *buffer
, // I - Buffer to dump
4044 int bytes
) // I - Bytes to dump
4046 int i
, j
, // Looping vars
4047 ch
; // Current character
4048 char line
[255], // Line buffer
4049 *start
, // Start of line after prefix
4050 *ptr
; // Pointer into line
4053 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4056 DEBUG_printf("9%s: %d bytes:", prefix
, bytes
);
4058 snprintf(line
, sizeof(line
), "9%s: ", prefix
);
4059 start
= line
+ strlen(line
);
4061 for (i
= 0; i
< bytes
; i
+= 16)
4063 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4064 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4068 memcpy(ptr
, " ", 3);
4073 memcpy(ptr
, " ", 3);
4076 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4078 ch
= buffer
[i
+ j
] & 255;
4080 if (ch
< ' ' || ch
>= 127)
4094 // 'http_read()' - Read a buffer from a HTTP connection.
4096 // This function does the low-level read from the socket, retrying and timing
4100 static ssize_t
// O - Number of bytes read or -1 on error
4101 http_read(http_t
*http
, // I - HTTP connection
4102 char *buffer
, // I - Buffer
4103 size_t length
) // I - Maximum bytes to read
4105 ssize_t bytes
; // Bytes read
4108 DEBUG_printf("7http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
);
4110 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4112 while (!httpWait(http
, http
->wait_value
))
4114 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4117 DEBUG_puts("8http_read: Timeout.");
4122 DEBUG_printf("8http_read: Reading %d bytes into buffer.", (int)length
);
4127 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4129 bytes
= recv(http
->fd
, buffer
, length
, 0);
4134 if (WSAGetLastError() != WSAEINTR
)
4136 http
->error
= WSAGetLastError();
4139 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4141 if (!http
->timeout_cb
||
4142 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4144 http
->error
= WSAEWOULDBLOCK
;
4149 DEBUG_printf("8http_read: %s", strerror(errno
));
4151 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4153 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4155 http
->error
= errno
;
4158 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4160 http
->error
= errno
;
4164 else if (errno
!= EINTR
)
4166 http
->error
= errno
;
4174 DEBUG_printf("8http_read: Read " CUPS_LLFMT
" bytes into buffer.", CUPS_LLCAST bytes
);
4178 http_debug_hex("http_read", buffer
, (int)bytes
);
4184 http
->error
= EPIPE
;
4193 // 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4195 // This function reads data from the HTTP buffer or from the socket, as needed.
4198 static ssize_t
// O - Number of bytes read or -1 on error
4199 http_read_buffered(http_t
*http
, // I - HTTP connection
4200 char *buffer
, // I - Buffer
4201 size_t length
) // I - Maximum bytes to read
4203 ssize_t bytes
; // Bytes read
4206 DEBUG_printf("7http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
);
4210 if (length
> (size_t)http
->used
)
4211 bytes
= (ssize_t
)http
->used
;
4213 bytes
= (ssize_t
)length
;
4215 DEBUG_printf("8http_read: Grabbing %d bytes from input buffer.", (int)bytes
);
4217 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4218 http
->used
-= (int)bytes
;
4221 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4225 bytes
= http_read(http
, buffer
, length
);
4233 // 'http_read_chunk()' - Read a chunk from a HTTP connection.
4235 // This function reads and validates the chunk length, then does a buffered read
4236 // returning the number of bytes placed in the buffer.
4239 static ssize_t
// O - Number of bytes read or -1 on error
4240 http_read_chunk(http_t
*http
, // I - HTTP connection
4241 char *buffer
, // I - Buffer
4242 size_t length
) // I - Maximum bytes to read
4244 DEBUG_printf("7http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
);
4246 if (http
->data_remaining
<= 0)
4248 char len
[32]; // Length string
4250 if (!httpGets2(http
, len
, sizeof(len
)))
4252 DEBUG_puts("8http_read_chunk: Could not get chunk length.");
4258 DEBUG_puts("8http_read_chunk: Blank chunk length, trying again...");
4259 if (!httpGets2(http
, len
, sizeof(len
)))
4261 DEBUG_puts("8http_read_chunk: Could not get chunk length.");
4266 http
->data_remaining
= strtoll(len
, NULL
, 16);
4268 if (http
->data_remaining
< 0)
4270 DEBUG_printf("8http_read_chunk: Negative chunk length \"%s\" (" CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
);
4274 DEBUG_printf("8http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
);
4276 if (http
->data_remaining
== 0)
4278 // 0-length chunk, grab trailing blank line...
4279 httpGets2(http
, len
, sizeof(len
));
4283 DEBUG_printf("8http_read_chunk: data_remaining=" CUPS_LLFMT
, CUPS_LLCAST http
->data_remaining
);
4285 if (http
->data_remaining
<= 0)
4287 else if (length
> (size_t)http
->data_remaining
)
4288 length
= (size_t)http
->data_remaining
;
4290 return (http_read_buffered(http
, buffer
, length
));
4295 // 'http_send()' - Send a request with all fields and the trailing blank line.
4298 static bool // O - `true` on success, `false` on error
4299 http_send(http_t
*http
, // I - HTTP connection
4300 http_state_t request
, // I - Request code
4301 const char *uri
) // I - URI
4303 int i
; // Looping var
4304 char buf
[1024]; // Encoded URI buffer
4305 const char *value
; // Field value
4306 static const char * const codes
[] = // Request code strings
4326 DEBUG_printf("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
);
4328 if (http
== NULL
|| uri
== NULL
)
4331 // Set the User-Agent field if it isn't already...
4332 if (!http
->fields
[HTTP_FIELD_USER_AGENT
])
4334 if (http
->default_fields
[HTTP_FIELD_USER_AGENT
])
4335 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_fields
[HTTP_FIELD_USER_AGENT
]);
4337 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsGetUserAgent());
4340 // Set the Accept-Encoding field if it isn't already...
4341 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
] && http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
])
4342 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
]);
4344 // Set the Authorization field if it isn't already...
4345 if (!http
->fields
[HTTP_FIELD_AUTHORIZATION
] && http
->authstring
)
4346 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
4348 // Encode the URI as needed...
4349 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4351 // See if we had an error the last time around; if so, reconnect...
4352 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
|| http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4354 DEBUG_printf("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d", http
->fd
, http
->status
, http
->tls_upgrade
);
4356 if (!httpConnectAgain(http
, 30000, NULL
))
4360 // Flush any written data that is pending...
4363 if (httpFlushWrite(http
) < 0)
4365 if (!httpConnectAgain(http
, 30000, NULL
))
4370 // Send the request header...
4371 http
->state
= request
;
4372 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4374 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4377 http
->status
= HTTP_STATUS_CONTINUE
;
4379 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4381 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4382 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4385 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4387 http
->status
= HTTP_STATUS_ERROR
;
4391 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4393 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4395 DEBUG_printf("5http_send: %s: %s", http_fields
[i
], value
);
4397 if (i
== HTTP_FIELD_HOST
)
4399 if (httpPrintf(http
, "Host: %s:%d\r\n", value
, httpAddrGetPort(http
->hostaddr
)) < 1)
4401 http
->status
= HTTP_STATUS_ERROR
;
4405 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4407 http
->status
= HTTP_STATUS_ERROR
;
4415 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4417 http
->status
= HTTP_STATUS_ERROR
;
4422 DEBUG_printf("5http_send: expect=%d, mode=%d, state=%d", http
->expect
, http
->mode
, http
->state
);
4424 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&& (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_PUT_RECV
))
4426 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4428 http
->status
= HTTP_STATUS_ERROR
;
4433 if (httpPrintf(http
, "\r\n") < 1)
4435 http
->status
= HTTP_STATUS_ERROR
;
4439 if (httpFlushWrite(http
) < 0)
4442 http_set_length(http
);
4443 httpClearFields(http
);
4445 // The Kerberos and AuthRef authentication strings can only be used once...
4446 if (http
->fields
[HTTP_FIELD_AUTHORIZATION
] && http
->authstring
&& (!strncmp(http
->authstring
, "Negotiate", 9) || !strncmp(http
->authstring
, "AuthRef", 7)))
4448 http
->_authstring
[0] = '\0';
4450 if (http
->authstring
!= http
->_authstring
)
4451 free(http
->authstring
);
4453 http
->authstring
= http
->_authstring
;
4461 // 'http_set_length()' - Set the data_encoding and data_remaining values.
4464 static off_t
// O - Remainder or -1 on error
4465 http_set_length(http_t
*http
) // I - Connection
4467 off_t remaining
; // Remainder
4470 DEBUG_printf("4http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
));
4472 if ((remaining
= httpGetLength2(http
)) >= 0)
4474 if (http
->mode
== _HTTP_MODE_SERVER
&& http
->state
!= HTTP_STATE_GET_SEND
&& http
->state
!= HTTP_STATE_PUT
&& http
->state
!= HTTP_STATE_POST
&& http
->state
!= HTTP_STATE_POST_SEND
)
4476 DEBUG_puts("5http_set_length: Not setting data_encoding/remaining.");
4480 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_TRANSFER_ENCODING
), "chunked"))
4482 DEBUG_puts("5http_set_length: Setting data_encoding to HTTP_ENCODING_CHUNKED.");
4483 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4487 DEBUG_puts("5http_set_length: Setting data_encoding to HTTP_ENCODING_LENGTH.");
4488 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4491 DEBUG_printf("5http_set_length: Setting data_remaining to " CUPS_LLFMT
".", CUPS_LLCAST remaining
);
4492 http
->data_remaining
= remaining
;
4494 if (remaining
<= INT_MAX
)
4495 http
->_data_remaining
= (int)remaining
;
4497 http
->_data_remaining
= INT_MAX
;
4505 // 'http_set_timeout()' - Set the socket timeout values.
4509 http_set_timeout(int fd
, // I - File descriptor
4510 double timeout
) // I - Timeout in seconds
4513 DWORD tv
= (DWORD
)(timeout
* 1000);
4514 // Timeout in milliseconds
4516 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4517 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4520 struct timeval tv
; // Timeout in secs and usecs
4522 tv
.tv_sec
= (int)timeout
;
4523 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4525 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4526 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4532 // 'http_set_wait()' - Set the default wait value for reads.
4536 http_set_wait(http_t
*http
) // I - HTTP connection
4540 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4542 if (http
->wait_value
<= 0)
4543 http
->wait_value
= 60000;
4547 http
->wait_value
= 10000;
4553 // 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4556 static bool // O - `true` on success, `false` on error
4557 http_tls_upgrade(http_t
*http
) // I - HTTP connection
4559 int ret
; // Return value
4560 http_t myhttp
; // Local copy of HTTP data
4563 DEBUG_printf("4http_tls_upgrade(%p)", (void *)http
);
4565 // Flush the connection to make sure any previous "Upgrade" message
4569 // Copy the HTTP data to a local variable so we can do the OPTIONS
4570 // request without interfering with the existing request data...
4571 memcpy(&myhttp
, http
, sizeof(myhttp
));
4573 // Send an OPTIONS request to the server, requiring TLS
4574 // encryption on the link...
4575 http
->tls_upgrade
= 1;
4576 memset(http
->fields
, 0, sizeof(http
->fields
));
4577 http
->expect
= (http_status_t
)0;
4579 if (http
->hostname
[0] == '/')
4580 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
4582 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
4584 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4585 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.3,TLS/1.2,TLS/1.1,TLS/1.0");
4587 if ((ret
= httpOptions(http
, "*")) == 0)
4589 // Wait for the secure connection...
4590 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4593 // Restore the HTTP request data...
4594 httpClearFields(http
);
4595 memcpy(http
->_fields
, myhttp
._fields
, sizeof(http
->_fields
));
4596 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4598 http
->data_encoding
= myhttp
.data_encoding
;
4599 http
->data_remaining
= myhttp
.data_remaining
;
4600 http
->_data_remaining
= myhttp
._data_remaining
;
4601 http
->expect
= myhttp
.expect
;
4602 http
->digest_tries
= myhttp
.digest_tries
;
4603 http
->tls_upgrade
= 0;
4605 // See if we actually went secure...
4608 // Server does not support HTTP upgrade...
4609 DEBUG_puts("5http_tls_upgrade: Server does not support HTTP upgrade!");
4611 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4612 httpAddrClose(NULL
, http
->fd
);
4626 // 'http_write()' - Write a buffer to a HTTP connection.
4629 static ssize_t
// O - Number of bytes written
4630 http_write(http_t
*http
, // I - HTTP connection
4631 const char *buffer
, // I - Buffer for data
4632 size_t length
) // I - Number of bytes to write
4634 ssize_t tbytes
, // Total bytes sent
4635 bytes
; // Bytes sent
4638 DEBUG_printf("7http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
);
4644 DEBUG_printf("8http_write: About to write %d bytes.", (int)length
);
4646 if (http
->timeout_value
> 0.0)
4648 struct pollfd pfd
; // Polled file descriptor
4649 int nfds
; // Result from select()/poll()
4654 pfd
.events
= POLLOUT
;
4658 nfds
= poll(&pfd
, 1, http
->wait_value
);
4661 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
));
4663 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4668 http
->error
= errno
;
4671 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4674 http
->error
= WSAEWOULDBLOCK
;
4676 http
->error
= EWOULDBLOCK
;
4685 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4687 bytes
= send(http
->fd
, buffer
, length
, 0);
4689 DEBUG_printf("8http_write: Write of " CUPS_LLFMT
" bytes returned " CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
);
4694 if (WSAGetLastError() == WSAEINTR
)
4698 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4700 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4703 http
->error
= WSAGetLastError();
4705 else if (WSAGetLastError() != http
->error
)
4707 http
->error
= WSAGetLastError();
4716 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4718 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4720 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4723 http
->error
= errno
;
4725 else if (errno
!= http
->error
)
4727 http
->error
= errno
;
4732 DEBUG_printf("8http_write: error writing data (%s).", strerror(http
->error
));
4739 length
-= (size_t)bytes
;
4743 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4746 DEBUG_printf("8http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
);
4753 // 'http_write_chunk()' - Write a chunked buffer.
4756 static ssize_t
// O - Number bytes written
4757 http_write_chunk(http_t
*http
, // I - HTTP connection
4758 const char *buffer
, // I - Buffer to write
4759 size_t length
) // I - Length of buffer
4761 char header
[16]; // Chunk header
4762 ssize_t bytes
; // Bytes written
4765 DEBUG_printf("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
);
4767 // Write the chunk header, data, and trailer.
4768 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4769 if (http_write(http
, header
, strlen(header
)) < 0)
4771 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4775 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4777 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4781 if (http_write(http
, "\r\n", 2) < 0)
4783 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");