2 * "$Id: http.c,v 1.82.2.53 2004/06/29 18:54:17 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-3142 USA
20 * Voice: (301) 373-9600
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 */
293 DEBUG_printf(("httpClose(http=%p)\n", http
));
299 free(http
->input_set
);
306 http_shutdown_ssl(http
);
307 #endif /* HAVE_SSL */
310 closesocket(http
->fd
);
320 * 'httpConnect()' - Connect to a HTTP server.
323 http_t
* /* O - New HTTP connection */
324 httpConnect(const char *host
, /* I - Host to connect to */
325 int port
) /* I - Port number */
327 http_encryption_t encrypt
; /* Type of encryption to use */
331 * Set the default encryption status...
335 encrypt
= HTTP_ENCRYPT_ALWAYS
;
337 encrypt
= HTTP_ENCRYPT_IF_REQUESTED
;
339 return (httpConnectEncrypt(host
, port
, encrypt
));
344 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
347 http_t
* /* O - New HTTP connection */
348 httpConnectEncrypt(const char *host
, /* I - Host to connect to */
349 int port
, /* I - Port number */
350 http_encryption_t encrypt
)
351 /* I - Type of encryption to use */
353 int i
; /* Looping var */
354 http_t
*http
; /* New HTTP connection */
355 struct hostent
*hostaddr
; /* Host address data */
358 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encrypt=%d)\n",
359 host
? host
: "(null)", port
, encrypt
));
370 if ((hostaddr
= httpGetHostByName(host
)) == NULL
)
373 * This hack to make users that don't have a localhost entry in
374 * their hosts file or DNS happy...
377 if (strcasecmp(host
, "localhost") != 0)
379 else if ((hostaddr
= httpGetHostByName("127.0.0.1")) == NULL
)
384 * Verify that it is an IPv4 address (IPv6 support will come in CUPS 1.2...)
388 if ((hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4) &&
389 (hostaddr
->h_addrtype
!= AF_INET6
|| hostaddr
->h_length
!= 16))
392 if (hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4)
394 #endif /* AF_INET6 */
397 * Allocate memory for the structure...
400 http
= calloc(sizeof(http_t
), 1);
404 http
->version
= HTTP_1_1
;
406 http
->activity
= time(NULL
);
410 * Set the encryption status...
413 if (port
== 443) /* Always use encryption for https */
414 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
416 http
->encryption
= encrypt
;
419 * Loop through the addresses we have until one of them connects...
422 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
424 for (i
= 0; hostaddr
->h_addr_list
[i
]; i
++)
427 * Load the address...
430 httpAddrLoad(hostaddr
, port
, i
, &(http
->hostaddr
));
433 * Connect to the remote system...
436 if (!httpReconnect(http
))
441 * Could not connect to any known address - bail out!
450 * 'httpEncryption()' - Set the required encryption on the link.
453 int /* O - -1 on error, 0 on success */
454 httpEncryption(http_t
*http
, /* I - HTTP data */
455 http_encryption_t e
) /* I - New encryption preference */
457 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
463 http
->encryption
= e
;
465 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
466 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
467 return (httpReconnect(http
));
468 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
469 return (http_upgrade(http
));
473 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
477 #endif /* HAVE_SSL */
482 * 'httpReconnect()' - Reconnect to a HTTP server...
485 int /* O - 0 on success, non-zero on failure */
486 httpReconnect(http_t
*http
) /* I - HTTP data */
488 int val
; /* Socket option value */
489 int status
; /* Connect status */
492 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
499 http_shutdown_ssl(http
);
500 #endif /* HAVE_SSL */
503 * Close any previously open socket...
508 closesocket(http
->fd
);
514 * Create the socket and set options to allow reuse.
517 if ((http
->fd
= socket(http
->hostaddr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
520 http
->error
= WSAGetLastError();
524 http
->status
= HTTP_ERROR
;
529 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
530 * other processes... */
531 #endif /* FD_CLOEXEC */
534 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
538 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
539 #endif /* SO_REUSEPORT */
542 * Using TCP_NODELAY improves responsiveness, especially on systems
543 * with a slow loopback interface... Since we write large buffers
544 * when sending print files and requests, there shouldn't be any
545 * performance penalty for this...
550 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
552 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
556 * Connect to the server...
560 if (http
->hostaddr
.addr
.sa_family
== AF_INET6
)
561 status
= connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
562 sizeof(http
->hostaddr
.ipv6
));
564 #endif /* AF_INET6 */
566 if (http
->hostaddr
.addr
.sa_family
== AF_LOCAL
)
567 status
= connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
568 SUN_LEN(&(http
->hostaddr
.un
)));
570 #endif /* AF_LOCAL */
571 status
= connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
572 sizeof(http
->hostaddr
.ipv4
));
577 http
->error
= WSAGetLastError();
581 http
->status
= HTTP_ERROR
;
584 closesocket(http
->fd
);
595 http
->status
= HTTP_CONTINUE
;
598 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
601 * Always do encryption via SSL.
604 if (http_setup_ssl(http
) != 0)
607 closesocket(http
->fd
);
615 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
616 return (http_upgrade(http
));
617 #endif /* HAVE_SSL */
624 * 'httpGetSubField()' - Get a sub-field value.
627 char * /* O - Value or NULL */
628 httpGetSubField(http_t
*http
, /* I - HTTP data */
629 http_field_t field
, /* I - Field index */
630 const char *name
, /* I - Name of sub-field */
631 char *value
) /* O - Value string */
633 const char *fptr
; /* Pointer into field */
634 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
635 *ptr
; /* Pointer into string buffer */
638 DEBUG_printf(("httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p)\n",
639 http
, field
, name
, value
));
642 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
643 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
644 name
== NULL
|| value
== NULL
)
647 for (fptr
= http
->fields
[field
]; *fptr
;)
650 * Skip leading whitespace...
653 while (isspace(*fptr
& 255))
663 * Get the sub-field name...
667 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) && ptr
< (temp
+ sizeof(temp
) - 1);
672 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
675 * Skip trailing chars up to the '='...
678 while (isspace(*fptr
& 255))
688 * Skip = and leading whitespace...
693 while (isspace(*fptr
& 255))
699 * Read quoted string...
702 for (ptr
= value
, fptr
++;
703 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
708 while (*fptr
&& *fptr
!= '\"')
717 * Read unquoted string...
721 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
726 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
730 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
733 * See if this is the one...
736 if (strcmp(name
, temp
) == 0)
747 * 'httpSetField()' - Set the value of an HTTP header.
751 httpSetField(http_t
*http
, /* I - HTTP data */
752 http_field_t field
, /* I - Field index */
753 const char *value
) /* I - Value */
756 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
757 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
761 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
766 * 'httpDelete()' - Send a DELETE request to the server.
769 int /* O - Status of call (0 = success) */
770 httpDelete(http_t
*http
, /* I - HTTP data */
771 const char *uri
) /* I - URI to delete */
773 return (http_send(http
, HTTP_DELETE
, uri
));
778 * 'httpGet()' - Send a GET request to the server.
781 int /* O - Status of call (0 = success) */
782 httpGet(http_t
*http
, /* I - HTTP data */
783 const char *uri
) /* I - URI to get */
785 return (http_send(http
, HTTP_GET
, uri
));
790 * 'httpHead()' - Send a HEAD request to the server.
793 int /* O - Status of call (0 = success) */
794 httpHead(http_t
*http
, /* I - HTTP data */
795 const char *uri
) /* I - URI for head */
797 return (http_send(http
, HTTP_HEAD
, uri
));
802 * 'httpOptions()' - Send an OPTIONS request to the server.
805 int /* O - Status of call (0 = success) */
806 httpOptions(http_t
*http
, /* I - HTTP data */
807 const char *uri
) /* I - URI for options */
809 return (http_send(http
, HTTP_OPTIONS
, uri
));
814 * 'httpPost()' - Send a POST request to the server.
817 int /* O - Status of call (0 = success) */
818 httpPost(http_t
*http
, /* I - HTTP data */
819 const char *uri
) /* I - URI for post */
823 return (http_send(http
, HTTP_POST
, uri
));
828 * 'httpPut()' - Send a PUT request to the server.
831 int /* O - Status of call (0 = success) */
832 httpPut(http_t
*http
, /* I - HTTP data */
833 const char *uri
) /* I - URI to put */
837 return (http_send(http
, HTTP_PUT
, uri
));
842 * 'httpTrace()' - Send an TRACE request to the server.
845 int /* O - Status of call (0 = success) */
846 httpTrace(http_t
*http
, /* I - HTTP data */
847 const char *uri
) /* I - URI for trace */
849 return (http_send(http
, HTTP_TRACE
, uri
));
854 * 'httpFlush()' - Flush data from a HTTP connection.
858 httpFlush(http_t
*http
) /* I - HTTP data */
860 char buffer
[8192]; /* Junk buffer */
863 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
865 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
870 * 'httpRead()' - Read data from a HTTP connection.
873 int /* O - Number of bytes read */
874 httpRead(http_t
*http
, /* I - HTTP data */
875 char *buffer
, /* I - Buffer for data */
876 int length
) /* I - Maximum number of bytes */
878 int bytes
; /* Bytes read */
879 char len
[32]; /* Length string */
882 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
883 http
, buffer
, length
));
885 if (http
== NULL
|| buffer
== NULL
)
888 http
->activity
= time(NULL
);
893 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
894 http
->data_remaining
<= 0)
896 DEBUG_puts("httpRead: Getting chunk length...");
898 if (httpGets(len
, sizeof(len
), http
) == NULL
)
900 DEBUG_puts("httpRead: Could not get length!");
904 http
->data_remaining
= strtol(len
, NULL
, 16);
905 if (http
->data_remaining
< 0)
907 DEBUG_puts("httpRead: Negative chunk length!");
912 DEBUG_printf(("httpRead: data_remaining=%d\n", http
->data_remaining
));
914 if (http
->data_remaining
<= 0)
917 * A zero-length chunk ends a transfer; unless we are reading POST
921 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
922 httpGets(len
, sizeof(len
), http
);
924 if (http
->state
== HTTP_POST_RECV
)
927 http
->state
= HTTP_WAITING
;
930 * Prevent future reads for this request...
933 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
937 else if (length
> http
->data_remaining
)
938 length
= http
->data_remaining
;
940 if (http
->used
== 0 && length
<= 256)
943 * Buffer small reads for better performance...
946 if (!http
->blocking
&& !httpWait(http
, 1000))
949 if (http
->data_remaining
> sizeof(http
->buffer
))
950 bytes
= sizeof(http
->buffer
);
952 bytes
= http
->data_remaining
;
956 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
958 #endif /* HAVE_SSL */
960 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
963 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
965 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
974 http
->error
= WSAGetLastError();
993 if (length
> http
->used
)
998 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
1000 memcpy(buffer
, http
->buffer
, length
);
1001 http
->used
-= length
;
1004 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1009 if (!http
->blocking
&& !httpWait(http
, 1000))
1012 bytes
= http_read_ssl(http
, buffer
, length
);
1014 #endif /* HAVE_SSL */
1017 if (!http
->blocking
&& !httpWait(http
, 1000))
1020 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
1021 bytes
= recv(http
->fd
, buffer
, length
, 0);
1022 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
1026 http
->data_remaining
-= bytes
;
1030 http
->error
= WSAGetLastError();
1035 http
->error
= errno
;
1040 http
->error
= EPIPE
;
1044 if (http
->data_remaining
== 0)
1046 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1047 httpGets(len
, sizeof(len
), http
);
1049 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1051 if (http
->state
== HTTP_POST_RECV
)
1054 http
->state
= HTTP_WAITING
;
1061 printf("httpRead: Read %d bytes:\n", bytes
);
1062 for (i
= 0; i
< bytes
; i
+= 16)
1066 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1067 printf(" %02X", buffer
[i
+ j
] & 255);
1076 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1078 ch
= buffer
[i
+ j
] & 255;
1080 if (ch
< ' ' || ch
== 127)
1095 * 'httpSetCookie()' - Set the cookie value(s)...
1099 httpSetCookie(http_t
*http
, /* I - Connection */
1100 const char *cookie
) /* I - Cookie string */
1109 http
->cookie
= strdup(cookie
);
1111 http
->cookie
= NULL
;
1116 * 'httpWait()' - Wait for data available on a connection.
1119 int /* O - 1 if data is available, 0 otherwise */
1120 httpWait(http_t
*http
, /* I - HTTP data */
1121 int msec
) /* I - Milliseconds to wait */
1124 * First see if there is data in the buffer...
1134 * If not, check the SSL/TLS buffers and do a select() on the connection...
1137 return (http_wait(http
, msec
));
1142 * 'httpWrite()' - Write data to a HTTP connection.
1145 int /* O - Number of bytes written */
1146 httpWrite(http_t
*http
, /* I - HTTP data */
1147 const char *buffer
, /* I - Buffer for data */
1148 int length
) /* I - Number of bytes to write */
1150 int tbytes
, /* Total bytes sent */
1151 bytes
; /* Bytes sent */
1154 if (http
== NULL
|| buffer
== NULL
)
1157 http
->activity
= time(NULL
);
1159 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1161 if (httpPrintf(http
, "%x\r\n", length
) < 0)
1167 * A zero-length chunk ends a transfer; unless we are sending POST
1168 * or PUT data, go idle...
1171 DEBUG_puts("httpWrite: changing states...");
1173 if (http
->state
== HTTP_POST_RECV
)
1175 else if (http
->state
== HTTP_PUT_RECV
)
1176 http
->state
= HTTP_STATUS
;
1178 http
->state
= HTTP_WAITING
;
1180 if (httpPrintf(http
, "\r\n") < 0)
1193 bytes
= http_write_ssl(http
, buffer
, length
);
1195 #endif /* HAVE_SSL */
1196 bytes
= send(http
->fd
, buffer
, length
, 0);
1201 if (WSAGetLastError() != http
->error
)
1203 http
->error
= WSAGetLastError();
1209 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
1211 http
->error
= errno
;
1216 DEBUG_puts("httpWrite: error writing data...\n");
1224 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1225 http
->data_remaining
-= bytes
;
1228 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1229 if (httpPrintf(http
, "\r\n") < 0)
1232 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1235 * Finished with the transfer; unless we are sending POST or PUT
1239 DEBUG_puts("httpWrite: changing states...");
1241 if (http
->state
== HTTP_POST_RECV
)
1243 else if (http
->state
== HTTP_PUT_RECV
)
1244 http
->state
= HTTP_STATUS
;
1246 http
->state
= HTTP_WAITING
;
1252 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1253 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1257 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1258 printf(" %02X", buffer
[i
+ j
] & 255);
1267 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1269 ch
= buffer
[i
+ j
] & 255;
1271 if (ch
< ' ' || ch
== 127)
1285 * 'httpGets()' - Get a line of text from a HTTP connection.
1288 char * /* O - Line or NULL */
1289 httpGets(char *line
, /* I - Line to read into */
1290 int length
, /* I - Max length of buffer */
1291 http_t
*http
) /* I - HTTP data */
1293 char *lineptr
, /* Pointer into line */
1294 *bufptr
, /* Pointer into input buffer */
1295 *bufend
; /* Pointer to end of buffer */
1296 int bytes
; /* Number of bytes read */
1299 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
1301 if (http
== NULL
|| line
== NULL
)
1305 * Pre-scan the buffer and see if there is a newline in there...
1316 bufptr
= http
->buffer
;
1317 bufend
= http
->buffer
+ http
->used
;
1319 while (bufptr
< bufend
)
1320 if (*bufptr
== 0x0a)
1325 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1328 * No newline; see if there is more data to be read...
1331 if (!http
->blocking
&& !http_wait(http
, 1000))
1336 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1338 #endif /* HAVE_SSL */
1339 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1341 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
1346 * Nope, can't get a line this time...
1350 if (WSAGetLastError() != http
->error
)
1352 http
->error
= WSAGetLastError();
1356 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
1358 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
1362 else if (errno
!= http
->error
)
1364 http
->error
= errno
;
1371 else if (bytes
== 0)
1373 http
->error
= EPIPE
;
1379 * Yup, update the amount used and the end pointer...
1382 http
->used
+= bytes
;
1387 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1389 http
->activity
= time(NULL
);
1392 * Read a line from the buffer...
1396 bufptr
= http
->buffer
;
1400 while (bufptr
< bufend
&& bytes
< length
)
1404 if (*bufptr
== 0x0a)
1409 else if (*bufptr
== 0x0d)
1412 *lineptr
++ = *bufptr
++;
1419 http
->used
-= bytes
;
1421 memmove(http
->buffer
, bufptr
, http
->used
);
1423 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
1427 DEBUG_puts("httpGets: No new line available!");
1434 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1437 int /* O - Number of bytes written */
1438 httpPrintf(http_t
*http
, /* I - HTTP data */
1439 const char *format
, /* I - printf-style format string */
1440 ...) /* I - Additional args as needed */
1442 int bytes
, /* Number of bytes to write */
1443 nbytes
, /* Number of bytes written */
1444 tbytes
; /* Number of bytes all together */
1445 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1446 *bufptr
; /* Pointer into buffer */
1447 va_list ap
; /* Variable argument pointer */
1450 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
1452 va_start(ap
, format
);
1453 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1456 DEBUG_printf(("httpPrintf: %s", buf
));
1458 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1462 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1464 #endif /* HAVE_SSL */
1465 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1472 if (WSAGetLastError() != http
->error
)
1474 http
->error
= WSAGetLastError();
1480 else if (errno
!= http
->error
)
1482 http
->error
= errno
;
1496 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1499 const char * /* O - Date/time string */
1500 httpGetDateString(time_t t
) /* I - UNIX time */
1503 static char datetime
[256];
1507 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1508 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1509 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1516 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1519 time_t /* O - UNIX time */
1520 httpGetDateTime(const char *s
) /* I - Date/time string */
1522 int i
; /* Looping var */
1523 struct tm tdate
; /* Time/date structure */
1524 char mon
[16]; /* Abbreviated month name */
1525 int day
, year
; /* Day of month and year */
1526 int hour
, min
, sec
; /* Time */
1529 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1532 for (i
= 0; i
< 12; i
++)
1533 if (strcasecmp(mon
, months
[i
]) == 0)
1540 tdate
.tm_mday
= day
;
1541 tdate
.tm_year
= year
- 1900;
1542 tdate
.tm_hour
= hour
;
1547 return (mktime(&tdate
));
1552 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1555 http_status_t
/* O - HTTP status */
1556 httpUpdate(http_t
*http
) /* I - HTTP data */
1558 char line
[1024], /* Line from connection... */
1559 *value
; /* Pointer to value on line */
1560 http_field_t field
; /* Field index */
1561 int major
, minor
, /* HTTP version numbers */
1562 status
; /* Request status */
1565 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1568 * If we haven't issued any commands, then there is nothing to "update"...
1571 if (http
->state
== HTTP_WAITING
)
1572 return (HTTP_CONTINUE
);
1575 * Grab all of the lines we can from the connection...
1578 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1580 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1582 if (line
[0] == '\0')
1585 * Blank line means the start of the data section (if any). Return
1586 * the result code, too...
1588 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1589 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1593 if (http
->status
== HTTP_CONTINUE
)
1594 return (http
->status
);
1596 if (http
->status
< HTTP_BAD_REQUEST
)
1597 http
->digest_tries
= 0;
1600 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1602 if (http_setup_ssl(http
) != 0)
1605 closesocket(http
->fd
);
1610 return (HTTP_ERROR
);
1613 return (HTTP_CONTINUE
);
1615 #endif /* HAVE_SSL */
1617 httpGetLength(http
);
1619 switch (http
->state
)
1623 case HTTP_POST_RECV
:
1626 case HTTP_POST_SEND
:
1630 http
->state
= HTTP_WAITING
;
1634 return (http
->status
);
1636 else if (strncmp(line
, "HTTP/", 5) == 0)
1639 * Got the beginning of a response...
1642 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1643 return (HTTP_ERROR
);
1645 http
->version
= (http_version_t
)(major
* 100 + minor
);
1646 http
->status
= (http_status_t
)status
;
1648 else if ((value
= strchr(line
, ':')) != NULL
)
1655 while (isspace(*value
& 255))
1659 * Be tolerants of servers that send unknown attribute fields...
1662 if (!strcasecmp(line
, "expect"))
1665 * "Expect: 100-continue" or similar...
1668 http
->expect
= (http_status_t
)atoi(value
);
1670 else if (!strcasecmp(line
, "cookie"))
1673 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1676 httpSetCookie(http
, value
);
1678 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1680 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1684 httpSetField(http
, field
, value
);
1688 http
->status
= HTTP_ERROR
;
1689 return (HTTP_ERROR
);
1694 * See if there was an error...
1697 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
1698 return (http
->status
);
1702 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
1703 strerror(http
->error
)));
1704 http
->status
= HTTP_ERROR
;
1705 return (HTTP_ERROR
);
1709 * If we haven't already returned, then there is nothing new...
1712 return (HTTP_CONTINUE
);
1717 * 'httpDecode64()' - Base64-decode a string.
1720 char * /* O - Decoded string */
1721 httpDecode64(char *out
, /* I - String to write to */
1722 const char *in
) /* I - String to read from */
1724 int pos
, /* Bit position */
1725 base64
; /* Value of this character */
1726 char *outptr
; /* Output pointer */
1729 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1732 * Decode this character into a number from 0 to 63...
1735 if (*in
>= 'A' && *in
<= 'Z')
1737 else if (*in
>= 'a' && *in
<= 'z')
1738 base64
= *in
- 'a' + 26;
1739 else if (*in
>= '0' && *in
<= '9')
1740 base64
= *in
- '0' + 52;
1741 else if (*in
== '+')
1743 else if (*in
== '/')
1745 else if (*in
== '=')
1751 * Store the result in the appropriate chars...
1757 *outptr
= base64
<< 2;
1761 *outptr
++ |= (base64
>> 4) & 3;
1762 *outptr
= (base64
<< 4) & 255;
1766 *outptr
++ |= (base64
>> 2) & 15;
1767 *outptr
= (base64
<< 6) & 255;
1771 *outptr
++ |= base64
;
1780 * Return the decoded string...
1788 * 'httpEncode64()' - Base64-encode a string.
1791 char * /* O - Encoded string */
1792 httpEncode64(char *out
, /* I - String to write to */
1793 const char *in
) /* I - String to read from */
1795 char *outptr
; /* Output pointer */
1796 static const char base64
[] = /* Base64 characters... */
1798 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1799 "abcdefghijklmnopqrstuvwxyz"
1805 for (outptr
= out
; *in
!= '\0'; in
++)
1808 * Encode the up to 3 characters as 4 Base64 numbers...
1811 *outptr
++ = base64
[in
[0] >> 2];
1812 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1822 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1831 *outptr
++ = base64
[in
[0] & 63];
1837 * Return the encoded string...
1845 * 'httpGetLength()' - Get the amount of data remaining from the
1846 * content-length or transfer-encoding fields.
1849 int /* O - Content length */
1850 httpGetLength(http_t
*http
) /* I - HTTP data */
1852 DEBUG_printf(("httpGetLength(http=%p), state=%d\n", http
, http
->state
));
1854 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1856 DEBUG_puts("httpGetLength: chunked request!");
1858 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1859 http
->data_remaining
= 0;
1863 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1866 * The following is a hack for HTTP servers that don't send a
1867 * content-length or transfer-encoding field...
1869 * If there is no content-length then the connection must close
1870 * after the transfer is complete...
1873 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1874 http
->data_remaining
= 2147483647;
1876 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1878 DEBUG_printf(("httpGetLength: content_length=%d\n", http
->data_remaining
));
1881 return (http
->data_remaining
);
1886 * 'http_field()' - Return the field index for a field name.
1889 static http_field_t
/* O - Field index */
1890 http_field(const char *name
) /* I - String name */
1892 int i
; /* Looping var */
1895 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1896 if (strcasecmp(name
, http_fields
[i
]) == 0)
1897 return ((http_field_t
)i
);
1899 return (HTTP_FIELD_UNKNOWN
);
1904 * 'http_send()' - Send a request with all fields and the trailing blank line.
1907 static int /* O - 0 on success, non-zero on error */
1908 http_send(http_t
*http
, /* I - HTTP data */
1909 http_state_t request
, /* I - Request code */
1910 const char *uri
) /* I - URI */
1912 int i
; /* Looping var */
1913 char *ptr
, /* Pointer in buffer */
1914 buf
[1024]; /* Encoded URI buffer */
1915 static const char * const codes
[] =
1916 { /* Request code strings */
1931 static const char hex
[] = "0123456789ABCDEF";
1935 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
1936 http
, codes
[request
], uri
));
1938 if (http
== NULL
|| uri
== NULL
)
1942 * Encode the URI as needed...
1945 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1946 if (*uri
<= ' ' || *uri
>= 127)
1948 if (ptr
< (buf
+ sizeof(buf
) - 1))
1950 if (ptr
< (buf
+ sizeof(buf
) - 1))
1951 *ptr
++ = hex
[(*uri
>> 4) & 15];
1952 if (ptr
< (buf
+ sizeof(buf
) - 1))
1953 *ptr
++ = hex
[*uri
& 15];
1961 * See if we had an error the last time around; if so, reconnect...
1964 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1965 httpReconnect(http
);
1968 * Send the request header...
1971 http
->state
= request
;
1972 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1975 http
->status
= HTTP_CONTINUE
;
1978 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1980 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1981 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1983 #endif /* HAVE_SSL */
1985 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1987 http
->status
= HTTP_ERROR
;
1991 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1992 if (http
->fields
[i
][0] != '\0')
1994 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1996 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1998 http
->status
= HTTP_ERROR
;
2003 if (httpPrintf(http
, "\r\n") < 1)
2005 http
->status
= HTTP_ERROR
;
2009 httpClearFields(http
);
2016 * 'http_wait()' - Wait for data available on a connection.
2019 static int /* O - 1 if data is available, 0 otherwise */
2020 http_wait(http_t
*http
, /* I - HTTP data */
2021 int msec
) /* I - Milliseconds to wait */
2024 struct rlimit limit
; /* Runtime limit */
2026 struct timeval timeout
; /* Timeout */
2027 int nfds
; /* Result from select() */
2028 int set_size
; /* Size of select set */
2031 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http
, msec
));
2034 * Check the SSL/TLS buffers for data first...
2041 if (SSL_pending((SSL
*)(http
->tls
)))
2043 # elif defined(HAVE_GNUTLS)
2044 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2046 # elif defined(HAVE_CDSASSL)
2047 size_t bytes
; /* Bytes that are available */
2049 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2051 # endif /* HAVE_LIBSSL */
2053 #endif /* HAVE_SSL */
2056 * Then try doing a select() to poll the socket...
2059 if (!http
->input_set
)
2063 * Windows has a fixed-size select() structure, different (surprise,
2064 * surprise!) from all UNIX implementations. Just allocate this
2065 * fixed structure...
2068 http
->input_set
= calloc(1, sizeof(fd_set
));
2071 * Allocate the select() input set based upon the max number of file
2072 * descriptors available for this process...
2075 getrlimit(RLIMIT_NOFILE
, &limit
);
2077 set_size
= (limit
.rlim_cur
+ 31) / 8 + 4;
2078 if (set_size
< sizeof(fd_set
))
2079 set_size
= sizeof(fd_set
);
2081 http
->input_set
= calloc(1, set_size
);
2084 if (!http
->input_set
)
2088 FD_SET(http
->fd
, http
->input_set
);
2092 timeout
.tv_sec
= msec
/ 1000;
2093 timeout
.tv_usec
= (msec
% 1000) * 1000;
2095 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2098 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2100 FD_CLR(http
->fd
, http
->input_set
);
2108 * 'http_upgrade()' - Force upgrade to TLS encryption.
2111 static int /* O - Status of connection */
2112 http_upgrade(http_t
*http
) /* I - HTTP data */
2114 int ret
; /* Return value */
2115 http_t myhttp
; /* Local copy of HTTP data */
2118 DEBUG_printf(("http_upgrade(%p)\n", http
));
2121 * Copy the HTTP data to a local variable so we can do the OPTIONS
2122 * request without interfering with the existing request data...
2125 memcpy(&myhttp
, http
, sizeof(myhttp
));
2128 * Send an OPTIONS request to the server, requiring SSL or TLS
2129 * encryption on the link...
2132 httpClearFields(&myhttp
);
2133 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2134 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2136 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2139 * Wait for the secure connection...
2142 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2148 * Copy the HTTP data back over, if any...
2151 http
->fd
= myhttp
.fd
;
2152 http
->error
= myhttp
.error
;
2153 http
->activity
= myhttp
.activity
;
2154 http
->status
= myhttp
.status
;
2155 http
->version
= myhttp
.version
;
2156 http
->keep_alive
= myhttp
.keep_alive
;
2157 http
->used
= myhttp
.used
;
2160 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2162 http
->auth_type
= myhttp
.auth_type
;
2163 http
->nonce_count
= myhttp
.nonce_count
;
2165 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2167 http
->tls
= myhttp
.tls
;
2168 http
->encryption
= myhttp
.encryption
;
2171 * See if we actually went secure...
2177 * Server does not support HTTP upgrade...
2180 DEBUG_puts("Server does not support HTTP upgrade!");
2183 closesocket(http
->fd
);
2198 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2201 static int /* O - Status of connection */
2202 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2205 SSL_CTX
*context
; /* Context for encryption */
2206 SSL
*conn
; /* Connection for encryption */
2207 # elif defined(HAVE_GNUTLS)
2208 http_tls_t
*conn
; /* TLS session object */
2209 gnutls_certificate_client_credentials
*credentials
;
2210 /* TLS credentials */
2211 # elif defined(HAVE_CDSASSL)
2212 SSLContextRef conn
; /* Context for encryption */
2213 OSStatus error
; /* Error info */
2214 # endif /* HAVE_LIBSSL */
2217 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
2220 context
= SSL_CTX_new(SSLv23_client_method());
2221 conn
= SSL_new(context
);
2223 SSL_set_fd(conn
, http
->fd
);
2224 if (SSL_connect(conn
) != 1)
2227 unsigned long error
; /* Error code */
2229 while ((error
= ERR_get_error()) != 0)
2230 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
2233 SSL_CTX_free(context
);
2237 http
->error
= WSAGetLastError();
2239 http
->error
= errno
;
2241 http
->status
= HTTP_ERROR
;
2243 return (HTTP_ERROR
);
2246 # elif defined(HAVE_GNUTLS)
2247 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2251 http
->error
= errno
;
2252 http
->status
= HTTP_ERROR
;
2257 credentials
= (gnutls_certificate_client_credentials
*)
2258 malloc(sizeof(gnutls_certificate_client_credentials
));
2259 if (credentials
== NULL
)
2263 http
->error
= errno
;
2264 http
->status
= HTTP_ERROR
;
2269 gnutls_certificate_allocate_credentials(credentials
);
2271 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2272 gnutls_set_default_priority(conn
->session
);
2273 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2274 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2276 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2278 http
->error
= errno
;
2279 http
->status
= HTTP_ERROR
;
2284 conn
->credentials
= credentials
;
2286 # elif defined(HAVE_CDSASSL)
2287 error
= SSLNewContext(false, &conn
);
2290 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2293 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2296 error
= SSLSetAllowsExpiredCerts(conn
, true);
2299 error
= SSLSetAllowsAnyRoot(conn
, true);
2302 error
= SSLHandshake(conn
);
2306 http
->error
= error
;
2307 http
->status
= HTTP_ERROR
;
2309 SSLDisposeContext(conn
);
2315 # endif /* HAVE_CDSASSL */
2323 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2327 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2330 SSL_CTX
*context
; /* Context for encryption */
2331 SSL
*conn
; /* Connection for encryption */
2334 conn
= (SSL
*)(http
->tls
);
2335 context
= SSL_get_SSL_CTX(conn
);
2338 SSL_CTX_free(context
);
2341 # elif defined(HAVE_GNUTLS)
2342 http_tls_t
*conn
; /* Encryption session */
2343 gnutls_certificate_client_credentials
*credentials
;
2344 /* TLS credentials */
2347 conn
= (http_tls_t
*)(http
->tls
);
2348 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2350 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2351 gnutls_deinit(conn
->session
);
2352 gnutls_certificate_free_credentials(*credentials
);
2356 # elif defined(HAVE_CDSASSL)
2357 SSLClose((SSLContextRef
)http
->tls
);
2358 SSLDisposeContext((SSLContextRef
)http
->tls
);
2359 # endif /* HAVE_LIBSSL */
2366 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2369 static int /* O - Bytes read */
2370 http_read_ssl(http_t
*http
, /* I - HTTP data */
2371 char *buf
, /* I - Buffer to store data */
2372 int len
) /* I - Length of buffer */
2374 # if defined(HAVE_LIBSSL)
2375 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2377 # elif defined(HAVE_GNUTLS)
2378 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2380 # elif defined(HAVE_CDSASSL)
2381 OSStatus error
; /* Error info */
2382 size_t processed
; /* Number of bytes processed */
2385 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2391 http
->error
= error
;
2395 # endif /* HAVE_LIBSSL */
2400 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2403 static int /* O - Bytes written */
2404 http_write_ssl(http_t
*http
, /* I - HTTP data */
2405 const char *buf
, /* I - Buffer holding data */
2406 int len
) /* I - Length of buffer */
2408 # if defined(HAVE_LIBSSL)
2409 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2411 # elif defined(HAVE_GNUTLS)
2412 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2413 # elif defined(HAVE_CDSASSL)
2414 OSStatus error
; /* Error info */
2415 size_t processed
; /* Number of bytes processed */
2418 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2424 http
->error
= error
;
2427 # endif /* HAVE_LIBSSL */
2431 # if defined(HAVE_CDSASSL)
2433 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2436 static OSStatus
/* O - -1 on error, 0 on success */
2437 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2438 void *data
, /* I - Data buffer */
2439 size_t *dataLength
) /* IO - Number of bytes */
2441 ssize_t bytes
; /* Number of bytes read */
2444 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2447 *dataLength
= bytes
;
2456 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2459 static OSStatus
/* O - -1 on error, 0 on success */
2460 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2461 const void *data
, /* I - Data buffer */
2462 size_t *dataLength
) /* IO - Number of bytes */
2467 bytes
= write((int)connection
, data
, *dataLength
);
2470 *dataLength
= bytes
;
2476 # endif /* HAVE_CDSASSL */
2477 #endif /* HAVE_SSL */
2481 * End of "$Id: http.c,v 1.82.2.53 2004/06/29 18:54:17 mike Exp $".