2 * "$Id: http.c 7850 2008-08-20 00:07:25Z mike $"
4 * HTTP routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * _httpBIOMethods() - Get the OpenSSL BIO methods for HTTP connections.
23 * httpBlocking() - Set blocking/non-blocking behavior on a connection.
24 * httpCheck() - Check to see if there is a pending response from the
26 * httpClearCookie() - Clear the cookie value(s).
27 * httpClearFields() - Clear HTTP request fields.
28 * httpClose() - Close an HTTP connection...
29 * httpConnect() - Connect to a HTTP server.
30 * httpConnectEncrypt() - Connect to a HTTP server using encryption.
31 * _httpCreate() - Create an unconnected HTTP connection.
32 * httpDelete() - Send a DELETE request to the server.
33 * httpEncryption() - Set the required encryption on the link.
34 * httpError() - Get the last error on a connection.
35 * httpFlush() - Flush data from a HTTP connection.
36 * httpFlushWrite() - Flush data in write buffer.
37 * httpGet() - Send a GET request to the server.
38 * httpGetAuthString() - Get the current authorization string.
39 * httpGetBlocking() - Get the blocking/non-block state of a connection.
40 * httpGetCookie() - Get any cookie data from the response.
41 * httpGetFd() - Get the file descriptor associated with a
43 * httpGetField() - Get a field value from a request/response.
44 * httpGetLength() - Get the amount of data remaining from the
45 * content-length or transfer-encoding fields.
46 * httpGetLength2() - Get the amount of data remaining from the
47 * content-length or transfer-encoding fields.
48 * httpGetStatus() - Get the status of the last HTTP request.
49 * httpGetSubField() - Get a sub-field value.
50 * httpGetSubField2() - Get a sub-field value.
51 * httpGets() - Get a line of text from a HTTP connection.
52 * httpHead() - Send a HEAD request to the server.
53 * httpInitialize() - Initialize the HTTP interface library and set the
54 * default HTTP proxy (if any).
55 * httpOptions() - Send an OPTIONS request to the server.
56 * httpPost() - Send a POST request to the server.
57 * httpPrintf() - Print a formatted string to a HTTP connection.
58 * httpPut() - Send a PUT request to the server.
59 * httpRead() - Read data from a HTTP connection.
60 * httpRead2() - Read data from a HTTP connection.
61 * _httpReadCDSA() - Read function for the CDSA library.
62 * _httpReadGNUTLS() - Read function for the GNU TLS library.
63 * httpReconnect() - Reconnect to a HTTP server.
64 * httpSetAuthString() - Set the current authorization string.
65 * httpSetCookie() - Set the cookie value(s)...
66 * httpSetExpect() - Set the Expect: header in a request.
67 * httpSetField() - Set the value of an HTTP header.
68 * httpSetLength() - Set the content-length and content-encoding.
69 * httpTrace() - Send an TRACE request to the server.
70 * httpUpdate() - Update the current HTTP state for incoming data.
71 * _httpWait() - Wait for data available on a connection (no flush).
72 * httpWait() - Wait for data available on a connection.
73 * httpWrite() - Write data to a HTTP connection.
74 * httpWrite2() - Write data to a HTTP connection.
75 * _httpWriteCDSA() - Write function for the CDSA library.
76 * _httpWriteGNUTLS() - Write function for the GNU TLS library.
77 * http_bio_ctrl() - Control the HTTP connection.
78 * http_bio_free() - Free OpenSSL data.
79 * http_bio_new() - Initialize an OpenSSL BIO structure.
80 * http_bio_puts() - Send a string for OpenSSL.
81 * http_bio_read() - Read data for OpenSSL.
82 * http_bio_write() - Write data for OpenSSL.
83 * http_debug_hex() - Do a hex dump of a buffer.
84 * http_field() - Return the field index for a field name.
85 * http_read_ssl() - Read from a SSL/TLS connection.
86 * http_send() - Send a request with all fields and the trailing
88 * http_setup_ssl() - Set up SSL/TLS support on a connection.
89 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
90 * http_upgrade() - Force upgrade to TLS encryption.
91 * http_write() - Write a buffer to a HTTP connection.
92 * http_write_chunk() - Write a chunked buffer.
93 * http_write_ssl() - Write to a SSL/TLS connection.
97 * Include necessary headers...
100 #include "http-private.h"
108 # include <sys/time.h>
109 # include <sys/resource.h>
112 # include <sys/poll.h>
113 #endif /* HAVE_POLL */
117 * Some operating systems have done away with the Fxxxx constants for
118 * the fcntl() call; this works around that "feature"...
122 # define FNONBLK O_NONBLOCK
123 #endif /* !FNONBLK */
131 static void http_debug_hex(const char *prefix
, const char *buffer
,
134 static http_field_t
http_field(const char *name
);
135 static int http_send(http_t
*http
, http_state_t request
,
137 static int http_write(http_t
*http
, const char *buffer
,
139 static int http_write_chunk(http_t
*http
, const char *buffer
,
142 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
143 static int http_setup_ssl(http_t
*http
);
144 static void http_shutdown_ssl(http_t
*http
);
145 static int http_upgrade(http_t
*http
);
146 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
147 #endif /* HAVE_SSL */
154 static const char * const http_fields
[] =
171 "If-Unmodified-since",
186 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
188 * BIO methods for OpenSSL...
191 static int http_bio_write(BIO
*h
, const char *buf
, int num
);
192 static int http_bio_read(BIO
*h
, char *buf
, int size
);
193 static int http_bio_puts(BIO
*h
, const char *str
);
194 static long http_bio_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
195 static int http_bio_new(BIO
*h
);
196 static int http_bio_free(BIO
*data
);
198 static BIO_METHOD http_bio_methods
=
205 NULL
, /* http_bio_gets, */
214 * '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections.
217 BIO_METHOD
* /* O - BIO methods for OpenSSL */
218 _httpBIOMethods(void)
220 return (&http_bio_methods
);
222 #endif /* HAVE_SSL && HAVE_LIBSSL */
226 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
230 httpBlocking(http_t
*http
, /* I - Connection to server */
231 int b
) /* I - 1 = blocking, 0 = non-blocking */
239 * 'httpCheck()' - Check to see if there is a pending response from the server.
242 int /* O - 0 = no data, 1 = data available */
243 httpCheck(http_t
*http
) /* I - Connection to server */
245 return (httpWait(http
, 0));
250 * 'httpClearCookie()' - Clear the cookie value(s).
252 * @since CUPS 1.1.19/Mac OS X 10.3@
256 httpClearCookie(http_t
*http
) /* I - Connection to server */
270 * 'httpClearFields()' - Clear HTTP request fields.
274 httpClearFields(http_t
*http
) /* I - Connection to server */
278 memset(http
->fields
, 0, sizeof(http
->fields
));
279 if (http
->hostname
[0] == '/')
280 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
282 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
284 if (http
->field_authorization
)
286 free(http
->field_authorization
);
287 http
->field_authorization
= NULL
;
290 http
->expect
= (http_status_t
)0;
296 * 'httpClose()' - Close an HTTP connection...
300 httpClose(http_t
*http
) /* I - Connection to server */
303 OM_uint32 minor_status
; /* Minor status code */
304 #endif /* HAVE_GSSAPI */
307 DEBUG_printf(("httpClose(http=%p)\n", http
));
312 httpAddrFreeList(http
->addrlist
);
319 http_shutdown_ssl(http
);
320 #endif /* HAVE_SSL */
323 closesocket(http
->fd
);
329 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
330 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
332 if (http
->gssname
!= GSS_C_NO_NAME
)
333 gss_release_name(&minor_status
, &http
->gssname
);
334 #endif /* HAVE_GSSAPI */
336 #ifdef HAVE_AUTHORIZATION_H
338 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
339 #endif /* HAVE_AUTHORIZATION_H */
341 httpClearFields(http
);
343 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
344 free(http
->authstring
);
351 * 'httpConnect()' - Connect to a HTTP server.
353 * This function is deprecated - use @link httpConnectEncrypt@ instead.
358 http_t
* /* O - New HTTP connection */
359 httpConnect(const char *host
, /* I - Host to connect to */
360 int port
) /* I - Port number */
362 return (httpConnectEncrypt(host
, port
, HTTP_ENCRYPT_IF_REQUESTED
));
367 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
370 http_t
* /* O - New HTTP connection */
372 const char *host
, /* I - Host to connect to */
373 int port
, /* I - Port number */
374 http_encryption_t encryption
) /* I - Type of encryption to use */
376 http_t
*http
; /* New HTTP connection */
379 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n",
380 host
, port
, encryption
));
383 * Create the HTTP structure...
386 if ((http
= _httpCreate(host
, port
, encryption
)) == NULL
)
390 * Connect to the remote system...
393 if (!httpReconnect(http
))
397 * Could not connect to any known address - bail out!
400 httpAddrFreeList(http
->addrlist
);
409 * '_httpCreate()' - Create an unconnected HTTP connection.
412 http_t
* /* O - HTTP connection */
414 const char *host
, /* I - Hostname */
415 int port
, /* I - Port number */
416 http_encryption_t encryption
) /* I - Encryption to use */
418 http_t
*http
; /* New HTTP connection */
419 http_addrlist_t
*addrlist
; /* Host address data */
420 char service
[255]; /* Service name */
423 DEBUG_printf(("_httpCreate(host=\"%s\", port=%d, encryption=%d)\n",
424 host
, port
, encryption
));
435 sprintf(service
, "%d", port
);
437 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
441 * Allocate memory for the structure...
444 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
446 httpAddrFreeList(addrlist
);
451 * Initialize the HTTP data...
454 http
->activity
= time(NULL
);
455 http
->addrlist
= addrlist
;
459 http
->gssctx
= GSS_C_NO_CONTEXT
;
460 http
->gssname
= GSS_C_NO_NAME
;
461 #endif /* HAVE_GSSAPI */
462 http
->version
= HTTP_1_1
;
464 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
466 if (port
== 443) /* Always use encryption for https */
467 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
469 http
->encryption
= encryption
;
472 * Return the new structure...
480 * 'httpDelete()' - Send a DELETE request to the server.
483 int /* O - Status of call (0 = success) */
484 httpDelete(http_t
*http
, /* I - Connection to server */
485 const char *uri
) /* I - URI to delete */
487 return (http_send(http
, HTTP_DELETE
, uri
));
492 * 'httpEncryption()' - Set the required encryption on the link.
495 int /* O - -1 on error, 0 on success */
496 httpEncryption(http_t
*http
, /* I - Connection to server */
497 http_encryption_t e
) /* I - New encryption preference */
499 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
505 http
->encryption
= e
;
507 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
508 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
509 return (httpReconnect(http
));
510 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
511 return (http_upgrade(http
));
515 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
519 #endif /* HAVE_SSL */
524 * 'httpError()' - Get the last error on a connection.
527 int /* O - Error code (errno) value */
528 httpError(http_t
*http
) /* I - Connection to server */
531 return (http
->error
);
538 * 'httpFlush()' - Flush data from a HTTP connection.
542 httpFlush(http_t
*http
) /* I - Connection to server */
544 char buffer
[8192]; /* Junk buffer */
545 int blocking
; /* To block or not to block */
548 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
551 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
554 blocking
= http
->blocking
;
558 * Read any data we can...
561 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
564 * Restore blocking and reset the connection if we didn't get all of
565 * the remaining data...
568 http
->blocking
= blocking
;
570 if (http
->state
!= HTTP_WAITING
&& http
->fd
>= 0)
573 * Didn't get the data back, so close the current connection.
576 http
->state
= HTTP_WAITING
;
580 http_shutdown_ssl(http
);
581 #endif /* HAVE_SSL */
584 closesocket(http
->fd
);
595 * 'httpFlushWrite()' - Flush data in write buffer.
597 * @since CUPS 1.2/Mac OS X 10.5@
600 int /* O - Bytes written or -1 on error */
601 httpFlushWrite(http_t
*http
) /* I - Connection to server */
603 int bytes
; /* Bytes written */
606 DEBUG_printf(("httpFlushWrite(http=%p)\n", http
));
608 if (!http
|| !http
->wused
)
611 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
612 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
614 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
623 * 'httpGet()' - Send a GET request to the server.
626 int /* O - Status of call (0 = success) */
627 httpGet(http_t
*http
, /* I - Connection to server */
628 const char *uri
) /* I - URI to get */
630 return (http_send(http
, HTTP_GET
, uri
));
635 * 'httpGetAuthString()' - Get the current authorization string.
637 * The authorization string is set by cupsDoAuthentication() and
638 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
639 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
642 * @since CUPS 1.3/Mac OS X 10.5@
645 char * /* O - Authorization string */
646 httpGetAuthString(http_t
*http
) /* I - Connection to server */
649 return (http
->authstring
);
656 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
658 * @since CUPS 1.2/Mac OS X 10.5@
661 int /* O - 1 if blocking, 0 if non-blocking */
662 httpGetBlocking(http_t
*http
) /* I - Connection to server */
664 return (http
? http
->blocking
: 0);
669 * 'httpGetCookie()' - Get any cookie data from the response.
671 * @since CUPS 1.1.19/Mac OS X 10.3@
674 const char * /* O - Cookie data or NULL */
675 httpGetCookie(http_t
*http
) /* I - HTTP connecion */
677 return (http
? http
->cookie
: NULL
);
682 * 'httpGetFd()' - Get the file descriptor associated with a connection.
684 * @since CUPS 1.2/Mac OS X 10.5@
687 int /* O - File descriptor or -1 if none */
688 httpGetFd(http_t
*http
) /* I - Connection to server */
690 return (http
? http
->fd
: -1);
695 * 'httpGetField()' - Get a field value from a request/response.
698 const char * /* O - Field value */
699 httpGetField(http_t
*http
, /* I - Connection to server */
700 http_field_t field
) /* I - Field to get */
702 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
704 else if (field
== HTTP_FIELD_AUTHORIZATION
&&
705 http
->field_authorization
)
708 * Special case for WWW-Authenticate: as its contents can be
709 * longer than HTTP_MAX_VALUE...
712 return (http
->field_authorization
);
715 return (http
->fields
[field
]);
720 * 'httpGetLength()' - Get the amount of data remaining from the
721 * content-length or transfer-encoding fields.
723 * This function is deprecated and will not return lengths larger than
724 * 2^31 - 1; use httpGetLength2() instead.
729 int /* O - Content length */
730 httpGetLength(http_t
*http
) /* I - Connection to server */
733 * Get the read content length and return the 32-bit value.
738 httpGetLength2(http
);
740 return (http
->_data_remaining
);
748 * 'httpGetLength2()' - Get the amount of data remaining from the
749 * content-length or transfer-encoding fields.
751 * This function returns the complete content length, even for
752 * content larger than 2^31 - 1.
754 * @since CUPS 1.2/Mac OS X 10.5@
757 off_t
/* O - Content length */
758 httpGetLength2(http_t
*http
) /* I - Connection to server */
760 DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http
, http
->state
));
765 if (!strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
767 DEBUG_puts("httpGetLength2: chunked request!");
769 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
770 http
->data_remaining
= 0;
774 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
777 * The following is a hack for HTTP servers that don't send a
778 * content-length or transfer-encoding field...
780 * If there is no content-length then the connection must close
781 * after the transfer is complete...
784 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
787 * Default content length is 0 for errors and 2^31-1 for other
788 * successful requests...
791 if (http
->status
>= HTTP_MULTIPLE_CHOICES
)
792 http
->data_remaining
= 0;
794 http
->data_remaining
= 2147483647;
797 http
->data_remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
800 DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT
"\n",
801 CUPS_LLCAST http
->data_remaining
));
804 if (http
->data_remaining
<= INT_MAX
)
805 http
->_data_remaining
= (int)http
->data_remaining
;
807 http
->_data_remaining
= INT_MAX
;
809 return (http
->data_remaining
);
814 * 'httpGetStatus()' - Get the status of the last HTTP request.
816 * @since CUPS 1.2/Mac OS X 10.5@
819 http_status_t
/* O - HTTP status */
820 httpGetStatus(http_t
*http
) /* I - Connection to server */
822 return (http
? http
->status
: HTTP_ERROR
);
827 * 'httpGetSubField()' - Get a sub-field value.
832 char * /* O - Value or NULL */
833 httpGetSubField(http_t
*http
, /* I - Connection to server */
834 http_field_t field
, /* I - Field index */
835 const char *name
, /* I - Name of sub-field */
836 char *value
) /* O - Value string */
838 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
843 * 'httpGetSubField2()' - Get a sub-field value.
845 * @since CUPS 1.2/Mac OS X 10.5@
848 char * /* O - Value or NULL */
849 httpGetSubField2(http_t
*http
, /* I - Connection to server */
850 http_field_t field
, /* I - Field index */
851 const char *name
, /* I - Name of sub-field */
852 char *value
, /* O - Value string */
853 int valuelen
) /* I - Size of value buffer */
855 const char *fptr
; /* Pointer into field */
856 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
857 *ptr
, /* Pointer into string buffer */
858 *end
; /* End of value buffer */
860 DEBUG_printf(("httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)\n",
861 http
, field
, name
, value
, valuelen
));
863 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
864 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
867 end
= value
+ valuelen
- 1;
869 for (fptr
= http
->fields
[field
]; *fptr
;)
872 * Skip leading whitespace...
875 while (isspace(*fptr
& 255))
885 * Get the sub-field name...
889 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) &&
890 ptr
< (temp
+ sizeof(temp
) - 1);
895 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
898 * Skip trailing chars up to the '='...
901 while (isspace(*fptr
& 255))
911 * Skip = and leading whitespace...
916 while (isspace(*fptr
& 255))
922 * Read quoted string...
925 for (ptr
= value
, fptr
++;
926 *fptr
&& *fptr
!= '\"' && ptr
< end
;
931 while (*fptr
&& *fptr
!= '\"')
940 * Read unquoted string...
944 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< end
;
949 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
953 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
956 * See if this is the one...
959 if (!strcmp(name
, temp
))
970 * 'httpGets()' - Get a line of text from a HTTP connection.
973 char * /* O - Line or NULL */
974 httpGets(char *line
, /* I - Line to read into */
975 int length
, /* I - Max length of buffer */
976 http_t
*http
) /* I - Connection to server */
978 char *lineptr
, /* Pointer into line */
979 *lineend
, /* End of line */
980 *bufptr
, /* Pointer into input buffer */
981 *bufend
; /* Pointer to end of buffer */
982 int bytes
, /* Number of bytes read */
983 eol
; /* End-of-line? */
986 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
988 if (http
== NULL
|| line
== NULL
)
992 * Read a line from the buffer...
996 lineend
= line
+ length
- 1;
999 while (lineptr
< lineend
)
1002 * Pre-load the buffer as needed...
1011 while (http
->used
== 0)
1014 * No newline; see if there is more data to be read...
1017 if (!http
->blocking
&& !_httpWait(http
, 10000, 1))
1019 DEBUG_puts("httpGets: Timed out!");
1021 http
->error
= WSAETIMEDOUT
;
1023 http
->error
= ETIMEDOUT
;
1030 bytes
= http_read_ssl(http
, http
->buffer
+ http
->used
,
1031 HTTP_MAX_BUFFER
- http
->used
);
1033 #endif /* HAVE_SSL */
1034 bytes
= recv(http
->fd
, http
->buffer
+ http
->used
,
1035 HTTP_MAX_BUFFER
- http
->used
, 0);
1037 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
1042 * Nope, can't get a line this time...
1046 if (WSAGetLastError() != http
->error
)
1048 http
->error
= WSAGetLastError();
1052 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
1054 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
1058 else if (errno
!= http
->error
)
1060 http
->error
= errno
;
1067 else if (bytes
== 0)
1069 http
->error
= EPIPE
;
1075 * Yup, update the amount used...
1078 http
->used
+= bytes
;
1082 * Now copy as much of the current line as possible...
1085 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1086 lineptr
< lineend
&& bufptr
< bufend
;)
1088 if (*bufptr
== 0x0a)
1094 else if (*bufptr
== 0x0d)
1097 *lineptr
++ = *bufptr
++;
1100 http
->used
-= (int)(bufptr
- http
->buffer
);
1102 memmove(http
->buffer
, bufptr
, http
->used
);
1110 http
->activity
= time(NULL
);
1114 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
1120 DEBUG_puts("httpGets: No new line available!");
1127 * 'httpHead()' - Send a HEAD request to the server.
1130 int /* O - Status of call (0 = success) */
1131 httpHead(http_t
*http
, /* I - Connection to server */
1132 const char *uri
) /* I - URI for head */
1134 return (http_send(http
, HTTP_HEAD
, uri
));
1139 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1140 * default HTTP proxy (if any).
1144 httpInitialize(void)
1148 struct timeval curtime
; /* Current time in microseconds */
1149 # endif /* !WIN32 */
1150 int i
; /* Looping var */
1151 unsigned char data
[1024]; /* Seed data */
1152 #endif /* HAVE_LIBSSL */
1155 WSADATA winsockdata
; /* WinSock data */
1156 static int initialized
= 0; /* Has WinSock been initialized? */
1160 WSAStartup(MAKEWORD(1,1), &winsockdata
);
1161 #elif !defined(SO_NOSIGPIPE)
1163 * Ignore SIGPIPE signals...
1167 sigset(SIGPIPE
, SIG_IGN
);
1168 # elif defined(HAVE_SIGACTION)
1169 struct sigaction action
; /* POSIX sigaction data */
1172 memset(&action
, 0, sizeof(action
));
1173 action
.sa_handler
= SIG_IGN
;
1174 sigaction(SIGPIPE
, &action
, NULL
);
1176 signal(SIGPIPE
, SIG_IGN
);
1177 # endif /* !SO_NOSIGPIPE */
1181 gnutls_global_init();
1182 #endif /* HAVE_GNUTLS */
1185 SSL_load_error_strings();
1189 * Using the current time is a dubious random seed, but on some systems
1190 * it is the best we can do (on others, this seed isn't even used...)
1195 gettimeofday(&curtime
, NULL
);
1196 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
1199 for (i
= 0; i
< sizeof(data
); i
++)
1202 RAND_seed(data
, sizeof(data
));
1203 #endif /* HAVE_LIBSSL */
1208 * 'httpOptions()' - Send an OPTIONS request to the server.
1211 int /* O - Status of call (0 = success) */
1212 httpOptions(http_t
*http
, /* I - Connection to server */
1213 const char *uri
) /* I - URI for options */
1215 return (http_send(http
, HTTP_OPTIONS
, uri
));
1220 * 'httpPost()' - Send a POST request to the server.
1223 int /* O - Status of call (0 = success) */
1224 httpPost(http_t
*http
, /* I - Connection to server */
1225 const char *uri
) /* I - URI for post */
1227 return (http_send(http
, HTTP_POST
, uri
));
1232 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1237 int /* O - Number of bytes written */
1238 httpPrintf(http_t
*http
, /* I - Connection to server */
1239 const char *format
, /* I - printf-style format string */
1240 ...) /* I - Additional args as needed */
1242 int bytes
; /* Number of bytes to write */
1243 char buf
[16384]; /* Buffer for formatted string */
1244 va_list ap
; /* Variable argument pointer */
1247 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
1249 va_start(ap
, format
);
1250 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1253 DEBUG_printf(("httpPrintf: %s", buf
));
1255 if (http
->data_encoding
== HTTP_ENCODE_FIELDS
)
1256 return (httpWrite2(http
, buf
, bytes
));
1261 DEBUG_puts(" flushing existing data...");
1263 if (httpFlushWrite(http
) < 0)
1267 return (http_write(http
, buf
, bytes
));
1273 * 'httpPut()' - Send a PUT request to the server.
1276 int /* O - Status of call (0 = success) */
1277 httpPut(http_t
*http
, /* I - Connection to server */
1278 const char *uri
) /* I - URI to put */
1280 return (http_send(http
, HTTP_PUT
, uri
));
1285 * 'httpRead()' - Read data from a HTTP connection.
1287 * This function is deprecated. Use the httpRead2() function which can
1288 * read more than 2GB of data.
1293 int /* O - Number of bytes read */
1294 httpRead(http_t
*http
, /* I - Connection to server */
1295 char *buffer
, /* I - Buffer for data */
1296 int length
) /* I - Maximum number of bytes */
1298 return ((int)httpRead2(http
, buffer
, length
));
1303 * 'httpRead2()' - Read data from a HTTP connection.
1305 * @since CUPS 1.2/Mac OS X 10.5@
1308 ssize_t
/* O - Number of bytes read */
1309 httpRead2(http_t
*http
, /* I - Connection to server */
1310 char *buffer
, /* I - Buffer for data */
1311 size_t length
) /* I - Maximum number of bytes */
1313 ssize_t bytes
; /* Bytes read */
1314 char len
[32]; /* Length string */
1317 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
")\n",
1318 http
, buffer
, CUPS_LLCAST length
));
1320 if (http
== NULL
|| buffer
== NULL
)
1323 http
->activity
= time(NULL
);
1328 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
1329 http
->data_remaining
<= 0)
1331 DEBUG_puts("httpRead2: Getting chunk length...");
1333 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1335 DEBUG_puts("httpRead2: Could not get length!");
1339 http
->data_remaining
= strtoll(len
, NULL
, 16);
1340 if (http
->data_remaining
< 0)
1342 DEBUG_puts("httpRead2: Negative chunk length!");
1347 DEBUG_printf(("httpRead2: data_remaining=" CUPS_LLFMT
"\n",
1348 CUPS_LLCAST http
->data_remaining
));
1350 if (http
->data_remaining
<= 0)
1353 * A zero-length chunk ends a transfer; unless we are reading POST
1357 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1358 httpGets(len
, sizeof(len
), http
);
1360 if (http
->state
== HTTP_POST_RECV
)
1363 http
->state
= HTTP_WAITING
;
1366 * Prevent future reads for this request...
1369 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1373 else if (length
> (size_t)http
->data_remaining
)
1374 length
= (size_t)http
->data_remaining
;
1376 if (http
->used
== 0 && length
<= 256)
1379 * Buffer small reads for better performance...
1382 if (!http
->blocking
&& !httpWait(http
, 10000))
1385 if (http
->data_remaining
> sizeof(http
->buffer
))
1386 bytes
= sizeof(http
->buffer
);
1388 bytes
= http
->data_remaining
;
1392 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
1394 #endif /* HAVE_SSL */
1396 DEBUG_printf(("httpRead2: reading %d bytes from socket into buffer...\n",
1399 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
1401 DEBUG_printf(("httpRead2: read %d bytes from socket into buffer...\n",
1410 http
->error
= WSAGetLastError();
1415 http
->error
= errno
;
1422 http
->error
= EPIPE
;
1429 if (length
> (size_t)http
->used
)
1430 length
= (size_t)http
->used
;
1432 bytes
= (ssize_t
)length
;
1434 DEBUG_printf(("httpRead2: grabbing %d bytes from input buffer...\n",
1437 memcpy(buffer
, http
->buffer
, length
);
1438 http
->used
-= (int)length
;
1441 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1446 if (!http
->blocking
&& !httpWait(http
, 10000))
1449 bytes
= (ssize_t
)http_read_ssl(http
, buffer
, (int)length
);
1451 #endif /* HAVE_SSL */
1454 if (!http
->blocking
&& !httpWait(http
, 10000))
1457 DEBUG_printf(("httpRead2: reading " CUPS_LLFMT
" bytes from socket...\n",
1458 CUPS_LLCAST length
));
1461 bytes
= (ssize_t
)recv(http
->fd
, buffer
, (int)length
, 0);
1463 while ((bytes
= recv(http
->fd
, buffer
, length
, 0)) < 0)
1464 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1468 DEBUG_printf(("httpRead2: read " CUPS_LLFMT
" bytes from socket...\n",
1469 CUPS_LLCAST bytes
));
1474 http
->data_remaining
-= bytes
;
1476 if (http
->data_remaining
<= INT_MAX
)
1477 http
->_data_remaining
= (int)http
->data_remaining
;
1479 http
->_data_remaining
= INT_MAX
;
1484 http
->error
= WSAGetLastError();
1486 if (errno
== EINTR
|| errno
== EAGAIN
)
1489 http
->error
= errno
;
1494 http
->error
= EPIPE
;
1498 if (http
->data_remaining
== 0)
1500 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1501 httpGets(len
, sizeof(len
), http
);
1503 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1505 if (http
->state
== HTTP_POST_RECV
)
1508 http
->state
= HTTP_WAITING
;
1513 http_debug_hex("httpRead2", buffer
, (int)bytes
);
1520 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1522 * '_httpReadCDSA()' - Read function for the CDSA library.
1525 OSStatus
/* O - -1 on error, 0 on success */
1527 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1528 void *data
, /* I - Data buffer */
1529 size_t *dataLength
) /* IO - Number of bytes */
1531 OSStatus result
; /* Return value */
1532 ssize_t bytes
; /* Number of bytes read */
1533 http_t
*http
; /* HTTP connection */
1536 http
= (http_t
*)connection
;
1538 if (!http
->blocking
)
1541 * Make sure we have data before we read...
1544 if (!_httpWait(http
, 10000, 0))
1546 http
->error
= ETIMEDOUT
;
1553 bytes
= recv(http
->fd
, data
, *dataLength
, 0);
1555 while (bytes
== -1 && errno
== EINTR
);
1557 if (bytes
== *dataLength
)
1563 *dataLength
= bytes
;
1564 result
= errSSLWouldBlock
;
1571 result
= errSSLClosedGraceful
;
1572 else if (errno
== EAGAIN
)
1573 result
= errSSLWouldBlock
;
1575 result
= errSSLClosedAbort
;
1580 #endif /* HAVE_SSL && HAVE_CDSASSL */
1583 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
1585 * '_httpReadGNUTLS()' - Read function for the GNU TLS library.
1588 ssize_t
/* O - Number of bytes read or -1 on error */
1590 gnutls_transport_ptr ptr
, /* I - Connection to server */
1591 void *data
, /* I - Buffer */
1592 size_t length
) /* I - Number of bytes to read */
1594 http_t
*http
; /* HTTP connection */
1597 http
= (http_t
*)ptr
;
1599 if (!http
->blocking
)
1602 * Make sure we have data before we read...
1605 if (!_httpWait(http
, 10000, 0))
1607 http
->error
= ETIMEDOUT
;
1612 return (recv(http
->fd
, data
, length
, 0));
1614 #endif /* HAVE_SSL && HAVE_GNUTLS */
1618 * 'httpReconnect()' - Reconnect to a HTTP server.
1621 int /* O - 0 on success, non-zero on failure */
1622 httpReconnect(http_t
*http
) /* I - Connection to server */
1624 http_addrlist_t
*addr
; /* Connected address */
1626 http_addrlist_t
*current
; /* Current address */
1627 char temp
[256]; /* Temporary address string */
1631 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
1639 DEBUG_puts("httpReconnect: Shutting down SSL/TLS...");
1640 http_shutdown_ssl(http
);
1642 #endif /* HAVE_SSL */
1645 * Close any previously open socket...
1650 DEBUG_printf(("httpReconnect: Closing socket %d...\n", http
->fd
));
1653 closesocket(http
->fd
);
1664 * Connect to the server...
1668 for (current
= http
->addrlist
; current
; current
= current
->next
)
1669 DEBUG_printf(("httpReconnect: Address %s:%d\n",
1670 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
1671 _httpAddrPort(&(current
->addr
))));
1674 if ((addr
= httpAddrConnect(http
->addrlist
, &(http
->fd
))) == NULL
)
1677 * Unable to connect...
1681 http
->error
= WSAGetLastError();
1683 http
->error
= errno
;
1685 http
->status
= HTTP_ERROR
;
1687 DEBUG_printf(("httpReconnect: httpAddrConnect failed: %s\n",
1688 strerror(http
->error
)));
1693 DEBUG_printf(("httpReconnect: New socket=%d\n", http
->fd
));
1695 http
->hostaddr
= &(addr
->addr
);
1697 http
->status
= HTTP_CONTINUE
;
1700 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
1703 * Always do encryption via SSL.
1706 if (http_setup_ssl(http
) != 0)
1709 closesocket(http
->fd
);
1717 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
1718 return (http_upgrade(http
));
1719 #endif /* HAVE_SSL */
1721 DEBUG_printf(("httpReconnect: Connected to %s:%d...\n",
1722 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
1723 _httpAddrPort(http
->hostaddr
)));
1730 * 'httpSetAuthString()' - Set the current authorization string.
1732 * This function just stores a copy of the current authorization string in
1733 * the HTTP connection object. You must still call httpSetField() to set
1734 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
1735 * httpHead(), httpOptions(), httpPost, or httpPut().
1737 * @since CUPS 1.3/Mac OS X 10.5@
1741 httpSetAuthString(http_t
*http
, /* I - Connection to server */
1742 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
1743 const char *data
) /* I - Auth data (NULL for none) */
1746 * Range check input...
1752 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
1753 free(http
->authstring
);
1755 http
->authstring
= http
->_authstring
;
1760 * Set the current authorization string...
1763 int len
= (int)strlen(scheme
) + (data
? (int)strlen(data
) + 1 : 0) + 1;
1766 if (len
> (int)sizeof(http
->_authstring
))
1768 if ((temp
= malloc(len
)) == NULL
)
1769 len
= sizeof(http
->_authstring
);
1771 http
->authstring
= temp
;
1775 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
1777 strlcpy(http
->authstring
, scheme
, len
);
1782 * Clear the current authorization string...
1785 http
->_authstring
[0] = '\0';
1791 * 'httpSetCookie()' - Set the cookie value(s)...
1793 * @since CUPS 1.1.19/Mac OS X 10.3@
1797 httpSetCookie(http_t
*http
, /* I - Connection */
1798 const char *cookie
) /* I - Cookie string */
1807 http
->cookie
= strdup(cookie
);
1809 http
->cookie
= NULL
;
1814 * 'httpSetExpect()' - Set the Expect: header in a request.
1816 * Currently only HTTP_CONTINUE is supported for the "expect" argument.
1818 * @since CUPS 1.2/Mac OS X 10.5@
1822 httpSetExpect(http_t
*http
, /* I - Connection to server */
1823 http_status_t expect
) /* I - HTTP status to expect (HTTP_CONTINUE) */
1826 http
->expect
= expect
;
1831 * 'httpSetField()' - Set the value of an HTTP header.
1835 httpSetField(http_t
*http
, /* I - Connection to server */
1836 http_field_t field
, /* I - Field index */
1837 const char *value
) /* I - Value */
1840 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
1841 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
1845 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
1848 * Special case for Authorization: as its contents can be
1849 * longer than HTTP_MAX_VALUE
1852 if (field
== HTTP_FIELD_AUTHORIZATION
)
1854 if (http
->field_authorization
)
1855 free(http
->field_authorization
);
1857 http
->field_authorization
= strdup(value
);
1863 * 'httpSetLength()' - Set the content-length and content-encoding.
1865 * @since CUPS 1.2/Mac OS X 10.5@
1869 httpSetLength(http_t
*http
, /* I - Connection to server */
1870 size_t length
) /* I - Length (0 for chunked) */
1877 strcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked");
1878 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
1882 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
1883 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
1884 CUPS_LLFMT
, CUPS_LLCAST length
);
1890 * 'httpTrace()' - Send an TRACE request to the server.
1893 int /* O - Status of call (0 = success) */
1894 httpTrace(http_t
*http
, /* I - Connection to server */
1895 const char *uri
) /* I - URI for trace */
1897 return (http_send(http
, HTTP_TRACE
, uri
));
1902 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1905 http_status_t
/* O - HTTP status */
1906 httpUpdate(http_t
*http
) /* I - Connection to server */
1908 char line
[32768], /* Line from connection... */
1909 *value
; /* Pointer to value on line */
1910 http_field_t field
; /* Field index */
1911 int major
, minor
, /* HTTP version numbers */
1912 status
; /* Request status */
1915 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1918 * Flush pending data, if any...
1923 DEBUG_puts(" flushing buffer...");
1925 if (httpFlushWrite(http
) < 0)
1926 return (HTTP_ERROR
);
1930 * If we haven't issued any commands, then there is nothing to "update"...
1933 if (http
->state
== HTTP_WAITING
)
1934 return (HTTP_CONTINUE
);
1937 * Grab all of the lines we can from the connection...
1940 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1942 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1944 if (line
[0] == '\0')
1947 * Blank line means the start of the data section (if any). Return
1948 * the result code, too...
1950 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1951 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1955 if (http
->status
== HTTP_CONTINUE
)
1956 return (http
->status
);
1958 if (http
->status
< HTTP_BAD_REQUEST
)
1959 http
->digest_tries
= 0;
1962 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1964 if (http_setup_ssl(http
) != 0)
1967 closesocket(http
->fd
);
1972 return (HTTP_ERROR
);
1975 return (HTTP_CONTINUE
);
1977 #endif /* HAVE_SSL */
1979 httpGetLength2(http
);
1981 switch (http
->state
)
1985 case HTTP_POST_RECV
:
1988 case HTTP_POST_SEND
:
1993 http
->state
= HTTP_WAITING
;
1997 return (http
->status
);
1999 else if (strncmp(line
, "HTTP/", 5) == 0)
2002 * Got the beginning of a response...
2005 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
2006 return (HTTP_ERROR
);
2008 http
->version
= (http_version_t
)(major
* 100 + minor
);
2009 http
->status
= (http_status_t
)status
;
2011 else if ((value
= strchr(line
, ':')) != NULL
)
2018 while (isspace(*value
& 255))
2022 * Be tolerants of servers that send unknown attribute fields...
2025 if (!strcasecmp(line
, "expect"))
2028 * "Expect: 100-continue" or similar...
2031 http
->expect
= (http_status_t
)atoi(value
);
2033 else if (!strcasecmp(line
, "cookie"))
2036 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2039 httpSetCookie(http
, value
);
2041 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
2043 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
2047 httpSetField(http
, field
, value
);
2051 http
->status
= HTTP_ERROR
;
2052 return (HTTP_ERROR
);
2057 * See if there was an error...
2060 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
2061 return (http
->status
);
2065 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
2066 strerror(http
->error
)));
2067 http
->status
= HTTP_ERROR
;
2068 return (HTTP_ERROR
);
2072 * If we haven't already returned, then there is nothing new...
2075 return (HTTP_CONTINUE
);
2080 * '_httpWait()' - Wait for data available on a connection (no flush).
2083 int /* O - 1 if data is available, 0 otherwise */
2084 _httpWait(http_t
*http
, /* I - Connection to server */
2085 int msec
, /* I - Milliseconds to wait */
2086 int usessl
) /* I - Use SSL context? */
2089 struct pollfd pfd
; /* Polled file descriptor */
2091 fd_set input_set
; /* select() input set */
2092 struct timeval timeout
; /* Timeout */
2093 #endif /* HAVE_POLL */
2094 int nfds
; /* Result from select()/poll() */
2097 DEBUG_printf(("_httpWait(http=%p, msec=%d, usessl=%d)\n", http
, msec
, usessl
));
2103 * Check the SSL/TLS buffers for data first...
2107 if (http
->tls
&& usessl
)
2110 if (SSL_pending((SSL
*)(http
->tls
)))
2112 # elif defined(HAVE_GNUTLS)
2113 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2115 # elif defined(HAVE_CDSASSL)
2116 size_t bytes
; /* Bytes that are available */
2118 if (!SSLGetBufferedReadSize(((http_tls_t
*)(http
->tls
))->session
, &bytes
) &&
2121 # endif /* HAVE_LIBSSL */
2123 #endif /* HAVE_SSL */
2126 * Then try doing a select() or poll() to poll the socket...
2131 pfd
.events
= POLLIN
;
2133 while ((nfds
= poll(&pfd
, 1, msec
)) < 0 && errno
== EINTR
);
2138 FD_ZERO(&input_set
);
2139 FD_SET(http
->fd
, &input_set
);
2141 DEBUG_printf(("_httpWait: msec=%d, http->fd=%d\n", msec
, http
->fd
));
2145 timeout
.tv_sec
= msec
/ 1000;
2146 timeout
.tv_usec
= (msec
% 1000) * 1000;
2148 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
2151 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
2153 DEBUG_printf(("_httpWait: select() returned %d...\n", nfds
));
2156 while (nfds
< 0 && WSAGetLastError() == WSAEINTR
);
2158 while (nfds
< 0 && errno
== EINTR
);
2160 #endif /* HAVE_POLL */
2162 DEBUG_printf(("_httpWait: returning with nfds=%d...\n", nfds
));
2169 * 'httpWait()' - Wait for data available on a connection.
2171 * @since CUPS 1.1.19/Mac OS X 10.3@
2174 int /* O - 1 if data is available, 0 otherwise */
2175 httpWait(http_t
*http
, /* I - Connection to server */
2176 int msec
) /* I - Milliseconds to wait */
2179 * First see if there is data in the buffer...
2189 * Flush pending data, if any...
2194 if (httpFlushWrite(http
) < 0)
2199 * If not, check the SSL/TLS buffers and do a select() on the connection...
2202 return (_httpWait(http
, msec
, 1));
2207 * 'httpWrite()' - Write data to a HTTP connection.
2209 * This function is deprecated. Use the httpWrite2() function which can
2210 * write more than 2GB of data.
2215 int /* O - Number of bytes written */
2216 httpWrite(http_t
*http
, /* I - Connection to server */
2217 const char *buffer
, /* I - Buffer for data */
2218 int length
) /* I - Number of bytes to write */
2220 return ((int)httpWrite2(http
, buffer
, length
));
2225 * 'httpWrite2()' - Write data to a HTTP connection.
2227 * @since CUPS 1.2/Mac OS X 10.5@
2230 ssize_t
/* O - Number of bytes written */
2231 httpWrite2(http_t
*http
, /* I - Connection to server */
2232 const char *buffer
, /* I - Buffer for data */
2233 size_t length
) /* I - Number of bytes to write */
2235 ssize_t bytes
; /* Bytes written */
2238 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")\n", http
,
2239 buffer
, CUPS_LLCAST length
));
2242 * Range check input...
2245 if (http
== NULL
|| buffer
== NULL
)
2249 * Mark activity on the connection...
2252 http
->activity
= time(NULL
);
2255 * Buffer small writes for better performance...
2260 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
2262 DEBUG_printf(("httpWrite2: Flushing buffer (wused=%d, length="
2263 CUPS_LLFMT
")\n", http
->wused
, CUPS_LLCAST length
));
2265 httpFlushWrite(http
);
2268 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
) &&
2269 length
< sizeof(http
->wbuffer
))
2272 * Write to buffer...
2275 DEBUG_printf(("httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...\n",
2276 CUPS_LLCAST length
));
2278 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
2279 http
->wused
+= (int)length
;
2280 bytes
= (ssize_t
)length
;
2285 * Otherwise write the data directly...
2288 DEBUG_printf(("httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...\n",
2289 CUPS_LLCAST length
));
2291 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2292 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, (int)length
);
2294 bytes
= (ssize_t
)http_write(http
, buffer
, (int)length
);
2296 DEBUG_printf(("httpWrite2: Wrote " CUPS_LLFMT
" bytes...\n",
2297 CUPS_LLCAST bytes
));
2300 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2301 http
->data_remaining
-= bytes
;
2307 * Handle end-of-request processing...
2310 if ((http
->data_encoding
== HTTP_ENCODE_CHUNKED
&& length
== 0) ||
2311 (http
->data_encoding
== HTTP_ENCODE_LENGTH
&& http
->data_remaining
== 0))
2314 * Finished with the transfer; unless we are sending POST or PUT
2318 DEBUG_puts("httpWrite: changing states...");
2321 httpFlushWrite(http
);
2323 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2326 * Send a 0-length chunk at the end of the request...
2329 http_write(http
, "0\r\n\r\n", 5);
2332 * Reset the data state...
2335 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
2336 http
->data_remaining
= 0;
2339 if (http
->state
== HTTP_POST_RECV
)
2341 else if (http
->state
== HTTP_PUT_RECV
)
2342 http
->state
= HTTP_STATUS
;
2344 http
->state
= HTTP_WAITING
;
2351 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
2353 * '_httpWriteCDSA()' - Write function for the CDSA library.
2356 OSStatus
/* O - -1 on error, 0 on success */
2358 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2359 const void *data
, /* I - Data buffer */
2360 size_t *dataLength
) /* IO - Number of bytes */
2362 OSStatus result
; /* Return value */
2363 ssize_t bytes
; /* Number of bytes read */
2364 http_t
*http
; /* HTTP connection */
2367 http
= (http_t
*)connection
;
2371 bytes
= write(http
->fd
, data
, *dataLength
);
2373 while (bytes
== -1 && errno
== EINTR
);
2375 if (bytes
== *dataLength
)
2379 else if (bytes
>= 0)
2381 *dataLength
= bytes
;
2382 result
= errSSLWouldBlock
;
2388 if (errno
== EAGAIN
)
2389 result
= errSSLWouldBlock
;
2391 result
= errSSLClosedAbort
;
2396 #endif /* HAVE_SSL && HAVE_CDSASSL */
2399 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
2401 * '_httpWriteGNUTLS()' - Write function for the GNU TLS library.
2404 ssize_t
/* O - Number of bytes written or -1 on error */
2406 gnutls_transport_ptr ptr
, /* I - Connection to server */
2407 const void *data
, /* I - Data buffer */
2408 size_t length
) /* I - Number of bytes to write */
2410 return (send(((http_t
*)ptr
)->fd
, data
, length
, 0));
2412 #endif /* HAVE_SSL && HAVE_GNUTLS */
2415 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
2417 * 'http_bio_ctrl()' - Control the HTTP connection.
2420 static long /* O - Result/data */
2421 http_bio_ctrl(BIO
*h
, /* I - BIO data */
2422 int cmd
, /* I - Control command */
2423 long arg1
, /* I - First argument */
2424 void *arg2
) /* I - Second argument */
2431 case BIO_CTRL_RESET
:
2435 case BIO_C_SET_FILE_PTR
:
2440 case BIO_C_GET_FILE_PTR
:
2443 *((void **)arg2
) = h
->ptr
;
2450 case BIO_CTRL_FLUSH
:
2457 * 'http_bio_free()' - Free OpenSSL data.
2460 static int /* O - 1 on success, 0 on failure */
2461 http_bio_free(BIO
*h
) /* I - BIO data */
2477 * 'http_bio_new()' - Initialize an OpenSSL BIO structure.
2480 static int /* O - 1 on success, 0 on failure */
2481 http_bio_new(BIO
*h
) /* I - BIO data */
2496 * 'http_bio_puts()' - Send a string for OpenSSL.
2499 static int /* O - Bytes written */
2500 http_bio_puts(BIO
*h
, /* I - BIO data */
2501 const char *str
) /* I - String to write */
2504 return (send(((http_t
*)h
->ptr
)->fd
, str
, (int)strlen(str
), 0));
2506 return (send(((http_t
*)h
->ptr
)->fd
, str
, strlen(str
), 0));
2512 * 'http_bio_read()' - Read data for OpenSSL.
2515 static int /* O - Bytes read */
2516 http_bio_read(BIO
*h
, /* I - BIO data */
2517 char *buf
, /* I - Buffer */
2518 int size
) /* I - Number of bytes to read */
2520 http_t
*http
; /* HTTP connection */
2523 http
= (http_t
*)h
->ptr
;
2525 if (!http
->blocking
)
2528 * Make sure we have data before we read...
2531 if (!_httpWait(http
, 10000, 0))
2534 http
->error
= WSAETIMEDOUT
;
2536 http
->error
= ETIMEDOUT
;
2543 return (recv(http
->fd
, buf
, size
, 0));
2548 * 'http_bio_write()' - Write data for OpenSSL.
2551 static int /* O - Bytes written */
2552 http_bio_write(BIO
*h
, /* I - BIO data */
2553 const char *buf
, /* I - Buffer to write */
2554 int num
) /* I - Number of bytes to write */
2556 return (send(((http_t
*)h
->ptr
)->fd
, buf
, num
, 0));
2558 #endif /* HAVE_SSL && HAVE_LIBSSL */
2563 * 'http_debug_hex()' - Do a hex dump of a buffer.
2567 http_debug_hex(const char *prefix
, /* I - Prefix for line */
2568 const char *buffer
, /* I - Buffer to dump */
2569 int bytes
) /* I - Bytes to dump */
2571 int i
, j
, /* Looping vars */
2572 ch
; /* Current character */
2573 char line
[255], /* Line buffer */
2574 *start
, /* Start of line after prefix */
2575 *ptr
; /* Pointer into line */
2578 if (_cups_debug_fd
< 0)
2581 DEBUG_printf(("%s: %d bytes:\n", prefix
, bytes
));
2583 snprintf(line
, sizeof(line
), "%s: ", prefix
);
2584 start
= line
+ strlen(line
);
2586 for (i
= 0; i
< bytes
; i
+= 16)
2588 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
2589 sprintf(ptr
, "%02X", buffer
[i
+ j
] & 255);
2601 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
2603 ch
= buffer
[i
+ j
] & 255;
2605 if (ch
< ' ' || ch
>= 127)
2619 * 'http_field()' - Return the field index for a field name.
2622 static http_field_t
/* O - Field index */
2623 http_field(const char *name
) /* I - String name */
2625 int i
; /* Looping var */
2628 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2629 if (strcasecmp(name
, http_fields
[i
]) == 0)
2630 return ((http_field_t
)i
);
2632 return (HTTP_FIELD_UNKNOWN
);
2638 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2641 static int /* O - Bytes read */
2642 http_read_ssl(http_t
*http
, /* I - Connection to server */
2643 char *buf
, /* I - Buffer to store data */
2644 int len
) /* I - Length of buffer */
2646 # if defined(HAVE_LIBSSL)
2647 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2649 # elif defined(HAVE_GNUTLS)
2650 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2652 # elif defined(HAVE_CDSASSL)
2653 int result
; /* Return value */
2654 OSStatus error
; /* Error info */
2655 size_t processed
; /* Number of bytes processed */
2658 error
= SSLRead(((http_tls_t
*)http
->tls
)->session
, buf
, len
, &processed
);
2663 result
= (int)processed
;
2665 case errSSLClosedGraceful
:
2668 case errSSLWouldBlock
:
2670 result
= (int)processed
;
2684 # endif /* HAVE_LIBSSL */
2686 #endif /* HAVE_SSL */
2690 * 'http_send()' - Send a request with all fields and the trailing blank line.
2693 static int /* O - 0 on success, non-zero on error */
2694 http_send(http_t
*http
, /* I - Connection to server */
2695 http_state_t request
, /* I - Request code */
2696 const char *uri
) /* I - URI */
2698 int i
; /* Looping var */
2699 char buf
[1024]; /* Encoded URI buffer */
2700 static const char * const codes
[] =
2701 { /* Request code strings */
2718 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
2719 http
, codes
[request
], uri
));
2721 if (http
== NULL
|| uri
== NULL
)
2725 * Set the User-Agent field if it isn't already...
2728 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
2729 httpSetField(http
, HTTP_FIELD_USER_AGENT
, CUPS_MINIMAL
);
2732 * Encode the URI as needed...
2735 _httpEncodeURI(buf
, uri
, sizeof(buf
));
2738 * See if we had an error the last time around; if so, reconnect...
2741 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
2742 if (httpReconnect(http
))
2746 * Flush any written data that is pending...
2750 httpFlushWrite(http
);
2753 * Send the request header...
2756 http
->state
= request
;
2757 http
->data_encoding
= HTTP_ENCODE_FIELDS
;
2759 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
2762 http
->status
= HTTP_CONTINUE
;
2765 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
2767 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
2768 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
2770 #endif /* HAVE_SSL */
2772 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
2774 http
->status
= HTTP_ERROR
;
2778 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2779 if (http
->fields
[i
][0] != '\0')
2781 DEBUG_printf(("http_send: %s: %s\n", http_fields
[i
],
2782 httpGetField(http
, i
)));
2784 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
],
2785 httpGetField(http
, i
)) < 1)
2787 http
->status
= HTTP_ERROR
;
2793 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
2795 http
->status
= HTTP_ERROR
;
2799 if (http
->expect
== HTTP_CONTINUE
&&
2800 (http
->state
== HTTP_POST_RECV
|| http
->state
== HTTP_PUT_RECV
))
2801 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
2803 http
->status
= HTTP_ERROR
;
2807 if (httpPrintf(http
, "\r\n") < 1)
2809 http
->status
= HTTP_ERROR
;
2813 httpFlushWrite(http
);
2814 httpGetLength2(http
);
2815 httpClearFields(http
);
2818 * The Kerberos and AuthRef authentication strings can only be used once...
2821 if (http
->field_authorization
&& http
->authstring
&&
2822 (!strncmp(http
->authstring
, "Negotiate", 9) ||
2823 !strncmp(http
->authstring
, "AuthRef", 7)))
2825 http
->_authstring
[0] = '\0';
2827 if (http
->authstring
!= http
->_authstring
)
2828 free(http
->authstring
);
2830 http
->authstring
= http
->_authstring
;
2839 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2842 static int /* O - Status of connection */
2843 http_setup_ssl(http_t
*http
) /* I - Connection to server */
2846 SSL_CTX
*context
; /* Context for encryption */
2847 SSL
*conn
; /* Connection for encryption */
2848 BIO
*bio
; /* BIO data */
2849 # elif defined(HAVE_GNUTLS)
2850 http_tls_t
*conn
; /* TLS session object */
2851 gnutls_certificate_client_credentials
*credentials
;
2852 /* TLS credentials */
2853 # elif defined(HAVE_CDSASSL)
2854 OSStatus error
; /* Error code */
2855 http_tls_t
*conn
; /* CDSA connection information */
2856 # endif /* HAVE_LIBSSL */
2859 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
2862 context
= SSL_CTX_new(SSLv23_client_method());
2864 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
2866 bio
= BIO_new(_httpBIOMethods());
2867 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)http
);
2869 conn
= SSL_new(context
);
2870 SSL_set_bio(conn
, bio
, bio
);
2872 if (SSL_connect(conn
) != 1)
2875 unsigned long error
; /* Error code */
2877 while ((error
= ERR_get_error()) != 0)
2878 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
2881 SSL_CTX_free(context
);
2885 http
->error
= WSAGetLastError();
2887 http
->error
= errno
;
2889 http
->status
= HTTP_ERROR
;
2891 return (HTTP_ERROR
);
2894 # elif defined(HAVE_GNUTLS)
2895 if ((conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
))) == NULL
)
2897 http
->error
= errno
;
2898 http
->status
= HTTP_ERROR
;
2903 credentials
= (gnutls_certificate_client_credentials
*)
2904 malloc(sizeof(gnutls_certificate_client_credentials
));
2905 if (credentials
== NULL
)
2909 http
->error
= errno
;
2910 http
->status
= HTTP_ERROR
;
2915 gnutls_certificate_allocate_credentials(credentials
);
2917 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2918 gnutls_set_default_priority(conn
->session
);
2919 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2920 gnutls_transport_set_ptr(conn
->session
, (gnutls_transport_ptr
)http
);
2921 gnutls_transport_set_pull_function(conn
->session
, _httpReadGNUTLS
);
2922 gnutls_transport_set_push_function(conn
->session
, _httpWriteGNUTLS
);
2924 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2926 http
->error
= errno
;
2927 http
->status
= HTTP_ERROR
;
2932 conn
->credentials
= credentials
;
2934 # elif defined(HAVE_CDSASSL)
2935 conn
= (http_tls_t
*)calloc(1, sizeof(http_tls_t
));
2940 if ((error
= SSLNewContext(false, &conn
->session
)))
2942 http
->error
= error
;
2943 http
->status
= HTTP_ERROR
;
2950 * Use a union to resolve warnings about int/pointer size mismatches...
2953 error
= SSLSetConnection(conn
->session
, http
);
2956 error
= SSLSetIOFuncs(conn
->session
, _httpReadCDSA
, _httpWriteCDSA
);
2959 error
= SSLSetAllowsExpiredCerts(conn
->session
, true);
2962 error
= SSLSetAllowsAnyRoot(conn
->session
, true);
2965 error
= SSLSetProtocolVersionEnabled(conn
->session
, kSSLProtocol2
, false);
2969 while ((error
= SSLHandshake(conn
->session
)) == errSSLWouldBlock
)
2975 http
->error
= error
;
2976 http
->status
= HTTP_ERROR
;
2978 SSLDisposeContext(conn
->session
);
2984 # endif /* HAVE_CDSASSL */
2989 #endif /* HAVE_SSL */
2994 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2998 http_shutdown_ssl(http_t
*http
) /* I - Connection to server */
3001 SSL_CTX
*context
; /* Context for encryption */
3002 SSL
*conn
; /* Connection for encryption */
3005 conn
= (SSL
*)(http
->tls
);
3006 context
= SSL_get_SSL_CTX(conn
);
3009 SSL_CTX_free(context
);
3012 # elif defined(HAVE_GNUTLS)
3013 http_tls_t
*conn
; /* Encryption session */
3014 gnutls_certificate_client_credentials
*credentials
;
3015 /* TLS credentials */
3018 conn
= (http_tls_t
*)(http
->tls
);
3019 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
3021 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
3022 gnutls_deinit(conn
->session
);
3023 gnutls_certificate_free_credentials(*credentials
);
3027 # elif defined(HAVE_CDSASSL)
3028 http_tls_t
*conn
; /* CDSA connection information */
3031 conn
= (http_tls_t
*)(http
->tls
);
3033 while (SSLClose(conn
->session
) == errSSLWouldBlock
)
3036 SSLDisposeContext(conn
->session
);
3038 if (conn
->certsArray
)
3039 CFRelease(conn
->certsArray
);
3042 # endif /* HAVE_LIBSSL */
3046 #endif /* HAVE_SSL */
3051 * 'http_upgrade()' - Force upgrade to TLS encryption.
3054 static int /* O - Status of connection */
3055 http_upgrade(http_t
*http
) /* I - Connection to server */
3057 int ret
; /* Return value */
3058 http_t myhttp
; /* Local copy of HTTP data */
3061 DEBUG_printf(("http_upgrade(%p)\n", http
));
3064 * Copy the HTTP data to a local variable so we can do the OPTIONS
3065 * request without interfering with the existing request data...
3068 memcpy(&myhttp
, http
, sizeof(myhttp
));
3071 * Send an OPTIONS request to the server, requiring SSL or TLS
3072 * encryption on the link...
3075 http
->field_authorization
= NULL
; /* Don't free the auth string */
3077 httpClearFields(http
);
3078 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
3079 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
3081 if ((ret
= httpOptions(http
, "*")) == 0)
3084 * Wait for the secure connection...
3087 while (httpUpdate(http
) == HTTP_CONTINUE
);
3093 * Restore the HTTP request data...
3096 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
3097 http
->data_encoding
= myhttp
.data_encoding
;
3098 http
->data_remaining
= myhttp
.data_remaining
;
3099 http
->_data_remaining
= myhttp
._data_remaining
;
3100 http
->expect
= myhttp
.expect
;
3101 http
->field_authorization
= myhttp
.field_authorization
;
3102 http
->digest_tries
= myhttp
.digest_tries
;
3105 * See if we actually went secure...
3111 * Server does not support HTTP upgrade...
3114 DEBUG_puts("Server does not support HTTP upgrade!");
3117 closesocket(http
->fd
);
3129 #endif /* HAVE_SSL */
3133 * 'http_write()' - Write a buffer to a HTTP connection.
3136 static int /* O - Number of bytes written */
3137 http_write(http_t
*http
, /* I - Connection to server */
3138 const char *buffer
, /* I - Buffer for data */
3139 int length
) /* I - Number of bytes to write */
3141 int tbytes
, /* Total bytes sent */
3142 bytes
; /* Bytes sent */
3151 bytes
= http_write_ssl(http
, buffer
, length
);
3153 #endif /* HAVE_SSL */
3154 bytes
= send(http
->fd
, buffer
, length
, 0);
3159 if (WSAGetLastError() != http
->error
)
3161 http
->error
= WSAGetLastError();
3167 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
3169 http
->error
= errno
;
3174 DEBUG_puts("http_write: error writing data...\n");
3185 http_debug_hex("http_write", buffer
- tbytes
, tbytes
);
3193 * 'http_write_chunk()' - Write a chunked buffer.
3196 static int /* O - Number bytes written */
3197 http_write_chunk(http_t
*http
, /* I - Connection to server */
3198 const char *buffer
, /* I - Buffer to write */
3199 int length
) /* I - Length of buffer */
3201 char header
[255]; /* Chunk header */
3202 int bytes
; /* Bytes written */
3204 DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n",
3205 http
, buffer
, length
));
3208 * Write the chunk header, data, and trailer.
3211 sprintf(header
, "%x\r\n", length
);
3212 if (http_write(http
, header
, (int)strlen(header
)) < 0)
3214 DEBUG_puts(" http_write of length failed!");
3218 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
3220 DEBUG_puts(" http_write of buffer failed!");
3224 if (http_write(http
, "\r\n", 2) < 0)
3226 DEBUG_puts(" http_write of CR LF failed!");
3236 * 'http_write_ssl()' - Write to a SSL/TLS connection.
3239 static int /* O - Bytes written */
3240 http_write_ssl(http_t
*http
, /* I - Connection to server */
3241 const char *buf
, /* I - Buffer holding data */
3242 int len
) /* I - Length of buffer */
3244 # if defined(HAVE_LIBSSL)
3245 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
3247 # elif defined(HAVE_GNUTLS)
3248 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
3249 # elif defined(HAVE_CDSASSL)
3250 int result
; /* Return value */
3251 OSStatus error
; /* Error info */
3252 size_t processed
; /* Number of bytes processed */
3255 error
= SSLWrite(((http_tls_t
*)http
->tls
)->session
, buf
, len
, &processed
);
3260 result
= (int)processed
;
3262 case errSSLClosedGraceful
:
3265 case errSSLWouldBlock
:
3267 result
= (int)processed
;
3281 # endif /* HAVE_LIBSSL */
3283 #endif /* HAVE_SSL */
3287 * End of "$Id: http.c 7850 2008-08-20 00:07:25Z mike $".