2 * "$Id: http.c,v 1.124 2003/08/29 23:56:52 mike Exp $"
4 * HTTP routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * httpInitialize() - Initialize the HTTP interface library and set the
29 * default HTTP proxy (if any).
30 * httpCheck() - Check to see if there is a pending response from
32 * httpClearCookie() - Clear the cookie value(s).
33 * httpClose() - Close an HTTP connection...
34 * httpConnect() - Connect to a HTTP server.
35 * httpConnectEncrypt() - Connect to a HTTP server using encryption.
36 * httpEncryption() - Set the required encryption on the link.
37 * httpReconnect() - Reconnect to a HTTP server...
38 * httpGetSubField() - Get a sub-field value.
39 * httpSetField() - Set the value of an HTTP header.
40 * httpDelete() - Send a DELETE request to the server.
41 * httpGet() - Send a GET request to the server.
42 * httpHead() - Send a HEAD request to the server.
43 * httpOptions() - Send an OPTIONS request to the server.
44 * httpPost() - Send a POST request to the server.
45 * httpPut() - Send a PUT request to the server.
46 * httpTrace() - Send an TRACE request to the server.
47 * httpFlush() - Flush data from a HTTP connection.
48 * httpRead() - Read data from a HTTP connection.
49 * httpSetCookie() - Set the cookie value(s)...
50 * httpWait() - Wait for data available on a connection.
51 * httpWrite() - Write data to a HTTP connection.
52 * httpGets() - Get a line of text from a HTTP connection.
53 * httpPrintf() - Print a formatted string to a HTTP connection.
54 * httpGetDateString() - Get a formatted date/time string from a time value.
55 * httpGetDateTime() - Get a time value from a formatted date/time string.
56 * httpUpdate() - Update the current HTTP state for incoming data.
57 * httpDecode64() - Base64-decode a string.
58 * httpEncode64() - Base64-encode a string.
59 * httpGetLength() - Get the amount of data remaining from the
60 * content-length or transfer-encoding fields.
61 * http_field() - Return the field index for a field name.
62 * http_send() - Send a request with all fields and the trailing
64 * http_wait() - Wait for data available on a connection.
65 * http_upgrade() - Force upgrade to TLS encryption.
66 * http_setup_ssl() - Set up SSL/TLS on a connection.
67 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
68 * http_read_ssl() - Read from a SSL/TLS connection.
69 * http_write_ssl() - Write to a SSL/TLS connection.
70 * CDSAReadFunc() - Read function for CDSA decryption code.
71 * CDSAWriteFunc() - Write function for CDSA encryption code.
75 * Include necessary headers...
78 #include "http-private.h"
93 # include <sys/time.h>
94 # include <sys/resource.h>
99 * Some operating systems have done away with the Fxxxx constants for
100 * the fcntl() call; this works around that "feature"...
104 # define FNONBLK O_NONBLOCK
105 #endif /* !FNONBLK */
112 static http_field_t
http_field(const char *name
);
113 static int http_send(http_t
*http
, http_state_t request
,
115 static int http_wait(http_t
*http
, int msec
);
117 static int http_upgrade(http_t
*http
);
118 static int http_setup_ssl(http_t
*http
);
119 static void http_shutdown_ssl(http_t
*http
);
120 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
121 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
123 static OSStatus
CDSAReadFunc(SSLConnectionRef connection
, void *data
, size_t *dataLength
);
124 static OSStatus
CDSAWriteFunc(SSLConnectionRef connection
, const void *data
, size_t *dataLength
);
125 # endif /* HAVE_CDSASSL */
126 #endif /* HAVE_SSL */
133 static const char * const http_fields
[] =
150 "If-Unmodified-since",
163 static const char * const days
[7] =
173 static const char * const months
[12] =
191 * 'httpInitialize()' - Initialize the HTTP interface library and set the
192 * default HTTP proxy (if any).
200 struct timeval curtime
; /* Current time in microseconds */
202 int i
; /* Looping var */
203 unsigned char data
[1024]; /* Seed data */
204 #endif /* HAVE_LIBSSL */
207 WSADATA winsockdata
; /* WinSock data */
208 static int initialized
= 0;/* Has WinSock been initialized? */
212 WSAStartup(MAKEWORD(1,1), &winsockdata
);
213 #elif defined(HAVE_SIGSET)
214 sigset(SIGPIPE
, SIG_IGN
);
215 #elif defined(HAVE_SIGACTION)
216 struct sigaction action
; /* POSIX sigaction data */
220 * Ignore SIGPIPE signals...
223 memset(&action
, 0, sizeof(action
));
224 action
.sa_handler
= SIG_IGN
;
225 sigaction(SIGPIPE
, &action
, NULL
);
227 signal(SIGPIPE
, SIG_IGN
);
231 gnutls_global_init();
232 #endif /* HAVE_GNUTLS */
235 SSL_load_error_strings();
239 * Using the current time is a dubious random seed, but on some systems
240 * it is the best we can do (on others, this seed isn't even used...)
245 gettimeofday(&curtime
, NULL
);
246 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
249 for (i
= 0; i
< sizeof(data
); i
++)
250 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
252 RAND_seed(&data
, sizeof(data
));
253 #endif /* HAVE_LIBSSL */
258 * 'httpCheck()' - Check to see if there is a pending response from the server.
261 int /* O - 0 = no data, 1 = data available */
262 httpCheck(http_t
*http
) /* I - HTTP connection */
264 return (httpWait(http
, 0));
269 * 'httpClearCookie()' - Clear the cookie value(s).
273 httpClearCookie(http_t
*http
) /* I - Connection */
287 * 'httpClose()' - Close an HTTP connection...
291 httpClose(http_t
*http
) /* I - Connection to close */
297 free(http
->input_set
);
304 http_shutdown_ssl(http
);
305 #endif /* HAVE_SSL */
308 closesocket(http
->fd
);
318 * 'httpConnect()' - Connect to a HTTP server.
321 http_t
* /* O - New HTTP connection */
322 httpConnect(const char *host
, /* I - Host to connect to */
323 int port
) /* I - Port number */
325 http_encryption_t encrypt
;/* Type of encryption to use */
329 * Set the default encryption status...
333 encrypt
= HTTP_ENCRYPT_ALWAYS
;
335 encrypt
= HTTP_ENCRYPT_IF_REQUESTED
;
337 return (httpConnectEncrypt(host
, port
, encrypt
));
342 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
345 http_t
* /* O - New HTTP connection */
346 httpConnectEncrypt(const char *host
, /* I - Host to connect to */
347 int port
, /* I - Port number */
348 http_encryption_t encrypt
)
349 /* I - Type of encryption to use */
351 int i
; /* Looping var */
352 http_t
*http
; /* New HTTP connection */
353 struct hostent
*hostaddr
; /* Host address data */
365 if ((hostaddr
= httpGetHostByName(host
)) == NULL
)
368 * This hack to make users that don't have a localhost entry in
369 * their hosts file or DNS happy...
372 if (strcasecmp(host
, "localhost") != 0)
374 else if ((hostaddr
= httpGetHostByName("127.0.0.1")) == NULL
)
379 * Verify that it is an IPv4 address (IPv6 support will come in CUPS 1.2...)
382 if (hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4)
386 * Allocate memory for the structure...
389 http
= calloc(sizeof(http_t
), 1);
393 http
->version
= HTTP_1_1
;
395 http
->activity
= time(NULL
);
399 * Copy the hostname and port and then "reconnect"...
402 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
403 http
->hostaddr
.sin_family
= hostaddr
->h_addrtype
;
405 http
->hostaddr
.sin_port
= htons((u_short
)port
);
407 http
->hostaddr
.sin_port
= htons(port
);
411 * Set the encryption status...
414 if (port
== 443) /* Always use encryption for https */
415 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
417 http
->encryption
= encrypt
;
420 * Loop through the addresses we have until one of them connects...
423 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
425 for (i
= 0; hostaddr
->h_addr_list
[i
]; i
++)
428 * Load the address...
431 memcpy((char *)&(http
->hostaddr
.sin_addr
), hostaddr
->h_addr_list
[i
],
435 * Connect to the remote system...
438 if (!httpReconnect(http
))
443 * Could not connect to any known address - bail out!
452 * 'httpEncryption()' - Set the required encryption on the link.
455 int /* O - -1 on error, 0 on success */
456 httpEncryption(http_t
*http
, /* I - HTTP data */
457 http_encryption_t e
) /* I - New encryption preference */
463 http
->encryption
= e
;
465 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
466 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
467 return (httpReconnect(http
));
468 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
469 return (http_upgrade(http
));
473 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
477 #endif /* HAVE_SSL */
482 * 'httpReconnect()' - Reconnect to a HTTP server...
485 int /* O - 0 on success, non-zero on failure */
486 httpReconnect(http_t
*http
) /* I - HTTP data */
488 int val
; /* Socket option value */
492 http_shutdown_ssl(http
);
493 #endif /* HAVE_SSL */
496 * Close any previously open socket...
501 closesocket(http
->fd
);
507 * Create the socket and set options to allow reuse.
510 if ((http
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
513 http
->error
= WSAGetLastError();
517 http
->status
= HTTP_ERROR
;
522 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
523 * other processes... */
524 #endif /* FD_CLOEXEC */
527 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
531 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
532 #endif /* SO_REUSEPORT */
535 * Using TCP_NODELAY improves responsiveness, especially on systems
536 * with a slow loopback interface... Since we write large buffers
537 * when sending print files and requests, there shouldn't be any
538 * performance penalty for this...
542 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
545 * Connect to the server...
548 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
549 sizeof(http
->hostaddr
)) < 0)
552 http
->error
= WSAGetLastError();
556 http
->status
= HTTP_ERROR
;
559 closesocket(http
->fd
);
570 http
->status
= HTTP_CONTINUE
;
573 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
576 * Always do encryption via SSL.
579 if (http_setup_ssl(http
) != 0)
582 closesocket(http
->fd
);
590 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
591 return (http_upgrade(http
));
592 #endif /* HAVE_SSL */
599 * 'httpGetSubField()' - Get a sub-field value.
602 char * /* O - Value or NULL */
603 httpGetSubField(http_t
*http
, /* I - HTTP data */
604 http_field_t field
, /* I - Field index */
605 const char *name
, /* I - Name of sub-field */
606 char *value
) /* O - Value string */
608 const char *fptr
; /* Pointer into field */
609 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
610 *ptr
; /* Pointer into string buffer */
614 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
615 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
616 name
== NULL
|| value
== NULL
)
619 DEBUG_printf(("httpGetSubField(%p, %d, \"%s\", %p)\n",
620 http
, field
, name
, value
));
622 for (fptr
= http
->fields
[field
]; *fptr
;)
625 * Skip leading whitespace...
628 while (isspace(*fptr
))
638 * Get the sub-field name...
642 *fptr
&& *fptr
!= '=' && !isspace(*fptr
) && ptr
< (temp
+ sizeof(temp
) - 1);
647 DEBUG_printf(("name = \"%s\"\n", temp
));
650 * Skip trailing chars up to the '='...
653 while (isspace(*fptr
))
663 * Skip = and leading whitespace...
668 while (isspace(*fptr
))
674 * Read quoted string...
677 for (ptr
= value
, fptr
++;
678 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
683 while (*fptr
&& *fptr
!= '\"')
692 * Read unquoted string...
696 *fptr
&& !isspace(*fptr
) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
701 while (*fptr
&& !isspace(*fptr
) && *fptr
!= ',')
705 DEBUG_printf(("value = \"%s\"\n", value
));
708 * See if this is the one...
711 if (strcmp(name
, temp
) == 0)
722 * 'httpSetField()' - Set the value of an HTTP header.
726 httpSetField(http_t
*http
, /* I - HTTP data */
727 http_field_t field
, /* I - Field index */
728 const char *value
) /* I - Value */
731 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
732 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
736 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
741 * 'httpDelete()' - Send a DELETE request to the server.
744 int /* O - Status of call (0 = success) */
745 httpDelete(http_t
*http
, /* I - HTTP data */
746 const char *uri
) /* I - URI to delete */
748 return (http_send(http
, HTTP_DELETE
, uri
));
753 * 'httpGet()' - Send a GET request to the server.
756 int /* O - Status of call (0 = success) */
757 httpGet(http_t
*http
, /* I - HTTP data */
758 const char *uri
) /* I - URI to get */
760 return (http_send(http
, HTTP_GET
, uri
));
765 * 'httpHead()' - Send a HEAD request to the server.
768 int /* O - Status of call (0 = success) */
769 httpHead(http_t
*http
, /* I - HTTP data */
770 const char *uri
) /* I - URI for head */
772 return (http_send(http
, HTTP_HEAD
, uri
));
777 * 'httpOptions()' - Send an OPTIONS request to the server.
780 int /* O - Status of call (0 = success) */
781 httpOptions(http_t
*http
, /* I - HTTP data */
782 const char *uri
) /* I - URI for options */
784 return (http_send(http
, HTTP_OPTIONS
, uri
));
789 * 'httpPost()' - Send a POST request to the server.
792 int /* O - Status of call (0 = success) */
793 httpPost(http_t
*http
, /* I - HTTP data */
794 const char *uri
) /* I - URI for post */
798 return (http_send(http
, HTTP_POST
, uri
));
803 * 'httpPut()' - Send a PUT request to the server.
806 int /* O - Status of call (0 = success) */
807 httpPut(http_t
*http
, /* I - HTTP data */
808 const char *uri
) /* I - URI to put */
812 return (http_send(http
, HTTP_PUT
, uri
));
817 * 'httpTrace()' - Send an TRACE request to the server.
820 int /* O - Status of call (0 = success) */
821 httpTrace(http_t
*http
, /* I - HTTP data */
822 const char *uri
) /* I - URI for trace */
824 return (http_send(http
, HTTP_TRACE
, uri
));
829 * 'httpFlush()' - Flush data from a HTTP connection.
833 httpFlush(http_t
*http
) /* I - HTTP data */
835 char buffer
[8192]; /* Junk buffer */
838 if (http
->state
!= HTTP_WAITING
)
840 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
846 * 'httpRead()' - Read data from a HTTP connection.
849 int /* O - Number of bytes read */
850 httpRead(http_t
*http
, /* I - HTTP data */
851 char *buffer
, /* I - Buffer for data */
852 int length
) /* I - Maximum number of bytes */
854 int bytes
; /* Bytes read */
855 char len
[32]; /* Length string */
858 DEBUG_printf(("httpRead(%p, %p, %d)\n", http
, buffer
, length
));
860 if (http
== NULL
|| buffer
== NULL
)
863 http
->activity
= time(NULL
);
868 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
869 http
->data_remaining
<= 0)
871 DEBUG_puts("httpRead: Getting chunk length...");
873 if (httpGets(len
, sizeof(len
), http
) == NULL
)
875 DEBUG_puts("httpRead: Could not get length!");
879 http
->data_remaining
= strtol(len
, NULL
, 16);
880 if (http
->data_remaining
< 0)
882 DEBUG_puts("httpRead: Negative chunk length!");
887 DEBUG_printf(("httpRead: data_remaining = %d\n", http
->data_remaining
));
889 if (http
->data_remaining
<= 0)
892 * A zero-length chunk ends a transfer; unless we are reading POST
896 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
897 httpGets(len
, sizeof(len
), http
);
899 if (http
->state
== HTTP_POST_RECV
)
902 http
->state
= HTTP_WAITING
;
906 else if (length
> http
->data_remaining
)
907 length
= http
->data_remaining
;
909 if (http
->used
== 0 && length
<= 256)
912 * Buffer small reads for better performance...
915 if (!http
->blocking
&& !httpWait(http
, 1000))
918 if (http
->data_remaining
> sizeof(http
->buffer
))
919 bytes
= sizeof(http
->buffer
);
921 bytes
= http
->data_remaining
;
925 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
927 #endif /* HAVE_SSL */
929 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
932 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
934 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
943 http
->error
= WSAGetLastError();
962 if (length
> http
->used
)
967 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
969 memcpy(buffer
, http
->buffer
, length
);
970 http
->used
-= length
;
973 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
978 if (!http
->blocking
&& !httpWait(http
, 1000))
981 bytes
= http_read_ssl(http
, buffer
, length
);
983 #endif /* HAVE_SSL */
986 if (!http
->blocking
&& !httpWait(http
, 1000))
989 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
990 bytes
= recv(http
->fd
, buffer
, length
, 0);
991 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
995 http
->data_remaining
-= bytes
;
999 http
->error
= WSAGetLastError();
1004 http
->error
= errno
;
1009 http
->error
= EPIPE
;
1013 if (http
->data_remaining
== 0)
1015 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1016 httpGets(len
, sizeof(len
), http
);
1018 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1020 if (http
->state
== HTTP_POST_RECV
)
1023 http
->state
= HTTP_WAITING
;
1030 printf("httpRead: Read %d bytes:\n", bytes
);
1031 for (i
= 0; i
< bytes
; i
+= 16)
1035 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1036 printf(" %02X", buffer
[i
+ j
] & 255);
1045 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1047 ch
= buffer
[i
+ j
] & 255;
1049 if (ch
< ' ' || ch
== 127)
1064 * 'httpSetCookie()' - Set the cookie value(s)...
1068 httpSetCookie(http_t
*http
, /* I - Connection */
1069 const char *cookie
) /* I - Cookie string */
1078 http
->cookie
= strdup(cookie
);
1080 http
->cookie
= NULL
;
1085 * 'httpWait()' - Wait for data available on a connection.
1088 int /* O - 1 if data is available, 0 otherwise */
1089 httpWait(http_t
*http
, /* I - HTTP data */
1090 int msec
) /* I - Milliseconds to wait */
1093 * First see if there is data in the buffer...
1103 * If not, check the SSL/TLS buffers and do a select() on the connection...
1106 return (http_wait(http
, msec
));
1111 * 'httpWrite()' - Write data to a HTTP connection.
1114 int /* O - Number of bytes written */
1115 httpWrite(http_t
*http
, /* I - HTTP data */
1116 const char *buffer
, /* I - Buffer for data */
1117 int length
) /* I - Number of bytes to write */
1119 int tbytes
, /* Total bytes sent */
1120 bytes
; /* Bytes sent */
1123 if (http
== NULL
|| buffer
== NULL
)
1126 http
->activity
= time(NULL
);
1128 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1130 if (httpPrintf(http
, "%x\r\n", length
) < 0)
1136 * A zero-length chunk ends a transfer; unless we are sending POST
1137 * or PUT data, go idle...
1140 DEBUG_puts("httpWrite: changing states...");
1142 if (http
->state
== HTTP_POST_RECV
)
1144 else if (http
->state
== HTTP_PUT_RECV
)
1145 http
->state
= HTTP_STATUS
;
1147 http
->state
= HTTP_WAITING
;
1149 if (httpPrintf(http
, "\r\n") < 0)
1162 bytes
= http_write_ssl(http
, buffer
, length
);
1164 #endif /* HAVE_SSL */
1165 bytes
= send(http
->fd
, buffer
, length
, 0);
1170 if (WSAGetLastError() != http
->error
)
1172 http
->error
= WSAGetLastError();
1178 else if (errno
!= http
->error
)
1180 http
->error
= errno
;
1185 DEBUG_puts("httpWrite: error writing data...\n");
1193 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1194 http
->data_remaining
-= bytes
;
1197 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1198 if (httpPrintf(http
, "\r\n") < 0)
1201 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1204 * Finished with the transfer; unless we are sending POST or PUT
1208 DEBUG_puts("httpWrite: changing states...");
1210 if (http
->state
== HTTP_POST_RECV
)
1212 else if (http
->state
== HTTP_PUT_RECV
)
1213 http
->state
= HTTP_STATUS
;
1215 http
->state
= HTTP_WAITING
;
1221 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1222 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1226 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1227 printf(" %02X", buffer
[i
+ j
] & 255);
1236 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1238 ch
= buffer
[i
+ j
] & 255;
1240 if (ch
< ' ' || ch
== 127)
1254 * 'httpGets()' - Get a line of text from a HTTP connection.
1257 char * /* O - Line or NULL */
1258 httpGets(char *line
, /* I - Line to read into */
1259 int length
, /* I - Max length of buffer */
1260 http_t
*http
) /* I - HTTP data */
1262 char *lineptr
, /* Pointer into line */
1263 *bufptr
, /* Pointer into input buffer */
1264 *bufend
; /* Pointer to end of buffer */
1265 int bytes
; /* Number of bytes read */
1268 DEBUG_printf(("httpGets(%p, %d, %p)\n", line
, length
, http
));
1270 if (http
== NULL
|| line
== NULL
)
1274 * Pre-scan the buffer and see if there is a newline in there...
1285 bufptr
= http
->buffer
;
1286 bufend
= http
->buffer
+ http
->used
;
1288 while (bufptr
< bufend
)
1289 if (*bufptr
== 0x0a)
1294 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1297 * No newline; see if there is more data to be read...
1300 if (!http
->blocking
&& !http_wait(http
, 1000))
1305 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1307 #endif /* HAVE_SSL */
1308 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1313 * Nope, can't get a line this time...
1317 if (WSAGetLastError() != http
->error
)
1319 http
->error
= WSAGetLastError();
1323 DEBUG_printf(("httpGets(): recv() error %d!\n", WSAGetLastError()));
1327 else if (errno
!= http
->error
)
1329 http
->error
= errno
;
1333 DEBUG_printf(("httpGets(): recv() error %d!\n", errno
));
1338 else if (bytes
== 0)
1340 http
->error
= EPIPE
;
1346 * Yup, update the amount used and the end pointer...
1349 http
->used
+= bytes
;
1354 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1356 http
->activity
= time(NULL
);
1359 * Read a line from the buffer...
1363 bufptr
= http
->buffer
;
1367 while (bufptr
< bufend
&& bytes
< length
)
1371 if (*bufptr
== 0x0a)
1376 else if (*bufptr
== 0x0d)
1379 *lineptr
++ = *bufptr
++;
1386 http
->used
-= bytes
;
1388 memmove(http
->buffer
, bufptr
, http
->used
);
1390 DEBUG_printf(("httpGets(): Returning \"%s\"\n", line
));
1394 DEBUG_puts("httpGets(): No new line available!");
1401 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1404 int /* O - Number of bytes written */
1405 httpPrintf(http_t
*http
, /* I - HTTP data */
1406 const char *format
, /* I - printf-style format string */
1407 ...) /* I - Additional args as needed */
1409 int bytes
, /* Number of bytes to write */
1410 nbytes
, /* Number of bytes written */
1411 tbytes
; /* Number of bytes all together */
1412 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1413 *bufptr
; /* Pointer into buffer */
1414 va_list ap
; /* Variable argument pointer */
1417 va_start(ap
, format
);
1418 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1421 DEBUG_printf(("httpPrintf: %s", buf
));
1423 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1427 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1429 #endif /* HAVE_SSL */
1430 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1437 if (WSAGetLastError() != http
->error
)
1439 http
->error
= WSAGetLastError();
1445 else if (errno
!= http
->error
)
1447 http
->error
= errno
;
1461 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1464 const char * /* O - Date/time string */
1465 httpGetDateString(time_t t
) /* I - UNIX time */
1468 static char datetime
[256];
1472 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1473 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1474 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1481 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1484 time_t /* O - UNIX time */
1485 httpGetDateTime(const char *s
) /* I - Date/time string */
1487 int i
; /* Looping var */
1488 struct tm tdate
; /* Time/date structure */
1489 char mon
[16]; /* Abbreviated month name */
1490 int day
, year
; /* Day of month and year */
1491 int hour
, min
, sec
; /* Time */
1494 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1497 for (i
= 0; i
< 12; i
++)
1498 if (strcasecmp(mon
, months
[i
]) == 0)
1505 tdate
.tm_mday
= day
;
1506 tdate
.tm_year
= year
- 1900;
1507 tdate
.tm_hour
= hour
;
1512 return (mktime(&tdate
));
1517 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1520 http_status_t
/* O - HTTP status */
1521 httpUpdate(http_t
*http
) /* I - HTTP data */
1523 char line
[1024], /* Line from connection... */
1524 *value
; /* Pointer to value on line */
1525 http_field_t field
; /* Field index */
1526 int major
, minor
; /* HTTP version numbers */
1527 http_status_t status
; /* Authorization status */
1530 DEBUG_printf(("httpUpdate(%p)\n", http
));
1533 * If we haven't issued any commands, then there is nothing to "update"...
1536 if (http
->state
== HTTP_WAITING
)
1537 return (HTTP_CONTINUE
);
1540 * Grab all of the lines we can from the connection...
1543 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1547 if (line
[0] == '\0')
1550 * Blank line means the start of the data section (if any). Return
1551 * the result code, too...
1553 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1554 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1558 if (http
->status
== HTTP_CONTINUE
)
1559 return (http
->status
);
1562 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1564 if (http_setup_ssl(http
) != 0)
1567 closesocket(http
->fd
);
1572 return (HTTP_ERROR
);
1575 return (HTTP_CONTINUE
);
1577 #endif /* HAVE_SSL */
1579 httpGetLength(http
);
1581 switch (http
->state
)
1585 case HTTP_POST_RECV
:
1588 case HTTP_POST_SEND
:
1592 http
->state
= HTTP_WAITING
;
1596 return (http
->status
);
1598 else if (strncmp(line
, "HTTP/", 5) == 0)
1601 * Got the beginning of a response...
1604 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, (int *)&status
) != 3)
1605 return (HTTP_ERROR
);
1607 http
->version
= (http_version_t
)(major
* 100 + minor
);
1608 http
->status
= status
;
1610 else if ((value
= strchr(line
, ':')) != NULL
)
1617 while (isspace(*value
))
1621 * Be tolerants of servers that send unknown attribute fields...
1624 if (!strcasecmp(line
, "expect"))
1627 * "Expect: 100-continue" or similar...
1630 http
->expect
= (http_status_t
)atoi(value
);
1632 else if (!strcasecmp(line
, "cookie"))
1635 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1638 httpSetCookie(http
, value
);
1640 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1642 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1646 httpSetField(http
, field
, value
);
1650 http
->status
= HTTP_ERROR
;
1651 return (HTTP_ERROR
);
1656 * See if there was an error...
1661 http
->status
= HTTP_ERROR
;
1662 return (HTTP_ERROR
);
1666 * If we haven't already returned, then there is nothing new...
1669 return (HTTP_CONTINUE
);
1674 * 'httpDecode64()' - Base64-decode a string.
1677 char * /* O - Decoded string */
1678 httpDecode64(char *out
, /* I - String to write to */
1679 const char *in
) /* I - String to read from */
1681 int pos
, /* Bit position */
1682 base64
; /* Value of this character */
1683 char *outptr
; /* Output pointer */
1686 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1689 * Decode this character into a number from 0 to 63...
1692 if (*in
>= 'A' && *in
<= 'Z')
1694 else if (*in
>= 'a' && *in
<= 'z')
1695 base64
= *in
- 'a' + 26;
1696 else if (*in
>= '0' && *in
<= '9')
1697 base64
= *in
- '0' + 52;
1698 else if (*in
== '+')
1700 else if (*in
== '/')
1702 else if (*in
== '=')
1708 * Store the result in the appropriate chars...
1714 *outptr
= base64
<< 2;
1718 *outptr
++ |= (base64
>> 4) & 3;
1719 *outptr
= (base64
<< 4) & 255;
1723 *outptr
++ |= (base64
>> 2) & 15;
1724 *outptr
= (base64
<< 6) & 255;
1728 *outptr
++ |= base64
;
1737 * Return the decoded string...
1745 * 'httpEncode64()' - Base64-encode a string.
1748 char * /* O - Encoded string */
1749 httpEncode64(char *out
, /* I - String to write to */
1750 const char *in
) /* I - String to read from */
1752 char *outptr
; /* Output pointer */
1753 static const char base64
[] = /* Base64 characters... */
1755 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1756 "abcdefghijklmnopqrstuvwxyz"
1762 for (outptr
= out
; *in
!= '\0'; in
++)
1765 * Encode the up to 3 characters as 4 Base64 numbers...
1768 *outptr
++ = base64
[in
[0] >> 2];
1769 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1778 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1784 *outptr
++ = base64
[in
[0] & 63];
1791 * Return the encoded string...
1799 * 'httpGetLength()' - Get the amount of data remaining from the
1800 * content-length or transfer-encoding fields.
1803 int /* O - Content length */
1804 httpGetLength(http_t
*http
) /* I - HTTP data */
1806 DEBUG_printf(("httpGetLength(%p), state = %d\n", http
, http
->state
));
1808 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1810 DEBUG_puts("httpGetLength: chunked request!");
1812 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1813 http
->data_remaining
= 0;
1817 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1820 * The following is a hack for HTTP servers that don't send a
1821 * content-length or transfer-encoding field...
1823 * If there is no content-length then the connection must close
1824 * after the transfer is complete...
1827 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1828 http
->data_remaining
= 2147483647;
1830 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1832 DEBUG_printf(("httpGetLength: content_length = %d\n", http
->data_remaining
));
1835 return (http
->data_remaining
);
1840 * 'http_field()' - Return the field index for a field name.
1843 static http_field_t
/* O - Field index */
1844 http_field(const char *name
) /* I - String name */
1846 int i
; /* Looping var */
1849 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1850 if (strcasecmp(name
, http_fields
[i
]) == 0)
1851 return ((http_field_t
)i
);
1853 return (HTTP_FIELD_UNKNOWN
);
1858 * 'http_send()' - Send a request with all fields and the trailing blank line.
1861 static int /* O - 0 on success, non-zero on error */
1862 http_send(http_t
*http
, /* I - HTTP data */
1863 http_state_t request
, /* I - Request code */
1864 const char *uri
) /* I - URI */
1866 int i
; /* Looping var */
1867 char *ptr
, /* Pointer in buffer */
1868 buf
[1024]; /* Encoded URI buffer */
1869 static const char * const codes
[] =
1870 { /* Request code strings */
1885 static const char hex
[] = "0123456789ABCDEF";
1889 if (http
== NULL
|| uri
== NULL
)
1893 * Encode the URI as needed...
1896 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1897 if (*uri
<= ' ' || *uri
>= 127)
1899 if (ptr
< (buf
+ sizeof(buf
) - 1))
1901 if (ptr
< (buf
+ sizeof(buf
) - 1))
1902 *ptr
++ = hex
[(*uri
>> 4) & 15];
1903 if (ptr
< (buf
+ sizeof(buf
) - 1))
1904 *ptr
++ = hex
[*uri
& 15];
1912 * See if we had an error the last time around; if so, reconnect...
1915 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1916 httpReconnect(http
);
1919 * Send the request header...
1922 http
->state
= request
;
1923 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1926 http
->status
= HTTP_CONTINUE
;
1929 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1931 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1932 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1934 #endif /* HAVE_SSL */
1936 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1938 http
->status
= HTTP_ERROR
;
1942 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1943 if (http
->fields
[i
][0] != '\0')
1945 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1947 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1949 http
->status
= HTTP_ERROR
;
1954 if (httpPrintf(http
, "\r\n") < 1)
1956 http
->status
= HTTP_ERROR
;
1960 httpClearFields(http
);
1967 * 'http_wait()' - Wait for data available on a connection.
1970 static int /* O - 1 if data is available, 0 otherwise */
1971 http_wait(http_t
*http
, /* I - HTTP data */
1972 int msec
) /* I - Milliseconds to wait */
1975 struct rlimit limit
; /* Runtime limit */
1977 struct timeval timeout
; /* Timeout */
1978 int nfds
; /* Result from select() */
1982 * Check the SSL/TLS buffers for data first...
1989 if (SSL_pending((SSL
*)(http
->tls
)))
1991 # elif defined(HAVE_GNUTLS)
1992 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
1994 # elif defined(HAVE_CDSASSL)
1995 size_t bytes
; /* Bytes that are available */
1997 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
1999 # endif /* HAVE_LIBSSL */
2001 #endif /* HAVE_SSL */
2004 * Then try doing a select() to poll the socket...
2007 if (!http
->input_set
)
2011 * Windows has a fixed-size select() structure, different (surprise,
2012 * surprise!) from all UNIX implementations. Just allocate this
2013 * fixed structure...
2016 http
->input_set
= calloc(1, sizeof(fd_set
));
2019 * Allocate the select() input set based upon the max number of file
2020 * descriptors available for this process...
2023 getrlimit(RLIMIT_NOFILE
, &limit
);
2025 http
->input_set
= calloc(1, (limit
.rlim_cur
+ 31) / 32);
2028 if (!http
->input_set
)
2032 FD_SET(http
->fd
, http
->input_set
);
2036 timeout
.tv_sec
= msec
/ 1000;
2037 timeout
.tv_usec
= (msec
% 1000) * 1000;
2039 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2042 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2044 FD_CLR(http
->fd
, http
->input_set
);
2052 * 'http_upgrade()' - Force upgrade to TLS encryption.
2055 static int /* O - Status of connection */
2056 http_upgrade(http_t
*http
) /* I - HTTP data */
2058 int ret
; /* Return value */
2059 http_t myhttp
; /* Local copy of HTTP data */
2062 DEBUG_printf(("http_upgrade(%p)\n", http
));
2065 * Copy the HTTP data to a local variable so we can do the OPTIONS
2066 * request without interfering with the existing request data...
2069 memcpy(&myhttp
, http
, sizeof(myhttp
));
2072 * Send an OPTIONS request to the server, requiring SSL or TLS
2073 * encryption on the link...
2076 httpClearFields(&myhttp
);
2077 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2078 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2080 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2083 * Wait for the secure connection...
2086 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2092 * Copy the HTTP data back over, if any...
2095 http
->fd
= myhttp
.fd
;
2096 http
->error
= myhttp
.error
;
2097 http
->activity
= myhttp
.activity
;
2098 http
->status
= myhttp
.status
;
2099 http
->version
= myhttp
.version
;
2100 http
->keep_alive
= myhttp
.keep_alive
;
2101 http
->used
= myhttp
.used
;
2104 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2106 http
->auth_type
= myhttp
.auth_type
;
2107 http
->nonce_count
= myhttp
.nonce_count
;
2109 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2111 http
->tls
= myhttp
.tls
;
2112 http
->encryption
= myhttp
.encryption
;
2115 * See if we actually went secure...
2121 * Server does not support HTTP upgrade...
2124 DEBUG_puts("Server does not support HTTP upgrade!");
2127 closesocket(http
->fd
);
2142 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2145 static int /* O - Status of connection */
2146 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2149 SSL_CTX
*context
; /* Context for encryption */
2150 SSL
*conn
; /* Connection for encryption */
2151 # elif defined(HAVE_GNUTLS)
2152 http_tls_t
*conn
; /* TLS session object */
2153 gnutls_certificate_client_credentials
*credentials
;
2154 /* TLS credentials */
2155 # elif defined(HAVE_CDSASSL)
2156 SSLContextRef conn
; /* Context for encryption */
2157 OSStatus error
; /* Error info */
2158 # endif /* HAVE_LIBSSL */
2162 context
= SSL_CTX_new(SSLv23_client_method());
2163 conn
= SSL_new(context
);
2165 SSL_set_fd(conn
, http
->fd
);
2166 if (SSL_connect(conn
) != 1)
2168 SSL_CTX_free(context
);
2172 http
->error
= WSAGetLastError();
2174 http
->error
= errno
;
2176 http
->status
= HTTP_ERROR
;
2178 return (HTTP_ERROR
);
2181 # elif defined(HAVE_GNUTLS)
2182 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2186 http
->error
= errno
;
2187 http
->status
= HTTP_ERROR
;
2192 credentials
= (gnutls_certificate_client_credentials
*)
2193 malloc(sizeof(gnutls_certificate_client_credentials
));
2194 if (credentials
== NULL
)
2198 http
->error
= errno
;
2199 http
->status
= HTTP_ERROR
;
2204 gnutls_certificate_allocate_credentials(credentials
);
2206 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2207 gnutls_set_default_priority(conn
->session
);
2208 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2209 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2211 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2213 http
->error
= errno
;
2214 http
->status
= HTTP_ERROR
;
2219 conn
->credentials
= credentials
;
2221 # elif defined(HAVE_CDSASSL)
2222 error
= SSLNewContext(false, &conn
);
2225 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2228 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2231 error
= SSLSetAllowsExpiredCerts(conn
, true);
2234 error
= SSLSetAllowsAnyRoot(conn
, true);
2237 error
= SSLHandshake(conn
);
2241 http
->error
= error
;
2242 http
->status
= HTTP_ERROR
;
2244 SSLDisposeContext(conn
);
2250 # endif /* HAVE_CDSASSL */
2258 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2262 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2265 SSL_CTX
*context
; /* Context for encryption */
2266 SSL
*conn
; /* Connection for encryption */
2269 conn
= (SSL
*)(http
->tls
);
2270 context
= SSL_get_SSL_CTX(conn
);
2273 SSL_CTX_free(context
);
2276 # elif defined(HAVE_GNUTLS)
2277 http_tls_t
*conn
; /* Encryption session */
2278 gnutls_certificate_client_credentials
*credentials
;
2279 /* TLS credentials */
2282 conn
= (http_tls_t
*)(http
->tls
);
2283 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2285 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2286 gnutls_deinit(conn
->session
);
2287 gnutls_certificate_free_credentials(*credentials
);
2291 # elif defined(HAVE_CDSASSL)
2292 SSLClose((SSLContextRef
)http
->tls
);
2293 SSLDisposeContext((SSLContextRef
)http
->tls
);
2294 # endif /* HAVE_LIBSSL */
2301 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2304 static int /* O - Bytes read */
2305 http_read_ssl(http_t
*http
, /* I - HTTP data */
2306 char *buf
, /* I - Buffer to store data */
2307 int len
) /* I - Length of buffer */
2309 # if defined(HAVE_LIBSSL)
2310 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2312 # elif defined(HAVE_GNUTLS)
2313 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2315 # elif defined(HAVE_CDSASSL)
2316 OSStatus error
; /* Error info */
2317 size_t processed
; /* Number of bytes processed */
2320 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2326 http
->error
= error
;
2330 # endif /* HAVE_LIBSSL */
2335 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2338 static int /* O - Bytes written */
2339 http_write_ssl(http_t
*http
, /* I - HTTP data */
2340 const char *buf
, /* I - Buffer holding data */
2341 int len
) /* I - Length of buffer */
2343 # if defined(HAVE_LIBSSL)
2344 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2346 # elif defined(HAVE_GNUTLS)
2347 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2348 # elif defined(HAVE_CDSASSL)
2349 OSStatus error
; /* Error info */
2350 size_t processed
; /* Number of bytes processed */
2353 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2359 http
->error
= error
;
2362 # endif /* HAVE_LIBSSL */
2366 # if defined(HAVE_CDSASSL)
2368 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2371 static OSStatus
/* O - -1 on error, 0 on success */
2372 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2373 void *data
, /* I - Data buffer */
2374 size_t *dataLength
) /* IO - Number of bytes */
2376 ssize_t bytes
; /* Number of bytes read */
2379 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2382 *dataLength
= bytes
;
2391 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2394 static OSStatus
/* O - -1 on error, 0 on success */
2395 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2396 const void *data
, /* I - Data buffer */
2397 size_t *dataLength
) /* IO - Number of bytes */
2402 bytes
= write((int)connection
, data
, *dataLength
);
2405 *dataLength
= bytes
;
2411 # endif /* HAVE_CDSASSL */
2412 #endif /* HAVE_SSL */
2416 * End of "$Id: http.c,v 1.124 2003/08/29 23:56:52 mike Exp $".