4 * HTTP routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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 * httpReconnect() - Reconnect to a HTTP server...
54 * httpSetCookie() - Set the cookie value(s)...
55 * httpSetField() - Set the value of an HTTP header.
56 * httpSetLength() - Set the content-length and transfer-encoding.
57 * httpTrace() - Send an TRACE request to the server.
58 * httpUpdate() - Update the current HTTP state for incoming data.
59 * httpWait() - Wait for data available on a connection.
60 * httpWrite() - Write data to a HTTP connection.
61 * http_field() - Return the field index for a field name.
62 * http_read_ssl() - Read from a SSL/TLS connection.
63 * http_send() - Send a request with all fields and the trailing
65 * http_setup_ssl() - Set up SSL/TLS on a connection.
66 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
67 * http_upgrade() - Force upgrade to TLS encryption.
68 * http_wait() - Wait for data available on a connection.
69 * http_write() - Write data to a connection.
70 * http_write_ssl() - Write to a SSL/TLS connection.
71 * http_read_cdsa() - Read function for CDSA decryption code.
72 * http_write_cdsa() - Write function for CDSA encryption code.
76 * Include necessary headers...
79 #include "http-private.h"
87 # include <sys/time.h>
88 # include <sys/resource.h>
93 * Some operating systems have done away with the Fxxxx constants for
94 * the fcntl() call; this works around that "feature"...
98 # define FNONBLK O_NONBLOCK
106 static http_field_t
http_field(const char *name
);
107 static int http_send(http_t
*http
, http_state_t request
,
109 static int http_wait(http_t
*http
, int msec
);
110 static int http_write(http_t
*http
, const char *buffer
,
112 static int http_write_chunk(http_t
*http
, const char *buffer
,
116 static OSStatus
http_read_cdsa(SSLConnectionRef connection
, void *data
, size_t *dataLength
);
117 # endif /* HAVE_CDSASSL */
118 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
119 static int http_setup_ssl(http_t
*http
);
120 static void http_shutdown_ssl(http_t
*http
);
121 static int http_upgrade(http_t
*http
);
123 static OSStatus
http_write_cdsa(SSLConnectionRef connection
, const void *data
, size_t *dataLength
);
124 # endif /* HAVE_CDSASSL */
125 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
126 #endif /* HAVE_SSL */
133 static const char * const http_fields
[] =
150 "If-Unmodified-since",
166 * 'httpCheck()' - Check to see if there is a pending response from the server.
169 int /* O - 0 = no data, 1 = data available */
170 httpCheck(http_t
*http
) /* I - HTTP connection */
172 return (httpWait(http
, 0));
177 * 'httpClearCookie()' - Clear the cookie value(s).
179 * @since CUPS 1.1.19@
183 httpClearCookie(http_t
*http
) /* I - Connection */
197 * 'httpClose()' - Close an HTTP connection...
201 httpClose(http_t
*http
) /* I - Connection to close */
203 DEBUG_printf(("httpClose(http=%p)\n", http
));
208 httpAddrFreeList(http
->addrlist
);
211 free(http
->input_set
);
218 http_shutdown_ssl(http
);
219 #endif /* HAVE_SSL */
222 closesocket(http
->fd
);
232 * 'httpConnect()' - Connect to a HTTP server.
235 http_t
* /* O - New HTTP connection */
236 httpConnect(const char *host
, /* I - Host to connect to */
237 int port
) /* I - Port number */
239 http_encryption_t encryption
; /* Type of encryption to use */
243 * Set the default encryption status...
247 encryption
= HTTP_ENCRYPT_ALWAYS
;
249 encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
251 return (httpConnectEncrypt(host
, port
, encryption
));
256 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
259 http_t
* /* O - New HTTP connection */
261 const char *host
, /* I - Host to connect to */
262 int port
, /* I - Port number */
263 http_encryption_t encryption
) /* I - Type of encryption to use */
265 http_t
*http
; /* New HTTP connection */
266 http_addrlist_t
*addrlist
; /* Host address data */
267 char service
[255]; /* Service name */
270 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n",
271 host
? host
: "(null)", port
, encryption
));
282 sprintf(service
, "%d", port
);
284 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
288 * Allocate memory for the structure...
291 http
= calloc(sizeof(http_t
), 1);
295 http
->version
= HTTP_1_1
;
297 http
->activity
= time(NULL
);
301 * Set the encryption status...
304 if (port
== 443) /* Always use encryption for https */
305 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
307 http
->encryption
= encryption
;
310 * Loop through the addresses we have until one of them connects...
313 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
316 * Connect to the remote system...
319 http
->addrlist
= addrlist
;
321 if (!httpReconnect(http
))
325 * Could not connect to any known address - bail out!
328 httpAddrFreeList(addrlist
);
337 * 'httpDelete()' - Send a DELETE request to the server.
340 int /* O - Status of call (0 = success) */
341 httpDelete(http_t
*http
, /* I - HTTP data */
342 const char *uri
) /* I - URI to delete */
344 return (http_send(http
, HTTP_DELETE
, uri
));
349 * 'httpEncryption()' - Set the required encryption on the link.
352 int /* O - -1 on error, 0 on success */
353 httpEncryption(http_t
*http
, /* I - HTTP data */
354 http_encryption_t e
) /* I - New encryption preference */
356 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
362 http
->encryption
= e
;
364 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
365 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
366 return (httpReconnect(http
));
367 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
368 return (http_upgrade(http
));
372 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
376 #endif /* HAVE_SSL */
381 * 'httpFlush()' - Flush data from a HTTP connection.
385 httpFlush(http_t
*http
) /* I - HTTP data */
387 char buffer
[8192]; /* Junk buffer */
390 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
392 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
397 * 'httpFlushWrite()' - Flush data in write buffer.
402 int /* O - Bytes written or -1 on error */
403 httpFlushWrite(http_t
*http
) /* I - HTTP data */
405 int bytes
; /* Bytes written */
408 DEBUG_printf(("httpFlushWrite(http=%p)\n", http
));
410 if (!http
|| !http
->wused
)
413 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
414 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
416 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
425 * 'httpGet()' - Send a GET request to the server.
428 int /* O - Status of call (0 = success) */
429 httpGet(http_t
*http
, /* I - HTTP data */
430 const char *uri
) /* I - URI to get */
432 return (http_send(http
, HTTP_GET
, uri
));
437 * 'httpGetSubField()' - Get a sub-field value.
440 char * /* O - Value or NULL */
441 httpGetSubField(http_t
*http
, /* I - HTTP data */
442 http_field_t field
, /* I - Field index */
443 const char *name
, /* I - Name of sub-field */
444 char *value
) /* O - Value string */
446 const char *fptr
; /* Pointer into field */
447 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
448 *ptr
; /* Pointer into string buffer */
451 DEBUG_printf(("httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p)\n",
452 http
, field
, name
, value
));
455 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
456 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
457 name
== NULL
|| value
== NULL
)
460 for (fptr
= http
->fields
[field
]; *fptr
;)
463 * Skip leading whitespace...
466 while (isspace(*fptr
& 255))
476 * Get the sub-field name...
480 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) && ptr
< (temp
+ sizeof(temp
) - 1);
485 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
488 * Skip trailing chars up to the '='...
491 while (isspace(*fptr
& 255))
501 * Skip = and leading whitespace...
506 while (isspace(*fptr
& 255))
512 * Read quoted string...
515 for (ptr
= value
, fptr
++;
516 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
521 while (*fptr
&& *fptr
!= '\"')
530 * Read unquoted string...
534 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
539 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
543 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
546 * See if this is the one...
549 if (strcmp(name
, temp
) == 0)
560 * 'httpGetLength()' - Get the amount of data remaining from the
561 * content-length or transfer-encoding fields.
563 * This function is deprecated and will not return lengths larger than
564 * 2^31 - 1; use httpGetLength2() instead.
569 int /* O - Content length */
570 httpGetLength(http_t
*http
) /* I - HTTP data */
573 * Get the read content length and return the 32-bit value.
576 httpGetLength2(http
);
578 return (http
->_data_remaining
);
583 * 'httpGetLength2()' - Get the amount of data remaining from the
584 * content-length or transfer-encoding fields.
586 * This function returns the complete content length, even for
587 * content larger than 2^31 - 1.
590 off_t
/* O - Content length */
591 httpGetLength2(http_t
*http
) /* I - HTTP data */
593 DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http
, http
->state
));
595 if (!strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
597 DEBUG_puts("httpGetLength2: chunked request!");
599 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
600 http
->data_remaining
= 0;
604 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
607 * The following is a hack for HTTP servers that don't send a
608 * content-length or transfer-encoding field...
610 * If there is no content-length then the connection must close
611 * after the transfer is complete...
614 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
615 http
->data_remaining
= 2147483647;
617 http
->data_remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
620 DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT
"\n",
621 CUPS_LLCAST http
->data_remaining
));
624 if (http
->data_remaining
<= INT_MAX
)
625 http
->_data_remaining
= (int)http
->data_remaining
;
627 http
->_data_remaining
= INT_MAX
;
629 return (http
->data_remaining
);
634 * 'httpGets()' - Get a line of text from a HTTP connection.
637 char * /* O - Line or NULL */
638 httpGets(char *line
, /* I - Line to read into */
639 int length
, /* I - Max length of buffer */
640 http_t
*http
) /* I - HTTP data */
642 char *lineptr
, /* Pointer into line */
643 *lineend
, /* End of line */
644 *bufptr
, /* Pointer into input buffer */
645 *bufend
; /* Pointer to end of buffer */
646 int bytes
, /* Number of bytes read */
647 eol
; /* End-of-line? */
650 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
652 if (http
== NULL
|| line
== NULL
)
656 * Read a line from the buffer...
660 lineend
= line
+ length
- 1;
663 while (lineptr
< lineend
)
666 * Pre-load the buffer as needed...
675 while (http
->used
== 0)
678 * No newline; see if there is more data to be read...
681 if (!http
->blocking
&& !http_wait(http
, 1000))
686 bytes
= http_read_ssl(http
, http
->buffer
+ http
->used
,
687 HTTP_MAX_BUFFER
- http
->used
);
689 #endif /* HAVE_SSL */
690 bytes
= recv(http
->fd
, http
->buffer
+ http
->used
,
691 HTTP_MAX_BUFFER
- http
->used
, 0);
693 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
698 * Nope, can't get a line this time...
702 if (WSAGetLastError() != http
->error
)
704 http
->error
= WSAGetLastError();
708 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
710 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
714 else if (errno
!= http
->error
)
731 * Yup, update the amount used...
738 * Now copy as much of the current line as possible...
741 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
742 lineptr
< lineend
&& bufptr
< bufend
;)
750 else if (*bufptr
== 0x0d)
753 *lineptr
++ = *bufptr
++;
756 http
->used
-= bufptr
- http
->buffer
;
758 memmove(http
->buffer
, bufptr
, http
->used
);
766 http
->activity
= time(NULL
);
770 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
776 DEBUG_puts("httpGets: No new line available!");
783 * 'httpHead()' - Send a HEAD request to the server.
786 int /* O - Status of call (0 = success) */
787 httpHead(http_t
*http
, /* I - HTTP data */
788 const char *uri
) /* I - URI for head */
790 return (http_send(http
, HTTP_HEAD
, uri
));
795 * 'httpInitialize()' - Initialize the HTTP interface library and set the
796 * default HTTP proxy (if any).
804 struct timeval curtime
; /* Current time in microseconds */
806 int i
; /* Looping var */
807 unsigned char data
[1024]; /* Seed data */
808 #endif /* HAVE_LIBSSL */
811 WSADATA winsockdata
; /* WinSock data */
812 static int initialized
= 0; /* Has WinSock been initialized? */
816 WSAStartup(MAKEWORD(1,1), &winsockdata
);
817 #elif defined(HAVE_SIGSET)
818 sigset(SIGPIPE
, SIG_IGN
);
819 #elif defined(HAVE_SIGACTION)
820 struct sigaction action
; /* POSIX sigaction data */
824 * Ignore SIGPIPE signals...
827 memset(&action
, 0, sizeof(action
));
828 action
.sa_handler
= SIG_IGN
;
829 sigaction(SIGPIPE
, &action
, NULL
);
831 signal(SIGPIPE
, SIG_IGN
);
835 gnutls_global_init();
836 #endif /* HAVE_GNUTLS */
839 SSL_load_error_strings();
843 * Using the current time is a dubious random seed, but on some systems
844 * it is the best we can do (on others, this seed isn't even used...)
849 gettimeofday(&curtime
, NULL
);
850 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
853 for (i
= 0; i
< sizeof(data
); i
++)
854 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
856 RAND_seed(&data
, sizeof(data
));
857 #endif /* HAVE_LIBSSL */
862 * 'httpOptions()' - Send an OPTIONS request to the server.
865 int /* O - Status of call (0 = success) */
866 httpOptions(http_t
*http
, /* I - HTTP data */
867 const char *uri
) /* I - URI for options */
869 return (http_send(http
, HTTP_OPTIONS
, uri
));
874 * 'httpPost()' - Send a POST request to the server.
877 int /* O - Status of call (0 = success) */
878 httpPost(http_t
*http
, /* I - HTTP data */
879 const char *uri
) /* I - URI for post */
881 return (http_send(http
, HTTP_POST
, uri
));
886 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
889 int /* O - Number of bytes written */
890 httpPrintf(http_t
*http
, /* I - HTTP data */
891 const char *format
, /* I - printf-style format string */
892 ...) /* I - Additional args as needed */
894 int bytes
; /* Number of bytes to write */
895 char buf
[16384]; /* Buffer for formatted string */
896 va_list ap
; /* Variable argument pointer */
899 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
901 va_start(ap
, format
);
902 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
905 DEBUG_printf(("httpPrintf: %s", buf
));
909 DEBUG_puts(" flushing existing data...");
911 if (httpFlushWrite(http
) < 0)
915 return (http_write(http
, buf
, bytes
));
920 * 'httpPut()' - Send a PUT request to the server.
923 int /* O - Status of call (0 = success) */
924 httpPut(http_t
*http
, /* I - HTTP data */
925 const char *uri
) /* I - URI to put */
927 return (http_send(http
, HTTP_PUT
, uri
));
932 * 'httpRead()' - Read data from a HTTP connection.
935 int /* O - Number of bytes read */
936 httpRead(http_t
*http
, /* I - HTTP data */
937 char *buffer
, /* I - Buffer for data */
938 int length
) /* I - Maximum number of bytes */
940 int bytes
; /* Bytes read */
941 char len
[32]; /* Length string */
944 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
945 http
, buffer
, length
));
947 if (http
== NULL
|| buffer
== NULL
)
950 http
->activity
= time(NULL
);
955 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
956 http
->data_remaining
<= 0)
958 DEBUG_puts("httpRead: Getting chunk length...");
960 if (httpGets(len
, sizeof(len
), http
) == NULL
)
962 DEBUG_puts("httpRead: Could not get length!");
966 http
->data_remaining
= strtoll(len
, NULL
, 16);
967 if (http
->data_remaining
< 0)
969 DEBUG_puts("httpRead: Negative chunk length!");
974 DEBUG_printf(("httpRead: data_remaining=" CUPS_LLFMT
"\n",
975 CUPS_LLCAST http
->data_remaining
));
977 if (http
->data_remaining
<= 0)
980 * A zero-length chunk ends a transfer; unless we are reading POST
984 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
985 httpGets(len
, sizeof(len
), http
);
987 if (http
->state
== HTTP_POST_RECV
)
990 http
->state
= HTTP_WAITING
;
993 * Prevent future reads for this request...
996 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1000 else if (length
> http
->data_remaining
)
1001 length
= http
->data_remaining
;
1003 if (http
->used
== 0 && length
<= 256)
1006 * Buffer small reads for better performance...
1009 if (!http
->blocking
&& !httpWait(http
, 1000))
1012 if (http
->data_remaining
> sizeof(http
->buffer
))
1013 bytes
= sizeof(http
->buffer
);
1015 bytes
= http
->data_remaining
;
1019 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
1021 #endif /* HAVE_SSL */
1023 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
1026 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
1028 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
1037 http
->error
= WSAGetLastError();
1042 http
->error
= errno
;
1049 http
->error
= EPIPE
;
1056 if (length
> http
->used
)
1057 length
= http
->used
;
1061 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
1063 memcpy(buffer
, http
->buffer
, length
);
1064 http
->used
-= length
;
1067 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1072 if (!http
->blocking
&& !httpWait(http
, 1000))
1075 bytes
= http_read_ssl(http
, buffer
, length
);
1077 #endif /* HAVE_SSL */
1080 if (!http
->blocking
&& !httpWait(http
, 1000))
1083 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
1085 while ((bytes
= recv(http
->fd
, buffer
, length
, 0)) < 0)
1089 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
1094 http
->data_remaining
-= bytes
;
1096 if (http
->data_remaining
<= INT_MAX
)
1097 http
->_data_remaining
= (int)http
->data_remaining
;
1099 http
->_data_remaining
= INT_MAX
;
1104 http
->error
= WSAGetLastError();
1109 http
->error
= errno
;
1114 http
->error
= EPIPE
;
1118 if (http
->data_remaining
== 0)
1120 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1121 httpGets(len
, sizeof(len
), http
);
1123 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1125 if (http
->state
== HTTP_POST_RECV
)
1128 http
->state
= HTTP_WAITING
;
1135 printf("httpRead: Read %d bytes:\n", bytes
);
1136 for (i
= 0; i
< bytes
; i
+= 16)
1140 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1141 printf(" %02X", buffer
[i
+ j
] & 255);
1150 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1152 ch
= buffer
[i
+ j
] & 255;
1154 if (ch
< ' ' || ch
== 127)
1169 * 'httpReconnect()' - Reconnect to a HTTP server...
1172 int /* O - 0 on success, non-zero on failure */
1173 httpReconnect(http_t
*http
) /* I - HTTP data */
1175 http_addrlist_t
*addr
; /* Connected address */
1178 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
1185 http_shutdown_ssl(http
);
1186 #endif /* HAVE_SSL */
1189 * Close any previously open socket...
1194 closesocket(http
->fd
);
1200 * Connect to the server...
1203 if ((addr
= httpAddrConnect(http
->addrlist
, &(http
->fd
))) == NULL
)
1206 * Unable to connect...
1210 http
->error
= WSAGetLastError();
1212 http
->error
= errno
;
1214 http
->status
= HTTP_ERROR
;
1219 http
->hostaddr
= &(addr
->addr
);
1221 http
->status
= HTTP_CONTINUE
;
1224 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
1227 * Always do encryption via SSL.
1230 if (http_setup_ssl(http
) != 0)
1233 closesocket(http
->fd
);
1241 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
1242 return (http_upgrade(http
));
1243 #endif /* HAVE_SSL */
1250 * 'httpSetCookie()' - Set the cookie value(s)...
1252 * @since CUPS 1.1.19@
1256 httpSetCookie(http_t
*http
, /* I - Connection */
1257 const char *cookie
) /* I - Cookie string */
1266 http
->cookie
= strdup(cookie
);
1268 http
->cookie
= NULL
;
1273 * 'httpSetField()' - Set the value of an HTTP header.
1277 httpSetField(http_t
*http
, /* I - HTTP data */
1278 http_field_t field
, /* I - Field index */
1279 const char *value
) /* I - Value */
1282 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
1283 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
1287 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
1292 * 'httpSetLength()' - Set the content-length and content-encoding.
1298 httpSetLength(http_t
*http
, /* I - HTTP data */
1299 off_t length
) /* I - Length (0 for chunked) */
1301 if (!http
|| length
< 0)
1306 strcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked");
1307 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
1311 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
1312 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
1313 CUPS_LLFMT
, CUPS_LLCAST length
);
1319 * 'httpTrace()' - Send an TRACE request to the server.
1322 int /* O - Status of call (0 = success) */
1323 httpTrace(http_t
*http
, /* I - HTTP data */
1324 const char *uri
) /* I - URI for trace */
1326 return (http_send(http
, HTTP_TRACE
, uri
));
1331 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1334 http_status_t
/* O - HTTP status */
1335 httpUpdate(http_t
*http
) /* I - HTTP data */
1337 char line
[32768], /* Line from connection... */
1338 *value
; /* Pointer to value on line */
1339 http_field_t field
; /* Field index */
1340 int major
, minor
, /* HTTP version numbers */
1341 status
; /* Request status */
1344 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1347 * Flush pending data, if any...
1352 DEBUG_puts(" flushing buffer...");
1354 if (httpFlushWrite(http
) < 0)
1355 return (HTTP_ERROR
);
1359 * If we haven't issued any commands, then there is nothing to "update"...
1362 if (http
->state
== HTTP_WAITING
)
1363 return (HTTP_CONTINUE
);
1366 * Grab all of the lines we can from the connection...
1369 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1371 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1373 if (line
[0] == '\0')
1376 * Blank line means the start of the data section (if any). Return
1377 * the result code, too...
1379 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1380 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1384 if (http
->status
== HTTP_CONTINUE
)
1385 return (http
->status
);
1387 if (http
->status
< HTTP_BAD_REQUEST
)
1388 http
->digest_tries
= 0;
1391 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1393 if (http_setup_ssl(http
) != 0)
1396 closesocket(http
->fd
);
1401 return (HTTP_ERROR
);
1404 return (HTTP_CONTINUE
);
1406 #endif /* HAVE_SSL */
1408 httpGetLength2(http
);
1410 switch (http
->state
)
1414 case HTTP_POST_RECV
:
1417 case HTTP_POST_SEND
:
1421 http
->state
= HTTP_WAITING
;
1425 return (http
->status
);
1427 else if (strncmp(line
, "HTTP/", 5) == 0)
1430 * Got the beginning of a response...
1433 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1434 return (HTTP_ERROR
);
1436 http
->version
= (http_version_t
)(major
* 100 + minor
);
1437 http
->status
= (http_status_t
)status
;
1439 else if ((value
= strchr(line
, ':')) != NULL
)
1446 while (isspace(*value
& 255))
1450 * Be tolerants of servers that send unknown attribute fields...
1453 if (!strcasecmp(line
, "expect"))
1456 * "Expect: 100-continue" or similar...
1459 http
->expect
= (http_status_t
)atoi(value
);
1461 else if (!strcasecmp(line
, "cookie"))
1464 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1467 httpSetCookie(http
, value
);
1469 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1471 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1475 httpSetField(http
, field
, value
);
1479 http
->status
= HTTP_ERROR
;
1480 return (HTTP_ERROR
);
1485 * See if there was an error...
1488 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
1489 return (http
->status
);
1493 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
1494 strerror(http
->error
)));
1495 http
->status
= HTTP_ERROR
;
1496 return (HTTP_ERROR
);
1500 * If we haven't already returned, then there is nothing new...
1503 return (HTTP_CONTINUE
);
1508 * 'httpWait()' - Wait for data available on a connection.
1510 * @since CUPS 1.1.19@
1513 int /* O - 1 if data is available, 0 otherwise */
1514 httpWait(http_t
*http
, /* I - HTTP data */
1515 int msec
) /* I - Milliseconds to wait */
1518 * First see if there is data in the buffer...
1528 * If not, check the SSL/TLS buffers and do a select() on the connection...
1531 return (http_wait(http
, msec
));
1536 * 'httpWrite()' - Write data to a HTTP connection.
1539 int /* O - Number of bytes written */
1540 httpWrite(http_t
*http
, /* I - HTTP data */
1541 const char *buffer
, /* I - Buffer for data */
1542 int length
) /* I - Number of bytes to write */
1544 int bytes
; /* Bytes written */
1547 DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http
,
1551 * Range check input...
1554 if (http
== NULL
|| buffer
== NULL
)
1558 * Mark activity on the connection...
1561 http
->activity
= time(NULL
);
1564 * Buffer small writes for better performance...
1569 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
1571 DEBUG_printf((" flushing buffer (wused=%d, length=%d)\n",
1572 http
->wused
, length
));
1574 httpFlushWrite(http
);
1577 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
))
1580 * Write to buffer...
1583 DEBUG_printf((" copying %d bytes to wbuffer...\n", length
));
1585 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
1586 http
->wused
+= length
;
1592 * Otherwise write the data directly...
1595 DEBUG_printf((" writing %d bytes to socket...\n", length
));
1597 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1598 bytes
= http_write_chunk(http
, buffer
, length
);
1600 bytes
= http_write(http
, buffer
, length
);
1602 DEBUG_printf((" wrote %d bytes...\n", bytes
));
1605 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1606 http
->data_remaining
-= bytes
;
1610 * Handle end-of-request processing...
1613 if ((http
->data_encoding
== HTTP_ENCODE_CHUNKED
&& length
== 0) ||
1614 (http
->data_encoding
== HTTP_ENCODE_LENGTH
&& http
->data_remaining
== 0))
1617 * Finished with the transfer; unless we are sending POST or PUT
1621 DEBUG_puts("httpWrite: changing states...");
1624 httpFlushWrite(http
);
1626 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1629 * Send a 0-length chunk at the end of the request...
1632 http_write(http
, "0\r\n\r\n", 5);
1635 * Reset the data state...
1638 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1639 http
->data_remaining
= 0;
1642 if (http
->state
== HTTP_POST_RECV
)
1644 else if (http
->state
== HTTP_PUT_RECV
)
1645 http
->state
= HTTP_STATUS
;
1647 http
->state
= HTTP_WAITING
;
1655 * 'http_field()' - Return the field index for a field name.
1658 static http_field_t
/* O - Field index */
1659 http_field(const char *name
) /* I - String name */
1661 int i
; /* Looping var */
1664 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1665 if (strcasecmp(name
, http_fields
[i
]) == 0)
1666 return ((http_field_t
)i
);
1668 return (HTTP_FIELD_UNKNOWN
);
1672 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1674 * 'http_read_cdsa()' - Read function for CDSA decryption code.
1677 static OSStatus
/* O - -1 on error, 0 on success */
1679 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1680 void *data
, /* I - Data buffer */
1681 size_t *dataLength
) /* IO - Number of bytes */
1683 ssize_t bytes
; /* Number of bytes read */
1686 bytes
= recv((int)connection
, data
, *dataLength
, 0);
1689 *dataLength
= bytes
;
1695 #endif /* HAVE_SSL && HAVE_CDSASSL */
1700 * 'http_read_ssl()' - Read from a SSL/TLS connection.
1703 static int /* O - Bytes read */
1704 http_read_ssl(http_t
*http
, /* I - HTTP data */
1705 char *buf
, /* I - Buffer to store data */
1706 int len
) /* I - Length of buffer */
1708 # if defined(HAVE_LIBSSL)
1709 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
1711 # elif defined(HAVE_GNUTLS)
1712 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
1714 # elif defined(HAVE_CDSASSL)
1715 OSStatus error
; /* Error info */
1716 size_t processed
; /* Number of bytes processed */
1719 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
1725 http
->error
= error
;
1729 # endif /* HAVE_LIBSSL */
1731 #endif /* HAVE_SSL */
1735 * 'http_send()' - Send a request with all fields and the trailing blank line.
1738 static int /* O - 0 on success, non-zero on error */
1739 http_send(http_t
*http
, /* I - HTTP data */
1740 http_state_t request
, /* I - Request code */
1741 const char *uri
) /* I - URI */
1743 int i
; /* Looping var */
1744 char *ptr
, /* Pointer in buffer */
1745 buf
[1024]; /* Encoded URI buffer */
1746 static const char * const codes
[] =
1747 { /* Request code strings */
1762 static const char hex
[] = "0123456789ABCDEF";
1766 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
1767 http
, codes
[request
], uri
));
1769 if (http
== NULL
|| uri
== NULL
)
1773 * Encode the URI as needed...
1776 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1777 if (*uri
<= ' ' || *uri
>= 127)
1779 if (ptr
< (buf
+ sizeof(buf
) - 1))
1781 if (ptr
< (buf
+ sizeof(buf
) - 1))
1782 *ptr
++ = hex
[(*uri
>> 4) & 15];
1783 if (ptr
< (buf
+ sizeof(buf
) - 1))
1784 *ptr
++ = hex
[*uri
& 15];
1792 * See if we had an error the last time around; if so, reconnect...
1795 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1796 httpReconnect(http
);
1799 * Send the request header...
1802 http
->state
= request
;
1803 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1806 http
->status
= HTTP_CONTINUE
;
1809 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1811 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1812 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1814 #endif /* HAVE_SSL */
1816 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1818 http
->status
= HTTP_ERROR
;
1822 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1823 if (http
->fields
[i
][0] != '\0')
1825 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1827 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1829 http
->status
= HTTP_ERROR
;
1834 if (httpPrintf(http
, "\r\n") < 1)
1836 http
->status
= HTTP_ERROR
;
1840 httpGetLength2(http
);
1841 httpClearFields(http
);
1849 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
1852 static int /* O - Status of connection */
1853 http_setup_ssl(http_t
*http
) /* I - HTTP data */
1856 SSL_CTX
*context
; /* Context for encryption */
1857 SSL
*conn
; /* Connection for encryption */
1858 # elif defined(HAVE_GNUTLS)
1859 http_tls_t
*conn
; /* TLS session object */
1860 gnutls_certificate_client_credentials
*credentials
;
1861 /* TLS credentials */
1862 # elif defined(HAVE_CDSASSL)
1863 SSLContextRef conn
; /* Context for encryption */
1864 OSStatus error
; /* Error info */
1865 # endif /* HAVE_LIBSSL */
1868 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
1871 context
= SSL_CTX_new(SSLv23_client_method());
1873 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
1875 conn
= SSL_new(context
);
1877 SSL_set_fd(conn
, http
->fd
);
1878 if (SSL_connect(conn
) != 1)
1881 unsigned long error
; /* Error code */
1883 while ((error
= ERR_get_error()) != 0)
1884 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
1887 SSL_CTX_free(context
);
1891 http
->error
= WSAGetLastError();
1893 http
->error
= errno
;
1895 http
->status
= HTTP_ERROR
;
1897 return (HTTP_ERROR
);
1900 # elif defined(HAVE_GNUTLS)
1901 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
1905 http
->error
= errno
;
1906 http
->status
= HTTP_ERROR
;
1911 credentials
= (gnutls_certificate_client_credentials
*)
1912 malloc(sizeof(gnutls_certificate_client_credentials
));
1913 if (credentials
== NULL
)
1917 http
->error
= errno
;
1918 http
->status
= HTTP_ERROR
;
1923 gnutls_certificate_allocate_credentials(credentials
);
1925 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
1926 gnutls_set_default_priority(conn
->session
);
1927 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
1928 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
1930 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
1932 http
->error
= errno
;
1933 http
->status
= HTTP_ERROR
;
1938 conn
->credentials
= credentials
;
1940 # elif defined(HAVE_CDSASSL)
1941 error
= SSLNewContext(false, &conn
);
1944 error
= SSLSetIOFuncs(conn
, http_read_cdsa
, http_write_cdsa
);
1947 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
1950 error
= SSLSetAllowsExpiredCerts(conn
, true);
1953 error
= SSLSetAllowsAnyRoot(conn
, true);
1956 error
= SSLHandshake(conn
);
1960 http
->error
= error
;
1961 http
->status
= HTTP_ERROR
;
1963 SSLDisposeContext(conn
);
1969 # endif /* HAVE_CDSASSL */
1974 #endif /* HAVE_SSL */
1979 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
1983 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
1986 SSL_CTX
*context
; /* Context for encryption */
1987 SSL
*conn
; /* Connection for encryption */
1990 conn
= (SSL
*)(http
->tls
);
1991 context
= SSL_get_SSL_CTX(conn
);
1994 SSL_CTX_free(context
);
1997 # elif defined(HAVE_GNUTLS)
1998 http_tls_t
*conn
; /* Encryption session */
1999 gnutls_certificate_client_credentials
*credentials
;
2000 /* TLS credentials */
2003 conn
= (http_tls_t
*)(http
->tls
);
2004 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2006 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2007 gnutls_deinit(conn
->session
);
2008 gnutls_certificate_free_credentials(*credentials
);
2012 # elif defined(HAVE_CDSASSL)
2013 SSLClose((SSLContextRef
)http
->tls
);
2014 SSLDisposeContext((SSLContextRef
)http
->tls
);
2015 # endif /* HAVE_LIBSSL */
2019 #endif /* HAVE_SSL */
2024 * 'http_upgrade()' - Force upgrade to TLS encryption.
2027 static int /* O - Status of connection */
2028 http_upgrade(http_t
*http
) /* I - HTTP data */
2030 int ret
; /* Return value */
2031 http_t myhttp
; /* Local copy of HTTP data */
2034 DEBUG_printf(("http_upgrade(%p)\n", http
));
2037 * Copy the HTTP data to a local variable so we can do the OPTIONS
2038 * request without interfering with the existing request data...
2041 memcpy(&myhttp
, http
, sizeof(myhttp
));
2044 * Send an OPTIONS request to the server, requiring SSL or TLS
2045 * encryption on the link...
2048 httpClearFields(&myhttp
);
2049 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2050 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2052 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2055 * Wait for the secure connection...
2058 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2064 * Copy the HTTP data back over, if any...
2067 http
->fd
= myhttp
.fd
;
2068 http
->error
= myhttp
.error
;
2069 http
->activity
= myhttp
.activity
;
2070 http
->status
= myhttp
.status
;
2071 http
->version
= myhttp
.version
;
2072 http
->keep_alive
= myhttp
.keep_alive
;
2073 http
->used
= myhttp
.used
;
2076 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2078 http
->auth_type
= myhttp
.auth_type
;
2079 http
->nonce_count
= myhttp
.nonce_count
;
2081 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2083 http
->tls
= myhttp
.tls
;
2084 http
->encryption
= myhttp
.encryption
;
2087 * See if we actually went secure...
2093 * Server does not support HTTP upgrade...
2096 DEBUG_puts("Server does not support HTTP upgrade!");
2099 closesocket(http
->fd
);
2111 #endif /* HAVE_SSL */
2115 * 'http_wait()' - Wait for data available on a connection.
2118 static int /* O - 1 if data is available, 0 otherwise */
2119 http_wait(http_t
*http
, /* I - HTTP data */
2120 int msec
) /* I - Milliseconds to wait */
2123 struct rlimit limit
; /* Runtime limit */
2125 struct timeval timeout
; /* Timeout */
2126 int nfds
; /* Result from select() */
2127 int set_size
; /* Size of select set */
2130 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http
, msec
));
2133 * Check the SSL/TLS buffers for data first...
2140 if (SSL_pending((SSL
*)(http
->tls
)))
2142 # elif defined(HAVE_GNUTLS)
2143 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2145 # elif defined(HAVE_CDSASSL)
2146 size_t bytes
; /* Bytes that are available */
2148 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2150 # endif /* HAVE_LIBSSL */
2152 #endif /* HAVE_SSL */
2155 * Then try doing a select() to poll the socket...
2158 if (!http
->input_set
)
2162 * Windows has a fixed-size select() structure, different (surprise,
2163 * surprise!) from all UNIX implementations. Just allocate this
2164 * fixed structure...
2167 http
->input_set
= calloc(1, sizeof(fd_set
));
2170 * Allocate the select() input set based upon the max number of file
2171 * descriptors available for this process...
2174 getrlimit(RLIMIT_NOFILE
, &limit
);
2176 set_size
= (limit
.rlim_cur
+ 31) / 8 + 4;
2177 if (set_size
< sizeof(fd_set
))
2178 set_size
= sizeof(fd_set
);
2180 http
->input_set
= calloc(1, set_size
);
2183 if (!http
->input_set
)
2189 FD_SET(http
->fd
, http
->input_set
);
2193 timeout
.tv_sec
= msec
/ 1000;
2194 timeout
.tv_usec
= (msec
% 1000) * 1000;
2196 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2199 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2202 while (nfds
< 0 && WSAGetLastError() == WSAEINTR
);
2204 while (nfds
< 0 && errno
== EINTR
);
2207 FD_CLR(http
->fd
, http
->input_set
);
2214 * 'http_write()' - Write a buffer to a HTTP connection.
2217 static int /* O - Number of bytes written */
2218 http_write(http_t
*http
, /* I - HTTP data */
2219 const char *buffer
, /* I - Buffer for data */
2220 int length
) /* I - Number of bytes to write */
2222 int tbytes
, /* Total bytes sent */
2223 bytes
; /* Bytes sent */
2232 bytes
= http_write_ssl(http
, buffer
, length
);
2234 #endif /* HAVE_SSL */
2235 bytes
= send(http
->fd
, buffer
, length
, 0);
2240 if (WSAGetLastError() != http
->error
)
2242 http
->error
= WSAGetLastError();
2248 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
2250 http
->error
= errno
;
2255 DEBUG_puts("http_write: error writing data...\n");
2268 printf("http_write: wrote %d bytes: \n", tbytes
);
2269 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
2273 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
2274 printf(" %02X", buffer
[i
+ j
] & 255);
2283 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
2285 ch
= buffer
[i
+ j
] & 255;
2287 if (ch
< ' ' || ch
== 127)
2301 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
2303 * 'http_write_cdsa()' - Write function for CDSA encryption code.
2306 static OSStatus
/* O - -1 on error, 0 on success */
2308 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2309 const void *data
, /* I - Data buffer */
2310 size_t *dataLength
) /* IO - Number of bytes */
2312 ssize_t bytes
; /* Number of write written */
2315 bytes
= write((int)connection
, data
, *dataLength
);
2318 *dataLength
= bytes
;
2324 #endif /* HAVE_SSL && HAVE_CDSASSL */
2328 * 'http_write_chunk()' - Write a chunked buffer.
2331 static int /* O - Number bytes written */
2332 http_write_chunk(http_t
*http
, /* I - HTTP data */
2333 const char *buffer
, /* I - Buffer to write */
2334 int length
) /* I - Length of buffer */
2336 char header
[255]; /* Chunk header */
2337 int bytes
; /* Bytes written */
2339 DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n",
2340 http
, buffer
, length
));
2343 * Write the chunk header, data, and trailer.
2346 sprintf(header
, "%x\r\n", length
);
2347 if (http_write(http
, header
, strlen(header
)) < 0)
2349 DEBUG_puts(" http_write of length failed!");
2353 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
2355 DEBUG_puts(" http_write of buffer failed!");
2359 if (http_write(http
, "\r\n", 2) < 0)
2361 DEBUG_puts(" http_write of CR LF failed!");
2371 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2374 static int /* O - Bytes written */
2375 http_write_ssl(http_t
*http
, /* I - HTTP data */
2376 const char *buf
, /* I - Buffer holding data */
2377 int len
) /* I - Length of buffer */
2379 # if defined(HAVE_LIBSSL)
2380 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2382 # elif defined(HAVE_GNUTLS)
2383 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2384 # elif defined(HAVE_CDSASSL)
2385 OSStatus error
; /* Error info */
2386 size_t processed
; /* Number of bytes processed */
2389 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2395 http
->error
= error
;
2398 # endif /* HAVE_LIBSSL */
2400 #endif /* HAVE_SSL */