2 * "$Id: http.c,v 1.82.2.39 2003/08/29 23:58:37 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
1127 * or PUT data, go idle...
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 or PUT
1198 DEBUG_puts("httpWrite: changing states...");
1200 if (http
->state
== HTTP_POST_RECV
)
1202 else if (http
->state
== HTTP_PUT_RECV
)
1203 http
->state
= HTTP_STATUS
;
1205 http
->state
= HTTP_WAITING
;
1211 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1212 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1216 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1217 printf(" %02X", buffer
[i
+ j
] & 255);
1226 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1228 ch
= buffer
[i
+ j
] & 255;
1230 if (ch
< ' ' || ch
== 127)
1244 * 'httpGets()' - Get a line of text from a HTTP connection.
1247 char * /* O - Line or NULL */
1248 httpGets(char *line
, /* I - Line to read into */
1249 int length
, /* I - Max length of buffer */
1250 http_t
*http
) /* I - HTTP data */
1252 char *lineptr
, /* Pointer into line */
1253 *bufptr
, /* Pointer into input buffer */
1254 *bufend
; /* Pointer to end of buffer */
1255 int bytes
; /* Number of bytes read */
1258 DEBUG_printf(("httpGets(%p, %d, %p)\n", line
, length
, http
));
1260 if (http
== NULL
|| line
== NULL
)
1264 * Pre-scan the buffer and see if there is a newline in there...
1275 bufptr
= http
->buffer
;
1276 bufend
= http
->buffer
+ http
->used
;
1278 while (bufptr
< bufend
)
1279 if (*bufptr
== 0x0a)
1284 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1287 * No newline; see if there is more data to be read...
1290 if (!http
->blocking
&& !http_wait(http
, 1000))
1295 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1297 #endif /* HAVE_SSL */
1298 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1303 * Nope, can't get a line this time...
1307 if (WSAGetLastError() != http
->error
)
1309 http
->error
= WSAGetLastError();
1313 DEBUG_printf(("httpGets(): recv() error %d!\n", WSAGetLastError()));
1317 else if (errno
!= http
->error
)
1319 http
->error
= errno
;
1323 DEBUG_printf(("httpGets(): recv() error %d!\n", errno
));
1328 else if (bytes
== 0)
1330 http
->error
= EPIPE
;
1336 * Yup, update the amount used and the end pointer...
1339 http
->used
+= bytes
;
1344 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1346 http
->activity
= time(NULL
);
1349 * Read a line from the buffer...
1353 bufptr
= http
->buffer
;
1357 while (bufptr
< bufend
&& bytes
< length
)
1361 if (*bufptr
== 0x0a)
1366 else if (*bufptr
== 0x0d)
1369 *lineptr
++ = *bufptr
++;
1376 http
->used
-= bytes
;
1378 memmove(http
->buffer
, bufptr
, http
->used
);
1380 DEBUG_printf(("httpGets(): Returning \"%s\"\n", line
));
1384 DEBUG_puts("httpGets(): No new line available!");
1391 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1394 int /* O - Number of bytes written */
1395 httpPrintf(http_t
*http
, /* I - HTTP data */
1396 const char *format
, /* I - printf-style format string */
1397 ...) /* I - Additional args as needed */
1399 int bytes
, /* Number of bytes to write */
1400 nbytes
, /* Number of bytes written */
1401 tbytes
; /* Number of bytes all together */
1402 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1403 *bufptr
; /* Pointer into buffer */
1404 va_list ap
; /* Variable argument pointer */
1407 va_start(ap
, format
);
1408 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1411 DEBUG_printf(("httpPrintf: %s", buf
));
1413 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1417 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1419 #endif /* HAVE_SSL */
1420 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1427 if (WSAGetLastError() != http
->error
)
1429 http
->error
= WSAGetLastError();
1435 else if (errno
!= http
->error
)
1437 http
->error
= errno
;
1451 * 'httpStatus()' - Return a short string describing a HTTP status code.
1454 const char * /* O - String or NULL */
1455 httpStatus(http_status_t status
) /* I - HTTP status code */
1459 case HTTP_CONTINUE
:
1460 return ("Continue");
1461 case HTTP_SWITCHING_PROTOCOLS
:
1462 return ("Switching Protocols");
1467 case HTTP_ACCEPTED
:
1468 return ("Accepted");
1469 case HTTP_NO_CONTENT
:
1470 return ("No Content");
1471 case HTTP_NOT_MODIFIED
:
1472 return ("Not Modified");
1473 case HTTP_BAD_REQUEST
:
1474 return ("Bad Request");
1475 case HTTP_UNAUTHORIZED
:
1476 return ("Unauthorized");
1477 case HTTP_FORBIDDEN
:
1478 return ("Forbidden");
1479 case HTTP_NOT_FOUND
:
1480 return ("Not Found");
1481 case HTTP_REQUEST_TOO_LARGE
:
1482 return ("Request Entity Too Large");
1483 case HTTP_URI_TOO_LONG
:
1484 return ("URI Too Long");
1485 case HTTP_UPGRADE_REQUIRED
:
1486 return ("Upgrade Required");
1487 case HTTP_NOT_IMPLEMENTED
:
1488 return ("Not Implemented");
1489 case HTTP_NOT_SUPPORTED
:
1490 return ("Not Supported");
1498 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1501 const char * /* O - Date/time string */
1502 httpGetDateString(time_t t
) /* I - UNIX time */
1505 static char datetime
[256];
1509 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1510 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1511 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1518 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1521 time_t /* O - UNIX time */
1522 httpGetDateTime(const char *s
) /* I - Date/time string */
1524 int i
; /* Looping var */
1525 struct tm tdate
; /* Time/date structure */
1526 char mon
[16]; /* Abbreviated month name */
1527 int day
, year
; /* Day of month and year */
1528 int hour
, min
, sec
; /* Time */
1531 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1534 for (i
= 0; i
< 12; i
++)
1535 if (strcasecmp(mon
, months
[i
]) == 0)
1542 tdate
.tm_mday
= day
;
1543 tdate
.tm_year
= year
- 1900;
1544 tdate
.tm_hour
= hour
;
1549 return (mktime(&tdate
));
1554 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1557 http_status_t
/* O - HTTP status */
1558 httpUpdate(http_t
*http
) /* I - HTTP data */
1560 char line
[1024], /* Line from connection... */
1561 *value
; /* Pointer to value on line */
1562 http_field_t field
; /* Field index */
1563 int major
, minor
; /* HTTP version numbers */
1564 http_status_t status
; /* Authorization status */
1567 DEBUG_printf(("httpUpdate(%p)\n", http
));
1570 * If we haven't issued any commands, then there is nothing to "update"...
1573 if (http
->state
== HTTP_WAITING
)
1574 return (HTTP_CONTINUE
);
1577 * Grab all of the lines we can from the connection...
1580 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1584 if (line
[0] == '\0')
1587 * Blank line means the start of the data section (if any). Return
1588 * the result code, too...
1590 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1591 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1595 if (http
->status
== HTTP_CONTINUE
)
1596 return (http
->status
);
1599 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1601 if (http_setup_ssl(http
) != 0)
1604 closesocket(http
->fd
);
1609 return (HTTP_ERROR
);
1612 return (HTTP_CONTINUE
);
1614 else if (http
->status
== HTTP_UPGRADE_REQUIRED
&&
1615 http
->encryption
!= HTTP_ENCRYPT_NEVER
)
1616 http
->encryption
= HTTP_ENCRYPT_REQUIRED
;
1617 #endif /* HAVE_SSL */
1619 httpGetLength(http
);
1621 switch (http
->state
)
1625 case HTTP_POST_RECV
:
1628 case HTTP_POST_SEND
:
1632 http
->state
= HTTP_WAITING
;
1636 return (http
->status
);
1638 else if (strncmp(line
, "HTTP/", 5) == 0)
1641 * Got the beginning of a response...
1644 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, (int *)&status
) != 3)
1645 return (HTTP_ERROR
);
1647 http
->version
= (http_version_t
)(major
* 100 + minor
);
1648 http
->status
= status
;
1650 else if ((value
= strchr(line
, ':')) != NULL
)
1657 while (isspace(*value
))
1661 * Be tolerants of servers that send unknown attribute fields...
1664 if (!strcasecmp(line
, "expect"))
1667 * "Expect: 100-continue" or similar...
1670 http
->expect
= (http_status_t
)atoi(value
);
1672 else if (!strcasecmp(line
, "cookie"))
1675 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1678 httpSetCookie(http
, value
);
1680 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1682 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1686 httpSetField(http
, field
, value
);
1690 http
->status
= HTTP_ERROR
;
1691 return (HTTP_ERROR
);
1696 * See if there was an error...
1701 http
->status
= HTTP_ERROR
;
1702 return (HTTP_ERROR
);
1706 * If we haven't already returned, then there is nothing new...
1709 return (HTTP_CONTINUE
);
1714 * 'httpDecode64()' - Base64-decode a string.
1717 char * /* O - Decoded string */
1718 httpDecode64(char *out
, /* I - String to write to */
1719 const char *in
) /* I - String to read from */
1721 int pos
, /* Bit position */
1722 base64
; /* Value of this character */
1723 char *outptr
; /* Output pointer */
1726 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1729 * Decode this character into a number from 0 to 63...
1732 if (*in
>= 'A' && *in
<= 'Z')
1734 else if (*in
>= 'a' && *in
<= 'z')
1735 base64
= *in
- 'a' + 26;
1736 else if (*in
>= '0' && *in
<= '9')
1737 base64
= *in
- '0' + 52;
1738 else if (*in
== '+')
1740 else if (*in
== '/')
1742 else if (*in
== '=')
1748 * Store the result in the appropriate chars...
1754 *outptr
= base64
<< 2;
1758 *outptr
++ |= (base64
>> 4) & 3;
1759 *outptr
= (base64
<< 4) & 255;
1763 *outptr
++ |= (base64
>> 2) & 15;
1764 *outptr
= (base64
<< 6) & 255;
1768 *outptr
++ |= base64
;
1777 * Return the decoded string...
1785 * 'httpEncode64()' - Base64-encode a string.
1788 char * /* O - Encoded string */
1789 httpEncode64(char *out
, /* I - String to write to */
1790 const char *in
) /* I - String to read from */
1792 char *outptr
; /* Output pointer */
1793 static const char base64
[] = /* Base64 characters... */
1795 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1796 "abcdefghijklmnopqrstuvwxyz"
1802 for (outptr
= out
; *in
!= '\0'; in
++)
1805 * Encode the up to 3 characters as 4 Base64 numbers...
1808 *outptr
++ = base64
[in
[0] >> 2];
1809 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1818 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1824 *outptr
++ = base64
[in
[0] & 63];
1831 * Return the encoded string...
1839 * 'httpGetLength()' - Get the amount of data remaining from the
1840 * content-length or transfer-encoding fields.
1843 int /* O - Content length */
1844 httpGetLength(http_t
*http
) /* I - HTTP data */
1846 DEBUG_printf(("httpGetLength(%p), state = %d\n", http
, http
->state
));
1848 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1850 DEBUG_puts("httpGetLength: chunked request!");
1852 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1853 http
->data_remaining
= 0;
1857 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1860 * The following is a hack for HTTP servers that don't send a
1861 * content-length or transfer-encoding field...
1863 * If there is no content-length then the connection must close
1864 * after the transfer is complete...
1867 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1868 http
->data_remaining
= 2147483647;
1870 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1872 DEBUG_printf(("httpGetLength: content_length = %d\n", http
->data_remaining
));
1875 return (http
->data_remaining
);
1880 * 'http_field()' - Return the field index for a field name.
1883 static http_field_t
/* O - Field index */
1884 http_field(const char *name
) /* I - String name */
1886 int i
; /* Looping var */
1889 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1890 if (strcasecmp(name
, http_fields
[i
]) == 0)
1891 return ((http_field_t
)i
);
1893 return (HTTP_FIELD_UNKNOWN
);
1898 * 'http_send()' - Send a request with all fields and the trailing blank line.
1901 static int /* O - 0 on success, non-zero on error */
1902 http_send(http_t
*http
, /* I - HTTP data */
1903 http_state_t request
, /* I - Request code */
1904 const char *uri
) /* I - URI */
1906 int i
; /* Looping var */
1907 char *ptr
, /* Pointer in buffer */
1908 buf
[1024]; /* Encoded URI buffer */
1909 static const char * const codes
[] =
1910 { /* Request code strings */
1925 static const char hex
[] = "0123456789ABCDEF";
1929 if (http
== NULL
|| uri
== NULL
)
1933 * Encode the URI as needed...
1936 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1937 if (*uri
<= ' ' || *uri
>= 127)
1939 if (ptr
< (buf
+ sizeof(buf
) - 1))
1941 if (ptr
< (buf
+ sizeof(buf
) - 1))
1942 *ptr
++ = hex
[(*uri
>> 4) & 15];
1943 if (ptr
< (buf
+ sizeof(buf
) - 1))
1944 *ptr
++ = hex
[*uri
& 15];
1952 * See if we had an error the last time around; if so, reconnect...
1955 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1956 httpReconnect(http
);
1959 * Send the request header...
1962 http
->state
= request
;
1963 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1966 http
->status
= HTTP_CONTINUE
;
1969 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1971 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1972 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1974 #endif /* HAVE_SSL */
1976 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1978 http
->status
= HTTP_ERROR
;
1982 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1983 if (http
->fields
[i
][0] != '\0')
1985 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1987 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1989 http
->status
= HTTP_ERROR
;
1994 if (httpPrintf(http
, "\r\n") < 1)
1996 http
->status
= HTTP_ERROR
;
2000 httpClearFields(http
);
2007 * 'http_wait()' - Wait for data available on a connection.
2010 static int /* O - 1 if data is available, 0 otherwise */
2011 http_wait(http_t
*http
, /* I - HTTP data */
2012 int msec
) /* I - Milliseconds to wait */
2015 struct rlimit limit
; /* Runtime limit */
2017 struct timeval timeout
; /* Timeout */
2018 int nfds
; /* Result from select() */
2022 * Check the SSL/TLS buffers for data first...
2029 if (SSL_pending((SSL
*)(http
->tls
)))
2031 # elif defined(HAVE_GNUTLS)
2032 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2034 # elif defined(HAVE_CDSASSL)
2035 size_t bytes
; /* Bytes that are available */
2037 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2039 # endif /* HAVE_LIBSSL */
2041 #endif /* HAVE_SSL */
2044 * Then try doing a select() to poll the socket...
2047 if (!http
->input_set
)
2051 * Windows has a fixed-size select() structure, different (surprise,
2052 * surprise!) from all UNIX implementations. Just allocate this
2053 * fixed structure...
2056 http
->input_set
= calloc(1, sizeof(fd_set
));
2059 * Allocate the select() input set based upon the max number of file
2060 * descriptors available for this process...
2063 getrlimit(RLIMIT_NOFILE
, &limit
);
2065 http
->input_set
= calloc(1, (limit
.rlim_cur
+ 31) / 32);
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 */
2202 context
= SSL_CTX_new(SSLv23_client_method());
2203 conn
= SSL_new(context
);
2205 SSL_set_fd(conn
, http
->fd
);
2206 if (SSL_connect(conn
) != 1)
2208 SSL_CTX_free(context
);
2212 http
->error
= WSAGetLastError();
2214 http
->error
= errno
;
2216 http
->status
= HTTP_ERROR
;
2218 return (HTTP_ERROR
);
2221 # elif defined(HAVE_GNUTLS)
2222 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2226 http
->error
= errno
;
2227 http
->status
= HTTP_ERROR
;
2232 credentials
= (gnutls_certificate_client_credentials
*)
2233 malloc(sizeof(gnutls_certificate_client_credentials
));
2234 if (credentials
== NULL
)
2238 http
->error
= errno
;
2239 http
->status
= HTTP_ERROR
;
2244 gnutls_certificate_allocate_credentials(credentials
);
2246 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2247 gnutls_set_default_priority(conn
->session
);
2248 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2249 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2251 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2253 http
->error
= errno
;
2254 http
->status
= HTTP_ERROR
;
2259 conn
->credentials
= credentials
;
2261 # elif defined(HAVE_CDSASSL)
2262 error
= SSLNewContext(false, &conn
);
2265 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2268 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2271 error
= SSLSetAllowsExpiredCerts(conn
, true);
2274 error
= SSLSetAllowsAnyRoot(conn
, true);
2277 error
= SSLHandshake(conn
);
2281 http
->error
= error
;
2282 http
->status
= HTTP_ERROR
;
2284 SSLDisposeContext(conn
);
2290 # endif /* HAVE_CDSASSL */
2298 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2302 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2305 SSL_CTX
*context
; /* Context for encryption */
2306 SSL
*conn
; /* Connection for encryption */
2309 conn
= (SSL
*)(http
->tls
);
2310 context
= SSL_get_SSL_CTX(conn
);
2313 SSL_CTX_free(context
);
2316 # elif defined(HAVE_GNUTLS)
2317 http_tls_t
*conn
; /* Encryption session */
2318 gnutls_certificate_client_credentials
*credentials
;
2319 /* TLS credentials */
2322 conn
= (http_tls_t
*)(http
->tls
);
2323 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2325 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2326 gnutls_deinit(conn
->session
);
2327 gnutls_certificate_free_credentials(*credentials
);
2331 # elif defined(HAVE_CDSASSL)
2332 SSLClose((SSLContextRef
)http
->tls
);
2333 SSLDisposeContext((SSLContextRef
)http
->tls
);
2334 # endif /* HAVE_LIBSSL */
2341 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2344 static int /* O - Bytes read */
2345 http_read_ssl(http_t
*http
, /* I - HTTP data */
2346 char *buf
, /* I - Buffer to store data */
2347 int len
) /* I - Length of buffer */
2349 # if defined(HAVE_LIBSSL)
2350 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2352 # elif defined(HAVE_GNUTLS)
2353 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2355 # elif defined(HAVE_CDSASSL)
2356 OSStatus error
; /* Error info */
2357 size_t processed
; /* Number of bytes processed */
2360 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2366 http
->error
= error
;
2370 # endif /* HAVE_LIBSSL */
2375 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2378 static int /* O - Bytes written */
2379 http_write_ssl(http_t
*http
, /* I - HTTP data */
2380 const char *buf
, /* I - Buffer holding data */
2381 int len
) /* I - Length of buffer */
2383 # if defined(HAVE_LIBSSL)
2384 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2386 # elif defined(HAVE_GNUTLS)
2387 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2388 # elif defined(HAVE_CDSASSL)
2389 OSStatus error
; /* Error info */
2390 size_t processed
; /* Number of bytes processed */
2393 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2399 http
->error
= error
;
2402 # endif /* HAVE_LIBSSL */
2406 # if defined(HAVE_CDSASSL)
2408 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2411 static OSStatus
/* O - -1 on error, 0 on success */
2412 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2413 void *data
, /* I - Data buffer */
2414 size_t *dataLength
) /* IO - Number of bytes */
2416 ssize_t bytes
; /* Number of bytes read */
2419 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2422 *dataLength
= bytes
;
2431 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2434 static OSStatus
/* O - -1 on error, 0 on success */
2435 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2436 const void *data
, /* I - Data buffer */
2437 size_t *dataLength
) /* IO - Number of bytes */
2442 bytes
= write((int)connection
, data
, *dataLength
);
2445 *dataLength
= bytes
;
2451 # endif /* HAVE_CDSASSL */
2452 #endif /* HAVE_SSL */
2456 * End of "$Id: http.c,v 1.82.2.39 2003/08/29 23:58:37 mike Exp $".