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 */
33 # endif /* HAVE_LIBZ */
40 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
42 static void http_content_coding_finish(http_t
*http
);
43 static void http_content_coding_start(http_t
*http
,
45 #endif /* HAVE_LIBZ */
46 static http_t
*http_create(const char *host
, int port
,
47 http_addrlist_t
*addrlist
, int family
,
48 http_encryption_t encryption
,
49 int blocking
, _http_mode_t mode
);
51 static void http_debug_hex(const char *prefix
, const char *buffer
,
54 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
55 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
56 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
57 static int http_send(http_t
*http
, http_state_t request
,
59 static ssize_t
http_write(http_t
*http
, const char *buffer
,
61 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
63 static off_t
http_set_length(http_t
*http
);
64 static void http_set_timeout(int fd
, double timeout
);
65 static void http_set_wait(http_t
*http
);
68 static int http_tls_upgrade(http_t
*http
);
76 static const char * const http_fields
[] =
93 "If-Unmodified-since",
108 "Authentication-Info"
113 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
114 * specified listening socket.
116 * @since CUPS 1.7/macOS 10.9@
119 http_t
* /* O - HTTP connection or @code NULL@ */
120 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
121 int blocking
) /* I - 1 if the connection should be
122 blocking, 0 otherwise */
124 http_t
*http
; /* HTTP connection */
125 http_addrlist_t addrlist
; /* Dummy address list */
126 socklen_t addrlen
; /* Length of address */
127 int val
; /* Socket option value */
131 * Range check input...
138 * Create the client connection...
141 memset(&addrlist
, 0, sizeof(addrlist
));
143 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
144 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
145 _HTTP_MODE_SERVER
)) == NULL
)
149 * Accept the client and get the remote address...
152 addrlen
= sizeof(http_addr_t
);
154 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
157 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
163 http
->hostaddr
= &(http
->addrlist
->addr
);
165 if (httpAddrLocalhost(http
->hostaddr
))
166 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
168 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
172 * Disable SIGPIPE for this socket.
176 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
177 #endif /* SO_NOSIGPIPE */
180 * Using TCP_NODELAY improves responsiveness, especially on systems
181 * with a slow loopback interface. Since we write large buffers
182 * when sending print files and requests, there shouldn't be any
183 * performance penalty for this...
187 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
191 * Close this socket when starting another process...
194 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
195 #endif /* FD_CLOEXEC */
202 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
204 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
206 * @since CUPS 1.5/macOS 10.7@
209 int /* O - 0 on success, -1 on error */
211 cups_array_t
*credentials
, /* I - Credentials array */
212 const void *data
, /* I - PEM-encoded X.509 data */
213 size_t datalen
) /* I - Length of data */
215 http_credential_t
*credential
; /* Credential data */
218 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
220 credential
->datalen
= datalen
;
222 if ((credential
->data
= malloc(datalen
)) != NULL
)
224 memcpy(credential
->data
, data
, datalen
);
225 cupsArrayAdd(credentials
, credential
);
237 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
241 httpBlocking(http_t
*http
, /* I - HTTP connection */
242 int b
) /* I - 1 = blocking, 0 = non-blocking */
253 * 'httpCheck()' - Check to see if there is a pending response from the server.
256 int /* O - 0 = no data, 1 = data available */
257 httpCheck(http_t
*http
) /* I - HTTP connection */
259 return (httpWait(http
, 0));
264 * 'httpClearCookie()' - Clear the cookie value(s).
266 * @since CUPS 1.1.19/macOS 10.3@
270 httpClearCookie(http_t
*http
) /* I - HTTP connection */
284 * 'httpClearFields()' - Clear HTTP request fields.
288 httpClearFields(http_t
*http
) /* I - HTTP connection */
290 http_field_t field
; /* Current field */
293 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
297 memset(http
->_fields
, 0, sizeof(http
->fields
));
299 for (field
= HTTP_FIELD_ACCEPT_LANGUAGE
; field
< HTTP_FIELD_MAX
; field
++)
301 if (http
->fields
[field
] && http
->fields
[field
] != http
->_fields
[field
])
302 free(http
->fields
[field
]);
304 http
->fields
[field
] = NULL
;
307 if (http
->mode
== _HTTP_MODE_CLIENT
)
309 if (http
->hostname
[0] == '/')
310 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
312 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
315 http
->expect
= (http_status_t
)0;
321 * 'httpClose()' - Close an HTTP connection.
325 httpClose(http_t
*http
) /* I - HTTP connection */
328 OM_uint32 minor_status
; /* Minor status code */
329 #endif /* HAVE_GSSAPI */
332 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
335 * Range check input...
342 * Close any open connection...
345 _httpDisconnect(http
);
348 * Free memory used...
351 httpAddrFreeList(http
->addrlist
);
357 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
358 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
360 if (http
->gssname
!= GSS_C_NO_NAME
)
361 gss_release_name(&minor_status
, &http
->gssname
);
362 #endif /* HAVE_GSSAPI */
364 #ifdef HAVE_AUTHORIZATION_H
366 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
367 #endif /* HAVE_AUTHORIZATION_H */
369 httpClearFields(http
);
371 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
372 free(http
->authstring
);
379 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
381 * @since CUPS 2.0/OS 10.10@
384 int /* O - 1 if they match, 0 if they do not */
385 httpCompareCredentials(
386 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
387 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
389 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
392 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
))
393 if (temp1
->datalen
!= temp2
->datalen
)
395 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
398 return (temp1
== temp2
);
403 * 'httpConnect()' - Connect to a HTTP server.
405 * This function is deprecated - use @link httpConnect2@ instead.
407 * @deprecated@ @exclude all@
410 http_t
* /* O - New HTTP connection */
411 httpConnect(const char *host
, /* I - Host to connect to */
412 int port
) /* I - Port number */
414 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
415 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
420 * 'httpConnect2()' - Connect to a HTTP server.
422 * @since CUPS 1.7/macOS 10.9@
425 http_t
* /* O - New HTTP connection */
427 const char *host
, /* I - Host to connect to */
428 int port
, /* I - Port number */
429 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
430 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
431 http_encryption_t encryption
, /* I - Type of encryption to use */
432 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
433 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
434 int *cancel
) /* I - Pointer to "cancel" variable */
436 http_t
*http
; /* New HTTP connection */
439 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
));
442 * Create the HTTP structure...
445 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
446 _HTTP_MODE_CLIENT
)) == NULL
)
450 * Optionally connect to the remote system...
453 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
457 * Could not connect to any known address - bail out!
467 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
469 * This function is now deprecated. Please use the @link httpConnect2@ function
472 * @deprecated@ @exclude all@
475 http_t
* /* O - New HTTP connection */
477 const char *host
, /* I - Host to connect to */
478 int port
, /* I - Port number */
479 http_encryption_t encryption
) /* I - Type of encryption to use */
481 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
482 host
, port
, encryption
));
484 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
490 * 'httpDelete()' - Send a DELETE request to the server.
493 int /* O - Status of call (0 = success) */
494 httpDelete(http_t
*http
, /* I - HTTP connection */
495 const char *uri
) /* I - URI to delete */
497 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
502 * '_httpDisconnect()' - Disconnect a HTTP connection.
506 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
511 #endif /* HAVE_SSL */
513 httpAddrClose(NULL
, http
->fd
);
520 * 'httpEncryption()' - Set the required encryption on the link.
523 int /* O - -1 on error, 0 on success */
524 httpEncryption(http_t
*http
, /* I - HTTP connection */
525 http_encryption_t e
) /* I - New encryption preference */
527 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
533 if (http
->mode
== _HTTP_MODE_CLIENT
)
535 http
->encryption
= e
;
537 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
538 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
539 return (httpReconnect2(http
, 30000, NULL
));
540 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
541 return (http_tls_upgrade(http
));
547 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
550 http
->encryption
= e
;
551 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
552 return (_httpTLSStart(http
));
557 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
561 #endif /* HAVE_SSL */
566 * 'httpError()' - Get the last error on a connection.
569 int /* O - Error code (errno) value */
570 httpError(http_t
*http
) /* I - HTTP connection */
573 return (http
->error
);
580 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
584 http_field_t
/* O - Field index */
585 httpFieldValue(const char *name
) /* I - String name */
587 int i
; /* Looping var */
590 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
591 if (!_cups_strcasecmp(name
, http_fields
[i
]))
592 return ((http_field_t
)i
);
594 return (HTTP_FIELD_UNKNOWN
);
599 * 'httpFlush()' - Flush data read from a HTTP connection.
603 httpFlush(http_t
*http
) /* I - HTTP connection */
605 char buffer
[8192]; /* Junk buffer */
606 int blocking
; /* To block or not to block */
607 http_state_t oldstate
; /* Old state */
610 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
613 * Nothing to do if we are in the "waiting" state...
616 if (http
->state
== HTTP_STATE_WAITING
)
620 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
623 blocking
= http
->blocking
;
627 * Read any data we can...
630 oldstate
= http
->state
;
631 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
634 * Restore blocking and reset the connection if we didn't get all of
635 * the remaining data...
638 http
->blocking
= blocking
;
640 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
644 * Didn't get the data back, so close the current connection.
649 http_content_coding_finish(http
);
650 #endif /* HAVE_LIBZ */
652 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
654 http
->state
= HTTP_STATE_WAITING
;
659 #endif /* HAVE_SSL */
661 httpAddrClose(NULL
, http
->fd
);
669 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
671 * @since CUPS 1.2/macOS 10.5@
674 int /* O - Bytes written or -1 on error */
675 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
677 ssize_t bytes
; /* Bytes written */
680 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
682 if (!http
|| !http
->wused
)
684 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
685 "1httpFlushWrite: No connection.");
689 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
690 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
692 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
696 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
703 * 'httpFreeCredentials()' - Free an array of credentials.
708 cups_array_t
*credentials
) /* I - Array of credentials */
710 http_credential_t
*credential
; /* Credential */
713 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
715 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
717 cupsArrayRemove(credentials
, credential
);
718 free((void *)credential
->data
);
722 cupsArrayDelete(credentials
);
727 * 'httpGet()' - Send a GET request to the server.
730 int /* O - Status of call (0 = success) */
731 httpGet(http_t
*http
, /* I - HTTP connection */
732 const char *uri
) /* I - URI to get */
734 return (http_send(http
, HTTP_STATE_GET
, uri
));
739 * 'httpGetActivity()' - Get the most recent activity for a connection.
741 * The return value is the time in seconds of the last read or write.
743 * @since CUPS 2.0/OS 10.10@
746 time_t /* O - Time of last read or write */
747 httpGetActivity(http_t
*http
) /* I - HTTP connection */
749 return (http
? http
->activity
: 0);
754 * 'httpGetAuthString()' - Get the current authorization string.
756 * The authorization string is set by @link cupsDoAuthentication@ and
757 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
758 * string to use with @link httpSetField@ for the
759 * @code HTTP_FIELD_AUTHORIZATION@ value.
761 * @since CUPS 1.3/macOS 10.5@
764 char * /* O - Authorization string */
765 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
768 return (http
->authstring
);
775 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
777 * @since CUPS 1.2/macOS 10.5@
780 int /* O - 1 if blocking, 0 if non-blocking */
781 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
783 return (http
? http
->blocking
: 0);
788 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
789 * the client and server.
791 * This function uses the value of the Accepts-Encoding HTTP header and must be
792 * called after receiving a response from the server or a request from the
793 * client. The value returned can be use in subsequent requests (for clients)
794 * or in the response (for servers) in order to compress the content stream.
796 * @since CUPS 1.7/macOS 10.9@
799 const char * /* O - Content-Coding value or
800 @code NULL@ for the identity
802 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
805 if (http
&& http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
807 int i
; /* Looping var */
808 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
809 *start
, /* Start of coding value */
810 *end
; /* End of coding value */
811 double qvalue
; /* "qvalue" for coding */
812 struct lconv
*loc
= localeconv(); /* Locale data */
813 static const char * const codings
[] =
814 { /* Supported content codings */
821 strlcpy(temp
, http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
], sizeof(temp
));
823 for (start
= temp
; *start
; start
= end
)
826 * Find the end of the coding name...
831 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
837 * Grab the qvalue as needed...
840 if (!strncmp(end
, ";q=", 3))
841 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
844 * Skip past all attributes...
848 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
854 while (*end
&& isspace(*end
& 255))
858 * Check value if it matches something we support...
864 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
865 if (!strcmp(start
, codings
[i
]))
869 #endif /* HAVE_LIBZ */
876 * 'httpGetCookie()' - Get any cookie data from the response.
878 * @since CUPS 1.1.19/macOS 10.3@
881 const char * /* O - Cookie data or @code NULL@ */
882 httpGetCookie(http_t
*http
) /* I - HTTP connection */
884 return (http
? http
->cookie
: NULL
);
889 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
891 * This function returns the encryption mode for the connection. Use the
892 * @link httpIsEncrypted@ function to determine whether a TLS session has
895 * @since CUPS 2.0/OS 10.10@
898 http_encryption_t
/* O - Current encryption mode */
899 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
901 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
906 * 'httpGetExpect()' - Get the value of the Expect header, if any.
908 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
909 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
911 * @since CUPS 1.7/macOS 10.9@
914 http_status_t
/* O - Expect: status, if any */
915 httpGetExpect(http_t
*http
) /* I - HTTP connection */
918 return (HTTP_STATUS_ERROR
);
920 return (http
->expect
);
925 * 'httpGetFd()' - Get the file descriptor associated with a connection.
927 * @since CUPS 1.2/macOS 10.5@
930 int /* O - File descriptor or -1 if none */
931 httpGetFd(http_t
*http
) /* I - HTTP connection */
933 return (http
? http
->fd
: -1);
938 * 'httpGetField()' - Get a field value from a request/response.
941 const char * /* O - Field value */
942 httpGetField(http_t
*http
, /* I - HTTP connection */
943 http_field_t field
) /* I - Field to get */
945 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
947 else if (http
->fields
[field
])
948 return (http
->fields
[field
]);
955 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
957 * @since CUPS 2.0/OS 10.10@
960 http_keepalive_t
/* O - Keep-Alive state */
961 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
963 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
968 * 'httpGetLength()' - Get the amount of data remaining from the
969 * content-length or transfer-encoding fields.
971 * This function is deprecated and will not return lengths larger than
972 * 2^31 - 1; use httpGetLength2() instead.
974 * @deprecated@ @exclude all@
977 int /* O - Content length */
978 httpGetLength(http_t
*http
) /* I - HTTP connection */
981 * Get the read content length and return the 32-bit value.
986 httpGetLength2(http
);
988 return (http
->_data_remaining
);
996 * 'httpGetLength2()' - Get the amount of data remaining from the
997 * content-length or transfer-encoding fields.
999 * This function returns the complete content length, even for
1000 * content larger than 2^31 - 1.
1002 * @since CUPS 1.2/macOS 10.5@
1005 off_t
/* O - Content length */
1006 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1008 off_t remaining
; /* Remaining length */
1011 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1016 if (http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
] && !_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1018 DEBUG_puts("4httpGetLength2: chunked request!");
1024 * The following is a hack for HTTP servers that don't send a
1025 * Content-Length or Transfer-Encoding field...
1027 * If there is no Content-Length then the connection must close
1028 * after the transfer is complete...
1031 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
] || !http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1034 * Default content length is 0 for errors and certain types of operations,
1035 * and 2^31-1 for other successful requests...
1038 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1039 http
->state
== HTTP_STATE_OPTIONS
||
1040 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1041 http
->state
== HTTP_STATE_HEAD
||
1042 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1043 http
->state
== HTTP_STATE_DELETE
||
1044 http
->state
== HTTP_STATE_TRACE
||
1045 http
->state
== HTTP_STATE_CONNECT
)
1048 remaining
= 2147483647;
1050 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1054 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1055 CUPS_LLCAST remaining
));
1063 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1065 * @since CUPS 2.0/OS 10.10@
1068 size_t /* O - Number of bytes buffered */
1069 httpGetPending(http_t
*http
) /* I - HTTP connection */
1071 return (http
? (size_t)http
->wused
: 0);
1076 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1078 * @since CUPS 2.0/OS 10.10@
1081 size_t /* O - Number of bytes available */
1082 httpGetReady(http_t
*http
) /* I - HTTP connection */
1086 else if (http
->used
> 0)
1087 return ((size_t)http
->used
);
1090 return (_httpTLSPending(http
));
1091 #endif /* HAVE_SSL */
1098 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1099 * body or current chunk.
1101 * The @link httpIsChunked@ function can be used to determine whether the
1102 * message body is chunked or fixed-length.
1104 * @since CUPS 2.0/OS 10.10@
1107 size_t /* O - Remaining bytes */
1108 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1110 return (http
? (size_t)http
->data_remaining
: 0);
1115 * 'httpGets()' - Get a line of text from a HTTP connection.
1118 char * /* O - Line or @code NULL@ */
1119 httpGets(char *line
, /* I - Line to read into */
1120 int length
, /* I - Max length of buffer */
1121 http_t
*http
) /* I - HTTP connection */
1123 char *lineptr
, /* Pointer into line */
1124 *lineend
, /* End of line */
1125 *bufptr
, /* Pointer into input buffer */
1126 *bufend
; /* Pointer to end of buffer */
1127 ssize_t bytes
; /* Number of bytes read */
1128 int eol
; /* End-of-line? */
1131 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1133 if (!http
|| !line
|| length
<= 1)
1137 * Read a line from the buffer...
1142 lineend
= line
+ length
- 1;
1145 while (lineptr
< lineend
)
1148 * Pre-load the buffer as needed...
1157 while (http
->used
== 0)
1160 * No newline; see if there is more data to be read...
1163 while (!_httpWait(http
, http
->wait_value
, 1))
1165 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1168 DEBUG_puts("3httpGets: Timed out!");
1170 http
->error
= WSAETIMEDOUT
;
1172 http
->error
= ETIMEDOUT
;
1177 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1179 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1184 * Nope, can't get a line this time...
1188 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1190 if (WSAGetLastError() == WSAEINTR
)
1192 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1194 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1197 http
->error
= WSAGetLastError();
1199 else if (WSAGetLastError() != http
->error
)
1201 http
->error
= WSAGetLastError();
1206 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1210 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1212 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1214 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1217 http
->error
= errno
;
1219 else if (errno
!= http
->error
)
1221 http
->error
= errno
;
1228 else if (bytes
== 0)
1230 http
->error
= EPIPE
;
1236 * Yup, update the amount used...
1239 http
->used
+= (int)bytes
;
1243 * Now copy as much of the current line as possible...
1246 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1247 lineptr
< lineend
&& bufptr
< bufend
;)
1249 if (*bufptr
== 0x0a)
1255 else if (*bufptr
== 0x0d)
1258 *lineptr
++ = *bufptr
++;
1261 http
->used
-= (int)(bufptr
- http
->buffer
);
1263 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1271 http
->activity
= time(NULL
);
1275 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1281 DEBUG_puts("3httpGets: No new line available!");
1288 * 'httpGetState()' - Get the current state of the HTTP request.
1291 http_state_t
/* O - HTTP state */
1292 httpGetState(http_t
*http
) /* I - HTTP connection */
1294 return (http
? http
->state
: HTTP_STATE_ERROR
);
1299 * 'httpGetStatus()' - Get the status of the last HTTP request.
1301 * @since CUPS 1.2/macOS 10.5@
1304 http_status_t
/* O - HTTP status */
1305 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1307 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1312 * 'httpGetSubField()' - Get a sub-field value.
1314 * @deprecated@ @exclude all@
1317 char * /* O - Value or @code NULL@ */
1318 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1319 http_field_t field
, /* I - Field index */
1320 const char *name
, /* I - Name of sub-field */
1321 char *value
) /* O - Value string */
1323 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1328 * 'httpGetSubField2()' - Get a sub-field value.
1330 * @since CUPS 1.2/macOS 10.5@
1333 char * /* O - Value or @code NULL@ */
1334 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1335 http_field_t field
, /* I - Field index */
1336 const char *name
, /* I - Name of sub-field */
1337 char *value
, /* O - Value string */
1338 int valuelen
) /* I - Size of value buffer */
1340 const char *fptr
; /* Pointer into field */
1341 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1342 *ptr
, /* Pointer into string buffer */
1343 *end
; /* End of value buffer */
1345 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1347 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1348 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1351 end
= value
+ valuelen
- 1;
1353 for (fptr
= http
->fields
[field
]; *fptr
;)
1356 * Skip leading whitespace...
1359 while (_cups_isspace(*fptr
))
1369 * Get the sub-field name...
1373 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1374 ptr
< (temp
+ sizeof(temp
) - 1);
1379 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1382 * Skip trailing chars up to the '='...
1385 while (_cups_isspace(*fptr
))
1395 * Skip = and leading whitespace...
1400 while (_cups_isspace(*fptr
))
1406 * Read quoted string...
1409 for (ptr
= value
, fptr
++;
1410 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1415 while (*fptr
&& *fptr
!= '\"')
1424 * Read unquoted string...
1428 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1433 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1437 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1440 * See if this is the one...
1443 if (!strcmp(name
, temp
))
1445 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1452 DEBUG_puts("3httpGetSubField2: Returning NULL");
1459 * 'httpGetVersion()' - Get the HTTP version at the other end.
1462 http_version_t
/* O - Version number */
1463 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1465 return (http
? http
->version
: HTTP_VERSION_1_0
);
1470 * 'httpHead()' - Send a HEAD request to the server.
1473 int /* O - Status of call (0 = success) */
1474 httpHead(http_t
*http
, /* I - HTTP connection */
1475 const char *uri
) /* I - URI for head */
1477 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1478 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1483 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1484 * default HTTP proxy (if any).
1488 httpInitialize(void)
1490 static int initialized
= 0; /* Have we been called before? */
1492 WSADATA winsockdata
; /* WinSock data */
1499 _cupsGlobalUnlock();
1504 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1506 #elif !defined(SO_NOSIGPIPE)
1508 * Ignore SIGPIPE signals...
1512 sigset(SIGPIPE
, SIG_IGN
);
1514 # elif defined(HAVE_SIGACTION)
1515 struct sigaction action
; /* POSIX sigaction data */
1518 memset(&action
, 0, sizeof(action
));
1519 action
.sa_handler
= SIG_IGN
;
1520 sigaction(SIGPIPE
, &action
, NULL
);
1523 signal(SIGPIPE
, SIG_IGN
);
1524 # endif /* !SO_NOSIGPIPE */
1528 _httpTLSInitialize();
1529 # endif /* HAVE_SSL */
1532 _cupsGlobalUnlock();
1537 * 'httpIsChunked()' - Report whether a message body is chunked.
1539 * This function returns non-zero if the message body is composed of
1540 * variable-length chunks.
1542 * @since CUPS 2.0/OS 10.10@
1545 int /* O - 1 if chunked, 0 if not */
1546 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1548 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1553 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1555 * This function returns non-zero if the connection is currently encrypted.
1557 * @since CUPS 2.0/OS 10.10@
1560 int /* O - 1 if encrypted, 0 if not */
1561 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1563 return (http
? http
->tls
!= NULL
: 0);
1568 * 'httpOptions()' - Send an OPTIONS request to the server.
1571 int /* O - Status of call (0 = success) */
1572 httpOptions(http_t
*http
, /* I - HTTP connection */
1573 const char *uri
) /* I - URI for options */
1575 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1580 * 'httpPeek()' - Peek at data from a HTTP connection.
1582 * This function copies available data from the given HTTP connection, reading
1583 * a buffer as needed. The data is still available for reading using
1586 * For non-blocking connections the usual timeouts apply.
1588 * @since CUPS 1.7/macOS 10.9@
1591 ssize_t
/* O - Number of bytes copied */
1592 httpPeek(http_t
*http
, /* I - HTTP connection */
1593 char *buffer
, /* I - Buffer for data */
1594 size_t length
) /* I - Maximum number of bytes */
1596 ssize_t bytes
; /* Bytes read */
1597 char len
[32]; /* Length string */
1600 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1602 if (http
== NULL
|| buffer
== NULL
)
1605 http
->activity
= time(NULL
);
1611 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1612 http
->data_remaining
<= 0)
1614 DEBUG_puts("2httpPeek: Getting chunk length...");
1616 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1618 DEBUG_puts("1httpPeek: Could not get length!");
1624 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1625 if (!httpGets(len
, sizeof(len
), http
))
1627 DEBUG_puts("1httpPeek: Could not get chunk length.");
1632 http
->data_remaining
= strtoll(len
, NULL
, 16);
1634 if (http
->data_remaining
< 0)
1636 DEBUG_puts("1httpPeek: Negative chunk length!");
1641 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1642 CUPS_LLCAST http
->data_remaining
));
1644 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1647 * A zero-length chunk ends a transfer; unless we are reading POST
1652 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1653 http_content_coding_finish(http
);
1654 #endif /* HAVE_LIBZ */
1656 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1657 httpGets(len
, sizeof(len
), http
);
1659 if (http
->state
== HTTP_STATE_POST_RECV
)
1662 http
->state
= HTTP_STATE_STATUS
;
1664 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1665 httpStateString(http
->state
)));
1668 * Prevent future reads for this request...
1671 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1675 else if (length
> (size_t)http
->data_remaining
)
1676 length
= (size_t)http
->data_remaining
;
1679 if (http
->used
== 0 &&
1680 (http
->coding
== _HTTP_CODING_IDENTITY
||
1681 (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)))
1683 if (http
->used
== 0)
1684 #endif /* HAVE_LIBZ */
1687 * Buffer small reads for better performance...
1690 ssize_t buflen
; /* Length of read for buffer */
1692 if (!http
->blocking
)
1694 while (!httpWait(http
, http
->wait_value
))
1696 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1703 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1704 buflen
= sizeof(http
->buffer
);
1706 buflen
= (ssize_t
)http
->data_remaining
;
1708 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1709 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1711 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1712 CUPS_LLCAST bytes
));
1716 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1719 http
->used
= (int)bytes
;
1724 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1726 # ifdef HAVE_INFLATECOPY
1727 int zerr
; /* Decompressor error */
1728 z_stream stream
; /* Copy of decompressor stream */
1730 if (http
->used
> 0 && ((z_stream
*)http
->stream
)->avail_in
< HTTP_MAX_BUFFER
)
1732 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- ((z_stream
*)http
->stream
)->avail_in
;
1733 /* Number of bytes to copy */
1735 if (((z_stream
*)http
->stream
)->avail_in
> 0 &&
1736 ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
1737 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
1739 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
1741 if (buflen
> (size_t)http
->data_remaining
)
1742 buflen
= (size_t)http
->data_remaining
;
1744 if (buflen
> (size_t)http
->used
)
1745 buflen
= (size_t)http
->used
;
1747 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1748 "decompression buffer.", (int)buflen
));
1750 memcpy(http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, http
->buffer
, buflen
);
1751 ((z_stream
*)http
->stream
)->avail_in
+= buflen
;
1752 http
->used
-= (int)buflen
;
1753 http
->data_remaining
-= (off_t
)buflen
;
1756 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1759 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1760 (int)((z_stream
*)http
->stream
)->avail_in
));
1762 if (inflateCopy(&stream
, (z_stream
*)http
->stream
) != Z_OK
)
1764 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1765 http
->error
= ENOMEM
;
1769 stream
.next_out
= (Bytef
*)buffer
;
1770 stream
.avail_out
= (uInt
)length
;
1772 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1773 inflateEnd(&stream
);
1777 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1779 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
1786 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
1789 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1790 "work with compressed streams.");
1792 # endif /* HAVE_INFLATECOPY */
1795 #endif /* HAVE_LIBZ */
1798 if (length
> (size_t)http
->used
)
1799 length
= (size_t)http
->used
;
1801 bytes
= (ssize_t
)length
;
1803 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1806 memcpy(buffer
, http
->buffer
, length
);
1814 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1817 http
->error
= WSAGetLastError();
1819 if (errno
== EINTR
|| errno
== EAGAIN
)
1822 http
->error
= errno
;
1825 else if (bytes
== 0)
1827 http
->error
= EPIPE
;
1836 * 'httpPost()' - Send a POST request to the server.
1839 int /* O - Status of call (0 = success) */
1840 httpPost(http_t
*http
, /* I - HTTP connection */
1841 const char *uri
) /* I - URI for post */
1843 return (http_send(http
, HTTP_STATE_POST
, uri
));
1848 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1853 int /* O - Number of bytes written */
1854 httpPrintf(http_t
*http
, /* I - HTTP connection */
1855 const char *format
, /* I - printf-style format string */
1856 ...) /* I - Additional args as needed */
1858 ssize_t bytes
; /* Number of bytes to write */
1859 char buf
[16384]; /* Buffer for formatted string */
1860 va_list ap
; /* Variable argument pointer */
1863 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1865 va_start(ap
, format
);
1866 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1869 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1871 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1872 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1877 DEBUG_puts("4httpPrintf: flushing existing data...");
1879 if (httpFlushWrite(http
) < 0)
1883 return ((int)http_write(http
, buf
, (size_t)bytes
));
1889 * 'httpPut()' - Send a PUT request to the server.
1892 int /* O - Status of call (0 = success) */
1893 httpPut(http_t
*http
, /* I - HTTP connection */
1894 const char *uri
) /* I - URI to put */
1896 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1897 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1902 * 'httpRead()' - Read data from a HTTP connection.
1904 * This function is deprecated. Use the httpRead2() function which can
1905 * read more than 2GB of data.
1907 * @deprecated@ @exclude all@
1910 int /* O - Number of bytes read */
1911 httpRead(http_t
*http
, /* I - HTTP connection */
1912 char *buffer
, /* I - Buffer for data */
1913 int length
) /* I - Maximum number of bytes */
1915 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1920 * 'httpRead2()' - Read data from a HTTP connection.
1922 * @since CUPS 1.2/macOS 10.5@
1925 ssize_t
/* O - Number of bytes read */
1926 httpRead2(http_t
*http
, /* I - HTTP connection */
1927 char *buffer
, /* I - Buffer for data */
1928 size_t length
) /* I - Maximum number of bytes */
1930 ssize_t bytes
; /* Bytes read */
1934 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
));
1936 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
));
1937 #endif /* HAVE_LIBZ */
1939 if (http
== NULL
|| buffer
== NULL
)
1942 http
->activity
= time(NULL
);
1949 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1953 if (((z_stream
*)http
->stream
)->avail_in
> 0)
1955 int zerr
; /* Decompressor error */
1957 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1958 (int)((z_stream
*)http
->stream
)->avail_in
, (int)length
));
1960 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)buffer
;
1961 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)length
;
1963 if ((zerr
= inflate((z_stream
*)http
->stream
, Z_SYNC_FLUSH
)) < Z_OK
)
1965 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
1967 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
1974 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
1976 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
1977 ((z_stream
*)http
->stream
)->avail_in
, ((z_stream
*)http
->stream
)->avail_out
,
1985 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)((z_stream
*)http
->stream
)->avail_in
;
1986 /* Additional bytes for buffer */
1990 if (((z_stream
*)http
->stream
)->avail_in
> 0 &&
1991 ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
1992 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
1994 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
1996 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
1997 "decompression buffer.", (int)buflen
));
1999 if (http
->data_remaining
> 0)
2001 if (buflen
> http
->data_remaining
)
2002 buflen
= (ssize_t
)http
->data_remaining
;
2004 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2006 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2007 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2013 else if (bytes
== 0)
2016 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2017 "decompression buffer.", CUPS_LLCAST bytes
));
2019 http
->data_remaining
-= bytes
;
2020 ((z_stream
*)http
->stream
)->avail_in
+= (uInt
)bytes
;
2022 if (http
->data_remaining
<= 0 &&
2023 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2026 * Read the trailing blank line now...
2029 char len
[32]; /* Length string */
2031 httpGets(len
, sizeof(len
), http
);
2043 #endif /* HAVE_LIBZ */
2044 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2046 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2048 http
->data_remaining
-= bytes
;
2050 if (http
->data_remaining
<= 0)
2053 * Read the trailing blank line now...
2056 char len
[32]; /* Length string */
2058 httpGets(len
, sizeof(len
), http
);
2062 else if (http
->data_remaining
<= 0)
2065 * No more data to read...
2072 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2075 if (length
> (size_t)http
->data_remaining
)
2076 length
= (size_t)http
->data_remaining
;
2078 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2080 http
->data_remaining
-= bytes
;
2082 if (http
->data_remaining
<= 0 &&
2083 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2086 * Read the trailing blank line now...
2089 char len
[32]; /* Length string */
2091 httpGets(len
, sizeof(len
), http
);
2098 (http
->coding
== _HTTP_CODING_IDENTITY
||
2099 (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)) &&
2100 #endif /* HAVE_LIBZ */
2101 ((http
->data_remaining
<= 0 &&
2102 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2103 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2106 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2107 http_content_coding_finish(http
);
2108 #endif /* HAVE_LIBZ */
2110 if (http
->state
== HTTP_STATE_POST_RECV
)
2112 else if (http
->state
== HTTP_STATE_GET_SEND
||
2113 http
->state
== HTTP_STATE_POST_SEND
)
2114 http
->state
= HTTP_STATE_WAITING
;
2116 http
->state
= HTTP_STATE_STATUS
;
2118 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2119 httpStateString(http
->state
)));
2127 * 'httpReadRequest()' - Read a HTTP request from a connection.
2129 * @since CUPS 1.7/macOS 10.9@
2132 http_state_t
/* O - New state of connection */
2133 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2134 char *uri
, /* I - URI buffer */
2135 size_t urilen
) /* I - Size of URI buffer */
2137 char line
[4096], /* HTTP request line */
2138 *req_method
, /* HTTP request method */
2139 *req_uri
, /* HTTP request URI */
2140 *req_version
; /* HTTP request version number string */
2144 * Range check input...
2147 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2152 if (!http
|| !uri
|| urilen
< 1)
2154 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2155 return (HTTP_STATE_ERROR
);
2157 else if (http
->state
!= HTTP_STATE_WAITING
)
2159 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2160 httpStateString(http
->state
)));
2161 return (HTTP_STATE_ERROR
);
2168 httpClearFields(http
);
2170 http
->activity
= time(NULL
);
2171 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2172 http
->data_remaining
= 0;
2173 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2174 http
->status
= HTTP_STATUS_OK
;
2175 http
->version
= HTTP_VERSION_1_1
;
2178 * Read a line from the socket...
2181 if (!httpGets(line
, sizeof(line
), http
))
2183 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2184 return (HTTP_STATE_ERROR
);
2189 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2190 return (HTTP_STATE_WAITING
);
2193 DEBUG_printf(("1httpReadRequest: %s", line
));
2202 while (*req_uri
&& !isspace(*req_uri
& 255))
2207 DEBUG_puts("1httpReadRequest: No request URI.");
2208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2209 return (HTTP_STATE_ERROR
);
2214 while (*req_uri
&& isspace(*req_uri
& 255))
2217 req_version
= req_uri
;
2219 while (*req_version
&& !isspace(*req_version
& 255))
2224 DEBUG_puts("1httpReadRequest: No request protocol version.");
2225 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2226 return (HTTP_STATE_ERROR
);
2229 *req_version
++ = '\0';
2231 while (*req_version
&& isspace(*req_version
& 255))
2238 if (!strcmp(req_method
, "OPTIONS"))
2239 http
->state
= HTTP_STATE_OPTIONS
;
2240 else if (!strcmp(req_method
, "GET"))
2241 http
->state
= HTTP_STATE_GET
;
2242 else if (!strcmp(req_method
, "HEAD"))
2243 http
->state
= HTTP_STATE_HEAD
;
2244 else if (!strcmp(req_method
, "POST"))
2245 http
->state
= HTTP_STATE_POST
;
2246 else if (!strcmp(req_method
, "PUT"))
2247 http
->state
= HTTP_STATE_PUT
;
2248 else if (!strcmp(req_method
, "DELETE"))
2249 http
->state
= HTTP_STATE_DELETE
;
2250 else if (!strcmp(req_method
, "TRACE"))
2251 http
->state
= HTTP_STATE_TRACE
;
2252 else if (!strcmp(req_method
, "CONNECT"))
2253 http
->state
= HTTP_STATE_CONNECT
;
2256 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2257 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2258 return (HTTP_STATE_UNKNOWN_METHOD
);
2261 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2262 httpStateString(http
->state
)));
2264 if (!strcmp(req_version
, "HTTP/1.0"))
2266 http
->version
= HTTP_VERSION_1_0
;
2267 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2269 else if (!strcmp(req_version
, "HTTP/1.1"))
2271 http
->version
= HTTP_VERSION_1_1
;
2272 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2276 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2277 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2278 return (HTTP_STATE_UNKNOWN_VERSION
);
2281 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2282 strlcpy(uri
, req_uri
, urilen
);
2284 return (http
->state
);
2289 * 'httpReconnect()' - Reconnect to a HTTP server.
2291 * This function is deprecated. Please use the @link httpReconnect2@ function
2294 * @deprecated@ @exclude all@
2297 int /* O - 0 on success, non-zero on failure */
2298 httpReconnect(http_t
*http
) /* I - HTTP connection */
2300 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2302 return (httpReconnect2(http
, 30000, NULL
));
2307 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2311 int /* O - 0 on success, non-zero on failure */
2312 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2313 int msec
, /* I - Timeout in milliseconds */
2314 int *cancel
) /* I - Pointer to "cancel" variable */
2316 http_addrlist_t
*addr
; /* Connected address */
2318 http_addrlist_t
*current
; /* Current address */
2319 char temp
[256]; /* Temporary address string */
2323 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2327 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2334 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2337 #endif /* HAVE_SSL */
2340 * Close any previously open socket...
2345 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2347 httpAddrClose(NULL
, http
->fd
);
2353 * Reset all state (except fields, which may be reused)...
2356 http
->state
= HTTP_STATE_WAITING
;
2357 http
->version
= HTTP_VERSION_1_1
;
2358 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2359 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2360 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2361 http
->_data_remaining
= 0;
2363 http
->data_remaining
= 0;
2364 http
->hostaddr
= NULL
;
2368 * Connect to the server...
2372 for (current
= http
->addrlist
; current
; current
= current
->next
)
2373 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2374 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2375 httpAddrPort(&(current
->addr
))));
2378 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2381 * Unable to connect...
2385 http
->error
= WSAGetLastError();
2387 http
->error
= errno
;
2389 http
->status
= HTTP_STATUS_ERROR
;
2391 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2392 strerror(http
->error
)));
2397 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2399 if (http
->timeout_value
> 0)
2400 http_set_timeout(http
->fd
, http
->timeout_value
);
2402 http
->hostaddr
= &(addr
->addr
);
2406 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2409 * Always do encryption via SSL.
2412 if (_httpTLSStart(http
) != 0)
2414 httpAddrClose(NULL
, http
->fd
);
2419 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2420 return (http_tls_upgrade(http
));
2421 #endif /* HAVE_SSL */
2423 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2424 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2425 httpAddrPort(http
->hostaddr
)));
2432 * 'httpSetAuthString()' - Set the current authorization string.
2434 * This function just stores a copy of the current authorization string in
2435 * the HTTP connection object. You must still call @link httpSetField@ to set
2436 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2437 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2440 * @since CUPS 1.3/macOS 10.5@
2444 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2445 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2446 const char *data
) /* I - Auth data (NULL for none) */
2449 * Range check input...
2455 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2456 free(http
->authstring
);
2458 http
->authstring
= http
->_authstring
;
2463 * Set the current authorization string...
2466 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2469 if (len
> sizeof(http
->_authstring
))
2471 if ((temp
= malloc(len
)) == NULL
)
2472 len
= sizeof(http
->_authstring
);
2474 http
->authstring
= temp
;
2478 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2480 strlcpy(http
->authstring
, scheme
, len
);
2485 * Clear the current authorization string...
2488 http
->_authstring
[0] = '\0';
2494 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2497 * @since CUPS 1.5/macOS 10.7@
2500 int /* O - Status of call (0 = success) */
2501 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2502 cups_array_t
*credentials
) /* I - Array of credentials */
2504 if (!http
|| cupsArrayCount(credentials
) < 1)
2508 _httpFreeCredentials(http
->tls_credentials
);
2510 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2511 #endif /* HAVE_SSL */
2513 return (http
->tls_credentials
? 0 : -1);
2518 * 'httpSetCookie()' - Set the cookie value(s).
2520 * @since CUPS 1.1.19/macOS 10.3@
2524 httpSetCookie(http_t
*http
, /* I - Connection */
2525 const char *cookie
) /* I - Cookie string */
2534 http
->cookie
= strdup(cookie
);
2536 http
->cookie
= NULL
;
2541 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2543 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2544 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2546 * @since CUPS 1.7/macOS 10.9@
2550 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2551 http_field_t field
, /* I - Field index */
2552 const char *value
)/* I - Value */
2554 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2556 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
2559 if (http
->default_fields
[field
])
2560 free(http
->default_fields
[field
]);
2562 http
->default_fields
[field
] = value
? strdup(value
) : NULL
;
2567 * 'httpSetExpect()' - Set the Expect: header in a request.
2569 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2572 * @since CUPS 1.2/macOS 10.5@
2576 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2577 http_status_t expect
) /* I - HTTP status to expect
2578 (@code HTTP_STATUS_CONTINUE@) */
2580 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2583 http
->expect
= expect
;
2588 * 'httpSetField()' - Set the value of an HTTP header.
2592 httpSetField(http_t
*http
, /* I - HTTP connection */
2593 http_field_t field
, /* I - Field index */
2594 const char *value
) /* I - Value */
2596 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2598 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !value
)
2601 http_add_field(http
, field
, value
, 0);
2606 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2608 * @since CUPS 2.0/OS 10.10@
2613 http_t
*http
, /* I - HTTP connection */
2614 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2617 http
->keep_alive
= keep_alive
;
2622 * 'httpSetLength()' - Set the content-length and content-encoding.
2624 * @since CUPS 1.2/macOS 10.5@
2628 httpSetLength(http_t
*http
, /* I - HTTP connection */
2629 size_t length
) /* I - Length (0 for chunked) */
2631 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2638 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
2639 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "");
2643 char len
[32]; /* Length string */
2646 snprintf(len
, sizeof(len
), CUPS_LLFMT
, CUPS_LLCAST length
);
2647 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "");
2648 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, len
);
2654 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2656 * The optional timeout callback receives both the HTTP connection and a user
2657 * data pointer and must return 1 to continue or 0 to error (time) out.
2659 * @since CUPS 1.5/macOS 10.7@
2664 http_t
*http
, /* I - HTTP connection */
2665 double timeout
, /* I - Number of seconds for timeout,
2666 must be greater than 0 */
2667 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2668 void *user_data
) /* I - User data pointer */
2670 if (!http
|| timeout
<= 0.0)
2673 http
->timeout_cb
= cb
;
2674 http
->timeout_data
= user_data
;
2675 http
->timeout_value
= timeout
;
2678 http_set_timeout(http
->fd
, timeout
);
2680 http_set_wait(http
);
2685 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2687 * @since CUPS 2.0/OS 10.10@
2691 httpShutdown(http_t
*http
) /* I - HTTP connection */
2693 if (!http
|| http
->fd
< 0)
2699 #endif /* HAVE_SSL */
2702 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2704 shutdown(http
->fd
, SHUT_RD
);
2710 * 'httpTrace()' - Send an TRACE request to the server.
2715 int /* O - Status of call (0 = success) */
2716 httpTrace(http_t
*http
, /* I - HTTP connection */
2717 const char *uri
) /* I - URI for trace */
2719 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2724 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2726 * Note: Unlike httpUpdate(), this function does not flush pending write data
2727 * and only retrieves a single status line from the HTTP connection.
2730 int /* O - 1 to continue, 0 to stop */
2731 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2732 http_status_t
*status
) /* O - Current HTTP status */
2734 char line
[32768], /* Line from connection... */
2735 *value
; /* Pointer to value on line */
2736 http_field_t field
; /* Field index */
2737 int major
, minor
; /* HTTP version numbers */
2740 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2743 * Grab a single line from the connection...
2746 if (!httpGets(line
, sizeof(line
), http
))
2748 *status
= HTTP_STATUS_ERROR
;
2752 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2754 if (line
[0] == '\0')
2757 * Blank line means the start of the data section (if any). Return
2758 * the result code, too...
2760 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2761 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2765 if (http
->status
== HTTP_STATUS_CONTINUE
)
2767 *status
= http
->status
;
2771 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2772 http
->digest_tries
= 0;
2775 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2777 if (_httpTLSStart(http
) != 0)
2779 httpAddrClose(NULL
, http
->fd
);
2781 *status
= http
->status
= HTTP_STATUS_ERROR
;
2785 *status
= HTTP_STATUS_CONTINUE
;
2788 #endif /* HAVE_SSL */
2790 if (http_set_length(http
) < 0)
2792 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2793 http
->error
= EINVAL
;
2794 http
->status
= *status
= HTTP_STATUS_ERROR
;
2798 switch (http
->state
)
2800 case HTTP_STATE_GET
:
2801 case HTTP_STATE_POST
:
2802 case HTTP_STATE_POST_RECV
:
2803 case HTTP_STATE_PUT
:
2806 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2807 httpStateString(http
->state
)));
2809 case HTTP_STATE_POST_SEND
:
2810 case HTTP_STATE_HEAD
:
2814 http
->state
= HTTP_STATE_WAITING
;
2816 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2821 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2822 http_content_coding_start(http
,
2823 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2824 #endif /* HAVE_LIBZ */
2826 *status
= http
->status
;
2829 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2832 * Got the beginning of a response...
2835 int intstatus
; /* Status value as an integer */
2837 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2839 *status
= http
->status
= HTTP_STATUS_ERROR
;
2843 httpClearFields(http
);
2845 http
->version
= (http_version_t
)(major
* 100 + minor
);
2846 *status
= http
->status
= (http_status_t
)intstatus
;
2848 else if ((value
= strchr(line
, ':')) != NULL
)
2855 while (_cups_isspace(*value
))
2858 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
2861 * Be tolerants of servers that send unknown attribute fields...
2864 if (!_cups_strcasecmp(line
, "expect"))
2867 * "Expect: 100-continue" or similar...
2870 http
->expect
= (http_status_t
)atoi(value
);
2872 else if (!_cups_strcasecmp(line
, "cookie"))
2875 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2878 httpSetCookie(http
, value
);
2880 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
2882 http_add_field(http
, field
, value
, 1);
2884 if (field
== HTTP_FIELD_AUTHENTICATION_INFO
)
2885 httpGetSubField2(http
, HTTP_FIELD_AUTHENTICATION_INFO
, "nextnonce", http
->nextnonce
, (int)sizeof(http
->nextnonce
));
2889 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
2894 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
2895 http
->error
= EINVAL
;
2896 http
->status
= *status
= HTTP_STATUS_ERROR
;
2905 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2908 http_status_t
/* O - HTTP status */
2909 httpUpdate(http_t
*http
) /* I - HTTP connection */
2911 http_status_t status
; /* Request status */
2914 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
2917 * Flush pending data, if any...
2922 DEBUG_puts("2httpUpdate: flushing buffer...");
2924 if (httpFlushWrite(http
) < 0)
2925 return (HTTP_STATUS_ERROR
);
2929 * If we haven't issued any commands, then there is nothing to "update"...
2932 if (http
->state
== HTTP_STATE_WAITING
)
2933 return (HTTP_STATUS_CONTINUE
);
2936 * Grab all of the lines we can from the connection...
2939 while (_httpUpdate(http
, &status
));
2942 * See if there was an error...
2945 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
2947 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
2948 return (http
->status
);
2953 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
2954 strerror(http
->error
)));
2955 http
->status
= HTTP_STATUS_ERROR
;
2956 return (HTTP_STATUS_ERROR
);
2960 * Return the current status...
2968 * '_httpWait()' - Wait for data available on a connection (no flush).
2971 int /* O - 1 if data is available, 0 otherwise */
2972 _httpWait(http_t
*http
, /* I - HTTP connection */
2973 int msec
, /* I - Milliseconds to wait */
2974 int usessl
) /* I - Use SSL context? */
2977 struct pollfd pfd
; /* Polled file descriptor */
2979 fd_set input_set
; /* select() input set */
2980 struct timeval timeout
; /* Timeout */
2981 #endif /* HAVE_POLL */
2982 int nfds
; /* Result from select()/poll() */
2985 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
2989 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
2994 * Check the SSL/TLS buffers for data first...
2998 if (http
->tls
&& _httpTLSPending(http
))
3000 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3003 #endif /* HAVE_SSL */
3006 * Then try doing a select() or poll() to poll the socket...
3011 pfd
.events
= POLLIN
;
3015 nfds
= poll(&pfd
, 1, msec
);
3017 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3022 FD_ZERO(&input_set
);
3023 FD_SET(http
->fd
, &input_set
);
3025 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3029 timeout
.tv_sec
= msec
/ 1000;
3030 timeout
.tv_usec
= (msec
% 1000) * 1000;
3032 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3035 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3037 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3040 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3041 WSAGetLastError() == WSAEWOULDBLOCK
));
3043 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3044 # endif /* _WIN32 */
3045 #endif /* HAVE_POLL */
3047 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3055 * 'httpWait()' - Wait for data available on a connection.
3057 * @since CUPS 1.1.19/macOS 10.3@
3060 int /* O - 1 if data is available, 0 otherwise */
3061 httpWait(http_t
*http
, /* I - HTTP connection */
3062 int msec
) /* I - Milliseconds to wait */
3065 * First see if there is data in the buffer...
3068 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3075 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3080 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
> 0)
3082 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3085 #endif /* HAVE_LIBZ */
3088 * Flush pending data, if any...
3093 DEBUG_puts("3httpWait: Flushing write buffer.");
3095 if (httpFlushWrite(http
) < 0)
3100 * If not, check the SSL/TLS buffers and do a select() on the connection...
3103 return (_httpWait(http
, msec
, 1));
3108 * 'httpWrite()' - Write data to a HTTP connection.
3110 * This function is deprecated. Use the httpWrite2() function which can
3111 * write more than 2GB of data.
3113 * @deprecated@ @exclude all@
3116 int /* O - Number of bytes written */
3117 httpWrite(http_t
*http
, /* I - HTTP connection */
3118 const char *buffer
, /* I - Buffer for data */
3119 int length
) /* I - Number of bytes to write */
3121 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3126 * 'httpWrite2()' - Write data to a HTTP connection.
3128 * @since CUPS 1.2/macOS 10.5@
3131 ssize_t
/* O - Number of bytes written */
3132 httpWrite2(http_t
*http
, /* I - HTTP connection */
3133 const char *buffer
, /* I - Buffer for data */
3134 size_t length
) /* I - Number of bytes to write */
3136 ssize_t bytes
; /* Bytes written */
3139 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3142 * Range check input...
3145 if (!http
|| !buffer
)
3147 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3152 * Mark activity on the connection...
3155 http
->activity
= time(NULL
);
3158 * Buffer small writes for better performance...
3162 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3164 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3168 http_content_coding_finish(http
);
3173 size_t slen
; /* Bytes to write */
3174 ssize_t sret
; /* Bytes written */
3176 ((z_stream
*)http
->stream
)->next_in
= (Bytef
*)buffer
;
3177 ((z_stream
*)http
->stream
)->avail_in
= (uInt
)length
;
3179 while (deflate((z_stream
*)http
->stream
, Z_NO_FLUSH
) == Z_OK
)
3181 DEBUG_printf(("1httpWrite2: avail_out=%d", ((z_stream
*)http
->stream
)->avail_out
));
3183 if (((z_stream
*)http
->stream
)->avail_out
> 0)
3186 slen
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3188 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3190 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3191 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3193 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3199 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3203 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3204 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3207 bytes
= (ssize_t
)length
;
3211 #endif /* HAVE_LIBZ */
3214 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3216 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3217 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3219 httpFlushWrite(http
);
3222 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3225 * Write to buffer...
3228 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3229 CUPS_LLCAST length
));
3231 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3232 http
->wused
+= (int)length
;
3233 bytes
= (ssize_t
)length
;
3238 * Otherwise write the data directly...
3241 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3242 CUPS_LLCAST length
));
3244 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3245 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3247 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3249 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3250 CUPS_LLCAST bytes
));
3253 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3254 http
->data_remaining
-= bytes
;
3260 * Handle end-of-request processing...
3263 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3264 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3267 * Finished with the transfer; unless we are sending POST or PUT
3272 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3273 http_content_coding_finish(http
);
3274 #endif /* HAVE_LIBZ */
3278 if (httpFlushWrite(http
) < 0)
3282 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3285 * Send a 0-length chunk at the end of the request...
3288 http_write(http
, "0\r\n\r\n", 5);
3291 * Reset the data state...
3294 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3295 http
->data_remaining
= 0;
3298 if (http
->state
== HTTP_STATE_POST_RECV
)
3300 else if (http
->state
== HTTP_STATE_POST_SEND
||
3301 http
->state
== HTTP_STATE_GET_SEND
)
3302 http
->state
= HTTP_STATE_WAITING
;
3304 http
->state
= HTTP_STATE_STATUS
;
3306 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3307 httpStateString(http
->state
)));
3310 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3317 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3319 * @since CUPS 1.7/macOS 10.9@
3322 int /* O - 0 on success, -1 on error */
3323 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3324 http_status_t status
) /* I - Status code */
3326 http_encoding_t old_encoding
; /* Old data_encoding value */
3327 off_t old_remaining
; /* Old data_remaining value */
3331 * Range check input...
3334 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3336 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3338 DEBUG_puts("1httpWriteResponse: Bad input.");
3343 * Set the various standard fields if they aren't already...
3346 if (!http
->fields
[HTTP_FIELD_DATE
])
3347 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3349 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3351 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3352 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3355 if (http
->version
== HTTP_VERSION_1_1
)
3357 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3359 if (http
->keep_alive
)
3360 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3362 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3365 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
])
3366 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3370 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3371 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3373 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3374 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3376 if (!http
->fields
[HTTP_FIELD_UPGRADE
])
3377 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3379 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
])
3380 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3382 #endif /* HAVE_SSL */
3384 if (!http
->fields
[HTTP_FIELD_SERVER
])
3385 httpSetField(http
, HTTP_FIELD_SERVER
, http
->default_fields
[HTTP_FIELD_SERVER
] ? http
->default_fields
[HTTP_FIELD_SERVER
] : CUPS_MINIMAL
);
3388 * Set the Accept-Encoding field if it isn't already...
3391 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
3392 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] ? http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] :
3394 "gzip, deflate, identity");
3397 #endif /* HAVE_LIBZ */
3400 * Send the response header...
3403 old_encoding
= http
->data_encoding
;
3404 old_remaining
= http
->data_remaining
;
3405 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3407 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100, http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3409 http
->status
= HTTP_STATUS_ERROR
;
3413 if (status
!= HTTP_STATUS_CONTINUE
)
3416 * 100 Continue doesn't have the rest of the response headers...
3419 int i
; /* Looping var */
3420 const char *value
; /* Field value */
3422 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3424 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3426 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3428 http
->status
= HTTP_STATUS_ERROR
;
3436 if (strchr(http
->cookie
, ';'))
3438 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3440 http
->status
= HTTP_STATUS_ERROR
;
3444 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3446 http
->status
= HTTP_STATUS_ERROR
;
3452 * "Click-jacking" defense (STR #4492)...
3455 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3456 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3458 http
->status
= HTTP_STATUS_ERROR
;
3463 if (httpWrite2(http
, "\r\n", 2) < 2)
3465 http
->status
= HTTP_STATUS_ERROR
;
3469 if (httpFlushWrite(http
) < 0)
3471 http
->status
= HTTP_STATUS_ERROR
;
3475 if (status
== HTTP_STATUS_CONTINUE
||
3476 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3479 * Restore the old data_encoding and data_length values...
3482 http
->data_encoding
= old_encoding
;
3483 http
->data_remaining
= old_remaining
;
3485 if (old_remaining
<= INT_MAX
)
3486 http
->_data_remaining
= (int)old_remaining
;
3488 http
->_data_remaining
= INT_MAX
;
3490 else if (http
->state
== HTTP_STATE_OPTIONS
||
3491 http
->state
== HTTP_STATE_HEAD
||
3492 http
->state
== HTTP_STATE_PUT
||
3493 http
->state
== HTTP_STATE_TRACE
||
3494 http
->state
== HTTP_STATE_CONNECT
||
3495 http
->state
== HTTP_STATE_STATUS
)
3497 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3498 "was %s.", httpStateString(http
->state
)));
3499 http
->state
= HTTP_STATE_WAITING
;
3504 * Force data_encoding and data_length to be set according to the response
3508 http_set_length(http
);
3510 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3512 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3513 "was %s.", httpStateString(http
->state
)));
3514 http
->state
= HTTP_STATE_WAITING
;
3518 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3523 * Then start any content encoding...
3526 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3527 http_content_coding_start(http
,
3528 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3529 #endif /* HAVE_LIBZ */
3538 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3542 http_add_field(http_t
*http
, /* I - HTTP connection */
3543 http_field_t field
, /* I - HTTP field */
3544 const char *value
, /* I - Value string */
3545 int append
) /* I - Append value? */
3547 char temp
[1024]; /* Temporary value string */
3548 size_t fieldlen
, /* Length of existing value */
3549 valuelen
, /* Length of value string */
3550 total
; /* Total length of string */
3553 if (field
== HTTP_FIELD_HOST
)
3556 * Special-case for Host: as we don't want a trailing "." on the hostname and
3557 * need to bracket IPv6 numeric addresses.
3560 char *ptr
= strchr(value
, ':');
3562 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3565 * Bracket IPv6 numeric addresses...
3567 * This is slightly inefficient (basically copying twice), but is an edge
3568 * case and not worth optimizing...
3571 snprintf(temp
, sizeof(temp
), "[%s]", value
);
3577 * Check for a trailing dot on the hostname...
3580 strlcpy(temp
, value
, sizeof(temp
));
3582 ptr
= temp
+ strlen(temp
) - 1;
3589 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
)
3592 if (!append
&& http
->fields
[field
])
3594 if (http
->fields
[field
] != http
->_fields
[field
])
3595 free(http
->fields
[field
]);
3597 http
->fields
[field
] = NULL
;
3600 valuelen
= strlen(value
);
3604 http
->_fields
[field
][0] = '\0';
3608 if (http
->fields
[field
])
3610 fieldlen
= strlen(http
->fields
[field
]);
3611 total
= fieldlen
+ 2 + valuelen
;
3619 if (total
< HTTP_MAX_VALUE
&& field
< HTTP_FIELD_ACCEPT_ENCODING
)
3622 * Copy short values to legacy char arrays (maintained for binary
3623 * compatibility with CUPS 1.2.x and earlier applications...)
3628 char combined
[HTTP_MAX_VALUE
];
3629 /* Combined value string */
3631 snprintf(combined
, sizeof(combined
), "%s, %s", http
->_fields
[field
], value
);
3635 strlcpy(http
->_fields
[field
], value
, sizeof(http
->_fields
[field
]));
3636 http
->fields
[field
] = http
->_fields
[field
];
3641 * Expand the field value...
3644 char *combined
; /* New value string */
3646 if ((combined
= realloc(http
->fields
[field
], total
+ 1)) != NULL
)
3648 http
->fields
[field
] = combined
;
3649 strlcat(combined
, ", ", total
+ 1);
3650 strlcat(combined
, value
, total
+ 1);
3656 * Allocate the field value...
3659 http
->fields
[field
] = strdup(value
);
3663 if (field
== HTTP_FIELD_CONTENT_ENCODING
&& http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3665 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
3666 http_content_coding_start(http
, value
);
3668 #endif /* HAVE_LIBZ */
3674 * 'http_content_coding_finish()' - Finish doing any content encoding.
3678 http_content_coding_finish(
3679 http_t
*http
) /* I - HTTP connection */
3681 int zerr
; /* Compression status */
3682 Byte dummy
[1]; /* Dummy read buffer */
3683 size_t bytes
; /* Number of bytes to write */
3686 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3687 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3689 switch (http
->coding
)
3691 case _HTTP_CODING_DEFLATE
:
3692 case _HTTP_CODING_GZIP
:
3693 ((z_stream
*)http
->stream
)->next_in
= dummy
;
3694 ((z_stream
*)http
->stream
)->avail_in
= 0;
3698 zerr
= deflate((z_stream
*)http
->stream
, Z_FINISH
);
3699 bytes
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3703 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3705 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3706 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3708 http_write(http
, (char *)http
->sbuffer
, bytes
);
3711 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3712 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3714 while (zerr
== Z_OK
);
3716 deflateEnd((z_stream
*)http
->stream
);
3718 free(http
->sbuffer
);
3721 http
->sbuffer
= NULL
;
3722 http
->stream
= NULL
;
3725 httpFlushWrite(http
);
3728 case _HTTP_CODING_INFLATE
:
3729 case _HTTP_CODING_GUNZIP
:
3730 inflateEnd((z_stream
*)http
->stream
);
3732 free(http
->sbuffer
);
3735 http
->sbuffer
= NULL
;
3736 http
->stream
= NULL
;
3743 http
->coding
= _HTTP_CODING_IDENTITY
;
3748 * 'http_content_coding_start()' - Start doing content encoding.
3752 http_content_coding_start(
3753 http_t
*http
, /* I - HTTP connection */
3754 const char *value
) /* I - Value of Content-Encoding */
3756 int zerr
; /* Error/status */
3757 _http_coding_t coding
; /* Content coding value */
3760 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3762 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3764 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3768 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3770 if (http
->state
== HTTP_STATE_GET_SEND
||
3771 http
->state
== HTTP_STATE_POST_SEND
)
3772 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3773 _HTTP_CODING_GUNZIP
;
3774 else if (http
->state
== HTTP_STATE_POST_RECV
||
3775 http
->state
== HTTP_STATE_PUT_RECV
)
3776 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3777 _HTTP_CODING_GUNZIP
;
3780 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3784 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3786 if (http
->state
== HTTP_STATE_GET_SEND
||
3787 http
->state
== HTTP_STATE_POST_SEND
)
3788 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3789 _HTTP_CODING_INFLATE
;
3790 else if (http
->state
== HTTP_STATE_POST_RECV
||
3791 http
->state
== HTTP_STATE_PUT_RECV
)
3792 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3793 _HTTP_CODING_INFLATE
;
3796 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3802 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3808 case _HTTP_CODING_DEFLATE
:
3809 case _HTTP_CODING_GZIP
:
3811 httpFlushWrite(http
);
3813 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3815 http
->status
= HTTP_STATUS_ERROR
;
3816 http
->error
= errno
;
3821 * Window size for compression is 11 bits - optimal based on PWG Raster
3822 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3826 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3828 free(http
->sbuffer
);
3830 http
->sbuffer
= NULL
;
3831 http
->status
= HTTP_STATUS_ERROR
;
3832 http
->error
= errno
;
3836 if ((zerr
= deflateInit2((z_stream
*)http
->stream
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7, Z_DEFAULT_STRATEGY
)) < Z_OK
)
3838 free(http
->sbuffer
);
3841 http
->sbuffer
= NULL
;
3842 http
->stream
= NULL
;
3843 http
->status
= HTTP_STATUS_ERROR
;
3844 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3848 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3849 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3852 case _HTTP_CODING_INFLATE
:
3853 case _HTTP_CODING_GUNZIP
:
3854 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3856 http
->status
= HTTP_STATUS_ERROR
;
3857 http
->error
= errno
;
3862 * Window size for decompression is up to 15 bits (maximum supported).
3863 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3866 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3868 free(http
->sbuffer
);
3870 http
->sbuffer
= NULL
;
3871 http
->status
= HTTP_STATUS_ERROR
;
3872 http
->error
= errno
;
3876 if ((zerr
= inflateInit2((z_stream
*)http
->stream
, coding
== _HTTP_CODING_INFLATE
? -15 : 31)) < Z_OK
)
3878 free(http
->sbuffer
);
3881 http
->sbuffer
= NULL
;
3882 http
->stream
= NULL
;
3883 http
->status
= HTTP_STATUS_ERROR
;
3884 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3888 ((z_stream
*)http
->stream
)->avail_in
= 0;
3889 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
3896 http
->coding
= coding
;
3898 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3901 #endif /* HAVE_LIBZ */
3905 * 'http_create()' - Create an unconnected HTTP connection.
3908 static http_t
* /* O - HTTP connection */
3910 const char *host
, /* I - Hostname */
3911 int port
, /* I - Port number */
3912 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3913 int family
, /* I - Address family or AF_UNSPEC */
3914 http_encryption_t encryption
, /* I - Encryption to use */
3915 int blocking
, /* I - 1 for blocking mode */
3916 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3918 http_t
*http
; /* New HTTP connection */
3919 char service
[255]; /* Service name */
3920 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3923 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
));
3925 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3931 * Lookup the host...
3936 myaddrlist
= httpAddrCopyList(addrlist
);
3940 snprintf(service
, sizeof(service
), "%d", port
);
3942 myaddrlist
= httpAddrGetList(host
, family
, service
);
3949 * Allocate memory for the structure...
3952 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3954 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3955 httpAddrFreeList(myaddrlist
);
3960 * Initialize the HTTP data...
3964 http
->activity
= time(NULL
);
3965 http
->addrlist
= myaddrlist
;
3966 http
->blocking
= blocking
;
3969 http
->gssctx
= GSS_C_NO_CONTEXT
;
3970 http
->gssname
= GSS_C_NO_NAME
;
3971 #endif /* HAVE_GSSAPI */
3972 http
->status
= HTTP_STATUS_CONTINUE
;
3973 http
->version
= HTTP_VERSION_1_1
;
3976 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3978 if (port
== 443) /* Always use encryption for https */
3979 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3981 http
->encryption
= encryption
;
3983 http_set_wait(http
);
3986 * Return the new structure...
3995 * 'http_debug_hex()' - Do a hex dump of a buffer.
3999 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4000 const char *buffer
, /* I - Buffer to dump */
4001 int bytes
) /* I - Bytes to dump */
4003 int i
, j
, /* Looping vars */
4004 ch
; /* Current character */
4005 char line
[255], /* Line buffer */
4006 *start
, /* Start of line after prefix */
4007 *ptr
; /* Pointer into line */
4010 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4013 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4015 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4016 start
= line
+ strlen(line
);
4018 for (i
= 0; i
< bytes
; i
+= 16)
4020 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4021 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4025 memcpy(ptr
, " ", 3);
4030 memcpy(ptr
, " ", 3);
4033 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4035 ch
= buffer
[i
+ j
] & 255;
4037 if (ch
< ' ' || ch
>= 127)
4051 * 'http_read()' - Read a buffer from a HTTP connection.
4053 * This function does the low-level read from the socket, retrying and timing
4057 static ssize_t
/* O - Number of bytes read or -1 on error */
4058 http_read(http_t
*http
, /* I - HTTP connection */
4059 char *buffer
, /* I - Buffer */
4060 size_t length
) /* I - Maximum bytes to read */
4062 ssize_t bytes
; /* Bytes read */
4065 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4067 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4069 while (!httpWait(http
, http
->wait_value
))
4071 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4074 DEBUG_puts("2http_read: Timeout.");
4079 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4085 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4087 #endif /* HAVE_SSL */
4088 bytes
= recv(http
->fd
, buffer
, length
, 0);
4093 if (WSAGetLastError() != WSAEINTR
)
4095 http
->error
= WSAGetLastError();
4098 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4100 if (!http
->timeout_cb
||
4101 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4103 http
->error
= WSAEWOULDBLOCK
;
4108 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4110 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4112 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4114 http
->error
= errno
;
4117 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4119 http
->error
= errno
;
4123 else if (errno
!= EINTR
)
4125 http
->error
= errno
;
4133 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4134 CUPS_LLCAST bytes
));
4137 http_debug_hex("http_read", buffer
, (int)bytes
);
4143 if (WSAGetLastError() == WSAEINTR
)
4146 http
->error
= WSAGetLastError();
4148 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4151 http
->error
= errno
;
4154 else if (bytes
== 0)
4156 http
->error
= EPIPE
;
4165 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4167 * This function reads data from the HTTP buffer or from the socket, as needed.
4170 static ssize_t
/* O - Number of bytes read or -1 on error */
4171 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4172 char *buffer
, /* I - Buffer */
4173 size_t length
) /* I - Maximum bytes to read */
4175 ssize_t bytes
; /* Bytes read */
4178 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4182 if (length
> (size_t)http
->used
)
4183 bytes
= (ssize_t
)http
->used
;
4185 bytes
= (ssize_t
)length
;
4187 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4190 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4191 http
->used
-= (int)bytes
;
4194 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4197 bytes
= http_read(http
, buffer
, length
);
4204 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4206 * This function reads and validates the chunk length, then does a buffered read
4207 * returning the number of bytes placed in the buffer.
4210 static ssize_t
/* O - Number of bytes read or -1 on error */
4211 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4212 char *buffer
, /* I - Buffer */
4213 size_t length
) /* I - Maximum bytes to read */
4215 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4217 if (http
->data_remaining
<= 0)
4219 char len
[32]; /* Length string */
4221 if (!httpGets(len
, sizeof(len
), http
))
4223 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4229 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4230 if (!httpGets(len
, sizeof(len
), http
))
4232 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4237 http
->data_remaining
= strtoll(len
, NULL
, 16);
4239 if (http
->data_remaining
< 0)
4241 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4242 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4246 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4247 len
, CUPS_LLCAST http
->data_remaining
));
4249 if (http
->data_remaining
== 0)
4252 * 0-length chunk, grab trailing blank line...
4255 httpGets(len
, sizeof(len
), http
);
4259 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4260 CUPS_LLCAST http
->data_remaining
));
4262 if (http
->data_remaining
<= 0)
4264 else if (length
> (size_t)http
->data_remaining
)
4265 length
= (size_t)http
->data_remaining
;
4267 return (http_read_buffered(http
, buffer
, length
));
4272 * 'http_send()' - Send a request with all fields and the trailing blank line.
4275 static int /* O - 0 on success, non-zero on error */
4276 http_send(http_t
*http
, /* I - HTTP connection */
4277 http_state_t request
, /* I - Request code */
4278 const char *uri
) /* I - URI */
4280 int i
; /* Looping var */
4281 char buf
[1024]; /* Encoded URI buffer */
4282 const char *value
; /* Field value */
4283 static const char * const codes
[] = /* Request code strings */
4303 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4305 if (http
== NULL
|| uri
== NULL
)
4309 * Set the User-Agent field if it isn't already...
4312 if (!http
->fields
[HTTP_FIELD_USER_AGENT
])
4314 if (http
->default_fields
[HTTP_FIELD_USER_AGENT
])
4315 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_fields
[HTTP_FIELD_USER_AGENT
]);
4317 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4321 * Set the Accept-Encoding field if it isn't already...
4324 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
] && http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
])
4325 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
]);
4328 * Encode the URI as needed...
4331 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4334 * See if we had an error the last time around; if so, reconnect...
4337 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4338 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4340 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4341 http
->fd
, http
->status
, http
->tls_upgrade
));
4343 if (httpReconnect2(http
, 30000, NULL
))
4348 * Flush any written data that is pending...
4353 if (httpFlushWrite(http
) < 0)
4354 if (httpReconnect2(http
, 30000, NULL
))
4359 * Send the request header...
4362 http
->state
= request
;
4363 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4365 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4368 http
->status
= HTTP_STATUS_CONTINUE
;
4371 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4373 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4374 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4376 #endif /* HAVE_SSL */
4378 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4380 http
->status
= HTTP_STATUS_ERROR
;
4384 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4385 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4387 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4389 if (i
== HTTP_FIELD_HOST
)
4391 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4392 httpAddrPort(http
->hostaddr
)) < 1)
4394 http
->status
= HTTP_STATUS_ERROR
;
4398 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4400 http
->status
= HTTP_STATUS_ERROR
;
4406 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4408 http
->status
= HTTP_STATUS_ERROR
;
4412 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4413 http
->mode
, http
->state
));
4415 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4416 (http
->state
== HTTP_STATE_POST_RECV
||
4417 http
->state
== HTTP_STATE_PUT_RECV
))
4418 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4420 http
->status
= HTTP_STATUS_ERROR
;
4424 if (httpPrintf(http
, "\r\n") < 1)
4426 http
->status
= HTTP_STATUS_ERROR
;
4430 if (httpFlushWrite(http
) < 0)
4433 http_set_length(http
);
4434 httpClearFields(http
);
4437 * The Kerberos and AuthRef authentication strings can only be used once...
4440 if (http
->fields
[HTTP_FIELD_AUTHORIZATION
] && http
->authstring
&&
4441 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4442 !strncmp(http
->authstring
, "AuthRef", 7)))
4444 http
->_authstring
[0] = '\0';
4446 if (http
->authstring
!= http
->_authstring
)
4447 free(http
->authstring
);
4449 http
->authstring
= http
->_authstring
;
4457 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4460 static off_t
/* O - Remainder or -1 on error */
4461 http_set_length(http_t
*http
) /* I - Connection */
4463 off_t remaining
; /* Remainder */
4466 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4468 if ((remaining
= httpGetLength2(http
)) >= 0)
4470 if (http
->mode
== _HTTP_MODE_SERVER
&&
4471 http
->state
!= HTTP_STATE_GET_SEND
&&
4472 http
->state
!= HTTP_STATE_PUT
&&
4473 http
->state
!= HTTP_STATE_POST
&&
4474 http
->state
!= HTTP_STATE_POST_SEND
)
4476 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4480 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_TRANSFER_ENCODING
), "chunked"))
4482 DEBUG_puts("1http_set_length: Setting data_encoding to "
4483 "HTTP_ENCODING_CHUNKED.");
4484 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4488 DEBUG_puts("1http_set_length: Setting data_encoding to "
4489 "HTTP_ENCODING_LENGTH.");
4490 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4493 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4494 CUPS_LLCAST remaining
));
4495 http
->data_remaining
= remaining
;
4497 if (remaining
<= INT_MAX
)
4498 http
->_data_remaining
= (int)remaining
;
4500 http
->_data_remaining
= INT_MAX
;
4507 * 'http_set_timeout()' - Set the socket timeout values.
4511 http_set_timeout(int fd
, /* I - File descriptor */
4512 double timeout
) /* I - Timeout in seconds */
4515 DWORD tv
= (DWORD
)(timeout
* 1000);
4516 /* Timeout in milliseconds */
4518 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4519 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4522 struct timeval tv
; /* Timeout in secs and usecs */
4524 tv
.tv_sec
= (int)timeout
;
4525 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4527 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4528 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4534 * 'http_set_wait()' - Set the default wait value for reads.
4538 http_set_wait(http_t
*http
) /* I - HTTP connection */
4542 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4544 if (http
->wait_value
<= 0)
4545 http
->wait_value
= 60000;
4548 http
->wait_value
= 10000;
4554 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4557 static int /* O - Status of connection */
4558 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4560 int ret
; /* Return value */
4561 http_t myhttp
; /* Local copy of HTTP data */
4564 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4567 * Flush the connection to make sure any previous "Upgrade" message
4574 * Copy the HTTP data to a local variable so we can do the OPTIONS
4575 * request without interfering with the existing request data...
4578 memcpy(&myhttp
, http
, sizeof(myhttp
));
4581 * Send an OPTIONS request to the server, requiring SSL or TLS
4582 * encryption on the link...
4585 http
->tls_upgrade
= 1;
4586 memset(http
->fields
, 0, sizeof(http
->fields
));
4587 http
->expect
= (http_status_t
)0;
4589 if (http
->hostname
[0] == '/')
4590 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
4592 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
4594 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4595 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4597 if ((ret
= httpOptions(http
, "*")) == 0)
4600 * Wait for the secure connection...
4603 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4607 * Restore the HTTP request data...
4610 memcpy(http
->_fields
, myhttp
._fields
, sizeof(http
->_fields
));
4611 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4613 http
->data_encoding
= myhttp
.data_encoding
;
4614 http
->data_remaining
= myhttp
.data_remaining
;
4615 http
->_data_remaining
= myhttp
._data_remaining
;
4616 http
->expect
= myhttp
.expect
;
4617 http
->digest_tries
= myhttp
.digest_tries
;
4618 http
->tls_upgrade
= 0;
4621 * See if we actually went secure...
4627 * Server does not support HTTP upgrade...
4630 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4632 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4633 httpAddrClose(NULL
, http
->fd
);
4642 #endif /* HAVE_SSL */
4646 * 'http_write()' - Write a buffer to a HTTP connection.
4649 static ssize_t
/* O - Number of bytes written */
4650 http_write(http_t
*http
, /* I - HTTP connection */
4651 const char *buffer
, /* I - Buffer for data */
4652 size_t length
) /* I - Number of bytes to write */
4654 ssize_t tbytes
, /* Total bytes sent */
4655 bytes
; /* Bytes sent */
4658 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4664 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4666 if (http
->timeout_value
> 0.0)
4669 struct pollfd pfd
; /* Polled file descriptor */
4671 fd_set output_set
; /* Output ready for write? */
4672 struct timeval timeout
; /* Timeout value */
4673 #endif /* HAVE_POLL */
4674 int nfds
; /* Result from select()/poll() */
4680 pfd
.events
= POLLOUT
;
4682 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4683 (errno
== EINTR
|| errno
== EAGAIN
))
4689 FD_ZERO(&output_set
);
4690 FD_SET(http
->fd
, &output_set
);
4692 timeout
.tv_sec
= http
->wait_value
/ 1000;
4693 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4695 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4698 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4699 WSAGetLastError() == WSAEWOULDBLOCK
));
4701 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4702 # endif /* _WIN32 */
4703 #endif /* HAVE_POLL */
4707 http
->error
= errno
;
4710 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4713 http
->error
= WSAEWOULDBLOCK
;
4715 http
->error
= EWOULDBLOCK
;
4725 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4727 #endif /* HAVE_SSL */
4728 bytes
= send(http
->fd
, buffer
, length
, 0);
4730 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4731 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4736 if (WSAGetLastError() == WSAEINTR
)
4738 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4740 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4743 http
->error
= WSAGetLastError();
4745 else if (WSAGetLastError() != http
->error
&&
4746 WSAGetLastError() != WSAECONNRESET
)
4748 http
->error
= WSAGetLastError();
4755 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4757 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4759 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4762 http
->error
= errno
;
4764 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4766 http
->error
= errno
;
4771 DEBUG_printf(("3http_write: error writing data (%s).",
4772 strerror(http
->error
)));
4779 length
-= (size_t)bytes
;
4783 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4786 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4793 * 'http_write_chunk()' - Write a chunked buffer.
4796 static ssize_t
/* O - Number bytes written */
4797 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4798 const char *buffer
, /* I - Buffer to write */
4799 size_t length
) /* I - Length of buffer */
4801 char header
[16]; /* Chunk header */
4802 ssize_t bytes
; /* Bytes written */
4805 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4808 * Write the chunk header, data, and trailer.
4811 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4812 if (http_write(http
, header
, strlen(header
)) < 0)
4814 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4818 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4820 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4824 if (http_write(http
, "\r\n", 2) < 0)
4826 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");