2 * HTTP routines for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Include necessary headers...
23 #include "cups-private.h"
30 # include <sys/time.h>
31 # include <sys/resource.h>
35 #endif /* HAVE_POLL */
43 static void http_content_coding_finish(http_t
*http
);
44 static void http_content_coding_start(http_t
*http
,
46 #endif /* HAVE_LIBZ */
47 static http_t
*http_create(const char *host
, int port
,
48 http_addrlist_t
*addrlist
, int family
,
49 http_encryption_t encryption
,
50 int blocking
, _http_mode_t mode
);
52 static void http_debug_hex(const char *prefix
, const char *buffer
,
55 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
56 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
57 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
58 static int http_send(http_t
*http
, http_state_t request
,
60 static ssize_t
http_write(http_t
*http
, const char *buffer
,
62 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
64 static off_t
http_set_length(http_t
*http
);
65 static void http_set_timeout(int fd
, double timeout
);
66 static void http_set_wait(http_t
*http
);
69 static int http_tls_upgrade(http_t
*http
);
77 static const char * const http_fields
[] =
94 "If-Unmodified-since",
113 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
114 * specified listening socket.
116 * @since CUPS 1.7/macOS 10.9@
119 http_t
* /* O - HTTP connection or @code NULL@ */
120 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
121 int blocking
) /* I - 1 if the connection should be
122 blocking, 0 otherwise */
124 http_t
*http
; /* HTTP connection */
125 http_addrlist_t addrlist
; /* Dummy address list */
126 socklen_t addrlen
; /* Length of address */
127 int val
; /* Socket option value */
131 * Range check input...
138 * Create the client connection...
141 memset(&addrlist
, 0, sizeof(addrlist
));
143 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
144 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
145 _HTTP_MODE_SERVER
)) == NULL
)
149 * Accept the client and get the remote address...
152 addrlen
= sizeof(http_addr_t
);
154 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
157 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
163 http
->hostaddr
= &(http
->addrlist
->addr
);
165 if (httpAddrLocalhost(http
->hostaddr
))
166 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
168 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
172 * Disable SIGPIPE for this socket.
176 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
177 #endif /* SO_NOSIGPIPE */
180 * Using TCP_NODELAY improves responsiveness, especially on systems
181 * with a slow loopback interface. Since we write large buffers
182 * when sending print files and requests, there shouldn't be any
183 * performance penalty for this...
187 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
191 * Close this socket when starting another process...
194 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
195 #endif /* FD_CLOEXEC */
202 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
204 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
206 * @since CUPS 1.5/macOS 10.7@
209 int /* O - 0 on success, -1 on error */
211 cups_array_t
*credentials
, /* I - Credentials array */
212 const void *data
, /* I - PEM-encoded X.509 data */
213 size_t datalen
) /* I - Length of data */
215 http_credential_t
*credential
; /* Credential data */
218 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
220 credential
->datalen
= datalen
;
222 if ((credential
->data
= malloc(datalen
)) != NULL
)
224 memcpy(credential
->data
, data
, datalen
);
225 cupsArrayAdd(credentials
, credential
);
237 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
241 httpBlocking(http_t
*http
, /* I - HTTP connection */
242 int b
) /* I - 1 = blocking, 0 = non-blocking */
253 * 'httpCheck()' - Check to see if there is a pending response from the server.
256 int /* O - 0 = no data, 1 = data available */
257 httpCheck(http_t
*http
) /* I - HTTP connection */
259 return (httpWait(http
, 0));
264 * 'httpClearCookie()' - Clear the cookie value(s).
266 * @since CUPS 1.1.19/macOS 10.3@
270 httpClearCookie(http_t
*http
) /* I - HTTP connection */
284 * 'httpClearFields()' - Clear HTTP request fields.
288 httpClearFields(http_t
*http
) /* I - HTTP connection */
290 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
294 memset(http
->fields
, 0, sizeof(http
->fields
));
296 if (http
->mode
== _HTTP_MODE_CLIENT
)
298 if (http
->hostname
[0] == '/')
299 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
301 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
304 if (http
->field_authorization
)
306 free(http
->field_authorization
);
307 http
->field_authorization
= NULL
;
310 if (http
->accept_encoding
)
312 _cupsStrFree(http
->accept_encoding
);
313 http
->accept_encoding
= NULL
;
318 _cupsStrFree(http
->allow
);
324 _cupsStrFree(http
->server
);
328 if (http
->www_authenticate
)
330 free(http
->www_authenticate
);
331 http
->www_authenticate
= NULL
;
334 http
->expect
= (http_status_t
)0;
340 * 'httpClose()' - Close an HTTP connection.
344 httpClose(http_t
*http
) /* I - HTTP connection */
347 OM_uint32 minor_status
; /* Minor status code */
348 #endif /* HAVE_GSSAPI */
351 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
354 * Range check input...
361 * Close any open connection...
364 _httpDisconnect(http
);
367 * Free memory used...
370 httpAddrFreeList(http
->addrlist
);
376 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
377 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
379 if (http
->gssname
!= GSS_C_NO_NAME
)
380 gss_release_name(&minor_status
, &http
->gssname
);
381 #endif /* HAVE_GSSAPI */
383 #ifdef HAVE_AUTHORIZATION_H
385 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
386 #endif /* HAVE_AUTHORIZATION_H */
388 httpClearFields(http
);
390 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
391 free(http
->authstring
);
398 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
400 * @since CUPS 2.0/OS 10.10@
403 int /* O - 1 if they match, 0 if they do not */
404 httpCompareCredentials(
405 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
406 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
408 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
411 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
))
412 if (temp1
->datalen
!= temp2
->datalen
)
414 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
417 return (temp1
== temp2
);
422 * 'httpConnect()' - Connect to a HTTP server.
424 * This function is deprecated - use @link httpConnect2@ instead.
426 * @deprecated@ @exclude all@
429 http_t
* /* O - New HTTP connection */
430 httpConnect(const char *host
, /* I - Host to connect to */
431 int port
) /* I - Port number */
433 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
434 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
439 * 'httpConnect2()' - Connect to a HTTP server.
441 * @since CUPS 1.7/macOS 10.9@
444 http_t
* /* O - New HTTP connection */
446 const char *host
, /* I - Host to connect to */
447 int port
, /* I - Port number */
448 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
449 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
450 http_encryption_t encryption
, /* I - Type of encryption to use */
451 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
452 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
453 int *cancel
) /* I - Pointer to "cancel" variable */
455 http_t
*http
; /* New HTTP connection */
458 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
));
461 * Create the HTTP structure...
464 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
465 _HTTP_MODE_CLIENT
)) == NULL
)
469 * Optionally connect to the remote system...
472 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
476 * Could not connect to any known address - bail out!
486 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
488 * This function is now deprecated. Please use the @link httpConnect2@ function
491 * @deprecated@ @exclude all@
494 http_t
* /* O - New HTTP connection */
496 const char *host
, /* I - Host to connect to */
497 int port
, /* I - Port number */
498 http_encryption_t encryption
) /* I - Type of encryption to use */
500 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
501 host
, port
, encryption
));
503 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
509 * 'httpDelete()' - Send a DELETE request to the server.
512 int /* O - Status of call (0 = success) */
513 httpDelete(http_t
*http
, /* I - HTTP connection */
514 const char *uri
) /* I - URI to delete */
516 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
521 * '_httpDisconnect()' - Disconnect a HTTP connection.
525 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
530 #endif /* HAVE_SSL */
532 httpAddrClose(NULL
, http
->fd
);
539 * 'httpEncryption()' - Set the required encryption on the link.
542 int /* O - -1 on error, 0 on success */
543 httpEncryption(http_t
*http
, /* I - HTTP connection */
544 http_encryption_t e
) /* I - New encryption preference */
546 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
552 if (http
->mode
== _HTTP_MODE_CLIENT
)
554 http
->encryption
= e
;
556 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
557 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
558 return (httpReconnect2(http
, 30000, NULL
));
559 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
560 return (http_tls_upgrade(http
));
566 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
569 http
->encryption
= e
;
570 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
571 return (_httpTLSStart(http
));
576 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
580 #endif /* HAVE_SSL */
585 * 'httpError()' - Get the last error on a connection.
588 int /* O - Error code (errno) value */
589 httpError(http_t
*http
) /* I - HTTP connection */
592 return (http
->error
);
599 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
603 http_field_t
/* O - Field index */
604 httpFieldValue(const char *name
) /* I - String name */
606 int i
; /* Looping var */
609 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
610 if (!_cups_strcasecmp(name
, http_fields
[i
]))
611 return ((http_field_t
)i
);
613 return (HTTP_FIELD_UNKNOWN
);
618 * 'httpFlush()' - Flush data read from a HTTP connection.
622 httpFlush(http_t
*http
) /* I - HTTP connection */
624 char buffer
[8192]; /* Junk buffer */
625 int blocking
; /* To block or not to block */
626 http_state_t oldstate
; /* Old state */
629 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
632 * Nothing to do if we are in the "waiting" state...
635 if (http
->state
== HTTP_STATE_WAITING
)
639 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
642 blocking
= http
->blocking
;
646 * Read any data we can...
649 oldstate
= http
->state
;
650 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
653 * Restore blocking and reset the connection if we didn't get all of
654 * the remaining data...
657 http
->blocking
= blocking
;
659 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
663 * Didn't get the data back, so close the current connection.
668 http_content_coding_finish(http
);
669 #endif /* HAVE_LIBZ */
671 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
673 http
->state
= HTTP_STATE_WAITING
;
678 #endif /* HAVE_SSL */
680 httpAddrClose(NULL
, http
->fd
);
688 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
690 * @since CUPS 1.2/macOS 10.5@
693 int /* O - Bytes written or -1 on error */
694 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
696 ssize_t bytes
; /* Bytes written */
699 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
701 if (!http
|| !http
->wused
)
703 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
704 "1httpFlushWrite: No connection.");
708 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
709 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
711 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
715 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
722 * 'httpFreeCredentials()' - Free an array of credentials.
727 cups_array_t
*credentials
) /* I - Array of credentials */
729 http_credential_t
*credential
; /* Credential */
732 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
734 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
736 cupsArrayRemove(credentials
, credential
);
737 free((void *)credential
->data
);
741 cupsArrayDelete(credentials
);
746 * 'httpGet()' - Send a GET request to the server.
749 int /* O - Status of call (0 = success) */
750 httpGet(http_t
*http
, /* I - HTTP connection */
751 const char *uri
) /* I - URI to get */
753 return (http_send(http
, HTTP_STATE_GET
, uri
));
758 * 'httpGetActivity()' - Get the most recent activity for a connection.
760 * The return value is the time in seconds of the last read or write.
762 * @since CUPS 2.0/OS 10.10@
765 time_t /* O - Time of last read or write */
766 httpGetActivity(http_t
*http
) /* I - HTTP connection */
768 return (http
? http
->activity
: 0);
773 * 'httpGetAuthString()' - Get the current authorization string.
775 * The authorization string is set by @link cupsDoAuthentication@ and
776 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
777 * string to use with @link httpSetField@ for the
778 * @code HTTP_FIELD_AUTHORIZATION@ value.
780 * @since CUPS 1.3/macOS 10.5@
783 char * /* O - Authorization string */
784 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
787 return (http
->authstring
);
794 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
796 * @since CUPS 1.2/macOS 10.5@
799 int /* O - 1 if blocking, 0 if non-blocking */
800 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
802 return (http
? http
->blocking
: 0);
807 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
808 * the client and server.
810 * This function uses the value of the Accepts-Encoding HTTP header and must be
811 * called after receiving a response from the server or a request from the
812 * client. The value returned can be use in subsequent requests (for clients)
813 * or in the response (for servers) in order to compress the content stream.
815 * @since CUPS 1.7/macOS 10.9@
818 const char * /* O - Content-Coding value or
819 @code NULL@ for the identity
821 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
824 if (http
&& http
->accept_encoding
)
826 int i
; /* Looping var */
827 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
828 *start
, /* Start of coding value */
829 *end
; /* End of coding value */
830 double qvalue
; /* "qvalue" for coding */
831 struct lconv
*loc
= localeconv(); /* Locale data */
832 static const char * const codings
[] =
833 { /* Supported content codings */
840 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
842 for (start
= temp
; *start
; start
= end
)
845 * Find the end of the coding name...
850 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
856 * Grab the qvalue as needed...
859 if (!strncmp(end
, ";q=", 3))
860 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
863 * Skip past all attributes...
867 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
873 while (*end
&& isspace(*end
& 255))
877 * Check value if it matches something we support...
883 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
884 if (!strcmp(start
, codings
[i
]))
888 #endif /* HAVE_LIBZ */
895 * 'httpGetCookie()' - Get any cookie data from the response.
897 * @since CUPS 1.1.19/macOS 10.3@
900 const char * /* O - Cookie data or @code NULL@ */
901 httpGetCookie(http_t
*http
) /* I - HTTP connection */
903 return (http
? http
->cookie
: NULL
);
908 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
910 * This function returns the encryption mode for the connection. Use the
911 * @link httpIsEncrypted@ function to determine whether a TLS session has
914 * @since CUPS 2.0/OS 10.10@
917 http_encryption_t
/* O - Current encryption mode */
918 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
920 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
925 * 'httpGetExpect()' - Get the value of the Expect header, if any.
927 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
928 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
930 * @since CUPS 1.7/macOS 10.9@
933 http_status_t
/* O - Expect: status, if any */
934 httpGetExpect(http_t
*http
) /* I - HTTP connection */
937 return (HTTP_STATUS_ERROR
);
939 return (http
->expect
);
944 * 'httpGetFd()' - Get the file descriptor associated with a connection.
946 * @since CUPS 1.2/macOS 10.5@
949 int /* O - File descriptor or -1 if none */
950 httpGetFd(http_t
*http
) /* I - HTTP connection */
952 return (http
? http
->fd
: -1);
957 * 'httpGetField()' - Get a field value from a request/response.
960 const char * /* O - Field value */
961 httpGetField(http_t
*http
, /* I - HTTP connection */
962 http_field_t field
) /* I - Field to get */
964 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
969 case HTTP_FIELD_ACCEPT_ENCODING
:
970 return (http
->accept_encoding
);
972 case HTTP_FIELD_ALLOW
:
973 return (http
->allow
);
975 case HTTP_FIELD_SERVER
:
976 return (http
->server
);
978 case HTTP_FIELD_AUTHORIZATION
:
979 if (http
->field_authorization
)
982 * Special case for Authorization: as its contents can be
983 * longer than HTTP_MAX_VALUE...
986 return (http
->field_authorization
);
989 return (http
->fields
[field
]);
991 case HTTP_FIELD_WWW_AUTHENTICATE
:
992 if (http
->www_authenticate
)
995 * Special case for WWW-Authenticate: as its contents can be
996 * longer than HTTP_MAX_VALUE...
999 return (http
->www_authenticate
);
1002 return (http
->fields
[field
]);
1005 return (http
->fields
[field
]);
1011 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
1013 * @since CUPS 2.0/OS 10.10@
1016 http_keepalive_t
/* O - Keep-Alive state */
1017 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
1019 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1024 * 'httpGetLength()' - Get the amount of data remaining from the
1025 * content-length or transfer-encoding fields.
1027 * This function is deprecated and will not return lengths larger than
1028 * 2^31 - 1; use httpGetLength2() instead.
1030 * @deprecated@ @exclude all@
1033 int /* O - Content length */
1034 httpGetLength(http_t
*http
) /* I - HTTP connection */
1037 * Get the read content length and return the 32-bit value.
1042 httpGetLength2(http
);
1044 return (http
->_data_remaining
);
1052 * 'httpGetLength2()' - Get the amount of data remaining from the
1053 * content-length or transfer-encoding fields.
1055 * This function returns the complete content length, even for
1056 * content larger than 2^31 - 1.
1058 * @since CUPS 1.2/macOS 10.5@
1061 off_t
/* O - Content length */
1062 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1064 off_t remaining
; /* Remaining length */
1067 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1072 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1074 DEBUG_puts("4httpGetLength2: chunked request!");
1080 * The following is a hack for HTTP servers that don't send a
1081 * Content-Length or Transfer-Encoding field...
1083 * If there is no Content-Length then the connection must close
1084 * after the transfer is complete...
1087 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1090 * Default content length is 0 for errors and certain types of operations,
1091 * and 2^31-1 for other successful requests...
1094 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1095 http
->state
== HTTP_STATE_OPTIONS
||
1096 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1097 http
->state
== HTTP_STATE_HEAD
||
1098 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1099 http
->state
== HTTP_STATE_DELETE
||
1100 http
->state
== HTTP_STATE_TRACE
||
1101 http
->state
== HTTP_STATE_CONNECT
)
1104 remaining
= 2147483647;
1106 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1110 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1111 CUPS_LLCAST remaining
));
1119 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1121 * @since CUPS 2.0/OS 10.10@
1124 size_t /* O - Number of bytes buffered */
1125 httpGetPending(http_t
*http
) /* I - HTTP connection */
1127 return (http
? (size_t)http
->wused
: 0);
1132 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1134 * @since CUPS 2.0/OS 10.10@
1137 size_t /* O - Number of bytes available */
1138 httpGetReady(http_t
*http
) /* I - HTTP connection */
1142 else if (http
->used
> 0)
1143 return ((size_t)http
->used
);
1146 return (_httpTLSPending(http
));
1147 #endif /* HAVE_SSL */
1154 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1155 * body or current chunk.
1157 * The @link httpIsChunked@ function can be used to determine whether the
1158 * message body is chunked or fixed-length.
1160 * @since CUPS 2.0/OS 10.10@
1163 size_t /* O - Remaining bytes */
1164 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1166 return (http
? (size_t)http
->data_remaining
: 0);
1171 * 'httpGets()' - Get a line of text from a HTTP connection.
1174 char * /* O - Line or @code NULL@ */
1175 httpGets(char *line
, /* I - Line to read into */
1176 int length
, /* I - Max length of buffer */
1177 http_t
*http
) /* I - HTTP connection */
1179 char *lineptr
, /* Pointer into line */
1180 *lineend
, /* End of line */
1181 *bufptr
, /* Pointer into input buffer */
1182 *bufend
; /* Pointer to end of buffer */
1183 ssize_t bytes
; /* Number of bytes read */
1184 int eol
; /* End-of-line? */
1187 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1189 if (!http
|| !line
|| length
<= 1)
1193 * Read a line from the buffer...
1198 lineend
= line
+ length
- 1;
1201 while (lineptr
< lineend
)
1204 * Pre-load the buffer as needed...
1213 while (http
->used
== 0)
1216 * No newline; see if there is more data to be read...
1219 while (!_httpWait(http
, http
->wait_value
, 1))
1221 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1224 DEBUG_puts("3httpGets: Timed out!");
1226 http
->error
= WSAETIMEDOUT
;
1228 http
->error
= ETIMEDOUT
;
1233 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1235 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1240 * Nope, can't get a line this time...
1244 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1246 if (WSAGetLastError() == WSAEINTR
)
1248 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1250 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1253 http
->error
= WSAGetLastError();
1255 else if (WSAGetLastError() != http
->error
)
1257 http
->error
= WSAGetLastError();
1262 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1266 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1268 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1270 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1273 http
->error
= errno
;
1275 else if (errno
!= http
->error
)
1277 http
->error
= errno
;
1284 else if (bytes
== 0)
1286 http
->error
= EPIPE
;
1292 * Yup, update the amount used...
1295 http
->used
+= (int)bytes
;
1299 * Now copy as much of the current line as possible...
1302 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1303 lineptr
< lineend
&& bufptr
< bufend
;)
1305 if (*bufptr
== 0x0a)
1311 else if (*bufptr
== 0x0d)
1314 *lineptr
++ = *bufptr
++;
1317 http
->used
-= (int)(bufptr
- http
->buffer
);
1319 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1327 http
->activity
= time(NULL
);
1331 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1337 DEBUG_puts("3httpGets: No new line available!");
1344 * 'httpGetState()' - Get the current state of the HTTP request.
1347 http_state_t
/* O - HTTP state */
1348 httpGetState(http_t
*http
) /* I - HTTP connection */
1350 return (http
? http
->state
: HTTP_STATE_ERROR
);
1355 * 'httpGetStatus()' - Get the status of the last HTTP request.
1357 * @since CUPS 1.2/macOS 10.5@
1360 http_status_t
/* O - HTTP status */
1361 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1363 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1368 * 'httpGetSubField()' - Get a sub-field value.
1370 * @deprecated@ @exclude all@
1373 char * /* O - Value or @code NULL@ */
1374 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1375 http_field_t field
, /* I - Field index */
1376 const char *name
, /* I - Name of sub-field */
1377 char *value
) /* O - Value string */
1379 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1384 * 'httpGetSubField2()' - Get a sub-field value.
1386 * @since CUPS 1.2/macOS 10.5@
1389 char * /* O - Value or @code NULL@ */
1390 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1391 http_field_t field
, /* I - Field index */
1392 const char *name
, /* I - Name of sub-field */
1393 char *value
, /* O - Value string */
1394 int valuelen
) /* I - Size of value buffer */
1396 const char *fptr
; /* Pointer into field */
1397 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1398 *ptr
, /* Pointer into string buffer */
1399 *end
; /* End of value buffer */
1401 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1403 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1404 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1407 end
= value
+ valuelen
- 1;
1409 for (fptr
= http
->fields
[field
]; *fptr
;)
1412 * Skip leading whitespace...
1415 while (_cups_isspace(*fptr
))
1425 * Get the sub-field name...
1429 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1430 ptr
< (temp
+ sizeof(temp
) - 1);
1435 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1438 * Skip trailing chars up to the '='...
1441 while (_cups_isspace(*fptr
))
1451 * Skip = and leading whitespace...
1456 while (_cups_isspace(*fptr
))
1462 * Read quoted string...
1465 for (ptr
= value
, fptr
++;
1466 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1471 while (*fptr
&& *fptr
!= '\"')
1480 * Read unquoted string...
1484 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1489 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1493 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1496 * See if this is the one...
1499 if (!strcmp(name
, temp
))
1501 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1508 DEBUG_puts("3httpGetSubField2: Returning NULL");
1515 * 'httpGetVersion()' - Get the HTTP version at the other end.
1518 http_version_t
/* O - Version number */
1519 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1521 return (http
? http
->version
: HTTP_VERSION_1_0
);
1526 * 'httpHead()' - Send a HEAD request to the server.
1529 int /* O - Status of call (0 = success) */
1530 httpHead(http_t
*http
, /* I - HTTP connection */
1531 const char *uri
) /* I - URI for head */
1533 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1534 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1539 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1540 * default HTTP proxy (if any).
1544 httpInitialize(void)
1546 static int initialized
= 0; /* Have we been called before? */
1548 WSADATA winsockdata
; /* WinSock data */
1555 _cupsGlobalUnlock();
1560 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1562 #elif !defined(SO_NOSIGPIPE)
1564 * Ignore SIGPIPE signals...
1568 sigset(SIGPIPE
, SIG_IGN
);
1570 # elif defined(HAVE_SIGACTION)
1571 struct sigaction action
; /* POSIX sigaction data */
1574 memset(&action
, 0, sizeof(action
));
1575 action
.sa_handler
= SIG_IGN
;
1576 sigaction(SIGPIPE
, &action
, NULL
);
1579 signal(SIGPIPE
, SIG_IGN
);
1580 # endif /* !SO_NOSIGPIPE */
1584 _httpTLSInitialize();
1585 # endif /* HAVE_SSL */
1588 _cupsGlobalUnlock();
1593 * 'httpIsChunked()' - Report whether a message body is chunked.
1595 * This function returns non-zero if the message body is composed of
1596 * variable-length chunks.
1598 * @since CUPS 2.0/OS 10.10@
1601 int /* O - 1 if chunked, 0 if not */
1602 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1604 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1609 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1611 * This function returns non-zero if the connection is currently encrypted.
1613 * @since CUPS 2.0/OS 10.10@
1616 int /* O - 1 if encrypted, 0 if not */
1617 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1619 return (http
? http
->tls
!= NULL
: 0);
1624 * 'httpOptions()' - Send an OPTIONS request to the server.
1627 int /* O - Status of call (0 = success) */
1628 httpOptions(http_t
*http
, /* I - HTTP connection */
1629 const char *uri
) /* I - URI for options */
1631 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1636 * 'httpPeek()' - Peek at data from a HTTP connection.
1638 * This function copies available data from the given HTTP connection, reading
1639 * a buffer as needed. The data is still available for reading using
1642 * For non-blocking connections the usual timeouts apply.
1644 * @since CUPS 1.7/macOS 10.9@
1647 ssize_t
/* O - Number of bytes copied */
1648 httpPeek(http_t
*http
, /* I - HTTP connection */
1649 char *buffer
, /* I - Buffer for data */
1650 size_t length
) /* I - Maximum number of bytes */
1652 ssize_t bytes
; /* Bytes read */
1653 char len
[32]; /* Length string */
1656 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1658 if (http
== NULL
|| buffer
== NULL
)
1661 http
->activity
= time(NULL
);
1667 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1668 http
->data_remaining
<= 0)
1670 DEBUG_puts("2httpPeek: Getting chunk length...");
1672 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1674 DEBUG_puts("1httpPeek: Could not get length!");
1680 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1681 if (!httpGets(len
, sizeof(len
), http
))
1683 DEBUG_puts("1httpPeek: Could not get chunk length.");
1688 http
->data_remaining
= strtoll(len
, NULL
, 16);
1690 if (http
->data_remaining
< 0)
1692 DEBUG_puts("1httpPeek: Negative chunk length!");
1697 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1698 CUPS_LLCAST http
->data_remaining
));
1700 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1703 * A zero-length chunk ends a transfer; unless we are reading POST
1708 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1709 http_content_coding_finish(http
);
1710 #endif /* HAVE_LIBZ */
1712 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1713 httpGets(len
, sizeof(len
), http
);
1715 if (http
->state
== HTTP_STATE_POST_RECV
)
1718 http
->state
= HTTP_STATE_STATUS
;
1720 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1721 httpStateString(http
->state
)));
1724 * Prevent future reads for this request...
1727 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1731 else if (length
> (size_t)http
->data_remaining
)
1732 length
= (size_t)http
->data_remaining
;
1735 if (http
->used
== 0 &&
1736 (http
->coding
== _HTTP_CODING_IDENTITY
||
1737 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1739 if (http
->used
== 0)
1740 #endif /* HAVE_LIBZ */
1743 * Buffer small reads for better performance...
1746 ssize_t buflen
; /* Length of read for buffer */
1748 if (!http
->blocking
)
1750 while (!httpWait(http
, http
->wait_value
))
1752 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1759 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1760 buflen
= sizeof(http
->buffer
);
1762 buflen
= (ssize_t
)http
->data_remaining
;
1764 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1765 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1767 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1768 CUPS_LLCAST bytes
));
1772 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1775 http
->used
= (int)bytes
;
1780 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1782 # ifdef HAVE_INFLATECOPY
1783 int zerr
; /* Decompressor error */
1784 z_stream stream
; /* Copy of decompressor stream */
1786 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1788 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1789 /* Number of bytes to copy */
1791 if (http
->stream
.avail_in
> 0 &&
1792 http
->stream
.next_in
> http
->sbuffer
)
1793 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1795 http
->stream
.next_in
= http
->sbuffer
;
1797 if (buflen
> (size_t)http
->data_remaining
)
1798 buflen
= (size_t)http
->data_remaining
;
1800 if (buflen
> (size_t)http
->used
)
1801 buflen
= (size_t)http
->used
;
1803 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1804 "decompression buffer.", (int)buflen
));
1806 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1807 http
->stream
.avail_in
+= buflen
;
1808 http
->used
-= (int)buflen
;
1809 http
->data_remaining
-= (off_t
)buflen
;
1812 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1815 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1816 (int)http
->stream
.avail_in
));
1818 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1820 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1821 http
->error
= ENOMEM
;
1825 stream
.next_out
= (Bytef
*)buffer
;
1826 stream
.avail_out
= (uInt
)length
;
1828 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1829 inflateEnd(&stream
);
1833 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1835 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1842 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1845 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1846 "work with compressed streams.");
1848 # endif /* HAVE_INFLATECOPY */
1851 #endif /* HAVE_LIBZ */
1854 if (length
> (size_t)http
->used
)
1855 length
= (size_t)http
->used
;
1857 bytes
= (ssize_t
)length
;
1859 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1862 memcpy(buffer
, http
->buffer
, length
);
1870 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1873 http
->error
= WSAGetLastError();
1875 if (errno
== EINTR
|| errno
== EAGAIN
)
1878 http
->error
= errno
;
1881 else if (bytes
== 0)
1883 http
->error
= EPIPE
;
1892 * 'httpPost()' - Send a POST request to the server.
1895 int /* O - Status of call (0 = success) */
1896 httpPost(http_t
*http
, /* I - HTTP connection */
1897 const char *uri
) /* I - URI for post */
1899 return (http_send(http
, HTTP_STATE_POST
, uri
));
1904 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1909 int /* O - Number of bytes written */
1910 httpPrintf(http_t
*http
, /* I - HTTP connection */
1911 const char *format
, /* I - printf-style format string */
1912 ...) /* I - Additional args as needed */
1914 ssize_t bytes
; /* Number of bytes to write */
1915 char buf
[16384]; /* Buffer for formatted string */
1916 va_list ap
; /* Variable argument pointer */
1919 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1921 va_start(ap
, format
);
1922 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1925 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1927 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1928 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1933 DEBUG_puts("4httpPrintf: flushing existing data...");
1935 if (httpFlushWrite(http
) < 0)
1939 return ((int)http_write(http
, buf
, (size_t)bytes
));
1945 * 'httpPut()' - Send a PUT request to the server.
1948 int /* O - Status of call (0 = success) */
1949 httpPut(http_t
*http
, /* I - HTTP connection */
1950 const char *uri
) /* I - URI to put */
1952 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1953 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1958 * 'httpRead()' - Read data from a HTTP connection.
1960 * This function is deprecated. Use the httpRead2() function which can
1961 * read more than 2GB of data.
1963 * @deprecated@ @exclude all@
1966 int /* O - Number of bytes read */
1967 httpRead(http_t
*http
, /* I - HTTP connection */
1968 char *buffer
, /* I - Buffer for data */
1969 int length
) /* I - Maximum number of bytes */
1971 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1976 * 'httpRead2()' - Read data from a HTTP connection.
1978 * @since CUPS 1.2/macOS 10.5@
1981 ssize_t
/* O - Number of bytes read */
1982 httpRead2(http_t
*http
, /* I - HTTP connection */
1983 char *buffer
, /* I - Buffer for data */
1984 size_t length
) /* I - Maximum number of bytes */
1986 ssize_t bytes
; /* Bytes read */
1990 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
));
1992 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
));
1993 #endif /* HAVE_LIBZ */
1995 if (http
== NULL
|| buffer
== NULL
)
1998 http
->activity
= time(NULL
);
2005 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2009 if (http
->stream
.avail_in
> 0)
2011 int zerr
; /* Decompressor error */
2013 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
2014 (int)http
->stream
.avail_in
, (int)length
));
2016 http
->stream
.next_out
= (Bytef
*)buffer
;
2017 http
->stream
.avail_out
= (uInt
)length
;
2019 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2021 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2023 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2030 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2032 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2033 http
->stream
.avail_in
, http
->stream
.avail_out
,
2041 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2042 /* Additional bytes for buffer */
2046 if (http
->stream
.avail_in
> 0 &&
2047 http
->stream
.next_in
> http
->sbuffer
)
2048 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2050 http
->stream
.next_in
= http
->sbuffer
;
2052 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2053 "decompression buffer.", (int)buflen
));
2055 if (http
->data_remaining
> 0)
2057 if (buflen
> http
->data_remaining
)
2058 buflen
= (ssize_t
)http
->data_remaining
;
2060 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2062 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2063 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2069 else if (bytes
== 0)
2072 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2073 "decompression buffer.", CUPS_LLCAST bytes
));
2075 http
->data_remaining
-= bytes
;
2076 http
->stream
.avail_in
+= (uInt
)bytes
;
2078 if (http
->data_remaining
<= 0 &&
2079 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2082 * Read the trailing blank line now...
2085 char len
[32]; /* Length string */
2087 httpGets(len
, sizeof(len
), http
);
2099 #endif /* HAVE_LIBZ */
2100 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2102 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2104 http
->data_remaining
-= bytes
;
2106 if (http
->data_remaining
<= 0)
2109 * Read the trailing blank line now...
2112 char len
[32]; /* Length string */
2114 httpGets(len
, sizeof(len
), http
);
2118 else if (http
->data_remaining
<= 0)
2121 * No more data to read...
2128 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2131 if (length
> (size_t)http
->data_remaining
)
2132 length
= (size_t)http
->data_remaining
;
2134 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2136 http
->data_remaining
-= bytes
;
2138 if (http
->data_remaining
<= 0 &&
2139 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2142 * Read the trailing blank line now...
2145 char len
[32]; /* Length string */
2147 httpGets(len
, sizeof(len
), http
);
2154 (http
->coding
== _HTTP_CODING_IDENTITY
||
2155 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2156 #endif /* HAVE_LIBZ */
2157 ((http
->data_remaining
<= 0 &&
2158 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2159 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2162 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2163 http_content_coding_finish(http
);
2164 #endif /* HAVE_LIBZ */
2166 if (http
->state
== HTTP_STATE_POST_RECV
)
2168 else if (http
->state
== HTTP_STATE_GET_SEND
||
2169 http
->state
== HTTP_STATE_POST_SEND
)
2170 http
->state
= HTTP_STATE_WAITING
;
2172 http
->state
= HTTP_STATE_STATUS
;
2174 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2175 httpStateString(http
->state
)));
2183 * 'httpReadRequest()' - Read a HTTP request from a connection.
2185 * @since CUPS 1.7/macOS 10.9@
2188 http_state_t
/* O - New state of connection */
2189 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2190 char *uri
, /* I - URI buffer */
2191 size_t urilen
) /* I - Size of URI buffer */
2193 char line
[4096], /* HTTP request line */
2194 *req_method
, /* HTTP request method */
2195 *req_uri
, /* HTTP request URI */
2196 *req_version
; /* HTTP request version number string */
2200 * Range check input...
2203 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2208 if (!http
|| !uri
|| urilen
< 1)
2210 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2211 return (HTTP_STATE_ERROR
);
2213 else if (http
->state
!= HTTP_STATE_WAITING
)
2215 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2216 httpStateString(http
->state
)));
2217 return (HTTP_STATE_ERROR
);
2224 httpClearFields(http
);
2226 http
->activity
= time(NULL
);
2227 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2228 http
->data_remaining
= 0;
2229 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2230 http
->status
= HTTP_STATUS_OK
;
2231 http
->version
= HTTP_VERSION_1_1
;
2234 * Read a line from the socket...
2237 if (!httpGets(line
, sizeof(line
), http
))
2239 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2240 return (HTTP_STATE_ERROR
);
2245 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2246 return (HTTP_STATE_WAITING
);
2249 DEBUG_printf(("1httpReadRequest: %s", line
));
2258 while (*req_uri
&& !isspace(*req_uri
& 255))
2263 DEBUG_puts("1httpReadRequest: No request URI.");
2264 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2265 return (HTTP_STATE_ERROR
);
2270 while (*req_uri
&& isspace(*req_uri
& 255))
2273 req_version
= req_uri
;
2275 while (*req_version
&& !isspace(*req_version
& 255))
2280 DEBUG_puts("1httpReadRequest: No request protocol version.");
2281 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2282 return (HTTP_STATE_ERROR
);
2285 *req_version
++ = '\0';
2287 while (*req_version
&& isspace(*req_version
& 255))
2294 if (!strcmp(req_method
, "OPTIONS"))
2295 http
->state
= HTTP_STATE_OPTIONS
;
2296 else if (!strcmp(req_method
, "GET"))
2297 http
->state
= HTTP_STATE_GET
;
2298 else if (!strcmp(req_method
, "HEAD"))
2299 http
->state
= HTTP_STATE_HEAD
;
2300 else if (!strcmp(req_method
, "POST"))
2301 http
->state
= HTTP_STATE_POST
;
2302 else if (!strcmp(req_method
, "PUT"))
2303 http
->state
= HTTP_STATE_PUT
;
2304 else if (!strcmp(req_method
, "DELETE"))
2305 http
->state
= HTTP_STATE_DELETE
;
2306 else if (!strcmp(req_method
, "TRACE"))
2307 http
->state
= HTTP_STATE_TRACE
;
2308 else if (!strcmp(req_method
, "CONNECT"))
2309 http
->state
= HTTP_STATE_CONNECT
;
2312 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2313 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2314 return (HTTP_STATE_UNKNOWN_METHOD
);
2317 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2318 httpStateString(http
->state
)));
2320 if (!strcmp(req_version
, "HTTP/1.0"))
2322 http
->version
= HTTP_VERSION_1_0
;
2323 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2325 else if (!strcmp(req_version
, "HTTP/1.1"))
2327 http
->version
= HTTP_VERSION_1_1
;
2328 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2332 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2333 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2334 return (HTTP_STATE_UNKNOWN_VERSION
);
2337 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2338 strlcpy(uri
, req_uri
, urilen
);
2340 return (http
->state
);
2345 * 'httpReconnect()' - Reconnect to a HTTP server.
2347 * This function is deprecated. Please use the @link httpReconnect2@ function
2350 * @deprecated@ @exclude all@
2353 int /* O - 0 on success, non-zero on failure */
2354 httpReconnect(http_t
*http
) /* I - HTTP connection */
2356 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2358 return (httpReconnect2(http
, 30000, NULL
));
2363 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2367 int /* O - 0 on success, non-zero on failure */
2368 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2369 int msec
, /* I - Timeout in milliseconds */
2370 int *cancel
) /* I - Pointer to "cancel" variable */
2372 http_addrlist_t
*addr
; /* Connected address */
2374 http_addrlist_t
*current
; /* Current address */
2375 char temp
[256]; /* Temporary address string */
2379 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2383 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2390 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2393 #endif /* HAVE_SSL */
2396 * Close any previously open socket...
2401 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2403 httpAddrClose(NULL
, http
->fd
);
2409 * Reset all state (except fields, which may be reused)...
2412 http
->state
= HTTP_STATE_WAITING
;
2413 http
->version
= HTTP_VERSION_1_1
;
2414 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2415 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2416 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2417 http
->_data_remaining
= 0;
2419 http
->data_remaining
= 0;
2420 http
->hostaddr
= NULL
;
2424 * Connect to the server...
2428 for (current
= http
->addrlist
; current
; current
= current
->next
)
2429 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2430 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2431 httpAddrPort(&(current
->addr
))));
2434 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2437 * Unable to connect...
2441 http
->error
= WSAGetLastError();
2443 http
->error
= errno
;
2445 http
->status
= HTTP_STATUS_ERROR
;
2447 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2448 strerror(http
->error
)));
2453 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2455 if (http
->timeout_value
> 0)
2456 http_set_timeout(http
->fd
, http
->timeout_value
);
2458 http
->hostaddr
= &(addr
->addr
);
2462 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2465 * Always do encryption via SSL.
2468 if (_httpTLSStart(http
) != 0)
2470 httpAddrClose(NULL
, http
->fd
);
2475 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2476 return (http_tls_upgrade(http
));
2477 #endif /* HAVE_SSL */
2479 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2480 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2481 httpAddrPort(http
->hostaddr
)));
2488 * 'httpSetAuthString()' - Set the current authorization string.
2490 * This function just stores a copy of the current authorization string in
2491 * the HTTP connection object. You must still call @link httpSetField@ to set
2492 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2493 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2496 * @since CUPS 1.3/macOS 10.5@
2500 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2501 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2502 const char *data
) /* I - Auth data (NULL for none) */
2505 * Range check input...
2511 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2512 free(http
->authstring
);
2514 http
->authstring
= http
->_authstring
;
2519 * Set the current authorization string...
2522 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2525 if (len
> sizeof(http
->_authstring
))
2527 if ((temp
= malloc(len
)) == NULL
)
2528 len
= sizeof(http
->_authstring
);
2530 http
->authstring
= temp
;
2534 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2536 strlcpy(http
->authstring
, scheme
, len
);
2541 * Clear the current authorization string...
2544 http
->_authstring
[0] = '\0';
2550 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2553 * @since CUPS 1.5/macOS 10.7@
2556 int /* O - Status of call (0 = success) */
2557 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2558 cups_array_t
*credentials
) /* I - Array of credentials */
2560 if (!http
|| cupsArrayCount(credentials
) < 1)
2564 _httpFreeCredentials(http
->tls_credentials
);
2566 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2567 #endif /* HAVE_SSL */
2569 return (http
->tls_credentials
? 0 : -1);
2574 * 'httpSetCookie()' - Set the cookie value(s).
2576 * @since CUPS 1.1.19/macOS 10.3@
2580 httpSetCookie(http_t
*http
, /* I - Connection */
2581 const char *cookie
) /* I - Cookie string */
2590 http
->cookie
= strdup(cookie
);
2592 http
->cookie
= NULL
;
2597 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2599 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2600 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2602 * @since CUPS 1.7/macOS 10.9@
2606 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2607 http_field_t field
, /* I - Field index */
2608 const char *value
)/* I - Value */
2610 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2617 case HTTP_FIELD_ACCEPT_ENCODING
:
2618 if (http
->default_accept_encoding
)
2619 _cupsStrFree(http
->default_accept_encoding
);
2621 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2624 case HTTP_FIELD_SERVER
:
2625 if (http
->default_server
)
2626 _cupsStrFree(http
->default_server
);
2628 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2631 case HTTP_FIELD_USER_AGENT
:
2632 if (http
->default_user_agent
)
2633 _cupsStrFree(http
->default_user_agent
);
2635 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2639 DEBUG_puts("1httpSetDefaultField: Ignored.");
2646 * 'httpSetExpect()' - Set the Expect: header in a request.
2648 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2651 * @since CUPS 1.2/macOS 10.5@
2655 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2656 http_status_t expect
) /* I - HTTP status to expect
2657 (@code HTTP_STATUS_CONTINUE@) */
2659 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2662 http
->expect
= expect
;
2667 * 'httpSetField()' - Set the value of an HTTP header.
2671 httpSetField(http_t
*http
, /* I - HTTP connection */
2672 http_field_t field
, /* I - Field index */
2673 const char *value
) /* I - Value */
2675 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2678 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2679 field
>= HTTP_FIELD_MAX
||
2685 case HTTP_FIELD_ACCEPT_ENCODING
:
2686 if (http
->accept_encoding
)
2687 _cupsStrFree(http
->accept_encoding
);
2689 http
->accept_encoding
= _cupsStrAlloc(value
);
2692 case HTTP_FIELD_ALLOW
:
2694 _cupsStrFree(http
->allow
);
2696 http
->allow
= _cupsStrAlloc(value
);
2699 case HTTP_FIELD_SERVER
:
2701 _cupsStrFree(http
->server
);
2703 http
->server
= _cupsStrAlloc(value
);
2706 case HTTP_FIELD_WWW_AUTHENTICATE
:
2707 if (!http
->www_authenticate
)
2710 * First WWW-Authenticate seen, just copy it over...
2713 http
->www_authenticate
= strdup(value
);
2714 strlcpy(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], value
, HTTP_MAX_VALUE
);
2719 * Nth WWW-Authenticate seen, append to existing string...
2722 size_t len
= strlen(http
->www_authenticate
) + 2 + strlen(value
) + 1;
2723 char *temp
= realloc(http
->www_authenticate
, len
);
2728 http
->www_authenticate
= temp
;
2729 strlcat(http
->www_authenticate
, ", ", len
);
2730 strlcat(http
->www_authenticate
, value
, len
);
2732 /* Probably more efficient than two more strlcat's */
2733 strlcpy(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], http
->www_authenticate
, HTTP_MAX_VALUE
);
2738 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2742 if (field
== HTTP_FIELD_AUTHORIZATION
)
2745 * Special case for Authorization: as its contents can be
2746 * longer than HTTP_MAX_VALUE
2749 if (http
->field_authorization
)
2750 free(http
->field_authorization
);
2752 http
->field_authorization
= strdup(value
);
2754 else if (field
== HTTP_FIELD_HOST
)
2757 * Special-case for Host: as we don't want a trailing "." on the hostname and
2758 * need to bracket IPv6 numeric addresses.
2761 char *ptr
= strchr(value
, ':');
2763 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2766 * Bracket IPv6 numeric addresses...
2768 * This is slightly inefficient (basically copying twice), but is an edge
2769 * case and not worth optimizing...
2772 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2773 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2778 * Check for a trailing dot on the hostname...
2781 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2785 ptr
+= strlen(ptr
) - 1;
2793 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2794 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
2796 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
2797 http_content_coding_start(http
, value
);
2799 #endif /* HAVE_LIBZ */
2804 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2806 * @since CUPS 2.0/OS 10.10@
2811 http_t
*http
, /* I - HTTP connection */
2812 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2815 http
->keep_alive
= keep_alive
;
2820 * 'httpSetLength()' - Set the content-length and content-encoding.
2822 * @since CUPS 1.2/macOS 10.5@
2826 httpSetLength(http_t
*http
, /* I - HTTP connection */
2827 size_t length
) /* I - Length (0 for chunked) */
2829 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2836 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2838 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2842 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2843 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2844 CUPS_LLFMT
, CUPS_LLCAST length
);
2850 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2852 * The optional timeout callback receives both the HTTP connection and a user
2853 * data pointer and must return 1 to continue or 0 to error (time) out.
2855 * @since CUPS 1.5/macOS 10.7@
2860 http_t
*http
, /* I - HTTP connection */
2861 double timeout
, /* I - Number of seconds for timeout,
2862 must be greater than 0 */
2863 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2864 void *user_data
) /* I - User data pointer */
2866 if (!http
|| timeout
<= 0.0)
2869 http
->timeout_cb
= cb
;
2870 http
->timeout_data
= user_data
;
2871 http
->timeout_value
= timeout
;
2874 http_set_timeout(http
->fd
, timeout
);
2876 http_set_wait(http
);
2881 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2883 * @since CUPS 2.0/OS 10.10@
2887 httpShutdown(http_t
*http
) /* I - HTTP connection */
2889 if (!http
|| http
->fd
< 0)
2895 #endif /* HAVE_SSL */
2898 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2900 shutdown(http
->fd
, SHUT_RD
);
2906 * 'httpTrace()' - Send an TRACE request to the server.
2911 int /* O - Status of call (0 = success) */
2912 httpTrace(http_t
*http
, /* I - HTTP connection */
2913 const char *uri
) /* I - URI for trace */
2915 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2920 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2922 * Note: Unlike httpUpdate(), this function does not flush pending write data
2923 * and only retrieves a single status line from the HTTP connection.
2926 int /* O - 1 to continue, 0 to stop */
2927 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2928 http_status_t
*status
) /* O - Current HTTP status */
2930 char line
[32768], /* Line from connection... */
2931 *value
; /* Pointer to value on line */
2932 http_field_t field
; /* Field index */
2933 int major
, minor
; /* HTTP version numbers */
2936 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2939 * Grab a single line from the connection...
2942 if (!httpGets(line
, sizeof(line
), http
))
2944 *status
= HTTP_STATUS_ERROR
;
2948 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2950 if (line
[0] == '\0')
2953 * Blank line means the start of the data section (if any). Return
2954 * the result code, too...
2956 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2957 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2961 if (http
->status
== HTTP_STATUS_CONTINUE
)
2963 *status
= http
->status
;
2967 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2968 http
->digest_tries
= 0;
2971 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2973 if (_httpTLSStart(http
) != 0)
2975 httpAddrClose(NULL
, http
->fd
);
2977 *status
= http
->status
= HTTP_STATUS_ERROR
;
2981 *status
= HTTP_STATUS_CONTINUE
;
2984 #endif /* HAVE_SSL */
2986 if (http_set_length(http
) < 0)
2988 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2989 http
->error
= EINVAL
;
2990 http
->status
= *status
= HTTP_STATUS_ERROR
;
2994 switch (http
->state
)
2996 case HTTP_STATE_GET
:
2997 case HTTP_STATE_POST
:
2998 case HTTP_STATE_POST_RECV
:
2999 case HTTP_STATE_PUT
:
3002 DEBUG_printf(("1_httpUpdate: Set state to %s.",
3003 httpStateString(http
->state
)));
3005 case HTTP_STATE_POST_SEND
:
3006 case HTTP_STATE_HEAD
:
3010 http
->state
= HTTP_STATE_WAITING
;
3012 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
3017 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
3018 http_content_coding_start(http
,
3019 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3020 #endif /* HAVE_LIBZ */
3022 *status
= http
->status
;
3025 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
3028 * Got the beginning of a response...
3031 int intstatus
; /* Status value as an integer */
3033 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
3035 *status
= http
->status
= HTTP_STATUS_ERROR
;
3039 httpClearFields(http
);
3041 http
->version
= (http_version_t
)(major
* 100 + minor
);
3042 *status
= http
->status
= (http_status_t
)intstatus
;
3044 else if ((value
= strchr(line
, ':')) != NULL
)
3051 while (_cups_isspace(*value
))
3054 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3057 * Be tolerants of servers that send unknown attribute fields...
3060 if (!_cups_strcasecmp(line
, "expect"))
3063 * "Expect: 100-continue" or similar...
3066 http
->expect
= (http_status_t
)atoi(value
);
3068 else if (!_cups_strcasecmp(line
, "cookie"))
3071 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3074 httpSetCookie(http
, value
);
3076 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3077 httpSetField(http
, field
, value
);
3080 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3085 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3086 http
->error
= EINVAL
;
3087 http
->status
= *status
= HTTP_STATUS_ERROR
;
3096 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3099 http_status_t
/* O - HTTP status */
3100 httpUpdate(http_t
*http
) /* I - HTTP connection */
3102 http_status_t status
; /* Request status */
3105 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
3108 * Flush pending data, if any...
3113 DEBUG_puts("2httpUpdate: flushing buffer...");
3115 if (httpFlushWrite(http
) < 0)
3116 return (HTTP_STATUS_ERROR
);
3120 * If we haven't issued any commands, then there is nothing to "update"...
3123 if (http
->state
== HTTP_STATE_WAITING
)
3124 return (HTTP_STATUS_CONTINUE
);
3127 * Grab all of the lines we can from the connection...
3130 while (_httpUpdate(http
, &status
));
3133 * See if there was an error...
3136 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3138 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3139 return (http
->status
);
3144 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3145 strerror(http
->error
)));
3146 http
->status
= HTTP_STATUS_ERROR
;
3147 return (HTTP_STATUS_ERROR
);
3151 * Return the current status...
3159 * '_httpWait()' - Wait for data available on a connection (no flush).
3162 int /* O - 1 if data is available, 0 otherwise */
3163 _httpWait(http_t
*http
, /* I - HTTP connection */
3164 int msec
, /* I - Milliseconds to wait */
3165 int usessl
) /* I - Use SSL context? */
3168 struct pollfd pfd
; /* Polled file descriptor */
3170 fd_set input_set
; /* select() input set */
3171 struct timeval timeout
; /* Timeout */
3172 #endif /* HAVE_POLL */
3173 int nfds
; /* Result from select()/poll() */
3176 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3180 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3185 * Check the SSL/TLS buffers for data first...
3189 if (http
->tls
&& _httpTLSPending(http
))
3191 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3194 #endif /* HAVE_SSL */
3197 * Then try doing a select() or poll() to poll the socket...
3202 pfd
.events
= POLLIN
;
3206 nfds
= poll(&pfd
, 1, msec
);
3208 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3213 FD_ZERO(&input_set
);
3214 FD_SET(http
->fd
, &input_set
);
3216 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3220 timeout
.tv_sec
= msec
/ 1000;
3221 timeout
.tv_usec
= (msec
% 1000) * 1000;
3223 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3226 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3228 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3231 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3232 WSAGetLastError() == WSAEWOULDBLOCK
));
3234 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3236 #endif /* HAVE_POLL */
3238 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3246 * 'httpWait()' - Wait for data available on a connection.
3248 * @since CUPS 1.1.19/macOS 10.3@
3251 int /* O - 1 if data is available, 0 otherwise */
3252 httpWait(http_t
*http
, /* I - HTTP connection */
3253 int msec
) /* I - Milliseconds to wait */
3256 * First see if there is data in the buffer...
3259 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3266 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3271 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3273 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3276 #endif /* HAVE_LIBZ */
3279 * Flush pending data, if any...
3284 DEBUG_puts("3httpWait: Flushing write buffer.");
3286 if (httpFlushWrite(http
) < 0)
3291 * If not, check the SSL/TLS buffers and do a select() on the connection...
3294 return (_httpWait(http
, msec
, 1));
3299 * 'httpWrite()' - Write data to a HTTP connection.
3301 * This function is deprecated. Use the httpWrite2() function which can
3302 * write more than 2GB of data.
3304 * @deprecated@ @exclude all@
3307 int /* O - Number of bytes written */
3308 httpWrite(http_t
*http
, /* I - HTTP connection */
3309 const char *buffer
, /* I - Buffer for data */
3310 int length
) /* I - Number of bytes to write */
3312 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3317 * 'httpWrite2()' - Write data to a HTTP connection.
3319 * @since CUPS 1.2/macOS 10.5@
3322 ssize_t
/* O - Number of bytes written */
3323 httpWrite2(http_t
*http
, /* I - HTTP connection */
3324 const char *buffer
, /* I - Buffer for data */
3325 size_t length
) /* I - Number of bytes to write */
3327 ssize_t bytes
; /* Bytes written */
3330 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3333 * Range check input...
3336 if (!http
|| !buffer
)
3338 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3343 * Mark activity on the connection...
3346 http
->activity
= time(NULL
);
3349 * Buffer small writes for better performance...
3353 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3355 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3359 http_content_coding_finish(http
);
3364 size_t slen
; /* Bytes to write */
3365 ssize_t sret
; /* Bytes written */
3367 http
->stream
.next_in
= (Bytef
*)buffer
;
3368 http
->stream
.avail_in
= (uInt
)length
;
3370 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3372 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3374 if (http
->stream
.avail_out
> 0)
3377 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3379 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3381 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3382 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3384 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3390 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3394 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3395 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3398 bytes
= (ssize_t
)length
;
3402 #endif /* HAVE_LIBZ */
3405 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3407 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3408 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3410 httpFlushWrite(http
);
3413 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3416 * Write to buffer...
3419 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3420 CUPS_LLCAST length
));
3422 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3423 http
->wused
+= (int)length
;
3424 bytes
= (ssize_t
)length
;
3429 * Otherwise write the data directly...
3432 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3433 CUPS_LLCAST length
));
3435 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3436 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3438 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3440 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3441 CUPS_LLCAST bytes
));
3444 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3445 http
->data_remaining
-= bytes
;
3451 * Handle end-of-request processing...
3454 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3455 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3458 * Finished with the transfer; unless we are sending POST or PUT
3463 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3464 http_content_coding_finish(http
);
3465 #endif /* HAVE_LIBZ */
3469 if (httpFlushWrite(http
) < 0)
3473 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3476 * Send a 0-length chunk at the end of the request...
3479 http_write(http
, "0\r\n\r\n", 5);
3482 * Reset the data state...
3485 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3486 http
->data_remaining
= 0;
3489 if (http
->state
== HTTP_STATE_POST_RECV
)
3491 else if (http
->state
== HTTP_STATE_POST_SEND
||
3492 http
->state
== HTTP_STATE_GET_SEND
)
3493 http
->state
= HTTP_STATE_WAITING
;
3495 http
->state
= HTTP_STATE_STATUS
;
3497 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3498 httpStateString(http
->state
)));
3501 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3508 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3510 * @since CUPS 1.7/macOS 10.9@
3513 int /* O - 0 on success, -1 on error */
3514 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3515 http_status_t status
) /* I - Status code */
3517 http_encoding_t old_encoding
; /* Old data_encoding value */
3518 off_t old_remaining
; /* Old data_remaining value */
3522 * Range check input...
3525 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3527 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3529 DEBUG_puts("1httpWriteResponse: Bad input.");
3534 * Set the various standard fields if they aren't already...
3537 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3538 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3540 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3542 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3543 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3546 if (http
->version
== HTTP_VERSION_1_1
)
3548 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3550 if (http
->keep_alive
)
3551 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3553 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3556 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3557 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3561 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3562 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3564 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3565 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3567 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3568 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3570 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3571 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3573 #endif /* HAVE_SSL */
3576 httpSetField(http
, HTTP_FIELD_SERVER
,
3577 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3580 * Set the Accept-Encoding field if it isn't already...
3583 if (!http
->accept_encoding
)
3584 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3585 http
->default_accept_encoding
? http
->default_accept_encoding
:
3587 "gzip, deflate, identity");
3590 #endif /* HAVE_LIBZ */
3593 * Send the response header...
3596 old_encoding
= http
->data_encoding
;
3597 old_remaining
= http
->data_remaining
;
3598 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3600 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3601 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3603 http
->status
= HTTP_STATUS_ERROR
;
3607 if (status
!= HTTP_STATUS_CONTINUE
)
3610 * 100 Continue doesn't have the rest of the response headers...
3613 int i
; /* Looping var */
3614 const char *value
; /* Field value */
3616 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3618 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3620 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3622 http
->status
= HTTP_STATUS_ERROR
;
3630 if (strchr(http
->cookie
, ';'))
3632 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3634 http
->status
= HTTP_STATUS_ERROR
;
3638 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3640 http
->status
= HTTP_STATUS_ERROR
;
3646 * "Click-jacking" defense (STR #4492)...
3649 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3650 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3652 http
->status
= HTTP_STATUS_ERROR
;
3657 if (httpWrite2(http
, "\r\n", 2) < 2)
3659 http
->status
= HTTP_STATUS_ERROR
;
3663 if (httpFlushWrite(http
) < 0)
3665 http
->status
= HTTP_STATUS_ERROR
;
3669 if (status
== HTTP_STATUS_CONTINUE
||
3670 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3673 * Restore the old data_encoding and data_length values...
3676 http
->data_encoding
= old_encoding
;
3677 http
->data_remaining
= old_remaining
;
3679 if (old_remaining
<= INT_MAX
)
3680 http
->_data_remaining
= (int)old_remaining
;
3682 http
->_data_remaining
= INT_MAX
;
3684 else if (http
->state
== HTTP_STATE_OPTIONS
||
3685 http
->state
== HTTP_STATE_HEAD
||
3686 http
->state
== HTTP_STATE_PUT
||
3687 http
->state
== HTTP_STATE_TRACE
||
3688 http
->state
== HTTP_STATE_CONNECT
||
3689 http
->state
== HTTP_STATE_STATUS
)
3691 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3692 "was %s.", httpStateString(http
->state
)));
3693 http
->state
= HTTP_STATE_WAITING
;
3698 * Force data_encoding and data_length to be set according to the response
3702 http_set_length(http
);
3704 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3706 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3707 "was %s.", httpStateString(http
->state
)));
3708 http
->state
= HTTP_STATE_WAITING
;
3712 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3717 * Then start any content encoding...
3720 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3721 http_content_coding_start(http
,
3722 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3723 #endif /* HAVE_LIBZ */
3733 * 'http_content_coding_finish()' - Finish doing any content encoding.
3737 http_content_coding_finish(
3738 http_t
*http
) /* I - HTTP connection */
3740 int zerr
; /* Compression status */
3741 Byte dummy
[1]; /* Dummy read buffer */
3742 size_t bytes
; /* Number of bytes to write */
3745 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3746 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3748 switch (http
->coding
)
3750 case _HTTP_CODING_DEFLATE
:
3751 case _HTTP_CODING_GZIP
:
3752 http
->stream
.next_in
= dummy
;
3753 http
->stream
.avail_in
= 0;
3757 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3758 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3762 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3764 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3765 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3767 http_write(http
, (char *)http
->sbuffer
, bytes
);
3770 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3771 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3773 while (zerr
== Z_OK
);
3775 deflateEnd(&(http
->stream
));
3777 free(http
->sbuffer
);
3778 http
->sbuffer
= NULL
;
3781 httpFlushWrite(http
);
3784 case _HTTP_CODING_INFLATE
:
3785 case _HTTP_CODING_GUNZIP
:
3786 inflateEnd(&(http
->stream
));
3787 free(http
->sbuffer
);
3788 http
->sbuffer
= NULL
;
3795 http
->coding
= _HTTP_CODING_IDENTITY
;
3800 * 'http_content_coding_start()' - Start doing content encoding.
3804 http_content_coding_start(
3805 http_t
*http
, /* I - HTTP connection */
3806 const char *value
) /* I - Value of Content-Encoding */
3808 int zerr
; /* Error/status */
3809 _http_coding_t coding
; /* Content coding value */
3812 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3814 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3816 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3820 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3822 if (http
->state
== HTTP_STATE_GET_SEND
||
3823 http
->state
== HTTP_STATE_POST_SEND
)
3824 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3825 _HTTP_CODING_GUNZIP
;
3826 else if (http
->state
== HTTP_STATE_POST_RECV
||
3827 http
->state
== HTTP_STATE_PUT_RECV
)
3828 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3829 _HTTP_CODING_GUNZIP
;
3832 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3836 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3838 if (http
->state
== HTTP_STATE_GET_SEND
||
3839 http
->state
== HTTP_STATE_POST_SEND
)
3840 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3841 _HTTP_CODING_INFLATE
;
3842 else if (http
->state
== HTTP_STATE_POST_RECV
||
3843 http
->state
== HTTP_STATE_PUT_RECV
)
3844 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3845 _HTTP_CODING_INFLATE
;
3848 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3854 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3858 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3862 case _HTTP_CODING_DEFLATE
:
3863 case _HTTP_CODING_GZIP
:
3865 httpFlushWrite(http
);
3867 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3869 http
->status
= HTTP_STATUS_ERROR
;
3870 http
->error
= errno
;
3875 * Window size for compression is 11 bits - optimal based on PWG Raster
3876 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3880 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3882 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3883 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3885 http
->status
= HTTP_STATUS_ERROR
;
3886 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3890 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3891 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3894 case _HTTP_CODING_INFLATE
:
3895 case _HTTP_CODING_GUNZIP
:
3896 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3898 http
->status
= HTTP_STATUS_ERROR
;
3899 http
->error
= errno
;
3904 * Window size for decompression is up to 15 bits (maximum supported).
3905 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3908 if ((zerr
= inflateInit2(&(http
->stream
),
3909 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3912 free(http
->sbuffer
);
3913 http
->sbuffer
= NULL
;
3914 http
->status
= HTTP_STATUS_ERROR
;
3915 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3919 http
->stream
.avail_in
= 0;
3920 http
->stream
.next_in
= http
->sbuffer
;
3927 http
->coding
= coding
;
3929 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3932 #endif /* HAVE_LIBZ */
3936 * 'http_create()' - Create an unconnected HTTP connection.
3939 static http_t
* /* O - HTTP connection */
3941 const char *host
, /* I - Hostname */
3942 int port
, /* I - Port number */
3943 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3944 int family
, /* I - Address family or AF_UNSPEC */
3945 http_encryption_t encryption
, /* I - Encryption to use */
3946 int blocking
, /* I - 1 for blocking mode */
3947 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3949 http_t
*http
; /* New HTTP connection */
3950 char service
[255]; /* Service name */
3951 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3954 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
));
3956 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3962 * Lookup the host...
3967 myaddrlist
= httpAddrCopyList(addrlist
);
3971 snprintf(service
, sizeof(service
), "%d", port
);
3973 myaddrlist
= httpAddrGetList(host
, family
, service
);
3980 * Allocate memory for the structure...
3983 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3985 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3986 httpAddrFreeList(addrlist
);
3991 * Initialize the HTTP data...
3995 http
->activity
= time(NULL
);
3996 http
->addrlist
= myaddrlist
;
3997 http
->blocking
= blocking
;
4000 http
->gssctx
= GSS_C_NO_CONTEXT
;
4001 http
->gssname
= GSS_C_NO_NAME
;
4002 #endif /* HAVE_GSSAPI */
4003 http
->status
= HTTP_STATUS_CONTINUE
;
4004 http
->version
= HTTP_VERSION_1_1
;
4007 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
4009 if (port
== 443) /* Always use encryption for https */
4010 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
4012 http
->encryption
= encryption
;
4014 http_set_wait(http
);
4017 * Return the new structure...
4026 * 'http_debug_hex()' - Do a hex dump of a buffer.
4030 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4031 const char *buffer
, /* I - Buffer to dump */
4032 int bytes
) /* I - Bytes to dump */
4034 int i
, j
, /* Looping vars */
4035 ch
; /* Current character */
4036 char line
[255], /* Line buffer */
4037 *start
, /* Start of line after prefix */
4038 *ptr
; /* Pointer into line */
4041 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4044 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4046 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4047 start
= line
+ strlen(line
);
4049 for (i
= 0; i
< bytes
; i
+= 16)
4051 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4052 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4056 memcpy(ptr
, " ", 3);
4061 memcpy(ptr
, " ", 3);
4064 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4066 ch
= buffer
[i
+ j
] & 255;
4068 if (ch
< ' ' || ch
>= 127)
4082 * 'http_read()' - Read a buffer from a HTTP connection.
4084 * This function does the low-level read from the socket, retrying and timing
4088 static ssize_t
/* O - Number of bytes read or -1 on error */
4089 http_read(http_t
*http
, /* I - HTTP connection */
4090 char *buffer
, /* I - Buffer */
4091 size_t length
) /* I - Maximum bytes to read */
4093 ssize_t bytes
; /* Bytes read */
4096 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4098 if (!http
->blocking
)
4100 while (!httpWait(http
, http
->wait_value
))
4102 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4105 DEBUG_puts("2http_read: Timeout.");
4110 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4116 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4118 #endif /* HAVE_SSL */
4119 bytes
= recv(http
->fd
, buffer
, length
, 0);
4124 if (WSAGetLastError() != WSAEINTR
)
4126 http
->error
= WSAGetLastError();
4129 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4131 if (!http
->timeout_cb
||
4132 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4134 http
->error
= WSAEWOULDBLOCK
;
4139 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4141 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4143 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4145 http
->error
= errno
;
4148 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4150 http
->error
= errno
;
4154 else if (errno
!= EINTR
)
4156 http
->error
= errno
;
4164 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4165 CUPS_LLCAST bytes
));
4168 http_debug_hex("http_read", buffer
, (int)bytes
);
4174 if (WSAGetLastError() == WSAEINTR
)
4177 http
->error
= WSAGetLastError();
4179 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4182 http
->error
= errno
;
4185 else if (bytes
== 0)
4187 http
->error
= EPIPE
;
4196 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4198 * This function reads data from the HTTP buffer or from the socket, as needed.
4201 static ssize_t
/* O - Number of bytes read or -1 on error */
4202 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4203 char *buffer
, /* I - Buffer */
4204 size_t length
) /* I - Maximum bytes to read */
4206 ssize_t bytes
; /* Bytes read */
4209 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4213 if (length
> (size_t)http
->used
)
4214 bytes
= (ssize_t
)http
->used
;
4216 bytes
= (ssize_t
)length
;
4218 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4221 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4222 http
->used
-= (int)bytes
;
4225 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4228 bytes
= http_read(http
, buffer
, length
);
4235 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4237 * This function reads and validates the chunk length, then does a buffered read
4238 * returning the number of bytes placed in the buffer.
4241 static ssize_t
/* O - Number of bytes read or -1 on error */
4242 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4243 char *buffer
, /* I - Buffer */
4244 size_t length
) /* I - Maximum bytes to read */
4246 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4248 if (http
->data_remaining
<= 0)
4250 char len
[32]; /* Length string */
4252 if (!httpGets(len
, sizeof(len
), http
))
4254 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4260 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4261 if (!httpGets(len
, sizeof(len
), http
))
4263 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4268 http
->data_remaining
= strtoll(len
, NULL
, 16);
4270 if (http
->data_remaining
< 0)
4272 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4273 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4277 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4278 len
, CUPS_LLCAST http
->data_remaining
));
4280 if (http
->data_remaining
== 0)
4283 * 0-length chunk, grab trailing blank line...
4286 httpGets(len
, sizeof(len
), http
);
4290 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4291 CUPS_LLCAST http
->data_remaining
));
4293 if (http
->data_remaining
<= 0)
4295 else if (length
> (size_t)http
->data_remaining
)
4296 length
= (size_t)http
->data_remaining
;
4298 return (http_read_buffered(http
, buffer
, length
));
4303 * 'http_send()' - Send a request with all fields and the trailing blank line.
4306 static int /* O - 0 on success, non-zero on error */
4307 http_send(http_t
*http
, /* I - HTTP connection */
4308 http_state_t request
, /* I - Request code */
4309 const char *uri
) /* I - URI */
4311 int i
; /* Looping var */
4312 char buf
[1024]; /* Encoded URI buffer */
4313 const char *value
; /* Field value */
4314 static const char * const codes
[] = /* Request code strings */
4334 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4336 if (http
== NULL
|| uri
== NULL
)
4340 * Set the User-Agent field if it isn't already...
4343 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4345 if (http
->default_user_agent
)
4346 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4348 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4352 * Set the Accept-Encoding field if it isn't already...
4355 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4356 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4357 http
->default_accept_encoding
);
4360 * Encode the URI as needed...
4363 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4366 * See if we had an error the last time around; if so, reconnect...
4369 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4370 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4372 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4373 http
->fd
, http
->status
, http
->tls_upgrade
));
4375 if (httpReconnect2(http
, 30000, NULL
))
4380 * Flush any written data that is pending...
4385 if (httpFlushWrite(http
) < 0)
4386 if (httpReconnect2(http
, 30000, NULL
))
4391 * Send the request header...
4394 http
->state
= request
;
4395 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4397 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4400 http
->status
= HTTP_STATUS_CONTINUE
;
4403 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4405 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4406 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4408 #endif /* HAVE_SSL */
4410 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4412 http
->status
= HTTP_STATUS_ERROR
;
4416 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4417 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4419 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4421 if (i
== HTTP_FIELD_HOST
)
4423 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4424 httpAddrPort(http
->hostaddr
)) < 1)
4426 http
->status
= HTTP_STATUS_ERROR
;
4430 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4432 http
->status
= HTTP_STATUS_ERROR
;
4438 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4440 http
->status
= HTTP_STATUS_ERROR
;
4444 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4445 http
->mode
, http
->state
));
4447 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4448 (http
->state
== HTTP_STATE_POST_RECV
||
4449 http
->state
== HTTP_STATE_PUT_RECV
))
4450 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4452 http
->status
= HTTP_STATUS_ERROR
;
4456 if (httpPrintf(http
, "\r\n") < 1)
4458 http
->status
= HTTP_STATUS_ERROR
;
4462 if (httpFlushWrite(http
) < 0)
4465 http_set_length(http
);
4466 httpClearFields(http
);
4469 * The Kerberos and AuthRef authentication strings can only be used once...
4472 if (http
->field_authorization
&& http
->authstring
&&
4473 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4474 !strncmp(http
->authstring
, "AuthRef", 7)))
4476 http
->_authstring
[0] = '\0';
4478 if (http
->authstring
!= http
->_authstring
)
4479 free(http
->authstring
);
4481 http
->authstring
= http
->_authstring
;
4489 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4492 static off_t
/* O - Remainder or -1 on error */
4493 http_set_length(http_t
*http
) /* I - Connection */
4495 off_t remaining
; /* Remainder */
4498 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4500 if ((remaining
= httpGetLength2(http
)) >= 0)
4502 if (http
->mode
== _HTTP_MODE_SERVER
&&
4503 http
->state
!= HTTP_STATE_GET_SEND
&&
4504 http
->state
!= HTTP_STATE_PUT
&&
4505 http
->state
!= HTTP_STATE_POST
&&
4506 http
->state
!= HTTP_STATE_POST_SEND
)
4508 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4512 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4515 DEBUG_puts("1http_set_length: Setting data_encoding to "
4516 "HTTP_ENCODING_CHUNKED.");
4517 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4521 DEBUG_puts("1http_set_length: Setting data_encoding to "
4522 "HTTP_ENCODING_LENGTH.");
4523 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4526 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4527 CUPS_LLCAST remaining
));
4528 http
->data_remaining
= remaining
;
4530 if (remaining
<= INT_MAX
)
4531 http
->_data_remaining
= (int)remaining
;
4533 http
->_data_remaining
= INT_MAX
;
4540 * 'http_set_timeout()' - Set the socket timeout values.
4544 http_set_timeout(int fd
, /* I - File descriptor */
4545 double timeout
) /* I - Timeout in seconds */
4548 DWORD tv
= (DWORD
)(timeout
* 1000);
4549 /* Timeout in milliseconds */
4551 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4552 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4555 struct timeval tv
; /* Timeout in secs and usecs */
4557 tv
.tv_sec
= (int)timeout
;
4558 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4560 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4561 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4567 * 'http_set_wait()' - Set the default wait value for reads.
4571 http_set_wait(http_t
*http
) /* I - HTTP connection */
4575 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4577 if (http
->wait_value
<= 0)
4578 http
->wait_value
= 60000;
4581 http
->wait_value
= 10000;
4587 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4590 static int /* O - Status of connection */
4591 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4593 int ret
; /* Return value */
4594 http_t myhttp
; /* Local copy of HTTP data */
4597 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4600 * Flush the connection to make sure any previous "Upgrade" message
4607 * Copy the HTTP data to a local variable so we can do the OPTIONS
4608 * request without interfering with the existing request data...
4611 memcpy(&myhttp
, http
, sizeof(myhttp
));
4614 * Send an OPTIONS request to the server, requiring SSL or TLS
4615 * encryption on the link...
4618 http
->tls_upgrade
= 1;
4619 http
->field_authorization
= NULL
; /* Don't free the auth string */
4621 httpClearFields(http
);
4622 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4623 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4625 if ((ret
= httpOptions(http
, "*")) == 0)
4628 * Wait for the secure connection...
4631 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4635 * Restore the HTTP request data...
4638 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4639 http
->data_encoding
= myhttp
.data_encoding
;
4640 http
->data_remaining
= myhttp
.data_remaining
;
4641 http
->_data_remaining
= myhttp
._data_remaining
;
4642 http
->expect
= myhttp
.expect
;
4643 http
->field_authorization
= myhttp
.field_authorization
;
4644 http
->digest_tries
= myhttp
.digest_tries
;
4645 http
->tls_upgrade
= 0;
4648 * See if we actually went secure...
4654 * Server does not support HTTP upgrade...
4657 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4659 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4660 httpAddrClose(NULL
, http
->fd
);
4669 #endif /* HAVE_SSL */
4673 * 'http_write()' - Write a buffer to a HTTP connection.
4676 static ssize_t
/* O - Number of bytes written */
4677 http_write(http_t
*http
, /* I - HTTP connection */
4678 const char *buffer
, /* I - Buffer for data */
4679 size_t length
) /* I - Number of bytes to write */
4681 ssize_t tbytes
, /* Total bytes sent */
4682 bytes
; /* Bytes sent */
4685 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4691 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4693 if (http
->timeout_cb
)
4696 struct pollfd pfd
; /* Polled file descriptor */
4698 fd_set output_set
; /* Output ready for write? */
4699 struct timeval timeout
; /* Timeout value */
4700 #endif /* HAVE_POLL */
4701 int nfds
; /* Result from select()/poll() */
4707 pfd
.events
= POLLOUT
;
4709 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4710 (errno
== EINTR
|| errno
== EAGAIN
))
4716 FD_ZERO(&output_set
);
4717 FD_SET(http
->fd
, &output_set
);
4719 timeout
.tv_sec
= http
->wait_value
/ 1000;
4720 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4722 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4725 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4726 WSAGetLastError() == WSAEWOULDBLOCK
));
4728 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4730 #endif /* HAVE_POLL */
4734 http
->error
= errno
;
4737 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4740 http
->error
= WSAEWOULDBLOCK
;
4742 http
->error
= EWOULDBLOCK
;
4752 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4754 #endif /* HAVE_SSL */
4755 bytes
= send(http
->fd
, buffer
, length
, 0);
4757 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4758 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4763 if (WSAGetLastError() == WSAEINTR
)
4765 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4767 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4770 http
->error
= WSAGetLastError();
4772 else if (WSAGetLastError() != http
->error
&&
4773 WSAGetLastError() != WSAECONNRESET
)
4775 http
->error
= WSAGetLastError();
4782 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4784 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4786 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4789 http
->error
= errno
;
4791 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4793 http
->error
= errno
;
4798 DEBUG_printf(("3http_write: error writing data (%s).",
4799 strerror(http
->error
)));
4806 length
-= (size_t)bytes
;
4810 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4813 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4820 * 'http_write_chunk()' - Write a chunked buffer.
4823 static ssize_t
/* O - Number bytes written */
4824 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4825 const char *buffer
, /* I - Buffer to write */
4826 size_t length
) /* I - Length of buffer */
4828 char header
[16]; /* Chunk header */
4829 ssize_t bytes
; /* Bytes written */
4832 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4835 * Write the chunk header, data, and trailer.
4838 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4839 if (http_write(http
, header
, strlen(header
)) < 0)
4841 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4845 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4847 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4851 if (http_write(http
, "\r\n", 2) < 0)
4853 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");