2 * "$Id: http.c,v 1.82.2.32 2003/05/09 16:06:43 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 * httpSetField() - Set the value of an HTTP header.
39 * httpDelete() - Send a DELETE request to the server.
40 * httpGet() - Send a GET request to the server.
41 * httpHead() - Send a HEAD request to the server.
42 * httpOptions() - Send an OPTIONS request to the server.
43 * httpPost() - Send a POST request to the server.
44 * httpPut() - Send a PUT request to the server.
45 * httpTrace() - Send an TRACE request to the server.
46 * httpFlush() - Flush data from a HTTP connection.
47 * httpRead() - Read data from a HTTP connection.
48 * httpSetCookie() - Set the cookie value(s)...
49 * httpWait() - Wait for data available on a connection.
50 * httpWrite() - Write data to a HTTP connection.
51 * httpGets() - Get a line of text from a HTTP connection.
52 * httpPrintf() - Print a formatted string to a HTTP connection.
53 * httpStatus() - Return a short string describing a HTTP status code.
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_upgrade() - Force upgrade to TLS encryption.
65 * http_setup_ssl() - Set up SSL/TLS on a connection.
66 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
67 * http_read_ssl() - Read from a SSL/TLS connection.
68 * http_write_ssl() - Write to a SSL/TLS connection.
69 * CDSAReadFunc() - Read function for CDSA decryption code.
70 * CDSAWriteFunc() - Write function for CDSA encryption code.
74 * Include necessary headers...
85 #include "http-private.h"
91 # include <sys/time.h>
92 # include <sys/resource.h>
97 * Some operating systems have done away with the Fxxxx constants for
98 * the fcntl() call; this works around that "feature"...
102 # define FNONBLK O_NONBLOCK
103 #endif /* !FNONBLK */
110 static http_field_t
http_field(const char *name
);
111 static int http_send(http_t
*http
, http_state_t request
,
114 static int http_upgrade(http_t
*http
);
115 static int http_setup_ssl(http_t
*http
);
116 static void http_shutdown_ssl(http_t
*http
);
117 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
118 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
120 static OSStatus
CDSAReadFunc(SSLConnectionRef connection
, void *data
, size_t *dataLength
);
121 static OSStatus
CDSAWriteFunc(SSLConnectionRef connection
, const void *data
, size_t *dataLength
);
122 # endif /* HAVE_CDSASSL */
123 #endif /* HAVE_SSL */
130 static const char * const http_fields
[] =
147 "If-Unmodified-since",
160 static const char * const days
[7] =
170 static const char * const months
[12] =
188 * 'httpInitialize()' - Initialize the HTTP interface library and set the
189 * default HTTP proxy (if any).
196 struct timeval curtime
; /* Current time in microseconds */
197 int i
; /* Looping var */
198 unsigned char data
[1024]; /* Seed data */
199 #endif /* HAVE_LIBSSL */
202 WSADATA winsockdata
; /* WinSock data */
203 static int initialized
= 0;/* Has WinSock been initialized? */
207 WSAStartup(MAKEWORD(1,1), &winsockdata
);
208 #elif defined(HAVE_SIGSET)
209 sigset(SIGPIPE
, SIG_IGN
);
210 #elif defined(HAVE_SIGACTION)
211 struct sigaction action
; /* POSIX sigaction data */
215 * Ignore SIGPIPE signals...
218 memset(&action
, 0, sizeof(action
));
219 action
.sa_handler
= SIG_IGN
;
220 sigaction(SIGPIPE
, &action
, NULL
);
222 signal(SIGPIPE
, SIG_IGN
);
226 gnutls_global_init();
227 #endif /* HAVE_GNUTLS */
230 SSL_load_error_strings();
234 * Using the current time is a dubious random seed, but on some systems
235 * it is the best we can do (on others, this seed isn't even used...)
238 gettimeofday(&curtime
, NULL
);
239 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
241 for (i
= 0; i
< sizeof(data
); i
++)
242 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
244 RAND_seed(&data
, sizeof(data
));
245 #endif /* HAVE_LIBSSL */
250 * 'httpCheck()' - Check to see if there is a pending response from the server.
253 int /* O - 0 = no data, 1 = data available */
254 httpCheck(http_t
*http
) /* I - HTTP connection */
256 return (httpWait(http
, 0));
261 * 'httpClearCookie()' - Clear the cookie value(s).
265 httpClearCookie(http_t
*http
) /* I - Connection */
279 * 'httpClose()' - Close an HTTP connection...
283 httpClose(http_t
*http
) /* I - Connection to close */
289 free(http
->input_set
);
296 http_shutdown_ssl(http
);
297 #endif /* HAVE_SSL */
300 closesocket(http
->fd
);
310 * 'httpConnect()' - Connect to a HTTP server.
313 http_t
* /* O - New HTTP connection */
314 httpConnect(const char *host
, /* I - Host to connect to */
315 int port
) /* I - Port number */
317 http_encryption_t encrypt
;/* Type of encryption to use */
321 * Set the default encryption status...
325 encrypt
= HTTP_ENCRYPT_ALWAYS
;
327 encrypt
= HTTP_ENCRYPT_IF_REQUESTED
;
329 return (httpConnectEncrypt(host
, port
, encrypt
));
334 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
337 http_t
* /* O - New HTTP connection */
338 httpConnectEncrypt(const char *host
, /* I - Host to connect to */
339 int port
, /* I - Port number */
340 http_encryption_t encrypt
)
341 /* I - Type of encryption to use */
343 int i
; /* Looping var */
344 http_t
*http
; /* New HTTP connection */
345 struct hostent
*hostaddr
; /* Host address data */
357 if ((hostaddr
= httpGetHostByName(host
)) == NULL
)
360 * This hack to make users that don't have a localhost entry in
361 * their hosts file or DNS happy...
364 if (strcasecmp(host
, "localhost") != 0)
366 else if ((hostaddr
= httpGetHostByName("127.0.0.1")) == NULL
)
371 * Verify that it is an IPv4 address (IPv6 support will come in CUPS 1.2...)
375 if ((hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4) &&
376 (hostaddr
->h_addrtype
!= AF_INET6
|| hostaddr
->h_length
!= 16))
379 if (hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4)
381 #endif /* AF_INET6 */
384 * Allocate memory for the structure...
387 http
= calloc(sizeof(http_t
), 1);
391 http
->version
= HTTP_1_1
;
393 http
->activity
= time(NULL
);
397 * Set the encryption status...
400 if (port
== 443) /* Always use encryption for https */
401 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
403 http
->encryption
= encrypt
;
406 * Loop through the addresses we have until one of them connects...
409 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
411 for (i
= 0; hostaddr
->h_addr_list
[i
]; i
++)
414 * Load the address...
417 httpAddrLoad(hostaddr
, port
, i
, &(http
->hostaddr
));
420 * Connect to the remote system...
423 if (!httpReconnect(http
))
428 * Return the new structure if we could connect; otherwise, bail out...
431 if (hostaddr
->h_addr_list
[i
])
442 * 'httpEncryption()' - Set the required encryption on the link.
445 int /* O - -1 on error, 0 on success */
446 httpEncryption(http_t
*http
, /* I - HTTP data */
447 http_encryption_t e
) /* I - New encryption preference */
453 http
->encryption
= e
;
455 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
456 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
457 return (httpReconnect(http
));
458 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
459 return (http_upgrade(http
));
463 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
467 #endif /* HAVE_SSL */
472 * 'httpReconnect()' - Reconnect to a HTTP server...
475 int /* O - 0 on success, non-zero on failure */
476 httpReconnect(http_t
*http
) /* I - HTTP data */
478 int val
; /* Socket option value */
482 http_shutdown_ssl(http
);
483 #endif /* HAVE_SSL */
486 * Close any previously open socket...
491 closesocket(http
->fd
);
497 * Create the socket and set options to allow reuse.
500 if ((http
->fd
= socket(http
->hostaddr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
503 http
->error
= WSAGetLastError();
507 http
->status
= HTTP_ERROR
;
512 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
513 * other processes... */
514 #endif /* FD_CLOEXEC */
517 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
521 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
522 #endif /* SO_REUSEPORT */
525 * Using TCP_NODELAY improves responsiveness, especially on systems
526 * with a slow loopback interface... Since we write large buffers
527 * when sending print files and requests, there shouldn't be any
528 * performance penalty for this...
532 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
535 * Connect to the server...
539 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
540 http
->hostaddr
.addr
.sa_family
== AF_INET
?
541 sizeof(http
->hostaddr
.ipv4
) :
542 sizeof(http
->hostaddr
.ipv6
)) < 0)
544 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
545 sizeof(http
->hostaddr
.ipv4
)) < 0)
546 #endif /* AF_INET6 */
549 http
->error
= WSAGetLastError();
553 http
->status
= HTTP_ERROR
;
556 closesocket(http
->fd
);
567 http
->status
= HTTP_CONTINUE
;
570 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
573 * Always do encryption via SSL.
576 if (http_setup_ssl(http
) != 0)
579 closesocket(http
->fd
);
587 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
588 return (http_upgrade(http
));
589 #endif /* HAVE_SSL */
596 * 'httpGetSubField()' - Get a sub-field value.
599 char * /* O - Value or NULL */
600 httpGetSubField(http_t
*http
, /* I - HTTP data */
601 http_field_t field
, /* I - Field index */
602 const char *name
, /* I - Name of sub-field */
603 char *value
) /* O - Value string */
605 const char *fptr
; /* Pointer into field */
606 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
607 *ptr
; /* Pointer into string buffer */
611 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
612 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
613 name
== NULL
|| value
== NULL
)
616 for (fptr
= http
->fields
[field
]; *fptr
;)
619 * Skip leading whitespace...
622 while (isspace(*fptr
))
632 * Get the sub-field name...
636 *fptr
&& *fptr
!= '=' && !isspace(*fptr
) && ptr
< (temp
+ sizeof(temp
) - 1);
642 * Skip trailing chars up to the '='...
645 while (*fptr
&& *fptr
!= '=')
652 * Skip = and leading whitespace...
657 while (isspace(*fptr
))
663 * Read quoted string...
666 for (ptr
= value
, fptr
++;
667 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
672 while (*fptr
&& *fptr
!= '\"')
681 * Read unquoted string...
685 *fptr
&& !isspace(*fptr
) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
690 while (*fptr
&& !isspace(*fptr
) && *fptr
!= ',')
695 * See if this is the one...
698 if (strcmp(name
, temp
) == 0)
709 * 'httpSetField()' - Set the value of an HTTP header.
713 httpSetField(http_t
*http
, /* I - HTTP data */
714 http_field_t field
, /* I - Field index */
715 const char *value
) /* I - Value */
718 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
719 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
723 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
728 * 'httpDelete()' - Send a DELETE request to the server.
731 int /* O - Status of call (0 = success) */
732 httpDelete(http_t
*http
, /* I - HTTP data */
733 const char *uri
) /* I - URI to delete */
735 return (http_send(http
, HTTP_DELETE
, uri
));
740 * 'httpGet()' - Send a GET request to the server.
743 int /* O - Status of call (0 = success) */
744 httpGet(http_t
*http
, /* I - HTTP data */
745 const char *uri
) /* I - URI to get */
747 return (http_send(http
, HTTP_GET
, uri
));
752 * 'httpHead()' - Send a HEAD request to the server.
755 int /* O - Status of call (0 = success) */
756 httpHead(http_t
*http
, /* I - HTTP data */
757 const char *uri
) /* I - URI for head */
759 return (http_send(http
, HTTP_HEAD
, uri
));
764 * 'httpOptions()' - Send an OPTIONS request to the server.
767 int /* O - Status of call (0 = success) */
768 httpOptions(http_t
*http
, /* I - HTTP data */
769 const char *uri
) /* I - URI for options */
771 return (http_send(http
, HTTP_OPTIONS
, uri
));
776 * 'httpPost()' - Send a POST request to the server.
779 int /* O - Status of call (0 = success) */
780 httpPost(http_t
*http
, /* I - HTTP data */
781 const char *uri
) /* I - URI for post */
785 return (http_send(http
, HTTP_POST
, uri
));
790 * 'httpPut()' - Send a PUT request to the server.
793 int /* O - Status of call (0 = success) */
794 httpPut(http_t
*http
, /* I - HTTP data */
795 const char *uri
) /* I - URI to put */
799 return (http_send(http
, HTTP_PUT
, uri
));
804 * 'httpTrace()' - Send an TRACE request to the server.
807 int /* O - Status of call (0 = success) */
808 httpTrace(http_t
*http
, /* I - HTTP data */
809 const char *uri
) /* I - URI for trace */
811 return (http_send(http
, HTTP_TRACE
, uri
));
816 * 'httpFlush()' - Flush data from a HTTP connection.
820 httpFlush(http_t
*http
) /* I - HTTP data */
822 char buffer
[8192]; /* Junk buffer */
825 if (http
->state
!= HTTP_WAITING
)
827 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
833 * 'httpRead()' - Read data from a HTTP connection.
836 int /* O - Number of bytes read */
837 httpRead(http_t
*http
, /* I - HTTP data */
838 char *buffer
, /* I - Buffer for data */
839 int length
) /* I - Maximum number of bytes */
841 int bytes
; /* Bytes read */
842 char len
[32]; /* Length string */
845 DEBUG_printf(("httpRead(%p, %p, %d)\n", http
, buffer
, length
));
847 if (http
== NULL
|| buffer
== NULL
)
850 http
->activity
= time(NULL
);
855 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
856 http
->data_remaining
<= 0)
858 DEBUG_puts("httpRead: Getting chunk length...");
860 if (httpGets(len
, sizeof(len
), http
) == NULL
)
862 DEBUG_puts("httpRead: Could not get length!");
866 http
->data_remaining
= strtol(len
, NULL
, 16);
867 if (http
->data_remaining
< 0)
869 DEBUG_puts("httpRead: Negative chunk length!");
874 DEBUG_printf(("httpRead: data_remaining = %d\n", http
->data_remaining
));
876 if (http
->data_remaining
<= 0)
879 * A zero-length chunk ends a transfer; unless we are reading POST
883 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
884 httpGets(len
, sizeof(len
), http
);
886 if (http
->state
== HTTP_POST_RECV
)
889 http
->state
= HTTP_WAITING
;
893 else if (length
> http
->data_remaining
)
894 length
= http
->data_remaining
;
896 if (http
->used
== 0 && length
<= 256)
899 * Buffer small reads for better performance...
902 if (http
->data_remaining
> sizeof(http
->buffer
))
903 bytes
= sizeof(http
->buffer
);
905 bytes
= http
->data_remaining
;
907 if (!http
->blocking
&& !httpWait(http
, 1000))
912 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
914 #endif /* HAVE_SSL */
916 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
919 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
921 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
930 http
->error
= WSAGetLastError();
949 if (length
> http
->used
)
954 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
956 memcpy(buffer
, http
->buffer
, length
);
957 http
->used
-= length
;
960 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
965 if (!http
->blocking
&& !httpWait(http
, 1000))
967 bytes
= http_read_ssl(http
, buffer
, length
);
969 #endif /* HAVE_SSL */
972 if (!http
->blocking
&& !httpWait(http
, 1000))
974 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
975 bytes
= recv(http
->fd
, buffer
, length
, 0);
976 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
980 http
->data_remaining
-= bytes
;
984 http
->error
= WSAGetLastError();
998 if (http
->data_remaining
== 0)
1000 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1001 httpGets(len
, sizeof(len
), http
);
1003 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1005 if (http
->state
== HTTP_POST_RECV
)
1008 http
->state
= HTTP_WAITING
;
1015 printf("httpRead: Read %d bytes:\n", bytes
);
1016 for (i
= 0; i
< bytes
; i
+= 16)
1020 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1021 printf(" %02X", buffer
[i
+ j
] & 255);
1030 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1032 ch
= buffer
[i
+ j
] & 255;
1034 if (ch
< ' ' || ch
== 127)
1049 * 'httpSetCookie()' - Set the cookie value(s)...
1053 httpSetCookie(http_t
*http
, /* I - Connection */
1054 const char *cookie
) /* I - Cookie string */
1063 http
->cookie
= strdup(cookie
);
1065 http
->cookie
= NULL
;
1070 * 'httpWait()' - Wait for data available on a connection.
1073 int /* O - 1 if data is available, 0 otherwise */
1074 httpWait(http_t
*http
, /* I - HTTP data */
1075 int msec
) /* I - Milliseconds to wait */
1078 struct rlimit limit
; /* Runtime limit */
1080 struct timeval timeout
; /* Timeout */
1081 int nfds
; /* Result from select() */
1085 * First see if there is data in the buffer...
1095 * Then try doing a select() to poll the socket...
1098 if (!http
->input_set
)
1102 * Windows has a fixed-size select() structure, different (surprise,
1103 * surprise!) from all UNIX implementations. Just allocate this
1104 * fixed structure...
1107 http
->input_set
= calloc(1, sizeof(fd_set
));
1110 * Allocate the select() input set based upon the max number of file
1111 * descriptors available for this process...
1114 getrlimit(RLIMIT_NOFILE
, &limit
);
1116 http
->input_set
= calloc(1, (limit
.rlim_cur
+ 7) / 8);
1119 if (!http
->input_set
)
1123 FD_SET(http
->fd
, http
->input_set
);
1127 timeout
.tv_sec
= msec
/ 1000;
1128 timeout
.tv_usec
= (msec
% 1000) * 1000;
1130 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
1133 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
1135 FD_CLR(http
->fd
, http
->input_set
);
1142 * 'httpWrite()' - Write data to a HTTP connection.
1145 int /* O - Number of bytes written */
1146 httpWrite(http_t
*http
, /* I - HTTP data */
1147 const char *buffer
, /* I - Buffer for data */
1148 int length
) /* I - Number of bytes to write */
1150 int tbytes
, /* Total bytes sent */
1151 bytes
; /* Bytes sent */
1154 if (http
== NULL
|| buffer
== NULL
)
1157 http
->activity
= time(NULL
);
1159 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1161 if (httpPrintf(http
, "%x\r\n", length
) < 0)
1167 * A zero-length chunk ends a transfer; unless we are sending POST
1171 DEBUG_puts("httpWrite: changing states...");
1173 if (http
->state
== HTTP_POST_RECV
)
1175 else if (http
->state
== HTTP_PUT_RECV
)
1176 http
->state
= HTTP_STATUS
;
1178 http
->state
= HTTP_WAITING
;
1180 if (httpPrintf(http
, "\r\n") < 0)
1193 bytes
= http_write_ssl(http
, buffer
, length
);
1195 #endif /* HAVE_SSL */
1196 bytes
= send(http
->fd
, buffer
, length
, 0);
1201 if (WSAGetLastError() != http
->error
)
1203 http
->error
= WSAGetLastError();
1209 else if (errno
!= http
->error
)
1211 http
->error
= errno
;
1216 DEBUG_puts("httpWrite: error writing data...\n");
1224 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1225 http
->data_remaining
-= bytes
;
1228 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1229 if (httpPrintf(http
, "\r\n") < 0)
1232 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1235 * Finished with the transfer; unless we are sending POST data, go idle...
1238 DEBUG_puts("httpWrite: changing states...");
1240 if (http
->state
== HTTP_POST_RECV
)
1243 http
->state
= HTTP_WAITING
;
1249 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1250 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1254 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1255 printf(" %02X", buffer
[i
+ j
] & 255);
1264 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1266 ch
= buffer
[i
+ j
] & 255;
1268 if (ch
< ' ' || ch
== 127)
1282 * 'httpGets()' - Get a line of text from a HTTP connection.
1285 char * /* O - Line or NULL */
1286 httpGets(char *line
, /* I - Line to read into */
1287 int length
, /* I - Max length of buffer */
1288 http_t
*http
) /* I - HTTP data */
1290 char *lineptr
, /* Pointer into line */
1291 *bufptr
, /* Pointer into input buffer */
1292 *bufend
; /* Pointer to end of buffer */
1293 int bytes
; /* Number of bytes read */
1296 DEBUG_printf(("httpGets(%p, %d, %p)\n", line
, length
, http
));
1298 if (http
== NULL
|| line
== NULL
)
1302 * Pre-scan the buffer and see if there is a newline in there...
1313 bufptr
= http
->buffer
;
1314 bufend
= http
->buffer
+ http
->used
;
1316 while (bufptr
< bufend
)
1317 if (*bufptr
== 0x0a)
1322 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1325 * No newline; see if there is more data to be read...
1328 if (!http
->blocking
&& !httpWait(http
, 1000))
1333 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1335 #endif /* HAVE_SSL */
1336 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1341 * Nope, can't get a line this time...
1345 if (WSAGetLastError() != http
->error
)
1347 http
->error
= WSAGetLastError();
1351 DEBUG_printf(("httpGets(): recv() error %d!\n", WSAGetLastError()));
1355 else if (errno
!= http
->error
)
1357 http
->error
= errno
;
1361 DEBUG_printf(("httpGets(): recv() error %d!\n", errno
));
1366 else if (bytes
== 0)
1368 http
->error
= EPIPE
;
1374 * Yup, update the amount used and the end pointer...
1377 http
->used
+= bytes
;
1382 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1384 http
->activity
= time(NULL
);
1387 * Read a line from the buffer...
1391 bufptr
= http
->buffer
;
1395 while (bufptr
< bufend
&& bytes
< length
)
1399 if (*bufptr
== 0x0a)
1404 else if (*bufptr
== 0x0d)
1407 *lineptr
++ = *bufptr
++;
1414 http
->used
-= bytes
;
1416 memmove(http
->buffer
, bufptr
, http
->used
);
1418 DEBUG_printf(("httpGets(): Returning \"%s\"\n", line
));
1422 DEBUG_puts("httpGets(): No new line available!");
1429 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1432 int /* O - Number of bytes written */
1433 httpPrintf(http_t
*http
, /* I - HTTP data */
1434 const char *format
, /* I - printf-style format string */
1435 ...) /* I - Additional args as needed */
1437 int bytes
, /* Number of bytes to write */
1438 nbytes
, /* Number of bytes written */
1439 tbytes
; /* Number of bytes all together */
1440 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1441 *bufptr
; /* Pointer into buffer */
1442 va_list ap
; /* Variable argument pointer */
1445 va_start(ap
, format
);
1446 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1449 DEBUG_printf(("httpPrintf: %s", buf
));
1451 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1455 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1457 #endif /* HAVE_SSL */
1458 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1465 if (WSAGetLastError() != http
->error
)
1467 http
->error
= WSAGetLastError();
1473 else if (errno
!= http
->error
)
1475 http
->error
= errno
;
1489 * 'httpStatus()' - Return a short string describing a HTTP status code.
1492 const char * /* O - String or NULL */
1493 httpStatus(http_status_t status
) /* I - HTTP status code */
1497 case HTTP_CONTINUE
:
1498 return ("Continue");
1499 case HTTP_SWITCHING_PROTOCOLS
:
1500 return ("Switching Protocols");
1505 case HTTP_ACCEPTED
:
1506 return ("Accepted");
1507 case HTTP_NO_CONTENT
:
1508 return ("No Content");
1509 case HTTP_NOT_MODIFIED
:
1510 return ("Not Modified");
1511 case HTTP_BAD_REQUEST
:
1512 return ("Bad Request");
1513 case HTTP_UNAUTHORIZED
:
1514 return ("Unauthorized");
1515 case HTTP_FORBIDDEN
:
1516 return ("Forbidden");
1517 case HTTP_NOT_FOUND
:
1518 return ("Not Found");
1519 case HTTP_REQUEST_TOO_LARGE
:
1520 return ("Request Entity Too Large");
1521 case HTTP_URI_TOO_LONG
:
1522 return ("URI Too Long");
1523 case HTTP_UPGRADE_REQUIRED
:
1524 return ("Upgrade Required");
1525 case HTTP_NOT_IMPLEMENTED
:
1526 return ("Not Implemented");
1527 case HTTP_NOT_SUPPORTED
:
1528 return ("Not Supported");
1536 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1539 const char * /* O - Date/time string */
1540 httpGetDateString(time_t t
) /* I - UNIX time */
1543 static char datetime
[256];
1547 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1548 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1549 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1556 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1559 time_t /* O - UNIX time */
1560 httpGetDateTime(const char *s
) /* I - Date/time string */
1562 int i
; /* Looping var */
1563 struct tm tdate
; /* Time/date structure */
1564 char mon
[16]; /* Abbreviated month name */
1565 int day
, year
; /* Day of month and year */
1566 int hour
, min
, sec
; /* Time */
1569 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1572 for (i
= 0; i
< 12; i
++)
1573 if (strcasecmp(mon
, months
[i
]) == 0)
1580 tdate
.tm_mday
= day
;
1581 tdate
.tm_year
= year
- 1900;
1582 tdate
.tm_hour
= hour
;
1587 return (mktime(&tdate
));
1592 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1595 http_status_t
/* O - HTTP status */
1596 httpUpdate(http_t
*http
) /* I - HTTP data */
1598 char line
[1024], /* Line from connection... */
1599 *value
; /* Pointer to value on line */
1600 http_field_t field
; /* Field index */
1601 int major
, minor
; /* HTTP version numbers */
1602 http_status_t status
; /* Authorization status */
1605 DEBUG_printf(("httpUpdate(%p)\n", http
));
1608 * If we haven't issued any commands, then there is nothing to "update"...
1611 if (http
->state
== HTTP_WAITING
)
1612 return (HTTP_CONTINUE
);
1615 * Grab all of the lines we can from the connection...
1618 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1622 if (line
[0] == '\0')
1625 * Blank line means the start of the data section (if any). Return
1626 * the result code, too...
1628 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1629 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1633 if (http
->status
== HTTP_CONTINUE
)
1634 return (http
->status
);
1637 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1639 if (http_setup_ssl(http
) != 0)
1642 closesocket(http
->fd
);
1647 return (HTTP_ERROR
);
1650 return (HTTP_CONTINUE
);
1652 else if (http
->status
== HTTP_UPGRADE_REQUIRED
&&
1653 http
->encryption
!= HTTP_ENCRYPT_NEVER
)
1654 http
->encryption
= HTTP_ENCRYPT_REQUIRED
;
1655 #endif /* HAVE_SSL */
1657 httpGetLength(http
);
1659 switch (http
->state
)
1663 case HTTP_POST_RECV
:
1666 case HTTP_POST_SEND
:
1670 http
->state
= HTTP_WAITING
;
1674 return (http
->status
);
1676 else if (strncmp(line
, "HTTP/", 5) == 0)
1679 * Got the beginning of a response...
1682 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, (int *)&status
) != 3)
1683 return (HTTP_ERROR
);
1685 http
->version
= (http_version_t
)(major
* 100 + minor
);
1686 http
->status
= status
;
1688 else if ((value
= strchr(line
, ':')) != NULL
)
1695 while (isspace(*value
))
1699 * Be tolerants of servers that send unknown attribute fields...
1702 if (!strcasecmp(line
, "expect"))
1705 * "Expect: 100-continue" or similar...
1708 http
->expect
= (http_status_t
)atoi(value
);
1710 else if (!strcasecmp(line
, "cookie"))
1713 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1716 httpSetCookie(http
, value
);
1718 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1720 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1724 httpSetField(http
, field
, value
);
1728 http
->status
= HTTP_ERROR
;
1729 return (HTTP_ERROR
);
1734 * See if there was an error...
1739 http
->status
= HTTP_ERROR
;
1740 return (HTTP_ERROR
);
1744 * If we haven't already returned, then there is nothing new...
1747 return (HTTP_CONTINUE
);
1752 * 'httpDecode64()' - Base64-decode a string.
1755 char * /* O - Decoded string */
1756 httpDecode64(char *out
, /* I - String to write to */
1757 const char *in
) /* I - String to read from */
1759 int pos
, /* Bit position */
1760 base64
; /* Value of this character */
1761 char *outptr
; /* Output pointer */
1764 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1767 * Decode this character into a number from 0 to 63...
1770 if (*in
>= 'A' && *in
<= 'Z')
1772 else if (*in
>= 'a' && *in
<= 'z')
1773 base64
= *in
- 'a' + 26;
1774 else if (*in
>= '0' && *in
<= '9')
1775 base64
= *in
- '0' + 52;
1776 else if (*in
== '+')
1778 else if (*in
== '/')
1780 else if (*in
== '=')
1786 * Store the result in the appropriate chars...
1792 *outptr
= base64
<< 2;
1796 *outptr
++ |= (base64
>> 4) & 3;
1797 *outptr
= (base64
<< 4) & 255;
1801 *outptr
++ |= (base64
>> 2) & 15;
1802 *outptr
= (base64
<< 6) & 255;
1806 *outptr
++ |= base64
;
1815 * Return the decoded string...
1823 * 'httpEncode64()' - Base64-encode a string.
1826 char * /* O - Encoded string */
1827 httpEncode64(char *out
, /* I - String to write to */
1828 const char *in
) /* I - String to read from */
1830 char *outptr
; /* Output pointer */
1831 static const char base64
[] = /* Base64 characters... */
1833 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1834 "abcdefghijklmnopqrstuvwxyz"
1840 for (outptr
= out
; *in
!= '\0'; in
++)
1843 * Encode the up to 3 characters as 4 Base64 numbers...
1846 *outptr
++ = base64
[in
[0] >> 2];
1847 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1856 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1862 *outptr
++ = base64
[in
[0] & 63];
1869 * Return the encoded string...
1877 * 'httpGetLength()' - Get the amount of data remaining from the
1878 * content-length or transfer-encoding fields.
1881 int /* O - Content length */
1882 httpGetLength(http_t
*http
) /* I - HTTP data */
1884 DEBUG_printf(("httpGetLength(%p), state = %d\n", http
, http
->state
));
1886 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1888 DEBUG_puts("httpGetLength: chunked request!");
1890 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1891 http
->data_remaining
= 0;
1895 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1898 * The following is a hack for HTTP servers that don't send a
1899 * content-length or transfer-encoding field...
1901 * If there is no content-length then the connection must close
1902 * after the transfer is complete...
1905 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1906 http
->data_remaining
= 2147483647;
1908 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1910 DEBUG_printf(("httpGetLength: content_length = %d\n", http
->data_remaining
));
1913 return (http
->data_remaining
);
1918 * 'http_field()' - Return the field index for a field name.
1921 static http_field_t
/* O - Field index */
1922 http_field(const char *name
) /* I - String name */
1924 int i
; /* Looping var */
1927 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1928 if (strcasecmp(name
, http_fields
[i
]) == 0)
1929 return ((http_field_t
)i
);
1931 return (HTTP_FIELD_UNKNOWN
);
1936 * 'http_send()' - Send a request with all fields and the trailing blank line.
1939 static int /* O - 0 on success, non-zero on error */
1940 http_send(http_t
*http
, /* I - HTTP data */
1941 http_state_t request
, /* I - Request code */
1942 const char *uri
) /* I - URI */
1944 int i
; /* Looping var */
1945 char *ptr
, /* Pointer in buffer */
1946 buf
[1024]; /* Encoded URI buffer */
1947 static const char * const codes
[] =
1948 { /* Request code strings */
1963 static const char hex
[] = "0123456789ABCDEF";
1967 if (http
== NULL
|| uri
== NULL
)
1971 * Encode the URI as needed...
1974 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1975 if (*uri
<= ' ' || *uri
>= 127)
1977 if (ptr
< (buf
+ sizeof(buf
) - 1))
1979 if (ptr
< (buf
+ sizeof(buf
) - 1))
1980 *ptr
++ = hex
[(*uri
>> 4) & 15];
1981 if (ptr
< (buf
+ sizeof(buf
) - 1))
1982 *ptr
++ = hex
[*uri
& 15];
1990 * See if we had an error the last time around; if so, reconnect...
1993 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1994 httpReconnect(http
);
1997 * Send the request header...
2000 http
->state
= request
;
2001 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
2004 http
->status
= HTTP_CONTINUE
;
2007 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
2009 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
2010 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
2012 #endif /* HAVE_SSL */
2014 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
2016 http
->status
= HTTP_ERROR
;
2020 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2021 if (http
->fields
[i
][0] != '\0')
2023 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
2025 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
2027 http
->status
= HTTP_ERROR
;
2032 if (httpPrintf(http
, "\r\n") < 1)
2034 http
->status
= HTTP_ERROR
;
2038 httpClearFields(http
);
2046 * 'http_upgrade()' - Force upgrade to TLS encryption.
2049 static int /* O - Status of connection */
2050 http_upgrade(http_t
*http
) /* I - HTTP data */
2052 int ret
; /* Return value */
2053 http_t myhttp
; /* Local copy of HTTP data */
2056 DEBUG_printf(("http_upgrade(%p)\n", http
));
2059 * Copy the HTTP data to a local variable so we can do the OPTIONS
2060 * request without interfering with the existing request data...
2063 memcpy(&myhttp
, http
, sizeof(myhttp
));
2066 * Send an OPTIONS request to the server, requiring SSL or TLS
2067 * encryption on the link...
2070 httpClearFields(&myhttp
);
2071 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2072 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2074 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2077 * Wait for the secure connection...
2080 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2086 * Copy the HTTP data back over, if any...
2089 http
->fd
= myhttp
.fd
;
2090 http
->error
= myhttp
.error
;
2091 http
->activity
= myhttp
.activity
;
2092 http
->status
= myhttp
.status
;
2093 http
->version
= myhttp
.version
;
2094 http
->keep_alive
= myhttp
.keep_alive
;
2095 http
->used
= myhttp
.used
;
2098 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2100 http
->auth_type
= myhttp
.auth_type
;
2101 http
->nonce_count
= myhttp
.nonce_count
;
2103 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2105 http
->tls
= myhttp
.tls
;
2106 http
->encryption
= myhttp
.encryption
;
2109 * See if we actually went secure...
2115 * Server does not support HTTP upgrade...
2118 DEBUG_puts("Server does not support HTTP upgrade!");
2121 closesocket(http
->fd
);
2136 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2139 static int /* O - Status of connection */
2140 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2143 SSL_CTX
*context
; /* Context for encryption */
2144 SSL
*conn
; /* Connection for encryption */
2145 # elif defined(HAVE_GNUTLS)
2146 http_tls_t
*conn
; /* TLS session object */
2147 gnutls_certificate_client_credentials
*credentials
;
2148 /* TLS credentials */
2149 # elif defined(HAVE_CDSASSL)
2150 SSLContextRef conn
; /* Context for encryption */
2151 OSStatus error
; /* Error info */
2152 # endif /* HAVE_LIBSSL */
2156 context
= SSL_CTX_new(SSLv23_client_method());
2157 conn
= SSL_new(context
);
2159 SSL_set_fd(conn
, http
->fd
);
2160 if (SSL_connect(conn
) != 1)
2162 SSL_CTX_free(context
);
2166 http
->error
= WSAGetLastError();
2168 http
->error
= errno
;
2170 http
->status
= HTTP_ERROR
;
2172 return (HTTP_ERROR
);
2175 # elif defined(HAVE_GNUTLS)
2176 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2180 http
->error
= errno
;
2181 http
->status
= HTTP_ERROR
;
2186 credentials
= (gnutls_certificate_client_credentials
*)
2187 malloc(sizeof(gnutls_certificate_client_credentials
));
2188 if (credentials
== NULL
)
2192 http
->error
= errno
;
2193 http
->status
= HTTP_ERROR
;
2198 gnutls_certificate_allocate_credentials(credentials
);
2200 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2201 gnutls_set_default_priority(conn
->session
);
2202 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2203 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2205 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2207 http
->error
= errno
;
2208 http
->status
= HTTP_ERROR
;
2213 conn
->credentials
= credentials
;
2215 # elif defined(HAVE_CDSASSL)
2216 error
= SSLNewContext(false, &conn
);
2219 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2222 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2225 error
= SSLSetAllowsExpiredCerts(conn
, true);
2228 error
= SSLSetAllowsAnyRoot(conn
, true);
2231 error
= SSLHandshake(conn
);
2235 http
->error
= error
;
2236 http
->status
= HTTP_ERROR
;
2238 SSLDisposeContext(conn
);
2244 # endif /* HAVE_CDSASSL */
2252 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2256 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2259 SSL_CTX
*context
; /* Context for encryption */
2260 SSL
*conn
; /* Connection for encryption */
2263 conn
= (SSL
*)(http
->tls
);
2264 context
= SSL_get_SSL_CTX(conn
);
2267 SSL_CTX_free(context
);
2270 # elif defined(HAVE_GNUTLS)
2271 http_tls_t
*conn
; /* Encryption session */
2272 gnutls_certificate_client_credentials
*credentials
;
2273 /* TLS credentials */
2276 conn
= (http_tls_t
*)(http
->tls
);
2277 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2279 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2280 gnutls_deinit(conn
->session
);
2281 gnutls_certificate_free_credentials(*credentials
);
2285 # elif defined(HAVE_CDSASSL)
2286 SSLClose((SSLContextRef
)http
->tls
);
2287 SSLDisposeContext((SSLContextRef
)http
->tls
);
2288 # endif /* HAVE_LIBSSL */
2295 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2298 static int /* O - Bytes read */
2299 http_read_ssl(http_t
*http
, /* I - HTTP data */
2300 char *buf
, /* I - Buffer to store data */
2301 int len
) /* I - Length of buffer */
2303 # if defined(HAVE_LIBSSL)
2304 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2306 # elif defined(HAVE_GNUTLS)
2307 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2309 # elif defined(HAVE_CDSASSL)
2310 OSStatus error
; /* Error info */
2311 size_t processed
; /* Number of bytes processed */
2314 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2320 http
->error
= error
;
2324 # endif /* HAVE_LIBSSL */
2329 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2332 static int /* O - Bytes written */
2333 http_write_ssl(http_t
*http
, /* I - HTTP data */
2334 const char *buf
, /* I - Buffer holding data */
2335 int len
) /* I - Length of buffer */
2337 # if defined(HAVE_LIBSSL)
2338 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2340 # elif defined(HAVE_GNUTLS)
2341 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2342 # elif defined(HAVE_CDSASSL)
2343 OSStatus error
; /* Error info */
2344 size_t processed
; /* Number of bytes processed */
2347 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2353 http
->error
= error
;
2356 # endif /* HAVE_LIBSSL */
2360 # if defined(HAVE_CDSASSL)
2362 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2365 static OSStatus
/* O - -1 on error, 0 on success */
2366 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2367 void *data
, /* I - Data buffer */
2368 size_t *dataLength
) /* IO - Number of bytes */
2370 ssize_t bytes
; /* Number of bytes read */
2373 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2376 *dataLength
= bytes
;
2385 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2388 static OSStatus
/* O - -1 on error, 0 on success */
2389 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2390 const void *data
, /* I - Data buffer */
2391 size_t *dataLength
) /* IO - Number of bytes */
2396 bytes
= write((int)connection
, data
, *dataLength
);
2399 *dataLength
= bytes
;
2405 # endif /* HAVE_CDSASSL */
2406 #endif /* HAVE_SSL */
2410 * End of "$Id: http.c,v 1.82.2.32 2003/05/09 16:06:43 mike Exp $".