4 * HTTP routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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 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 * httpCheck() - Check to see if there is a pending response from
30 * httpClearCookie() - Clear the cookie value(s).
31 * httpClose() - Close an HTTP connection...
32 * httpConnect() - Connect to a HTTP server.
33 * httpConnectEncrypt() - Connect to a HTTP server using encryption.
34 * httpDelete() - Send a DELETE request to the server.
35 * httpEncryption() - Set the required encryption on the link.
36 * httpFlush() - Flush data from a HTTP connection.
37 * httpFlushWrite() - Flush data in write buffer.
38 * httpGet() - Send a GET request to the server.
39 * httpGetLength() - Get the amount of data remaining from the
40 * content-length or transfer-encoding fields.
41 * httpGetLength2() - Get the amount of data remaining from the
42 * content-length or transfer-encoding fields.
43 * httpGetSubField() - Get a sub-field value.
44 * httpGets() - Get a line of text from a HTTP connection.
45 * httpHead() - Send a HEAD request to the server.
46 * httpInitialize() - Initialize the HTTP interface library and set the
47 * default HTTP proxy (if any).
48 * httpOptions() - Send an OPTIONS request to the server.
49 * httpPost() - Send a POST request to the server.
50 * httpPrintf() - Print a formatted string to a HTTP connection.
51 * httpPut() - Send a PUT request to the server.
52 * httpRead() - Read data from a HTTP connection.
53 * httpRead2() - Read data from a HTTP connection.
54 * _httpReadCDSA() - Read function for CDSA decryption code.
55 * httpReconnect() - Reconnect to a HTTP server...
56 * httpSetCookie() - Set the cookie value(s)...
57 * httpSetField() - Set the value of an HTTP header.
58 * httpSetLength() - Set the content-length and transfer-encoding.
59 * httpTrace() - Send an TRACE request to the server.
60 * httpUpdate() - Update the current HTTP state for incoming data.
61 * httpWait() - Wait for data available on a connection.
62 * httpWrite() - Write data to a HTTP connection.
63 * httpWrite2() - Write data to a HTTP connection.
64 * _httpWriteCDSA() - Write function for CDSA encryption code.
65 * http_field() - Return the field index for a field name.
66 * http_read_ssl() - Read from a SSL/TLS connection.
67 * http_send() - Send a request with all fields and the trailing
69 * http_setup_ssl() - Set up SSL/TLS on a connection.
70 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
71 * http_upgrade() - Force upgrade to TLS encryption.
72 * http_wait() - Wait for data available on a connection.
73 * http_write() - Write data to a connection.
74 * http_write_ssl() - Write to a SSL/TLS connection.
78 * Include necessary headers...
81 #include "http-private.h"
89 # include <sys/time.h>
90 # include <sys/resource.h>
95 * Some operating systems have done away with the Fxxxx constants for
96 * the fcntl() call; this works around that "feature"...
100 # define FNONBLK O_NONBLOCK
101 #endif /* !FNONBLK */
108 static http_field_t
http_field(const char *name
);
109 static int http_send(http_t
*http
, http_state_t request
,
111 static int http_wait(http_t
*http
, int msec
);
112 static int http_write(http_t
*http
, const char *buffer
,
114 static int http_write_chunk(http_t
*http
, const char *buffer
,
117 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
118 static int http_setup_ssl(http_t
*http
);
119 static void http_shutdown_ssl(http_t
*http
);
120 static int http_upgrade(http_t
*http
);
121 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
122 #endif /* HAVE_SSL */
129 static const char * const http_fields
[] =
146 "If-Unmodified-since",
162 * 'httpCheck()' - Check to see if there is a pending response from the server.
165 int /* O - 0 = no data, 1 = data available */
166 httpCheck(http_t
*http
) /* I - HTTP connection */
168 return (httpWait(http
, 0));
173 * 'httpClearCookie()' - Clear the cookie value(s).
175 * @since CUPS 1.1.19@
179 httpClearCookie(http_t
*http
) /* I - Connection */
193 * 'httpClose()' - Close an HTTP connection...
197 httpClose(http_t
*http
) /* I - Connection to close */
199 DEBUG_printf(("httpClose(http=%p)\n", http
));
204 httpAddrFreeList(http
->addrlist
);
207 free(http
->input_set
);
214 http_shutdown_ssl(http
);
215 #endif /* HAVE_SSL */
218 closesocket(http
->fd
);
228 * 'httpConnect()' - Connect to a HTTP server.
231 http_t
* /* O - New HTTP connection */
232 httpConnect(const char *host
, /* I - Host to connect to */
233 int port
) /* I - Port number */
235 http_encryption_t encryption
; /* Type of encryption to use */
239 * Set the default encryption status...
243 encryption
= HTTP_ENCRYPT_ALWAYS
;
245 encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
247 return (httpConnectEncrypt(host
, port
, encryption
));
252 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
255 http_t
* /* O - New HTTP connection */
257 const char *host
, /* I - Host to connect to */
258 int port
, /* I - Port number */
259 http_encryption_t encryption
) /* I - Type of encryption to use */
261 http_t
*http
; /* New HTTP connection */
262 http_addrlist_t
*addrlist
; /* Host address data */
263 char service
[255]; /* Service name */
266 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n",
267 host
? host
: "(null)", port
, encryption
));
278 sprintf(service
, "%d", port
);
280 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
284 * Allocate memory for the structure...
287 http
= calloc(sizeof(http_t
), 1);
291 http
->version
= HTTP_1_1
;
293 http
->activity
= time(NULL
);
297 * Set the encryption status...
300 if (port
== 443) /* Always use encryption for https */
301 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
303 http
->encryption
= encryption
;
306 * Loop through the addresses we have until one of them connects...
309 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
312 * Connect to the remote system...
315 http
->addrlist
= addrlist
;
317 if (!httpReconnect(http
))
321 * Could not connect to any known address - bail out!
324 httpAddrFreeList(addrlist
);
333 * 'httpDelete()' - Send a DELETE request to the server.
336 int /* O - Status of call (0 = success) */
337 httpDelete(http_t
*http
, /* I - HTTP data */
338 const char *uri
) /* I - URI to delete */
340 return (http_send(http
, HTTP_DELETE
, uri
));
345 * 'httpEncryption()' - Set the required encryption on the link.
348 int /* O - -1 on error, 0 on success */
349 httpEncryption(http_t
*http
, /* I - HTTP data */
350 http_encryption_t e
) /* I - New encryption preference */
352 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
358 http
->encryption
= e
;
360 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
361 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
362 return (httpReconnect(http
));
363 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
364 return (http_upgrade(http
));
368 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
372 #endif /* HAVE_SSL */
377 * 'httpFlush()' - Flush data from a HTTP connection.
381 httpFlush(http_t
*http
) /* I - HTTP data */
383 char buffer
[8192]; /* Junk buffer */
384 int blocking
; /* To block or not to block */
387 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
390 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
393 blocking
= http
->blocking
;
397 * Read any data we can...
400 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
403 * Restore blocking and reset the connection if we didn't get all of
404 * the remaining data...
407 http
->blocking
= blocking
;
409 if (http
->state
!= HTTP_WAITING
&& http
->fd
>= 0)
412 * Didn't get the data back, so close the current connection.
415 http
->state
= HTTP_WAITING
;
419 http_shutdown_ssl(http
);
420 #endif /* HAVE_SSL */
423 closesocket(http
->fd
);
434 * 'httpFlushWrite()' - Flush data in write buffer.
439 int /* O - Bytes written or -1 on error */
440 httpFlushWrite(http_t
*http
) /* I - HTTP data */
442 int bytes
; /* Bytes written */
445 DEBUG_printf(("httpFlushWrite(http=%p)\n", http
));
447 if (!http
|| !http
->wused
)
450 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
451 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
453 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
462 * 'httpGet()' - Send a GET request to the server.
465 int /* O - Status of call (0 = success) */
466 httpGet(http_t
*http
, /* I - HTTP data */
467 const char *uri
) /* I - URI to get */
469 return (http_send(http
, HTTP_GET
, uri
));
474 * 'httpGetSubField()' - Get a sub-field value.
479 char * /* O - Value or NULL */
480 httpGetSubField(http_t
*http
, /* I - HTTP data */
481 http_field_t field
, /* I - Field index */
482 const char *name
, /* I - Name of sub-field */
483 char *value
) /* O - Value string */
485 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
490 * 'httpGetSubField2()' - Get a sub-field value.
495 char * /* O - Value or NULL */
496 httpGetSubField2(http_t
*http
, /* I - HTTP data */
497 http_field_t field
, /* I - Field index */
498 const char *name
, /* I - Name of sub-field */
499 char *value
, /* O - Value string */
500 int valuelen
) /* I - Size of value buffer */
502 const char *fptr
; /* Pointer into field */
503 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
504 *ptr
, /* Pointer into string buffer */
505 *end
; /* End of value buffer */
507 DEBUG_printf(("httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)\n",
508 http
, field
, name
, value
, valuelen
));
510 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
511 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
512 field
> HTTP_FIELD_WWW_AUTHENTICATE
)
515 end
= value
+ valuelen
- 1;
517 for (fptr
= http
->fields
[field
]; *fptr
;)
520 * Skip leading whitespace...
523 while (isspace(*fptr
& 255))
533 * Get the sub-field name...
537 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) &&
538 ptr
< (temp
+ sizeof(temp
) - 1);
543 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
546 * Skip trailing chars up to the '='...
549 while (isspace(*fptr
& 255))
559 * Skip = and leading whitespace...
564 while (isspace(*fptr
& 255))
570 * Read quoted string...
573 for (ptr
= value
, fptr
++;
574 *fptr
&& *fptr
!= '\"' && ptr
< end
;
579 while (*fptr
&& *fptr
!= '\"')
588 * Read unquoted string...
592 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< end
;
597 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
601 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
604 * See if this is the one...
607 if (!strcmp(name
, temp
))
618 * 'httpGetLength()' - Get the amount of data remaining from the
619 * content-length or transfer-encoding fields.
621 * This function is deprecated and will not return lengths larger than
622 * 2^31 - 1; use httpGetLength2() instead.
627 int /* O - Content length */
628 httpGetLength(http_t
*http
) /* I - HTTP data */
631 * Get the read content length and return the 32-bit value.
634 httpGetLength2(http
);
636 return (http
->_data_remaining
);
641 * 'httpGetLength2()' - Get the amount of data remaining from the
642 * content-length or transfer-encoding fields.
644 * This function returns the complete content length, even for
645 * content larger than 2^31 - 1.
648 off_t
/* O - Content length */
649 httpGetLength2(http_t
*http
) /* I - HTTP data */
651 DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http
, http
->state
));
653 if (!strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
655 DEBUG_puts("httpGetLength2: chunked request!");
657 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
658 http
->data_remaining
= 0;
662 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
665 * The following is a hack for HTTP servers that don't send a
666 * content-length or transfer-encoding field...
668 * If there is no content-length then the connection must close
669 * after the transfer is complete...
672 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
673 http
->data_remaining
= 2147483647;
675 http
->data_remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
678 DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT
"\n",
679 CUPS_LLCAST http
->data_remaining
));
682 if (http
->data_remaining
<= INT_MAX
)
683 http
->_data_remaining
= (int)http
->data_remaining
;
685 http
->_data_remaining
= INT_MAX
;
687 return (http
->data_remaining
);
692 * 'httpGets()' - Get a line of text from a HTTP connection.
695 char * /* O - Line or NULL */
696 httpGets(char *line
, /* I - Line to read into */
697 int length
, /* I - Max length of buffer */
698 http_t
*http
) /* I - HTTP data */
700 char *lineptr
, /* Pointer into line */
701 *lineend
, /* End of line */
702 *bufptr
, /* Pointer into input buffer */
703 *bufend
; /* Pointer to end of buffer */
704 int bytes
, /* Number of bytes read */
705 eol
; /* End-of-line? */
708 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
710 if (http
== NULL
|| line
== NULL
)
714 * Read a line from the buffer...
718 lineend
= line
+ length
- 1;
721 while (lineptr
< lineend
)
724 * Pre-load the buffer as needed...
733 while (http
->used
== 0)
736 * No newline; see if there is more data to be read...
739 if (!http
->blocking
&& !http_wait(http
, 1000))
744 bytes
= http_read_ssl(http
, http
->buffer
+ http
->used
,
745 HTTP_MAX_BUFFER
- http
->used
);
747 #endif /* HAVE_SSL */
748 bytes
= recv(http
->fd
, http
->buffer
+ http
->used
,
749 HTTP_MAX_BUFFER
- http
->used
, 0);
751 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
756 * Nope, can't get a line this time...
760 if (WSAGetLastError() != http
->error
)
762 http
->error
= WSAGetLastError();
766 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
768 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
772 else if (errno
!= http
->error
)
789 * Yup, update the amount used...
796 * Now copy as much of the current line as possible...
799 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
800 lineptr
< lineend
&& bufptr
< bufend
;)
808 else if (*bufptr
== 0x0d)
811 *lineptr
++ = *bufptr
++;
814 http
->used
-= bufptr
- http
->buffer
;
816 memmove(http
->buffer
, bufptr
, http
->used
);
824 http
->activity
= time(NULL
);
828 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
834 DEBUG_puts("httpGets: No new line available!");
841 * 'httpHead()' - Send a HEAD request to the server.
844 int /* O - Status of call (0 = success) */
845 httpHead(http_t
*http
, /* I - HTTP data */
846 const char *uri
) /* I - URI for head */
848 return (http_send(http
, HTTP_HEAD
, uri
));
853 * 'httpInitialize()' - Initialize the HTTP interface library and set the
854 * default HTTP proxy (if any).
862 struct timeval curtime
; /* Current time in microseconds */
864 int i
; /* Looping var */
865 unsigned char data
[1024]; /* Seed data */
866 #endif /* HAVE_LIBSSL */
869 WSADATA winsockdata
; /* WinSock data */
870 static int initialized
= 0; /* Has WinSock been initialized? */
874 WSAStartup(MAKEWORD(1,1), &winsockdata
);
875 #elif !defined(SO_NOSIGPIPE)
877 * Ignore SIGPIPE signals...
881 sigset(SIGPIPE
, SIG_IGN
);
882 # elif defined(HAVE_SIGACTION)
883 struct sigaction action
; /* POSIX sigaction data */
886 memset(&action
, 0, sizeof(action
));
887 action
.sa_handler
= SIG_IGN
;
888 sigaction(SIGPIPE
, &action
, NULL
);
890 signal(SIGPIPE
, SIG_IGN
);
891 # endif /* !SO_NOSIGPIPE */
895 gnutls_global_init();
896 #endif /* HAVE_GNUTLS */
899 SSL_load_error_strings();
903 * Using the current time is a dubious random seed, but on some systems
904 * it is the best we can do (on others, this seed isn't even used...)
909 gettimeofday(&curtime
, NULL
);
910 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
913 for (i
= 0; i
< sizeof(data
); i
++)
914 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
916 RAND_seed(&data
, sizeof(data
));
917 #endif /* HAVE_LIBSSL */
922 * 'httpOptions()' - Send an OPTIONS request to the server.
925 int /* O - Status of call (0 = success) */
926 httpOptions(http_t
*http
, /* I - HTTP data */
927 const char *uri
) /* I - URI for options */
929 return (http_send(http
, HTTP_OPTIONS
, uri
));
934 * 'httpPost()' - Send a POST request to the server.
937 int /* O - Status of call (0 = success) */
938 httpPost(http_t
*http
, /* I - HTTP data */
939 const char *uri
) /* I - URI for post */
941 return (http_send(http
, HTTP_POST
, uri
));
946 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
949 int /* O - Number of bytes written */
950 httpPrintf(http_t
*http
, /* I - HTTP data */
951 const char *format
, /* I - printf-style format string */
952 ...) /* I - Additional args as needed */
954 int bytes
; /* Number of bytes to write */
955 char buf
[16384]; /* Buffer for formatted string */
956 va_list ap
; /* Variable argument pointer */
959 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
961 va_start(ap
, format
);
962 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
965 DEBUG_printf(("httpPrintf: %s", buf
));
969 DEBUG_puts(" flushing existing data...");
971 if (httpFlushWrite(http
) < 0)
975 return (http_write(http
, buf
, bytes
));
980 * 'httpPut()' - Send a PUT request to the server.
983 int /* O - Status of call (0 = success) */
984 httpPut(http_t
*http
, /* I - HTTP data */
985 const char *uri
) /* I - URI to put */
987 return (http_send(http
, HTTP_PUT
, uri
));
992 * 'httpRead()' - Read data from a HTTP connection.
994 * This function is deprecated. Use the httpRead2() function which can
995 * read more than 2GB of data.
1000 int /* O - Number of bytes read */
1001 httpRead(http_t
*http
, /* I - HTTP data */
1002 char *buffer
, /* I - Buffer for data */
1003 int length
) /* I - Maximum number of bytes */
1005 return ((int)httpRead2(http
, buffer
, length
));
1010 * 'httpRead2()' - Read data from a HTTP connection.
1013 ssize_t
/* O - Number of bytes read */
1014 httpRead2(http_t
*http
, /* I - HTTP data */
1015 char *buffer
, /* I - Buffer for data */
1016 size_t length
) /* I - Maximum number of bytes */
1018 ssize_t bytes
; /* Bytes read */
1019 char len
[32]; /* Length string */
1022 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
1023 http
, buffer
, length
));
1025 if (http
== NULL
|| buffer
== NULL
)
1028 http
->activity
= time(NULL
);
1033 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
1034 http
->data_remaining
<= 0)
1036 DEBUG_puts("httpRead2: Getting chunk length...");
1038 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1040 DEBUG_puts("httpRead2: Could not get length!");
1044 http
->data_remaining
= strtoll(len
, NULL
, 16);
1045 if (http
->data_remaining
< 0)
1047 DEBUG_puts("httpRead2: Negative chunk length!");
1052 DEBUG_printf(("httpRead2: data_remaining=" CUPS_LLFMT
"\n",
1053 CUPS_LLCAST http
->data_remaining
));
1055 if (http
->data_remaining
<= 0)
1058 * A zero-length chunk ends a transfer; unless we are reading POST
1062 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1063 httpGets(len
, sizeof(len
), http
);
1065 if (http
->state
== HTTP_POST_RECV
)
1068 http
->state
= HTTP_WAITING
;
1071 * Prevent future reads for this request...
1074 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1078 else if (length
> http
->data_remaining
)
1079 length
= http
->data_remaining
;
1081 if (http
->used
== 0 && length
<= 256)
1084 * Buffer small reads for better performance...
1087 if (!http
->blocking
&& !httpWait(http
, 1000))
1090 if (http
->data_remaining
> sizeof(http
->buffer
))
1091 bytes
= sizeof(http
->buffer
);
1093 bytes
= http
->data_remaining
;
1097 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
1099 #endif /* HAVE_SSL */
1101 DEBUG_printf(("httpRead2: reading %d bytes from socket into buffer...\n",
1104 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
1106 DEBUG_printf(("httpRead2: read %d bytes from socket into buffer...\n",
1115 http
->error
= WSAGetLastError();
1120 http
->error
= errno
;
1127 http
->error
= EPIPE
;
1134 if (length
> http
->used
)
1135 length
= http
->used
;
1139 DEBUG_printf(("httpRead2: grabbing %d bytes from input buffer...\n", bytes
));
1141 memcpy(buffer
, http
->buffer
, length
);
1142 http
->used
-= length
;
1145 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1150 if (!http
->blocking
&& !httpWait(http
, 1000))
1153 bytes
= http_read_ssl(http
, buffer
, length
);
1155 #endif /* HAVE_SSL */
1158 if (!http
->blocking
&& !httpWait(http
, 1000))
1161 DEBUG_printf(("httpRead2: reading %d bytes from socket...\n", length
));
1163 while ((bytes
= recv(http
->fd
, buffer
, length
, 0)) < 0)
1167 DEBUG_printf(("httpRead2: read %d bytes from socket...\n", bytes
));
1172 http
->data_remaining
-= bytes
;
1174 if (http
->data_remaining
<= INT_MAX
)
1175 http
->_data_remaining
= (int)http
->data_remaining
;
1177 http
->_data_remaining
= INT_MAX
;
1182 http
->error
= WSAGetLastError();
1187 http
->error
= errno
;
1192 http
->error
= EPIPE
;
1196 if (http
->data_remaining
== 0)
1198 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1199 httpGets(len
, sizeof(len
), http
);
1201 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1203 if (http
->state
== HTTP_POST_RECV
)
1206 http
->state
= HTTP_WAITING
;
1213 printf("httpRead2: Read %d bytes:\n", bytes
);
1214 for (i
= 0; i
< bytes
; i
+= 16)
1218 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1219 printf(" %02X", buffer
[i
+ j
] & 255);
1228 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1230 ch
= buffer
[i
+ j
] & 255;
1232 if (ch
< ' ' || ch
>= 127)
1246 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1248 * '_httpReadCDSA()' - Read function for CDSA decryption code.
1251 OSStatus
/* O - -1 on error, 0 on success */
1253 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1254 void *data
, /* I - Data buffer */
1255 size_t *dataLength
) /* IO - Number of bytes */
1257 OSStatus result
; /* Return value */
1258 ssize_t bytes
; /* Number of bytes read */
1263 bytes
= recv((int)connection
, data
, *dataLength
, 0);
1267 result
= (bytes
== *dataLength
);
1268 *dataLength
= bytes
;
1274 return (errSSLClosedAbort
);
1276 if (errno
== EAGAIN
)
1277 return (errSSLWouldBlock
);
1280 return (errSSLClosedAbort
);
1283 return (errSSLInternal
);
1286 #endif /* HAVE_SSL && HAVE_CDSASSL */
1290 * 'httpReconnect()' - Reconnect to a HTTP server...
1293 int /* O - 0 on success, non-zero on failure */
1294 httpReconnect(http_t
*http
) /* I - HTTP data */
1296 http_addrlist_t
*addr
; /* Connected address */
1299 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
1306 http_shutdown_ssl(http
);
1307 #endif /* HAVE_SSL */
1310 * Close any previously open socket...
1316 closesocket(http
->fd
);
1325 * Connect to the server...
1328 if ((addr
= httpAddrConnect(http
->addrlist
, &(http
->fd
))) == NULL
)
1331 * Unable to connect...
1335 http
->error
= WSAGetLastError();
1337 http
->error
= errno
;
1339 http
->status
= HTTP_ERROR
;
1344 http
->hostaddr
= &(addr
->addr
);
1346 http
->status
= HTTP_CONTINUE
;
1349 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
1352 * Always do encryption via SSL.
1355 if (http_setup_ssl(http
) != 0)
1358 closesocket(http
->fd
);
1366 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
1367 return (http_upgrade(http
));
1368 #endif /* HAVE_SSL */
1375 * 'httpSetCookie()' - Set the cookie value(s)...
1377 * @since CUPS 1.1.19@
1381 httpSetCookie(http_t
*http
, /* I - Connection */
1382 const char *cookie
) /* I - Cookie string */
1391 http
->cookie
= strdup(cookie
);
1393 http
->cookie
= NULL
;
1398 * 'httpSetField()' - Set the value of an HTTP header.
1402 httpSetField(http_t
*http
, /* I - HTTP data */
1403 http_field_t field
, /* I - Field index */
1404 const char *value
) /* I - Value */
1407 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
1408 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
1412 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
1417 * 'httpSetLength()' - Set the content-length and content-encoding.
1423 httpSetLength(http_t
*http
, /* I - HTTP data */
1424 size_t length
) /* I - Length (0 for chunked) */
1431 strcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked");
1432 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
1436 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
1437 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
1438 CUPS_LLFMT
, CUPS_LLCAST length
);
1444 * 'httpTrace()' - Send an TRACE request to the server.
1447 int /* O - Status of call (0 = success) */
1448 httpTrace(http_t
*http
, /* I - HTTP data */
1449 const char *uri
) /* I - URI for trace */
1451 return (http_send(http
, HTTP_TRACE
, uri
));
1456 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1459 http_status_t
/* O - HTTP status */
1460 httpUpdate(http_t
*http
) /* I - HTTP data */
1462 char line
[32768], /* Line from connection... */
1463 *value
; /* Pointer to value on line */
1464 http_field_t field
; /* Field index */
1465 int major
, minor
, /* HTTP version numbers */
1466 status
; /* Request status */
1469 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1472 * Flush pending data, if any...
1477 DEBUG_puts(" flushing buffer...");
1479 if (httpFlushWrite(http
) < 0)
1480 return (HTTP_ERROR
);
1484 * If we haven't issued any commands, then there is nothing to "update"...
1487 if (http
->state
== HTTP_WAITING
)
1488 return (HTTP_CONTINUE
);
1491 * Grab all of the lines we can from the connection...
1494 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1496 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1498 if (line
[0] == '\0')
1501 * Blank line means the start of the data section (if any). Return
1502 * the result code, too...
1504 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1505 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1509 if (http
->status
== HTTP_CONTINUE
)
1510 return (http
->status
);
1512 if (http
->status
< HTTP_BAD_REQUEST
)
1513 http
->digest_tries
= 0;
1516 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1518 if (http_setup_ssl(http
) != 0)
1521 closesocket(http
->fd
);
1526 return (HTTP_ERROR
);
1529 return (HTTP_CONTINUE
);
1531 #endif /* HAVE_SSL */
1533 httpGetLength2(http
);
1535 switch (http
->state
)
1539 case HTTP_POST_RECV
:
1542 case HTTP_POST_SEND
:
1546 http
->state
= HTTP_WAITING
;
1550 return (http
->status
);
1552 else if (strncmp(line
, "HTTP/", 5) == 0)
1555 * Got the beginning of a response...
1558 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1559 return (HTTP_ERROR
);
1561 http
->version
= (http_version_t
)(major
* 100 + minor
);
1562 http
->status
= (http_status_t
)status
;
1564 else if ((value
= strchr(line
, ':')) != NULL
)
1571 while (isspace(*value
& 255))
1575 * Be tolerants of servers that send unknown attribute fields...
1578 if (!strcasecmp(line
, "expect"))
1581 * "Expect: 100-continue" or similar...
1584 http
->expect
= (http_status_t
)atoi(value
);
1586 else if (!strcasecmp(line
, "cookie"))
1589 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1592 httpSetCookie(http
, value
);
1594 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1596 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1600 httpSetField(http
, field
, value
);
1604 http
->status
= HTTP_ERROR
;
1605 return (HTTP_ERROR
);
1610 * See if there was an error...
1613 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
1614 return (http
->status
);
1618 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
1619 strerror(http
->error
)));
1620 http
->status
= HTTP_ERROR
;
1621 return (HTTP_ERROR
);
1625 * If we haven't already returned, then there is nothing new...
1628 return (HTTP_CONTINUE
);
1633 * 'httpWait()' - Wait for data available on a connection.
1635 * @since CUPS 1.1.19@
1638 int /* O - 1 if data is available, 0 otherwise */
1639 httpWait(http_t
*http
, /* I - HTTP data */
1640 int msec
) /* I - Milliseconds to wait */
1643 * First see if there is data in the buffer...
1653 * If not, check the SSL/TLS buffers and do a select() on the connection...
1656 return (http_wait(http
, msec
));
1661 * 'httpWrite()' - Write data to a HTTP connection.
1663 * This function is deprecated. Use the httpWrite2() function which can
1664 * write more than 2GB of data.
1669 int /* O - Number of bytes written */
1670 httpWrite(http_t
*http
, /* I - HTTP data */
1671 const char *buffer
, /* I - Buffer for data */
1672 int length
) /* I - Number of bytes to write */
1674 return ((int)httpWrite2(http
, buffer
, length
));
1679 * 'httpWrite2()' - Write data to a HTTP connection.
1682 ssize_t
/* O - Number of bytes written */
1683 httpWrite2(http_t
*http
, /* I - HTTP data */
1684 const char *buffer
, /* I - Buffer for data */
1685 size_t length
) /* I - Number of bytes to write */
1687 ssize_t bytes
; /* Bytes written */
1690 DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http
,
1694 * Range check input...
1697 if (http
== NULL
|| buffer
== NULL
)
1701 * Mark activity on the connection...
1704 http
->activity
= time(NULL
);
1707 * Buffer small writes for better performance...
1712 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
1714 DEBUG_printf((" flushing buffer (wused=%d, length=%d)\n",
1715 http
->wused
, length
));
1717 httpFlushWrite(http
);
1720 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
))
1723 * Write to buffer...
1726 DEBUG_printf((" copying %d bytes to wbuffer...\n", length
));
1728 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
1729 http
->wused
+= length
;
1735 * Otherwise write the data directly...
1738 DEBUG_printf((" writing %d bytes to socket...\n", length
));
1740 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1741 bytes
= http_write_chunk(http
, buffer
, length
);
1743 bytes
= http_write(http
, buffer
, length
);
1745 DEBUG_printf((" wrote %d bytes...\n", bytes
));
1748 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1749 http
->data_remaining
-= bytes
;
1755 * Handle end-of-request processing...
1758 if ((http
->data_encoding
== HTTP_ENCODE_CHUNKED
&& length
== 0) ||
1759 (http
->data_encoding
== HTTP_ENCODE_LENGTH
&& http
->data_remaining
== 0))
1762 * Finished with the transfer; unless we are sending POST or PUT
1766 DEBUG_puts("httpWrite: changing states...");
1769 httpFlushWrite(http
);
1771 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1774 * Send a 0-length chunk at the end of the request...
1777 http_write(http
, "0\r\n\r\n", 5);
1780 * Reset the data state...
1783 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1784 http
->data_remaining
= 0;
1787 if (http
->state
== HTTP_POST_RECV
)
1789 else if (http
->state
== HTTP_PUT_RECV
)
1790 http
->state
= HTTP_STATUS
;
1792 http
->state
= HTTP_WAITING
;
1799 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1801 * '_httpWriteCDSA()' - Write function for CDSA encryption code.
1804 OSStatus
/* O - -1 on error, 0 on success */
1806 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1807 const void *data
, /* I - Data buffer */
1808 size_t *dataLength
) /* IO - Number of bytes */
1810 OSStatus result
; /* Return value */
1811 ssize_t bytes
; /* Number of bytes read */
1816 bytes
= write((int)connection
, data
, *dataLength
);
1820 result
= (bytes
== *dataLength
) ? 0 : errSSLWouldBlock
;
1821 *dataLength
= bytes
;
1826 if (errno
== EAGAIN
)
1827 return (errSSLWouldBlock
);
1830 return (errSSLClosedAbort
);
1833 return (errSSLInternal
);
1836 #endif /* HAVE_SSL && HAVE_CDSASSL */
1840 * 'http_field()' - Return the field index for a field name.
1843 static http_field_t
/* O - Field index */
1844 http_field(const char *name
) /* I - String name */
1846 int i
; /* Looping var */
1849 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1850 if (strcasecmp(name
, http_fields
[i
]) == 0)
1851 return ((http_field_t
)i
);
1853 return (HTTP_FIELD_UNKNOWN
);
1859 * 'http_read_ssl()' - Read from a SSL/TLS connection.
1862 static int /* O - Bytes read */
1863 http_read_ssl(http_t
*http
, /* I - HTTP data */
1864 char *buf
, /* I - Buffer to store data */
1865 int len
) /* I - Length of buffer */
1867 # if defined(HAVE_LIBSSL)
1868 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
1870 # elif defined(HAVE_GNUTLS)
1871 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
1873 # elif defined(HAVE_CDSASSL)
1874 int result
; /* Return value */
1875 OSStatus error
; /* Error info */
1876 size_t processed
; /* Number of bytes processed */
1879 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
1884 result
= (int)processed
;
1886 case errSSLClosedGraceful
:
1889 case errSSLWouldBlock
:
1900 # endif /* HAVE_LIBSSL */
1902 #endif /* HAVE_SSL */
1906 * 'http_send()' - Send a request with all fields and the trailing blank line.
1909 static int /* O - 0 on success, non-zero on error */
1910 http_send(http_t
*http
, /* I - HTTP data */
1911 http_state_t request
, /* I - Request code */
1912 const char *uri
) /* I - URI */
1914 int i
; /* Looping var */
1915 char *ptr
, /* Pointer in buffer */
1916 buf
[1024]; /* Encoded URI buffer */
1917 static const char * const codes
[] =
1918 { /* Request code strings */
1933 static const char hex
[] = "0123456789ABCDEF";
1937 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
1938 http
, codes
[request
], uri
));
1940 if (http
== NULL
|| uri
== NULL
)
1944 * Set the User-Agent field if it isn't already...
1947 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
1948 httpSetField(http
, HTTP_FIELD_USER_AGENT
, CUPS_MINIMAL
);
1951 * Encode the URI as needed...
1954 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1955 if (*uri
<= ' ' || *uri
>= 127)
1957 if (ptr
< (buf
+ sizeof(buf
) - 1))
1959 if (ptr
< (buf
+ sizeof(buf
) - 1))
1960 *ptr
++ = hex
[(*uri
>> 4) & 15];
1961 if (ptr
< (buf
+ sizeof(buf
) - 1))
1962 *ptr
++ = hex
[*uri
& 15];
1970 * See if we had an error the last time around; if so, reconnect...
1973 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1974 if (httpReconnect(http
))
1978 * Send the request header...
1981 http
->state
= request
;
1982 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1985 http
->status
= HTTP_CONTINUE
;
1988 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1990 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1991 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1993 #endif /* HAVE_SSL */
1995 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1997 http
->status
= HTTP_ERROR
;
2001 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2002 if (http
->fields
[i
][0] != '\0')
2004 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
2006 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
2008 http
->status
= HTTP_ERROR
;
2014 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
2016 http
->status
= HTTP_ERROR
;
2020 if (httpPrintf(http
, "\r\n") < 1)
2022 http
->status
= HTTP_ERROR
;
2026 httpGetLength2(http
);
2027 httpClearFields(http
);
2035 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2038 static int /* O - Status of connection */
2039 http_setup_ssl(http_t
*http
) /* I - HTTP data */
2042 SSL_CTX
*context
; /* Context for encryption */
2043 SSL
*conn
; /* Connection for encryption */
2044 # elif defined(HAVE_GNUTLS)
2045 http_tls_t
*conn
; /* TLS session object */
2046 gnutls_certificate_client_credentials
*credentials
;
2047 /* TLS credentials */
2048 # elif defined(HAVE_CDSASSL)
2049 SSLContextRef conn
; /* Context for encryption */
2050 OSStatus error
; /* Error info */
2051 # endif /* HAVE_LIBSSL */
2054 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
2057 context
= SSL_CTX_new(SSLv23_client_method());
2059 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
2061 conn
= SSL_new(context
);
2063 SSL_set_fd(conn
, http
->fd
);
2064 if (SSL_connect(conn
) != 1)
2067 unsigned long error
; /* Error code */
2069 while ((error
= ERR_get_error()) != 0)
2070 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
2073 SSL_CTX_free(context
);
2077 http
->error
= WSAGetLastError();
2079 http
->error
= errno
;
2081 http
->status
= HTTP_ERROR
;
2083 return (HTTP_ERROR
);
2086 # elif defined(HAVE_GNUTLS)
2087 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
2091 http
->error
= errno
;
2092 http
->status
= HTTP_ERROR
;
2097 credentials
= (gnutls_certificate_client_credentials
*)
2098 malloc(sizeof(gnutls_certificate_client_credentials
));
2099 if (credentials
== NULL
)
2103 http
->error
= errno
;
2104 http
->status
= HTTP_ERROR
;
2109 gnutls_certificate_allocate_credentials(credentials
);
2111 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2112 gnutls_set_default_priority(conn
->session
);
2113 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2114 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
2116 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
2118 http
->error
= errno
;
2119 http
->status
= HTTP_ERROR
;
2124 conn
->credentials
= credentials
;
2126 # elif defined(HAVE_CDSASSL)
2127 error
= SSLNewContext(false, &conn
);
2130 error
= SSLSetIOFuncs(conn
, _httpReadCDSA
, _httpWriteCDSA
);
2133 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
2136 error
= SSLSetAllowsExpiredCerts(conn
, true);
2139 error
= SSLSetAllowsAnyRoot(conn
, true);
2142 error
= SSLHandshake(conn
);
2146 http
->error
= error
;
2147 http
->status
= HTTP_ERROR
;
2149 SSLDisposeContext(conn
);
2155 # endif /* HAVE_CDSASSL */
2160 #endif /* HAVE_SSL */
2165 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2169 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2172 SSL_CTX
*context
; /* Context for encryption */
2173 SSL
*conn
; /* Connection for encryption */
2176 conn
= (SSL
*)(http
->tls
);
2177 context
= SSL_get_SSL_CTX(conn
);
2180 SSL_CTX_free(context
);
2183 # elif defined(HAVE_GNUTLS)
2184 http_tls_t
*conn
; /* Encryption session */
2185 gnutls_certificate_client_credentials
*credentials
;
2186 /* TLS credentials */
2189 conn
= (http_tls_t
*)(http
->tls
);
2190 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2192 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2193 gnutls_deinit(conn
->session
);
2194 gnutls_certificate_free_credentials(*credentials
);
2198 # elif defined(HAVE_CDSASSL)
2199 SSLClose((SSLContextRef
)http
->tls
);
2200 SSLDisposeContext((SSLContextRef
)http
->tls
);
2201 # endif /* HAVE_LIBSSL */
2205 #endif /* HAVE_SSL */
2210 * 'http_upgrade()' - Force upgrade to TLS encryption.
2213 static int /* O - Status of connection */
2214 http_upgrade(http_t
*http
) /* I - HTTP data */
2216 int ret
; /* Return value */
2217 http_t myhttp
; /* Local copy of HTTP data */
2220 DEBUG_printf(("http_upgrade(%p)\n", http
));
2223 * Copy the HTTP data to a local variable so we can do the OPTIONS
2224 * request without interfering with the existing request data...
2227 memcpy(&myhttp
, http
, sizeof(myhttp
));
2230 * Send an OPTIONS request to the server, requiring SSL or TLS
2231 * encryption on the link...
2234 httpClearFields(&myhttp
);
2235 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2236 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2238 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2241 * Wait for the secure connection...
2244 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2250 * Copy the HTTP data back over, if any...
2253 http
->fd
= myhttp
.fd
;
2254 http
->error
= myhttp
.error
;
2255 http
->activity
= myhttp
.activity
;
2256 http
->status
= myhttp
.status
;
2257 http
->version
= myhttp
.version
;
2258 http
->keep_alive
= myhttp
.keep_alive
;
2259 http
->used
= myhttp
.used
;
2262 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2264 http
->auth_type
= myhttp
.auth_type
;
2265 http
->nonce_count
= myhttp
.nonce_count
;
2267 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2269 http
->tls
= myhttp
.tls
;
2270 http
->encryption
= myhttp
.encryption
;
2273 * See if we actually went secure...
2279 * Server does not support HTTP upgrade...
2282 DEBUG_puts("Server does not support HTTP upgrade!");
2285 closesocket(http
->fd
);
2297 #endif /* HAVE_SSL */
2301 * 'http_wait()' - Wait for data available on a connection.
2304 static int /* O - 1 if data is available, 0 otherwise */
2305 http_wait(http_t
*http
, /* I - HTTP data */
2306 int msec
) /* I - Milliseconds to wait */
2309 struct rlimit limit
; /* Runtime limit */
2310 int set_size
; /* Size of select set */
2312 struct timeval timeout
; /* Timeout */
2313 int nfds
; /* Result from select() */
2316 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http
, msec
));
2322 * Check the SSL/TLS buffers for data first...
2329 if (SSL_pending((SSL
*)(http
->tls
)))
2331 # elif defined(HAVE_GNUTLS)
2332 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2334 # elif defined(HAVE_CDSASSL)
2335 size_t bytes
; /* Bytes that are available */
2337 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2339 # endif /* HAVE_LIBSSL */
2341 #endif /* HAVE_SSL */
2344 * Then try doing a select() to poll the socket...
2347 if (!http
->input_set
)
2351 * Windows has a fixed-size select() structure, different (surprise,
2352 * surprise!) from all UNIX implementations. Just allocate this
2353 * fixed structure...
2356 http
->input_set
= calloc(1, sizeof(fd_set
));
2359 * Allocate the select() input set based upon the max number of file
2360 * descriptors available for this process...
2363 getrlimit(RLIMIT_NOFILE
, &limit
);
2365 set_size
= (limit
.rlim_cur
+ 31) / 8 + 4;
2366 if (set_size
< sizeof(fd_set
))
2367 set_size
= sizeof(fd_set
);
2369 http
->input_set
= calloc(1, set_size
);
2372 if (!http
->input_set
)
2378 FD_SET(http
->fd
, http
->input_set
);
2382 timeout
.tv_sec
= msec
/ 1000;
2383 timeout
.tv_usec
= (msec
% 1000) * 1000;
2385 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2388 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2391 while (nfds
< 0 && WSAGetLastError() == WSAEINTR
);
2393 while (nfds
< 0 && errno
== EINTR
);
2396 FD_CLR(http
->fd
, http
->input_set
);
2403 * 'http_write()' - Write a buffer to a HTTP connection.
2406 static int /* O - Number of bytes written */
2407 http_write(http_t
*http
, /* I - HTTP data */
2408 const char *buffer
, /* I - Buffer for data */
2409 int length
) /* I - Number of bytes to write */
2411 int tbytes
, /* Total bytes sent */
2412 bytes
; /* Bytes sent */
2421 bytes
= http_write_ssl(http
, buffer
, length
);
2423 #endif /* HAVE_SSL */
2424 bytes
= send(http
->fd
, buffer
, length
, 0);
2429 if (WSAGetLastError() != http
->error
)
2431 http
->error
= WSAGetLastError();
2437 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
2439 http
->error
= errno
;
2444 DEBUG_puts("http_write: error writing data...\n");
2457 printf("http_write: wrote %d bytes: \n", tbytes
);
2458 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
2462 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
2463 printf(" %02X", buffer
[i
+ j
] & 255);
2472 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
2474 ch
= buffer
[i
+ j
] & 255;
2476 if (ch
< ' ' || ch
== 127)
2491 * 'http_write_chunk()' - Write a chunked buffer.
2494 static int /* O - Number bytes written */
2495 http_write_chunk(http_t
*http
, /* I - HTTP data */
2496 const char *buffer
, /* I - Buffer to write */
2497 int length
) /* I - Length of buffer */
2499 char header
[255]; /* Chunk header */
2500 int bytes
; /* Bytes written */
2502 DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n",
2503 http
, buffer
, length
));
2506 * Write the chunk header, data, and trailer.
2509 sprintf(header
, "%x\r\n", length
);
2510 if (http_write(http
, header
, strlen(header
)) < 0)
2512 DEBUG_puts(" http_write of length failed!");
2516 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
2518 DEBUG_puts(" http_write of buffer failed!");
2522 if (http_write(http
, "\r\n", 2) < 0)
2524 DEBUG_puts(" http_write of CR LF failed!");
2534 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2537 static int /* O - Bytes written */
2538 http_write_ssl(http_t
*http
, /* I - HTTP data */
2539 const char *buf
, /* I - Buffer holding data */
2540 int len
) /* I - Length of buffer */
2542 # if defined(HAVE_LIBSSL)
2543 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2545 # elif defined(HAVE_GNUTLS)
2546 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2547 # elif defined(HAVE_CDSASSL)
2548 int result
; /* Return value */
2549 OSStatus error
; /* Error info */
2550 size_t processed
; /* Number of bytes processed */
2553 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2558 result
= (int)processed
;
2560 case errSSLClosedGraceful
:
2563 case errSSLWouldBlock
:
2574 # endif /* HAVE_LIBSSL */
2576 #endif /* HAVE_SSL */