2 * "$Id: http.c 7850 2008-08-20 00:07:25Z mike $"
4 * HTTP routines for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * _httpBIOMethods() - Get the OpenSSL BIO methods for HTTP connections.
23 * httpBlocking() - Set blocking/non-blocking behavior on a connection.
24 * httpCheck() - Check to see if there is a pending response from the
26 * httpClearCookie() - Clear the cookie value(s).
27 * httpClearFields() - Clear HTTP request fields.
28 * httpClose() - Close an HTTP connection...
29 * httpConnectEncrypt() - Connect to a HTTP server using encryption.
30 * _httpCreate() - Create an unconnected HTTP connection.
31 * httpDelete() - Send a DELETE request to the server.
32 * httpEncryption() - Set the required encryption on the link.
33 * httpError() - Get the last error on a connection.
34 * httpFlush() - Flush data from a HTTP connection.
35 * httpFlushWrite() - Flush data in write buffer.
36 * httpGet() - Send a GET request to the server.
37 * httpGetAuthString() - Get the current authorization string.
38 * httpGetBlocking() - Get the blocking/non-block state of a connection.
39 * httpGetCookie() - Get any cookie data from the response.
40 * httpGetFd() - Get the file descriptor associated with a
42 * httpGetField() - Get a field value from a request/response.
43 * httpGetLength() - Get the amount of data remaining from the
44 * content-length or transfer-encoding fields.
45 * httpGetLength2() - Get the amount of data remaining from the
46 * content-length or transfer-encoding fields.
47 * httpGetStatus() - Get the status of the last HTTP request.
48 * httpGetSubField() - Get a sub-field value.
49 * httpGetSubField2() - Get a sub-field value.
50 * httpGets() - Get a line of text from a HTTP connection.
51 * httpHead() - Send a HEAD request to the server.
52 * httpInitialize() - Initialize the HTTP interface library and set the
53 * default HTTP proxy (if any).
54 * httpOptions() - Send an OPTIONS request to the server.
55 * httpPost() - Send a POST request to the server.
56 * httpPrintf() - Print a formatted string to a HTTP connection.
57 * httpPut() - Send a PUT request to the server.
58 * httpRead() - Read data from a HTTP connection.
59 * httpRead2() - Read data from a HTTP connection.
60 * _httpReadCDSA() - Read function for the CDSA library.
61 * _httpReadGNUTLS() - Read function for the GNU TLS library.
62 * httpReconnect() - Reconnect to a HTTP server.
63 * httpSetAuthString() - Set the current authorization string.
64 * httpSetCookie() - Set the cookie value(s)...
65 * httpSetExpect() - Set the Expect: header in a request.
66 * httpSetField() - Set the value of an HTTP header.
67 * httpSetLength() - Set the content-length and content-encoding.
68 * httpTrace() - Send an TRACE request to the server.
69 * httpUpdate() - Update the current HTTP state for incoming data.
70 * _httpWait() - Wait for data available on a connection (no flush).
71 * httpWait() - Wait for data available on a connection.
72 * httpWrite() - Write data to a HTTP connection.
73 * httpWrite2() - Write data to a HTTP connection.
74 * _httpWriteCDSA() - Write function for the CDSA library.
75 * _httpWriteGNUTLS() - Write function for the GNU TLS library.
76 * http_bio_ctrl() - Control the HTTP connection.
77 * http_bio_free() - Free OpenSSL data.
78 * http_bio_new() - Initialize an OpenSSL BIO structure.
79 * http_bio_puts() - Send a string for OpenSSL.
80 * http_bio_read() - Read data for OpenSSL.
81 * http_bio_write() - Write data for OpenSSL.
82 * http_debug_hex() - Do a hex dump of a buffer.
83 * http_field() - Return the field index for a field name.
84 * http_read_ssl() - Read from a SSL/TLS connection.
85 * http_send() - Send a request with all fields and the trailing
87 * http_setup_ssl() - Set up SSL/TLS support on a connection.
88 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
89 * http_upgrade() - Force upgrade to TLS encryption.
90 * http_write() - Write a buffer to a HTTP connection.
91 * http_write_chunk() - Write a chunked buffer.
92 * http_write_ssl() - Write to a SSL/TLS connection.
96 * Include necessary headers...
99 #include "cups-private.h"
103 # include <sys/time.h>
104 # include <sys/resource.h>
107 # include <sys/poll.h>
108 #endif /* HAVE_POLL */
112 * Some operating systems have done away with the Fxxxx constants for
113 * the fcntl() call; this works around that "feature"...
117 # define FNONBLK O_NONBLOCK
118 #endif /* !FNONBLK */
126 static void http_debug_hex(const char *prefix
, const char *buffer
,
129 static http_field_t
http_field(const char *name
);
130 static int http_send(http_t
*http
, http_state_t request
,
132 static int http_write(http_t
*http
, const char *buffer
,
134 static int http_write_chunk(http_t
*http
, const char *buffer
,
137 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
138 static int http_setup_ssl(http_t
*http
);
139 static void http_shutdown_ssl(http_t
*http
);
140 static int http_upgrade(http_t
*http
);
141 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
142 #endif /* HAVE_SSL */
149 static const char * const http_fields
[] =
166 "If-Unmodified-since",
180 static const char * const http_states
[] =
200 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
202 * BIO methods for OpenSSL...
205 static int http_bio_write(BIO
*h
, const char *buf
, int num
);
206 static int http_bio_read(BIO
*h
, char *buf
, int size
);
207 static int http_bio_puts(BIO
*h
, const char *str
);
208 static long http_bio_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
209 static int http_bio_new(BIO
*h
);
210 static int http_bio_free(BIO
*data
);
212 static BIO_METHOD http_bio_methods
=
219 NULL
, /* http_bio_gets, */
228 * '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections.
231 BIO_METHOD
* /* O - BIO methods for OpenSSL */
232 _httpBIOMethods(void)
234 return (&http_bio_methods
);
236 #endif /* HAVE_SSL && HAVE_LIBSSL */
240 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
244 httpBlocking(http_t
*http
, /* I - Connection to server */
245 int b
) /* I - 1 = blocking, 0 = non-blocking */
253 * 'httpCheck()' - Check to see if there is a pending response from the server.
256 int /* O - 0 = no data, 1 = data available */
257 httpCheck(http_t
*http
) /* I - Connection to server */
259 return (httpWait(http
, 0));
264 * 'httpClearCookie()' - Clear the cookie value(s).
266 * @since CUPS 1.1.19/Mac OS X 10.3@
270 httpClearCookie(http_t
*http
) /* I - Connection to server */
284 * 'httpClearFields()' - Clear HTTP request fields.
288 httpClearFields(http_t
*http
) /* I - Connection to server */
292 memset(http
->fields
, 0, sizeof(http
->fields
));
293 if (http
->hostname
[0] == '/')
294 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
296 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
298 if (http
->field_authorization
)
300 free(http
->field_authorization
);
301 http
->field_authorization
= NULL
;
304 http
->expect
= (http_status_t
)0;
310 * 'httpClose()' - Close an HTTP connection...
314 httpClose(http_t
*http
) /* I - Connection to server */
317 OM_uint32 minor_status
; /* Minor status code */
318 #endif /* HAVE_GSSAPI */
321 DEBUG_printf(("httpClose(http=%p)", http
));
326 httpAddrFreeList(http
->addrlist
);
333 http_shutdown_ssl(http
);
334 #endif /* HAVE_SSL */
337 closesocket(http
->fd
);
343 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
344 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
346 if (http
->gssname
!= GSS_C_NO_NAME
)
347 gss_release_name(&minor_status
, &http
->gssname
);
348 #endif /* HAVE_GSSAPI */
350 #ifdef HAVE_AUTHORIZATION_H
352 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
353 #endif /* HAVE_AUTHORIZATION_H */
355 httpClearFields(http
);
357 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
358 free(http
->authstring
);
365 * 'httpConnect()' - Connect to a HTTP server.
367 * This function is deprecated - use @link httpConnectEncrypt@ instead.
372 http_t
* /* O - New HTTP connection */
373 httpConnect(const char *host
, /* I - Host to connect to */
374 int port
) /* I - Port number */
376 return (httpConnectEncrypt(host
, port
, HTTP_ENCRYPT_IF_REQUESTED
));
381 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
384 http_t
* /* O - New HTTP connection */
386 const char *host
, /* I - Host to connect to */
387 int port
, /* I - Port number */
388 http_encryption_t encryption
) /* I - Type of encryption to use */
390 http_t
*http
; /* New HTTP connection */
393 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
394 host
, port
, encryption
));
397 * Create the HTTP structure...
400 if ((http
= _httpCreate(host
, port
, encryption
)) == NULL
)
404 * Connect to the remote system...
407 if (!httpReconnect(http
))
411 * Could not connect to any known address - bail out!
414 httpAddrFreeList(http
->addrlist
);
423 * '_httpCreate()' - Create an unconnected HTTP connection.
426 http_t
* /* O - HTTP connection */
428 const char *host
, /* I - Hostname */
429 int port
, /* I - Port number */
430 http_encryption_t encryption
) /* I - Encryption to use */
432 http_t
*http
; /* New HTTP connection */
433 http_addrlist_t
*addrlist
; /* Host address data */
434 char service
[255]; /* Service name */
437 DEBUG_printf(("4_httpCreate(host=\"%s\", port=%d, encryption=%d)",
438 host
, port
, encryption
));
449 sprintf(service
, "%d", port
);
451 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
455 * Allocate memory for the structure...
458 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
460 httpAddrFreeList(addrlist
);
465 * Initialize the HTTP data...
468 http
->activity
= time(NULL
);
469 http
->addrlist
= addrlist
;
473 http
->gssctx
= GSS_C_NO_CONTEXT
;
474 http
->gssname
= GSS_C_NO_NAME
;
475 #endif /* HAVE_GSSAPI */
476 http
->version
= HTTP_1_1
;
478 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
480 if (port
== 443) /* Always use encryption for https */
481 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
483 http
->encryption
= encryption
;
486 * Return the new structure...
494 * 'httpDelete()' - Send a DELETE request to the server.
497 int /* O - Status of call (0 = success) */
498 httpDelete(http_t
*http
, /* I - Connection to server */
499 const char *uri
) /* I - URI to delete */
501 return (http_send(http
, HTTP_DELETE
, uri
));
506 * 'httpEncryption()' - Set the required encryption on the link.
509 int /* O - -1 on error, 0 on success */
510 httpEncryption(http_t
*http
, /* I - Connection to server */
511 http_encryption_t e
) /* I - New encryption preference */
513 DEBUG_printf(("httpEncryption(http=%p, e=%d)", http
, e
));
519 http
->encryption
= e
;
521 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
522 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
523 return (httpReconnect(http
));
524 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
525 return (http_upgrade(http
));
529 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
533 #endif /* HAVE_SSL */
538 * 'httpError()' - Get the last error on a connection.
541 int /* O - Error code (errno) value */
542 httpError(http_t
*http
) /* I - Connection to server */
545 return (http
->error
);
552 * 'httpFlush()' - Flush data from a HTTP connection.
556 httpFlush(http_t
*http
) /* I - Connection to server */
558 char buffer
[8192]; /* Junk buffer */
559 int blocking
; /* To block or not to block */
560 http_state_t oldstate
; /* Old state */
563 DEBUG_printf(("httpFlush(http=%p), state=%s", http
,
564 http_states
[http
->state
]));
567 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
570 blocking
= http
->blocking
;
574 * Read any data we can...
577 oldstate
= http
->state
;
578 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
581 * Restore blocking and reset the connection if we didn't get all of
582 * the remaining data...
585 http
->blocking
= blocking
;
587 if (http
->state
== oldstate
&& http
->state
!= HTTP_WAITING
&& http
->fd
>= 0)
590 * Didn't get the data back, so close the current connection.
593 http
->state
= HTTP_WAITING
;
597 http_shutdown_ssl(http
);
598 #endif /* HAVE_SSL */
601 closesocket(http
->fd
);
612 * 'httpFlushWrite()' - Flush data in write buffer.
614 * @since CUPS 1.2/Mac OS X 10.5@
617 int /* O - Bytes written or -1 on error */
618 httpFlushWrite(http_t
*http
) /* I - Connection to server */
620 int bytes
; /* Bytes written */
623 DEBUG_printf(("httpFlushWrite(http=%p)", http
));
625 if (!http
|| !http
->wused
)
627 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
628 "1httpFlushWrite: No connection.");
632 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
633 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
635 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
639 DEBUG_printf(("1httpFlushWrite: Returning %d.", bytes
));
646 * 'httpGet()' - Send a GET request to the server.
649 int /* O - Status of call (0 = success) */
650 httpGet(http_t
*http
, /* I - Connection to server */
651 const char *uri
) /* I - URI to get */
653 return (http_send(http
, HTTP_GET
, uri
));
658 * 'httpGetAuthString()' - Get the current authorization string.
660 * The authorization string is set by cupsDoAuthentication() and
661 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
662 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
665 * @since CUPS 1.3/Mac OS X 10.5@
668 char * /* O - Authorization string */
669 httpGetAuthString(http_t
*http
) /* I - Connection to server */
672 return (http
->authstring
);
679 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
681 * @since CUPS 1.2/Mac OS X 10.5@
684 int /* O - 1 if blocking, 0 if non-blocking */
685 httpGetBlocking(http_t
*http
) /* I - Connection to server */
687 return (http
? http
->blocking
: 0);
692 * 'httpGetCookie()' - Get any cookie data from the response.
694 * @since CUPS 1.1.19/Mac OS X 10.3@
697 const char * /* O - Cookie data or NULL */
698 httpGetCookie(http_t
*http
) /* I - HTTP connecion */
700 return (http
? http
->cookie
: NULL
);
705 * 'httpGetFd()' - Get the file descriptor associated with a connection.
707 * @since CUPS 1.2/Mac OS X 10.5@
710 int /* O - File descriptor or -1 if none */
711 httpGetFd(http_t
*http
) /* I - Connection to server */
713 return (http
? http
->fd
: -1);
718 * 'httpGetField()' - Get a field value from a request/response.
721 const char * /* O - Field value */
722 httpGetField(http_t
*http
, /* I - Connection to server */
723 http_field_t field
) /* I - Field to get */
725 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
727 else if (field
== HTTP_FIELD_AUTHORIZATION
&&
728 http
->field_authorization
)
731 * Special case for WWW-Authenticate: as its contents can be
732 * longer than HTTP_MAX_VALUE...
735 return (http
->field_authorization
);
738 return (http
->fields
[field
]);
743 * 'httpGetLength()' - Get the amount of data remaining from the
744 * content-length or transfer-encoding fields.
746 * This function is deprecated and will not return lengths larger than
747 * 2^31 - 1; use httpGetLength2() instead.
752 int /* O - Content length */
753 httpGetLength(http_t
*http
) /* I - Connection to server */
756 * Get the read content length and return the 32-bit value.
761 httpGetLength2(http
);
763 return (http
->_data_remaining
);
771 * 'httpGetLength2()' - Get the amount of data remaining from the
772 * content-length or transfer-encoding fields.
774 * This function returns the complete content length, even for
775 * content larger than 2^31 - 1.
777 * @since CUPS 1.2/Mac OS X 10.5@
780 off_t
/* O - Content length */
781 httpGetLength2(http_t
*http
) /* I - Connection to server */
783 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http
,
784 http_states
[http
->state
]));
789 if (!strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
791 DEBUG_puts("4httpGetLength2: chunked request!");
793 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
794 http
->data_remaining
= 0;
798 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
801 * The following is a hack for HTTP servers that don't send a
802 * content-length or transfer-encoding field...
804 * If there is no content-length then the connection must close
805 * after the transfer is complete...
808 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
811 * Default content length is 0 for errors and 2^31-1 for other
812 * successful requests...
815 if (http
->status
>= HTTP_MULTIPLE_CHOICES
)
816 http
->data_remaining
= 0;
818 http
->data_remaining
= 2147483647;
821 http
->data_remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
824 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
825 CUPS_LLCAST http
->data_remaining
));
828 if (http
->data_remaining
<= INT_MAX
)
829 http
->_data_remaining
= (int)http
->data_remaining
;
831 http
->_data_remaining
= INT_MAX
;
833 return (http
->data_remaining
);
838 * 'httpGetStatus()' - Get the status of the last HTTP request.
840 * @since CUPS 1.2/Mac OS X 10.5@
843 http_status_t
/* O - HTTP status */
844 httpGetStatus(http_t
*http
) /* I - Connection to server */
846 return (http
? http
->status
: HTTP_ERROR
);
851 * 'httpGetSubField()' - Get a sub-field value.
856 char * /* O - Value or NULL */
857 httpGetSubField(http_t
*http
, /* I - Connection to server */
858 http_field_t field
, /* I - Field index */
859 const char *name
, /* I - Name of sub-field */
860 char *value
) /* O - Value string */
862 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
867 * 'httpGetSubField2()' - Get a sub-field value.
869 * @since CUPS 1.2/Mac OS X 10.5@
872 char * /* O - Value or NULL */
873 httpGetSubField2(http_t
*http
, /* I - Connection to server */
874 http_field_t field
, /* I - Field index */
875 const char *name
, /* I - Name of sub-field */
876 char *value
, /* O - Value string */
877 int valuelen
) /* I - Size of value buffer */
879 const char *fptr
; /* Pointer into field */
880 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
881 *ptr
, /* Pointer into string buffer */
882 *end
; /* End of value buffer */
884 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, "
885 "valuelen=%d)", http
, field
, name
, value
, valuelen
));
887 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
888 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
891 end
= value
+ valuelen
- 1;
893 for (fptr
= http
->fields
[field
]; *fptr
;)
896 * Skip leading whitespace...
899 while (isspace(*fptr
& 255))
909 * Get the sub-field name...
913 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) &&
914 ptr
< (temp
+ sizeof(temp
) - 1);
919 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
922 * Skip trailing chars up to the '='...
925 while (isspace(*fptr
& 255))
935 * Skip = and leading whitespace...
940 while (isspace(*fptr
& 255))
946 * Read quoted string...
949 for (ptr
= value
, fptr
++;
950 *fptr
&& *fptr
!= '\"' && ptr
< end
;
955 while (*fptr
&& *fptr
!= '\"')
964 * Read unquoted string...
968 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< end
;
973 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
977 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
980 * See if this is the one...
983 if (!strcmp(name
, temp
))
985 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
992 DEBUG_puts("3httpGetSubField2: Returning NULL");
999 * 'httpGets()' - Get a line of text from a HTTP connection.
1002 char * /* O - Line or NULL */
1003 httpGets(char *line
, /* I - Line to read into */
1004 int length
, /* I - Max length of buffer */
1005 http_t
*http
) /* I - Connection to server */
1007 char *lineptr
, /* Pointer into line */
1008 *lineend
, /* End of line */
1009 *bufptr
, /* Pointer into input buffer */
1010 *bufend
; /* Pointer to end of buffer */
1011 int bytes
, /* Number of bytes read */
1012 eol
; /* End-of-line? */
1015 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", line
, length
, http
));
1017 if (http
== NULL
|| line
== NULL
)
1021 * Read a line from the buffer...
1026 lineend
= line
+ length
- 1;
1029 while (lineptr
< lineend
)
1032 * Pre-load the buffer as needed...
1041 while (http
->used
== 0)
1044 * No newline; see if there is more data to be read...
1047 if (!http
->blocking
&& !_httpWait(http
, 10000, 1))
1049 DEBUG_puts("3httpGets: Timed out!");
1051 http
->error
= WSAETIMEDOUT
;
1053 http
->error
= ETIMEDOUT
;
1060 bytes
= http_read_ssl(http
, http
->buffer
+ http
->used
,
1061 HTTP_MAX_BUFFER
- http
->used
);
1063 #endif /* HAVE_SSL */
1064 bytes
= recv(http
->fd
, http
->buffer
+ http
->used
,
1065 HTTP_MAX_BUFFER
- http
->used
, 0);
1067 DEBUG_printf(("4httpGets: read %d bytes...", bytes
));
1072 * Nope, can't get a line this time...
1076 if (WSAGetLastError() != http
->error
)
1078 http
->error
= WSAGetLastError();
1082 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1084 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1086 if (errno
== EINTR
|| errno
== EAGAIN
)
1088 else if (errno
!= http
->error
)
1090 http
->error
= errno
;
1097 else if (bytes
== 0)
1099 http
->error
= EPIPE
;
1105 * Yup, update the amount used...
1108 http
->used
+= bytes
;
1112 * Now copy as much of the current line as possible...
1115 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1116 lineptr
< lineend
&& bufptr
< bufend
;)
1118 if (*bufptr
== 0x0a)
1124 else if (*bufptr
== 0x0d)
1127 *lineptr
++ = *bufptr
++;
1130 http
->used
-= (int)(bufptr
- http
->buffer
);
1132 memmove(http
->buffer
, bufptr
, http
->used
);
1140 http
->activity
= time(NULL
);
1144 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1150 DEBUG_puts("3httpGets: No new line available!");
1157 * 'httpHead()' - Send a HEAD request to the server.
1160 int /* O - Status of call (0 = success) */
1161 httpHead(http_t
*http
, /* I - Connection to server */
1162 const char *uri
) /* I - URI for head */
1164 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http
, uri
));
1165 return (http_send(http
, HTTP_HEAD
, uri
));
1170 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1171 * default HTTP proxy (if any).
1175 httpInitialize(void)
1179 struct timeval curtime
; /* Current time in microseconds */
1180 # endif /* !WIN32 */
1181 int i
; /* Looping var */
1182 unsigned char data
[1024]; /* Seed data */
1183 #endif /* HAVE_LIBSSL */
1186 WSADATA winsockdata
; /* WinSock data */
1187 static int initialized
= 0; /* Has WinSock been initialized? */
1191 WSAStartup(MAKEWORD(1,1), &winsockdata
);
1192 #elif !defined(SO_NOSIGPIPE)
1194 * Ignore SIGPIPE signals...
1198 sigset(SIGPIPE
, SIG_IGN
);
1199 # elif defined(HAVE_SIGACTION)
1200 struct sigaction action
; /* POSIX sigaction data */
1203 memset(&action
, 0, sizeof(action
));
1204 action
.sa_handler
= SIG_IGN
;
1205 sigaction(SIGPIPE
, &action
, NULL
);
1207 signal(SIGPIPE
, SIG_IGN
);
1208 # endif /* !SO_NOSIGPIPE */
1212 gnutls_global_init();
1213 #endif /* HAVE_GNUTLS */
1216 SSL_load_error_strings();
1220 * Using the current time is a dubious random seed, but on some systems
1221 * it is the best we can do (on others, this seed isn't even used...)
1226 gettimeofday(&curtime
, NULL
);
1227 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
1230 for (i
= 0; i
< sizeof(data
); i
++)
1233 RAND_seed(data
, sizeof(data
));
1234 #endif /* HAVE_LIBSSL */
1239 * 'httpOptions()' - Send an OPTIONS request to the server.
1242 int /* O - Status of call (0 = success) */
1243 httpOptions(http_t
*http
, /* I - Connection to server */
1244 const char *uri
) /* I - URI for options */
1246 return (http_send(http
, HTTP_OPTIONS
, uri
));
1251 * 'httpPost()' - Send a POST request to the server.
1254 int /* O - Status of call (0 = success) */
1255 httpPost(http_t
*http
, /* I - Connection to server */
1256 const char *uri
) /* I - URI for post */
1258 return (http_send(http
, HTTP_POST
, uri
));
1263 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1268 int /* O - Number of bytes written */
1269 httpPrintf(http_t
*http
, /* I - Connection to server */
1270 const char *format
, /* I - printf-style format string */
1271 ...) /* I - Additional args as needed */
1273 int bytes
; /* Number of bytes to write */
1274 char buf
[16384]; /* Buffer for formatted string */
1275 va_list ap
; /* Variable argument pointer */
1278 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", http
, format
));
1280 va_start(ap
, format
);
1281 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1284 DEBUG_printf(("3httpPrintf: %s", buf
));
1286 if (http
->data_encoding
== HTTP_ENCODE_FIELDS
)
1287 return (httpWrite2(http
, buf
, bytes
));
1292 DEBUG_puts("4httpPrintf: flushing existing data...");
1294 if (httpFlushWrite(http
) < 0)
1298 return (http_write(http
, buf
, bytes
));
1304 * 'httpPut()' - Send a PUT request to the server.
1307 int /* O - Status of call (0 = success) */
1308 httpPut(http_t
*http
, /* I - Connection to server */
1309 const char *uri
) /* I - URI to put */
1311 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http
, uri
));
1312 return (http_send(http
, HTTP_PUT
, uri
));
1317 * 'httpRead()' - Read data from a HTTP connection.
1319 * This function is deprecated. Use the httpRead2() function which can
1320 * read more than 2GB of data.
1325 int /* O - Number of bytes read */
1326 httpRead(http_t
*http
, /* I - Connection to server */
1327 char *buffer
, /* I - Buffer for data */
1328 int length
) /* I - Maximum number of bytes */
1330 return ((int)httpRead2(http
, buffer
, length
));
1335 * 'httpRead2()' - Read data from a HTTP connection.
1337 * @since CUPS 1.2/Mac OS X 10.5@
1340 ssize_t
/* O - Number of bytes read */
1341 httpRead2(http_t
*http
, /* I - Connection to server */
1342 char *buffer
, /* I - Buffer for data */
1343 size_t length
) /* I - Maximum number of bytes */
1345 ssize_t bytes
; /* Bytes read */
1346 char len
[32]; /* Length string */
1349 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
1350 http
, buffer
, CUPS_LLCAST length
));
1352 if (http
== NULL
|| buffer
== NULL
)
1355 http
->activity
= time(NULL
);
1361 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
1362 http
->data_remaining
<= 0)
1364 DEBUG_puts("2httpRead2: Getting chunk length...");
1366 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1368 DEBUG_puts("1httpRead2: Could not get length!");
1372 http
->data_remaining
= strtoll(len
, NULL
, 16);
1373 if (http
->data_remaining
< 0)
1375 DEBUG_puts("1httpRead2: Negative chunk length!");
1380 DEBUG_printf(("2httpRead2: data_remaining=" CUPS_LLFMT
,
1381 CUPS_LLCAST http
->data_remaining
));
1383 if (http
->data_remaining
<= 0)
1386 * A zero-length chunk ends a transfer; unless we are reading POST
1390 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1391 httpGets(len
, sizeof(len
), http
);
1393 if (http
->state
== HTTP_POST_RECV
)
1396 http
->state
= HTTP_WAITING
;
1399 * Prevent future reads for this request...
1402 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1406 else if (length
> (size_t)http
->data_remaining
)
1407 length
= (size_t)http
->data_remaining
;
1409 if (http
->used
== 0 && length
<= 256)
1412 * Buffer small reads for better performance...
1415 if (!http
->blocking
&& !httpWait(http
, 10000))
1418 if (http
->data_remaining
> sizeof(http
->buffer
))
1419 bytes
= sizeof(http
->buffer
);
1421 bytes
= http
->data_remaining
;
1425 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
1427 #endif /* HAVE_SSL */
1429 DEBUG_printf(("2httpRead2: reading %d bytes from socket into buffer...",
1432 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
1434 DEBUG_printf(("2httpRead2: read %d bytes from socket into buffer...",
1443 http
->error
= WSAGetLastError();
1446 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1448 http
->error
= errno
;
1455 http
->error
= EPIPE
;
1462 if (length
> (size_t)http
->used
)
1463 length
= (size_t)http
->used
;
1465 bytes
= (ssize_t
)length
;
1467 DEBUG_printf(("2httpRead2: grabbing %d bytes from input buffer...",
1470 memcpy(buffer
, http
->buffer
, length
);
1471 http
->used
-= (int)length
;
1474 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1479 if (!http
->blocking
&& !httpWait(http
, 10000))
1482 bytes
= (ssize_t
)http_read_ssl(http
, buffer
, (int)length
);
1484 #endif /* HAVE_SSL */
1487 if (!http
->blocking
&& !httpWait(http
, 10000))
1490 DEBUG_printf(("2httpRead2: reading " CUPS_LLFMT
" bytes from socket...",
1491 CUPS_LLCAST length
));
1494 bytes
= (ssize_t
)recv(http
->fd
, buffer
, (int)length
, 0);
1496 while ((bytes
= recv(http
->fd
, buffer
, length
, 0)) < 0)
1497 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1501 DEBUG_printf(("2httpRead2: read " CUPS_LLFMT
" bytes from socket...",
1502 CUPS_LLCAST bytes
));
1507 http
->data_remaining
-= bytes
;
1509 if (http
->data_remaining
<= INT_MAX
)
1510 http
->_data_remaining
= (int)http
->data_remaining
;
1512 http
->_data_remaining
= INT_MAX
;
1517 http
->error
= WSAGetLastError();
1519 if (errno
== EINTR
|| errno
== EAGAIN
)
1522 http
->error
= errno
;
1527 http
->error
= EPIPE
;
1531 if (http
->data_remaining
== 0)
1533 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1534 httpGets(len
, sizeof(len
), http
);
1536 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1538 if (http
->state
== HTTP_POST_RECV
)
1541 http
->state
= HTTP_WAITING
;
1546 http_debug_hex("httpRead2", buffer
, (int)bytes
);
1553 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1555 * '_httpReadCDSA()' - Read function for the CDSA library.
1558 OSStatus
/* O - -1 on error, 0 on success */
1560 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1561 void *data
, /* I - Data buffer */
1562 size_t *dataLength
) /* IO - Number of bytes */
1564 OSStatus result
; /* Return value */
1565 ssize_t bytes
; /* Number of bytes read */
1566 http_t
*http
; /* HTTP connection */
1569 http
= (http_t
*)connection
;
1571 if (!http
->blocking
)
1574 * Make sure we have data before we read...
1577 if (!_httpWait(http
, 10000, 0))
1579 http
->error
= ETIMEDOUT
;
1586 bytes
= recv(http
->fd
, data
, *dataLength
, 0);
1588 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1590 if (bytes
== *dataLength
)
1596 *dataLength
= bytes
;
1597 result
= errSSLWouldBlock
;
1604 result
= errSSLClosedGraceful
;
1605 else if (errno
== EAGAIN
)
1606 result
= errSSLWouldBlock
;
1608 result
= errSSLClosedAbort
;
1613 #endif /* HAVE_SSL && HAVE_CDSASSL */
1616 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
1618 * '_httpReadGNUTLS()' - Read function for the GNU TLS library.
1621 ssize_t
/* O - Number of bytes read or -1 on error */
1623 gnutls_transport_ptr ptr
, /* I - Connection to server */
1624 void *data
, /* I - Buffer */
1625 size_t length
) /* I - Number of bytes to read */
1627 http_t
*http
; /* HTTP connection */
1630 http
= (http_t
*)ptr
;
1632 if (!http
->blocking
)
1635 * Make sure we have data before we read...
1638 if (!_httpWait(http
, 10000, 0))
1640 http
->error
= ETIMEDOUT
;
1645 return (recv(http
->fd
, data
, length
, 0));
1647 #endif /* HAVE_SSL && HAVE_GNUTLS */
1651 * 'httpReconnect()' - Reconnect to a HTTP server.
1654 int /* O - 0 on success, non-zero on failure */
1655 httpReconnect(http_t
*http
) /* I - Connection to server */
1657 http_addrlist_t
*addr
; /* Connected address */
1659 http_addrlist_t
*current
; /* Current address */
1660 char temp
[256]; /* Temporary address string */
1664 DEBUG_printf(("httpReconnect(http=%p)", http
));
1672 DEBUG_puts("2httpReconnect: Shutting down SSL/TLS...");
1673 http_shutdown_ssl(http
);
1675 #endif /* HAVE_SSL */
1678 * Close any previously open socket...
1683 DEBUG_printf(("2httpReconnect: Closing socket %d...", http
->fd
));
1686 closesocket(http
->fd
);
1695 * Connect to the server...
1699 for (current
= http
->addrlist
; current
; current
= current
->next
)
1700 DEBUG_printf(("2httpReconnect: Address %s:%d",
1701 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
1702 _httpAddrPort(&(current
->addr
))));
1705 if ((addr
= httpAddrConnect(http
->addrlist
, &(http
->fd
))) == NULL
)
1708 * Unable to connect...
1712 http
->error
= WSAGetLastError();
1714 http
->error
= errno
;
1716 http
->status
= HTTP_ERROR
;
1718 DEBUG_printf(("1httpReconnect: httpAddrConnect failed: %s",
1719 strerror(http
->error
)));
1724 DEBUG_printf(("2httpReconnect: New socket=%d", http
->fd
));
1726 http
->hostaddr
= &(addr
->addr
);
1728 http
->status
= HTTP_CONTINUE
;
1731 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
1734 * Always do encryption via SSL.
1737 if (http_setup_ssl(http
) != 0)
1740 closesocket(http
->fd
);
1748 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
1749 return (http_upgrade(http
));
1750 #endif /* HAVE_SSL */
1752 DEBUG_printf(("1httpReconnect: Connected to %s:%d...",
1753 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
1754 _httpAddrPort(http
->hostaddr
)));
1761 * 'httpSetAuthString()' - Set the current authorization string.
1763 * This function just stores a copy of the current authorization string in
1764 * the HTTP connection object. You must still call httpSetField() to set
1765 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
1766 * httpHead(), httpOptions(), httpPost, or httpPut().
1768 * @since CUPS 1.3/Mac OS X 10.5@
1772 httpSetAuthString(http_t
*http
, /* I - Connection to server */
1773 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
1774 const char *data
) /* I - Auth data (NULL for none) */
1777 * Range check input...
1783 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
1784 free(http
->authstring
);
1786 http
->authstring
= http
->_authstring
;
1791 * Set the current authorization string...
1794 int len
= (int)strlen(scheme
) + (data
? (int)strlen(data
) + 1 : 0) + 1;
1797 if (len
> (int)sizeof(http
->_authstring
))
1799 if ((temp
= malloc(len
)) == NULL
)
1800 len
= sizeof(http
->_authstring
);
1802 http
->authstring
= temp
;
1806 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
1808 strlcpy(http
->authstring
, scheme
, len
);
1813 * Clear the current authorization string...
1816 http
->_authstring
[0] = '\0';
1822 * 'httpSetCookie()' - Set the cookie value(s)...
1824 * @since CUPS 1.1.19/Mac OS X 10.3@
1828 httpSetCookie(http_t
*http
, /* I - Connection */
1829 const char *cookie
) /* I - Cookie string */
1838 http
->cookie
= strdup(cookie
);
1840 http
->cookie
= NULL
;
1845 * 'httpSetExpect()' - Set the Expect: header in a request.
1847 * Currently only HTTP_CONTINUE is supported for the "expect" argument.
1849 * @since CUPS 1.2/Mac OS X 10.5@
1853 httpSetExpect(http_t
*http
, /* I - Connection to server */
1854 http_status_t expect
) /* I - HTTP status to expect (HTTP_CONTINUE) */
1857 http
->expect
= expect
;
1862 * 'httpSetField()' - Set the value of an HTTP header.
1866 httpSetField(http_t
*http
, /* I - Connection to server */
1867 http_field_t field
, /* I - Field index */
1868 const char *value
) /* I - Value */
1871 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
1872 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
1876 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
1878 if (field
== HTTP_FIELD_AUTHORIZATION
)
1881 * Special case for Authorization: as its contents can be
1882 * longer than HTTP_MAX_VALUE
1885 if (http
->field_authorization
)
1886 free(http
->field_authorization
);
1888 http
->field_authorization
= strdup(value
);
1890 else if (field
== HTTP_FIELD_HOST
)
1893 * Special-case for Host: as we don't want a trailing "." on the hostname and
1894 * need to bracket IPv6 numeric addresses.
1897 char *ptr
= strchr(value
, ':');
1899 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
1902 * Bracket IPv6 numeric addresses...
1904 * This is slightly inefficient (basically copying twice), but is an edge
1905 * case and not worth optimizing...
1908 snprintf(http
->fields
[HTTP_FIELD_HOST
],
1909 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
1914 * Check for a trailing dot on the hostname...
1917 ptr
= http
->fields
[HTTP_FIELD_HOST
];
1921 ptr
+= strlen(ptr
) - 1;
1932 * 'httpSetLength()' - Set the content-length and content-encoding.
1934 * @since CUPS 1.2/Mac OS X 10.5@
1938 httpSetLength(http_t
*http
, /* I - Connection to server */
1939 size_t length
) /* I - Length (0 for chunked) */
1946 strcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked");
1947 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
1951 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
1952 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
1953 CUPS_LLFMT
, CUPS_LLCAST length
);
1959 * 'httpTrace()' - Send an TRACE request to the server.
1962 int /* O - Status of call (0 = success) */
1963 httpTrace(http_t
*http
, /* I - Connection to server */
1964 const char *uri
) /* I - URI for trace */
1966 return (http_send(http
, HTTP_TRACE
, uri
));
1971 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1974 http_status_t
/* O - HTTP status */
1975 httpUpdate(http_t
*http
) /* I - Connection to server */
1977 char line
[32768], /* Line from connection... */
1978 *value
; /* Pointer to value on line */
1979 http_field_t field
; /* Field index */
1980 int major
, minor
, /* HTTP version numbers */
1981 status
; /* Request status */
1984 DEBUG_printf(("httpUpdate(http=%p), state=%s", http
,
1985 http_states
[http
->state
]));
1988 * Flush pending data, if any...
1993 DEBUG_puts("2httpUpdate: flushing buffer...");
1995 if (httpFlushWrite(http
) < 0)
1996 return (HTTP_ERROR
);
2000 * If we haven't issued any commands, then there is nothing to "update"...
2003 if (http
->state
== HTTP_WAITING
)
2004 return (HTTP_CONTINUE
);
2007 * Grab all of the lines we can from the connection...
2010 while (httpGets(line
, sizeof(line
), http
) != NULL
)
2012 DEBUG_printf(("2httpUpdate: Got \"%s\"", line
));
2014 if (line
[0] == '\0')
2017 * Blank line means the start of the data section (if any). Return
2018 * the result code, too...
2020 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
2021 * Instead, we just return HTTP_CONTINUE to the caller and keep on
2025 if (http
->status
== HTTP_CONTINUE
)
2026 return (http
->status
);
2028 if (http
->status
< HTTP_BAD_REQUEST
)
2029 http
->digest_tries
= 0;
2032 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
2034 if (http_setup_ssl(http
) != 0)
2037 closesocket(http
->fd
);
2042 return (HTTP_ERROR
);
2045 return (HTTP_CONTINUE
);
2047 #endif /* HAVE_SSL */
2049 httpGetLength2(http
);
2051 switch (http
->state
)
2055 case HTTP_POST_RECV
:
2058 case HTTP_POST_SEND
:
2063 http
->state
= HTTP_WAITING
;
2067 return (http
->status
);
2069 else if (!strncmp(line
, "HTTP/", 5))
2072 * Got the beginning of a response...
2075 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
2076 return (HTTP_ERROR
);
2078 http
->version
= (http_version_t
)(major
* 100 + minor
);
2079 http
->status
= (http_status_t
)status
;
2081 else if ((value
= strchr(line
, ':')) != NULL
)
2088 while (isspace(*value
& 255))
2092 * Be tolerants of servers that send unknown attribute fields...
2095 if (!strcasecmp(line
, "expect"))
2098 * "Expect: 100-continue" or similar...
2101 http
->expect
= (http_status_t
)atoi(value
);
2103 else if (!strcasecmp(line
, "cookie"))
2106 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2109 httpSetCookie(http
, value
);
2111 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
2113 DEBUG_printf(("1httpUpdate: unknown field %s seen!", line
));
2117 httpSetField(http
, field
, value
);
2121 DEBUG_printf(("1httpUpdate: Bad response line \"%s\"!", line
));
2122 http
->status
= HTTP_ERROR
;
2123 return (HTTP_ERROR
);
2128 * See if there was an error...
2131 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
2133 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
2134 return (http
->status
);
2139 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
2140 strerror(http
->error
)));
2141 http
->status
= HTTP_ERROR
;
2142 return (HTTP_ERROR
);
2146 * If we haven't already returned, then there is nothing new...
2149 return (HTTP_CONTINUE
);
2154 * '_httpWait()' - Wait for data available on a connection (no flush).
2157 int /* O - 1 if data is available, 0 otherwise */
2158 _httpWait(http_t
*http
, /* I - Connection to server */
2159 int msec
, /* I - Milliseconds to wait */
2160 int usessl
) /* I - Use SSL context? */
2163 struct pollfd pfd
; /* Polled file descriptor */
2165 fd_set input_set
; /* select() input set */
2166 struct timeval timeout
; /* Timeout */
2167 #endif /* HAVE_POLL */
2168 int nfds
; /* Result from select()/poll() */
2171 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", http
, msec
, usessl
));
2175 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
2180 * Check the SSL/TLS buffers for data first...
2184 if (http
->tls
&& usessl
)
2187 if (SSL_pending((SSL
*)(http
->tls
)))
2189 DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data.");
2193 # elif defined(HAVE_GNUTLS)
2194 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2196 DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data.");
2200 # elif defined(HAVE_CDSASSL)
2201 size_t bytes
; /* Bytes that are available */
2203 if (!SSLGetBufferedReadSize(((http_tls_t
*)(http
->tls
))->session
, &bytes
) &&
2206 DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data.");
2209 # endif /* HAVE_LIBSSL */
2211 #endif /* HAVE_SSL */
2214 * Then try doing a select() or poll() to poll the socket...
2219 pfd
.events
= POLLIN
;
2221 while ((nfds
= poll(&pfd
, 1, msec
)) < 0 &&
2222 (errno
== EINTR
|| errno
== EAGAIN
));
2227 FD_ZERO(&input_set
);
2228 FD_SET(http
->fd
, &input_set
);
2230 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
2234 timeout
.tv_sec
= msec
/ 1000;
2235 timeout
.tv_usec
= (msec
% 1000) * 1000;
2237 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
2240 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
2242 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
2245 while (nfds
< 0 && WSAGetLastError() == WSAEINTR
);
2247 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
2249 #endif /* HAVE_POLL */
2251 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
2259 * 'httpWait()' - Wait for data available on a connection.
2261 * @since CUPS 1.1.19/Mac OS X 10.3@
2264 int /* O - 1 if data is available, 0 otherwise */
2265 httpWait(http_t
*http
, /* I - Connection to server */
2266 int msec
) /* I - Milliseconds to wait */
2269 * First see if there is data in the buffer...
2279 * Flush pending data, if any...
2284 if (httpFlushWrite(http
) < 0)
2289 * If not, check the SSL/TLS buffers and do a select() on the connection...
2292 return (_httpWait(http
, msec
, 1));
2297 * 'httpWrite()' - Write data to a HTTP connection.
2299 * This function is deprecated. Use the httpWrite2() function which can
2300 * write more than 2GB of data.
2305 int /* O - Number of bytes written */
2306 httpWrite(http_t
*http
, /* I - Connection to server */
2307 const char *buffer
, /* I - Buffer for data */
2308 int length
) /* I - Number of bytes to write */
2310 return ((int)httpWrite2(http
, buffer
, length
));
2315 * 'httpWrite2()' - Write data to a HTTP connection.
2317 * @since CUPS 1.2/Mac OS X 10.5@
2320 ssize_t
/* O - Number of bytes written */
2321 httpWrite2(http_t
*http
, /* I - Connection to server */
2322 const char *buffer
, /* I - Buffer for data */
2323 size_t length
) /* I - Number of bytes to write */
2325 ssize_t bytes
; /* Bytes written */
2328 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
2329 buffer
, CUPS_LLCAST length
));
2332 * Range check input...
2335 if (http
== NULL
|| buffer
== NULL
)
2339 * Mark activity on the connection...
2342 http
->activity
= time(NULL
);
2345 * Buffer small writes for better performance...
2350 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
2352 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
2353 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
2355 httpFlushWrite(http
);
2358 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
) &&
2359 length
< sizeof(http
->wbuffer
))
2362 * Write to buffer...
2365 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
2366 CUPS_LLCAST length
));
2368 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
2369 http
->wused
+= (int)length
;
2370 bytes
= (ssize_t
)length
;
2375 * Otherwise write the data directly...
2378 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
2379 CUPS_LLCAST length
));
2381 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2382 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, (int)length
);
2384 bytes
= (ssize_t
)http_write(http
, buffer
, (int)length
);
2386 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
2387 CUPS_LLCAST bytes
));
2390 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2391 http
->data_remaining
-= bytes
;
2397 * Handle end-of-request processing...
2400 if ((http
->data_encoding
== HTTP_ENCODE_CHUNKED
&& length
== 0) ||
2401 (http
->data_encoding
== HTTP_ENCODE_LENGTH
&& http
->data_remaining
== 0))
2404 * Finished with the transfer; unless we are sending POST or PUT
2408 DEBUG_puts("2httpWrite: changing states...");
2411 httpFlushWrite(http
);
2413 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2416 * Send a 0-length chunk at the end of the request...
2419 http_write(http
, "0\r\n\r\n", 5);
2422 * Reset the data state...
2425 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
2426 http
->data_remaining
= 0;
2429 if (http
->state
== HTTP_POST_RECV
)
2431 else if (http
->state
== HTTP_PUT_RECV
)
2432 http
->state
= HTTP_STATUS
;
2434 http
->state
= HTTP_WAITING
;
2441 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
2443 * '_httpWriteCDSA()' - Write function for the CDSA library.
2446 OSStatus
/* O - -1 on error, 0 on success */
2448 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2449 const void *data
, /* I - Data buffer */
2450 size_t *dataLength
) /* IO - Number of bytes */
2452 OSStatus result
; /* Return value */
2453 ssize_t bytes
; /* Number of bytes read */
2454 http_t
*http
; /* HTTP connection */
2457 http
= (http_t
*)connection
;
2461 bytes
= write(http
->fd
, data
, *dataLength
);
2463 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
2465 if (bytes
== *dataLength
)
2469 else if (bytes
>= 0)
2471 *dataLength
= bytes
;
2472 result
= errSSLWouldBlock
;
2478 if (errno
== EAGAIN
)
2479 result
= errSSLWouldBlock
;
2481 result
= errSSLClosedAbort
;
2486 #endif /* HAVE_SSL && HAVE_CDSASSL */
2489 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
2491 * '_httpWriteGNUTLS()' - Write function for the GNU TLS library.
2494 ssize_t
/* O - Number of bytes written or -1 on error */
2496 gnutls_transport_ptr ptr
, /* I - Connection to server */
2497 const void *data
, /* I - Data buffer */
2498 size_t length
) /* I - Number of bytes to write */
2500 return (send(((http_t
*)ptr
)->fd
, data
, length
, 0));
2502 #endif /* HAVE_SSL && HAVE_GNUTLS */
2505 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
2507 * 'http_bio_ctrl()' - Control the HTTP connection.
2510 static long /* O - Result/data */
2511 http_bio_ctrl(BIO
*h
, /* I - BIO data */
2512 int cmd
, /* I - Control command */
2513 long arg1
, /* I - First argument */
2514 void *arg2
) /* I - Second argument */
2521 case BIO_CTRL_RESET
:
2525 case BIO_C_SET_FILE_PTR
:
2530 case BIO_C_GET_FILE_PTR
:
2533 *((void **)arg2
) = h
->ptr
;
2540 case BIO_CTRL_FLUSH
:
2547 * 'http_bio_free()' - Free OpenSSL data.
2550 static int /* O - 1 on success, 0 on failure */
2551 http_bio_free(BIO
*h
) /* I - BIO data */
2567 * 'http_bio_new()' - Initialize an OpenSSL BIO structure.
2570 static int /* O - 1 on success, 0 on failure */
2571 http_bio_new(BIO
*h
) /* I - BIO data */
2586 * 'http_bio_puts()' - Send a string for OpenSSL.
2589 static int /* O - Bytes written */
2590 http_bio_puts(BIO
*h
, /* I - BIO data */
2591 const char *str
) /* I - String to write */
2594 return (send(((http_t
*)h
->ptr
)->fd
, str
, (int)strlen(str
), 0));
2596 return (send(((http_t
*)h
->ptr
)->fd
, str
, strlen(str
), 0));
2602 * 'http_bio_read()' - Read data for OpenSSL.
2605 static int /* O - Bytes read */
2606 http_bio_read(BIO
*h
, /* I - BIO data */
2607 char *buf
, /* I - Buffer */
2608 int size
) /* I - Number of bytes to read */
2610 http_t
*http
; /* HTTP connection */
2613 http
= (http_t
*)h
->ptr
;
2615 if (!http
->blocking
)
2618 * Make sure we have data before we read...
2621 if (!_httpWait(http
, 10000, 0))
2624 http
->error
= WSAETIMEDOUT
;
2626 http
->error
= ETIMEDOUT
;
2633 return (recv(http
->fd
, buf
, size
, 0));
2638 * 'http_bio_write()' - Write data for OpenSSL.
2641 static int /* O - Bytes written */
2642 http_bio_write(BIO
*h
, /* I - BIO data */
2643 const char *buf
, /* I - Buffer to write */
2644 int num
) /* I - Number of bytes to write */
2646 return (send(((http_t
*)h
->ptr
)->fd
, buf
, num
, 0));
2648 #endif /* HAVE_SSL && HAVE_LIBSSL */
2653 * 'http_debug_hex()' - Do a hex dump of a buffer.
2657 http_debug_hex(const char *prefix
, /* I - Prefix for line */
2658 const char *buffer
, /* I - Buffer to dump */
2659 int bytes
) /* I - Bytes to dump */
2661 int i
, j
, /* Looping vars */
2662 ch
; /* Current character */
2663 char line
[255], /* Line buffer */
2664 *start
, /* Start of line after prefix */
2665 *ptr
; /* Pointer into line */
2668 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
2671 DEBUG_printf(("6%s: %d bytes:\n", prefix
, bytes
));
2673 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
2674 start
= line
+ strlen(line
);
2676 for (i
= 0; i
< bytes
; i
+= 16)
2678 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
2679 sprintf(ptr
, "%02X", buffer
[i
+ j
] & 255);
2691 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
2693 ch
= buffer
[i
+ j
] & 255;
2695 if (ch
< ' ' || ch
>= 127)
2709 * 'http_field()' - Return the field index for a field name.
2712 static http_field_t
/* O - Field index */
2713 http_field(const char *name
) /* I - String name */
2715 int i
; /* Looping var */
2718 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2719 if (strcasecmp(name
, http_fields
[i
]) == 0)
2720 return ((http_field_t
)i
);
2722 return (HTTP_FIELD_UNKNOWN
);
2728 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2731 static int /* O - Bytes read */
2732 http_read_ssl(http_t
*http
, /* I - Connection to server */
2733 char *buf
, /* I - Buffer to store data */
2734 int len
) /* I - Length of buffer */
2736 # if defined(HAVE_LIBSSL)
2737 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2739 # elif defined(HAVE_GNUTLS)
2740 ssize_t result
; /* Return value */
2743 result
= gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
);
2745 if (result
< 0 && !errno
)
2748 * Convert GNU TLS error to errno value...
2753 case GNUTLS_E_INTERRUPTED
:
2757 case GNUTLS_E_AGAIN
:
2769 return ((int)result
);
2771 # elif defined(HAVE_CDSASSL)
2772 int result
; /* Return value */
2773 OSStatus error
; /* Error info */
2774 size_t processed
; /* Number of bytes processed */
2777 error
= SSLRead(((http_tls_t
*)http
->tls
)->session
, buf
, len
, &processed
);
2782 result
= (int)processed
;
2784 case errSSLClosedGraceful
:
2787 case errSSLWouldBlock
:
2789 result
= (int)processed
;
2803 # endif /* HAVE_LIBSSL */
2805 #endif /* HAVE_SSL */
2809 * 'http_send()' - Send a request with all fields and the trailing blank line.
2812 static int /* O - 0 on success, non-zero on error */
2813 http_send(http_t
*http
, /* I - Connection to server */
2814 http_state_t request
, /* I - Request code */
2815 const char *uri
) /* I - URI */
2817 int i
; /* Looping var */
2818 char buf
[1024]; /* Encoded URI buffer */
2819 static const char * const codes
[] =
2820 { /* Request code strings */
2837 DEBUG_printf(("7http_send(http=%p, request=HTTP_%s, uri=\"%s\")",
2838 http
, codes
[request
], uri
));
2840 if (http
== NULL
|| uri
== NULL
)
2844 * Set the User-Agent field if it isn't already...
2847 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
2848 httpSetField(http
, HTTP_FIELD_USER_AGENT
, CUPS_MINIMAL
);
2851 * Encode the URI as needed...
2854 _httpEncodeURI(buf
, uri
, sizeof(buf
));
2857 * See if we had an error the last time around; if so, reconnect...
2860 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
2861 if (httpReconnect(http
))
2865 * Flush any written data that is pending...
2870 if (httpFlushWrite(http
) < 0)
2871 if (httpReconnect(http
))
2876 * Send the request header...
2879 http
->state
= request
;
2880 http
->data_encoding
= HTTP_ENCODE_FIELDS
;
2882 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
2885 http
->status
= HTTP_CONTINUE
;
2888 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
2890 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
2891 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
2893 #endif /* HAVE_SSL */
2895 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
2897 http
->status
= HTTP_ERROR
;
2901 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2902 if (http
->fields
[i
][0] != '\0')
2904 DEBUG_printf(("9http_send: %s: %s", http_fields
[i
],
2905 httpGetField(http
, i
)));
2907 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
],
2908 httpGetField(http
, i
)) < 1)
2910 http
->status
= HTTP_ERROR
;
2916 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
2918 http
->status
= HTTP_ERROR
;
2922 if (http
->expect
== HTTP_CONTINUE
&&
2923 (http
->state
== HTTP_POST_RECV
|| http
->state
== HTTP_PUT_RECV
))
2924 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
2926 http
->status
= HTTP_ERROR
;
2930 if (httpPrintf(http
, "\r\n") < 1)
2932 http
->status
= HTTP_ERROR
;
2936 if (httpFlushWrite(http
) < 0)
2939 httpGetLength2(http
);
2940 httpClearFields(http
);
2943 * The Kerberos and AuthRef authentication strings can only be used once...
2946 if (http
->field_authorization
&& http
->authstring
&&
2947 (!strncmp(http
->authstring
, "Negotiate", 9) ||
2948 !strncmp(http
->authstring
, "AuthRef", 7)))
2950 http
->_authstring
[0] = '\0';
2952 if (http
->authstring
!= http
->_authstring
)
2953 free(http
->authstring
);
2955 http
->authstring
= http
->_authstring
;
2964 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2967 static int /* O - Status of connection */
2968 http_setup_ssl(http_t
*http
) /* I - Connection to server */
2971 SSL_CTX
*context
; /* Context for encryption */
2972 SSL
*conn
; /* Connection for encryption */
2973 BIO
*bio
; /* BIO data */
2974 # elif defined(HAVE_GNUTLS)
2975 http_tls_t
*conn
; /* TLS session object */
2976 gnutls_certificate_client_credentials
*credentials
;
2977 /* TLS credentials */
2978 # elif defined(HAVE_CDSASSL)
2979 OSStatus error
; /* Error code */
2980 http_tls_t
*conn
; /* CDSA connection information */
2981 # endif /* HAVE_LIBSSL */
2984 DEBUG_printf(("7http_setup_ssl(http=%p)", http
));
2987 context
= SSL_CTX_new(SSLv23_client_method());
2989 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
2991 bio
= BIO_new(_httpBIOMethods());
2992 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)http
);
2994 conn
= SSL_new(context
);
2995 SSL_set_bio(conn
, bio
, bio
);
2997 if (SSL_connect(conn
) != 1)
3000 unsigned long error
; /* Error code */
3002 while ((error
= ERR_get_error()) != 0)
3003 DEBUG_printf(("8http_setup_ssl: %s", ERR_error_string(error
, NULL
)));
3006 SSL_CTX_free(context
);
3010 http
->error
= WSAGetLastError();
3012 http
->error
= errno
;
3014 http
->status
= HTTP_ERROR
;
3016 return (HTTP_ERROR
);
3019 # elif defined(HAVE_GNUTLS)
3020 if ((conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
))) == NULL
)
3022 http
->error
= errno
;
3023 http
->status
= HTTP_ERROR
;
3028 credentials
= (gnutls_certificate_client_credentials
*)
3029 malloc(sizeof(gnutls_certificate_client_credentials
));
3030 if (credentials
== NULL
)
3034 http
->error
= errno
;
3035 http
->status
= HTTP_ERROR
;
3040 gnutls_certificate_allocate_credentials(credentials
);
3042 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
3043 gnutls_set_default_priority(conn
->session
);
3044 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3045 gnutls_transport_set_ptr(conn
->session
, (gnutls_transport_ptr
)http
);
3046 gnutls_transport_set_pull_function(conn
->session
, _httpReadGNUTLS
);
3047 gnutls_transport_set_push_function(conn
->session
, _httpWriteGNUTLS
);
3049 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
3051 http
->error
= errno
;
3052 http
->status
= HTTP_ERROR
;
3054 gnutls_deinit(conn
->session
);
3055 gnutls_certificate_free_credentials(*credentials
);
3062 conn
->credentials
= credentials
;
3064 # elif defined(HAVE_CDSASSL)
3065 conn
= (http_tls_t
*)calloc(1, sizeof(http_tls_t
));
3070 if ((error
= SSLNewContext(false, &conn
->session
)))
3072 http
->error
= error
;
3073 http
->status
= HTTP_ERROR
;
3080 * Use a union to resolve warnings about int/pointer size mismatches...
3083 error
= SSLSetConnection(conn
->session
, http
);
3086 error
= SSLSetIOFuncs(conn
->session
, _httpReadCDSA
, _httpWriteCDSA
);
3089 error
= SSLSetAllowsExpiredCerts(conn
->session
, true);
3092 error
= SSLSetAllowsAnyRoot(conn
->session
, true);
3095 error
= SSLSetProtocolVersionEnabled(conn
->session
, kSSLProtocol2
, false);
3099 while ((error
= SSLHandshake(conn
->session
)) == errSSLWouldBlock
)
3105 http
->error
= error
;
3106 http
->status
= HTTP_ERROR
;
3108 SSLDisposeContext(conn
->session
);
3114 # endif /* HAVE_CDSASSL */
3119 #endif /* HAVE_SSL */
3124 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
3128 http_shutdown_ssl(http_t
*http
) /* I - Connection to server */
3131 SSL_CTX
*context
; /* Context for encryption */
3132 SSL
*conn
; /* Connection for encryption */
3135 conn
= (SSL
*)(http
->tls
);
3136 context
= SSL_get_SSL_CTX(conn
);
3139 SSL_CTX_free(context
);
3142 # elif defined(HAVE_GNUTLS)
3143 http_tls_t
*conn
; /* Encryption session */
3144 gnutls_certificate_client_credentials
*credentials
;
3145 /* TLS credentials */
3148 conn
= (http_tls_t
*)(http
->tls
);
3149 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
3151 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
3152 gnutls_deinit(conn
->session
);
3153 gnutls_certificate_free_credentials(*credentials
);
3157 # elif defined(HAVE_CDSASSL)
3158 http_tls_t
*conn
; /* CDSA connection information */
3161 conn
= (http_tls_t
*)(http
->tls
);
3163 while (SSLClose(conn
->session
) == errSSLWouldBlock
)
3166 SSLDisposeContext(conn
->session
);
3168 if (conn
->certsArray
)
3169 CFRelease(conn
->certsArray
);
3172 # endif /* HAVE_LIBSSL */
3176 #endif /* HAVE_SSL */
3181 * 'http_upgrade()' - Force upgrade to TLS encryption.
3184 static int /* O - Status of connection */
3185 http_upgrade(http_t
*http
) /* I - Connection to server */
3187 int ret
; /* Return value */
3188 http_t myhttp
; /* Local copy of HTTP data */
3191 DEBUG_printf(("7http_upgrade(%p)", http
));
3194 * Flush the connection to make sure any previous "Upgrade" message
3201 * Copy the HTTP data to a local variable so we can do the OPTIONS
3202 * request without interfering with the existing request data...
3205 memcpy(&myhttp
, http
, sizeof(myhttp
));
3208 * Send an OPTIONS request to the server, requiring SSL or TLS
3209 * encryption on the link...
3212 http
->field_authorization
= NULL
; /* Don't free the auth string */
3214 httpClearFields(http
);
3215 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
3216 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
3218 if ((ret
= httpOptions(http
, "*")) == 0)
3221 * Wait for the secure connection...
3224 while (httpUpdate(http
) == HTTP_CONTINUE
);
3228 * Restore the HTTP request data...
3231 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
3232 http
->data_encoding
= myhttp
.data_encoding
;
3233 http
->data_remaining
= myhttp
.data_remaining
;
3234 http
->_data_remaining
= myhttp
._data_remaining
;
3235 http
->expect
= myhttp
.expect
;
3236 http
->field_authorization
= myhttp
.field_authorization
;
3237 http
->digest_tries
= myhttp
.digest_tries
;
3240 * See if we actually went secure...
3246 * Server does not support HTTP upgrade...
3249 DEBUG_puts("8http_upgrade: Server does not support HTTP upgrade!");
3252 closesocket(http
->fd
);
3264 #endif /* HAVE_SSL */
3268 * 'http_write()' - Write a buffer to a HTTP connection.
3271 static int /* O - Number of bytes written */
3272 http_write(http_t
*http
, /* I - Connection to server */
3273 const char *buffer
, /* I - Buffer for data */
3274 int length
) /* I - Number of bytes to write */
3276 int tbytes
, /* Total bytes sent */
3277 bytes
; /* Bytes sent */
3280 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=%d)", http
, buffer
,
3289 bytes
= http_write_ssl(http
, buffer
, length
);
3291 #endif /* HAVE_SSL */
3292 bytes
= send(http
->fd
, buffer
, length
, 0);
3297 if (WSAGetLastError() != http
->error
)
3299 http
->error
= WSAGetLastError();
3303 if (errno
== EINTR
|| errno
== EAGAIN
)
3305 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
3307 http
->error
= errno
;
3312 DEBUG_printf(("3http_write: error writing data (%s).",
3313 strerror(http
->error
)));
3324 http_debug_hex("http_write", buffer
- tbytes
, tbytes
);
3327 DEBUG_printf(("3http_write: Returning %d.", tbytes
));
3334 * 'http_write_chunk()' - Write a chunked buffer.
3337 static int /* O - Number bytes written */
3338 http_write_chunk(http_t
*http
, /* I - Connection to server */
3339 const char *buffer
, /* I - Buffer to write */
3340 int length
) /* I - Length of buffer */
3342 char header
[255]; /* Chunk header */
3343 int bytes
; /* Bytes written */
3346 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=%d)",
3347 http
, buffer
, length
));
3350 * Write the chunk header, data, and trailer.
3353 sprintf(header
, "%x\r\n", length
);
3354 if (http_write(http
, header
, (int)strlen(header
)) < 0)
3356 DEBUG_puts("8http_write_chunk: http_write of length failed!");
3360 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
3362 DEBUG_puts("8http_write_chunk: http_write of buffer failed!");
3366 if (http_write(http
, "\r\n", 2) < 0)
3368 DEBUG_puts("8http_write_chunk: http_write of CR LF failed!");
3378 * 'http_write_ssl()' - Write to a SSL/TLS connection.
3381 static int /* O - Bytes written */
3382 http_write_ssl(http_t
*http
, /* I - Connection to server */
3383 const char *buf
, /* I - Buffer holding data */
3384 int len
) /* I - Length of buffer */
3386 ssize_t result
; /* Return value */
3389 DEBUG_printf(("2http_write_ssl(http=%p, buf=%p, len=%d)", http
, buf
, len
));
3391 # if defined(HAVE_LIBSSL)
3392 result
= SSL_write((SSL
*)(http
->tls
), buf
, len
);
3394 # elif defined(HAVE_GNUTLS)
3395 result
= gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
);
3397 if (result
< 0 && !errno
)
3400 * Convert GNU TLS error to errno value...
3405 case GNUTLS_E_INTERRUPTED
:
3409 case GNUTLS_E_AGAIN
:
3421 # elif defined(HAVE_CDSASSL)
3422 OSStatus error
; /* Error info */
3423 size_t processed
; /* Number of bytes processed */
3426 error
= SSLWrite(((http_tls_t
*)http
->tls
)->session
, buf
, len
, &processed
);
3431 result
= (int)processed
;
3433 case errSSLClosedGraceful
:
3436 case errSSLWouldBlock
:
3438 result
= (int)processed
;
3450 # endif /* HAVE_LIBSSL */
3452 DEBUG_printf(("3http_write_ssl: Returning %d.", (int)result
));
3454 return ((int)result
);
3456 #endif /* HAVE_SSL */
3460 * End of "$Id: http.c 7850 2008-08-20 00:07:25Z mike $".