2 * HTTP routines for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Include necessary headers...
23 #include "cups-private.h"
30 # include <sys/time.h>
31 # include <sys/resource.h>
35 #endif /* HAVE_POLL */
43 static void http_content_coding_finish(http_t
*http
);
44 static void http_content_coding_start(http_t
*http
,
46 #endif /* HAVE_LIBZ */
47 static http_t
*http_create(const char *host
, int port
,
48 http_addrlist_t
*addrlist
, int family
,
49 http_encryption_t encryption
,
50 int blocking
, _http_mode_t mode
);
52 static void http_debug_hex(const char *prefix
, const char *buffer
,
55 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
56 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
57 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
58 static int http_send(http_t
*http
, http_state_t request
,
60 static ssize_t
http_write(http_t
*http
, const char *buffer
,
62 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
64 static off_t
http_set_length(http_t
*http
);
65 static void http_set_timeout(int fd
, double timeout
);
66 static void http_set_wait(http_t
*http
);
69 static int http_tls_upgrade(http_t
*http
);
77 static const char * const http_fields
[] =
94 "If-Unmodified-since",
113 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
114 * specified listening socket.
116 * @since CUPS 1.7/OS X 10.9@
119 http_t
* /* O - HTTP connection or @code NULL@ */
120 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
121 int blocking
) /* I - 1 if the connection should be
122 blocking, 0 otherwise */
124 http_t
*http
; /* HTTP connection */
125 http_addrlist_t addrlist
; /* Dummy address list */
126 socklen_t addrlen
; /* Length of address */
127 int val
; /* Socket option value */
131 * Range check input...
138 * Create the client connection...
141 memset(&addrlist
, 0, sizeof(addrlist
));
143 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
144 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
145 _HTTP_MODE_SERVER
)) == NULL
)
149 * Accept the client and get the remote address...
152 addrlen
= sizeof(http_addr_t
);
154 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
157 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
163 http
->hostaddr
= &(http
->addrlist
->addr
);
165 if (httpAddrLocalhost(http
->hostaddr
))
166 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
168 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
172 * Disable SIGPIPE for this socket.
176 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
177 #endif /* SO_NOSIGPIPE */
180 * Using TCP_NODELAY improves responsiveness, especially on systems
181 * with a slow loopback interface. Since we write large buffers
182 * when sending print files and requests, there shouldn't be any
183 * performance penalty for this...
187 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
191 * Close this socket when starting another process...
194 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
195 #endif /* FD_CLOEXEC */
202 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
204 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
206 * @since CUPS 1.5/OS X 10.7@
209 int /* O - 0 on success, -1 on error */
211 cups_array_t
*credentials
, /* I - Credentials array */
212 const void *data
, /* I - PEM-encoded X.509 data */
213 size_t datalen
) /* I - Length of data */
215 http_credential_t
*credential
; /* Credential data */
218 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
220 credential
->datalen
= datalen
;
222 if ((credential
->data
= malloc(datalen
)) != NULL
)
224 memcpy(credential
->data
, data
, datalen
);
225 cupsArrayAdd(credentials
, credential
);
237 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
241 httpBlocking(http_t
*http
, /* I - HTTP connection */
242 int b
) /* I - 1 = blocking, 0 = non-blocking */
253 * 'httpCheck()' - Check to see if there is a pending response from the server.
256 int /* O - 0 = no data, 1 = data available */
257 httpCheck(http_t
*http
) /* I - HTTP connection */
259 return (httpWait(http
, 0));
264 * 'httpClearCookie()' - Clear the cookie value(s).
266 * @since CUPS 1.1.19/OS X 10.3@
270 httpClearCookie(http_t
*http
) /* I - HTTP connection */
284 * 'httpClearFields()' - Clear HTTP request fields.
288 httpClearFields(http_t
*http
) /* I - HTTP connection */
290 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
294 memset(http
->fields
, 0, sizeof(http
->fields
));
296 if (http
->mode
== _HTTP_MODE_CLIENT
)
298 if (http
->hostname
[0] == '/')
299 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
301 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
304 if (http
->field_authorization
)
306 free(http
->field_authorization
);
307 http
->field_authorization
= NULL
;
310 if (http
->accept_encoding
)
312 _cupsStrFree(http
->accept_encoding
);
313 http
->accept_encoding
= NULL
;
318 _cupsStrFree(http
->allow
);
324 _cupsStrFree(http
->server
);
328 http
->expect
= (http_status_t
)0;
334 * 'httpClose()' - Close an HTTP connection.
338 httpClose(http_t
*http
) /* I - HTTP connection */
341 OM_uint32 minor_status
; /* Minor status code */
342 #endif /* HAVE_GSSAPI */
345 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
348 * Range check input...
355 * Close any open connection...
358 _httpDisconnect(http
);
361 * Free memory used...
364 httpAddrFreeList(http
->addrlist
);
370 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
371 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
373 if (http
->gssname
!= GSS_C_NO_NAME
)
374 gss_release_name(&minor_status
, &http
->gssname
);
375 #endif /* HAVE_GSSAPI */
377 #ifdef HAVE_AUTHORIZATION_H
379 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
380 #endif /* HAVE_AUTHORIZATION_H */
382 httpClearFields(http
);
384 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
385 free(http
->authstring
);
392 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
394 * @since CUPS 2.0/OS 10.10@
397 int /* O - 1 if they match, 0 if they do not */
398 httpCompareCredentials(
399 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
400 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
402 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
405 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
))
406 if (temp1
->datalen
!= temp2
->datalen
)
408 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
411 return (temp1
== temp2
);
416 * 'httpConnect()' - Connect to a HTTP server.
418 * This function is deprecated - use @link httpConnect2@ instead.
423 http_t
* /* O - New HTTP connection */
424 httpConnect(const char *host
, /* I - Host to connect to */
425 int port
) /* I - Port number */
427 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
428 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
433 * 'httpConnect2()' - Connect to a HTTP server.
435 * @since CUPS 1.7/OS X 10.9@
438 http_t
* /* O - New HTTP connection */
440 const char *host
, /* I - Host to connect to */
441 int port
, /* I - Port number */
442 http_addrlist_t
*addrlist
, /* I - List of addresses or NULL to lookup */
443 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
444 http_encryption_t encryption
, /* I - Type of encryption to use */
445 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
446 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
447 int *cancel
) /* I - Pointer to "cancel" variable */
449 http_t
*http
; /* New HTTP connection */
452 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
));
455 * Create the HTTP structure...
458 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
459 _HTTP_MODE_CLIENT
)) == NULL
)
463 * Optionally connect to the remote system...
466 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
470 * Could not connect to any known address - bail out!
480 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
482 * This function is now deprecated. Please use the @link httpConnect2@ function
488 http_t
* /* O - New HTTP connection */
490 const char *host
, /* I - Host to connect to */
491 int port
, /* I - Port number */
492 http_encryption_t encryption
) /* I - Type of encryption to use */
494 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
495 host
, port
, encryption
));
497 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
503 * 'httpDelete()' - Send a DELETE request to the server.
506 int /* O - Status of call (0 = success) */
507 httpDelete(http_t
*http
, /* I - HTTP connection */
508 const char *uri
) /* I - URI to delete */
510 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
515 * '_httpDisconnect()' - Disconnect a HTTP connection.
519 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
524 #endif /* HAVE_SSL */
526 httpAddrClose(NULL
, http
->fd
);
533 * 'httpEncryption()' - Set the required encryption on the link.
536 int /* O - -1 on error, 0 on success */
537 httpEncryption(http_t
*http
, /* I - HTTP connection */
538 http_encryption_t e
) /* I - New encryption preference */
540 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
546 if (http
->mode
== _HTTP_MODE_CLIENT
)
548 http
->encryption
= e
;
550 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
551 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
552 return (httpReconnect2(http
, 30000, NULL
));
553 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
554 return (http_tls_upgrade(http
));
560 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
563 http
->encryption
= e
;
564 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
565 return (_httpTLSStart(http
));
570 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
574 #endif /* HAVE_SSL */
579 * 'httpError()' - Get the last error on a connection.
582 int /* O - Error code (errno) value */
583 httpError(http_t
*http
) /* I - HTTP connection */
586 return (http
->error
);
593 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
597 http_field_t
/* O - Field index */
598 httpFieldValue(const char *name
) /* I - String name */
600 int i
; /* Looping var */
603 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
604 if (!_cups_strcasecmp(name
, http_fields
[i
]))
605 return ((http_field_t
)i
);
607 return (HTTP_FIELD_UNKNOWN
);
612 * 'httpFlush()' - Flush data from a HTTP connection.
616 httpFlush(http_t
*http
) /* I - HTTP connection */
618 char buffer
[8192]; /* Junk buffer */
619 int blocking
; /* To block or not to block */
620 http_state_t oldstate
; /* Old state */
623 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
626 * Nothing to do if we are in the "waiting" state...
629 if (http
->state
== HTTP_STATE_WAITING
)
633 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
636 blocking
= http
->blocking
;
640 * Read any data we can...
643 oldstate
= http
->state
;
644 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
647 * Restore blocking and reset the connection if we didn't get all of
648 * the remaining data...
651 http
->blocking
= blocking
;
653 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
657 * Didn't get the data back, so close the current connection.
662 http_content_coding_finish(http
);
663 #endif /* HAVE_LIBZ */
665 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
667 http
->state
= HTTP_STATE_WAITING
;
672 #endif /* HAVE_SSL */
674 httpAddrClose(NULL
, http
->fd
);
682 * 'httpFlushWrite()' - Flush data in write buffer.
684 * @since CUPS 1.2/OS X 10.5@
687 int /* O - Bytes written or -1 on error */
688 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
690 ssize_t bytes
; /* Bytes written */
693 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
695 if (!http
|| !http
->wused
)
697 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
698 "1httpFlushWrite: No connection.");
702 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
703 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
705 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
709 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
716 * 'httpFreeCredentials()' - Free an array of credentials.
721 cups_array_t
*credentials
) /* I - Array of credentials */
723 http_credential_t
*credential
; /* Credential */
726 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
728 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
730 cupsArrayRemove(credentials
, credential
);
731 free((void *)credential
->data
);
735 cupsArrayDelete(credentials
);
740 * 'httpGet()' - Send a GET request to the server.
743 int /* O - Status of call (0 = success) */
744 httpGet(http_t
*http
, /* I - HTTP connection */
745 const char *uri
) /* I - URI to get */
747 return (http_send(http
, HTTP_STATE_GET
, uri
));
752 * 'httpGetActivity()' - Get the most recent activity for a connection.
754 * The return value is the UNIX time of the last read or write.
756 * @since CUPS 2.0/OS 10.10@
759 time_t /* O - Time of last read or write */
760 httpGetActivity(http_t
*http
) /* I - HTTP connection */
762 return (http
? http
->activity
: 0);
767 * 'httpGetAuthString()' - Get the current authorization string.
769 * The authorization string is set by cupsDoAuthentication() and
770 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
771 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
774 * @since CUPS 1.3/OS X 10.5@
777 char * /* O - Authorization string */
778 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
781 return (http
->authstring
);
788 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
790 * @since CUPS 1.2/OS X 10.5@
793 int /* O - 1 if blocking, 0 if non-blocking */
794 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
796 return (http
? http
->blocking
: 0);
801 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
802 * the client and server.
804 * This function uses the value of the Accepts-Encoding HTTP header and must be
805 * called after receiving a response from the server or a request from the
806 * client. The value returned can be use in subsequent requests (for clients)
807 * or in the response (for servers) in order to compress the content stream.
809 * @since CUPS 1.7/OS X 10.9@
812 const char * /* O - Content-Coding value or
813 @code NULL@ for the identity
815 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
818 if (http
&& http
->accept_encoding
)
820 int i
; /* Looping var */
821 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
822 *start
, /* Start of coding value */
823 *end
; /* End of coding value */
824 double qvalue
; /* "qvalue" for coding */
825 struct lconv
*loc
= localeconv(); /* Locale data */
826 static const char * const codings
[] =
827 { /* Supported content codings */
834 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
836 for (start
= temp
; *start
; start
= end
)
839 * Find the end of the coding name...
844 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
850 * Grab the qvalue as needed...
853 if (!strncmp(end
, ";q=", 3))
854 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
857 * Skip past all attributes...
861 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
867 while (*end
&& isspace(*end
& 255))
871 * Check value if it matches something we support...
877 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
878 if (!strcmp(start
, codings
[i
]))
882 #endif /* HAVE_LIBZ */
889 * 'httpGetCookie()' - Get any cookie data from the response.
891 * @since CUPS 1.1.19/OS X 10.3@
894 const char * /* O - Cookie data or NULL */
895 httpGetCookie(http_t
*http
) /* I - HTTP connection */
897 return (http
? http
->cookie
: NULL
);
902 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
904 * This function returns the encryption mode for the connection. Use the
905 * @link httpIsEncrypted@ function to determine whether a TLS session has
908 * @since CUPS 2.0/OS 10.10@
911 http_encryption_t
/* O - Current encryption mode */
912 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
914 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
919 * 'httpGetExpect()' - Get the value of the Expect header, if any.
921 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
922 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
924 * @since CUPS 1.7/OS X 10.9@
927 http_status_t
/* O - Expect: status, if any */
928 httpGetExpect(http_t
*http
) /* I - HTTP connection */
931 return (HTTP_STATUS_ERROR
);
933 return (http
->expect
);
938 * 'httpGetFd()' - Get the file descriptor associated with a connection.
940 * @since CUPS 1.2/OS X 10.5@
943 int /* O - File descriptor or -1 if none */
944 httpGetFd(http_t
*http
) /* I - HTTP connection */
946 return (http
? http
->fd
: -1);
951 * 'httpGetField()' - Get a field value from a request/response.
954 const char * /* O - Field value */
955 httpGetField(http_t
*http
, /* I - HTTP connection */
956 http_field_t field
) /* I - Field to get */
958 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
963 case HTTP_FIELD_ACCEPT_ENCODING
:
964 return (http
->accept_encoding
);
966 case HTTP_FIELD_ALLOW
:
967 return (http
->allow
);
969 case HTTP_FIELD_SERVER
:
970 return (http
->server
);
972 case HTTP_FIELD_AUTHORIZATION
:
973 if (http
->field_authorization
)
976 * Special case for WWW-Authenticate: as its contents can be
977 * longer than HTTP_MAX_VALUE...
980 return (http
->field_authorization
);
984 return (http
->fields
[field
]);
990 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
992 * @since CUPS 2.0/OS 10.10@
995 http_keepalive_t
/* O - Keep-Alive state */
996 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
998 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1003 * 'httpGetLength()' - Get the amount of data remaining from the
1004 * content-length or transfer-encoding fields.
1006 * This function is deprecated and will not return lengths larger than
1007 * 2^31 - 1; use httpGetLength2() instead.
1012 int /* O - Content length */
1013 httpGetLength(http_t
*http
) /* I - HTTP connection */
1016 * Get the read content length and return the 32-bit value.
1021 httpGetLength2(http
);
1023 return (http
->_data_remaining
);
1031 * 'httpGetLength2()' - Get the amount of data remaining from the
1032 * content-length or transfer-encoding fields.
1034 * This function returns the complete content length, even for
1035 * content larger than 2^31 - 1.
1037 * @since CUPS 1.2/OS X 10.5@
1040 off_t
/* O - Content length */
1041 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1043 off_t remaining
; /* Remaining length */
1046 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1051 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1053 DEBUG_puts("4httpGetLength2: chunked request!");
1059 * The following is a hack for HTTP servers that don't send a
1060 * Content-Length or Transfer-Encoding field...
1062 * If there is no Content-Length then the connection must close
1063 * after the transfer is complete...
1066 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1069 * Default content length is 0 for errors and certain types of operations,
1070 * and 2^31-1 for other successful requests...
1073 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1074 http
->state
== HTTP_STATE_OPTIONS
||
1075 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1076 http
->state
== HTTP_STATE_HEAD
||
1077 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1078 http
->state
== HTTP_STATE_DELETE
||
1079 http
->state
== HTTP_STATE_TRACE
||
1080 http
->state
== HTTP_STATE_CONNECT
)
1083 remaining
= 2147483647;
1085 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1089 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1090 CUPS_LLCAST remaining
));
1098 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1100 * @since CUPS 2.0/OS 10.10@
1103 size_t /* O - Number of bytes buffered */
1104 httpGetPending(http_t
*http
) /* I - HTTP connection */
1106 return (http
? (size_t)http
->wused
: 0);
1111 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1113 * @since CUPS 2.0/OS 10.10@
1116 size_t /* O - Number of bytes available */
1117 httpGetReady(http_t
*http
) /* I - HTTP connection */
1121 else if (http
->used
> 0)
1122 return ((size_t)http
->used
);
1125 return (_httpTLSPending(http
));
1126 #endif /* HAVE_SSL */
1133 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1134 * body or current chunk.
1136 * The @link httpIsChunked@ function can be used to determine whether the
1137 * message body is chunked or fixed-length.
1139 * @since CUPS 2.0/OS 10.10@
1142 size_t /* O - Remaining bytes */
1143 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1145 return (http
? (size_t)http
->data_remaining
: 0);
1150 * 'httpGets()' - Get a line of text from a HTTP connection.
1153 char * /* O - Line or NULL */
1154 httpGets(char *line
, /* I - Line to read into */
1155 int length
, /* I - Max length of buffer */
1156 http_t
*http
) /* I - HTTP connection */
1158 char *lineptr
, /* Pointer into line */
1159 *lineend
, /* End of line */
1160 *bufptr
, /* Pointer into input buffer */
1161 *bufend
; /* Pointer to end of buffer */
1162 ssize_t bytes
; /* Number of bytes read */
1163 int eol
; /* End-of-line? */
1166 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1168 if (!http
|| !line
|| length
<= 1)
1172 * Read a line from the buffer...
1177 lineend
= line
+ length
- 1;
1180 while (lineptr
< lineend
)
1183 * Pre-load the buffer as needed...
1192 while (http
->used
== 0)
1195 * No newline; see if there is more data to be read...
1198 while (!_httpWait(http
, http
->wait_value
, 1))
1200 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1203 DEBUG_puts("3httpGets: Timed out!");
1205 http
->error
= WSAETIMEDOUT
;
1207 http
->error
= ETIMEDOUT
;
1212 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1214 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1219 * Nope, can't get a line this time...
1223 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1225 if (WSAGetLastError() == WSAEINTR
)
1227 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1229 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1232 http
->error
= WSAGetLastError();
1234 else if (WSAGetLastError() != http
->error
)
1236 http
->error
= WSAGetLastError();
1241 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1245 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1247 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1249 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1252 http
->error
= errno
;
1254 else if (errno
!= http
->error
)
1256 http
->error
= errno
;
1263 else if (bytes
== 0)
1265 http
->error
= EPIPE
;
1271 * Yup, update the amount used...
1274 http
->used
+= (int)bytes
;
1278 * Now copy as much of the current line as possible...
1281 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1282 lineptr
< lineend
&& bufptr
< bufend
;)
1284 if (*bufptr
== 0x0a)
1290 else if (*bufptr
== 0x0d)
1293 *lineptr
++ = *bufptr
++;
1296 http
->used
-= (int)(bufptr
- http
->buffer
);
1298 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1306 http
->activity
= time(NULL
);
1310 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1316 DEBUG_puts("3httpGets: No new line available!");
1323 * 'httpGetState()' - Get the current state of the HTTP request.
1326 http_state_t
/* O - HTTP state */
1327 httpGetState(http_t
*http
) /* I - HTTP connection */
1329 return (http
? http
->state
: HTTP_STATE_ERROR
);
1334 * 'httpGetStatus()' - Get the status of the last HTTP request.
1336 * @since CUPS 1.2/OS X 10.5@
1339 http_status_t
/* O - HTTP status */
1340 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1342 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1347 * 'httpGetSubField()' - Get a sub-field value.
1352 char * /* O - Value or NULL */
1353 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1354 http_field_t field
, /* I - Field index */
1355 const char *name
, /* I - Name of sub-field */
1356 char *value
) /* O - Value string */
1358 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1363 * 'httpGetSubField2()' - Get a sub-field value.
1365 * @since CUPS 1.2/OS X 10.5@
1368 char * /* O - Value or NULL */
1369 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1370 http_field_t field
, /* I - Field index */
1371 const char *name
, /* I - Name of sub-field */
1372 char *value
, /* O - Value string */
1373 int valuelen
) /* I - Size of value buffer */
1375 const char *fptr
; /* Pointer into field */
1376 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1377 *ptr
, /* Pointer into string buffer */
1378 *end
; /* End of value buffer */
1380 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1382 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1383 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1386 end
= value
+ valuelen
- 1;
1388 for (fptr
= http
->fields
[field
]; *fptr
;)
1391 * Skip leading whitespace...
1394 while (_cups_isspace(*fptr
))
1404 * Get the sub-field name...
1408 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1409 ptr
< (temp
+ sizeof(temp
) - 1);
1414 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1417 * Skip trailing chars up to the '='...
1420 while (_cups_isspace(*fptr
))
1430 * Skip = and leading whitespace...
1435 while (_cups_isspace(*fptr
))
1441 * Read quoted string...
1444 for (ptr
= value
, fptr
++;
1445 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1450 while (*fptr
&& *fptr
!= '\"')
1459 * Read unquoted string...
1463 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1468 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1472 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1475 * See if this is the one...
1478 if (!strcmp(name
, temp
))
1480 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1487 DEBUG_puts("3httpGetSubField2: Returning NULL");
1494 * 'httpGetVersion()' - Get the HTTP version at the other end.
1497 http_version_t
/* O - Version number */
1498 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1500 return (http
? http
->version
: HTTP_VERSION_1_0
);
1505 * 'httpHead()' - Send a HEAD request to the server.
1508 int /* O - Status of call (0 = success) */
1509 httpHead(http_t
*http
, /* I - HTTP connection */
1510 const char *uri
) /* I - URI for head */
1512 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1513 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1518 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1519 * default HTTP proxy (if any).
1523 httpInitialize(void)
1525 static int initialized
= 0; /* Have we been called before? */
1527 WSADATA winsockdata
; /* WinSock data */
1534 _cupsGlobalUnlock();
1539 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1541 #elif !defined(SO_NOSIGPIPE)
1543 * Ignore SIGPIPE signals...
1547 sigset(SIGPIPE
, SIG_IGN
);
1549 # elif defined(HAVE_SIGACTION)
1550 struct sigaction action
; /* POSIX sigaction data */
1553 memset(&action
, 0, sizeof(action
));
1554 action
.sa_handler
= SIG_IGN
;
1555 sigaction(SIGPIPE
, &action
, NULL
);
1558 signal(SIGPIPE
, SIG_IGN
);
1559 # endif /* !SO_NOSIGPIPE */
1563 _httpTLSInitialize();
1564 # endif /* HAVE_SSL */
1567 _cupsGlobalUnlock();
1572 * 'httpIsChunked()' - Report whether a message body is chunked.
1574 * This function returns non-zero if the message body is composed of
1575 * variable-length chunks.
1577 * @since CUPS 2.0/OS 10.10@
1580 int /* O - 1 if chunked, 0 if not */
1581 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1583 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1588 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1590 * This function returns non-zero if the connection is currently encrypted.
1592 * @since CUPS 2.0/OS 10.10@
1595 int /* O - 1 if encrypted, 0 if not */
1596 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1598 return (http
? http
->tls
!= NULL
: 0);
1603 * 'httpOptions()' - Send an OPTIONS request to the server.
1606 int /* O - Status of call (0 = success) */
1607 httpOptions(http_t
*http
, /* I - HTTP connection */
1608 const char *uri
) /* I - URI for options */
1610 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1615 * 'httpPeek()' - Peek at data from a HTTP connection.
1617 * This function copies available data from the given HTTP connection, reading
1618 * a buffer as needed. The data is still available for reading using
1619 * @link httpRead@ or @link httpRead2@.
1621 * For non-blocking connections the usual timeouts apply.
1623 * @since CUPS 1.7/OS X 10.9@
1626 ssize_t
/* O - Number of bytes copied */
1627 httpPeek(http_t
*http
, /* I - HTTP connection */
1628 char *buffer
, /* I - Buffer for data */
1629 size_t length
) /* I - Maximum number of bytes */
1631 ssize_t bytes
; /* Bytes read */
1632 char len
[32]; /* Length string */
1635 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1637 if (http
== NULL
|| buffer
== NULL
)
1640 http
->activity
= time(NULL
);
1646 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1647 http
->data_remaining
<= 0)
1649 DEBUG_puts("2httpPeek: Getting chunk length...");
1651 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1653 DEBUG_puts("1httpPeek: Could not get length!");
1659 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1660 if (!httpGets(len
, sizeof(len
), http
))
1662 DEBUG_puts("1httpPeek: Could not get chunk length.");
1667 http
->data_remaining
= strtoll(len
, NULL
, 16);
1669 if (http
->data_remaining
< 0)
1671 DEBUG_puts("1httpPeek: Negative chunk length!");
1676 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1677 CUPS_LLCAST http
->data_remaining
));
1679 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1682 * A zero-length chunk ends a transfer; unless we are reading POST
1687 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1688 http_content_coding_finish(http
);
1689 #endif /* HAVE_LIBZ */
1691 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1692 httpGets(len
, sizeof(len
), http
);
1694 if (http
->state
== HTTP_STATE_POST_RECV
)
1697 http
->state
= HTTP_STATE_STATUS
;
1699 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1700 httpStateString(http
->state
)));
1703 * Prevent future reads for this request...
1706 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1710 else if (length
> (size_t)http
->data_remaining
)
1711 length
= (size_t)http
->data_remaining
;
1714 if (http
->used
== 0 &&
1715 (http
->coding
== _HTTP_CODING_IDENTITY
||
1716 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1718 if (http
->used
== 0)
1719 #endif /* HAVE_LIBZ */
1722 * Buffer small reads for better performance...
1725 ssize_t buflen
; /* Length of read for buffer */
1727 if (!http
->blocking
)
1729 while (!httpWait(http
, http
->wait_value
))
1731 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1738 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1739 buflen
= sizeof(http
->buffer
);
1741 buflen
= (ssize_t
)http
->data_remaining
;
1743 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1744 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1746 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1747 CUPS_LLCAST bytes
));
1751 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1754 http
->used
= (int)bytes
;
1759 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1761 # ifdef HAVE_INFLATECOPY
1762 int zerr
; /* Decompressor error */
1763 z_stream stream
; /* Copy of decompressor stream */
1765 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1767 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1768 /* Number of bytes to copy */
1770 if (http
->stream
.avail_in
> 0 &&
1771 http
->stream
.next_in
> http
->sbuffer
)
1772 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1774 http
->stream
.next_in
= http
->sbuffer
;
1776 if (buflen
> (size_t)http
->data_remaining
)
1777 buflen
= (size_t)http
->data_remaining
;
1779 if (buflen
> (size_t)http
->used
)
1780 buflen
= (size_t)http
->used
;
1782 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1783 "decompression buffer.", (int)buflen
));
1785 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1786 http
->stream
.avail_in
+= buflen
;
1787 http
->used
-= (int)buflen
;
1788 http
->data_remaining
-= (off_t
)buflen
;
1791 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1794 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1795 (int)http
->stream
.avail_in
));
1797 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1799 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1800 http
->error
= ENOMEM
;
1804 stream
.next_out
= (Bytef
*)buffer
;
1805 stream
.avail_out
= (uInt
)length
;
1807 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1808 inflateEnd(&stream
);
1812 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1814 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1821 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1824 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1825 "work with compressed streams.");
1827 # endif /* HAVE_INFLATECOPY */
1830 #endif /* HAVE_LIBZ */
1833 if (length
> (size_t)http
->used
)
1834 length
= (size_t)http
->used
;
1836 bytes
= (ssize_t
)length
;
1838 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1841 memcpy(buffer
, http
->buffer
, length
);
1849 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1852 http
->error
= WSAGetLastError();
1854 if (errno
== EINTR
|| errno
== EAGAIN
)
1857 http
->error
= errno
;
1860 else if (bytes
== 0)
1862 http
->error
= EPIPE
;
1871 * 'httpPost()' - Send a POST request to the server.
1874 int /* O - Status of call (0 = success) */
1875 httpPost(http_t
*http
, /* I - HTTP connection */
1876 const char *uri
) /* I - URI for post */
1878 return (http_send(http
, HTTP_STATE_POST
, uri
));
1883 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1888 int /* O - Number of bytes written */
1889 httpPrintf(http_t
*http
, /* I - HTTP connection */
1890 const char *format
, /* I - printf-style format string */
1891 ...) /* I - Additional args as needed */
1893 ssize_t bytes
; /* Number of bytes to write */
1894 char buf
[16384]; /* Buffer for formatted string */
1895 va_list ap
; /* Variable argument pointer */
1898 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1900 va_start(ap
, format
);
1901 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1904 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1906 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1907 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1912 DEBUG_puts("4httpPrintf: flushing existing data...");
1914 if (httpFlushWrite(http
) < 0)
1918 return ((int)http_write(http
, buf
, (size_t)bytes
));
1924 * 'httpPut()' - Send a PUT request to the server.
1927 int /* O - Status of call (0 = success) */
1928 httpPut(http_t
*http
, /* I - HTTP connection */
1929 const char *uri
) /* I - URI to put */
1931 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1932 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1937 * 'httpRead()' - Read data from a HTTP connection.
1939 * This function is deprecated. Use the httpRead2() function which can
1940 * read more than 2GB of data.
1945 int /* O - Number of bytes read */
1946 httpRead(http_t
*http
, /* I - HTTP connection */
1947 char *buffer
, /* I - Buffer for data */
1948 int length
) /* I - Maximum number of bytes */
1950 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1955 * 'httpRead2()' - Read data from a HTTP connection.
1957 * @since CUPS 1.2/OS X 10.5@
1960 ssize_t
/* O - Number of bytes read */
1961 httpRead2(http_t
*http
, /* I - HTTP connection */
1962 char *buffer
, /* I - Buffer for data */
1963 size_t length
) /* I - Maximum number of bytes */
1965 ssize_t bytes
; /* Bytes read */
1969 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
));
1971 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
));
1972 #endif /* HAVE_LIBZ */
1974 if (http
== NULL
|| buffer
== NULL
)
1977 http
->activity
= time(NULL
);
1984 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1988 if (http
->stream
.avail_in
> 0)
1990 int zerr
; /* Decompressor error */
1992 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1993 (int)http
->stream
.avail_in
, (int)length
));
1995 http
->stream
.next_out
= (Bytef
*)buffer
;
1996 http
->stream
.avail_out
= (uInt
)length
;
1998 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2000 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2002 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
2009 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
2011 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2012 http
->stream
.avail_in
, http
->stream
.avail_out
,
2020 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
2021 /* Additional bytes for buffer */
2025 if (http
->stream
.avail_in
> 0 &&
2026 http
->stream
.next_in
> http
->sbuffer
)
2027 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2029 http
->stream
.next_in
= http
->sbuffer
;
2031 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2032 "decompression buffer.", (int)buflen
));
2034 if (http
->data_remaining
> 0)
2036 if (buflen
> http
->data_remaining
)
2037 buflen
= (ssize_t
)http
->data_remaining
;
2039 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2041 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2042 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2048 else if (bytes
== 0)
2051 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2052 "decompression buffer.", CUPS_LLCAST bytes
));
2054 http
->data_remaining
-= bytes
;
2055 http
->stream
.avail_in
+= (uInt
)bytes
;
2057 if (http
->data_remaining
<= 0 &&
2058 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2061 * Read the trailing blank line now...
2064 char len
[32]; /* Length string */
2066 httpGets(len
, sizeof(len
), http
);
2078 #endif /* HAVE_LIBZ */
2079 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2081 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2083 http
->data_remaining
-= bytes
;
2085 if (http
->data_remaining
<= 0)
2088 * Read the trailing blank line now...
2091 char len
[32]; /* Length string */
2093 httpGets(len
, sizeof(len
), http
);
2097 else if (http
->data_remaining
<= 0)
2100 * No more data to read...
2107 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2110 if (length
> (size_t)http
->data_remaining
)
2111 length
= (size_t)http
->data_remaining
;
2113 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2115 http
->data_remaining
-= bytes
;
2117 if (http
->data_remaining
<= 0 &&
2118 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2121 * Read the trailing blank line now...
2124 char len
[32]; /* Length string */
2126 httpGets(len
, sizeof(len
), http
);
2133 (http
->coding
== _HTTP_CODING_IDENTITY
||
2134 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2135 #endif /* HAVE_LIBZ */
2136 ((http
->data_remaining
<= 0 &&
2137 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2138 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2141 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2142 http_content_coding_finish(http
);
2143 #endif /* HAVE_LIBZ */
2145 if (http
->state
== HTTP_STATE_POST_RECV
)
2147 else if (http
->state
== HTTP_STATE_GET_SEND
||
2148 http
->state
== HTTP_STATE_POST_SEND
)
2149 http
->state
= HTTP_STATE_WAITING
;
2151 http
->state
= HTTP_STATE_STATUS
;
2153 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2154 httpStateString(http
->state
)));
2162 * 'httpReadRequest()' - Read a HTTP request from a connection.
2164 * @since CUPS 1.7/OS X 10.9@
2167 http_state_t
/* O - New state of connection */
2168 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2169 char *uri
, /* I - URI buffer */
2170 size_t urilen
) /* I - Size of URI buffer */
2172 char line
[4096], /* HTTP request line */
2173 *req_method
, /* HTTP request method */
2174 *req_uri
, /* HTTP request URI */
2175 *req_version
; /* HTTP request version number string */
2179 * Range check input...
2182 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2187 if (!http
|| !uri
|| urilen
< 1)
2189 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2190 return (HTTP_STATE_ERROR
);
2192 else if (http
->state
!= HTTP_STATE_WAITING
)
2194 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2195 httpStateString(http
->state
)));
2196 return (HTTP_STATE_ERROR
);
2203 httpClearFields(http
);
2205 http
->activity
= time(NULL
);
2206 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2207 http
->data_remaining
= 0;
2208 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2209 http
->status
= HTTP_STATUS_OK
;
2210 http
->version
= HTTP_VERSION_1_1
;
2213 * Read a line from the socket...
2216 if (!httpGets(line
, sizeof(line
), http
))
2218 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2219 return (HTTP_STATE_ERROR
);
2224 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2225 return (HTTP_STATE_WAITING
);
2228 DEBUG_printf(("1httpReadRequest: %s", line
));
2237 while (*req_uri
&& !isspace(*req_uri
& 255))
2242 DEBUG_puts("1httpReadRequest: No request URI.");
2243 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2244 return (HTTP_STATE_ERROR
);
2249 while (*req_uri
&& isspace(*req_uri
& 255))
2252 req_version
= req_uri
;
2254 while (*req_version
&& !isspace(*req_version
& 255))
2259 DEBUG_puts("1httpReadRequest: No request protocol version.");
2260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2261 return (HTTP_STATE_ERROR
);
2264 *req_version
++ = '\0';
2266 while (*req_version
&& isspace(*req_version
& 255))
2273 if (!strcmp(req_method
, "OPTIONS"))
2274 http
->state
= HTTP_STATE_OPTIONS
;
2275 else if (!strcmp(req_method
, "GET"))
2276 http
->state
= HTTP_STATE_GET
;
2277 else if (!strcmp(req_method
, "HEAD"))
2278 http
->state
= HTTP_STATE_HEAD
;
2279 else if (!strcmp(req_method
, "POST"))
2280 http
->state
= HTTP_STATE_POST
;
2281 else if (!strcmp(req_method
, "PUT"))
2282 http
->state
= HTTP_STATE_PUT
;
2283 else if (!strcmp(req_method
, "DELETE"))
2284 http
->state
= HTTP_STATE_DELETE
;
2285 else if (!strcmp(req_method
, "TRACE"))
2286 http
->state
= HTTP_STATE_TRACE
;
2287 else if (!strcmp(req_method
, "CONNECT"))
2288 http
->state
= HTTP_STATE_CONNECT
;
2291 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2292 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2293 return (HTTP_STATE_UNKNOWN_METHOD
);
2296 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2297 httpStateString(http
->state
)));
2299 if (!strcmp(req_version
, "HTTP/1.0"))
2301 http
->version
= HTTP_VERSION_1_0
;
2302 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2304 else if (!strcmp(req_version
, "HTTP/1.1"))
2306 http
->version
= HTTP_VERSION_1_1
;
2307 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2311 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2312 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2313 return (HTTP_STATE_UNKNOWN_VERSION
);
2316 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2317 strlcpy(uri
, req_uri
, urilen
);
2319 return (http
->state
);
2324 * 'httpReconnect()' - Reconnect to a HTTP server.
2326 * This function is deprecated. Please use the @link httpReconnect2@ function
2332 int /* O - 0 on success, non-zero on failure */
2333 httpReconnect(http_t
*http
) /* I - HTTP connection */
2335 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2337 return (httpReconnect2(http
, 30000, NULL
));
2342 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2346 int /* O - 0 on success, non-zero on failure */
2347 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2348 int msec
, /* I - Timeout in milliseconds */
2349 int *cancel
) /* I - Pointer to "cancel" variable */
2351 http_addrlist_t
*addr
; /* Connected address */
2353 http_addrlist_t
*current
; /* Current address */
2354 char temp
[256]; /* Temporary address string */
2358 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2362 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2369 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2372 #endif /* HAVE_SSL */
2375 * Close any previously open socket...
2380 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2382 httpAddrClose(NULL
, http
->fd
);
2388 * Reset all state (except fields, which may be reused)...
2391 http
->state
= HTTP_STATE_WAITING
;
2392 http
->version
= HTTP_VERSION_1_1
;
2393 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2394 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2395 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2396 http
->_data_remaining
= 0;
2398 http
->data_remaining
= 0;
2399 http
->hostaddr
= NULL
;
2403 * Connect to the server...
2407 for (current
= http
->addrlist
; current
; current
= current
->next
)
2408 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2409 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2410 httpAddrPort(&(current
->addr
))));
2413 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2416 * Unable to connect...
2420 http
->error
= WSAGetLastError();
2422 http
->error
= errno
;
2424 http
->status
= HTTP_STATUS_ERROR
;
2426 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2427 strerror(http
->error
)));
2432 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2434 if (http
->timeout_value
> 0)
2435 http_set_timeout(http
->fd
, http
->timeout_value
);
2437 http
->hostaddr
= &(addr
->addr
);
2441 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2444 * Always do encryption via SSL.
2447 if (_httpTLSStart(http
) != 0)
2449 httpAddrClose(NULL
, http
->fd
);
2454 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2455 return (http_tls_upgrade(http
));
2456 #endif /* HAVE_SSL */
2458 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2459 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2460 httpAddrPort(http
->hostaddr
)));
2467 * 'httpSetAuthString()' - Set the current authorization string.
2469 * This function just stores a copy of the current authorization string in
2470 * the HTTP connection object. You must still call httpSetField() to set
2471 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
2472 * httpHead(), httpOptions(), httpPost, or httpPut().
2474 * @since CUPS 1.3/OS X 10.5@
2478 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2479 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2480 const char *data
) /* I - Auth data (NULL for none) */
2483 * Range check input...
2489 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2490 free(http
->authstring
);
2492 http
->authstring
= http
->_authstring
;
2497 * Set the current authorization string...
2500 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2503 if (len
> sizeof(http
->_authstring
))
2505 if ((temp
= malloc(len
)) == NULL
)
2506 len
= sizeof(http
->_authstring
);
2508 http
->authstring
= temp
;
2512 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2514 strlcpy(http
->authstring
, scheme
, len
);
2519 * Clear the current authorization string...
2522 http
->_authstring
[0] = '\0';
2528 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2531 * @since CUPS 1.5/OS X 10.7@
2534 int /* O - Status of call (0 = success) */
2535 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2536 cups_array_t
*credentials
) /* I - Array of credentials */
2538 if (!http
|| cupsArrayCount(credentials
) < 1)
2542 _httpFreeCredentials(http
->tls_credentials
);
2544 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2545 #endif /* HAVE_SSL */
2547 return (http
->tls_credentials
? 0 : -1);
2552 * 'httpSetCookie()' - Set the cookie value(s).
2554 * @since CUPS 1.1.19/OS X 10.3@
2558 httpSetCookie(http_t
*http
, /* I - Connection */
2559 const char *cookie
) /* I - Cookie string */
2568 http
->cookie
= strdup(cookie
);
2570 http
->cookie
= NULL
;
2575 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2577 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2578 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2580 * @since CUPS 1.7/OS X 10.9@
2584 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2585 http_field_t field
, /* I - Field index */
2586 const char *value
)/* I - Value */
2588 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2595 case HTTP_FIELD_ACCEPT_ENCODING
:
2596 if (http
->default_accept_encoding
)
2597 _cupsStrFree(http
->default_accept_encoding
);
2599 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2602 case HTTP_FIELD_SERVER
:
2603 if (http
->default_server
)
2604 _cupsStrFree(http
->default_server
);
2606 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2609 case HTTP_FIELD_USER_AGENT
:
2610 if (http
->default_user_agent
)
2611 _cupsStrFree(http
->default_user_agent
);
2613 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2617 DEBUG_puts("1httpSetDefaultField: Ignored.");
2624 * 'httpSetExpect()' - Set the Expect: header in a request.
2626 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2629 * @since CUPS 1.2/OS X 10.5@
2633 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2634 http_status_t expect
) /* I - HTTP status to expect
2635 (@code HTTP_STATUS_CONTINUE@) */
2637 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2640 http
->expect
= expect
;
2645 * 'httpSetField()' - Set the value of an HTTP header.
2649 httpSetField(http_t
*http
, /* I - HTTP connection */
2650 http_field_t field
, /* I - Field index */
2651 const char *value
) /* I - Value */
2653 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2656 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2657 field
>= HTTP_FIELD_MAX
||
2663 case HTTP_FIELD_ACCEPT_ENCODING
:
2664 if (http
->accept_encoding
)
2665 _cupsStrFree(http
->accept_encoding
);
2667 http
->accept_encoding
= _cupsStrAlloc(value
);
2670 case HTTP_FIELD_ALLOW
:
2672 _cupsStrFree(http
->allow
);
2674 http
->allow
= _cupsStrAlloc(value
);
2677 case HTTP_FIELD_SERVER
:
2679 _cupsStrFree(http
->server
);
2681 http
->server
= _cupsStrAlloc(value
);
2684 case HTTP_FIELD_WWW_AUTHENTICATE
:
2685 /* CUPS STR #4503 - don't override WWW-Authenticate for unknown auth schemes */
2686 if (http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
][0] &&
2687 _cups_strncasecmp(value
, "Basic ", 6) &&
2688 _cups_strncasecmp(value
, "Digest ", 7) &&
2689 _cups_strncasecmp(value
, "Negotiate ", 10))
2691 DEBUG_printf(("1httpSetField: Ignoring unknown auth scheme in \"%s\".", value
));
2695 /* Fall through to copy */
2698 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2702 if (field
== HTTP_FIELD_AUTHORIZATION
)
2705 * Special case for Authorization: as its contents can be
2706 * longer than HTTP_MAX_VALUE
2709 if (http
->field_authorization
)
2710 free(http
->field_authorization
);
2712 http
->field_authorization
= strdup(value
);
2714 else if (field
== HTTP_FIELD_HOST
)
2717 * Special-case for Host: as we don't want a trailing "." on the hostname and
2718 * need to bracket IPv6 numeric addresses.
2721 char *ptr
= strchr(value
, ':');
2723 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2726 * Bracket IPv6 numeric addresses...
2728 * This is slightly inefficient (basically copying twice), but is an edge
2729 * case and not worth optimizing...
2732 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2733 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2738 * Check for a trailing dot on the hostname...
2741 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2745 ptr
+= strlen(ptr
) - 1;
2753 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2754 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
2756 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
2757 http_content_coding_start(http
, value
);
2759 #endif /* HAVE_LIBZ */
2764 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2766 * @since CUPS 2.0/OS 10.10@
2771 http_t
*http
, /* I - HTTP connection */
2772 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2775 http
->keep_alive
= keep_alive
;
2780 * 'httpSetLength()' - Set the content-length and content-encoding.
2782 * @since CUPS 1.2/OS X 10.5@
2786 httpSetLength(http_t
*http
, /* I - HTTP connection */
2787 size_t length
) /* I - Length (0 for chunked) */
2789 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2796 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
2798 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
2802 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
2803 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
2804 CUPS_LLFMT
, CUPS_LLCAST length
);
2810 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2812 * The optional timeout callback receives both the HTTP connection and a user
2813 * data pointer and must return 1 to continue or 0 to error (time) out.
2815 * @since CUPS 1.5/OS X 10.7@
2820 http_t
*http
, /* I - HTTP connection */
2821 double timeout
, /* I - Number of seconds for timeout,
2822 must be greater than 0 */
2823 http_timeout_cb_t cb
, /* I - Callback function or NULL */
2824 void *user_data
) /* I - User data pointer */
2826 if (!http
|| timeout
<= 0.0)
2829 http
->timeout_cb
= cb
;
2830 http
->timeout_data
= user_data
;
2831 http
->timeout_value
= timeout
;
2834 http_set_timeout(http
->fd
, timeout
);
2836 http_set_wait(http
);
2841 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2843 * @since CUPS 2.0/OS 10.10@
2847 httpShutdown(http_t
*http
) /* I - HTTP connection */
2849 if (!http
|| http
->fd
< 0)
2855 #endif /* HAVE_SSL */
2858 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2860 shutdown(http
->fd
, SHUT_RD
);
2866 * 'httpTrace()' - Send an TRACE request to the server.
2869 int /* O - Status of call (0 = success) */
2870 httpTrace(http_t
*http
, /* I - HTTP connection */
2871 const char *uri
) /* I - URI for trace */
2873 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2878 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2880 * Note: Unlike httpUpdate(), this function does not flush pending write data
2881 * and only retrieves a single status line from the HTTP connection.
2884 int /* O - 1 to continue, 0 to stop */
2885 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2886 http_status_t
*status
) /* O - Current HTTP status */
2888 char line
[32768], /* Line from connection... */
2889 *value
; /* Pointer to value on line */
2890 http_field_t field
; /* Field index */
2891 int major
, minor
; /* HTTP version numbers */
2894 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2897 * Grab a single line from the connection...
2900 if (!httpGets(line
, sizeof(line
), http
))
2902 *status
= HTTP_STATUS_ERROR
;
2906 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2908 if (line
[0] == '\0')
2911 * Blank line means the start of the data section (if any). Return
2912 * the result code, too...
2914 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2915 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2919 if (http
->status
== HTTP_STATUS_CONTINUE
)
2921 *status
= http
->status
;
2925 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2926 http
->digest_tries
= 0;
2929 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2931 if (_httpTLSStart(http
) != 0)
2933 httpAddrClose(NULL
, http
->fd
);
2935 *status
= http
->status
= HTTP_STATUS_ERROR
;
2939 *status
= HTTP_STATUS_CONTINUE
;
2942 #endif /* HAVE_SSL */
2944 if (http_set_length(http
) < 0)
2946 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2947 http
->error
= EINVAL
;
2948 http
->status
= *status
= HTTP_STATUS_ERROR
;
2952 switch (http
->state
)
2954 case HTTP_STATE_GET
:
2955 case HTTP_STATE_POST
:
2956 case HTTP_STATE_POST_RECV
:
2957 case HTTP_STATE_PUT
:
2960 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2961 httpStateString(http
->state
)));
2963 case HTTP_STATE_POST_SEND
:
2964 case HTTP_STATE_HEAD
:
2968 http
->state
= HTTP_STATE_WAITING
;
2970 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2975 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2976 http_content_coding_start(http
,
2977 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2978 #endif /* HAVE_LIBZ */
2980 *status
= http
->status
;
2983 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2986 * Got the beginning of a response...
2989 int intstatus
; /* Status value as an integer */
2991 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2993 *status
= http
->status
= HTTP_STATUS_ERROR
;
2997 httpClearFields(http
);
2999 http
->version
= (http_version_t
)(major
* 100 + minor
);
3000 *status
= http
->status
= (http_status_t
)intstatus
;
3002 else if ((value
= strchr(line
, ':')) != NULL
)
3009 while (_cups_isspace(*value
))
3012 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3015 * Be tolerants of servers that send unknown attribute fields...
3018 if (!_cups_strcasecmp(line
, "expect"))
3021 * "Expect: 100-continue" or similar...
3024 http
->expect
= (http_status_t
)atoi(value
);
3026 else if (!_cups_strcasecmp(line
, "cookie"))
3029 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3032 httpSetCookie(http
, value
);
3034 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
3035 httpSetField(http
, field
, value
);
3038 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3043 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3044 http
->error
= EINVAL
;
3045 http
->status
= *status
= HTTP_STATUS_ERROR
;
3054 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3057 http_status_t
/* O - HTTP status */
3058 httpUpdate(http_t
*http
) /* I - HTTP connection */
3060 http_status_t status
; /* Request status */
3063 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
3066 * Flush pending data, if any...
3071 DEBUG_puts("2httpUpdate: flushing buffer...");
3073 if (httpFlushWrite(http
) < 0)
3074 return (HTTP_STATUS_ERROR
);
3078 * If we haven't issued any commands, then there is nothing to "update"...
3081 if (http
->state
== HTTP_STATE_WAITING
)
3082 return (HTTP_STATUS_CONTINUE
);
3085 * Grab all of the lines we can from the connection...
3088 while (_httpUpdate(http
, &status
));
3091 * See if there was an error...
3094 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3096 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3097 return (http
->status
);
3102 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3103 strerror(http
->error
)));
3104 http
->status
= HTTP_STATUS_ERROR
;
3105 return (HTTP_STATUS_ERROR
);
3109 * Return the current status...
3117 * '_httpWait()' - Wait for data available on a connection (no flush).
3120 int /* O - 1 if data is available, 0 otherwise */
3121 _httpWait(http_t
*http
, /* I - HTTP connection */
3122 int msec
, /* I - Milliseconds to wait */
3123 int usessl
) /* I - Use SSL context? */
3126 struct pollfd pfd
; /* Polled file descriptor */
3128 fd_set input_set
; /* select() input set */
3129 struct timeval timeout
; /* Timeout */
3130 #endif /* HAVE_POLL */
3131 int nfds
; /* Result from select()/poll() */
3134 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
3138 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3143 * Check the SSL/TLS buffers for data first...
3147 if (http
->tls
&& _httpTLSPending(http
))
3149 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
3152 #endif /* HAVE_SSL */
3155 * Then try doing a select() or poll() to poll the socket...
3160 pfd
.events
= POLLIN
;
3164 nfds
= poll(&pfd
, 1, msec
);
3166 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3171 FD_ZERO(&input_set
);
3172 FD_SET(http
->fd
, &input_set
);
3174 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3178 timeout
.tv_sec
= msec
/ 1000;
3179 timeout
.tv_usec
= (msec
% 1000) * 1000;
3181 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3184 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3186 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3189 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3190 WSAGetLastError() == WSAEWOULDBLOCK
));
3192 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3194 #endif /* HAVE_POLL */
3196 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3204 * 'httpWait()' - Wait for data available on a connection.
3206 * @since CUPS 1.1.19/OS X 10.3@
3209 int /* O - 1 if data is available, 0 otherwise */
3210 httpWait(http_t
*http
, /* I - HTTP connection */
3211 int msec
) /* I - Milliseconds to wait */
3214 * First see if there is data in the buffer...
3217 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3224 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3229 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3231 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3234 #endif /* HAVE_LIBZ */
3237 * Flush pending data, if any...
3242 DEBUG_puts("3httpWait: Flushing write buffer.");
3244 if (httpFlushWrite(http
) < 0)
3249 * If not, check the SSL/TLS buffers and do a select() on the connection...
3252 return (_httpWait(http
, msec
, 1));
3257 * 'httpWrite()' - Write data to a HTTP connection.
3259 * This function is deprecated. Use the httpWrite2() function which can
3260 * write more than 2GB of data.
3265 int /* O - Number of bytes written */
3266 httpWrite(http_t
*http
, /* I - HTTP connection */
3267 const char *buffer
, /* I - Buffer for data */
3268 int length
) /* I - Number of bytes to write */
3270 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3275 * 'httpWrite2()' - Write data to a HTTP connection.
3277 * @since CUPS 1.2/OS X 10.5@
3280 ssize_t
/* O - Number of bytes written */
3281 httpWrite2(http_t
*http
, /* I - HTTP connection */
3282 const char *buffer
, /* I - Buffer for data */
3283 size_t length
) /* I - Number of bytes to write */
3285 ssize_t bytes
; /* Bytes written */
3288 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3291 * Range check input...
3294 if (!http
|| !buffer
)
3296 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3301 * Mark activity on the connection...
3304 http
->activity
= time(NULL
);
3307 * Buffer small writes for better performance...
3311 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3313 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3317 http_content_coding_finish(http
);
3322 size_t slen
; /* Bytes to write */
3323 ssize_t sret
; /* Bytes written */
3325 http
->stream
.next_in
= (Bytef
*)buffer
;
3326 http
->stream
.avail_in
= (uInt
)length
;
3328 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3330 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3332 if (http
->stream
.avail_out
> 0)
3335 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3337 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3339 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3340 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3342 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3348 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3352 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3353 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3356 bytes
= (ssize_t
)length
;
3360 #endif /* HAVE_LIBZ */
3363 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3365 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3366 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3368 httpFlushWrite(http
);
3371 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3374 * Write to buffer...
3377 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3378 CUPS_LLCAST length
));
3380 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3381 http
->wused
+= (int)length
;
3382 bytes
= (ssize_t
)length
;
3387 * Otherwise write the data directly...
3390 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3391 CUPS_LLCAST length
));
3393 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3394 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3396 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3398 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3399 CUPS_LLCAST bytes
));
3402 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3403 http
->data_remaining
-= bytes
;
3409 * Handle end-of-request processing...
3412 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3413 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3416 * Finished with the transfer; unless we are sending POST or PUT
3421 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3422 http_content_coding_finish(http
);
3423 #endif /* HAVE_LIBZ */
3427 if (httpFlushWrite(http
) < 0)
3431 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3434 * Send a 0-length chunk at the end of the request...
3437 http_write(http
, "0\r\n\r\n", 5);
3440 * Reset the data state...
3443 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3444 http
->data_remaining
= 0;
3447 if (http
->state
== HTTP_STATE_POST_RECV
)
3449 else if (http
->state
== HTTP_STATE_POST_SEND
||
3450 http
->state
== HTTP_STATE_GET_SEND
)
3451 http
->state
= HTTP_STATE_WAITING
;
3453 http
->state
= HTTP_STATE_STATUS
;
3455 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3456 httpStateString(http
->state
)));
3459 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3466 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3468 * @since CUPS 1.7/OS X 10.9@
3471 int /* O - 0 on success, -1 on error */
3472 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3473 http_status_t status
) /* I - Status code */
3475 http_encoding_t old_encoding
; /* Old data_encoding value */
3476 off_t old_remaining
; /* Old data_remaining value */
3480 * Range check input...
3483 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3485 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3487 DEBUG_puts("1httpWriteResponse: Bad input.");
3492 * Set the various standard fields if they aren't already...
3495 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3496 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3498 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3500 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3501 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3504 if (http
->version
== HTTP_VERSION_1_1
)
3506 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3508 if (http
->keep_alive
)
3509 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3511 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3514 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3515 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3519 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3520 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3522 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3523 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3525 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3526 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3528 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
3529 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3531 #endif /* HAVE_SSL */
3534 httpSetField(http
, HTTP_FIELD_SERVER
,
3535 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3538 * Set the Accept-Encoding field if it isn't already...
3541 if (!http
->accept_encoding
)
3542 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3543 http
->default_accept_encoding
? http
->default_accept_encoding
:
3545 "gzip, deflate, identity");
3548 #endif /* HAVE_LIBZ */
3551 * Send the response header...
3554 old_encoding
= http
->data_encoding
;
3555 old_remaining
= http
->data_remaining
;
3556 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3558 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3559 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3561 http
->status
= HTTP_STATUS_ERROR
;
3565 if (status
!= HTTP_STATUS_CONTINUE
)
3568 * 100 Continue doesn't have the rest of the response headers...
3571 int i
; /* Looping var */
3572 const char *value
; /* Field value */
3574 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3576 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3578 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3580 http
->status
= HTTP_STATUS_ERROR
;
3588 if (strchr(http
->cookie
, ';'))
3590 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3592 http
->status
= HTTP_STATUS_ERROR
;
3596 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3598 http
->status
= HTTP_STATUS_ERROR
;
3604 * "Click-jacking" defense (STR #4492)...
3607 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3608 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3610 http
->status
= HTTP_STATUS_ERROR
;
3615 if (httpWrite2(http
, "\r\n", 2) < 2)
3617 http
->status
= HTTP_STATUS_ERROR
;
3621 if (httpFlushWrite(http
) < 0)
3623 http
->status
= HTTP_STATUS_ERROR
;
3627 if (status
== HTTP_STATUS_CONTINUE
||
3628 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3631 * Restore the old data_encoding and data_length values...
3634 http
->data_encoding
= old_encoding
;
3635 http
->data_remaining
= old_remaining
;
3637 if (old_remaining
<= INT_MAX
)
3638 http
->_data_remaining
= (int)old_remaining
;
3640 http
->_data_remaining
= INT_MAX
;
3642 else if (http
->state
== HTTP_STATE_OPTIONS
||
3643 http
->state
== HTTP_STATE_HEAD
||
3644 http
->state
== HTTP_STATE_PUT
||
3645 http
->state
== HTTP_STATE_TRACE
||
3646 http
->state
== HTTP_STATE_CONNECT
||
3647 http
->state
== HTTP_STATE_STATUS
)
3649 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3650 "was %s.", httpStateString(http
->state
)));
3651 http
->state
= HTTP_STATE_WAITING
;
3656 * Force data_encoding and data_length to be set according to the response
3660 http_set_length(http
);
3662 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3664 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3665 "was %s.", httpStateString(http
->state
)));
3666 http
->state
= HTTP_STATE_WAITING
;
3672 * Then start any content encoding...
3675 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3676 http_content_coding_start(http
,
3677 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3678 #endif /* HAVE_LIBZ */
3688 * 'http_content_coding_finish()' - Finish doing any content encoding.
3692 http_content_coding_finish(
3693 http_t
*http
) /* I - HTTP connection */
3695 int zerr
; /* Compression status */
3696 Byte dummy
[1]; /* Dummy read buffer */
3697 size_t bytes
; /* Number of bytes to write */
3700 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3701 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3703 switch (http
->coding
)
3705 case _HTTP_CODING_DEFLATE
:
3706 case _HTTP_CODING_GZIP
:
3707 http
->stream
.next_in
= dummy
;
3708 http
->stream
.avail_in
= 0;
3712 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3713 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3717 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3719 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3720 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3722 http_write(http
, (char *)http
->sbuffer
, bytes
);
3725 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3726 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3728 while (zerr
== Z_OK
);
3730 deflateEnd(&(http
->stream
));
3732 free(http
->sbuffer
);
3733 http
->sbuffer
= NULL
;
3736 httpFlushWrite(http
);
3739 case _HTTP_CODING_INFLATE
:
3740 case _HTTP_CODING_GUNZIP
:
3741 inflateEnd(&(http
->stream
));
3742 free(http
->sbuffer
);
3743 http
->sbuffer
= NULL
;
3750 http
->coding
= _HTTP_CODING_IDENTITY
;
3755 * 'http_content_coding_start()' - Start doing content encoding.
3759 http_content_coding_start(
3760 http_t
*http
, /* I - HTTP connection */
3761 const char *value
) /* I - Value of Content-Encoding */
3763 int zerr
; /* Error/status */
3764 _http_coding_t coding
; /* Content coding value */
3767 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3769 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3771 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3775 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3777 if (http
->state
== HTTP_STATE_GET_SEND
||
3778 http
->state
== HTTP_STATE_POST_SEND
)
3779 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3780 _HTTP_CODING_GUNZIP
;
3781 else if (http
->state
== HTTP_STATE_POST_RECV
||
3782 http
->state
== HTTP_STATE_PUT_RECV
)
3783 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3784 _HTTP_CODING_GUNZIP
;
3787 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3791 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3793 if (http
->state
== HTTP_STATE_GET_SEND
||
3794 http
->state
== HTTP_STATE_POST_SEND
)
3795 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3796 _HTTP_CODING_INFLATE
;
3797 else if (http
->state
== HTTP_STATE_POST_RECV
||
3798 http
->state
== HTTP_STATE_PUT_RECV
)
3799 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3800 _HTTP_CODING_INFLATE
;
3803 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3809 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3813 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3817 case _HTTP_CODING_DEFLATE
:
3818 case _HTTP_CODING_GZIP
:
3820 httpFlushWrite(http
);
3822 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3824 http
->status
= HTTP_STATUS_ERROR
;
3825 http
->error
= errno
;
3830 * Window size for compression is 11 bits - optimal based on PWG Raster
3831 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3835 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3837 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3838 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3840 http
->status
= HTTP_STATUS_ERROR
;
3841 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3845 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3846 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3849 case _HTTP_CODING_INFLATE
:
3850 case _HTTP_CODING_GUNZIP
:
3851 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3853 http
->status
= HTTP_STATUS_ERROR
;
3854 http
->error
= errno
;
3859 * Window size for decompression is up to 15 bits (maximum supported).
3860 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3863 if ((zerr
= inflateInit2(&(http
->stream
),
3864 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3867 free(http
->sbuffer
);
3868 http
->sbuffer
= NULL
;
3869 http
->status
= HTTP_STATUS_ERROR
;
3870 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3874 http
->stream
.avail_in
= 0;
3875 http
->stream
.next_in
= http
->sbuffer
;
3882 http
->coding
= coding
;
3884 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3887 #endif /* HAVE_LIBZ */
3891 * 'http_create()' - Create an unconnected HTTP connection.
3894 static http_t
* /* O - HTTP connection */
3896 const char *host
, /* I - Hostname */
3897 int port
, /* I - Port number */
3898 http_addrlist_t
*addrlist
, /* I - Address list or NULL */
3899 int family
, /* I - Address family or AF_UNSPEC */
3900 http_encryption_t encryption
, /* I - Encryption to use */
3901 int blocking
, /* I - 1 for blocking mode */
3902 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3904 http_t
*http
; /* New HTTP connection */
3905 char service
[255]; /* Service name */
3906 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3909 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
));
3911 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3917 * Lookup the host...
3922 myaddrlist
= httpAddrCopyList(addrlist
);
3926 snprintf(service
, sizeof(service
), "%d", port
);
3928 myaddrlist
= httpAddrGetList(host
, family
, service
);
3935 * Allocate memory for the structure...
3938 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3940 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3941 httpAddrFreeList(addrlist
);
3946 * Initialize the HTTP data...
3950 http
->activity
= time(NULL
);
3951 http
->addrlist
= myaddrlist
;
3952 http
->blocking
= blocking
;
3955 http
->gssctx
= GSS_C_NO_CONTEXT
;
3956 http
->gssname
= GSS_C_NO_NAME
;
3957 #endif /* HAVE_GSSAPI */
3958 http
->status
= HTTP_STATUS_CONTINUE
;
3959 http
->version
= HTTP_VERSION_1_1
;
3962 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3964 if (port
== 443) /* Always use encryption for https */
3965 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3967 http
->encryption
= encryption
;
3969 http_set_wait(http
);
3972 * Return the new structure...
3981 * 'http_debug_hex()' - Do a hex dump of a buffer.
3985 http_debug_hex(const char *prefix
, /* I - Prefix for line */
3986 const char *buffer
, /* I - Buffer to dump */
3987 int bytes
) /* I - Bytes to dump */
3989 int i
, j
, /* Looping vars */
3990 ch
; /* Current character */
3991 char line
[255], /* Line buffer */
3992 *start
, /* Start of line after prefix */
3993 *ptr
; /* Pointer into line */
3996 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
3999 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4001 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4002 start
= line
+ strlen(line
);
4004 for (i
= 0; i
< bytes
; i
+= 16)
4006 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4007 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
4011 memcpy(ptr
, " ", 3);
4016 memcpy(ptr
, " ", 3);
4019 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4021 ch
= buffer
[i
+ j
] & 255;
4023 if (ch
< ' ' || ch
>= 127)
4037 * 'http_read()' - Read a buffer from a HTTP connection.
4039 * This function does the low-level read from the socket, retrying and timing
4043 static ssize_t
/* O - Number of bytes read or -1 on error */
4044 http_read(http_t
*http
, /* I - HTTP connection */
4045 char *buffer
, /* I - Buffer */
4046 size_t length
) /* I - Maximum bytes to read */
4048 ssize_t bytes
; /* Bytes read */
4051 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4053 if (!http
->blocking
)
4055 while (!httpWait(http
, http
->wait_value
))
4057 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4060 DEBUG_puts("2http_read: Timeout.");
4065 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4071 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4073 #endif /* HAVE_SSL */
4074 bytes
= recv(http
->fd
, buffer
, length
, 0);
4079 if (WSAGetLastError() != WSAEINTR
)
4081 http
->error
= WSAGetLastError();
4084 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4086 if (!http
->timeout_cb
||
4087 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4089 http
->error
= WSAEWOULDBLOCK
;
4094 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4096 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4098 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4100 http
->error
= errno
;
4103 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4105 http
->error
= errno
;
4109 else if (errno
!= EINTR
)
4111 http
->error
= errno
;
4119 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4120 CUPS_LLCAST bytes
));
4123 http_debug_hex("http_read", buffer
, (int)bytes
);
4129 if (WSAGetLastError() == WSAEINTR
)
4132 http
->error
= WSAGetLastError();
4134 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4137 http
->error
= errno
;
4140 else if (bytes
== 0)
4142 http
->error
= EPIPE
;
4151 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4153 * This function reads data from the HTTP buffer or from the socket, as needed.
4156 static ssize_t
/* O - Number of bytes read or -1 on error */
4157 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4158 char *buffer
, /* I - Buffer */
4159 size_t length
) /* I - Maximum bytes to read */
4161 ssize_t bytes
; /* Bytes read */
4164 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4168 if (length
> (size_t)http
->used
)
4169 bytes
= (ssize_t
)http
->used
;
4171 bytes
= (ssize_t
)length
;
4173 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4176 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4177 http
->used
-= (int)bytes
;
4180 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4183 bytes
= http_read(http
, buffer
, length
);
4190 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4192 * This function reads and validates the chunk length, then does a buffered read
4193 * returning the number of bytes placed in the buffer.
4196 static ssize_t
/* O - Number of bytes read or -1 on error */
4197 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4198 char *buffer
, /* I - Buffer */
4199 size_t length
) /* I - Maximum bytes to read */
4201 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4203 if (http
->data_remaining
<= 0)
4205 char len
[32]; /* Length string */
4207 if (!httpGets(len
, sizeof(len
), http
))
4209 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4215 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4216 if (!httpGets(len
, sizeof(len
), http
))
4218 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4223 http
->data_remaining
= strtoll(len
, NULL
, 16);
4225 if (http
->data_remaining
< 0)
4227 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4228 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4232 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4233 len
, CUPS_LLCAST http
->data_remaining
));
4235 if (http
->data_remaining
== 0)
4238 * 0-length chunk, grab trailing blank line...
4241 httpGets(len
, sizeof(len
), http
);
4245 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4246 CUPS_LLCAST http
->data_remaining
));
4248 if (http
->data_remaining
<= 0)
4250 else if (length
> (size_t)http
->data_remaining
)
4251 length
= (size_t)http
->data_remaining
;
4253 return (http_read_buffered(http
, buffer
, length
));
4258 * 'http_send()' - Send a request with all fields and the trailing blank line.
4261 static int /* O - 0 on success, non-zero on error */
4262 http_send(http_t
*http
, /* I - HTTP connection */
4263 http_state_t request
, /* I - Request code */
4264 const char *uri
) /* I - URI */
4266 int i
; /* Looping var */
4267 char buf
[1024]; /* Encoded URI buffer */
4268 const char *value
; /* Field value */
4269 static const char * const codes
[] = /* Request code strings */
4289 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4291 if (http
== NULL
|| uri
== NULL
)
4295 * Set the User-Agent field if it isn't already...
4298 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4300 if (http
->default_user_agent
)
4301 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4303 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4307 * Set the Accept-Encoding field if it isn't already...
4310 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4311 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4312 http
->default_accept_encoding
);
4315 * Encode the URI as needed...
4318 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4321 * See if we had an error the last time around; if so, reconnect...
4324 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4325 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4327 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4328 http
->fd
, http
->status
, http
->tls_upgrade
));
4330 if (httpReconnect2(http
, 30000, NULL
))
4335 * Flush any written data that is pending...
4340 if (httpFlushWrite(http
) < 0)
4341 if (httpReconnect2(http
, 30000, NULL
))
4346 * Send the request header...
4349 http
->state
= request
;
4350 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4352 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4355 http
->status
= HTTP_STATUS_CONTINUE
;
4358 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4360 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4361 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4363 #endif /* HAVE_SSL */
4365 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4367 http
->status
= HTTP_STATUS_ERROR
;
4371 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4372 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4374 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4376 if (i
== HTTP_FIELD_HOST
)
4378 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4379 httpAddrPort(http
->hostaddr
)) < 1)
4381 http
->status
= HTTP_STATUS_ERROR
;
4385 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4387 http
->status
= HTTP_STATUS_ERROR
;
4393 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4395 http
->status
= HTTP_STATUS_ERROR
;
4399 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4400 http
->mode
, http
->state
));
4402 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4403 (http
->state
== HTTP_STATE_POST_RECV
||
4404 http
->state
== HTTP_STATE_PUT_RECV
))
4405 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4407 http
->status
= HTTP_STATUS_ERROR
;
4411 if (httpPrintf(http
, "\r\n") < 1)
4413 http
->status
= HTTP_STATUS_ERROR
;
4417 if (httpFlushWrite(http
) < 0)
4420 http_set_length(http
);
4421 httpClearFields(http
);
4424 * The Kerberos and AuthRef authentication strings can only be used once...
4427 if (http
->field_authorization
&& http
->authstring
&&
4428 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4429 !strncmp(http
->authstring
, "AuthRef", 7)))
4431 http
->_authstring
[0] = '\0';
4433 if (http
->authstring
!= http
->_authstring
)
4434 free(http
->authstring
);
4436 http
->authstring
= http
->_authstring
;
4444 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4447 static off_t
/* O - Remainder or -1 on error */
4448 http_set_length(http_t
*http
) /* I - Connection */
4450 off_t remaining
; /* Remainder */
4453 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4455 if ((remaining
= httpGetLength2(http
)) >= 0)
4457 if (http
->mode
== _HTTP_MODE_SERVER
&&
4458 http
->state
!= HTTP_STATE_GET_SEND
&&
4459 http
->state
!= HTTP_STATE_PUT
&&
4460 http
->state
!= HTTP_STATE_POST
&&
4461 http
->state
!= HTTP_STATE_POST_SEND
)
4463 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4467 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
4470 DEBUG_puts("1http_set_length: Setting data_encoding to "
4471 "HTTP_ENCODING_CHUNKED.");
4472 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4476 DEBUG_puts("1http_set_length: Setting data_encoding to "
4477 "HTTP_ENCODING_LENGTH.");
4478 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4481 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4482 CUPS_LLCAST remaining
));
4483 http
->data_remaining
= remaining
;
4485 if (remaining
<= INT_MAX
)
4486 http
->_data_remaining
= (int)remaining
;
4488 http
->_data_remaining
= INT_MAX
;
4495 * 'http_set_timeout()' - Set the socket timeout values.
4499 http_set_timeout(int fd
, /* I - File descriptor */
4500 double timeout
) /* I - Timeout in seconds */
4503 DWORD tv
= (DWORD
)(timeout
* 1000);
4504 /* Timeout in milliseconds */
4506 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4507 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4510 struct timeval tv
; /* Timeout in secs and usecs */
4512 tv
.tv_sec
= (int)timeout
;
4513 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4515 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4516 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4522 * 'http_set_wait()' - Set the default wait value for reads.
4526 http_set_wait(http_t
*http
) /* I - HTTP connection */
4530 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4532 if (http
->wait_value
<= 0)
4533 http
->wait_value
= 60000;
4536 http
->wait_value
= 10000;
4542 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4545 static int /* O - Status of connection */
4546 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4548 int ret
; /* Return value */
4549 http_t myhttp
; /* Local copy of HTTP data */
4552 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4555 * Flush the connection to make sure any previous "Upgrade" message
4562 * Copy the HTTP data to a local variable so we can do the OPTIONS
4563 * request without interfering with the existing request data...
4566 memcpy(&myhttp
, http
, sizeof(myhttp
));
4569 * Send an OPTIONS request to the server, requiring SSL or TLS
4570 * encryption on the link...
4573 http
->tls_upgrade
= 1;
4574 http
->field_authorization
= NULL
; /* Don't free the auth string */
4576 httpClearFields(http
);
4577 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4578 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4580 if ((ret
= httpOptions(http
, "*")) == 0)
4583 * Wait for the secure connection...
4586 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4590 * Restore the HTTP request data...
4593 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4594 http
->data_encoding
= myhttp
.data_encoding
;
4595 http
->data_remaining
= myhttp
.data_remaining
;
4596 http
->_data_remaining
= myhttp
._data_remaining
;
4597 http
->expect
= myhttp
.expect
;
4598 http
->field_authorization
= myhttp
.field_authorization
;
4599 http
->digest_tries
= myhttp
.digest_tries
;
4600 http
->tls_upgrade
= 0;
4603 * See if we actually went secure...
4609 * Server does not support HTTP upgrade...
4612 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4614 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4615 httpAddrClose(NULL
, http
->fd
);
4624 #endif /* HAVE_SSL */
4628 * 'http_write()' - Write a buffer to a HTTP connection.
4631 static ssize_t
/* O - Number of bytes written */
4632 http_write(http_t
*http
, /* I - HTTP connection */
4633 const char *buffer
, /* I - Buffer for data */
4634 size_t length
) /* I - Number of bytes to write */
4636 ssize_t tbytes
, /* Total bytes sent */
4637 bytes
; /* Bytes sent */
4640 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4646 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4648 if (http
->timeout_cb
)
4651 struct pollfd pfd
; /* Polled file descriptor */
4653 fd_set output_set
; /* Output ready for write? */
4654 struct timeval timeout
; /* Timeout value */
4655 #endif /* HAVE_POLL */
4656 int nfds
; /* Result from select()/poll() */
4662 pfd
.events
= POLLOUT
;
4664 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4665 (errno
== EINTR
|| errno
== EAGAIN
))
4671 FD_ZERO(&output_set
);
4672 FD_SET(http
->fd
, &output_set
);
4674 timeout
.tv_sec
= http
->wait_value
/ 1000;
4675 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4677 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4680 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4681 WSAGetLastError() == WSAEWOULDBLOCK
));
4683 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4685 #endif /* HAVE_POLL */
4689 http
->error
= errno
;
4692 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4695 http
->error
= WSAEWOULDBLOCK
;
4697 http
->error
= EWOULDBLOCK
;
4707 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4709 #endif /* HAVE_SSL */
4710 bytes
= send(http
->fd
, buffer
, length
, 0);
4712 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4713 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4718 if (WSAGetLastError() == WSAEINTR
)
4720 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4722 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4725 http
->error
= WSAGetLastError();
4727 else if (WSAGetLastError() != http
->error
&&
4728 WSAGetLastError() != WSAECONNRESET
)
4730 http
->error
= WSAGetLastError();
4737 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4739 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4741 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4744 http
->error
= errno
;
4746 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4748 http
->error
= errno
;
4753 DEBUG_printf(("3http_write: error writing data (%s).",
4754 strerror(http
->error
)));
4761 length
-= (size_t)bytes
;
4765 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4768 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4775 * 'http_write_chunk()' - Write a chunked buffer.
4778 static ssize_t
/* O - Number bytes written */
4779 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4780 const char *buffer
, /* I - Buffer to write */
4781 size_t length
) /* I - Length of buffer */
4783 char header
[16]; /* Chunk header */
4784 ssize_t bytes
; /* Bytes written */
4787 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4790 * Write the chunk header, data, and trailer.
4793 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4794 if (http_write(http
, header
, strlen(header
)) < 0)
4796 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4800 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4802 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4806 if (http_write(http
, "\r\n", 2) < 0)
4808 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");