2 * "$Id: http.c,v 1.82.2.37 2003/08/04 19:01:42 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_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).
199 struct timeval curtime
; /* Current time in microseconds */
200 int i
; /* Looping var */
201 unsigned char data
[1024]; /* Seed data */
202 #endif /* HAVE_LIBSSL */
205 WSADATA winsockdata
; /* WinSock data */
206 static int initialized
= 0;/* Has WinSock been initialized? */
210 WSAStartup(MAKEWORD(1,1), &winsockdata
);
211 #elif defined(HAVE_SIGSET)
212 sigset(SIGPIPE
, SIG_IGN
);
213 #elif defined(HAVE_SIGACTION)
214 struct sigaction action
; /* POSIX sigaction data */
218 * Ignore SIGPIPE signals...
221 memset(&action
, 0, sizeof(action
));
222 action
.sa_handler
= SIG_IGN
;
223 sigaction(SIGPIPE
, &action
, NULL
);
225 signal(SIGPIPE
, SIG_IGN
);
229 gnutls_global_init();
230 #endif /* HAVE_GNUTLS */
233 SSL_load_error_strings();
237 * Using the current time is a dubious random seed, but on some systems
238 * it is the best we can do (on others, this seed isn't even used...)
241 gettimeofday(&curtime
, NULL
);
242 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
244 for (i
= 0; i
< sizeof(data
); i
++)
245 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
247 RAND_seed(&data
, sizeof(data
));
248 #endif /* HAVE_LIBSSL */
253 * 'httpCheck()' - Check to see if there is a pending response from the server.
256 int /* O - 0 = no data, 1 = data available */
257 httpCheck(http_t
*http
) /* I - HTTP connection */
259 return (httpWait(http
, 0));
264 * 'httpClearCookie()' - Clear the cookie value(s).
268 httpClearCookie(http_t
*http
) /* I - Connection */
282 * 'httpClose()' - Close an HTTP connection...
286 httpClose(http_t
*http
) /* I - Connection to close */
292 free(http
->input_set
);
299 http_shutdown_ssl(http
);
300 #endif /* HAVE_SSL */
303 closesocket(http
->fd
);
313 * 'httpConnect()' - Connect to a HTTP server.
316 http_t
* /* O - New HTTP connection */
317 httpConnect(const char *host
, /* I - Host to connect to */
318 int port
) /* I - Port number */
320 http_encryption_t encrypt
;/* Type of encryption to use */
324 * Set the default encryption status...
328 encrypt
= HTTP_ENCRYPT_ALWAYS
;
330 encrypt
= HTTP_ENCRYPT_IF_REQUESTED
;
332 return (httpConnectEncrypt(host
, port
, encrypt
));
337 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
340 http_t
* /* O - New HTTP connection */
341 httpConnectEncrypt(const char *host
, /* I - Host to connect to */
342 int port
, /* I - Port number */
343 http_encryption_t encrypt
)
344 /* I - Type of encryption to use */
346 int i
; /* Looping var */
347 http_t
*http
; /* New HTTP connection */
348 struct hostent
*hostaddr
; /* Host address data */
360 if ((hostaddr
= httpGetHostByName(host
)) == NULL
)
363 * This hack to make users that don't have a localhost entry in
364 * their hosts file or DNS happy...
367 if (strcasecmp(host
, "localhost") != 0)
369 else if ((hostaddr
= httpGetHostByName("127.0.0.1")) == NULL
)
374 * Verify that it is an IPv4 address (IPv6 support will come in CUPS 1.2...)
378 if ((hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4) &&
379 (hostaddr
->h_addrtype
!= AF_INET6
|| hostaddr
->h_length
!= 16))
382 if (hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4)
384 #endif /* AF_INET6 */
387 * Allocate memory for the structure...
390 http
= calloc(sizeof(http_t
), 1);
394 http
->version
= HTTP_1_1
;
396 http
->activity
= time(NULL
);
400 * Set the encryption status...
403 if (port
== 443) /* Always use encryption for https */
404 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
406 http
->encryption
= encrypt
;
409 * Loop through the addresses we have until one of them connects...
412 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
414 for (i
= 0; hostaddr
->h_addr_list
[i
]; i
++)
417 * Load the address...
420 httpAddrLoad(hostaddr
, port
, i
, &(http
->hostaddr
));
423 * Connect to the remote system...
426 if (!httpReconnect(http
))
431 * Return the new structure if we could connect; otherwise, bail out...
434 if (hostaddr
->h_addr_list
[i
])
445 * 'httpEncryption()' - Set the required encryption on the link.
448 int /* O - -1 on error, 0 on success */
449 httpEncryption(http_t
*http
, /* I - HTTP data */
450 http_encryption_t e
) /* I - New encryption preference */
456 http
->encryption
= e
;
458 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
459 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
460 return (httpReconnect(http
));
461 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
462 return (http_upgrade(http
));
466 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
470 #endif /* HAVE_SSL */
475 * 'httpReconnect()' - Reconnect to a HTTP server...
478 int /* O - 0 on success, non-zero on failure */
479 httpReconnect(http_t
*http
) /* I - HTTP data */
481 int val
; /* Socket option value */
485 http_shutdown_ssl(http
);
486 #endif /* HAVE_SSL */
489 * Close any previously open socket...
494 closesocket(http
->fd
);
500 * Create the socket and set options to allow reuse.
503 if ((http
->fd
= socket(http
->hostaddr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
506 http
->error
= WSAGetLastError();
510 http
->status
= HTTP_ERROR
;
515 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
516 * other processes... */
517 #endif /* FD_CLOEXEC */
520 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
524 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
525 #endif /* SO_REUSEPORT */
528 * Using TCP_NODELAY improves responsiveness, especially on systems
529 * with a slow loopback interface... Since we write large buffers
530 * when sending print files and requests, there shouldn't be any
531 * performance penalty for this...
535 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
538 * Connect to the server...
542 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
543 http
->hostaddr
.addr
.sa_family
== AF_INET
?
544 sizeof(http
->hostaddr
.ipv4
) :
545 sizeof(http
->hostaddr
.ipv6
)) < 0)
547 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
548 sizeof(http
->hostaddr
.ipv4
)) < 0)
549 #endif /* AF_INET6 */
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 for (fptr
= http
->fields
[field
]; *fptr
;)
622 * Skip leading whitespace...
625 while (isspace(*fptr
))
635 * Get the sub-field name...
639 *fptr
&& *fptr
!= '=' && !isspace(*fptr
) && ptr
< (temp
+ sizeof(temp
) - 1);
645 * Skip trailing chars up to the '='...
648 while (*fptr
&& *fptr
!= '=')
655 * Skip = and leading whitespace...
660 while (isspace(*fptr
))
666 * Read quoted string...
669 for (ptr
= value
, fptr
++;
670 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
675 while (*fptr
&& *fptr
!= '\"')
684 * Read unquoted string...
688 *fptr
&& !isspace(*fptr
) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
693 while (*fptr
&& !isspace(*fptr
) && *fptr
!= ',')
698 * See if this is the one...
701 if (strcmp(name
, temp
) == 0)
712 * 'httpSetField()' - Set the value of an HTTP header.
716 httpSetField(http_t
*http
, /* I - HTTP data */
717 http_field_t field
, /* I - Field index */
718 const char *value
) /* I - Value */
721 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
722 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
726 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
731 * 'httpDelete()' - Send a DELETE request to the server.
734 int /* O - Status of call (0 = success) */
735 httpDelete(http_t
*http
, /* I - HTTP data */
736 const char *uri
) /* I - URI to delete */
738 return (http_send(http
, HTTP_DELETE
, uri
));
743 * 'httpGet()' - Send a GET request to the server.
746 int /* O - Status of call (0 = success) */
747 httpGet(http_t
*http
, /* I - HTTP data */
748 const char *uri
) /* I - URI to get */
750 return (http_send(http
, HTTP_GET
, uri
));
755 * 'httpHead()' - Send a HEAD request to the server.
758 int /* O - Status of call (0 = success) */
759 httpHead(http_t
*http
, /* I - HTTP data */
760 const char *uri
) /* I - URI for head */
762 return (http_send(http
, HTTP_HEAD
, uri
));
767 * 'httpOptions()' - Send an OPTIONS request to the server.
770 int /* O - Status of call (0 = success) */
771 httpOptions(http_t
*http
, /* I - HTTP data */
772 const char *uri
) /* I - URI for options */
774 return (http_send(http
, HTTP_OPTIONS
, uri
));
779 * 'httpPost()' - Send a POST request to the server.
782 int /* O - Status of call (0 = success) */
783 httpPost(http_t
*http
, /* I - HTTP data */
784 const char *uri
) /* I - URI for post */
788 return (http_send(http
, HTTP_POST
, uri
));
793 * 'httpPut()' - Send a PUT request to the server.
796 int /* O - Status of call (0 = success) */
797 httpPut(http_t
*http
, /* I - HTTP data */
798 const char *uri
) /* I - URI to put */
802 return (http_send(http
, HTTP_PUT
, uri
));
807 * 'httpTrace()' - Send an TRACE request to the server.
810 int /* O - Status of call (0 = success) */
811 httpTrace(http_t
*http
, /* I - HTTP data */
812 const char *uri
) /* I - URI for trace */
814 return (http_send(http
, HTTP_TRACE
, uri
));
819 * 'httpFlush()' - Flush data from a HTTP connection.
823 httpFlush(http_t
*http
) /* I - HTTP data */
825 char buffer
[8192]; /* Junk buffer */
828 if (http
->state
!= HTTP_WAITING
)
830 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
836 * 'httpRead()' - Read data from a HTTP connection.
839 int /* O - Number of bytes read */
840 httpRead(http_t
*http
, /* I - HTTP data */
841 char *buffer
, /* I - Buffer for data */
842 int length
) /* I - Maximum number of bytes */
844 int bytes
; /* Bytes read */
845 char len
[32]; /* Length string */
848 DEBUG_printf(("httpRead(%p, %p, %d)\n", http
, buffer
, length
));
850 if (http
== NULL
|| buffer
== NULL
)
853 http
->activity
= time(NULL
);
858 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
859 http
->data_remaining
<= 0)
861 DEBUG_puts("httpRead: Getting chunk length...");
863 if (httpGets(len
, sizeof(len
), http
) == NULL
)
865 DEBUG_puts("httpRead: Could not get length!");
869 http
->data_remaining
= strtol(len
, NULL
, 16);
870 if (http
->data_remaining
< 0)
872 DEBUG_puts("httpRead: Negative chunk length!");
877 DEBUG_printf(("httpRead: data_remaining = %d\n", http
->data_remaining
));
879 if (http
->data_remaining
<= 0)
882 * A zero-length chunk ends a transfer; unless we are reading POST
886 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
887 httpGets(len
, sizeof(len
), http
);
889 if (http
->state
== HTTP_POST_RECV
)
892 http
->state
= HTTP_WAITING
;
896 else if (length
> http
->data_remaining
)
897 length
= http
->data_remaining
;
899 if (http
->used
== 0 && length
<= 256)
902 * Buffer small reads for better performance...
905 if (!http
->blocking
&& !httpWait(http
, 1000))
908 if (http
->data_remaining
> sizeof(http
->buffer
))
909 bytes
= sizeof(http
->buffer
);
911 bytes
= http
->data_remaining
;
915 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
917 #endif /* HAVE_SSL */
919 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
922 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
924 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
933 http
->error
= WSAGetLastError();
952 if (length
> http
->used
)
957 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
959 memcpy(buffer
, http
->buffer
, length
);
960 http
->used
-= length
;
963 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
968 if (!http
->blocking
&& !httpWait(http
, 1000))
971 bytes
= http_read_ssl(http
, buffer
, length
);
973 #endif /* HAVE_SSL */
976 if (!http
->blocking
&& !httpWait(http
, 1000))
979 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
980 bytes
= recv(http
->fd
, buffer
, length
, 0);
981 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
985 http
->data_remaining
-= bytes
;
989 http
->error
= WSAGetLastError();
1003 if (http
->data_remaining
== 0)
1005 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1006 httpGets(len
, sizeof(len
), http
);
1008 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1010 if (http
->state
== HTTP_POST_RECV
)
1013 http
->state
= HTTP_WAITING
;
1020 printf("httpRead: Read %d bytes:\n", bytes
);
1021 for (i
= 0; i
< bytes
; i
+= 16)
1025 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1026 printf(" %02X", buffer
[i
+ j
] & 255);
1035 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1037 ch
= buffer
[i
+ j
] & 255;
1039 if (ch
< ' ' || ch
== 127)
1054 * 'httpSetCookie()' - Set the cookie value(s)...
1058 httpSetCookie(http_t
*http
, /* I - Connection */
1059 const char *cookie
) /* I - Cookie string */
1068 http
->cookie
= strdup(cookie
);
1070 http
->cookie
= NULL
;
1075 * 'httpWait()' - Wait for data available on a connection.
1078 int /* O - 1 if data is available, 0 otherwise */
1079 httpWait(http_t
*http
, /* I - HTTP data */
1080 int msec
) /* I - Milliseconds to wait */
1083 * First see if there is data in the buffer...
1093 * If not, check the SSL/TLS buffers and do a select() on the connection...
1096 return (http_wait(http
, msec
));
1101 * 'httpWrite()' - Write data to a HTTP connection.
1104 int /* O - Number of bytes written */
1105 httpWrite(http_t
*http
, /* I - HTTP data */
1106 const char *buffer
, /* I - Buffer for data */
1107 int length
) /* I - Number of bytes to write */
1109 int tbytes
, /* Total bytes sent */
1110 bytes
; /* Bytes sent */
1113 if (http
== NULL
|| buffer
== NULL
)
1116 http
->activity
= time(NULL
);
1118 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1120 if (httpPrintf(http
, "%x\r\n", length
) < 0)
1126 * A zero-length chunk ends a transfer; unless we are sending POST
1130 DEBUG_puts("httpWrite: changing states...");
1132 if (http
->state
== HTTP_POST_RECV
)
1134 else if (http
->state
== HTTP_PUT_RECV
)
1135 http
->state
= HTTP_STATUS
;
1137 http
->state
= HTTP_WAITING
;
1139 if (httpPrintf(http
, "\r\n") < 0)
1152 bytes
= http_write_ssl(http
, buffer
, length
);
1154 #endif /* HAVE_SSL */
1155 bytes
= send(http
->fd
, buffer
, length
, 0);
1160 if (WSAGetLastError() != http
->error
)
1162 http
->error
= WSAGetLastError();
1168 else if (errno
!= http
->error
)
1170 http
->error
= errno
;
1175 DEBUG_puts("httpWrite: error writing data...\n");
1183 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1184 http
->data_remaining
-= bytes
;
1187 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1188 if (httpPrintf(http
, "\r\n") < 0)
1191 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1194 * Finished with the transfer; unless we are sending POST data, go idle...
1197 DEBUG_puts("httpWrite: changing states...");
1199 if (http
->state
== HTTP_POST_RECV
)
1202 http
->state
= HTTP_WAITING
;
1208 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1209 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1213 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1214 printf(" %02X", buffer
[i
+ j
] & 255);
1223 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1225 ch
= buffer
[i
+ j
] & 255;
1227 if (ch
< ' ' || ch
== 127)
1241 * 'httpGets()' - Get a line of text from a HTTP connection.
1244 char * /* O - Line or NULL */
1245 httpGets(char *line
, /* I - Line to read into */
1246 int length
, /* I - Max length of buffer */
1247 http_t
*http
) /* I - HTTP data */
1249 char *lineptr
, /* Pointer into line */
1250 *bufptr
, /* Pointer into input buffer */
1251 *bufend
; /* Pointer to end of buffer */
1252 int bytes
; /* Number of bytes read */
1255 DEBUG_printf(("httpGets(%p, %d, %p)\n", line
, length
, http
));
1257 if (http
== NULL
|| line
== NULL
)
1261 * Pre-scan the buffer and see if there is a newline in there...
1272 bufptr
= http
->buffer
;
1273 bufend
= http
->buffer
+ http
->used
;
1275 while (bufptr
< bufend
)
1276 if (*bufptr
== 0x0a)
1281 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1284 * No newline; see if there is more data to be read...
1287 if (!http
->blocking
&& !http_wait(http
, 1000))
1292 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1294 #endif /* HAVE_SSL */
1295 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1300 * Nope, can't get a line this time...
1304 if (WSAGetLastError() != http
->error
)
1306 http
->error
= WSAGetLastError();
1310 DEBUG_printf(("httpGets(): recv() error %d!\n", WSAGetLastError()));
1314 else if (errno
!= http
->error
)
1316 http
->error
= errno
;
1320 DEBUG_printf(("httpGets(): recv() error %d!\n", errno
));
1325 else if (bytes
== 0)
1327 http
->error
= EPIPE
;
1333 * Yup, update the amount used and the end pointer...
1336 http
->used
+= bytes
;
1341 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1343 http
->activity
= time(NULL
);
1346 * Read a line from the buffer...
1350 bufptr
= http
->buffer
;
1354 while (bufptr
< bufend
&& bytes
< length
)
1358 if (*bufptr
== 0x0a)
1363 else if (*bufptr
== 0x0d)
1366 *lineptr
++ = *bufptr
++;
1373 http
->used
-= bytes
;
1375 memmove(http
->buffer
, bufptr
, http
->used
);
1377 DEBUG_printf(("httpGets(): Returning \"%s\"\n", line
));
1381 DEBUG_puts("httpGets(): No new line available!");
1388 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1391 int /* O - Number of bytes written */
1392 httpPrintf(http_t
*http
, /* I - HTTP data */
1393 const char *format
, /* I - printf-style format string */
1394 ...) /* I - Additional args as needed */
1396 int bytes
, /* Number of bytes to write */
1397 nbytes
, /* Number of bytes written */
1398 tbytes
; /* Number of bytes all together */
1399 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1400 *bufptr
; /* Pointer into buffer */
1401 va_list ap
; /* Variable argument pointer */
1404 va_start(ap
, format
);
1405 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1408 DEBUG_printf(("httpPrintf: %s", buf
));
1410 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1414 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1416 #endif /* HAVE_SSL */
1417 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1424 if (WSAGetLastError() != http
->error
)
1426 http
->error
= WSAGetLastError();
1432 else if (errno
!= http
->error
)
1434 http
->error
= errno
;
1448 * 'httpStatus()' - Return a short string describing a HTTP status code.
1451 const char * /* O - String or NULL */
1452 httpStatus(http_status_t status
) /* I - HTTP status code */
1456 case HTTP_CONTINUE
:
1457 return ("Continue");
1458 case HTTP_SWITCHING_PROTOCOLS
:
1459 return ("Switching Protocols");
1464 case HTTP_ACCEPTED
:
1465 return ("Accepted");
1466 case HTTP_NO_CONTENT
:
1467 return ("No Content");
1468 case HTTP_NOT_MODIFIED
:
1469 return ("Not Modified");
1470 case HTTP_BAD_REQUEST
:
1471 return ("Bad Request");
1472 case HTTP_UNAUTHORIZED
:
1473 return ("Unauthorized");
1474 case HTTP_FORBIDDEN
:
1475 return ("Forbidden");
1476 case HTTP_NOT_FOUND
:
1477 return ("Not Found");
1478 case HTTP_REQUEST_TOO_LARGE
:
1479 return ("Request Entity Too Large");
1480 case HTTP_URI_TOO_LONG
:
1481 return ("URI Too Long");
1482 case HTTP_UPGRADE_REQUIRED
:
1483 return ("Upgrade Required");
1484 case HTTP_NOT_IMPLEMENTED
:
1485 return ("Not Implemented");
1486 case HTTP_NOT_SUPPORTED
:
1487 return ("Not Supported");
1495 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1498 const char * /* O - Date/time string */
1499 httpGetDateString(time_t t
) /* I - UNIX time */
1502 static char datetime
[256];
1506 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1507 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1508 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1515 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1518 time_t /* O - UNIX time */
1519 httpGetDateTime(const char *s
) /* I - Date/time string */
1521 int i
; /* Looping var */
1522 struct tm tdate
; /* Time/date structure */
1523 char mon
[16]; /* Abbreviated month name */
1524 int day
, year
; /* Day of month and year */
1525 int hour
, min
, sec
; /* Time */
1528 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1531 for (i
= 0; i
< 12; i
++)
1532 if (strcasecmp(mon
, months
[i
]) == 0)
1539 tdate
.tm_mday
= day
;
1540 tdate
.tm_year
= year
- 1900;
1541 tdate
.tm_hour
= hour
;
1546 return (mktime(&tdate
));
1551 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1554 http_status_t
/* O - HTTP status */
1555 httpUpdate(http_t
*http
) /* I - HTTP data */
1557 char line
[1024], /* Line from connection... */
1558 *value
; /* Pointer to value on line */
1559 http_field_t field
; /* Field index */
1560 int major
, minor
; /* HTTP version numbers */
1561 http_status_t status
; /* Authorization status */
1564 DEBUG_printf(("httpUpdate(%p)\n", http
));
1567 * If we haven't issued any commands, then there is nothing to "update"...
1570 if (http
->state
== HTTP_WAITING
)
1571 return (HTTP_CONTINUE
);
1574 * Grab all of the lines we can from the connection...
1577 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1581 if (line
[0] == '\0')
1584 * Blank line means the start of the data section (if any). Return
1585 * the result code, too...
1587 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1588 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1592 if (http
->status
== HTTP_CONTINUE
)
1593 return (http
->status
);
1596 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1598 if (http_setup_ssl(http
) != 0)
1601 closesocket(http
->fd
);
1606 return (HTTP_ERROR
);
1609 return (HTTP_CONTINUE
);
1611 else if (http
->status
== HTTP_UPGRADE_REQUIRED
&&
1612 http
->encryption
!= HTTP_ENCRYPT_NEVER
)
1613 http
->encryption
= HTTP_ENCRYPT_REQUIRED
;
1614 #endif /* HAVE_SSL */
1616 httpGetLength(http
);
1618 switch (http
->state
)
1622 case HTTP_POST_RECV
:
1625 case HTTP_POST_SEND
:
1629 http
->state
= HTTP_WAITING
;
1633 return (http
->status
);
1635 else if (strncmp(line
, "HTTP/", 5) == 0)
1638 * Got the beginning of a response...
1641 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, (int *)&status
) != 3)
1642 return (HTTP_ERROR
);
1644 http
->version
= (http_version_t
)(major
* 100 + minor
);
1645 http
->status
= status
;
1647 else if ((value
= strchr(line
, ':')) != NULL
)
1654 while (isspace(*value
))
1658 * Be tolerants of servers that send unknown attribute fields...
1661 if (!strcasecmp(line
, "expect"))
1664 * "Expect: 100-continue" or similar...
1667 http
->expect
= (http_status_t
)atoi(value
);
1669 else if (!strcasecmp(line
, "cookie"))
1672 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1675 httpSetCookie(http
, value
);
1677 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1679 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1683 httpSetField(http
, field
, value
);
1687 http
->status
= HTTP_ERROR
;
1688 return (HTTP_ERROR
);
1693 * See if there was an error...
1698 http
->status
= HTTP_ERROR
;
1699 return (HTTP_ERROR
);
1703 * If we haven't already returned, then there is nothing new...
1706 return (HTTP_CONTINUE
);
1711 * 'httpDecode64()' - Base64-decode a string.
1714 char * /* O - Decoded string */
1715 httpDecode64(char *out
, /* I - String to write to */
1716 const char *in
) /* I - String to read from */
1718 int pos
, /* Bit position */
1719 base64
; /* Value of this character */
1720 char *outptr
; /* Output pointer */
1723 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1726 * Decode this character into a number from 0 to 63...
1729 if (*in
>= 'A' && *in
<= 'Z')
1731 else if (*in
>= 'a' && *in
<= 'z')
1732 base64
= *in
- 'a' + 26;
1733 else if (*in
>= '0' && *in
<= '9')
1734 base64
= *in
- '0' + 52;
1735 else if (*in
== '+')
1737 else if (*in
== '/')
1739 else if (*in
== '=')
1745 * Store the result in the appropriate chars...
1751 *outptr
= base64
<< 2;
1755 *outptr
++ |= (base64
>> 4) & 3;
1756 *outptr
= (base64
<< 4) & 255;
1760 *outptr
++ |= (base64
>> 2) & 15;
1761 *outptr
= (base64
<< 6) & 255;
1765 *outptr
++ |= base64
;
1774 * Return the decoded string...
1782 * 'httpEncode64()' - Base64-encode a string.
1785 char * /* O - Encoded string */
1786 httpEncode64(char *out
, /* I - String to write to */
1787 const char *in
) /* I - String to read from */
1789 char *outptr
; /* Output pointer */
1790 static const char base64
[] = /* Base64 characters... */
1792 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1793 "abcdefghijklmnopqrstuvwxyz"
1799 for (outptr
= out
; *in
!= '\0'; in
++)
1802 * Encode the up to 3 characters as 4 Base64 numbers...
1805 *outptr
++ = base64
[in
[0] >> 2];
1806 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1815 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1821 *outptr
++ = base64
[in
[0] & 63];
1828 * Return the encoded string...
1836 * 'httpGetLength()' - Get the amount of data remaining from the
1837 * content-length or transfer-encoding fields.
1840 int /* O - Content length */
1841 httpGetLength(http_t
*http
) /* I - HTTP data */
1843 DEBUG_printf(("httpGetLength(%p), state = %d\n", http
, http
->state
));
1845 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1847 DEBUG_puts("httpGetLength: chunked request!");
1849 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1850 http
->data_remaining
= 0;
1854 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1857 * The following is a hack for HTTP servers that don't send a
1858 * content-length or transfer-encoding field...
1860 * If there is no content-length then the connection must close
1861 * after the transfer is complete...
1864 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1865 http
->data_remaining
= 2147483647;
1867 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1869 DEBUG_printf(("httpGetLength: content_length = %d\n", http
->data_remaining
));
1872 return (http
->data_remaining
);
1877 * 'http_field()' - Return the field index for a field name.
1880 static http_field_t
/* O - Field index */
1881 http_field(const char *name
) /* I - String name */
1883 int i
; /* Looping var */
1886 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1887 if (strcasecmp(name
, http_fields
[i
]) == 0)
1888 return ((http_field_t
)i
);
1890 return (HTTP_FIELD_UNKNOWN
);
1895 * 'http_send()' - Send a request with all fields and the trailing blank line.
1898 static int /* O - 0 on success, non-zero on error */
1899 http_send(http_t
*http
, /* I - HTTP data */
1900 http_state_t request
, /* I - Request code */
1901 const char *uri
) /* I - URI */
1903 int i
; /* Looping var */
1904 char *ptr
, /* Pointer in buffer */
1905 buf
[1024]; /* Encoded URI buffer */
1906 static const char * const codes
[] =
1907 { /* Request code strings */
1922 static const char hex
[] = "0123456789ABCDEF";
1926 if (http
== NULL
|| uri
== NULL
)
1930 * Encode the URI as needed...
1933 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1934 if (*uri
<= ' ' || *uri
>= 127)
1936 if (ptr
< (buf
+ sizeof(buf
) - 1))
1938 if (ptr
< (buf
+ sizeof(buf
) - 1))
1939 *ptr
++ = hex
[(*uri
>> 4) & 15];
1940 if (ptr
< (buf
+ sizeof(buf
) - 1))
1941 *ptr
++ = hex
[*uri
& 15];
1949 * See if we had an error the last time around; if so, reconnect...
1952 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1953 httpReconnect(http
);
1956 * Send the request header...
1959 http
->state
= request
;
1960 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1963 http
->status
= HTTP_CONTINUE
;
1966 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1968 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1969 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1971 #endif /* HAVE_SSL */
1973 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1975 http
->status
= HTTP_ERROR
;
1979 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1980 if (http
->fields
[i
][0] != '\0')
1982 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1984 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1986 http
->status
= HTTP_ERROR
;
1991 if (httpPrintf(http
, "\r\n") < 1)
1993 http
->status
= HTTP_ERROR
;
1997 httpClearFields(http
);
2004 * 'http_wait()' - Wait for data available on a connection.
2007 static int /* O - 1 if data is available, 0 otherwise */
2008 http_wait(http_t
*http
, /* I - HTTP data */
2009 int msec
) /* I - Milliseconds to wait */
2012 struct rlimit limit
; /* Runtime limit */
2014 struct timeval timeout
; /* Timeout */
2015 int nfds
; /* Result from select() */
2019 * Check the SSL/TLS buffers for data first...
2026 if (SSL_pending((SSL
*)(http
->tls
)))
2028 # elif defined(HAVE_GNUTLS)
2029 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2031 # elif defined(HAVE_CDSASSL)
2032 size_t bytes
; /* Bytes that are available */
2034 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2036 # endif /* HAVE_LIBSSL */
2038 #endif /* HAVE_SSL */
2041 * Then try doing a select() to poll the socket...
2044 if (!http
->input_set
)
2048 * Windows has a fixed-size select() structure, different (surprise,
2049 * surprise!) from all UNIX implementations. Just allocate this
2050 * fixed structure...
2053 http
->input_set
= calloc(1, sizeof(fd_set
));
2056 * Allocate the select() input set based upon the max number of file
2057 * descriptors available for this process...
2060 getrlimit(RLIMIT_NOFILE
, &limit
);
2062 http
->input_set
= calloc(1, (limit
.rlim_cur
+ 7) / 8);
2065 if (!http
->input_set
)
2069 FD_SET(http
->fd
, http
->input_set
);
2073 timeout
.tv_sec
= msec
/ 1000;
2074 timeout
.tv_usec
= (msec
% 1000) * 1000;
2076 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2079 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2081 FD_CLR(http
->fd
, http
->input_set
);
2089 * 'http_upgrade()' - Force upgrade to TLS encryption.
2092 static int /* O - Status of connection */
2093 http_upgrade(http_t
*http
) /* I - HTTP data */
2095 int ret
; /* Return value */
2096 http_t myhttp
; /* Local copy of HTTP data */
2099 DEBUG_printf(("http_upgrade(%p)\n", http
));
2102 * Copy the HTTP data to a local variable so we can do the OPTIONS
2103 * request without interfering with the existing request data...
2106 memcpy(&myhttp
, http
, sizeof(myhttp
));
2109 * Send an OPTIONS request to the server, requiring SSL or TLS
2110 * encryption on the link...
2113 httpClearFields(&myhttp
);
2114 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2115 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2117 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2120 * Wait for the secure connection...
2123 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2129 * Copy the HTTP data back over, if any...
2132 http
->fd
= myhttp
.fd
;
2133 http
->error
= myhttp
.error
;
2134 http
->activity
= myhttp
.activity
;
2135 http
->status
= myhttp
.status
;
2136 http
->version
= myhttp
.version
;
2137 http
->keep_alive
= myhttp
.keep_alive
;
2138 http
->used
= myhttp
.used
;
2141 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2143 http
->auth_type
= myhttp
.auth_type
;
2144 http
->nonce_count
= myhttp
.nonce_count
;
2146 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2148 http
->tls
= myhttp
.tls
;
2149 http
->encryption
= myhttp
.encryption
;
2152 * See if we actually went secure...
2158 * Server does not support HTTP upgrade...
2161 DEBUG_puts("Server does not support HTTP upgrade!");
2164 closesocket(http
->fd
);
2179 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2182 static int /* O - Status of connection */
2183 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2186 SSL_CTX
*context
; /* Context for encryption */
2187 SSL
*conn
; /* Connection for encryption */
2188 # elif defined(HAVE_GNUTLS)
2189 http_tls_t
*conn
; /* TLS session object */
2190 gnutls_certificate_client_credentials
*credentials
;
2191 /* TLS credentials */
2192 # elif defined(HAVE_CDSASSL)
2193 SSLContextRef conn
; /* Context for encryption */
2194 OSStatus error
; /* Error info */
2195 # endif /* HAVE_LIBSSL */
2199 context
= SSL_CTX_new(SSLv23_client_method());
2200 conn
= SSL_new(context
);
2202 SSL_set_fd(conn
, http
->fd
);
2203 if (SSL_connect(conn
) != 1)
2205 SSL_CTX_free(context
);
2209 http
->error
= WSAGetLastError();
2211 http
->error
= errno
;
2213 http
->status
= HTTP_ERROR
;
2215 return (HTTP_ERROR
);
2218 # elif defined(HAVE_GNUTLS)
2219 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2223 http
->error
= errno
;
2224 http
->status
= HTTP_ERROR
;
2229 credentials
= (gnutls_certificate_client_credentials
*)
2230 malloc(sizeof(gnutls_certificate_client_credentials
));
2231 if (credentials
== NULL
)
2235 http
->error
= errno
;
2236 http
->status
= HTTP_ERROR
;
2241 gnutls_certificate_allocate_credentials(credentials
);
2243 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2244 gnutls_set_default_priority(conn
->session
);
2245 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2246 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2248 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2250 http
->error
= errno
;
2251 http
->status
= HTTP_ERROR
;
2256 conn
->credentials
= credentials
;
2258 # elif defined(HAVE_CDSASSL)
2259 error
= SSLNewContext(false, &conn
);
2262 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2265 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2268 error
= SSLSetAllowsExpiredCerts(conn
, true);
2271 error
= SSLSetAllowsAnyRoot(conn
, true);
2274 error
= SSLHandshake(conn
);
2278 http
->error
= error
;
2279 http
->status
= HTTP_ERROR
;
2281 SSLDisposeContext(conn
);
2287 # endif /* HAVE_CDSASSL */
2295 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2299 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2302 SSL_CTX
*context
; /* Context for encryption */
2303 SSL
*conn
; /* Connection for encryption */
2306 conn
= (SSL
*)(http
->tls
);
2307 context
= SSL_get_SSL_CTX(conn
);
2310 SSL_CTX_free(context
);
2313 # elif defined(HAVE_GNUTLS)
2314 http_tls_t
*conn
; /* Encryption session */
2315 gnutls_certificate_client_credentials
*credentials
;
2316 /* TLS credentials */
2319 conn
= (http_tls_t
*)(http
->tls
);
2320 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2322 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2323 gnutls_deinit(conn
->session
);
2324 gnutls_certificate_free_credentials(*credentials
);
2328 # elif defined(HAVE_CDSASSL)
2329 SSLClose((SSLContextRef
)http
->tls
);
2330 SSLDisposeContext((SSLContextRef
)http
->tls
);
2331 # endif /* HAVE_LIBSSL */
2338 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2341 static int /* O - Bytes read */
2342 http_read_ssl(http_t
*http
, /* I - HTTP data */
2343 char *buf
, /* I - Buffer to store data */
2344 int len
) /* I - Length of buffer */
2346 # if defined(HAVE_LIBSSL)
2347 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2349 # elif defined(HAVE_GNUTLS)
2350 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2352 # elif defined(HAVE_CDSASSL)
2353 OSStatus error
; /* Error info */
2354 size_t processed
; /* Number of bytes processed */
2357 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2363 http
->error
= error
;
2367 # endif /* HAVE_LIBSSL */
2372 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2375 static int /* O - Bytes written */
2376 http_write_ssl(http_t
*http
, /* I - HTTP data */
2377 const char *buf
, /* I - Buffer holding data */
2378 int len
) /* I - Length of buffer */
2380 # if defined(HAVE_LIBSSL)
2381 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2383 # elif defined(HAVE_GNUTLS)
2384 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2385 # elif defined(HAVE_CDSASSL)
2386 OSStatus error
; /* Error info */
2387 size_t processed
; /* Number of bytes processed */
2390 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2396 http
->error
= error
;
2399 # endif /* HAVE_LIBSSL */
2403 # if defined(HAVE_CDSASSL)
2405 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2408 static OSStatus
/* O - -1 on error, 0 on success */
2409 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2410 void *data
, /* I - Data buffer */
2411 size_t *dataLength
) /* IO - Number of bytes */
2413 ssize_t bytes
; /* Number of bytes read */
2416 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2419 *dataLength
= bytes
;
2428 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2431 static OSStatus
/* O - -1 on error, 0 on success */
2432 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2433 const void *data
, /* I - Data buffer */
2434 size_t *dataLength
) /* IO - Number of bytes */
2439 bytes
= write((int)connection
, data
, *dataLength
);
2442 *dataLength
= bytes
;
2448 # endif /* HAVE_CDSASSL */
2449 #endif /* HAVE_SSL */
2453 * End of "$Id: http.c,v 1.82.2.37 2003/08/04 19:01:42 mike Exp $".