2 * "$Id: http.c 7850 2008-08-20 00:07:25Z mike $"
4 * HTTP routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * httpConnect() - Connect to a HTTP server.
30 * httpConnectEncrypt() - Connect to a HTTP server using encryption.
31 * _httpCreate() - Create an unconnected HTTP connection.
32 * httpDelete() - Send a DELETE request to the server.
33 * httpEncryption() - Set the required encryption on the link.
34 * httpError() - Get the last error on a connection.
35 * httpFlush() - Flush data from a HTTP connection.
36 * httpFlushWrite() - Flush data in write buffer.
37 * httpGet() - Send a GET request to the server.
38 * httpGetAuthString() - Get the current authorization string.
39 * httpGetBlocking() - Get the blocking/non-block state of a connection.
40 * httpGetCookie() - Get any cookie data from the response.
41 * httpGetFd() - Get the file descriptor associated with a
43 * httpGetField() - Get a field value from a request/response.
44 * httpGetLength() - Get the amount of data remaining from the
45 * content-length or transfer-encoding fields.
46 * httpGetLength2() - Get the amount of data remaining from the
47 * content-length or transfer-encoding fields.
48 * httpGetStatus() - Get the status of the last HTTP request.
49 * httpGetSubField() - Get a sub-field value.
50 * httpGetSubField2() - Get a sub-field value.
51 * httpGets() - Get a line of text from a HTTP connection.
52 * httpHead() - Send a HEAD request to the server.
53 * httpInitialize() - Initialize the HTTP interface library and set the
54 * default HTTP proxy (if any).
55 * httpOptions() - Send an OPTIONS request to the server.
56 * httpPost() - Send a POST request to the server.
57 * httpPrintf() - Print a formatted string to a HTTP connection.
58 * httpPut() - Send a PUT request to the server.
59 * httpRead() - Read data from a HTTP connection.
60 * httpRead2() - Read data from a HTTP connection.
61 * _httpReadCDSA() - Read function for the CDSA library.
62 * _httpReadGNUTLS() - Read function for the GNU TLS library.
63 * httpReconnect() - Reconnect to a HTTP server.
64 * httpSetAuthString() - Set the current authorization string.
65 * httpSetCookie() - Set the cookie value(s)...
66 * httpSetExpect() - Set the Expect: header in a request.
67 * httpSetField() - Set the value of an HTTP header.
68 * httpSetLength() - Set the content-length and content-encoding.
69 * httpTrace() - Send an TRACE request to the server.
70 * httpUpdate() - Update the current HTTP state for incoming data.
71 * _httpWait() - Wait for data available on a connection (no flush).
72 * httpWait() - Wait for data available on a connection.
73 * httpWrite() - Write data to a HTTP connection.
74 * httpWrite2() - Write data to a HTTP connection.
75 * _httpWriteCDSA() - Write function for the CDSA library.
76 * _httpWriteGNUTLS() - Write function for the GNU TLS library.
77 * http_bio_ctrl() - Control the HTTP connection.
78 * http_bio_free() - Free OpenSSL data.
79 * http_bio_new() - Initialize an OpenSSL BIO structure.
80 * http_bio_puts() - Send a string for OpenSSL.
81 * http_bio_read() - Read data for OpenSSL.
82 * http_bio_write() - Write data for OpenSSL.
83 * http_debug_hex() - Do a hex dump of a buffer.
84 * http_field() - Return the field index for a field name.
85 * http_read_ssl() - Read from a SSL/TLS connection.
86 * http_send() - Send a request with all fields and the trailing
88 * http_setup_ssl() - Set up SSL/TLS support on a connection.
89 * http_shutdown_ssl() - Shut down SSL/TLS on a connection.
90 * http_upgrade() - Force upgrade to TLS encryption.
91 * http_write() - Write a buffer to a HTTP connection.
92 * http_write_chunk() - Write a chunked buffer.
93 * http_write_ssl() - Write to a SSL/TLS connection.
97 * Include necessary headers...
100 #include "http-private.h"
108 # include <sys/time.h>
109 # include <sys/resource.h>
112 # include <sys/poll.h>
113 #endif /* HAVE_POLL */
117 * Some operating systems have done away with the Fxxxx constants for
118 * the fcntl() call; this works around that "feature"...
122 # define FNONBLK O_NONBLOCK
123 #endif /* !FNONBLK */
131 static void http_debug_hex(const char *prefix
, const char *buffer
,
134 static http_field_t
http_field(const char *name
);
135 static int http_send(http_t
*http
, http_state_t request
,
137 static int http_write(http_t
*http
, const char *buffer
,
139 static int http_write_chunk(http_t
*http
, const char *buffer
,
142 static int http_read_ssl(http_t
*http
, char *buf
, int len
);
143 static int http_setup_ssl(http_t
*http
);
144 static void http_shutdown_ssl(http_t
*http
);
145 static int http_upgrade(http_t
*http
);
146 static int http_write_ssl(http_t
*http
, const char *buf
, int len
);
147 #endif /* HAVE_SSL */
154 static const char * const http_fields
[] =
171 "If-Unmodified-since",
185 static const char * const http_states
[] =
205 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
207 * BIO methods for OpenSSL...
210 static int http_bio_write(BIO
*h
, const char *buf
, int num
);
211 static int http_bio_read(BIO
*h
, char *buf
, int size
);
212 static int http_bio_puts(BIO
*h
, const char *str
);
213 static long http_bio_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
214 static int http_bio_new(BIO
*h
);
215 static int http_bio_free(BIO
*data
);
217 static BIO_METHOD http_bio_methods
=
224 NULL
, /* http_bio_gets, */
233 * '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections.
236 BIO_METHOD
* /* O - BIO methods for OpenSSL */
237 _httpBIOMethods(void)
239 return (&http_bio_methods
);
241 #endif /* HAVE_SSL && HAVE_LIBSSL */
245 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
249 httpBlocking(http_t
*http
, /* I - Connection to server */
250 int b
) /* I - 1 = blocking, 0 = non-blocking */
258 * 'httpCheck()' - Check to see if there is a pending response from the server.
261 int /* O - 0 = no data, 1 = data available */
262 httpCheck(http_t
*http
) /* I - Connection to server */
264 return (httpWait(http
, 0));
269 * 'httpClearCookie()' - Clear the cookie value(s).
271 * @since CUPS 1.1.19/Mac OS X 10.3@
275 httpClearCookie(http_t
*http
) /* I - Connection to server */
289 * 'httpClearFields()' - Clear HTTP request fields.
293 httpClearFields(http_t
*http
) /* I - Connection to server */
297 memset(http
->fields
, 0, sizeof(http
->fields
));
298 if (http
->hostname
[0] == '/')
299 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
301 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
303 if (http
->field_authorization
)
305 free(http
->field_authorization
);
306 http
->field_authorization
= NULL
;
309 http
->expect
= (http_status_t
)0;
315 * 'httpClose()' - Close an HTTP connection...
319 httpClose(http_t
*http
) /* I - Connection to server */
322 OM_uint32 minor_status
; /* Minor status code */
323 #endif /* HAVE_GSSAPI */
326 DEBUG_printf(("httpClose(http=%p)", http
));
331 httpAddrFreeList(http
->addrlist
);
338 http_shutdown_ssl(http
);
339 #endif /* HAVE_SSL */
342 closesocket(http
->fd
);
348 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
349 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
351 if (http
->gssname
!= GSS_C_NO_NAME
)
352 gss_release_name(&minor_status
, &http
->gssname
);
353 #endif /* HAVE_GSSAPI */
355 #ifdef HAVE_AUTHORIZATION_H
357 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
358 #endif /* HAVE_AUTHORIZATION_H */
360 httpClearFields(http
);
362 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
363 free(http
->authstring
);
370 * 'httpConnect()' - Connect to a HTTP server.
372 * This function is deprecated - use @link httpConnectEncrypt@ instead.
377 http_t
* /* O - New HTTP connection */
378 httpConnect(const char *host
, /* I - Host to connect to */
379 int port
) /* I - Port number */
381 return (httpConnectEncrypt(host
, port
, HTTP_ENCRYPT_IF_REQUESTED
));
386 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
389 http_t
* /* O - New HTTP connection */
391 const char *host
, /* I - Host to connect to */
392 int port
, /* I - Port number */
393 http_encryption_t encryption
) /* I - Type of encryption to use */
395 http_t
*http
; /* New HTTP connection */
398 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
399 host
, port
, encryption
));
402 * Create the HTTP structure...
405 if ((http
= _httpCreate(host
, port
, encryption
)) == NULL
)
409 * Connect to the remote system...
412 if (!httpReconnect(http
))
416 * Could not connect to any known address - bail out!
419 httpAddrFreeList(http
->addrlist
);
428 * '_httpCreate()' - Create an unconnected HTTP connection.
431 http_t
* /* O - HTTP connection */
433 const char *host
, /* I - Hostname */
434 int port
, /* I - Port number */
435 http_encryption_t encryption
) /* I - Encryption to use */
437 http_t
*http
; /* New HTTP connection */
438 http_addrlist_t
*addrlist
; /* Host address data */
439 char service
[255]; /* Service name */
442 DEBUG_printf(("4_httpCreate(host=\"%s\", port=%d, encryption=%d)",
443 host
, port
, encryption
));
454 sprintf(service
, "%d", port
);
456 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
460 * Allocate memory for the structure...
463 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
465 httpAddrFreeList(addrlist
);
470 * Initialize the HTTP data...
473 http
->activity
= time(NULL
);
474 http
->addrlist
= addrlist
;
478 http
->gssctx
= GSS_C_NO_CONTEXT
;
479 http
->gssname
= GSS_C_NO_NAME
;
480 #endif /* HAVE_GSSAPI */
481 http
->version
= HTTP_1_1
;
483 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
485 if (port
== 443) /* Always use encryption for https */
486 http
->encryption
= HTTP_ENCRYPT_ALWAYS
;
488 http
->encryption
= encryption
;
491 * Return the new structure...
499 * 'httpDelete()' - Send a DELETE request to the server.
502 int /* O - Status of call (0 = success) */
503 httpDelete(http_t
*http
, /* I - Connection to server */
504 const char *uri
) /* I - URI to delete */
506 return (http_send(http
, HTTP_DELETE
, uri
));
511 * 'httpEncryption()' - Set the required encryption on the link.
514 int /* O - -1 on error, 0 on success */
515 httpEncryption(http_t
*http
, /* I - Connection to server */
516 http_encryption_t e
) /* I - New encryption preference */
518 DEBUG_printf(("httpEncryption(http=%p, e=%d)", http
, e
));
524 http
->encryption
= e
;
526 if ((http
->encryption
== HTTP_ENCRYPT_ALWAYS
&& !http
->tls
) ||
527 (http
->encryption
== HTTP_ENCRYPT_NEVER
&& http
->tls
))
528 return (httpReconnect(http
));
529 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
530 return (http_upgrade(http
));
534 if (e
== HTTP_ENCRYPT_ALWAYS
|| e
== HTTP_ENCRYPT_REQUIRED
)
538 #endif /* HAVE_SSL */
543 * 'httpError()' - Get the last error on a connection.
546 int /* O - Error code (errno) value */
547 httpError(http_t
*http
) /* I - Connection to server */
550 return (http
->error
);
557 * 'httpFlush()' - Flush data from a HTTP connection.
561 httpFlush(http_t
*http
) /* I - Connection to server */
563 char buffer
[8192]; /* Junk buffer */
564 int blocking
; /* To block or not to block */
567 DEBUG_printf(("httpFlush(http=%p), state=%s", http
,
568 http_states
[http
->state
]));
571 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
574 blocking
= http
->blocking
;
578 * Read any data we can...
581 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
584 * Restore blocking and reset the connection if we didn't get all of
585 * the remaining data...
588 http
->blocking
= blocking
;
590 if (http
->state
!= HTTP_WAITING
&& http
->fd
>= 0)
593 * Didn't get the data back, so close the current connection.
596 http
->state
= HTTP_WAITING
;
600 http_shutdown_ssl(http
);
601 #endif /* HAVE_SSL */
604 closesocket(http
->fd
);
615 * 'httpFlushWrite()' - Flush data in write buffer.
617 * @since CUPS 1.2/Mac OS X 10.5@
620 int /* O - Bytes written or -1 on error */
621 httpFlushWrite(http_t
*http
) /* I - Connection to server */
623 int bytes
; /* Bytes written */
626 DEBUG_printf(("httpFlushWrite(http=%p)", http
));
628 if (!http
|| !http
->wused
)
631 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
632 bytes
= http_write_chunk(http
, http
->wbuffer
, http
->wused
);
634 bytes
= http_write(http
, http
->wbuffer
, http
->wused
);
643 * 'httpGet()' - Send a GET request to the server.
646 int /* O - Status of call (0 = success) */
647 httpGet(http_t
*http
, /* I - Connection to server */
648 const char *uri
) /* I - URI to get */
650 return (http_send(http
, HTTP_GET
, uri
));
655 * 'httpGetAuthString()' - Get the current authorization string.
657 * The authorization string is set by cupsDoAuthentication() and
658 * httpSetAuthString(). Use httpGetAuthString() to retrieve the
659 * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
662 * @since CUPS 1.3/Mac OS X 10.5@
665 char * /* O - Authorization string */
666 httpGetAuthString(http_t
*http
) /* I - Connection to server */
669 return (http
->authstring
);
676 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
678 * @since CUPS 1.2/Mac OS X 10.5@
681 int /* O - 1 if blocking, 0 if non-blocking */
682 httpGetBlocking(http_t
*http
) /* I - Connection to server */
684 return (http
? http
->blocking
: 0);
689 * 'httpGetCookie()' - Get any cookie data from the response.
691 * @since CUPS 1.1.19/Mac OS X 10.3@
694 const char * /* O - Cookie data or NULL */
695 httpGetCookie(http_t
*http
) /* I - HTTP connecion */
697 return (http
? http
->cookie
: NULL
);
702 * 'httpGetFd()' - Get the file descriptor associated with a connection.
704 * @since CUPS 1.2/Mac OS X 10.5@
707 int /* O - File descriptor or -1 if none */
708 httpGetFd(http_t
*http
) /* I - Connection to server */
710 return (http
? http
->fd
: -1);
715 * 'httpGetField()' - Get a field value from a request/response.
718 const char * /* O - Field value */
719 httpGetField(http_t
*http
, /* I - Connection to server */
720 http_field_t field
) /* I - Field to get */
722 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
724 else if (field
== HTTP_FIELD_AUTHORIZATION
&&
725 http
->field_authorization
)
728 * Special case for WWW-Authenticate: as its contents can be
729 * longer than HTTP_MAX_VALUE...
732 return (http
->field_authorization
);
735 return (http
->fields
[field
]);
740 * 'httpGetLength()' - Get the amount of data remaining from the
741 * content-length or transfer-encoding fields.
743 * This function is deprecated and will not return lengths larger than
744 * 2^31 - 1; use httpGetLength2() instead.
749 int /* O - Content length */
750 httpGetLength(http_t
*http
) /* I - Connection to server */
753 * Get the read content length and return the 32-bit value.
758 httpGetLength2(http
);
760 return (http
->_data_remaining
);
768 * 'httpGetLength2()' - Get the amount of data remaining from the
769 * content-length or transfer-encoding fields.
771 * This function returns the complete content length, even for
772 * content larger than 2^31 - 1.
774 * @since CUPS 1.2/Mac OS X 10.5@
777 off_t
/* O - Content length */
778 httpGetLength2(http_t
*http
) /* I - Connection to server */
780 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http
,
781 http_states
[http
->state
]));
786 if (!strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
788 DEBUG_puts("4httpGetLength2: chunked request!");
790 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
791 http
->data_remaining
= 0;
795 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
798 * The following is a hack for HTTP servers that don't send a
799 * content-length or transfer-encoding field...
801 * If there is no content-length then the connection must close
802 * after the transfer is complete...
805 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
808 * Default content length is 0 for errors and 2^31-1 for other
809 * successful requests...
812 if (http
->status
>= HTTP_MULTIPLE_CHOICES
)
813 http
->data_remaining
= 0;
815 http
->data_remaining
= 2147483647;
818 http
->data_remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
821 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
822 CUPS_LLCAST http
->data_remaining
));
825 if (http
->data_remaining
<= INT_MAX
)
826 http
->_data_remaining
= (int)http
->data_remaining
;
828 http
->_data_remaining
= INT_MAX
;
830 return (http
->data_remaining
);
835 * 'httpGetStatus()' - Get the status of the last HTTP request.
837 * @since CUPS 1.2/Mac OS X 10.5@
840 http_status_t
/* O - HTTP status */
841 httpGetStatus(http_t
*http
) /* I - Connection to server */
843 return (http
? http
->status
: HTTP_ERROR
);
848 * 'httpGetSubField()' - Get a sub-field value.
853 char * /* O - Value or NULL */
854 httpGetSubField(http_t
*http
, /* I - Connection to server */
855 http_field_t field
, /* I - Field index */
856 const char *name
, /* I - Name of sub-field */
857 char *value
) /* O - Value string */
859 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
864 * 'httpGetSubField2()' - Get a sub-field value.
866 * @since CUPS 1.2/Mac OS X 10.5@
869 char * /* O - Value or NULL */
870 httpGetSubField2(http_t
*http
, /* I - Connection to server */
871 http_field_t field
, /* I - Field index */
872 const char *name
, /* I - Name of sub-field */
873 char *value
, /* O - Value string */
874 int valuelen
) /* I - Size of value buffer */
876 const char *fptr
; /* Pointer into field */
877 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
878 *ptr
, /* Pointer into string buffer */
879 *end
; /* End of value buffer */
881 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, "
882 "valuelen=%d)", http
, field
, name
, value
, valuelen
));
884 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
885 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
888 end
= value
+ valuelen
- 1;
890 for (fptr
= http
->fields
[field
]; *fptr
;)
893 * Skip leading whitespace...
896 while (isspace(*fptr
& 255))
906 * Get the sub-field name...
910 *fptr
&& *fptr
!= '=' && !isspace(*fptr
& 255) &&
911 ptr
< (temp
+ sizeof(temp
) - 1);
916 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
919 * Skip trailing chars up to the '='...
922 while (isspace(*fptr
& 255))
932 * Skip = and leading whitespace...
937 while (isspace(*fptr
& 255))
943 * Read quoted string...
946 for (ptr
= value
, fptr
++;
947 *fptr
&& *fptr
!= '\"' && ptr
< end
;
952 while (*fptr
&& *fptr
!= '\"')
961 * Read unquoted string...
965 *fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',' && ptr
< end
;
970 while (*fptr
&& !isspace(*fptr
& 255) && *fptr
!= ',')
974 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
977 * See if this is the one...
980 if (!strcmp(name
, temp
))
982 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
989 DEBUG_puts("3httpGetSubField2: Returning NULL");
996 * 'httpGets()' - Get a line of text from a HTTP connection.
999 char * /* O - Line or NULL */
1000 httpGets(char *line
, /* I - Line to read into */
1001 int length
, /* I - Max length of buffer */
1002 http_t
*http
) /* I - Connection to server */
1004 char *lineptr
, /* Pointer into line */
1005 *lineend
, /* End of line */
1006 *bufptr
, /* Pointer into input buffer */
1007 *bufend
; /* Pointer to end of buffer */
1008 int bytes
, /* Number of bytes read */
1009 eol
; /* End-of-line? */
1012 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", line
, length
, http
));
1014 if (http
== NULL
|| line
== NULL
)
1018 * Read a line from the buffer...
1023 lineend
= line
+ length
- 1;
1026 while (lineptr
< lineend
)
1029 * Pre-load the buffer as needed...
1038 while (http
->used
== 0)
1041 * No newline; see if there is more data to be read...
1044 if (!http
->blocking
&& !_httpWait(http
, 10000, 1))
1046 DEBUG_puts("3httpGets: Timed out!");
1048 http
->error
= WSAETIMEDOUT
;
1050 http
->error
= ETIMEDOUT
;
1057 bytes
= http_read_ssl(http
, http
->buffer
+ http
->used
,
1058 HTTP_MAX_BUFFER
- http
->used
);
1060 #endif /* HAVE_SSL */
1061 bytes
= recv(http
->fd
, http
->buffer
+ http
->used
,
1062 HTTP_MAX_BUFFER
- http
->used
, 0);
1064 DEBUG_printf(("4httpGets: read %d bytes...", bytes
));
1069 * Nope, can't get a line this time...
1073 if (WSAGetLastError() != http
->error
)
1075 http
->error
= WSAGetLastError();
1079 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1081 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1083 if (errno
== EINTR
|| errno
== EAGAIN
)
1085 else if (errno
!= http
->error
)
1087 http
->error
= errno
;
1094 else if (bytes
== 0)
1096 http
->error
= EPIPE
;
1102 * Yup, update the amount used...
1105 http
->used
+= bytes
;
1109 * Now copy as much of the current line as possible...
1112 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1113 lineptr
< lineend
&& bufptr
< bufend
;)
1115 if (*bufptr
== 0x0a)
1121 else if (*bufptr
== 0x0d)
1124 *lineptr
++ = *bufptr
++;
1127 http
->used
-= (int)(bufptr
- http
->buffer
);
1129 memmove(http
->buffer
, bufptr
, http
->used
);
1137 http
->activity
= time(NULL
);
1141 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1147 DEBUG_puts("3httpGets: No new line available!");
1154 * 'httpHead()' - Send a HEAD request to the server.
1157 int /* O - Status of call (0 = success) */
1158 httpHead(http_t
*http
, /* I - Connection to server */
1159 const char *uri
) /* I - URI for head */
1161 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http
, uri
));
1162 return (http_send(http
, HTTP_HEAD
, uri
));
1167 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1168 * default HTTP proxy (if any).
1172 httpInitialize(void)
1176 struct timeval curtime
; /* Current time in microseconds */
1177 # endif /* !WIN32 */
1178 int i
; /* Looping var */
1179 unsigned char data
[1024]; /* Seed data */
1180 #endif /* HAVE_LIBSSL */
1183 WSADATA winsockdata
; /* WinSock data */
1184 static int initialized
= 0; /* Has WinSock been initialized? */
1188 WSAStartup(MAKEWORD(1,1), &winsockdata
);
1189 #elif !defined(SO_NOSIGPIPE)
1191 * Ignore SIGPIPE signals...
1195 sigset(SIGPIPE
, SIG_IGN
);
1196 # elif defined(HAVE_SIGACTION)
1197 struct sigaction action
; /* POSIX sigaction data */
1200 memset(&action
, 0, sizeof(action
));
1201 action
.sa_handler
= SIG_IGN
;
1202 sigaction(SIGPIPE
, &action
, NULL
);
1204 signal(SIGPIPE
, SIG_IGN
);
1205 # endif /* !SO_NOSIGPIPE */
1209 gnutls_global_init();
1210 #endif /* HAVE_GNUTLS */
1213 SSL_load_error_strings();
1217 * Using the current time is a dubious random seed, but on some systems
1218 * it is the best we can do (on others, this seed isn't even used...)
1223 gettimeofday(&curtime
, NULL
);
1224 srand(curtime
.tv_sec
+ curtime
.tv_usec
);
1227 for (i
= 0; i
< sizeof(data
); i
++)
1230 RAND_seed(data
, sizeof(data
));
1231 #endif /* HAVE_LIBSSL */
1236 * 'httpOptions()' - Send an OPTIONS request to the server.
1239 int /* O - Status of call (0 = success) */
1240 httpOptions(http_t
*http
, /* I - Connection to server */
1241 const char *uri
) /* I - URI for options */
1243 return (http_send(http
, HTTP_OPTIONS
, uri
));
1248 * 'httpPost()' - Send a POST request to the server.
1251 int /* O - Status of call (0 = success) */
1252 httpPost(http_t
*http
, /* I - Connection to server */
1253 const char *uri
) /* I - URI for post */
1255 return (http_send(http
, HTTP_POST
, uri
));
1260 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1265 int /* O - Number of bytes written */
1266 httpPrintf(http_t
*http
, /* I - Connection to server */
1267 const char *format
, /* I - printf-style format string */
1268 ...) /* I - Additional args as needed */
1270 int bytes
; /* Number of bytes to write */
1271 char buf
[16384]; /* Buffer for formatted string */
1272 va_list ap
; /* Variable argument pointer */
1275 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", http
, format
));
1277 va_start(ap
, format
);
1278 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1281 DEBUG_printf(("3httpPrintf: %s", buf
));
1283 if (http
->data_encoding
== HTTP_ENCODE_FIELDS
)
1284 return (httpWrite2(http
, buf
, bytes
));
1289 DEBUG_puts("4httpPrintf: flushing existing data...");
1291 if (httpFlushWrite(http
) < 0)
1295 return (http_write(http
, buf
, bytes
));
1301 * 'httpPut()' - Send a PUT request to the server.
1304 int /* O - Status of call (0 = success) */
1305 httpPut(http_t
*http
, /* I - Connection to server */
1306 const char *uri
) /* I - URI to put */
1308 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http
, uri
));
1309 return (http_send(http
, HTTP_PUT
, uri
));
1314 * 'httpRead()' - Read data from a HTTP connection.
1316 * This function is deprecated. Use the httpRead2() function which can
1317 * read more than 2GB of data.
1322 int /* O - Number of bytes read */
1323 httpRead(http_t
*http
, /* I - Connection to server */
1324 char *buffer
, /* I - Buffer for data */
1325 int length
) /* I - Maximum number of bytes */
1327 return ((int)httpRead2(http
, buffer
, length
));
1332 * 'httpRead2()' - Read data from a HTTP connection.
1334 * @since CUPS 1.2/Mac OS X 10.5@
1337 ssize_t
/* O - Number of bytes read */
1338 httpRead2(http_t
*http
, /* I - Connection to server */
1339 char *buffer
, /* I - Buffer for data */
1340 size_t length
) /* I - Maximum number of bytes */
1342 ssize_t bytes
; /* Bytes read */
1343 char len
[32]; /* Length string */
1346 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
")",
1347 http
, buffer
, CUPS_LLCAST length
));
1349 if (http
== NULL
|| buffer
== NULL
)
1352 http
->activity
= time(NULL
);
1358 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
1359 http
->data_remaining
<= 0)
1361 DEBUG_puts("2httpRead2: Getting chunk length...");
1363 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1365 DEBUG_puts("1httpRead2: Could not get length!");
1369 http
->data_remaining
= strtoll(len
, NULL
, 16);
1370 if (http
->data_remaining
< 0)
1372 DEBUG_puts("1httpRead2: Negative chunk length!");
1377 DEBUG_printf(("2httpRead2: data_remaining=" CUPS_LLFMT
,
1378 CUPS_LLCAST http
->data_remaining
));
1380 if (http
->data_remaining
<= 0)
1383 * A zero-length chunk ends a transfer; unless we are reading POST
1387 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1388 httpGets(len
, sizeof(len
), http
);
1390 if (http
->state
== HTTP_POST_RECV
)
1393 http
->state
= HTTP_WAITING
;
1396 * Prevent future reads for this request...
1399 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1403 else if (length
> (size_t)http
->data_remaining
)
1404 length
= (size_t)http
->data_remaining
;
1406 if (http
->used
== 0 && length
<= 256)
1409 * Buffer small reads for better performance...
1412 if (!http
->blocking
&& !httpWait(http
, 10000))
1415 if (http
->data_remaining
> sizeof(http
->buffer
))
1416 bytes
= sizeof(http
->buffer
);
1418 bytes
= http
->data_remaining
;
1422 bytes
= http_read_ssl(http
, http
->buffer
, bytes
);
1424 #endif /* HAVE_SSL */
1426 DEBUG_printf(("2httpRead2: reading %d bytes from socket into buffer...",
1429 bytes
= recv(http
->fd
, http
->buffer
, bytes
, 0);
1431 DEBUG_printf(("2httpRead2: read %d bytes from socket into buffer...",
1440 http
->error
= WSAGetLastError();
1443 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1445 http
->error
= errno
;
1452 http
->error
= EPIPE
;
1459 if (length
> (size_t)http
->used
)
1460 length
= (size_t)http
->used
;
1462 bytes
= (ssize_t
)length
;
1464 DEBUG_printf(("2httpRead2: grabbing %d bytes from input buffer...",
1467 memcpy(buffer
, http
->buffer
, length
);
1468 http
->used
-= (int)length
;
1471 memmove(http
->buffer
, http
->buffer
+ length
, http
->used
);
1476 if (!http
->blocking
&& !httpWait(http
, 10000))
1479 bytes
= (ssize_t
)http_read_ssl(http
, buffer
, (int)length
);
1481 #endif /* HAVE_SSL */
1484 if (!http
->blocking
&& !httpWait(http
, 10000))
1487 DEBUG_printf(("2httpRead2: reading " CUPS_LLFMT
" bytes from socket...",
1488 CUPS_LLCAST length
));
1491 bytes
= (ssize_t
)recv(http
->fd
, buffer
, (int)length
, 0);
1493 while ((bytes
= recv(http
->fd
, buffer
, length
, 0)) < 0)
1494 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1498 DEBUG_printf(("2httpRead2: read " CUPS_LLFMT
" bytes from socket...",
1499 CUPS_LLCAST bytes
));
1504 http
->data_remaining
-= bytes
;
1506 if (http
->data_remaining
<= INT_MAX
)
1507 http
->_data_remaining
= (int)http
->data_remaining
;
1509 http
->_data_remaining
= INT_MAX
;
1514 http
->error
= WSAGetLastError();
1516 if (errno
== EINTR
|| errno
== EAGAIN
)
1519 http
->error
= errno
;
1524 http
->error
= EPIPE
;
1528 if (http
->data_remaining
== 0)
1530 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
1531 httpGets(len
, sizeof(len
), http
);
1533 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
1535 if (http
->state
== HTTP_POST_RECV
)
1538 http
->state
= HTTP_WAITING
;
1543 http_debug_hex("httpRead2", buffer
, (int)bytes
);
1550 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
1552 * '_httpReadCDSA()' - Read function for the CDSA library.
1555 OSStatus
/* O - -1 on error, 0 on success */
1557 SSLConnectionRef connection
, /* I - SSL/TLS connection */
1558 void *data
, /* I - Data buffer */
1559 size_t *dataLength
) /* IO - Number of bytes */
1561 OSStatus result
; /* Return value */
1562 ssize_t bytes
; /* Number of bytes read */
1563 http_t
*http
; /* HTTP connection */
1566 http
= (http_t
*)connection
;
1568 if (!http
->blocking
)
1571 * Make sure we have data before we read...
1574 if (!_httpWait(http
, 10000, 0))
1576 http
->error
= ETIMEDOUT
;
1583 bytes
= recv(http
->fd
, data
, *dataLength
, 0);
1585 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1587 if (bytes
== *dataLength
)
1593 *dataLength
= bytes
;
1594 result
= errSSLWouldBlock
;
1601 result
= errSSLClosedGraceful
;
1602 else if (errno
== EAGAIN
)
1603 result
= errSSLWouldBlock
;
1605 result
= errSSLClosedAbort
;
1610 #endif /* HAVE_SSL && HAVE_CDSASSL */
1613 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
1615 * '_httpReadGNUTLS()' - Read function for the GNU TLS library.
1618 ssize_t
/* O - Number of bytes read or -1 on error */
1620 gnutls_transport_ptr ptr
, /* I - Connection to server */
1621 void *data
, /* I - Buffer */
1622 size_t length
) /* I - Number of bytes to read */
1624 http_t
*http
; /* HTTP connection */
1627 http
= (http_t
*)ptr
;
1629 if (!http
->blocking
)
1632 * Make sure we have data before we read...
1635 if (!_httpWait(http
, 10000, 0))
1637 http
->error
= ETIMEDOUT
;
1642 return (recv(http
->fd
, data
, length
, 0));
1644 #endif /* HAVE_SSL && HAVE_GNUTLS */
1648 * 'httpReconnect()' - Reconnect to a HTTP server.
1651 int /* O - 0 on success, non-zero on failure */
1652 httpReconnect(http_t
*http
) /* I - Connection to server */
1654 http_addrlist_t
*addr
; /* Connected address */
1656 http_addrlist_t
*current
; /* Current address */
1657 char temp
[256]; /* Temporary address string */
1661 DEBUG_printf(("httpReconnect(http=%p)", http
));
1669 DEBUG_puts("2httpReconnect: Shutting down SSL/TLS...");
1670 http_shutdown_ssl(http
);
1672 #endif /* HAVE_SSL */
1675 * Close any previously open socket...
1680 DEBUG_printf(("2httpReconnect: Closing socket %d...", http
->fd
));
1683 closesocket(http
->fd
);
1692 * Connect to the server...
1696 for (current
= http
->addrlist
; current
; current
= current
->next
)
1697 DEBUG_printf(("2httpReconnect: Address %s:%d",
1698 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
1699 _httpAddrPort(&(current
->addr
))));
1702 if ((addr
= httpAddrConnect(http
->addrlist
, &(http
->fd
))) == NULL
)
1705 * Unable to connect...
1709 http
->error
= WSAGetLastError();
1711 http
->error
= errno
;
1713 http
->status
= HTTP_ERROR
;
1715 DEBUG_printf(("1httpReconnect: httpAddrConnect failed: %s",
1716 strerror(http
->error
)));
1721 DEBUG_printf(("2httpReconnect: New socket=%d", http
->fd
));
1723 http
->hostaddr
= &(addr
->addr
);
1725 http
->status
= HTTP_CONTINUE
;
1728 if (http
->encryption
== HTTP_ENCRYPT_ALWAYS
)
1731 * Always do encryption via SSL.
1734 if (http_setup_ssl(http
) != 0)
1737 closesocket(http
->fd
);
1745 else if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
)
1746 return (http_upgrade(http
));
1747 #endif /* HAVE_SSL */
1749 DEBUG_printf(("1httpReconnect: Connected to %s:%d...",
1750 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
1751 _httpAddrPort(http
->hostaddr
)));
1758 * 'httpSetAuthString()' - Set the current authorization string.
1760 * This function just stores a copy of the current authorization string in
1761 * the HTTP connection object. You must still call httpSetField() to set
1762 * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
1763 * httpHead(), httpOptions(), httpPost, or httpPut().
1765 * @since CUPS 1.3/Mac OS X 10.5@
1769 httpSetAuthString(http_t
*http
, /* I - Connection to server */
1770 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
1771 const char *data
) /* I - Auth data (NULL for none) */
1774 * Range check input...
1780 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
1781 free(http
->authstring
);
1783 http
->authstring
= http
->_authstring
;
1788 * Set the current authorization string...
1791 int len
= (int)strlen(scheme
) + (data
? (int)strlen(data
) + 1 : 0) + 1;
1794 if (len
> (int)sizeof(http
->_authstring
))
1796 if ((temp
= malloc(len
)) == NULL
)
1797 len
= sizeof(http
->_authstring
);
1799 http
->authstring
= temp
;
1803 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
1805 strlcpy(http
->authstring
, scheme
, len
);
1810 * Clear the current authorization string...
1813 http
->_authstring
[0] = '\0';
1819 * 'httpSetCookie()' - Set the cookie value(s)...
1821 * @since CUPS 1.1.19/Mac OS X 10.3@
1825 httpSetCookie(http_t
*http
, /* I - Connection */
1826 const char *cookie
) /* I - Cookie string */
1835 http
->cookie
= strdup(cookie
);
1837 http
->cookie
= NULL
;
1842 * 'httpSetExpect()' - Set the Expect: header in a request.
1844 * Currently only HTTP_CONTINUE is supported for the "expect" argument.
1846 * @since CUPS 1.2/Mac OS X 10.5@
1850 httpSetExpect(http_t
*http
, /* I - Connection to server */
1851 http_status_t expect
) /* I - HTTP status to expect (HTTP_CONTINUE) */
1854 http
->expect
= expect
;
1859 * 'httpSetField()' - Set the value of an HTTP header.
1863 httpSetField(http_t
*http
, /* I - Connection to server */
1864 http_field_t field
, /* I - Field index */
1865 const char *value
) /* I - Value */
1868 field
< HTTP_FIELD_ACCEPT_LANGUAGE
||
1869 field
> HTTP_FIELD_WWW_AUTHENTICATE
||
1873 strlcpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
);
1875 if (field
== HTTP_FIELD_AUTHORIZATION
)
1878 * Special case for Authorization: as its contents can be
1879 * longer than HTTP_MAX_VALUE
1882 if (http
->field_authorization
)
1883 free(http
->field_authorization
);
1885 http
->field_authorization
= strdup(value
);
1887 else if (field
== HTTP_FIELD_HOST
)
1890 * Special-case for Host: as we don't want a trailing "." on the hostname and
1891 * need to bracket IPv6 numeric addresses.
1894 char *ptr
= strchr(value
, ':');
1896 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
1899 * Bracket IPv6 numeric addresses...
1901 * This is slightly inefficient (basically copying twice), but is an edge
1902 * case and not worth optimizing...
1905 snprintf(http
->fields
[HTTP_FIELD_HOST
],
1906 sizeof(http
->fields
[HTTP_FIELD_HOST
]), "[%s]", value
);
1911 * Check for a trailing dot on the hostname...
1914 ptr
= http
->fields
[HTTP_FIELD_HOST
];
1918 ptr
+= strlen(ptr
) - 1;
1929 * 'httpSetLength()' - Set the content-length and content-encoding.
1931 * @since CUPS 1.2/Mac OS X 10.5@
1935 httpSetLength(http_t
*http
, /* I - Connection to server */
1936 size_t length
) /* I - Length (0 for chunked) */
1943 strcpy(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked");
1944 http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] = '\0';
1948 http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
][0] = '\0';
1949 snprintf(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
], HTTP_MAX_VALUE
,
1950 CUPS_LLFMT
, CUPS_LLCAST length
);
1956 * 'httpTrace()' - Send an TRACE request to the server.
1959 int /* O - Status of call (0 = success) */
1960 httpTrace(http_t
*http
, /* I - Connection to server */
1961 const char *uri
) /* I - URI for trace */
1963 return (http_send(http
, HTTP_TRACE
, uri
));
1968 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1971 http_status_t
/* O - HTTP status */
1972 httpUpdate(http_t
*http
) /* I - Connection to server */
1974 char line
[32768], /* Line from connection... */
1975 *value
; /* Pointer to value on line */
1976 http_field_t field
; /* Field index */
1977 int major
, minor
, /* HTTP version numbers */
1978 status
; /* Request status */
1981 DEBUG_printf(("httpUpdate(http=%p), state=%s", http
,
1982 http_states
[http
->state
]));
1985 * Flush pending data, if any...
1990 DEBUG_puts("2httpUpdate: flushing buffer...");
1992 if (httpFlushWrite(http
) < 0)
1993 return (HTTP_ERROR
);
1997 * If we haven't issued any commands, then there is nothing to "update"...
2000 if (http
->state
== HTTP_WAITING
)
2001 return (HTTP_CONTINUE
);
2004 * Grab all of the lines we can from the connection...
2007 while (httpGets(line
, sizeof(line
), http
) != NULL
)
2009 DEBUG_printf(("2httpUpdate: Got \"%s\"", line
));
2011 if (line
[0] == '\0')
2014 * Blank line means the start of the data section (if any). Return
2015 * the result code, too...
2017 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
2018 * Instead, we just return HTTP_CONTINUE to the caller and keep on
2022 if (http
->status
== HTTP_CONTINUE
)
2023 return (http
->status
);
2025 if (http
->status
< HTTP_BAD_REQUEST
)
2026 http
->digest_tries
= 0;
2029 if (http
->status
== HTTP_SWITCHING_PROTOCOLS
&& !http
->tls
)
2031 if (http_setup_ssl(http
) != 0)
2034 closesocket(http
->fd
);
2039 return (HTTP_ERROR
);
2042 return (HTTP_CONTINUE
);
2044 #endif /* HAVE_SSL */
2046 httpGetLength2(http
);
2048 switch (http
->state
)
2052 case HTTP_POST_RECV
:
2055 case HTTP_POST_SEND
:
2060 http
->state
= HTTP_WAITING
;
2064 return (http
->status
);
2066 else if (!strncmp(line
, "HTTP/", 5))
2069 * Got the beginning of a response...
2072 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
2073 return (HTTP_ERROR
);
2075 http
->version
= (http_version_t
)(major
* 100 + minor
);
2076 http
->status
= (http_status_t
)status
;
2078 else if ((value
= strchr(line
, ':')) != NULL
)
2085 while (isspace(*value
& 255))
2089 * Be tolerants of servers that send unknown attribute fields...
2092 if (!strcasecmp(line
, "expect"))
2095 * "Expect: 100-continue" or similar...
2098 http
->expect
= (http_status_t
)atoi(value
);
2100 else if (!strcasecmp(line
, "cookie"))
2103 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2106 httpSetCookie(http
, value
);
2108 else if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
2110 DEBUG_printf(("1httpUpdate: unknown field %s seen!", line
));
2114 httpSetField(http
, field
, value
);
2118 DEBUG_printf(("1httpUpdate: Bad response line \"%s\"!", line
));
2119 http
->status
= HTTP_ERROR
;
2120 return (HTTP_ERROR
);
2125 * See if there was an error...
2128 if (http
->error
== EPIPE
&& http
->status
> HTTP_CONTINUE
)
2130 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
2131 return (http
->status
);
2136 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
2137 strerror(http
->error
)));
2138 http
->status
= HTTP_ERROR
;
2139 return (HTTP_ERROR
);
2143 * If we haven't already returned, then there is nothing new...
2146 return (HTTP_CONTINUE
);
2151 * '_httpWait()' - Wait for data available on a connection (no flush).
2154 int /* O - 1 if data is available, 0 otherwise */
2155 _httpWait(http_t
*http
, /* I - Connection to server */
2156 int msec
, /* I - Milliseconds to wait */
2157 int usessl
) /* I - Use SSL context? */
2160 struct pollfd pfd
; /* Polled file descriptor */
2162 fd_set input_set
; /* select() input set */
2163 struct timeval timeout
; /* Timeout */
2164 #endif /* HAVE_POLL */
2165 int nfds
; /* Result from select()/poll() */
2168 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", http
, msec
, usessl
));
2174 * Check the SSL/TLS buffers for data first...
2178 if (http
->tls
&& usessl
)
2181 if (SSL_pending((SSL
*)(http
->tls
)))
2183 # elif defined(HAVE_GNUTLS)
2184 if (gnutls_record_check_pending(((http_tls_t
*)(http
->tls
))->session
))
2186 # elif defined(HAVE_CDSASSL)
2187 size_t bytes
; /* Bytes that are available */
2189 if (!SSLGetBufferedReadSize(((http_tls_t
*)(http
->tls
))->session
, &bytes
) &&
2192 # endif /* HAVE_LIBSSL */
2194 #endif /* HAVE_SSL */
2197 * Then try doing a select() or poll() to poll the socket...
2202 pfd
.events
= POLLIN
;
2204 while ((nfds
= poll(&pfd
, 1, msec
)) < 0 &&
2205 (errno
== EINTR
|| errno
== EAGAIN
));
2210 FD_ZERO(&input_set
);
2211 FD_SET(http
->fd
, &input_set
);
2213 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
2217 timeout
.tv_sec
= msec
/ 1000;
2218 timeout
.tv_usec
= (msec
% 1000) * 1000;
2220 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
2223 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
2225 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
2228 while (nfds
< 0 && WSAGetLastError() == WSAEINTR
);
2230 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
2232 #endif /* HAVE_POLL */
2234 DEBUG_printf(("5_httpWait: returning with nfds=%d...", nfds
));
2241 * 'httpWait()' - Wait for data available on a connection.
2243 * @since CUPS 1.1.19/Mac OS X 10.3@
2246 int /* O - 1 if data is available, 0 otherwise */
2247 httpWait(http_t
*http
, /* I - Connection to server */
2248 int msec
) /* I - Milliseconds to wait */
2251 * First see if there is data in the buffer...
2261 * Flush pending data, if any...
2266 if (httpFlushWrite(http
) < 0)
2271 * If not, check the SSL/TLS buffers and do a select() on the connection...
2274 return (_httpWait(http
, msec
, 1));
2279 * 'httpWrite()' - Write data to a HTTP connection.
2281 * This function is deprecated. Use the httpWrite2() function which can
2282 * write more than 2GB of data.
2287 int /* O - Number of bytes written */
2288 httpWrite(http_t
*http
, /* I - Connection to server */
2289 const char *buffer
, /* I - Buffer for data */
2290 int length
) /* I - Number of bytes to write */
2292 return ((int)httpWrite2(http
, buffer
, length
));
2297 * 'httpWrite2()' - Write data to a HTTP connection.
2299 * @since CUPS 1.2/Mac OS X 10.5@
2302 ssize_t
/* O - Number of bytes written */
2303 httpWrite2(http_t
*http
, /* I - Connection to server */
2304 const char *buffer
, /* I - Buffer for data */
2305 size_t length
) /* I - Number of bytes to write */
2307 ssize_t bytes
; /* Bytes written */
2310 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", http
,
2311 buffer
, CUPS_LLCAST length
));
2314 * Range check input...
2317 if (http
== NULL
|| buffer
== NULL
)
2321 * Mark activity on the connection...
2324 http
->activity
= time(NULL
);
2327 * Buffer small writes for better performance...
2332 if (http
->wused
&& (length
+ http
->wused
) > sizeof(http
->wbuffer
))
2334 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
2335 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
2337 httpFlushWrite(http
);
2340 if ((length
+ http
->wused
) <= sizeof(http
->wbuffer
) &&
2341 length
< sizeof(http
->wbuffer
))
2344 * Write to buffer...
2347 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
2348 CUPS_LLCAST length
));
2350 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
2351 http
->wused
+= (int)length
;
2352 bytes
= (ssize_t
)length
;
2357 * Otherwise write the data directly...
2360 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
2361 CUPS_LLCAST length
));
2363 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2364 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, (int)length
);
2366 bytes
= (ssize_t
)http_write(http
, buffer
, (int)length
);
2368 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
2369 CUPS_LLCAST bytes
));
2372 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2373 http
->data_remaining
-= bytes
;
2379 * Handle end-of-request processing...
2382 if ((http
->data_encoding
== HTTP_ENCODE_CHUNKED
&& length
== 0) ||
2383 (http
->data_encoding
== HTTP_ENCODE_LENGTH
&& http
->data_remaining
== 0))
2386 * Finished with the transfer; unless we are sending POST or PUT
2390 DEBUG_puts("2httpWrite: changing states...");
2393 httpFlushWrite(http
);
2395 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2398 * Send a 0-length chunk at the end of the request...
2401 http_write(http
, "0\r\n\r\n", 5);
2404 * Reset the data state...
2407 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
2408 http
->data_remaining
= 0;
2411 if (http
->state
== HTTP_POST_RECV
)
2413 else if (http
->state
== HTTP_PUT_RECV
)
2414 http
->state
= HTTP_STATUS
;
2416 http
->state
= HTTP_WAITING
;
2423 #if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
2425 * '_httpWriteCDSA()' - Write function for the CDSA library.
2428 OSStatus
/* O - -1 on error, 0 on success */
2430 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2431 const void *data
, /* I - Data buffer */
2432 size_t *dataLength
) /* IO - Number of bytes */
2434 OSStatus result
; /* Return value */
2435 ssize_t bytes
; /* Number of bytes read */
2436 http_t
*http
; /* HTTP connection */
2439 http
= (http_t
*)connection
;
2443 bytes
= write(http
->fd
, data
, *dataLength
);
2445 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
2447 if (bytes
== *dataLength
)
2451 else if (bytes
>= 0)
2453 *dataLength
= bytes
;
2454 result
= errSSLWouldBlock
;
2460 if (errno
== EAGAIN
)
2461 result
= errSSLWouldBlock
;
2463 result
= errSSLClosedAbort
;
2468 #endif /* HAVE_SSL && HAVE_CDSASSL */
2471 #if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
2473 * '_httpWriteGNUTLS()' - Write function for the GNU TLS library.
2476 ssize_t
/* O - Number of bytes written or -1 on error */
2478 gnutls_transport_ptr ptr
, /* I - Connection to server */
2479 const void *data
, /* I - Data buffer */
2480 size_t length
) /* I - Number of bytes to write */
2482 return (send(((http_t
*)ptr
)->fd
, data
, length
, 0));
2484 #endif /* HAVE_SSL && HAVE_GNUTLS */
2487 #if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
2489 * 'http_bio_ctrl()' - Control the HTTP connection.
2492 static long /* O - Result/data */
2493 http_bio_ctrl(BIO
*h
, /* I - BIO data */
2494 int cmd
, /* I - Control command */
2495 long arg1
, /* I - First argument */
2496 void *arg2
) /* I - Second argument */
2503 case BIO_CTRL_RESET
:
2507 case BIO_C_SET_FILE_PTR
:
2512 case BIO_C_GET_FILE_PTR
:
2515 *((void **)arg2
) = h
->ptr
;
2522 case BIO_CTRL_FLUSH
:
2529 * 'http_bio_free()' - Free OpenSSL data.
2532 static int /* O - 1 on success, 0 on failure */
2533 http_bio_free(BIO
*h
) /* I - BIO data */
2549 * 'http_bio_new()' - Initialize an OpenSSL BIO structure.
2552 static int /* O - 1 on success, 0 on failure */
2553 http_bio_new(BIO
*h
) /* I - BIO data */
2568 * 'http_bio_puts()' - Send a string for OpenSSL.
2571 static int /* O - Bytes written */
2572 http_bio_puts(BIO
*h
, /* I - BIO data */
2573 const char *str
) /* I - String to write */
2576 return (send(((http_t
*)h
->ptr
)->fd
, str
, (int)strlen(str
), 0));
2578 return (send(((http_t
*)h
->ptr
)->fd
, str
, strlen(str
), 0));
2584 * 'http_bio_read()' - Read data for OpenSSL.
2587 static int /* O - Bytes read */
2588 http_bio_read(BIO
*h
, /* I - BIO data */
2589 char *buf
, /* I - Buffer */
2590 int size
) /* I - Number of bytes to read */
2592 http_t
*http
; /* HTTP connection */
2595 http
= (http_t
*)h
->ptr
;
2597 if (!http
->blocking
)
2600 * Make sure we have data before we read...
2603 if (!_httpWait(http
, 10000, 0))
2606 http
->error
= WSAETIMEDOUT
;
2608 http
->error
= ETIMEDOUT
;
2615 return (recv(http
->fd
, buf
, size
, 0));
2620 * 'http_bio_write()' - Write data for OpenSSL.
2623 static int /* O - Bytes written */
2624 http_bio_write(BIO
*h
, /* I - BIO data */
2625 const char *buf
, /* I - Buffer to write */
2626 int num
) /* I - Number of bytes to write */
2628 return (send(((http_t
*)h
->ptr
)->fd
, buf
, num
, 0));
2630 #endif /* HAVE_SSL && HAVE_LIBSSL */
2635 * 'http_debug_hex()' - Do a hex dump of a buffer.
2639 http_debug_hex(const char *prefix
, /* I - Prefix for line */
2640 const char *buffer
, /* I - Buffer to dump */
2641 int bytes
) /* I - Bytes to dump */
2643 int i
, j
, /* Looping vars */
2644 ch
; /* Current character */
2645 char line
[255], /* Line buffer */
2646 *start
, /* Start of line after prefix */
2647 *ptr
; /* Pointer into line */
2650 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
2653 DEBUG_printf(("6%s: %d bytes:\n", prefix
, bytes
));
2655 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
2656 start
= line
+ strlen(line
);
2658 for (i
= 0; i
< bytes
; i
+= 16)
2660 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
2661 sprintf(ptr
, "%02X", buffer
[i
+ j
] & 255);
2673 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
2675 ch
= buffer
[i
+ j
] & 255;
2677 if (ch
< ' ' || ch
>= 127)
2691 * 'http_field()' - Return the field index for a field name.
2694 static http_field_t
/* O - Field index */
2695 http_field(const char *name
) /* I - String name */
2697 int i
; /* Looping var */
2700 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2701 if (strcasecmp(name
, http_fields
[i
]) == 0)
2702 return ((http_field_t
)i
);
2704 return (HTTP_FIELD_UNKNOWN
);
2710 * 'http_read_ssl()' - Read from a SSL/TLS connection.
2713 static int /* O - Bytes read */
2714 http_read_ssl(http_t
*http
, /* I - Connection to server */
2715 char *buf
, /* I - Buffer to store data */
2716 int len
) /* I - Length of buffer */
2718 # if defined(HAVE_LIBSSL)
2719 return (SSL_read((SSL
*)(http
->tls
), buf
, len
));
2721 # elif defined(HAVE_GNUTLS)
2722 return (gnutls_record_recv(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
2724 # elif defined(HAVE_CDSASSL)
2725 int result
; /* Return value */
2726 OSStatus error
; /* Error info */
2727 size_t processed
; /* Number of bytes processed */
2730 error
= SSLRead(((http_tls_t
*)http
->tls
)->session
, buf
, len
, &processed
);
2735 result
= (int)processed
;
2737 case errSSLClosedGraceful
:
2740 case errSSLWouldBlock
:
2742 result
= (int)processed
;
2756 # endif /* HAVE_LIBSSL */
2758 #endif /* HAVE_SSL */
2762 * 'http_send()' - Send a request with all fields and the trailing blank line.
2765 static int /* O - 0 on success, non-zero on error */
2766 http_send(http_t
*http
, /* I - Connection to server */
2767 http_state_t request
, /* I - Request code */
2768 const char *uri
) /* I - URI */
2770 int i
; /* Looping var */
2771 char buf
[1024]; /* Encoded URI buffer */
2772 static const char * const codes
[] =
2773 { /* Request code strings */
2790 DEBUG_printf(("7http_send(http=%p, request=HTTP_%s, uri=\"%s\")",
2791 http
, codes
[request
], uri
));
2793 if (http
== NULL
|| uri
== NULL
)
2797 * Set the User-Agent field if it isn't already...
2800 if (!http
->fields
[HTTP_FIELD_USER_AGENT
][0])
2801 httpSetField(http
, HTTP_FIELD_USER_AGENT
, CUPS_MINIMAL
);
2804 * Encode the URI as needed...
2807 _httpEncodeURI(buf
, uri
, sizeof(buf
));
2810 * See if we had an error the last time around; if so, reconnect...
2813 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
2814 if (httpReconnect(http
))
2818 * Flush any written data that is pending...
2823 if (httpFlushWrite(http
) < 0)
2824 if (httpReconnect(http
))
2829 * Send the request header...
2832 http
->state
= request
;
2833 http
->data_encoding
= HTTP_ENCODE_FIELDS
;
2835 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
2838 http
->status
= HTTP_CONTINUE
;
2841 if (http
->encryption
== HTTP_ENCRYPT_REQUIRED
&& !http
->tls
)
2843 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
2844 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0,SSL/2.0,SSL/3.0");
2846 #endif /* HAVE_SSL */
2848 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
2850 http
->status
= HTTP_ERROR
;
2854 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
2855 if (http
->fields
[i
][0] != '\0')
2857 DEBUG_printf(("9http_send: %s: %s", http_fields
[i
],
2858 httpGetField(http
, i
)));
2860 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
],
2861 httpGetField(http
, i
)) < 1)
2863 http
->status
= HTTP_ERROR
;
2869 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
2871 http
->status
= HTTP_ERROR
;
2875 if (http
->expect
== HTTP_CONTINUE
&&
2876 (http
->state
== HTTP_POST_RECV
|| http
->state
== HTTP_PUT_RECV
))
2877 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
2879 http
->status
= HTTP_ERROR
;
2883 if (httpPrintf(http
, "\r\n") < 1)
2885 http
->status
= HTTP_ERROR
;
2889 if (httpFlushWrite(http
) < 0)
2892 httpGetLength2(http
);
2893 httpClearFields(http
);
2896 * The Kerberos and AuthRef authentication strings can only be used once...
2899 if (http
->field_authorization
&& http
->authstring
&&
2900 (!strncmp(http
->authstring
, "Negotiate", 9) ||
2901 !strncmp(http
->authstring
, "AuthRef", 7)))
2903 http
->_authstring
[0] = '\0';
2905 if (http
->authstring
!= http
->_authstring
)
2906 free(http
->authstring
);
2908 http
->authstring
= http
->_authstring
;
2917 * 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
2920 static int /* O - Status of connection */
2921 http_setup_ssl(http_t
*http
) /* I - Connection to server */
2924 SSL_CTX
*context
; /* Context for encryption */
2925 SSL
*conn
; /* Connection for encryption */
2926 BIO
*bio
; /* BIO data */
2927 # elif defined(HAVE_GNUTLS)
2928 http_tls_t
*conn
; /* TLS session object */
2929 gnutls_certificate_client_credentials
*credentials
;
2930 /* TLS credentials */
2931 # elif defined(HAVE_CDSASSL)
2932 OSStatus error
; /* Error code */
2933 http_tls_t
*conn
; /* CDSA connection information */
2934 # endif /* HAVE_LIBSSL */
2937 DEBUG_printf(("7http_setup_ssl(http=%p)", http
));
2940 context
= SSL_CTX_new(SSLv23_client_method());
2942 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
2944 bio
= BIO_new(_httpBIOMethods());
2945 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)http
);
2947 conn
= SSL_new(context
);
2948 SSL_set_bio(conn
, bio
, bio
);
2950 if (SSL_connect(conn
) != 1)
2953 unsigned long error
; /* Error code */
2955 while ((error
= ERR_get_error()) != 0)
2956 DEBUG_printf(("8http_setup_ssl: %s", ERR_error_string(error
, NULL
)));
2959 SSL_CTX_free(context
);
2963 http
->error
= WSAGetLastError();
2965 http
->error
= errno
;
2967 http
->status
= HTTP_ERROR
;
2969 return (HTTP_ERROR
);
2972 # elif defined(HAVE_GNUTLS)
2973 if ((conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
))) == NULL
)
2975 http
->error
= errno
;
2976 http
->status
= HTTP_ERROR
;
2981 credentials
= (gnutls_certificate_client_credentials
*)
2982 malloc(sizeof(gnutls_certificate_client_credentials
));
2983 if (credentials
== NULL
)
2987 http
->error
= errno
;
2988 http
->status
= HTTP_ERROR
;
2993 gnutls_certificate_allocate_credentials(credentials
);
2995 gnutls_init(&(conn
->session
), GNUTLS_CLIENT
);
2996 gnutls_set_default_priority(conn
->session
);
2997 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
2998 gnutls_transport_set_ptr(conn
->session
, (gnutls_transport_ptr
)http
);
2999 gnutls_transport_set_pull_function(conn
->session
, _httpReadGNUTLS
);
3000 gnutls_transport_set_push_function(conn
->session
, _httpWriteGNUTLS
);
3002 if ((gnutls_handshake(conn
->session
)) != GNUTLS_E_SUCCESS
)
3004 http
->error
= errno
;
3005 http
->status
= HTTP_ERROR
;
3007 gnutls_deinit(conn
->session
);
3008 gnutls_certificate_free_credentials(*credentials
);
3015 conn
->credentials
= credentials
;
3017 # elif defined(HAVE_CDSASSL)
3018 conn
= (http_tls_t
*)calloc(1, sizeof(http_tls_t
));
3023 if ((error
= SSLNewContext(false, &conn
->session
)))
3025 http
->error
= error
;
3026 http
->status
= HTTP_ERROR
;
3033 * Use a union to resolve warnings about int/pointer size mismatches...
3036 error
= SSLSetConnection(conn
->session
, http
);
3039 error
= SSLSetIOFuncs(conn
->session
, _httpReadCDSA
, _httpWriteCDSA
);
3042 error
= SSLSetAllowsExpiredCerts(conn
->session
, true);
3045 error
= SSLSetAllowsAnyRoot(conn
->session
, true);
3048 error
= SSLSetProtocolVersionEnabled(conn
->session
, kSSLProtocol2
, false);
3052 while ((error
= SSLHandshake(conn
->session
)) == errSSLWouldBlock
)
3058 http
->error
= error
;
3059 http
->status
= HTTP_ERROR
;
3061 SSLDisposeContext(conn
->session
);
3067 # endif /* HAVE_CDSASSL */
3072 #endif /* HAVE_SSL */
3077 * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
3081 http_shutdown_ssl(http_t
*http
) /* I - Connection to server */
3084 SSL_CTX
*context
; /* Context for encryption */
3085 SSL
*conn
; /* Connection for encryption */
3088 conn
= (SSL
*)(http
->tls
);
3089 context
= SSL_get_SSL_CTX(conn
);
3092 SSL_CTX_free(context
);
3095 # elif defined(HAVE_GNUTLS)
3096 http_tls_t
*conn
; /* Encryption session */
3097 gnutls_certificate_client_credentials
*credentials
;
3098 /* TLS credentials */
3101 conn
= (http_tls_t
*)(http
->tls
);
3102 credentials
= (gnutls_certificate_client_credentials
*)(conn
->credentials
);
3104 gnutls_bye(conn
->session
, GNUTLS_SHUT_RDWR
);
3105 gnutls_deinit(conn
->session
);
3106 gnutls_certificate_free_credentials(*credentials
);
3110 # elif defined(HAVE_CDSASSL)
3111 http_tls_t
*conn
; /* CDSA connection information */
3114 conn
= (http_tls_t
*)(http
->tls
);
3116 while (SSLClose(conn
->session
) == errSSLWouldBlock
)
3119 SSLDisposeContext(conn
->session
);
3121 if (conn
->certsArray
)
3122 CFRelease(conn
->certsArray
);
3125 # endif /* HAVE_LIBSSL */
3129 #endif /* HAVE_SSL */
3134 * 'http_upgrade()' - Force upgrade to TLS encryption.
3137 static int /* O - Status of connection */
3138 http_upgrade(http_t
*http
) /* I - Connection to server */
3140 int ret
; /* Return value */
3141 http_t myhttp
; /* Local copy of HTTP data */
3144 DEBUG_printf(("7http_upgrade(%p)", http
));
3147 * Flush the connection to make sure any previous "Upgrade" message
3154 * Copy the HTTP data to a local variable so we can do the OPTIONS
3155 * request without interfering with the existing request data...
3158 memcpy(&myhttp
, http
, sizeof(myhttp
));
3161 * Send an OPTIONS request to the server, requiring SSL or TLS
3162 * encryption on the link...
3165 http
->field_authorization
= NULL
; /* Don't free the auth string */
3167 httpClearFields(http
);
3168 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
3169 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.0, SSL/2.0, SSL/3.0");
3171 if ((ret
= httpOptions(http
, "*")) == 0)
3174 * Wait for the secure connection...
3177 while (httpUpdate(http
) == HTTP_CONTINUE
);
3181 * Restore the HTTP request data...
3184 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
3185 http
->data_encoding
= myhttp
.data_encoding
;
3186 http
->data_remaining
= myhttp
.data_remaining
;
3187 http
->_data_remaining
= myhttp
._data_remaining
;
3188 http
->expect
= myhttp
.expect
;
3189 http
->field_authorization
= myhttp
.field_authorization
;
3190 http
->digest_tries
= myhttp
.digest_tries
;
3193 * See if we actually went secure...
3199 * Server does not support HTTP upgrade...
3202 DEBUG_puts("8http_upgrade: Server does not support HTTP upgrade!");
3205 closesocket(http
->fd
);
3217 #endif /* HAVE_SSL */
3221 * 'http_write()' - Write a buffer to a HTTP connection.
3224 static int /* O - Number of bytes written */
3225 http_write(http_t
*http
, /* I - Connection to server */
3226 const char *buffer
, /* I - Buffer for data */
3227 int length
) /* I - Number of bytes to write */
3229 int tbytes
, /* Total bytes sent */
3230 bytes
; /* Bytes sent */
3240 bytes
= http_write_ssl(http
, buffer
, length
);
3242 #endif /* HAVE_SSL */
3243 bytes
= send(http
->fd
, buffer
, length
, 0);
3248 if (WSAGetLastError() != http
->error
)
3250 http
->error
= WSAGetLastError();
3254 if (errno
== EINTR
|| errno
== EAGAIN
)
3256 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
3258 http
->error
= errno
;
3263 DEBUG_puts("8http_write: error writing data...");
3274 http_debug_hex("http_write", buffer
- tbytes
, tbytes
);
3282 * 'http_write_chunk()' - Write a chunked buffer.
3285 static int /* O - Number bytes written */
3286 http_write_chunk(http_t
*http
, /* I - Connection to server */
3287 const char *buffer
, /* I - Buffer to write */
3288 int length
) /* I - Length of buffer */
3290 char header
[255]; /* Chunk header */
3291 int bytes
; /* Bytes written */
3294 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=%d)",
3295 http
, buffer
, length
));
3298 * Write the chunk header, data, and trailer.
3301 sprintf(header
, "%x\r\n", length
);
3302 if (http_write(http
, header
, (int)strlen(header
)) < 0)
3304 DEBUG_puts("8http_write_chunk: http_write of length failed!");
3308 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
3310 DEBUG_puts("8http_write_chunk: http_write of buffer failed!");
3314 if (http_write(http
, "\r\n", 2) < 0)
3316 DEBUG_puts("8http_write_chunk: http_write of CR LF failed!");
3326 * 'http_write_ssl()' - Write to a SSL/TLS connection.
3329 static int /* O - Bytes written */
3330 http_write_ssl(http_t
*http
, /* I - Connection to server */
3331 const char *buf
, /* I - Buffer holding data */
3332 int len
) /* I - Length of buffer */
3334 # if defined(HAVE_LIBSSL)
3335 return (SSL_write((SSL
*)(http
->tls
), buf
, len
));
3337 # elif defined(HAVE_GNUTLS)
3338 return (gnutls_record_send(((http_tls_t
*)(http
->tls
))->session
, buf
, len
));
3339 # elif defined(HAVE_CDSASSL)
3340 int result
; /* Return value */
3341 OSStatus error
; /* Error info */
3342 size_t processed
; /* Number of bytes processed */
3345 error
= SSLWrite(((http_tls_t
*)http
->tls
)->session
, buf
, len
, &processed
);
3350 result
= (int)processed
;
3352 case errSSLClosedGraceful
:
3355 case errSSLWouldBlock
:
3357 result
= (int)processed
;
3371 # endif /* HAVE_LIBSSL */
3373 #endif /* HAVE_SSL */
3377 * End of "$Id: http.c 7850 2008-08-20 00:07:25Z mike $".