2 * HTTP routines for CUPS.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Include necessary headers...
23 #include "cups-private.h"
30 # include <sys/time.h>
31 # include <sys/resource.h>
35 #endif /* HAVE_POLL */
38 # endif /* HAVE_LIBZ */
45 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
47 static void http_content_coding_finish(http_t
*http
);
48 static void http_content_coding_start(http_t
*http
,
50 #endif /* HAVE_LIBZ */
51 static http_t
*http_create(const char *host
, int port
,
52 http_addrlist_t
*addrlist
, int family
,
53 http_encryption_t encryption
,
54 int blocking
, _http_mode_t mode
);
56 static void http_debug_hex(const char *prefix
, const char *buffer
,
59 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
60 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
61 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
62 static int http_send(http_t
*http
, http_state_t request
,
64 static ssize_t
http_write(http_t
*http
, const char *buffer
,
66 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
68 static off_t
http_set_length(http_t
*http
);
69 static void http_set_timeout(int fd
, double timeout
);
70 static void http_set_wait(http_t
*http
);
73 static int http_tls_upgrade(http_t
*http
);
81 static const char * const http_fields
[] =
98 "If-Unmodified-since",
113 "Authentication-Info"
118 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
119 * specified listening socket.
121 * @since CUPS 1.7/macOS 10.9@
124 http_t
* /* O - HTTP connection or @code NULL@ */
125 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
126 int blocking
) /* I - 1 if the connection should be
127 blocking, 0 otherwise */
129 http_t
*http
; /* HTTP connection */
130 http_addrlist_t addrlist
; /* Dummy address list */
131 socklen_t addrlen
; /* Length of address */
132 int val
; /* Socket option value */
136 * Range check input...
143 * Create the client connection...
146 memset(&addrlist
, 0, sizeof(addrlist
));
148 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
149 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
150 _HTTP_MODE_SERVER
)) == NULL
)
154 * Accept the client and get the remote address...
157 addrlen
= sizeof(http_addr_t
);
159 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
162 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
168 http
->hostaddr
= &(http
->addrlist
->addr
);
170 if (httpAddrLocalhost(http
->hostaddr
))
171 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
173 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
177 * Disable SIGPIPE for this socket.
181 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
182 #endif /* SO_NOSIGPIPE */
185 * Using TCP_NODELAY improves responsiveness, especially on systems
186 * with a slow loopback interface. Since we write large buffers
187 * when sending print files and requests, there shouldn't be any
188 * performance penalty for this...
192 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
196 * Close this socket when starting another process...
199 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
200 #endif /* FD_CLOEXEC */
207 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
209 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
211 * @since CUPS 1.5/macOS 10.7@
214 int /* O - 0 on success, -1 on error */
216 cups_array_t
*credentials
, /* I - Credentials array */
217 const void *data
, /* I - PEM-encoded X.509 data */
218 size_t datalen
) /* I - Length of data */
220 http_credential_t
*credential
; /* Credential data */
223 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
225 credential
->datalen
= datalen
;
227 if ((credential
->data
= malloc(datalen
)) != NULL
)
229 memcpy(credential
->data
, data
, datalen
);
230 cupsArrayAdd(credentials
, credential
);
242 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
246 httpBlocking(http_t
*http
, /* I - HTTP connection */
247 int b
) /* I - 1 = blocking, 0 = non-blocking */
258 * 'httpCheck()' - Check to see if there is a pending response from the server.
261 int /* O - 0 = no data, 1 = data available */
262 httpCheck(http_t
*http
) /* I - HTTP connection */
264 return (httpWait(http
, 0));
269 * 'httpClearCookie()' - Clear the cookie value(s).
271 * @since CUPS 1.1.19/macOS 10.3@
275 httpClearCookie(http_t
*http
) /* I - HTTP connection */
289 * 'httpClearFields()' - Clear HTTP request fields.
293 httpClearFields(http_t
*http
) /* I - HTTP connection */
295 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
299 memset(http
->fields
, 0, sizeof(http
->fields
));
301 if (http
->mode
== _HTTP_MODE_CLIENT
)
303 if (http
->hostname
[0] == '/')
304 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
306 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
309 if (http
->field_authorization
)
311 free(http
->field_authorization
);
312 http
->field_authorization
= NULL
;
315 if (http
->accept_encoding
)
317 _cupsStrFree(http
->accept_encoding
);
318 http
->accept_encoding
= NULL
;
323 _cupsStrFree(http
->allow
);
329 _cupsStrFree(http
->server
);
333 if (http
->authentication_info
)
335 _cupsStrFree(http
->authentication_info
);
336 http
->authentication_info
= NULL
;
339 http
->expect
= (http_status_t
)0;
345 * 'httpClose()' - Close an HTTP connection.
349 httpClose(http_t
*http
) /* I - HTTP connection */
352 OM_uint32 minor_status
; /* Minor status code */
353 #endif /* HAVE_GSSAPI */
356 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
359 * Range check input...
366 * Close any open connection...
369 _httpDisconnect(http
);
372 * Free memory used...
375 httpAddrFreeList(http
->addrlist
);
381 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
382 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
384 if (http
->gssname
!= GSS_C_NO_NAME
)
385 gss_release_name(&minor_status
, &http
->gssname
);
386 #endif /* HAVE_GSSAPI */
388 #ifdef HAVE_AUTHORIZATION_H
390 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
391 #endif /* HAVE_AUTHORIZATION_H */
393 httpClearFields(http
);
395 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
396 free(http
->authstring
);
403 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
405 * @since CUPS 2.0/OS 10.10@
408 int /* O - 1 if they match, 0 if they do not */
409 httpCompareCredentials(
410 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
411 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
413 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
416 for (temp1
= (http_credential_t
*)cupsArrayFirst(cred1
), temp2
= (http_credential_t
*)cupsArrayFirst(cred2
); temp1
&& temp2
; temp1
= (http_credential_t
*)cupsArrayNext(cred1
), temp2
= (http_credential_t
*)cupsArrayNext(cred2
))
417 if (temp1
->datalen
!= temp2
->datalen
)
419 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
422 return (temp1
== temp2
);
427 * 'httpConnect()' - Connect to a HTTP server.
429 * This function is deprecated - use @link httpConnect2@ instead.
431 * @deprecated@ @exclude all@
434 http_t
* /* O - New HTTP connection */
435 httpConnect(const char *host
, /* I - Host to connect to */
436 int port
) /* I - Port number */
438 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
439 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
444 * 'httpConnect2()' - Connect to a HTTP server.
446 * @since CUPS 1.7/macOS 10.9@
449 http_t
* /* O - New HTTP connection */
451 const char *host
, /* I - Host to connect to */
452 int port
, /* I - Port number */
453 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
454 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
455 http_encryption_t encryption
, /* I - Type of encryption to use */
456 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
457 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
458 int *cancel
) /* I - Pointer to "cancel" variable */
460 http_t
*http
; /* New HTTP connection */
463 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
));
466 * Create the HTTP structure...
469 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
470 _HTTP_MODE_CLIENT
)) == NULL
)
474 * Optionally connect to the remote system...
477 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
481 * Could not connect to any known address - bail out!
491 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
493 * This function is now deprecated. Please use the @link httpConnect2@ function
496 * @deprecated@ @exclude all@
499 http_t
* /* O - New HTTP connection */
501 const char *host
, /* I - Host to connect to */
502 int port
, /* I - Port number */
503 http_encryption_t encryption
) /* I - Type of encryption to use */
505 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
506 host
, port
, encryption
));
508 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
514 * 'httpDelete()' - Send a DELETE request to the server.
517 int /* O - Status of call (0 = success) */
518 httpDelete(http_t
*http
, /* I - HTTP connection */
519 const char *uri
) /* I - URI to delete */
521 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
526 * '_httpDisconnect()' - Disconnect a HTTP connection.
530 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
535 #endif /* HAVE_SSL */
537 httpAddrClose(NULL
, http
->fd
);
544 * 'httpEncryption()' - Set the required encryption on the link.
547 int /* O - -1 on error, 0 on success */
548 httpEncryption(http_t
*http
, /* I - HTTP connection */
549 http_encryption_t e
) /* I - New encryption preference */
551 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
557 if (http
->mode
== _HTTP_MODE_CLIENT
)
559 http
->encryption
= e
;
561 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
562 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
563 return (httpReconnect2(http
, 30000, NULL
));
564 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
565 return (http_tls_upgrade(http
));
571 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
574 http
->encryption
= e
;
575 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
576 return (_httpTLSStart(http
));
581 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
585 #endif /* HAVE_SSL */
590 * 'httpError()' - Get the last error on a connection.
593 int /* O - Error code (errno) value */
594 httpError(http_t
*http
) /* I - HTTP connection */
597 return (http
->error
);
604 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
608 http_field_t
/* O - Field index */
609 httpFieldValue(const char *name
) /* I - String name */
611 int i
; /* Looping var */
614 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
615 if (!_cups_strcasecmp(name
, http_fields
[i
]))
616 return ((http_field_t
)i
);
618 return (HTTP_FIELD_UNKNOWN
);
623 * 'httpFlush()' - Flush data read from a HTTP connection.
627 httpFlush(http_t
*http
) /* I - HTTP connection */
629 char buffer
[8192]; /* Junk buffer */
630 int blocking
; /* To block or not to block */
631 http_state_t oldstate
; /* Old state */
634 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
637 * Nothing to do if we are in the "waiting" state...
640 if (http
->state
== HTTP_STATE_WAITING
)
644 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
647 blocking
= http
->blocking
;
651 * Read any data we can...
654 oldstate
= http
->state
;
655 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
658 * Restore blocking and reset the connection if we didn't get all of
659 * the remaining data...
662 http
->blocking
= blocking
;
664 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
668 * Didn't get the data back, so close the current connection.
673 http_content_coding_finish(http
);
674 #endif /* HAVE_LIBZ */
676 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
678 http
->state
= HTTP_STATE_WAITING
;
683 #endif /* HAVE_SSL */
685 httpAddrClose(NULL
, http
->fd
);
693 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
695 * @since CUPS 1.2/macOS 10.5@
698 int /* O - Bytes written or -1 on error */
699 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
701 ssize_t bytes
; /* Bytes written */
704 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
706 if (!http
|| !http
->wused
)
708 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
709 "1httpFlushWrite: No connection.");
713 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
714 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
716 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
720 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
727 * 'httpFreeCredentials()' - Free an array of credentials.
732 cups_array_t
*credentials
) /* I - Array of credentials */
734 http_credential_t
*credential
; /* Credential */
737 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
739 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
741 cupsArrayRemove(credentials
, credential
);
742 free((void *)credential
->data
);
746 cupsArrayDelete(credentials
);
751 * 'httpGet()' - Send a GET request to the server.
754 int /* O - Status of call (0 = success) */
755 httpGet(http_t
*http
, /* I - HTTP connection */
756 const char *uri
) /* I - URI to get */
758 return (http_send(http
, HTTP_STATE_GET
, uri
));
763 * 'httpGetActivity()' - Get the most recent activity for a connection.
765 * The return value is the time in seconds of the last read or write.
767 * @since CUPS 2.0/OS 10.10@
770 time_t /* O - Time of last read or write */
771 httpGetActivity(http_t
*http
) /* I - HTTP connection */
773 return (http
? http
->activity
: 0);
778 * 'httpGetAuthString()' - Get the current authorization string.
780 * The authorization string is set by @link cupsDoAuthentication@ and
781 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
782 * string to use with @link httpSetField@ for the
783 * @code HTTP_FIELD_AUTHORIZATION@ value.
785 * @since CUPS 1.3/macOS 10.5@
788 char * /* O - Authorization string */
789 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
792 return (http
->authstring
);
799 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
801 * @since CUPS 1.2/macOS 10.5@
804 int /* O - 1 if blocking, 0 if non-blocking */
805 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
807 return (http
? http
->blocking
: 0);
812 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
813 * the client and server.
815 * This function uses the value of the Accepts-Encoding HTTP header and must be
816 * called after receiving a response from the server or a request from the
817 * client. The value returned can be use in subsequent requests (for clients)
818 * or in the response (for servers) in order to compress the content stream.
820 * @since CUPS 1.7/macOS 10.9@
823 const char * /* O - Content-Coding value or
824 @code NULL@ for the identity
826 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
829 if (http
&& http
->accept_encoding
)
831 int i
; /* Looping var */
832 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
833 *start
, /* Start of coding value */
834 *end
; /* End of coding value */
835 double qvalue
; /* "qvalue" for coding */
836 struct lconv
*loc
= localeconv(); /* Locale data */
837 static const char * const codings
[] =
838 { /* Supported content codings */
845 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
847 for (start
= temp
; *start
; start
= end
)
850 * Find the end of the coding name...
855 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
861 * Grab the qvalue as needed...
864 if (!strncmp(end
, ";q=", 3))
865 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
868 * Skip past all attributes...
872 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
878 while (*end
&& isspace(*end
& 255))
882 * Check value if it matches something we support...
888 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
889 if (!strcmp(start
, codings
[i
]))
893 #endif /* HAVE_LIBZ */
900 * 'httpGetCookie()' - Get any cookie data from the response.
902 * @since CUPS 1.1.19/macOS 10.3@
905 const char * /* O - Cookie data or @code NULL@ */
906 httpGetCookie(http_t
*http
) /* I - HTTP connection */
908 return (http
? http
->cookie
: NULL
);
913 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
915 * This function returns the encryption mode for the connection. Use the
916 * @link httpIsEncrypted@ function to determine whether a TLS session has
919 * @since CUPS 2.0/OS 10.10@
922 http_encryption_t
/* O - Current encryption mode */
923 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
925 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
930 * 'httpGetExpect()' - Get the value of the Expect header, if any.
932 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
933 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
935 * @since CUPS 1.7/macOS 10.9@
938 http_status_t
/* O - Expect: status, if any */
939 httpGetExpect(http_t
*http
) /* I - HTTP connection */
942 return (HTTP_STATUS_ERROR
);
944 return (http
->expect
);
949 * 'httpGetFd()' - Get the file descriptor associated with a connection.
951 * @since CUPS 1.2/macOS 10.5@
954 int /* O - File descriptor or -1 if none */
955 httpGetFd(http_t
*http
) /* I - HTTP connection */
957 return (http
? http
->fd
: -1);
962 * 'httpGetField()' - Get a field value from a request/response.
965 const char * /* O - Field value */
966 httpGetField(http_t
*http
, /* I - HTTP connection */
967 http_field_t field
) /* I - Field to get */
969 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
974 case HTTP_FIELD_ACCEPT_ENCODING
:
975 return (http
->accept_encoding
);
977 case HTTP_FIELD_ALLOW
:
978 return (http
->allow
);
980 case HTTP_FIELD_SERVER
:
981 return (http
->server
);
983 case HTTP_FIELD_AUTHENTICATION_INFO
:
984 return (http
->authentication_info
);
986 case HTTP_FIELD_AUTHORIZATION
:
987 if (http
->field_authorization
)
990 * Special case for Authorization: as its contents can be
991 * longer than HTTP_MAX_VALUE...
994 return (http
->field_authorization
);
998 return (http
->fields
[field
]);
1004 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
1006 * @since CUPS 2.0/OS 10.10@
1009 http_keepalive_t
/* O - Keep-Alive state */
1010 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
1012 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1017 * 'httpGetLength()' - Get the amount of data remaining from the
1018 * content-length or transfer-encoding fields.
1020 * This function is deprecated and will not return lengths larger than
1021 * 2^31 - 1; use httpGetLength2() instead.
1023 * @deprecated@ @exclude all@
1026 int /* O - Content length */
1027 httpGetLength(http_t
*http
) /* I - HTTP connection */
1030 * Get the read content length and return the 32-bit value.
1035 httpGetLength2(http
);
1037 return (http
->_data_remaining
);
1045 * 'httpGetLength2()' - Get the amount of data remaining from the
1046 * content-length or transfer-encoding fields.
1048 * This function returns the complete content length, even for
1049 * content larger than 2^31 - 1.
1051 * @since CUPS 1.2/macOS 10.5@
1054 off_t
/* O - Content length */
1055 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1057 off_t remaining
; /* Remaining length */
1060 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1065 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1067 DEBUG_puts("4httpGetLength2: chunked request!");
1073 * The following is a hack for HTTP servers that don't send a
1074 * Content-Length or Transfer-Encoding field...
1076 * If there is no Content-Length then the connection must close
1077 * after the transfer is complete...
1080 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1083 * Default content length is 0 for errors and certain types of operations,
1084 * and 2^31-1 for other successful requests...
1087 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1088 http
->state
== HTTP_STATE_OPTIONS
||
1089 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1090 http
->state
== HTTP_STATE_HEAD
||
1091 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1092 http
->state
== HTTP_STATE_DELETE
||
1093 http
->state
== HTTP_STATE_TRACE
||
1094 http
->state
== HTTP_STATE_CONNECT
)
1097 remaining
= 2147483647;
1099 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1103 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1104 CUPS_LLCAST remaining
));
1112 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1114 * @since CUPS 2.0/OS 10.10@
1117 size_t /* O - Number of bytes buffered */
1118 httpGetPending(http_t
*http
) /* I - HTTP connection */
1120 return (http
? (size_t)http
->wused
: 0);
1125 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1127 * @since CUPS 2.0/OS 10.10@
1130 size_t /* O - Number of bytes available */
1131 httpGetReady(http_t
*http
) /* I - HTTP connection */
1135 else if (http
->used
> 0)
1136 return ((size_t)http
->used
);
1139 return (_httpTLSPending(http
));
1140 #endif /* HAVE_SSL */
1147 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1148 * body or current chunk.
1150 * The @link httpIsChunked@ function can be used to determine whether the
1151 * message body is chunked or fixed-length.
1153 * @since CUPS 2.0/OS 10.10@
1156 size_t /* O - Remaining bytes */
1157 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1159 return (http
? (size_t)http
->data_remaining
: 0);
1164 * 'httpGets()' - Get a line of text from a HTTP connection.
1167 char * /* O - Line or @code NULL@ */
1168 httpGets(char *line
, /* I - Line to read into */
1169 int length
, /* I - Max length of buffer */
1170 http_t
*http
) /* I - HTTP connection */
1172 char *lineptr
, /* Pointer into line */
1173 *lineend
, /* End of line */
1174 *bufptr
, /* Pointer into input buffer */
1175 *bufend
; /* Pointer to end of buffer */
1176 ssize_t bytes
; /* Number of bytes read */
1177 int eol
; /* End-of-line? */
1180 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1182 if (!http
|| !line
|| length
<= 1)
1186 * Read a line from the buffer...
1191 lineend
= line
+ length
- 1;
1194 while (lineptr
< lineend
)
1197 * Pre-load the buffer as needed...
1206 while (http
->used
== 0)
1209 * No newline; see if there is more data to be read...
1212 while (!_httpWait(http
, http
->wait_value
, 1))
1214 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1217 DEBUG_puts("3httpGets: Timed out!");
1219 http
->error
= WSAETIMEDOUT
;
1221 http
->error
= ETIMEDOUT
;
1226 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1228 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1233 * Nope, can't get a line this time...
1237 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1239 if (WSAGetLastError() == WSAEINTR
)
1241 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1243 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1246 http
->error
= WSAGetLastError();
1248 else if (WSAGetLastError() != http
->error
)
1250 http
->error
= WSAGetLastError();
1255 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1259 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1261 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1263 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1266 http
->error
= errno
;
1268 else if (errno
!= http
->error
)
1270 http
->error
= errno
;
1277 else if (bytes
== 0)
1279 http
->error
= EPIPE
;
1285 * Yup, update the amount used...
1288 http
->used
+= (int)bytes
;
1292 * Now copy as much of the current line as possible...
1295 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1296 lineptr
< lineend
&& bufptr
< bufend
;)
1298 if (*bufptr
== 0x0a)
1304 else if (*bufptr
== 0x0d)
1307 *lineptr
++ = *bufptr
++;
1310 http
->used
-= (int)(bufptr
- http
->buffer
);
1312 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1320 http
->activity
= time(NULL
);
1324 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1330 DEBUG_puts("3httpGets: No new line available!");
1337 * 'httpGetState()' - Get the current state of the HTTP request.
1340 http_state_t
/* O - HTTP state */
1341 httpGetState(http_t
*http
) /* I - HTTP connection */
1343 return (http
? http
->state
: HTTP_STATE_ERROR
);
1348 * 'httpGetStatus()' - Get the status of the last HTTP request.
1350 * @since CUPS 1.2/macOS 10.5@
1353 http_status_t
/* O - HTTP status */
1354 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1356 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1361 * 'httpGetSubField()' - Get a sub-field value.
1363 * @deprecated@ @exclude all@
1366 char * /* O - Value or @code NULL@ */
1367 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1368 http_field_t field
, /* I - Field index */
1369 const char *name
, /* I - Name of sub-field */
1370 char *value
) /* O - Value string */
1372 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1377 * 'httpGetSubField2()' - Get a sub-field value.
1379 * @since CUPS 1.2/macOS 10.5@
1382 char * /* O - Value or @code NULL@ */
1383 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1384 http_field_t field
, /* I - Field index */
1385 const char *name
, /* I - Name of sub-field */
1386 char *value
, /* O - Value string */
1387 int valuelen
) /* I - Size of value buffer */
1389 const char *fptr
; /* Pointer into field */
1390 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1391 *ptr
, /* Pointer into string buffer */
1392 *end
; /* End of value buffer */
1394 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1396 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1397 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1400 end
= value
+ valuelen
- 1;
1402 for (fptr
= http
->fields
[field
]; *fptr
;)
1405 * Skip leading whitespace...
1408 while (_cups_isspace(*fptr
))
1418 * Get the sub-field name...
1422 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1423 ptr
< (temp
+ sizeof(temp
) - 1);
1428 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1431 * Skip trailing chars up to the '='...
1434 while (_cups_isspace(*fptr
))
1444 * Skip = and leading whitespace...
1449 while (_cups_isspace(*fptr
))
1455 * Read quoted string...
1458 for (ptr
= value
, fptr
++;
1459 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1464 while (*fptr
&& *fptr
!= '\"')
1473 * Read unquoted string...
1477 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1482 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1486 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1489 * See if this is the one...
1492 if (!strcmp(name
, temp
))
1494 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1501 DEBUG_puts("3httpGetSubField2: Returning NULL");
1508 * 'httpGetVersion()' - Get the HTTP version at the other end.
1511 http_version_t
/* O - Version number */
1512 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1514 return (http
? http
->version
: HTTP_VERSION_1_0
);
1519 * 'httpHead()' - Send a HEAD request to the server.
1522 int /* O - Status of call (0 = success) */
1523 httpHead(http_t
*http
, /* I - HTTP connection */
1524 const char *uri
) /* I - URI for head */
1526 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1527 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1532 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1533 * default HTTP proxy (if any).
1537 httpInitialize(void)
1539 static int initialized
= 0; /* Have we been called before? */
1541 WSADATA winsockdata
; /* WinSock data */
1548 _cupsGlobalUnlock();
1553 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1555 #elif !defined(SO_NOSIGPIPE)
1557 * Ignore SIGPIPE signals...
1561 sigset(SIGPIPE
, SIG_IGN
);
1563 # elif defined(HAVE_SIGACTION)
1564 struct sigaction action
; /* POSIX sigaction data */
1567 memset(&action
, 0, sizeof(action
));
1568 action
.sa_handler
= SIG_IGN
;
1569 sigaction(SIGPIPE
, &action
, NULL
);
1572 signal(SIGPIPE
, SIG_IGN
);
1573 # endif /* !SO_NOSIGPIPE */
1577 _httpTLSInitialize();
1578 # endif /* HAVE_SSL */
1581 _cupsGlobalUnlock();
1586 * 'httpIsChunked()' - Report whether a message body is chunked.
1588 * This function returns non-zero if the message body is composed of
1589 * variable-length chunks.
1591 * @since CUPS 2.0/OS 10.10@
1594 int /* O - 1 if chunked, 0 if not */
1595 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1597 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1602 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1604 * This function returns non-zero if the connection is currently encrypted.
1606 * @since CUPS 2.0/OS 10.10@
1609 int /* O - 1 if encrypted, 0 if not */
1610 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1612 return (http
? http
->tls
!= NULL
: 0);
1617 * 'httpOptions()' - Send an OPTIONS request to the server.
1620 int /* O - Status of call (0 = success) */
1621 httpOptions(http_t
*http
, /* I - HTTP connection */
1622 const char *uri
) /* I - URI for options */
1624 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1629 * 'httpPeek()' - Peek at data from a HTTP connection.
1631 * This function copies available data from the given HTTP connection, reading
1632 * a buffer as needed. The data is still available for reading using
1635 * For non-blocking connections the usual timeouts apply.
1637 * @since CUPS 1.7/macOS 10.9@
1640 ssize_t
/* O - Number of bytes copied */
1641 httpPeek(http_t
*http
, /* I - HTTP connection */
1642 char *buffer
, /* I - Buffer for data */
1643 size_t length
) /* I - Maximum number of bytes */
1645 ssize_t bytes
; /* Bytes read */
1646 char len
[32]; /* Length string */
1649 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1651 if (http
== NULL
|| buffer
== NULL
)
1654 http
->activity
= time(NULL
);
1660 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1661 http
->data_remaining
<= 0)
1663 DEBUG_puts("2httpPeek: Getting chunk length...");
1665 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1667 DEBUG_puts("1httpPeek: Could not get length!");
1673 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1674 if (!httpGets(len
, sizeof(len
), http
))
1676 DEBUG_puts("1httpPeek: Could not get chunk length.");
1681 http
->data_remaining
= strtoll(len
, NULL
, 16);
1683 if (http
->data_remaining
< 0)
1685 DEBUG_puts("1httpPeek: Negative chunk length!");
1690 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1691 CUPS_LLCAST http
->data_remaining
));
1693 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1696 * A zero-length chunk ends a transfer; unless we are reading POST
1701 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1702 http_content_coding_finish(http
);
1703 #endif /* HAVE_LIBZ */
1705 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1706 httpGets(len
, sizeof(len
), http
);
1708 if (http
->state
== HTTP_STATE_POST_RECV
)
1711 http
->state
= HTTP_STATE_STATUS
;
1713 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1714 httpStateString(http
->state
)));
1717 * Prevent future reads for this request...
1720 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1724 else if (length
> (size_t)http
->data_remaining
)
1725 length
= (size_t)http
->data_remaining
;
1728 if (http
->used
== 0 &&
1729 (http
->coding
== _HTTP_CODING_IDENTITY
||
1730 (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)))
1732 if (http
->used
== 0)
1733 #endif /* HAVE_LIBZ */
1736 * Buffer small reads for better performance...
1739 ssize_t buflen
; /* Length of read for buffer */
1741 if (!http
->blocking
)
1743 while (!httpWait(http
, http
->wait_value
))
1745 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1752 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1753 buflen
= sizeof(http
->buffer
);
1755 buflen
= (ssize_t
)http
->data_remaining
;
1757 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1758 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1760 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1761 CUPS_LLCAST bytes
));
1765 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1768 http
->used
= (int)bytes
;
1773 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1775 # ifdef HAVE_INFLATECOPY
1776 int zerr
; /* Decompressor error */
1777 z_stream stream
; /* Copy of decompressor stream */
1779 if (http
->used
> 0 && ((z_stream
*)http
->stream
)->avail_in
< HTTP_MAX_BUFFER
)
1781 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- ((z_stream
*)http
->stream
)->avail_in
;
1782 /* Number of bytes to copy */
1784 if (((z_stream
*)http
->stream
)->avail_in
> 0 &&
1785 ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
1786 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
1788 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
1790 if (buflen
> (size_t)http
->data_remaining
)
1791 buflen
= (size_t)http
->data_remaining
;
1793 if (buflen
> (size_t)http
->used
)
1794 buflen
= (size_t)http
->used
;
1796 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1797 "decompression buffer.", (int)buflen
));
1799 memcpy(http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, http
->buffer
, buflen
);
1800 ((z_stream
*)http
->stream
)->avail_in
+= buflen
;
1801 http
->used
-= (int)buflen
;
1802 http
->data_remaining
-= (off_t
)buflen
;
1805 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1808 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1809 (int)((z_stream
*)http
->stream
)->avail_in
));
1811 if (inflateCopy(&stream
, (z_stream
*)http
->stream
) != Z_OK
)
1813 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1814 http
->error
= ENOMEM
;
1818 stream
.next_out
= (Bytef
*)buffer
;
1819 stream
.avail_out
= (uInt
)length
;
1821 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1822 inflateEnd(&stream
);
1826 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1828 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
1835 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
1838 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1839 "work with compressed streams.");
1841 # endif /* HAVE_INFLATECOPY */
1844 #endif /* HAVE_LIBZ */
1847 if (length
> (size_t)http
->used
)
1848 length
= (size_t)http
->used
;
1850 bytes
= (ssize_t
)length
;
1852 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1855 memcpy(buffer
, http
->buffer
, length
);
1863 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1866 http
->error
= WSAGetLastError();
1868 if (errno
== EINTR
|| errno
== EAGAIN
)
1871 http
->error
= errno
;
1874 else if (bytes
== 0)
1876 http
->error
= EPIPE
;
1885 * 'httpPost()' - Send a POST request to the server.
1888 int /* O - Status of call (0 = success) */
1889 httpPost(http_t
*http
, /* I - HTTP connection */
1890 const char *uri
) /* I - URI for post */
1892 return (http_send(http
, HTTP_STATE_POST
, uri
));
1897 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1902 int /* O - Number of bytes written */
1903 httpPrintf(http_t
*http
, /* I - HTTP connection */
1904 const char *format
, /* I - printf-style format string */
1905 ...) /* I - Additional args as needed */
1907 ssize_t bytes
; /* Number of bytes to write */
1908 char buf
[16384]; /* Buffer for formatted string */
1909 va_list ap
; /* Variable argument pointer */
1912 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1914 va_start(ap
, format
);
1915 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1918 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1920 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1921 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1926 DEBUG_puts("4httpPrintf: flushing existing data...");
1928 if (httpFlushWrite(http
) < 0)
1932 return ((int)http_write(http
, buf
, (size_t)bytes
));
1938 * 'httpPut()' - Send a PUT request to the server.
1941 int /* O - Status of call (0 = success) */
1942 httpPut(http_t
*http
, /* I - HTTP connection */
1943 const char *uri
) /* I - URI to put */
1945 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1946 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1951 * 'httpRead()' - Read data from a HTTP connection.
1953 * This function is deprecated. Use the httpRead2() function which can
1954 * read more than 2GB of data.
1956 * @deprecated@ @exclude all@
1959 int /* O - Number of bytes read */
1960 httpRead(http_t
*http
, /* I - HTTP connection */
1961 char *buffer
, /* I - Buffer for data */
1962 int length
) /* I - Maximum number of bytes */
1964 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1969 * 'httpRead2()' - Read data from a HTTP connection.
1971 * @since CUPS 1.2/macOS 10.5@
1974 ssize_t
/* O - Number of bytes read */
1975 httpRead2(http_t
*http
, /* I - HTTP connection */
1976 char *buffer
, /* I - Buffer for data */
1977 size_t length
) /* I - Maximum number of bytes */
1979 ssize_t bytes
; /* Bytes read */
1983 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT
, (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->coding
, http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1985 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
") data_encoding=%d data_remaining=" CUPS_LLFMT
, (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1986 #endif /* HAVE_LIBZ */
1988 if (http
== NULL
|| buffer
== NULL
)
1991 http
->activity
= time(NULL
);
1998 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2002 if (((z_stream
*)http
->stream
)->avail_in
> 0)
2004 int zerr
; /* Decompressor error */
2006 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
2007 (int)((z_stream
*)http
->stream
)->avail_in
, (int)length
));
2009 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)buffer
;
2010 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)length
;
2012 if ((zerr
= inflate((z_stream
*)http
->stream
, Z_SYNC_FLUSH
)) < Z_OK
)
2014 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2016 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
2023 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
2025 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2026 ((z_stream
*)http
->stream
)->avail_in
, ((z_stream
*)http
->stream
)->avail_out
,
2034 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)((z_stream
*)http
->stream
)->avail_in
;
2035 /* Additional bytes for buffer */
2039 if (((z_stream
*)http
->stream
)->avail_in
> 0 &&
2040 ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
2041 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
2043 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
2045 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2046 "decompression buffer.", (int)buflen
));
2048 if (http
->data_remaining
> 0)
2050 if (buflen
> http
->data_remaining
)
2051 buflen
= (ssize_t
)http
->data_remaining
;
2053 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2055 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2056 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2062 else if (bytes
== 0)
2065 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2066 "decompression buffer.", CUPS_LLCAST bytes
));
2068 http
->data_remaining
-= bytes
;
2069 ((z_stream
*)http
->stream
)->avail_in
+= (uInt
)bytes
;
2071 if (http
->data_remaining
<= 0 &&
2072 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2075 * Read the trailing blank line now...
2078 char len
[32]; /* Length string */
2080 httpGets(len
, sizeof(len
), http
);
2092 #endif /* HAVE_LIBZ */
2093 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2095 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2097 http
->data_remaining
-= bytes
;
2099 if (http
->data_remaining
<= 0)
2102 * Read the trailing blank line now...
2105 char len
[32]; /* Length string */
2107 httpGets(len
, sizeof(len
), http
);
2111 else if (http
->data_remaining
<= 0)
2114 * No more data to read...
2121 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2124 if (length
> (size_t)http
->data_remaining
)
2125 length
= (size_t)http
->data_remaining
;
2127 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2129 http
->data_remaining
-= bytes
;
2131 if (http
->data_remaining
<= 0 &&
2132 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2135 * Read the trailing blank line now...
2138 char len
[32]; /* Length string */
2140 httpGets(len
, sizeof(len
), http
);
2147 (http
->coding
== _HTTP_CODING_IDENTITY
||
2148 (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)) &&
2149 #endif /* HAVE_LIBZ */
2150 ((http
->data_remaining
<= 0 &&
2151 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2152 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2155 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2156 http_content_coding_finish(http
);
2157 #endif /* HAVE_LIBZ */
2159 if (http
->state
== HTTP_STATE_POST_RECV
)
2161 else if (http
->state
== HTTP_STATE_GET_SEND
||
2162 http
->state
== HTTP_STATE_POST_SEND
)
2163 http
->state
= HTTP_STATE_WAITING
;
2165 http
->state
= HTTP_STATE_STATUS
;
2167 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2168 httpStateString(http
->state
)));
2176 * 'httpReadRequest()' - Read a HTTP request from a connection.
2178 * @since CUPS 1.7/macOS 10.9@
2181 http_state_t
/* O - New state of connection */
2182 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2183 char *uri
, /* I - URI buffer */
2184 size_t urilen
) /* I - Size of URI buffer */
2186 char line
[4096], /* HTTP request line */
2187 *req_method
, /* HTTP request method */
2188 *req_uri
, /* HTTP request URI */
2189 *req_version
; /* HTTP request version number string */
2193 * Range check input...
2196 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2201 if (!http
|| !uri
|| urilen
< 1)
2203 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2204 return (HTTP_STATE_ERROR
);
2206 else if (http
->state
!= HTTP_STATE_WAITING
)
2208 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2209 httpStateString(http
->state
)));
2210 return (HTTP_STATE_ERROR
);
2217 httpClearFields(http
);
2219 http
->activity
= time(NULL
);
2220 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2221 http
->data_remaining
= 0;
2222 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2223 http
->status
= HTTP_STATUS_OK
;
2224 http
->version
= HTTP_VERSION_1_1
;
2227 * Read a line from the socket...
2230 if (!httpGets(line
, sizeof(line
), http
))
2232 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2233 return (HTTP_STATE_ERROR
);
2238 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2239 return (HTTP_STATE_WAITING
);
2242 DEBUG_printf(("1httpReadRequest: %s", line
));
2251 while (*req_uri
&& !isspace(*req_uri
& 255))
2256 DEBUG_puts("1httpReadRequest: No request URI.");
2257 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2258 return (HTTP_STATE_ERROR
);
2263 while (*req_uri
&& isspace(*req_uri
& 255))
2266 req_version
= req_uri
;
2268 while (*req_version
&& !isspace(*req_version
& 255))
2273 DEBUG_puts("1httpReadRequest: No request protocol version.");
2274 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2275 return (HTTP_STATE_ERROR
);
2278 *req_version
++ = '\0';
2280 while (*req_version
&& isspace(*req_version
& 255))
2287 if (!strcmp(req_method
, "OPTIONS"))
2288 http
->state
= HTTP_STATE_OPTIONS
;
2289 else if (!strcmp(req_method
, "GET"))
2290 http
->state
= HTTP_STATE_GET
;
2291 else if (!strcmp(req_method
, "HEAD"))
2292 http
->state
= HTTP_STATE_HEAD
;
2293 else if (!strcmp(req_method
, "POST"))
2294 http
->state
= HTTP_STATE_POST
;
2295 else if (!strcmp(req_method
, "PUT"))
2296 http
->state
= HTTP_STATE_PUT
;
2297 else if (!strcmp(req_method
, "DELETE"))
2298 http
->state
= HTTP_STATE_DELETE
;
2299 else if (!strcmp(req_method
, "TRACE"))
2300 http
->state
= HTTP_STATE_TRACE
;
2301 else if (!strcmp(req_method
, "CONNECT"))
2302 http
->state
= HTTP_STATE_CONNECT
;
2305 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2306 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2307 return (HTTP_STATE_UNKNOWN_METHOD
);
2310 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2311 httpStateString(http
->state
)));
2313 if (!strcmp(req_version
, "HTTP/1.0"))
2315 http
->version
= HTTP_VERSION_1_0
;
2316 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2318 else if (!strcmp(req_version
, "HTTP/1.1"))
2320 http
->version
= HTTP_VERSION_1_1
;
2321 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2325 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2326 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2327 return (HTTP_STATE_UNKNOWN_VERSION
);
2330 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2331 strlcpy(uri
, req_uri
, urilen
);
2333 return (http
->state
);
2338 * 'httpReconnect()' - Reconnect to a HTTP server.
2340 * This function is deprecated. Please use the @link httpReconnect2@ function
2343 * @deprecated@ @exclude all@
2346 int /* O - 0 on success, non-zero on failure */
2347 httpReconnect(http_t
*http
) /* I - HTTP connection */
2349 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2351 return (httpReconnect2(http
, 30000, NULL
));
2356 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2360 int /* O - 0 on success, non-zero on failure */
2361 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2362 int msec
, /* I - Timeout in milliseconds */
2363 int *cancel
) /* I - Pointer to "cancel" variable */
2365 http_addrlist_t
*addr
; /* Connected address */
2367 http_addrlist_t
*current
; /* Current address */
2368 char temp
[256]; /* Temporary address string */
2372 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2376 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2383 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2386 #endif /* HAVE_SSL */
2389 * Close any previously open socket...
2394 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2396 httpAddrClose(NULL
, http
->fd
);
2402 * Reset all state (except fields, which may be reused)...
2405 http
->state
= HTTP_STATE_WAITING
;
2406 http
->version
= HTTP_VERSION_1_1
;
2407 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2408 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2409 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2410 http
->_data_remaining
= 0;
2412 http
->data_remaining
= 0;
2413 http
->hostaddr
= NULL
;
2417 * Connect to the server...
2421 for (current
= http
->addrlist
; current
; current
= current
->next
)
2422 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2423 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2424 httpAddrPort(&(current
->addr
))));
2427 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2430 * Unable to connect...
2434 http
->error
= WSAGetLastError();
2436 http
->error
= errno
;
2438 http
->status
= HTTP_STATUS_ERROR
;
2440 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2441 strerror(http
->error
)));
2446 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2448 if (http
->timeout_value
> 0)
2449 http_set_timeout(http
->fd
, http
->timeout_value
);
2451 http
->hostaddr
= &(addr
->addr
);
2455 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2458 * Always do encryption via SSL.
2461 if (_httpTLSStart(http
) != 0)
2463 httpAddrClose(NULL
, http
->fd
);
2468 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2469 return (http_tls_upgrade(http
));
2470 #endif /* HAVE_SSL */
2472 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2473 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2474 httpAddrPort(http
->hostaddr
)));
2481 * 'httpSetAuthString()' - Set the current authorization string.
2483 * This function just stores a copy of the current authorization string in
2484 * the HTTP connection object. You must still call @link httpSetField@ to set
2485 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2486 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2489 * @since CUPS 1.3/macOS 10.5@
2493 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2494 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2495 const char *data
) /* I - Auth data (NULL for none) */
2498 * Range check input...
2504 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2505 free(http
->authstring
);
2507 http
->authstring
= http
->_authstring
;
2512 * Set the current authorization string...
2515 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2518 if (len
> sizeof(http
->_authstring
))
2520 if ((temp
= malloc(len
)) == NULL
)
2521 len
= sizeof(http
->_authstring
);
2523 http
->authstring
= temp
;
2527 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2529 strlcpy(http
->authstring
, scheme
, len
);
2534 * Clear the current authorization string...
2537 http
->_authstring
[0] = '\0';
2543 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2546 * @since CUPS 1.5/macOS 10.7@
2549 int /* O - Status of call (0 = success) */
2550 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2551 cups_array_t
*credentials
) /* I - Array of credentials */
2553 if (!http
|| cupsArrayCount(credentials
) < 1)
2557 _httpFreeCredentials(http
->tls_credentials
);
2559 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2560 #endif /* HAVE_SSL */
2562 return (http
->tls_credentials
? 0 : -1);
2567 * 'httpSetCookie()' - Set the cookie value(s).
2569 * @since CUPS 1.1.19/macOS 10.3@
2573 httpSetCookie(http_t
*http
, /* I - Connection */
2574 const char *cookie
) /* I - Cookie string */
2583 http
->cookie
= strdup(cookie
);
2585 http
->cookie
= NULL
;
2590 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2592 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2593 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2595 * @since CUPS 1.7/macOS 10.9@
2599 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2600 http_field_t field
, /* I - Field index */
2601 const char *value
)/* I - Value */
2603 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2610 case HTTP_FIELD_ACCEPT_ENCODING
:
2611 if (http
->default_accept_encoding
)
2612 _cupsStrFree(http
->default_accept_encoding
);
2614 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2617 case HTTP_FIELD_SERVER
:
2618 if (http
->default_server
)
2619 _cupsStrFree(http
->default_server
);
2621 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2624 case HTTP_FIELD_USER_AGENT
:
2625 if (http
->default_user_agent
)
2626 _cupsStrFree(http
->default_user_agent
);
2628 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2632 DEBUG_puts("1httpSetDefaultField: Ignored.");
2639 * 'httpSetExpect()' - Set the Expect: header in a request.
2641 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2644 * @since CUPS 1.2/macOS 10.5@
2648 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2649 http_status_t expect
) /* I - HTTP status to expect
2650 (@code HTTP_STATUS_CONTINUE@) */
2652 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2655 http
->expect
= expect
;
2660 * 'httpSetField()' - Set the value of an HTTP header.
2664 httpSetField(http_t
*http
, /* I - HTTP connection */
2665 http_field_t field
, /* I - Field index */
2666 const char *value
) /* I - Value */
2668 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2671 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2672 field
>= HTTP_FIELD_MAX
||
2676 http_add_field(http
, field
, value
, 0);
2681 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2683 * @since CUPS 2.0/OS 10.10@
2688 http_t
*http
, /* I - HTTP connection */
2689 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2692 http
->keep_alive
= keep_alive
;
2697 * 'httpSetLength()' - Set the content-length and content-encoding.
2699 * @since CUPS 1.2/macOS 10.5@
2703 httpSetLength(http_t
*http
, /* I - HTTP connection */
2704 size_t length
) /* I - Length (0 for chunked) */
2706 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2713 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2715 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2719 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2720 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2721 CUPS_LLFMT
, CUPS_LLCAST length
);
2727 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2729 * The optional timeout callback receives both the HTTP connection and a user
2730 * data pointer and must return 1 to continue or 0 to error (time) out.
2732 * @since CUPS 1.5/macOS 10.7@
2737 http_t
*http
, /* I - HTTP connection */
2738 double timeout
, /* I - Number of seconds for timeout,
2739 must be greater than 0 */
2740 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2741 void *user_data
) /* I - User data pointer */
2743 if (!http
|| timeout
<= 0.0)
2746 http
->timeout_cb
= cb
;
2747 http
->timeout_data
= user_data
;
2748 http
->timeout_value
= timeout
;
2751 http_set_timeout(http
->fd
, timeout
);
2753 http_set_wait(http
);
2758 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2760 * @since CUPS 2.0/OS 10.10@
2764 httpShutdown(http_t
*http
) /* I - HTTP connection */
2766 if (!http
|| http
->fd
< 0)
2772 #endif /* HAVE_SSL */
2775 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2777 shutdown(http
->fd
, SHUT_RD
);
2783 * 'httpTrace()' - Send an TRACE request to the server.
2788 int /* O - Status of call (0 = success) */
2789 httpTrace(http_t
*http
, /* I - HTTP connection */
2790 const char *uri
) /* I - URI for trace */
2792 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2797 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2799 * Note: Unlike httpUpdate(), this function does not flush pending write data
2800 * and only retrieves a single status line from the HTTP connection.
2803 int /* O - 1 to continue, 0 to stop */
2804 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2805 http_status_t
*status
) /* O - Current HTTP status */
2807 char line
[32768], /* Line from connection... */
2808 *value
; /* Pointer to value on line */
2809 http_field_t field
; /* Field index */
2810 int major
, minor
; /* HTTP version numbers */
2813 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2816 * Grab a single line from the connection...
2819 if (!httpGets(line
, sizeof(line
), http
))
2821 *status
= HTTP_STATUS_ERROR
;
2825 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2827 if (line
[0] == '\0')
2830 * Blank line means the start of the data section (if any). Return
2831 * the result code, too...
2833 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2834 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2838 if (http
->status
== HTTP_STATUS_CONTINUE
)
2840 *status
= http
->status
;
2844 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2845 http
->digest_tries
= 0;
2848 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2850 if (_httpTLSStart(http
) != 0)
2852 httpAddrClose(NULL
, http
->fd
);
2854 *status
= http
->status
= HTTP_STATUS_ERROR
;
2858 *status
= HTTP_STATUS_CONTINUE
;
2861 #endif /* HAVE_SSL */
2863 if (http_set_length(http
) < 0)
2865 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2866 http
->error
= EINVAL
;
2867 http
->status
= *status
= HTTP_STATUS_ERROR
;
2871 switch (http
->state
)
2873 case HTTP_STATE_GET
:
2874 case HTTP_STATE_POST
:
2875 case HTTP_STATE_POST_RECV
:
2876 case HTTP_STATE_PUT
:
2879 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2880 httpStateString(http
->state
)));
2882 case HTTP_STATE_POST_SEND
:
2883 case HTTP_STATE_HEAD
:
2887 http
->state
= HTTP_STATE_WAITING
;
2889 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2894 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2895 http_content_coding_start(http
,
2896 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2897 #endif /* HAVE_LIBZ */
2899 *status
= http
->status
;
2902 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2905 * Got the beginning of a response...
2908 int intstatus
; /* Status value as an integer */
2910 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2912 *status
= http
->status
= HTTP_STATUS_ERROR
;
2916 httpClearFields(http
);
2918 http
->version
= (http_version_t
)(major
* 100 + minor
);
2919 *status
= http
->status
= (http_status_t
)intstatus
;
2921 else if ((value
= strchr(line
, ':')) != NULL
)
2928 while (_cups_isspace(*value
))
2931 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
2934 * Be tolerants of servers that send unknown attribute fields...
2937 if (!_cups_strcasecmp(line
, "expect"))
2940 * "Expect: 100-continue" or similar...
2943 http
->expect
= (http_status_t
)atoi(value
);
2945 else if (!_cups_strcasecmp(line
, "cookie"))
2948 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2951 httpSetCookie(http
, value
);
2953 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
2955 http_add_field(http
, field
, value
, 1);
2957 if (field
== HTTP_FIELD_AUTHENTICATION_INFO
)
2958 httpGetSubField2(http
, HTTP_FIELD_AUTHENTICATION_INFO
, "nextnonce", http
->nextnonce
, (int)sizeof(http
->nextnonce
));
2962 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
2967 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
2968 http
->error
= EINVAL
;
2969 http
->status
= *status
= HTTP_STATUS_ERROR
;
2978 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2981 http_status_t
/* O - HTTP status */
2982 httpUpdate(http_t
*http
) /* I - HTTP connection */
2984 http_status_t status
; /* Request status */
2987 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
2990 * Flush pending data, if any...
2995 DEBUG_puts("2httpUpdate: flushing buffer...");
2997 if (httpFlushWrite(http
) < 0)
2998 return (HTTP_STATUS_ERROR
);
3002 * If we haven't issued any commands, then there is nothing to "update"...
3005 if (http
->state
== HTTP_STATE_WAITING
)
3006 return (HTTP_STATUS_CONTINUE
);
3009 * Grab all of the lines we can from the connection...
3012 while (_httpUpdate(http
, &status
));
3015 * See if there was an error...
3018 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3020 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3021 return (http
->status
);
3026 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3027 strerror(http
->error
)));
3028 http
->status
= HTTP_STATUS_ERROR
;
3029 return (HTTP_STATUS_ERROR
);
3033 * Return the current status...
3041 * '_httpWait()' - Wait for data available on a connection (no flush).
3044 int /* O - 1 if data is available, 0 otherwise */
3045 _httpWait(http_t
*http
, /* I - HTTP connection */
3046 int msec
, /* I - Milliseconds to wait */
3047 int usessl
) /* I - Use SSL context? */
3050 struct pollfd pfd
; /* Polled file descriptor */
3052 fd_set input_set
; /* select() input set */
3053 struct timeval timeout
; /* Timeout */
3054 #endif /* HAVE_POLL */
3055 int nfds
; /* Result from select()/poll() */
3058 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3062 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3067 * Check the SSL/TLS buffers for data first...
3071 if (http
->tls
&& _httpTLSPending(http
))
3073 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3076 #endif /* HAVE_SSL */
3079 * Then try doing a select() or poll() to poll the socket...
3084 pfd
.events
= POLLIN
;
3088 nfds
= poll(&pfd
, 1, msec
);
3090 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3095 FD_ZERO(&input_set
);
3096 FD_SET(http
->fd
, &input_set
);
3098 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3102 timeout
.tv_sec
= msec
/ 1000;
3103 timeout
.tv_usec
= (msec
% 1000) * 1000;
3105 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3108 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3110 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3113 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3114 WSAGetLastError() == WSAEWOULDBLOCK
));
3116 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3117 # endif /* _WIN32 */
3118 #endif /* HAVE_POLL */
3120 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3128 * 'httpWait()' - Wait for data available on a connection.
3130 * @since CUPS 1.1.19/macOS 10.3@
3133 int /* O - 1 if data is available, 0 otherwise */
3134 httpWait(http_t
*http
, /* I - HTTP connection */
3135 int msec
) /* I - Milliseconds to wait */
3138 * First see if there is data in the buffer...
3141 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3148 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3153 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
> 0)
3155 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3158 #endif /* HAVE_LIBZ */
3161 * Flush pending data, if any...
3166 DEBUG_puts("3httpWait: Flushing write buffer.");
3168 if (httpFlushWrite(http
) < 0)
3173 * If not, check the SSL/TLS buffers and do a select() on the connection...
3176 return (_httpWait(http
, msec
, 1));
3181 * 'httpWrite()' - Write data to a HTTP connection.
3183 * This function is deprecated. Use the httpWrite2() function which can
3184 * write more than 2GB of data.
3186 * @deprecated@ @exclude all@
3189 int /* O - Number of bytes written */
3190 httpWrite(http_t
*http
, /* I - HTTP connection */
3191 const char *buffer
, /* I - Buffer for data */
3192 int length
) /* I - Number of bytes to write */
3194 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3199 * 'httpWrite2()' - Write data to a HTTP connection.
3201 * @since CUPS 1.2/macOS 10.5@
3204 ssize_t
/* O - Number of bytes written */
3205 httpWrite2(http_t
*http
, /* I - HTTP connection */
3206 const char *buffer
, /* I - Buffer for data */
3207 size_t length
) /* I - Number of bytes to write */
3209 ssize_t bytes
; /* Bytes written */
3212 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3215 * Range check input...
3218 if (!http
|| !buffer
)
3220 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3225 * Mark activity on the connection...
3228 http
->activity
= time(NULL
);
3231 * Buffer small writes for better performance...
3235 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3237 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3241 http_content_coding_finish(http
);
3246 size_t slen
; /* Bytes to write */
3247 ssize_t sret
; /* Bytes written */
3249 ((z_stream
*)http
->stream
)->next_in
= (Bytef
*)buffer
;
3250 ((z_stream
*)http
->stream
)->avail_in
= (uInt
)length
;
3252 while (deflate((z_stream
*)http
->stream
, Z_NO_FLUSH
) == Z_OK
)
3254 DEBUG_printf(("1httpWrite2: avail_out=%d", ((z_stream
*)http
->stream
)->avail_out
));
3256 if (((z_stream
*)http
->stream
)->avail_out
> 0)
3259 slen
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3261 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3263 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3264 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3266 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3272 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3276 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3277 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3280 bytes
= (ssize_t
)length
;
3284 #endif /* HAVE_LIBZ */
3287 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3289 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3290 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3292 httpFlushWrite(http
);
3295 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3298 * Write to buffer...
3301 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3302 CUPS_LLCAST length
));
3304 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3305 http
->wused
+= (int)length
;
3306 bytes
= (ssize_t
)length
;
3311 * Otherwise write the data directly...
3314 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3315 CUPS_LLCAST length
));
3317 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3318 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3320 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3322 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3323 CUPS_LLCAST bytes
));
3326 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3327 http
->data_remaining
-= bytes
;
3333 * Handle end-of-request processing...
3336 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3337 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3340 * Finished with the transfer; unless we are sending POST or PUT
3345 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3346 http_content_coding_finish(http
);
3347 #endif /* HAVE_LIBZ */
3351 if (httpFlushWrite(http
) < 0)
3355 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3358 * Send a 0-length chunk at the end of the request...
3361 http_write(http
, "0\r\n\r\n", 5);
3364 * Reset the data state...
3367 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3368 http
->data_remaining
= 0;
3371 if (http
->state
== HTTP_STATE_POST_RECV
)
3373 else if (http
->state
== HTTP_STATE_POST_SEND
||
3374 http
->state
== HTTP_STATE_GET_SEND
)
3375 http
->state
= HTTP_STATE_WAITING
;
3377 http
->state
= HTTP_STATE_STATUS
;
3379 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3380 httpStateString(http
->state
)));
3383 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3390 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3392 * @since CUPS 1.7/macOS 10.9@
3395 int /* O - 0 on success, -1 on error */
3396 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3397 http_status_t status
) /* I - Status code */
3399 http_encoding_t old_encoding
; /* Old data_encoding value */
3400 off_t old_remaining
; /* Old data_remaining value */
3404 * Range check input...
3407 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3409 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3411 DEBUG_puts("1httpWriteResponse: Bad input.");
3416 * Set the various standard fields if they aren't already...
3419 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3420 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3422 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3424 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3425 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3428 if (http
->version
== HTTP_VERSION_1_1
)
3430 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3432 if (http
->keep_alive
)
3433 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3435 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3438 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3439 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3443 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3444 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3446 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3447 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3449 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3450 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3452 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3453 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3455 #endif /* HAVE_SSL */
3458 httpSetField(http
, HTTP_FIELD_SERVER
,
3459 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3462 * Set the Accept-Encoding field if it isn't already...
3465 if (!http
->accept_encoding
)
3466 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3467 http
->default_accept_encoding
? http
->default_accept_encoding
:
3469 "gzip, deflate, identity");
3472 #endif /* HAVE_LIBZ */
3475 * Send the response header...
3478 old_encoding
= http
->data_encoding
;
3479 old_remaining
= http
->data_remaining
;
3480 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3482 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3483 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3485 http
->status
= HTTP_STATUS_ERROR
;
3489 if (status
!= HTTP_STATUS_CONTINUE
)
3492 * 100 Continue doesn't have the rest of the response headers...
3495 int i
; /* Looping var */
3496 const char *value
; /* Field value */
3498 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3500 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3502 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3504 http
->status
= HTTP_STATUS_ERROR
;
3512 if (strchr(http
->cookie
, ';'))
3514 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3516 http
->status
= HTTP_STATUS_ERROR
;
3520 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3522 http
->status
= HTTP_STATUS_ERROR
;
3528 * "Click-jacking" defense (STR #4492)...
3531 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3532 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3534 http
->status
= HTTP_STATUS_ERROR
;
3539 if (httpWrite2(http
, "\r\n", 2) < 2)
3541 http
->status
= HTTP_STATUS_ERROR
;
3545 if (httpFlushWrite(http
) < 0)
3547 http
->status
= HTTP_STATUS_ERROR
;
3551 if (status
== HTTP_STATUS_CONTINUE
||
3552 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3555 * Restore the old data_encoding and data_length values...
3558 http
->data_encoding
= old_encoding
;
3559 http
->data_remaining
= old_remaining
;
3561 if (old_remaining
<= INT_MAX
)
3562 http
->_data_remaining
= (int)old_remaining
;
3564 http
->_data_remaining
= INT_MAX
;
3566 else if (http
->state
== HTTP_STATE_OPTIONS
||
3567 http
->state
== HTTP_STATE_HEAD
||
3568 http
->state
== HTTP_STATE_PUT
||
3569 http
->state
== HTTP_STATE_TRACE
||
3570 http
->state
== HTTP_STATE_CONNECT
||
3571 http
->state
== HTTP_STATE_STATUS
)
3573 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3574 "was %s.", httpStateString(http
->state
)));
3575 http
->state
= HTTP_STATE_WAITING
;
3580 * Force data_encoding and data_length to be set according to the response
3584 http_set_length(http
);
3586 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3588 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3589 "was %s.", httpStateString(http
->state
)));
3590 http
->state
= HTTP_STATE_WAITING
;
3594 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3599 * Then start any content encoding...
3602 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3603 http_content_coding_start(http
,
3604 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3605 #endif /* HAVE_LIBZ */
3614 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3618 http_add_field(http_t
*http
, /* I - HTTP connection */
3619 http_field_t field
, /* I - HTTP field */
3620 const char *value
, /* I - Value string */
3621 int append
) /* I - Append value? */
3623 char newvalue
[1024]; /* New value string */
3624 const char *oldvalue
; /* Old field value */
3629 * Optionally append the new value to the existing one...
3632 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
)
3635 if (field
== HTTP_FIELD_HOST
)
3638 * Special-case for Host: as we don't want a trailing "." on the hostname and
3639 * need to bracket IPv6 numeric addresses.
3642 char *ptr
= strchr(value
, ':');
3644 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3647 * Bracket IPv6 numeric addresses...
3650 snprintf(newvalue
, sizeof(newvalue
), "[%s]", value
);
3653 else if (*value
&& value
[strlen(value
) - 1] == '.')
3656 * Strip the trailing dot on the hostname...
3659 strlcpy(newvalue
, value
, sizeof(newvalue
));
3660 newvalue
[strlen(newvalue
) - 1] = '\0';
3664 else if (append
&& *value
&& (oldvalue
= httpGetField(http
, field
)) != NULL
&& *oldvalue
)
3666 snprintf(newvalue
, sizeof(newvalue
), "%s, %s", oldvalue
, value
);
3671 * Save the new value...
3676 case HTTP_FIELD_ACCEPT_ENCODING
:
3677 if (http
->accept_encoding
)
3678 _cupsStrFree(http
->accept_encoding
);
3680 http
->accept_encoding
= _cupsStrAlloc(value
);
3683 case HTTP_FIELD_ALLOW
:
3685 _cupsStrFree(http
->allow
);
3687 http
->allow
= _cupsStrAlloc(value
);
3690 case HTTP_FIELD_SERVER
:
3692 _cupsStrFree(http
->server
);
3694 http
->server
= _cupsStrAlloc(value
);
3697 case HTTP_FIELD_AUTHENTICATION_INFO
:
3698 if (http
->authentication_info
)
3699 _cupsStrFree(http
->authentication_info
);
3701 http
->authentication_info
= _cupsStrAlloc(value
);
3705 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
3709 if (field
== HTTP_FIELD_AUTHORIZATION
)
3712 * Special case for Authorization: as its contents can be
3713 * longer than HTTP_MAX_VALUE
3716 if (http
->field_authorization
)
3717 free(http
->field_authorization
);
3719 http
->field_authorization
= strdup(value
);
3722 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
3723 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3725 DEBUG_puts("1http_add_field: Calling http_content_coding_start.");
3726 http_content_coding_start(http
, value
);
3728 #endif /* HAVE_LIBZ */
3734 * 'http_content_coding_finish()' - Finish doing any content encoding.
3738 http_content_coding_finish(
3739 http_t
*http
) /* I - HTTP connection */
3741 int zerr
; /* Compression status */
3742 Byte dummy
[1]; /* Dummy read buffer */
3743 size_t bytes
; /* Number of bytes to write */
3746 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3747 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3749 switch (http
->coding
)
3751 case _HTTP_CODING_DEFLATE
:
3752 case _HTTP_CODING_GZIP
:
3753 ((z_stream
*)http
->stream
)->next_in
= dummy
;
3754 ((z_stream
*)http
->stream
)->avail_in
= 0;
3758 zerr
= deflate((z_stream
*)http
->stream
, Z_FINISH
);
3759 bytes
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3763 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3765 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3766 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3768 http_write(http
, (char *)http
->sbuffer
, bytes
);
3771 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3772 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3774 while (zerr
== Z_OK
);
3776 deflateEnd((z_stream
*)http
->stream
);
3778 free(http
->sbuffer
);
3781 http
->sbuffer
= NULL
;
3782 http
->stream
= NULL
;
3785 httpFlushWrite(http
);
3788 case _HTTP_CODING_INFLATE
:
3789 case _HTTP_CODING_GUNZIP
:
3790 inflateEnd((z_stream
*)http
->stream
);
3792 free(http
->sbuffer
);
3795 http
->sbuffer
= NULL
;
3796 http
->stream
= NULL
;
3803 http
->coding
= _HTTP_CODING_IDENTITY
;
3808 * 'http_content_coding_start()' - Start doing content encoding.
3812 http_content_coding_start(
3813 http_t
*http
, /* I - HTTP connection */
3814 const char *value
) /* I - Value of Content-Encoding */
3816 int zerr
; /* Error/status */
3817 _http_coding_t coding
; /* Content coding value */
3820 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3822 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3824 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3828 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3830 if (http
->state
== HTTP_STATE_GET_SEND
||
3831 http
->state
== HTTP_STATE_POST_SEND
)
3832 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3833 _HTTP_CODING_GUNZIP
;
3834 else if (http
->state
== HTTP_STATE_POST_RECV
||
3835 http
->state
== HTTP_STATE_PUT_RECV
)
3836 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3837 _HTTP_CODING_GUNZIP
;
3840 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3844 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3846 if (http
->state
== HTTP_STATE_GET_SEND
||
3847 http
->state
== HTTP_STATE_POST_SEND
)
3848 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3849 _HTTP_CODING_INFLATE
;
3850 else if (http
->state
== HTTP_STATE_POST_RECV
||
3851 http
->state
== HTTP_STATE_PUT_RECV
)
3852 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3853 _HTTP_CODING_INFLATE
;
3856 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3862 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3868 case _HTTP_CODING_DEFLATE
:
3869 case _HTTP_CODING_GZIP
:
3871 httpFlushWrite(http
);
3873 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3875 http
->status
= HTTP_STATUS_ERROR
;
3876 http
->error
= errno
;
3881 * Window size for compression is 11 bits - optimal based on PWG Raster
3882 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3886 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3888 free(http
->sbuffer
);
3890 http
->sbuffer
= NULL
;
3891 http
->status
= HTTP_STATUS_ERROR
;
3892 http
->error
= errno
;
3896 if ((zerr
= deflateInit2((z_stream
*)http
->stream
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7, Z_DEFAULT_STRATEGY
)) < Z_OK
)
3898 free(http
->sbuffer
);
3901 http
->sbuffer
= NULL
;
3902 http
->stream
= NULL
;
3903 http
->status
= HTTP_STATUS_ERROR
;
3904 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3908 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3909 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3912 case _HTTP_CODING_INFLATE
:
3913 case _HTTP_CODING_GUNZIP
:
3914 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3916 http
->status
= HTTP_STATUS_ERROR
;
3917 http
->error
= errno
;
3922 * Window size for decompression is up to 15 bits (maximum supported).
3923 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3926 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3928 free(http
->sbuffer
);
3930 http
->sbuffer
= NULL
;
3931 http
->status
= HTTP_STATUS_ERROR
;
3932 http
->error
= errno
;
3936 if ((zerr
= inflateInit2((z_stream
*)http
->stream
, coding
== _HTTP_CODING_INFLATE
? -15 : 31)) < Z_OK
)
3938 free(http
->sbuffer
);
3941 http
->sbuffer
= NULL
;
3942 http
->stream
= NULL
;
3943 http
->status
= HTTP_STATUS_ERROR
;
3944 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3948 ((z_stream
*)http
->stream
)->avail_in
= 0;
3949 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
3956 http
->coding
= coding
;
3958 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3961 #endif /* HAVE_LIBZ */
3965 * 'http_create()' - Create an unconnected HTTP connection.
3968 static http_t
* /* O - HTTP connection */
3970 const char *host
, /* I - Hostname */
3971 int port
, /* I - Port number */
3972 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3973 int family
, /* I - Address family or AF_UNSPEC */
3974 http_encryption_t encryption
, /* I - Encryption to use */
3975 int blocking
, /* I - 1 for blocking mode */
3976 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3978 http_t
*http
; /* New HTTP connection */
3979 char service
[255]; /* Service name */
3980 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3983 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
));
3985 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3991 * Lookup the host...
3996 myaddrlist
= httpAddrCopyList(addrlist
);
4000 snprintf(service
, sizeof(service
), "%d", port
);
4002 myaddrlist
= httpAddrGetList(host
, family
, service
);
4009 * Allocate memory for the structure...
4012 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
4014 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
4015 httpAddrFreeList(myaddrlist
);
4020 * Initialize the HTTP data...
4024 http
->activity
= time(NULL
);
4025 http
->addrlist
= myaddrlist
;
4026 http
->blocking
= blocking
;
4029 http
->gssctx
= GSS_C_NO_CONTEXT
;
4030 http
->gssname
= GSS_C_NO_NAME
;
4031 #endif /* HAVE_GSSAPI */
4032 http
->status
= HTTP_STATUS_CONTINUE
;
4033 http
->version
= HTTP_VERSION_1_1
;
4036 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
4038 if (port
== 443) /* Always use encryption for https */
4039 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
4041 http
->encryption
= encryption
;
4043 http_set_wait(http
);
4046 * Return the new structure...
4055 * 'http_debug_hex()' - Do a hex dump of a buffer.
4059 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4060 const char *buffer
, /* I - Buffer to dump */
4061 int bytes
) /* I - Bytes to dump */
4063 int i
, j
, /* Looping vars */
4064 ch
; /* Current character */
4065 char line
[255], /* Line buffer */
4066 *start
, /* Start of line after prefix */
4067 *ptr
; /* Pointer into line */
4070 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4073 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4075 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4076 start
= line
+ strlen(line
);
4078 for (i
= 0; i
< bytes
; i
+= 16)
4080 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4081 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4085 memcpy(ptr
, " ", 3);
4090 memcpy(ptr
, " ", 3);
4093 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4095 ch
= buffer
[i
+ j
] & 255;
4097 if (ch
< ' ' || ch
>= 127)
4111 * 'http_read()' - Read a buffer from a HTTP connection.
4113 * This function does the low-level read from the socket, retrying and timing
4117 static ssize_t
/* O - Number of bytes read or -1 on error */
4118 http_read(http_t
*http
, /* I - HTTP connection */
4119 char *buffer
, /* I - Buffer */
4120 size_t length
) /* I - Maximum bytes to read */
4122 ssize_t bytes
; /* Bytes read */
4125 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4127 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4129 while (!httpWait(http
, http
->wait_value
))
4131 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4134 DEBUG_puts("2http_read: Timeout.");
4139 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4145 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4147 #endif /* HAVE_SSL */
4148 bytes
= recv(http
->fd
, buffer
, length
, 0);
4153 if (WSAGetLastError() != WSAEINTR
)
4155 http
->error
= WSAGetLastError();
4158 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4160 if (!http
->timeout_cb
||
4161 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4163 http
->error
= WSAEWOULDBLOCK
;
4168 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4170 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4172 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4174 http
->error
= errno
;
4177 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4179 http
->error
= errno
;
4183 else if (errno
!= EINTR
)
4185 http
->error
= errno
;
4193 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4194 CUPS_LLCAST bytes
));
4197 http_debug_hex("http_read", buffer
, (int)bytes
);
4203 if (WSAGetLastError() == WSAEINTR
)
4206 http
->error
= WSAGetLastError();
4208 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4211 http
->error
= errno
;
4214 else if (bytes
== 0)
4216 http
->error
= EPIPE
;
4225 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4227 * This function reads data from the HTTP buffer or from the socket, as needed.
4230 static ssize_t
/* O - Number of bytes read or -1 on error */
4231 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4232 char *buffer
, /* I - Buffer */
4233 size_t length
) /* I - Maximum bytes to read */
4235 ssize_t bytes
; /* Bytes read */
4238 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4242 if (length
> (size_t)http
->used
)
4243 bytes
= (ssize_t
)http
->used
;
4245 bytes
= (ssize_t
)length
;
4247 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4250 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4251 http
->used
-= (int)bytes
;
4254 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4257 bytes
= http_read(http
, buffer
, length
);
4264 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4266 * This function reads and validates the chunk length, then does a buffered read
4267 * returning the number of bytes placed in the buffer.
4270 static ssize_t
/* O - Number of bytes read or -1 on error */
4271 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4272 char *buffer
, /* I - Buffer */
4273 size_t length
) /* I - Maximum bytes to read */
4275 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4277 if (http
->data_remaining
<= 0)
4279 char len
[32]; /* Length string */
4281 if (!httpGets(len
, sizeof(len
), http
))
4283 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4289 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4290 if (!httpGets(len
, sizeof(len
), http
))
4292 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4297 http
->data_remaining
= strtoll(len
, NULL
, 16);
4299 if (http
->data_remaining
< 0)
4301 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4302 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4306 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4307 len
, CUPS_LLCAST http
->data_remaining
));
4309 if (http
->data_remaining
== 0)
4312 * 0-length chunk, grab trailing blank line...
4315 httpGets(len
, sizeof(len
), http
);
4319 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4320 CUPS_LLCAST http
->data_remaining
));
4322 if (http
->data_remaining
<= 0)
4324 else if (length
> (size_t)http
->data_remaining
)
4325 length
= (size_t)http
->data_remaining
;
4327 return (http_read_buffered(http
, buffer
, length
));
4332 * 'http_send()' - Send a request with all fields and the trailing blank line.
4335 static int /* O - 0 on success, non-zero on error */
4336 http_send(http_t
*http
, /* I - HTTP connection */
4337 http_state_t request
, /* I - Request code */
4338 const char *uri
) /* I - URI */
4340 int i
; /* Looping var */
4341 char buf
[1024]; /* Encoded URI buffer */
4342 const char *value
; /* Field value */
4343 static const char * const codes
[] = /* Request code strings */
4363 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4365 if (http
== NULL
|| uri
== NULL
)
4369 * Set the User-Agent field if it isn't already...
4372 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4374 if (http
->default_user_agent
)
4375 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4377 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4381 * Set the Accept-Encoding field if it isn't already...
4384 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4385 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4386 http
->default_accept_encoding
);
4389 * Encode the URI as needed...
4392 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4395 * See if we had an error the last time around; if so, reconnect...
4398 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4399 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4401 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4402 http
->fd
, http
->status
, http
->tls_upgrade
));
4404 if (httpReconnect2(http
, 30000, NULL
))
4409 * Flush any written data that is pending...
4414 if (httpFlushWrite(http
) < 0)
4415 if (httpReconnect2(http
, 30000, NULL
))
4420 * Send the request header...
4423 http
->state
= request
;
4424 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4426 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4429 http
->status
= HTTP_STATUS_CONTINUE
;
4432 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4434 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4435 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4437 #endif /* HAVE_SSL */
4439 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4441 http
->status
= HTTP_STATUS_ERROR
;
4445 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4446 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4448 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4450 if (i
== HTTP_FIELD_HOST
)
4452 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4453 httpAddrPort(http
->hostaddr
)) < 1)
4455 http
->status
= HTTP_STATUS_ERROR
;
4459 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4461 http
->status
= HTTP_STATUS_ERROR
;
4467 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4469 http
->status
= HTTP_STATUS_ERROR
;
4473 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4474 http
->mode
, http
->state
));
4476 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4477 (http
->state
== HTTP_STATE_POST_RECV
||
4478 http
->state
== HTTP_STATE_PUT_RECV
))
4479 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4481 http
->status
= HTTP_STATUS_ERROR
;
4485 if (httpPrintf(http
, "\r\n") < 1)
4487 http
->status
= HTTP_STATUS_ERROR
;
4491 if (httpFlushWrite(http
) < 0)
4494 http_set_length(http
);
4495 httpClearFields(http
);
4498 * The Kerberos and AuthRef authentication strings can only be used once...
4501 if (http
->field_authorization
&& http
->authstring
&&
4502 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4503 !strncmp(http
->authstring
, "AuthRef", 7)))
4505 http
->_authstring
[0] = '\0';
4507 if (http
->authstring
!= http
->_authstring
)
4508 free(http
->authstring
);
4510 http
->authstring
= http
->_authstring
;
4518 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4521 static off_t
/* O - Remainder or -1 on error */
4522 http_set_length(http_t
*http
) /* I - Connection */
4524 off_t remaining
; /* Remainder */
4527 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4529 if ((remaining
= httpGetLength2(http
)) >= 0)
4531 if (http
->mode
== _HTTP_MODE_SERVER
&&
4532 http
->state
!= HTTP_STATE_GET_SEND
&&
4533 http
->state
!= HTTP_STATE_PUT
&&
4534 http
->state
!= HTTP_STATE_POST
&&
4535 http
->state
!= HTTP_STATE_POST_SEND
)
4537 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4541 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4544 DEBUG_puts("1http_set_length: Setting data_encoding to "
4545 "HTTP_ENCODING_CHUNKED.");
4546 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4550 DEBUG_puts("1http_set_length: Setting data_encoding to "
4551 "HTTP_ENCODING_LENGTH.");
4552 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4555 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4556 CUPS_LLCAST remaining
));
4557 http
->data_remaining
= remaining
;
4559 if (remaining
<= INT_MAX
)
4560 http
->_data_remaining
= (int)remaining
;
4562 http
->_data_remaining
= INT_MAX
;
4569 * 'http_set_timeout()' - Set the socket timeout values.
4573 http_set_timeout(int fd
, /* I - File descriptor */
4574 double timeout
) /* I - Timeout in seconds */
4577 DWORD tv
= (DWORD
)(timeout
* 1000);
4578 /* Timeout in milliseconds */
4580 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4581 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4584 struct timeval tv
; /* Timeout in secs and usecs */
4586 tv
.tv_sec
= (int)timeout
;
4587 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4589 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4590 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4596 * 'http_set_wait()' - Set the default wait value for reads.
4600 http_set_wait(http_t
*http
) /* I - HTTP connection */
4604 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4606 if (http
->wait_value
<= 0)
4607 http
->wait_value
= 60000;
4610 http
->wait_value
= 10000;
4616 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4619 static int /* O - Status of connection */
4620 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4622 int ret
; /* Return value */
4623 http_t myhttp
; /* Local copy of HTTP data */
4626 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4629 * Flush the connection to make sure any previous "Upgrade" message
4636 * Copy the HTTP data to a local variable so we can do the OPTIONS
4637 * request without interfering with the existing request data...
4640 memcpy(&myhttp
, http
, sizeof(myhttp
));
4643 * Send an OPTIONS request to the server, requiring SSL or TLS
4644 * encryption on the link...
4647 http
->tls_upgrade
= 1;
4648 http
->field_authorization
= NULL
; /* Don't free the auth string */
4650 httpClearFields(http
);
4651 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4652 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4654 if ((ret
= httpOptions(http
, "*")) == 0)
4657 * Wait for the secure connection...
4660 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4664 * Restore the HTTP request data...
4667 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4668 http
->data_encoding
= myhttp
.data_encoding
;
4669 http
->data_remaining
= myhttp
.data_remaining
;
4670 http
->_data_remaining
= myhttp
._data_remaining
;
4671 http
->expect
= myhttp
.expect
;
4672 http
->field_authorization
= myhttp
.field_authorization
;
4673 http
->digest_tries
= myhttp
.digest_tries
;
4674 http
->tls_upgrade
= 0;
4677 * See if we actually went secure...
4683 * Server does not support HTTP upgrade...
4686 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4688 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4689 httpAddrClose(NULL
, http
->fd
);
4698 #endif /* HAVE_SSL */
4702 * 'http_write()' - Write a buffer to a HTTP connection.
4705 static ssize_t
/* O - Number of bytes written */
4706 http_write(http_t
*http
, /* I - HTTP connection */
4707 const char *buffer
, /* I - Buffer for data */
4708 size_t length
) /* I - Number of bytes to write */
4710 ssize_t tbytes
, /* Total bytes sent */
4711 bytes
; /* Bytes sent */
4714 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4720 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4722 if (http
->timeout_value
> 0.0)
4725 struct pollfd pfd
; /* Polled file descriptor */
4727 fd_set output_set
; /* Output ready for write? */
4728 struct timeval timeout
; /* Timeout value */
4729 #endif /* HAVE_POLL */
4730 int nfds
; /* Result from select()/poll() */
4736 pfd
.events
= POLLOUT
;
4738 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4739 (errno
== EINTR
|| errno
== EAGAIN
))
4745 FD_ZERO(&output_set
);
4746 FD_SET(http
->fd
, &output_set
);
4748 timeout
.tv_sec
= http
->wait_value
/ 1000;
4749 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4751 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4754 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4755 WSAGetLastError() == WSAEWOULDBLOCK
));
4757 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4758 # endif /* _WIN32 */
4759 #endif /* HAVE_POLL */
4763 http
->error
= errno
;
4766 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4769 http
->error
= WSAEWOULDBLOCK
;
4771 http
->error
= EWOULDBLOCK
;
4781 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4783 #endif /* HAVE_SSL */
4784 bytes
= send(http
->fd
, buffer
, length
, 0);
4786 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4787 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4792 if (WSAGetLastError() == WSAEINTR
)
4794 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4796 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4799 http
->error
= WSAGetLastError();
4801 else if (WSAGetLastError() != http
->error
&&
4802 WSAGetLastError() != WSAECONNRESET
)
4804 http
->error
= WSAGetLastError();
4811 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4813 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4815 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4818 http
->error
= errno
;
4820 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4822 http
->error
= errno
;
4827 DEBUG_printf(("3http_write: error writing data (%s).",
4828 strerror(http
->error
)));
4835 length
-= (size_t)bytes
;
4839 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4842 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4849 * 'http_write_chunk()' - Write a chunked buffer.
4852 static ssize_t
/* O - Number bytes written */
4853 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4854 const char *buffer
, /* I - Buffer to write */
4855 size_t length
) /* I - Length of buffer */
4857 char header
[16]; /* Chunk header */
4858 ssize_t bytes
; /* Bytes written */
4861 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4864 * Write the chunk header, data, and trailer.
4867 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4868 if (http_write(http
, header
, strlen(header
)) < 0)
4870 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4874 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4876 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4880 if (http_write(http
, "\r\n", 2) < 0)
4882 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");