2 * HTTP routines for CUPS.
4 * Copyright © 2007-2019 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"
19 #include "debug-internal.h"
26 # include <sys/time.h>
27 # include <sys/resource.h>
31 #endif /* HAVE_POLL */
34 # endif /* HAVE_LIBZ */
41 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
43 static void http_content_coding_finish(http_t
*http
);
44 static void http_content_coding_start(http_t
*http
,
46 #endif /* HAVE_LIBZ */
47 static http_t
*http_create(const char *host
, int port
,
48 http_addrlist_t
*addrlist
, int family
,
49 http_encryption_t encryption
,
50 int blocking
, _http_mode_t mode
);
52 static void http_debug_hex(const char *prefix
, const char *buffer
,
55 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
56 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
57 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
58 static int http_send(http_t
*http
, http_state_t request
,
60 static ssize_t
http_write(http_t
*http
, const char *buffer
,
62 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
64 static off_t
http_set_length(http_t
*http
);
65 static void http_set_timeout(int fd
, double timeout
);
66 static void http_set_wait(http_t
*http
);
69 static int http_tls_upgrade(http_t
*http
);
77 static const char * const http_fields
[] =
94 "If-Unmodified-since",
109 "Authentication-Info"
114 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
115 * specified listening socket.
117 * @since CUPS 1.7/macOS 10.9@
120 http_t
* /* O - HTTP connection or @code NULL@ */
121 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
122 int blocking
) /* I - 1 if the connection should be
123 blocking, 0 otherwise */
125 http_t
*http
; /* HTTP connection */
126 http_addrlist_t addrlist
; /* Dummy address list */
127 socklen_t addrlen
; /* Length of address */
128 int val
; /* Socket option value */
132 * Range check input...
139 * Create the client connection...
142 memset(&addrlist
, 0, sizeof(addrlist
));
144 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
145 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
146 _HTTP_MODE_SERVER
)) == NULL
)
150 * Accept the client and get the remote address...
153 addrlen
= sizeof(http_addr_t
);
155 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
158 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
164 http
->hostaddr
= &(http
->addrlist
->addr
);
166 if (httpAddrLocalhost(http
->hostaddr
))
167 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
169 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
173 * Disable SIGPIPE for this socket.
177 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
178 #endif /* SO_NOSIGPIPE */
181 * Using TCP_NODELAY improves responsiveness, especially on systems
182 * with a slow loopback interface. Since we write large buffers
183 * when sending print files and requests, there shouldn't be any
184 * performance penalty for this...
188 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
192 * Close this socket when starting another process...
195 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
196 #endif /* FD_CLOEXEC */
203 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
205 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
207 * @since CUPS 1.5/macOS 10.7@
210 int /* O - 0 on success, -1 on error */
212 cups_array_t
*credentials
, /* I - Credentials array */
213 const void *data
, /* I - PEM-encoded X.509 data */
214 size_t datalen
) /* I - Length of data */
216 http_credential_t
*credential
; /* Credential data */
219 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
221 credential
->datalen
= datalen
;
223 if ((credential
->data
= malloc(datalen
)) != NULL
)
225 memcpy(credential
->data
, data
, datalen
);
226 cupsArrayAdd(credentials
, credential
);
238 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
242 httpBlocking(http_t
*http
, /* I - HTTP connection */
243 int b
) /* I - 1 = blocking, 0 = non-blocking */
254 * 'httpCheck()' - Check to see if there is a pending response from the server.
257 int /* O - 0 = no data, 1 = data available */
258 httpCheck(http_t
*http
) /* I - HTTP connection */
260 return (httpWait(http
, 0));
265 * 'httpClearCookie()' - Clear the cookie value(s).
267 * @since CUPS 1.1.19/macOS 10.3@
271 httpClearCookie(http_t
*http
) /* I - HTTP connection */
285 * 'httpClearFields()' - Clear HTTP request fields.
289 httpClearFields(http_t
*http
) /* I - HTTP connection */
291 http_field_t field
; /* Current field */
294 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
298 memset(http
->_fields
, 0, sizeof(http
->fields
));
300 for (field
= HTTP_FIELD_ACCEPT_LANGUAGE
; field
< HTTP_FIELD_MAX
; field
++)
302 if (http
->fields
[field
] && http
->fields
[field
] != http
->_fields
[field
])
303 free(http
->fields
[field
]);
305 http
->fields
[field
] = NULL
;
308 if (http
->mode
== _HTTP_MODE_CLIENT
)
310 if (http
->hostname
[0] == '/')
311 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
313 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
316 http
->expect
= (http_status_t
)0;
322 * 'httpClose()' - Close an HTTP connection.
326 httpClose(http_t
*http
) /* I - HTTP connection */
329 OM_uint32 minor_status
; /* Minor status code */
330 #endif /* HAVE_GSSAPI */
333 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
336 * Range check input...
343 * Close any open connection...
346 _httpDisconnect(http
);
349 * Free memory used...
352 httpAddrFreeList(http
->addrlist
);
358 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
359 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
361 if (http
->gssname
!= GSS_C_NO_NAME
)
362 gss_release_name(&minor_status
, &http
->gssname
);
363 #endif /* HAVE_GSSAPI */
365 #ifdef HAVE_AUTHORIZATION_H
367 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
368 #endif /* HAVE_AUTHORIZATION_H */
370 httpClearFields(http
);
372 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
373 free(http
->authstring
);
380 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
382 * @since CUPS 2.0/OS 10.10@
385 int /* O - 1 if they match, 0 if they do not */
386 httpCompareCredentials(
387 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
388 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
390 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
393 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
))
394 if (temp1
->datalen
!= temp2
->datalen
)
396 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
399 return (temp1
== temp2
);
404 * 'httpConnect()' - Connect to a HTTP server.
406 * This function is deprecated - use @link httpConnect2@ instead.
408 * @deprecated@ @exclude all@
411 http_t
* /* O - New HTTP connection */
412 httpConnect(const char *host
, /* I - Host to connect to */
413 int port
) /* I - Port number */
415 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
416 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
421 * 'httpConnect2()' - Connect to a HTTP server.
423 * @since CUPS 1.7/macOS 10.9@
426 http_t
* /* O - New HTTP connection */
428 const char *host
, /* I - Host to connect to */
429 int port
, /* I - Port number */
430 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
431 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
432 http_encryption_t encryption
, /* I - Type of encryption to use */
433 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
434 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
435 int *cancel
) /* I - Pointer to "cancel" variable */
437 http_t
*http
; /* New HTTP connection */
440 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
));
443 * Create the HTTP structure...
446 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
447 _HTTP_MODE_CLIENT
)) == NULL
)
451 * Optionally connect to the remote system...
454 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
458 * Could not connect to any known address - bail out!
468 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
470 * This function is now deprecated. Please use the @link httpConnect2@ function
473 * @deprecated@ @exclude all@
476 http_t
* /* O - New HTTP connection */
478 const char *host
, /* I - Host to connect to */
479 int port
, /* I - Port number */
480 http_encryption_t encryption
) /* I - Type of encryption to use */
482 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
483 host
, port
, encryption
));
485 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
491 * 'httpDelete()' - Send a DELETE request to the server.
494 int /* O - Status of call (0 = success) */
495 httpDelete(http_t
*http
, /* I - HTTP connection */
496 const char *uri
) /* I - URI to delete */
498 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
503 * '_httpDisconnect()' - Disconnect a HTTP connection.
507 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
512 #endif /* HAVE_SSL */
514 httpAddrClose(NULL
, http
->fd
);
521 * 'httpEncryption()' - Set the required encryption on the link.
524 int /* O - -1 on error, 0 on success */
525 httpEncryption(http_t
*http
, /* I - HTTP connection */
526 http_encryption_t e
) /* I - New encryption preference */
528 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
534 if (http
->mode
== _HTTP_MODE_CLIENT
)
536 http
->encryption
= e
;
538 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
539 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
540 return (httpReconnect2(http
, 30000, NULL
));
541 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
542 return (http_tls_upgrade(http
));
548 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
551 http
->encryption
= e
;
552 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
553 return (_httpTLSStart(http
));
558 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
562 #endif /* HAVE_SSL */
567 * 'httpError()' - Get the last error on a connection.
570 int /* O - Error code (errno) value */
571 httpError(http_t
*http
) /* I - HTTP connection */
574 return (http
->error
);
581 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
585 http_field_t
/* O - Field index */
586 httpFieldValue(const char *name
) /* I - String name */
588 int i
; /* Looping var */
591 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
592 if (!_cups_strcasecmp(name
, http_fields
[i
]))
593 return ((http_field_t
)i
);
595 return (HTTP_FIELD_UNKNOWN
);
600 * 'httpFlush()' - Flush data read from a HTTP connection.
604 httpFlush(http_t
*http
) /* I - HTTP connection */
606 char buffer
[8192]; /* Junk buffer */
607 int blocking
; /* To block or not to block */
608 http_state_t oldstate
; /* Old state */
611 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
614 * Nothing to do if we are in the "waiting" state...
617 if (http
->state
== HTTP_STATE_WAITING
)
621 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
624 blocking
= http
->blocking
;
628 * Read any data we can...
631 oldstate
= http
->state
;
632 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
635 * Restore blocking and reset the connection if we didn't get all of
636 * the remaining data...
639 http
->blocking
= blocking
;
641 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
645 * Didn't get the data back, so close the current connection.
650 http_content_coding_finish(http
);
651 #endif /* HAVE_LIBZ */
653 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
655 http
->state
= HTTP_STATE_WAITING
;
660 #endif /* HAVE_SSL */
662 httpAddrClose(NULL
, http
->fd
);
670 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
672 * @since CUPS 1.2/macOS 10.5@
675 int /* O - Bytes written or -1 on error */
676 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
678 ssize_t bytes
; /* Bytes written */
681 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
683 if (!http
|| !http
->wused
)
685 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
686 "1httpFlushWrite: No connection.");
690 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
691 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
693 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
697 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
704 * 'httpFreeCredentials()' - Free an array of credentials.
709 cups_array_t
*credentials
) /* I - Array of credentials */
711 http_credential_t
*credential
; /* Credential */
714 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
716 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
718 cupsArrayRemove(credentials
, credential
);
719 free((void *)credential
->data
);
723 cupsArrayDelete(credentials
);
728 * 'httpGet()' - Send a GET request to the server.
731 int /* O - Status of call (0 = success) */
732 httpGet(http_t
*http
, /* I - HTTP connection */
733 const char *uri
) /* I - URI to get */
735 return (http_send(http
, HTTP_STATE_GET
, uri
));
740 * 'httpGetActivity()' - Get the most recent activity for a connection.
742 * The return value is the time in seconds of the last read or write.
744 * @since CUPS 2.0/OS 10.10@
747 time_t /* O - Time of last read or write */
748 httpGetActivity(http_t
*http
) /* I - HTTP connection */
750 return (http
? http
->activity
: 0);
755 * 'httpGetAuthString()' - Get the current authorization string.
757 * The authorization string is set by @link cupsDoAuthentication@ and
758 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
759 * string to use with @link httpSetField@ for the
760 * @code HTTP_FIELD_AUTHORIZATION@ value.
762 * @since CUPS 1.3/macOS 10.5@
765 char * /* O - Authorization string */
766 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
769 return (http
->authstring
);
776 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
778 * @since CUPS 1.2/macOS 10.5@
781 int /* O - 1 if blocking, 0 if non-blocking */
782 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
784 return (http
? http
->blocking
: 0);
789 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
790 * the client and server.
792 * This function uses the value of the Accepts-Encoding HTTP header and must be
793 * called after receiving a response from the server or a request from the
794 * client. The value returned can be use in subsequent requests (for clients)
795 * or in the response (for servers) in order to compress the content stream.
797 * @since CUPS 1.7/macOS 10.9@
800 const char * /* O - Content-Coding value or
801 @code NULL@ for the identity
803 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
806 if (http
&& http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
808 int i
; /* Looping var */
809 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
810 *start
, /* Start of coding value */
811 *end
; /* End of coding value */
812 double qvalue
; /* "qvalue" for coding */
813 struct lconv
*loc
= localeconv(); /* Locale data */
814 static const char * const codings
[] =
815 { /* Supported content codings */
822 strlcpy(temp
, http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
], sizeof(temp
));
824 for (start
= temp
; *start
; start
= end
)
827 * Find the end of the coding name...
832 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
838 * Grab the qvalue as needed...
841 if (!strncmp(end
, ";q=", 3))
842 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
845 * Skip past all attributes...
849 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
855 while (*end
&& isspace(*end
& 255))
859 * Check value if it matches something we support...
865 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
866 if (!strcmp(start
, codings
[i
]))
870 #endif /* HAVE_LIBZ */
877 * 'httpGetCookie()' - Get any cookie data from the response.
879 * @since CUPS 1.1.19/macOS 10.3@
882 const char * /* O - Cookie data or @code NULL@ */
883 httpGetCookie(http_t
*http
) /* I - HTTP connection */
885 return (http
? http
->cookie
: NULL
);
890 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
892 * This function returns the encryption mode for the connection. Use the
893 * @link httpIsEncrypted@ function to determine whether a TLS session has
896 * @since CUPS 2.0/OS 10.10@
899 http_encryption_t
/* O - Current encryption mode */
900 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
902 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
907 * 'httpGetExpect()' - Get the value of the Expect header, if any.
909 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
910 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
912 * @since CUPS 1.7/macOS 10.9@
915 http_status_t
/* O - Expect: status, if any */
916 httpGetExpect(http_t
*http
) /* I - HTTP connection */
919 return (HTTP_STATUS_ERROR
);
921 return (http
->expect
);
926 * 'httpGetFd()' - Get the file descriptor associated with a connection.
928 * @since CUPS 1.2/macOS 10.5@
931 int /* O - File descriptor or -1 if none */
932 httpGetFd(http_t
*http
) /* I - HTTP connection */
934 return (http
? http
->fd
: -1);
939 * 'httpGetField()' - Get a field value from a request/response.
942 const char * /* O - Field value */
943 httpGetField(http_t
*http
, /* I - HTTP connection */
944 http_field_t field
) /* I - Field to get */
946 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
948 else if (http
->fields
[field
])
949 return (http
->fields
[field
]);
956 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
958 * @since CUPS 2.0/OS 10.10@
961 http_keepalive_t
/* O - Keep-Alive state */
962 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
964 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
969 * 'httpGetLength()' - Get the amount of data remaining from the
970 * content-length or transfer-encoding fields.
972 * This function is deprecated and will not return lengths larger than
973 * 2^31 - 1; use httpGetLength2() instead.
975 * @deprecated@ @exclude all@
978 int /* O - Content length */
979 httpGetLength(http_t
*http
) /* I - HTTP connection */
982 * Get the read content length and return the 32-bit value.
987 httpGetLength2(http
);
989 return (http
->_data_remaining
);
997 * 'httpGetLength2()' - Get the amount of data remaining from the
998 * content-length or transfer-encoding fields.
1000 * This function returns the complete content length, even for
1001 * content larger than 2^31 - 1.
1003 * @since CUPS 1.2/macOS 10.5@
1006 off_t
/* O - Content length */
1007 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1009 off_t remaining
; /* Remaining length */
1012 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1017 if (http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
] && !_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1019 DEBUG_puts("4httpGetLength2: chunked request!");
1025 * The following is a hack for HTTP servers that don't send a
1026 * Content-Length or Transfer-Encoding field...
1028 * If there is no Content-Length then the connection must close
1029 * after the transfer is complete...
1032 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
] || !http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1035 * Default content length is 0 for errors and certain types of operations,
1036 * and 2^31-1 for other successful requests...
1039 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1040 http
->state
== HTTP_STATE_OPTIONS
||
1041 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1042 http
->state
== HTTP_STATE_HEAD
||
1043 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1044 http
->state
== HTTP_STATE_DELETE
||
1045 http
->state
== HTTP_STATE_TRACE
||
1046 http
->state
== HTTP_STATE_CONNECT
)
1049 remaining
= 2147483647;
1051 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1055 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1056 CUPS_LLCAST remaining
));
1064 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1066 * @since CUPS 2.0/OS 10.10@
1069 size_t /* O - Number of bytes buffered */
1070 httpGetPending(http_t
*http
) /* I - HTTP connection */
1072 return (http
? (size_t)http
->wused
: 0);
1077 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1079 * @since CUPS 2.0/OS 10.10@
1082 size_t /* O - Number of bytes available */
1083 httpGetReady(http_t
*http
) /* I - HTTP connection */
1087 else if (http
->used
> 0)
1088 return ((size_t)http
->used
);
1091 return (_httpTLSPending(http
));
1092 #endif /* HAVE_SSL */
1099 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1100 * body or current chunk.
1102 * The @link httpIsChunked@ function can be used to determine whether the
1103 * message body is chunked or fixed-length.
1105 * @since CUPS 2.0/OS 10.10@
1108 size_t /* O - Remaining bytes */
1109 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1111 return (http
? (size_t)http
->data_remaining
: 0);
1116 * 'httpGets()' - Get a line of text from a HTTP connection.
1119 char * /* O - Line or @code NULL@ */
1120 httpGets(char *line
, /* I - Line to read into */
1121 int length
, /* I - Max length of buffer */
1122 http_t
*http
) /* I - HTTP connection */
1124 char *lineptr
, /* Pointer into line */
1125 *lineend
, /* End of line */
1126 *bufptr
, /* Pointer into input buffer */
1127 *bufend
; /* Pointer to end of buffer */
1128 ssize_t bytes
; /* Number of bytes read */
1129 int eol
; /* End-of-line? */
1132 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1134 if (!http
|| !line
|| length
<= 1)
1138 * Read a line from the buffer...
1143 lineend
= line
+ length
- 1;
1146 while (lineptr
< lineend
)
1149 * Pre-load the buffer as needed...
1158 while (http
->used
== 0)
1161 * No newline; see if there is more data to be read...
1164 while (!_httpWait(http
, http
->wait_value
, 1))
1166 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1169 DEBUG_puts("3httpGets: Timed out!");
1171 http
->error
= WSAETIMEDOUT
;
1173 http
->error
= ETIMEDOUT
;
1178 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1180 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1185 * Nope, can't get a line this time...
1189 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1191 if (WSAGetLastError() == WSAEINTR
)
1193 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1195 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1198 http
->error
= WSAGetLastError();
1200 else if (WSAGetLastError() != http
->error
)
1202 http
->error
= WSAGetLastError();
1207 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1211 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1213 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1215 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1218 http
->error
= errno
;
1220 else if (errno
!= http
->error
)
1222 http
->error
= errno
;
1229 else if (bytes
== 0)
1231 http
->error
= EPIPE
;
1237 * Yup, update the amount used...
1240 http
->used
+= (int)bytes
;
1244 * Now copy as much of the current line as possible...
1247 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1248 lineptr
< lineend
&& bufptr
< bufend
;)
1250 if (*bufptr
== 0x0a)
1256 else if (*bufptr
== 0x0d)
1259 *lineptr
++ = *bufptr
++;
1262 http
->used
-= (int)(bufptr
- http
->buffer
);
1264 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1272 http
->activity
= time(NULL
);
1276 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1282 DEBUG_puts("3httpGets: No new line available!");
1289 * 'httpGetState()' - Get the current state of the HTTP request.
1292 http_state_t
/* O - HTTP state */
1293 httpGetState(http_t
*http
) /* I - HTTP connection */
1295 return (http
? http
->state
: HTTP_STATE_ERROR
);
1300 * 'httpGetStatus()' - Get the status of the last HTTP request.
1302 * @since CUPS 1.2/macOS 10.5@
1305 http_status_t
/* O - HTTP status */
1306 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1308 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1313 * 'httpGetSubField()' - Get a sub-field value.
1315 * @deprecated@ @exclude all@
1318 char * /* O - Value or @code NULL@ */
1319 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1320 http_field_t field
, /* I - Field index */
1321 const char *name
, /* I - Name of sub-field */
1322 char *value
) /* O - Value string */
1324 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1329 * 'httpGetSubField2()' - Get a sub-field value.
1331 * @since CUPS 1.2/macOS 10.5@
1334 char * /* O - Value or @code NULL@ */
1335 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1336 http_field_t field
, /* I - Field index */
1337 const char *name
, /* I - Name of sub-field */
1338 char *value
, /* O - Value string */
1339 int valuelen
) /* I - Size of value buffer */
1341 const char *fptr
; /* Pointer into field */
1342 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1343 *ptr
, /* Pointer into string buffer */
1344 *end
; /* End of value buffer */
1346 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1351 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1352 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !http
->fields
[field
])
1355 end
= value
+ valuelen
- 1;
1357 for (fptr
= http
->fields
[field
]; *fptr
;)
1360 * Skip leading whitespace...
1363 while (_cups_isspace(*fptr
))
1373 * Get the sub-field name...
1377 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1378 ptr
< (temp
+ sizeof(temp
) - 1);
1383 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1386 * Skip trailing chars up to the '='...
1389 while (_cups_isspace(*fptr
))
1399 * Skip = and leading whitespace...
1404 while (_cups_isspace(*fptr
))
1410 * Read quoted string...
1413 for (ptr
= value
, fptr
++;
1414 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1419 while (*fptr
&& *fptr
!= '\"')
1428 * Read unquoted string...
1432 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1437 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1441 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1444 * See if this is the one...
1447 if (!strcmp(name
, temp
))
1449 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1456 DEBUG_puts("3httpGetSubField2: Returning NULL");
1463 * 'httpGetVersion()' - Get the HTTP version at the other end.
1466 http_version_t
/* O - Version number */
1467 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1469 return (http
? http
->version
: HTTP_VERSION_1_0
);
1474 * 'httpHead()' - Send a HEAD request to the server.
1477 int /* O - Status of call (0 = success) */
1478 httpHead(http_t
*http
, /* I - HTTP connection */
1479 const char *uri
) /* I - URI for head */
1481 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1482 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1487 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1488 * default HTTP proxy (if any).
1492 httpInitialize(void)
1494 static int initialized
= 0; /* Have we been called before? */
1496 WSADATA winsockdata
; /* WinSock data */
1503 _cupsGlobalUnlock();
1508 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1510 #elif !defined(SO_NOSIGPIPE)
1512 * Ignore SIGPIPE signals...
1516 sigset(SIGPIPE
, SIG_IGN
);
1518 # elif defined(HAVE_SIGACTION)
1519 struct sigaction action
; /* POSIX sigaction data */
1522 memset(&action
, 0, sizeof(action
));
1523 action
.sa_handler
= SIG_IGN
;
1524 sigaction(SIGPIPE
, &action
, NULL
);
1527 signal(SIGPIPE
, SIG_IGN
);
1528 # endif /* !SO_NOSIGPIPE */
1532 _httpTLSInitialize();
1533 # endif /* HAVE_SSL */
1536 _cupsGlobalUnlock();
1541 * 'httpIsChunked()' - Report whether a message body is chunked.
1543 * This function returns non-zero if the message body is composed of
1544 * variable-length chunks.
1546 * @since CUPS 2.0/OS 10.10@
1549 int /* O - 1 if chunked, 0 if not */
1550 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1552 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1557 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1559 * This function returns non-zero if the connection is currently encrypted.
1561 * @since CUPS 2.0/OS 10.10@
1564 int /* O - 1 if encrypted, 0 if not */
1565 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1567 return (http
? http
->tls
!= NULL
: 0);
1572 * 'httpOptions()' - Send an OPTIONS request to the server.
1575 int /* O - Status of call (0 = success) */
1576 httpOptions(http_t
*http
, /* I - HTTP connection */
1577 const char *uri
) /* I - URI for options */
1579 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1584 * 'httpPeek()' - Peek at data from a HTTP connection.
1586 * This function copies available data from the given HTTP connection, reading
1587 * a buffer as needed. The data is still available for reading using
1590 * For non-blocking connections the usual timeouts apply.
1592 * @since CUPS 1.7/macOS 10.9@
1595 ssize_t
/* O - Number of bytes copied */
1596 httpPeek(http_t
*http
, /* I - HTTP connection */
1597 char *buffer
, /* I - Buffer for data */
1598 size_t length
) /* I - Maximum number of bytes */
1600 ssize_t bytes
; /* Bytes read */
1601 char len
[32]; /* Length string */
1604 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1606 if (http
== NULL
|| buffer
== NULL
)
1609 http
->activity
= time(NULL
);
1615 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1616 http
->data_remaining
<= 0)
1618 DEBUG_puts("2httpPeek: Getting chunk length...");
1620 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1622 DEBUG_puts("1httpPeek: Could not get length!");
1628 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1629 if (!httpGets(len
, sizeof(len
), http
))
1631 DEBUG_puts("1httpPeek: Could not get chunk length.");
1636 http
->data_remaining
= strtoll(len
, NULL
, 16);
1638 if (http
->data_remaining
< 0)
1640 DEBUG_puts("1httpPeek: Negative chunk length!");
1645 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1646 CUPS_LLCAST http
->data_remaining
));
1648 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1651 * A zero-length chunk ends a transfer; unless we are reading POST
1656 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1657 http_content_coding_finish(http
);
1658 #endif /* HAVE_LIBZ */
1660 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1661 httpGets(len
, sizeof(len
), http
);
1663 if (http
->state
== HTTP_STATE_POST_RECV
)
1666 http
->state
= HTTP_STATE_STATUS
;
1668 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1669 httpStateString(http
->state
)));
1672 * Prevent future reads for this request...
1675 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1679 else if (length
> (size_t)http
->data_remaining
)
1680 length
= (size_t)http
->data_remaining
;
1683 if (http
->used
== 0 &&
1684 (http
->coding
== _HTTP_CODING_IDENTITY
||
1685 (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)))
1687 if (http
->used
== 0)
1688 #endif /* HAVE_LIBZ */
1691 * Buffer small reads for better performance...
1694 ssize_t buflen
; /* Length of read for buffer */
1696 if (!http
->blocking
)
1698 while (!httpWait(http
, http
->wait_value
))
1700 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1707 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1708 buflen
= sizeof(http
->buffer
);
1710 buflen
= (ssize_t
)http
->data_remaining
;
1712 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1713 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1715 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1716 CUPS_LLCAST bytes
));
1720 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1723 http
->used
= (int)bytes
;
1728 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1730 # ifdef HAVE_INFLATECOPY
1731 int zerr
; /* Decompressor error */
1732 z_stream stream
; /* Copy of decompressor stream */
1734 if (http
->used
> 0 && ((z_stream
*)http
->stream
)->avail_in
< HTTP_MAX_BUFFER
)
1736 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- ((z_stream
*)http
->stream
)->avail_in
;
1737 /* Number of bytes to copy */
1739 if (((z_stream
*)http
->stream
)->avail_in
> 0 &&
1740 ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
1741 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
1743 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
1745 if (buflen
> (size_t)http
->data_remaining
)
1746 buflen
= (size_t)http
->data_remaining
;
1748 if (buflen
> (size_t)http
->used
)
1749 buflen
= (size_t)http
->used
;
1751 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1752 "decompression buffer.", (int)buflen
));
1754 memcpy(http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, http
->buffer
, buflen
);
1755 ((z_stream
*)http
->stream
)->avail_in
+= buflen
;
1756 http
->used
-= (int)buflen
;
1757 http
->data_remaining
-= (off_t
)buflen
;
1760 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1763 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1764 (int)((z_stream
*)http
->stream
)->avail_in
));
1766 if (inflateCopy(&stream
, (z_stream
*)http
->stream
) != Z_OK
)
1768 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1769 http
->error
= ENOMEM
;
1773 stream
.next_out
= (Bytef
*)buffer
;
1774 stream
.avail_out
= (uInt
)length
;
1776 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1777 inflateEnd(&stream
);
1781 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1783 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
1790 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
1793 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1794 "work with compressed streams.");
1796 # endif /* HAVE_INFLATECOPY */
1799 #endif /* HAVE_LIBZ */
1802 if (length
> (size_t)http
->used
)
1803 length
= (size_t)http
->used
;
1805 bytes
= (ssize_t
)length
;
1807 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1810 memcpy(buffer
, http
->buffer
, length
);
1818 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1821 http
->error
= WSAGetLastError();
1823 if (errno
== EINTR
|| errno
== EAGAIN
)
1826 http
->error
= errno
;
1829 else if (bytes
== 0)
1831 http
->error
= EPIPE
;
1840 * 'httpPost()' - Send a POST request to the server.
1843 int /* O - Status of call (0 = success) */
1844 httpPost(http_t
*http
, /* I - HTTP connection */
1845 const char *uri
) /* I - URI for post */
1847 return (http_send(http
, HTTP_STATE_POST
, uri
));
1852 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1857 int /* O - Number of bytes written */
1858 httpPrintf(http_t
*http
, /* I - HTTP connection */
1859 const char *format
, /* I - printf-style format string */
1860 ...) /* I - Additional args as needed */
1862 ssize_t bytes
; /* Number of bytes to write */
1863 char buf
[65536]; /* Buffer for formatted string */
1864 va_list ap
; /* Variable argument pointer */
1867 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1869 va_start(ap
, format
);
1870 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1873 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1875 if (bytes
> (ssize_t
)(sizeof(buf
) - 1))
1877 http
->error
= ENOMEM
;
1880 else if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1881 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1886 DEBUG_puts("4httpPrintf: flushing existing data...");
1888 if (httpFlushWrite(http
) < 0)
1892 return ((int)http_write(http
, buf
, (size_t)bytes
));
1898 * 'httpPut()' - Send a PUT request to the server.
1901 int /* O - Status of call (0 = success) */
1902 httpPut(http_t
*http
, /* I - HTTP connection */
1903 const char *uri
) /* I - URI to put */
1905 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1906 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1911 * 'httpRead()' - Read data from a HTTP connection.
1913 * This function is deprecated. Use the httpRead2() function which can
1914 * read more than 2GB of data.
1916 * @deprecated@ @exclude all@
1919 int /* O - Number of bytes read */
1920 httpRead(http_t
*http
, /* I - HTTP connection */
1921 char *buffer
, /* I - Buffer for data */
1922 int length
) /* I - Maximum number of bytes */
1924 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1929 * 'httpRead2()' - Read data from a HTTP connection.
1931 * @since CUPS 1.2/macOS 10.5@
1934 ssize_t
/* O - Number of bytes read */
1935 httpRead2(http_t
*http
, /* I - HTTP connection */
1936 char *buffer
, /* I - Buffer for data */
1937 size_t length
) /* I - Maximum number of bytes */
1939 ssize_t bytes
; /* Bytes read */
1943 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
));
1945 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
));
1946 #endif /* HAVE_LIBZ */
1948 if (http
== NULL
|| buffer
== NULL
)
1951 http
->activity
= time(NULL
);
1958 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1962 if (((z_stream
*)http
->stream
)->avail_in
> 0)
1964 int zerr
; /* Decompressor error */
1966 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1967 (int)((z_stream
*)http
->stream
)->avail_in
, (int)length
));
1969 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)buffer
;
1970 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)length
;
1972 if ((zerr
= inflate((z_stream
*)http
->stream
, Z_SYNC_FLUSH
)) < Z_OK
)
1974 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
1976 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)((z_stream
*)http
->stream
)->avail_in
);
1983 bytes
= (ssize_t
)(length
- ((z_stream
*)http
->stream
)->avail_out
);
1985 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
1986 ((z_stream
*)http
->stream
)->avail_in
, ((z_stream
*)http
->stream
)->avail_out
,
1994 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)((z_stream
*)http
->stream
)->avail_in
;
1995 /* Additional bytes for buffer */
1999 if (((z_stream
*)http
->stream
)->avail_in
> 0 &&
2000 ((z_stream
*)http
->stream
)->next_in
> http
->sbuffer
)
2001 memmove(http
->sbuffer
, ((z_stream
*)http
->stream
)->next_in
, ((z_stream
*)http
->stream
)->avail_in
);
2003 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
2005 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2006 "decompression buffer.", (int)buflen
));
2008 if (http
->data_remaining
> 0)
2010 if (buflen
> http
->data_remaining
)
2011 buflen
= (ssize_t
)http
->data_remaining
;
2013 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2015 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2016 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ ((z_stream
*)http
->stream
)->avail_in
, (size_t)buflen
);
2022 else if (bytes
== 0)
2025 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2026 "decompression buffer.", CUPS_LLCAST bytes
));
2028 http
->data_remaining
-= bytes
;
2029 ((z_stream
*)http
->stream
)->avail_in
+= (uInt
)bytes
;
2031 if (http
->data_remaining
<= 0 &&
2032 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2035 * Read the trailing blank line now...
2038 char len
[32]; /* Length string */
2040 httpGets(len
, sizeof(len
), http
);
2052 #endif /* HAVE_LIBZ */
2053 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2055 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2057 http
->data_remaining
-= bytes
;
2059 if (http
->data_remaining
<= 0)
2062 * Read the trailing blank line now...
2065 char len
[32]; /* Length string */
2067 httpGets(len
, sizeof(len
), http
);
2071 else if (http
->data_remaining
<= 0)
2074 * No more data to read...
2081 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2084 if (length
> (size_t)http
->data_remaining
)
2085 length
= (size_t)http
->data_remaining
;
2087 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2089 http
->data_remaining
-= bytes
;
2091 if (http
->data_remaining
<= 0 &&
2092 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2095 * Read the trailing blank line now...
2098 char len
[32]; /* Length string */
2100 httpGets(len
, sizeof(len
), http
);
2107 (http
->coding
== _HTTP_CODING_IDENTITY
||
2108 (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
== 0)) &&
2109 #endif /* HAVE_LIBZ */
2110 ((http
->data_remaining
<= 0 &&
2111 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2112 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2115 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2116 http_content_coding_finish(http
);
2117 #endif /* HAVE_LIBZ */
2119 if (http
->state
== HTTP_STATE_POST_RECV
)
2121 else if (http
->state
== HTTP_STATE_GET_SEND
||
2122 http
->state
== HTTP_STATE_POST_SEND
)
2123 http
->state
= HTTP_STATE_WAITING
;
2125 http
->state
= HTTP_STATE_STATUS
;
2127 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2128 httpStateString(http
->state
)));
2136 * 'httpReadRequest()' - Read a HTTP request from a connection.
2138 * @since CUPS 1.7/macOS 10.9@
2141 http_state_t
/* O - New state of connection */
2142 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2143 char *uri
, /* I - URI buffer */
2144 size_t urilen
) /* I - Size of URI buffer */
2146 char line
[4096], /* HTTP request line */
2147 *req_method
, /* HTTP request method */
2148 *req_uri
, /* HTTP request URI */
2149 *req_version
; /* HTTP request version number string */
2153 * Range check input...
2156 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2161 if (!http
|| !uri
|| urilen
< 1)
2163 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2164 return (HTTP_STATE_ERROR
);
2166 else if (http
->state
!= HTTP_STATE_WAITING
)
2168 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2169 httpStateString(http
->state
)));
2170 return (HTTP_STATE_ERROR
);
2177 httpClearFields(http
);
2179 http
->activity
= time(NULL
);
2180 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2181 http
->data_remaining
= 0;
2182 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2183 http
->status
= HTTP_STATUS_OK
;
2184 http
->version
= HTTP_VERSION_1_1
;
2187 * Read a line from the socket...
2190 if (!httpGets(line
, sizeof(line
), http
))
2192 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2193 return (HTTP_STATE_ERROR
);
2198 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2199 return (HTTP_STATE_WAITING
);
2202 DEBUG_printf(("1httpReadRequest: %s", line
));
2211 while (*req_uri
&& !isspace(*req_uri
& 255))
2216 DEBUG_puts("1httpReadRequest: No request URI.");
2217 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2218 return (HTTP_STATE_ERROR
);
2223 while (*req_uri
&& isspace(*req_uri
& 255))
2226 req_version
= req_uri
;
2228 while (*req_version
&& !isspace(*req_version
& 255))
2233 DEBUG_puts("1httpReadRequest: No request protocol version.");
2234 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2235 return (HTTP_STATE_ERROR
);
2238 *req_version
++ = '\0';
2240 while (*req_version
&& isspace(*req_version
& 255))
2247 if (!strcmp(req_method
, "OPTIONS"))
2248 http
->state
= HTTP_STATE_OPTIONS
;
2249 else if (!strcmp(req_method
, "GET"))
2250 http
->state
= HTTP_STATE_GET
;
2251 else if (!strcmp(req_method
, "HEAD"))
2252 http
->state
= HTTP_STATE_HEAD
;
2253 else if (!strcmp(req_method
, "POST"))
2254 http
->state
= HTTP_STATE_POST
;
2255 else if (!strcmp(req_method
, "PUT"))
2256 http
->state
= HTTP_STATE_PUT
;
2257 else if (!strcmp(req_method
, "DELETE"))
2258 http
->state
= HTTP_STATE_DELETE
;
2259 else if (!strcmp(req_method
, "TRACE"))
2260 http
->state
= HTTP_STATE_TRACE
;
2261 else if (!strcmp(req_method
, "CONNECT"))
2262 http
->state
= HTTP_STATE_CONNECT
;
2265 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2266 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2267 return (HTTP_STATE_UNKNOWN_METHOD
);
2270 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2271 httpStateString(http
->state
)));
2273 if (!strcmp(req_version
, "HTTP/1.0"))
2275 http
->version
= HTTP_VERSION_1_0
;
2276 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2278 else if (!strcmp(req_version
, "HTTP/1.1"))
2280 http
->version
= HTTP_VERSION_1_1
;
2281 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2285 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2286 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2287 return (HTTP_STATE_UNKNOWN_VERSION
);
2290 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2291 strlcpy(uri
, req_uri
, urilen
);
2293 return (http
->state
);
2298 * 'httpReconnect()' - Reconnect to a HTTP server.
2300 * This function is deprecated. Please use the @link httpReconnect2@ function
2303 * @deprecated@ @exclude all@
2306 int /* O - 0 on success, non-zero on failure */
2307 httpReconnect(http_t
*http
) /* I - HTTP connection */
2309 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2311 return (httpReconnect2(http
, 30000, NULL
));
2316 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2320 int /* O - 0 on success, non-zero on failure */
2321 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2322 int msec
, /* I - Timeout in milliseconds */
2323 int *cancel
) /* I - Pointer to "cancel" variable */
2325 http_addrlist_t
*addr
; /* Connected address */
2327 http_addrlist_t
*current
; /* Current address */
2328 char temp
[256]; /* Temporary address string */
2332 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2336 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2343 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2346 #endif /* HAVE_SSL */
2349 * Close any previously open socket...
2354 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2356 httpAddrClose(NULL
, http
->fd
);
2362 * Reset all state (except fields, which may be reused)...
2365 http
->state
= HTTP_STATE_WAITING
;
2366 http
->version
= HTTP_VERSION_1_1
;
2367 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2368 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2369 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2370 http
->_data_remaining
= 0;
2372 http
->data_remaining
= 0;
2373 http
->hostaddr
= NULL
;
2377 * Connect to the server...
2381 for (current
= http
->addrlist
; current
; current
= current
->next
)
2382 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2383 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2384 httpAddrPort(&(current
->addr
))));
2387 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2390 * Unable to connect...
2394 http
->error
= WSAGetLastError();
2396 http
->error
= errno
;
2398 http
->status
= HTTP_STATUS_ERROR
;
2400 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2401 strerror(http
->error
)));
2406 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2408 if (http
->timeout_value
> 0)
2409 http_set_timeout(http
->fd
, http
->timeout_value
);
2411 http
->hostaddr
= &(addr
->addr
);
2415 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2418 * Always do encryption via SSL.
2421 if (_httpTLSStart(http
) != 0)
2423 httpAddrClose(NULL
, http
->fd
);
2428 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2429 return (http_tls_upgrade(http
));
2430 #endif /* HAVE_SSL */
2432 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2433 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2434 httpAddrPort(http
->hostaddr
)));
2441 * 'httpSetAuthString()' - Set the current authorization string.
2443 * This function just stores a copy of the current authorization string in
2444 * the HTTP connection object. You must still call @link httpSetField@ to set
2445 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2446 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2449 * @since CUPS 1.3/macOS 10.5@
2453 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2454 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2455 const char *data
) /* I - Auth data (NULL for none) */
2458 * Range check input...
2464 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2465 free(http
->authstring
);
2467 http
->authstring
= http
->_authstring
;
2472 * Set the current authorization string...
2475 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2478 if (len
> sizeof(http
->_authstring
))
2480 if ((temp
= malloc(len
)) == NULL
)
2481 len
= sizeof(http
->_authstring
);
2483 http
->authstring
= temp
;
2487 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2489 strlcpy(http
->authstring
, scheme
, len
);
2494 * Clear the current authorization string...
2497 http
->_authstring
[0] = '\0';
2503 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2506 * @since CUPS 1.5/macOS 10.7@
2509 int /* O - Status of call (0 = success) */
2510 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2511 cups_array_t
*credentials
) /* I - Array of credentials */
2513 if (!http
|| cupsArrayCount(credentials
) < 1)
2517 _httpFreeCredentials(http
->tls_credentials
);
2519 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2520 #endif /* HAVE_SSL */
2522 return (http
->tls_credentials
? 0 : -1);
2527 * 'httpSetCookie()' - Set the cookie value(s).
2529 * @since CUPS 1.1.19/macOS 10.3@
2533 httpSetCookie(http_t
*http
, /* I - Connection */
2534 const char *cookie
) /* I - Cookie string */
2543 http
->cookie
= strdup(cookie
);
2545 http
->cookie
= NULL
;
2550 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2552 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2553 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2555 * @since CUPS 1.7/macOS 10.9@
2559 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2560 http_field_t field
, /* I - Field index */
2561 const char *value
)/* I - Value */
2563 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2565 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
2568 if (http
->default_fields
[field
])
2569 free(http
->default_fields
[field
]);
2571 http
->default_fields
[field
] = value
? strdup(value
) : NULL
;
2576 * 'httpSetExpect()' - Set the Expect: header in a request.
2578 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2581 * @since CUPS 1.2/macOS 10.5@
2585 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2586 http_status_t expect
) /* I - HTTP status to expect
2587 (@code HTTP_STATUS_CONTINUE@) */
2589 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2592 http
->expect
= expect
;
2597 * 'httpSetField()' - Set the value of an HTTP header.
2601 httpSetField(http_t
*http
, /* I - HTTP connection */
2602 http_field_t field
, /* I - Field index */
2603 const char *value
) /* I - Value */
2605 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2607 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !value
)
2610 http_add_field(http
, field
, value
, 0);
2615 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2617 * @since CUPS 2.0/OS 10.10@
2622 http_t
*http
, /* I - HTTP connection */
2623 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2626 http
->keep_alive
= keep_alive
;
2631 * 'httpSetLength()' - Set the content-length and content-encoding.
2633 * @since CUPS 1.2/macOS 10.5@
2637 httpSetLength(http_t
*http
, /* I - HTTP connection */
2638 size_t length
) /* I - Length (0 for chunked) */
2640 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2647 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
2648 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "");
2652 char len
[32]; /* Length string */
2655 snprintf(len
, sizeof(len
), CUPS_LLFMT
, CUPS_LLCAST length
);
2656 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "");
2657 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, len
);
2663 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2665 * The optional timeout callback receives both the HTTP connection and a user
2666 * data pointer and must return 1 to continue or 0 to error (time) out.
2668 * @since CUPS 1.5/macOS 10.7@
2673 http_t
*http
, /* I - HTTP connection */
2674 double timeout
, /* I - Number of seconds for timeout,
2675 must be greater than 0 */
2676 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2677 void *user_data
) /* I - User data pointer */
2679 if (!http
|| timeout
<= 0.0)
2682 http
->timeout_cb
= cb
;
2683 http
->timeout_data
= user_data
;
2684 http
->timeout_value
= timeout
;
2687 http_set_timeout(http
->fd
, timeout
);
2689 http_set_wait(http
);
2694 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2696 * @since CUPS 2.0/OS 10.10@
2700 httpShutdown(http_t
*http
) /* I - HTTP connection */
2702 if (!http
|| http
->fd
< 0)
2708 #endif /* HAVE_SSL */
2711 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2713 shutdown(http
->fd
, SHUT_RD
);
2719 * 'httpTrace()' - Send an TRACE request to the server.
2724 int /* O - Status of call (0 = success) */
2725 httpTrace(http_t
*http
, /* I - HTTP connection */
2726 const char *uri
) /* I - URI for trace */
2728 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2733 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2735 * Note: Unlike httpUpdate(), this function does not flush pending write data
2736 * and only retrieves a single status line from the HTTP connection.
2739 int /* O - 1 to continue, 0 to stop */
2740 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2741 http_status_t
*status
) /* O - Current HTTP status */
2743 char line
[32768], /* Line from connection... */
2744 *value
; /* Pointer to value on line */
2745 http_field_t field
; /* Field index */
2746 int major
, minor
; /* HTTP version numbers */
2749 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2752 * Grab a single line from the connection...
2755 if (!httpGets(line
, sizeof(line
), http
))
2757 *status
= HTTP_STATUS_ERROR
;
2761 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2763 if (line
[0] == '\0')
2766 * Blank line means the start of the data section (if any). Return
2767 * the result code, too...
2769 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2770 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2774 if (http
->status
== HTTP_STATUS_CONTINUE
)
2776 *status
= http
->status
;
2780 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2781 http
->digest_tries
= 0;
2784 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2786 if (_httpTLSStart(http
) != 0)
2788 httpAddrClose(NULL
, http
->fd
);
2790 *status
= http
->status
= HTTP_STATUS_ERROR
;
2794 *status
= HTTP_STATUS_CONTINUE
;
2797 #endif /* HAVE_SSL */
2799 if (http_set_length(http
) < 0)
2801 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2802 http
->error
= EINVAL
;
2803 http
->status
= *status
= HTTP_STATUS_ERROR
;
2807 switch (http
->state
)
2809 case HTTP_STATE_GET
:
2810 case HTTP_STATE_POST
:
2811 case HTTP_STATE_POST_RECV
:
2812 case HTTP_STATE_PUT
:
2815 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2816 httpStateString(http
->state
)));
2818 case HTTP_STATE_POST_SEND
:
2819 case HTTP_STATE_HEAD
:
2823 http
->state
= HTTP_STATE_WAITING
;
2825 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2830 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2831 http_content_coding_start(http
,
2832 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2833 #endif /* HAVE_LIBZ */
2835 *status
= http
->status
;
2838 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2841 * Got the beginning of a response...
2844 int intstatus
; /* Status value as an integer */
2846 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2848 *status
= http
->status
= HTTP_STATUS_ERROR
;
2852 httpClearFields(http
);
2854 http
->version
= (http_version_t
)(major
* 100 + minor
);
2855 *status
= http
->status
= (http_status_t
)intstatus
;
2857 else if ((value
= strchr(line
, ':')) != NULL
)
2864 while (_cups_isspace(*value
))
2867 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
2870 * Be tolerants of servers that send unknown attribute fields...
2873 if (!_cups_strcasecmp(line
, "expect"))
2876 * "Expect: 100-continue" or similar...
2879 http
->expect
= (http_status_t
)atoi(value
);
2881 else if (!_cups_strcasecmp(line
, "cookie"))
2884 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2887 httpSetCookie(http
, value
);
2889 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
2891 http_add_field(http
, field
, value
, 1);
2893 if (field
== HTTP_FIELD_AUTHENTICATION_INFO
)
2894 httpGetSubField2(http
, HTTP_FIELD_AUTHENTICATION_INFO
, "nextnonce", http
->nextnonce
, (int)sizeof(http
->nextnonce
));
2898 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
2903 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
2904 http
->error
= EINVAL
;
2905 http
->status
= *status
= HTTP_STATUS_ERROR
;
2914 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2917 http_status_t
/* O - HTTP status */
2918 httpUpdate(http_t
*http
) /* I - HTTP connection */
2920 http_status_t status
; /* Request status */
2923 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
2926 * Flush pending data, if any...
2931 DEBUG_puts("2httpUpdate: flushing buffer...");
2933 if (httpFlushWrite(http
) < 0)
2934 return (HTTP_STATUS_ERROR
);
2938 * If we haven't issued any commands, then there is nothing to "update"...
2941 if (http
->state
== HTTP_STATE_WAITING
)
2942 return (HTTP_STATUS_CONTINUE
);
2945 * Grab all of the lines we can from the connection...
2948 while (_httpUpdate(http
, &status
));
2951 * See if there was an error...
2954 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
2956 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
2957 return (http
->status
);
2962 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
2963 strerror(http
->error
)));
2964 http
->status
= HTTP_STATUS_ERROR
;
2965 return (HTTP_STATUS_ERROR
);
2969 * Return the current status...
2977 * '_httpWait()' - Wait for data available on a connection (no flush).
2980 int /* O - 1 if data is available, 0 otherwise */
2981 _httpWait(http_t
*http
, /* I - HTTP connection */
2982 int msec
, /* I - Milliseconds to wait */
2983 int usessl
) /* I - Use SSL context? */
2986 struct pollfd pfd
; /* Polled file descriptor */
2988 fd_set input_set
; /* select() input set */
2989 struct timeval timeout
; /* Timeout */
2990 #endif /* HAVE_POLL */
2991 int nfds
; /* Result from select()/poll() */
2994 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
2998 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3003 * Check the SSL/TLS buffers for data first...
3007 if (http
->tls
&& _httpTLSPending(http
))
3009 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3012 #endif /* HAVE_SSL */
3015 * Then try doing a select() or poll() to poll the socket...
3020 pfd
.events
= POLLIN
;
3024 nfds
= poll(&pfd
, 1, msec
);
3026 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3031 FD_ZERO(&input_set
);
3032 FD_SET(http
->fd
, &input_set
);
3034 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3038 timeout
.tv_sec
= msec
/ 1000;
3039 timeout
.tv_usec
= (msec
% 1000) * 1000;
3041 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3044 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3046 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3049 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3050 WSAGetLastError() == WSAEWOULDBLOCK
));
3052 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3053 # endif /* _WIN32 */
3054 #endif /* HAVE_POLL */
3056 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3064 * 'httpWait()' - Wait for data available on a connection.
3066 * @since CUPS 1.1.19/macOS 10.3@
3069 int /* O - 1 if data is available, 0 otherwise */
3070 httpWait(http_t
*http
, /* I - HTTP connection */
3071 int msec
) /* I - Milliseconds to wait */
3074 * First see if there is data in the buffer...
3077 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3084 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3089 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& ((z_stream
*)http
->stream
)->avail_in
> 0)
3091 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3094 #endif /* HAVE_LIBZ */
3097 * Flush pending data, if any...
3102 DEBUG_puts("3httpWait: Flushing write buffer.");
3104 if (httpFlushWrite(http
) < 0)
3109 * If not, check the SSL/TLS buffers and do a select() on the connection...
3112 return (_httpWait(http
, msec
, 1));
3117 * 'httpWrite()' - Write data to a HTTP connection.
3119 * This function is deprecated. Use the httpWrite2() function which can
3120 * write more than 2GB of data.
3122 * @deprecated@ @exclude all@
3125 int /* O - Number of bytes written */
3126 httpWrite(http_t
*http
, /* I - HTTP connection */
3127 const char *buffer
, /* I - Buffer for data */
3128 int length
) /* I - Number of bytes to write */
3130 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3135 * 'httpWrite2()' - Write data to a HTTP connection.
3137 * @since CUPS 1.2/macOS 10.5@
3140 ssize_t
/* O - Number of bytes written */
3141 httpWrite2(http_t
*http
, /* I - HTTP connection */
3142 const char *buffer
, /* I - Buffer for data */
3143 size_t length
) /* I - Number of bytes to write */
3145 ssize_t bytes
; /* Bytes written */
3148 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3151 * Range check input...
3154 if (!http
|| !buffer
)
3156 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3161 * Mark activity on the connection...
3164 http
->activity
= time(NULL
);
3167 * Buffer small writes for better performance...
3171 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3173 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3177 http_content_coding_finish(http
);
3182 size_t slen
; /* Bytes to write */
3183 ssize_t sret
; /* Bytes written */
3185 ((z_stream
*)http
->stream
)->next_in
= (Bytef
*)buffer
;
3186 ((z_stream
*)http
->stream
)->avail_in
= (uInt
)length
;
3188 while (deflate((z_stream
*)http
->stream
, Z_NO_FLUSH
) == Z_OK
)
3190 DEBUG_printf(("1httpWrite2: avail_out=%d", ((z_stream
*)http
->stream
)->avail_out
));
3192 if (((z_stream
*)http
->stream
)->avail_out
> 0)
3195 slen
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3197 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3199 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3200 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3202 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3208 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3212 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3213 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3216 bytes
= (ssize_t
)length
;
3220 #endif /* HAVE_LIBZ */
3223 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3225 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3226 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3228 httpFlushWrite(http
);
3231 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3234 * Write to buffer...
3237 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3238 CUPS_LLCAST length
));
3240 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3241 http
->wused
+= (int)length
;
3242 bytes
= (ssize_t
)length
;
3247 * Otherwise write the data directly...
3250 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3251 CUPS_LLCAST length
));
3253 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3254 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3256 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3258 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3259 CUPS_LLCAST bytes
));
3262 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3263 http
->data_remaining
-= bytes
;
3269 * Handle end-of-request processing...
3272 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3273 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3276 * Finished with the transfer; unless we are sending POST or PUT
3281 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3282 http_content_coding_finish(http
);
3283 #endif /* HAVE_LIBZ */
3287 if (httpFlushWrite(http
) < 0)
3291 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3294 * Send a 0-length chunk at the end of the request...
3297 http_write(http
, "0\r\n\r\n", 5);
3300 * Reset the data state...
3303 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3304 http
->data_remaining
= 0;
3307 if (http
->state
== HTTP_STATE_POST_RECV
)
3309 else if (http
->state
== HTTP_STATE_POST_SEND
||
3310 http
->state
== HTTP_STATE_GET_SEND
)
3311 http
->state
= HTTP_STATE_WAITING
;
3313 http
->state
= HTTP_STATE_STATUS
;
3315 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3316 httpStateString(http
->state
)));
3319 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3326 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3328 * @since CUPS 1.7/macOS 10.9@
3331 int /* O - 0 on success, -1 on error */
3332 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3333 http_status_t status
) /* I - Status code */
3335 http_encoding_t old_encoding
; /* Old data_encoding value */
3336 off_t old_remaining
; /* Old data_remaining value */
3340 * Range check input...
3343 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3345 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3347 DEBUG_puts("1httpWriteResponse: Bad input.");
3352 * Set the various standard fields if they aren't already...
3355 if (!http
->fields
[HTTP_FIELD_DATE
])
3356 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3358 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3360 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3361 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3364 if (http
->version
== HTTP_VERSION_1_1
)
3366 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3368 if (http
->keep_alive
)
3369 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3371 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3374 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
])
3375 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3379 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3380 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3382 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3383 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3385 if (!http
->fields
[HTTP_FIELD_UPGRADE
])
3386 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3388 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
])
3389 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3391 #endif /* HAVE_SSL */
3393 if (!http
->fields
[HTTP_FIELD_SERVER
])
3394 httpSetField(http
, HTTP_FIELD_SERVER
, http
->default_fields
[HTTP_FIELD_SERVER
] ? http
->default_fields
[HTTP_FIELD_SERVER
] : CUPS_MINIMAL
);
3397 * Set the Accept-Encoding field if it isn't already...
3400 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
3401 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] ? http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] :
3403 "gzip, deflate, identity");
3406 #endif /* HAVE_LIBZ */
3409 * Send the response header...
3412 old_encoding
= http
->data_encoding
;
3413 old_remaining
= http
->data_remaining
;
3414 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3416 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100, http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3418 http
->status
= HTTP_STATUS_ERROR
;
3422 if (status
!= HTTP_STATUS_CONTINUE
)
3425 * 100 Continue doesn't have the rest of the response headers...
3428 int i
; /* Looping var */
3429 const char *value
; /* Field value */
3431 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3433 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3435 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3437 http
->status
= HTTP_STATUS_ERROR
;
3445 if (strchr(http
->cookie
, ';'))
3447 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3449 http
->status
= HTTP_STATUS_ERROR
;
3453 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3455 http
->status
= HTTP_STATUS_ERROR
;
3461 * "Click-jacking" defense (STR #4492)...
3464 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3465 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3467 http
->status
= HTTP_STATUS_ERROR
;
3472 if (httpWrite2(http
, "\r\n", 2) < 2)
3474 http
->status
= HTTP_STATUS_ERROR
;
3478 if (httpFlushWrite(http
) < 0)
3480 http
->status
= HTTP_STATUS_ERROR
;
3484 if (status
== HTTP_STATUS_CONTINUE
||
3485 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3488 * Restore the old data_encoding and data_length values...
3491 http
->data_encoding
= old_encoding
;
3492 http
->data_remaining
= old_remaining
;
3494 if (old_remaining
<= INT_MAX
)
3495 http
->_data_remaining
= (int)old_remaining
;
3497 http
->_data_remaining
= INT_MAX
;
3499 else if (http
->state
== HTTP_STATE_OPTIONS
||
3500 http
->state
== HTTP_STATE_HEAD
||
3501 http
->state
== HTTP_STATE_PUT
||
3502 http
->state
== HTTP_STATE_TRACE
||
3503 http
->state
== HTTP_STATE_CONNECT
||
3504 http
->state
== HTTP_STATE_STATUS
)
3506 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3507 "was %s.", httpStateString(http
->state
)));
3508 http
->state
= HTTP_STATE_WAITING
;
3513 * Force data_encoding and data_length to be set according to the response
3517 http_set_length(http
);
3519 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3521 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3522 "was %s.", httpStateString(http
->state
)));
3523 http
->state
= HTTP_STATE_WAITING
;
3527 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3532 * Then start any content encoding...
3535 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3536 http_content_coding_start(http
,
3537 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3538 #endif /* HAVE_LIBZ */
3547 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3551 http_add_field(http_t
*http
, /* I - HTTP connection */
3552 http_field_t field
, /* I - HTTP field */
3553 const char *value
, /* I - Value string */
3554 int append
) /* I - Append value? */
3556 char temp
[1024]; /* Temporary value string */
3557 size_t fieldlen
, /* Length of existing value */
3558 valuelen
, /* Length of value string */
3559 total
; /* Total length of string */
3562 if (field
== HTTP_FIELD_HOST
)
3565 * Special-case for Host: as we don't want a trailing "." on the hostname and
3566 * need to bracket IPv6 numeric addresses.
3569 char *ptr
= strchr(value
, ':');
3571 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3574 * Bracket IPv6 numeric addresses...
3576 * This is slightly inefficient (basically copying twice), but is an edge
3577 * case and not worth optimizing...
3580 snprintf(temp
, sizeof(temp
), "[%s]", value
);
3586 * Check for a trailing dot on the hostname...
3589 strlcpy(temp
, value
, sizeof(temp
));
3591 ptr
= temp
+ strlen(temp
) - 1;
3598 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
)
3601 if (!append
&& http
->fields
[field
])
3603 if (http
->fields
[field
] != http
->_fields
[field
])
3604 free(http
->fields
[field
]);
3606 http
->fields
[field
] = NULL
;
3609 valuelen
= strlen(value
);
3613 http
->_fields
[field
][0] = '\0';
3617 if (http
->fields
[field
])
3619 fieldlen
= strlen(http
->fields
[field
]);
3620 total
= fieldlen
+ 2 + valuelen
;
3628 if (total
< HTTP_MAX_VALUE
&& field
< HTTP_FIELD_ACCEPT_ENCODING
)
3631 * Copy short values to legacy char arrays (maintained for binary
3632 * compatibility with CUPS 1.2.x and earlier applications...)
3637 char combined
[HTTP_MAX_VALUE
];
3638 /* Combined value string */
3640 snprintf(combined
, sizeof(combined
), "%s, %s", http
->_fields
[field
], value
);
3644 strlcpy(http
->_fields
[field
], value
, sizeof(http
->_fields
[field
]));
3645 http
->fields
[field
] = http
->_fields
[field
];
3650 * Expand the field value...
3653 char *combined
; /* New value string */
3655 if (http
->fields
[field
] == http
->_fields
[field
])
3657 if ((combined
= malloc(total
+ 1)) != NULL
)
3659 http
->fields
[field
] = combined
;
3660 snprintf(combined
, total
+ 1, "%s, %s", http
->_fields
[field
], value
);
3663 else if ((combined
= realloc(http
->fields
[field
], total
+ 1)) != NULL
)
3665 http
->fields
[field
] = combined
;
3666 strlcat(combined
, ", ", total
+ 1);
3667 strlcat(combined
, value
, total
+ 1);
3673 * Allocate the field value...
3676 http
->fields
[field
] = strdup(value
);
3680 if (field
== HTTP_FIELD_CONTENT_ENCODING
&& http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3682 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
3683 http_content_coding_start(http
, value
);
3685 #endif /* HAVE_LIBZ */
3691 * 'http_content_coding_finish()' - Finish doing any content encoding.
3695 http_content_coding_finish(
3696 http_t
*http
) /* I - HTTP connection */
3698 int zerr
; /* Compression status */
3699 Byte dummy
[1]; /* Dummy read buffer */
3700 size_t bytes
; /* Number of bytes to write */
3703 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3704 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3706 switch (http
->coding
)
3708 case _HTTP_CODING_DEFLATE
:
3709 case _HTTP_CODING_GZIP
:
3710 ((z_stream
*)http
->stream
)->next_in
= dummy
;
3711 ((z_stream
*)http
->stream
)->avail_in
= 0;
3715 zerr
= deflate((z_stream
*)http
->stream
, Z_FINISH
);
3716 bytes
= _HTTP_MAX_SBUFFER
- ((z_stream
*)http
->stream
)->avail_out
;
3720 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3722 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3723 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3725 http_write(http
, (char *)http
->sbuffer
, bytes
);
3728 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3729 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3731 while (zerr
== Z_OK
);
3733 deflateEnd((z_stream
*)http
->stream
);
3735 free(http
->sbuffer
);
3738 http
->sbuffer
= NULL
;
3739 http
->stream
= NULL
;
3742 httpFlushWrite(http
);
3745 case _HTTP_CODING_INFLATE
:
3746 case _HTTP_CODING_GUNZIP
:
3747 inflateEnd((z_stream
*)http
->stream
);
3749 free(http
->sbuffer
);
3752 http
->sbuffer
= NULL
;
3753 http
->stream
= NULL
;
3760 http
->coding
= _HTTP_CODING_IDENTITY
;
3765 * 'http_content_coding_start()' - Start doing content encoding.
3769 http_content_coding_start(
3770 http_t
*http
, /* I - HTTP connection */
3771 const char *value
) /* I - Value of Content-Encoding */
3773 int zerr
; /* Error/status */
3774 _http_coding_t coding
; /* Content coding value */
3777 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3779 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3781 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3785 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3787 if (http
->state
== HTTP_STATE_GET_SEND
||
3788 http
->state
== HTTP_STATE_POST_SEND
)
3789 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3790 _HTTP_CODING_GUNZIP
;
3791 else if (http
->state
== HTTP_STATE_POST_RECV
||
3792 http
->state
== HTTP_STATE_PUT_RECV
)
3793 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3794 _HTTP_CODING_GUNZIP
;
3797 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3801 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3803 if (http
->state
== HTTP_STATE_GET_SEND
||
3804 http
->state
== HTTP_STATE_POST_SEND
)
3805 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3806 _HTTP_CODING_INFLATE
;
3807 else if (http
->state
== HTTP_STATE_POST_RECV
||
3808 http
->state
== HTTP_STATE_PUT_RECV
)
3809 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3810 _HTTP_CODING_INFLATE
;
3813 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3819 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3825 case _HTTP_CODING_DEFLATE
:
3826 case _HTTP_CODING_GZIP
:
3828 httpFlushWrite(http
);
3830 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3832 http
->status
= HTTP_STATUS_ERROR
;
3833 http
->error
= errno
;
3838 * Window size for compression is 11 bits - optimal based on PWG Raster
3839 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3843 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3845 free(http
->sbuffer
);
3847 http
->sbuffer
= NULL
;
3848 http
->status
= HTTP_STATUS_ERROR
;
3849 http
->error
= errno
;
3853 if ((zerr
= deflateInit2((z_stream
*)http
->stream
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7, Z_DEFAULT_STRATEGY
)) < Z_OK
)
3855 free(http
->sbuffer
);
3858 http
->sbuffer
= NULL
;
3859 http
->stream
= NULL
;
3860 http
->status
= HTTP_STATUS_ERROR
;
3861 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3865 ((z_stream
*)http
->stream
)->next_out
= (Bytef
*)http
->sbuffer
;
3866 ((z_stream
*)http
->stream
)->avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3869 case _HTTP_CODING_INFLATE
:
3870 case _HTTP_CODING_GUNZIP
:
3871 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3873 http
->status
= HTTP_STATUS_ERROR
;
3874 http
->error
= errno
;
3879 * Window size for decompression is up to 15 bits (maximum supported).
3880 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3883 if ((http
->stream
= calloc(1, sizeof(z_stream
))) == NULL
)
3885 free(http
->sbuffer
);
3887 http
->sbuffer
= NULL
;
3888 http
->status
= HTTP_STATUS_ERROR
;
3889 http
->error
= errno
;
3893 if ((zerr
= inflateInit2((z_stream
*)http
->stream
, coding
== _HTTP_CODING_INFLATE
? -15 : 31)) < Z_OK
)
3895 free(http
->sbuffer
);
3898 http
->sbuffer
= NULL
;
3899 http
->stream
= NULL
;
3900 http
->status
= HTTP_STATUS_ERROR
;
3901 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3905 ((z_stream
*)http
->stream
)->avail_in
= 0;
3906 ((z_stream
*)http
->stream
)->next_in
= http
->sbuffer
;
3913 http
->coding
= coding
;
3915 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3918 #endif /* HAVE_LIBZ */
3922 * 'http_create()' - Create an unconnected HTTP connection.
3925 static http_t
* /* O - HTTP connection */
3927 const char *host
, /* I - Hostname */
3928 int port
, /* I - Port number */
3929 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3930 int family
, /* I - Address family or AF_UNSPEC */
3931 http_encryption_t encryption
, /* I - Encryption to use */
3932 int blocking
, /* I - 1 for blocking mode */
3933 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3935 http_t
*http
; /* New HTTP connection */
3936 char service
[255]; /* Service name */
3937 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3940 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
));
3942 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3948 * Lookup the host...
3953 myaddrlist
= httpAddrCopyList(addrlist
);
3957 snprintf(service
, sizeof(service
), "%d", port
);
3959 myaddrlist
= httpAddrGetList(host
, family
, service
);
3966 * Allocate memory for the structure...
3969 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3971 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3972 httpAddrFreeList(myaddrlist
);
3977 * Initialize the HTTP data...
3981 http
->activity
= time(NULL
);
3982 http
->addrlist
= myaddrlist
;
3983 http
->blocking
= blocking
;
3986 http
->gssctx
= GSS_C_NO_CONTEXT
;
3987 http
->gssname
= GSS_C_NO_NAME
;
3988 #endif /* HAVE_GSSAPI */
3989 http
->status
= HTTP_STATUS_CONTINUE
;
3990 http
->version
= HTTP_VERSION_1_1
;
3993 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3995 if (port
== 443) /* Always use encryption for https */
3996 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3998 http
->encryption
= encryption
;
4000 http_set_wait(http
);
4003 * Return the new structure...
4012 * 'http_debug_hex()' - Do a hex dump of a buffer.
4016 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4017 const char *buffer
, /* I - Buffer to dump */
4018 int bytes
) /* I - Bytes to dump */
4020 int i
, j
, /* Looping vars */
4021 ch
; /* Current character */
4022 char line
[255], /* Line buffer */
4023 *start
, /* Start of line after prefix */
4024 *ptr
; /* Pointer into line */
4027 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4030 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4032 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4033 start
= line
+ strlen(line
);
4035 for (i
= 0; i
< bytes
; i
+= 16)
4037 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4038 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4042 memcpy(ptr
, " ", 3);
4047 memcpy(ptr
, " ", 3);
4050 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4052 ch
= buffer
[i
+ j
] & 255;
4054 if (ch
< ' ' || ch
>= 127)
4068 * 'http_read()' - Read a buffer from a HTTP connection.
4070 * This function does the low-level read from the socket, retrying and timing
4074 static ssize_t
/* O - Number of bytes read or -1 on error */
4075 http_read(http_t
*http
, /* I - HTTP connection */
4076 char *buffer
, /* I - Buffer */
4077 size_t length
) /* I - Maximum bytes to read */
4079 ssize_t bytes
; /* Bytes read */
4082 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4084 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4086 while (!httpWait(http
, http
->wait_value
))
4088 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4091 DEBUG_puts("2http_read: Timeout.");
4096 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4102 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4104 #endif /* HAVE_SSL */
4105 bytes
= recv(http
->fd
, buffer
, length
, 0);
4110 if (WSAGetLastError() != WSAEINTR
)
4112 http
->error
= WSAGetLastError();
4115 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4117 if (!http
->timeout_cb
||
4118 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4120 http
->error
= WSAEWOULDBLOCK
;
4125 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4127 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4129 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4131 http
->error
= errno
;
4134 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4136 http
->error
= errno
;
4140 else if (errno
!= EINTR
)
4142 http
->error
= errno
;
4150 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4151 CUPS_LLCAST bytes
));
4154 http_debug_hex("http_read", buffer
, (int)bytes
);
4160 if (WSAGetLastError() == WSAEINTR
)
4163 http
->error
= WSAGetLastError();
4165 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4168 http
->error
= errno
;
4171 else if (bytes
== 0)
4173 http
->error
= EPIPE
;
4182 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4184 * This function reads data from the HTTP buffer or from the socket, as needed.
4187 static ssize_t
/* O - Number of bytes read or -1 on error */
4188 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4189 char *buffer
, /* I - Buffer */
4190 size_t length
) /* I - Maximum bytes to read */
4192 ssize_t bytes
; /* Bytes read */
4195 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4199 if (length
> (size_t)http
->used
)
4200 bytes
= (ssize_t
)http
->used
;
4202 bytes
= (ssize_t
)length
;
4204 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4207 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4208 http
->used
-= (int)bytes
;
4211 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4214 bytes
= http_read(http
, buffer
, length
);
4221 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4223 * This function reads and validates the chunk length, then does a buffered read
4224 * returning the number of bytes placed in the buffer.
4227 static ssize_t
/* O - Number of bytes read or -1 on error */
4228 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4229 char *buffer
, /* I - Buffer */
4230 size_t length
) /* I - Maximum bytes to read */
4232 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4234 if (http
->data_remaining
<= 0)
4236 char len
[32]; /* Length string */
4238 if (!httpGets(len
, sizeof(len
), http
))
4240 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4246 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4247 if (!httpGets(len
, sizeof(len
), http
))
4249 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4254 http
->data_remaining
= strtoll(len
, NULL
, 16);
4256 if (http
->data_remaining
< 0)
4258 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4259 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4263 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4264 len
, CUPS_LLCAST http
->data_remaining
));
4266 if (http
->data_remaining
== 0)
4269 * 0-length chunk, grab trailing blank line...
4272 httpGets(len
, sizeof(len
), http
);
4276 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4277 CUPS_LLCAST http
->data_remaining
));
4279 if (http
->data_remaining
<= 0)
4281 else if (length
> (size_t)http
->data_remaining
)
4282 length
= (size_t)http
->data_remaining
;
4284 return (http_read_buffered(http
, buffer
, length
));
4289 * 'http_send()' - Send a request with all fields and the trailing blank line.
4292 static int /* O - 0 on success, non-zero on error */
4293 http_send(http_t
*http
, /* I - HTTP connection */
4294 http_state_t request
, /* I - Request code */
4295 const char *uri
) /* I - URI */
4297 int i
; /* Looping var */
4298 char buf
[1024]; /* Encoded URI buffer */
4299 const char *value
; /* Field value */
4300 static const char * const codes
[] = /* Request code strings */
4320 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4322 if (http
== NULL
|| uri
== NULL
)
4326 * Set the User-Agent field if it isn't already...
4329 if (!http
->fields
[HTTP_FIELD_USER_AGENT
])
4331 if (http
->default_fields
[HTTP_FIELD_USER_AGENT
])
4332 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_fields
[HTTP_FIELD_USER_AGENT
]);
4334 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4338 * Set the Accept-Encoding field if it isn't already...
4341 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
] && http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
])
4342 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
]);
4345 * Encode the URI as needed...
4348 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4351 * See if we had an error the last time around; if so, reconnect...
4354 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4355 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4357 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4358 http
->fd
, http
->status
, http
->tls_upgrade
));
4360 if (httpReconnect2(http
, 30000, NULL
))
4365 * Flush any written data that is pending...
4370 if (httpFlushWrite(http
) < 0)
4371 if (httpReconnect2(http
, 30000, NULL
))
4376 * Send the request header...
4379 http
->state
= request
;
4380 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4382 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4385 http
->status
= HTTP_STATUS_CONTINUE
;
4388 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4390 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4391 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4393 #endif /* HAVE_SSL */
4395 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4397 http
->status
= HTTP_STATUS_ERROR
;
4401 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4402 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4404 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4406 if (i
== HTTP_FIELD_HOST
)
4408 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4409 httpAddrPort(http
->hostaddr
)) < 1)
4411 http
->status
= HTTP_STATUS_ERROR
;
4415 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4417 http
->status
= HTTP_STATUS_ERROR
;
4423 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4425 http
->status
= HTTP_STATUS_ERROR
;
4429 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4430 http
->mode
, http
->state
));
4432 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4433 (http
->state
== HTTP_STATE_POST_RECV
||
4434 http
->state
== HTTP_STATE_PUT_RECV
))
4435 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4437 http
->status
= HTTP_STATUS_ERROR
;
4441 if (httpPrintf(http
, "\r\n") < 1)
4443 http
->status
= HTTP_STATUS_ERROR
;
4447 if (httpFlushWrite(http
) < 0)
4450 http_set_length(http
);
4451 httpClearFields(http
);
4454 * The Kerberos and AuthRef authentication strings can only be used once...
4457 if (http
->fields
[HTTP_FIELD_AUTHORIZATION
] && http
->authstring
&&
4458 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4459 !strncmp(http
->authstring
, "AuthRef", 7)))
4461 http
->_authstring
[0] = '\0';
4463 if (http
->authstring
!= http
->_authstring
)
4464 free(http
->authstring
);
4466 http
->authstring
= http
->_authstring
;
4474 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4477 static off_t
/* O - Remainder or -1 on error */
4478 http_set_length(http_t
*http
) /* I - Connection */
4480 off_t remaining
; /* Remainder */
4483 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4485 if ((remaining
= httpGetLength2(http
)) >= 0)
4487 if (http
->mode
== _HTTP_MODE_SERVER
&&
4488 http
->state
!= HTTP_STATE_GET_SEND
&&
4489 http
->state
!= HTTP_STATE_PUT
&&
4490 http
->state
!= HTTP_STATE_POST
&&
4491 http
->state
!= HTTP_STATE_POST_SEND
)
4493 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4497 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_TRANSFER_ENCODING
), "chunked"))
4499 DEBUG_puts("1http_set_length: Setting data_encoding to "
4500 "HTTP_ENCODING_CHUNKED.");
4501 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4505 DEBUG_puts("1http_set_length: Setting data_encoding to "
4506 "HTTP_ENCODING_LENGTH.");
4507 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4510 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4511 CUPS_LLCAST remaining
));
4512 http
->data_remaining
= remaining
;
4514 if (remaining
<= INT_MAX
)
4515 http
->_data_remaining
= (int)remaining
;
4517 http
->_data_remaining
= INT_MAX
;
4524 * 'http_set_timeout()' - Set the socket timeout values.
4528 http_set_timeout(int fd
, /* I - File descriptor */
4529 double timeout
) /* I - Timeout in seconds */
4532 DWORD tv
= (DWORD
)(timeout
* 1000);
4533 /* Timeout in milliseconds */
4535 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4536 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4539 struct timeval tv
; /* Timeout in secs and usecs */
4541 tv
.tv_sec
= (int)timeout
;
4542 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4544 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4545 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4551 * 'http_set_wait()' - Set the default wait value for reads.
4555 http_set_wait(http_t
*http
) /* I - HTTP connection */
4559 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4561 if (http
->wait_value
<= 0)
4562 http
->wait_value
= 60000;
4565 http
->wait_value
= 10000;
4571 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4574 static int /* O - Status of connection */
4575 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4577 int ret
; /* Return value */
4578 http_t myhttp
; /* Local copy of HTTP data */
4581 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4584 * Flush the connection to make sure any previous "Upgrade" message
4591 * Copy the HTTP data to a local variable so we can do the OPTIONS
4592 * request without interfering with the existing request data...
4595 memcpy(&myhttp
, http
, sizeof(myhttp
));
4598 * Send an OPTIONS request to the server, requiring SSL or TLS
4599 * encryption on the link...
4602 http
->tls_upgrade
= 1;
4603 memset(http
->fields
, 0, sizeof(http
->fields
));
4604 http
->expect
= (http_status_t
)0;
4606 if (http
->hostname
[0] == '/')
4607 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
4609 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
4611 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4612 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4614 if ((ret
= httpOptions(http
, "*")) == 0)
4617 * Wait for the secure connection...
4620 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4624 * Restore the HTTP request data...
4627 memcpy(http
->_fields
, myhttp
._fields
, sizeof(http
->_fields
));
4628 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4630 http
->data_encoding
= myhttp
.data_encoding
;
4631 http
->data_remaining
= myhttp
.data_remaining
;
4632 http
->_data_remaining
= myhttp
._data_remaining
;
4633 http
->expect
= myhttp
.expect
;
4634 http
->digest_tries
= myhttp
.digest_tries
;
4635 http
->tls_upgrade
= 0;
4638 * See if we actually went secure...
4644 * Server does not support HTTP upgrade...
4647 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4649 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4650 httpAddrClose(NULL
, http
->fd
);
4659 #endif /* HAVE_SSL */
4663 * 'http_write()' - Write a buffer to a HTTP connection.
4666 static ssize_t
/* O - Number of bytes written */
4667 http_write(http_t
*http
, /* I - HTTP connection */
4668 const char *buffer
, /* I - Buffer for data */
4669 size_t length
) /* I - Number of bytes to write */
4671 ssize_t tbytes
, /* Total bytes sent */
4672 bytes
; /* Bytes sent */
4675 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4681 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4683 if (http
->timeout_value
> 0.0)
4686 struct pollfd pfd
; /* Polled file descriptor */
4688 fd_set output_set
; /* Output ready for write? */
4689 struct timeval timeout
; /* Timeout value */
4690 #endif /* HAVE_POLL */
4691 int nfds
; /* Result from select()/poll() */
4697 pfd
.events
= POLLOUT
;
4699 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4700 (errno
== EINTR
|| errno
== EAGAIN
))
4706 FD_ZERO(&output_set
);
4707 FD_SET(http
->fd
, &output_set
);
4709 timeout
.tv_sec
= http
->wait_value
/ 1000;
4710 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4712 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4715 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4716 WSAGetLastError() == WSAEWOULDBLOCK
));
4718 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4719 # endif /* _WIN32 */
4720 #endif /* HAVE_POLL */
4724 http
->error
= errno
;
4727 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4730 http
->error
= WSAEWOULDBLOCK
;
4732 http
->error
= EWOULDBLOCK
;
4742 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4744 #endif /* HAVE_SSL */
4745 bytes
= send(http
->fd
, buffer
, length
, 0);
4747 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4748 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4753 if (WSAGetLastError() == WSAEINTR
)
4755 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4757 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4760 http
->error
= WSAGetLastError();
4762 else if (WSAGetLastError() != http
->error
&&
4763 WSAGetLastError() != WSAECONNRESET
)
4765 http
->error
= WSAGetLastError();
4772 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4774 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4776 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4779 http
->error
= errno
;
4781 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4783 http
->error
= errno
;
4788 DEBUG_printf(("3http_write: error writing data (%s).",
4789 strerror(http
->error
)));
4796 length
-= (size_t)bytes
;
4800 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4803 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4810 * 'http_write_chunk()' - Write a chunked buffer.
4813 static ssize_t
/* O - Number bytes written */
4814 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4815 const char *buffer
, /* I - Buffer to write */
4816 size_t length
) /* I - Length of buffer */
4818 char header
[16]; /* Chunk header */
4819 ssize_t bytes
; /* Bytes written */
4822 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4825 * Write the chunk header, data, and trailer.
4828 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4829 if (http_write(http
, header
, strlen(header
)) < 0)
4831 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4835 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4837 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4841 if (http_write(http
, "\r\n", 2) < 0)
4843 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");