4 * HTTP routines for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * Include necessary headers...
25 #include "cups-private.h"
32 # include <sys/time.h>
33 # include <sys/resource.h>
37 #endif /* HAVE_POLL */
45 static void http_content_coding_finish(http_t
*http
);
46 static void http_content_coding_start(http_t
*http
,
48 #endif /* HAVE_LIBZ */
49 static http_t
*http_create(const char *host
, int port
,
50 http_addrlist_t
*addrlist
, int family
,
51 http_encryption_t encryption
,
52 int blocking
, _http_mode_t mode
);
54 static void http_debug_hex(const char *prefix
, const char *buffer
,
57 static http_field_t
http_field(const char *name
);
58 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
59 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
60 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
61 static int http_send(http_t
*http
, http_state_t request
,
63 static ssize_t
http_write(http_t
*http
, const char *buffer
,
65 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
68 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
70 static int http_set_credentials(http_t
*http
);
71 # endif /* HAVE_CDSASSL */
73 static off_t
http_set_length(http_t
*http
);
74 static void http_set_timeout(int fd
, double timeout
);
75 static void http_set_wait(http_t
*http
);
77 static const char *http_state_string(http_state_t state
);
80 static int http_setup_ssl(http_t
*http
);
81 static void http_shutdown_ssl(http_t
*http
);
82 static int http_upgrade(http_t
*http
);
83 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
91 static const char * const http_fields
[] =
108 "If-Unmodified-since",
126 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
128 * BIO methods for OpenSSL...
131 static int http_bio_write(BIO
*h
, const char *buf
, int num
);
132 static int http_bio_read(BIO
*h
, char *buf
, int size
);
133 static int http_bio_puts(BIO
*h
, const char *str
);
134 static long http_bio_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
135 static int http_bio_new(BIO
*h
);
136 static int http_bio_free(BIO
*data
);
138 static BIO_METHOD http_bio_methods
=
145 NULL
, /* http_bio_gets, */
151 #endif /* HAVE_SSL && HAVE_LIBSSL */
155 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
156 * specified listening socket.
158 * @since CUPS 1.7/OS X 10.9@
161 http_t
* /* O - HTTP connection or @code NULL@ */
162 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
163 int blocking
) /* I - 1 if the connection should be
164 blocking, 0 otherwise */
166 http_t
*http
; /* HTTP connection */
167 http_addrlist_t addrlist
; /* Dummy address list */
168 socklen_t addrlen
; /* Length of address */
169 int val
; /* Socket option value */
173 * Range check input...
180 * Create the client connection...
183 memset(&addrlist
, 0, sizeof(addrlist
));
185 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
186 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
187 _HTTP_MODE_SERVER
)) == NULL
)
191 * Accept the client and get the remote address...
194 addrlen
= sizeof(http_addr_t
);
196 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
199 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
205 httpAddrString(&(http
->addrlist
->addr
), http
->hostname
,
206 sizeof(http
->hostname
));
210 * Disable SIGPIPE for this socket.
214 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
215 #endif /* SO_NOSIGPIPE */
218 * Using TCP_NODELAY improves responsiveness, especially on systems
219 * with a slow loopback interface. Since we write large buffers
220 * when sending print files and requests, there shouldn't be any
221 * performance penalty for this...
225 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
229 * Close this socket when starting another process...
232 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
233 #endif /* FD_CLOEXEC */
240 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
242 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
244 * @since CUPS 1.5/OS X 10.7@
247 int /* O - 0 on success, -1 on error */
249 cups_array_t
*credentials
, /* I - Credentials array */
250 const void *data
, /* I - PEM-encoded X.509 data */
251 size_t datalen
) /* I - Length of data */
253 http_credential_t
*credential
; /* Credential data */
256 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
258 credential
->datalen
= datalen
;
260 if ((credential
->data
= malloc(datalen
)) != NULL
)
262 memcpy(credential
->data
, data
, datalen
);
263 cupsArrayAdd(credentials
, credential
);
274 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
276 * '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections.
279 BIO_METHOD
* /* O - BIO methods for OpenSSL */
280 _httpBIOMethods(void)
282 return (&http_bio_methods
);
284 #endif /* HAVE_SSL && HAVE_LIBSSL */
288 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
292 httpBlocking(http_t
*http
, /* I - HTTP connection */
293 int b
) /* I - 1 = blocking, 0 = non-blocking */
304 * 'httpCheck()' - Check to see if there is a pending response from the server.
307 int /* O - 0 = no data, 1 = data available */
308 httpCheck(http_t
*http
) /* I - HTTP connection */
310 return (httpWait(http
, 0));
315 * 'httpClearCookie()' - Clear the cookie value(s).
317 * @since CUPS 1.1.19/OS X 10.3@
321 httpClearCookie(http_t
*http
) /* I - HTTP connection */
335 * 'httpClearFields()' - Clear HTTP request fields.
339 httpClearFields(http_t
*http
) /* I - HTTP connection */
341 DEBUG_printf(("httpClearFields(http=%p)", http
));
345 memset(http
->fields
, 0, sizeof(http
->fields
));
347 if (http
->mode
== _HTTP_MODE_CLIENT
)
349 if (http
->hostname
[0] == '/')
350 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
352 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
355 if (http
->field_authorization
)
357 free(http
->field_authorization
);
358 http
->field_authorization
= NULL
;
361 if (http
->accept_encoding
)
363 _cupsStrFree(http
->accept_encoding
);
364 http
->accept_encoding
= NULL
;
369 _cupsStrFree(http
->allow
);
375 _cupsStrFree(http
->server
);
379 http
->expect
= (http_status_t
)0;
385 * 'httpClose()' - Close an HTTP connection.
389 httpClose(http_t
*http
) /* I - HTTP connection */
392 OM_uint32 minor_status
; /* Minor status code */
393 #endif /* HAVE_GSSAPI */
396 DEBUG_printf(("httpClose(http=%p)", http
));
399 * Range check input...
406 * Close any open connection...
409 _httpDisconnect(http
);
412 * Free memory used...
415 httpAddrFreeList(http
->addrlist
);
421 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
422 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
424 if (http
->gssname
!= GSS_C_NO_NAME
)
425 gss_release_name(&minor_status
, &http
->gssname
);
426 #endif /* HAVE_GSSAPI */
428 #ifdef HAVE_AUTHORIZATION_H
430 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
431 #endif /* HAVE_AUTHORIZATION_H */
433 httpClearFields(http
);
435 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
436 free(http
->authstring
);
443 * 'httpConnect()' - Connect to a HTTP server.
445 * This function is deprecated - use @link httpConnect2@ instead.
450 http_t
* /* O - New HTTP connection */
451 httpConnect(const char *host
, /* I - Host to connect to */
452 int port
) /* I - Port number */
454 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
,
460 * 'httpConnect2()' - Connect to a HTTP server.
462 * @since CUPS 1.7/OS X 10.9@
465 http_t
* /* O - New HTTP connection */
467 const char *host
, /* I - Host to connect to */
468 int port
, /* I - Port number */
469 http_addrlist_t
*addrlist
, /* I - List of addresses or NULL to lookup */
470 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
471 http_encryption_t encryption
, /* I - Type of encryption to use */
472 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
473 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
474 int *cancel
) /* I - Pointer to "cancel" variable */
476 http_t
*http
; /* New HTTP connection */
479 DEBUG_printf(("httpConnect2(host=\"%s\", port=%d, addrlist=%p, family=%d, "
480 "encryption=%d, blocking=%d, msec=%d, cancel=%p)", host
, port
,
481 addrlist
, family
, encryption
, blocking
, msec
, cancel
));
484 * Create the HTTP structure...
487 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
488 _HTTP_MODE_CLIENT
)) == NULL
)
492 * Optionally connect to the remote system...
495 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
499 * Could not connect to any known address - bail out!
509 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
511 * This function is now deprecated. Please use the @link httpConnect2@ function
517 http_t
* /* O - New HTTP connection */
519 const char *host
, /* I - Host to connect to */
520 int port
, /* I - Port number */
521 http_encryption_t encryption
) /* I - Type of encryption to use */
523 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
524 host
, port
, encryption
));
526 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
532 * 'httpCopyCredentials()' - Copy the credentials associated with an encrypted
535 * @since CUPS 1.5/OS X 10.7@
538 int /* O - Status of call (0 = success) */
540 http_t
*http
, /* I - HTTP connection */
541 cups_array_t
**credentials
) /* O - Array of credentials */
544 # elif defined(HAVE_GNUTLS)
545 # elif defined(HAVE_CDSASSL)
546 OSStatus error
; /* Error code */
547 SecTrustRef peerTrust
; /* Peer trust reference */
548 CFIndex count
; /* Number of credentials */
549 SecCertificateRef secCert
; /* Certificate reference */
550 CFDataRef data
; /* Certificate data */
551 int i
; /* Looping var */
552 # elif defined(HAVE_SSPISSL)
553 # endif /* HAVE_LIBSSL */
559 if (!http
|| !http
->tls
|| !credentials
)
565 # elif defined(HAVE_GNUTLS)
568 # elif defined(HAVE_CDSASSL)
569 if (!(error
= SSLCopyPeerTrust(http
->tls
, &peerTrust
)) && peerTrust
)
571 if ((*credentials
= cupsArrayNew(NULL
, NULL
)) != NULL
)
573 count
= SecTrustGetCertificateCount(peerTrust
);
575 for (i
= 0; i
< count
; i
++)
577 secCert
= SecTrustGetCertificateAtIndex(peerTrust
, i
);
578 if ((data
= SecCertificateCopyData(secCert
)))
580 httpAddCredential(*credentials
, CFDataGetBytePtr(data
),
581 CFDataGetLength(data
));
587 CFRelease(peerTrust
);
592 # elif defined(HAVE_SSPISSL)
597 # endif /* HAVE_LIBSSL */
602 * '_httpCreateCredentials()' - Create credentials in the internal format.
605 http_tls_credentials_t
/* O - Internal credentials */
606 _httpCreateCredentials(
607 cups_array_t
*credentials
) /* I - Array of credentials */
615 # elif defined(HAVE_GNUTLS)
618 # elif defined(HAVE_CDSASSL)
619 CFMutableArrayRef peerCerts
; /* Peer credentials reference */
620 SecCertificateRef secCert
; /* Certificate reference */
621 CFDataRef data
; /* Credential data reference */
622 http_credential_t
*credential
; /* Credential data */
625 if ((peerCerts
= CFArrayCreateMutable(kCFAllocatorDefault
,
626 cupsArrayCount(credentials
),
627 &kCFTypeArrayCallBacks
)) == NULL
)
630 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
632 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
634 if ((data
= CFDataCreate(kCFAllocatorDefault
, credential
->data
,
635 credential
->datalen
)))
637 if ((secCert
= SecCertificateCreateWithData(kCFAllocatorDefault
, data
))
640 CFArrayAppendValue(peerCerts
, secCert
);
650 # elif defined(HAVE_SSPISSL)
655 # endif /* HAVE_LIBSSL */
660 * 'httpDelete()' - Send a DELETE request to the server.
663 int /* O - Status of call (0 = success) */
664 httpDelete(http_t
*http
, /* I - HTTP connection */
665 const char *uri
) /* I - URI to delete */
667 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
672 * '_httpDisconnect()' - Disconnect a HTTP connection.
676 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
680 http_shutdown_ssl(http
);
681 #endif /* HAVE_SSL */
684 closesocket(http
->fd
);
694 * 'httpEncryption()' - Set the required encryption on the link.
697 int /* O - -1 on error, 0 on success */
698 httpEncryption(http_t
*http
, /* I - HTTP connection */
699 http_encryption_t e
) /* I - New encryption preference */
701 DEBUG_printf(("httpEncryption(http=%p, e=%d)", http
, e
));
707 http
->encryption
= e
;
709 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
710 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
711 return (httpReconnect2(http
, 30000, NULL
));
712 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
713 return (http_upgrade(http
));
717 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
721 #endif /* HAVE_SSL */
726 * 'httpError()' - Get the last error on a connection.
729 int /* O - Error code (errno) value */
730 httpError(http_t
*http
) /* I - HTTP connection */
733 return (http
->error
);
740 * 'httpFlush()' - Flush data from a HTTP connection.
744 httpFlush(http_t
*http
) /* I - HTTP connection */
746 char buffer
[8192]; /* Junk buffer */
747 int blocking
; /* To block or not to block */
748 http_state_t oldstate
; /* Old state */
751 DEBUG_printf(("httpFlush(http=%p), state=%s", http
,
752 http_state_string(http
->state
)));
755 * Nothing to do if we are in the "waiting" state...
758 if (http
->state
== HTTP_STATE_WAITING
)
762 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
765 blocking
= http
->blocking
;
769 * Read any data we can...
772 oldstate
= http
->state
;
773 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
776 * Restore blocking and reset the connection if we didn't get all of
777 * the remaining data...
780 http
->blocking
= blocking
;
782 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
786 * Didn't get the data back, so close the current connection.
791 http_content_coding_finish(http
);
792 #endif /* HAVE_LIBZ */
794 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
796 http
->state
= HTTP_STATE_WAITING
;
800 http_shutdown_ssl(http
);
801 #endif /* HAVE_SSL */
804 closesocket(http
->fd
);
815 * 'httpFlushWrite()' - Flush data in write buffer.
817 * @since CUPS 1.2/OS X 10.5@
820 int /* O - Bytes written or -1 on error */
821 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
823 int bytes
; /* Bytes written */
826 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", http
,
827 http
? http
->data_encoding
: -1));
829 if (!http
|| !http
->wused
)
831 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
832 "1httpFlushWrite: No connection.");
836 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
837 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
839 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
843 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", bytes
, errno
));
850 * '_httpFreeCredentials()' - Free internal credentials.
854 _httpFreeCredentials(
855 http_tls_credentials_t credentials
) /* I - Internal credentials */
863 #elif defined(HAVE_GNUTLS)
866 #elif defined(HAVE_CDSASSL)
867 CFRelease(credentials
);
869 #elif defined(HAVE_SSPISSL)
872 #endif /* HAVE_LIBSSL */
877 * 'httpFreeCredentials()' - Free an array of credentials.
882 cups_array_t
*credentials
) /* I - Array of credentials */
884 http_credential_t
*credential
; /* Credential */
887 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
889 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
891 cupsArrayRemove(credentials
, credential
);
892 free((void *)credential
->data
);
896 cupsArrayDelete(credentials
);
901 * 'httpGet()' - Send a GET request to the server.
904 int /* O - Status of call (0 = success) */
905 httpGet(http_t
*http
, /* I - HTTP connection */
906 const char *uri
) /* I - URI to get */
908 return (http_send(http
, HTTP_STATE_GET
, uri
));
913 * 'httpGetActivity()' - Get the most recent activity for a connection.
915 * The return value is the UNIX time of the last read or write.
920 time_t /* O - Time of last read or write */
921 httpGetActivity(http_t
*http
) /* I - HTTP connection */
923 return (http
? http
->activity
: 0);
928 * 'httpGetAuthString()' - Get the current authorization string.
930 * The authorization string is set by cupsDoAuthentication() and
931 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
932 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
935 * @since CUPS 1.3/OS X 10.5@
938 char * /* O - Authorization string */
939 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
942 return (http
->authstring
);
949 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
951 * @since CUPS 1.2/OS X 10.5@
954 int /* O - 1 if blocking, 0 if non-blocking */
955 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
957 return (http
? http
->blocking
: 0);
962 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
963 * the client and server.
965 * This function uses the value of the Accepts-Encoding HTTP header and must be
966 * called after receiving a response from the server or a request from the
967 * client. The value returned can be use in subsequent requests (for clients)
968 * or in the response (for servers) in order to compress the content stream.
970 * @since CUPS 1.7/OS X 10.9@
973 const char * /* O - Content-Coding value or
974 @code NULL@ for the identity
976 httpGetContentEncoding(http_t
*http
) /* I - Connection to client/server */
979 if (http
&& http
->accept_encoding
)
981 int i
; /* Looping var */
982 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
983 *start
, /* Start of coding value */
984 *end
; /* End of coding value */
985 double qvalue
; /* "qvalue" for coding */
986 struct lconv
*loc
= localeconv(); /* Locale data */
987 static const char * const codings
[] =
988 { /* Supported content codings */
995 strlcpy(temp
, http
->accept_encoding
, sizeof(temp
));
997 for (start
= temp
; *start
; start
= end
)
1000 * Find the end of the coding name...
1005 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
1011 * Grab the qvalue as needed...
1014 if (!strncmp(end
, ";q=", 3))
1015 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
1018 * Skip past all attributes...
1022 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
1028 while (*end
&& isspace(*end
& 255))
1032 * Check value if it matches something we support...
1038 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
1039 if (!strcmp(start
, codings
[i
]))
1040 return (codings
[i
]);
1043 #endif /* HAVE_LIBZ */
1050 * 'httpGetCookie()' - Get any cookie data from the response.
1052 * @since CUPS 1.1.19/OS X 10.3@
1055 const char * /* O - Cookie data or NULL */
1056 httpGetCookie(http_t
*http
) /* I - HTTP connecion */
1058 return (http
? http
->cookie
: NULL
);
1063 * 'httpGetExpect()' - Get the value of the Expect header, if any.
1065 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
1066 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
1068 * @since CUPS 1.7/OS X 10.9@
1071 http_status_t
/* O - Expect: status, if any */
1072 httpGetExpect(http_t
*http
) /* I - Connection to client */
1075 return (HTTP_STATUS_ERROR
);
1077 return (http
->expect
);
1082 * 'httpGetFd()' - Get the file descriptor associated with a connection.
1084 * @since CUPS 1.2/OS X 10.5@
1087 int /* O - File descriptor or -1 if none */
1088 httpGetFd(http_t
*http
) /* I - HTTP connection */
1090 return (http
? http
->fd
: -1);
1095 * 'httpGetField()' - Get a field value from a request/response.
1098 const char * /* O - Field value */
1099 httpGetField(http_t
*http
, /* I - HTTP connection */
1100 http_field_t field
) /* I - Field to get */
1102 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1107 case HTTP_FIELD_ACCEPT_ENCODING
:
1108 return (http
->accept_encoding
);
1110 case HTTP_FIELD_ALLOW
:
1111 return (http
->allow
);
1113 case HTTP_FIELD_SERVER
:
1114 return (http
->server
);
1116 case HTTP_FIELD_AUTHORIZATION
:
1117 if (http
->field_authorization
)
1120 * Special case for WWW-Authenticate: as its contents can be
1121 * longer than HTTP_MAX_VALUE...
1124 return (http
->field_authorization
);
1128 return (http
->fields
[field
]);
1134 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
1139 http_keepalive_t
/* O - Keep-Alive state */
1140 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
1142 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
1147 * 'httpGetLength()' - Get the amount of data remaining from the
1148 * content-length or transfer-encoding fields.
1150 * This function is deprecated and will not return lengths larger than
1151 * 2^31 - 1; use httpGetLength2() instead.
1156 int /* O - Content length */
1157 httpGetLength(http_t
*http
) /* I - HTTP connection */
1160 * Get the read content length and return the 32-bit value.
1165 httpGetLength2(http
);
1167 return (http
->_data_remaining
);
1175 * 'httpGetLength2()' - Get the amount of data remaining from the
1176 * content-length or transfer-encoding fields.
1178 * This function returns the complete content length, even for
1179 * content larger than 2^31 - 1.
1181 * @since CUPS 1.2/OS X 10.5@
1184 off_t
/* O - Content length */
1185 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1187 off_t remaining
; /* Remaining length */
1190 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http
,
1191 http_state_string(http
->state
)));
1196 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1198 DEBUG_puts("4httpGetLength2: chunked request!");
1204 * The following is a hack for HTTP servers that don't send a
1205 * Content-Length or Transfer-Encoding field...
1207 * If there is no Content-Length then the connection must close
1208 * after the transfer is complete...
1211 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1214 * Default content length is 0 for errors and certain types of operations,
1215 * and 2^31-1 for other successful requests...
1218 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1219 http
->state
== HTTP_STATE_OPTIONS
||
1220 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1221 http
->state
== HTTP_STATE_HEAD
||
1222 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1223 http
->state
== HTTP_STATE_DELETE
||
1224 http
->state
== HTTP_STATE_TRACE
||
1225 http
->state
== HTTP_STATE_CONNECT
)
1228 remaining
= 2147483647;
1230 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1234 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1235 CUPS_LLCAST remaining
));
1243 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1248 size_t /* O - Number of bytes available */
1249 httpGetReady(http_t
*http
) /* I - HTTP connection */
1253 else if (http
->used
> 0)
1254 return (http
->used
);
1258 size_t ready
; /* Ready bytes */
1261 if ((ready
= SSL_pending((SSL
*)(http
->tls
))) > 0)
1263 # elif defined(HAVE_GNUTLS)
1264 if ((ready
= gnutls_record_check_pending(http
->tls
)) > 0)
1266 # elif defined(HAVE_CDSASSL)
1267 if (!SSLGetBufferedReadSize(http
->tls
, &ready
) && ready
> 0)
1269 # endif /* HAVE_LIBSSL */
1271 #endif /* HAVE_SSL */
1278 * 'httpGets()' - Get a line of text from a HTTP connection.
1281 char * /* O - Line or NULL */
1282 httpGets(char *line
, /* I - Line to read into */
1283 int length
, /* I - Max length of buffer */
1284 http_t
*http
) /* I - HTTP connection */
1286 char *lineptr
, /* Pointer into line */
1287 *lineend
, /* End of line */
1288 *bufptr
, /* Pointer into input buffer */
1289 *bufend
; /* Pointer to end of buffer */
1290 int bytes
, /* Number of bytes read */
1291 eol
; /* End-of-line? */
1294 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", line
, length
, http
));
1296 if (!http
|| !line
|| length
<= 1)
1300 * Read a line from the buffer...
1305 lineend
= line
+ length
- 1;
1308 while (lineptr
< lineend
)
1311 * Pre-load the buffer as needed...
1320 while (http
->used
== 0)
1323 * No newline; see if there is more data to be read...
1326 while (!_httpWait(http
, http
->wait_value
, 1))
1328 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1331 DEBUG_puts("3httpGets: Timed out!");
1333 http
->error
= WSAETIMEDOUT
;
1335 http
->error
= ETIMEDOUT
;
1340 bytes
= http_read(http
, http
->buffer
+ http
->used
,
1341 HTTP_MAX_BUFFER
- http
->used
);
1343 DEBUG_printf(("4httpGets: read %d bytes.", bytes
));
1348 * Nope, can't get a line this time...
1352 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1354 if (WSAGetLastError() == WSAEINTR
)
1356 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1358 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1361 http
->error
= WSAGetLastError();
1363 else if (WSAGetLastError() != http
->error
)
1365 http
->error
= WSAGetLastError();
1370 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1374 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1376 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1378 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1381 http
->error
= errno
;
1383 else if (errno
!= http
->error
)
1385 http
->error
= errno
;
1392 else if (bytes
== 0)
1394 http
->error
= EPIPE
;
1400 * Yup, update the amount used...
1403 http
->used
+= bytes
;
1407 * Now copy as much of the current line as possible...
1410 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1411 lineptr
< lineend
&& bufptr
< bufend
;)
1413 if (*bufptr
== 0x0a)
1419 else if (*bufptr
== 0x0d)
1422 *lineptr
++ = *bufptr
++;
1425 http
->used
-= (int)(bufptr
- http
->buffer
);
1427 memmove(http
->buffer
, bufptr
, http
->used
);
1435 http
->activity
= time(NULL
);
1439 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1445 DEBUG_puts("3httpGets: No new line available!");
1452 * 'httpGetState()' - Get the current state of the HTTP request.
1455 http_state_t
/* O - HTTP state */
1456 httpGetState(http_t
*http
) /* I - HTTP connection */
1458 return (http
? http
->state
: HTTP_STATE_ERROR
);
1463 * 'httpGetStatus()' - Get the status of the last HTTP request.
1465 * @since CUPS 1.2/OS X 10.5@
1468 http_status_t
/* O - HTTP status */
1469 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1471 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1476 * 'httpGetSubField()' - Get a sub-field value.
1481 char * /* O - Value or NULL */
1482 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1483 http_field_t field
, /* I - Field index */
1484 const char *name
, /* I - Name of sub-field */
1485 char *value
) /* O - Value string */
1487 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1492 * 'httpGetSubField2()' - Get a sub-field value.
1494 * @since CUPS 1.2/OS X 10.5@
1497 char * /* O - Value or NULL */
1498 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1499 http_field_t field
, /* I - Field index */
1500 const char *name
, /* I - Name of sub-field */
1501 char *value
, /* O - Value string */
1502 int valuelen
) /* I - Size of value buffer */
1504 const char *fptr
; /* Pointer into field */
1505 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1506 *ptr
, /* Pointer into string buffer */
1507 *end
; /* End of value buffer */
1509 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, "
1510 "valuelen=%d)", http
, field
, name
, value
, valuelen
));
1512 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1513 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1516 end
= value
+ valuelen
- 1;
1518 for (fptr
= http
->fields
[field
]; *fptr
;)
1521 * Skip leading whitespace...
1524 while (_cups_isspace(*fptr
))
1534 * Get the sub-field name...
1538 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1539 ptr
< (temp
+ sizeof(temp
) - 1);
1544 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1547 * Skip trailing chars up to the '='...
1550 while (_cups_isspace(*fptr
))
1560 * Skip = and leading whitespace...
1565 while (_cups_isspace(*fptr
))
1571 * Read quoted string...
1574 for (ptr
= value
, fptr
++;
1575 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1580 while (*fptr
&& *fptr
!= '\"')
1589 * Read unquoted string...
1593 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1598 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1602 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1605 * See if this is the one...
1608 if (!strcmp(name
, temp
))
1610 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1617 DEBUG_puts("3httpGetSubField2: Returning NULL");
1624 * 'httpGetVersion()' - Get the HTTP version at the other end.
1627 http_version_t
/* O - Version number */
1628 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1630 return (http
? http
->version
: HTTP_VERSION_1_0
);
1635 * 'httpHead()' - Send a HEAD request to the server.
1638 int /* O - Status of call (0 = success) */
1639 httpHead(http_t
*http
, /* I - HTTP connection */
1640 const char *uri
) /* I - URI for head */
1642 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http
, uri
));
1643 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1648 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1649 * default HTTP proxy (if any).
1653 httpInitialize(void)
1655 static int initialized
= 0; /* Have we been called before? */
1657 WSADATA winsockdata
; /* WinSock data */
1660 int i
; /* Looping var */
1661 unsigned char data
[1024]; /* Seed data */
1662 #endif /* HAVE_LIBSSL */
1668 _cupsGlobalUnlock();
1673 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1675 #elif !defined(SO_NOSIGPIPE)
1677 * Ignore SIGPIPE signals...
1681 sigset(SIGPIPE
, SIG_IGN
);
1683 # elif defined(HAVE_SIGACTION)
1684 struct sigaction action
; /* POSIX sigaction data */
1687 memset(&action
, 0, sizeof(action
));
1688 action
.sa_handler
= SIG_IGN
;
1689 sigaction(SIGPIPE
, &action
, NULL
);
1692 signal(SIGPIPE
, SIG_IGN
);
1693 # endif /* !SO_NOSIGPIPE */
1698 * Initialize GNU TLS...
1701 gnutls_global_init();
1703 #elif defined(HAVE_LIBSSL)
1705 * Initialize OpenSSL...
1708 SSL_load_error_strings();
1712 * Using the current time is a dubious random seed, but on some systems
1713 * it is the best we can do (on others, this seed isn't even used...)
1716 CUPS_SRAND(time(NULL
));
1718 for (i
= 0; i
< sizeof(data
); i
++)
1719 data
[i
] = CUPS_RAND();
1721 RAND_seed(data
, sizeof(data
));
1722 #endif /* HAVE_GNUTLS */
1725 _cupsGlobalUnlock();
1730 * 'httpOptions()' - Send an OPTIONS request to the server.
1733 int /* O - Status of call (0 = success) */
1734 httpOptions(http_t
*http
, /* I - HTTP connection */
1735 const char *uri
) /* I - URI for options */
1737 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1742 * 'httpPeek()' - Peek at data from a HTTP connection.
1744 * This function copies available data from the given HTTP connection, reading
1745 * a buffer as needed. The data is still available for reading using
1746 * @link httpRead@ or @link httpRead2@.
1748 * For non-blocking connections the usual timeouts apply.
1750 * @since CUPS 1.7/OS X 10.9@
1753 ssize_t
/* O - Number of bytes copied */
1754 httpPeek(http_t
*http
, /* I - HTTP connection */
1755 char *buffer
, /* I - Buffer for data */
1756 size_t length
) /* I - Maximum number of bytes */
1758 ssize_t bytes
; /* Bytes read */
1759 char len
[32]; /* Length string */
1762 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
1763 http
, buffer
, CUPS_LLCAST length
));
1765 if (http
== NULL
|| buffer
== NULL
)
1768 http
->activity
= time(NULL
);
1774 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1775 http
->data_remaining
<= 0)
1777 DEBUG_puts("2httpPeek: Getting chunk length...");
1779 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1781 DEBUG_puts("1httpPeek: Could not get length!");
1787 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1788 if (!httpGets(len
, sizeof(len
), http
))
1790 DEBUG_puts("1httpPeek: Could not get chunk length.");
1795 http
->data_remaining
= strtoll(len
, NULL
, 16);
1797 if (http
->data_remaining
< 0)
1799 DEBUG_puts("1httpPeek: Negative chunk length!");
1804 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1805 CUPS_LLCAST http
->data_remaining
));
1807 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1810 * A zero-length chunk ends a transfer; unless we are reading POST
1816 http_content_coding_finish(http
);
1817 #endif /* HAVE_LIBZ */
1819 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1820 httpGets(len
, sizeof(len
), http
);
1822 if (http
->state
== HTTP_STATE_POST_RECV
)
1825 http
->state
= HTTP_STATE_STATUS
;
1827 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1828 http_state_string(http
->state
)));
1831 * Prevent future reads for this request...
1834 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1838 else if (length
> (size_t)http
->data_remaining
)
1839 length
= (size_t)http
->data_remaining
;
1842 if (http
->used
== 0 &&
1843 (http
->coding
== _HTTP_CODING_IDENTITY
|| http
->stream
.avail_in
== 0))
1845 if (http
->used
== 0)
1846 #endif /* HAVE_LIBZ */
1849 * Buffer small reads for better performance...
1852 ssize_t buflen
; /* Length of read for buffer */
1854 if (!http
->blocking
)
1856 while (!httpWait(http
, http
->wait_value
))
1858 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1865 if (http
->data_remaining
> sizeof(http
->buffer
))
1866 buflen
= sizeof(http
->buffer
);
1868 buflen
= http
->data_remaining
;
1870 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1871 bytes
= http_read(http
, http
->buffer
, buflen
);
1873 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1874 CUPS_LLCAST bytes
));
1878 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1888 # ifdef HAVE_INFLATECOPY
1889 int zerr
; /* Decompressor error */
1890 z_stream stream
; /* Copy of decompressor stream */
1892 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1894 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1895 /* Number of bytes to copy */
1897 if (http
->stream
.avail_in
> 0 &&
1898 http
->stream
.next_in
> http
->dbuffer
)
1899 memmove(http
->dbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1901 http
->stream
.next_in
= http
->dbuffer
;
1903 if (buflen
> http
->data_remaining
)
1904 buflen
= http
->data_remaining
;
1906 if (buflen
> http
->used
)
1907 buflen
= http
->used
;
1909 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1910 "decompression buffer.", (int)buflen
));
1912 memcpy(http
->dbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1913 http
->stream
.avail_in
+= buflen
;
1914 http
->used
-= buflen
;
1915 http
->data_remaining
-= buflen
;
1918 memmove(http
->buffer
, http
->buffer
+ buflen
, http
->used
);
1921 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1922 (int)http
->stream
.avail_in
));
1924 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1926 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1927 http
->error
= ENOMEM
;
1931 stream
.next_out
= (Bytef
*)buffer
;
1932 stream
.avail_out
= length
;
1934 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1935 inflateEnd(&stream
);
1939 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1941 http_debug_hex("2httpPeek", (char *)http
->dbuffer
,
1942 http
->stream
.avail_in
);
1949 bytes
= length
- http
->stream
.avail_out
;
1952 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1953 "work with compressed streams.");
1955 # endif /* HAVE_INFLATECOPY */
1958 #endif /* HAVE_LIBZ */
1961 if (length
> (size_t)http
->used
)
1962 length
= (size_t)http
->used
;
1964 bytes
= (ssize_t
)length
;
1966 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1969 memcpy(buffer
, http
->buffer
, length
);
1977 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1980 http
->error
= WSAGetLastError();
1982 if (errno
== EINTR
|| errno
== EAGAIN
)
1985 http
->error
= errno
;
1988 else if (bytes
== 0)
1990 http
->error
= EPIPE
;
1997 /* For OS X 10.8 and earlier */
1998 ssize_t
_httpPeek(http_t
*http
, char *buffer
, size_t length
)
1999 { return (httpPeek(http
, buffer
, length
)); }
2003 * 'httpPost()' - Send a POST request to the server.
2006 int /* O - Status of call (0 = success) */
2007 httpPost(http_t
*http
, /* I - HTTP connection */
2008 const char *uri
) /* I - URI for post */
2010 return (http_send(http
, HTTP_STATE_POST
, uri
));
2015 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
2020 int /* O - Number of bytes written */
2021 httpPrintf(http_t
*http
, /* I - HTTP connection */
2022 const char *format
, /* I - printf-style format string */
2023 ...) /* I - Additional args as needed */
2025 int bytes
; /* Number of bytes to write */
2026 char buf
[16384]; /* Buffer for formatted string */
2027 va_list ap
; /* Variable argument pointer */
2030 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", http
, format
));
2032 va_start(ap
, format
);
2033 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
2036 DEBUG_printf(("3httpPrintf: (%d bytes) %s", bytes
, buf
));
2038 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
2039 return (httpWrite2(http
, buf
, bytes
));
2044 DEBUG_puts("4httpPrintf: flushing existing data...");
2046 if (httpFlushWrite(http
) < 0)
2050 return (http_write(http
, buf
, bytes
));
2056 * 'httpPut()' - Send a PUT request to the server.
2059 int /* O - Status of call (0 = success) */
2060 httpPut(http_t
*http
, /* I - HTTP connection */
2061 const char *uri
) /* I - URI to put */
2063 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http
, uri
));
2064 return (http_send(http
, HTTP_STATE_PUT
, uri
));
2069 * 'httpRead()' - Read data from a HTTP connection.
2071 * This function is deprecated. Use the httpRead2() function which can
2072 * read more than 2GB of data.
2077 int /* O - Number of bytes read */
2078 httpRead(http_t
*http
, /* I - HTTP connection */
2079 char *buffer
, /* I - Buffer for data */
2080 int length
) /* I - Maximum number of bytes */
2082 return ((int)httpRead2(http
, buffer
, length
));
2087 * 'httpRead2()' - Read data from a HTTP connection.
2089 * @since CUPS 1.2/OS X 10.5@
2092 ssize_t
/* O - Number of bytes read */
2093 httpRead2(http_t
*http
, /* I - HTTP connection */
2094 char *buffer
, /* I - Buffer for data */
2095 size_t length
) /* I - Maximum number of bytes */
2097 ssize_t bytes
; /* Bytes read */
2101 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
2102 ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT
,
2103 http
, buffer
, CUPS_LLCAST length
,
2105 http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
2107 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
2108 ") data_encoding=%d data_remaining=" CUPS_LLFMT
,
2109 http
, buffer
, CUPS_LLCAST length
,
2110 http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
2111 #endif /* HAVE_LIBZ */
2113 if (http
== NULL
|| buffer
== NULL
)
2116 http
->activity
= time(NULL
);
2127 if (http
->stream
.avail_in
> 0)
2129 int zerr
; /* Decompressor error */
2131 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
2132 (int)http
->stream
.avail_in
, (int)length
));
2134 http
->stream
.next_out
= (Bytef
*)buffer
;
2135 http
->stream
.avail_out
= length
;
2137 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
2139 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
2141 http_debug_hex("2httpRead2", (char *)http
->dbuffer
,
2142 http
->stream
.avail_in
);
2149 bytes
= length
- http
->stream
.avail_out
;
2151 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
2152 http
->stream
.avail_in
, http
->stream
.avail_out
,
2160 ssize_t buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
2161 /* Additional bytes for buffer */
2165 if (http
->stream
.avail_in
> 0 &&
2166 http
->stream
.next_in
> http
->dbuffer
)
2167 memmove(http
->dbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
2169 http
->stream
.next_in
= http
->dbuffer
;
2171 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
2172 "decompression buffer.", (int)buflen
));
2174 if (http
->data_remaining
> 0)
2176 if (buflen
> http
->data_remaining
)
2177 buflen
= http
->data_remaining
;
2179 bytes
= http_read_buffered(http
,
2180 (char *)http
->dbuffer
+
2181 http
->stream
.avail_in
, buflen
);
2183 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2184 bytes
= http_read_chunk(http
,
2185 (char *)http
->dbuffer
+
2186 http
->stream
.avail_in
, buflen
);
2192 else if (bytes
== 0)
2195 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2196 "decompression buffer.", CUPS_LLCAST bytes
));
2198 http
->data_remaining
-= bytes
;
2199 http
->stream
.avail_in
+= bytes
;
2201 if (http
->data_remaining
<= 0 &&
2202 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2205 * Read the trailing blank line now...
2208 char len
[32]; /* Length string */
2210 httpGets(len
, sizeof(len
), http
);
2222 #endif /* HAVE_LIBZ */
2223 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2225 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2227 http
->data_remaining
-= bytes
;
2229 if (http
->data_remaining
<= 0)
2232 * Read the trailing blank line now...
2235 char len
[32]; /* Length string */
2237 httpGets(len
, sizeof(len
), http
);
2241 else if (http
->data_remaining
<= 0)
2244 * No more data to read...
2251 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2254 if (length
> (size_t)http
->data_remaining
)
2255 length
= (size_t)http
->data_remaining
;
2257 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2259 http
->data_remaining
-= bytes
;
2261 if (http
->data_remaining
<= 0 &&
2262 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2265 * Read the trailing blank line now...
2268 char len
[32]; /* Length string */
2270 httpGets(len
, sizeof(len
), http
);
2277 (http
->coding
== _HTTP_CODING_IDENTITY
|| http
->stream
.avail_in
== 0) &&
2278 #endif /* HAVE_LIBZ */
2279 ((http
->data_remaining
<= 0 &&
2280 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2281 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2285 http_content_coding_finish(http
);
2286 #endif /* HAVE_LIBZ */
2288 if (http
->state
== HTTP_STATE_POST_RECV
)
2290 else if (http
->state
== HTTP_STATE_GET_SEND
||
2291 http
->state
== HTTP_STATE_POST_SEND
)
2292 http
->state
= HTTP_STATE_WAITING
;
2294 http
->state
= HTTP_STATE_STATUS
;
2296 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2297 http_state_string(http
->state
)));
2304 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
2306 * '_httpReadCDSA()' - Read function for the CDSA library.
2309 OSStatus
/* O - -1 on error, 0 on success */
2311 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2312 void *data
, /* I - Data buffer */
2313 size_t *dataLength
) /* IO - Number of bytes */
2315 OSStatus result
; /* Return value */
2316 ssize_t bytes
; /* Number of bytes read */
2317 http_t
*http
; /* HTTP connection */
2320 http
= (http_t
*)connection
;
2322 if (!http
->blocking
)
2325 * Make sure we have data before we read...
2328 while (!_httpWait(http
, http
->wait_value
, 0))
2330 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
2333 http
->error
= ETIMEDOUT
;
2340 bytes
= recv(http
->fd
, data
, *dataLength
, 0);
2342 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
2344 if (bytes
== *dataLength
)
2350 *dataLength
= bytes
;
2351 result
= errSSLWouldBlock
;
2358 result
= errSSLClosedGraceful
;
2359 else if (errno
== EAGAIN
)
2360 result
= errSSLWouldBlock
;
2362 result
= errSSLClosedAbort
;
2367 #endif /* HAVE_SSL && HAVE_CDSASSL */
2370 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
2372 * '_httpReadGNUTLS()' - Read function for the GNU TLS library.
2375 ssize_t
/* O - Number of bytes read or -1 on error */
2377 gnutls_transport_ptr ptr
, /* I - HTTP connection */
2378 void *data
, /* I - Buffer */
2379 size_t length
) /* I - Number of bytes to read */
2381 http_t
*http
; /* HTTP connection */
2382 ssize_t bytes
; /* Bytes read */
2385 DEBUG_printf(("6_httpReadGNUTLS(ptr=%p, data=%p, length=%d)", ptr
, data
, (int)length
));
2387 http
= (http_t
*)ptr
;
2389 if (!http
->blocking
)
2392 * Make sure we have data before we read...
2395 while (!_httpWait(http
, http
->wait_value
, 0))
2397 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
2400 http
->error
= ETIMEDOUT
;
2405 bytes
= recv(http
->fd
, data
, length
, 0);
2406 DEBUG_printf(("6_httpReadGNUTLS: bytes=%d", (int)bytes
));
2409 #endif /* HAVE_SSL && HAVE_GNUTLS */
2413 * 'httpReadRequest()' - Read a HTTP request from a connection.
2415 * @since CUPS 1.7/OS X 10.9@
2418 http_state_t
/* O - New state of connection */
2419 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2420 char *uri
, /* I - URI buffer */
2421 size_t urilen
) /* I - Size of URI buffer */
2423 char line
[4096], /* HTTP request line */
2424 *req_method
, /* HTTP request method */
2425 *req_uri
, /* HTTP request URI */
2426 *req_version
; /* HTTP request version number string */
2430 * Range check input...
2433 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")",
2434 http
, uri
, CUPS_LLCAST urilen
));
2439 if (!http
|| !uri
|| urilen
< 1)
2441 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2442 return (HTTP_STATE_ERROR
);
2444 else if (http
->state
!= HTTP_STATE_WAITING
)
2446 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2447 http_state_string(http
->state
)));
2448 return (HTTP_STATE_ERROR
);
2455 httpClearFields(http
);
2457 http
->activity
= time(NULL
);
2458 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2459 http
->data_remaining
= 0;
2460 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2461 http
->status
= HTTP_STATUS_OK
;
2462 http
->version
= HTTP_VERSION_1_1
;
2465 * Read a line from the socket...
2468 if (!httpGets(line
, sizeof(line
), http
))
2470 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2471 return (HTTP_STATE_ERROR
);
2476 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2477 return (HTTP_STATE_WAITING
);
2480 DEBUG_printf(("1httpReadRequest: %s", line
));
2489 while (*req_uri
&& !isspace(*req_uri
& 255))
2494 DEBUG_puts("1httpReadRequest: No request URI.");
2495 return (HTTP_STATE_ERROR
);
2500 while (*req_uri
&& isspace(*req_uri
& 255))
2503 req_version
= req_uri
;
2505 while (*req_version
&& !isspace(*req_version
& 255))
2510 DEBUG_puts("1httpReadRequest: No request protocol version.");
2511 return (HTTP_STATE_ERROR
);
2514 *req_version
++ = '\0';
2516 while (*req_version
&& isspace(*req_version
& 255))
2523 if (!strcmp(req_method
, "OPTIONS"))
2524 http
->state
= HTTP_STATE_OPTIONS
;
2525 else if (!strcmp(req_method
, "GET"))
2526 http
->state
= HTTP_STATE_GET
;
2527 else if (!strcmp(req_method
, "HEAD"))
2528 http
->state
= HTTP_STATE_HEAD
;
2529 else if (!strcmp(req_method
, "POST"))
2530 http
->state
= HTTP_STATE_POST
;
2531 else if (!strcmp(req_method
, "PUT"))
2532 http
->state
= HTTP_STATE_PUT
;
2533 else if (!strcmp(req_method
, "DELETE"))
2534 http
->state
= HTTP_STATE_DELETE
;
2535 else if (!strcmp(req_method
, "TRACE"))
2536 http
->state
= HTTP_STATE_TRACE
;
2537 else if (!strcmp(req_method
, "CONNECT"))
2538 http
->state
= HTTP_STATE_CONNECT
;
2541 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2542 return (HTTP_STATE_UNKNOWN_METHOD
);
2545 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2546 http_state_string(http
->state
)));
2548 if (!strcmp(req_version
, "HTTP/1.0"))
2550 http
->version
= HTTP_VERSION_1_0
;
2551 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2553 else if (!strcmp(req_version
, "HTTP/1.1"))
2555 http
->version
= HTTP_VERSION_1_1
;
2556 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2560 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2561 return (HTTP_STATE_UNKNOWN_VERSION
);
2564 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2565 strlcpy(uri
, req_uri
, urilen
);
2567 return (http
->state
);
2572 * 'httpReconnect()' - Reconnect to a HTTP server.
2574 * This function is deprecated. Please use the @link httpReconnect2@ function
2580 int /* O - 0 on success, non-zero on failure */
2581 httpReconnect(http_t
*http
) /* I - HTTP connection */
2583 DEBUG_printf(("httpReconnect(http=%p)", http
));
2585 return (httpReconnect2(http
, 30000, NULL
));
2590 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2594 int /* O - 0 on success, non-zero on failure */
2595 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2596 int msec
, /* I - Timeout in milliseconds */
2597 int *cancel
) /* I - Pointer to "cancel" variable */
2599 http_addrlist_t
*addr
; /* Connected address */
2601 http_addrlist_t
*current
; /* Current address */
2602 char temp
[256]; /* Temporary address string */
2606 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", http
, msec
,
2611 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2618 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2619 http_shutdown_ssl(http
);
2621 #endif /* HAVE_SSL */
2624 * Close any previously open socket...
2629 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2632 closesocket(http
->fd
);
2641 * Reset all state (except fields, which may be reused)...
2644 http
->state
= HTTP_STATE_WAITING
;
2645 http
->version
= HTTP_VERSION_1_1
;
2646 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2647 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2648 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2649 http
->_data_remaining
= 0;
2651 http
->data_remaining
= 0;
2652 http
->hostaddr
= NULL
;
2656 * Connect to the server...
2660 for (current
= http
->addrlist
; current
; current
= current
->next
)
2661 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2662 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2663 httpAddrPort(&(current
->addr
))));
2666 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
,
2670 * Unable to connect...
2674 http
->error
= WSAGetLastError();
2676 http
->error
= errno
;
2678 http
->status
= HTTP_STATUS_ERROR
;
2680 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2681 strerror(http
->error
)));
2686 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2688 if (http
->timeout_value
> 0)
2689 http_set_timeout(http
->fd
, http
->timeout_value
);
2691 http
->hostaddr
= &(addr
->addr
);
2695 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2698 * Always do encryption via SSL.
2701 if (http_setup_ssl(http
) != 0)
2704 closesocket(http
->fd
);
2712 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2713 return (http_upgrade(http
));
2714 #endif /* HAVE_SSL */
2716 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2717 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2718 httpAddrPort(http
->hostaddr
)));
2725 * 'httpSetAuthString()' - Set the current authorization string.
2727 * This function just stores a copy of the current authorization string in
2728 * the HTTP connection object. You must still call httpSetField() to set
2729 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
2730 * httpHead(), httpOptions(), httpPost, or httpPut().
2732 * @since CUPS 1.3/OS X 10.5@
2736 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2737 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2738 const char *data
) /* I - Auth data (NULL for none) */
2741 * Range check input...
2747 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2748 free(http
->authstring
);
2750 http
->authstring
= http
->_authstring
;
2755 * Set the current authorization string...
2758 int len
= (int)strlen(scheme
) + (data
? (int)strlen(data
) + 1 : 0) + 1;
2761 if (len
> (int)sizeof(http
->_authstring
))
2763 if ((temp
= malloc(len
)) == NULL
)
2764 len
= sizeof(http
->_authstring
);
2766 http
->authstring
= temp
;
2770 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2772 strlcpy(http
->authstring
, scheme
, len
);
2777 * Clear the current authorization string...
2780 http
->_authstring
[0] = '\0';
2786 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2789 * @since CUPS 1.5/OS X 10.7@
2792 int /* O - Status of call (0 = success) */
2793 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2794 cups_array_t
*credentials
) /* I - Array of credentials */
2796 if (!http
|| cupsArrayCount(credentials
) < 1)
2799 _httpFreeCredentials(http
->tls_credentials
);
2801 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2803 return (http
->tls_credentials
? 0 : -1);
2808 * 'httpSetCookie()' - Set the cookie value(s).
2810 * @since CUPS 1.1.19/OS X 10.3@
2814 httpSetCookie(http_t
*http
, /* I - Connection */
2815 const char *cookie
) /* I - Cookie string */
2824 http
->cookie
= strdup(cookie
);
2826 http
->cookie
= NULL
;
2831 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2833 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2834 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2836 * @since CUPS 1.7/OS X 10.9@
2840 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2841 http_field_t field
, /* I - Field index */
2842 const char *value
)/* I - Value */
2844 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")",
2845 http
, field
, http_fields
[field
], value
));
2852 case HTTP_FIELD_ACCEPT_ENCODING
:
2853 if (http
->default_accept_encoding
)
2854 _cupsStrFree(http
->default_accept_encoding
);
2856 http
->default_accept_encoding
= value
? _cupsStrAlloc(value
) : NULL
;
2859 case HTTP_FIELD_SERVER
:
2860 if (http
->default_server
)
2861 _cupsStrFree(http
->default_server
);
2863 http
->default_server
= value
? _cupsStrAlloc(value
) : NULL
;
2866 case HTTP_FIELD_USER_AGENT
:
2867 if (http
->default_user_agent
)
2868 _cupsStrFree(http
->default_user_agent
);
2870 http
->default_user_agent
= value
? _cupsStrAlloc(value
) : NULL
;
2874 DEBUG_puts("1httpSetDefaultField: Ignored.");
2881 * 'httpSetExpect()' - Set the Expect: header in a request.
2883 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2886 * @since CUPS 1.2/OS X 10.5@
2890 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2891 http_status_t expect
) /* I - HTTP status to expect
2892 (@code HTTP_STATUS_CONTINUE@) */
2894 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", http
, expect
));
2897 http
->expect
= expect
;
2902 * 'httpSetField()' - Set the value of an HTTP header.
2906 httpSetField(http_t
*http
, /* I - HTTP connection */
2907 http_field_t field
, /* I - Field index */
2908 const char *value
) /* I - Value */
2910 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", http
,
2911 field
, http_fields
[field
], value
));
2914 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
2915 field
>= HTTP_FIELD_MAX
||
2921 case HTTP_FIELD_ACCEPT_ENCODING
:
2922 if (http
->accept_encoding
)
2923 _cupsStrFree(http
->accept_encoding
);
2925 http
->accept_encoding
= _cupsStrAlloc(value
);
2928 case HTTP_FIELD_ALLOW
:
2930 _cupsStrFree(http
->allow
);
2932 http
->allow
= _cupsStrAlloc(value
);
2935 case HTTP_FIELD_SERVER
:
2937 _cupsStrFree(http
->server
);
2939 http
->server
= _cupsStrAlloc(value
);
2943 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
2947 if (field
== HTTP_FIELD_AUTHORIZATION
)
2950 * Special case for Authorization: as its contents can be
2951 * longer than HTTP_MAX_VALUE
2954 if (http
->field_authorization
)
2955 free(http
->field_authorization
);
2957 http
->field_authorization
= strdup(value
);
2959 else if (field
== HTTP_FIELD_HOST
)
2962 * Special-case for Host: as we don't want a trailing "." on the hostname and
2963 * need to bracket IPv6 numeric addresses.
2966 char *ptr
= strchr(value
, ':');
2968 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
2971 * Bracket IPv6 numeric addresses...
2973 * This is slightly inefficient (basically copying twice), but is an edge
2974 * case and not worth optimizing...
2977 snprintf(http
->fields
[HTTP_FIELD_HOST
],
2978 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
2983 * Check for a trailing dot on the hostname...
2986 ptr
= http
->fields
[HTTP_FIELD_HOST
];
2990 ptr
+= strlen(ptr
) - 1;
2998 else if (field
== HTTP_FIELD_CONTENT_ENCODING
&&
2999 http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3001 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
3002 http_content_coding_start(http
, value
);
3004 #endif /* HAVE_LIBZ */
3009 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
3016 http_t
*http
, /* I - HTTP connection */
3017 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
3020 http
->keep_alive
= keep_alive
;
3025 * 'httpSetLength()' - Set the content-length and content-encoding.
3027 * @since CUPS 1.2/OS X 10.5@
3031 httpSetLength(http_t
*http
, /* I - HTTP connection */
3032 size_t length
) /* I - Length (0 for chunked) */
3034 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", http
,
3035 CUPS_LLCAST length
));
3042 strlcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked",
3044 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
3048 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
3049 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
3050 CUPS_LLFMT
, CUPS_LLCAST length
);
3056 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
3058 * The optional timeout callback receives both the HTTP connection and a user
3059 * data pointer and must return 1 to continue or 0 to error (time) out.
3061 * @since CUPS 1.5/OS X 10.7@
3066 http_t
*http
, /* I - HTTP connection */
3067 double timeout
, /* I - Number of seconds for timeout,
3068 must be greater than 0 */
3069 http_timeout_cb_t cb
, /* I - Callback function or NULL */
3070 void *user_data
) /* I - User data pointer */
3072 if (!http
|| timeout
<= 0.0)
3075 http
->timeout_cb
= cb
;
3076 http
->timeout_data
= user_data
;
3077 http
->timeout_value
= timeout
;
3080 http_set_timeout(http
->fd
, timeout
);
3082 http_set_wait(http
);
3087 * 'httpTrace()' - Send an TRACE request to the server.
3090 int /* O - Status of call (0 = success) */
3091 httpTrace(http_t
*http
, /* I - HTTP connection */
3092 const char *uri
) /* I - URI for trace */
3094 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
3099 * '_httpUpdate()' - Update the current HTTP status for incoming data.
3101 * Note: Unlike httpUpdate(), this function does not flush pending write data
3102 * and only retrieves a single status line from the HTTP connection.
3105 int /* O - 1 to continue, 0 to stop */
3106 _httpUpdate(http_t
*http
, /* I - HTTP connection */
3107 http_status_t
*status
) /* O - Current HTTP status */
3109 char line
[32768], /* Line from connection... */
3110 *value
; /* Pointer to value on line */
3111 http_field_t field
; /* Field index */
3112 int major
, minor
; /* HTTP version numbers */
3115 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http
, status
,
3116 http_state_string(http
->state
)));
3119 * Grab a single line from the connection...
3122 if (!httpGets(line
, sizeof(line
), http
))
3124 *status
= HTTP_STATUS_ERROR
;
3128 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
3130 if (line
[0] == '\0')
3133 * Blank line means the start of the data section (if any). Return
3134 * the result code, too...
3136 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
3137 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
3141 if (http
->status
== HTTP_STATUS_CONTINUE
)
3143 *status
= http
->status
;
3147 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
3148 http
->digest_tries
= 0;
3151 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
3153 if (http_setup_ssl(http
) != 0)
3156 closesocket(http
->fd
);
3161 *status
= http
->status
= HTTP_STATUS_ERROR
;
3165 *status
= HTTP_STATUS_CONTINUE
;
3168 #endif /* HAVE_SSL */
3170 if (http_set_length(http
) < 0)
3172 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
3173 http
->error
= EINVAL
;
3174 http
->status
= *status
= HTTP_STATUS_ERROR
;
3178 switch (http
->state
)
3180 case HTTP_STATE_GET
:
3181 case HTTP_STATE_POST
:
3182 case HTTP_STATE_POST_RECV
:
3183 case HTTP_STATE_PUT
:
3186 DEBUG_printf(("1_httpUpdate: Set state to %s.",
3187 http_state_string(http
->state
)));
3189 case HTTP_STATE_POST_SEND
:
3190 case HTTP_STATE_HEAD
:
3194 http
->state
= HTTP_STATE_WAITING
;
3196 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
3201 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
3202 http_content_coding_start(http
,
3203 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3204 #endif /* HAVE_LIBZ */
3206 *status
= http
->status
;
3209 else if (!strncmp(line
, "HTTP/", 5))
3212 * Got the beginning of a response...
3215 int intstatus
; /* Status value as an integer */
3217 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
3219 *status
= http
->status
= HTTP_STATUS_ERROR
;
3223 httpClearFields(http
);
3225 http
->version
= (http_version_t
)(major
* 100 + minor
);
3226 *status
= http
->status
= (http_status_t
)intstatus
;
3228 else if ((value
= strchr(line
, ':')) != NULL
)
3235 while (_cups_isspace(*value
))
3238 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
3241 * Be tolerants of servers that send unknown attribute fields...
3244 if (!_cups_strcasecmp(line
, "expect"))
3247 * "Expect: 100-continue" or similar...
3250 http
->expect
= (http_status_t
)atoi(value
);
3252 else if (!_cups_strcasecmp(line
, "cookie"))
3255 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
3258 httpSetCookie(http
, value
);
3260 else if ((field
= http_field(line
)) != HTTP_FIELD_UNKNOWN
)
3261 httpSetField(http
, field
, value
);
3264 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
3269 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
3270 http
->error
= EINVAL
;
3271 http
->status
= *status
= HTTP_STATUS_ERROR
;
3280 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3283 http_status_t
/* O - HTTP status */
3284 httpUpdate(http_t
*http
) /* I - HTTP connection */
3286 http_status_t status
; /* Request status */
3289 DEBUG_printf(("httpUpdate(http=%p), state=%s", http
,
3290 http_state_string(http
->state
)));
3293 * Flush pending data, if any...
3298 DEBUG_puts("2httpUpdate: flushing buffer...");
3300 if (httpFlushWrite(http
) < 0)
3301 return (HTTP_STATUS_ERROR
);
3305 * If we haven't issued any commands, then there is nothing to "update"...
3308 if (http
->state
== HTTP_STATE_WAITING
)
3309 return (HTTP_STATUS_CONTINUE
);
3312 * Grab all of the lines we can from the connection...
3315 while (_httpUpdate(http
, &status
));
3318 * See if there was an error...
3321 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
3323 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
3324 return (http
->status
);
3329 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
3330 strerror(http
->error
)));
3331 http
->status
= HTTP_STATUS_ERROR
;
3332 return (HTTP_STATUS_ERROR
);
3336 * Return the current status...
3344 * '_httpWait()' - Wait for data available on a connection (no flush).
3347 int /* O - 1 if data is available, 0 otherwise */
3348 _httpWait(http_t
*http
, /* I - HTTP connection */
3349 int msec
, /* I - Milliseconds to wait */
3350 int usessl
) /* I - Use SSL context? */
3353 struct pollfd pfd
; /* Polled file descriptor */
3355 fd_set input_set
; /* select() input set */
3356 struct timeval timeout
; /* Timeout */
3357 #endif /* HAVE_POLL */
3358 int nfds
; /* Result from select()/poll() */
3361 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", http
, msec
, usessl
));
3365 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
3370 * Check the SSL/TLS buffers for data first...
3374 if (http
->tls
&& usessl
)
3377 if (SSL_pending(http
->tls
))
3379 DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data.");
3383 # elif defined(HAVE_GNUTLS)
3384 if (gnutls_record_check_pending(http
->tls
))
3386 DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data.");
3390 # elif defined(HAVE_CDSASSL)
3391 size_t bytes
; /* Bytes that are available */
3393 if (!SSLGetBufferedReadSize(http
->tls
, &bytes
) &&
3396 DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data.");
3399 # endif /* HAVE_LIBSSL */
3401 #endif /* HAVE_SSL */
3404 * Then try doing a select() or poll() to poll the socket...
3409 pfd
.events
= POLLIN
;
3413 nfds
= poll(&pfd
, 1, msec
);
3415 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3420 FD_ZERO(&input_set
);
3421 FD_SET(http
->fd
, &input_set
);
3423 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3427 timeout
.tv_sec
= msec
/ 1000;
3428 timeout
.tv_usec
= (msec
% 1000) * 1000;
3430 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3433 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3435 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3438 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3439 WSAGetLastError() == WSAEWOULDBLOCK
));
3441 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3443 #endif /* HAVE_POLL */
3445 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3453 * 'httpWait()' - Wait for data available on a connection.
3455 * @since CUPS 1.1.19/OS X 10.3@
3458 int /* O - 1 if data is available, 0 otherwise */
3459 httpWait(http_t
*http
, /* I - HTTP connection */
3460 int msec
) /* I - Milliseconds to wait */
3463 * First see if there is data in the buffer...
3466 DEBUG_printf(("2httpWait(http=%p, msec=%d)", http
, msec
));
3473 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3478 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3480 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3483 #endif /* HAVE_LIBZ */
3486 * Flush pending data, if any...
3491 DEBUG_puts("3httpWait: Flushing write buffer.");
3493 if (httpFlushWrite(http
) < 0)
3498 * If not, check the SSL/TLS buffers and do a select() on the connection...
3501 return (_httpWait(http
, msec
, 1));
3506 * 'httpWrite()' - Write data to a HTTP connection.
3508 * This function is deprecated. Use the httpWrite2() function which can
3509 * write more than 2GB of data.
3514 int /* O - Number of bytes written */
3515 httpWrite(http_t
*http
, /* I - HTTP connection */
3516 const char *buffer
, /* I - Buffer for data */
3517 int length
) /* I - Number of bytes to write */
3519 return ((int)httpWrite2(http
, buffer
, length
));
3524 * 'httpWrite2()' - Write data to a HTTP connection.
3526 * @since CUPS 1.2/OS X 10.5@
3529 ssize_t
/* O - Number of bytes written */
3530 httpWrite2(http_t
*http
, /* I - HTTP connection */
3531 const char *buffer
, /* I - Buffer for data */
3532 size_t length
) /* I - Number of bytes to write */
3534 ssize_t bytes
; /* Bytes written */
3537 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
3538 buffer
, CUPS_LLCAST length
));
3541 * Range check input...
3544 if (!http
|| !buffer
)
3546 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3551 * Mark activity on the connection...
3554 http
->activity
= time(NULL
);
3557 * Buffer small writes for better performance...
3563 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3567 http_content_coding_finish(http
);
3572 http
->stream
.next_in
= (Bytef
*)buffer
;
3573 http
->stream
.avail_in
= length
;
3574 http
->stream
.next_out
= (Bytef
*)http
->wbuffer
+ http
->wused
;
3575 http
->stream
.avail_out
= sizeof(http
->wbuffer
) - http
->wused
;
3577 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3579 http
->wused
= sizeof(http
->wbuffer
) - http
->stream
.avail_out
;
3581 if (http
->stream
.avail_out
== 0)
3583 if (httpFlushWrite(http
) < 0)
3585 DEBUG_puts("1httpWrite2: Unable to flush, returning -1.");
3589 http
->stream
.next_out
= (Bytef
*)http
->wbuffer
;
3590 http
->stream
.avail_out
= sizeof(http
->wbuffer
);
3594 http
->wused
= sizeof(http
->wbuffer
) - http
->stream
.avail_out
;
3599 #endif /* HAVE_LIBZ */
3602 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
3604 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3605 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3607 httpFlushWrite(http
);
3610 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
) &&
3611 length
< sizeof(http
->wbuffer
))
3614 * Write to buffer...
3617 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3618 CUPS_LLCAST length
));
3620 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3621 http
->wused
+= (int)length
;
3622 bytes
= (ssize_t
)length
;
3627 * Otherwise write the data directly...
3630 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3631 CUPS_LLCAST length
));
3633 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3634 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, (int)length
);
3636 bytes
= (ssize_t
)http_write(http
, buffer
, (int)length
);
3638 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3639 CUPS_LLCAST bytes
));
3642 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3643 http
->data_remaining
-= bytes
;
3649 * Handle end-of-request processing...
3652 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3653 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3656 * Finished with the transfer; unless we are sending POST or PUT
3662 http_content_coding_finish(http
);
3663 #endif /* HAVE_LIBZ */
3667 if (httpFlushWrite(http
) < 0)
3671 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3674 * Send a 0-length chunk at the end of the request...
3677 http_write(http
, "0\r\n\r\n", 5);
3680 * Reset the data state...
3683 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3684 http
->data_remaining
= 0;
3687 if (http
->state
== HTTP_STATE_POST_RECV
)
3689 else if (http
->state
== HTTP_STATE_POST_SEND
)
3690 http
->state
= HTTP_STATE_WAITING
;
3692 http
->state
= HTTP_STATE_STATUS
;
3694 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3695 http_state_string(http
->state
)));
3698 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3704 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
3706 * '_httpWriteCDSA()' - Write function for the CDSA library.
3709 OSStatus
/* O - -1 on error, 0 on success */
3711 SSLConnectionRef connection
, /* I - SSL/TLS connection */
3712 const void *data
, /* I - Data buffer */
3713 size_t *dataLength
) /* IO - Number of bytes */
3715 OSStatus result
; /* Return value */
3716 ssize_t bytes
; /* Number of bytes read */
3717 http_t
*http
; /* HTTP connection */
3720 http
= (http_t
*)connection
;
3724 bytes
= write(http
->fd
, data
, *dataLength
);
3726 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
3728 if (bytes
== *dataLength
)
3732 else if (bytes
>= 0)
3734 *dataLength
= bytes
;
3735 result
= errSSLWouldBlock
;
3741 if (errno
== EAGAIN
)
3742 result
= errSSLWouldBlock
;
3744 result
= errSSLClosedAbort
;
3749 #endif /* HAVE_SSL && HAVE_CDSASSL */
3752 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
3754 * '_httpWriteGNUTLS()' - Write function for the GNU TLS library.
3757 ssize_t
/* O - Number of bytes written or -1 on error */
3759 gnutls_transport_ptr ptr
, /* I - HTTP connection */
3760 const void *data
, /* I - Data buffer */
3761 size_t length
) /* I - Number of bytes to write */
3763 ssize_t bytes
; /* Bytes written */
3766 DEBUG_printf(("6_httpWriteGNUTLS(ptr=%p, data=%p, length=%d)", ptr
, data
,
3769 http_debug_hex("_httpWriteGNUTLS", data
, (int)length
);
3772 bytes
= send(((http_t
*)ptr
)->fd
, data
, length
, 0);
3773 DEBUG_printf(("_httpWriteGNUTLS: bytes=%d", (int)bytes
));
3777 #endif /* HAVE_SSL && HAVE_GNUTLS */
3781 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3783 * @since CUPS 1.7/OS X 10.9@
3786 int /* O - 0 on success, -1 on error */
3787 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3788 http_status_t status
) /* I - Status code */
3790 http_encoding_t old_encoding
; /* Old data_encoding value */
3791 off_t old_remaining
; /* Old data_remaining value */
3795 * Range check input...
3798 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", http
, status
));
3800 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3802 DEBUG_puts("1httpWriteResponse: Bad input.");
3807 * Set the various standard fields if they aren't already...
3810 if (!http
->fields
[HTTP_FIELD_DATE
][0])
3811 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3813 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3815 http
->keep_alive
= 0;
3816 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3819 if (http
->version
== HTTP_VERSION_1_1
)
3821 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3823 if (http
->keep_alive
)
3824 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3826 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3829 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
][0])
3830 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3834 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
3836 if (!http
->fields
[HTTP_FIELD_CONNECTION
][0])
3837 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3839 if (!http
->fields
[HTTP_FIELD_UPGRADE
][0])
3840 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3842 #endif /* HAVE_SSL */
3845 httpSetField(http
, HTTP_FIELD_SERVER
,
3846 http
->default_server
? http
->default_server
: CUPS_MINIMAL
);
3849 * Set the Accept-Encoding field if it isn't already...
3852 if (!http
->accept_encoding
)
3853 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
3854 http
->default_accept_encoding
? http
->default_accept_encoding
:
3856 "gzip, deflate, identity");
3859 #endif /* HAVE_LIBZ */
3862 * Send the response header...
3865 old_encoding
= http
->data_encoding
;
3866 old_remaining
= http
->data_remaining
;
3867 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3869 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100,
3870 http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3872 http
->status
= HTTP_STATUS_ERROR
;
3876 if (status
!= HTTP_STATUS_CONTINUE
)
3879 * 100 Continue doesn't have the rest of the response headers...
3882 int i
; /* Looping var */
3883 const char *value
; /* Field value */
3885 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3887 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3889 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3891 http
->status
= HTTP_STATUS_ERROR
;
3899 if (httpPrintf(http
, "Set-Cookie: %s path=/%s\r\n", http
->cookie
,
3900 http
->tls
? " secure" : "") < 1)
3902 http
->status
= HTTP_STATUS_ERROR
;
3908 if (httpWrite2(http
, "\r\n", 2) < 2)
3910 http
->status
= HTTP_STATUS_ERROR
;
3914 if (httpFlushWrite(http
) < 0)
3916 http
->status
= HTTP_STATUS_ERROR
;
3920 if (status
== HTTP_STATUS_CONTINUE
)
3923 * Restore the old data_encoding and data_length values...
3926 http
->data_encoding
= old_encoding
;
3927 http
->data_remaining
= old_remaining
;
3929 if (old_remaining
<= INT_MAX
)
3930 http
->_data_remaining
= (int)old_remaining
;
3932 http
->_data_remaining
= INT_MAX
;
3934 else if (http
->state
== HTTP_STATE_OPTIONS
||
3935 http
->state
== HTTP_STATE_HEAD
||
3936 http
->state
== HTTP_STATE_PUT
||
3937 http
->state
== HTTP_STATE_TRACE
||
3938 http
->state
== HTTP_STATE_CONNECT
||
3939 http
->state
== HTTP_STATE_STATUS
)
3941 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3942 "was %s.", http_state_string(http
->state
)));
3943 http
->state
= HTTP_STATE_WAITING
;
3948 * Force data_encoding and data_length to be set according to the response
3952 http_set_length(http
);
3956 * Then start any content encoding...
3959 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3960 http_content_coding_start(http
,
3961 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3962 #endif /* HAVE_LIBZ */
3969 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
3971 * 'http_bio_ctrl()' - Control the HTTP connection.
3974 static long /* O - Result/data */
3975 http_bio_ctrl(BIO
*h
, /* I - BIO data */
3976 int cmd
, /* I - Control command */
3977 long arg1
, /* I - First argument */
3978 void *arg2
) /* I - Second argument */
3985 case BIO_CTRL_RESET
:
3989 case BIO_C_SET_FILE_PTR
:
3994 case BIO_C_GET_FILE_PTR
:
3997 *((void **)arg2
) = h
->ptr
;
4004 case BIO_CTRL_FLUSH
:
4011 * 'http_bio_free()' - Free OpenSSL data.
4014 static int /* O - 1 on success, 0 on failure */
4015 http_bio_free(BIO
*h
) /* I - BIO data */
4031 * 'http_bio_new()' - Initialize an OpenSSL BIO structure.
4034 static int /* O - 1 on success, 0 on failure */
4035 http_bio_new(BIO
*h
) /* I - BIO data */
4050 * 'http_bio_puts()' - Send a string for OpenSSL.
4053 static int /* O - Bytes written */
4054 http_bio_puts(BIO
*h
, /* I - BIO data */
4055 const char *str
) /* I - String to write */
4058 return (send(((http_t
*)h
->ptr
)->fd
, str
, (int)strlen(str
), 0));
4060 return (send(((http_t
*)h
->ptr
)->fd
, str
, strlen(str
), 0));
4066 * 'http_bio_read()' - Read data for OpenSSL.
4069 static int /* O - Bytes read */
4070 http_bio_read(BIO
*h
, /* I - BIO data */
4071 char *buf
, /* I - Buffer */
4072 int size
) /* I - Number of bytes to read */
4074 http_t
*http
; /* HTTP connection */
4077 http
= (http_t
*)h
->ptr
;
4079 if (!http
->blocking
)
4082 * Make sure we have data before we read...
4085 while (!_httpWait(http
, http
->wait_value
, 0))
4087 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4091 http
->error
= WSAETIMEDOUT
;
4093 http
->error
= ETIMEDOUT
;
4100 return (recv(http
->fd
, buf
, size
, 0));
4105 * 'http_bio_write()' - Write data for OpenSSL.
4108 static int /* O - Bytes written */
4109 http_bio_write(BIO
*h
, /* I - BIO data */
4110 const char *buf
, /* I - Buffer to write */
4111 int num
) /* I - Number of bytes to write */
4113 return (send(((http_t
*)h
->ptr
)->fd
, buf
, num
, 0));
4115 #endif /* HAVE_SSL && HAVE_LIBSSL */
4120 * 'http_content_coding_finish()' - Finish doing any content encoding.
4124 http_content_coding_finish(
4125 http_t
*http
) /* I - HTTP connection */
4127 int zerr
; /* Compression status */
4130 switch (http
->coding
)
4132 case _HTTP_CODING_DEFLATE
:
4133 case _HTTP_CODING_GZIP
:
4136 http
->stream
.next_out
= (Bytef
*)http
->wbuffer
+ http
->wused
;
4137 http
->stream
.avail_out
= sizeof(http
->wbuffer
) - http
->wused
;
4139 zerr
= deflate(&(http
->stream
), Z_FINISH
);
4141 http
->wused
= sizeof(http
->wbuffer
) - http
->stream
.avail_out
;
4142 if (http
->wused
== sizeof(http
->wbuffer
))
4143 httpFlushWrite(http
);
4145 while (zerr
== Z_OK
);
4147 deflateEnd(&(http
->stream
));
4150 httpFlushWrite(http
);
4153 case _HTTP_CODING_INFLATE
:
4154 case _HTTP_CODING_GUNZIP
:
4155 inflateEnd(&(http
->stream
));
4156 free(http
->dbuffer
);
4157 http
->dbuffer
= NULL
;
4164 http
->coding
= _HTTP_CODING_IDENTITY
;
4169 * 'http_content_coding_start()' - Start doing content encoding.
4173 http_content_coding_start(
4174 http_t
*http
, /* I - HTTP connection */
4175 const char *value
) /* I - Value of Content-Encoding */
4177 int zerr
; /* Error/status */
4178 _http_coding_t coding
; /* Content coding value */
4181 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", http
,
4184 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
4186 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
4190 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
4192 if (http
->state
== HTTP_STATE_GET_SEND
||
4193 http
->state
== HTTP_STATE_POST_SEND
)
4194 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
4195 _HTTP_CODING_GUNZIP
;
4196 else if (http
->state
== HTTP_STATE_POST_RECV
||
4197 http
->state
== HTTP_STATE_PUT_RECV
)
4198 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
4199 _HTTP_CODING_GUNZIP
;
4202 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
4206 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
4208 if (http
->state
== HTTP_STATE_GET_SEND
||
4209 http
->state
== HTTP_STATE_POST_SEND
)
4210 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
4211 _HTTP_CODING_INFLATE
;
4212 else if (http
->state
== HTTP_STATE_POST_RECV
||
4213 http
->state
== HTTP_STATE_PUT_RECV
)
4214 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
4215 _HTTP_CODING_INFLATE
;
4218 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
4224 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
4228 memset(&(http
->stream
), 0, sizeof(http
->stream
));
4232 case _HTTP_CODING_DEFLATE
:
4233 case _HTTP_CODING_GZIP
:
4235 httpFlushWrite(http
);
4238 * Window size for compression is 11 bits - optimal based on PWG Raster
4239 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
4243 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
4245 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
4246 Z_DEFAULT_STRATEGY
)) < Z_OK
)
4248 http
->status
= HTTP_STATUS_ERROR
;
4249 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
4254 case _HTTP_CODING_INFLATE
:
4255 case _HTTP_CODING_GUNZIP
:
4256 if ((http
->dbuffer
= malloc(HTTP_MAX_BUFFER
)) == NULL
)
4258 http
->status
= HTTP_STATUS_ERROR
;
4259 http
->error
= errno
;
4264 * Window size for decompression is up to 15 bits (maximum supported).
4265 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
4268 if ((zerr
= inflateInit2(&(http
->stream
),
4269 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
4272 free(http
->dbuffer
);
4273 http
->dbuffer
= NULL
;
4274 http
->status
= HTTP_STATUS_ERROR
;
4275 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
4279 http
->stream
.avail_in
= 0;
4280 http
->stream
.next_in
= http
->dbuffer
;
4287 http
->coding
= coding
;
4289 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
4292 #endif /* HAVE_LIBZ */
4296 * 'http_create()' - Create an unconnected HTTP connection.
4299 static http_t
* /* O - HTTP connection */
4301 const char *host
, /* I - Hostname */
4302 int port
, /* I - Port number */
4303 http_addrlist_t
*addrlist
, /* I - Address list or NULL */
4304 int family
, /* I - Address family or AF_UNSPEC */
4305 http_encryption_t encryption
, /* I - Encryption to use */
4306 int blocking
, /* I - 1 for blocking mode */
4307 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
4309 http_t
*http
; /* New HTTP connection */
4310 char service
[255]; /* Service name */
4311 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
4314 DEBUG_printf(("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, "
4315 "encryption=%d, blocking=%d, mode=%d)", host
, port
, addrlist
,
4316 family
, encryption
, blocking
, mode
));
4318 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
4324 * Lookup the host...
4329 myaddrlist
= httpAddrCopyList(addrlist
);
4333 snprintf(service
, sizeof(service
), "%d", port
);
4335 myaddrlist
= httpAddrGetList(host
, family
, service
);
4342 * Allocate memory for the structure...
4345 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
4347 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
4348 httpAddrFreeList(addrlist
);
4353 * Initialize the HTTP data...
4357 http
->activity
= time(NULL
);
4358 http
->addrlist
= myaddrlist
;
4359 http
->blocking
= blocking
;
4362 http
->gssctx
= GSS_C_NO_CONTEXT
;
4363 http
->gssname
= GSS_C_NO_NAME
;
4364 #endif /* HAVE_GSSAPI */
4365 http
->status
= HTTP_STATUS_CONTINUE
;
4366 http
->version
= HTTP_VERSION_1_1
;
4369 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
4371 if (port
== 443) /* Always use encryption for https */
4372 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
4374 http
->encryption
= encryption
;
4376 http_set_wait(http
);
4379 * Return the new structure...
4385 /* For OS X 10.8 and earlier */
4386 http_t
*_httpCreate(const char *host
, int port
, http_addrlist_t
*addrlist
,
4387 http_encryption_t encryption
, int family
)
4388 { return (http_create(host
, port
, addrlist
, family
, encryption
, 1,
4389 _HTTP_MODE_CLIENT
)); }
4394 * 'http_debug_hex()' - Do a hex dump of a buffer.
4398 http_debug_hex(const char *prefix
, /* I - Prefix for line */
4399 const char *buffer
, /* I - Buffer to dump */
4400 int bytes
) /* I - Bytes to dump */
4402 int i
, j
, /* Looping vars */
4403 ch
; /* Current character */
4404 char line
[255], /* Line buffer */
4405 *start
, /* Start of line after prefix */
4406 *ptr
; /* Pointer into line */
4409 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
4412 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
4414 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
4415 start
= line
+ strlen(line
);
4417 for (i
= 0; i
< bytes
; i
+= 16)
4419 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
4420 sprintf(ptr
, "%02X", buffer
[i
+ j
] & 255);
4424 memcpy(ptr
, " ", 3);
4429 memcpy(ptr
, " ", 3);
4432 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
4434 ch
= buffer
[i
+ j
] & 255;
4436 if (ch
< ' ' || ch
>= 127)
4450 * 'http_field()' - Return the field index for a field name.
4453 static http_field_t
/* O - Field index */
4454 http_field(const char *name
) /* I - String name */
4456 int i
; /* Looping var */
4459 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4460 if (_cups_strcasecmp(name
, http_fields
[i
]) == 0)
4461 return ((http_field_t
)i
);
4463 return (HTTP_FIELD_UNKNOWN
);
4468 * 'http_read()' - Read a buffer from a HTTP connection.
4470 * This function does the low-level read from the socket, retrying and timing
4474 static ssize_t
/* O - Number of bytes read or -1 on error */
4475 http_read(http_t
*http
, /* I - HTTP connection */
4476 char *buffer
, /* I - Buffer */
4477 size_t length
) /* I - Maximum bytes to read */
4479 ssize_t bytes
; /* Bytes read */
4482 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
4483 buffer
, CUPS_LLCAST length
));
4485 if (!http
->blocking
)
4487 while (!httpWait(http
, http
->wait_value
))
4489 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4492 DEBUG_puts("2http_read: Timeout.");
4497 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4503 bytes
= http_read_ssl(http
, buffer
, length
);
4505 #endif /* HAVE_SSL */
4506 bytes
= recv(http
->fd
, buffer
, length
, 0);
4511 if (WSAGetLastError() != WSAEINTR
)
4513 http
->error
= WSAGetLastError();
4516 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4518 if (!http
->timeout_cb
||
4519 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4521 http
->error
= WSAEWOULDBLOCK
;
4526 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4528 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4530 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4532 http
->error
= errno
;
4535 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4537 http
->error
= errno
;
4541 else if (errno
!= EINTR
)
4543 http
->error
= errno
;
4551 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4552 CUPS_LLCAST bytes
));
4555 http_debug_hex("http_read", buffer
, (int)bytes
);
4561 if (WSAGetLastError() == WSAEINTR
)
4564 http
->error
= WSAGetLastError();
4566 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4569 http
->error
= errno
;
4572 else if (bytes
== 0)
4574 http
->error
= EPIPE
;
4583 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4585 * This function reads data from the HTTP buffer or from the socket, as needed.
4588 static ssize_t
/* O - Number of bytes read or -1 on error */
4589 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4590 char *buffer
, /* I - Buffer */
4591 size_t length
) /* I - Maximum bytes to read */
4593 ssize_t bytes
; /* Bytes read */
4596 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
4598 http
, buffer
, CUPS_LLCAST length
, http
->used
));
4602 if (length
> (size_t)http
->used
)
4603 bytes
= (size_t)http
->used
;
4607 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4610 memcpy(buffer
, http
->buffer
, bytes
);
4611 http
->used
-= (int)bytes
;
4614 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
4617 bytes
= http_read(http
, buffer
, length
);
4624 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4626 * This function reads and validates the chunk length, then does a buffered read
4627 * returning the number of bytes placed in the buffer.
4630 static ssize_t
/* O - Number of bytes read or -1 on error */
4631 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4632 char *buffer
, /* I - Buffer */
4633 size_t length
) /* I - Maximum bytes to read */
4635 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
4636 http
, buffer
, CUPS_LLCAST length
));
4638 if (http
->data_remaining
<= 0)
4640 char len
[32]; /* Length string */
4642 if (!httpGets(len
, sizeof(len
), http
))
4644 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4650 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4651 if (!httpGets(len
, sizeof(len
), http
))
4653 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4658 http
->data_remaining
= strtoll(len
, NULL
, 16);
4660 if (http
->data_remaining
< 0)
4662 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4663 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4667 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4668 len
, CUPS_LLCAST http
->data_remaining
));
4670 if (http
->data_remaining
== 0)
4673 * 0-length chunk, grab trailing blank line...
4676 httpGets(len
, sizeof(len
), http
);
4680 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4681 CUPS_LLCAST http
->data_remaining
));
4683 if (http
->data_remaining
<= 0)
4685 else if (length
> (size_t)http
->data_remaining
)
4686 length
= (size_t)http
->data_remaining
;
4688 return (http_read_buffered(http
, buffer
, length
));
4694 * 'http_read_ssl()' - Read from a SSL/TLS connection.
4697 static int /* O - Bytes read */
4698 http_read_ssl(http_t
*http
, /* I - HTTP connection */
4699 char *buf
, /* I - Buffer to store data */
4700 int len
) /* I - Length of buffer */
4702 # if defined(HAVE_LIBSSL)
4703 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
4705 # elif defined(HAVE_GNUTLS)
4706 ssize_t result
; /* Return value */
4709 result
= gnutls_record_recv(http
->tls
, buf
, len
);
4711 if (result
< 0 && !errno
)
4714 * Convert GNU TLS error to errno value...
4719 case GNUTLS_E_INTERRUPTED
:
4723 case GNUTLS_E_AGAIN
:
4735 return ((int)result
);
4737 # elif defined(HAVE_CDSASSL)
4738 int result
; /* Return value */
4739 OSStatus error
; /* Error info */
4740 size_t processed
; /* Number of bytes processed */
4743 error
= SSLRead(http
->tls
, buf
, len
, &processed
);
4744 DEBUG_printf(("6http_read_ssl: error=%d, processed=%d", (int)error
,
4749 result
= (int)processed
;
4752 case errSSLWouldBlock
:
4754 result
= (int)processed
;
4762 case errSSLClosedGraceful
:
4765 result
= (int)processed
;
4776 # elif defined(HAVE_SSPISSL)
4777 return _sspiRead((_sspi_struct_t
*) http
->tls
, buf
, len
);
4778 # endif /* HAVE_LIBSSL */
4780 #endif /* HAVE_SSL */
4784 * 'http_send()' - Send a request with all fields and the trailing blank line.
4787 static int /* O - 0 on success, non-zero on error */
4788 http_send(http_t
*http
, /* I - HTTP connection */
4789 http_state_t request
, /* I - Request code */
4790 const char *uri
) /* I - URI */
4792 int i
; /* Looping var */
4793 char buf
[1024]; /* Encoded URI buffer */
4794 const char *value
; /* Field value */
4795 static const char * const codes
[] = /* Request code strings */
4815 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")",
4816 http
, codes
[request
], uri
));
4818 if (http
== NULL
|| uri
== NULL
)
4822 * Set the User-Agent field if it isn't already...
4825 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
4827 if (http
->default_user_agent
)
4828 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_user_agent
);
4830 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4834 * Set the Accept-Encoding field if it isn't already...
4837 if (!http
->accept_encoding
&& http
->default_accept_encoding
)
4838 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
4839 http
->default_accept_encoding
);
4842 * Encode the URI as needed...
4845 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4848 * See if we had an error the last time around; if so, reconnect...
4851 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4852 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4854 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4855 http
->fd
, http
->status
, http
->tls_upgrade
));
4857 if (httpReconnect2(http
, 30000, NULL
))
4862 * Flush any written data that is pending...
4867 if (httpFlushWrite(http
) < 0)
4868 if (httpReconnect2(http
, 30000, NULL
))
4873 * Send the request header...
4876 http
->state
= request
;
4877 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4879 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4882 http
->status
= HTTP_STATUS_CONTINUE
;
4885 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4887 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4888 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4890 #endif /* HAVE_SSL */
4892 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4894 http
->status
= HTTP_STATUS_ERROR
;
4898 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4899 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4901 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4903 if (i
== HTTP_FIELD_HOST
)
4905 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4906 httpAddrPort(http
->hostaddr
)) < 1)
4908 http
->status
= HTTP_STATUS_ERROR
;
4912 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4914 http
->status
= HTTP_STATUS_ERROR
;
4920 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4922 http
->status
= HTTP_STATUS_ERROR
;
4926 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4927 http
->mode
, http
->state
));
4929 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4930 (http
->state
== HTTP_STATE_POST_RECV
||
4931 http
->state
== HTTP_STATE_PUT_RECV
))
4932 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4934 http
->status
= HTTP_STATUS_ERROR
;
4938 if (httpPrintf(http
, "\r\n") < 1)
4940 http
->status
= HTTP_STATUS_ERROR
;
4944 if (httpFlushWrite(http
) < 0)
4947 http_set_length(http
);
4948 httpClearFields(http
);
4951 * The Kerberos and AuthRef authentication strings can only be used once...
4954 if (http
->field_authorization
&& http
->authstring
&&
4955 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4956 !strncmp(http
->authstring
, "AuthRef", 7)))
4958 http
->_authstring
[0] = '\0';
4960 if (http
->authstring
!= http
->_authstring
)
4961 free(http
->authstring
);
4963 http
->authstring
= http
->_authstring
;
4971 # if defined(HAVE_CDSASSL)
4973 * 'http_set_credentials()' - Set the SSL/TLS credentials.
4976 static int /* O - Status of connection */
4977 http_set_credentials(http_t
*http
) /* I - HTTP connection */
4979 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
4980 OSStatus error
= 0; /* Error code */
4981 http_tls_credentials_t credentials
= NULL
;
4982 /* TLS credentials */
4985 DEBUG_printf(("7http_set_credentials(%p)", http
));
4988 * Prefer connection specific credentials...
4991 if ((credentials
= http
->tls_credentials
) == NULL
)
4992 credentials
= cg
->tls_credentials
;
4996 error
= SSLSetCertificate(http
->tls
, credentials
);
4997 DEBUG_printf(("4http_set_credentials: SSLSetCertificate, error=%d",
5001 DEBUG_puts("4http_set_credentials: No credentials to set.");
5005 # endif /* HAVE_CDSASSL */
5006 #endif /* HAVE_SSL */
5010 * 'http_set_length()' - Set the data_encoding and data_remaining values.
5013 static off_t
/* O - Remainder or -1 on error */
5014 http_set_length(http_t
*http
) /* I - Connection */
5016 off_t remaining
; /* Remainder */
5019 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", http
, http
->mode
,
5020 http_state_string(http
->state
)));
5022 if ((remaining
= httpGetLength2(http
)) >= 0)
5024 if (http
->mode
== _HTTP_MODE_SERVER
&&
5025 http
->state
!= HTTP_STATE_GET_SEND
&&
5026 http
->state
!= HTTP_STATE_PUT
&&
5027 http
->state
!= HTTP_STATE_POST
&&
5028 http
->state
!= HTTP_STATE_POST_SEND
)
5030 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
5034 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
],
5037 DEBUG_puts("1http_set_length: Setting data_encoding to "
5038 "HTTP_ENCODING_CHUNKED.");
5039 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
5043 DEBUG_puts("1http_set_length: Setting data_encoding to "
5044 "HTTP_ENCODING_LENGTH.");
5045 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
5048 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
5049 CUPS_LLCAST remaining
));
5050 http
->data_remaining
= remaining
;
5052 if (remaining
<= INT_MAX
)
5053 http
->_data_remaining
= remaining
;
5055 http
->_data_remaining
= INT_MAX
;
5062 * 'http_set_timeout()' - Set the socket timeout values.
5066 http_set_timeout(int fd
, /* I - File descriptor */
5067 double timeout
) /* I - Timeout in seconds */
5070 DWORD tv
= (DWORD
)(timeout
* 1000);
5071 /* Timeout in milliseconds */
5073 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
5074 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
5077 struct timeval tv
; /* Timeout in secs and usecs */
5079 tv
.tv_sec
= (int)timeout
;
5080 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
5082 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
5083 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
5089 * 'http_set_wait()' - Set the default wait value for reads.
5093 http_set_wait(http_t
*http
) /* I - HTTP connection */
5097 http
->wait_value
= (int)(http
->timeout_value
* 1000);
5099 if (http
->wait_value
<= 0)
5100 http
->wait_value
= 60000;
5103 http
->wait_value
= 10000;
5109 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
5112 static int /* O - 0 on success, -1 on failure */
5113 http_setup_ssl(http_t
*http
) /* I - HTTP connection */
5115 char hostname
[256], /* Hostname */
5116 *hostptr
; /* Pointer into hostname */
5119 SSL_CTX
*context
; /* Context for encryption */
5120 BIO
*bio
; /* BIO data */
5121 const char *message
= NULL
;/* Error message */
5122 # elif defined(HAVE_GNUTLS)
5123 int status
; /* Status of handshake */
5124 gnutls_certificate_client_credentials
*credentials
;
5125 /* TLS credentials */
5126 # elif defined(HAVE_CDSASSL)
5127 _cups_globals_t
*cg
= _cupsGlobals();
5128 /* Pointer to library globals */
5129 OSStatus error
; /* Error code */
5130 const char *message
= NULL
;/* Error message */
5131 cups_array_t
*credentials
; /* Credentials array */
5132 cups_array_t
*names
; /* CUPS distinguished names */
5133 CFArrayRef dn_array
; /* CF distinguished names array */
5134 CFIndex count
; /* Number of credentials */
5135 CFDataRef data
; /* Certificate data */
5136 int i
; /* Looping var */
5137 http_credential_t
*credential
; /* Credential data */
5138 # elif defined(HAVE_SSPISSL)
5139 TCHAR username
[256]; /* Username returned from GetUserName() */
5140 TCHAR commonName
[256];/* Common name for certificate */
5141 DWORD dwSize
; /* 32 bit size */
5142 # endif /* HAVE_LIBSSL */
5145 DEBUG_printf(("7http_setup_ssl(http=%p)", http
));
5148 * Get the hostname to use for SSL...
5151 if (httpAddrLocalhost(http
->hostaddr
))
5153 strlcpy(hostname
, "localhost", sizeof(hostname
));
5158 * Otherwise make sure the hostname we have does not end in a trailing dot.
5161 strlcpy(hostname
, http
->hostname
, sizeof(hostname
));
5162 if ((hostptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&&
5168 context
= SSL_CTX_new(SSLv23_client_method());
5170 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
5172 bio
= BIO_new(_httpBIOMethods());
5173 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)http
);
5175 http
->tls
= SSL_new(context
);
5176 SSL_set_bio(http
->tls
, bio
, bio
);
5178 # ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
5179 SSL_set_tlsext_host_name(http
->tls
, hostname
);
5180 # endif /* HAVE_SSL_SET_TLSEXT_HOST_NAME */
5182 if (SSL_connect(http
->tls
) != 1)
5184 unsigned long error
; /* Error code */
5186 while ((error
= ERR_get_error()) != 0)
5188 message
= ERR_error_string(error
, NULL
);
5189 DEBUG_printf(("8http_setup_ssl: %s", message
));
5192 SSL_CTX_free(context
);
5193 SSL_free(http
->tls
);
5197 http
->error
= WSAGetLastError();
5199 http
->error
= errno
;
5201 http
->status
= HTTP_STATUS_ERROR
;
5204 message
= _("Unable to establish a secure connection to host.");
5206 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, message
, 1);
5211 # elif defined(HAVE_GNUTLS)
5212 credentials
= (gnutls_certificate_client_credentials
*)
5213 malloc(sizeof(gnutls_certificate_client_credentials
));
5214 if (credentials
== NULL
)
5216 DEBUG_printf(("8http_setup_ssl: Unable to allocate credentials: %s",
5218 http
->error
= errno
;
5219 http
->status
= HTTP_STATUS_ERROR
;
5220 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
5225 gnutls_certificate_allocate_credentials(credentials
);
5227 gnutls_init(&http
->tls
, GNUTLS_CLIENT
);
5228 gnutls_set_default_priority(http
->tls
);
5229 gnutls_server_name_set(http
->tls
, GNUTLS_NAME_DNS
, hostname
,
5231 gnutls_credentials_set(http
->tls
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
5232 gnutls_transport_set_ptr(http
->tls
, (gnutls_transport_ptr
)http
);
5233 gnutls_transport_set_pull_function(http
->tls
, _httpReadGNUTLS
);
5234 gnutls_transport_set_push_function(http
->tls
, _httpWriteGNUTLS
);
5236 while ((status
= gnutls_handshake(http
->tls
)) != GNUTLS_E_SUCCESS
)
5238 DEBUG_printf(("8http_setup_ssl: gnutls_handshake returned %d (%s)",
5239 status
, gnutls_strerror(status
)));
5241 if (gnutls_error_is_fatal(status
))
5244 http
->status
= HTTP_STATUS_ERROR
;
5246 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, gnutls_strerror(status
), 0);
5248 gnutls_deinit(http
->tls
);
5249 gnutls_certificate_free_credentials(*credentials
);
5257 http
->tls_credentials
= credentials
;
5259 # elif defined(HAVE_CDSASSL)
5260 if ((http
->tls
= SSLCreateContext(kCFAllocatorDefault
, kSSLClientSide
,
5261 kSSLStreamType
)) == NULL
)
5263 DEBUG_puts("4http_setup_ssl: SSLCreateContext failed.");
5264 http
->error
= errno
= ENOMEM
;
5265 http
->status
= HTTP_STATUS_ERROR
;
5266 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
5271 error
= SSLSetConnection(http
->tls
, http
);
5272 DEBUG_printf(("4http_setup_ssl: SSLSetConnection, error=%d", (int)error
));
5276 error
= SSLSetIOFuncs(http
->tls
, _httpReadCDSA
, _httpWriteCDSA
);
5277 DEBUG_printf(("4http_setup_ssl: SSLSetIOFuncs, error=%d", (int)error
));
5282 error
= SSLSetSessionOption(http
->tls
, kSSLSessionOptionBreakOnServerAuth
,
5284 DEBUG_printf(("4http_setup_ssl: SSLSetSessionOption, error=%d",
5290 if (cg
->client_cert_cb
)
5292 error
= SSLSetSessionOption(http
->tls
,
5293 kSSLSessionOptionBreakOnCertRequested
, true);
5294 DEBUG_printf(("4http_setup_ssl: kSSLSessionOptionBreakOnCertRequested, "
5295 "error=%d", (int)error
));
5299 error
= http_set_credentials(http
);
5300 DEBUG_printf(("4http_setup_ssl: http_set_credentials, error=%d",
5306 * Let the server know which hostname/domain we are trying to connect to
5307 * in case it wants to serve up a certificate with a matching common name.
5312 error
= SSLSetPeerDomainName(http
->tls
, hostname
, strlen(hostname
));
5314 DEBUG_printf(("4http_setup_ssl: SSLSetPeerDomainName, error=%d",
5320 int done
= 0; /* Are we done yet? */
5322 while (!error
&& !done
)
5324 error
= SSLHandshake(http
->tls
);
5326 DEBUG_printf(("4http_setup_ssl: SSLHandshake returned %d.", (int)error
));
5334 case errSSLWouldBlock
:
5335 error
= noErr
; /* Force a retry */
5336 usleep(1000); /* in 1 millisecond */
5339 case errSSLServerAuthCompleted
:
5341 if (cg
->server_cert_cb
)
5343 error
= httpCopyCredentials(http
, &credentials
);
5346 error
= (cg
->server_cert_cb
)(http
, http
->tls
, credentials
,
5347 cg
->server_cert_data
);
5348 httpFreeCredentials(credentials
);
5351 DEBUG_printf(("4http_setup_ssl: Server certificate callback "
5352 "returned %d.", (int)error
));
5356 case errSSLClientCertRequested
:
5359 if (cg
->client_cert_cb
)
5362 if (!(error
= SSLCopyDistinguishedNames(http
->tls
, &dn_array
)) &&
5365 if ((names
= cupsArrayNew(NULL
, NULL
)) != NULL
)
5367 for (i
= 0, count
= CFArrayGetCount(dn_array
); i
< count
; i
++)
5369 data
= (CFDataRef
)CFArrayGetValueAtIndex(dn_array
, i
);
5371 if ((credential
= malloc(sizeof(*credential
))) != NULL
)
5373 credential
->datalen
= CFDataGetLength(data
);
5374 if ((credential
->data
= malloc(credential
->datalen
)))
5376 memcpy((void *)credential
->data
, CFDataGetBytePtr(data
),
5377 credential
->datalen
);
5378 cupsArrayAdd(names
, credential
);
5386 CFRelease(dn_array
);
5391 error
= (cg
->client_cert_cb
)(http
, http
->tls
, names
,
5392 cg
->client_cert_data
);
5394 DEBUG_printf(("4http_setup_ssl: Client certificate callback "
5395 "returned %d.", (int)error
));
5398 httpFreeCredentials(names
);
5402 case errSSLUnknownRootCert
:
5403 message
= _("Unable to establish a secure connection to host "
5404 "(untrusted certificate).");
5407 case errSSLNoRootCert
:
5408 message
= _("Unable to establish a secure connection to host "
5409 "(self-signed certificate).");
5412 case errSSLCertExpired
:
5413 message
= _("Unable to establish a secure connection to host "
5414 "(expired certificate).");
5417 case errSSLCertNotYetValid
:
5418 message
= _("Unable to establish a secure connection to host "
5419 "(certificate not yet valid).");
5422 case errSSLHostNameMismatch
:
5423 message
= _("Unable to establish a secure connection to host "
5424 "(host name mismatch).");
5427 case errSSLXCertChainInvalid
:
5428 message
= _("Unable to establish a secure connection to host "
5429 "(certificate chain invalid).");
5432 case errSSLConnectionRefused
:
5433 message
= _("Unable to establish a secure connection to host "
5434 "(peer dropped connection before responding).");
5445 http
->error
= error
;
5446 http
->status
= HTTP_STATUS_ERROR
;
5447 errno
= ECONNREFUSED
;
5449 CFRelease(http
->tls
);
5453 * If an error string wasn't set by the callbacks use a generic one...
5457 #ifdef HAVE_CSSMERRORSTRING
5458 message
= cssmErrorString(error
);
5460 message
= _("Unable to establish a secure connection to host.");
5461 #endif /* HAVE_CSSMERRORSTRING */
5463 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, message
, 1);
5468 # elif defined(HAVE_SSPISSL)
5469 http
->tls
= _sspiAlloc();
5473 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
5477 http
->tls
->sock
= http
->fd
;
5478 dwSize
= sizeof(username
) / sizeof(TCHAR
);
5479 GetUserName(username
, &dwSize
);
5480 _sntprintf_s(commonName
, sizeof(commonName
) / sizeof(TCHAR
),
5481 sizeof(commonName
) / sizeof(TCHAR
), TEXT("CN=%s"), username
);
5483 if (!_sspiGetCredentials(http
->tls_credentials
, L
"ClientContainer",
5486 _sspiFree(http
->tls_credentials
);
5487 http
->tls_credentials
= NULL
;
5490 http
->status
= HTTP_STATUS_ERROR
;
5492 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
,
5493 _("Unable to establish a secure connection to host."), 1);
5498 _sspiSetAllowsAnyRoot(http
->tls_credentials
, TRUE
);
5499 _sspiSetAllowsExpiredCerts(http
->tls_credentials
, TRUE
);
5501 if (!_sspiConnect(http
->tls_credentials
, hostname
))
5503 _sspiFree(http
->tls_credentials
);
5504 http
->tls_credentials
= NULL
;
5507 http
->status
= HTTP_STATUS_ERROR
;
5509 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
,
5510 _("Unable to establish a secure connection to host."), 1);
5514 # endif /* HAVE_CDSASSL */
5521 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
5525 http_shutdown_ssl(http_t
*http
) /* I - HTTP connection */
5528 SSL_CTX
*context
; /* Context for encryption */
5530 context
= SSL_get_SSL_CTX(http
->tls
);
5532 SSL_shutdown(http
->tls
);
5533 SSL_CTX_free(context
);
5534 SSL_free(http
->tls
);
5536 # elif defined(HAVE_GNUTLS)
5537 gnutls_certificate_client_credentials
*credentials
;
5538 /* TLS credentials */
5540 credentials
= (gnutls_certificate_client_credentials
*)(http
->tls_credentials
);
5542 gnutls_bye(http
->tls
, GNUTLS_SHUT_RDWR
);
5543 gnutls_deinit(http
->tls
);
5544 gnutls_certificate_free_credentials(*credentials
);
5547 # elif defined(HAVE_CDSASSL)
5548 while (SSLClose(http
->tls
) == errSSLWouldBlock
)
5551 CFRelease(http
->tls
);
5553 if (http
->tls_credentials
)
5554 CFRelease(http
->tls_credentials
);
5556 # elif defined(HAVE_SSPISSL)
5557 _sspiFree(http
->tls_credentials
);
5558 # endif /* HAVE_LIBSSL */
5561 http
->tls_credentials
= NULL
;
5563 #endif /* HAVE_SSL */
5568 * 'http_state_string()' - Return the string associated with a given HTTP state.
5571 static const char * /* O - State string */
5572 http_state_string(http_state_t state
) /* I - HTTP state */
5574 static char buffer
[255]; /* Unknown value buffer */
5575 static const char * const states
[] = /* State strings */
5578 "HTTP_STATE_WAITING",
5579 "HTTP_STATE_OPTIONS",
5581 "HTTP_STATE_GET_SEND",
5584 "HTTP_STATE_POST_RECV",
5585 "HTTP_STATE_POST_SEND",
5587 "HTTP_STATE_PUT_RECV",
5588 "HTTP_STATE_DELETE",
5590 "HTTP_STATE_CONNECT",
5591 "HTTP_STATE_STATUS",
5592 "HTTP_STATE_UNKNOWN_METHOD",
5593 "HTTP_STATE_UNKNOWN_VERSION"
5596 if (state
>= HTTP_STATE_ERROR
&& state
<= HTTP_STATE_UNKNOWN_VERSION
)
5597 return (states
[state
- HTTP_STATE_ERROR
]);
5599 snprintf(buffer
, sizeof(buffer
), "??? %d ???", (int)state
);
5607 * 'http_upgrade()' - Force upgrade to TLS encryption.
5610 static int /* O - Status of connection */
5611 http_upgrade(http_t
*http
) /* I - HTTP connection */
5613 int ret
; /* Return value */
5614 http_t myhttp
; /* Local copy of HTTP data */
5617 DEBUG_printf(("7http_upgrade(%p)", http
));
5620 * Flush the connection to make sure any previous "Upgrade" message
5627 * Copy the HTTP data to a local variable so we can do the OPTIONS
5628 * request without interfering with the existing request data...
5631 memcpy(&myhttp
, http
, sizeof(myhttp
));
5634 * Send an OPTIONS request to the server, requiring SSL or TLS
5635 * encryption on the link...
5638 http
->tls_upgrade
= 1;
5639 http
->field_authorization
= NULL
; /* Don't free the auth string */
5641 httpClearFields(http
);
5642 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
5643 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
5645 if ((ret
= httpOptions(http
, "*")) == 0)
5648 * Wait for the secure connection...
5651 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
5655 * Restore the HTTP request data...
5658 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
5659 http
->data_encoding
= myhttp
.data_encoding
;
5660 http
->data_remaining
= myhttp
.data_remaining
;
5661 http
->_data_remaining
= myhttp
._data_remaining
;
5662 http
->expect
= myhttp
.expect
;
5663 http
->field_authorization
= myhttp
.field_authorization
;
5664 http
->digest_tries
= myhttp
.digest_tries
;
5665 http
->tls_upgrade
= 0;
5668 * See if we actually went secure...
5674 * Server does not support HTTP upgrade...
5677 DEBUG_puts("8http_upgrade: Server does not support HTTP upgrade!");
5680 closesocket(http
->fd
);
5692 #endif /* HAVE_SSL */
5696 * 'http_write()' - Write a buffer to a HTTP connection.
5699 static ssize_t
/* O - Number of bytes written */
5700 http_write(http_t
*http
, /* I - HTTP connection */
5701 const char *buffer
, /* I - Buffer for data */
5702 size_t length
) /* I - Number of bytes to write */
5704 ssize_t tbytes
, /* Total bytes sent */
5705 bytes
; /* Bytes sent */
5708 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
5709 buffer
, CUPS_LLCAST length
));
5715 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
5717 if (http
->timeout_cb
)
5720 struct pollfd pfd
; /* Polled file descriptor */
5722 fd_set output_set
; /* Output ready for write? */
5723 struct timeval timeout
; /* Timeout value */
5724 #endif /* HAVE_POLL */
5725 int nfds
; /* Result from select()/poll() */
5731 pfd
.events
= POLLOUT
;
5733 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
5734 (errno
== EINTR
|| errno
== EAGAIN
))
5740 FD_ZERO(&output_set
);
5741 FD_SET(http
->fd
, &output_set
);
5743 timeout
.tv_sec
= http
->wait_value
/ 1000;
5744 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
5746 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
5749 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
5750 WSAGetLastError() == WSAEWOULDBLOCK
));
5752 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
5754 #endif /* HAVE_POLL */
5758 http
->error
= errno
;
5761 else if (nfds
== 0 && !(*http
->timeout_cb
)(http
, http
->timeout_data
))
5764 http
->error
= WSAEWOULDBLOCK
;
5766 http
->error
= EWOULDBLOCK
;
5776 bytes
= http_write_ssl(http
, buffer
, length
);
5778 #endif /* HAVE_SSL */
5779 bytes
= send(http
->fd
, buffer
, length
, 0);
5781 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
5782 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
5787 if (WSAGetLastError() == WSAEINTR
)
5789 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
5791 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
5794 http
->error
= WSAGetLastError();
5796 else if (WSAGetLastError() != http
->error
&&
5797 WSAGetLastError() != WSAECONNRESET
)
5799 http
->error
= WSAGetLastError();
5806 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
5808 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
5810 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
5813 http
->error
= errno
;
5815 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
5817 http
->error
= errno
;
5822 DEBUG_printf(("3http_write: error writing data (%s).",
5823 strerror(http
->error
)));
5834 http_debug_hex("http_write", buffer
- tbytes
, tbytes
);
5837 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
5844 * 'http_write_chunk()' - Write a chunked buffer.
5847 static ssize_t
/* O - Number bytes written */
5848 http_write_chunk(http_t
*http
, /* I - HTTP connection */
5849 const char *buffer
, /* I - Buffer to write */
5850 size_t length
) /* I - Length of buffer */
5852 char header
[16]; /* Chunk header */
5853 ssize_t bytes
; /* Bytes written */
5856 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
5857 http
, buffer
, CUPS_LLCAST length
));
5860 * Write the chunk header, data, and trailer.
5863 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
5864 if (http_write(http
, header
, strlen(header
)) < 0)
5866 DEBUG_puts("8http_write_chunk: http_write of length failed.");
5870 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
5872 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
5876 if (http_write(http
, "\r\n", 2) < 0)
5878 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");
5888 * 'http_write_ssl()' - Write to a SSL/TLS connection.
5891 static int /* O - Bytes written */
5892 http_write_ssl(http_t
*http
, /* I - HTTP connection */
5893 const char *buf
, /* I - Buffer holding data */
5894 int len
) /* I - Length of buffer */
5896 ssize_t result
; /* Return value */
5899 DEBUG_printf(("2http_write_ssl(http=%p, buf=%p, len=%d)", http
, buf
, len
));
5901 # if defined(HAVE_LIBSSL)
5902 result
= SSL_write((SSL
*)(http
->tls
), buf
, len
);
5904 # elif defined(HAVE_GNUTLS)
5905 result
= gnutls_record_send(http
->tls
, buf
, len
);
5907 if (result
< 0 && !errno
)
5910 * Convert GNU TLS error to errno value...
5915 case GNUTLS_E_INTERRUPTED
:
5919 case GNUTLS_E_AGAIN
:
5931 # elif defined(HAVE_CDSASSL)
5932 OSStatus error
; /* Error info */
5933 size_t processed
; /* Number of bytes processed */
5936 error
= SSLWrite(http
->tls
, buf
, len
, &processed
);
5941 result
= (int)processed
;
5944 case errSSLWouldBlock
:
5946 result
= (int)processed
;
5954 case errSSLClosedGraceful
:
5957 result
= (int)processed
;
5965 # elif defined(HAVE_SSPISSL)
5966 return _sspiWrite((_sspi_struct_t
*)http
->tls
, (void *)buf
, len
);
5967 # endif /* HAVE_LIBSSL */
5969 DEBUG_printf(("3http_write_ssl: Returning %d.", (int)result
));
5971 return ((int)result
);
5973 #endif /* HAVE_SSL */