2 * HTTP routines for CUPS.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * Licensed under Apache License v2.0. See the file "LICENSE" for more
15 * Include necessary headers...
18 #include "cups-private.h"
25 # include <sys/time.h>
26 # include <sys/resource.h>
30 #endif /* HAVE_POLL */
37 static void http_add_field(http_t
*http
, http_field_t field
, const char *value
, int append
);
39 static void http_content_coding_finish(http_t
*http
);
40 static void http_content_coding_start(http_t
*http
,
42 #endif /* HAVE_LIBZ */
43 static http_t
*http_create(const char *host
, int port
,
44 http_addrlist_t
*addrlist
, int family
,
45 http_encryption_t encryption
,
46 int blocking
, _http_mode_t mode
);
48 static void http_debug_hex(const char *prefix
, const char *buffer
,
51 static ssize_t
http_read(http_t
*http
, char *buffer
, size_t length
);
52 static ssize_t
http_read_buffered(http_t
*http
, char *buffer
, size_t length
);
53 static ssize_t
http_read_chunk(http_t
*http
, char *buffer
, size_t length
);
54 static int http_send(http_t
*http
, http_state_t request
,
56 static ssize_t
http_write(http_t
*http
, const char *buffer
,
58 static ssize_t
http_write_chunk(http_t
*http
, const char *buffer
,
60 static off_t
http_set_length(http_t
*http
);
61 static void http_set_timeout(int fd
, double timeout
);
62 static void http_set_wait(http_t
*http
);
65 static int http_tls_upgrade(http_t
*http
);
73 static const char * const http_fields
[] =
90 "If-Unmodified-since",
109 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
110 * specified listening socket.
112 * @since CUPS 1.7/macOS 10.9@
115 http_t
* /* O - HTTP connection or @code NULL@ */
116 httpAcceptConnection(int fd
, /* I - Listen socket file descriptor */
117 int blocking
) /* I - 1 if the connection should be
118 blocking, 0 otherwise */
120 http_t
*http
; /* HTTP connection */
121 http_addrlist_t addrlist
; /* Dummy address list */
122 socklen_t addrlen
; /* Length of address */
123 int val
; /* Socket option value */
127 * Range check input...
134 * Create the client connection...
137 memset(&addrlist
, 0, sizeof(addrlist
));
139 if ((http
= http_create(NULL
, 0, &addrlist
, AF_UNSPEC
,
140 HTTP_ENCRYPTION_IF_REQUESTED
, blocking
,
141 _HTTP_MODE_SERVER
)) == NULL
)
145 * Accept the client and get the remote address...
148 addrlen
= sizeof(http_addr_t
);
150 if ((http
->fd
= accept(fd
, (struct sockaddr
*)&(http
->addrlist
->addr
),
153 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
159 http
->hostaddr
= &(http
->addrlist
->addr
);
161 if (httpAddrLocalhost(http
->hostaddr
))
162 strlcpy(http
->hostname
, "localhost", sizeof(http
->hostname
));
164 httpAddrString(http
->hostaddr
, http
->hostname
, sizeof(http
->hostname
));
168 * Disable SIGPIPE for this socket.
172 setsockopt(http
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
173 #endif /* SO_NOSIGPIPE */
176 * Using TCP_NODELAY improves responsiveness, especially on systems
177 * with a slow loopback interface. Since we write large buffers
178 * when sending print files and requests, there shouldn't be any
179 * performance penalty for this...
183 setsockopt(http
->fd
, IPPROTO_TCP
, TCP_NODELAY
, CUPS_SOCAST
&val
, sizeof(val
));
187 * Close this socket when starting another process...
190 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
);
191 #endif /* FD_CLOEXEC */
198 * 'httpAddCredential()' - Allocates and adds a single credential to an array.
200 * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
202 * @since CUPS 1.5/macOS 10.7@
205 int /* O - 0 on success, -1 on error */
207 cups_array_t
*credentials
, /* I - Credentials array */
208 const void *data
, /* I - PEM-encoded X.509 data */
209 size_t datalen
) /* I - Length of data */
211 http_credential_t
*credential
; /* Credential data */
214 if ((credential
= malloc(sizeof(http_credential_t
))) != NULL
)
216 credential
->datalen
= datalen
;
218 if ((credential
->data
= malloc(datalen
)) != NULL
)
220 memcpy(credential
->data
, data
, datalen
);
221 cupsArrayAdd(credentials
, credential
);
233 * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
237 httpBlocking(http_t
*http
, /* I - HTTP connection */
238 int b
) /* I - 1 = blocking, 0 = non-blocking */
249 * 'httpCheck()' - Check to see if there is a pending response from the server.
252 int /* O - 0 = no data, 1 = data available */
253 httpCheck(http_t
*http
) /* I - HTTP connection */
255 return (httpWait(http
, 0));
260 * 'httpClearCookie()' - Clear the cookie value(s).
262 * @since CUPS 1.1.19/macOS 10.3@
266 httpClearCookie(http_t
*http
) /* I - HTTP connection */
280 * 'httpClearFields()' - Clear HTTP request fields.
284 httpClearFields(http_t
*http
) /* I - HTTP connection */
286 http_field_t field
; /* Current field */
289 DEBUG_printf(("httpClearFields(http=%p)", (void *)http
));
293 memset(http
->_fields
, 0, sizeof(http
->fields
));
295 for (field
= HTTP_FIELD_ACCEPT_LANGUAGE
; field
< HTTP_FIELD_MAX
; field
++)
297 if (http
->fields
[field
] && http
->fields
[field
] != http
->_fields
[field
])
298 free(http
->fields
[field
]);
300 http
->fields
[field
] = NULL
;
303 if (http
->mode
== _HTTP_MODE_CLIENT
)
305 if (http
->hostname
[0] == '/')
306 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
308 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
311 http
->expect
= (http_status_t
)0;
317 * 'httpClose()' - Close an HTTP connection.
321 httpClose(http_t
*http
) /* I - HTTP connection */
324 OM_uint32 minor_status
; /* Minor status code */
325 #endif /* HAVE_GSSAPI */
328 DEBUG_printf(("httpClose(http=%p)", (void *)http
));
331 * Range check input...
338 * Close any open connection...
341 _httpDisconnect(http
);
344 * Free memory used...
347 httpAddrFreeList(http
->addrlist
);
353 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
354 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
356 if (http
->gssname
!= GSS_C_NO_NAME
)
357 gss_release_name(&minor_status
, &http
->gssname
);
358 #endif /* HAVE_GSSAPI */
360 #ifdef HAVE_AUTHORIZATION_H
362 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
363 #endif /* HAVE_AUTHORIZATION_H */
365 httpClearFields(http
);
367 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
368 free(http
->authstring
);
375 * 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
377 * @since CUPS 2.0/OS 10.10@
380 int /* O - 1 if they match, 0 if they do not */
381 httpCompareCredentials(
382 cups_array_t
*cred1
, /* I - First set of X.509 credentials */
383 cups_array_t
*cred2
) /* I - Second set of X.509 credentials */
385 http_credential_t
*temp1
, *temp2
; /* Temporary credentials */
388 for (temp1
= (http_credential_t
*)cupsArrayFirst(cred1
), temp2
= (http_credential_t
*)cupsArrayFirst(cred2
); temp1
&& temp2
; temp1
= (http_credential_t
*)cupsArrayNext(cred1
), temp2
= (http_credential_t
*)cupsArrayNext(cred2
))
389 if (temp1
->datalen
!= temp2
->datalen
)
391 else if (memcmp(temp1
->data
, temp2
->data
, temp1
->datalen
))
394 return (temp1
== temp2
);
399 * 'httpConnect()' - Connect to a HTTP server.
401 * This function is deprecated - use @link httpConnect2@ instead.
403 * @deprecated@ @exclude all@
406 http_t
* /* O - New HTTP connection */
407 httpConnect(const char *host
, /* I - Host to connect to */
408 int port
) /* I - Port number */
410 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
,
411 HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
));
416 * 'httpConnect2()' - Connect to a HTTP server.
418 * @since CUPS 1.7/macOS 10.9@
421 http_t
* /* O - New HTTP connection */
423 const char *host
, /* I - Host to connect to */
424 int port
, /* I - Port number */
425 http_addrlist_t
*addrlist
, /* I - List of addresses or @code NULL@ to lookup */
426 int family
, /* I - Address family to use or @code AF_UNSPEC@ for any */
427 http_encryption_t encryption
, /* I - Type of encryption to use */
428 int blocking
, /* I - 1 for blocking connection, 0 for non-blocking */
429 int msec
, /* I - Connection timeout in milliseconds, 0 means don't connect */
430 int *cancel
) /* I - Pointer to "cancel" variable */
432 http_t
*http
; /* New HTTP connection */
435 DEBUG_printf(("httpConnect2(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%d, msec=%d, cancel=%p)", host
, port
, (void *)addrlist
, family
, encryption
, blocking
, msec
, (void *)cancel
));
438 * Create the HTTP structure...
441 if ((http
= http_create(host
, port
, addrlist
, family
, encryption
, blocking
,
442 _HTTP_MODE_CLIENT
)) == NULL
)
446 * Optionally connect to the remote system...
449 if (msec
== 0 || !httpReconnect2(http
, msec
, cancel
))
453 * Could not connect to any known address - bail out!
463 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
465 * This function is now deprecated. Please use the @link httpConnect2@ function
468 * @deprecated@ @exclude all@
471 http_t
* /* O - New HTTP connection */
473 const char *host
, /* I - Host to connect to */
474 int port
, /* I - Port number */
475 http_encryption_t encryption
) /* I - Type of encryption to use */
477 DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
478 host
, port
, encryption
));
480 return (httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
486 * 'httpDelete()' - Send a DELETE request to the server.
489 int /* O - Status of call (0 = success) */
490 httpDelete(http_t
*http
, /* I - HTTP connection */
491 const char *uri
) /* I - URI to delete */
493 return (http_send(http
, HTTP_STATE_DELETE
, uri
));
498 * '_httpDisconnect()' - Disconnect a HTTP connection.
502 _httpDisconnect(http_t
*http
) /* I - HTTP connection */
507 #endif /* HAVE_SSL */
509 httpAddrClose(NULL
, http
->fd
);
516 * 'httpEncryption()' - Set the required encryption on the link.
519 int /* O - -1 on error, 0 on success */
520 httpEncryption(http_t
*http
, /* I - HTTP connection */
521 http_encryption_t e
) /* I - New encryption preference */
523 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http
, e
));
529 if (http
->mode
== _HTTP_MODE_CLIENT
)
531 http
->encryption
= e
;
533 if ((http
->encryption
== HTTP_ENCRYPTION_ALWAYS
&& !http
->tls
) ||
534 (http
->encryption
== HTTP_ENCRYPTION_NEVER
&& http
->tls
))
535 return (httpReconnect2(http
, 30000, NULL
));
536 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
537 return (http_tls_upgrade(http
));
543 if (e
== HTTP_ENCRYPTION_NEVER
&& http
->tls
)
546 http
->encryption
= e
;
547 if (e
!= HTTP_ENCRYPTION_IF_REQUESTED
&& !http
->tls
)
548 return (_httpTLSStart(http
));
553 if (e
== HTTP_ENCRYPTION_ALWAYS
|| e
== HTTP_ENCRYPTION_REQUIRED
)
557 #endif /* HAVE_SSL */
562 * 'httpError()' - Get the last error on a connection.
565 int /* O - Error code (errno) value */
566 httpError(http_t
*http
) /* I - HTTP connection */
569 return (http
->error
);
576 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
580 http_field_t
/* O - Field index */
581 httpFieldValue(const char *name
) /* I - String name */
583 int i
; /* Looping var */
586 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
587 if (!_cups_strcasecmp(name
, http_fields
[i
]))
588 return ((http_field_t
)i
);
590 return (HTTP_FIELD_UNKNOWN
);
595 * 'httpFlush()' - Flush data read from a HTTP connection.
599 httpFlush(http_t
*http
) /* I - HTTP connection */
601 char buffer
[8192]; /* Junk buffer */
602 int blocking
; /* To block or not to block */
603 http_state_t oldstate
; /* Old state */
606 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
609 * Nothing to do if we are in the "waiting" state...
612 if (http
->state
== HTTP_STATE_WAITING
)
616 * Temporarily set non-blocking mode so we don't get stuck in httpRead()...
619 blocking
= http
->blocking
;
623 * Read any data we can...
626 oldstate
= http
->state
;
627 while (httpRead2(http
, buffer
, sizeof(buffer
)) > 0);
630 * Restore blocking and reset the connection if we didn't get all of
631 * the remaining data...
634 http
->blocking
= blocking
;
636 if (http
->state
== oldstate
&& http
->state
!= HTTP_STATE_WAITING
&&
640 * Didn't get the data back, so close the current connection.
645 http_content_coding_finish(http
);
646 #endif /* HAVE_LIBZ */
648 DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
650 http
->state
= HTTP_STATE_WAITING
;
655 #endif /* HAVE_SSL */
657 httpAddrClose(NULL
, http
->fd
);
665 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
667 * @since CUPS 1.2/macOS 10.5@
670 int /* O - Bytes written or -1 on error */
671 httpFlushWrite(http_t
*http
) /* I - HTTP connection */
673 ssize_t bytes
; /* Bytes written */
676 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http
, http
? http
->data_encoding
: 100));
678 if (!http
|| !http
->wused
)
680 DEBUG_puts(http
? "1httpFlushWrite: Write buffer is empty." :
681 "1httpFlushWrite: No connection.");
685 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
686 bytes
= http_write_chunk(http
, http
->wbuffer
, (size_t)http
->wused
);
688 bytes
= http_write(http
, http
->wbuffer
, (size_t)http
->wused
);
692 DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes
, errno
));
699 * 'httpFreeCredentials()' - Free an array of credentials.
704 cups_array_t
*credentials
) /* I - Array of credentials */
706 http_credential_t
*credential
; /* Credential */
709 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
711 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
713 cupsArrayRemove(credentials
, credential
);
714 free((void *)credential
->data
);
718 cupsArrayDelete(credentials
);
723 * 'httpGet()' - Send a GET request to the server.
726 int /* O - Status of call (0 = success) */
727 httpGet(http_t
*http
, /* I - HTTP connection */
728 const char *uri
) /* I - URI to get */
730 return (http_send(http
, HTTP_STATE_GET
, uri
));
735 * 'httpGetActivity()' - Get the most recent activity for a connection.
737 * The return value is the time in seconds of the last read or write.
739 * @since CUPS 2.0/OS 10.10@
742 time_t /* O - Time of last read or write */
743 httpGetActivity(http_t
*http
) /* I - HTTP connection */
745 return (http
? http
->activity
: 0);
750 * 'httpGetAuthString()' - Get the current authorization string.
752 * The authorization string is set by @link cupsDoAuthentication@ and
753 * @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
754 * string to use with @link httpSetField@ for the
755 * @code HTTP_FIELD_AUTHORIZATION@ value.
757 * @since CUPS 1.3/macOS 10.5@
760 char * /* O - Authorization string */
761 httpGetAuthString(http_t
*http
) /* I - HTTP connection */
764 return (http
->authstring
);
771 * 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
773 * @since CUPS 1.2/macOS 10.5@
776 int /* O - 1 if blocking, 0 if non-blocking */
777 httpGetBlocking(http_t
*http
) /* I - HTTP connection */
779 return (http
? http
->blocking
: 0);
784 * 'httpGetContentEncoding()' - Get a common content encoding, if any, between
785 * the client and server.
787 * This function uses the value of the Accepts-Encoding HTTP header and must be
788 * called after receiving a response from the server or a request from the
789 * client. The value returned can be use in subsequent requests (for clients)
790 * or in the response (for servers) in order to compress the content stream.
792 * @since CUPS 1.7/macOS 10.9@
795 const char * /* O - Content-Coding value or
796 @code NULL@ for the identity
798 httpGetContentEncoding(http_t
*http
) /* I - HTTP connection */
801 if (http
&& http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
803 int i
; /* Looping var */
804 char temp
[HTTP_MAX_VALUE
], /* Copy of Accepts-Encoding value */
805 *start
, /* Start of coding value */
806 *end
; /* End of coding value */
807 double qvalue
; /* "qvalue" for coding */
808 struct lconv
*loc
= localeconv(); /* Locale data */
809 static const char * const codings
[] =
810 { /* Supported content codings */
817 strlcpy(temp
, http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
], sizeof(temp
));
819 for (start
= temp
; *start
; start
= end
)
822 * Find the end of the coding name...
827 while (*end
&& *end
!= ';' && *end
!= ',' && !isspace(*end
& 255))
833 * Grab the qvalue as needed...
836 if (!strncmp(end
, ";q=", 3))
837 qvalue
= _cupsStrScand(end
+ 3, NULL
, loc
);
840 * Skip past all attributes...
844 while (*end
&& *end
!= ',' && !isspace(*end
& 255))
850 while (*end
&& isspace(*end
& 255))
854 * Check value if it matches something we support...
860 for (i
= 0; i
< (int)(sizeof(codings
) / sizeof(codings
[0])); i
++)
861 if (!strcmp(start
, codings
[i
]))
865 #endif /* HAVE_LIBZ */
872 * 'httpGetCookie()' - Get any cookie data from the response.
874 * @since CUPS 1.1.19/macOS 10.3@
877 const char * /* O - Cookie data or @code NULL@ */
878 httpGetCookie(http_t
*http
) /* I - HTTP connection */
880 return (http
? http
->cookie
: NULL
);
885 * 'httpGetEncryption()' - Get the current encryption mode of a connection.
887 * This function returns the encryption mode for the connection. Use the
888 * @link httpIsEncrypted@ function to determine whether a TLS session has
891 * @since CUPS 2.0/OS 10.10@
894 http_encryption_t
/* O - Current encryption mode */
895 httpGetEncryption(http_t
*http
) /* I - HTTP connection */
897 return (http
? http
->encryption
: HTTP_ENCRYPTION_IF_REQUESTED
);
902 * 'httpGetExpect()' - Get the value of the Expect header, if any.
904 * Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
905 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
907 * @since CUPS 1.7/macOS 10.9@
910 http_status_t
/* O - Expect: status, if any */
911 httpGetExpect(http_t
*http
) /* I - HTTP connection */
914 return (HTTP_STATUS_ERROR
);
916 return (http
->expect
);
921 * 'httpGetFd()' - Get the file descriptor associated with a connection.
923 * @since CUPS 1.2/macOS 10.5@
926 int /* O - File descriptor or -1 if none */
927 httpGetFd(http_t
*http
) /* I - HTTP connection */
929 return (http
? http
->fd
: -1);
934 * 'httpGetField()' - Get a field value from a request/response.
937 const char * /* O - Field value */
938 httpGetField(http_t
*http
, /* I - HTTP connection */
939 http_field_t field
) /* I - Field to get */
941 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
943 else if (http
->fields
[field
])
944 return (http
->fields
[field
]);
951 * 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
953 * @since CUPS 2.0/OS 10.10@
956 http_keepalive_t
/* O - Keep-Alive state */
957 httpGetKeepAlive(http_t
*http
) /* I - HTTP connection */
959 return (http
? http
->keep_alive
: HTTP_KEEPALIVE_OFF
);
964 * 'httpGetLength()' - Get the amount of data remaining from the
965 * content-length or transfer-encoding fields.
967 * This function is deprecated and will not return lengths larger than
968 * 2^31 - 1; use httpGetLength2() instead.
970 * @deprecated@ @exclude all@
973 int /* O - Content length */
974 httpGetLength(http_t
*http
) /* I - HTTP connection */
977 * Get the read content length and return the 32-bit value.
982 httpGetLength2(http
);
984 return (http
->_data_remaining
);
992 * 'httpGetLength2()' - Get the amount of data remaining from the
993 * content-length or transfer-encoding fields.
995 * This function returns the complete content length, even for
996 * content larger than 2^31 - 1.
998 * @since CUPS 1.2/macOS 10.5@
1001 off_t
/* O - Content length */
1002 httpGetLength2(http_t
*http
) /* I - HTTP connection */
1004 off_t remaining
; /* Remaining length */
1007 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
1012 if (http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
] && !_cups_strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked"))
1014 DEBUG_puts("4httpGetLength2: chunked request!");
1020 * The following is a hack for HTTP servers that don't send a
1021 * Content-Length or Transfer-Encoding field...
1023 * If there is no Content-Length then the connection must close
1024 * after the transfer is complete...
1027 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
] || !http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0])
1030 * Default content length is 0 for errors and certain types of operations,
1031 * and 2^31-1 for other successful requests...
1034 if (http
->status
>= HTTP_STATUS_MULTIPLE_CHOICES
||
1035 http
->state
== HTTP_STATE_OPTIONS
||
1036 (http
->state
== HTTP_STATE_GET
&& http
->mode
== _HTTP_MODE_SERVER
) ||
1037 http
->state
== HTTP_STATE_HEAD
||
1038 (http
->state
== HTTP_STATE_PUT
&& http
->mode
== _HTTP_MODE_CLIENT
) ||
1039 http
->state
== HTTP_STATE_DELETE
||
1040 http
->state
== HTTP_STATE_TRACE
||
1041 http
->state
== HTTP_STATE_CONNECT
)
1044 remaining
= 2147483647;
1046 else if ((remaining
= strtoll(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
],
1050 DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT
,
1051 CUPS_LLCAST remaining
));
1059 * 'httpGetPending()' - Get the number of bytes that are buffered for writing.
1061 * @since CUPS 2.0/OS 10.10@
1064 size_t /* O - Number of bytes buffered */
1065 httpGetPending(http_t
*http
) /* I - HTTP connection */
1067 return (http
? (size_t)http
->wused
: 0);
1072 * 'httpGetReady()' - Get the number of bytes that can be read without blocking.
1074 * @since CUPS 2.0/OS 10.10@
1077 size_t /* O - Number of bytes available */
1078 httpGetReady(http_t
*http
) /* I - HTTP connection */
1082 else if (http
->used
> 0)
1083 return ((size_t)http
->used
);
1086 return (_httpTLSPending(http
));
1087 #endif /* HAVE_SSL */
1094 * 'httpGetRemaining()' - Get the number of remaining bytes in the message
1095 * body or current chunk.
1097 * The @link httpIsChunked@ function can be used to determine whether the
1098 * message body is chunked or fixed-length.
1100 * @since CUPS 2.0/OS 10.10@
1103 size_t /* O - Remaining bytes */
1104 httpGetRemaining(http_t
*http
) /* I - HTTP connection */
1106 return (http
? (size_t)http
->data_remaining
: 0);
1111 * 'httpGets()' - Get a line of text from a HTTP connection.
1114 char * /* O - Line or @code NULL@ */
1115 httpGets(char *line
, /* I - Line to read into */
1116 int length
, /* I - Max length of buffer */
1117 http_t
*http
) /* I - HTTP connection */
1119 char *lineptr
, /* Pointer into line */
1120 *lineend
, /* End of line */
1121 *bufptr
, /* Pointer into input buffer */
1122 *bufend
; /* Pointer to end of buffer */
1123 ssize_t bytes
; /* Number of bytes read */
1124 int eol
; /* End-of-line? */
1127 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line
, length
, (void *)http
));
1129 if (!http
|| !line
|| length
<= 1)
1133 * Read a line from the buffer...
1138 lineend
= line
+ length
- 1;
1141 while (lineptr
< lineend
)
1144 * Pre-load the buffer as needed...
1153 while (http
->used
== 0)
1156 * No newline; see if there is more data to be read...
1159 while (!_httpWait(http
, http
->wait_value
, 1))
1161 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1164 DEBUG_puts("3httpGets: Timed out!");
1166 http
->error
= WSAETIMEDOUT
;
1168 http
->error
= ETIMEDOUT
;
1173 bytes
= http_read(http
, http
->buffer
+ http
->used
, (size_t)(HTTP_MAX_BUFFER
- http
->used
));
1175 DEBUG_printf(("4httpGets: read " CUPS_LLFMT
" bytes.", CUPS_LLCAST bytes
));
1180 * Nope, can't get a line this time...
1184 DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError()));
1186 if (WSAGetLastError() == WSAEINTR
)
1188 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
1190 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1193 http
->error
= WSAGetLastError();
1195 else if (WSAGetLastError() != http
->error
)
1197 http
->error
= WSAGetLastError();
1202 DEBUG_printf(("3httpGets: recv() error %d!", errno
));
1206 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1208 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1210 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
1213 http
->error
= errno
;
1215 else if (errno
!= http
->error
)
1217 http
->error
= errno
;
1224 else if (bytes
== 0)
1226 http
->error
= EPIPE
;
1232 * Yup, update the amount used...
1235 http
->used
+= (int)bytes
;
1239 * Now copy as much of the current line as possible...
1242 for (bufptr
= http
->buffer
, bufend
= http
->buffer
+ http
->used
;
1243 lineptr
< lineend
&& bufptr
< bufend
;)
1245 if (*bufptr
== 0x0a)
1251 else if (*bufptr
== 0x0d)
1254 *lineptr
++ = *bufptr
++;
1257 http
->used
-= (int)(bufptr
- http
->buffer
);
1259 memmove(http
->buffer
, bufptr
, (size_t)http
->used
);
1267 http
->activity
= time(NULL
);
1271 DEBUG_printf(("3httpGets: Returning \"%s\"", line
));
1277 DEBUG_puts("3httpGets: No new line available!");
1284 * 'httpGetState()' - Get the current state of the HTTP request.
1287 http_state_t
/* O - HTTP state */
1288 httpGetState(http_t
*http
) /* I - HTTP connection */
1290 return (http
? http
->state
: HTTP_STATE_ERROR
);
1295 * 'httpGetStatus()' - Get the status of the last HTTP request.
1297 * @since CUPS 1.2/macOS 10.5@
1300 http_status_t
/* O - HTTP status */
1301 httpGetStatus(http_t
*http
) /* I - HTTP connection */
1303 return (http
? http
->status
: HTTP_STATUS_ERROR
);
1308 * 'httpGetSubField()' - Get a sub-field value.
1310 * @deprecated@ @exclude all@
1313 char * /* O - Value or @code NULL@ */
1314 httpGetSubField(http_t
*http
, /* I - HTTP connection */
1315 http_field_t field
, /* I - Field index */
1316 const char *name
, /* I - Name of sub-field */
1317 char *value
) /* O - Value string */
1319 return (httpGetSubField2(http
, field
, name
, value
, HTTP_MAX_VALUE
));
1324 * 'httpGetSubField2()' - Get a sub-field value.
1326 * @since CUPS 1.2/macOS 10.5@
1329 char * /* O - Value or @code NULL@ */
1330 httpGetSubField2(http_t
*http
, /* I - HTTP connection */
1331 http_field_t field
, /* I - Field index */
1332 const char *name
, /* I - Name of sub-field */
1333 char *value
, /* O - Value string */
1334 int valuelen
) /* I - Size of value buffer */
1336 const char *fptr
; /* Pointer into field */
1337 char temp
[HTTP_MAX_VALUE
], /* Temporary buffer for name */
1338 *ptr
, /* Pointer into string buffer */
1339 *end
; /* End of value buffer */
1341 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http
, field
, name
, (void *)value
, valuelen
));
1343 if (!http
|| !name
|| !value
|| valuelen
< 2 ||
1344 field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
1347 end
= value
+ valuelen
- 1;
1349 for (fptr
= http
->fields
[field
]; *fptr
;)
1352 * Skip leading whitespace...
1355 while (_cups_isspace(*fptr
))
1365 * Get the sub-field name...
1369 *fptr
&& *fptr
!= '=' && !_cups_isspace(*fptr
) &&
1370 ptr
< (temp
+ sizeof(temp
) - 1);
1375 DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp
));
1378 * Skip trailing chars up to the '='...
1381 while (_cups_isspace(*fptr
))
1391 * Skip = and leading whitespace...
1396 while (_cups_isspace(*fptr
))
1402 * Read quoted string...
1405 for (ptr
= value
, fptr
++;
1406 *fptr
&& *fptr
!= '\"' && ptr
< end
;
1411 while (*fptr
&& *fptr
!= '\"')
1420 * Read unquoted string...
1424 *fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',' && ptr
< end
;
1429 while (*fptr
&& !_cups_isspace(*fptr
) && *fptr
!= ',')
1433 DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value
));
1436 * See if this is the one...
1439 if (!strcmp(name
, temp
))
1441 DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value
));
1448 DEBUG_puts("3httpGetSubField2: Returning NULL");
1455 * 'httpGetVersion()' - Get the HTTP version at the other end.
1458 http_version_t
/* O - Version number */
1459 httpGetVersion(http_t
*http
) /* I - HTTP connection */
1461 return (http
? http
->version
: HTTP_VERSION_1_0
);
1466 * 'httpHead()' - Send a HEAD request to the server.
1469 int /* O - Status of call (0 = success) */
1470 httpHead(http_t
*http
, /* I - HTTP connection */
1471 const char *uri
) /* I - URI for head */
1473 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http
, uri
));
1474 return (http_send(http
, HTTP_STATE_HEAD
, uri
));
1479 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1480 * default HTTP proxy (if any).
1484 httpInitialize(void)
1486 static int initialized
= 0; /* Have we been called before? */
1488 WSADATA winsockdata
; /* WinSock data */
1495 _cupsGlobalUnlock();
1500 WSAStartup(MAKEWORD(2,2), &winsockdata
);
1502 #elif !defined(SO_NOSIGPIPE)
1504 * Ignore SIGPIPE signals...
1508 sigset(SIGPIPE
, SIG_IGN
);
1510 # elif defined(HAVE_SIGACTION)
1511 struct sigaction action
; /* POSIX sigaction data */
1514 memset(&action
, 0, sizeof(action
));
1515 action
.sa_handler
= SIG_IGN
;
1516 sigaction(SIGPIPE
, &action
, NULL
);
1519 signal(SIGPIPE
, SIG_IGN
);
1520 # endif /* !SO_NOSIGPIPE */
1524 _httpTLSInitialize();
1525 # endif /* HAVE_SSL */
1528 _cupsGlobalUnlock();
1533 * 'httpIsChunked()' - Report whether a message body is chunked.
1535 * This function returns non-zero if the message body is composed of
1536 * variable-length chunks.
1538 * @since CUPS 2.0/OS 10.10@
1541 int /* O - 1 if chunked, 0 if not */
1542 httpIsChunked(http_t
*http
) /* I - HTTP connection */
1544 return (http
? http
->data_encoding
== HTTP_ENCODING_CHUNKED
: 0);
1549 * 'httpIsEncrypted()' - Report whether a connection is encrypted.
1551 * This function returns non-zero if the connection is currently encrypted.
1553 * @since CUPS 2.0/OS 10.10@
1556 int /* O - 1 if encrypted, 0 if not */
1557 httpIsEncrypted(http_t
*http
) /* I - HTTP connection */
1559 return (http
? http
->tls
!= NULL
: 0);
1564 * 'httpOptions()' - Send an OPTIONS request to the server.
1567 int /* O - Status of call (0 = success) */
1568 httpOptions(http_t
*http
, /* I - HTTP connection */
1569 const char *uri
) /* I - URI for options */
1571 return (http_send(http
, HTTP_STATE_OPTIONS
, uri
));
1576 * 'httpPeek()' - Peek at data from a HTTP connection.
1578 * This function copies available data from the given HTTP connection, reading
1579 * a buffer as needed. The data is still available for reading using
1582 * For non-blocking connections the usual timeouts apply.
1584 * @since CUPS 1.7/macOS 10.9@
1587 ssize_t
/* O - Number of bytes copied */
1588 httpPeek(http_t
*http
, /* I - HTTP connection */
1589 char *buffer
, /* I - Buffer for data */
1590 size_t length
) /* I - Maximum number of bytes */
1592 ssize_t bytes
; /* Bytes read */
1593 char len
[32]; /* Length string */
1596 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
1598 if (http
== NULL
|| buffer
== NULL
)
1601 http
->activity
= time(NULL
);
1607 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&&
1608 http
->data_remaining
<= 0)
1610 DEBUG_puts("2httpPeek: Getting chunk length...");
1612 if (httpGets(len
, sizeof(len
), http
) == NULL
)
1614 DEBUG_puts("1httpPeek: Could not get length!");
1620 DEBUG_puts("1httpPeek: Blank chunk length, trying again...");
1621 if (!httpGets(len
, sizeof(len
), http
))
1623 DEBUG_puts("1httpPeek: Could not get chunk length.");
1628 http
->data_remaining
= strtoll(len
, NULL
, 16);
1630 if (http
->data_remaining
< 0)
1632 DEBUG_puts("1httpPeek: Negative chunk length!");
1637 DEBUG_printf(("2httpPeek: data_remaining=" CUPS_LLFMT
,
1638 CUPS_LLCAST http
->data_remaining
));
1640 if (http
->data_remaining
<= 0 && http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
1643 * A zero-length chunk ends a transfer; unless we are reading POST
1648 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1649 http_content_coding_finish(http
);
1650 #endif /* HAVE_LIBZ */
1652 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
1653 httpGets(len
, sizeof(len
), http
);
1655 if (http
->state
== HTTP_STATE_POST_RECV
)
1658 http
->state
= HTTP_STATE_STATUS
;
1660 DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
1661 httpStateString(http
->state
)));
1664 * Prevent future reads for this request...
1667 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
1671 else if (length
> (size_t)http
->data_remaining
)
1672 length
= (size_t)http
->data_remaining
;
1675 if (http
->used
== 0 &&
1676 (http
->coding
== _HTTP_CODING_IDENTITY
||
1677 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)))
1679 if (http
->used
== 0)
1680 #endif /* HAVE_LIBZ */
1683 * Buffer small reads for better performance...
1686 ssize_t buflen
; /* Length of read for buffer */
1688 if (!http
->blocking
)
1690 while (!httpWait(http
, http
->wait_value
))
1692 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1699 if ((size_t)http
->data_remaining
> sizeof(http
->buffer
))
1700 buflen
= sizeof(http
->buffer
);
1702 buflen
= (ssize_t
)http
->data_remaining
;
1704 DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen
));
1705 bytes
= http_read(http
, http
->buffer
, (size_t)buflen
);
1707 DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT
" bytes into buffer.",
1708 CUPS_LLCAST bytes
));
1712 http_debug_hex("httpPeek", http
->buffer
, (int)bytes
);
1715 http
->used
= (int)bytes
;
1720 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1722 # ifdef HAVE_INFLATECOPY
1723 int zerr
; /* Decompressor error */
1724 z_stream stream
; /* Copy of decompressor stream */
1726 if (http
->used
> 0 && http
->stream
.avail_in
< HTTP_MAX_BUFFER
)
1728 size_t buflen
= buflen
= HTTP_MAX_BUFFER
- http
->stream
.avail_in
;
1729 /* Number of bytes to copy */
1731 if (http
->stream
.avail_in
> 0 &&
1732 http
->stream
.next_in
> http
->sbuffer
)
1733 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1735 http
->stream
.next_in
= http
->sbuffer
;
1737 if (buflen
> (size_t)http
->data_remaining
)
1738 buflen
= (size_t)http
->data_remaining
;
1740 if (buflen
> (size_t)http
->used
)
1741 buflen
= (size_t)http
->used
;
1743 DEBUG_printf(("1httpPeek: Copying %d more bytes of data into "
1744 "decompression buffer.", (int)buflen
));
1746 memcpy(http
->sbuffer
+ http
->stream
.avail_in
, http
->buffer
, buflen
);
1747 http
->stream
.avail_in
+= buflen
;
1748 http
->used
-= (int)buflen
;
1749 http
->data_remaining
-= (off_t
)buflen
;
1752 memmove(http
->buffer
, http
->buffer
+ buflen
, (size_t)http
->used
);
1755 DEBUG_printf(("2httpPeek: length=%d, avail_in=%d", (int)length
,
1756 (int)http
->stream
.avail_in
));
1758 if (inflateCopy(&stream
, &(http
->stream
)) != Z_OK
)
1760 DEBUG_puts("2httpPeek: Unable to copy decompressor stream.");
1761 http
->error
= ENOMEM
;
1765 stream
.next_out
= (Bytef
*)buffer
;
1766 stream
.avail_out
= (uInt
)length
;
1768 zerr
= inflate(&stream
, Z_SYNC_FLUSH
);
1769 inflateEnd(&stream
);
1773 DEBUG_printf(("2httpPeek: zerr=%d", zerr
));
1775 http_debug_hex("2httpPeek", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1782 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1785 DEBUG_puts("2httpPeek: No inflateCopy on this platform, httpPeek does not "
1786 "work with compressed streams.");
1788 # endif /* HAVE_INFLATECOPY */
1791 #endif /* HAVE_LIBZ */
1794 if (length
> (size_t)http
->used
)
1795 length
= (size_t)http
->used
;
1797 bytes
= (ssize_t
)length
;
1799 DEBUG_printf(("2httpPeek: grabbing %d bytes from input buffer...",
1802 memcpy(buffer
, http
->buffer
, length
);
1810 if (WSAGetLastError() == WSAEINTR
|| WSAGetLastError() == WSAEWOULDBLOCK
)
1813 http
->error
= WSAGetLastError();
1815 if (errno
== EINTR
|| errno
== EAGAIN
)
1818 http
->error
= errno
;
1821 else if (bytes
== 0)
1823 http
->error
= EPIPE
;
1832 * 'httpPost()' - Send a POST request to the server.
1835 int /* O - Status of call (0 = success) */
1836 httpPost(http_t
*http
, /* I - HTTP connection */
1837 const char *uri
) /* I - URI for post */
1839 return (http_send(http
, HTTP_STATE_POST
, uri
));
1844 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1849 int /* O - Number of bytes written */
1850 httpPrintf(http_t
*http
, /* I - HTTP connection */
1851 const char *format
, /* I - printf-style format string */
1852 ...) /* I - Additional args as needed */
1854 ssize_t bytes
; /* Number of bytes to write */
1855 char buf
[16384]; /* Buffer for formatted string */
1856 va_list ap
; /* Variable argument pointer */
1859 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http
, format
));
1861 va_start(ap
, format
);
1862 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1865 DEBUG_printf(("3httpPrintf: (" CUPS_LLFMT
" bytes) %s", CUPS_LLCAST bytes
, buf
));
1867 if (http
->data_encoding
== HTTP_ENCODING_FIELDS
)
1868 return ((int)httpWrite2(http
, buf
, (size_t)bytes
));
1873 DEBUG_puts("4httpPrintf: flushing existing data...");
1875 if (httpFlushWrite(http
) < 0)
1879 return ((int)http_write(http
, buf
, (size_t)bytes
));
1885 * 'httpPut()' - Send a PUT request to the server.
1888 int /* O - Status of call (0 = success) */
1889 httpPut(http_t
*http
, /* I - HTTP connection */
1890 const char *uri
) /* I - URI to put */
1892 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http
, uri
));
1893 return (http_send(http
, HTTP_STATE_PUT
, uri
));
1898 * 'httpRead()' - Read data from a HTTP connection.
1900 * This function is deprecated. Use the httpRead2() function which can
1901 * read more than 2GB of data.
1903 * @deprecated@ @exclude all@
1906 int /* O - Number of bytes read */
1907 httpRead(http_t
*http
, /* I - HTTP connection */
1908 char *buffer
, /* I - Buffer for data */
1909 int length
) /* I - Maximum number of bytes */
1911 return ((int)httpRead2(http
, buffer
, (size_t)length
));
1916 * 'httpRead2()' - Read data from a HTTP connection.
1918 * @since CUPS 1.2/macOS 10.5@
1921 ssize_t
/* O - Number of bytes read */
1922 httpRead2(http_t
*http
, /* I - HTTP connection */
1923 char *buffer
, /* I - Buffer for data */
1924 size_t length
) /* I - Maximum number of bytes */
1926 ssize_t bytes
; /* Bytes read */
1930 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT
, (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->coding
, http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1932 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT
") data_encoding=%d data_remaining=" CUPS_LLFMT
, (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->data_encoding
, CUPS_LLCAST http
->data_remaining
));
1933 #endif /* HAVE_LIBZ */
1935 if (http
== NULL
|| buffer
== NULL
)
1938 http
->activity
= time(NULL
);
1945 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
1949 if (http
->stream
.avail_in
> 0)
1951 int zerr
; /* Decompressor error */
1953 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d",
1954 (int)http
->stream
.avail_in
, (int)length
));
1956 http
->stream
.next_out
= (Bytef
*)buffer
;
1957 http
->stream
.avail_out
= (uInt
)length
;
1959 if ((zerr
= inflate(&(http
->stream
), Z_SYNC_FLUSH
)) < Z_OK
)
1961 DEBUG_printf(("2httpRead2: zerr=%d", zerr
));
1963 http_debug_hex("2httpRead2", (char *)http
->sbuffer
, (int)http
->stream
.avail_in
);
1970 bytes
= (ssize_t
)(length
- http
->stream
.avail_out
);
1972 DEBUG_printf(("2httpRead2: avail_in=%d, avail_out=%d, bytes=%d",
1973 http
->stream
.avail_in
, http
->stream
.avail_out
,
1981 ssize_t buflen
= HTTP_MAX_BUFFER
- (ssize_t
)http
->stream
.avail_in
;
1982 /* Additional bytes for buffer */
1986 if (http
->stream
.avail_in
> 0 &&
1987 http
->stream
.next_in
> http
->sbuffer
)
1988 memmove(http
->sbuffer
, http
->stream
.next_in
, http
->stream
.avail_in
);
1990 http
->stream
.next_in
= http
->sbuffer
;
1992 DEBUG_printf(("1httpRead2: Reading up to %d more bytes of data into "
1993 "decompression buffer.", (int)buflen
));
1995 if (http
->data_remaining
> 0)
1997 if (buflen
> http
->data_remaining
)
1998 buflen
= (ssize_t
)http
->data_remaining
;
2000 bytes
= http_read_buffered(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2002 else if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2003 bytes
= http_read_chunk(http
, (char *)http
->sbuffer
+ http
->stream
.avail_in
, (size_t)buflen
);
2009 else if (bytes
== 0)
2012 DEBUG_printf(("1httpRead2: Adding " CUPS_LLFMT
" bytes to "
2013 "decompression buffer.", CUPS_LLCAST bytes
));
2015 http
->data_remaining
-= bytes
;
2016 http
->stream
.avail_in
+= (uInt
)bytes
;
2018 if (http
->data_remaining
<= 0 &&
2019 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2022 * Read the trailing blank line now...
2025 char len
[32]; /* Length string */
2027 httpGets(len
, sizeof(len
), http
);
2039 #endif /* HAVE_LIBZ */
2040 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2042 if ((bytes
= http_read_chunk(http
, buffer
, length
)) > 0)
2044 http
->data_remaining
-= bytes
;
2046 if (http
->data_remaining
<= 0)
2049 * Read the trailing blank line now...
2052 char len
[32]; /* Length string */
2054 httpGets(len
, sizeof(len
), http
);
2058 else if (http
->data_remaining
<= 0)
2061 * No more data to read...
2068 DEBUG_printf(("1httpRead2: Reading up to %d bytes into buffer.",
2071 if (length
> (size_t)http
->data_remaining
)
2072 length
= (size_t)http
->data_remaining
;
2074 if ((bytes
= http_read_buffered(http
, buffer
, length
)) > 0)
2076 http
->data_remaining
-= bytes
;
2078 if (http
->data_remaining
<= 0 &&
2079 http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
2082 * Read the trailing blank line now...
2085 char len
[32]; /* Length string */
2087 httpGets(len
, sizeof(len
), http
);
2094 (http
->coding
== _HTTP_CODING_IDENTITY
||
2095 (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
== 0)) &&
2096 #endif /* HAVE_LIBZ */
2097 ((http
->data_remaining
<= 0 &&
2098 http
->data_encoding
== HTTP_ENCODING_LENGTH
) ||
2099 (http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& bytes
== 0)))
2102 if (http
->coding
>= _HTTP_CODING_GUNZIP
)
2103 http_content_coding_finish(http
);
2104 #endif /* HAVE_LIBZ */
2106 if (http
->state
== HTTP_STATE_POST_RECV
)
2108 else if (http
->state
== HTTP_STATE_GET_SEND
||
2109 http
->state
== HTTP_STATE_POST_SEND
)
2110 http
->state
= HTTP_STATE_WAITING
;
2112 http
->state
= HTTP_STATE_STATUS
;
2114 DEBUG_printf(("1httpRead2: End of content, set state to %s.",
2115 httpStateString(http
->state
)));
2123 * 'httpReadRequest()' - Read a HTTP request from a connection.
2125 * @since CUPS 1.7/macOS 10.9@
2128 http_state_t
/* O - New state of connection */
2129 httpReadRequest(http_t
*http
, /* I - HTTP connection */
2130 char *uri
, /* I - URI buffer */
2131 size_t urilen
) /* I - Size of URI buffer */
2133 char line
[4096], /* HTTP request line */
2134 *req_method
, /* HTTP request method */
2135 *req_uri
, /* HTTP request URI */
2136 *req_version
; /* HTTP request version number string */
2140 * Range check input...
2143 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT
")", (void *)http
, (void *)uri
, CUPS_LLCAST urilen
));
2148 if (!http
|| !uri
|| urilen
< 1)
2150 DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR.");
2151 return (HTTP_STATE_ERROR
);
2153 else if (http
->state
!= HTTP_STATE_WAITING
)
2155 DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
2156 httpStateString(http
->state
)));
2157 return (HTTP_STATE_ERROR
);
2164 httpClearFields(http
);
2166 http
->activity
= time(NULL
);
2167 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2168 http
->data_remaining
= 0;
2169 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2170 http
->status
= HTTP_STATUS_OK
;
2171 http
->version
= HTTP_VERSION_1_1
;
2174 * Read a line from the socket...
2177 if (!httpGets(line
, sizeof(line
), http
))
2179 DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR");
2180 return (HTTP_STATE_ERROR
);
2185 DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING");
2186 return (HTTP_STATE_WAITING
);
2189 DEBUG_printf(("1httpReadRequest: %s", line
));
2198 while (*req_uri
&& !isspace(*req_uri
& 255))
2203 DEBUG_puts("1httpReadRequest: No request URI.");
2204 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request URI."), 1);
2205 return (HTTP_STATE_ERROR
);
2210 while (*req_uri
&& isspace(*req_uri
& 255))
2213 req_version
= req_uri
;
2215 while (*req_version
&& !isspace(*req_version
& 255))
2220 DEBUG_puts("1httpReadRequest: No request protocol version.");
2221 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request protocol version."), 1);
2222 return (HTTP_STATE_ERROR
);
2225 *req_version
++ = '\0';
2227 while (*req_version
&& isspace(*req_version
& 255))
2234 if (!strcmp(req_method
, "OPTIONS"))
2235 http
->state
= HTTP_STATE_OPTIONS
;
2236 else if (!strcmp(req_method
, "GET"))
2237 http
->state
= HTTP_STATE_GET
;
2238 else if (!strcmp(req_method
, "HEAD"))
2239 http
->state
= HTTP_STATE_HEAD
;
2240 else if (!strcmp(req_method
, "POST"))
2241 http
->state
= HTTP_STATE_POST
;
2242 else if (!strcmp(req_method
, "PUT"))
2243 http
->state
= HTTP_STATE_PUT
;
2244 else if (!strcmp(req_method
, "DELETE"))
2245 http
->state
= HTTP_STATE_DELETE
;
2246 else if (!strcmp(req_method
, "TRACE"))
2247 http
->state
= HTTP_STATE_TRACE
;
2248 else if (!strcmp(req_method
, "CONNECT"))
2249 http
->state
= HTTP_STATE_CONNECT
;
2252 DEBUG_printf(("1httpReadRequest: Unknown method \"%s\".", req_method
));
2253 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request method."), 1);
2254 return (HTTP_STATE_UNKNOWN_METHOD
);
2257 DEBUG_printf(("1httpReadRequest: Set state to %s.",
2258 httpStateString(http
->state
)));
2260 if (!strcmp(req_version
, "HTTP/1.0"))
2262 http
->version
= HTTP_VERSION_1_0
;
2263 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2265 else if (!strcmp(req_version
, "HTTP/1.1"))
2267 http
->version
= HTTP_VERSION_1_1
;
2268 http
->keep_alive
= HTTP_KEEPALIVE_ON
;
2272 DEBUG_printf(("1httpReadRequest: Unknown version \"%s\".", req_version
));
2273 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown request version."), 1);
2274 return (HTTP_STATE_UNKNOWN_VERSION
);
2277 DEBUG_printf(("1httpReadRequest: URI is \"%s\".", req_uri
));
2278 strlcpy(uri
, req_uri
, urilen
);
2280 return (http
->state
);
2285 * 'httpReconnect()' - Reconnect to a HTTP server.
2287 * This function is deprecated. Please use the @link httpReconnect2@ function
2290 * @deprecated@ @exclude all@
2293 int /* O - 0 on success, non-zero on failure */
2294 httpReconnect(http_t
*http
) /* I - HTTP connection */
2296 DEBUG_printf(("httpReconnect(http=%p)", (void *)http
));
2298 return (httpReconnect2(http
, 30000, NULL
));
2303 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2307 int /* O - 0 on success, non-zero on failure */
2308 httpReconnect2(http_t
*http
, /* I - HTTP connection */
2309 int msec
, /* I - Timeout in milliseconds */
2310 int *cancel
) /* I - Pointer to "cancel" variable */
2312 http_addrlist_t
*addr
; /* Connected address */
2314 http_addrlist_t
*current
; /* Current address */
2315 char temp
[256]; /* Temporary address string */
2319 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http
, msec
, (void *)cancel
));
2323 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2330 DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS...");
2333 #endif /* HAVE_SSL */
2336 * Close any previously open socket...
2341 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http
->fd
));
2343 httpAddrClose(NULL
, http
->fd
);
2349 * Reset all state (except fields, which may be reused)...
2352 http
->state
= HTTP_STATE_WAITING
;
2353 http
->version
= HTTP_VERSION_1_1
;
2354 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
2355 memset(&http
->_hostaddr
, 0, sizeof(http
->_hostaddr
));
2356 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
2357 http
->_data_remaining
= 0;
2359 http
->data_remaining
= 0;
2360 http
->hostaddr
= NULL
;
2364 * Connect to the server...
2368 for (current
= http
->addrlist
; current
; current
= current
->next
)
2369 DEBUG_printf(("2httpReconnect2: Address %s:%d",
2370 httpAddrString(&(current
->addr
), temp
, sizeof(temp
)),
2371 httpAddrPort(&(current
->addr
))));
2374 if ((addr
= httpAddrConnect2(http
->addrlist
, &(http
->fd
), msec
, cancel
)) == NULL
)
2377 * Unable to connect...
2381 http
->error
= WSAGetLastError();
2383 http
->error
= errno
;
2385 http
->status
= HTTP_STATUS_ERROR
;
2387 DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s",
2388 strerror(http
->error
)));
2393 DEBUG_printf(("2httpReconnect2: New socket=%d", http
->fd
));
2395 if (http
->timeout_value
> 0)
2396 http_set_timeout(http
->fd
, http
->timeout_value
);
2398 http
->hostaddr
= &(addr
->addr
);
2402 if (http
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
2405 * Always do encryption via SSL.
2408 if (_httpTLSStart(http
) != 0)
2410 httpAddrClose(NULL
, http
->fd
);
2415 else if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls_upgrade
)
2416 return (http_tls_upgrade(http
));
2417 #endif /* HAVE_SSL */
2419 DEBUG_printf(("1httpReconnect2: Connected to %s:%d...",
2420 httpAddrString(http
->hostaddr
, temp
, sizeof(temp
)),
2421 httpAddrPort(http
->hostaddr
)));
2428 * 'httpSetAuthString()' - Set the current authorization string.
2430 * This function just stores a copy of the current authorization string in
2431 * the HTTP connection object. You must still call @link httpSetField@ to set
2432 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2433 * @link httpGet@, @link httpHead@, @link httpOptions@, @link httpPost@, or
2436 * @since CUPS 1.3/macOS 10.5@
2440 httpSetAuthString(http_t
*http
, /* I - HTTP connection */
2441 const char *scheme
, /* I - Auth scheme (NULL to clear it) */
2442 const char *data
) /* I - Auth data (NULL for none) */
2445 * Range check input...
2451 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
2452 free(http
->authstring
);
2454 http
->authstring
= http
->_authstring
;
2459 * Set the current authorization string...
2462 size_t len
= strlen(scheme
) + (data
? strlen(data
) + 1 : 0) + 1;
2465 if (len
> sizeof(http
->_authstring
))
2467 if ((temp
= malloc(len
)) == NULL
)
2468 len
= sizeof(http
->_authstring
);
2470 http
->authstring
= temp
;
2474 snprintf(http
->authstring
, len
, "%s %s", scheme
, data
);
2476 strlcpy(http
->authstring
, scheme
, len
);
2481 * Clear the current authorization string...
2484 http
->_authstring
[0] = '\0';
2490 * 'httpSetCredentials()' - Set the credentials associated with an encrypted
2493 * @since CUPS 1.5/macOS 10.7@
2496 int /* O - Status of call (0 = success) */
2497 httpSetCredentials(http_t
*http
, /* I - HTTP connection */
2498 cups_array_t
*credentials
) /* I - Array of credentials */
2500 if (!http
|| cupsArrayCount(credentials
) < 1)
2504 _httpFreeCredentials(http
->tls_credentials
);
2506 http
->tls_credentials
= _httpCreateCredentials(credentials
);
2507 #endif /* HAVE_SSL */
2509 return (http
->tls_credentials
? 0 : -1);
2514 * 'httpSetCookie()' - Set the cookie value(s).
2516 * @since CUPS 1.1.19/macOS 10.3@
2520 httpSetCookie(http_t
*http
, /* I - Connection */
2521 const char *cookie
) /* I - Cookie string */
2530 http
->cookie
= strdup(cookie
);
2532 http
->cookie
= NULL
;
2537 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2539 * Currently only @code HTTP_FIELD_ACCEPT_ENCODING@, @code HTTP_FIELD_SERVER@,
2540 * and @code HTTP_FIELD_USER_AGENT@ can be set.
2542 * @since CUPS 1.7/macOS 10.9@
2546 httpSetDefaultField(http_t
*http
, /* I - HTTP connection */
2547 http_field_t field
, /* I - Field index */
2548 const char *value
)/* I - Value */
2550 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2552 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
)
2555 if (http
->default_fields
[field
])
2556 free(http
->default_fields
[field
]);
2558 http
->default_fields
[field
] = value
? strdup(value
) : NULL
;
2563 * 'httpSetExpect()' - Set the Expect: header in a request.
2565 * Currently only @code HTTP_STATUS_CONTINUE@ is supported for the "expect"
2568 * @since CUPS 1.2/macOS 10.5@
2572 httpSetExpect(http_t
*http
, /* I - HTTP connection */
2573 http_status_t expect
) /* I - HTTP status to expect
2574 (@code HTTP_STATUS_CONTINUE@) */
2576 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http
, expect
));
2579 http
->expect
= expect
;
2584 * 'httpSetField()' - Set the value of an HTTP header.
2588 httpSetField(http_t
*http
, /* I - HTTP connection */
2589 http_field_t field
, /* I - Field index */
2590 const char *value
) /* I - Value */
2592 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http
, field
, http_fields
[field
], value
));
2594 if (!http
|| field
<= HTTP_FIELD_UNKNOWN
|| field
>= HTTP_FIELD_MAX
|| !value
)
2597 http_add_field(http
, field
, value
, 0);
2602 * 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection.
2604 * @since CUPS 2.0/OS 10.10@
2609 http_t
*http
, /* I - HTTP connection */
2610 http_keepalive_t keep_alive
) /* I - New Keep-Alive value */
2613 http
->keep_alive
= keep_alive
;
2618 * 'httpSetLength()' - Set the content-length and content-encoding.
2620 * @since CUPS 1.2/macOS 10.5@
2624 httpSetLength(http_t
*http
, /* I - HTTP connection */
2625 size_t length
) /* I - Length (0 for chunked) */
2627 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT
")", (void *)http
, CUPS_LLCAST length
));
2634 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
2635 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "");
2639 char len
[32]; /* Length string */
2642 snprintf(len
, sizeof(len
), CUPS_LLFMT
, CUPS_LLCAST length
);
2643 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "");
2644 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, len
);
2650 * 'httpSetTimeout()' - Set read/write timeouts and an optional callback.
2652 * The optional timeout callback receives both the HTTP connection and a user
2653 * data pointer and must return 1 to continue or 0 to error (time) out.
2655 * @since CUPS 1.5/macOS 10.7@
2660 http_t
*http
, /* I - HTTP connection */
2661 double timeout
, /* I - Number of seconds for timeout,
2662 must be greater than 0 */
2663 http_timeout_cb_t cb
, /* I - Callback function or @code NULL@ */
2664 void *user_data
) /* I - User data pointer */
2666 if (!http
|| timeout
<= 0.0)
2669 http
->timeout_cb
= cb
;
2670 http
->timeout_data
= user_data
;
2671 http
->timeout_value
= timeout
;
2674 http_set_timeout(http
->fd
, timeout
);
2676 http_set_wait(http
);
2681 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2683 * @since CUPS 2.0/OS 10.10@
2687 httpShutdown(http_t
*http
) /* I - HTTP connection */
2689 if (!http
|| http
->fd
< 0)
2695 #endif /* HAVE_SSL */
2698 shutdown(http
->fd
, SD_RECEIVE
); /* Microsoft-ism... */
2700 shutdown(http
->fd
, SHUT_RD
);
2706 * 'httpTrace()' - Send an TRACE request to the server.
2711 int /* O - Status of call (0 = success) */
2712 httpTrace(http_t
*http
, /* I - HTTP connection */
2713 const char *uri
) /* I - URI for trace */
2715 return (http_send(http
, HTTP_STATE_TRACE
, uri
));
2720 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2722 * Note: Unlike httpUpdate(), this function does not flush pending write data
2723 * and only retrieves a single status line from the HTTP connection.
2726 int /* O - 1 to continue, 0 to stop */
2727 _httpUpdate(http_t
*http
, /* I - HTTP connection */
2728 http_status_t
*status
) /* O - Current HTTP status */
2730 char line
[32768], /* Line from connection... */
2731 *value
; /* Pointer to value on line */
2732 http_field_t field
; /* Field index */
2733 int major
, minor
; /* HTTP version numbers */
2736 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http
, (void *)status
, httpStateString(http
->state
)));
2739 * Grab a single line from the connection...
2742 if (!httpGets(line
, sizeof(line
), http
))
2744 *status
= HTTP_STATUS_ERROR
;
2748 DEBUG_printf(("2_httpUpdate: Got \"%s\"", line
));
2750 if (line
[0] == '\0')
2753 * Blank line means the start of the data section (if any). Return
2754 * the result code, too...
2756 * If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change
2757 * states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and
2761 if (http
->status
== HTTP_STATUS_CONTINUE
)
2763 *status
= http
->status
;
2767 if (http
->status
< HTTP_STATUS_BAD_REQUEST
)
2768 http
->digest_tries
= 0;
2771 if (http
->status
== HTTP_STATUS_SWITCHING_PROTOCOLS
&& !http
->tls
)
2773 if (_httpTLSStart(http
) != 0)
2775 httpAddrClose(NULL
, http
->fd
);
2777 *status
= http
->status
= HTTP_STATUS_ERROR
;
2781 *status
= HTTP_STATUS_CONTINUE
;
2784 #endif /* HAVE_SSL */
2786 if (http_set_length(http
) < 0)
2788 DEBUG_puts("1_httpUpdate: Bad Content-Length.");
2789 http
->error
= EINVAL
;
2790 http
->status
= *status
= HTTP_STATUS_ERROR
;
2794 switch (http
->state
)
2796 case HTTP_STATE_GET
:
2797 case HTTP_STATE_POST
:
2798 case HTTP_STATE_POST_RECV
:
2799 case HTTP_STATE_PUT
:
2802 DEBUG_printf(("1_httpUpdate: Set state to %s.",
2803 httpStateString(http
->state
)));
2805 case HTTP_STATE_POST_SEND
:
2806 case HTTP_STATE_HEAD
:
2810 http
->state
= HTTP_STATE_WAITING
;
2812 DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING.");
2817 DEBUG_puts("1_httpUpdate: Calling http_content_coding_start.");
2818 http_content_coding_start(http
,
2819 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
2820 #endif /* HAVE_LIBZ */
2822 *status
= http
->status
;
2825 else if (!strncmp(line
, "HTTP/", 5) && http
->mode
== _HTTP_MODE_CLIENT
)
2828 * Got the beginning of a response...
2831 int intstatus
; /* Status value as an integer */
2833 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &intstatus
) != 3)
2835 *status
= http
->status
= HTTP_STATUS_ERROR
;
2839 httpClearFields(http
);
2841 http
->version
= (http_version_t
)(major
* 100 + minor
);
2842 *status
= http
->status
= (http_status_t
)intstatus
;
2844 else if ((value
= strchr(line
, ':')) != NULL
)
2851 while (_cups_isspace(*value
))
2854 DEBUG_printf(("1_httpUpdate: Header %s: %s", line
, value
));
2857 * Be tolerants of servers that send unknown attribute fields...
2860 if (!_cups_strcasecmp(line
, "expect"))
2863 * "Expect: 100-continue" or similar...
2866 http
->expect
= (http_status_t
)atoi(value
);
2868 else if (!_cups_strcasecmp(line
, "cookie"))
2871 * "Cookie: name=value[; name=value ...]" - replaces previous cookies...
2874 httpSetCookie(http
, value
);
2876 else if ((field
= httpFieldValue(line
)) != HTTP_FIELD_UNKNOWN
)
2877 http_add_field(http
, field
, value
, 1);
2880 DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line
));
2885 DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line
));
2886 http
->error
= EINVAL
;
2887 http
->status
= *status
= HTTP_STATUS_ERROR
;
2896 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2899 http_status_t
/* O - HTTP status */
2900 httpUpdate(http_t
*http
) /* I - HTTP connection */
2902 http_status_t status
; /* Request status */
2905 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http
, httpStateString(http
->state
)));
2908 * Flush pending data, if any...
2913 DEBUG_puts("2httpUpdate: flushing buffer...");
2915 if (httpFlushWrite(http
) < 0)
2916 return (HTTP_STATUS_ERROR
);
2920 * If we haven't issued any commands, then there is nothing to "update"...
2923 if (http
->state
== HTTP_STATE_WAITING
)
2924 return (HTTP_STATUS_CONTINUE
);
2927 * Grab all of the lines we can from the connection...
2930 while (_httpUpdate(http
, &status
));
2933 * See if there was an error...
2936 if (http
->error
== EPIPE
&& http
->status
> HTTP_STATUS_CONTINUE
)
2938 DEBUG_printf(("1httpUpdate: Returning status %d...", http
->status
));
2939 return (http
->status
);
2944 DEBUG_printf(("1httpUpdate: socket error %d - %s", http
->error
,
2945 strerror(http
->error
)));
2946 http
->status
= HTTP_STATUS_ERROR
;
2947 return (HTTP_STATUS_ERROR
);
2951 * Return the current status...
2959 * '_httpWait()' - Wait for data available on a connection (no flush).
2962 int /* O - 1 if data is available, 0 otherwise */
2963 _httpWait(http_t
*http
, /* I - HTTP connection */
2964 int msec
, /* I - Milliseconds to wait */
2965 int usessl
) /* I - Use SSL context? */
2968 struct pollfd pfd
; /* Polled file descriptor */
2970 fd_set input_set
; /* select() input set */
2971 struct timeval timeout
; /* Timeout */
2972 #endif /* HAVE_POLL */
2973 int nfds
; /* Result from select()/poll() */
2976 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http
, msec
, usessl
));
2980 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http
->fd
));
2985 * Check the SSL/TLS buffers for data first...
2989 if (http
->tls
&& _httpTLSPending(http
))
2991 DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data.");
2994 #endif /* HAVE_SSL */
2997 * Then try doing a select() or poll() to poll the socket...
3002 pfd
.events
= POLLIN
;
3006 nfds
= poll(&pfd
, 1, msec
);
3008 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3013 FD_ZERO(&input_set
);
3014 FD_SET(http
->fd
, &input_set
);
3016 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec
, http
->fd
));
3020 timeout
.tv_sec
= msec
/ 1000;
3021 timeout
.tv_usec
= (msec
% 1000) * 1000;
3023 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, &timeout
);
3026 nfds
= select(http
->fd
+ 1, &input_set
, NULL
, NULL
, NULL
);
3028 DEBUG_printf(("6_httpWait: select() returned %d...", nfds
));
3031 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
3032 WSAGetLastError() == WSAEWOULDBLOCK
));
3034 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
3036 #endif /* HAVE_POLL */
3038 DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds
,
3046 * 'httpWait()' - Wait for data available on a connection.
3048 * @since CUPS 1.1.19/macOS 10.3@
3051 int /* O - 1 if data is available, 0 otherwise */
3052 httpWait(http_t
*http
, /* I - HTTP connection */
3053 int msec
) /* I - Milliseconds to wait */
3056 * First see if there is data in the buffer...
3059 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http
, msec
));
3066 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3071 if (http
->coding
>= _HTTP_CODING_GUNZIP
&& http
->stream
.avail_in
> 0)
3073 DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready.");
3076 #endif /* HAVE_LIBZ */
3079 * Flush pending data, if any...
3084 DEBUG_puts("3httpWait: Flushing write buffer.");
3086 if (httpFlushWrite(http
) < 0)
3091 * If not, check the SSL/TLS buffers and do a select() on the connection...
3094 return (_httpWait(http
, msec
, 1));
3099 * 'httpWrite()' - Write data to a HTTP connection.
3101 * This function is deprecated. Use the httpWrite2() function which can
3102 * write more than 2GB of data.
3104 * @deprecated@ @exclude all@
3107 int /* O - Number of bytes written */
3108 httpWrite(http_t
*http
, /* I - HTTP connection */
3109 const char *buffer
, /* I - Buffer for data */
3110 int length
) /* I - Number of bytes to write */
3112 return ((int)httpWrite2(http
, buffer
, (size_t)length
));
3117 * 'httpWrite2()' - Write data to a HTTP connection.
3119 * @since CUPS 1.2/macOS 10.5@
3122 ssize_t
/* O - Number of bytes written */
3123 httpWrite2(http_t
*http
, /* I - HTTP connection */
3124 const char *buffer
, /* I - Buffer for data */
3125 size_t length
) /* I - Number of bytes to write */
3127 ssize_t bytes
; /* Bytes written */
3130 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
3133 * Range check input...
3136 if (!http
|| !buffer
)
3138 DEBUG_puts("1httpWrite2: Returning -1 due to bad input.");
3143 * Mark activity on the connection...
3146 http
->activity
= time(NULL
);
3149 * Buffer small writes for better performance...
3153 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3155 DEBUG_printf(("1httpWrite2: http->coding=%d", http
->coding
));
3159 http_content_coding_finish(http
);
3164 size_t slen
; /* Bytes to write */
3165 ssize_t sret
; /* Bytes written */
3167 http
->stream
.next_in
= (Bytef
*)buffer
;
3168 http
->stream
.avail_in
= (uInt
)length
;
3170 while (deflate(&(http
->stream
), Z_NO_FLUSH
) == Z_OK
)
3172 DEBUG_printf(("1httpWrite2: avail_out=%d", http
->stream
.avail_out
));
3174 if (http
->stream
.avail_out
> 0)
3177 slen
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3179 DEBUG_printf(("1httpWrite2: Writing intermediate chunk, len=%d", (int)slen
));
3181 if (slen
> 0 && http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3182 sret
= http_write_chunk(http
, (char *)http
->sbuffer
, slen
);
3184 sret
= http_write(http
, (char *)http
->sbuffer
, slen
);
3190 DEBUG_puts("1httpWrite2: Unable to write, returning -1.");
3194 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3195 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3198 bytes
= (ssize_t
)length
;
3202 #endif /* HAVE_LIBZ */
3205 if (http
->wused
&& (length
+ (size_t)http
->wused
) > sizeof(http
->wbuffer
))
3207 DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length="
3208 CUPS_LLFMT
")", http
->wused
, CUPS_LLCAST length
));
3210 httpFlushWrite(http
);
3213 if ((length
+ (size_t)http
->wused
) <= sizeof(http
->wbuffer
) && length
< sizeof(http
->wbuffer
))
3216 * Write to buffer...
3219 DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT
" bytes to wbuffer...",
3220 CUPS_LLCAST length
));
3222 memcpy(http
->wbuffer
+ http
->wused
, buffer
, length
);
3223 http
->wused
+= (int)length
;
3224 bytes
= (ssize_t
)length
;
3229 * Otherwise write the data directly...
3232 DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT
" bytes to socket...",
3233 CUPS_LLCAST length
));
3235 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3236 bytes
= (ssize_t
)http_write_chunk(http
, buffer
, length
);
3238 bytes
= (ssize_t
)http_write(http
, buffer
, length
);
3240 DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT
" bytes...",
3241 CUPS_LLCAST bytes
));
3244 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
)
3245 http
->data_remaining
-= bytes
;
3251 * Handle end-of-request processing...
3254 if ((http
->data_encoding
== HTTP_ENCODING_CHUNKED
&& length
== 0) ||
3255 (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0))
3258 * Finished with the transfer; unless we are sending POST or PUT
3263 if (http
->coding
== _HTTP_CODING_GZIP
|| http
->coding
== _HTTP_CODING_DEFLATE
)
3264 http_content_coding_finish(http
);
3265 #endif /* HAVE_LIBZ */
3269 if (httpFlushWrite(http
) < 0)
3273 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3276 * Send a 0-length chunk at the end of the request...
3279 http_write(http
, "0\r\n\r\n", 5);
3282 * Reset the data state...
3285 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3286 http
->data_remaining
= 0;
3289 if (http
->state
== HTTP_STATE_POST_RECV
)
3291 else if (http
->state
== HTTP_STATE_POST_SEND
||
3292 http
->state
== HTTP_STATE_GET_SEND
)
3293 http
->state
= HTTP_STATE_WAITING
;
3295 http
->state
= HTTP_STATE_STATUS
;
3297 DEBUG_printf(("2httpWrite2: Changed state to %s.",
3298 httpStateString(http
->state
)));
3301 DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT
".", CUPS_LLCAST bytes
));
3308 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3310 * @since CUPS 1.7/macOS 10.9@
3313 int /* O - 0 on success, -1 on error */
3314 httpWriteResponse(http_t
*http
, /* I - HTTP connection */
3315 http_status_t status
) /* I - Status code */
3317 http_encoding_t old_encoding
; /* Old data_encoding value */
3318 off_t old_remaining
; /* Old data_remaining value */
3322 * Range check input...
3325 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http
, status
));
3327 if (!http
|| status
< HTTP_STATUS_CONTINUE
)
3329 DEBUG_puts("1httpWriteResponse: Bad input.");
3334 * Set the various standard fields if they aren't already...
3337 if (!http
->fields
[HTTP_FIELD_DATE
])
3338 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString(time(NULL
)));
3340 if (status
>= HTTP_STATUS_BAD_REQUEST
&& http
->keep_alive
)
3342 http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
3343 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "");
3346 if (http
->version
== HTTP_VERSION_1_1
)
3348 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3350 if (http
->keep_alive
)
3351 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Keep-Alive");
3353 httpSetField(http
, HTTP_FIELD_CONNECTION
, "close");
3356 if (http
->keep_alive
&& !http
->fields
[HTTP_FIELD_KEEP_ALIVE
])
3357 httpSetField(http
, HTTP_FIELD_KEEP_ALIVE
, "timeout=10");
3361 if (status
== HTTP_STATUS_UPGRADE_REQUIRED
||
3362 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3364 if (!http
->fields
[HTTP_FIELD_CONNECTION
])
3365 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
3367 if (!http
->fields
[HTTP_FIELD_UPGRADE
])
3368 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
3370 if (!http
->fields
[HTTP_FIELD_CONTENT_LENGTH
])
3371 httpSetField(http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
3373 #endif /* HAVE_SSL */
3375 if (!http
->fields
[HTTP_FIELD_SERVER
])
3376 httpSetField(http
, HTTP_FIELD_SERVER
, http
->default_fields
[HTTP_FIELD_SERVER
] ? http
->default_fields
[HTTP_FIELD_SERVER
] : CUPS_MINIMAL
);
3379 * Set the Accept-Encoding field if it isn't already...
3382 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
])
3383 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] ? http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
] :
3385 "gzip, deflate, identity");
3388 #endif /* HAVE_LIBZ */
3391 * Send the response header...
3394 old_encoding
= http
->data_encoding
;
3395 old_remaining
= http
->data_remaining
;
3396 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
3398 if (httpPrintf(http
, "HTTP/%d.%d %d %s\r\n", http
->version
/ 100, http
->version
% 100, (int)status
, httpStatus(status
)) < 0)
3400 http
->status
= HTTP_STATUS_ERROR
;
3404 if (status
!= HTTP_STATUS_CONTINUE
)
3407 * 100 Continue doesn't have the rest of the response headers...
3410 int i
; /* Looping var */
3411 const char *value
; /* Field value */
3413 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
3415 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
3417 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
3419 http
->status
= HTTP_STATUS_ERROR
;
3427 if (strchr(http
->cookie
, ';'))
3429 if (httpPrintf(http
, "Set-Cookie: %s\r\n", http
->cookie
) < 1)
3431 http
->status
= HTTP_STATUS_ERROR
;
3435 else if (httpPrintf(http
, "Set-Cookie: %s; path=/; httponly;%s\r\n", http
->cookie
, http
->tls
? " secure;" : "") < 1)
3437 http
->status
= HTTP_STATUS_ERROR
;
3443 * "Click-jacking" defense (STR #4492)...
3446 if (httpPrintf(http
, "X-Frame-Options: DENY\r\n"
3447 "Content-Security-Policy: frame-ancestors 'none'\r\n") < 1)
3449 http
->status
= HTTP_STATUS_ERROR
;
3454 if (httpWrite2(http
, "\r\n", 2) < 2)
3456 http
->status
= HTTP_STATUS_ERROR
;
3460 if (httpFlushWrite(http
) < 0)
3462 http
->status
= HTTP_STATUS_ERROR
;
3466 if (status
== HTTP_STATUS_CONTINUE
||
3467 status
== HTTP_STATUS_SWITCHING_PROTOCOLS
)
3470 * Restore the old data_encoding and data_length values...
3473 http
->data_encoding
= old_encoding
;
3474 http
->data_remaining
= old_remaining
;
3476 if (old_remaining
<= INT_MAX
)
3477 http
->_data_remaining
= (int)old_remaining
;
3479 http
->_data_remaining
= INT_MAX
;
3481 else if (http
->state
== HTTP_STATE_OPTIONS
||
3482 http
->state
== HTTP_STATE_HEAD
||
3483 http
->state
== HTTP_STATE_PUT
||
3484 http
->state
== HTTP_STATE_TRACE
||
3485 http
->state
== HTTP_STATE_CONNECT
||
3486 http
->state
== HTTP_STATE_STATUS
)
3488 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3489 "was %s.", httpStateString(http
->state
)));
3490 http
->state
= HTTP_STATE_WAITING
;
3495 * Force data_encoding and data_length to be set according to the response
3499 http_set_length(http
);
3501 if (http
->data_encoding
== HTTP_ENCODING_LENGTH
&& http
->data_remaining
== 0)
3503 DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
3504 "was %s.", httpStateString(http
->state
)));
3505 http
->state
= HTTP_STATE_WAITING
;
3509 if (http
->state
== HTTP_STATE_POST_RECV
|| http
->state
== HTTP_STATE_GET
)
3514 * Then start any content encoding...
3517 DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start.");
3518 http_content_coding_start(http
,
3519 httpGetField(http
, HTTP_FIELD_CONTENT_ENCODING
));
3520 #endif /* HAVE_LIBZ */
3529 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3533 http_add_field(http_t
*http
, /* I - HTTP connection */
3534 http_field_t field
, /* I - HTTP field */
3535 const char *value
, /* I - Value string */
3536 int append
) /* I - Append value? */
3538 char temp
[1024]; /* Temporary value string */
3539 size_t fieldlen
, /* Length of existing value */
3540 valuelen
, /* Length of value string */
3541 total
; /* Total length of string */
3544 if (field
== HTTP_FIELD_HOST
)
3547 * Special-case for Host: as we don't want a trailing "." on the hostname and
3548 * need to bracket IPv6 numeric addresses.
3551 char *ptr
= strchr(value
, ':');
3553 if (value
[0] != '[' && ptr
&& strchr(ptr
+ 1, ':'))
3556 * Bracket IPv6 numeric addresses...
3558 * This is slightly inefficient (basically copying twice), but is an edge
3559 * case and not worth optimizing...
3562 snprintf(temp
, sizeof(temp
), "[%s]", value
);
3568 * Check for a trailing dot on the hostname...
3571 strlcpy(temp
, value
, sizeof(temp
));
3573 ptr
= temp
+ strlen(temp
) - 1;
3580 if (append
&& field
!= HTTP_FIELD_ACCEPT_ENCODING
&& field
!= HTTP_FIELD_ACCEPT_LANGUAGE
&& field
!= HTTP_FIELD_ACCEPT_RANGES
&& field
!= HTTP_FIELD_ALLOW
&& field
!= HTTP_FIELD_LINK
&& field
!= HTTP_FIELD_TRANSFER_ENCODING
&& field
!= HTTP_FIELD_UPGRADE
&& field
!= HTTP_FIELD_WWW_AUTHENTICATE
)
3583 if (!append
&& http
->fields
[field
])
3585 if (http
->fields
[field
] != http
->_fields
[field
])
3586 free(http
->fields
[field
]);
3588 http
->fields
[field
] = NULL
;
3591 valuelen
= strlen(value
);
3595 http
->_fields
[field
][0] = '\0';
3599 if (http
->fields
[field
])
3601 fieldlen
= strlen(http
->fields
[field
]);
3602 total
= fieldlen
+ 2 + valuelen
;
3610 if (total
< HTTP_MAX_VALUE
&& field
< HTTP_FIELD_ACCEPT_ENCODING
)
3613 * Copy short values to legacy char arrays (maintained for binary
3614 * compatibility with CUPS 1.2.x and earlier applications...)
3619 char combined
[HTTP_MAX_VALUE
];
3620 /* Combined value string */
3622 snprintf(combined
, sizeof(combined
), "%s, %s", http
->_fields
[field
], value
);
3626 strlcpy(http
->_fields
[field
], value
, sizeof(http
->_fields
[field
]));
3627 http
->fields
[field
] = http
->_fields
[field
];
3632 * Expand the field value...
3635 char *combined
; /* New value string */
3637 if ((combined
= realloc(http
->fields
[field
], total
+ 1)) != NULL
)
3639 http
->fields
[field
] = combined
;
3640 strlcat(combined
, ", ", total
+ 1);
3641 strlcat(combined
, value
, total
+ 1);
3647 * Allocate the field value...
3650 http
->fields
[field
] = strdup(value
);
3654 if (field
== HTTP_FIELD_CONTENT_ENCODING
&& http
->data_encoding
!= HTTP_ENCODING_FIELDS
)
3656 DEBUG_puts("1httpSetField: Calling http_content_coding_start.");
3657 http_content_coding_start(http
, value
);
3659 #endif /* HAVE_LIBZ */
3665 * 'http_content_coding_finish()' - Finish doing any content encoding.
3669 http_content_coding_finish(
3670 http_t
*http
) /* I - HTTP connection */
3672 int zerr
; /* Compression status */
3673 Byte dummy
[1]; /* Dummy read buffer */
3674 size_t bytes
; /* Number of bytes to write */
3677 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http
));
3678 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http
->coding
));
3680 switch (http
->coding
)
3682 case _HTTP_CODING_DEFLATE
:
3683 case _HTTP_CODING_GZIP
:
3684 http
->stream
.next_in
= dummy
;
3685 http
->stream
.avail_in
= 0;
3689 zerr
= deflate(&(http
->stream
), Z_FINISH
);
3690 bytes
= _HTTP_MAX_SBUFFER
- http
->stream
.avail_out
;
3694 DEBUG_printf(("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes
));
3696 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
3697 http_write_chunk(http
, (char *)http
->sbuffer
, bytes
);
3699 http_write(http
, (char *)http
->sbuffer
, bytes
);
3702 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3703 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3705 while (zerr
== Z_OK
);
3707 deflateEnd(&(http
->stream
));
3709 free(http
->sbuffer
);
3710 http
->sbuffer
= NULL
;
3713 httpFlushWrite(http
);
3716 case _HTTP_CODING_INFLATE
:
3717 case _HTTP_CODING_GUNZIP
:
3718 inflateEnd(&(http
->stream
));
3719 free(http
->sbuffer
);
3720 http
->sbuffer
= NULL
;
3727 http
->coding
= _HTTP_CODING_IDENTITY
;
3732 * 'http_content_coding_start()' - Start doing content encoding.
3736 http_content_coding_start(
3737 http_t
*http
, /* I - HTTP connection */
3738 const char *value
) /* I - Value of Content-Encoding */
3740 int zerr
; /* Error/status */
3741 _http_coding_t coding
; /* Content coding value */
3744 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http
, value
));
3746 if (http
->coding
!= _HTTP_CODING_IDENTITY
)
3748 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3752 else if (!strcmp(value
, "x-gzip") || !strcmp(value
, "gzip"))
3754 if (http
->state
== HTTP_STATE_GET_SEND
||
3755 http
->state
== HTTP_STATE_POST_SEND
)
3756 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_GZIP
:
3757 _HTTP_CODING_GUNZIP
;
3758 else if (http
->state
== HTTP_STATE_POST_RECV
||
3759 http
->state
== HTTP_STATE_PUT_RECV
)
3760 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_GZIP
:
3761 _HTTP_CODING_GUNZIP
;
3764 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3768 else if (!strcmp(value
, "x-deflate") || !strcmp(value
, "deflate"))
3770 if (http
->state
== HTTP_STATE_GET_SEND
||
3771 http
->state
== HTTP_STATE_POST_SEND
)
3772 coding
= http
->mode
== _HTTP_MODE_SERVER
? _HTTP_CODING_DEFLATE
:
3773 _HTTP_CODING_INFLATE
;
3774 else if (http
->state
== HTTP_STATE_POST_RECV
||
3775 http
->state
== HTTP_STATE_PUT_RECV
)
3776 coding
= http
->mode
== _HTTP_MODE_CLIENT
? _HTTP_CODING_DEFLATE
:
3777 _HTTP_CODING_INFLATE
;
3780 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3786 DEBUG_puts("1http_content_coding_start: Not doing content coding.");
3790 memset(&(http
->stream
), 0, sizeof(http
->stream
));
3794 case _HTTP_CODING_DEFLATE
:
3795 case _HTTP_CODING_GZIP
:
3797 httpFlushWrite(http
);
3799 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3801 http
->status
= HTTP_STATUS_ERROR
;
3802 http
->error
= errno
;
3807 * Window size for compression is 11 bits - optimal based on PWG Raster
3808 * sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB
3812 if ((zerr
= deflateInit2(&(http
->stream
), Z_DEFAULT_COMPRESSION
,
3814 coding
== _HTTP_CODING_DEFLATE
? -11 : 27, 7,
3815 Z_DEFAULT_STRATEGY
)) < Z_OK
)
3817 http
->status
= HTTP_STATUS_ERROR
;
3818 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3822 http
->stream
.next_out
= (Bytef
*)http
->sbuffer
;
3823 http
->stream
.avail_out
= (uInt
)_HTTP_MAX_SBUFFER
;
3826 case _HTTP_CODING_INFLATE
:
3827 case _HTTP_CODING_GUNZIP
:
3828 if ((http
->sbuffer
= malloc(_HTTP_MAX_SBUFFER
)) == NULL
)
3830 http
->status
= HTTP_STATUS_ERROR
;
3831 http
->error
= errno
;
3836 * Window size for decompression is up to 15 bits (maximum supported).
3837 * -15 is raw inflate, 31 is gunzip, per ZLIB documentation.
3840 if ((zerr
= inflateInit2(&(http
->stream
),
3841 coding
== _HTTP_CODING_INFLATE
? -15 : 31))
3844 free(http
->sbuffer
);
3845 http
->sbuffer
= NULL
;
3846 http
->status
= HTTP_STATUS_ERROR
;
3847 http
->error
= zerr
== Z_MEM_ERROR
? ENOMEM
: EINVAL
;
3851 http
->stream
.avail_in
= 0;
3852 http
->stream
.next_in
= http
->sbuffer
;
3859 http
->coding
= coding
;
3861 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3864 #endif /* HAVE_LIBZ */
3868 * 'http_create()' - Create an unconnected HTTP connection.
3871 static http_t
* /* O - HTTP connection */
3873 const char *host
, /* I - Hostname */
3874 int port
, /* I - Port number */
3875 http_addrlist_t
*addrlist
, /* I - Address list or @code NULL@ */
3876 int family
, /* I - Address family or AF_UNSPEC */
3877 http_encryption_t encryption
, /* I - Encryption to use */
3878 int blocking
, /* I - 1 for blocking mode */
3879 _http_mode_t mode
) /* I - _HTTP_MODE_CLIENT or _SERVER */
3881 http_t
*http
; /* New HTTP connection */
3882 char service
[255]; /* Service name */
3883 http_addrlist_t
*myaddrlist
= NULL
; /* My address list */
3886 DEBUG_printf(("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%d, mode=%d)", host
, port
, (void *)addrlist
, family
, encryption
, blocking
, mode
));
3888 if (!host
&& mode
== _HTTP_MODE_CLIENT
)
3894 * Lookup the host...
3899 myaddrlist
= httpAddrCopyList(addrlist
);
3903 snprintf(service
, sizeof(service
), "%d", port
);
3905 myaddrlist
= httpAddrGetList(host
, family
, service
);
3912 * Allocate memory for the structure...
3915 if ((http
= calloc(sizeof(http_t
), 1)) == NULL
)
3917 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3918 httpAddrFreeList(addrlist
);
3923 * Initialize the HTTP data...
3927 http
->activity
= time(NULL
);
3928 http
->addrlist
= myaddrlist
;
3929 http
->blocking
= blocking
;
3932 http
->gssctx
= GSS_C_NO_CONTEXT
;
3933 http
->gssname
= GSS_C_NO_NAME
;
3934 #endif /* HAVE_GSSAPI */
3935 http
->status
= HTTP_STATUS_CONTINUE
;
3936 http
->version
= HTTP_VERSION_1_1
;
3939 strlcpy(http
->hostname
, host
, sizeof(http
->hostname
));
3941 if (port
== 443) /* Always use encryption for https */
3942 http
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
3944 http
->encryption
= encryption
;
3946 http_set_wait(http
);
3949 * Return the new structure...
3958 * 'http_debug_hex()' - Do a hex dump of a buffer.
3962 http_debug_hex(const char *prefix
, /* I - Prefix for line */
3963 const char *buffer
, /* I - Buffer to dump */
3964 int bytes
) /* I - Bytes to dump */
3966 int i
, j
, /* Looping vars */
3967 ch
; /* Current character */
3968 char line
[255], /* Line buffer */
3969 *start
, /* Start of line after prefix */
3970 *ptr
; /* Pointer into line */
3973 if (_cups_debug_fd
< 0 || _cups_debug_level
< 6)
3976 DEBUG_printf(("6%s: %d bytes:", prefix
, bytes
));
3978 snprintf(line
, sizeof(line
), "6%s: ", prefix
);
3979 start
= line
+ strlen(line
);
3981 for (i
= 0; i
< bytes
; i
+= 16)
3983 for (j
= 0, ptr
= start
; j
< 16 && (i
+ j
) < bytes
; j
++, ptr
+= 2)
3984 snprintf(ptr
, 3, "%02X", buffer
[i
+ j
] & 255);
3988 memcpy(ptr
, " ", 3);
3993 memcpy(ptr
, " ", 3);
3996 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
3998 ch
= buffer
[i
+ j
] & 255;
4000 if (ch
< ' ' || ch
>= 127)
4014 * 'http_read()' - Read a buffer from a HTTP connection.
4016 * This function does the low-level read from the socket, retrying and timing
4020 static ssize_t
/* O - Number of bytes read or -1 on error */
4021 http_read(http_t
*http
, /* I - HTTP connection */
4022 char *buffer
, /* I - Buffer */
4023 size_t length
) /* I - Maximum bytes to read */
4025 ssize_t bytes
; /* Bytes read */
4028 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4030 if (!http
->blocking
|| http
->timeout_value
> 0.0)
4032 while (!httpWait(http
, http
->wait_value
))
4034 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4037 DEBUG_puts("2http_read: Timeout.");
4042 DEBUG_printf(("2http_read: Reading %d bytes into buffer.", (int)length
));
4048 bytes
= _httpTLSRead(http
, buffer
, (int)length
);
4050 #endif /* HAVE_SSL */
4051 bytes
= recv(http
->fd
, buffer
, length
, 0);
4056 if (WSAGetLastError() != WSAEINTR
)
4058 http
->error
= WSAGetLastError();
4061 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4063 if (!http
->timeout_cb
||
4064 !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4066 http
->error
= WSAEWOULDBLOCK
;
4071 DEBUG_printf(("2http_read: %s", strerror(errno
)));
4073 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4075 if (http
->timeout_cb
&& !(*http
->timeout_cb
)(http
, http
->timeout_data
))
4077 http
->error
= errno
;
4080 else if (!http
->timeout_cb
&& errno
!= EAGAIN
)
4082 http
->error
= errno
;
4086 else if (errno
!= EINTR
)
4088 http
->error
= errno
;
4096 DEBUG_printf(("2http_read: Read " CUPS_LLFMT
" bytes into buffer.",
4097 CUPS_LLCAST bytes
));
4100 http_debug_hex("http_read", buffer
, (int)bytes
);
4106 if (WSAGetLastError() == WSAEINTR
)
4109 http
->error
= WSAGetLastError();
4111 if (errno
== EINTR
|| (errno
== EAGAIN
&& !http
->timeout_cb
))
4114 http
->error
= errno
;
4117 else if (bytes
== 0)
4119 http
->error
= EPIPE
;
4128 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4130 * This function reads data from the HTTP buffer or from the socket, as needed.
4133 static ssize_t
/* O - Number of bytes read or -1 on error */
4134 http_read_buffered(http_t
*http
, /* I - HTTP connection */
4135 char *buffer
, /* I - Buffer */
4136 size_t length
) /* I - Maximum bytes to read */
4138 ssize_t bytes
; /* Bytes read */
4141 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT
") used=%d", (void *)http
, (void *)buffer
, CUPS_LLCAST length
, http
->used
));
4145 if (length
> (size_t)http
->used
)
4146 bytes
= (ssize_t
)http
->used
;
4148 bytes
= (ssize_t
)length
;
4150 DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
4153 memcpy(buffer
, http
->buffer
, (size_t)bytes
);
4154 http
->used
-= (int)bytes
;
4157 memmove(http
->buffer
, http
->buffer
+ bytes
, (size_t)http
->used
);
4160 bytes
= http_read(http
, buffer
, length
);
4167 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4169 * This function reads and validates the chunk length, then does a buffered read
4170 * returning the number of bytes placed in the buffer.
4173 static ssize_t
/* O - Number of bytes read or -1 on error */
4174 http_read_chunk(http_t
*http
, /* I - HTTP connection */
4175 char *buffer
, /* I - Buffer */
4176 size_t length
) /* I - Maximum bytes to read */
4178 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4180 if (http
->data_remaining
<= 0)
4182 char len
[32]; /* Length string */
4184 if (!httpGets(len
, sizeof(len
), http
))
4186 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4192 DEBUG_puts("1http_read_chunk: Blank chunk length, trying again...");
4193 if (!httpGets(len
, sizeof(len
), http
))
4195 DEBUG_puts("1http_read_chunk: Could not get chunk length.");
4200 http
->data_remaining
= strtoll(len
, NULL
, 16);
4202 if (http
->data_remaining
< 0)
4204 DEBUG_printf(("1http_read_chunk: Negative chunk length \"%s\" ("
4205 CUPS_LLFMT
")", len
, CUPS_LLCAST http
->data_remaining
));
4209 DEBUG_printf(("2http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT
")",
4210 len
, CUPS_LLCAST http
->data_remaining
));
4212 if (http
->data_remaining
== 0)
4215 * 0-length chunk, grab trailing blank line...
4218 httpGets(len
, sizeof(len
), http
);
4222 DEBUG_printf(("2http_read_chunk: data_remaining=" CUPS_LLFMT
,
4223 CUPS_LLCAST http
->data_remaining
));
4225 if (http
->data_remaining
<= 0)
4227 else if (length
> (size_t)http
->data_remaining
)
4228 length
= (size_t)http
->data_remaining
;
4230 return (http_read_buffered(http
, buffer
, length
));
4235 * 'http_send()' - Send a request with all fields and the trailing blank line.
4238 static int /* O - 0 on success, non-zero on error */
4239 http_send(http_t
*http
, /* I - HTTP connection */
4240 http_state_t request
, /* I - Request code */
4241 const char *uri
) /* I - URI */
4243 int i
; /* Looping var */
4244 char buf
[1024]; /* Encoded URI buffer */
4245 const char *value
; /* Field value */
4246 static const char * const codes
[] = /* Request code strings */
4266 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http
, codes
[request
], uri
));
4268 if (http
== NULL
|| uri
== NULL
)
4272 * Set the User-Agent field if it isn't already...
4275 if (!http
->fields
[HTTP_FIELD_USER_AGENT
])
4277 if (http
->default_fields
[HTTP_FIELD_USER_AGENT
])
4278 httpSetField(http
, HTTP_FIELD_USER_AGENT
, http
->default_fields
[HTTP_FIELD_USER_AGENT
]);
4280 httpSetField(http
, HTTP_FIELD_USER_AGENT
, cupsUserAgent());
4284 * Set the Accept-Encoding field if it isn't already...
4287 if (!http
->fields
[HTTP_FIELD_ACCEPT_ENCODING
] && http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
])
4288 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, http
->default_fields
[HTTP_FIELD_ACCEPT_ENCODING
]);
4291 * Encode the URI as needed...
4294 _httpEncodeURI(buf
, uri
, sizeof(buf
));
4297 * See if we had an error the last time around; if so, reconnect...
4300 if (http
->fd
< 0 || http
->status
== HTTP_STATUS_ERROR
||
4301 http
->status
>= HTTP_STATUS_BAD_REQUEST
)
4303 DEBUG_printf(("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d",
4304 http
->fd
, http
->status
, http
->tls_upgrade
));
4306 if (httpReconnect2(http
, 30000, NULL
))
4311 * Flush any written data that is pending...
4316 if (httpFlushWrite(http
) < 0)
4317 if (httpReconnect2(http
, 30000, NULL
))
4322 * Send the request header...
4325 http
->state
= request
;
4326 http
->data_encoding
= HTTP_ENCODING_FIELDS
;
4328 if (request
== HTTP_STATE_POST
|| request
== HTTP_STATE_PUT
)
4331 http
->status
= HTTP_STATUS_CONTINUE
;
4334 if (http
->encryption
== HTTP_ENCRYPTION_REQUIRED
&& !http
->tls
)
4336 httpSetField(http
, HTTP_FIELD_CONNECTION
, "Upgrade");
4337 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4339 #endif /* HAVE_SSL */
4341 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
4343 http
->status
= HTTP_STATUS_ERROR
;
4347 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
4348 if ((value
= httpGetField(http
, i
)) != NULL
&& *value
)
4350 DEBUG_printf(("5http_send: %s: %s", http_fields
[i
], value
));
4352 if (i
== HTTP_FIELD_HOST
)
4354 if (httpPrintf(http
, "Host: %s:%d\r\n", value
,
4355 httpAddrPort(http
->hostaddr
)) < 1)
4357 http
->status
= HTTP_STATUS_ERROR
;
4361 else if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], value
) < 1)
4363 http
->status
= HTTP_STATUS_ERROR
;
4369 if (httpPrintf(http
, "Cookie: $Version=0; %s\r\n", http
->cookie
) < 1)
4371 http
->status
= HTTP_STATUS_ERROR
;
4375 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http
->expect
,
4376 http
->mode
, http
->state
));
4378 if (http
->expect
== HTTP_STATUS_CONTINUE
&& http
->mode
== _HTTP_MODE_CLIENT
&&
4379 (http
->state
== HTTP_STATE_POST_RECV
||
4380 http
->state
== HTTP_STATE_PUT_RECV
))
4381 if (httpPrintf(http
, "Expect: 100-continue\r\n") < 1)
4383 http
->status
= HTTP_STATUS_ERROR
;
4387 if (httpPrintf(http
, "\r\n") < 1)
4389 http
->status
= HTTP_STATUS_ERROR
;
4393 if (httpFlushWrite(http
) < 0)
4396 http_set_length(http
);
4397 httpClearFields(http
);
4400 * The Kerberos and AuthRef authentication strings can only be used once...
4403 if (http
->fields
[HTTP_FIELD_AUTHORIZATION
] && http
->authstring
&&
4404 (!strncmp(http
->authstring
, "Negotiate", 9) ||
4405 !strncmp(http
->authstring
, "AuthRef", 7)))
4407 http
->_authstring
[0] = '\0';
4409 if (http
->authstring
!= http
->_authstring
)
4410 free(http
->authstring
);
4412 http
->authstring
= http
->_authstring
;
4420 * 'http_set_length()' - Set the data_encoding and data_remaining values.
4423 static off_t
/* O - Remainder or -1 on error */
4424 http_set_length(http_t
*http
) /* I - Connection */
4426 off_t remaining
; /* Remainder */
4429 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http
, http
->mode
, httpStateString(http
->state
)));
4431 if ((remaining
= httpGetLength2(http
)) >= 0)
4433 if (http
->mode
== _HTTP_MODE_SERVER
&&
4434 http
->state
!= HTTP_STATE_GET_SEND
&&
4435 http
->state
!= HTTP_STATE_PUT
&&
4436 http
->state
!= HTTP_STATE_POST
&&
4437 http
->state
!= HTTP_STATE_POST_SEND
)
4439 DEBUG_puts("1http_set_length: Not setting data_encoding/remaining.");
4443 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_TRANSFER_ENCODING
), "chunked"))
4445 DEBUG_puts("1http_set_length: Setting data_encoding to "
4446 "HTTP_ENCODING_CHUNKED.");
4447 http
->data_encoding
= HTTP_ENCODING_CHUNKED
;
4451 DEBUG_puts("1http_set_length: Setting data_encoding to "
4452 "HTTP_ENCODING_LENGTH.");
4453 http
->data_encoding
= HTTP_ENCODING_LENGTH
;
4456 DEBUG_printf(("1http_set_length: Setting data_remaining to " CUPS_LLFMT
".",
4457 CUPS_LLCAST remaining
));
4458 http
->data_remaining
= remaining
;
4460 if (remaining
<= INT_MAX
)
4461 http
->_data_remaining
= (int)remaining
;
4463 http
->_data_remaining
= INT_MAX
;
4470 * 'http_set_timeout()' - Set the socket timeout values.
4474 http_set_timeout(int fd
, /* I - File descriptor */
4475 double timeout
) /* I - Timeout in seconds */
4478 DWORD tv
= (DWORD
)(timeout
* 1000);
4479 /* Timeout in milliseconds */
4481 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4482 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4485 struct timeval tv
; /* Timeout in secs and usecs */
4487 tv
.tv_sec
= (int)timeout
;
4488 tv
.tv_usec
= (int)(1000000 * fmod(timeout
, 1.0));
4490 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4491 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, CUPS_SOCAST
&tv
, sizeof(tv
));
4497 * 'http_set_wait()' - Set the default wait value for reads.
4501 http_set_wait(http_t
*http
) /* I - HTTP connection */
4505 http
->wait_value
= (int)(http
->timeout_value
* 1000);
4507 if (http
->wait_value
<= 0)
4508 http
->wait_value
= 60000;
4511 http
->wait_value
= 10000;
4517 * 'http_tls_upgrade()' - Force upgrade to TLS encryption.
4520 static int /* O - Status of connection */
4521 http_tls_upgrade(http_t
*http
) /* I - HTTP connection */
4523 int ret
; /* Return value */
4524 http_t myhttp
; /* Local copy of HTTP data */
4527 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http
));
4530 * Flush the connection to make sure any previous "Upgrade" message
4537 * Copy the HTTP data to a local variable so we can do the OPTIONS
4538 * request without interfering with the existing request data...
4541 memcpy(&myhttp
, http
, sizeof(myhttp
));
4544 * Send an OPTIONS request to the server, requiring SSL or TLS
4545 * encryption on the link...
4548 http
->tls_upgrade
= 1;
4549 memset(http
->fields
, 0, sizeof(http
->fields
));
4550 http
->expect
= (http_status_t
)0;
4552 if (http
->hostname
[0] == '/')
4553 httpSetField(http
, HTTP_FIELD_HOST
, "localhost");
4555 httpSetField(http
, HTTP_FIELD_HOST
, http
->hostname
);
4557 httpSetField(http
, HTTP_FIELD_CONNECTION
, "upgrade");
4558 httpSetField(http
, HTTP_FIELD_UPGRADE
, "TLS/1.2,TLS/1.1,TLS/1.0");
4560 if ((ret
= httpOptions(http
, "*")) == 0)
4563 * Wait for the secure connection...
4566 while (httpUpdate(http
) == HTTP_STATUS_CONTINUE
);
4570 * Restore the HTTP request data...
4573 memcpy(http
->_fields
, myhttp
._fields
, sizeof(http
->_fields
));
4574 memcpy(http
->fields
, myhttp
.fields
, sizeof(http
->fields
));
4576 http
->data_encoding
= myhttp
.data_encoding
;
4577 http
->data_remaining
= myhttp
.data_remaining
;
4578 http
->_data_remaining
= myhttp
._data_remaining
;
4579 http
->expect
= myhttp
.expect
;
4580 http
->digest_tries
= myhttp
.digest_tries
;
4581 http
->tls_upgrade
= 0;
4584 * See if we actually went secure...
4590 * Server does not support HTTP upgrade...
4593 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4595 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, _("Encryption is not supported."), 1);
4596 httpAddrClose(NULL
, http
->fd
);
4605 #endif /* HAVE_SSL */
4609 * 'http_write()' - Write a buffer to a HTTP connection.
4612 static ssize_t
/* O - Number of bytes written */
4613 http_write(http_t
*http
, /* I - HTTP connection */
4614 const char *buffer
, /* I - Buffer for data */
4615 size_t length
) /* I - Number of bytes to write */
4617 ssize_t tbytes
, /* Total bytes sent */
4618 bytes
; /* Bytes sent */
4621 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4627 DEBUG_printf(("3http_write: About to write %d bytes.", (int)length
));
4629 if (http
->timeout_value
> 0.0)
4632 struct pollfd pfd
; /* Polled file descriptor */
4634 fd_set output_set
; /* Output ready for write? */
4635 struct timeval timeout
; /* Timeout value */
4636 #endif /* HAVE_POLL */
4637 int nfds
; /* Result from select()/poll() */
4643 pfd
.events
= POLLOUT
;
4645 while ((nfds
= poll(&pfd
, 1, http
->wait_value
)) < 0 &&
4646 (errno
== EINTR
|| errno
== EAGAIN
))
4652 FD_ZERO(&output_set
);
4653 FD_SET(http
->fd
, &output_set
);
4655 timeout
.tv_sec
= http
->wait_value
/ 1000;
4656 timeout
.tv_usec
= 1000 * (http
->wait_value
% 1000);
4658 nfds
= select(http
->fd
+ 1, NULL
, &output_set
, NULL
, &timeout
);
4661 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
4662 WSAGetLastError() == WSAEWOULDBLOCK
));
4664 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
4666 #endif /* HAVE_POLL */
4670 http
->error
= errno
;
4673 else if (nfds
== 0 && (!http
->timeout_cb
|| !(*http
->timeout_cb
)(http
, http
->timeout_data
)))
4676 http
->error
= WSAEWOULDBLOCK
;
4678 http
->error
= EWOULDBLOCK
;
4688 bytes
= _httpTLSWrite(http
, buffer
, (int)length
);
4690 #endif /* HAVE_SSL */
4691 bytes
= send(http
->fd
, buffer
, length
, 0);
4693 DEBUG_printf(("3http_write: Write of " CUPS_LLFMT
" bytes returned "
4694 CUPS_LLFMT
".", CUPS_LLCAST length
, CUPS_LLCAST bytes
));
4699 if (WSAGetLastError() == WSAEINTR
)
4701 else if (WSAGetLastError() == WSAEWOULDBLOCK
)
4703 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4706 http
->error
= WSAGetLastError();
4708 else if (WSAGetLastError() != http
->error
&&
4709 WSAGetLastError() != WSAECONNRESET
)
4711 http
->error
= WSAGetLastError();
4718 else if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
4720 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
4722 else if (!http
->timeout_cb
&& errno
== EAGAIN
)
4725 http
->error
= errno
;
4727 else if (errno
!= http
->error
&& errno
!= ECONNRESET
)
4729 http
->error
= errno
;
4734 DEBUG_printf(("3http_write: error writing data (%s).",
4735 strerror(http
->error
)));
4742 length
-= (size_t)bytes
;
4746 http_debug_hex("http_write", buffer
- tbytes
, (int)tbytes
);
4749 DEBUG_printf(("3http_write: Returning " CUPS_LLFMT
".", CUPS_LLCAST tbytes
));
4756 * 'http_write_chunk()' - Write a chunked buffer.
4759 static ssize_t
/* O - Number bytes written */
4760 http_write_chunk(http_t
*http
, /* I - HTTP connection */
4761 const char *buffer
, /* I - Buffer to write */
4762 size_t length
) /* I - Length of buffer */
4764 char header
[16]; /* Chunk header */
4765 ssize_t bytes
; /* Bytes written */
4768 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
4771 * Write the chunk header, data, and trailer.
4774 snprintf(header
, sizeof(header
), "%x\r\n", (unsigned)length
);
4775 if (http_write(http
, header
, strlen(header
)) < 0)
4777 DEBUG_puts("8http_write_chunk: http_write of length failed.");
4781 if ((bytes
= http_write(http
, buffer
, length
)) < 0)
4783 DEBUG_puts("8http_write_chunk: http_write of buffer failed.");
4787 if (http_write(http
, "\r\n", 2) < 0)
4789 DEBUG_puts("8http_write_chunk: http_write of CR LF failed.");