2 * "$Id: http.c,v 1.135 2004/04/20 14:13:54 mike Exp $"
4 * HTTP routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2004 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 */
459 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
465 http
->encryption
= e
;
467 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
468 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
469 return (httpReconnect(http
));
470 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
471 return (http_upgrade(http
));
475 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
479 #endif /* HAVE_SSL */
484 * 'httpReconnect()' - Reconnect to a HTTP server...
487 int /* O - 0 on success, non-zero on failure */
488 httpReconnect(http_t
*http
) /* I - HTTP data */
490 int val
; /* Socket option value */
493 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
500 http_shutdown_ssl(http
);
501 #endif /* HAVE_SSL */
504 * Close any previously open socket...
509 closesocket(http
->fd
);
515 * Create the socket and set options to allow reuse.
518 if ((http
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
521 http
->error
= WSAGetLastError();
525 http
->status
= HTTP_ERROR
;
530 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
531 * other processes... */
532 #endif /* FD_CLOEXEC */
535 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
539 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
540 #endif /* SO_REUSEPORT */
543 * Using TCP_NODELAY improves responsiveness, especially on systems
544 * with a slow loopback interface... Since we write large buffers
545 * when sending print files and requests, there shouldn't be any
546 * performance penalty for this...
551 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
553 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
557 * Connect to the server...
560 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
561 sizeof(http
->hostaddr
)) < 0)
564 http
->error
= WSAGetLastError();
568 http
->status
= HTTP_ERROR
;
571 closesocket(http
->fd
);
582 http
->status
= HTTP_CONTINUE
;
585 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
588 * Always do encryption via SSL.
591 if (http_setup_ssl(http
) != 0)
594 closesocket(http
->fd
);
602 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
603 return (http_upgrade(http
));
604 #endif /* HAVE_SSL */
611 * 'httpGetSubField()' - Get a sub-field value.
614 char * /* O - Value or NULL */
615 httpGetSubField(http_t
*http
, /* I - HTTP data */
616 http_field_t field
, /* I - Field index */
617 const char *name
, /* I - Name of sub-field */
618 char *value
) /* O - Value string */
620 const char *fptr
; /* Pointer into field */
621 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
622 *ptr
; /* Pointer into string buffer */
625 DEBUG_printf(("httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p)\n",
626 http
, field
, name
, value
));
629 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
630 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
631 name
== NULL
|| value
== NULL
)
634 for (fptr
= http
->fields
[field
]; *fptr
;)
637 * Skip leading whitespace...
640 while (isspace(*fptr
& 255))
650 * Get the sub-field name...
654 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) && ptr
< (temp
+ sizeof(temp
) - 1);
659 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
662 * Skip trailing chars up to the '='...
665 while (isspace(*fptr
& 255))
675 * Skip = and leading whitespace...
680 while (isspace(*fptr
& 255))
686 * Read quoted string...
689 for (ptr
= value
, fptr
++;
690 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
695 while (*fptr
&& *fptr
!= '\"')
704 * Read unquoted string...
708 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
713 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
717 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
720 * See if this is the one...
723 if (strcmp(name
, temp
) == 0)
734 * 'httpSetField()' - Set the value of an HTTP header.
738 httpSetField(http_t
*http
, /* I - HTTP data */
739 http_field_t field
, /* I - Field index */
740 const char *value
) /* I - Value */
743 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
744 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
748 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
753 * 'httpDelete()' - Send a DELETE request to the server.
756 int /* O - Status of call (0 = success) */
757 httpDelete(http_t
*http
, /* I - HTTP data */
758 const char *uri
) /* I - URI to delete */
760 return (http_send(http
, HTTP_DELETE
, uri
));
765 * 'httpGet()' - Send a GET request to the server.
768 int /* O - Status of call (0 = success) */
769 httpGet(http_t
*http
, /* I - HTTP data */
770 const char *uri
) /* I - URI to get */
772 return (http_send(http
, HTTP_GET
, uri
));
777 * 'httpHead()' - Send a HEAD request to the server.
780 int /* O - Status of call (0 = success) */
781 httpHead(http_t
*http
, /* I - HTTP data */
782 const char *uri
) /* I - URI for head */
784 return (http_send(http
, HTTP_HEAD
, uri
));
789 * 'httpOptions()' - Send an OPTIONS request to the server.
792 int /* O - Status of call (0 = success) */
793 httpOptions(http_t
*http
, /* I - HTTP data */
794 const char *uri
) /* I - URI for options */
796 return (http_send(http
, HTTP_OPTIONS
, uri
));
801 * 'httpPost()' - Send a POST request to the server.
804 int /* O - Status of call (0 = success) */
805 httpPost(http_t
*http
, /* I - HTTP data */
806 const char *uri
) /* I - URI for post */
810 return (http_send(http
, HTTP_POST
, uri
));
815 * 'httpPut()' - Send a PUT request to the server.
818 int /* O - Status of call (0 = success) */
819 httpPut(http_t
*http
, /* I - HTTP data */
820 const char *uri
) /* I - URI to put */
824 return (http_send(http
, HTTP_PUT
, uri
));
829 * 'httpTrace()' - Send an TRACE request to the server.
832 int /* O - Status of call (0 = success) */
833 httpTrace(http_t
*http
, /* I - HTTP data */
834 const char *uri
) /* I - URI for trace */
836 return (http_send(http
, HTTP_TRACE
, uri
));
841 * 'httpFlush()' - Flush data from a HTTP connection.
845 httpFlush(http_t
*http
) /* I - HTTP data */
847 char buffer
[8192]; /* Junk buffer */
850 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
852 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
857 * 'httpRead()' - Read data from a HTTP connection.
860 int /* O - Number of bytes read */
861 httpRead(http_t
*http
, /* I - HTTP data */
862 char *buffer
, /* I - Buffer for data */
863 int length
) /* I - Maximum number of bytes */
865 int bytes
; /* Bytes read */
866 char len
[32]; /* Length string */
869 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
870 http
, buffer
, length
));
872 if (http
== NULL
|| buffer
== NULL
)
875 http
->activity
= time(NULL
);
880 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
881 http
->data_remaining
<= 0)
883 DEBUG_puts("httpRead: Getting chunk length...");
885 if (httpGets(len
, sizeof(len
), http
) == NULL
)
887 DEBUG_puts("httpRead: Could not get length!");
891 http
->data_remaining
= strtol(len
, NULL
, 16);
892 if (http
->data_remaining
< 0)
894 DEBUG_puts("httpRead: Negative chunk length!");
899 DEBUG_printf(("httpRead: data_remaining=%d\n", http
->data_remaining
));
901 if (http
->data_remaining
<= 0)
904 * A zero-length chunk ends a transfer; unless we are reading POST
908 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
909 httpGets(len
, sizeof(len
), http
);
911 if (http
->state
== HTTP_POST_RECV
)
914 http
->state
= HTTP_WAITING
;
917 * Prevent future reads for this request...
920 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
924 else if (length
> http
->data_remaining
)
925 length
= http
->data_remaining
;
927 if (http
->used
== 0 && length
<= 256)
930 * Buffer small reads for better performance...
933 if (!http
->blocking
&& !httpWait(http
, 1000))
936 if (http
->data_remaining
> sizeof(http
->buffer
))
937 bytes
= sizeof(http
->buffer
);
939 bytes
= http
->data_remaining
;
943 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
945 #endif /* HAVE_SSL */
947 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
950 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
952 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
961 http
->error
= WSAGetLastError();
980 if (length
> http
->used
)
985 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
987 memcpy(buffer
, http
->buffer
, length
);
988 http
->used
-= length
;
991 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
996 if (!http
->blocking
&& !httpWait(http
, 1000))
999 bytes
= http_read_ssl(http
, buffer
, length
);
1001 #endif /* HAVE_SSL */
1004 if (!http
->blocking
&& !httpWait(http
, 1000))
1007 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
1008 bytes
= recv(http
->fd
, buffer
, length
, 0);
1009 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
1013 http
->data_remaining
-= bytes
;
1017 http
->error
= WSAGetLastError();
1022 http
->error
= errno
;
1027 http
->error
= EPIPE
;
1031 if (http
->data_remaining
== 0)
1033 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1034 httpGets(len
, sizeof(len
), http
);
1036 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1038 if (http
->state
== HTTP_POST_RECV
)
1041 http
->state
= HTTP_WAITING
;
1048 printf("httpRead: Read %d bytes:\n", bytes
);
1049 for (i
= 0; i
< bytes
; i
+= 16)
1053 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1054 printf(" %02X", buffer
[i
+ j
] & 255);
1063 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1065 ch
= buffer
[i
+ j
] & 255;
1067 if (ch
< ' ' || ch
== 127)
1082 * 'httpSetCookie()' - Set the cookie value(s)...
1086 httpSetCookie(http_t
*http
, /* I - Connection */
1087 const char *cookie
) /* I - Cookie string */
1096 http
->cookie
= strdup(cookie
);
1098 http
->cookie
= NULL
;
1103 * 'httpWait()' - Wait for data available on a connection.
1106 int /* O - 1 if data is available, 0 otherwise */
1107 httpWait(http_t
*http
, /* I - HTTP data */
1108 int msec
) /* I - Milliseconds to wait */
1111 * First see if there is data in the buffer...
1121 * If not, check the SSL/TLS buffers and do a select() on the connection...
1124 return (http_wait(http
, msec
));
1129 * 'httpWrite()' - Write data to a HTTP connection.
1132 int /* O - Number of bytes written */
1133 httpWrite(http_t
*http
, /* I - HTTP data */
1134 const char *buffer
, /* I - Buffer for data */
1135 int length
) /* I - Number of bytes to write */
1137 int tbytes
, /* Total bytes sent */
1138 bytes
; /* Bytes sent */
1141 if (http
== NULL
|| buffer
== NULL
)
1144 http
->activity
= time(NULL
);
1146 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1148 if (httpPrintf(http
, "%x\r\n", length
) < 0)
1154 * A zero-length chunk ends a transfer; unless we are sending POST
1155 * or PUT data, go idle...
1158 DEBUG_puts("httpWrite: changing states...");
1160 if (http
->state
== HTTP_POST_RECV
)
1162 else if (http
->state
== HTTP_PUT_RECV
)
1163 http
->state
= HTTP_STATUS
;
1165 http
->state
= HTTP_WAITING
;
1167 if (httpPrintf(http
, "\r\n") < 0)
1180 bytes
= http_write_ssl(http
, buffer
, length
);
1182 #endif /* HAVE_SSL */
1183 bytes
= send(http
->fd
, buffer
, length
, 0);
1188 if (WSAGetLastError() != http
->error
)
1190 http
->error
= WSAGetLastError();
1196 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
1198 http
->error
= errno
;
1203 DEBUG_puts("httpWrite: error writing data...\n");
1211 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1212 http
->data_remaining
-= bytes
;
1215 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1216 if (httpPrintf(http
, "\r\n") < 0)
1219 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1222 * Finished with the transfer; unless we are sending POST or PUT
1226 DEBUG_puts("httpWrite: changing states...");
1228 if (http
->state
== HTTP_POST_RECV
)
1230 else if (http
->state
== HTTP_PUT_RECV
)
1231 http
->state
= HTTP_STATUS
;
1233 http
->state
= HTTP_WAITING
;
1239 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1240 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1244 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1245 printf(" %02X", buffer
[i
+ j
] & 255);
1254 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1256 ch
= buffer
[i
+ j
] & 255;
1258 if (ch
< ' ' || ch
== 127)
1272 * 'httpGets()' - Get a line of text from a HTTP connection.
1275 char * /* O - Line or NULL */
1276 httpGets(char *line
, /* I - Line to read into */
1277 int length
, /* I - Max length of buffer */
1278 http_t
*http
) /* I - HTTP data */
1280 char *lineptr
, /* Pointer into line */
1281 *bufptr
, /* Pointer into input buffer */
1282 *bufend
; /* Pointer to end of buffer */
1283 int bytes
; /* Number of bytes read */
1286 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
1288 if (http
== NULL
|| line
== NULL
)
1292 * Pre-scan the buffer and see if there is a newline in there...
1303 bufptr
= http
->buffer
;
1304 bufend
= http
->buffer
+ http
->used
;
1306 while (bufptr
< bufend
)
1307 if (*bufptr
== 0x0a)
1312 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1315 * No newline; see if there is more data to be read...
1318 if (!http
->blocking
&& !http_wait(http
, 1000))
1323 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1325 #endif /* HAVE_SSL */
1326 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1328 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
1333 * Nope, can't get a line this time...
1337 if (WSAGetLastError() != http
->error
)
1339 http
->error
= WSAGetLastError();
1343 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
1345 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
1349 else if (errno
!= http
->error
)
1351 http
->error
= errno
;
1358 else if (bytes
== 0)
1360 http
->error
= EPIPE
;
1366 * Yup, update the amount used and the end pointer...
1369 http
->used
+= bytes
;
1374 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1376 http
->activity
= time(NULL
);
1379 * Read a line from the buffer...
1383 bufptr
= http
->buffer
;
1387 while (bufptr
< bufend
&& bytes
< length
)
1391 if (*bufptr
== 0x0a)
1396 else if (*bufptr
== 0x0d)
1399 *lineptr
++ = *bufptr
++;
1406 http
->used
-= bytes
;
1408 memmove(http
->buffer
, bufptr
, http
->used
);
1410 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
1414 DEBUG_puts("httpGets: No new line available!");
1421 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1424 int /* O - Number of bytes written */
1425 httpPrintf(http_t
*http
, /* I - HTTP data */
1426 const char *format
, /* I - printf-style format string */
1427 ...) /* I - Additional args as needed */
1429 int bytes
, /* Number of bytes to write */
1430 nbytes
, /* Number of bytes written */
1431 tbytes
; /* Number of bytes all together */
1432 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1433 *bufptr
; /* Pointer into buffer */
1434 va_list ap
; /* Variable argument pointer */
1437 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
1439 va_start(ap
, format
);
1440 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1443 DEBUG_printf(("httpPrintf: %s", buf
));
1445 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1449 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1451 #endif /* HAVE_SSL */
1452 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1459 if (WSAGetLastError() != http
->error
)
1461 http
->error
= WSAGetLastError();
1467 else if (errno
!= http
->error
)
1469 http
->error
= errno
;
1483 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1486 const char * /* O - Date/time string */
1487 httpGetDateString(time_t t
) /* I - UNIX time */
1490 static char datetime
[256];
1494 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1495 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1496 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1503 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1506 time_t /* O - UNIX time */
1507 httpGetDateTime(const char *s
) /* I - Date/time string */
1509 int i
; /* Looping var */
1510 struct tm tdate
; /* Time/date structure */
1511 char mon
[16]; /* Abbreviated month name */
1512 int day
, year
; /* Day of month and year */
1513 int hour
, min
, sec
; /* Time */
1516 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1519 for (i
= 0; i
< 12; i
++)
1520 if (strcasecmp(mon
, months
[i
]) == 0)
1527 tdate
.tm_mday
= day
;
1528 tdate
.tm_year
= year
- 1900;
1529 tdate
.tm_hour
= hour
;
1534 return (mktime(&tdate
));
1539 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1542 http_status_t
/* O - HTTP status */
1543 httpUpdate(http_t
*http
) /* I - HTTP data */
1545 char line
[1024], /* Line from connection... */
1546 *value
; /* Pointer to value on line */
1547 http_field_t field
; /* Field index */
1548 int major
, minor
; /* HTTP version numbers */
1549 http_status_t status
; /* Authorization status */
1552 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1555 * If we haven't issued any commands, then there is nothing to "update"...
1558 if (http
->state
== HTTP_WAITING
)
1559 return (HTTP_CONTINUE
);
1562 * Grab all of the lines we can from the connection...
1565 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1567 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1569 if (line
[0] == '\0')
1572 * Blank line means the start of the data section (if any). Return
1573 * the result code, too...
1575 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1576 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1580 if (http
->status
== HTTP_CONTINUE
)
1581 return (http
->status
);
1584 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1586 if (http_setup_ssl(http
) != 0)
1589 closesocket(http
->fd
);
1594 return (HTTP_ERROR
);
1597 return (HTTP_CONTINUE
);
1599 #endif /* HAVE_SSL */
1601 httpGetLength(http
);
1603 switch (http
->state
)
1607 case HTTP_POST_RECV
:
1610 case HTTP_POST_SEND
:
1614 http
->state
= HTTP_WAITING
;
1618 return (http
->status
);
1620 else if (strncmp(line
, "HTTP/", 5) == 0)
1623 * Got the beginning of a response...
1626 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, (int *)&status
) != 3)
1627 return (HTTP_ERROR
);
1629 http
->version
= (http_version_t
)(major
* 100 + minor
);
1630 http
->status
= status
;
1632 else if ((value
= strchr(line
, ':')) != NULL
)
1639 while (isspace(*value
& 255))
1643 * Be tolerants of servers that send unknown attribute fields...
1646 if (!strcasecmp(line
, "expect"))
1649 * "Expect: 100-continue" or similar...
1652 http
->expect
= (http_status_t
)atoi(value
);
1654 else if (!strcasecmp(line
, "cookie"))
1657 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1660 httpSetCookie(http
, value
);
1662 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1664 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1668 httpSetField(http
, field
, value
);
1672 http
->status
= HTTP_ERROR
;
1673 return (HTTP_ERROR
);
1678 * See if there was an error...
1681 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
1682 return (http
->status
);
1686 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
1687 strerror(http
->error
)));
1688 http
->status
= HTTP_ERROR
;
1689 return (HTTP_ERROR
);
1693 * If we haven't already returned, then there is nothing new...
1696 return (HTTP_CONTINUE
);
1701 * 'httpDecode64()' - Base64-decode a string.
1704 char * /* O - Decoded string */
1705 httpDecode64(char *out
, /* I - String to write to */
1706 const char *in
) /* I - String to read from */
1708 int pos
, /* Bit position */
1709 base64
; /* Value of this character */
1710 char *outptr
; /* Output pointer */
1713 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1716 * Decode this character into a number from 0 to 63...
1719 if (*in
>= 'A' && *in
<= 'Z')
1721 else if (*in
>= 'a' && *in
<= 'z')
1722 base64
= *in
- 'a' + 26;
1723 else if (*in
>= '0' && *in
<= '9')
1724 base64
= *in
- '0' + 52;
1725 else if (*in
== '+')
1727 else if (*in
== '/')
1729 else if (*in
== '=')
1735 * Store the result in the appropriate chars...
1741 *outptr
= base64
<< 2;
1745 *outptr
++ |= (base64
>> 4) & 3;
1746 *outptr
= (base64
<< 4) & 255;
1750 *outptr
++ |= (base64
>> 2) & 15;
1751 *outptr
= (base64
<< 6) & 255;
1755 *outptr
++ |= base64
;
1764 * Return the decoded string...
1772 * 'httpEncode64()' - Base64-encode a string.
1775 char * /* O - Encoded string */
1776 httpEncode64(char *out
, /* I - String to write to */
1777 const char *in
) /* I - String to read from */
1779 char *outptr
; /* Output pointer */
1780 static const char base64
[] = /* Base64 characters... */
1782 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1783 "abcdefghijklmnopqrstuvwxyz"
1789 for (outptr
= out
; *in
!= '\0'; in
++)
1792 * Encode the up to 3 characters as 4 Base64 numbers...
1795 *outptr
++ = base64
[in
[0] >> 2];
1796 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1806 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1815 *outptr
++ = base64
[in
[0] & 63];
1821 * Return the encoded string...
1829 * 'httpGetLength()' - Get the amount of data remaining from the
1830 * content-length or transfer-encoding fields.
1833 int /* O - Content length */
1834 httpGetLength(http_t
*http
) /* I - HTTP data */
1836 DEBUG_printf(("httpGetLength(http=%p), state=%d\n", http
, http
->state
));
1838 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1840 DEBUG_puts("httpGetLength: chunked request!");
1842 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1843 http
->data_remaining
= 0;
1847 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1850 * The following is a hack for HTTP servers that don't send a
1851 * content-length or transfer-encoding field...
1853 * If there is no content-length then the connection must close
1854 * after the transfer is complete...
1857 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1858 http
->data_remaining
= 2147483647;
1860 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1862 DEBUG_printf(("httpGetLength: content_length=%d\n", http
->data_remaining
));
1865 return (http
->data_remaining
);
1870 * 'http_field()' - Return the field index for a field name.
1873 static http_field_t
/* O - Field index */
1874 http_field(const char *name
) /* I - String name */
1876 int i
; /* Looping var */
1879 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1880 if (strcasecmp(name
, http_fields
[i
]) == 0)
1881 return ((http_field_t
)i
);
1883 return (HTTP_FIELD_UNKNOWN
);
1888 * 'http_send()' - Send a request with all fields and the trailing blank line.
1891 static int /* O - 0 on success, non-zero on error */
1892 http_send(http_t
*http
, /* I - HTTP data */
1893 http_state_t request
, /* I - Request code */
1894 const char *uri
) /* I - URI */
1896 int i
; /* Looping var */
1897 char *ptr
, /* Pointer in buffer */
1898 buf
[1024]; /* Encoded URI buffer */
1899 static const char * const codes
[] =
1900 { /* Request code strings */
1915 static const char hex
[] = "0123456789ABCDEF";
1919 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
1920 http
, codes
[request
], uri
));
1922 if (http
== NULL
|| uri
== NULL
)
1926 * Encode the URI as needed...
1929 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1930 if (*uri
<= ' ' || *uri
>= 127)
1932 if (ptr
< (buf
+ sizeof(buf
) - 1))
1934 if (ptr
< (buf
+ sizeof(buf
) - 1))
1935 *ptr
++ = hex
[(*uri
>> 4) & 15];
1936 if (ptr
< (buf
+ sizeof(buf
) - 1))
1937 *ptr
++ = hex
[*uri
& 15];
1945 * See if we had an error the last time around; if so, reconnect...
1948 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1949 httpReconnect(http
);
1952 * Send the request header...
1955 http
->state
= request
;
1956 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1959 http
->status
= HTTP_CONTINUE
;
1962 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1964 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1965 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1967 #endif /* HAVE_SSL */
1969 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1971 http
->status
= HTTP_ERROR
;
1975 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1976 if (http
->fields
[i
][0] != '\0')
1978 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1980 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1982 http
->status
= HTTP_ERROR
;
1987 if (httpPrintf(http
, "\r\n") < 1)
1989 http
->status
= HTTP_ERROR
;
1993 httpClearFields(http
);
2000 * 'http_wait()' - Wait for data available on a connection.
2003 static int /* O - 1 if data is available, 0 otherwise */
2004 http_wait(http_t
*http
, /* I - HTTP data */
2005 int msec
) /* I - Milliseconds to wait */
2008 struct rlimit limit
; /* Runtime limit */
2010 struct timeval timeout
; /* Timeout */
2011 int nfds
; /* Result from select() */
2012 int set_size
; /* Size of select set */
2015 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http
, msec
));
2018 * Check the SSL/TLS buffers for data first...
2025 if (SSL_pending((SSL
*)(http
->tls
)))
2027 # elif defined(HAVE_GNUTLS)
2028 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2030 # elif defined(HAVE_CDSASSL)
2031 size_t bytes
; /* Bytes that are available */
2033 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2035 # endif /* HAVE_LIBSSL */
2037 #endif /* HAVE_SSL */
2040 * Then try doing a select() to poll the socket...
2043 if (!http
->input_set
)
2047 * Windows has a fixed-size select() structure, different (surprise,
2048 * surprise!) from all UNIX implementations. Just allocate this
2049 * fixed structure...
2052 http
->input_set
= calloc(1, sizeof(fd_set
));
2055 * Allocate the select() input set based upon the max number of file
2056 * descriptors available for this process...
2059 getrlimit(RLIMIT_NOFILE
, &limit
);
2061 set_size
= (limit
.rlim_cur
+ 31) / 8;
2062 if (set_size
< sizeof(fd_set
))
2063 set_size
= sizeof(fd_set
);
2065 http
->input_set
= calloc(1, set_size
);
2068 if (!http
->input_set
)
2072 FD_SET(http
->fd
, http
->input_set
);
2076 timeout
.tv_sec
= msec
/ 1000;
2077 timeout
.tv_usec
= (msec
% 1000) * 1000;
2079 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2082 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2084 FD_CLR(http
->fd
, http
->input_set
);
2092 * 'http_upgrade()' - Force upgrade to TLS encryption.
2095 static int /* O - Status of connection */
2096 http_upgrade(http_t
*http
) /* I - HTTP data */
2098 int ret
; /* Return value */
2099 http_t myhttp
; /* Local copy of HTTP data */
2102 DEBUG_printf(("http_upgrade(%p)\n", http
));
2105 * Copy the HTTP data to a local variable so we can do the OPTIONS
2106 * request without interfering with the existing request data...
2109 memcpy(&myhttp
, http
, sizeof(myhttp
));
2112 * Send an OPTIONS request to the server, requiring SSL or TLS
2113 * encryption on the link...
2116 httpClearFields(&myhttp
);
2117 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2118 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2120 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2123 * Wait for the secure connection...
2126 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2132 * Copy the HTTP data back over, if any...
2135 http
->fd
= myhttp
.fd
;
2136 http
->error
= myhttp
.error
;
2137 http
->activity
= myhttp
.activity
;
2138 http
->status
= myhttp
.status
;
2139 http
->version
= myhttp
.version
;
2140 http
->keep_alive
= myhttp
.keep_alive
;
2141 http
->used
= myhttp
.used
;
2144 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2146 http
->auth_type
= myhttp
.auth_type
;
2147 http
->nonce_count
= myhttp
.nonce_count
;
2149 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2151 http
->tls
= myhttp
.tls
;
2152 http
->encryption
= myhttp
.encryption
;
2155 * See if we actually went secure...
2161 * Server does not support HTTP upgrade...
2164 DEBUG_puts("Server does not support HTTP upgrade!");
2167 closesocket(http
->fd
);
2182 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2185 static int /* O - Status of connection */
2186 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2189 SSL_CTX
*context
; /* Context for encryption */
2190 SSL
*conn
; /* Connection for encryption */
2191 # elif defined(HAVE_GNUTLS)
2192 http_tls_t
*conn
; /* TLS session object */
2193 gnutls_certificate_client_credentials
*credentials
;
2194 /* TLS credentials */
2195 # elif defined(HAVE_CDSASSL)
2196 SSLContextRef conn
; /* Context for encryption */
2197 OSStatus error
; /* Error info */
2198 # endif /* HAVE_LIBSSL */
2201 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
2204 context
= SSL_CTX_new(SSLv23_client_method());
2205 conn
= SSL_new(context
);
2207 SSL_set_fd(conn
, http
->fd
);
2208 if (SSL_connect(conn
) != 1)
2211 unsigned long error
; /* Error code */
2213 while ((error
= ERR_get_error()) != 0)
2214 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
2217 SSL_CTX_free(context
);
2221 http
->error
= WSAGetLastError();
2223 http
->error
= errno
;
2225 http
->status
= HTTP_ERROR
;
2227 return (HTTP_ERROR
);
2230 # elif defined(HAVE_GNUTLS)
2231 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2235 http
->error
= errno
;
2236 http
->status
= HTTP_ERROR
;
2241 credentials
= (gnutls_certificate_client_credentials
*)
2242 malloc(sizeof(gnutls_certificate_client_credentials
));
2243 if (credentials
== NULL
)
2247 http
->error
= errno
;
2248 http
->status
= HTTP_ERROR
;
2253 gnutls_certificate_allocate_credentials(credentials
);
2255 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2256 gnutls_set_default_priority(conn
->session
);
2257 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2258 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2260 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2262 http
->error
= errno
;
2263 http
->status
= HTTP_ERROR
;
2268 conn
->credentials
= credentials
;
2270 # elif defined(HAVE_CDSASSL)
2271 error
= SSLNewContext(false, &conn
);
2274 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2277 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2280 error
= SSLSetAllowsExpiredCerts(conn
, true);
2283 error
= SSLSetAllowsAnyRoot(conn
, true);
2286 error
= SSLHandshake(conn
);
2290 http
->error
= error
;
2291 http
->status
= HTTP_ERROR
;
2293 SSLDisposeContext(conn
);
2299 # endif /* HAVE_CDSASSL */
2307 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2311 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2314 SSL_CTX
*context
; /* Context for encryption */
2315 SSL
*conn
; /* Connection for encryption */
2318 conn
= (SSL
*)(http
->tls
);
2319 context
= SSL_get_SSL_CTX(conn
);
2322 SSL_CTX_free(context
);
2325 # elif defined(HAVE_GNUTLS)
2326 http_tls_t
*conn
; /* Encryption session */
2327 gnutls_certificate_client_credentials
*credentials
;
2328 /* TLS credentials */
2331 conn
= (http_tls_t
*)(http
->tls
);
2332 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2334 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2335 gnutls_deinit(conn
->session
);
2336 gnutls_certificate_free_credentials(*credentials
);
2340 # elif defined(HAVE_CDSASSL)
2341 SSLClose((SSLContextRef
)http
->tls
);
2342 SSLDisposeContext((SSLContextRef
)http
->tls
);
2343 # endif /* HAVE_LIBSSL */
2350 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2353 static int /* O - Bytes read */
2354 http_read_ssl(http_t
*http
, /* I - HTTP data */
2355 char *buf
, /* I - Buffer to store data */
2356 int len
) /* I - Length of buffer */
2358 # if defined(HAVE_LIBSSL)
2359 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2361 # elif defined(HAVE_GNUTLS)
2362 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2364 # elif defined(HAVE_CDSASSL)
2365 OSStatus error
; /* Error info */
2366 size_t processed
; /* Number of bytes processed */
2369 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2375 http
->error
= error
;
2379 # endif /* HAVE_LIBSSL */
2384 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2387 static int /* O - Bytes written */
2388 http_write_ssl(http_t
*http
, /* I - HTTP data */
2389 const char *buf
, /* I - Buffer holding data */
2390 int len
) /* I - Length of buffer */
2392 # if defined(HAVE_LIBSSL)
2393 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2395 # elif defined(HAVE_GNUTLS)
2396 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2397 # elif defined(HAVE_CDSASSL)
2398 OSStatus error
; /* Error info */
2399 size_t processed
; /* Number of bytes processed */
2402 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2408 http
->error
= error
;
2411 # endif /* HAVE_LIBSSL */
2415 # if defined(HAVE_CDSASSL)
2417 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2420 static OSStatus
/* O - -1 on error, 0 on success */
2421 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2422 void *data
, /* I - Data buffer */
2423 size_t *dataLength
) /* IO - Number of bytes */
2425 ssize_t bytes
; /* Number of bytes read */
2428 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2431 *dataLength
= bytes
;
2440 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2443 static OSStatus
/* O - -1 on error, 0 on success */
2444 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2445 const void *data
, /* I - Data buffer */
2446 size_t *dataLength
) /* IO - Number of bytes */
2451 bytes
= write((int)connection
, data
, *dataLength
);
2454 *dataLength
= bytes
;
2460 # endif /* HAVE_CDSASSL */
2461 #endif /* HAVE_SSL */
2465 * End of "$Id: http.c,v 1.135 2004/04/20 14:13:54 mike Exp $".