2 * HTTP routines for CUPS.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Include necessary headers...
23 #include "cups-private.h"
30 # include <sys/time.h>
31 # include <sys/resource.h>
35 #endif /* HAVE_POLL */
42 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
44 static void http_content_coding_finish(http_t
*http
);
45 static void http_content_coding_start(http_t
*http
,
47 #endif /* HAVE_LIBZ */
48 static http_t
*http_create(const char *host
, int port
,
49 http_addrlist_t
*addrlist
, int family
,
50 http_encryption_t encryption
,
51 int blocking
, _http_mode_t mode
);
53 static void http_debug_hex(const char *prefix
, const char *buffer
,
56 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
57 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
58 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
59 static int http_send(http_t
*http
, http_state_t request
,
61 static ssize_t
http_write(http_t
*http
, const char *buffer
,
63 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
65 static off_t
http_set_length(http_t
*http
);
66 static void http_set_timeout(int fd
, double timeout
);
67 static void http_set_wait(http_t
*http
);
70 static int http_tls_upgrade(http_t
*http
);
78 static const char * const http_fields
[] =
95 "If-Unmodified-since",
114 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
115 * specified listening socket.
117 * @since CUPS 1.7/macOS 10.9@
120 http_t
* /* O - HTTP connection or @code NULL@ */
121 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
122 int blocking
) /* I - 1 if the connection should be
123 blocking, 0 otherwise */
125 http_t
*http
; /* HTTP connection */
126 http_addrlist_t addrlist
; /* Dummy address list */
127 socklen_t addrlen
; /* Length of address */
128 int val
; /* Socket option value */
132 * Range check input...
139 * Create the client connection...
142 memset(&addrlist
, 0, sizeof(addrlist
));
144 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
145 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
146 _HTTP_MODE_SERVER
)) == NULL
)
150 * Accept the client and get the remote address...
153 addrlen
= sizeof(http_addr_t
);
155 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
158 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
164 http
->hostaddr
= &(http
->addrlist
->addr
);
166 if (httpAddrLocalhost(http
->hostaddr
))
167 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
169 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
173 * Disable SIGPIPE for this socket.
177 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
178 #endif /* SO_NOSIGPIPE */
181 * Using TCP_NODELAY improves responsiveness, especially on systems
182 * with a slow loopback interface. Since we write large buffers
183 * when sending print files and requests, there shouldn't be any
184 * performance penalty for this...
188 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
192 * Close this socket when starting another process...
195 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
196 #endif /* FD_CLOEXEC */
203 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
205 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
207 * @since CUPS 1.5/macOS 10.7@
210 int /* O - 0 on success, -1 on error */
212 cups_array_t
*credentials
, /* I - Credentials array */
213 const void *data
, /* I - PEM-encoded X.509 data */
214 size_t datalen
) /* I - Length of data */
216 http_credential_t
*credential
; /* Credential data */
219 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
221 credential
->datalen
= datalen
;
223 if ((credential
->data
= malloc(datalen
)) != NULL
)
225 memcpy(credential
->data
, data
, datalen
);
226 cupsArrayAdd(credentials
, credential
);
238 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
242 httpBlocking(http_t
*http
, /* I - HTTP connection */
243 int b
) /* I - 1 = blocking, 0 = non-blocking */
254 * 'httpCheck()' - Check to see if there is a pending response from the server.
257 int /* O - 0 = no data, 1 = data available */
258 httpCheck(http_t
*http
) /* I - HTTP connection */
260 return (httpWait(http
, 0));
265 * 'httpClearCookie()' - Clear the cookie value(s).
267 * @since CUPS 1.1.19/macOS 10.3@
271 httpClearCookie(http_t
*http
) /* I - HTTP connection */
285 * 'httpClearFields()' - Clear HTTP request fields.
289 httpClearFields(http_t
*http
) /* I - HTTP connection */
291 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
295 memset(http
->fields
, 0, sizeof(http
->fields
));
297 if (http
->mode
== _HTTP_MODE_CLIENT
)
299 if (http
->hostname
[0] == '/')
300 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
302 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
305 if (http
->field_authorization
)
307 free(http
->field_authorization
);
308 http
->field_authorization
= NULL
;
311 if (http
->accept_encoding
)
313 _cupsStrFree(http
->accept_encoding
);
314 http
->accept_encoding
= NULL
;
319 _cupsStrFree(http
->allow
);
325 _cupsStrFree(http
->server
);
329 http
->expect
= (http_status_t
)0;
335 * 'httpClose()' - Close an HTTP connection.
339 httpClose(http_t
*http
) /* I - HTTP connection */
342 OM_uint32 minor_status
; /* Minor status code */
343 #endif /* HAVE_GSSAPI */
346 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
349 * Range check input...
356 * Close any open connection...
359 _httpDisconnect(http
);
362 * Free memory used...
365 httpAddrFreeList(http
->addrlist
);
371 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
372 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
374 if (http
->gssname
!= GSS_C_NO_NAME
)
375 gss_release_name(&minor_status
, &http
->gssname
);
376 #endif /* HAVE_GSSAPI */
378 #ifdef HAVE_AUTHORIZATION_H
380 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
381 #endif /* HAVE_AUTHORIZATION_H */
383 httpClearFields(http
);
385 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
386 free(http
->authstring
);
393 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
395 * @since CUPS 2.0/OS 10.10@
398 int /* O - 1 if they match, 0 if they do not */
399 httpCompareCredentials(
400 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
401 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
403 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
406 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
))
407 if (temp1
->datalen
!= temp2
->datalen
)
409 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
412 return (temp1
== temp2
);
417 * 'httpConnect()' - Connect to a HTTP server.
419 * This function is deprecated - use @link httpConnect2@ instead.
421 * @deprecated@ @exclude all@
424 http_t
* /* O - New HTTP connection */
425 httpConnect(const char *host
, /* I - Host to connect to */
426 int port
) /* I - Port number */
428 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
429 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
434 * 'httpConnect2()' - Connect to a HTTP server.
436 * @since CUPS 1.7/macOS 10.9@
439 http_t
* /* O - New HTTP connection */
441 const char *host
, /* I - Host to connect to */
442 int port
, /* I - Port number */
443 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
444 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
445 http_encryption_t encryption
, /* I - Type of encryption to use */
446 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
447 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
448 int *cancel
) /* I - Pointer to "cancel" variable */
450 http_t
*http
; /* New HTTP connection */
453 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
));
456 * Create the HTTP structure...
459 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
460 _HTTP_MODE_CLIENT
)) == NULL
)
464 * Optionally connect to the remote system...
467 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
471 * Could not connect to any known address - bail out!
481 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
483 * This function is now deprecated. Please use the @link httpConnect2@ function
486 * @deprecated@ @exclude all@
489 http_t
* /* O - New HTTP connection */
491 const char *host
, /* I - Host to connect to */
492 int port
, /* I - Port number */
493 http_encryption_t encryption
) /* I - Type of encryption to use */
495 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
496 host
, port
, encryption
));
498 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
504 * 'httpDelete()' - Send a DELETE request to the server.
507 int /* O - Status of call (0 = success) */
508 httpDelete(http_t
*http
, /* I - HTTP connection */
509 const char *uri
) /* I - URI to delete */
511 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
516 * '_httpDisconnect()' - Disconnect a HTTP connection.
520 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
525 #endif /* HAVE_SSL */
527 httpAddrClose(NULL
, http
->fd
);
534 * 'httpEncryption()' - Set the required encryption on the link.
537 int /* O - -1 on error, 0 on success */
538 httpEncryption(http_t
*http
, /* I - HTTP connection */
539 http_encryption_t e
) /* I - New encryption preference */
541 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
547 if (http
->mode
== _HTTP_MODE_CLIENT
)
549 http
->encryption
= e
;
551 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
552 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
553 return (httpReconnect2(http
, 30000, NULL
));
554 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
555 return (http_tls_upgrade(http
));
561 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
564 http
->encryption
= e
;
565 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
566 return (_httpTLSStart(http
));
571 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
575 #endif /* HAVE_SSL */
580 * 'httpError()' - Get the last error on a connection.
583 int /* O - Error code (errno) value */
584 httpError(http_t
*http
) /* I - HTTP connection */
587 return (http
->error
);
594 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
598 http_field_t
/* O - Field index */
599 httpFieldValue(const char *name
) /* I - String name */
601 int i
; /* Looping var */
604 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
605 if (!_cups_strcasecmp(name
, http_fields
[i
]))
606 return ((http_field_t
)i
);
608 return (HTTP_FIELD_UNKNOWN
);
613 * 'httpFlush()' - Flush data read from a HTTP connection.
617 httpFlush(http_t
*http
) /* I - HTTP connection */
619 char buffer
[8192]; /* Junk buffer */
620 int blocking
; /* To block or not to block */
621 http_state_t oldstate
; /* Old state */
624 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
627 * Nothing to do if we are in the "waiting" state...
630 if (http
->state
== HTTP_STATE_WAITING
)
634 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
637 blocking
= http
->blocking
;
641 * Read any data we can...
644 oldstate
= http
->state
;
645 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
648 * Restore blocking and reset the connection if we didn't get all of
649 * the remaining data...
652 http
->blocking
= blocking
;
654 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
658 * Didn't get the data back, so close the current connection.
663 http_content_coding_finish(http
);
664 #endif /* HAVE_LIBZ */
666 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
668 http
->state
= HTTP_STATE_WAITING
;
673 #endif /* HAVE_SSL */
675 httpAddrClose(NULL
, http
->fd
);
683 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
685 * @since CUPS 1.2/macOS 10.5@
688 int /* O - Bytes written or -1 on error */
689 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
691 ssize_t bytes
; /* Bytes written */
694 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
696 if (!http
|| !http
->wused
)
698 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
699 "1httpFlushWrite: No connection.");
703 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
704 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
706 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
710 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
717 * 'httpFreeCredentials()' - Free an array of credentials.
722 cups_array_t
*credentials
) /* I - Array of credentials */
724 http_credential_t
*credential
; /* Credential */
727 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
729 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
731 cupsArrayRemove(credentials
, credential
);
732 free((void *)credential
->data
);
736 cupsArrayDelete(credentials
);
741 * 'httpGet()' - Send a GET request to the server.
744 int /* O - Status of call (0 = success) */
745 httpGet(http_t
*http
, /* I - HTTP connection */
746 const char *uri
) /* I - URI to get */
748 return (http_send(http
, HTTP_STATE_GET
, uri
));
753 * 'httpGetActivity()' - Get the most recent activity for a connection.
755 * The return value is the time in seconds of the last read or write.
757 * @since CUPS 2.0/OS 10.10@
760 time_t /* O - Time of last read or write */
761 httpGetActivity(http_t
*http
) /* I - HTTP connection */
763 return (http
? http
->activity
: 0);
768 * 'httpGetAuthString()' - Get the current authorization string.
770 * The authorization string is set by @link cupsDoAuthentication@ and
771 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
772 * string to use with @link httpSetField@ for the
773 * @code HTTP_FIELD_AUTHORIZATION@ value.
775 * @since CUPS 1.3/macOS 10.5@
778 char * /* O - Authorization string */
779 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
782 return (http
->authstring
);
789 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
791 * @since CUPS 1.2/macOS 10.5@
794 int /* O - 1 if blocking, 0 if non-blocking */
795 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
797 return (http
? http
->blocking
: 0);
802 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
803 * the client and server.
805 * This function uses the value of the Accepts-Encoding HTTP header and must be
806 * called after receiving a response from the server or a request from the
807 * client. The value returned can be use in subsequent requests (for clients)
808 * or in the response (for servers) in order to compress the content stream.
810 * @since CUPS 1.7/macOS 10.9@
813 const char * /* O - Content-Coding value or
814 @code NULL@ for the identity
816 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
819 if (http
&& http
->accept_encoding
)
821 int i
; /* Looping var */
822 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
823 *start
, /* Start of coding value */
824 *end
; /* End of coding value */
825 double qvalue
; /* "qvalue" for coding */
826 struct lconv
*loc
= localeconv(); /* Locale data */
827 static const char * const codings
[] =
828 { /* Supported content codings */
835 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
837 for (start
= temp
; *start
; start
= end
)
840 * Find the end of the coding name...
845 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
851 * Grab the qvalue as needed...
854 if (!strncmp(end
, ";q=", 3))
855 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
858 * Skip past all attributes...
862 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
868 while (*end
&& isspace(*end
& 255))
872 * Check value if it matches something we support...
878 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
879 if (!strcmp(start
, codings
[i
]))
883 #endif /* HAVE_LIBZ */
890 * 'httpGetCookie()' - Get any cookie data from the response.
892 * @since CUPS 1.1.19/macOS 10.3@
895 const char * /* O - Cookie data or @code NULL@ */
896 httpGetCookie(http_t
*http
) /* I - HTTP connection */
898 return (http
? http
->cookie
: NULL
);
903 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
905 * This function returns the encryption mode for the connection. Use the
906 * @link httpIsEncrypted@ function to determine whether a TLS session has
909 * @since CUPS 2.0/OS 10.10@
912 http_encryption_t
/* O - Current encryption mode */
913 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
915 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
920 * 'httpGetExpect()' - Get the value of the Expect header, if any.
922 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
923 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
925 * @since CUPS 1.7/macOS 10.9@
928 http_status_t
/* O - Expect: status, if any */
929 httpGetExpect(http_t
*http
) /* I - HTTP connection */
932 return (HTTP_STATUS_ERROR
);
934 return (http
->expect
);
939 * 'httpGetFd()' - Get the file descriptor associated with a connection.
941 * @since CUPS 1.2/macOS 10.5@
944 int /* O - File descriptor or -1 if none */
945 httpGetFd(http_t
*http
) /* I - HTTP connection */
947 return (http
? http
->fd
: -1);
952 * 'httpGetField()' - Get a field value from a request/response.
955 const char * /* O - Field value */
956 httpGetField(http_t
*http
, /* I - HTTP connection */
957 http_field_t field
) /* I - Field to get */
959 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
964 case HTTP_FIELD_ACCEPT_ENCODING
:
965 return (http
->accept_encoding
);
967 case HTTP_FIELD_ALLOW
:
968 return (http
->allow
);
970 case HTTP_FIELD_SERVER
:
971 return (http
->server
);
973 case HTTP_FIELD_AUTHORIZATION
:
974 if (http
->field_authorization
)
977 * Special case for WWW-Authenticate: as its contents can be
978 * longer than HTTP_MAX_VALUE...
981 return (http
->field_authorization
);
985 return (http
->fields
[field
]);
991 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
993 * @since CUPS 2.0/OS 10.10@
996 http_keepalive_t
/* O - Keep-Alive state */
997 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
999 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1004 * 'httpGetLength()' - Get the amount of data remaining from the
1005 * content-length or transfer-encoding fields.
1007 * This function is deprecated and will not return lengths larger than
1008 * 2^31 - 1; use httpGetLength2() instead.
1010 * @deprecated@ @exclude all@
1013 int /* O - Content length */
1014 httpGetLength(http_t
*http
) /* I - HTTP connection */
1017 * Get the read content length and return the 32-bit value.
1022 httpGetLength2(http
);
1024 return (http
->_data_remaining
);
1032 * 'httpGetLength2()' - Get the amount of data remaining from the
1033 * content-length or transfer-encoding fields.
1035 * This function returns the complete content length, even for
1036 * content larger than 2^31 - 1.
1038 * @since CUPS 1.2/macOS 10.5@
1041 off_t
/* O - Content length */
1042 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1044 off_t remaining
; /* Remaining length */
1047 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1052 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1054 DEBUG_puts("4httpGetLength2: chunked request!");
1060 * The following is a hack for HTTP servers that don't send a
1061 * Content-Length or Transfer-Encoding field...
1063 * If there is no Content-Length then the connection must close
1064 * after the transfer is complete...
1067 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1070 * Default content length is 0 for errors and certain types of operations,
1071 * and 2^31-1 for other successful requests...
1074 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1075 http
->state
== HTTP_STATE_OPTIONS
||
1076 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1077 http
->state
== HTTP_STATE_HEAD
||
1078 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1079 http
->state
== HTTP_STATE_DELETE
||
1080 http
->state
== HTTP_STATE_TRACE
||
1081 http
->state
== HTTP_STATE_CONNECT
)
1084 remaining
= 2147483647;
1086 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1090 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1091 CUPS_LLCAST remaining
));
1099 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1101 * @since CUPS 2.0/OS 10.10@
1104 size_t /* O - Number of bytes buffered */
1105 httpGetPending(http_t
*http
) /* I - HTTP connection */
1107 return (http
? (size_t)http
->wused
: 0);
1112 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1114 * @since CUPS 2.0/OS 10.10@
1117 size_t /* O - Number of bytes available */
1118 httpGetReady(http_t
*http
) /* I - HTTP connection */
1122 else if (http
->used
> 0)
1123 return ((size_t)http
->used
);
1126 return (_httpTLSPending(http
));
1127 #endif /* HAVE_SSL */
1134 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1135 * body or current chunk.
1137 * The @link httpIsChunked@ function can be used to determine whether the
1138 * message body is chunked or fixed-length.
1140 * @since CUPS 2.0/OS 10.10@
1143 size_t /* O - Remaining bytes */
1144 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1146 return (http
? (size_t)http
->data_remaining
: 0);
1151 * 'httpGets()' - Get a line of text from a HTTP connection.
1154 char * /* O - Line or @code NULL@ */
1155 httpGets(char *line
, /* I - Line to read into */
1156 int length
, /* I - Max length of buffer */
1157 http_t
*http
) /* I - HTTP connection */
1159 char *lineptr
, /* Pointer into line */
1160 *lineend
, /* End of line */
1161 *bufptr
, /* Pointer into input buffer */
1162 *bufend
; /* Pointer to end of buffer */
1163 ssize_t bytes
; /* Number of bytes read */
1164 int eol
; /* End-of-line? */
1167 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1169 if (!http
|| !line
|| length
<= 1)
1173 * Read a line from the buffer...
1178 lineend
= line
+ length
- 1;
1181 while (lineptr
< lineend
)
1184 * Pre-load the buffer as needed...
1193 while (http
->used
== 0)
1196 * No newline; see if there is more data to be read...
1199 while (!_httpWait(http
, http
->wait_value
, 1))
1201 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1204 DEBUG_puts("3httpGets: Timed out!");
1206 http
->error
= WSAETIMEDOUT
;
1208 http
->error
= ETIMEDOUT
;
1213 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1215 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1220 * Nope, can't get a line this time...
1224 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1226 if (WSAGetLastError() == WSAEINTR
)
1228 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1230 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1233 http
->error
= WSAGetLastError();
1235 else if (WSAGetLastError() != http
->error
)
1237 http
->error
= WSAGetLastError();
1242 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1246 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1248 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1250 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1253 http
->error
= errno
;
1255 else if (errno
!= http
->error
)
1257 http
->error
= errno
;
1264 else if (bytes
== 0)
1266 http
->error
= EPIPE
;
1272 * Yup, update the amount used...
1275 http
->used
+= (int)bytes
;
1279 * Now copy as much of the current line as possible...
1282 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1283 lineptr
< lineend
&& bufptr
< bufend
;)
1285 if (*bufptr
== 0x0a)
1291 else if (*bufptr
== 0x0d)
1294 *lineptr
++ = *bufptr
++;
1297 http
->used
-= (int)(bufptr
- http
->buffer
);
1299 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1307 http
->activity
= time(NULL
);
1311 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1317 DEBUG_puts("3httpGets: No new line available!");
1324 * 'httpGetState()' - Get the current state of the HTTP request.
1327 http_state_t
/* O - HTTP state */
1328 httpGetState(http_t
*http
) /* I - HTTP connection */
1330 return (http
? http
->state
: HTTP_STATE_ERROR
);
1335 * 'httpGetStatus()' - Get the status of the last HTTP request.
1337 * @since CUPS 1.2/macOS 10.5@
1340 http_status_t
/* O - HTTP status */
1341 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1343 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1348 * 'httpGetSubField()' - Get a sub-field value.
1350 * @deprecated@ @exclude all@
1353 char * /* O - Value or @code NULL@ */
1354 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1355 http_field_t field
, /* I - Field index */
1356 const char *name
, /* I - Name of sub-field */
1357 char *value
) /* O - Value string */
1359 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1364 * 'httpGetSubField2()' - Get a sub-field value.
1366 * @since CUPS 1.2/macOS 10.5@
1369 char * /* O - Value or @code NULL@ */
1370 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1371 http_field_t field
, /* I - Field index */
1372 const char *name
, /* I - Name of sub-field */
1373 char *value
, /* O - Value string */
1374 int valuelen
) /* I - Size of value buffer */
1376 const char *fptr
; /* Pointer into field */
1377 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1378 *ptr
, /* Pointer into string buffer */
1379 *end
; /* End of value buffer */
1381 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1383 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1384 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1387 end
= value
+ valuelen
- 1;
1389 for (fptr
= http
->fields
[field
]; *fptr
;)
1392 * Skip leading whitespace...
1395 while (_cups_isspace(*fptr
))
1405 * Get the sub-field name...
1409 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1410 ptr
< (temp
+ sizeof(temp
) - 1);
1415 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1418 * Skip trailing chars up to the '='...
1421 while (_cups_isspace(*fptr
))
1431 * Skip = and leading whitespace...
1436 while (_cups_isspace(*fptr
))
1442 * Read quoted string...
1445 for (ptr
= value
, fptr
++;
1446 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1451 while (*fptr
&& *fptr
!= '\"')
1460 * Read unquoted string...
1464 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1469 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1473 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1476 * See if this is the one...
1479 if (!strcmp(name
, temp
))
1481 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1488 DEBUG_puts("3httpGetSubField2: Returning NULL");
1495 * 'httpGetVersion()' - Get the HTTP version at the other end.
1498 http_version_t
/* O - Version number */
1499 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1501 return (http
? http
->version
: HTTP_VERSION_1_0
);
1506 * 'httpHead()' - Send a HEAD request to the server.
1509 int /* O - Status of call (0 = success) */
1510 httpHead(http_t
*http
, /* I - HTTP connection */
1511 const char *uri
) /* I - URI for head */
1513 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1514 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1519 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1520 * default HTTP proxy (if any).
1524 httpInitialize(void)
1526 static int initialized
= 0; /* Have we been called before? */
1528 WSADATA winsockdata
; /* WinSock data */
1535 _cupsGlobalUnlock();
1540 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1542 #elif !defined(SO_NOSIGPIPE)
1544 * Ignore SIGPIPE signals...
1548 sigset(SIGPIPE
, SIG_IGN
);
1550 # elif defined(HAVE_SIGACTION)
1551 struct sigaction action
; /* POSIX sigaction data */
1554 memset(&action
, 0, sizeof(action
));
1555 action
.sa_handler
= SIG_IGN
;
1556 sigaction(SIGPIPE
, &action
, NULL
);
1559 signal(SIGPIPE
, SIG_IGN
);
1560 # endif /* !SO_NOSIGPIPE */
1564 _httpTLSInitialize();
1565 # endif /* HAVE_SSL */
1568 _cupsGlobalUnlock();
1573 * 'httpIsChunked()' - Report whether a message body is chunked.
1575 * This function returns non-zero if the message body is composed of
1576 * variable-length chunks.
1578 * @since CUPS 2.0/OS 10.10@
1581 int /* O - 1 if chunked, 0 if not */
1582 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1584 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1589 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1591 * This function returns non-zero if the connection is currently encrypted.
1593 * @since CUPS 2.0/OS 10.10@
1596 int /* O - 1 if encrypted, 0 if not */
1597 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1599 return (http
? http
->tls
!= NULL
: 0);
1604 * 'httpOptions()' - Send an OPTIONS request to the server.
1607 int /* O - Status of call (0 = success) */
1608 httpOptions(http_t
*http
, /* I - HTTP connection */
1609 const char *uri
) /* I - URI for options */
1611 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1616 * 'httpPeek()' - Peek at data from a HTTP connection.
1618 * This function copies available data from the given HTTP connection, reading
1619 * a buffer as needed. The data is still available for reading using
1622 * For non-blocking connections the usual timeouts apply.
1624 * @since CUPS 1.7/macOS 10.9@
1627 ssize_t
/* O - Number of bytes copied */
1628 httpPeek(http_t
*http
, /* I - HTTP connection */
1629 char *buffer
, /* I - Buffer for data */
1630 size_t length
) /* I - Maximum number of bytes */
1632 ssize_t bytes
; /* Bytes read */
1633 char len
[32]; /* Length string */
1636 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1638 if (http
== NULL
|| buffer
== NULL
)
1641 http
->activity
= time(NULL
);
1647 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1648 http
->data_remaining
<= 0)
1650 DEBUG_puts("2httpPeek: Getting chunk length...");
1652 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1654 DEBUG_puts("1httpPeek: Could not get length!");
1660 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1661 if (!httpGets(len
, sizeof(len
), http
))
1663 DEBUG_puts("1httpPeek: Could not get chunk length.");
1668 http
->data_remaining
= strtoll(len
, NULL
, 16);
1670 if (http
->data_remaining
< 0)
1672 DEBUG_puts("1httpPeek: Negative chunk length!");
1677 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1678 CUPS_LLCAST http
->data_remaining
));
1680 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1683 * A zero-length chunk ends a transfer; unless we are reading POST
1688 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1689 http_content_coding_finish(http
);
1690 #endif /* HAVE_LIBZ */
1692 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1693 httpGets(len
, sizeof(len
), http
);
1695 if (http
->state
== HTTP_STATE_POST_RECV
)
1698 http
->state
= HTTP_STATE_STATUS
;
1700 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1701 httpStateString(http
->state
)));
1704 * Prevent future reads for this request...
1707 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1711 else if (length
> (size_t)http
->data_remaining
)
1712 length
= (size_t)http
->data_remaining
;
1715 if (http
->used
== 0 &&
1716 (http
->coding
== _HTTP_CODING_IDENTITY
||
1717 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1719 if (http
->used
== 0)
1720 #endif /* HAVE_LIBZ */
1723 * Buffer small reads for better performance...
1726 ssize_t buflen
; /* Length of read for buffer */
1728 if (!http
->blocking
)
1730 while (!httpWait(http
, http
->wait_value
))
1732 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1739 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1740 buflen
= sizeof(http
->buffer
);
1742 buflen
= (ssize_t
)http
->data_remaining
;
1744 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1745 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1747 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1748 CUPS_LLCAST bytes
));
1752 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1755 http
->used
= (int)bytes
;
1760 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1762 # ifdef HAVE_INFLATECOPY
1763 int zerr
; /* Decompressor error */
1764 z_stream stream
; /* Copy of decompressor stream */
1766 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1768 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1769 /* Number of bytes to copy */
1771 if (http
->stream
.avail_in
> 0 &&
1772 http
->stream
.next_in
> http
->sbuffer
)
1773 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1775 http
->stream
.next_in
= http
->sbuffer
;
1777 if (buflen
> (size_t)http
->data_remaining
)
1778 buflen
= (size_t)http
->data_remaining
;
1780 if (buflen
> (size_t)http
->used
)
1781 buflen
= (size_t)http
->used
;
1783 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1784 "decompression buffer.", (int)buflen
));
1786 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1787 http
->stream
.avail_in
+= buflen
;
1788 http
->used
-= (int)buflen
;
1789 http
->data_remaining
-= (off_t
)buflen
;
1792 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1795 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1796 (int)http
->stream
.avail_in
));
1798 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1800 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1801 http
->error
= ENOMEM
;
1805 stream
.next_out
= (Bytef
*)buffer
;
1806 stream
.avail_out
= (uInt
)length
;
1808 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1809 inflateEnd(&stream
);
1813 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1815 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1822 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1825 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1826 "work with compressed streams.");
1828 # endif /* HAVE_INFLATECOPY */
1831 #endif /* HAVE_LIBZ */
1834 if (length
> (size_t)http
->used
)
1835 length
= (size_t)http
->used
;
1837 bytes
= (ssize_t
)length
;
1839 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1842 memcpy(buffer
, http
->buffer
, length
);
1850 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1853 http
->error
= WSAGetLastError();
1855 if (errno
== EINTR
|| errno
== EAGAIN
)
1858 http
->error
= errno
;
1861 else if (bytes
== 0)
1863 http
->error
= EPIPE
;
1872 * 'httpPost()' - Send a POST request to the server.
1875 int /* O - Status of call (0 = success) */
1876 httpPost(http_t
*http
, /* I - HTTP connection */
1877 const char *uri
) /* I - URI for post */
1879 return (http_send(http
, HTTP_STATE_POST
, uri
));
1884 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1889 int /* O - Number of bytes written */
1890 httpPrintf(http_t
*http
, /* I - HTTP connection */
1891 const char *format
, /* I - printf-style format string */
1892 ...) /* I - Additional args as needed */
1894 ssize_t bytes
; /* Number of bytes to write */
1895 char buf
[16384]; /* Buffer for formatted string */
1896 va_list ap
; /* Variable argument pointer */
1899 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1901 va_start(ap
, format
);
1902 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1905 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1907 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1908 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1913 DEBUG_puts("4httpPrintf: flushing existing data...");
1915 if (httpFlushWrite(http
) < 0)
1919 return ((int)http_write(http
, buf
, (size_t)bytes
));
1925 * 'httpPut()' - Send a PUT request to the server.
1928 int /* O - Status of call (0 = success) */
1929 httpPut(http_t
*http
, /* I - HTTP connection */
1930 const char *uri
) /* I - URI to put */
1932 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1933 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1938 * 'httpRead()' - Read data from a HTTP connection.
1940 * This function is deprecated. Use the httpRead2() function which can
1941 * read more than 2GB of data.
1943 * @deprecated@ @exclude all@
1946 int /* O - Number of bytes read */
1947 httpRead(http_t
*http
, /* I - HTTP connection */
1948 char *buffer
, /* I - Buffer for data */
1949 int length
) /* I - Maximum number of bytes */
1951 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1956 * 'httpRead2()' - Read data from a HTTP connection.
1958 * @since CUPS 1.2/macOS 10.5@
1961 ssize_t
/* O - Number of bytes read */
1962 httpRead2(http_t
*http
, /* I - HTTP connection */
1963 char *buffer
, /* I - Buffer for data */
1964 size_t length
) /* I - Maximum number of bytes */
1966 ssize_t bytes
; /* Bytes read */
1970 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
));
1972 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
));
1973 #endif /* HAVE_LIBZ */
1975 if (http
== NULL
|| buffer
== NULL
)
1978 http
->activity
= time(NULL
);
1985 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1989 if (http
->stream
.avail_in
> 0)
1991 int zerr
; /* Decompressor error */
1993 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1994 (int)http
->stream
.avail_in
, (int)length
));
1996 http
->stream
.next_out
= (Bytef
*)buffer
;
1997 http
->stream
.avail_out
= (uInt
)length
;
1999 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2001 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2003 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2010 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2012 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2013 http
->stream
.avail_in
, http
->stream
.avail_out
,
2021 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2022 /* Additional bytes for buffer */
2026 if (http
->stream
.avail_in
> 0 &&
2027 http
->stream
.next_in
> http
->sbuffer
)
2028 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2030 http
->stream
.next_in
= http
->sbuffer
;
2032 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2033 "decompression buffer.", (int)buflen
));
2035 if (http
->data_remaining
> 0)
2037 if (buflen
> http
->data_remaining
)
2038 buflen
= (ssize_t
)http
->data_remaining
;
2040 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2042 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2043 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2049 else if (bytes
== 0)
2052 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2053 "decompression buffer.", CUPS_LLCAST bytes
));
2055 http
->data_remaining
-= bytes
;
2056 http
->stream
.avail_in
+= (uInt
)bytes
;
2058 if (http
->data_remaining
<= 0 &&
2059 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2062 * Read the trailing blank line now...
2065 char len
[32]; /* Length string */
2067 httpGets(len
, sizeof(len
), http
);
2079 #endif /* HAVE_LIBZ */
2080 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2082 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2084 http
->data_remaining
-= bytes
;
2086 if (http
->data_remaining
<= 0)
2089 * Read the trailing blank line now...
2092 char len
[32]; /* Length string */
2094 httpGets(len
, sizeof(len
), http
);
2098 else if (http
->data_remaining
<= 0)
2101 * No more data to read...
2108 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2111 if (length
> (size_t)http
->data_remaining
)
2112 length
= (size_t)http
->data_remaining
;
2114 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2116 http
->data_remaining
-= bytes
;
2118 if (http
->data_remaining
<= 0 &&
2119 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2122 * Read the trailing blank line now...
2125 char len
[32]; /* Length string */
2127 httpGets(len
, sizeof(len
), http
);
2134 (http
->coding
== _HTTP_CODING_IDENTITY
||
2135 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2136 #endif /* HAVE_LIBZ */
2137 ((http
->data_remaining
<= 0 &&
2138 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2139 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2142 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2143 http_content_coding_finish(http
);
2144 #endif /* HAVE_LIBZ */
2146 if (http
->state
== HTTP_STATE_POST_RECV
)
2148 else if (http
->state
== HTTP_STATE_GET_SEND
||
2149 http
->state
== HTTP_STATE_POST_SEND
)
2150 http
->state
= HTTP_STATE_WAITING
;
2152 http
->state
= HTTP_STATE_STATUS
;
2154 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2155 httpStateString(http
->state
)));
2163 * 'httpReadRequest()' - Read a HTTP request from a connection.
2165 * @since CUPS 1.7/macOS 10.9@
2168 http_state_t
/* O - New state of connection */
2169 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2170 char *uri
, /* I - URI buffer */
2171 size_t urilen
) /* I - Size of URI buffer */
2173 char line
[4096], /* HTTP request line */
2174 *req_method
, /* HTTP request method */
2175 *req_uri
, /* HTTP request URI */
2176 *req_version
; /* HTTP request version number string */
2180 * Range check input...
2183 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2188 if (!http
|| !uri
|| urilen
< 1)
2190 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2191 return (HTTP_STATE_ERROR
);
2193 else if (http
->state
!= HTTP_STATE_WAITING
)
2195 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2196 httpStateString(http
->state
)));
2197 return (HTTP_STATE_ERROR
);
2204 httpClearFields(http
);
2206 http
->activity
= time(NULL
);
2207 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2208 http
->data_remaining
= 0;
2209 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2210 http
->status
= HTTP_STATUS_OK
;
2211 http
->version
= HTTP_VERSION_1_1
;
2214 * Read a line from the socket...
2217 if (!httpGets(line
, sizeof(line
), http
))
2219 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2220 return (HTTP_STATE_ERROR
);
2225 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2226 return (HTTP_STATE_WAITING
);
2229 DEBUG_printf(("1httpReadRequest: %s", line
));
2238 while (*req_uri
&& !isspace(*req_uri
& 255))
2243 DEBUG_puts("1httpReadRequest: No request URI.");
2244 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2245 return (HTTP_STATE_ERROR
);
2250 while (*req_uri
&& isspace(*req_uri
& 255))
2253 req_version
= req_uri
;
2255 while (*req_version
&& !isspace(*req_version
& 255))
2260 DEBUG_puts("1httpReadRequest: No request protocol version.");
2261 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2262 return (HTTP_STATE_ERROR
);
2265 *req_version
++ = '\0';
2267 while (*req_version
&& isspace(*req_version
& 255))
2274 if (!strcmp(req_method
, "OPTIONS"))
2275 http
->state
= HTTP_STATE_OPTIONS
;
2276 else if (!strcmp(req_method
, "GET"))
2277 http
->state
= HTTP_STATE_GET
;
2278 else if (!strcmp(req_method
, "HEAD"))
2279 http
->state
= HTTP_STATE_HEAD
;
2280 else if (!strcmp(req_method
, "POST"))
2281 http
->state
= HTTP_STATE_POST
;
2282 else if (!strcmp(req_method
, "PUT"))
2283 http
->state
= HTTP_STATE_PUT
;
2284 else if (!strcmp(req_method
, "DELETE"))
2285 http
->state
= HTTP_STATE_DELETE
;
2286 else if (!strcmp(req_method
, "TRACE"))
2287 http
->state
= HTTP_STATE_TRACE
;
2288 else if (!strcmp(req_method
, "CONNECT"))
2289 http
->state
= HTTP_STATE_CONNECT
;
2292 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2293 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2294 return (HTTP_STATE_UNKNOWN_METHOD
);
2297 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2298 httpStateString(http
->state
)));
2300 if (!strcmp(req_version
, "HTTP/1.0"))
2302 http
->version
= HTTP_VERSION_1_0
;
2303 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2305 else if (!strcmp(req_version
, "HTTP/1.1"))
2307 http
->version
= HTTP_VERSION_1_1
;
2308 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2312 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2313 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2314 return (HTTP_STATE_UNKNOWN_VERSION
);
2317 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2318 strlcpy(uri
, req_uri
, urilen
);
2320 return (http
->state
);
2325 * 'httpReconnect()' - Reconnect to a HTTP server.
2327 * This function is deprecated. Please use the @link httpReconnect2@ function
2330 * @deprecated@ @exclude all@
2333 int /* O - 0 on success, non-zero on failure */
2334 httpReconnect(http_t
*http
) /* I - HTTP connection */
2336 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2338 return (httpReconnect2(http
, 30000, NULL
));
2343 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2347 int /* O - 0 on success, non-zero on failure */
2348 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2349 int msec
, /* I - Timeout in milliseconds */
2350 int *cancel
) /* I - Pointer to "cancel" variable */
2352 http_addrlist_t
*addr
; /* Connected address */
2354 http_addrlist_t
*current
; /* Current address */
2355 char temp
[256]; /* Temporary address string */
2359 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2363 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2370 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2373 #endif /* HAVE_SSL */
2376 * Close any previously open socket...
2381 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2383 httpAddrClose(NULL
, http
->fd
);
2389 * Reset all state (except fields, which may be reused)...
2392 http
->state
= HTTP_STATE_WAITING
;
2393 http
->version
= HTTP_VERSION_1_1
;
2394 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2395 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2396 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2397 http
->_data_remaining
= 0;
2399 http
->data_remaining
= 0;
2400 http
->hostaddr
= NULL
;
2404 * Connect to the server...
2408 for (current
= http
->addrlist
; current
; current
= current
->next
)
2409 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2410 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2411 httpAddrPort(&(current
->addr
))));
2414 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2417 * Unable to connect...
2421 http
->error
= WSAGetLastError();
2423 http
->error
= errno
;
2425 http
->status
= HTTP_STATUS_ERROR
;
2427 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2428 strerror(http
->error
)));
2433 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2435 if (http
->timeout_value
> 0)
2436 http_set_timeout(http
->fd
, http
->timeout_value
);
2438 http
->hostaddr
= &(addr
->addr
);
2442 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2445 * Always do encryption via SSL.
2448 if (_httpTLSStart(http
) != 0)
2450 httpAddrClose(NULL
, http
->fd
);
2455 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2456 return (http_tls_upgrade(http
));
2457 #endif /* HAVE_SSL */
2459 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2460 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2461 httpAddrPort(http
->hostaddr
)));
2468 * 'httpSetAuthString()' - Set the current authorization string.
2470 * This function just stores a copy of the current authorization string in
2471 * the HTTP connection object. You must still call @link httpSetField@ to set
2472 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2473 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2476 * @since CUPS 1.3/macOS 10.5@
2480 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2481 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2482 const char *data
) /* I - Auth data (NULL for none) */
2485 * Range check input...
2491 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2492 free(http
->authstring
);
2494 http
->authstring
= http
->_authstring
;
2499 * Set the current authorization string...
2502 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2505 if (len
> sizeof(http
->_authstring
))
2507 if ((temp
= malloc(len
)) == NULL
)
2508 len
= sizeof(http
->_authstring
);
2510 http
->authstring
= temp
;
2514 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2516 strlcpy(http
->authstring
, scheme
, len
);
2521 * Clear the current authorization string...
2524 http
->_authstring
[0] = '\0';
2530 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2533 * @since CUPS 1.5/macOS 10.7@
2536 int /* O - Status of call (0 = success) */
2537 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2538 cups_array_t
*credentials
) /* I - Array of credentials */
2540 if (!http
|| cupsArrayCount(credentials
) < 1)
2544 _httpFreeCredentials(http
->tls_credentials
);
2546 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2547 #endif /* HAVE_SSL */
2549 return (http
->tls_credentials
? 0 : -1);
2554 * 'httpSetCookie()' - Set the cookie value(s).
2556 * @since CUPS 1.1.19/macOS 10.3@
2560 httpSetCookie(http_t
*http
, /* I - Connection */
2561 const char *cookie
) /* I - Cookie string */
2570 http
->cookie
= strdup(cookie
);
2572 http
->cookie
= NULL
;
2577 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2579 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2580 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2582 * @since CUPS 1.7/macOS 10.9@
2586 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2587 http_field_t field
, /* I - Field index */
2588 const char *value
)/* I - Value */
2590 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2597 case HTTP_FIELD_ACCEPT_ENCODING
:
2598 if (http
->default_accept_encoding
)
2599 _cupsStrFree(http
->default_accept_encoding
);
2601 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2604 case HTTP_FIELD_SERVER
:
2605 if (http
->default_server
)
2606 _cupsStrFree(http
->default_server
);
2608 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2611 case HTTP_FIELD_USER_AGENT
:
2612 if (http
->default_user_agent
)
2613 _cupsStrFree(http
->default_user_agent
);
2615 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2619 DEBUG_puts("1httpSetDefaultField: Ignored.");
2626 * 'httpSetExpect()' - Set the Expect: header in a request.
2628 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2631 * @since CUPS 1.2/macOS 10.5@
2635 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2636 http_status_t expect
) /* I - HTTP status to expect
2637 (@code HTTP_STATUS_CONTINUE@) */
2639 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2642 http
->expect
= expect
;
2647 * 'httpSetField()' - Set the value of an HTTP header.
2651 httpSetField(http_t
*http
, /* I - HTTP connection */
2652 http_field_t field
, /* I - Field index */
2653 const char *value
) /* I - Value */
2655 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2658 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2659 field
>= HTTP_FIELD_MAX
||
2663 http_add_field(http
, field
, value
, 0);
2668 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2670 * @since CUPS 2.0/OS 10.10@
2675 http_t
*http
, /* I - HTTP connection */
2676 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2679 http
->keep_alive
= keep_alive
;
2684 * 'httpSetLength()' - Set the content-length and content-encoding.
2686 * @since CUPS 1.2/macOS 10.5@
2690 httpSetLength(http_t
*http
, /* I - HTTP connection */
2691 size_t length
) /* I - Length (0 for chunked) */
2693 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2700 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2702 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2706 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2707 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2708 CUPS_LLFMT
, CUPS_LLCAST length
);
2714 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2716 * The optional timeout callback receives both the HTTP connection and a user
2717 * data pointer and must return 1 to continue or 0 to error (time) out.
2719 * @since CUPS 1.5/macOS 10.7@
2724 http_t
*http
, /* I - HTTP connection */
2725 double timeout
, /* I - Number of seconds for timeout,
2726 must be greater than 0 */
2727 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2728 void *user_data
) /* I - User data pointer */
2730 if (!http
|| timeout
<= 0.0)
2733 http
->timeout_cb
= cb
;
2734 http
->timeout_data
= user_data
;
2735 http
->timeout_value
= timeout
;
2738 http_set_timeout(http
->fd
, timeout
);
2740 http_set_wait(http
);
2745 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2747 * @since CUPS 2.0/OS 10.10@
2751 httpShutdown(http_t
*http
) /* I - HTTP connection */
2753 if (!http
|| http
->fd
< 0)
2759 #endif /* HAVE_SSL */
2762 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2764 shutdown(http
->fd
, SHUT_RD
);
2770 * 'httpTrace()' - Send an TRACE request to the server.
2775 int /* O - Status of call (0 = success) */
2776 httpTrace(http_t
*http
, /* I - HTTP connection */
2777 const char *uri
) /* I - URI for trace */
2779 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2784 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2786 * Note: Unlike httpUpdate(), this function does not flush pending write data
2787 * and only retrieves a single status line from the HTTP connection.
2790 int /* O - 1 to continue, 0 to stop */
2791 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2792 http_status_t
*status
) /* O - Current HTTP status */
2794 char line
[32768], /* Line from connection... */
2795 *value
; /* Pointer to value on line */
2796 http_field_t field
; /* Field index */
2797 int major
, minor
; /* HTTP version numbers */
2800 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2803 * Grab a single line from the connection...
2806 if (!httpGets(line
, sizeof(line
), http
))
2808 *status
= HTTP_STATUS_ERROR
;
2812 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2814 if (line
[0] == '\0')
2817 * Blank line means the start of the data section (if any). Return
2818 * the result code, too...
2820 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2821 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2825 if (http
->status
== HTTP_STATUS_CONTINUE
)
2827 *status
= http
->status
;
2831 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2832 http
->digest_tries
= 0;
2835 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2837 if (_httpTLSStart(http
) != 0)
2839 httpAddrClose(NULL
, http
->fd
);
2841 *status
= http
->status
= HTTP_STATUS_ERROR
;
2845 *status
= HTTP_STATUS_CONTINUE
;
2848 #endif /* HAVE_SSL */
2850 if (http_set_length(http
) < 0)
2852 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2853 http
->error
= EINVAL
;
2854 http
->status
= *status
= HTTP_STATUS_ERROR
;
2858 switch (http
->state
)
2860 case HTTP_STATE_GET
:
2861 case HTTP_STATE_POST
:
2862 case HTTP_STATE_POST_RECV
:
2863 case HTTP_STATE_PUT
:
2866 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2867 httpStateString(http
->state
)));
2869 case HTTP_STATE_POST_SEND
:
2870 case HTTP_STATE_HEAD
:
2874 http
->state
= HTTP_STATE_WAITING
;
2876 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2881 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2882 http_content_coding_start(http
,
2883 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2884 #endif /* HAVE_LIBZ */
2886 *status
= http
->status
;
2889 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2892 * Got the beginning of a response...
2895 int intstatus
; /* Status value as an integer */
2897 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2899 *status
= http
->status
= HTTP_STATUS_ERROR
;
2903 httpClearFields(http
);
2905 http
->version
= (http_version_t
)(major
* 100 + minor
);
2906 *status
= http
->status
= (http_status_t
)intstatus
;
2908 else if ((value
= strchr(line
, ':')) != NULL
)
2915 while (_cups_isspace(*value
))
2918 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
2921 * Be tolerants of servers that send unknown attribute fields...
2924 if (!_cups_strcasecmp(line
, "expect"))
2927 * "Expect: 100-continue" or similar...
2930 http
->expect
= (http_status_t
)atoi(value
);
2932 else if (!_cups_strcasecmp(line
, "cookie"))
2935 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2938 httpSetCookie(http
, value
);
2940 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
2941 http_add_field(http
, field
, value
, 1);
2944 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
2949 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
2950 http
->error
= EINVAL
;
2951 http
->status
= *status
= HTTP_STATUS_ERROR
;
2960 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2963 http_status_t
/* O - HTTP status */
2964 httpUpdate(http_t
*http
) /* I - HTTP connection */
2966 http_status_t status
; /* Request status */
2969 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
2972 * Flush pending data, if any...
2977 DEBUG_puts("2httpUpdate: flushing buffer...");
2979 if (httpFlushWrite(http
) < 0)
2980 return (HTTP_STATUS_ERROR
);
2984 * If we haven't issued any commands, then there is nothing to "update"...
2987 if (http
->state
== HTTP_STATE_WAITING
)
2988 return (HTTP_STATUS_CONTINUE
);
2991 * Grab all of the lines we can from the connection...
2994 while (_httpUpdate(http
, &status
));
2997 * See if there was an error...
3000 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3002 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3003 return (http
->status
);
3008 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3009 strerror(http
->error
)));
3010 http
->status
= HTTP_STATUS_ERROR
;
3011 return (HTTP_STATUS_ERROR
);
3015 * Return the current status...
3023 * '_httpWait()' - Wait for data available on a connection (no flush).
3026 int /* O - 1 if data is available, 0 otherwise */
3027 _httpWait(http_t
*http
, /* I - HTTP connection */
3028 int msec
, /* I - Milliseconds to wait */
3029 int usessl
) /* I - Use SSL context? */
3032 struct pollfd pfd
; /* Polled file descriptor */
3034 fd_set input_set
; /* select() input set */
3035 struct timeval timeout
; /* Timeout */
3036 #endif /* HAVE_POLL */
3037 int nfds
; /* Result from select()/poll() */
3040 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3044 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3049 * Check the SSL/TLS buffers for data first...
3053 if (http
->tls
&& _httpTLSPending(http
))
3055 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3058 #endif /* HAVE_SSL */
3061 * Then try doing a select() or poll() to poll the socket...
3066 pfd
.events
= POLLIN
;
3070 nfds
= poll(&pfd
, 1, msec
);
3072 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3077 FD_ZERO(&input_set
);
3078 FD_SET(http
->fd
, &input_set
);
3080 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3084 timeout
.tv_sec
= msec
/ 1000;
3085 timeout
.tv_usec
= (msec
% 1000) * 1000;
3087 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3090 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3092 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3095 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3096 WSAGetLastError() == WSAEWOULDBLOCK
));
3098 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3100 #endif /* HAVE_POLL */
3102 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3110 * 'httpWait()' - Wait for data available on a connection.
3112 * @since CUPS 1.1.19/macOS 10.3@
3115 int /* O - 1 if data is available, 0 otherwise */
3116 httpWait(http_t
*http
, /* I - HTTP connection */
3117 int msec
) /* I - Milliseconds to wait */
3120 * First see if there is data in the buffer...
3123 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3130 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3135 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3137 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3140 #endif /* HAVE_LIBZ */
3143 * Flush pending data, if any...
3148 DEBUG_puts("3httpWait: Flushing write buffer.");
3150 if (httpFlushWrite(http
) < 0)
3155 * If not, check the SSL/TLS buffers and do a select() on the connection...
3158 return (_httpWait(http
, msec
, 1));
3163 * 'httpWrite()' - Write data to a HTTP connection.
3165 * This function is deprecated. Use the httpWrite2() function which can
3166 * write more than 2GB of data.
3168 * @deprecated@ @exclude all@
3171 int /* O - Number of bytes written */
3172 httpWrite(http_t
*http
, /* I - HTTP connection */
3173 const char *buffer
, /* I - Buffer for data */
3174 int length
) /* I - Number of bytes to write */
3176 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3181 * 'httpWrite2()' - Write data to a HTTP connection.
3183 * @since CUPS 1.2/macOS 10.5@
3186 ssize_t
/* O - Number of bytes written */
3187 httpWrite2(http_t
*http
, /* I - HTTP connection */
3188 const char *buffer
, /* I - Buffer for data */
3189 size_t length
) /* I - Number of bytes to write */
3191 ssize_t bytes
; /* Bytes written */
3194 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3197 * Range check input...
3200 if (!http
|| !buffer
)
3202 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3207 * Mark activity on the connection...
3210 http
->activity
= time(NULL
);
3213 * Buffer small writes for better performance...
3217 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3219 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3223 http_content_coding_finish(http
);
3228 size_t slen
; /* Bytes to write */
3229 ssize_t sret
; /* Bytes written */
3231 http
->stream
.next_in
= (Bytef
*)buffer
;
3232 http
->stream
.avail_in
= (uInt
)length
;
3234 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3236 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3238 if (http
->stream
.avail_out
> 0)
3241 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3243 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3245 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3246 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3248 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3254 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3258 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3259 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3262 bytes
= (ssize_t
)length
;
3266 #endif /* HAVE_LIBZ */
3269 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3271 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3272 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3274 httpFlushWrite(http
);
3277 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3280 * Write to buffer...
3283 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3284 CUPS_LLCAST length
));
3286 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3287 http
->wused
+= (int)length
;
3288 bytes
= (ssize_t
)length
;
3293 * Otherwise write the data directly...
3296 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3297 CUPS_LLCAST length
));
3299 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3300 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3302 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3304 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3305 CUPS_LLCAST bytes
));
3308 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3309 http
->data_remaining
-= bytes
;
3315 * Handle end-of-request processing...
3318 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3319 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3322 * Finished with the transfer; unless we are sending POST or PUT
3327 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3328 http_content_coding_finish(http
);
3329 #endif /* HAVE_LIBZ */
3333 if (httpFlushWrite(http
) < 0)
3337 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3340 * Send a 0-length chunk at the end of the request...
3343 http_write(http
, "0\r\n\r\n", 5);
3346 * Reset the data state...
3349 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3350 http
->data_remaining
= 0;
3353 if (http
->state
== HTTP_STATE_POST_RECV
)
3355 else if (http
->state
== HTTP_STATE_POST_SEND
||
3356 http
->state
== HTTP_STATE_GET_SEND
)
3357 http
->state
= HTTP_STATE_WAITING
;
3359 http
->state
= HTTP_STATE_STATUS
;
3361 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3362 httpStateString(http
->state
)));
3365 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3372 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3374 * @since CUPS 1.7/macOS 10.9@
3377 int /* O - 0 on success, -1 on error */
3378 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3379 http_status_t status
) /* I - Status code */
3381 http_encoding_t old_encoding
; /* Old data_encoding value */
3382 off_t old_remaining
; /* Old data_remaining value */
3386 * Range check input...
3389 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3391 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3393 DEBUG_puts("1httpWriteResponse: Bad input.");
3398 * Set the various standard fields if they aren't already...
3401 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3402 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3404 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3406 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3407 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3410 if (http
->version
== HTTP_VERSION_1_1
)
3412 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3414 if (http
->keep_alive
)
3415 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3417 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3420 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3421 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3425 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3426 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3428 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3429 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3431 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3432 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3434 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3435 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3437 #endif /* HAVE_SSL */
3440 httpSetField(http
, HTTP_FIELD_SERVER
,
3441 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3444 * Set the Accept-Encoding field if it isn't already...
3447 if (!http
->accept_encoding
)
3448 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3449 http
->default_accept_encoding
? http
->default_accept_encoding
:
3451 "gzip, deflate, identity");
3454 #endif /* HAVE_LIBZ */
3457 * Send the response header...
3460 old_encoding
= http
->data_encoding
;
3461 old_remaining
= http
->data_remaining
;
3462 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3464 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3465 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3467 http
->status
= HTTP_STATUS_ERROR
;
3471 if (status
!= HTTP_STATUS_CONTINUE
)
3474 * 100 Continue doesn't have the rest of the response headers...
3477 int i
; /* Looping var */
3478 const char *value
; /* Field value */
3480 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3482 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3484 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3486 http
->status
= HTTP_STATUS_ERROR
;
3494 if (strchr(http
->cookie
, ';'))
3496 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3498 http
->status
= HTTP_STATUS_ERROR
;
3502 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3504 http
->status
= HTTP_STATUS_ERROR
;
3510 * "Click-jacking" defense (STR #4492)...
3513 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3514 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3516 http
->status
= HTTP_STATUS_ERROR
;
3521 if (httpWrite2(http
, "\r\n", 2) < 2)
3523 http
->status
= HTTP_STATUS_ERROR
;
3527 if (httpFlushWrite(http
) < 0)
3529 http
->status
= HTTP_STATUS_ERROR
;
3533 if (status
== HTTP_STATUS_CONTINUE
||
3534 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3537 * Restore the old data_encoding and data_length values...
3540 http
->data_encoding
= old_encoding
;
3541 http
->data_remaining
= old_remaining
;
3543 if (old_remaining
<= INT_MAX
)
3544 http
->_data_remaining
= (int)old_remaining
;
3546 http
->_data_remaining
= INT_MAX
;
3548 else if (http
->state
== HTTP_STATE_OPTIONS
||
3549 http
->state
== HTTP_STATE_HEAD
||
3550 http
->state
== HTTP_STATE_PUT
||
3551 http
->state
== HTTP_STATE_TRACE
||
3552 http
->state
== HTTP_STATE_CONNECT
||
3553 http
->state
== HTTP_STATE_STATUS
)
3555 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3556 "was %s.", httpStateString(http
->state
)));
3557 http
->state
= HTTP_STATE_WAITING
;
3562 * Force data_encoding and data_length to be set according to the response
3566 http_set_length(http
);
3568 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3570 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3571 "was %s.", httpStateString(http
->state
)));
3572 http
->state
= HTTP_STATE_WAITING
;
3576 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3581 * Then start any content encoding...
3584 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3585 http_content_coding_start(http
,
3586 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3587 #endif /* HAVE_LIBZ */
3596 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3600 http_add_field(http_t
*http
, /* I - HTTP connection */
3601 http_field_t field
, /* I - HTTP field */
3602 const char *value
, /* I - Value string */
3603 int append
) /* I - Append value? */
3605 char newvalue
[1024]; /* New value string */
3606 const char *oldvalue
; /* Old field value */
3611 * Optionally append the new value to the existing one...
3614 if (append
&& field
!= HTTP_FIELD_ACCEPT_ENCODING
&& field
!= HTTP_FIELD_ACCEPT_LANGUAGE
&& field
!= HTTP_FIELD_ACCEPT_RANGES
&& field
!= HTTP_FIELD_ALLOW
&& field
!= HTTP_FIELD_LINK
&& field
!= HTTP_FIELD_TRANSFER_ENCODING
&& field
!= HTTP_FIELD_UPGRADE
&& field
!= HTTP_FIELD_WWW_AUTHENTICATE
)
3617 if (field
== HTTP_FIELD_HOST
)
3620 * Special-case for Host: as we don't want a trailing "." on the hostname and
3621 * need to bracket IPv6 numeric addresses.
3624 char *ptr
= strchr(value
, ':');
3626 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3629 * Bracket IPv6 numeric addresses...
3632 snprintf(newvalue
, sizeof(newvalue
), "[%s]", value
);
3635 else if (*value
&& value
[strlen(value
) - 1] == '.')
3638 * Strip the trailing dot on the hostname...
3641 strlcpy(newvalue
, value
, sizeof(newvalue
));
3642 newvalue
[strlen(newvalue
) - 1] = '\0';
3646 else if (append
&& *value
&& (oldvalue
= httpGetField(http
, field
)) != NULL
&& *oldvalue
)
3648 snprintf(newvalue
, sizeof(newvalue
), "%s, %s", oldvalue
, value
);
3653 * Save the new value...
3658 case HTTP_FIELD_ACCEPT_ENCODING
:
3659 if (http
->accept_encoding
)
3660 _cupsStrFree(http
->accept_encoding
);
3662 http
->accept_encoding
= _cupsStrAlloc(value
);
3665 case HTTP_FIELD_ALLOW
:
3667 _cupsStrFree(http
->allow
);
3669 http
->allow
= _cupsStrAlloc(value
);
3672 case HTTP_FIELD_SERVER
:
3674 _cupsStrFree(http
->server
);
3676 http
->server
= _cupsStrAlloc(value
);
3680 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
3684 if (field
== HTTP_FIELD_AUTHORIZATION
)
3687 * Special case for Authorization: as its contents can be
3688 * longer than HTTP_MAX_VALUE
3691 if (http
->field_authorization
)
3692 free(http
->field_authorization
);
3694 http
->field_authorization
= strdup(value
);
3697 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
3698 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3700 DEBUG_puts("1http_add_field: Calling http_content_coding_start.");
3701 http_content_coding_start(http
, value
);
3703 #endif /* HAVE_LIBZ */
3709 * 'http_content_coding_finish()' - Finish doing any content encoding.
3713 http_content_coding_finish(
3714 http_t
*http
) /* I - HTTP connection */
3716 int zerr
; /* Compression status */
3717 Byte dummy
[1]; /* Dummy read buffer */
3718 size_t bytes
; /* Number of bytes to write */
3721 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3722 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3724 switch (http
->coding
)
3726 case _HTTP_CODING_DEFLATE
:
3727 case _HTTP_CODING_GZIP
:
3728 http
->stream
.next_in
= dummy
;
3729 http
->stream
.avail_in
= 0;
3733 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3734 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3738 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3740 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3741 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3743 http_write(http
, (char *)http
->sbuffer
, bytes
);
3746 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3747 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3749 while (zerr
== Z_OK
);
3751 deflateEnd(&(http
->stream
));
3753 free(http
->sbuffer
);
3754 http
->sbuffer
= NULL
;
3757 httpFlushWrite(http
);
3760 case _HTTP_CODING_INFLATE
:
3761 case _HTTP_CODING_GUNZIP
:
3762 inflateEnd(&(http
->stream
));
3763 free(http
->sbuffer
);
3764 http
->sbuffer
= NULL
;
3771 http
->coding
= _HTTP_CODING_IDENTITY
;
3776 * 'http_content_coding_start()' - Start doing content encoding.
3780 http_content_coding_start(
3781 http_t
*http
, /* I - HTTP connection */
3782 const char *value
) /* I - Value of Content-Encoding */
3784 int zerr
; /* Error/status */
3785 _http_coding_t coding
; /* Content coding value */
3788 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3790 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3792 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3796 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3798 if (http
->state
== HTTP_STATE_GET_SEND
||
3799 http
->state
== HTTP_STATE_POST_SEND
)
3800 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3801 _HTTP_CODING_GUNZIP
;
3802 else if (http
->state
== HTTP_STATE_POST_RECV
||
3803 http
->state
== HTTP_STATE_PUT_RECV
)
3804 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3805 _HTTP_CODING_GUNZIP
;
3808 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3812 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3814 if (http
->state
== HTTP_STATE_GET_SEND
||
3815 http
->state
== HTTP_STATE_POST_SEND
)
3816 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3817 _HTTP_CODING_INFLATE
;
3818 else if (http
->state
== HTTP_STATE_POST_RECV
||
3819 http
->state
== HTTP_STATE_PUT_RECV
)
3820 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3821 _HTTP_CODING_INFLATE
;
3824 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3830 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3834 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3838 case _HTTP_CODING_DEFLATE
:
3839 case _HTTP_CODING_GZIP
:
3841 httpFlushWrite(http
);
3843 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3845 http
->status
= HTTP_STATUS_ERROR
;
3846 http
->error
= errno
;
3851 * Window size for compression is 11 bits - optimal based on PWG Raster
3852 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3856 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3858 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3859 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3861 http
->status
= HTTP_STATUS_ERROR
;
3862 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3866 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3867 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3870 case _HTTP_CODING_INFLATE
:
3871 case _HTTP_CODING_GUNZIP
:
3872 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3874 http
->status
= HTTP_STATUS_ERROR
;
3875 http
->error
= errno
;
3880 * Window size for decompression is up to 15 bits (maximum supported).
3881 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3884 if ((zerr
= inflateInit2(&(http
->stream
),
3885 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3888 free(http
->sbuffer
);
3889 http
->sbuffer
= NULL
;
3890 http
->status
= HTTP_STATUS_ERROR
;
3891 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3895 http
->stream
.avail_in
= 0;
3896 http
->stream
.next_in
= http
->sbuffer
;
3903 http
->coding
= coding
;
3905 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3908 #endif /* HAVE_LIBZ */
3912 * 'http_create()' - Create an unconnected HTTP connection.
3915 static http_t
* /* O - HTTP connection */
3917 const char *host
, /* I - Hostname */
3918 int port
, /* I - Port number */
3919 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3920 int family
, /* I - Address family or AF_UNSPEC */
3921 http_encryption_t encryption
, /* I - Encryption to use */
3922 int blocking
, /* I - 1 for blocking mode */
3923 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3925 http_t
*http
; /* New HTTP connection */
3926 char service
[255]; /* Service name */
3927 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3930 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
));
3932 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3938 * Lookup the host...
3943 myaddrlist
= httpAddrCopyList(addrlist
);
3947 snprintf(service
, sizeof(service
), "%d", port
);
3949 myaddrlist
= httpAddrGetList(host
, family
, service
);
3956 * Allocate memory for the structure...
3959 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3961 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3962 httpAddrFreeList(addrlist
);
3967 * Initialize the HTTP data...
3971 http
->activity
= time(NULL
);
3972 http
->addrlist
= myaddrlist
;
3973 http
->blocking
= blocking
;
3976 http
->gssctx
= GSS_C_NO_CONTEXT
;
3977 http
->gssname
= GSS_C_NO_NAME
;
3978 #endif /* HAVE_GSSAPI */
3979 http
->status
= HTTP_STATUS_CONTINUE
;
3980 http
->version
= HTTP_VERSION_1_1
;
3983 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3985 if (port
== 443) /* Always use encryption for https */
3986 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3988 http
->encryption
= encryption
;
3990 http_set_wait(http
);
3993 * Return the new structure...
4002 * 'http_debug_hex()' - Do a hex dump of a buffer.
4006 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4007 const char *buffer
, /* I - Buffer to dump */
4008 int bytes
) /* I - Bytes to dump */
4010 int i
, j
, /* Looping vars */
4011 ch
; /* Current character */
4012 char line
[255], /* Line buffer */
4013 *start
, /* Start of line after prefix */
4014 *ptr
; /* Pointer into line */
4017 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4020 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4022 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4023 start
= line
+ strlen(line
);
4025 for (i
= 0; i
< bytes
; i
+= 16)
4027 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4028 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4032 memcpy(ptr
, " ", 3);
4037 memcpy(ptr
, " ", 3);
4040 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4042 ch
= buffer
[i
+ j
] & 255;
4044 if (ch
< ' ' || ch
>= 127)
4058 * 'http_read()' - Read a buffer from a HTTP connection.
4060 * This function does the low-level read from the socket, retrying and timing
4064 static ssize_t
/* O - Number of bytes read or -1 on error */
4065 http_read(http_t
*http
, /* I - HTTP connection */
4066 char *buffer
, /* I - Buffer */
4067 size_t length
) /* I - Maximum bytes to read */
4069 ssize_t bytes
; /* Bytes read */
4072 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4074 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4076 while (!httpWait(http
, http
->wait_value
))
4078 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4081 DEBUG_puts("2http_read: Timeout.");
4086 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4092 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4094 #endif /* HAVE_SSL */
4095 bytes
= recv(http
->fd
, buffer
, length
, 0);
4100 if (WSAGetLastError() != WSAEINTR
)
4102 http
->error
= WSAGetLastError();
4105 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4107 if (!http
->timeout_cb
||
4108 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4110 http
->error
= WSAEWOULDBLOCK
;
4115 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4117 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4119 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4121 http
->error
= errno
;
4124 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4126 http
->error
= errno
;
4130 else if (errno
!= EINTR
)
4132 http
->error
= errno
;
4140 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4141 CUPS_LLCAST bytes
));
4144 http_debug_hex("http_read", buffer
, (int)bytes
);
4150 if (WSAGetLastError() == WSAEINTR
)
4153 http
->error
= WSAGetLastError();
4155 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4158 http
->error
= errno
;
4161 else if (bytes
== 0)
4163 http
->error
= EPIPE
;
4172 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4174 * This function reads data from the HTTP buffer or from the socket, as needed.
4177 static ssize_t
/* O - Number of bytes read or -1 on error */
4178 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4179 char *buffer
, /* I - Buffer */
4180 size_t length
) /* I - Maximum bytes to read */
4182 ssize_t bytes
; /* Bytes read */
4185 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4189 if (length
> (size_t)http
->used
)
4190 bytes
= (ssize_t
)http
->used
;
4192 bytes
= (ssize_t
)length
;
4194 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4197 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4198 http
->used
-= (int)bytes
;
4201 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4204 bytes
= http_read(http
, buffer
, length
);
4211 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4213 * This function reads and validates the chunk length, then does a buffered read
4214 * returning the number of bytes placed in the buffer.
4217 static ssize_t
/* O - Number of bytes read or -1 on error */
4218 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4219 char *buffer
, /* I - Buffer */
4220 size_t length
) /* I - Maximum bytes to read */
4222 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4224 if (http
->data_remaining
<= 0)
4226 char len
[32]; /* Length string */
4228 if (!httpGets(len
, sizeof(len
), http
))
4230 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4236 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4237 if (!httpGets(len
, sizeof(len
), http
))
4239 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4244 http
->data_remaining
= strtoll(len
, NULL
, 16);
4246 if (http
->data_remaining
< 0)
4248 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4249 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4253 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4254 len
, CUPS_LLCAST http
->data_remaining
));
4256 if (http
->data_remaining
== 0)
4259 * 0-length chunk, grab trailing blank line...
4262 httpGets(len
, sizeof(len
), http
);
4266 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4267 CUPS_LLCAST http
->data_remaining
));
4269 if (http
->data_remaining
<= 0)
4271 else if (length
> (size_t)http
->data_remaining
)
4272 length
= (size_t)http
->data_remaining
;
4274 return (http_read_buffered(http
, buffer
, length
));
4279 * 'http_send()' - Send a request with all fields and the trailing blank line.
4282 static int /* O - 0 on success, non-zero on error */
4283 http_send(http_t
*http
, /* I - HTTP connection */
4284 http_state_t request
, /* I - Request code */
4285 const char *uri
) /* I - URI */
4287 int i
; /* Looping var */
4288 char buf
[1024]; /* Encoded URI buffer */
4289 const char *value
; /* Field value */
4290 static const char * const codes
[] = /* Request code strings */
4310 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4312 if (http
== NULL
|| uri
== NULL
)
4316 * Set the User-Agent field if it isn't already...
4319 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4321 if (http
->default_user_agent
)
4322 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4324 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4328 * Set the Accept-Encoding field if it isn't already...
4331 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4332 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4333 http
->default_accept_encoding
);
4336 * Encode the URI as needed...
4339 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4342 * See if we had an error the last time around; if so, reconnect...
4345 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4346 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4348 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4349 http
->fd
, http
->status
, http
->tls_upgrade
));
4351 if (httpReconnect2(http
, 30000, NULL
))
4356 * Flush any written data that is pending...
4361 if (httpFlushWrite(http
) < 0)
4362 if (httpReconnect2(http
, 30000, NULL
))
4367 * Send the request header...
4370 http
->state
= request
;
4371 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4373 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4376 http
->status
= HTTP_STATUS_CONTINUE
;
4379 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4381 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4382 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4384 #endif /* HAVE_SSL */
4386 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4388 http
->status
= HTTP_STATUS_ERROR
;
4392 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4393 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4395 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4397 if (i
== HTTP_FIELD_HOST
)
4399 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4400 httpAddrPort(http
->hostaddr
)) < 1)
4402 http
->status
= HTTP_STATUS_ERROR
;
4406 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4408 http
->status
= HTTP_STATUS_ERROR
;
4414 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4416 http
->status
= HTTP_STATUS_ERROR
;
4420 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4421 http
->mode
, http
->state
));
4423 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4424 (http
->state
== HTTP_STATE_POST_RECV
||
4425 http
->state
== HTTP_STATE_PUT_RECV
))
4426 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4428 http
->status
= HTTP_STATUS_ERROR
;
4432 if (httpPrintf(http
, "\r\n") < 1)
4434 http
->status
= HTTP_STATUS_ERROR
;
4438 if (httpFlushWrite(http
) < 0)
4441 http_set_length(http
);
4442 httpClearFields(http
);
4445 * The Kerberos and AuthRef authentication strings can only be used once...
4448 if (http
->field_authorization
&& http
->authstring
&&
4449 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4450 !strncmp(http
->authstring
, "AuthRef", 7)))
4452 http
->_authstring
[0] = '\0';
4454 if (http
->authstring
!= http
->_authstring
)
4455 free(http
->authstring
);
4457 http
->authstring
= http
->_authstring
;
4465 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4468 static off_t
/* O - Remainder or -1 on error */
4469 http_set_length(http_t
*http
) /* I - Connection */
4471 off_t remaining
; /* Remainder */
4474 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4476 if ((remaining
= httpGetLength2(http
)) >= 0)
4478 if (http
->mode
== _HTTP_MODE_SERVER
&&
4479 http
->state
!= HTTP_STATE_GET_SEND
&&
4480 http
->state
!= HTTP_STATE_PUT
&&
4481 http
->state
!= HTTP_STATE_POST
&&
4482 http
->state
!= HTTP_STATE_POST_SEND
)
4484 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4488 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4491 DEBUG_puts("1http_set_length: Setting data_encoding to "
4492 "HTTP_ENCODING_CHUNKED.");
4493 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4497 DEBUG_puts("1http_set_length: Setting data_encoding to "
4498 "HTTP_ENCODING_LENGTH.");
4499 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4502 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4503 CUPS_LLCAST remaining
));
4504 http
->data_remaining
= remaining
;
4506 if (remaining
<= INT_MAX
)
4507 http
->_data_remaining
= (int)remaining
;
4509 http
->_data_remaining
= INT_MAX
;
4516 * 'http_set_timeout()' - Set the socket timeout values.
4520 http_set_timeout(int fd
, /* I - File descriptor */
4521 double timeout
) /* I - Timeout in seconds */
4524 DWORD tv
= (DWORD
)(timeout
* 1000);
4525 /* Timeout in milliseconds */
4527 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4528 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4531 struct timeval tv
; /* Timeout in secs and usecs */
4533 tv
.tv_sec
= (int)timeout
;
4534 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4536 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4537 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4543 * 'http_set_wait()' - Set the default wait value for reads.
4547 http_set_wait(http_t
*http
) /* I - HTTP connection */
4551 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4553 if (http
->wait_value
<= 0)
4554 http
->wait_value
= 60000;
4557 http
->wait_value
= 10000;
4563 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4566 static int /* O - Status of connection */
4567 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4569 int ret
; /* Return value */
4570 http_t myhttp
; /* Local copy of HTTP data */
4573 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4576 * Flush the connection to make sure any previous "Upgrade" message
4583 * Copy the HTTP data to a local variable so we can do the OPTIONS
4584 * request without interfering with the existing request data...
4587 memcpy(&myhttp
, http
, sizeof(myhttp
));
4590 * Send an OPTIONS request to the server, requiring SSL or TLS
4591 * encryption on the link...
4594 http
->tls_upgrade
= 1;
4595 http
->field_authorization
= NULL
; /* Don't free the auth string */
4597 httpClearFields(http
);
4598 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4599 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4601 if ((ret
= httpOptions(http
, "*")) == 0)
4604 * Wait for the secure connection...
4607 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4611 * Restore the HTTP request data...
4614 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4615 http
->data_encoding
= myhttp
.data_encoding
;
4616 http
->data_remaining
= myhttp
.data_remaining
;
4617 http
->_data_remaining
= myhttp
._data_remaining
;
4618 http
->expect
= myhttp
.expect
;
4619 http
->field_authorization
= myhttp
.field_authorization
;
4620 http
->digest_tries
= myhttp
.digest_tries
;
4621 http
->tls_upgrade
= 0;
4624 * See if we actually went secure...
4630 * Server does not support HTTP upgrade...
4633 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4635 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4636 httpAddrClose(NULL
, http
->fd
);
4645 #endif /* HAVE_SSL */
4649 * 'http_write()' - Write a buffer to a HTTP connection.
4652 static ssize_t
/* O - Number of bytes written */
4653 http_write(http_t
*http
, /* I - HTTP connection */
4654 const char *buffer
, /* I - Buffer for data */
4655 size_t length
) /* I - Number of bytes to write */
4657 ssize_t tbytes
, /* Total bytes sent */
4658 bytes
; /* Bytes sent */
4661 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4667 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4669 if (http
->timeout_value
> 0.0)
4672 struct pollfd pfd
; /* Polled file descriptor */
4674 fd_set output_set
; /* Output ready for write? */
4675 struct timeval timeout
; /* Timeout value */
4676 #endif /* HAVE_POLL */
4677 int nfds
; /* Result from select()/poll() */
4683 pfd
.events
= POLLOUT
;
4685 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4686 (errno
== EINTR
|| errno
== EAGAIN
))
4692 FD_ZERO(&output_set
);
4693 FD_SET(http
->fd
, &output_set
);
4695 timeout
.tv_sec
= http
->wait_value
/ 1000;
4696 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4698 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4701 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4702 WSAGetLastError() == WSAEWOULDBLOCK
));
4704 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4706 #endif /* HAVE_POLL */
4710 http
->error
= errno
;
4713 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4716 http
->error
= WSAEWOULDBLOCK
;
4718 http
->error
= EWOULDBLOCK
;
4728 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4730 #endif /* HAVE_SSL */
4731 bytes
= send(http
->fd
, buffer
, length
, 0);
4733 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4734 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4739 if (WSAGetLastError() == WSAEINTR
)
4741 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4743 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4746 http
->error
= WSAGetLastError();
4748 else if (WSAGetLastError() != http
->error
&&
4749 WSAGetLastError() != WSAECONNRESET
)
4751 http
->error
= WSAGetLastError();
4758 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4760 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4762 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4765 http
->error
= errno
;
4767 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4769 http
->error
= errno
;
4774 DEBUG_printf(("3http_write: error writing data (%s).",
4775 strerror(http
->error
)));
4782 length
-= (size_t)bytes
;
4786 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4789 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4796 * 'http_write_chunk()' - Write a chunked buffer.
4799 static ssize_t
/* O - Number bytes written */
4800 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4801 const char *buffer
, /* I - Buffer to write */
4802 size_t length
) /* I - Length of buffer */
4804 char header
[16]; /* Chunk header */
4805 ssize_t bytes
; /* Bytes written */
4808 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4811 * Write the chunk header, data, and trailer.
4814 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4815 if (http_write(http
, header
, strlen(header
)) < 0)
4817 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4821 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4823 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4827 if (http_write(http
, "\r\n", 2) < 0)
4829 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");