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 * _httpReadCDSA() - Read function for CDSA decryption code.
54 * httpReconnect() - Reconnect to a HTTP server...
55 * httpSetCookie() - Set the cookie value(s)...
56 * httpSetField() - Set the value of an HTTP header.
57 * httpSetLength() - Set the content-length and transfer-encoding.
58 * httpTrace() - Send an TRACE request to the server.
59 * httpUpdate() - Update the current HTTP state for incoming data.
60 * httpWait() - Wait for data available on a connection.
61 * httpWrite() - Write data to a HTTP connection.
62 * _httpWriteCDSA() - Write function for CDSA encryption code.
63 * http_field() - Return the field index for a field name.
64 * http_read_ssl() - Read from a SSL/TLS connection.
65 * http_send() - Send a request with all fields and the trailing
67 * http_setup_ssl() - Set up SSL/TLS on a connection.
68 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
69 * http_upgrade() - Force upgrade to TLS encryption.
70 * http_wait() - Wait for data available on a connection.
71 * http_write() - Write data to a connection.
72 * http_write_ssl() - Write to a SSL/TLS connection.
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
,
115 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
116 static int http_setup_ssl(http_t
*http
);
117 static void http_shutdown_ssl(http_t
*http
);
118 static int http_upgrade(http_t
*http
);
119 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
120 #endif /* HAVE_SSL */
127 static const char * const http_fields
[] =
144 "If-Unmodified-since",
160 * 'httpCheck()' - Check to see if there is a pending response from the server.
163 int /* O - 0 = no data, 1 = data available */
164 httpCheck(http_t
*http
) /* I - HTTP connection */
166 return (httpWait(http
, 0));
171 * 'httpClearCookie()' - Clear the cookie value(s).
173 * @since CUPS 1.1.19@
177 httpClearCookie(http_t
*http
) /* I - Connection */
191 * 'httpClose()' - Close an HTTP connection...
195 httpClose(http_t
*http
) /* I - Connection to close */
197 DEBUG_printf(("httpClose(http=%p)\n", http
));
202 httpAddrFreeList(http
->addrlist
);
205 free(http
->input_set
);
212 http_shutdown_ssl(http
);
213 #endif /* HAVE_SSL */
216 closesocket(http
->fd
);
226 * 'httpConnect()' - Connect to a HTTP server.
229 http_t
* /* O - New HTTP connection */
230 httpConnect(const char *host
, /* I - Host to connect to */
231 int port
) /* I - Port number */
233 http_encryption_t encryption
; /* Type of encryption to use */
237 * Set the default encryption status...
241 encryption
= HTTP_ENCRYPT_ALWAYS
;
243 encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
245 return (httpConnectEncrypt(host
, port
, encryption
));
250 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
253 http_t
* /* O - New HTTP connection */
255 const char *host
, /* I - Host to connect to */
256 int port
, /* I - Port number */
257 http_encryption_t encryption
) /* I - Type of encryption to use */
259 http_t
*http
; /* New HTTP connection */
260 http_addrlist_t
*addrlist
; /* Host address data */
261 char service
[255]; /* Service name */
264 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n",
265 host
? host
: "(null)", port
, encryption
));
276 sprintf(service
, "%d", port
);
278 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
282 * Allocate memory for the structure...
285 http
= calloc(sizeof(http_t
), 1);
289 http
->version
= HTTP_1_1
;
291 http
->activity
= time(NULL
);
295 * Set the encryption status...
298 if (port
== 443) /* Always use encryption for https */
299 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
301 http
->encryption
= encryption
;
304 * Loop through the addresses we have until one of them connects...
307 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
310 * Connect to the remote system...
313 http
->addrlist
= addrlist
;
315 if (!httpReconnect(http
))
319 * Could not connect to any known address - bail out!
322 httpAddrFreeList(addrlist
);
331 * 'httpDelete()' - Send a DELETE request to the server.
334 int /* O - Status of call (0 = success) */
335 httpDelete(http_t
*http
, /* I - HTTP data */
336 const char *uri
) /* I - URI to delete */
338 return (http_send(http
, HTTP_DELETE
, uri
));
343 * 'httpEncryption()' - Set the required encryption on the link.
346 int /* O - -1 on error, 0 on success */
347 httpEncryption(http_t
*http
, /* I - HTTP data */
348 http_encryption_t e
) /* I - New encryption preference */
350 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http
, e
));
356 http
->encryption
= e
;
358 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
359 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
360 return (httpReconnect(http
));
361 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
362 return (http_upgrade(http
));
366 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
370 #endif /* HAVE_SSL */
375 * 'httpFlush()' - Flush data from a HTTP connection.
379 httpFlush(http_t
*http
) /* I - HTTP data */
381 char buffer
[8192]; /* Junk buffer */
384 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http
, http
->state
));
386 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
391 * 'httpFlushWrite()' - Flush data in write buffer.
396 int /* O - Bytes written or -1 on error */
397 httpFlushWrite(http_t
*http
) /* I - HTTP data */
399 int bytes
; /* Bytes written */
402 DEBUG_printf(("httpFlushWrite(http=%p)\n", http
));
404 if (!http
|| !http
->wused
)
407 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
408 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
410 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
419 * 'httpGet()' - Send a GET request to the server.
422 int /* O - Status of call (0 = success) */
423 httpGet(http_t
*http
, /* I - HTTP data */
424 const char *uri
) /* I - URI to get */
426 return (http_send(http
, HTTP_GET
, uri
));
431 * 'httpGetSubField()' - Get a sub-field value.
434 char * /* O - Value or NULL */
435 httpGetSubField(http_t
*http
, /* I - HTTP data */
436 http_field_t field
, /* I - Field index */
437 const char *name
, /* I - Name of sub-field */
438 char *value
) /* O - Value string */
440 const char *fptr
; /* Pointer into field */
441 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
442 *ptr
; /* Pointer into string buffer */
445 DEBUG_printf(("httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p)\n",
446 http
, field
, name
, value
));
449 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
450 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
451 name
== NULL
|| value
== NULL
)
454 for (fptr
= http
->fields
[field
]; *fptr
;)
457 * Skip leading whitespace...
460 while (isspace(*fptr
& 255))
470 * Get the sub-field name...
474 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) && ptr
< (temp
+ sizeof(temp
) - 1);
479 DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp
));
482 * Skip trailing chars up to the '='...
485 while (isspace(*fptr
& 255))
495 * Skip = and leading whitespace...
500 while (isspace(*fptr
& 255))
506 * Read quoted string...
509 for (ptr
= value
, fptr
++;
510 *fptr
&& *fptr
!= '\"' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
515 while (*fptr
&& *fptr
!= '\"')
524 * Read unquoted string...
528 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< (value
+ HTTP_MAX_VALUE
- 1);
533 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
537 DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value
));
540 * See if this is the one...
543 if (strcmp(name
, temp
) == 0)
554 * 'httpGetLength()' - Get the amount of data remaining from the
555 * content-length or transfer-encoding fields.
557 * This function is deprecated and will not return lengths larger than
558 * 2^31 - 1; use httpGetLength2() instead.
563 int /* O - Content length */
564 httpGetLength(http_t
*http
) /* I - HTTP data */
567 * Get the read content length and return the 32-bit value.
570 httpGetLength2(http
);
572 return (http
->_data_remaining
);
577 * 'httpGetLength2()' - Get the amount of data remaining from the
578 * content-length or transfer-encoding fields.
580 * This function returns the complete content length, even for
581 * content larger than 2^31 - 1.
584 off_t
/* O - Content length */
585 httpGetLength2(http_t
*http
) /* I - HTTP data */
587 DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http
, http
->state
));
589 if (!strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
591 DEBUG_puts("httpGetLength2: chunked request!");
593 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
594 http
->data_remaining
= 0;
598 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
601 * The following is a hack for HTTP servers that don't send a
602 * content-length or transfer-encoding field...
604 * If there is no content-length then the connection must close
605 * after the transfer is complete...
608 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
609 http
->data_remaining
= 2147483647;
611 http
->data_remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
614 DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT
"\n",
615 CUPS_LLCAST http
->data_remaining
));
618 if (http
->data_remaining
<= INT_MAX
)
619 http
->_data_remaining
= (int)http
->data_remaining
;
621 http
->_data_remaining
= INT_MAX
;
623 return (http
->data_remaining
);
628 * 'httpGets()' - Get a line of text from a HTTP connection.
631 char * /* O - Line or NULL */
632 httpGets(char *line
, /* I - Line to read into */
633 int length
, /* I - Max length of buffer */
634 http_t
*http
) /* I - HTTP data */
636 char *lineptr
, /* Pointer into line */
637 *lineend
, /* End of line */
638 *bufptr
, /* Pointer into input buffer */
639 *bufend
; /* Pointer to end of buffer */
640 int bytes
, /* Number of bytes read */
641 eol
; /* End-of-line? */
644 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line
, length
, http
));
646 if (http
== NULL
|| line
== NULL
)
650 * Read a line from the buffer...
654 lineend
= line
+ length
- 1;
657 while (lineptr
< lineend
)
660 * Pre-load the buffer as needed...
669 while (http
->used
== 0)
672 * No newline; see if there is more data to be read...
675 if (!http
->blocking
&& !http_wait(http
, 1000))
680 bytes
= http_read_ssl(http
, http
->buffer
+ http
->used
,
681 HTTP_MAX_BUFFER
- http
->used
);
683 #endif /* HAVE_SSL */
684 bytes
= recv(http
->fd
, http
->buffer
+ http
->used
,
685 HTTP_MAX_BUFFER
- http
->used
, 0);
687 DEBUG_printf(("httpGets: read %d bytes...\n", bytes
));
692 * Nope, can't get a line this time...
696 if (WSAGetLastError() != http
->error
)
698 http
->error
= WSAGetLastError();
702 DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
704 DEBUG_printf(("httpGets: recv() error %d!\n", errno
));
708 else if (errno
!= http
->error
)
725 * Yup, update the amount used...
732 * Now copy as much of the current line as possible...
735 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
736 lineptr
< lineend
&& bufptr
< bufend
;)
744 else if (*bufptr
== 0x0d)
747 *lineptr
++ = *bufptr
++;
750 http
->used
-= bufptr
- http
->buffer
;
752 memmove(http
->buffer
, bufptr
, http
->used
);
760 http
->activity
= time(NULL
);
764 DEBUG_printf(("httpGets: Returning \"%s\"\n", line
));
770 DEBUG_puts("httpGets: No new line available!");
777 * 'httpHead()' - Send a HEAD request to the server.
780 int /* O - Status of call (0 = success) */
781 httpHead(http_t
*http
, /* I - HTTP data */
782 const char *uri
) /* I - URI for head */
784 return (http_send(http
, HTTP_HEAD
, uri
));
789 * 'httpInitialize()' - Initialize the HTTP interface library and set the
790 * default HTTP proxy (if any).
798 struct timeval curtime
; /* Current time in microseconds */
800 int i
; /* Looping var */
801 unsigned char data
[1024]; /* Seed data */
802 #endif /* HAVE_LIBSSL */
805 WSADATA winsockdata
; /* WinSock data */
806 static int initialized
= 0; /* Has WinSock been initialized? */
810 WSAStartup(MAKEWORD(1,1), &winsockdata
);
811 #elif defined(HAVE_SIGSET)
812 sigset(SIGPIPE
, SIG_IGN
);
813 #elif defined(HAVE_SIGACTION)
814 struct sigaction action
; /* POSIX sigaction data */
818 * Ignore SIGPIPE signals...
821 memset(&action
, 0, sizeof(action
));
822 action
.sa_handler
= SIG_IGN
;
823 sigaction(SIGPIPE
, &action
, NULL
);
825 signal(SIGPIPE
, SIG_IGN
);
829 gnutls_global_init();
830 #endif /* HAVE_GNUTLS */
833 SSL_load_error_strings();
837 * Using the current time is a dubious random seed, but on some systems
838 * it is the best we can do (on others, this seed isn't even used...)
843 gettimeofday(&curtime
, NULL
);
844 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
847 for (i
= 0; i
< sizeof(data
); i
++)
848 data
[i
] = rand(); /* Yes, this is a poor source of random data... */
850 RAND_seed(&data
, sizeof(data
));
851 #endif /* HAVE_LIBSSL */
856 * 'httpOptions()' - Send an OPTIONS request to the server.
859 int /* O - Status of call (0 = success) */
860 httpOptions(http_t
*http
, /* I - HTTP data */
861 const char *uri
) /* I - URI for options */
863 return (http_send(http
, HTTP_OPTIONS
, uri
));
868 * 'httpPost()' - Send a POST request to the server.
871 int /* O - Status of call (0 = success) */
872 httpPost(http_t
*http
, /* I - HTTP data */
873 const char *uri
) /* I - URI for post */
875 return (http_send(http
, HTTP_POST
, uri
));
880 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
883 int /* O - Number of bytes written */
884 httpPrintf(http_t
*http
, /* I - HTTP data */
885 const char *format
, /* I - printf-style format string */
886 ...) /* I - Additional args as needed */
888 int bytes
; /* Number of bytes to write */
889 char buf
[16384]; /* Buffer for formatted string */
890 va_list ap
; /* Variable argument pointer */
893 DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http
, format
));
895 va_start(ap
, format
);
896 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
899 DEBUG_printf(("httpPrintf: %s", buf
));
903 DEBUG_puts(" flushing existing data...");
905 if (httpFlushWrite(http
) < 0)
909 return (http_write(http
, buf
, bytes
));
914 * 'httpPut()' - Send a PUT request to the server.
917 int /* O - Status of call (0 = success) */
918 httpPut(http_t
*http
, /* I - HTTP data */
919 const char *uri
) /* I - URI to put */
921 return (http_send(http
, HTTP_PUT
, uri
));
926 * 'httpRead()' - Read data from a HTTP connection.
929 int /* O - Number of bytes read */
930 httpRead(http_t
*http
, /* I - HTTP data */
931 char *buffer
, /* I - Buffer for data */
932 int length
) /* I - Maximum number of bytes */
934 int bytes
; /* Bytes read */
935 char len
[32]; /* Length string */
938 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
939 http
, buffer
, length
));
941 if (http
== NULL
|| buffer
== NULL
)
944 http
->activity
= time(NULL
);
949 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
950 http
->data_remaining
<= 0)
952 DEBUG_puts("httpRead: Getting chunk length...");
954 if (httpGets(len
, sizeof(len
), http
) == NULL
)
956 DEBUG_puts("httpRead: Could not get length!");
960 http
->data_remaining
= strtoll(len
, NULL
, 16);
961 if (http
->data_remaining
< 0)
963 DEBUG_puts("httpRead: Negative chunk length!");
968 DEBUG_printf(("httpRead: data_remaining=" CUPS_LLFMT
"\n",
969 CUPS_LLCAST http
->data_remaining
));
971 if (http
->data_remaining
<= 0)
974 * A zero-length chunk ends a transfer; unless we are reading POST
978 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
979 httpGets(len
, sizeof(len
), http
);
981 if (http
->state
== HTTP_POST_RECV
)
984 http
->state
= HTTP_WAITING
;
987 * Prevent future reads for this request...
990 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
994 else if (length
> http
->data_remaining
)
995 length
= http
->data_remaining
;
997 if (http
->used
== 0 && length
<= 256)
1000 * Buffer small reads for better performance...
1003 if (!http
->blocking
&& !httpWait(http
, 1000))
1006 if (http
->data_remaining
> sizeof(http
->buffer
))
1007 bytes
= sizeof(http
->buffer
);
1009 bytes
= http
->data_remaining
;
1013 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
1015 #endif /* HAVE_SSL */
1017 DEBUG_printf(("httpRead: reading %d bytes from socket into buffer...\n",
1020 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
1022 DEBUG_printf(("httpRead: read %d bytes from socket into buffer...\n",
1031 http
->error
= WSAGetLastError();
1036 http
->error
= errno
;
1043 http
->error
= EPIPE
;
1050 if (length
> http
->used
)
1051 length
= http
->used
;
1055 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
1057 memcpy(buffer
, http
->buffer
, length
);
1058 http
->used
-= length
;
1061 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1066 if (!http
->blocking
&& !httpWait(http
, 1000))
1069 bytes
= http_read_ssl(http
, buffer
, length
);
1071 #endif /* HAVE_SSL */
1074 if (!http
->blocking
&& !httpWait(http
, 1000))
1077 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
1079 while ((bytes
= recv(http
->fd
, buffer
, length
, 0)) < 0)
1083 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
1088 http
->data_remaining
-= bytes
;
1090 if (http
->data_remaining
<= INT_MAX
)
1091 http
->_data_remaining
= (int)http
->data_remaining
;
1093 http
->_data_remaining
= INT_MAX
;
1098 http
->error
= WSAGetLastError();
1103 http
->error
= errno
;
1108 http
->error
= EPIPE
;
1112 if (http
->data_remaining
== 0)
1114 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1115 httpGets(len
, sizeof(len
), http
);
1117 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1119 if (http
->state
== HTTP_POST_RECV
)
1122 http
->state
= HTTP_WAITING
;
1129 printf("httpRead: Read %d bytes:\n", bytes
);
1130 for (i
= 0; i
< bytes
; i
+= 16)
1134 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1135 printf(" %02X", buffer
[i
+ j
] & 255);
1144 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
1146 ch
= buffer
[i
+ j
] & 255;
1148 if (ch
< ' ' || ch
== 127)
1162 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1164 * '_httpReadCDSA()' - Read function for CDSA decryption code.
1167 OSStatus
/* O - -1 on error, 0 on success */
1169 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1170 void *data
, /* I - Data buffer */
1171 size_t *dataLength
) /* IO - Number of bytes */
1173 ssize_t bytes
; /* Number of bytes read */
1176 bytes
= recv((int)connection
, data
, *dataLength
, 0);
1179 *dataLength
= bytes
;
1185 #endif /* HAVE_SSL && HAVE_CDSASSL */
1189 * 'httpReconnect()' - Reconnect to a HTTP server...
1192 int /* O - 0 on success, non-zero on failure */
1193 httpReconnect(http_t
*http
) /* I - HTTP data */
1195 http_addrlist_t
*addr
; /* Connected address */
1198 DEBUG_printf(("httpReconnect(http=%p)\n", http
));
1205 http_shutdown_ssl(http
);
1206 #endif /* HAVE_SSL */
1209 * Close any previously open socket...
1214 closesocket(http
->fd
);
1220 * Connect to the server...
1223 if ((addr
= httpAddrConnect(http
->addrlist
, &(http
->fd
))) == NULL
)
1226 * Unable to connect...
1230 http
->error
= WSAGetLastError();
1232 http
->error
= errno
;
1234 http
->status
= HTTP_ERROR
;
1239 http
->hostaddr
= &(addr
->addr
);
1241 http
->status
= HTTP_CONTINUE
;
1244 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
1247 * Always do encryption via SSL.
1250 if (http_setup_ssl(http
) != 0)
1253 closesocket(http
->fd
);
1261 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
1262 return (http_upgrade(http
));
1263 #endif /* HAVE_SSL */
1270 * 'httpSetCookie()' - Set the cookie value(s)...
1272 * @since CUPS 1.1.19@
1276 httpSetCookie(http_t
*http
, /* I - Connection */
1277 const char *cookie
) /* I - Cookie string */
1286 http
->cookie
= strdup(cookie
);
1288 http
->cookie
= NULL
;
1293 * 'httpSetField()' - Set the value of an HTTP header.
1297 httpSetField(http_t
*http
, /* I - HTTP data */
1298 http_field_t field
, /* I - Field index */
1299 const char *value
) /* I - Value */
1302 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
1303 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
1307 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
1312 * 'httpSetLength()' - Set the content-length and content-encoding.
1318 httpSetLength(http_t
*http
, /* I - HTTP data */
1319 off_t length
) /* I - Length (0 for chunked) */
1321 if (!http
|| length
< 0)
1326 strcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked");
1327 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
1331 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
1332 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
1333 CUPS_LLFMT
, CUPS_LLCAST length
);
1339 * 'httpTrace()' - Send an TRACE request to the server.
1342 int /* O - Status of call (0 = success) */
1343 httpTrace(http_t
*http
, /* I - HTTP data */
1344 const char *uri
) /* I - URI for trace */
1346 return (http_send(http
, HTTP_TRACE
, uri
));
1351 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1354 http_status_t
/* O - HTTP status */
1355 httpUpdate(http_t
*http
) /* I - HTTP data */
1357 char line
[32768], /* Line from connection... */
1358 *value
; /* Pointer to value on line */
1359 http_field_t field
; /* Field index */
1360 int major
, minor
, /* HTTP version numbers */
1361 status
; /* Request status */
1364 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http
, http
->state
));
1367 * Flush pending data, if any...
1372 DEBUG_puts(" flushing buffer...");
1374 if (httpFlushWrite(http
) < 0)
1375 return (HTTP_ERROR
);
1379 * If we haven't issued any commands, then there is nothing to "update"...
1382 if (http
->state
== HTTP_WAITING
)
1383 return (HTTP_CONTINUE
);
1386 * Grab all of the lines we can from the connection...
1389 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1391 DEBUG_printf(("httpUpdate: Got \"%s\"\n", line
));
1393 if (line
[0] == '\0')
1396 * Blank line means the start of the data section (if any). Return
1397 * the result code, too...
1399 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1400 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1404 if (http
->status
== HTTP_CONTINUE
)
1405 return (http
->status
);
1407 if (http
->status
< HTTP_BAD_REQUEST
)
1408 http
->digest_tries
= 0;
1411 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
1413 if (http_setup_ssl(http
) != 0)
1416 closesocket(http
->fd
);
1421 return (HTTP_ERROR
);
1424 return (HTTP_CONTINUE
);
1426 #endif /* HAVE_SSL */
1428 httpGetLength2(http
);
1430 switch (http
->state
)
1434 case HTTP_POST_RECV
:
1437 case HTTP_POST_SEND
:
1441 http
->state
= HTTP_WAITING
;
1445 return (http
->status
);
1447 else if (strncmp(line
, "HTTP/", 5) == 0)
1450 * Got the beginning of a response...
1453 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1454 return (HTTP_ERROR
);
1456 http
->version
= (http_version_t
)(major
* 100 + minor
);
1457 http
->status
= (http_status_t
)status
;
1459 else if ((value
= strchr(line
, ':')) != NULL
)
1466 while (isspace(*value
& 255))
1470 * Be tolerants of servers that send unknown attribute fields...
1473 if (!strcasecmp(line
, "expect"))
1476 * "Expect: 100-continue" or similar...
1479 http
->expect
= (http_status_t
)atoi(value
);
1481 else if (!strcasecmp(line
, "cookie"))
1484 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
1487 httpSetCookie(http
, value
);
1489 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1491 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1495 httpSetField(http
, field
, value
);
1499 http
->status
= HTTP_ERROR
;
1500 return (HTTP_ERROR
);
1505 * See if there was an error...
1508 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
1509 return (http
->status
);
1513 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http
->error
,
1514 strerror(http
->error
)));
1515 http
->status
= HTTP_ERROR
;
1516 return (HTTP_ERROR
);
1520 * If we haven't already returned, then there is nothing new...
1523 return (HTTP_CONTINUE
);
1528 * 'httpWait()' - Wait for data available on a connection.
1530 * @since CUPS 1.1.19@
1533 int /* O - 1 if data is available, 0 otherwise */
1534 httpWait(http_t
*http
, /* I - HTTP data */
1535 int msec
) /* I - Milliseconds to wait */
1538 * First see if there is data in the buffer...
1548 * If not, check the SSL/TLS buffers and do a select() on the connection...
1551 return (http_wait(http
, msec
));
1556 * 'httpWrite()' - Write data to a HTTP connection.
1559 int /* O - Number of bytes written */
1560 httpWrite(http_t
*http
, /* I - HTTP data */
1561 const char *buffer
, /* I - Buffer for data */
1562 int length
) /* I - Number of bytes to write */
1564 int bytes
; /* Bytes written */
1567 DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http
,
1571 * Range check input...
1574 if (http
== NULL
|| buffer
== NULL
)
1578 * Mark activity on the connection...
1581 http
->activity
= time(NULL
);
1584 * Buffer small writes for better performance...
1589 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
1591 DEBUG_printf((" flushing buffer (wused=%d, length=%d)\n",
1592 http
->wused
, length
));
1594 httpFlushWrite(http
);
1597 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
))
1600 * Write to buffer...
1603 DEBUG_printf((" copying %d bytes to wbuffer...\n", length
));
1605 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
1606 http
->wused
+= length
;
1612 * Otherwise write the data directly...
1615 DEBUG_printf((" writing %d bytes to socket...\n", length
));
1617 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1618 bytes
= http_write_chunk(http
, buffer
, length
);
1620 bytes
= http_write(http
, buffer
, length
);
1622 DEBUG_printf((" wrote %d bytes...\n", bytes
));
1625 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
1626 http
->data_remaining
-= bytes
;
1630 * Handle end-of-request processing...
1633 if ((http
->data_encoding
== HTTP_ENCODE_CHUNKED
&& length
== 0) ||
1634 (http
->data_encoding
== HTTP_ENCODE_LENGTH
&& http
->data_remaining
== 0))
1637 * Finished with the transfer; unless we are sending POST or PUT
1641 DEBUG_puts("httpWrite: changing states...");
1644 httpFlushWrite(http
);
1646 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1649 * Send a 0-length chunk at the end of the request...
1652 http_write(http
, "0\r\n\r\n", 5);
1655 * Reset the data state...
1658 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1659 http
->data_remaining
= 0;
1662 if (http
->state
== HTTP_POST_RECV
)
1664 else if (http
->state
== HTTP_PUT_RECV
)
1665 http
->state
= HTTP_STATUS
;
1667 http
->state
= HTTP_WAITING
;
1674 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1676 * '_httpWriteCDSA()' - Write function for CDSA encryption code.
1679 OSStatus
/* O - -1 on error, 0 on success */
1681 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1682 const void *data
, /* I - Data buffer */
1683 size_t *dataLength
) /* IO - Number of bytes */
1685 ssize_t bytes
; /* Number of write written */
1688 bytes
= write((int)connection
, data
, *dataLength
);
1691 *dataLength
= bytes
;
1697 #endif /* HAVE_SSL && HAVE_CDSASSL */
1701 * 'http_field()' - Return the field index for a field name.
1704 static http_field_t
/* O - Field index */
1705 http_field(const char *name
) /* I - String name */
1707 int i
; /* Looping var */
1710 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1711 if (strcasecmp(name
, http_fields
[i
]) == 0)
1712 return ((http_field_t
)i
);
1714 return (HTTP_FIELD_UNKNOWN
);
1720 * 'http_read_ssl()' - Read from a SSL/TLS connection.
1723 static int /* O - Bytes read */
1724 http_read_ssl(http_t
*http
, /* I - HTTP data */
1725 char *buf
, /* I - Buffer to store data */
1726 int len
) /* I - Length of buffer */
1728 # if defined(HAVE_LIBSSL)
1729 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
1731 # elif defined(HAVE_GNUTLS)
1732 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
1734 # elif defined(HAVE_CDSASSL)
1735 OSStatus error
; /* Error info */
1736 size_t processed
; /* Number of bytes processed */
1739 error
= SSLRead((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
1745 http
->error
= error
;
1749 # endif /* HAVE_LIBSSL */
1751 #endif /* HAVE_SSL */
1755 * 'http_send()' - Send a request with all fields and the trailing blank line.
1758 static int /* O - 0 on success, non-zero on error */
1759 http_send(http_t
*http
, /* I - HTTP data */
1760 http_state_t request
, /* I - Request code */
1761 const char *uri
) /* I - URI */
1763 int i
; /* Looping var */
1764 char *ptr
, /* Pointer in buffer */
1765 buf
[1024]; /* Encoded URI buffer */
1766 static const char * const codes
[] =
1767 { /* Request code strings */
1782 static const char hex
[] = "0123456789ABCDEF";
1786 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
1787 http
, codes
[request
], uri
));
1789 if (http
== NULL
|| uri
== NULL
)
1793 * Encode the URI as needed...
1796 for (ptr
= buf
; *uri
!= '\0' && ptr
< (buf
+ sizeof(buf
) - 1); uri
++)
1797 if (*uri
<= ' ' || *uri
>= 127)
1799 if (ptr
< (buf
+ sizeof(buf
) - 1))
1801 if (ptr
< (buf
+ sizeof(buf
) - 1))
1802 *ptr
++ = hex
[(*uri
>> 4) & 15];
1803 if (ptr
< (buf
+ sizeof(buf
) - 1))
1804 *ptr
++ = hex
[*uri
& 15];
1812 * See if we had an error the last time around; if so, reconnect...
1815 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1816 httpReconnect(http
);
1819 * Send the request header...
1822 http
->state
= request
;
1823 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1826 http
->status
= HTTP_CONTINUE
;
1829 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
1831 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
1832 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
1834 #endif /* HAVE_SSL */
1836 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1838 http
->status
= HTTP_ERROR
;
1842 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1843 if (http
->fields
[i
][0] != '\0')
1845 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1847 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1849 http
->status
= HTTP_ERROR
;
1854 if (httpPrintf(http
, "\r\n") < 1)
1856 http
->status
= HTTP_ERROR
;
1860 httpGetLength2(http
);
1861 httpClearFields(http
);
1869 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
1872 static int /* O - Status of connection */
1873 http_setup_ssl(http_t
*http
) /* I - HTTP data */
1876 SSL_CTX
*context
; /* Context for encryption */
1877 SSL
*conn
; /* Connection for encryption */
1878 # elif defined(HAVE_GNUTLS)
1879 http_tls_t
*conn
; /* TLS session object */
1880 gnutls_certificate_client_credentials
*credentials
;
1881 /* TLS credentials */
1882 # elif defined(HAVE_CDSASSL)
1883 SSLContextRef conn
; /* Context for encryption */
1884 OSStatus error
; /* Error info */
1885 # endif /* HAVE_LIBSSL */
1888 DEBUG_printf(("http_setup_ssl(http=%p)\n", http
));
1891 context
= SSL_CTX_new(SSLv23_client_method());
1893 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
1895 conn
= SSL_new(context
);
1897 SSL_set_fd(conn
, http
->fd
);
1898 if (SSL_connect(conn
) != 1)
1901 unsigned long error
; /* Error code */
1903 while ((error
= ERR_get_error()) != 0)
1904 printf("http_setup_ssl: %s\n", ERR_error_string(error
, NULL
));
1907 SSL_CTX_free(context
);
1911 http
->error
= WSAGetLastError();
1913 http
->error
= errno
;
1915 http
->status
= HTTP_ERROR
;
1917 return (HTTP_ERROR
);
1920 # elif defined(HAVE_GNUTLS)
1921 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
1925 http
->error
= errno
;
1926 http
->status
= HTTP_ERROR
;
1931 credentials
= (gnutls_certificate_client_credentials
*)
1932 malloc(sizeof(gnutls_certificate_client_credentials
));
1933 if (credentials
== NULL
)
1937 http
->error
= errno
;
1938 http
->status
= HTTP_ERROR
;
1943 gnutls_certificate_allocate_credentials(credentials
);
1945 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
1946 gnutls_set_default_priority(conn
->session
);
1947 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
1948 gnutls_transport_set_ptr(conn
->session
, http
->fd
);
1950 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
1952 http
->error
= errno
;
1953 http
->status
= HTTP_ERROR
;
1958 conn
->credentials
= credentials
;
1960 # elif defined(HAVE_CDSASSL)
1961 error
= SSLNewContext(false, &conn
);
1964 error
= SSLSetIOFuncs(conn
, _httpReadCDSA
, _httpWriteCDSA
);
1967 error
= SSLSetConnection(conn
, (SSLConnectionRef
)http
->fd
);
1970 error
= SSLSetAllowsExpiredCerts(conn
, true);
1973 error
= SSLSetAllowsAnyRoot(conn
, true);
1976 error
= SSLHandshake(conn
);
1980 http
->error
= error
;
1981 http
->status
= HTTP_ERROR
;
1983 SSLDisposeContext(conn
);
1989 # endif /* HAVE_CDSASSL */
1994 #endif /* HAVE_SSL */
1999 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
2003 http_shutdown_ssl(http_t
*http
) /* I - HTTP data */
2006 SSL_CTX
*context
; /* Context for encryption */
2007 SSL
*conn
; /* Connection for encryption */
2010 conn
= (SSL
*)(http
->tls
);
2011 context
= SSL_get_SSL_CTX(conn
);
2014 SSL_CTX_free(context
);
2017 # elif defined(HAVE_GNUTLS)
2018 http_tls_t
*conn
; /* Encryption session */
2019 gnutls_certificate_client_credentials
*credentials
;
2020 /* TLS credentials */
2023 conn
= (http_tls_t
*)(http
->tls
);
2024 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
2026 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
2027 gnutls_deinit(conn
->session
);
2028 gnutls_certificate_free_credentials(*credentials
);
2032 # elif defined(HAVE_CDSASSL)
2033 SSLClose((SSLContextRef
)http
->tls
);
2034 SSLDisposeContext((SSLContextRef
)http
->tls
);
2035 # endif /* HAVE_LIBSSL */
2039 #endif /* HAVE_SSL */
2044 * 'http_upgrade()' - Force upgrade to TLS encryption.
2047 static int /* O - Status of connection */
2048 http_upgrade(http_t
*http
) /* I - HTTP data */
2050 int ret
; /* Return value */
2051 http_t myhttp
; /* Local copy of HTTP data */
2054 DEBUG_printf(("http_upgrade(%p)\n", http
));
2057 * Copy the HTTP data to a local variable so we can do the OPTIONS
2058 * request without interfering with the existing request data...
2061 memcpy(&myhttp
, http
, sizeof(myhttp
));
2064 * Send an OPTIONS request to the server, requiring SSL or TLS
2065 * encryption on the link...
2068 httpClearFields(&myhttp
);
2069 httpSetField(&myhttp
, HTTP_FIELD_CONNECTION
, "upgrade");
2070 httpSetField(&myhttp
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
2072 if ((ret
= httpOptions(&myhttp
, "*")) == 0)
2075 * Wait for the secure connection...
2078 while (httpUpdate(&myhttp
) == HTTP_CONTINUE
);
2084 * Copy the HTTP data back over, if any...
2087 http
->fd
= myhttp
.fd
;
2088 http
->error
= myhttp
.error
;
2089 http
->activity
= myhttp
.activity
;
2090 http
->status
= myhttp
.status
;
2091 http
->version
= myhttp
.version
;
2092 http
->keep_alive
= myhttp
.keep_alive
;
2093 http
->used
= myhttp
.used
;
2096 memcpy(http
->buffer
, myhttp
.buffer
, http
->used
);
2098 http
->auth_type
= myhttp
.auth_type
;
2099 http
->nonce_count
= myhttp
.nonce_count
;
2101 memcpy(http
->nonce
, myhttp
.nonce
, sizeof(http
->nonce
));
2103 http
->tls
= myhttp
.tls
;
2104 http
->encryption
= myhttp
.encryption
;
2107 * See if we actually went secure...
2113 * Server does not support HTTP upgrade...
2116 DEBUG_puts("Server does not support HTTP upgrade!");
2119 closesocket(http
->fd
);
2131 #endif /* HAVE_SSL */
2135 * 'http_wait()' - Wait for data available on a connection.
2138 static int /* O - 1 if data is available, 0 otherwise */
2139 http_wait(http_t
*http
, /* I - HTTP data */
2140 int msec
) /* I - Milliseconds to wait */
2143 struct rlimit limit
; /* Runtime limit */
2145 struct timeval timeout
; /* Timeout */
2146 int nfds
; /* Result from select() */
2147 int set_size
; /* Size of select set */
2150 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http
, msec
));
2153 * Check the SSL/TLS buffers for data first...
2160 if (SSL_pending((SSL
*)(http
->tls
)))
2162 # elif defined(HAVE_GNUTLS)
2163 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2165 # elif defined(HAVE_CDSASSL)
2166 size_t bytes
; /* Bytes that are available */
2168 if (!SSLGetBufferedReadSize((SSLContextRef
)http
->tls
, &bytes
) && bytes
> 0)
2170 # endif /* HAVE_LIBSSL */
2172 #endif /* HAVE_SSL */
2175 * Then try doing a select() to poll the socket...
2178 if (!http
->input_set
)
2182 * Windows has a fixed-size select() structure, different (surprise,
2183 * surprise!) from all UNIX implementations. Just allocate this
2184 * fixed structure...
2187 http
->input_set
= calloc(1, sizeof(fd_set
));
2190 * Allocate the select() input set based upon the max number of file
2191 * descriptors available for this process...
2194 getrlimit(RLIMIT_NOFILE
, &limit
);
2196 set_size
= (limit
.rlim_cur
+ 31) / 8 + 4;
2197 if (set_size
< sizeof(fd_set
))
2198 set_size
= sizeof(fd_set
);
2200 http
->input_set
= calloc(1, set_size
);
2203 if (!http
->input_set
)
2209 FD_SET(http
->fd
, http
->input_set
);
2213 timeout
.tv_sec
= msec
/ 1000;
2214 timeout
.tv_usec
= (msec
% 1000) * 1000;
2216 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, &timeout
);
2219 nfds
= select(http
->fd
+ 1, http
->input_set
, NULL
, NULL
, NULL
);
2222 while (nfds
< 0 && WSAGetLastError() == WSAEINTR
);
2224 while (nfds
< 0 && errno
== EINTR
);
2227 FD_CLR(http
->fd
, http
->input_set
);
2234 * 'http_write()' - Write a buffer to a HTTP connection.
2237 static int /* O - Number of bytes written */
2238 http_write(http_t
*http
, /* I - HTTP data */
2239 const char *buffer
, /* I - Buffer for data */
2240 int length
) /* I - Number of bytes to write */
2242 int tbytes
, /* Total bytes sent */
2243 bytes
; /* Bytes sent */
2252 bytes
= http_write_ssl(http
, buffer
, length
);
2254 #endif /* HAVE_SSL */
2255 bytes
= send(http
->fd
, buffer
, length
, 0);
2260 if (WSAGetLastError() != http
->error
)
2262 http
->error
= WSAGetLastError();
2268 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
2270 http
->error
= errno
;
2275 DEBUG_puts("http_write: error writing data...\n");
2288 printf("http_write: wrote %d bytes: \n", tbytes
);
2289 for (i
= 0, buffer
-= tbytes
; i
< tbytes
; i
+= 16)
2293 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
2294 printf(" %02X", buffer
[i
+ j
] & 255);
2303 for (j
= 0; j
< 16 && (i
+ j
) < tbytes
; j
++)
2305 ch
= buffer
[i
+ j
] & 255;
2307 if (ch
< ' ' || ch
== 127)
2322 * 'http_write_chunk()' - Write a chunked buffer.
2325 static int /* O - Number bytes written */
2326 http_write_chunk(http_t
*http
, /* I - HTTP data */
2327 const char *buffer
, /* I - Buffer to write */
2328 int length
) /* I - Length of buffer */
2330 char header
[255]; /* Chunk header */
2331 int bytes
; /* Bytes written */
2333 DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n",
2334 http
, buffer
, length
));
2337 * Write the chunk header, data, and trailer.
2340 sprintf(header
, "%x\r\n", length
);
2341 if (http_write(http
, header
, strlen(header
)) < 0)
2343 DEBUG_puts(" http_write of length failed!");
2347 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
2349 DEBUG_puts(" http_write of buffer failed!");
2353 if (http_write(http
, "\r\n", 2) < 0)
2355 DEBUG_puts(" http_write of CR LF failed!");
2365 * 'http_write_ssl()' - Write to a SSL/TLS connection.
2368 static int /* O - Bytes written */
2369 http_write_ssl(http_t
*http
, /* I - HTTP data */
2370 const char *buf
, /* I - Buffer holding data */
2371 int len
) /* I - Length of buffer */
2373 # if defined(HAVE_LIBSSL)
2374 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
2376 # elif defined(HAVE_GNUTLS)
2377 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2378 # elif defined(HAVE_CDSASSL)
2379 OSStatus error
; /* Error info */
2380 size_t processed
; /* Number of bytes processed */
2383 error
= SSLWrite((SSLContextRef
)http
->tls
, buf
, len
, &processed
);
2389 http
->error
= error
;
2392 # endif /* HAVE_LIBSSL */
2394 #endif /* HAVE_SSL */