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 * Licensed under Apache License v2.0. See the file "LICENSE" for more
15 * Include necessary headers...
18 #include "cups-private.h"
25 # include <sys/time.h>
26 # include <sys/resource.h>
30 #endif /* HAVE_POLL */
37 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
39 static void http_content_coding_finish(http_t
*http
);
40 static void http_content_coding_start(http_t
*http
,
42 #endif /* HAVE_LIBZ */
43 static http_t
*http_create(const char *host
, int port
,
44 http_addrlist_t
*addrlist
, int family
,
45 http_encryption_t encryption
,
46 int blocking
, _http_mode_t mode
);
48 static void http_debug_hex(const char *prefix
, const char *buffer
,
51 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
52 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
53 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
54 static int http_send(http_t
*http
, http_state_t request
,
56 static ssize_t
http_write(http_t
*http
, const char *buffer
,
58 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
60 static off_t
http_set_length(http_t
*http
);
61 static void http_set_timeout(int fd
, double timeout
);
62 static void http_set_wait(http_t
*http
);
65 static int http_tls_upgrade(http_t
*http
);
73 static const char * const http_fields
[] =
90 "If-Unmodified-since",
105 "Authentication-Info"
110 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
111 * specified listening socket.
113 * @since CUPS 1.7/macOS 10.9@
116 http_t
* /* O - HTTP connection or @code NULL@ */
117 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
118 int blocking
) /* I - 1 if the connection should be
119 blocking, 0 otherwise */
121 http_t
*http
; /* HTTP connection */
122 http_addrlist_t addrlist
; /* Dummy address list */
123 socklen_t addrlen
; /* Length of address */
124 int val
; /* Socket option value */
128 * Range check input...
135 * Create the client connection...
138 memset(&addrlist
, 0, sizeof(addrlist
));
140 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
141 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
142 _HTTP_MODE_SERVER
)) == NULL
)
146 * Accept the client and get the remote address...
149 addrlen
= sizeof(http_addr_t
);
151 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
154 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
160 http
->hostaddr
= &(http
->addrlist
->addr
);
162 if (httpAddrLocalhost(http
->hostaddr
))
163 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
165 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
169 * Disable SIGPIPE for this socket.
173 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
174 #endif /* SO_NOSIGPIPE */
177 * Using TCP_NODELAY improves responsiveness, especially on systems
178 * with a slow loopback interface. Since we write large buffers
179 * when sending print files and requests, there shouldn't be any
180 * performance penalty for this...
184 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
188 * Close this socket when starting another process...
191 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
192 #endif /* FD_CLOEXEC */
199 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
201 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
203 * @since CUPS 1.5/macOS 10.7@
206 int /* O - 0 on success, -1 on error */
208 cups_array_t
*credentials
, /* I - Credentials array */
209 const void *data
, /* I - PEM-encoded X.509 data */
210 size_t datalen
) /* I - Length of data */
212 http_credential_t
*credential
; /* Credential data */
215 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
217 credential
->datalen
= datalen
;
219 if ((credential
->data
= malloc(datalen
)) != NULL
)
221 memcpy(credential
->data
, data
, datalen
);
222 cupsArrayAdd(credentials
, credential
);
234 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
238 httpBlocking(http_t
*http
, /* I - HTTP connection */
239 int b
) /* I - 1 = blocking, 0 = non-blocking */
250 * 'httpCheck()' - Check to see if there is a pending response from the server.
253 int /* O - 0 = no data, 1 = data available */
254 httpCheck(http_t
*http
) /* I - HTTP connection */
256 return (httpWait(http
, 0));
261 * 'httpClearCookie()' - Clear the cookie value(s).
263 * @since CUPS 1.1.19/macOS 10.3@
267 httpClearCookie(http_t
*http
) /* I - HTTP connection */
281 * 'httpClearFields()' - Clear HTTP request fields.
285 httpClearFields(http_t
*http
) /* I - HTTP connection */
287 http_field_t field
; /* Current field */
290 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
294 memset(http
->_fields
, 0, sizeof(http
->fields
));
296 for (field
= HTTP_FIELD_ACCEPT_LANGUAGE
; field
< HTTP_FIELD_MAX
; field
++)
298 if (http
->fields
[field
] && http
->fields
[field
] != http
->_fields
[field
])
299 free(http
->fields
[field
]);
301 http
->fields
[field
] = NULL
;
304 if (http
->mode
== _HTTP_MODE_CLIENT
)
306 if (http
->hostname
[0] == '/')
307 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
309 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
312 http
->expect
= (http_status_t
)0;
318 * 'httpClose()' - Close an HTTP connection.
322 httpClose(http_t
*http
) /* I - HTTP connection */
325 OM_uint32 minor_status
; /* Minor status code */
326 #endif /* HAVE_GSSAPI */
329 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
332 * Range check input...
339 * Close any open connection...
342 _httpDisconnect(http
);
345 * Free memory used...
348 httpAddrFreeList(http
->addrlist
);
354 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
355 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
357 if (http
->gssname
!= GSS_C_NO_NAME
)
358 gss_release_name(&minor_status
, &http
->gssname
);
359 #endif /* HAVE_GSSAPI */
361 #ifdef HAVE_AUTHORIZATION_H
363 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
364 #endif /* HAVE_AUTHORIZATION_H */
366 httpClearFields(http
);
368 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
369 free(http
->authstring
);
376 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
378 * @since CUPS 2.0/OS 10.10@
381 int /* O - 1 if they match, 0 if they do not */
382 httpCompareCredentials(
383 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
384 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
386 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
389 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
))
390 if (temp1
->datalen
!= temp2
->datalen
)
392 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
395 return (temp1
== temp2
);
400 * 'httpConnect()' - Connect to a HTTP server.
402 * This function is deprecated - use @link httpConnect2@ instead.
404 * @deprecated@ @exclude all@
407 http_t
* /* O - New HTTP connection */
408 httpConnect(const char *host
, /* I - Host to connect to */
409 int port
) /* I - Port number */
411 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
412 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
417 * 'httpConnect2()' - Connect to a HTTP server.
419 * @since CUPS 1.7/macOS 10.9@
422 http_t
* /* O - New HTTP connection */
424 const char *host
, /* I - Host to connect to */
425 int port
, /* I - Port number */
426 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
427 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
428 http_encryption_t encryption
, /* I - Type of encryption to use */
429 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
430 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
431 int *cancel
) /* I - Pointer to "cancel" variable */
433 http_t
*http
; /* New HTTP connection */
436 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
));
439 * Create the HTTP structure...
442 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
443 _HTTP_MODE_CLIENT
)) == NULL
)
447 * Optionally connect to the remote system...
450 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
454 * Could not connect to any known address - bail out!
464 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
466 * This function is now deprecated. Please use the @link httpConnect2@ function
469 * @deprecated@ @exclude all@
472 http_t
* /* O - New HTTP connection */
474 const char *host
, /* I - Host to connect to */
475 int port
, /* I - Port number */
476 http_encryption_t encryption
) /* I - Type of encryption to use */
478 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
479 host
, port
, encryption
));
481 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
487 * 'httpDelete()' - Send a DELETE request to the server.
490 int /* O - Status of call (0 = success) */
491 httpDelete(http_t
*http
, /* I - HTTP connection */
492 const char *uri
) /* I - URI to delete */
494 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
499 * '_httpDisconnect()' - Disconnect a HTTP connection.
503 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
508 #endif /* HAVE_SSL */
510 httpAddrClose(NULL
, http
->fd
);
517 * 'httpEncryption()' - Set the required encryption on the link.
520 int /* O - -1 on error, 0 on success */
521 httpEncryption(http_t
*http
, /* I - HTTP connection */
522 http_encryption_t e
) /* I - New encryption preference */
524 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
530 if (http
->mode
== _HTTP_MODE_CLIENT
)
532 http
->encryption
= e
;
534 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
535 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
536 return (httpReconnect2(http
, 30000, NULL
));
537 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
538 return (http_tls_upgrade(http
));
544 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
547 http
->encryption
= e
;
548 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
549 return (_httpTLSStart(http
));
554 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
558 #endif /* HAVE_SSL */
563 * 'httpError()' - Get the last error on a connection.
566 int /* O - Error code (errno) value */
567 httpError(http_t
*http
) /* I - HTTP connection */
570 return (http
->error
);
577 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
581 http_field_t
/* O - Field index */
582 httpFieldValue(const char *name
) /* I - String name */
584 int i
; /* Looping var */
587 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
588 if (!_cups_strcasecmp(name
, http_fields
[i
]))
589 return ((http_field_t
)i
);
591 return (HTTP_FIELD_UNKNOWN
);
596 * 'httpFlush()' - Flush data read from a HTTP connection.
600 httpFlush(http_t
*http
) /* I - HTTP connection */
602 char buffer
[8192]; /* Junk buffer */
603 int blocking
; /* To block or not to block */
604 http_state_t oldstate
; /* Old state */
607 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
610 * Nothing to do if we are in the "waiting" state...
613 if (http
->state
== HTTP_STATE_WAITING
)
617 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
620 blocking
= http
->blocking
;
624 * Read any data we can...
627 oldstate
= http
->state
;
628 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
631 * Restore blocking and reset the connection if we didn't get all of
632 * the remaining data...
635 http
->blocking
= blocking
;
637 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
641 * Didn't get the data back, so close the current connection.
646 http_content_coding_finish(http
);
647 #endif /* HAVE_LIBZ */
649 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
651 http
->state
= HTTP_STATE_WAITING
;
656 #endif /* HAVE_SSL */
658 httpAddrClose(NULL
, http
->fd
);
666 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
668 * @since CUPS 1.2/macOS 10.5@
671 int /* O - Bytes written or -1 on error */
672 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
674 ssize_t bytes
; /* Bytes written */
677 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
679 if (!http
|| !http
->wused
)
681 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
682 "1httpFlushWrite: No connection.");
686 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
687 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
689 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
693 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
700 * 'httpFreeCredentials()' - Free an array of credentials.
705 cups_array_t
*credentials
) /* I - Array of credentials */
707 http_credential_t
*credential
; /* Credential */
710 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
712 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
714 cupsArrayRemove(credentials
, credential
);
715 free((void *)credential
->data
);
719 cupsArrayDelete(credentials
);
724 * 'httpGet()' - Send a GET request to the server.
727 int /* O - Status of call (0 = success) */
728 httpGet(http_t
*http
, /* I - HTTP connection */
729 const char *uri
) /* I - URI to get */
731 return (http_send(http
, HTTP_STATE_GET
, uri
));
736 * 'httpGetActivity()' - Get the most recent activity for a connection.
738 * The return value is the time in seconds of the last read or write.
740 * @since CUPS 2.0/OS 10.10@
743 time_t /* O - Time of last read or write */
744 httpGetActivity(http_t
*http
) /* I - HTTP connection */
746 return (http
? http
->activity
: 0);
751 * 'httpGetAuthString()' - Get the current authorization string.
753 * The authorization string is set by @link cupsDoAuthentication@ and
754 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
755 * string to use with @link httpSetField@ for the
756 * @code HTTP_FIELD_AUTHORIZATION@ value.
758 * @since CUPS 1.3/macOS 10.5@
761 char * /* O - Authorization string */
762 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
765 return (http
->authstring
);
772 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
774 * @since CUPS 1.2/macOS 10.5@
777 int /* O - 1 if blocking, 0 if non-blocking */
778 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
780 return (http
? http
->blocking
: 0);
785 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
786 * the client and server.
788 * This function uses the value of the Accepts-Encoding HTTP header and must be
789 * called after receiving a response from the server or a request from the
790 * client. The value returned can be use in subsequent requests (for clients)
791 * or in the response (for servers) in order to compress the content stream.
793 * @since CUPS 1.7/macOS 10.9@
796 const char * /* O - Content-Coding value or
797 @code NULL@ for the identity
799 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
802 if (http
&& http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
804 int i
; /* Looping var */
805 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
806 *start
, /* Start of coding value */
807 *end
; /* End of coding value */
808 double qvalue
; /* "qvalue" for coding */
809 struct lconv
*loc
= localeconv(); /* Locale data */
810 static const char * const codings
[] =
811 { /* Supported content codings */
818 strlcpy(temp
, http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
], sizeof(temp
));
820 for (start
= temp
; *start
; start
= end
)
823 * Find the end of the coding name...
828 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
834 * Grab the qvalue as needed...
837 if (!strncmp(end
, ";q=", 3))
838 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
841 * Skip past all attributes...
845 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
851 while (*end
&& isspace(*end
& 255))
855 * Check value if it matches something we support...
861 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
862 if (!strcmp(start
, codings
[i
]))
866 #endif /* HAVE_LIBZ */
873 * 'httpGetCookie()' - Get any cookie data from the response.
875 * @since CUPS 1.1.19/macOS 10.3@
878 const char * /* O - Cookie data or @code NULL@ */
879 httpGetCookie(http_t
*http
) /* I - HTTP connection */
881 return (http
? http
->cookie
: NULL
);
886 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
888 * This function returns the encryption mode for the connection. Use the
889 * @link httpIsEncrypted@ function to determine whether a TLS session has
892 * @since CUPS 2.0/OS 10.10@
895 http_encryption_t
/* O - Current encryption mode */
896 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
898 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
903 * 'httpGetExpect()' - Get the value of the Expect header, if any.
905 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
906 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
908 * @since CUPS 1.7/macOS 10.9@
911 http_status_t
/* O - Expect: status, if any */
912 httpGetExpect(http_t
*http
) /* I - HTTP connection */
915 return (HTTP_STATUS_ERROR
);
917 return (http
->expect
);
922 * 'httpGetFd()' - Get the file descriptor associated with a connection.
924 * @since CUPS 1.2/macOS 10.5@
927 int /* O - File descriptor or -1 if none */
928 httpGetFd(http_t
*http
) /* I - HTTP connection */
930 return (http
? http
->fd
: -1);
935 * 'httpGetField()' - Get a field value from a request/response.
938 const char * /* O - Field value */
939 httpGetField(http_t
*http
, /* I - HTTP connection */
940 http_field_t field
) /* I - Field to get */
942 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
944 else if (http
->fields
[field
])
945 return (http
->fields
[field
]);
952 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
954 * @since CUPS 2.0/OS 10.10@
957 http_keepalive_t
/* O - Keep-Alive state */
958 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
960 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
965 * 'httpGetLength()' - Get the amount of data remaining from the
966 * content-length or transfer-encoding fields.
968 * This function is deprecated and will not return lengths larger than
969 * 2^31 - 1; use httpGetLength2() instead.
971 * @deprecated@ @exclude all@
974 int /* O - Content length */
975 httpGetLength(http_t
*http
) /* I - HTTP connection */
978 * Get the read content length and return the 32-bit value.
983 httpGetLength2(http
);
985 return (http
->_data_remaining
);
993 * 'httpGetLength2()' - Get the amount of data remaining from the
994 * content-length or transfer-encoding fields.
996 * This function returns the complete content length, even for
997 * content larger than 2^31 - 1.
999 * @since CUPS 1.2/macOS 10.5@
1002 off_t
/* O - Content length */
1003 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1005 off_t remaining
; /* Remaining length */
1008 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1013 if (http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
] && !_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1015 DEBUG_puts("4httpGetLength2: chunked request!");
1021 * The following is a hack for HTTP servers that don't send a
1022 * Content-Length or Transfer-Encoding field...
1024 * If there is no Content-Length then the connection must close
1025 * after the transfer is complete...
1028 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
] || !http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1031 * Default content length is 0 for errors and certain types of operations,
1032 * and 2^31-1 for other successful requests...
1035 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1036 http
->state
== HTTP_STATE_OPTIONS
||
1037 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1038 http
->state
== HTTP_STATE_HEAD
||
1039 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1040 http
->state
== HTTP_STATE_DELETE
||
1041 http
->state
== HTTP_STATE_TRACE
||
1042 http
->state
== HTTP_STATE_CONNECT
)
1045 remaining
= 2147483647;
1047 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1051 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1052 CUPS_LLCAST remaining
));
1060 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1062 * @since CUPS 2.0/OS 10.10@
1065 size_t /* O - Number of bytes buffered */
1066 httpGetPending(http_t
*http
) /* I - HTTP connection */
1068 return (http
? (size_t)http
->wused
: 0);
1073 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1075 * @since CUPS 2.0/OS 10.10@
1078 size_t /* O - Number of bytes available */
1079 httpGetReady(http_t
*http
) /* I - HTTP connection */
1083 else if (http
->used
> 0)
1084 return ((size_t)http
->used
);
1087 return (_httpTLSPending(http
));
1088 #endif /* HAVE_SSL */
1095 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1096 * body or current chunk.
1098 * The @link httpIsChunked@ function can be used to determine whether the
1099 * message body is chunked or fixed-length.
1101 * @since CUPS 2.0/OS 10.10@
1104 size_t /* O - Remaining bytes */
1105 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1107 return (http
? (size_t)http
->data_remaining
: 0);
1112 * 'httpGets()' - Get a line of text from a HTTP connection.
1115 char * /* O - Line or @code NULL@ */
1116 httpGets(char *line
, /* I - Line to read into */
1117 int length
, /* I - Max length of buffer */
1118 http_t
*http
) /* I - HTTP connection */
1120 char *lineptr
, /* Pointer into line */
1121 *lineend
, /* End of line */
1122 *bufptr
, /* Pointer into input buffer */
1123 *bufend
; /* Pointer to end of buffer */
1124 ssize_t bytes
; /* Number of bytes read */
1125 int eol
; /* End-of-line? */
1128 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1130 if (!http
|| !line
|| length
<= 1)
1134 * Read a line from the buffer...
1139 lineend
= line
+ length
- 1;
1142 while (lineptr
< lineend
)
1145 * Pre-load the buffer as needed...
1154 while (http
->used
== 0)
1157 * No newline; see if there is more data to be read...
1160 while (!_httpWait(http
, http
->wait_value
, 1))
1162 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1165 DEBUG_puts("3httpGets: Timed out!");
1167 http
->error
= WSAETIMEDOUT
;
1169 http
->error
= ETIMEDOUT
;
1174 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1176 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1181 * Nope, can't get a line this time...
1185 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1187 if (WSAGetLastError() == WSAEINTR
)
1189 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1191 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1194 http
->error
= WSAGetLastError();
1196 else if (WSAGetLastError() != http
->error
)
1198 http
->error
= WSAGetLastError();
1203 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1207 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1209 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1211 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1214 http
->error
= errno
;
1216 else if (errno
!= http
->error
)
1218 http
->error
= errno
;
1225 else if (bytes
== 0)
1227 http
->error
= EPIPE
;
1233 * Yup, update the amount used...
1236 http
->used
+= (int)bytes
;
1240 * Now copy as much of the current line as possible...
1243 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1244 lineptr
< lineend
&& bufptr
< bufend
;)
1246 if (*bufptr
== 0x0a)
1252 else if (*bufptr
== 0x0d)
1255 *lineptr
++ = *bufptr
++;
1258 http
->used
-= (int)(bufptr
- http
->buffer
);
1260 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1268 http
->activity
= time(NULL
);
1272 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1278 DEBUG_puts("3httpGets: No new line available!");
1285 * 'httpGetState()' - Get the current state of the HTTP request.
1288 http_state_t
/* O - HTTP state */
1289 httpGetState(http_t
*http
) /* I - HTTP connection */
1291 return (http
? http
->state
: HTTP_STATE_ERROR
);
1296 * 'httpGetStatus()' - Get the status of the last HTTP request.
1298 * @since CUPS 1.2/macOS 10.5@
1301 http_status_t
/* O - HTTP status */
1302 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1304 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1309 * 'httpGetSubField()' - Get a sub-field value.
1311 * @deprecated@ @exclude all@
1314 char * /* O - Value or @code NULL@ */
1315 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1316 http_field_t field
, /* I - Field index */
1317 const char *name
, /* I - Name of sub-field */
1318 char *value
) /* O - Value string */
1320 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1325 * 'httpGetSubField2()' - Get a sub-field value.
1327 * @since CUPS 1.2/macOS 10.5@
1330 char * /* O - Value or @code NULL@ */
1331 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1332 http_field_t field
, /* I - Field index */
1333 const char *name
, /* I - Name of sub-field */
1334 char *value
, /* O - Value string */
1335 int valuelen
) /* I - Size of value buffer */
1337 const char *fptr
; /* Pointer into field */
1338 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1339 *ptr
, /* Pointer into string buffer */
1340 *end
; /* End of value buffer */
1342 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1344 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1345 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1348 end
= value
+ valuelen
- 1;
1350 for (fptr
= http
->fields
[field
]; *fptr
;)
1353 * Skip leading whitespace...
1356 while (_cups_isspace(*fptr
))
1366 * Get the sub-field name...
1370 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1371 ptr
< (temp
+ sizeof(temp
) - 1);
1376 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1379 * Skip trailing chars up to the '='...
1382 while (_cups_isspace(*fptr
))
1392 * Skip = and leading whitespace...
1397 while (_cups_isspace(*fptr
))
1403 * Read quoted string...
1406 for (ptr
= value
, fptr
++;
1407 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1412 while (*fptr
&& *fptr
!= '\"')
1421 * Read unquoted string...
1425 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1430 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1434 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1437 * See if this is the one...
1440 if (!strcmp(name
, temp
))
1442 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1449 DEBUG_puts("3httpGetSubField2: Returning NULL");
1456 * 'httpGetVersion()' - Get the HTTP version at the other end.
1459 http_version_t
/* O - Version number */
1460 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1462 return (http
? http
->version
: HTTP_VERSION_1_0
);
1467 * 'httpHead()' - Send a HEAD request to the server.
1470 int /* O - Status of call (0 = success) */
1471 httpHead(http_t
*http
, /* I - HTTP connection */
1472 const char *uri
) /* I - URI for head */
1474 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1475 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1480 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1481 * default HTTP proxy (if any).
1485 httpInitialize(void)
1487 static int initialized
= 0; /* Have we been called before? */
1489 WSADATA winsockdata
; /* WinSock data */
1496 _cupsGlobalUnlock();
1501 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1503 #elif !defined(SO_NOSIGPIPE)
1505 * Ignore SIGPIPE signals...
1509 sigset(SIGPIPE
, SIG_IGN
);
1511 # elif defined(HAVE_SIGACTION)
1512 struct sigaction action
; /* POSIX sigaction data */
1515 memset(&action
, 0, sizeof(action
));
1516 action
.sa_handler
= SIG_IGN
;
1517 sigaction(SIGPIPE
, &action
, NULL
);
1520 signal(SIGPIPE
, SIG_IGN
);
1521 # endif /* !SO_NOSIGPIPE */
1525 _httpTLSInitialize();
1526 # endif /* HAVE_SSL */
1529 _cupsGlobalUnlock();
1534 * 'httpIsChunked()' - Report whether a message body is chunked.
1536 * This function returns non-zero if the message body is composed of
1537 * variable-length chunks.
1539 * @since CUPS 2.0/OS 10.10@
1542 int /* O - 1 if chunked, 0 if not */
1543 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1545 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1550 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1552 * This function returns non-zero if the connection is currently encrypted.
1554 * @since CUPS 2.0/OS 10.10@
1557 int /* O - 1 if encrypted, 0 if not */
1558 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1560 return (http
? http
->tls
!= NULL
: 0);
1565 * 'httpOptions()' - Send an OPTIONS request to the server.
1568 int /* O - Status of call (0 = success) */
1569 httpOptions(http_t
*http
, /* I - HTTP connection */
1570 const char *uri
) /* I - URI for options */
1572 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1577 * 'httpPeek()' - Peek at data from a HTTP connection.
1579 * This function copies available data from the given HTTP connection, reading
1580 * a buffer as needed. The data is still available for reading using
1583 * For non-blocking connections the usual timeouts apply.
1585 * @since CUPS 1.7/macOS 10.9@
1588 ssize_t
/* O - Number of bytes copied */
1589 httpPeek(http_t
*http
, /* I - HTTP connection */
1590 char *buffer
, /* I - Buffer for data */
1591 size_t length
) /* I - Maximum number of bytes */
1593 ssize_t bytes
; /* Bytes read */
1594 char len
[32]; /* Length string */
1597 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1599 if (http
== NULL
|| buffer
== NULL
)
1602 http
->activity
= time(NULL
);
1608 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1609 http
->data_remaining
<= 0)
1611 DEBUG_puts("2httpPeek: Getting chunk length...");
1613 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1615 DEBUG_puts("1httpPeek: Could not get length!");
1621 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1622 if (!httpGets(len
, sizeof(len
), http
))
1624 DEBUG_puts("1httpPeek: Could not get chunk length.");
1629 http
->data_remaining
= strtoll(len
, NULL
, 16);
1631 if (http
->data_remaining
< 0)
1633 DEBUG_puts("1httpPeek: Negative chunk length!");
1638 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1639 CUPS_LLCAST http
->data_remaining
));
1641 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1644 * A zero-length chunk ends a transfer; unless we are reading POST
1649 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1650 http_content_coding_finish(http
);
1651 #endif /* HAVE_LIBZ */
1653 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1654 httpGets(len
, sizeof(len
), http
);
1656 if (http
->state
== HTTP_STATE_POST_RECV
)
1659 http
->state
= HTTP_STATE_STATUS
;
1661 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1662 httpStateString(http
->state
)));
1665 * Prevent future reads for this request...
1668 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1672 else if (length
> (size_t)http
->data_remaining
)
1673 length
= (size_t)http
->data_remaining
;
1676 if (http
->used
== 0 &&
1677 (http
->coding
== _HTTP_CODING_IDENTITY
||
1678 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1680 if (http
->used
== 0)
1681 #endif /* HAVE_LIBZ */
1684 * Buffer small reads for better performance...
1687 ssize_t buflen
; /* Length of read for buffer */
1689 if (!http
->blocking
)
1691 while (!httpWait(http
, http
->wait_value
))
1693 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1700 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1701 buflen
= sizeof(http
->buffer
);
1703 buflen
= (ssize_t
)http
->data_remaining
;
1705 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1706 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1708 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1709 CUPS_LLCAST bytes
));
1713 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1716 http
->used
= (int)bytes
;
1721 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1723 # ifdef HAVE_INFLATECOPY
1724 int zerr
; /* Decompressor error */
1725 z_stream stream
; /* Copy of decompressor stream */
1727 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1729 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1730 /* Number of bytes to copy */
1732 if (http
->stream
.avail_in
> 0 &&
1733 http
->stream
.next_in
> http
->sbuffer
)
1734 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1736 http
->stream
.next_in
= http
->sbuffer
;
1738 if (buflen
> (size_t)http
->data_remaining
)
1739 buflen
= (size_t)http
->data_remaining
;
1741 if (buflen
> (size_t)http
->used
)
1742 buflen
= (size_t)http
->used
;
1744 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1745 "decompression buffer.", (int)buflen
));
1747 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1748 http
->stream
.avail_in
+= buflen
;
1749 http
->used
-= (int)buflen
;
1750 http
->data_remaining
-= (off_t
)buflen
;
1753 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1756 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1757 (int)http
->stream
.avail_in
));
1759 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1761 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1762 http
->error
= ENOMEM
;
1766 stream
.next_out
= (Bytef
*)buffer
;
1767 stream
.avail_out
= (uInt
)length
;
1769 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1770 inflateEnd(&stream
);
1774 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1776 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1783 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1786 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1787 "work with compressed streams.");
1789 # endif /* HAVE_INFLATECOPY */
1792 #endif /* HAVE_LIBZ */
1795 if (length
> (size_t)http
->used
)
1796 length
= (size_t)http
->used
;
1798 bytes
= (ssize_t
)length
;
1800 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1803 memcpy(buffer
, http
->buffer
, length
);
1811 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1814 http
->error
= WSAGetLastError();
1816 if (errno
== EINTR
|| errno
== EAGAIN
)
1819 http
->error
= errno
;
1822 else if (bytes
== 0)
1824 http
->error
= EPIPE
;
1833 * 'httpPost()' - Send a POST request to the server.
1836 int /* O - Status of call (0 = success) */
1837 httpPost(http_t
*http
, /* I - HTTP connection */
1838 const char *uri
) /* I - URI for post */
1840 return (http_send(http
, HTTP_STATE_POST
, uri
));
1845 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1850 int /* O - Number of bytes written */
1851 httpPrintf(http_t
*http
, /* I - HTTP connection */
1852 const char *format
, /* I - printf-style format string */
1853 ...) /* I - Additional args as needed */
1855 ssize_t bytes
; /* Number of bytes to write */
1856 char buf
[16384]; /* Buffer for formatted string */
1857 va_list ap
; /* Variable argument pointer */
1860 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1862 va_start(ap
, format
);
1863 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1866 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1868 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1869 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1874 DEBUG_puts("4httpPrintf: flushing existing data...");
1876 if (httpFlushWrite(http
) < 0)
1880 return ((int)http_write(http
, buf
, (size_t)bytes
));
1886 * 'httpPut()' - Send a PUT request to the server.
1889 int /* O - Status of call (0 = success) */
1890 httpPut(http_t
*http
, /* I - HTTP connection */
1891 const char *uri
) /* I - URI to put */
1893 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1894 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1899 * 'httpRead()' - Read data from a HTTP connection.
1901 * This function is deprecated. Use the httpRead2() function which can
1902 * read more than 2GB of data.
1904 * @deprecated@ @exclude all@
1907 int /* O - Number of bytes read */
1908 httpRead(http_t
*http
, /* I - HTTP connection */
1909 char *buffer
, /* I - Buffer for data */
1910 int length
) /* I - Maximum number of bytes */
1912 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1917 * 'httpRead2()' - Read data from a HTTP connection.
1919 * @since CUPS 1.2/macOS 10.5@
1922 ssize_t
/* O - Number of bytes read */
1923 httpRead2(http_t
*http
, /* I - HTTP connection */
1924 char *buffer
, /* I - Buffer for data */
1925 size_t length
) /* I - Maximum number of bytes */
1927 ssize_t bytes
; /* Bytes read */
1931 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
));
1933 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
));
1934 #endif /* HAVE_LIBZ */
1936 if (http
== NULL
|| buffer
== NULL
)
1939 http
->activity
= time(NULL
);
1946 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1950 if (http
->stream
.avail_in
> 0)
1952 int zerr
; /* Decompressor error */
1954 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1955 (int)http
->stream
.avail_in
, (int)length
));
1957 http
->stream
.next_out
= (Bytef
*)buffer
;
1958 http
->stream
.avail_out
= (uInt
)length
;
1960 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
1962 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
1964 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1971 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1973 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
1974 http
->stream
.avail_in
, http
->stream
.avail_out
,
1982 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
1983 /* Additional bytes for buffer */
1987 if (http
->stream
.avail_in
> 0 &&
1988 http
->stream
.next_in
> http
->sbuffer
)
1989 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1991 http
->stream
.next_in
= http
->sbuffer
;
1993 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
1994 "decompression buffer.", (int)buflen
));
1996 if (http
->data_remaining
> 0)
1998 if (buflen
> http
->data_remaining
)
1999 buflen
= (ssize_t
)http
->data_remaining
;
2001 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2003 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2004 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2010 else if (bytes
== 0)
2013 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2014 "decompression buffer.", CUPS_LLCAST bytes
));
2016 http
->data_remaining
-= bytes
;
2017 http
->stream
.avail_in
+= (uInt
)bytes
;
2019 if (http
->data_remaining
<= 0 &&
2020 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2023 * Read the trailing blank line now...
2026 char len
[32]; /* Length string */
2028 httpGets(len
, sizeof(len
), http
);
2040 #endif /* HAVE_LIBZ */
2041 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2043 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2045 http
->data_remaining
-= bytes
;
2047 if (http
->data_remaining
<= 0)
2050 * Read the trailing blank line now...
2053 char len
[32]; /* Length string */
2055 httpGets(len
, sizeof(len
), http
);
2059 else if (http
->data_remaining
<= 0)
2062 * No more data to read...
2069 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2072 if (length
> (size_t)http
->data_remaining
)
2073 length
= (size_t)http
->data_remaining
;
2075 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2077 http
->data_remaining
-= bytes
;
2079 if (http
->data_remaining
<= 0 &&
2080 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2083 * Read the trailing blank line now...
2086 char len
[32]; /* Length string */
2088 httpGets(len
, sizeof(len
), http
);
2095 (http
->coding
== _HTTP_CODING_IDENTITY
||
2096 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2097 #endif /* HAVE_LIBZ */
2098 ((http
->data_remaining
<= 0 &&
2099 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2100 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2103 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2104 http_content_coding_finish(http
);
2105 #endif /* HAVE_LIBZ */
2107 if (http
->state
== HTTP_STATE_POST_RECV
)
2109 else if (http
->state
== HTTP_STATE_GET_SEND
||
2110 http
->state
== HTTP_STATE_POST_SEND
)
2111 http
->state
= HTTP_STATE_WAITING
;
2113 http
->state
= HTTP_STATE_STATUS
;
2115 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2116 httpStateString(http
->state
)));
2124 * 'httpReadRequest()' - Read a HTTP request from a connection.
2126 * @since CUPS 1.7/macOS 10.9@
2129 http_state_t
/* O - New state of connection */
2130 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2131 char *uri
, /* I - URI buffer */
2132 size_t urilen
) /* I - Size of URI buffer */
2134 char line
[4096], /* HTTP request line */
2135 *req_method
, /* HTTP request method */
2136 *req_uri
, /* HTTP request URI */
2137 *req_version
; /* HTTP request version number string */
2141 * Range check input...
2144 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2149 if (!http
|| !uri
|| urilen
< 1)
2151 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2152 return (HTTP_STATE_ERROR
);
2154 else if (http
->state
!= HTTP_STATE_WAITING
)
2156 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2157 httpStateString(http
->state
)));
2158 return (HTTP_STATE_ERROR
);
2165 httpClearFields(http
);
2167 http
->activity
= time(NULL
);
2168 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2169 http
->data_remaining
= 0;
2170 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2171 http
->status
= HTTP_STATUS_OK
;
2172 http
->version
= HTTP_VERSION_1_1
;
2175 * Read a line from the socket...
2178 if (!httpGets(line
, sizeof(line
), http
))
2180 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2181 return (HTTP_STATE_ERROR
);
2186 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2187 return (HTTP_STATE_WAITING
);
2190 DEBUG_printf(("1httpReadRequest: %s", line
));
2199 while (*req_uri
&& !isspace(*req_uri
& 255))
2204 DEBUG_puts("1httpReadRequest: No request URI.");
2205 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2206 return (HTTP_STATE_ERROR
);
2211 while (*req_uri
&& isspace(*req_uri
& 255))
2214 req_version
= req_uri
;
2216 while (*req_version
&& !isspace(*req_version
& 255))
2221 DEBUG_puts("1httpReadRequest: No request protocol version.");
2222 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2223 return (HTTP_STATE_ERROR
);
2226 *req_version
++ = '\0';
2228 while (*req_version
&& isspace(*req_version
& 255))
2235 if (!strcmp(req_method
, "OPTIONS"))
2236 http
->state
= HTTP_STATE_OPTIONS
;
2237 else if (!strcmp(req_method
, "GET"))
2238 http
->state
= HTTP_STATE_GET
;
2239 else if (!strcmp(req_method
, "HEAD"))
2240 http
->state
= HTTP_STATE_HEAD
;
2241 else if (!strcmp(req_method
, "POST"))
2242 http
->state
= HTTP_STATE_POST
;
2243 else if (!strcmp(req_method
, "PUT"))
2244 http
->state
= HTTP_STATE_PUT
;
2245 else if (!strcmp(req_method
, "DELETE"))
2246 http
->state
= HTTP_STATE_DELETE
;
2247 else if (!strcmp(req_method
, "TRACE"))
2248 http
->state
= HTTP_STATE_TRACE
;
2249 else if (!strcmp(req_method
, "CONNECT"))
2250 http
->state
= HTTP_STATE_CONNECT
;
2253 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2254 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2255 return (HTTP_STATE_UNKNOWN_METHOD
);
2258 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2259 httpStateString(http
->state
)));
2261 if (!strcmp(req_version
, "HTTP/1.0"))
2263 http
->version
= HTTP_VERSION_1_0
;
2264 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2266 else if (!strcmp(req_version
, "HTTP/1.1"))
2268 http
->version
= HTTP_VERSION_1_1
;
2269 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2273 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2274 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2275 return (HTTP_STATE_UNKNOWN_VERSION
);
2278 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2279 strlcpy(uri
, req_uri
, urilen
);
2281 return (http
->state
);
2286 * 'httpReconnect()' - Reconnect to a HTTP server.
2288 * This function is deprecated. Please use the @link httpReconnect2@ function
2291 * @deprecated@ @exclude all@
2294 int /* O - 0 on success, non-zero on failure */
2295 httpReconnect(http_t
*http
) /* I - HTTP connection */
2297 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2299 return (httpReconnect2(http
, 30000, NULL
));
2304 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2308 int /* O - 0 on success, non-zero on failure */
2309 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2310 int msec
, /* I - Timeout in milliseconds */
2311 int *cancel
) /* I - Pointer to "cancel" variable */
2313 http_addrlist_t
*addr
; /* Connected address */
2315 http_addrlist_t
*current
; /* Current address */
2316 char temp
[256]; /* Temporary address string */
2320 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2324 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2331 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2334 #endif /* HAVE_SSL */
2337 * Close any previously open socket...
2342 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2344 httpAddrClose(NULL
, http
->fd
);
2350 * Reset all state (except fields, which may be reused)...
2353 http
->state
= HTTP_STATE_WAITING
;
2354 http
->version
= HTTP_VERSION_1_1
;
2355 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2356 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2357 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2358 http
->_data_remaining
= 0;
2360 http
->data_remaining
= 0;
2361 http
->hostaddr
= NULL
;
2365 * Connect to the server...
2369 for (current
= http
->addrlist
; current
; current
= current
->next
)
2370 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2371 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2372 httpAddrPort(&(current
->addr
))));
2375 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2378 * Unable to connect...
2382 http
->error
= WSAGetLastError();
2384 http
->error
= errno
;
2386 http
->status
= HTTP_STATUS_ERROR
;
2388 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2389 strerror(http
->error
)));
2394 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2396 if (http
->timeout_value
> 0)
2397 http_set_timeout(http
->fd
, http
->timeout_value
);
2399 http
->hostaddr
= &(addr
->addr
);
2403 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2406 * Always do encryption via SSL.
2409 if (_httpTLSStart(http
) != 0)
2411 httpAddrClose(NULL
, http
->fd
);
2416 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2417 return (http_tls_upgrade(http
));
2418 #endif /* HAVE_SSL */
2420 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2421 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2422 httpAddrPort(http
->hostaddr
)));
2429 * 'httpSetAuthString()' - Set the current authorization string.
2431 * This function just stores a copy of the current authorization string in
2432 * the HTTP connection object. You must still call @link httpSetField@ to set
2433 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2434 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2437 * @since CUPS 1.3/macOS 10.5@
2441 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2442 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2443 const char *data
) /* I - Auth data (NULL for none) */
2446 * Range check input...
2452 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2453 free(http
->authstring
);
2455 http
->authstring
= http
->_authstring
;
2460 * Set the current authorization string...
2463 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2466 if (len
> sizeof(http
->_authstring
))
2468 if ((temp
= malloc(len
)) == NULL
)
2469 len
= sizeof(http
->_authstring
);
2471 http
->authstring
= temp
;
2475 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2477 strlcpy(http
->authstring
, scheme
, len
);
2482 * Clear the current authorization string...
2485 http
->_authstring
[0] = '\0';
2491 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2494 * @since CUPS 1.5/macOS 10.7@
2497 int /* O - Status of call (0 = success) */
2498 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2499 cups_array_t
*credentials
) /* I - Array of credentials */
2501 if (!http
|| cupsArrayCount(credentials
) < 1)
2505 _httpFreeCredentials(http
->tls_credentials
);
2507 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2508 #endif /* HAVE_SSL */
2510 return (http
->tls_credentials
? 0 : -1);
2515 * 'httpSetCookie()' - Set the cookie value(s).
2517 * @since CUPS 1.1.19/macOS 10.3@
2521 httpSetCookie(http_t
*http
, /* I - Connection */
2522 const char *cookie
) /* I - Cookie string */
2531 http
->cookie
= strdup(cookie
);
2533 http
->cookie
= NULL
;
2538 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2540 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2541 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2543 * @since CUPS 1.7/macOS 10.9@
2547 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2548 http_field_t field
, /* I - Field index */
2549 const char *value
)/* I - Value */
2551 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2553 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
2556 if (http
->default_fields
[field
])
2557 free(http
->default_fields
[field
]);
2559 http
->default_fields
[field
] = value
? strdup(value
) : NULL
;
2564 * 'httpSetExpect()' - Set the Expect: header in a request.
2566 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2569 * @since CUPS 1.2/macOS 10.5@
2573 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2574 http_status_t expect
) /* I - HTTP status to expect
2575 (@code HTTP_STATUS_CONTINUE@) */
2577 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2580 http
->expect
= expect
;
2585 * 'httpSetField()' - Set the value of an HTTP header.
2589 httpSetField(http_t
*http
, /* I - HTTP connection */
2590 http_field_t field
, /* I - Field index */
2591 const char *value
) /* I - Value */
2593 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2595 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !value
)
2598 http_add_field(http
, field
, value
, 0);
2603 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2605 * @since CUPS 2.0/OS 10.10@
2610 http_t
*http
, /* I - HTTP connection */
2611 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2614 http
->keep_alive
= keep_alive
;
2619 * 'httpSetLength()' - Set the content-length and content-encoding.
2621 * @since CUPS 1.2/macOS 10.5@
2625 httpSetLength(http_t
*http
, /* I - HTTP connection */
2626 size_t length
) /* I - Length (0 for chunked) */
2628 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2635 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
2636 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "");
2640 char len
[32]; /* Length string */
2643 snprintf(len
, sizeof(len
), CUPS_LLFMT
, CUPS_LLCAST length
);
2644 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "");
2645 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, len
);
2651 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2653 * The optional timeout callback receives both the HTTP connection and a user
2654 * data pointer and must return 1 to continue or 0 to error (time) out.
2656 * @since CUPS 1.5/macOS 10.7@
2661 http_t
*http
, /* I - HTTP connection */
2662 double timeout
, /* I - Number of seconds for timeout,
2663 must be greater than 0 */
2664 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2665 void *user_data
) /* I - User data pointer */
2667 if (!http
|| timeout
<= 0.0)
2670 http
->timeout_cb
= cb
;
2671 http
->timeout_data
= user_data
;
2672 http
->timeout_value
= timeout
;
2675 http_set_timeout(http
->fd
, timeout
);
2677 http_set_wait(http
);
2682 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2684 * @since CUPS 2.0/OS 10.10@
2688 httpShutdown(http_t
*http
) /* I - HTTP connection */
2690 if (!http
|| http
->fd
< 0)
2696 #endif /* HAVE_SSL */
2699 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2701 shutdown(http
->fd
, SHUT_RD
);
2707 * 'httpTrace()' - Send an TRACE request to the server.
2712 int /* O - Status of call (0 = success) */
2713 httpTrace(http_t
*http
, /* I - HTTP connection */
2714 const char *uri
) /* I - URI for trace */
2716 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2721 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2723 * Note: Unlike httpUpdate(), this function does not flush pending write data
2724 * and only retrieves a single status line from the HTTP connection.
2727 int /* O - 1 to continue, 0 to stop */
2728 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2729 http_status_t
*status
) /* O - Current HTTP status */
2731 char line
[32768], /* Line from connection... */
2732 *value
; /* Pointer to value on line */
2733 http_field_t field
; /* Field index */
2734 int major
, minor
; /* HTTP version numbers */
2737 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2740 * Grab a single line from the connection...
2743 if (!httpGets(line
, sizeof(line
), http
))
2745 *status
= HTTP_STATUS_ERROR
;
2749 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2751 if (line
[0] == '\0')
2754 * Blank line means the start of the data section (if any). Return
2755 * the result code, too...
2757 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2758 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2762 if (http
->status
== HTTP_STATUS_CONTINUE
)
2764 *status
= http
->status
;
2768 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2769 http
->digest_tries
= 0;
2772 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2774 if (_httpTLSStart(http
) != 0)
2776 httpAddrClose(NULL
, http
->fd
);
2778 *status
= http
->status
= HTTP_STATUS_ERROR
;
2782 *status
= HTTP_STATUS_CONTINUE
;
2785 #endif /* HAVE_SSL */
2787 if (http_set_length(http
) < 0)
2789 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2790 http
->error
= EINVAL
;
2791 http
->status
= *status
= HTTP_STATUS_ERROR
;
2795 switch (http
->state
)
2797 case HTTP_STATE_GET
:
2798 case HTTP_STATE_POST
:
2799 case HTTP_STATE_POST_RECV
:
2800 case HTTP_STATE_PUT
:
2803 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2804 httpStateString(http
->state
)));
2806 case HTTP_STATE_POST_SEND
:
2807 case HTTP_STATE_HEAD
:
2811 http
->state
= HTTP_STATE_WAITING
;
2813 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2818 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2819 http_content_coding_start(http
,
2820 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2821 #endif /* HAVE_LIBZ */
2823 *status
= http
->status
;
2826 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2829 * Got the beginning of a response...
2832 int intstatus
; /* Status value as an integer */
2834 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2836 *status
= http
->status
= HTTP_STATUS_ERROR
;
2840 httpClearFields(http
);
2842 http
->version
= (http_version_t
)(major
* 100 + minor
);
2843 *status
= http
->status
= (http_status_t
)intstatus
;
2845 else if ((value
= strchr(line
, ':')) != NULL
)
2852 while (_cups_isspace(*value
))
2855 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
2858 * Be tolerants of servers that send unknown attribute fields...
2861 if (!_cups_strcasecmp(line
, "expect"))
2864 * "Expect: 100-continue" or similar...
2867 http
->expect
= (http_status_t
)atoi(value
);
2869 else if (!_cups_strcasecmp(line
, "cookie"))
2872 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2875 httpSetCookie(http
, value
);
2877 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
2879 http_add_field(http
, field
, value
, 1);
2881 if (field
== HTTP_FIELD_AUTHENTICATION_INFO
)
2882 httpGetSubField2(http
, HTTP_FIELD_AUTHENTICATION_INFO
, "nextnonce", http
->nextnonce
, (int)sizeof(http
->nextnonce
));
2886 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
2891 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
2892 http
->error
= EINVAL
;
2893 http
->status
= *status
= HTTP_STATUS_ERROR
;
2902 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2905 http_status_t
/* O - HTTP status */
2906 httpUpdate(http_t
*http
) /* I - HTTP connection */
2908 http_status_t status
; /* Request status */
2911 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
2914 * Flush pending data, if any...
2919 DEBUG_puts("2httpUpdate: flushing buffer...");
2921 if (httpFlushWrite(http
) < 0)
2922 return (HTTP_STATUS_ERROR
);
2926 * If we haven't issued any commands, then there is nothing to "update"...
2929 if (http
->state
== HTTP_STATE_WAITING
)
2930 return (HTTP_STATUS_CONTINUE
);
2933 * Grab all of the lines we can from the connection...
2936 while (_httpUpdate(http
, &status
));
2939 * See if there was an error...
2942 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
2944 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
2945 return (http
->status
);
2950 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
2951 strerror(http
->error
)));
2952 http
->status
= HTTP_STATUS_ERROR
;
2953 return (HTTP_STATUS_ERROR
);
2957 * Return the current status...
2965 * '_httpWait()' - Wait for data available on a connection (no flush).
2968 int /* O - 1 if data is available, 0 otherwise */
2969 _httpWait(http_t
*http
, /* I - HTTP connection */
2970 int msec
, /* I - Milliseconds to wait */
2971 int usessl
) /* I - Use SSL context? */
2974 struct pollfd pfd
; /* Polled file descriptor */
2976 fd_set input_set
; /* select() input set */
2977 struct timeval timeout
; /* Timeout */
2978 #endif /* HAVE_POLL */
2979 int nfds
; /* Result from select()/poll() */
2982 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
2986 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
2991 * Check the SSL/TLS buffers for data first...
2995 if (http
->tls
&& _httpTLSPending(http
))
2997 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3000 #endif /* HAVE_SSL */
3003 * Then try doing a select() or poll() to poll the socket...
3008 pfd
.events
= POLLIN
;
3012 nfds
= poll(&pfd
, 1, msec
);
3014 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3019 FD_ZERO(&input_set
);
3020 FD_SET(http
->fd
, &input_set
);
3022 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3026 timeout
.tv_sec
= msec
/ 1000;
3027 timeout
.tv_usec
= (msec
% 1000) * 1000;
3029 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3032 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3034 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3037 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3038 WSAGetLastError() == WSAEWOULDBLOCK
));
3040 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3042 #endif /* HAVE_POLL */
3044 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3052 * 'httpWait()' - Wait for data available on a connection.
3054 * @since CUPS 1.1.19/macOS 10.3@
3057 int /* O - 1 if data is available, 0 otherwise */
3058 httpWait(http_t
*http
, /* I - HTTP connection */
3059 int msec
) /* I - Milliseconds to wait */
3062 * First see if there is data in the buffer...
3065 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3072 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3077 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3079 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3082 #endif /* HAVE_LIBZ */
3085 * Flush pending data, if any...
3090 DEBUG_puts("3httpWait: Flushing write buffer.");
3092 if (httpFlushWrite(http
) < 0)
3097 * If not, check the SSL/TLS buffers and do a select() on the connection...
3100 return (_httpWait(http
, msec
, 1));
3105 * 'httpWrite()' - Write data to a HTTP connection.
3107 * This function is deprecated. Use the httpWrite2() function which can
3108 * write more than 2GB of data.
3110 * @deprecated@ @exclude all@
3113 int /* O - Number of bytes written */
3114 httpWrite(http_t
*http
, /* I - HTTP connection */
3115 const char *buffer
, /* I - Buffer for data */
3116 int length
) /* I - Number of bytes to write */
3118 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3123 * 'httpWrite2()' - Write data to a HTTP connection.
3125 * @since CUPS 1.2/macOS 10.5@
3128 ssize_t
/* O - Number of bytes written */
3129 httpWrite2(http_t
*http
, /* I - HTTP connection */
3130 const char *buffer
, /* I - Buffer for data */
3131 size_t length
) /* I - Number of bytes to write */
3133 ssize_t bytes
; /* Bytes written */
3136 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3139 * Range check input...
3142 if (!http
|| !buffer
)
3144 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3149 * Mark activity on the connection...
3152 http
->activity
= time(NULL
);
3155 * Buffer small writes for better performance...
3159 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3161 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3165 http_content_coding_finish(http
);
3170 size_t slen
; /* Bytes to write */
3171 ssize_t sret
; /* Bytes written */
3173 http
->stream
.next_in
= (Bytef
*)buffer
;
3174 http
->stream
.avail_in
= (uInt
)length
;
3176 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3178 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3180 if (http
->stream
.avail_out
> 0)
3183 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3185 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3187 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3188 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3190 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3196 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3200 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3201 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3204 bytes
= (ssize_t
)length
;
3208 #endif /* HAVE_LIBZ */
3211 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3213 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3214 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3216 httpFlushWrite(http
);
3219 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3222 * Write to buffer...
3225 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3226 CUPS_LLCAST length
));
3228 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3229 http
->wused
+= (int)length
;
3230 bytes
= (ssize_t
)length
;
3235 * Otherwise write the data directly...
3238 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3239 CUPS_LLCAST length
));
3241 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3242 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3244 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3246 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3247 CUPS_LLCAST bytes
));
3250 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3251 http
->data_remaining
-= bytes
;
3257 * Handle end-of-request processing...
3260 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3261 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3264 * Finished with the transfer; unless we are sending POST or PUT
3269 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3270 http_content_coding_finish(http
);
3271 #endif /* HAVE_LIBZ */
3275 if (httpFlushWrite(http
) < 0)
3279 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3282 * Send a 0-length chunk at the end of the request...
3285 http_write(http
, "0\r\n\r\n", 5);
3288 * Reset the data state...
3291 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3292 http
->data_remaining
= 0;
3295 if (http
->state
== HTTP_STATE_POST_RECV
)
3297 else if (http
->state
== HTTP_STATE_POST_SEND
||
3298 http
->state
== HTTP_STATE_GET_SEND
)
3299 http
->state
= HTTP_STATE_WAITING
;
3301 http
->state
= HTTP_STATE_STATUS
;
3303 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3304 httpStateString(http
->state
)));
3307 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3314 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3316 * @since CUPS 1.7/macOS 10.9@
3319 int /* O - 0 on success, -1 on error */
3320 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3321 http_status_t status
) /* I - Status code */
3323 http_encoding_t old_encoding
; /* Old data_encoding value */
3324 off_t old_remaining
; /* Old data_remaining value */
3328 * Range check input...
3331 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3333 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3335 DEBUG_puts("1httpWriteResponse: Bad input.");
3340 * Set the various standard fields if they aren't already...
3343 if (!http
->fields
[HTTP_FIELD_DATE
])
3344 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3346 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3348 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3349 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3352 if (http
->version
== HTTP_VERSION_1_1
)
3354 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3356 if (http
->keep_alive
)
3357 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3359 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3362 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
])
3363 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3367 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3368 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3370 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3371 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3373 if (!http
->fields
[HTTP_FIELD_UPGRADE
])
3374 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3376 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
])
3377 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3379 #endif /* HAVE_SSL */
3381 if (!http
->fields
[HTTP_FIELD_SERVER
])
3382 httpSetField(http
, HTTP_FIELD_SERVER
, http
->default_fields
[HTTP_FIELD_SERVER
] ? http
->default_fields
[HTTP_FIELD_SERVER
] : CUPS_MINIMAL
);
3385 * Set the Accept-Encoding field if it isn't already...
3388 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
3389 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] ? http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] :
3391 "gzip, deflate, identity");
3394 #endif /* HAVE_LIBZ */
3397 * Send the response header...
3400 old_encoding
= http
->data_encoding
;
3401 old_remaining
= http
->data_remaining
;
3402 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3404 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100, http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3406 http
->status
= HTTP_STATUS_ERROR
;
3410 if (status
!= HTTP_STATUS_CONTINUE
)
3413 * 100 Continue doesn't have the rest of the response headers...
3416 int i
; /* Looping var */
3417 const char *value
; /* Field value */
3419 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3421 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3423 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3425 http
->status
= HTTP_STATUS_ERROR
;
3433 if (strchr(http
->cookie
, ';'))
3435 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3437 http
->status
= HTTP_STATUS_ERROR
;
3441 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3443 http
->status
= HTTP_STATUS_ERROR
;
3449 * "Click-jacking" defense (STR #4492)...
3452 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3453 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3455 http
->status
= HTTP_STATUS_ERROR
;
3460 if (httpWrite2(http
, "\r\n", 2) < 2)
3462 http
->status
= HTTP_STATUS_ERROR
;
3466 if (httpFlushWrite(http
) < 0)
3468 http
->status
= HTTP_STATUS_ERROR
;
3472 if (status
== HTTP_STATUS_CONTINUE
||
3473 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3476 * Restore the old data_encoding and data_length values...
3479 http
->data_encoding
= old_encoding
;
3480 http
->data_remaining
= old_remaining
;
3482 if (old_remaining
<= INT_MAX
)
3483 http
->_data_remaining
= (int)old_remaining
;
3485 http
->_data_remaining
= INT_MAX
;
3487 else if (http
->state
== HTTP_STATE_OPTIONS
||
3488 http
->state
== HTTP_STATE_HEAD
||
3489 http
->state
== HTTP_STATE_PUT
||
3490 http
->state
== HTTP_STATE_TRACE
||
3491 http
->state
== HTTP_STATE_CONNECT
||
3492 http
->state
== HTTP_STATE_STATUS
)
3494 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3495 "was %s.", httpStateString(http
->state
)));
3496 http
->state
= HTTP_STATE_WAITING
;
3501 * Force data_encoding and data_length to be set according to the response
3505 http_set_length(http
);
3507 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3509 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3510 "was %s.", httpStateString(http
->state
)));
3511 http
->state
= HTTP_STATE_WAITING
;
3515 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3520 * Then start any content encoding...
3523 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3524 http_content_coding_start(http
,
3525 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3526 #endif /* HAVE_LIBZ */
3535 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3539 http_add_field(http_t
*http
, /* I - HTTP connection */
3540 http_field_t field
, /* I - HTTP field */
3541 const char *value
, /* I - Value string */
3542 int append
) /* I - Append value? */
3544 char temp
[1024]; /* Temporary value string */
3545 size_t fieldlen
, /* Length of existing value */
3546 valuelen
, /* Length of value string */
3547 total
; /* Total length of string */
3550 if (field
== HTTP_FIELD_HOST
)
3553 * Special-case for Host: as we don't want a trailing "." on the hostname and
3554 * need to bracket IPv6 numeric addresses.
3557 char *ptr
= strchr(value
, ':');
3559 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3562 * Bracket IPv6 numeric addresses...
3564 * This is slightly inefficient (basically copying twice), but is an edge
3565 * case and not worth optimizing...
3568 snprintf(temp
, sizeof(temp
), "[%s]", value
);
3574 * Check for a trailing dot on the hostname...
3577 strlcpy(temp
, value
, sizeof(temp
));
3579 ptr
= temp
+ strlen(temp
) - 1;
3586 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
)
3589 if (!append
&& http
->fields
[field
])
3591 if (http
->fields
[field
] != http
->_fields
[field
])
3592 free(http
->fields
[field
]);
3594 http
->fields
[field
] = NULL
;
3597 valuelen
= strlen(value
);
3601 http
->_fields
[field
][0] = '\0';
3605 if (http
->fields
[field
])
3607 fieldlen
= strlen(http
->fields
[field
]);
3608 total
= fieldlen
+ 2 + valuelen
;
3616 if (total
< HTTP_MAX_VALUE
&& field
< HTTP_FIELD_ACCEPT_ENCODING
)
3619 * Copy short values to legacy char arrays (maintained for binary
3620 * compatibility with CUPS 1.2.x and earlier applications...)
3625 char combined
[HTTP_MAX_VALUE
];
3626 /* Combined value string */
3628 snprintf(combined
, sizeof(combined
), "%s, %s", http
->_fields
[field
], value
);
3632 strlcpy(http
->_fields
[field
], value
, sizeof(http
->_fields
[field
]));
3633 http
->fields
[field
] = http
->_fields
[field
];
3638 * Expand the field value...
3641 char *combined
; /* New value string */
3643 if ((combined
= realloc(http
->fields
[field
], total
+ 1)) != NULL
)
3645 http
->fields
[field
] = combined
;
3646 strlcat(combined
, ", ", total
+ 1);
3647 strlcat(combined
, value
, total
+ 1);
3653 * Allocate the field value...
3656 http
->fields
[field
] = strdup(value
);
3660 if (field
== HTTP_FIELD_CONTENT_ENCODING
&& http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3662 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
3663 http_content_coding_start(http
, value
);
3665 #endif /* HAVE_LIBZ */
3671 * 'http_content_coding_finish()' - Finish doing any content encoding.
3675 http_content_coding_finish(
3676 http_t
*http
) /* I - HTTP connection */
3678 int zerr
; /* Compression status */
3679 Byte dummy
[1]; /* Dummy read buffer */
3680 size_t bytes
; /* Number of bytes to write */
3683 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3684 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3686 switch (http
->coding
)
3688 case _HTTP_CODING_DEFLATE
:
3689 case _HTTP_CODING_GZIP
:
3690 http
->stream
.next_in
= dummy
;
3691 http
->stream
.avail_in
= 0;
3695 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3696 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3700 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3702 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3703 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3705 http_write(http
, (char *)http
->sbuffer
, bytes
);
3708 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3709 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3711 while (zerr
== Z_OK
);
3713 deflateEnd(&(http
->stream
));
3715 free(http
->sbuffer
);
3716 http
->sbuffer
= NULL
;
3719 httpFlushWrite(http
);
3722 case _HTTP_CODING_INFLATE
:
3723 case _HTTP_CODING_GUNZIP
:
3724 inflateEnd(&(http
->stream
));
3725 free(http
->sbuffer
);
3726 http
->sbuffer
= NULL
;
3733 http
->coding
= _HTTP_CODING_IDENTITY
;
3738 * 'http_content_coding_start()' - Start doing content encoding.
3742 http_content_coding_start(
3743 http_t
*http
, /* I - HTTP connection */
3744 const char *value
) /* I - Value of Content-Encoding */
3746 int zerr
; /* Error/status */
3747 _http_coding_t coding
; /* Content coding value */
3750 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3752 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3754 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3758 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3760 if (http
->state
== HTTP_STATE_GET_SEND
||
3761 http
->state
== HTTP_STATE_POST_SEND
)
3762 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3763 _HTTP_CODING_GUNZIP
;
3764 else if (http
->state
== HTTP_STATE_POST_RECV
||
3765 http
->state
== HTTP_STATE_PUT_RECV
)
3766 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3767 _HTTP_CODING_GUNZIP
;
3770 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3774 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3776 if (http
->state
== HTTP_STATE_GET_SEND
||
3777 http
->state
== HTTP_STATE_POST_SEND
)
3778 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3779 _HTTP_CODING_INFLATE
;
3780 else if (http
->state
== HTTP_STATE_POST_RECV
||
3781 http
->state
== HTTP_STATE_PUT_RECV
)
3782 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3783 _HTTP_CODING_INFLATE
;
3786 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3792 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3796 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3800 case _HTTP_CODING_DEFLATE
:
3801 case _HTTP_CODING_GZIP
:
3803 httpFlushWrite(http
);
3805 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3807 http
->status
= HTTP_STATUS_ERROR
;
3808 http
->error
= errno
;
3813 * Window size for compression is 11 bits - optimal based on PWG Raster
3814 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3818 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3820 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3821 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3823 http
->status
= HTTP_STATUS_ERROR
;
3824 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3828 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3829 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3832 case _HTTP_CODING_INFLATE
:
3833 case _HTTP_CODING_GUNZIP
:
3834 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3836 http
->status
= HTTP_STATUS_ERROR
;
3837 http
->error
= errno
;
3842 * Window size for decompression is up to 15 bits (maximum supported).
3843 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3846 if ((zerr
= inflateInit2(&(http
->stream
),
3847 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3850 free(http
->sbuffer
);
3851 http
->sbuffer
= NULL
;
3852 http
->status
= HTTP_STATUS_ERROR
;
3853 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3857 http
->stream
.avail_in
= 0;
3858 http
->stream
.next_in
= http
->sbuffer
;
3865 http
->coding
= coding
;
3867 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3870 #endif /* HAVE_LIBZ */
3874 * 'http_create()' - Create an unconnected HTTP connection.
3877 static http_t
* /* O - HTTP connection */
3879 const char *host
, /* I - Hostname */
3880 int port
, /* I - Port number */
3881 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3882 int family
, /* I - Address family or AF_UNSPEC */
3883 http_encryption_t encryption
, /* I - Encryption to use */
3884 int blocking
, /* I - 1 for blocking mode */
3885 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3887 http_t
*http
; /* New HTTP connection */
3888 char service
[255]; /* Service name */
3889 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3892 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
));
3894 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3900 * Lookup the host...
3905 myaddrlist
= httpAddrCopyList(addrlist
);
3909 snprintf(service
, sizeof(service
), "%d", port
);
3911 myaddrlist
= httpAddrGetList(host
, family
, service
);
3918 * Allocate memory for the structure...
3921 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3923 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3924 httpAddrFreeList(myaddrlist
);
3929 * Initialize the HTTP data...
3933 http
->activity
= time(NULL
);
3934 http
->addrlist
= myaddrlist
;
3935 http
->blocking
= blocking
;
3938 http
->gssctx
= GSS_C_NO_CONTEXT
;
3939 http
->gssname
= GSS_C_NO_NAME
;
3940 #endif /* HAVE_GSSAPI */
3941 http
->status
= HTTP_STATUS_CONTINUE
;
3942 http
->version
= HTTP_VERSION_1_1
;
3945 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3947 if (port
== 443) /* Always use encryption for https */
3948 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3950 http
->encryption
= encryption
;
3952 http_set_wait(http
);
3955 * Return the new structure...
3964 * 'http_debug_hex()' - Do a hex dump of a buffer.
3968 http_debug_hex(const char *prefix
, /* I - Prefix for line */
3969 const char *buffer
, /* I - Buffer to dump */
3970 int bytes
) /* I - Bytes to dump */
3972 int i
, j
, /* Looping vars */
3973 ch
; /* Current character */
3974 char line
[255], /* Line buffer */
3975 *start
, /* Start of line after prefix */
3976 *ptr
; /* Pointer into line */
3979 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
3982 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
3984 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
3985 start
= line
+ strlen(line
);
3987 for (i
= 0; i
< bytes
; i
+= 16)
3989 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
3990 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
3994 memcpy(ptr
, " ", 3);
3999 memcpy(ptr
, " ", 3);
4002 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4004 ch
= buffer
[i
+ j
] & 255;
4006 if (ch
< ' ' || ch
>= 127)
4020 * 'http_read()' - Read a buffer from a HTTP connection.
4022 * This function does the low-level read from the socket, retrying and timing
4026 static ssize_t
/* O - Number of bytes read or -1 on error */
4027 http_read(http_t
*http
, /* I - HTTP connection */
4028 char *buffer
, /* I - Buffer */
4029 size_t length
) /* I - Maximum bytes to read */
4031 ssize_t bytes
; /* Bytes read */
4034 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4036 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4038 while (!httpWait(http
, http
->wait_value
))
4040 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4043 DEBUG_puts("2http_read: Timeout.");
4048 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4054 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4056 #endif /* HAVE_SSL */
4057 bytes
= recv(http
->fd
, buffer
, length
, 0);
4062 if (WSAGetLastError() != WSAEINTR
)
4064 http
->error
= WSAGetLastError();
4067 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4069 if (!http
->timeout_cb
||
4070 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4072 http
->error
= WSAEWOULDBLOCK
;
4077 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4079 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4081 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4083 http
->error
= errno
;
4086 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4088 http
->error
= errno
;
4092 else if (errno
!= EINTR
)
4094 http
->error
= errno
;
4102 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4103 CUPS_LLCAST bytes
));
4106 http_debug_hex("http_read", buffer
, (int)bytes
);
4112 if (WSAGetLastError() == WSAEINTR
)
4115 http
->error
= WSAGetLastError();
4117 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4120 http
->error
= errno
;
4123 else if (bytes
== 0)
4125 http
->error
= EPIPE
;
4134 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4136 * This function reads data from the HTTP buffer or from the socket, as needed.
4139 static ssize_t
/* O - Number of bytes read or -1 on error */
4140 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4141 char *buffer
, /* I - Buffer */
4142 size_t length
) /* I - Maximum bytes to read */
4144 ssize_t bytes
; /* Bytes read */
4147 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4151 if (length
> (size_t)http
->used
)
4152 bytes
= (ssize_t
)http
->used
;
4154 bytes
= (ssize_t
)length
;
4156 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4159 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4160 http
->used
-= (int)bytes
;
4163 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4166 bytes
= http_read(http
, buffer
, length
);
4173 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4175 * This function reads and validates the chunk length, then does a buffered read
4176 * returning the number of bytes placed in the buffer.
4179 static ssize_t
/* O - Number of bytes read or -1 on error */
4180 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4181 char *buffer
, /* I - Buffer */
4182 size_t length
) /* I - Maximum bytes to read */
4184 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4186 if (http
->data_remaining
<= 0)
4188 char len
[32]; /* Length string */
4190 if (!httpGets(len
, sizeof(len
), http
))
4192 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4198 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4199 if (!httpGets(len
, sizeof(len
), http
))
4201 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4206 http
->data_remaining
= strtoll(len
, NULL
, 16);
4208 if (http
->data_remaining
< 0)
4210 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4211 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4215 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4216 len
, CUPS_LLCAST http
->data_remaining
));
4218 if (http
->data_remaining
== 0)
4221 * 0-length chunk, grab trailing blank line...
4224 httpGets(len
, sizeof(len
), http
);
4228 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4229 CUPS_LLCAST http
->data_remaining
));
4231 if (http
->data_remaining
<= 0)
4233 else if (length
> (size_t)http
->data_remaining
)
4234 length
= (size_t)http
->data_remaining
;
4236 return (http_read_buffered(http
, buffer
, length
));
4241 * 'http_send()' - Send a request with all fields and the trailing blank line.
4244 static int /* O - 0 on success, non-zero on error */
4245 http_send(http_t
*http
, /* I - HTTP connection */
4246 http_state_t request
, /* I - Request code */
4247 const char *uri
) /* I - URI */
4249 int i
; /* Looping var */
4250 char buf
[1024]; /* Encoded URI buffer */
4251 const char *value
; /* Field value */
4252 static const char * const codes
[] = /* Request code strings */
4272 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4274 if (http
== NULL
|| uri
== NULL
)
4278 * Set the User-Agent field if it isn't already...
4281 if (!http
->fields
[HTTP_FIELD_USER_AGENT
])
4283 if (http
->default_fields
[HTTP_FIELD_USER_AGENT
])
4284 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_fields
[HTTP_FIELD_USER_AGENT
]);
4286 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4290 * Set the Accept-Encoding field if it isn't already...
4293 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
] && http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
])
4294 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
]);
4297 * Encode the URI as needed...
4300 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4303 * See if we had an error the last time around; if so, reconnect...
4306 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4307 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4309 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4310 http
->fd
, http
->status
, http
->tls_upgrade
));
4312 if (httpReconnect2(http
, 30000, NULL
))
4317 * Flush any written data that is pending...
4322 if (httpFlushWrite(http
) < 0)
4323 if (httpReconnect2(http
, 30000, NULL
))
4328 * Send the request header...
4331 http
->state
= request
;
4332 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4334 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4337 http
->status
= HTTP_STATUS_CONTINUE
;
4340 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4342 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4343 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4345 #endif /* HAVE_SSL */
4347 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4349 http
->status
= HTTP_STATUS_ERROR
;
4353 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4354 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4356 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4358 if (i
== HTTP_FIELD_HOST
)
4360 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4361 httpAddrPort(http
->hostaddr
)) < 1)
4363 http
->status
= HTTP_STATUS_ERROR
;
4367 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4369 http
->status
= HTTP_STATUS_ERROR
;
4375 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4377 http
->status
= HTTP_STATUS_ERROR
;
4381 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4382 http
->mode
, http
->state
));
4384 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4385 (http
->state
== HTTP_STATE_POST_RECV
||
4386 http
->state
== HTTP_STATE_PUT_RECV
))
4387 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4389 http
->status
= HTTP_STATUS_ERROR
;
4393 if (httpPrintf(http
, "\r\n") < 1)
4395 http
->status
= HTTP_STATUS_ERROR
;
4399 if (httpFlushWrite(http
) < 0)
4402 http_set_length(http
);
4403 httpClearFields(http
);
4406 * The Kerberos and AuthRef authentication strings can only be used once...
4409 if (http
->fields
[HTTP_FIELD_AUTHORIZATION
] && http
->authstring
&&
4410 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4411 !strncmp(http
->authstring
, "AuthRef", 7)))
4413 http
->_authstring
[0] = '\0';
4415 if (http
->authstring
!= http
->_authstring
)
4416 free(http
->authstring
);
4418 http
->authstring
= http
->_authstring
;
4426 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4429 static off_t
/* O - Remainder or -1 on error */
4430 http_set_length(http_t
*http
) /* I - Connection */
4432 off_t remaining
; /* Remainder */
4435 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4437 if ((remaining
= httpGetLength2(http
)) >= 0)
4439 if (http
->mode
== _HTTP_MODE_SERVER
&&
4440 http
->state
!= HTTP_STATE_GET_SEND
&&
4441 http
->state
!= HTTP_STATE_PUT
&&
4442 http
->state
!= HTTP_STATE_POST
&&
4443 http
->state
!= HTTP_STATE_POST_SEND
)
4445 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4449 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_TRANSFER_ENCODING
), "chunked"))
4451 DEBUG_puts("1http_set_length: Setting data_encoding to "
4452 "HTTP_ENCODING_CHUNKED.");
4453 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4457 DEBUG_puts("1http_set_length: Setting data_encoding to "
4458 "HTTP_ENCODING_LENGTH.");
4459 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4462 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4463 CUPS_LLCAST remaining
));
4464 http
->data_remaining
= remaining
;
4466 if (remaining
<= INT_MAX
)
4467 http
->_data_remaining
= (int)remaining
;
4469 http
->_data_remaining
= INT_MAX
;
4476 * 'http_set_timeout()' - Set the socket timeout values.
4480 http_set_timeout(int fd
, /* I - File descriptor */
4481 double timeout
) /* I - Timeout in seconds */
4484 DWORD tv
= (DWORD
)(timeout
* 1000);
4485 /* Timeout in milliseconds */
4487 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4488 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4491 struct timeval tv
; /* Timeout in secs and usecs */
4493 tv
.tv_sec
= (int)timeout
;
4494 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4496 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4497 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4503 * 'http_set_wait()' - Set the default wait value for reads.
4507 http_set_wait(http_t
*http
) /* I - HTTP connection */
4511 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4513 if (http
->wait_value
<= 0)
4514 http
->wait_value
= 60000;
4517 http
->wait_value
= 10000;
4523 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4526 static int /* O - Status of connection */
4527 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4529 int ret
; /* Return value */
4530 http_t myhttp
; /* Local copy of HTTP data */
4533 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4536 * Flush the connection to make sure any previous "Upgrade" message
4543 * Copy the HTTP data to a local variable so we can do the OPTIONS
4544 * request without interfering with the existing request data...
4547 memcpy(&myhttp
, http
, sizeof(myhttp
));
4550 * Send an OPTIONS request to the server, requiring SSL or TLS
4551 * encryption on the link...
4554 http
->tls_upgrade
= 1;
4555 memset(http
->fields
, 0, sizeof(http
->fields
));
4556 http
->expect
= (http_status_t
)0;
4558 if (http
->hostname
[0] == '/')
4559 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
4561 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
4563 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4564 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4566 if ((ret
= httpOptions(http
, "*")) == 0)
4569 * Wait for the secure connection...
4572 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4576 * Restore the HTTP request data...
4579 memcpy(http
->_fields
, myhttp
._fields
, sizeof(http
->_fields
));
4580 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4582 http
->data_encoding
= myhttp
.data_encoding
;
4583 http
->data_remaining
= myhttp
.data_remaining
;
4584 http
->_data_remaining
= myhttp
._data_remaining
;
4585 http
->expect
= myhttp
.expect
;
4586 http
->digest_tries
= myhttp
.digest_tries
;
4587 http
->tls_upgrade
= 0;
4590 * See if we actually went secure...
4596 * Server does not support HTTP upgrade...
4599 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4601 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4602 httpAddrClose(NULL
, http
->fd
);
4611 #endif /* HAVE_SSL */
4615 * 'http_write()' - Write a buffer to a HTTP connection.
4618 static ssize_t
/* O - Number of bytes written */
4619 http_write(http_t
*http
, /* I - HTTP connection */
4620 const char *buffer
, /* I - Buffer for data */
4621 size_t length
) /* I - Number of bytes to write */
4623 ssize_t tbytes
, /* Total bytes sent */
4624 bytes
; /* Bytes sent */
4627 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4633 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4635 if (http
->timeout_value
> 0.0)
4638 struct pollfd pfd
; /* Polled file descriptor */
4640 fd_set output_set
; /* Output ready for write? */
4641 struct timeval timeout
; /* Timeout value */
4642 #endif /* HAVE_POLL */
4643 int nfds
; /* Result from select()/poll() */
4649 pfd
.events
= POLLOUT
;
4651 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4652 (errno
== EINTR
|| errno
== EAGAIN
))
4658 FD_ZERO(&output_set
);
4659 FD_SET(http
->fd
, &output_set
);
4661 timeout
.tv_sec
= http
->wait_value
/ 1000;
4662 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4664 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4667 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4668 WSAGetLastError() == WSAEWOULDBLOCK
));
4670 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4672 #endif /* HAVE_POLL */
4676 http
->error
= errno
;
4679 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4682 http
->error
= WSAEWOULDBLOCK
;
4684 http
->error
= EWOULDBLOCK
;
4694 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4696 #endif /* HAVE_SSL */
4697 bytes
= send(http
->fd
, buffer
, length
, 0);
4699 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4700 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4705 if (WSAGetLastError() == WSAEINTR
)
4707 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4709 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4712 http
->error
= WSAGetLastError();
4714 else if (WSAGetLastError() != http
->error
&&
4715 WSAGetLastError() != WSAECONNRESET
)
4717 http
->error
= WSAGetLastError();
4724 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4726 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4728 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4731 http
->error
= errno
;
4733 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4735 http
->error
= errno
;
4740 DEBUG_printf(("3http_write: error writing data (%s).",
4741 strerror(http
->error
)));
4748 length
-= (size_t)bytes
;
4752 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4755 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4762 * 'http_write_chunk()' - Write a chunked buffer.
4765 static ssize_t
/* O - Number bytes written */
4766 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4767 const char *buffer
, /* I - Buffer to write */
4768 size_t length
) /* I - Length of buffer */
4770 char header
[16]; /* Chunk header */
4771 ssize_t bytes
; /* Bytes written */
4774 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4777 * Write the chunk header, data, and trailer.
4780 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4781 if (http_write(http
, header
, strlen(header
)) < 0)
4783 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4787 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4789 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4793 if (http_write(http
, "\r\n", 2) < 0)
4795 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");