2 * "$Id: http.c,v 1.82.2.55 2004/07/06 00:35:31 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 * httpDecode64_2() - Base64-decode a string.
59 * httpEncode64() - Base64-encode a string.
60 * httpEncode64_2() - Base64-encode a string.
61 * httpGetLength() - Get the amount of data remaining from the
62 * content-length or transfer-encoding fields.
63 * http_field() - Return the field index for a field name.
64 * http_send() - Send a request with all fields and the trailing
66 * http_wait() - Wait for data available on a connection.
67 * http_upgrade() - Force upgrade to TLS encryption.
68 * http_setup_ssl() - Set up SSL/TLS on a connection.
69 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
70 * http_read_ssl() - Read from a SSL/TLS connection.
71 * http_write_ssl() - Write to a SSL/TLS connection.
72 * CDSAReadFunc() - Read function for CDSA decryption code.
73 * CDSAWriteFunc() - Write function for CDSA encryption code.
77 * Include necessary headers...
80 #include "http-private.h"
95 # include <sys/time.h>
96 # include <sys/resource.h>
101 * Some operating systems have done away with the Fxxxx constants for
102 * the fcntl() call; this works around that "feature"...
106 # define FNONBLK O_NONBLOCK
107 #endif /* !FNONBLK */
114 static http_field_t
http_field(const char *name
);
115 static int http_send(http_t
*http
, http_state_t request
,
117 static int http_wait(http_t
*http
, int msec
);
119 static int http_upgrade(http_t
*http
);
120 static int http_setup_ssl(http_t
*http
);
121 static void http_shutdown_ssl(http_t
*http
);
122 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
123 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
125 static OSStatus
CDSAReadFunc(SSLConnectionRef connection
, void *data
, size_t *dataLength
);
126 static OSStatus
CDSAWriteFunc(SSLConnectionRef connection
, const void *data
, size_t *dataLength
);
127 # endif /* HAVE_CDSASSL */
128 #endif /* HAVE_SSL */
135 static const char * const http_fields
[] =
152 "If-Unmodified-since",
165 static const char * const days
[7] =
175 static const char * const months
[12] =
193 * 'httpInitialize()' - Initialize the HTTP interface library and set the
194 * default HTTP proxy (if any).
202 struct timeval curtime
; /* Current time in microseconds */
204 int i
; /* Looping var */
205 unsigned char data
[1024]; /* Seed data */
206 #endif /* HAVE_LIBSSL */
209 WSADATA winsockdata
; /* WinSock data */
210 static int initialized
= 0; /* Has WinSock been initialized? */
214 WSAStartup(MAKEWORD(1,1), &winsockdata
);
215 #elif defined(HAVE_SIGSET)
216 sigset(SIGPIPE
, SIG_IGN
);
217 #elif defined(HAVE_SIGACTION)
218 struct sigaction action
; /* POSIX sigaction data */
222 * Ignore SIGPIPE signals...
225 memset(&action
, 0, sizeof(action
));
226 action
.sa_handler
= SIG_IGN
;
227 sigaction(SIGPIPE
, &action
, NULL
);
229 signal(SIGPIPE
, SIG_IGN
);
233 gnutls_global_init();
234 #endif /* HAVE_GNUTLS */
237 SSL_load_error_strings();
241 * Using the current time is a dubious random seed, but on some systems
242 * it is the best we can do (on others, this seed isn't even used...)
247 gettimeofday(&curtime
, NULL
);
248 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
251 for (i
= 0; i
< sizeof(data
); i
++)
252 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
254 RAND_seed(&data
, sizeof(data
));
255 #endif /* HAVE_LIBSSL */
260 * 'httpCheck()' - Check to see if there is a pending response from the server.
263 int /* O - 0 = no data, 1 = data available */
264 httpCheck(http_t
*http
) /* I - HTTP connection */
266 return (httpWait(http
, 0));
271 * 'httpClearCookie()' - Clear the cookie value(s).
275 httpClearCookie(http_t
*http
) /* I - Connection */
289 * 'httpClose()' - Close an HTTP connection...
293 httpClose(http_t
*http
) /* I - Connection to close */
295 DEBUG_printf(("httpClose(http=%p)\n", http
));
301 free(http
->input_set
);
308 http_shutdown_ssl(http
);
309 #endif /* HAVE_SSL */
312 closesocket(http
->fd
);
322 * 'httpConnect()' - Connect to a HTTP server.
325 http_t
* /* O - New HTTP connection */
326 httpConnect(const char *host
, /* I - Host to connect to */
327 int port
) /* I - Port number */
329 http_encryption_t encrypt
; /* Type of encryption to use */
333 * Set the default encryption status...
337 encrypt
= HTTP_ENCRYPT_ALWAYS
;
339 encrypt
= HTTP_ENCRYPT_IF_REQUESTED
;
341 return (httpConnectEncrypt(host
, port
, encrypt
));
346 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
349 http_t
* /* O - New HTTP connection */
350 httpConnectEncrypt(const char *host
, /* I - Host to connect to */
351 int port
, /* I - Port number */
352 http_encryption_t encrypt
)
353 /* I - Type of encryption to use */
355 int i
; /* Looping var */
356 http_t
*http
; /* New HTTP connection */
357 struct hostent
*hostaddr
; /* Host address data */
360 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encrypt=%d)\n",
361 host
? host
: "(null)", port
, encrypt
));
372 if ((hostaddr
= httpGetHostByName(host
)) == NULL
)
375 * This hack to make users that don't have a localhost entry in
376 * their hosts file or DNS happy...
379 if (strcasecmp(host
, "localhost") != 0)
381 else if ((hostaddr
= httpGetHostByName("127.0.0.1")) == NULL
)
386 * Verify that it is an IPv4, IPv6, or domain address...
389 if ((hostaddr
->h_addrtype
!= AF_INET
|| hostaddr
->h_length
!= 4)
391 && (hostaddr
->h_addrtype
!= AF_INET6
|| hostaddr
->h_length
!= 16)
392 #endif /* AF_INET6 */
394 && (hostaddr
->h_addrtype
!= AF_LOCAL
)
395 #endif /* AF_LOCAL */
400 * Allocate memory for the structure...
403 http
= calloc(sizeof(http_t
), 1);
407 http
->version
= HTTP_1_1
;
409 http
->activity
= time(NULL
);
413 * Set the encryption status...
416 if (port
== 443) /* Always use encryption for https */
417 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
419 http
->encryption
= encrypt
;
422 * Loop through the addresses we have until one of them connects...
425 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
427 for (i
= 0; hostaddr
->h_addr_list
[i
]; i
++)
430 * Load the address...
433 httpAddrLoad(hostaddr
, port
, i
, &(http
->hostaddr
));
436 * Connect to the remote system...
439 if (!httpReconnect(http
))
444 * Could not connect to any known address - bail out!
453 * 'httpEncryption()' - Set the required encryption on the link.
456 int /* O - -1 on error, 0 on success */
457 httpEncryption(http_t
*http
, /* I - HTTP data */
458 http_encryption_t e
) /* I - New encryption preference */
460 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
466 http
->encryption
= e
;
468 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
469 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
470 return (httpReconnect(http
));
471 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
472 return (http_upgrade(http
));
476 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
480 #endif /* HAVE_SSL */
485 * 'httpReconnect()' - Reconnect to a HTTP server...
488 int /* O - 0 on success, non-zero on failure */
489 httpReconnect(http_t
*http
) /* I - HTTP data */
491 int val
; /* Socket option value */
492 int status
; /* Connect status */
495 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
502 http_shutdown_ssl(http
);
503 #endif /* HAVE_SSL */
506 * Close any previously open socket...
511 closesocket(http
->fd
);
517 * Create the socket and set options to allow reuse.
520 if ((http
->fd
= socket(http
->hostaddr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
523 http
->error
= WSAGetLastError();
527 http
->status
= HTTP_ERROR
;
532 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
533 * other processes... */
534 #endif /* FD_CLOEXEC */
537 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
541 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
542 #endif /* SO_REUSEPORT */
545 * Using TCP_NODELAY improves responsiveness, especially on systems
546 * with a slow loopback interface... Since we write large buffers
547 * when sending print files and requests, there shouldn't be any
548 * performance penalty for this...
553 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
555 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
559 * Connect to the server...
563 if (http
->hostaddr
.addr
.sa_family
== AF_INET6
)
564 status
= connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
565 sizeof(http
->hostaddr
.ipv6
));
567 #endif /* AF_INET6 */
569 if (http
->hostaddr
.addr
.sa_family
== AF_LOCAL
)
570 status
= connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
571 SUN_LEN(&(http
->hostaddr
.un
)));
573 #endif /* AF_LOCAL */
574 status
= connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
575 sizeof(http
->hostaddr
.ipv4
));
580 http
->error
= WSAGetLastError();
584 http
->status
= HTTP_ERROR
;
587 closesocket(http
->fd
);
598 http
->status
= HTTP_CONTINUE
;
601 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
604 * Always do encryption via SSL.
607 if (http_setup_ssl(http
) != 0)
610 closesocket(http
->fd
);
618 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
619 return (http_upgrade(http
));
620 #endif /* HAVE_SSL */
627 * 'httpGetSubField()' - Get a sub-field value.
630 char * /* O - Value or NULL */
631 httpGetSubField(http_t
*http
, /* I - HTTP data */
632 http_field_t field
, /* I - Field index */
633 const char *name
, /* I - Name of sub-field */
634 char *value
) /* O - Value string */
636 const char *fptr
; /* Pointer into field */
637 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
638 *ptr
; /* Pointer into string buffer */
641 DEBUG_printf(("httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p)\n",
642 http
, field
, name
, value
));
645 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
646 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
647 name
== NULL
|| value
== NULL
)
650 for (fptr
= http
->fields
[field
]; *fptr
;)
653 * Skip leading whitespace...
656 while (isspace(*fptr
& 255))
666 * Get the sub-field name...
670 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) && ptr
< (temp
+ sizeof(temp
) - 1);
675 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
678 * Skip trailing chars up to the '='...
681 while (isspace(*fptr
& 255))
691 * Skip = and leading whitespace...
696 while (isspace(*fptr
& 255))
702 * Read quoted string...
705 for (ptr
= value
, fptr
++;
706 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
711 while (*fptr
&& *fptr
!= '\"')
720 * Read unquoted string...
724 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
729 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
733 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
736 * See if this is the one...
739 if (strcmp(name
, temp
) == 0)
750 * 'httpSetField()' - Set the value of an HTTP header.
754 httpSetField(http_t
*http
, /* I - HTTP data */
755 http_field_t field
, /* I - Field index */
756 const char *value
) /* I - Value */
759 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
760 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
764 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
769 * 'httpDelete()' - Send a DELETE request to the server.
772 int /* O - Status of call (0 = success) */
773 httpDelete(http_t
*http
, /* I - HTTP data */
774 const char *uri
) /* I - URI to delete */
776 return (http_send(http
, HTTP_DELETE
, uri
));
781 * 'httpGet()' - Send a GET request to the server.
784 int /* O - Status of call (0 = success) */
785 httpGet(http_t
*http
, /* I - HTTP data */
786 const char *uri
) /* I - URI to get */
788 return (http_send(http
, HTTP_GET
, uri
));
793 * 'httpHead()' - Send a HEAD request to the server.
796 int /* O - Status of call (0 = success) */
797 httpHead(http_t
*http
, /* I - HTTP data */
798 const char *uri
) /* I - URI for head */
800 return (http_send(http
, HTTP_HEAD
, uri
));
805 * 'httpOptions()' - Send an OPTIONS request to the server.
808 int /* O - Status of call (0 = success) */
809 httpOptions(http_t
*http
, /* I - HTTP data */
810 const char *uri
) /* I - URI for options */
812 return (http_send(http
, HTTP_OPTIONS
, uri
));
817 * 'httpPost()' - Send a POST request to the server.
820 int /* O - Status of call (0 = success) */
821 httpPost(http_t
*http
, /* I - HTTP data */
822 const char *uri
) /* I - URI for post */
826 return (http_send(http
, HTTP_POST
, uri
));
831 * 'httpPut()' - Send a PUT request to the server.
834 int /* O - Status of call (0 = success) */
835 httpPut(http_t
*http
, /* I - HTTP data */
836 const char *uri
) /* I - URI to put */
840 return (http_send(http
, HTTP_PUT
, uri
));
845 * 'httpTrace()' - Send an TRACE request to the server.
848 int /* O - Status of call (0 = success) */
849 httpTrace(http_t
*http
, /* I - HTTP data */
850 const char *uri
) /* I - URI for trace */
852 return (http_send(http
, HTTP_TRACE
, uri
));
857 * 'httpFlush()' - Flush data from a HTTP connection.
861 httpFlush(http_t
*http
) /* I - HTTP data */
863 char buffer
[8192]; /* Junk buffer */
866 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
868 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
873 * 'httpRead()' - Read data from a HTTP connection.
876 int /* O - Number of bytes read */
877 httpRead(http_t
*http
, /* I - HTTP data */
878 char *buffer
, /* I - Buffer for data */
879 int length
) /* I - Maximum number of bytes */
881 int bytes
; /* Bytes read */
882 char len
[32]; /* Length string */
885 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
886 http
, buffer
, length
));
888 if (http
== NULL
|| buffer
== NULL
)
891 http
->activity
= time(NULL
);
896 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
897 http
->data_remaining
<= 0)
899 DEBUG_puts("httpRead: Getting chunk length...");
901 if (httpGets(len
, sizeof(len
), http
) == NULL
)
903 DEBUG_puts("httpRead: Could not get length!");
907 http
->data_remaining
= strtol(len
, NULL
, 16);
908 if (http
->data_remaining
< 0)
910 DEBUG_puts("httpRead: Negative chunk length!");
915 DEBUG_printf(("httpRead: data_remaining=%d\n", http
->data_remaining
));
917 if (http
->data_remaining
<= 0)
920 * A zero-length chunk ends a transfer; unless we are reading POST
924 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
925 httpGets(len
, sizeof(len
), http
);
927 if (http
->state
== HTTP_POST_RECV
)
930 http
->state
= HTTP_WAITING
;
933 * Prevent future reads for this request...
936 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
940 else if (length
> http
->data_remaining
)
941 length
= http
->data_remaining
;
943 if (http
->used
== 0 && length
<= 256)
946 * Buffer small reads for better performance...
949 if (!http
->blocking
&& !httpWait(http
, 1000))
952 if (http
->data_remaining
> sizeof(http
->buffer
))
953 bytes
= sizeof(http
->buffer
);
955 bytes
= http
->data_remaining
;
959 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
961 #endif /* HAVE_SSL */
963 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
966 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
968 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
977 http
->error
= WSAGetLastError();
996 if (length
> http
->used
)
1001 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
1003 memcpy(buffer
, http
->buffer
, length
);
1004 http
->used
-= length
;
1007 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1012 if (!http
->blocking
&& !httpWait(http
, 1000))
1015 bytes
= http_read_ssl(http
, buffer
, length
);
1017 #endif /* HAVE_SSL */
1020 if (!http
->blocking
&& !httpWait(http
, 1000))
1023 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
1024 bytes
= recv(http
->fd
, buffer
, length
, 0);
1025 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
1029 http
->data_remaining
-= bytes
;
1033 http
->error
= WSAGetLastError();
1038 http
->error
= errno
;
1043 http
->error
= EPIPE
;
1047 if (http
->data_remaining
== 0)
1049 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1050 httpGets(len
, sizeof(len
), http
);
1052 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1054 if (http
->state
== HTTP_POST_RECV
)
1057 http
->state
= HTTP_WAITING
;
1064 printf("httpRead: Read %d bytes:\n", bytes
);
1065 for (i
= 0; i
< bytes
; i
+= 16)
1069 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1070 printf(" %02X", buffer
[i
+ j
] & 255);
1079 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1081 ch
= buffer
[i
+ j
] & 255;
1083 if (ch
< ' ' || ch
== 127)
1098 * 'httpSetCookie()' - Set the cookie value(s)...
1102 httpSetCookie(http_t
*http
, /* I - Connection */
1103 const char *cookie
) /* I - Cookie string */
1112 http
->cookie
= strdup(cookie
);
1114 http
->cookie
= NULL
;
1119 * 'httpWait()' - Wait for data available on a connection.
1122 int /* O - 1 if data is available, 0 otherwise */
1123 httpWait(http_t
*http
, /* I - HTTP data */
1124 int msec
) /* I - Milliseconds to wait */
1127 * First see if there is data in the buffer...
1137 * If not, check the SSL/TLS buffers and do a select() on the connection...
1140 return (http_wait(http
, msec
));
1145 * 'httpWrite()' - Write data to a HTTP connection.
1148 int /* O - Number of bytes written */
1149 httpWrite(http_t
*http
, /* I - HTTP data */
1150 const char *buffer
, /* I - Buffer for data */
1151 int length
) /* I - Number of bytes to write */
1153 int tbytes
, /* Total bytes sent */
1154 bytes
; /* Bytes sent */
1157 if (http
== NULL
|| buffer
== NULL
)
1160 http
->activity
= time(NULL
);
1162 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1164 if (httpPrintf(http
, "%x\r\n", length
) < 0)
1170 * A zero-length chunk ends a transfer; unless we are sending POST
1171 * or PUT data, go idle...
1174 DEBUG_puts("httpWrite: changing states...");
1176 if (http
->state
== HTTP_POST_RECV
)
1178 else if (http
->state
== HTTP_PUT_RECV
)
1179 http
->state
= HTTP_STATUS
;
1181 http
->state
= HTTP_WAITING
;
1183 if (httpPrintf(http
, "\r\n") < 0)
1196 bytes
= http_write_ssl(http
, buffer
, length
);
1198 #endif /* HAVE_SSL */
1199 bytes
= send(http
->fd
, buffer
, length
, 0);
1204 if (WSAGetLastError() != http
->error
)
1206 http
->error
= WSAGetLastError();
1212 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
1214 http
->error
= errno
;
1219 DEBUG_puts("httpWrite: error writing data...\n");
1227 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1228 http
->data_remaining
-= bytes
;
1231 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1232 if (httpPrintf(http
, "\r\n") < 0)
1235 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1238 * Finished with the transfer; unless we are sending POST or PUT
1242 DEBUG_puts("httpWrite: changing states...");
1244 if (http
->state
== HTTP_POST_RECV
)
1246 else if (http
->state
== HTTP_PUT_RECV
)
1247 http
->state
= HTTP_STATUS
;
1249 http
->state
= HTTP_WAITING
;
1255 printf("httpWrite: wrote %d bytes: \n", tbytes
);
1256 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
1260 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1261 printf(" %02X", buffer
[i
+ j
] & 255);
1270 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
1272 ch
= buffer
[i
+ j
] & 255;
1274 if (ch
< ' ' || ch
== 127)
1288 * 'httpGets()' - Get a line of text from a HTTP connection.
1291 char * /* O - Line or NULL */
1292 httpGets(char *line
, /* I - Line to read into */
1293 int length
, /* I - Max length of buffer */
1294 http_t
*http
) /* I - HTTP data */
1296 char *lineptr
, /* Pointer into line */
1297 *bufptr
, /* Pointer into input buffer */
1298 *bufend
; /* Pointer to end of buffer */
1299 int bytes
; /* Number of bytes read */
1302 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
1304 if (http
== NULL
|| line
== NULL
)
1308 * Pre-scan the buffer and see if there is a newline in there...
1319 bufptr
= http
->buffer
;
1320 bufend
= http
->buffer
+ http
->used
;
1322 while (bufptr
< bufend
)
1323 if (*bufptr
== 0x0a)
1328 if (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
)
1331 * No newline; see if there is more data to be read...
1334 if (!http
->blocking
&& !http_wait(http
, 1000))
1339 bytes
= http_read_ssl(http
, bufend
, HTTP_MAX_BUFFER
- http
->used
);
1341 #endif /* HAVE_SSL */
1342 bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0);
1344 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
1349 * Nope, can't get a line this time...
1353 if (WSAGetLastError() != http
->error
)
1355 http
->error
= WSAGetLastError();
1359 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
1361 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
1365 else if (errno
!= http
->error
)
1367 http
->error
= errno
;
1374 else if (bytes
== 0)
1376 http
->error
= EPIPE
;
1382 * Yup, update the amount used and the end pointer...
1385 http
->used
+= bytes
;
1390 while (bufptr
>= bufend
&& http
->used
< HTTP_MAX_BUFFER
);
1392 http
->activity
= time(NULL
);
1395 * Read a line from the buffer...
1399 bufptr
= http
->buffer
;
1403 while (bufptr
< bufend
&& bytes
< length
)
1407 if (*bufptr
== 0x0a)
1412 else if (*bufptr
== 0x0d)
1415 *lineptr
++ = *bufptr
++;
1422 http
->used
-= bytes
;
1424 memmove(http
->buffer
, bufptr
, http
->used
);
1426 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
1430 DEBUG_puts("httpGets: No new line available!");
1437 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1440 int /* O - Number of bytes written */
1441 httpPrintf(http_t
*http
, /* I - HTTP data */
1442 const char *format
, /* I - printf-style format string */
1443 ...) /* I - Additional args as needed */
1445 int bytes
, /* Number of bytes to write */
1446 nbytes
, /* Number of bytes written */
1447 tbytes
; /* Number of bytes all together */
1448 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1449 *bufptr
; /* Pointer into buffer */
1450 va_list ap
; /* Variable argument pointer */
1453 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
1455 va_start(ap
, format
);
1456 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1459 DEBUG_printf(("httpPrintf: %s", buf
));
1461 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1465 nbytes
= http_write_ssl(http
, bufptr
, bytes
- tbytes
);
1467 #endif /* HAVE_SSL */
1468 nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0);
1475 if (WSAGetLastError() != http
->error
)
1477 http
->error
= WSAGetLastError();
1483 else if (errno
!= http
->error
)
1485 http
->error
= errno
;
1499 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1502 const char * /* O - Date/time string */
1503 httpGetDateString(time_t t
) /* I - UNIX time */
1506 static char datetime
[256];
1510 snprintf(datetime
, sizeof(datetime
), "%s, %02d %s %d %02d:%02d:%02d GMT",
1511 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1512 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1519 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1522 time_t /* O - UNIX time */
1523 httpGetDateTime(const char *s
) /* I - Date/time string */
1525 int i
; /* Looping var */
1526 struct tm tdate
; /* Time/date structure */
1527 char mon
[16]; /* Abbreviated month name */
1528 int day
, year
; /* Day of month and year */
1529 int hour
, min
, sec
; /* Time */
1532 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1535 for (i
= 0; i
< 12; i
++)
1536 if (strcasecmp(mon
, months
[i
]) == 0)
1543 tdate
.tm_mday
= day
;
1544 tdate
.tm_year
= year
- 1900;
1545 tdate
.tm_hour
= hour
;
1550 return (mktime(&tdate
));
1555 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1558 http_status_t
/* O - HTTP status */
1559 httpUpdate(http_t
*http
) /* I - HTTP data */
1561 char line
[1024], /* Line from connection... */
1562 *value
; /* Pointer to value on line */
1563 http_field_t field
; /* Field index */
1564 int major
, minor
, /* HTTP version numbers */
1565 status
; /* Request status */
1568 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1571 * If we haven't issued any commands, then there is nothing to "update"...
1574 if (http
->state
== HTTP_WAITING
)
1575 return (HTTP_CONTINUE
);
1578 * Grab all of the lines we can from the connection...
1581 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1583 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1585 if (line
[0] == '\0')
1588 * Blank line means the start of the data section (if any). Return
1589 * the result code, too...
1591 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1592 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1596 if (http
->status
== HTTP_CONTINUE
)
1597 return (http
->status
);
1599 if (http
->status
< HTTP_BAD_REQUEST
)
1600 http
->digest_tries
= 0;
1603 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1605 if (http_setup_ssl(http
) != 0)
1608 closesocket(http
->fd
);
1613 return (HTTP_ERROR
);
1616 return (HTTP_CONTINUE
);
1618 #endif /* HAVE_SSL */
1620 httpGetLength(http
);
1622 switch (http
->state
)
1626 case HTTP_POST_RECV
:
1629 case HTTP_POST_SEND
:
1633 http
->state
= HTTP_WAITING
;
1637 return (http
->status
);
1639 else if (strncmp(line
, "HTTP/", 5) == 0)
1642 * Got the beginning of a response...
1645 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1646 return (HTTP_ERROR
);
1648 http
->version
= (http_version_t
)(major
* 100 + minor
);
1649 http
->status
= (http_status_t
)status
;
1651 else if ((value
= strchr(line
, ':')) != NULL
)
1658 while (isspace(*value
& 255))
1662 * Be tolerants of servers that send unknown attribute fields...
1665 if (!strcasecmp(line
, "expect"))
1668 * "Expect: 100-continue" or similar...
1671 http
->expect
= (http_status_t
)atoi(value
);
1673 else if (!strcasecmp(line
, "cookie"))
1676 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1679 httpSetCookie(http
, value
);
1681 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1683 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1687 httpSetField(http
, field
, value
);
1691 http
->status
= HTTP_ERROR
;
1692 return (HTTP_ERROR
);
1697 * See if there was an error...
1700 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
1701 return (http
->status
);
1705 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
1706 strerror(http
->error
)));
1707 http
->status
= HTTP_ERROR
;
1708 return (HTTP_ERROR
);
1712 * If we haven't already returned, then there is nothing new...
1715 return (HTTP_CONTINUE
);
1720 * 'httpDecode64()' - Base64-decode a string.
1723 char * /* O - Decoded string */
1724 httpDecode64(char *out
, /* I - String to write to */
1725 const char *in
) /* I - String to read from */
1727 return (httpDecode64_2(out
, in
, 512));
1732 * 'httpDecode64_2()' - Base64-decode a string.
1735 char * /* O - Decoded string */
1736 httpDecode64_2(char *out
, /* I - String to write to */
1737 const char *in
, /* I - String to read from */
1738 int outlen
) /* I - Size of output string */
1740 int pos
, /* Bit position */
1741 base64
; /* Value of this character */
1742 char *outptr
, /* Output pointer */
1743 *outend
; /* End of output buffer */
1746 for (outptr
= out
, outend
= out
+ outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
1749 * Decode this character into a number from 0 to 63...
1752 if (*in
>= 'A' && *in
<= 'Z')
1754 else if (*in
>= 'a' && *in
<= 'z')
1755 base64
= *in
- 'a' + 26;
1756 else if (*in
>= '0' && *in
<= '9')
1757 base64
= *in
- '0' + 52;
1758 else if (*in
== '+')
1760 else if (*in
== '/')
1762 else if (*in
== '=')
1768 * Store the result in the appropriate chars...
1774 if (outptr
< outend
)
1775 *outptr
= base64
<< 2;
1779 if (outptr
< outend
)
1780 *outptr
++ |= (base64
>> 4) & 3;
1781 if (outptr
< outend
)
1782 *outptr
= (base64
<< 4) & 255;
1786 if (outptr
< outend
)
1787 *outptr
++ |= (base64
>> 2) & 15;
1788 if (outptr
< outend
)
1789 *outptr
= (base64
<< 6) & 255;
1793 if (outptr
< outend
)
1794 *outptr
++ |= base64
;
1803 * Return the decoded string...
1811 * 'httpEncode64()' - Base64-encode a string.
1814 char * /* O - Encoded string */
1815 httpEncode64(char *out
, /* I - String to write to */
1816 const char *in
) /* I - String to read from */
1818 return (httpEncode64_2(out
, in
, 512));
1823 * 'httpEncode64_2()' - Base64-encode a string.
1826 char * /* O - Encoded string */
1827 httpEncode64_2(char *out
, /* I - String to write to */
1828 const char *in
, /* I - String to read from */
1829 int outlen
) /* I - Size of output string */
1831 char *outptr
, /* Output pointer */
1832 *outend
; /* End of output buffer */
1833 static const char base64
[] = /* Base64 characters... */
1835 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1836 "abcdefghijklmnopqrstuvwxyz"
1842 for (outptr
= out
, outend
= out
+ outlen
- 1; *in
!= '\0'; in
++)
1845 * Encode the up to 3 characters as 4 Base64 numbers...
1848 if (outptr
< outend
)
1849 *outptr
++ = base64
[in
[0] >> 2];
1850 if (outptr
< outend
)
1851 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1856 if (outptr
< outend
)
1858 if (outptr
< outend
)
1863 if (outptr
< outend
)
1864 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1869 if (outptr
< outend
)
1874 if (outptr
< outend
)
1875 *outptr
++ = base64
[in
[0] & 63];
1881 * Return the encoded string...
1889 * 'httpGetLength()' - Get the amount of data remaining from the
1890 * content-length or transfer-encoding fields.
1893 int /* O - Content length */
1894 httpGetLength(http_t
*http
) /* I - HTTP data */
1896 DEBUG_printf(("httpGetLength(http=%p), state=%d\n", http
, http
->state
));
1898 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1900 DEBUG_puts("httpGetLength: chunked request!");
1902 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1903 http
->data_remaining
= 0;
1907 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1910 * The following is a hack for HTTP servers that don't send a
1911 * content-length or transfer-encoding field...
1913 * If there is no content-length then the connection must close
1914 * after the transfer is complete...
1917 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1918 http
->data_remaining
= 2147483647;
1920 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1922 DEBUG_printf(("httpGetLength: content_length=%d\n", http
->data_remaining
));
1925 return (http
->data_remaining
);
1930 * 'http_field()' - Return the field index for a field name.
1933 static http_field_t
/* O - Field index */
1934 http_field(const char *name
) /* I - String name */
1936 int i
; /* Looping var */
1939 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1940 if (strcasecmp(name
, http_fields
[i
]) == 0)
1941 return ((http_field_t
)i
);
1943 return (HTTP_FIELD_UNKNOWN
);
1948 * 'http_send()' - Send a request with all fields and the trailing blank line.
1951 static int /* O - 0 on success, non-zero on error */
1952 http_send(http_t
*http
, /* I - HTTP data */
1953 http_state_t request
, /* I - Request code */
1954 const char *uri
) /* I - URI */
1956 int i
; /* Looping var */
1957 char *ptr
, /* Pointer in buffer */
1958 buf
[1024]; /* Encoded URI buffer */
1959 static const char * const codes
[] =
1960 { /* Request code strings */
1975 static const char hex
[] = "0123456789ABCDEF";
1979 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
1980 http
, codes
[request
], uri
));
1982 if (http
== NULL
|| uri
== NULL
)
1986 * Encode the URI as needed...
1989 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1990 if (*uri
<= ' ' || *uri
>= 127)
1992 if (ptr
< (buf
+ sizeof(buf
) - 1))
1994 if (ptr
< (buf
+ sizeof(buf
) - 1))
1995 *ptr
++ = hex
[(*uri
>> 4) & 15];
1996 if (ptr
< (buf
+ sizeof(buf
) - 1))
1997 *ptr
++ = hex
[*uri
& 15];
2005 * See if we had an error the last time around; if so, reconnect...
2008 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
2009 httpReconnect(http
);
2012 * Send the request header...
2015 http
->state
= request
;
2016 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
2019 http
->status
= HTTP_CONTINUE
;
2022 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
2024 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
2025 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
2027 #endif /* HAVE_SSL */
2029 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
2031 http
->status
= HTTP_ERROR
;
2035 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2036 if (http
->fields
[i
][0] != '\0')
2038 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
2040 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
2042 http
->status
= HTTP_ERROR
;
2047 if (httpPrintf(http
, "\r\n") < 1)
2049 http
->status
= HTTP_ERROR
;
2053 httpClearFields(http
);
2060 * 'http_wait()' - Wait for data available on a connection.
2063 static int /* O - 1 if data is available, 0 otherwise */
2064 http_wait(http_t
*http
, /* I - HTTP data */
2065 int msec
) /* I - Milliseconds to wait */
2068 struct rlimit limit
; /* Runtime limit */
2070 struct timeval timeout
; /* Timeout */
2071 int nfds
; /* Result from select() */
2072 int set_size
; /* Size of select set */
2075 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http
, msec
));
2078 * Check the SSL/TLS buffers for data first...
2085 if (SSL_pending((SSL
*)(http
->tls
)))
2087 # elif defined(HAVE_GNUTLS)
2088 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2090 # elif defined(HAVE_CDSASSL)
2091 size_t bytes
; /* Bytes that are available */
2093 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2095 # endif /* HAVE_LIBSSL */
2097 #endif /* HAVE_SSL */
2100 * Then try doing a select() to poll the socket...
2103 if (!http
->input_set
)
2107 * Windows has a fixed-size select() structure, different (surprise,
2108 * surprise!) from all UNIX implementations. Just allocate this
2109 * fixed structure...
2112 http
->input_set
= calloc(1, sizeof(fd_set
));
2115 * Allocate the select() input set based upon the max number of file
2116 * descriptors available for this process...
2119 getrlimit(RLIMIT_NOFILE
, &limit
);
2121 set_size
= (limit
.rlim_cur
+ 31) / 8 + 4;
2122 if (set_size
< sizeof(fd_set
))
2123 set_size
= sizeof(fd_set
);
2125 http
->input_set
= calloc(1, set_size
);
2128 if (!http
->input_set
)
2132 FD_SET(http
->fd
, http
->input_set
);
2136 timeout
.tv_sec
= msec
/ 1000;
2137 timeout
.tv_usec
= (msec
% 1000) * 1000;
2139 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2142 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2144 FD_CLR(http
->fd
, http
->input_set
);
2152 * 'http_upgrade()' - Force upgrade to TLS encryption.
2155 static int /* O - Status of connection */
2156 http_upgrade(http_t
*http
) /* I - HTTP data */
2158 int ret
; /* Return value */
2159 http_t myhttp
; /* Local copy of HTTP data */
2162 DEBUG_printf(("http_upgrade(%p)\n", http
));
2165 * Copy the HTTP data to a local variable so we can do the OPTIONS
2166 * request without interfering with the existing request data...
2169 memcpy(&myhttp
, http
, sizeof(myhttp
));
2172 * Send an OPTIONS request to the server, requiring SSL or TLS
2173 * encryption on the link...
2176 httpClearFields(&myhttp
);
2177 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2178 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2180 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2183 * Wait for the secure connection...
2186 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2192 * Copy the HTTP data back over, if any...
2195 http
->fd
= myhttp
.fd
;
2196 http
->error
= myhttp
.error
;
2197 http
->activity
= myhttp
.activity
;
2198 http
->status
= myhttp
.status
;
2199 http
->version
= myhttp
.version
;
2200 http
->keep_alive
= myhttp
.keep_alive
;
2201 http
->used
= myhttp
.used
;
2204 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2206 http
->auth_type
= myhttp
.auth_type
;
2207 http
->nonce_count
= myhttp
.nonce_count
;
2209 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2211 http
->tls
= myhttp
.tls
;
2212 http
->encryption
= myhttp
.encryption
;
2215 * See if we actually went secure...
2221 * Server does not support HTTP upgrade...
2224 DEBUG_puts("Server does not support HTTP upgrade!");
2227 closesocket(http
->fd
);
2242 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2245 static int /* O - Status of connection */
2246 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2249 SSL_CTX
*context
; /* Context for encryption */
2250 SSL
*conn
; /* Connection for encryption */
2251 # elif defined(HAVE_GNUTLS)
2252 http_tls_t
*conn
; /* TLS session object */
2253 gnutls_certificate_client_credentials
*credentials
;
2254 /* TLS credentials */
2255 # elif defined(HAVE_CDSASSL)
2256 SSLContextRef conn
; /* Context for encryption */
2257 OSStatus error
; /* Error info */
2258 # endif /* HAVE_LIBSSL */
2261 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
2264 context
= SSL_CTX_new(SSLv23_client_method());
2265 conn
= SSL_new(context
);
2267 SSL_set_fd(conn
, http
->fd
);
2268 if (SSL_connect(conn
) != 1)
2271 unsigned long error
; /* Error code */
2273 while ((error
= ERR_get_error()) != 0)
2274 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
2277 SSL_CTX_free(context
);
2281 http
->error
= WSAGetLastError();
2283 http
->error
= errno
;
2285 http
->status
= HTTP_ERROR
;
2287 return (HTTP_ERROR
);
2290 # elif defined(HAVE_GNUTLS)
2291 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2295 http
->error
= errno
;
2296 http
->status
= HTTP_ERROR
;
2301 credentials
= (gnutls_certificate_client_credentials
*)
2302 malloc(sizeof(gnutls_certificate_client_credentials
));
2303 if (credentials
== NULL
)
2307 http
->error
= errno
;
2308 http
->status
= HTTP_ERROR
;
2313 gnutls_certificate_allocate_credentials(credentials
);
2315 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2316 gnutls_set_default_priority(conn
->session
);
2317 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2318 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2320 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2322 http
->error
= errno
;
2323 http
->status
= HTTP_ERROR
;
2328 conn
->credentials
= credentials
;
2330 # elif defined(HAVE_CDSASSL)
2331 error
= SSLNewContext(false, &conn
);
2334 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
2337 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2340 error
= SSLSetAllowsExpiredCerts(conn
, true);
2343 error
= SSLSetAllowsAnyRoot(conn
, true);
2346 error
= SSLHandshake(conn
);
2350 http
->error
= error
;
2351 http
->status
= HTTP_ERROR
;
2353 SSLDisposeContext(conn
);
2359 # endif /* HAVE_CDSASSL */
2367 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2371 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2374 SSL_CTX
*context
; /* Context for encryption */
2375 SSL
*conn
; /* Connection for encryption */
2378 conn
= (SSL
*)(http
->tls
);
2379 context
= SSL_get_SSL_CTX(conn
);
2382 SSL_CTX_free(context
);
2385 # elif defined(HAVE_GNUTLS)
2386 http_tls_t
*conn
; /* Encryption session */
2387 gnutls_certificate_client_credentials
*credentials
;
2388 /* TLS credentials */
2391 conn
= (http_tls_t
*)(http
->tls
);
2392 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2394 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2395 gnutls_deinit(conn
->session
);
2396 gnutls_certificate_free_credentials(*credentials
);
2400 # elif defined(HAVE_CDSASSL)
2401 SSLClose((SSLContextRef
)http
->tls
);
2402 SSLDisposeContext((SSLContextRef
)http
->tls
);
2403 # endif /* HAVE_LIBSSL */
2410 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2413 static int /* O - Bytes read */
2414 http_read_ssl(http_t
*http
, /* I - HTTP data */
2415 char *buf
, /* I - Buffer to store data */
2416 int len
) /* I - Length of buffer */
2418 # if defined(HAVE_LIBSSL)
2419 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2421 # elif defined(HAVE_GNUTLS)
2422 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2424 # elif defined(HAVE_CDSASSL)
2425 OSStatus error
; /* Error info */
2426 size_t processed
; /* Number of bytes processed */
2429 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2435 http
->error
= error
;
2439 # endif /* HAVE_LIBSSL */
2444 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2447 static int /* O - Bytes written */
2448 http_write_ssl(http_t
*http
, /* I - HTTP data */
2449 const char *buf
, /* I - Buffer holding data */
2450 int len
) /* I - Length of buffer */
2452 # if defined(HAVE_LIBSSL)
2453 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2455 # elif defined(HAVE_GNUTLS)
2456 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2457 # elif defined(HAVE_CDSASSL)
2458 OSStatus error
; /* Error info */
2459 size_t processed
; /* Number of bytes processed */
2462 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2468 http
->error
= error
;
2471 # endif /* HAVE_LIBSSL */
2475 # if defined(HAVE_CDSASSL)
2477 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2480 static OSStatus
/* O - -1 on error, 0 on success */
2481 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2482 void *data
, /* I - Data buffer */
2483 size_t *dataLength
) /* IO - Number of bytes */
2485 ssize_t bytes
; /* Number of bytes read */
2488 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2491 *dataLength
= bytes
;
2500 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2503 static OSStatus
/* O - -1 on error, 0 on success */
2504 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2505 const void *data
, /* I - Data buffer */
2506 size_t *dataLength
) /* IO - Number of bytes */
2511 bytes
= write((int)connection
, data
, *dataLength
);
2514 *dataLength
= bytes
;
2520 # endif /* HAVE_CDSASSL */
2521 #endif /* HAVE_SSL */
2525 * End of "$Id: http.c,v 1.82.2.55 2004/07/06 00:35:31 mike Exp $".