Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // HTTP routines for CUPS. |
3 | | // |
4 | | // Copyright © 2021-2025 by OpenPrinting. |
5 | | // Copyright © 2007-2021 by Apple Inc. |
6 | | // Copyright © 1997-2007 by Easy Software Products, all rights reserved. |
7 | | // |
8 | | // Licensed under Apache License v2.0. See the file "LICENSE" for more |
9 | | // information. |
10 | | // |
11 | | |
12 | | #include "cups-private.h" |
13 | | #include <fcntl.h> |
14 | | #include <math.h> |
15 | | #ifdef _WIN32 |
16 | | # include <tchar.h> |
17 | | #else |
18 | | # include <poll.h> |
19 | | # include <signal.h> |
20 | | # include <sys/time.h> |
21 | | # include <sys/resource.h> |
22 | | #endif // _WIN32 |
23 | | #include <zlib.h> |
24 | | |
25 | | |
26 | | // |
27 | | // Local functions... |
28 | | // |
29 | | |
30 | | static void http_add_field(http_t *http, http_field_t field, const char *value, bool append); |
31 | | static void http_content_coding_finish(http_t *http); |
32 | | static void http_content_coding_start(http_t *http, const char *value); |
33 | | static http_t *http_create(const char *host, int port, http_addrlist_t *addrlist, int family, http_encryption_t encryption, bool blocking, _http_mode_t mode); |
34 | | #ifdef DEBUG |
35 | | static void http_debug_hex(const char *prefix, const char *buffer, int bytes); |
36 | | #endif // DEBUG |
37 | | static ssize_t http_read(http_t *http, char *buffer, size_t length); |
38 | | static ssize_t http_read_buffered(http_t *http, char *buffer, size_t length); |
39 | | static ssize_t http_read_chunk(http_t *http, char *buffer, size_t length); |
40 | | static bool http_send(http_t *http, http_state_t request, const char *uri); |
41 | | static ssize_t http_write(http_t *http, const char *buffer, size_t length); |
42 | | static ssize_t http_write_chunk(http_t *http, const char *buffer, size_t length); |
43 | | static off_t http_set_length(http_t *http); |
44 | | static void http_set_timeout(int fd, double timeout); |
45 | | static void http_set_wait(http_t *http); |
46 | | static bool http_tls_upgrade(http_t *http); |
47 | | |
48 | | |
49 | | // |
50 | | // Local globals... |
51 | | // |
52 | | |
53 | | static const char * const http_fields[HTTP_FIELD_MAX] = |
54 | | { |
55 | | "Accept", |
56 | | "Accept-CH", |
57 | | "Accept-Encoding", |
58 | | "Accept-Language", |
59 | | "Accept-Ranges", |
60 | | "Access-Control-Allow-Credentials", |
61 | | "Access-Control-Allow-Headers", |
62 | | "Access-Control-Allow-Methods", |
63 | | "Access-Control-Allow-Origin", |
64 | | "Access-Control-Expose-Headers", |
65 | | "Access-Control-Max-Age", |
66 | | "Access-Control-Request-Headers", |
67 | | "Access-Control-Request-Method", |
68 | | "Age", |
69 | | "Allow", |
70 | | "Authentication-Control", |
71 | | "Authentication-Info", |
72 | | "Authorization", |
73 | | "Cache-Control", |
74 | | "Cache-Status", |
75 | | "Cert-Not-After", |
76 | | "Cert-Not-Before", |
77 | | "Connection", |
78 | | "Content-Disposition", |
79 | | "Content-Encoding", |
80 | | "Content-Language", |
81 | | "Content-Length", |
82 | | "Content-Location", |
83 | | "Content-Range", |
84 | | "Content-Security-Policy", |
85 | | "Content-Security-Policy-Report-Only", |
86 | | "Content-Type", |
87 | | "Cross-Origin-Embedder-Policy", |
88 | | "Cross-Origin-Embedder-Policy-Report-Only", |
89 | | "Cross-Origin-Opener-Policy", |
90 | | "Cross-Origin-Opener-Policy-Report-Only", |
91 | | "Cross-Origin-Resource-Policy", |
92 | | "DASL", |
93 | | "Date", |
94 | | "DAV", |
95 | | "Depth", |
96 | | "Destination", |
97 | | "ETag", |
98 | | "Expires", |
99 | | "Forwarded", |
100 | | "From", |
101 | | "Host", |
102 | | "If", |
103 | | "If-Match", |
104 | | "If-Modified-Since", |
105 | | "If-None-Match", |
106 | | "If-Range", |
107 | | "If-Schedule-Tag-Match", |
108 | | "If-Unmodified-since", |
109 | | "Keep-Alive", |
110 | | "Last-Modified", |
111 | | "Link", |
112 | | "Location", |
113 | | "Lock-Token", |
114 | | "Max-Forwards", |
115 | | "Optional-WWW-Authenticate", |
116 | | "Origin", |
117 | | "OSCORE", |
118 | | "Overwrite", |
119 | | "Pragma", |
120 | | "Proxy-Authenticate", |
121 | | "Proxy-Authentication-Info", |
122 | | "Proxy-Authorization", |
123 | | "Proxy-Status", |
124 | | "Public", |
125 | | "Range", |
126 | | "Referer", |
127 | | "Refresh", |
128 | | "Replay-Nonce", |
129 | | "Retry-After", |
130 | | "Schedule-Reply", |
131 | | "Schedule-Tag", |
132 | | "Server", |
133 | | "Strict-Transport-Security", |
134 | | "TE", |
135 | | "Timeout", |
136 | | "Trailer", |
137 | | "Transfer-Encoding", |
138 | | "Upgrade", |
139 | | "User-Agent", |
140 | | "Vary", |
141 | | "Via", |
142 | | "WWW-Authenticate", |
143 | | "X-Content-Options", |
144 | | "X-Frame-Options" |
145 | | }; |
146 | | |
147 | | |
148 | | // |
149 | | // 'httpAcceptConnection()' - Accept a new HTTP client connection. |
150 | | // |
151 | | // This function accepts a new HTTP client connection from the specified |
152 | | // listening socket "fd". The "blocking" argument specifies whether the new |
153 | | // HTTP connection is blocking. |
154 | | // |
155 | | |
156 | | http_t * // O - HTTP connection or `NULL` |
157 | | httpAcceptConnection(int fd, // I - Listen socket file descriptor |
158 | | bool blocking) // I - `true` if the connection should be blocking, `false` otherwise |
159 | 0 | { |
160 | 0 | http_t *http; // HTTP connection |
161 | 0 | http_addrlist_t addrlist; // Dummy address list |
162 | 0 | socklen_t addrlen; // Length of address |
163 | 0 | int val; // Socket option value |
164 | | |
165 | | |
166 | | // Range check input... |
167 | 0 | if (fd < 0) |
168 | 0 | return (NULL); |
169 | | |
170 | | // Create the client connection... |
171 | 0 | memset(&addrlist, 0, sizeof(addrlist)); |
172 | |
|
173 | 0 | if ((http = http_create(NULL, 0, &addrlist, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, blocking, _HTTP_MODE_SERVER)) == NULL) |
174 | 0 | return (NULL); |
175 | | |
176 | | // Accept the client and get the remote address... |
177 | 0 | addrlen = sizeof(http_addr_t); |
178 | |
|
179 | 0 | if ((http->fd = accept(fd, (struct sockaddr *)&(http->hostlist->addr), &addrlen)) < 0) |
180 | 0 | { |
181 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false); |
182 | 0 | httpClose(http); |
183 | |
|
184 | 0 | return (NULL); |
185 | 0 | } |
186 | | |
187 | 0 | http->hostaddr = &(http->hostlist->addr); |
188 | |
|
189 | 0 | if (httpAddrIsLocalhost(http->hostaddr)) |
190 | 0 | cupsCopyString(http->hostname, "localhost", sizeof(http->hostname)); |
191 | 0 | else |
192 | 0 | httpAddrGetString(http->hostaddr, http->hostname, sizeof(http->hostname)); |
193 | |
|
194 | | #ifdef SO_NOSIGPIPE |
195 | | // Disable SIGPIPE for this socket. |
196 | | val = 1; |
197 | | if (setsockopt(http->fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val))) |
198 | | DEBUG_printf("httpAcceptConnection: setsockopt(SO_NOSIGPIPE) failed - %s", strerror(errno)); |
199 | | #endif // SO_NOSIGPIPE |
200 | | |
201 | | // Using TCP_NODELAY improves responsiveness, especially on systems with a |
202 | | // slow loopback interface. Since we write large buffers when sending print |
203 | | // files and requests, there shouldn't be any performance penalty for this... |
204 | 0 | val = 1; |
205 | 0 | if (setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val))) |
206 | 0 | DEBUG_printf("httpAcceptConnection: setsockopt(TCP_NODELAY) failed - %s", strerror(errno)); |
207 | |
|
208 | 0 | #ifdef FD_CLOEXEC |
209 | | // Close this socket when starting another process... |
210 | 0 | if (fcntl(http->fd, F_SETFD, FD_CLOEXEC)) |
211 | 0 | DEBUG_printf("httpAcceptConnection: fcntl(F_SETFD, FD_CLOEXEC) failed - %s", strerror(errno)); |
212 | 0 | #endif // FD_CLOEXEC |
213 | |
|
214 | 0 | return (http); |
215 | 0 | } |
216 | | |
217 | | |
218 | | // |
219 | | // 'httpClearCookie()' - Clear the cookie value(s). |
220 | | // |
221 | | |
222 | | void |
223 | | httpClearCookie(http_t *http) // I - HTTP connection |
224 | 0 | { |
225 | 0 | if (!http) |
226 | 0 | return; |
227 | | |
228 | 0 | free(http->cookie); |
229 | 0 | http->cookie = NULL; |
230 | 0 | } |
231 | | |
232 | | |
233 | | // |
234 | | // 'httpClearFields()' - Clear HTTP request/response fields. |
235 | | // |
236 | | |
237 | | void |
238 | | httpClearFields(http_t *http) // I - HTTP connection |
239 | 0 | { |
240 | 0 | http_field_t field; // Current field |
241 | | |
242 | |
|
243 | 0 | DEBUG_printf("httpClearFields(http=%p)", (void *)http); |
244 | |
|
245 | 0 | if (http) |
246 | 0 | { |
247 | 0 | for (field = HTTP_FIELD_ACCEPT; field < HTTP_FIELD_MAX; field ++) |
248 | 0 | { |
249 | 0 | free(http->fields[field]); |
250 | 0 | http->fields[field] = NULL; |
251 | 0 | } |
252 | |
|
253 | 0 | if (http->mode == _HTTP_MODE_CLIENT) |
254 | 0 | { |
255 | 0 | if (http->hostname[0] == '/') |
256 | 0 | httpSetField(http, HTTP_FIELD_HOST, "localhost"); |
257 | 0 | else |
258 | 0 | httpSetField(http, HTTP_FIELD_HOST, http->hostname); |
259 | 0 | } |
260 | |
|
261 | 0 | http->expect = (http_status_t)0; |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | |
266 | | // |
267 | | // 'httpClose()' - Close a HTTP connection. |
268 | | // |
269 | | |
270 | | void |
271 | | httpClose(http_t *http) // I - HTTP connection |
272 | 0 | { |
273 | 0 | http_field_t field; // Current field |
274 | | |
275 | |
|
276 | 0 | DEBUG_printf("httpClose(http=%p)", (void *)http); |
277 | | |
278 | | // Range check input... |
279 | 0 | if (!http) |
280 | 0 | return; |
281 | | |
282 | | // Close any open connection... |
283 | 0 | _httpDisconnect(http); |
284 | | |
285 | | // Free memory used... |
286 | 0 | httpAddrFreeList(http->hostlist); |
287 | |
|
288 | 0 | for (field = HTTP_FIELD_ACCEPT; field < HTTP_FIELD_MAX; field ++) |
289 | 0 | { |
290 | 0 | free(http->default_fields[field]); |
291 | 0 | free(http->fields[field]); |
292 | 0 | } |
293 | |
|
294 | 0 | free(http->authstring); |
295 | 0 | free(http->cookie); |
296 | |
|
297 | 0 | _httpFreeCredentials(http->tls_credentials); |
298 | |
|
299 | 0 | free(http); |
300 | 0 | } |
301 | | |
302 | | |
303 | | // |
304 | | // 'httpConnect()' - Connect to a HTTP server. |
305 | | // |
306 | | // This function creates a connection to a HTTP server. The "host" and "port" |
307 | | // arguments specify a hostname or IP address and port number to use while the |
308 | | // "addrlist" argument specifies a list of addresses to use or `NULL` to do a |
309 | | // fresh lookup. The "family" argument specifies the address family to use - |
310 | | // `AF_UNSPEC` to try both IPv4 and IPv6, `AF_INET` for IPv4, or `AF_INET6` for |
311 | | // IPv6. |
312 | | // |
313 | | // The "encryption" argument specifies whether to encrypt the connection and the |
314 | | // "blocking" argument specifies whether to use blocking behavior when reading |
315 | | // or writing data. |
316 | | // |
317 | | // The "msec" argument specifies how long to try to connect to the server or `0` |
318 | | // to just create an unconnected `http_t` object. The "cancel" argument |
319 | | // specifies an integer variable that can be set to a non-zero value to cancel |
320 | | // the connection process. |
321 | | // |
322 | | |
323 | | http_t * // O - New HTTP connection |
324 | | httpConnect( |
325 | | const char *host, // I - Host to connect to |
326 | | int port, // I - Port number |
327 | | http_addrlist_t *addrlist, // I - List of addresses or `NULL` to lookup |
328 | | int family, // I - Address family to use or `AF_UNSPEC` for any |
329 | | http_encryption_t encryption, // I - Type of encryption to use |
330 | | bool blocking, // I - `true` for blocking connection, `false` for non-blocking |
331 | | int msec, // I - Connection timeout in milliseconds, `0` means don't connect |
332 | | int *cancel) // I - Pointer to "cancel" variable or `NULL` for none |
333 | 0 | { |
334 | 0 | http_t *http; // New HTTP connection |
335 | | |
336 | |
|
337 | 0 | DEBUG_printf("httpConnect(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); |
338 | | |
339 | | // Create the HTTP structure... |
340 | 0 | if ((http = http_create(host, port, addrlist, family, encryption, blocking, _HTTP_MODE_CLIENT)) == NULL) |
341 | 0 | return (NULL); |
342 | | |
343 | | // Optionally connect to the remote system... |
344 | 0 | if (msec == 0 || httpConnectAgain(http, msec, cancel)) |
345 | 0 | return (http); |
346 | | |
347 | | // Could not connect to any known address - bail out! |
348 | 0 | httpClose(http); |
349 | |
|
350 | 0 | return (NULL); |
351 | 0 | } |
352 | | |
353 | | |
354 | | // |
355 | | // 'httpConnectAgain()' - Reconnect to a HTTP server with timeout and optional cancel variable. |
356 | | // |
357 | | |
358 | | bool // O - `true` on success, `false` on failure |
359 | | httpConnectAgain(http_t *http, // I - HTTP connection |
360 | | int msec, // I - Timeout in milliseconds |
361 | | int *cancel) // I - Pointer to "cancel" variable |
362 | 0 | { |
363 | 0 | http_addrlist_t *addr; // Connected address |
364 | 0 | char *orig_creds; // Original peer credentials |
365 | | #ifdef DEBUG |
366 | | http_addrlist_t *current; // Current address |
367 | | char temp[256]; // Temporary address string |
368 | | #endif // DEBUG |
369 | | |
370 | |
|
371 | 0 | DEBUG_printf("httpConnectAgain(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel); |
372 | | |
373 | | // Range check input... |
374 | 0 | if (!http) |
375 | 0 | { |
376 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
377 | 0 | return (false); |
378 | 0 | } |
379 | | |
380 | 0 | orig_creds = httpCopyPeerCredentials(http); |
381 | |
|
382 | 0 | if (http->tls) |
383 | 0 | { |
384 | 0 | DEBUG_puts("2httpConnectAgain: Shutting down SSL/TLS..."); |
385 | 0 | _httpTLSStop(http); |
386 | 0 | } |
387 | | |
388 | | // Close any previously open socket... |
389 | 0 | if (http->fd >= 0) |
390 | 0 | { |
391 | 0 | DEBUG_printf("2httpConnectAgain: Closing socket %d...", http->fd); |
392 | |
|
393 | 0 | httpAddrClose(NULL, http->fd); |
394 | |
|
395 | 0 | http->fd = -1; |
396 | 0 | } |
397 | | |
398 | | // Reset all state (except fields, which may be reused)... |
399 | 0 | http->state = HTTP_STATE_WAITING; |
400 | 0 | http->version = HTTP_VERSION_1_1; |
401 | 0 | http->keep_alive = HTTP_KEEPALIVE_OFF; |
402 | 0 | http->data_encoding = HTTP_ENCODING_FIELDS; |
403 | 0 | http->used = 0; |
404 | 0 | http->data_remaining = 0; |
405 | 0 | http->hostaddr = NULL; |
406 | 0 | http->wused = 0; |
407 | | |
408 | | // Connect to the server... |
409 | | #ifdef DEBUG |
410 | | for (current = http->hostlist; current; current = current->next) |
411 | | DEBUG_printf("2httpConnectAgain: Address %s:%d", httpAddrGetString(&(current->addr), temp, sizeof(temp)), httpAddrGetPort(&(current->addr))); |
412 | | #endif // DEBUG |
413 | |
|
414 | 0 | if ((addr = httpAddrConnect(http->hostlist, &(http->fd), msec, cancel)) == NULL) |
415 | 0 | { |
416 | | // Unable to connect... |
417 | | #ifdef _WIN32 |
418 | | http->error = WSAGetLastError(); |
419 | | #else |
420 | 0 | http->error = errno; |
421 | 0 | #endif // _WIN32 |
422 | 0 | http->status = HTTP_STATUS_ERROR; |
423 | |
|
424 | 0 | DEBUG_printf("1httpConnectAgain: httpAddrConnect failed: %s", strerror(http->error)); |
425 | |
|
426 | 0 | free(orig_creds); |
427 | |
|
428 | 0 | return (false); |
429 | 0 | } |
430 | | |
431 | 0 | DEBUG_printf("2httpConnectAgain: New socket=%d", http->fd); |
432 | |
|
433 | 0 | if (http->timeout_value > 0) |
434 | 0 | http_set_timeout(http->fd, http->timeout_value); |
435 | |
|
436 | 0 | http->hostaddr = &(addr->addr); |
437 | 0 | http->error = 0; |
438 | |
|
439 | 0 | if (http->encryption == HTTP_ENCRYPTION_ALWAYS) |
440 | 0 | { |
441 | | // Always do encryption via SSL. |
442 | 0 | if (!_httpTLSStart(http)) |
443 | 0 | { |
444 | 0 | httpAddrClose(NULL, http->fd); |
445 | 0 | http->fd = -1; |
446 | |
|
447 | 0 | free(orig_creds); |
448 | |
|
449 | 0 | return (false); |
450 | 0 | } |
451 | 0 | } |
452 | 0 | else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls_upgrade) |
453 | 0 | { |
454 | 0 | if (!http_tls_upgrade(http)) |
455 | 0 | { |
456 | 0 | free(orig_creds); |
457 | |
|
458 | 0 | return (false); |
459 | 0 | } |
460 | 0 | } |
461 | | |
462 | 0 | DEBUG_printf("1httpConnectAgain: Connected to %s:%d...", httpAddrGetString(http->hostaddr, temp, sizeof(temp)), httpAddrGetPort(http->hostaddr)); |
463 | |
|
464 | 0 | if (orig_creds) |
465 | 0 | { |
466 | 0 | char *new_creds = httpCopyPeerCredentials(http); |
467 | | // New peer credentials |
468 | |
|
469 | 0 | if (!new_creds || (strcmp(orig_creds, new_creds) && cupsGetCredentialsTrust(/*path*/NULL, http->hostname, new_creds, /*require_ca*/true) != HTTP_TRUST_OK)) |
470 | 0 | { |
471 | | // New and old credentials don't match and the new cert doesn't validate... |
472 | 0 | _httpDisconnect(http); |
473 | |
|
474 | 0 | free(orig_creds); |
475 | 0 | free(new_creds); |
476 | |
|
477 | 0 | return (false); |
478 | 0 | } |
479 | | |
480 | 0 | free(new_creds); |
481 | 0 | } |
482 | | |
483 | 0 | free(orig_creds); |
484 | |
|
485 | 0 | return (true); |
486 | 0 | } |
487 | | |
488 | | |
489 | | // |
490 | | // 'httpConnectURI()' - Connect to a HTTP service using a URI. |
491 | | // |
492 | | // This function creates a connection to a HTTP server. The "uri" argument |
493 | | // specifies a "http", "https", "ipp", or "ipps" URI for the service. |
494 | | // |
495 | | // The resource path for the service is returned in the buffer pointed to by |
496 | | // the "resource" argument of size "rsize". |
497 | | // |
498 | | // The "msec" argument specifies how long to try to connect to the server or `0` |
499 | | // to just create an unconnected `http_t` object. The "cancel" argument |
500 | | // specifies an integer variable that can be set to a non-zero value to cancel |
501 | | // the connection process. |
502 | | // |
503 | | // The "require_ca" argument specifies whether to verify that the service |
504 | | // connection is using a CA-signed X.509 certificate. |
505 | | // |
506 | | |
507 | | http_t * // O - New HTTP connection |
508 | | httpConnectURI(const char *uri, // I - Service to connect to |
509 | | char *host, // I - Host name buffer (`NULL` for don't care) |
510 | | size_t hsize, // I - Size of host name buffer |
511 | | int *port, // O - Port number (`NULL` for don't care) |
512 | | char *resource, // I - Resource path buffer (`NULL` for don't care) |
513 | | size_t rsize, // I - Size of resource path buffer |
514 | | bool blocking, // I - `true` for blocking connection, `false` for non-blocking |
515 | | int msec, // I - Connection timeout in milliseconds, `0` means don't connect |
516 | | int *cancel, // I - Pointer to "cancel" variable or `NULL` for none |
517 | | bool require_ca) // I - `true` to require a CA-signed X.509 certificate |
518 | 0 | { |
519 | 0 | http_t *http; // New HTTP connection |
520 | 0 | char scheme[32], // URI scheme |
521 | 0 | userpass[32], // URI username:password |
522 | 0 | lhost[256], // URI host (local copy) |
523 | 0 | lresource[256]; // URI resource (local copy) |
524 | 0 | int lport; // URI port (local copy) |
525 | 0 | http_encryption_t encryption; // Type of encryption to use |
526 | 0 | http_uri_status_t uri_status; // URI separation status |
527 | | |
528 | |
|
529 | 0 | DEBUG_printf("httpConnectURI(uri=\"%s\", host=%p, hsize=%u, port=%p, resource=%p, rsize=%u, blocking=%d, msec=%d, cancel=%p, require_ca=%s)", uri, (void *)host, (unsigned)hsize, (void *)port, (void *)resource, (unsigned)rsize, blocking, msec, (void *)cancel, require_ca ? "true" : "false"); |
530 | | |
531 | | // Range check input... |
532 | 0 | if (!uri) |
533 | 0 | { |
534 | 0 | if (host) |
535 | 0 | *host = '\0'; |
536 | |
|
537 | 0 | if (port) |
538 | 0 | *port = 0; |
539 | |
|
540 | 0 | if (resource) |
541 | 0 | *resource = '\0'; |
542 | |
|
543 | 0 | return (NULL); |
544 | 0 | } |
545 | | |
546 | 0 | if (!host) |
547 | 0 | { |
548 | 0 | host = lhost; |
549 | 0 | hsize = sizeof(lhost); |
550 | 0 | } |
551 | |
|
552 | 0 | if (!port) |
553 | 0 | port = &lport; |
554 | |
|
555 | 0 | if (!resource) |
556 | 0 | { |
557 | 0 | resource = lresource; |
558 | 0 | rsize = sizeof(lresource); |
559 | 0 | } |
560 | | |
561 | | // Get the URI components... |
562 | 0 | if ((uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, hsize, port, resource, rsize)) < HTTP_URI_STATUS_OK) |
563 | 0 | { |
564 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, httpURIStatusString(uri_status), 0); |
565 | 0 | return (NULL); |
566 | 0 | } |
567 | | |
568 | 0 | DEBUG_printf("1httpConnectURI: scheme=\"%s\", host=\"%s\", port=%d, resource=\"%s\"", scheme, host, *port, resource); |
569 | |
|
570 | 0 | if (!strcmp(scheme, "https") || !strcmp(scheme, "ipps") || *port == 443) |
571 | 0 | encryption = HTTP_ENCRYPTION_ALWAYS; |
572 | 0 | else |
573 | 0 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; |
574 | |
|
575 | 0 | http = httpConnect(host, *port, /*addrlist*/NULL, AF_UNSPEC, encryption, blocking, msec, cancel); |
576 | |
|
577 | 0 | if (httpIsEncrypted(http)) |
578 | 0 | { |
579 | | // Validate trust with service... |
580 | 0 | char *creds; // Peer credentials... |
581 | 0 | http_trust_t trust; // Trust in credentials... |
582 | |
|
583 | 0 | creds = httpCopyPeerCredentials(http); |
584 | 0 | trust = cupsGetCredentialsTrust(/*path*/NULL, host, creds, require_ca); |
585 | |
|
586 | 0 | if (trust == HTTP_TRUST_OK) |
587 | 0 | { |
588 | | // Pin the trusted credentials (for TOFU)... |
589 | 0 | cupsSaveCredentials(/*path*/NULL, host, creds, /*key*/NULL); |
590 | 0 | } |
591 | 0 | else if (trust != HTTP_TRUST_RENEWED) |
592 | 0 | { |
593 | | // Don't allow the connection... |
594 | 0 | httpClose(http); |
595 | 0 | http = NULL; |
596 | 0 | } |
597 | |
|
598 | 0 | free(creds); |
599 | 0 | } |
600 | |
|
601 | 0 | return (http); |
602 | 0 | } |
603 | | |
604 | | |
605 | | // |
606 | | // '_httpDisconnect()' - Disconnect a HTTP connection. |
607 | | // |
608 | | |
609 | | void |
610 | | _httpDisconnect(http_t *http) // I - HTTP connection |
611 | 0 | { |
612 | 0 | if (http->tls) |
613 | 0 | _httpTLSStop(http); |
614 | |
|
615 | 0 | httpAddrClose(NULL, http->fd); |
616 | |
|
617 | 0 | http->fd = -1; |
618 | 0 | } |
619 | | |
620 | | |
621 | | // |
622 | | // 'httpFieldValue()' - Return the HTTP field enumeration value for a field |
623 | | // name. |
624 | | // |
625 | | |
626 | | http_field_t // O - Field index |
627 | | httpFieldValue(const char *name) // I - String name |
628 | 0 | { |
629 | 0 | int i; // Looping var |
630 | | |
631 | |
|
632 | 0 | for (i = 0; i < HTTP_FIELD_MAX; i ++) |
633 | 0 | { |
634 | 0 | if (!_cups_strcasecmp(name, http_fields[i])) |
635 | 0 | return ((http_field_t)i); |
636 | 0 | } |
637 | | |
638 | 0 | return (HTTP_FIELD_UNKNOWN); |
639 | 0 | } |
640 | | |
641 | | |
642 | | // |
643 | | // 'httpFlush()' - Flush data read from a HTTP connection. |
644 | | // |
645 | | |
646 | | void |
647 | | httpFlush(http_t *http) // I - HTTP connection |
648 | 0 | { |
649 | 0 | char buffer[8192]; // Junk buffer |
650 | 0 | bool blocking; // To block or not to block |
651 | 0 | http_state_t oldstate; // Old state |
652 | | |
653 | |
|
654 | 0 | DEBUG_printf("httpFlush(http=%p), state=%s", (void *)http, httpStateString(http->state)); |
655 | | |
656 | | // Nothing to do if we are in the "waiting" state... |
657 | 0 | if (http->state == HTTP_STATE_WAITING) |
658 | 0 | return; |
659 | | |
660 | | // Temporarily set non-blocking mode so we don't get stuck in httpRead()... |
661 | 0 | blocking = http->blocking; |
662 | 0 | http->blocking = false; |
663 | | |
664 | | // Read any data we can... |
665 | 0 | oldstate = http->state; |
666 | 0 | while (httpRead(http, buffer, sizeof(buffer)) > 0); |
667 | | |
668 | | // Restore blocking and reset the connection if we didn't get all of the remaining data... |
669 | 0 | http->blocking = blocking; |
670 | |
|
671 | 0 | if (http->state == oldstate && http->state != HTTP_STATE_WAITING && http->fd >= 0) |
672 | 0 | { |
673 | | // Didn't get the data back, so close the current connection. |
674 | 0 | if (http->coding) |
675 | 0 | http_content_coding_finish(http); |
676 | |
|
677 | 0 | DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing."); |
678 | |
|
679 | 0 | http->state = HTTP_STATE_WAITING; |
680 | |
|
681 | 0 | if (http->tls) |
682 | 0 | _httpTLSStop(http); |
683 | |
|
684 | 0 | httpAddrClose(NULL, http->fd); |
685 | |
|
686 | 0 | http->fd = -1; |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | | |
691 | | // |
692 | | // 'httpFlushWrite()' - Flush data written to a HTTP connection. |
693 | | // |
694 | | |
695 | | int // O - Bytes written or -1 on error |
696 | | httpFlushWrite(http_t *http) // I - HTTP connection |
697 | 0 | { |
698 | 0 | ssize_t bytes; // Bytes written |
699 | | |
700 | |
|
701 | 0 | DEBUG_printf("httpFlushWrite(http=%p) data_encoding=%d", (void *)http, http ? http->data_encoding : 100); |
702 | |
|
703 | 0 | if (!http || !http->wused) |
704 | 0 | { |
705 | 0 | DEBUG_puts(http ? "1httpFlushWrite: Write buffer is empty." : "1httpFlushWrite: No connection."); |
706 | 0 | return (0); |
707 | 0 | } |
708 | | |
709 | 0 | if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
710 | 0 | bytes = http_write_chunk(http, http->wbuffer, (size_t)http->wused); |
711 | 0 | else |
712 | 0 | bytes = http_write(http, http->wbuffer, (size_t)http->wused); |
713 | |
|
714 | 0 | http->wused = 0; |
715 | |
|
716 | 0 | DEBUG_printf("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes, errno); |
717 | |
|
718 | 0 | return ((int)bytes); |
719 | 0 | } |
720 | | |
721 | | |
722 | | // |
723 | | // 'httpGetActivity()' - Get the most recent activity for a connection. |
724 | | // |
725 | | // The return value is the time in seconds of the last read or write. |
726 | | // |
727 | | |
728 | | time_t // O - Time of last read or write |
729 | | httpGetActivity(http_t *http) // I - HTTP connection |
730 | 0 | { |
731 | 0 | return (http ? http->activity : 0); |
732 | 0 | } |
733 | | |
734 | | |
735 | | // |
736 | | // 'httpGetAuthString()' - Get the current authorization string. |
737 | | // |
738 | | // The authorization string is set by @link cupsDoAuthentication@ and |
739 | | // @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the |
740 | | // string to use with @link httpSetField@ for the |
741 | | // `HTTP_FIELD_AUTHORIZATION` value. |
742 | | // |
743 | | |
744 | | const char * // O - Authorization string |
745 | | httpGetAuthString(http_t *http) // I - HTTP connection |
746 | 0 | { |
747 | 0 | if (http) |
748 | 0 | return (http->authstring); |
749 | 0 | else |
750 | 0 | return (NULL); |
751 | 0 | } |
752 | | |
753 | | |
754 | | // |
755 | | // 'httpGetBlocking()' - Get the blocking/non-blocking state of a connection. |
756 | | // |
757 | | |
758 | | bool // O - `true` if blocking, `false` if non-blocking |
759 | | httpGetBlocking(http_t *http) // I - HTTP connection |
760 | 0 | { |
761 | 0 | return (http ? http->blocking : false); |
762 | 0 | } |
763 | | |
764 | | |
765 | | // |
766 | | // 'httpGetContentEncoding()' - Get a common content encoding, if any, between |
767 | | // the client and server. |
768 | | // |
769 | | // This function uses the value of the Accepts-Encoding HTTP header and must be |
770 | | // called after receiving a response from the server or a request from the |
771 | | // client. The value returned can be use in subsequent requests (for clients) |
772 | | // or in the response (for servers) in order to compress the content stream. |
773 | | // |
774 | | |
775 | | const char * // O - Content-Coding value or `NULL` for the identity coding. |
776 | | httpGetContentEncoding(http_t *http) // I - HTTP connection |
777 | 0 | { |
778 | 0 | if (http && http->fields[HTTP_FIELD_ACCEPT_ENCODING]) |
779 | 0 | { |
780 | 0 | int i; // Looping var |
781 | 0 | char temp[HTTP_MAX_VALUE], // Copy of Accepts-Encoding value |
782 | 0 | *start, // Start of coding value |
783 | 0 | *end; // End of coding value |
784 | 0 | double qvalue; // "qvalue" for coding |
785 | 0 | struct lconv *loc = localeconv(); // Locale data |
786 | 0 | static const char * const codings[] = |
787 | 0 | { // Supported content codings |
788 | 0 | "deflate", |
789 | 0 | "gzip", |
790 | 0 | "x-deflate", |
791 | 0 | "x-gzip" |
792 | 0 | }; |
793 | |
|
794 | 0 | cupsCopyString(temp, http->fields[HTTP_FIELD_ACCEPT_ENCODING], sizeof(temp)); |
795 | |
|
796 | 0 | for (start = temp; *start; start = end) |
797 | 0 | { |
798 | | // Find the end of the coding name... |
799 | 0 | qvalue = 1.0; |
800 | 0 | end = start; |
801 | 0 | while (*end && *end != ';' && *end != ',' && !isspace(*end & 255)) |
802 | 0 | end ++; |
803 | |
|
804 | 0 | if (*end == ';') |
805 | 0 | { |
806 | | // Grab the qvalue as needed... |
807 | 0 | if (!strncmp(end, ";q=", 3)) |
808 | 0 | qvalue = _cupsStrScand(end + 3, NULL, loc); |
809 | | |
810 | | // Skip past all attributes... |
811 | 0 | *end++ = '\0'; |
812 | 0 | while (*end && *end != ',' && !isspace(*end & 255)) |
813 | 0 | end ++; |
814 | 0 | } |
815 | 0 | else if (*end) |
816 | 0 | { |
817 | 0 | *end++ = '\0'; |
818 | 0 | } |
819 | |
|
820 | 0 | while (*end && isspace(*end & 255)) |
821 | 0 | end ++; |
822 | | |
823 | | // Check value if it matches something we support... |
824 | 0 | if (qvalue <= 0.0) |
825 | 0 | continue; |
826 | | |
827 | 0 | for (i = 0; i < (int)(sizeof(codings) / sizeof(codings[0])); i ++) |
828 | 0 | { |
829 | 0 | if (!strcmp(start, codings[i])) |
830 | 0 | return (codings[i]); |
831 | 0 | } |
832 | 0 | } |
833 | 0 | } |
834 | | |
835 | 0 | return (NULL); |
836 | 0 | } |
837 | | |
838 | | |
839 | | // |
840 | | // 'httpGetCookie()' - Get cookie data from the HTTP connection. |
841 | | // |
842 | | // This function returns any HTTP "Set-Cookie:" or "Cookie:" header data for the |
843 | | // given HTTP connection as described in RFC 6265. Use the |
844 | | // @link httpGetCookieValue@ to get the value of a named "Cookie:" value. |
845 | | // |
846 | | |
847 | | const char * // O - Cookie data or `NULL` |
848 | | httpGetCookie(http_t *http) // I - HTTP connection |
849 | 0 | { |
850 | 0 | return (http ? http->cookie : NULL); |
851 | 0 | } |
852 | | |
853 | | |
854 | | // |
855 | | // 'httpGetCookieValue()' - Get the value of a named cookie from the HTTP connection. |
856 | | // |
857 | | // This function copies the value of a named cookie in the HTTP "Cookie:" header |
858 | | // for the given HTTP connection as described in RFC 6265. Use the |
859 | | // @link httpGetCookie@ function to get the original "Cookie:" string. |
860 | | // |
861 | | |
862 | | char * // O - Cookie value or `NULL` if not present |
863 | | httpGetCookieValue(http_t *http, // I - HTTP connection |
864 | | const char *name, // I - Cookie name |
865 | | char *buffer, // I - Value buffer |
866 | | size_t bufsize) // I - Size of value buffer |
867 | 0 | { |
868 | 0 | const char *cookie; // Cookie: header value |
869 | 0 | char current[128], // Current name string |
870 | 0 | *ptr, // Pointer into name/buffer |
871 | 0 | *end; // End of name/buffer |
872 | 0 | bool match; // Does the current name match? |
873 | | |
874 | | |
875 | | // Range check input... |
876 | 0 | if (buffer) |
877 | 0 | *buffer = '\0'; |
878 | |
|
879 | 0 | if (!http || !http->cookie || !name || !buffer || bufsize < 2) |
880 | 0 | return (NULL); |
881 | | |
882 | | // Loop through the cookie string... |
883 | 0 | for (cookie = http->cookie; *cookie;) |
884 | 0 | { |
885 | | // Skip leading whitespace... |
886 | 0 | while (isspace(*cookie & 255)) |
887 | 0 | cookie ++; |
888 | 0 | if (!*cookie) |
889 | 0 | break; |
890 | | |
891 | | // Copy the name... |
892 | 0 | for (ptr = current, end = current + sizeof(current) - 1; *cookie && *cookie != '=';) |
893 | 0 | { |
894 | 0 | if (ptr < end) |
895 | 0 | *ptr++ = *cookie++; |
896 | 0 | else |
897 | 0 | cookie ++; |
898 | 0 | } |
899 | |
|
900 | 0 | if (*cookie != '=') |
901 | 0 | break; |
902 | | |
903 | 0 | *ptr = '\0'; |
904 | 0 | match = !strcmp(current, name); |
905 | 0 | cookie ++; |
906 | | |
907 | | // Then the value... |
908 | 0 | if (*cookie == '\"') |
909 | 0 | { |
910 | | // Copy quoted value... |
911 | 0 | for (cookie ++, ptr = buffer, end = buffer + bufsize - 1; *cookie && *cookie != '\"';) |
912 | 0 | { |
913 | 0 | if (match && ptr < end) |
914 | 0 | *ptr++ = *cookie++; |
915 | 0 | else |
916 | 0 | cookie ++; |
917 | 0 | } |
918 | |
|
919 | 0 | if (*cookie == '\"') |
920 | 0 | cookie ++; |
921 | 0 | else |
922 | 0 | match = false; |
923 | 0 | } |
924 | 0 | else |
925 | 0 | { |
926 | | // Copy unquoted value... |
927 | 0 | for (ptr = buffer, end = buffer + bufsize - 1; *cookie && *cookie != ';';) |
928 | 0 | { |
929 | 0 | if (match && ptr < end) |
930 | 0 | *ptr++ = *cookie++; |
931 | 0 | else |
932 | 0 | cookie ++; |
933 | 0 | } |
934 | 0 | } |
935 | |
|
936 | 0 | if (match) |
937 | 0 | { |
938 | | // Got the value we were looking for, nul-terminate and return... |
939 | 0 | *ptr = '\0'; |
940 | 0 | return (buffer); |
941 | 0 | } |
942 | | |
943 | | // Skip over separator... |
944 | 0 | if (*cookie == ';') |
945 | 0 | cookie ++; |
946 | 0 | } |
947 | | |
948 | | // If we get here then we never found the cookie... |
949 | 0 | return (NULL); |
950 | 0 | } |
951 | | |
952 | | |
953 | | // |
954 | | // 'httpGetEncryption()' - Get the current encryption mode of a connection. |
955 | | // |
956 | | // This function returns the encryption mode for the connection. Use the |
957 | | // @link httpIsEncrypted@ function to determine whether a TLS session has |
958 | | // been established. |
959 | | // |
960 | | |
961 | | http_encryption_t // O - Current encryption mode |
962 | | httpGetEncryption(http_t *http) // I - HTTP connection |
963 | 0 | { |
964 | 0 | return (http ? http->encryption : HTTP_ENCRYPTION_IF_REQUESTED); |
965 | 0 | } |
966 | | |
967 | | |
968 | | // |
969 | | // 'httpGetError()' - Get the last error on a connection. |
970 | | // |
971 | | |
972 | | int // O - Error code (errno) value |
973 | | httpGetError(http_t *http) // I - HTTP connection |
974 | 0 | { |
975 | 0 | if (http) |
976 | 0 | return (http->error); |
977 | 0 | else |
978 | 0 | return (EINVAL); |
979 | 0 | } |
980 | | |
981 | | |
982 | | // |
983 | | // 'httpGetExpect()' - Get the value of the Expect header, if any. |
984 | | // |
985 | | // Returns `HTTP_STATUS_NONE` if there is no Expect header, otherwise |
986 | | // returns the expected HTTP status code, typically `HTTP_STATUS_CONTINUE`. |
987 | | // |
988 | | |
989 | | http_status_t // O - Expect: status, if any |
990 | | httpGetExpect(http_t *http) // I - HTTP connection |
991 | 0 | { |
992 | 0 | if (!http) |
993 | 0 | return (HTTP_STATUS_ERROR); |
994 | 0 | else |
995 | 0 | return (http->expect); |
996 | 0 | } |
997 | | |
998 | | |
999 | | // |
1000 | | // 'httpGetFd()' - Get the file descriptor associated with a connection. |
1001 | | // |
1002 | | |
1003 | | int // O - File descriptor or -1 if none |
1004 | | httpGetFd(http_t *http) // I - HTTP connection |
1005 | 0 | { |
1006 | 0 | return (http ? http->fd : -1); |
1007 | 0 | } |
1008 | | |
1009 | | |
1010 | | // |
1011 | | // 'httpGetField()' - Get a field value from a request/response. |
1012 | | // |
1013 | | |
1014 | | const char * // O - Field value |
1015 | | httpGetField(http_t *http, // I - HTTP connection |
1016 | | http_field_t field) // I - Field to get |
1017 | 0 | { |
1018 | 0 | if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) |
1019 | 0 | return (NULL); |
1020 | 0 | else if (http->fields[field]) |
1021 | 0 | return (http->fields[field]); |
1022 | 0 | else |
1023 | 0 | return (""); |
1024 | 0 | } |
1025 | | |
1026 | | |
1027 | | // |
1028 | | // 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection. |
1029 | | // |
1030 | | |
1031 | | http_keepalive_t // O - Keep-Alive state |
1032 | | httpGetKeepAlive(http_t *http) // I - HTTP connection |
1033 | 0 | { |
1034 | 0 | return (http ? http->keep_alive : HTTP_KEEPALIVE_OFF); |
1035 | 0 | } |
1036 | | |
1037 | | |
1038 | | // |
1039 | | // 'httpGetLength()' - Get the amount of data remaining from the content-length |
1040 | | // or transfer-encoding fields. |
1041 | | // |
1042 | | // This function returns the complete content length, even for content larger |
1043 | | // than 2^31 - 1. |
1044 | | // |
1045 | | |
1046 | | off_t // O - Content length |
1047 | | httpGetLength(http_t *http) // I - HTTP connection |
1048 | 0 | { |
1049 | 0 | off_t remaining; // Remaining length |
1050 | | |
1051 | |
|
1052 | 0 | DEBUG_printf("2httpGetLength(http=%p), state=%s", (void *)http, http ? httpStateString(http->state) : "NONE"); |
1053 | |
|
1054 | 0 | if (!http) |
1055 | 0 | return (-1); |
1056 | | |
1057 | 0 | if (http->fields[HTTP_FIELD_TRANSFER_ENCODING] && !_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked")) |
1058 | 0 | { |
1059 | 0 | DEBUG_puts("4httpGetLength: chunked request!"); |
1060 | 0 | remaining = 0; |
1061 | 0 | } |
1062 | 0 | else |
1063 | 0 | { |
1064 | | // The following is a hack for HTTP servers that don't send a Content-Length |
1065 | | // or Transfer-Encoding field. If there is no Content-Length then the |
1066 | | // connection must close after the transfer is complete... |
1067 | 0 | if (!http->fields[HTTP_FIELD_CONTENT_LENGTH] || !http->fields[HTTP_FIELD_CONTENT_LENGTH][0]) |
1068 | 0 | { |
1069 | | // Default content length is 0 for errors and certain types of operations, |
1070 | | // and 2^31-1 for other successful requests... |
1071 | 0 | if (http->status >= HTTP_STATUS_MULTIPLE_CHOICES || |
1072 | 0 | http->state == HTTP_STATE_OPTIONS || |
1073 | 0 | (http->state == HTTP_STATE_GET && http->mode == _HTTP_MODE_SERVER) || |
1074 | 0 | http->state == HTTP_STATE_HEAD || |
1075 | 0 | (http->state == HTTP_STATE_PUT && http->mode == _HTTP_MODE_CLIENT) || |
1076 | 0 | http->state == HTTP_STATE_DELETE || |
1077 | 0 | http->state == HTTP_STATE_TRACE || |
1078 | 0 | http->state == HTTP_STATE_CONNECT) |
1079 | 0 | remaining = 0; |
1080 | 0 | else |
1081 | 0 | remaining = 2147483647; |
1082 | 0 | } |
1083 | 0 | else if ((remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH], NULL, 10)) < 0) |
1084 | 0 | { |
1085 | 0 | remaining = -1; |
1086 | 0 | } |
1087 | |
|
1088 | 0 | DEBUG_printf("4httpGetLength: content_length=" CUPS_LLFMT, CUPS_LLCAST remaining); |
1089 | 0 | } |
1090 | |
|
1091 | 0 | return (remaining); |
1092 | 0 | } |
1093 | | |
1094 | | |
1095 | | // |
1096 | | // 'httpGetPending()' - Get the number of bytes that are buffered for writing. |
1097 | | // |
1098 | | |
1099 | | size_t // O - Number of bytes buffered |
1100 | | httpGetPending(http_t *http) // I - HTTP connection |
1101 | 0 | { |
1102 | 0 | return (http ? (size_t)http->wused : 0); |
1103 | 0 | } |
1104 | | |
1105 | | |
1106 | | // |
1107 | | // 'httpGetReady()' - Get the number of bytes that can be read without blocking. |
1108 | | // |
1109 | | |
1110 | | size_t // O - Number of bytes available |
1111 | | httpGetReady(http_t *http) // I - HTTP connection |
1112 | 0 | { |
1113 | 0 | if (!http) |
1114 | 0 | return (0); |
1115 | 0 | else if (http->used > 0) |
1116 | 0 | return ((size_t)http->used); |
1117 | 0 | else if (http->tls) |
1118 | 0 | return (_httpTLSPending(http)); |
1119 | | |
1120 | 0 | return (0); |
1121 | 0 | } |
1122 | | |
1123 | | |
1124 | | // |
1125 | | // 'httpGetRemaining()' - Get the number of remaining bytes in the message |
1126 | | // body or current chunk. |
1127 | | // |
1128 | | // The @link httpIsChunked@ function can be used to determine whether the |
1129 | | // message body is chunked or fixed-length. |
1130 | | // |
1131 | | |
1132 | | size_t // O - Remaining bytes |
1133 | | httpGetRemaining(http_t *http) // I - HTTP connection |
1134 | 0 | { |
1135 | 0 | return (http ? (size_t)http->data_remaining : 0); |
1136 | 0 | } |
1137 | | |
1138 | | |
1139 | | // |
1140 | | // 'httpGets()' - Get a line of text from a HTTP connection. |
1141 | | // |
1142 | | |
1143 | | char * // O - Line or `NULL` |
1144 | | httpGets(http_t *http, // I - HTTP connection |
1145 | | char *line, // I - Line to read into |
1146 | | size_t length) // I - Max length of buffer |
1147 | 0 | { |
1148 | 0 | char *lineptr, // Pointer into line |
1149 | 0 | *lineend, // End of line |
1150 | 0 | *bufptr, // Pointer into input buffer |
1151 | 0 | *bufend; // Pointer to end of buffer |
1152 | 0 | ssize_t bytes; // Number of bytes read |
1153 | 0 | int eol; // End-of-line? |
1154 | | |
1155 | |
|
1156 | 0 | DEBUG_printf("2httpGets(http=%p, line=%p, length=%u)", (void *)http, (void *)line, (unsigned)length); |
1157 | |
|
1158 | 0 | if (!http || !line || length <= 1) |
1159 | 0 | return (NULL); |
1160 | | |
1161 | | // Read a line from the buffer... |
1162 | 0 | http->error = 0; |
1163 | 0 | lineptr = line; |
1164 | 0 | lineend = line + length - 1; |
1165 | 0 | eol = 0; |
1166 | |
|
1167 | 0 | while (lineptr < lineend) |
1168 | 0 | { |
1169 | | // Pre-load the buffer as needed... |
1170 | | #ifdef _WIN32 |
1171 | | WSASetLastError(0); |
1172 | | #else |
1173 | 0 | errno = 0; |
1174 | 0 | #endif // _WIN32 |
1175 | |
|
1176 | 0 | while (http->used == 0) |
1177 | 0 | { |
1178 | | // No newline; see if there is more data to be read... |
1179 | 0 | while (!_httpWait(http, http->wait_value, 1)) |
1180 | 0 | { |
1181 | 0 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
1182 | 0 | continue; |
1183 | | |
1184 | 0 | DEBUG_puts("3httpGets: Timed out!"); |
1185 | | #ifdef _WIN32 |
1186 | | http->error = WSAETIMEDOUT; |
1187 | | #else |
1188 | 0 | http->error = ETIMEDOUT; |
1189 | 0 | #endif // _WIN32 |
1190 | 0 | return (NULL); |
1191 | 0 | } |
1192 | | |
1193 | 0 | bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used)); |
1194 | |
|
1195 | 0 | DEBUG_printf("4httpGets: read " CUPS_LLFMT " bytes.", CUPS_LLCAST bytes); |
1196 | |
|
1197 | 0 | if (bytes < 0) |
1198 | 0 | { |
1199 | | // Nope, can't get a line this time... |
1200 | | #ifdef _WIN32 |
1201 | | DEBUG_printf("3httpGets: recv() error %d!", WSAGetLastError()); |
1202 | | |
1203 | | if (WSAGetLastError() == WSAEINTR) |
1204 | | { |
1205 | | continue; |
1206 | | } |
1207 | | else if (WSAGetLastError() == WSAEWOULDBLOCK) |
1208 | | { |
1209 | | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
1210 | | continue; |
1211 | | |
1212 | | http->error = WSAGetLastError(); |
1213 | | } |
1214 | | else if (WSAGetLastError() != http->error) |
1215 | | { |
1216 | | http->error = WSAGetLastError(); |
1217 | | continue; |
1218 | | } |
1219 | | |
1220 | | #else |
1221 | 0 | DEBUG_printf("3httpGets: recv() error %d!", errno); |
1222 | |
|
1223 | 0 | if (errno == EINTR) |
1224 | 0 | { |
1225 | 0 | continue; |
1226 | 0 | } |
1227 | 0 | else if (errno == EWOULDBLOCK || errno == EAGAIN) |
1228 | 0 | { |
1229 | 0 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
1230 | 0 | continue; |
1231 | 0 | else if (!http->timeout_cb && errno == EAGAIN) |
1232 | 0 | continue; |
1233 | | |
1234 | 0 | http->error = errno; |
1235 | 0 | } |
1236 | 0 | else if (errno != http->error) |
1237 | 0 | { |
1238 | 0 | http->error = errno; |
1239 | 0 | continue; |
1240 | 0 | } |
1241 | 0 | #endif // _WIN32 |
1242 | | |
1243 | 0 | return (NULL); |
1244 | 0 | } |
1245 | 0 | else if (bytes == 0) |
1246 | 0 | { |
1247 | 0 | http->error = EPIPE; |
1248 | |
|
1249 | 0 | return (NULL); |
1250 | 0 | } |
1251 | | |
1252 | | // Yup, update the amount used... |
1253 | 0 | http->used += (int)bytes; |
1254 | 0 | } |
1255 | | |
1256 | | // Now copy as much of the current line as possible... |
1257 | 0 | for (bufptr = http->buffer, bufend = http->buffer + http->used; lineptr < lineend && bufptr < bufend;) |
1258 | 0 | { |
1259 | 0 | if (*bufptr == 0x0a) |
1260 | 0 | { |
1261 | 0 | eol = 1; |
1262 | 0 | bufptr ++; |
1263 | 0 | break; |
1264 | 0 | } |
1265 | 0 | else if (*bufptr == 0x0d) |
1266 | 0 | { |
1267 | 0 | bufptr ++; |
1268 | 0 | } |
1269 | 0 | else |
1270 | 0 | { |
1271 | 0 | *lineptr++ = *bufptr++; |
1272 | 0 | } |
1273 | 0 | } |
1274 | |
|
1275 | 0 | http->used -= (int)(bufptr - http->buffer); |
1276 | 0 | if (http->used > 0) |
1277 | 0 | memmove(http->buffer, bufptr, (size_t)http->used); |
1278 | |
|
1279 | 0 | if (eol) |
1280 | 0 | { |
1281 | | // End of line... |
1282 | 0 | http->activity = time(NULL); |
1283 | |
|
1284 | 0 | *lineptr = '\0'; |
1285 | |
|
1286 | 0 | DEBUG_printf("3httpGets: Returning \"%s\"", line); |
1287 | |
|
1288 | 0 | return (line); |
1289 | 0 | } |
1290 | 0 | } |
1291 | | |
1292 | 0 | DEBUG_puts("3httpGets: No new line available!"); |
1293 | |
|
1294 | 0 | return (NULL); |
1295 | 0 | } |
1296 | | |
1297 | | |
1298 | | // |
1299 | | // 'httpGetState()' - Get the current state of the HTTP request. |
1300 | | // |
1301 | | |
1302 | | http_state_t // O - HTTP state |
1303 | | httpGetState(http_t *http) // I - HTTP connection |
1304 | 0 | { |
1305 | 0 | return (http ? http->state : HTTP_STATE_ERROR); |
1306 | 0 | } |
1307 | | |
1308 | | |
1309 | | // |
1310 | | // 'httpGetStatus()' - Get the status of the last HTTP request. |
1311 | | // |
1312 | | // |
1313 | | // |
1314 | | |
1315 | | http_status_t // O - HTTP status |
1316 | | httpGetStatus(http_t *http) // I - HTTP connection |
1317 | 0 | { |
1318 | 0 | return (http ? http->status : HTTP_STATUS_ERROR); |
1319 | 0 | } |
1320 | | |
1321 | | |
1322 | | // |
1323 | | // 'httpGetSubField()' - Get a sub-field value. |
1324 | | // |
1325 | | |
1326 | | char * // O - Value or `NULL` |
1327 | | httpGetSubField(http_t *http, // I - HTTP connection |
1328 | | http_field_t field, // I - Field index |
1329 | | const char *name, // I - Name of sub-field |
1330 | | char *value, // O - Value string |
1331 | | size_t valuelen) // I - Size of value buffer |
1332 | 0 | { |
1333 | 0 | const char *fptr; // Pointer into field |
1334 | 0 | char temp[HTTP_MAX_VALUE], // Temporary buffer for name |
1335 | 0 | *ptr, // Pointer into string buffer |
1336 | 0 | *end; // End of value buffer |
1337 | | |
1338 | |
|
1339 | 0 | DEBUG_printf("2httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%u)", (void *)http, field, name, (void *)value, (unsigned)valuelen); |
1340 | |
|
1341 | 0 | if (value) |
1342 | 0 | *value = '\0'; |
1343 | |
|
1344 | 0 | if (!http || !name || !value || valuelen < 2 || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX || !http->fields[field]) |
1345 | 0 | return (NULL); |
1346 | | |
1347 | 0 | end = value + valuelen - 1; |
1348 | |
|
1349 | 0 | for (fptr = http->fields[field]; *fptr;) |
1350 | 0 | { |
1351 | | // Skip leading whitespace... |
1352 | 0 | while (_cups_isspace(*fptr)) |
1353 | 0 | fptr ++; |
1354 | |
|
1355 | 0 | if (*fptr == ',') |
1356 | 0 | { |
1357 | 0 | fptr ++; |
1358 | 0 | continue; |
1359 | 0 | } |
1360 | | |
1361 | | // Get the sub-field name... |
1362 | 0 | for (ptr = temp; *fptr && *fptr != '=' && !_cups_isspace(*fptr) && ptr < (temp + sizeof(temp) - 1); *ptr++ = *fptr++); |
1363 | |
|
1364 | 0 | *ptr = '\0'; |
1365 | |
|
1366 | 0 | DEBUG_printf("4httpGetSubField: name=\"%s\"", temp); |
1367 | | |
1368 | | // Skip trailing chars up to the '='... |
1369 | 0 | while (_cups_isspace(*fptr)) |
1370 | 0 | fptr ++; |
1371 | |
|
1372 | 0 | if (!*fptr) |
1373 | 0 | break; |
1374 | | |
1375 | 0 | if (*fptr != '=') |
1376 | 0 | continue; |
1377 | | |
1378 | | // Skip = and leading whitespace... |
1379 | 0 | fptr ++; |
1380 | |
|
1381 | 0 | while (_cups_isspace(*fptr)) |
1382 | 0 | fptr ++; |
1383 | |
|
1384 | 0 | if (*fptr == '\"') |
1385 | 0 | { |
1386 | | // Read quoted string... |
1387 | 0 | for (ptr = value, fptr ++; *fptr && *fptr != '\"' && ptr < end; *ptr++ = *fptr++); |
1388 | |
|
1389 | 0 | *ptr = '\0'; |
1390 | |
|
1391 | 0 | while (*fptr && *fptr != '\"') |
1392 | 0 | fptr ++; |
1393 | |
|
1394 | 0 | if (*fptr) |
1395 | 0 | fptr ++; |
1396 | 0 | } |
1397 | 0 | else |
1398 | 0 | { |
1399 | | // Read unquoted string... |
1400 | 0 | for (ptr = value; *fptr && !_cups_isspace(*fptr) && *fptr != ',' && ptr < end; *ptr++ = *fptr++); |
1401 | |
|
1402 | 0 | *ptr = '\0'; |
1403 | |
|
1404 | 0 | while (*fptr && !_cups_isspace(*fptr) && *fptr != ',') |
1405 | 0 | fptr ++; |
1406 | 0 | } |
1407 | |
|
1408 | 0 | DEBUG_printf("4httpGetSubField: value=\"%s\"", value); |
1409 | | |
1410 | | // See if this is the one... |
1411 | 0 | if (!strcmp(name, temp)) |
1412 | 0 | { |
1413 | 0 | DEBUG_printf("3httpGetSubField: Returning \"%s\"", value); |
1414 | 0 | return (value); |
1415 | 0 | } |
1416 | 0 | } |
1417 | | |
1418 | 0 | value[0] = '\0'; |
1419 | |
|
1420 | 0 | DEBUG_puts("3httpGetSubField: Returning NULL"); |
1421 | |
|
1422 | 0 | return (NULL); |
1423 | 0 | } |
1424 | | |
1425 | | |
1426 | | // |
1427 | | // 'httpGetVersion()' - Get the HTTP version at the other end. |
1428 | | // |
1429 | | |
1430 | | http_version_t // O - Version number |
1431 | | httpGetVersion(http_t *http) // I - HTTP connection |
1432 | 0 | { |
1433 | 0 | return (http ? http->version : HTTP_VERSION_1_0); |
1434 | 0 | } |
1435 | | |
1436 | | |
1437 | | // |
1438 | | // 'httpInitialize()' - Initialize the HTTP interface library and set the default HTTP proxy (if any). |
1439 | | // |
1440 | | |
1441 | | void |
1442 | | httpInitialize(void) |
1443 | 0 | { |
1444 | 0 | static int initialized = 0; // Have we been called before? |
1445 | | #ifdef _WIN32 |
1446 | | WSADATA winsockdata; // WinSock data |
1447 | | #endif // _WIN32 |
1448 | | |
1449 | |
|
1450 | 0 | _cupsGlobalLock(); |
1451 | 0 | if (initialized) |
1452 | 0 | { |
1453 | 0 | _cupsGlobalUnlock(); |
1454 | 0 | return; |
1455 | 0 | } |
1456 | | |
1457 | | #ifdef _WIN32 |
1458 | | WSAStartup(MAKEWORD(2,2), &winsockdata); |
1459 | | |
1460 | | #elif !defined(SO_NOSIGPIPE) |
1461 | | // Ignore SIGPIPE signals... |
1462 | 0 | struct sigaction action; // POSIX sigaction data |
1463 | |
|
1464 | 0 | memset(&action, 0, sizeof(action)); |
1465 | 0 | action.sa_handler = SIG_IGN; |
1466 | 0 | sigaction(SIGPIPE, &action, NULL); |
1467 | 0 | #endif // _WIN32 |
1468 | |
|
1469 | 0 | _httpTLSInitialize(); |
1470 | |
|
1471 | 0 | initialized = 1; |
1472 | 0 | _cupsGlobalUnlock(); |
1473 | 0 | } |
1474 | | |
1475 | | |
1476 | | // |
1477 | | // 'httpIsChunked()' - Report whether a message body is chunked. |
1478 | | // |
1479 | | // This function returns `true` if the message body is composed of |
1480 | | // variable-length chunks. |
1481 | | // |
1482 | | |
1483 | | bool // O - `true` if chunked, `false` if not |
1484 | | httpIsChunked(http_t *http) // I - HTTP connection |
1485 | 0 | { |
1486 | 0 | return (http ? http->data_encoding == HTTP_ENCODING_CHUNKED : 0); |
1487 | 0 | } |
1488 | | |
1489 | | |
1490 | | // |
1491 | | // 'httpIsEncrypted()' - Report whether a connection is encrypted. |
1492 | | // |
1493 | | // This function returns `true` if the connection is currently encrypted. |
1494 | | // |
1495 | | |
1496 | | bool // O - `true` if encrypted, `false` if not |
1497 | | httpIsEncrypted(http_t *http) // I - HTTP connection |
1498 | 0 | { |
1499 | 0 | return (http ? http->tls != NULL : 0); |
1500 | 0 | } |
1501 | | |
1502 | | |
1503 | | // |
1504 | | // 'httpPeek()' - Peek at data from a HTTP connection. |
1505 | | // |
1506 | | // This function copies available data from the given HTTP connection, reading |
1507 | | // a buffer as needed. The data is still available for reading using |
1508 | | // @link httpRead@. |
1509 | | // |
1510 | | // For non-blocking connections the usual timeouts apply. |
1511 | | // |
1512 | | |
1513 | | ssize_t // O - Number of bytes copied |
1514 | | httpPeek(http_t *http, // I - HTTP connection |
1515 | | char *buffer, // I - Buffer for data |
1516 | | size_t length) // I - Maximum number of bytes |
1517 | 0 | { |
1518 | 0 | ssize_t bytes; // Bytes read |
1519 | 0 | char len[32]; // Length string |
1520 | | |
1521 | |
|
1522 | 0 | DEBUG_printf("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length); |
1523 | |
|
1524 | 0 | if (http == NULL || buffer == NULL) |
1525 | 0 | return (-1); |
1526 | | |
1527 | 0 | http->activity = time(NULL); |
1528 | 0 | http->error = 0; |
1529 | |
|
1530 | 0 | if (length <= 0) |
1531 | 0 | return (0); |
1532 | | |
1533 | 0 | if (http->data_encoding == HTTP_ENCODING_CHUNKED && http->data_remaining <= 0) |
1534 | 0 | { |
1535 | 0 | DEBUG_puts("2httpPeek: Getting chunk length..."); |
1536 | |
|
1537 | 0 | if (httpGets(http, len, sizeof(len)) == NULL) |
1538 | 0 | { |
1539 | 0 | DEBUG_puts("1httpPeek: Could not get length!"); |
1540 | 0 | return (0); |
1541 | 0 | } |
1542 | | |
1543 | 0 | if (!len[0]) |
1544 | 0 | { |
1545 | 0 | DEBUG_puts("1httpPeek: Blank chunk length, trying again..."); |
1546 | 0 | if (!httpGets(http, len, sizeof(len))) |
1547 | 0 | { |
1548 | 0 | DEBUG_puts("1httpPeek: Could not get chunk length."); |
1549 | 0 | return (0); |
1550 | 0 | } |
1551 | 0 | } |
1552 | | |
1553 | 0 | http->data_remaining = strtoll(len, NULL, 16); |
1554 | |
|
1555 | 0 | if (http->data_remaining < 0) |
1556 | 0 | { |
1557 | 0 | DEBUG_puts("1httpPeek: Negative chunk length!"); |
1558 | 0 | return (0); |
1559 | 0 | } |
1560 | 0 | } |
1561 | | |
1562 | 0 | DEBUG_printf("2httpPeek: data_remaining=" CUPS_LLFMT, CUPS_LLCAST http->data_remaining); |
1563 | |
|
1564 | 0 | if (http->data_remaining <= 0 && http->data_encoding != HTTP_ENCODING_FIELDS) |
1565 | 0 | { |
1566 | | // A zero-length chunk ends a transfer; unless we are reading POST data, go idle... |
1567 | 0 | if (http->coding >= _HTTP_CODING_GUNZIP) |
1568 | 0 | http_content_coding_finish(http); |
1569 | |
|
1570 | 0 | if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
1571 | 0 | httpGets(http, len, sizeof(len)); |
1572 | |
|
1573 | 0 | if (http->state == HTTP_STATE_LOCK_RECV || http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_PROPFIND_RECV || http->state == HTTP_STATE_PROPPATCH_RECV) |
1574 | 0 | http->state ++; |
1575 | 0 | else |
1576 | 0 | http->state = HTTP_STATE_STATUS; |
1577 | |
|
1578 | 0 | DEBUG_printf("1httpPeek: 0-length chunk, set state to %s.", httpStateString(http->state)); |
1579 | | |
1580 | | // Prevent future reads for this request... |
1581 | 0 | http->data_encoding = HTTP_ENCODING_FIELDS; |
1582 | |
|
1583 | 0 | return (0); |
1584 | 0 | } |
1585 | 0 | else if (length > (size_t)http->data_remaining) |
1586 | 0 | { |
1587 | 0 | length = (size_t)http->data_remaining; |
1588 | 0 | } |
1589 | | |
1590 | 0 | if (http->used == 0 && (http->coding == _HTTP_CODING_IDENTITY || (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in == 0))) |
1591 | 0 | { |
1592 | | // Buffer small reads for better performance... |
1593 | 0 | ssize_t buflen; // Length of read for buffer |
1594 | |
|
1595 | 0 | if (!http->blocking) |
1596 | 0 | { |
1597 | 0 | while (!httpWait(http, http->wait_value)) |
1598 | 0 | { |
1599 | 0 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
1600 | 0 | continue; |
1601 | | |
1602 | 0 | return (0); |
1603 | 0 | } |
1604 | 0 | } |
1605 | | |
1606 | 0 | if ((size_t)http->data_remaining > sizeof(http->buffer)) |
1607 | 0 | buflen = sizeof(http->buffer); |
1608 | 0 | else |
1609 | 0 | buflen = (ssize_t)http->data_remaining; |
1610 | |
|
1611 | 0 | DEBUG_printf("2httpPeek: Reading %d bytes into buffer.", (int)buflen); |
1612 | 0 | bytes = http_read(http, http->buffer, (size_t)buflen); |
1613 | |
|
1614 | 0 | DEBUG_printf("2httpPeek: Read " CUPS_LLFMT " bytes into buffer.", CUPS_LLCAST bytes); |
1615 | 0 | if (bytes > 0) |
1616 | 0 | { |
1617 | | #ifdef DEBUG |
1618 | | http_debug_hex("httpPeek", http->buffer, (int)bytes); |
1619 | | #endif // DEBUG |
1620 | |
|
1621 | 0 | http->used = (int)bytes; |
1622 | 0 | } |
1623 | 0 | } |
1624 | | |
1625 | 0 | if (http->coding >= _HTTP_CODING_GUNZIP) |
1626 | 0 | { |
1627 | 0 | int zerr; // Decompressor error |
1628 | 0 | z_stream stream; // Copy of decompressor stream |
1629 | |
|
1630 | 0 | memset(&stream, 0, sizeof(stream)); |
1631 | |
|
1632 | 0 | if (http->used > 0 && ((z_stream *)http->stream)->avail_in < HTTP_MAX_BUFFER) |
1633 | 0 | { |
1634 | 0 | size_t buflen = HTTP_MAX_BUFFER - ((z_stream *)http->stream)->avail_in; |
1635 | | // Number of bytes to copy |
1636 | |
|
1637 | 0 | if (((z_stream *)http->stream)->avail_in > 0 && ((z_stream *)http->stream)->next_in > http->sbuffer) |
1638 | 0 | memmove(http->sbuffer, ((z_stream *)http->stream)->next_in, ((z_stream *)http->stream)->avail_in); |
1639 | |
|
1640 | 0 | ((z_stream *)http->stream)->next_in = http->sbuffer; |
1641 | |
|
1642 | 0 | if (buflen > (size_t)http->data_remaining) |
1643 | 0 | buflen = (size_t)http->data_remaining; |
1644 | |
|
1645 | 0 | if (buflen > (size_t)http->used) |
1646 | 0 | buflen = (size_t)http->used; |
1647 | |
|
1648 | 0 | DEBUG_printf("1httpPeek: Copying %d more bytes of data into decompression buffer.", (int)buflen); |
1649 | |
|
1650 | 0 | memcpy(http->sbuffer + ((z_stream *)http->stream)->avail_in, http->buffer, buflen); |
1651 | 0 | ((z_stream *)http->stream)->avail_in += buflen; |
1652 | 0 | http->used -= (int)buflen; |
1653 | 0 | http->data_remaining -= (off_t)buflen; |
1654 | |
|
1655 | 0 | if (http->used > 0) |
1656 | 0 | memmove(http->buffer, http->buffer + buflen, (size_t)http->used); |
1657 | 0 | } |
1658 | |
|
1659 | 0 | DEBUG_printf("2httpPeek: length=%d, avail_in=%d", (int)length, (int)((z_stream *)http->stream)->avail_in); |
1660 | |
|
1661 | 0 | if (inflateCopy(&stream, (z_stream *)http->stream) != Z_OK) |
1662 | 0 | { |
1663 | 0 | DEBUG_puts("2httpPeek: Unable to copy decompressor stream."); |
1664 | 0 | http->error = ENOMEM; |
1665 | 0 | inflateEnd(&stream); |
1666 | 0 | return (-1); |
1667 | 0 | } |
1668 | | |
1669 | 0 | stream.next_out = (Bytef *)buffer; |
1670 | 0 | stream.avail_out = (uInt)length; |
1671 | |
|
1672 | 0 | zerr = inflate(&stream, Z_SYNC_FLUSH); |
1673 | 0 | inflateEnd(&stream); |
1674 | |
|
1675 | 0 | if (zerr < Z_OK) |
1676 | 0 | { |
1677 | 0 | DEBUG_printf("2httpPeek: zerr=%d", zerr); |
1678 | | #ifdef DEBUG |
1679 | | http_debug_hex("2httpPeek", (char *)http->sbuffer, (int)((z_stream *)http->stream)->avail_in); |
1680 | | #endif // DEBUG |
1681 | |
|
1682 | 0 | http->error = EIO; |
1683 | 0 | return (-1); |
1684 | 0 | } |
1685 | | |
1686 | 0 | bytes = (ssize_t)(length - ((z_stream *)http->stream)->avail_out); |
1687 | 0 | } |
1688 | 0 | else if (http->used > 0) |
1689 | 0 | { |
1690 | 0 | if (length > (size_t)http->used) |
1691 | 0 | length = (size_t)http->used; |
1692 | |
|
1693 | 0 | bytes = (ssize_t)length; |
1694 | |
|
1695 | 0 | DEBUG_printf("2httpPeek: grabbing %d bytes from input buffer...", (int)bytes); |
1696 | |
|
1697 | 0 | memcpy(buffer, http->buffer, length); |
1698 | 0 | } |
1699 | 0 | else |
1700 | 0 | { |
1701 | 0 | bytes = 0; |
1702 | 0 | } |
1703 | | |
1704 | 0 | if (bytes < 0) |
1705 | 0 | { |
1706 | | #ifdef _WIN32 |
1707 | | if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) |
1708 | | bytes = 0; |
1709 | | else |
1710 | | http->error = WSAGetLastError(); |
1711 | | |
1712 | | #else |
1713 | 0 | if (errno == EINTR || errno == EAGAIN) |
1714 | 0 | bytes = 0; |
1715 | 0 | else |
1716 | 0 | http->error = errno; |
1717 | 0 | #endif // _WIN32 |
1718 | 0 | } |
1719 | 0 | else if (bytes == 0) |
1720 | 0 | { |
1721 | 0 | http->error = EPIPE; |
1722 | 0 | return (0); |
1723 | 0 | } |
1724 | | |
1725 | 0 | return (bytes); |
1726 | 0 | } |
1727 | | |
1728 | | |
1729 | | // |
1730 | | // 'httpPrintf()' - Print a formatted string to a HTTP connection. |
1731 | | // |
1732 | | |
1733 | | ssize_t // O - Number of bytes written or `-1` on error |
1734 | | httpPrintf(http_t *http, // I - HTTP connection |
1735 | | const char *format, // I - printf-style format string |
1736 | | ...) // I - Additional args as needed |
1737 | 0 | { |
1738 | 0 | ssize_t bytes; // Number of bytes to write |
1739 | 0 | char buf[65536]; // Buffer for formatted string |
1740 | 0 | va_list ap; // Variable argument pointer |
1741 | | |
1742 | |
|
1743 | 0 | DEBUG_printf("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http, format); |
1744 | |
|
1745 | 0 | va_start(ap, format); |
1746 | 0 | bytes = vsnprintf(buf, sizeof(buf), format, ap); |
1747 | 0 | va_end(ap); |
1748 | |
|
1749 | 0 | DEBUG_printf("3httpPrintf: (" CUPS_LLFMT " bytes) %s", CUPS_LLCAST bytes, buf); |
1750 | |
|
1751 | 0 | if (bytes > (ssize_t)(sizeof(buf) - 1)) |
1752 | 0 | { |
1753 | 0 | http->error = ENOMEM; |
1754 | 0 | return (-1); |
1755 | 0 | } |
1756 | 0 | else if (http->data_encoding == HTTP_ENCODING_FIELDS) |
1757 | 0 | { |
1758 | 0 | return (httpWrite(http, buf, (size_t)bytes)); |
1759 | 0 | } |
1760 | 0 | else |
1761 | 0 | { |
1762 | 0 | if (http->wused) |
1763 | 0 | { |
1764 | 0 | DEBUG_puts("4httpPrintf: flushing existing data..."); |
1765 | |
|
1766 | 0 | if (httpFlushWrite(http) < 0) |
1767 | 0 | return (-1); |
1768 | 0 | } |
1769 | | |
1770 | 0 | return (http_write(http, buf, (size_t)bytes)); |
1771 | 0 | } |
1772 | 0 | } |
1773 | | |
1774 | | |
1775 | | // |
1776 | | // 'httpRead()' - Read data from a HTTP connection. |
1777 | | // |
1778 | | |
1779 | | ssize_t // O - Number of bytes read |
1780 | | httpRead(http_t *http, // I - HTTP connection |
1781 | | char *buffer, // I - Buffer for data |
1782 | | size_t length) // I - Maximum number of bytes |
1783 | 0 | { |
1784 | 0 | ssize_t bytes; // Bytes read |
1785 | | |
1786 | |
|
1787 | 0 | DEBUG_printf("httpRead(http=%p, buffer=%p, length=" CUPS_LLFMT ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)http, (void *)buffer, CUPS_LLCAST length, http ? http->coding : 0, http ? http->data_encoding : 0, CUPS_LLCAST (http ? http->data_remaining : -1)); |
1788 | |
|
1789 | 0 | if (http == NULL || buffer == NULL) |
1790 | 0 | return (-1); |
1791 | | |
1792 | 0 | http->activity = time(NULL); |
1793 | 0 | http->error = 0; |
1794 | |
|
1795 | 0 | if (length <= 0) |
1796 | 0 | return (0); |
1797 | | |
1798 | 0 | if (http->coding >= _HTTP_CODING_GUNZIP) |
1799 | 0 | { |
1800 | 0 | do |
1801 | 0 | { |
1802 | 0 | if (((z_stream *)http->stream)->avail_in > 0) |
1803 | 0 | { |
1804 | 0 | int zerr; // Decompressor error |
1805 | |
|
1806 | 0 | DEBUG_printf("2httpRead: avail_in=%d, avail_out=%d", (int)((z_stream *)http->stream)->avail_in, (int)length); |
1807 | |
|
1808 | 0 | ((z_stream *)http->stream)->next_out = (Bytef *)buffer; |
1809 | 0 | ((z_stream *)http->stream)->avail_out = (uInt)length; |
1810 | |
|
1811 | 0 | if ((zerr = inflate((z_stream *)http->stream, Z_SYNC_FLUSH)) < Z_OK) |
1812 | 0 | { |
1813 | 0 | DEBUG_printf("2httpRead: zerr=%d", zerr); |
1814 | | #ifdef DEBUG |
1815 | | http_debug_hex("2httpRead", (char *)http->sbuffer, (int)((z_stream *)http->stream)->avail_in); |
1816 | | #endif // DEBUG |
1817 | |
|
1818 | 0 | http->error = EIO; |
1819 | 0 | return (-1); |
1820 | 0 | } |
1821 | | |
1822 | 0 | bytes = (ssize_t)(length - ((z_stream *)http->stream)->avail_out); |
1823 | |
|
1824 | 0 | DEBUG_printf("2httpRead: avail_in=%d, avail_out=%d, bytes=%d", ((z_stream *)http->stream)->avail_in, ((z_stream *)http->stream)->avail_out, (int)bytes); |
1825 | 0 | } |
1826 | 0 | else |
1827 | 0 | { |
1828 | 0 | bytes = 0; |
1829 | 0 | } |
1830 | | |
1831 | 0 | if (bytes == 0) |
1832 | 0 | { |
1833 | 0 | ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)((z_stream *)http->stream)->avail_in; |
1834 | | // Additional bytes for buffer |
1835 | |
|
1836 | 0 | if (buflen > 0) |
1837 | 0 | { |
1838 | 0 | if (((z_stream *)http->stream)->avail_in > 0 && ((z_stream *)http->stream)->next_in > http->sbuffer) |
1839 | 0 | memmove(http->sbuffer, ((z_stream *)http->stream)->next_in, ((z_stream *)http->stream)->avail_in); |
1840 | |
|
1841 | 0 | ((z_stream *)http->stream)->next_in = http->sbuffer; |
1842 | |
|
1843 | 0 | DEBUG_printf("1httpRead: Reading up to %d more bytes of data into decompression buffer.", (int)buflen); |
1844 | |
|
1845 | 0 | if (http->data_remaining > 0) |
1846 | 0 | { |
1847 | 0 | if (buflen > http->data_remaining) |
1848 | 0 | buflen = (ssize_t)http->data_remaining; |
1849 | |
|
1850 | 0 | bytes = http_read_buffered(http, (char *)http->sbuffer + ((z_stream *)http->stream)->avail_in, (size_t)buflen); |
1851 | 0 | } |
1852 | 0 | else if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
1853 | 0 | { |
1854 | 0 | bytes = http_read_chunk(http, (char *)http->sbuffer + ((z_stream *)http->stream)->avail_in, (size_t)buflen); |
1855 | 0 | } |
1856 | 0 | else |
1857 | 0 | { |
1858 | 0 | bytes = 0; |
1859 | 0 | } |
1860 | |
|
1861 | 0 | if (bytes < 0) |
1862 | 0 | return (bytes); |
1863 | 0 | else if (bytes == 0) |
1864 | 0 | break; |
1865 | | |
1866 | 0 | DEBUG_printf("1httpRead: Adding " CUPS_LLFMT " bytes to decompression buffer.", CUPS_LLCAST bytes); |
1867 | |
|
1868 | 0 | http->data_remaining -= bytes; |
1869 | 0 | ((z_stream *)http->stream)->avail_in += (uInt)bytes; |
1870 | |
|
1871 | 0 | if (http->data_remaining <= 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) |
1872 | 0 | { |
1873 | | // Read the trailing blank line now... |
1874 | 0 | char len[32]; // Length string |
1875 | |
|
1876 | 0 | httpGets(http, len, sizeof(len)); |
1877 | 0 | } |
1878 | |
|
1879 | 0 | bytes = 0; |
1880 | 0 | } |
1881 | 0 | else |
1882 | 0 | { |
1883 | 0 | return (0); |
1884 | 0 | } |
1885 | 0 | } |
1886 | 0 | } |
1887 | 0 | while (bytes == 0); |
1888 | 0 | } |
1889 | 0 | else if (http->data_remaining == 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) |
1890 | 0 | { |
1891 | 0 | if ((bytes = http_read_chunk(http, buffer, length)) > 0) |
1892 | 0 | { |
1893 | 0 | http->data_remaining -= bytes; |
1894 | |
|
1895 | 0 | if (http->data_remaining <= 0) |
1896 | 0 | { |
1897 | | // Read the trailing blank line now... |
1898 | 0 | char len[32]; // Length string |
1899 | |
|
1900 | 0 | httpGets(http, len, sizeof(len)); |
1901 | 0 | } |
1902 | 0 | } |
1903 | 0 | } |
1904 | 0 | else if (http->data_remaining <= 0) |
1905 | 0 | { |
1906 | | // No more data to read... |
1907 | 0 | return (0); |
1908 | 0 | } |
1909 | 0 | else |
1910 | 0 | { |
1911 | 0 | DEBUG_printf("1httpRead: Reading up to %d bytes into buffer.", (int)length); |
1912 | |
|
1913 | 0 | if (length > (size_t)http->data_remaining) |
1914 | 0 | length = (size_t)http->data_remaining; |
1915 | |
|
1916 | 0 | if ((bytes = http_read_buffered(http, buffer, length)) > 0) |
1917 | 0 | { |
1918 | 0 | http->data_remaining -= bytes; |
1919 | |
|
1920 | 0 | if (http->data_remaining <= 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) |
1921 | 0 | { |
1922 | | // Read the trailing blank line now... |
1923 | 0 | char len[32]; // Length string |
1924 | |
|
1925 | 0 | httpGets(http, len, sizeof(len)); |
1926 | 0 | } |
1927 | 0 | } |
1928 | 0 | } |
1929 | | |
1930 | 0 | if ((http->coding == _HTTP_CODING_IDENTITY || |
1931 | 0 | (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in == 0)) && |
1932 | 0 | ((http->data_remaining <= 0 && http->data_encoding == HTTP_ENCODING_LENGTH) || |
1933 | 0 | (http->data_encoding == HTTP_ENCODING_CHUNKED && bytes == 0))) |
1934 | 0 | { |
1935 | 0 | if (http->coding >= _HTTP_CODING_GUNZIP) |
1936 | 0 | http_content_coding_finish(http); |
1937 | |
|
1938 | 0 | if (http->state == HTTP_STATE_LOCK_RECV || http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_PROPFIND_RECV || http->state == HTTP_STATE_PROPPATCH_RECV) |
1939 | 0 | http->state ++; |
1940 | 0 | else if (http->state == HTTP_STATE_COPY_SEND || http->state == HTTP_STATE_DELETE_SEND || http->state == HTTP_STATE_GET_SEND || http->state == HTTP_STATE_LOCK_SEND || http->state == HTTP_STATE_MOVE_SEND || http->state == HTTP_STATE_POST_SEND || http->state == HTTP_STATE_PROPFIND_SEND || http->state == HTTP_STATE_PROPPATCH_SEND) |
1941 | 0 | http->state = HTTP_STATE_WAITING; |
1942 | 0 | else |
1943 | 0 | http->state = HTTP_STATE_STATUS; |
1944 | |
|
1945 | 0 | DEBUG_printf("1httpRead: End of content, set state to %s.", httpStateString(http->state)); |
1946 | 0 | } |
1947 | |
|
1948 | 0 | return (bytes); |
1949 | 0 | } |
1950 | | |
1951 | | |
1952 | | // |
1953 | | // 'httpReadRequest()' - Read a HTTP request from a connection. |
1954 | | // |
1955 | | |
1956 | | http_state_t // O - New state of connection |
1957 | | httpReadRequest(http_t *http, // I - HTTP connection |
1958 | | char *uri, // I - URI buffer |
1959 | | size_t urilen) // I - Size of URI buffer |
1960 | 0 | { |
1961 | 0 | char line[4096], // HTTP request line |
1962 | 0 | *req_method, // HTTP request method |
1963 | 0 | *req_uri, // HTTP request URI |
1964 | 0 | *req_version; // HTTP request version number string |
1965 | | |
1966 | | |
1967 | | // Range check input... |
1968 | 0 | DEBUG_printf("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT ")", (void *)http, (void *)uri, CUPS_LLCAST urilen); |
1969 | |
|
1970 | 0 | if (uri) |
1971 | 0 | *uri = '\0'; |
1972 | |
|
1973 | 0 | if (!http || !uri || urilen < 1) |
1974 | 0 | { |
1975 | 0 | DEBUG_puts("1httpReadRequest: Bad arguments, returning HTTP_STATE_ERROR."); |
1976 | 0 | return (HTTP_STATE_ERROR); |
1977 | 0 | } |
1978 | 0 | else if (http->state != HTTP_STATE_WAITING) |
1979 | 0 | { |
1980 | 0 | DEBUG_printf("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.", httpStateString(http->state)); |
1981 | 0 | return (HTTP_STATE_ERROR); |
1982 | 0 | } |
1983 | | |
1984 | | // Reset state... |
1985 | 0 | httpClearFields(http); |
1986 | |
|
1987 | 0 | http->activity = time(NULL); |
1988 | 0 | http->data_encoding = HTTP_ENCODING_FIELDS; |
1989 | 0 | http->data_remaining = 0; |
1990 | 0 | http->keep_alive = HTTP_KEEPALIVE_OFF; |
1991 | 0 | http->status = HTTP_STATUS_OK; |
1992 | 0 | http->version = HTTP_VERSION_1_1; |
1993 | | |
1994 | | // Read a line from the socket... |
1995 | 0 | if (!httpGets(http, line, sizeof(line))) |
1996 | 0 | { |
1997 | 0 | DEBUG_puts("1httpReadRequest: Unable to read, returning HTTP_STATE_ERROR"); |
1998 | 0 | return (HTTP_STATE_ERROR); |
1999 | 0 | } |
2000 | | |
2001 | 0 | if (!line[0]) |
2002 | 0 | { |
2003 | 0 | DEBUG_puts("1httpReadRequest: Blank line, returning HTTP_STATE_WAITING"); |
2004 | 0 | return (HTTP_STATE_WAITING); |
2005 | 0 | } |
2006 | | |
2007 | 0 | DEBUG_printf("1httpReadRequest: %s", line); |
2008 | | |
2009 | | // Parse it... |
2010 | 0 | req_method = line; |
2011 | 0 | req_uri = line; |
2012 | |
|
2013 | 0 | while (*req_uri && !isspace(*req_uri & 255)) |
2014 | 0 | req_uri ++; |
2015 | |
|
2016 | 0 | if (!*req_uri) |
2017 | 0 | { |
2018 | 0 | DEBUG_puts("1httpReadRequest: No request URI."); |
2019 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request URI."), 1); |
2020 | 0 | return (HTTP_STATE_ERROR); |
2021 | 0 | } |
2022 | | |
2023 | 0 | *req_uri++ = '\0'; |
2024 | |
|
2025 | 0 | while (*req_uri && isspace(*req_uri & 255)) |
2026 | 0 | req_uri ++; |
2027 | |
|
2028 | 0 | req_version = req_uri; |
2029 | |
|
2030 | 0 | while (*req_version && !isspace(*req_version & 255)) |
2031 | 0 | req_version ++; |
2032 | |
|
2033 | 0 | if (!*req_version) |
2034 | 0 | { |
2035 | 0 | DEBUG_puts("1httpReadRequest: No request protocol version."); |
2036 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request protocol version."), 1); |
2037 | 0 | return (HTTP_STATE_ERROR); |
2038 | 0 | } |
2039 | | |
2040 | 0 | *req_version++ = '\0'; |
2041 | |
|
2042 | 0 | while (*req_version && isspace(*req_version & 255)) |
2043 | 0 | req_version ++; |
2044 | | |
2045 | | // Validate... |
2046 | 0 | if (!strcmp(req_method, "CONNECT")) |
2047 | 0 | { |
2048 | 0 | http->state = HTTP_STATE_CONNECT; |
2049 | 0 | } |
2050 | 0 | else if (!strcmp(req_method, "COPY")) |
2051 | 0 | { |
2052 | 0 | http->state = HTTP_STATE_COPY; |
2053 | 0 | } |
2054 | 0 | else if (!strcmp(req_method, "DELETE")) |
2055 | 0 | { |
2056 | 0 | http->state = HTTP_STATE_DELETE; |
2057 | 0 | } |
2058 | 0 | else if (!strcmp(req_method, "GET")) |
2059 | 0 | { |
2060 | 0 | http->state = HTTP_STATE_GET; |
2061 | 0 | } |
2062 | 0 | else if (!strcmp(req_method, "HEAD")) |
2063 | 0 | { |
2064 | 0 | http->state = HTTP_STATE_HEAD; |
2065 | 0 | } |
2066 | 0 | else if (!strcmp(req_method, "LOCK")) |
2067 | 0 | { |
2068 | 0 | http->state = HTTP_STATE_LOCK; |
2069 | 0 | } |
2070 | 0 | else if (!strcmp(req_method, "MKCOL")) |
2071 | 0 | { |
2072 | 0 | http->state = HTTP_STATE_MKCOL; |
2073 | 0 | } |
2074 | 0 | else if (!strcmp(req_method, "MOVE")) |
2075 | 0 | { |
2076 | 0 | http->state = HTTP_STATE_MOVE; |
2077 | 0 | } |
2078 | 0 | else if (!strcmp(req_method, "OPTIONS")) |
2079 | 0 | { |
2080 | 0 | http->state = HTTP_STATE_OPTIONS; |
2081 | 0 | } |
2082 | 0 | else if (!strcmp(req_method, "POST")) |
2083 | 0 | { |
2084 | 0 | http->state = HTTP_STATE_POST; |
2085 | 0 | } |
2086 | 0 | else if (!strcmp(req_method, "PROPFIND")) |
2087 | 0 | { |
2088 | 0 | http->state = HTTP_STATE_PROPFIND; |
2089 | 0 | } |
2090 | 0 | else if (!strcmp(req_method, "PROPPATCH")) |
2091 | 0 | { |
2092 | 0 | http->state = HTTP_STATE_PROPPATCH; |
2093 | 0 | } |
2094 | 0 | else if (!strcmp(req_method, "PUT")) |
2095 | 0 | { |
2096 | 0 | http->state = HTTP_STATE_PUT; |
2097 | 0 | } |
2098 | 0 | else if (!strcmp(req_method, "TRACE")) |
2099 | 0 | { |
2100 | 0 | http->state = HTTP_STATE_TRACE; |
2101 | 0 | } |
2102 | 0 | else if (!strcmp(req_method, "UNLOCK")) |
2103 | 0 | { |
2104 | 0 | http->state = HTTP_STATE_UNLOCK; |
2105 | 0 | } |
2106 | 0 | else |
2107 | 0 | { |
2108 | 0 | DEBUG_printf("1httpReadRequest: Unknown method \"%s\".", req_method); |
2109 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown request method."), 1); |
2110 | 0 | return (HTTP_STATE_UNKNOWN_METHOD); |
2111 | 0 | } |
2112 | | |
2113 | 0 | DEBUG_printf("1httpReadRequest: Set state to %s.", httpStateString(http->state)); |
2114 | |
|
2115 | 0 | if (!strcmp(req_version, "HTTP/1.0")) |
2116 | 0 | { |
2117 | 0 | http->version = HTTP_VERSION_1_0; |
2118 | 0 | http->keep_alive = HTTP_KEEPALIVE_OFF; |
2119 | 0 | } |
2120 | 0 | else if (!strcmp(req_version, "HTTP/1.1")) |
2121 | 0 | { |
2122 | 0 | http->version = HTTP_VERSION_1_1; |
2123 | 0 | http->keep_alive = HTTP_KEEPALIVE_ON; |
2124 | 0 | } |
2125 | 0 | else |
2126 | 0 | { |
2127 | 0 | DEBUG_printf("1httpReadRequest: Unknown version \"%s\".", req_version); |
2128 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown request version."), 1); |
2129 | 0 | return (HTTP_STATE_UNKNOWN_VERSION); |
2130 | 0 | } |
2131 | | |
2132 | 0 | DEBUG_printf("1httpReadRequest: URI is \"%s\".", req_uri); |
2133 | 0 | cupsCopyString(uri, req_uri, urilen); |
2134 | |
|
2135 | 0 | return (http->state); |
2136 | 0 | } |
2137 | | |
2138 | | |
2139 | | // |
2140 | | // 'httpSetAuthString()' - Set the current authorization string. |
2141 | | // |
2142 | | // This function stores a copy of the current authorization string in the HTTP |
2143 | | // connection object. You must still call @link httpSetField@ to set |
2144 | | // `HTTP_FIELD_AUTHORIZATION` prior to issuing a HTTP request using |
2145 | | // @link httpWriteRequest@. |
2146 | | // |
2147 | | |
2148 | | void |
2149 | | httpSetAuthString(http_t *http, // I - HTTP connection |
2150 | | const char *scheme, // I - Auth scheme (`NULL` to clear it) |
2151 | | const char *data) // I - Auth data (`NULL` for none) |
2152 | 0 | { |
2153 | | // Range check input... |
2154 | 0 | DEBUG_printf("httpSetAuthString(http=%p, scheme=\"%s\", data=\"%s\")", (void *)http, scheme, data); |
2155 | |
|
2156 | 0 | if (!http) |
2157 | 0 | return; |
2158 | | |
2159 | 0 | free(http->authstring); |
2160 | |
|
2161 | 0 | if (scheme) |
2162 | 0 | { |
2163 | | // Set the current authorization string... |
2164 | 0 | size_t len = strlen(scheme) + (data ? strlen(data) + 1 : 0) + 1; |
2165 | |
|
2166 | 0 | if ((http->authstring = malloc(len)) != NULL) |
2167 | 0 | { |
2168 | 0 | if (data) |
2169 | 0 | snprintf(http->authstring, len, "%s %s", scheme, data); |
2170 | 0 | else |
2171 | 0 | cupsCopyString(http->authstring, scheme, len); |
2172 | 0 | } |
2173 | 0 | } |
2174 | 0 | else |
2175 | 0 | { |
2176 | | // Clear the current authorization string... |
2177 | 0 | http->authstring = NULL; |
2178 | 0 | } |
2179 | |
|
2180 | 0 | DEBUG_printf("1httpSetAuthString: authstring=\"%s\"", http->authstring); |
2181 | 0 | } |
2182 | | |
2183 | | |
2184 | | // |
2185 | | // 'httpSetBlocking()' - Set blocking/non-blocking behavior on a connection. |
2186 | | // |
2187 | | // This function sets whether a connection uses blocking behavior when reading |
2188 | | // or writing data. The "http" argument specifies the HTTP connection and the |
2189 | | // "blocking" argument specifies whether to use blocking behavior. |
2190 | | // |
2191 | | |
2192 | | void |
2193 | | httpSetBlocking(http_t *http, // I - HTTP connection |
2194 | | bool blocking) // I - `true` = blocking, `false` = non-blocking |
2195 | 0 | { |
2196 | 0 | if (http) |
2197 | 0 | { |
2198 | 0 | http->blocking = blocking; |
2199 | 0 | http_set_wait(http); |
2200 | 0 | } |
2201 | 0 | } |
2202 | | |
2203 | | |
2204 | | // |
2205 | | // 'httpSetCookie()' - Add Set-Cookie value(s). |
2206 | | // |
2207 | | // This function adds one or more Set-Cookie header values that will be sent to |
2208 | | // the client with the @link httpWriteResponse@ function. Each value conforms |
2209 | | // to the format defined in RFC 6265. Multiple values can be passed in the |
2210 | | // "cookie" string separated by a newline character. |
2211 | | // |
2212 | | // Call the @link httpClearCookies@ function to clear all Set-Cookie values. |
2213 | | // |
2214 | | |
2215 | | void |
2216 | | httpSetCookie(http_t *http, // I - Connection |
2217 | | const char *cookie) // I - Cookie string |
2218 | 0 | { |
2219 | | // Range check input... |
2220 | 0 | if (!http || !cookie) |
2221 | 0 | return; |
2222 | | |
2223 | | // Set or append the Set-Cookie value.... |
2224 | 0 | if (http->cookie) |
2225 | 0 | { |
2226 | | // Append with a newline between values... |
2227 | 0 | size_t clen, // Length of cookie string |
2228 | 0 | ctotal; // Total length of cookies |
2229 | 0 | char *temp; // Temporary value |
2230 | |
|
2231 | 0 | clen = strlen(http->cookie); |
2232 | 0 | ctotal = clen + strlen(cookie) + 2; |
2233 | |
|
2234 | 0 | if ((temp = realloc(http->cookie, ctotal)) == NULL) |
2235 | 0 | return; |
2236 | | |
2237 | 0 | http->cookie = temp; |
2238 | 0 | temp[clen] = '\n'; |
2239 | 0 | cupsCopyString(temp + clen + 1, cookie, ctotal - clen - 1); |
2240 | 0 | } |
2241 | 0 | else |
2242 | 0 | { |
2243 | | // Just copy/set this cookie... |
2244 | 0 | http->cookie = strdup(cookie); |
2245 | 0 | } |
2246 | 0 | } |
2247 | | |
2248 | | |
2249 | | // |
2250 | | // 'httpSetDefaultField()' - Set the default value of a HTTP header. |
2251 | | // |
2252 | | // This function sets the default value for a HTTP header. |
2253 | | // |
2254 | | // > *Note:* Currently only the `HTTP_FIELD_ACCEPT_ENCODING`, |
2255 | | // > `HTTP_FIELD_SERVER`, and `HTTP_FIELD_USER_AGENT` fields can be set. |
2256 | | // |
2257 | | |
2258 | | void |
2259 | | httpSetDefaultField(http_t *http, // I - HTTP connection |
2260 | | http_field_t field, // I - Field index |
2261 | | const char *value)// I - Value |
2262 | 0 | { |
2263 | 0 | DEBUG_printf("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, field >= HTTP_FIELD_ACCEPT_LANGUAGE && field < HTTP_FIELD_MAX ? http_fields[field] : "unknown", value); |
2264 | |
|
2265 | 0 | if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) |
2266 | 0 | return; |
2267 | | |
2268 | 0 | free(http->default_fields[field]); |
2269 | |
|
2270 | 0 | http->default_fields[field] = value ? strdup(value) : NULL; |
2271 | 0 | } |
2272 | | |
2273 | | |
2274 | | // |
2275 | | // 'httpSetEncryption()' - Set the required encryption on a HTTP connection. |
2276 | | // |
2277 | | |
2278 | | bool // O - `true` on success, `false` on error |
2279 | | httpSetEncryption( |
2280 | | http_t *http, // I - HTTP connection |
2281 | | http_encryption_t e) // I - New encryption preference |
2282 | 0 | { |
2283 | 0 | DEBUG_printf("httpSetEncryption(http=%p, e=%d)", (void *)http, e); |
2284 | |
|
2285 | 0 | if (!http) |
2286 | 0 | return (false); |
2287 | | |
2288 | 0 | if (http->mode == _HTTP_MODE_CLIENT) |
2289 | 0 | { |
2290 | 0 | http->encryption = e; |
2291 | |
|
2292 | 0 | if ((http->encryption == HTTP_ENCRYPTION_ALWAYS && !http->tls) || (http->encryption == HTTP_ENCRYPTION_NEVER && http->tls)) |
2293 | 0 | return (httpConnectAgain(http, 30000, NULL)); |
2294 | 0 | else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls) |
2295 | 0 | return (http_tls_upgrade(http)); |
2296 | 0 | else |
2297 | 0 | return (true); |
2298 | 0 | } |
2299 | 0 | else |
2300 | 0 | { |
2301 | 0 | if (e == HTTP_ENCRYPTION_NEVER && http->tls) |
2302 | 0 | return (false); |
2303 | | |
2304 | 0 | http->encryption = e; |
2305 | 0 | if (e != HTTP_ENCRYPTION_IF_REQUESTED && !http->tls) |
2306 | 0 | return (_httpTLSStart(http)); |
2307 | 0 | else |
2308 | 0 | return (true); |
2309 | 0 | } |
2310 | 0 | } |
2311 | | |
2312 | | |
2313 | | // |
2314 | | // 'httpSetExpect()' - Set the Expect: header in a request. |
2315 | | // |
2316 | | // This function sets the `Expect:` header in a request. Currently only |
2317 | | // `HTTP_STATUS_NONE` or `HTTP_STATUS_CONTINUE` is supported for the "expect" |
2318 | | // argument. |
2319 | | // |
2320 | | |
2321 | | void |
2322 | | httpSetExpect(http_t *http, // I - HTTP connection |
2323 | | http_status_t expect) // I - HTTP status to expect (`HTTP_STATUS_NONE` or `HTTP_STATUS_CONTINUE`) |
2324 | 0 | { |
2325 | 0 | DEBUG_printf("httpSetExpect(http=%p, expect=%d)", (void *)http, expect); |
2326 | |
|
2327 | 0 | if (http) |
2328 | 0 | http->expect = expect; |
2329 | 0 | } |
2330 | | |
2331 | | |
2332 | | // |
2333 | | // 'httpSetField()' - Set the value of a HTTP header. |
2334 | | // |
2335 | | |
2336 | | void |
2337 | | httpSetField(http_t *http, // I - HTTP connection |
2338 | | http_field_t field, // I - Field index |
2339 | | const char *value) // I - Value |
2340 | 0 | { |
2341 | 0 | DEBUG_printf("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, field >= HTTP_FIELD_ACCEPT_LANGUAGE && field < HTTP_FIELD_MAX ? http_fields[field] : "unknown", value); |
2342 | |
|
2343 | 0 | if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX || !value) |
2344 | 0 | return; |
2345 | | |
2346 | 0 | http_add_field(http, field, value, false); |
2347 | 0 | } |
2348 | | |
2349 | | |
2350 | | // |
2351 | | // 'httpSetKeepAlive()' - Set the current Keep-Alive state of a connection. |
2352 | | // |
2353 | | |
2354 | | void |
2355 | | httpSetKeepAlive( |
2356 | | http_t *http, // I - HTTP connection |
2357 | | http_keepalive_t keep_alive) // I - New Keep-Alive value |
2358 | 0 | { |
2359 | 0 | if (http) |
2360 | 0 | http->keep_alive = keep_alive; |
2361 | 0 | } |
2362 | | |
2363 | | |
2364 | | // |
2365 | | // 'httpSetLength()' - Set the `Content-Length` and `Transfer-Encoding` fields. |
2366 | | // |
2367 | | // This function sets the `Content-Length` and `Transfer-Encoding` fields. |
2368 | | // Passing `0` to the "length" argument specifies a chunked transfer encoding |
2369 | | // while other values specify a fixed `Content-Length`. |
2370 | | // |
2371 | | |
2372 | | void |
2373 | | httpSetLength(http_t *http, // I - HTTP connection |
2374 | | size_t length) // I - Length (`0` for chunked) |
2375 | 0 | { |
2376 | 0 | DEBUG_printf("httpSetLength(http=%p, length=" CUPS_LLFMT ")", (void *)http, CUPS_LLCAST length); |
2377 | |
|
2378 | 0 | if (!http) |
2379 | 0 | return; |
2380 | | |
2381 | 0 | if (!length) |
2382 | 0 | { |
2383 | 0 | httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked"); |
2384 | 0 | httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, ""); |
2385 | 0 | } |
2386 | 0 | else |
2387 | 0 | { |
2388 | 0 | char len[32]; // Length string |
2389 | |
|
2390 | 0 | snprintf(len, sizeof(len), CUPS_LLFMT, CUPS_LLCAST length); |
2391 | 0 | httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, ""); |
2392 | 0 | httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, len); |
2393 | 0 | } |
2394 | 0 | } |
2395 | | |
2396 | | |
2397 | | // |
2398 | | // 'httpSetTimeout()' - Set read/write timeouts and an optional callback. |
2399 | | // |
2400 | | // This function sets the read/write timeouts for a HTTP connection with an |
2401 | | // optional timeout callback. The timeout callback receives both the HTTP |
2402 | | // connection pointer and a user data pointer, and returns `true` to continue or |
2403 | | // `false` to error (time) out. |
2404 | | // |
2405 | | // ``` |
2406 | | // bool // true to continue, false to stop |
2407 | | // timeout_cb(http_t *http, void *user_data) |
2408 | | // { |
2409 | | // ... "http" contains the HTTP connection, "user_data" contains the callback pointer |
2410 | | // } |
2411 | | // ``` |
2412 | | // |
2413 | | |
2414 | | void |
2415 | | httpSetTimeout( |
2416 | | http_t *http, // I - HTTP connection |
2417 | | double timeout, // I - Number of seconds for timeout, must be greater than `0` |
2418 | | http_timeout_cb_t cb, // I - Callback function or `NULL` for none |
2419 | | void *cb_data) // I - Callback data pointer |
2420 | 0 | { |
2421 | 0 | if (!http || timeout <= 0.0) |
2422 | 0 | return; |
2423 | | |
2424 | 0 | http->timeout_cb = cb; |
2425 | 0 | http->timeout_data = cb_data; |
2426 | 0 | http->timeout_value = timeout; |
2427 | |
|
2428 | 0 | if (http->fd >= 0) |
2429 | 0 | http_set_timeout(http->fd, timeout); |
2430 | |
|
2431 | 0 | http_set_wait(http); |
2432 | 0 | } |
2433 | | |
2434 | | |
2435 | | // |
2436 | | // 'httpShutdown()' - Shutdown one side of a HTTP connection. |
2437 | | // |
2438 | | |
2439 | | void |
2440 | | httpShutdown(http_t *http) // I - HTTP connection |
2441 | 0 | { |
2442 | 0 | if (!http || http->fd < 0) |
2443 | 0 | return; |
2444 | | |
2445 | 0 | if (http->tls) |
2446 | 0 | _httpTLSStop(http); |
2447 | |
|
2448 | | #ifdef _WIN32 |
2449 | | shutdown(http->fd, SD_RECEIVE); // Microsoft-ism... |
2450 | | #else |
2451 | 0 | shutdown(http->fd, SHUT_RD); |
2452 | 0 | #endif // _WIN32 |
2453 | 0 | } |
2454 | | |
2455 | | |
2456 | | // |
2457 | | // '_httpUpdate()' - Update the current HTTP status for incoming data. |
2458 | | // |
2459 | | // Note: Unlike httpUpdate(), this function does not flush pending write data |
2460 | | // and only retrieves a single status line from the HTTP connection. |
2461 | | // |
2462 | | |
2463 | | bool // O - `true` to continue, `false` to stop |
2464 | | _httpUpdate(http_t *http, // I - HTTP connection |
2465 | | http_status_t *status) // O - Current HTTP status |
2466 | 0 | { |
2467 | 0 | char line[32768], // Line from connection... |
2468 | 0 | *value; // Pointer to value on line |
2469 | 0 | http_field_t field; // Field index |
2470 | 0 | int major, minor; // HTTP version numbers |
2471 | | |
2472 | |
|
2473 | 0 | DEBUG_printf("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpStateString(http->state)); |
2474 | | |
2475 | | // Grab a single line from the connection... |
2476 | 0 | if (!httpGets(http, line, sizeof(line))) |
2477 | 0 | { |
2478 | 0 | *status = HTTP_STATUS_ERROR; |
2479 | 0 | return (false); |
2480 | 0 | } |
2481 | | |
2482 | 0 | DEBUG_printf("2_httpUpdate: Got \"%s\"", line); |
2483 | |
|
2484 | 0 | if (line[0] == '\0') |
2485 | 0 | { |
2486 | | // Blank line means the start of the data section (if any). Return |
2487 | | // the result code, too... |
2488 | | // |
2489 | | // If we get status 100 (HTTP_STATUS_CONTINUE), then we *don't* change |
2490 | | // states. Instead, we just return HTTP_STATUS_CONTINUE to the caller and |
2491 | | // keep on trying. |
2492 | 0 | if (http->status == HTTP_STATUS_CONTINUE) |
2493 | 0 | { |
2494 | 0 | *status = http->status; |
2495 | 0 | return (false); |
2496 | 0 | } |
2497 | | |
2498 | 0 | if (http->status < HTTP_STATUS_BAD_REQUEST) |
2499 | 0 | http->digest_tries = 0; |
2500 | |
|
2501 | 0 | if (http->status == HTTP_STATUS_SWITCHING_PROTOCOLS && !http->tls) |
2502 | 0 | { |
2503 | 0 | if (!_httpTLSStart(http)) |
2504 | 0 | { |
2505 | 0 | httpAddrClose(NULL, http->fd); |
2506 | 0 | http->fd = -1; |
2507 | |
|
2508 | 0 | *status = http->status = HTTP_STATUS_ERROR; |
2509 | 0 | return (false); |
2510 | 0 | } |
2511 | | |
2512 | 0 | *status = HTTP_STATUS_CONTINUE; |
2513 | 0 | return (false); |
2514 | 0 | } |
2515 | | |
2516 | 0 | if (http_set_length(http) < 0) |
2517 | 0 | { |
2518 | 0 | DEBUG_puts("1_httpUpdate: Bad Content-Length."); |
2519 | 0 | http->error = EINVAL; |
2520 | 0 | http->status = *status = HTTP_STATUS_ERROR; |
2521 | 0 | return (false); |
2522 | 0 | } |
2523 | | |
2524 | 0 | switch (http->state) |
2525 | 0 | { |
2526 | 0 | case HTTP_STATE_COPY : |
2527 | 0 | case HTTP_STATE_DELETE : |
2528 | 0 | case HTTP_STATE_GET : |
2529 | 0 | case HTTP_STATE_LOCK : |
2530 | 0 | case HTTP_STATE_LOCK_RECV : |
2531 | 0 | case HTTP_STATE_POST : |
2532 | 0 | case HTTP_STATE_POST_RECV : |
2533 | 0 | case HTTP_STATE_PROPFIND : |
2534 | 0 | case HTTP_STATE_PROPFIND_RECV : |
2535 | 0 | case HTTP_STATE_PROPPATCH : |
2536 | 0 | case HTTP_STATE_PROPPATCH_RECV : |
2537 | 0 | case HTTP_STATE_PUT : |
2538 | 0 | http->state ++; |
2539 | |
|
2540 | 0 | DEBUG_printf("1_httpUpdate: Set state to %s.", httpStateString(http->state)); |
2541 | |
|
2542 | 0 | case HTTP_STATE_CONNECT : |
2543 | 0 | case HTTP_STATE_HEAD : |
2544 | 0 | case HTTP_STATE_LOCK_SEND : |
2545 | 0 | case HTTP_STATE_MKCOL : |
2546 | 0 | case HTTP_STATE_POST_SEND : |
2547 | 0 | case HTTP_STATE_PROPFIND_SEND : |
2548 | 0 | case HTTP_STATE_PROPPATCH_SEND : |
2549 | 0 | case HTTP_STATE_TRACE : |
2550 | 0 | case HTTP_STATE_UNLOCK : |
2551 | 0 | break; |
2552 | | |
2553 | 0 | default : |
2554 | 0 | http->state = HTTP_STATE_WAITING; |
2555 | |
|
2556 | 0 | DEBUG_puts("1_httpUpdate: Reset state to HTTP_STATE_WAITING."); |
2557 | 0 | break; |
2558 | 0 | } |
2559 | | |
2560 | 0 | DEBUG_puts("1_httpUpdate: Calling http_content_coding_start."); |
2561 | 0 | http_content_coding_start(http, |
2562 | 0 | httpGetField(http, HTTP_FIELD_CONTENT_ENCODING)); |
2563 | |
|
2564 | 0 | *status = http->status; |
2565 | 0 | return (false); |
2566 | 0 | } |
2567 | 0 | else if (!strncmp(line, "HTTP/", 5) && http->mode == _HTTP_MODE_CLIENT) |
2568 | 0 | { |
2569 | | // Got the beginning of a response... |
2570 | 0 | int intstatus; // Status value as an integer |
2571 | |
|
2572 | 0 | if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &intstatus) != 3) |
2573 | 0 | { |
2574 | 0 | *status = http->status = HTTP_STATUS_ERROR; |
2575 | 0 | return (false); |
2576 | 0 | } |
2577 | | |
2578 | 0 | httpClearFields(http); |
2579 | |
|
2580 | 0 | http->version = (http_version_t)(major * 100 + minor); |
2581 | 0 | *status = http->status = (http_status_t)intstatus; |
2582 | 0 | } |
2583 | 0 | else if ((value = strchr(line, ':')) != NULL) |
2584 | 0 | { |
2585 | | // Got a value... |
2586 | 0 | *value++ = '\0'; |
2587 | 0 | while (_cups_isspace(*value)) |
2588 | 0 | value ++; |
2589 | |
|
2590 | 0 | DEBUG_printf("1_httpUpdate: Header %s: %s", line, value); |
2591 | | |
2592 | | // Be tolerants of servers that send unknown attribute fields... |
2593 | 0 | if (!_cups_strcasecmp(line, "expect")) |
2594 | 0 | { |
2595 | | // "Expect: 100-continue" or similar... |
2596 | 0 | http->expect = (http_status_t)atoi(value); |
2597 | 0 | } |
2598 | 0 | else if (!_cups_strcasecmp(line, "cookie")) |
2599 | 0 | { |
2600 | | // "Cookie: name=value[; name=value ...]" - replaces previous cookies... |
2601 | 0 | httpSetCookie(http, value); |
2602 | 0 | } |
2603 | 0 | else if ((field = httpFieldValue(line)) != HTTP_FIELD_UNKNOWN) |
2604 | 0 | { |
2605 | 0 | http_add_field(http, field, value, true); |
2606 | |
|
2607 | 0 | if (field == HTTP_FIELD_AUTHENTICATION_INFO) |
2608 | 0 | httpGetSubField(http, HTTP_FIELD_AUTHENTICATION_INFO, "nextnonce", http->nextnonce, (int)sizeof(http->nextnonce)); |
2609 | 0 | } |
2610 | | #ifdef DEBUG |
2611 | | else |
2612 | | { |
2613 | | DEBUG_printf("1_httpUpdate: unknown field %s seen!", line); |
2614 | | } |
2615 | | #endif // DEBUG |
2616 | 0 | } |
2617 | 0 | else |
2618 | 0 | { |
2619 | 0 | DEBUG_printf("1_httpUpdate: Bad response line \"%s\"!", line); |
2620 | 0 | http->error = EINVAL; |
2621 | 0 | http->status = *status = HTTP_STATUS_ERROR; |
2622 | 0 | return (false); |
2623 | 0 | } |
2624 | | |
2625 | 0 | return (true); |
2626 | 0 | } |
2627 | | |
2628 | | |
2629 | | // |
2630 | | // 'httpUpdate()' - Update the current HTTP state for incoming data. |
2631 | | // |
2632 | | |
2633 | | http_status_t // O - HTTP status |
2634 | | httpUpdate(http_t *http) // I - HTTP connection |
2635 | 0 | { |
2636 | 0 | http_status_t status; // Request status |
2637 | | |
2638 | |
|
2639 | 0 | DEBUG_printf("httpUpdate(http=%p), state=%s", (void *)http, httpStateString(http->state)); |
2640 | | |
2641 | | // Flush pending data, if any... |
2642 | 0 | if (http->wused) |
2643 | 0 | { |
2644 | 0 | DEBUG_puts("2httpUpdate: flushing buffer..."); |
2645 | |
|
2646 | 0 | if (httpFlushWrite(http) < 0) |
2647 | 0 | return (HTTP_STATUS_ERROR); |
2648 | 0 | } |
2649 | | |
2650 | | // If we haven't issued any commands, then there is nothing to "update"... |
2651 | 0 | if (http->state == HTTP_STATE_WAITING) |
2652 | 0 | return (HTTP_STATUS_CONTINUE); |
2653 | | |
2654 | | // Grab all of the lines we can from the connection... |
2655 | 0 | while (_httpUpdate(http, &status)); |
2656 | | |
2657 | | // See if there was an error... |
2658 | 0 | if (http->error == EPIPE && http->status > HTTP_STATUS_CONTINUE) |
2659 | 0 | { |
2660 | 0 | DEBUG_printf("1httpUpdate: Returning status %d...", http->status); |
2661 | 0 | return (http->status); |
2662 | 0 | } |
2663 | | |
2664 | 0 | if (http->error) |
2665 | 0 | { |
2666 | 0 | DEBUG_printf("1httpUpdate: socket error %d - %s", http->error, strerror(http->error)); |
2667 | 0 | http->status = HTTP_STATUS_ERROR; |
2668 | 0 | return (HTTP_STATUS_ERROR); |
2669 | 0 | } |
2670 | | |
2671 | | // Return the current status... |
2672 | 0 | return (status); |
2673 | 0 | } |
2674 | | |
2675 | | |
2676 | | // |
2677 | | // '_httpWait()' - Wait for data available on a connection (no flush). |
2678 | | // |
2679 | | |
2680 | | bool // O - `true` if data is available, `false` otherwise |
2681 | | _httpWait(http_t *http, // I - HTTP connection |
2682 | | int msec, // I - Milliseconds to wait |
2683 | | bool usessl) // I - Use SSL context? |
2684 | 0 | { |
2685 | 0 | struct pollfd pfd; // Polled file descriptor |
2686 | 0 | int nfds; // Result from select()/poll() |
2687 | | |
2688 | |
|
2689 | 0 | DEBUG_printf("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http, msec, usessl); |
2690 | |
|
2691 | 0 | if (http->fd < 0) |
2692 | 0 | { |
2693 | 0 | DEBUG_printf("5_httpWait: Returning 0 since fd=%d", http->fd); |
2694 | 0 | return (false); |
2695 | 0 | } |
2696 | | |
2697 | | // Check the SSL/TLS buffers for data first... |
2698 | 0 | if (http->tls && _httpTLSPending(http)) |
2699 | 0 | { |
2700 | 0 | DEBUG_puts("5_httpWait: Return 1 since there is pending TLS data."); |
2701 | 0 | return (true); |
2702 | 0 | } |
2703 | | |
2704 | | // Then try polling the socket... |
2705 | 0 | pfd.fd = http->fd; |
2706 | 0 | pfd.events = POLLIN; |
2707 | |
|
2708 | 0 | do |
2709 | 0 | { |
2710 | 0 | nfds = poll(&pfd, 1, msec); |
2711 | 0 | } |
2712 | | #ifdef _WIN32 |
2713 | | while (nfds < 0 && (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK)); |
2714 | | #else |
2715 | 0 | while (nfds < 0 && (errno == EINTR || errno == EAGAIN)); |
2716 | 0 | #endif // _WIN32 |
2717 | |
|
2718 | 0 | DEBUG_printf("5_httpWait: returning with nfds=%d, errno=%d...", nfds, errno); |
2719 | |
|
2720 | 0 | return (nfds > 0); |
2721 | 0 | } |
2722 | | |
2723 | | |
2724 | | // |
2725 | | // 'httpWait()' - Wait for data available on a connection. |
2726 | | // |
2727 | | |
2728 | | bool // O - `true` when data is available, `false` otherwise |
2729 | | httpWait(http_t *http, // I - HTTP connection |
2730 | | int msec) // I - Milliseconds to wait |
2731 | 0 | { |
2732 | | // First see if there is data in the buffer... |
2733 | 0 | DEBUG_printf("2httpWait(http=%p, msec=%d)", (void *)http, msec); |
2734 | |
|
2735 | 0 | if (http == NULL) |
2736 | 0 | return (false); |
2737 | | |
2738 | 0 | if (http->used) |
2739 | 0 | { |
2740 | 0 | DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready."); |
2741 | 0 | return (true); |
2742 | 0 | } |
2743 | | |
2744 | 0 | if (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in > 0) |
2745 | 0 | { |
2746 | 0 | DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready."); |
2747 | 0 | return (true); |
2748 | 0 | } |
2749 | | |
2750 | | // Flush pending data, if any... |
2751 | 0 | if (http->wused) |
2752 | 0 | { |
2753 | 0 | DEBUG_puts("3httpWait: Flushing write buffer."); |
2754 | |
|
2755 | 0 | if (httpFlushWrite(http) < 0) |
2756 | 0 | return (false); |
2757 | 0 | } |
2758 | | |
2759 | | // If not, check the SSL/TLS buffers and do a select() on the connection... |
2760 | 0 | return (_httpWait(http, msec, 1)); |
2761 | 0 | } |
2762 | | |
2763 | | |
2764 | | // |
2765 | | // 'httpWrite()' - Write data to a HTTP connection. |
2766 | | // |
2767 | | // This function writes data to a HTTP connection. When using chunking or |
2768 | | // content coding, specifying a "length" of `0` will complete the current HTTP |
2769 | | // request/response message. |
2770 | | // |
2771 | | |
2772 | | ssize_t // O - Number of bytes written |
2773 | | httpWrite(http_t *http, // I - HTTP connection |
2774 | | const char *buffer, // I - Buffer for data |
2775 | | size_t length) // I - Number of bytes to write |
2776 | 0 | { |
2777 | 0 | ssize_t bytes; // Bytes written |
2778 | | |
2779 | |
|
2780 | 0 | DEBUG_printf("httpWrite(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length); |
2781 | | |
2782 | | // Range check input... |
2783 | 0 | if (!http || !buffer) |
2784 | 0 | { |
2785 | 0 | DEBUG_puts("1httpWrite: Returning -1 due to bad input."); |
2786 | 0 | return (-1); |
2787 | 0 | } |
2788 | | |
2789 | | // Mark activity on the connection... |
2790 | 0 | http->activity = time(NULL); |
2791 | | |
2792 | | // Buffer small writes for better performance... |
2793 | 0 | if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE) |
2794 | 0 | { |
2795 | 0 | DEBUG_printf("1httpWrite: http->coding=%d", http->coding); |
2796 | |
|
2797 | 0 | if (length == 0) |
2798 | 0 | { |
2799 | 0 | http_content_coding_finish(http); |
2800 | 0 | bytes = 0; |
2801 | 0 | } |
2802 | 0 | else |
2803 | 0 | { |
2804 | 0 | size_t slen; // Bytes to write |
2805 | 0 | ssize_t sret; // Bytes written |
2806 | |
|
2807 | 0 | ((z_stream *)http->stream)->next_in = (Bytef *)buffer; |
2808 | 0 | ((z_stream *)http->stream)->avail_in = (uInt)length; |
2809 | |
|
2810 | 0 | while (deflate((z_stream *)http->stream, Z_NO_FLUSH) == Z_OK) |
2811 | 0 | { |
2812 | 0 | DEBUG_printf("1httpWrite: avail_out=%d", ((z_stream *)http->stream)->avail_out); |
2813 | |
|
2814 | 0 | if (((z_stream *)http->stream)->avail_out > 0) |
2815 | 0 | continue; |
2816 | | |
2817 | 0 | slen = _HTTP_MAX_SBUFFER - ((z_stream *)http->stream)->avail_out; |
2818 | |
|
2819 | 0 | DEBUG_printf("1httpWrite: Writing intermediate chunk, len=%d", (int)slen); |
2820 | |
|
2821 | 0 | if (slen > 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) |
2822 | 0 | sret = http_write_chunk(http, (char *)http->sbuffer, slen); |
2823 | 0 | else if (slen > 0) |
2824 | 0 | sret = http_write(http, (char *)http->sbuffer, slen); |
2825 | 0 | else |
2826 | 0 | sret = 0; |
2827 | |
|
2828 | 0 | if (sret < 0) |
2829 | 0 | { |
2830 | 0 | DEBUG_puts("1httpWrite: Unable to write, returning -1."); |
2831 | 0 | return (-1); |
2832 | 0 | } |
2833 | | |
2834 | 0 | ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; |
2835 | 0 | ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; |
2836 | 0 | } |
2837 | | |
2838 | 0 | bytes = (ssize_t)length; |
2839 | 0 | } |
2840 | 0 | } |
2841 | 0 | else if (length > 0) |
2842 | 0 | { |
2843 | 0 | if (http->wused && (length + (size_t)http->wused) > sizeof(http->wbuffer)) |
2844 | 0 | { |
2845 | 0 | DEBUG_printf("2httpWrite: Flushing buffer (wused=%d, length=" CUPS_LLFMT ")", http->wused, CUPS_LLCAST length); |
2846 | |
|
2847 | 0 | httpFlushWrite(http); |
2848 | 0 | } |
2849 | |
|
2850 | 0 | if ((length + (size_t)http->wused) <= sizeof(http->wbuffer) && length < sizeof(http->wbuffer)) |
2851 | 0 | { |
2852 | | // Write to buffer... |
2853 | 0 | DEBUG_printf("2httpWrite: Copying " CUPS_LLFMT " bytes to wbuffer...", CUPS_LLCAST length); |
2854 | |
|
2855 | 0 | memcpy(http->wbuffer + http->wused, buffer, length); |
2856 | 0 | http->wused += (int)length; |
2857 | 0 | bytes = (ssize_t)length; |
2858 | 0 | } |
2859 | 0 | else |
2860 | 0 | { |
2861 | | // Otherwise write the data directly... |
2862 | 0 | DEBUG_printf("2httpWrite: Writing " CUPS_LLFMT " bytes to socket...", CUPS_LLCAST length); |
2863 | |
|
2864 | 0 | if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
2865 | 0 | bytes = (ssize_t)http_write_chunk(http, buffer, length); |
2866 | 0 | else |
2867 | 0 | bytes = (ssize_t)http_write(http, buffer, length); |
2868 | |
|
2869 | 0 | DEBUG_printf("2httpWrite: Wrote " CUPS_LLFMT " bytes...", CUPS_LLCAST bytes); |
2870 | 0 | } |
2871 | |
|
2872 | 0 | if (http->data_encoding == HTTP_ENCODING_LENGTH) |
2873 | 0 | http->data_remaining -= bytes; |
2874 | 0 | } |
2875 | 0 | else |
2876 | 0 | { |
2877 | 0 | bytes = 0; |
2878 | 0 | } |
2879 | | |
2880 | | // Handle end-of-request processing... |
2881 | 0 | if ((http->data_encoding == HTTP_ENCODING_CHUNKED && length == 0) || |
2882 | 0 | (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0)) |
2883 | 0 | { |
2884 | | // Finished with the transfer; unless we are sending POST or PUT data, go idle... |
2885 | 0 | if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE) |
2886 | 0 | http_content_coding_finish(http); |
2887 | |
|
2888 | 0 | if (http->wused) |
2889 | 0 | { |
2890 | 0 | if (httpFlushWrite(http) < 0) |
2891 | 0 | return (-1); |
2892 | 0 | } |
2893 | | |
2894 | 0 | if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
2895 | 0 | { |
2896 | | // Send a 0-length chunk at the end of the request... |
2897 | 0 | http_write(http, "0\r\n\r\n", 5); |
2898 | | |
2899 | | // Reset the data state... |
2900 | 0 | http->data_encoding = HTTP_ENCODING_FIELDS; |
2901 | 0 | http->data_remaining = 0; |
2902 | 0 | } |
2903 | |
|
2904 | 0 | if (http->state == HTTP_STATE_LOCK_RECV || http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_PROPFIND_RECV || http->state == HTTP_STATE_PROPPATCH_RECV) |
2905 | 0 | http->state ++; |
2906 | 0 | else if (http->state == HTTP_STATE_COPY_SEND || http->state == HTTP_STATE_DELETE_SEND || http->state == HTTP_STATE_GET_SEND || http->state == HTTP_STATE_LOCK_SEND || http->state == HTTP_STATE_MOVE_SEND || http->state == HTTP_STATE_POST_SEND || http->state == HTTP_STATE_PROPFIND_SEND || http->state == HTTP_STATE_PROPPATCH_SEND) |
2907 | 0 | http->state = HTTP_STATE_WAITING; |
2908 | 0 | else |
2909 | 0 | http->state = HTTP_STATE_STATUS; |
2910 | |
|
2911 | 0 | DEBUG_printf("2httpWrite: Changed state to %s.", httpStateString(http->state)); |
2912 | 0 | } |
2913 | | |
2914 | 0 | DEBUG_printf("1httpWrite: Returning " CUPS_LLFMT ".", CUPS_LLCAST bytes); |
2915 | |
|
2916 | 0 | return (bytes); |
2917 | 0 | } |
2918 | | |
2919 | | |
2920 | | // |
2921 | | // 'httpWriteRequest()' - Write a HTTP request. |
2922 | | // |
2923 | | // This function writes a HTTP request to the specified connection. The |
2924 | | // "method" parameter specifies the HTTP method ("GET", "POST", "PUT", etc) |
2925 | | // while the "uri" parameter specifies the request URI. All fields must be |
2926 | | // set using the @link httpSetCookie@, @link httpSetField@, and |
2927 | | // @link httpSetLength@ functions prior to writing the request. |
2928 | | // |
2929 | | |
2930 | | bool // O - `true` on success, `false` on failure |
2931 | | httpWriteRequest(http_t *http, // I - HTTP connection |
2932 | | const char *method, // I - Request method ("GET", "POST", "PUT", etc.) |
2933 | | const char *uri) // I - Request URI |
2934 | 0 | { |
2935 | 0 | DEBUG_printf("httpWriteRequest(http=%p, method=\"%s\", uri=\"%s\")", (void *)http, method, uri); |
2936 | |
|
2937 | 0 | if (!strcasecmp(method, "COPY")) |
2938 | 0 | return (http_send(http, HTTP_STATE_COPY, uri)); |
2939 | 0 | else if (!strcasecmp(method, "DELETE")) |
2940 | 0 | return (http_send(http, HTTP_STATE_DELETE, uri)); |
2941 | 0 | else if (!strcasecmp(method, "GET")) |
2942 | 0 | return (http_send(http, HTTP_STATE_GET, uri)); |
2943 | 0 | else if (!strcasecmp(method, "HEAD")) |
2944 | 0 | return (http_send(http, HTTP_STATE_HEAD, uri)); |
2945 | 0 | else if (!strcasecmp(method, "LOCK")) |
2946 | 0 | return (http_send(http, HTTP_STATE_LOCK, uri)); |
2947 | 0 | else if (!strcasecmp(method, "MKCOL")) |
2948 | 0 | return (http_send(http, HTTP_STATE_MKCOL, uri)); |
2949 | 0 | else if (!strcasecmp(method, "MOVE")) |
2950 | 0 | return (http_send(http, HTTP_STATE_MOVE, uri)); |
2951 | 0 | else if (!strcasecmp(method, "OPTIONS")) |
2952 | 0 | return (http_send(http, HTTP_STATE_OPTIONS, uri)); |
2953 | 0 | else if (!strcasecmp(method, "POST")) |
2954 | 0 | return (http_send(http, HTTP_STATE_POST, uri)); |
2955 | 0 | else if (!strcasecmp(method, "PROPFIND")) |
2956 | 0 | return (http_send(http, HTTP_STATE_PROPFIND, uri)); |
2957 | 0 | else if (!strcasecmp(method, "PROPPATCH")) |
2958 | 0 | return (http_send(http, HTTP_STATE_PROPPATCH, uri)); |
2959 | 0 | else if (!strcasecmp(method, "PUT")) |
2960 | 0 | return (http_send(http, HTTP_STATE_PUT, uri)); |
2961 | 0 | else if (!strcasecmp(method, "TRACE")) |
2962 | 0 | return (http_send(http, HTTP_STATE_TRACE, uri)); |
2963 | 0 | else if (!strcasecmp(method, "UNLOCK")) |
2964 | 0 | return (http_send(http, HTTP_STATE_UNLOCK, uri)); |
2965 | | |
2966 | 0 | DEBUG_printf("httpWriteRequest: Unknown method, returning false."); |
2967 | |
|
2968 | 0 | return (false); |
2969 | 0 | } |
2970 | | |
2971 | | |
2972 | | // |
2973 | | // 'httpWriteResponse()' - Write a HTTP response to a client connection. |
2974 | | // |
2975 | | |
2976 | | bool // O - `true` on success, `false` on error |
2977 | | httpWriteResponse(http_t *http, // I - HTTP connection |
2978 | | http_status_t status) // I - Status code |
2979 | 0 | { |
2980 | 0 | http_encoding_t old_encoding; // Old data_encoding value |
2981 | 0 | off_t old_remaining; // Old data_remaining value |
2982 | 0 | cups_lang_t *lang; // Response language |
2983 | | |
2984 | | |
2985 | | // Range check input... |
2986 | 0 | DEBUG_printf("httpWriteResponse(http=%p, status=%d)", (void *)http, status); |
2987 | |
|
2988 | 0 | if (!http || status < HTTP_STATUS_CONTINUE) |
2989 | 0 | { |
2990 | 0 | DEBUG_puts("1httpWriteResponse: Bad input."); |
2991 | 0 | return (false); |
2992 | 0 | } |
2993 | | |
2994 | | // Set the various standard fields if they aren't already... |
2995 | 0 | if (!http->fields[HTTP_FIELD_DATE]) |
2996 | 0 | { |
2997 | 0 | char buffer[256]; // Date value |
2998 | |
|
2999 | 0 | httpSetField(http, HTTP_FIELD_DATE, httpGetDateString(time(NULL), buffer, sizeof(buffer))); |
3000 | 0 | } |
3001 | |
|
3002 | 0 | if (status >= HTTP_STATUS_BAD_REQUEST && http->keep_alive) |
3003 | 0 | { |
3004 | 0 | http->keep_alive = HTTP_KEEPALIVE_OFF; |
3005 | 0 | httpSetField(http, HTTP_FIELD_KEEP_ALIVE, ""); |
3006 | 0 | } |
3007 | |
|
3008 | 0 | if (http->version == HTTP_VERSION_1_1) |
3009 | 0 | { |
3010 | 0 | if (!http->fields[HTTP_FIELD_CONNECTION]) |
3011 | 0 | { |
3012 | 0 | if (http->keep_alive) |
3013 | 0 | httpSetField(http, HTTP_FIELD_CONNECTION, "Keep-Alive"); |
3014 | 0 | else |
3015 | 0 | httpSetField(http, HTTP_FIELD_CONNECTION, "close"); |
3016 | 0 | } |
3017 | |
|
3018 | 0 | if (http->keep_alive && !http->fields[HTTP_FIELD_KEEP_ALIVE]) |
3019 | 0 | httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "timeout=10"); |
3020 | 0 | } |
3021 | |
|
3022 | 0 | if (status == HTTP_STATUS_UPGRADE_REQUIRED || status == HTTP_STATUS_SWITCHING_PROTOCOLS) |
3023 | 0 | { |
3024 | 0 | if (!http->fields[HTTP_FIELD_CONNECTION]) |
3025 | 0 | httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); |
3026 | |
|
3027 | 0 | if (!http->fields[HTTP_FIELD_UPGRADE]) |
3028 | 0 | httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.3,TLS/1.2"); |
3029 | |
|
3030 | 0 | if (!http->fields[HTTP_FIELD_CONTENT_LENGTH]) |
3031 | 0 | httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "0"); |
3032 | 0 | } |
3033 | |
|
3034 | 0 | if (!http->fields[HTTP_FIELD_SERVER]) |
3035 | 0 | httpSetField(http, HTTP_FIELD_SERVER, http->default_fields[HTTP_FIELD_SERVER] ? http->default_fields[HTTP_FIELD_SERVER] : "CUPS/" LIBCUPS_VERSION " IPP/2.0"); |
3036 | | |
3037 | | // Set the Accept-Encoding field if it isn't already... |
3038 | 0 | if (!http->fields[HTTP_FIELD_ACCEPT_ENCODING]) |
3039 | 0 | httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, http->default_fields[HTTP_FIELD_ACCEPT_ENCODING] ? http->default_fields[HTTP_FIELD_ACCEPT_ENCODING] : "gzip, deflate, identity"); |
3040 | | |
3041 | | // Get the response language, if any... |
3042 | 0 | lang = cupsLangFind(http->fields[HTTP_FIELD_CONTENT_LANGUAGE]); |
3043 | | |
3044 | | // Send the response header... |
3045 | 0 | old_encoding = http->data_encoding; |
3046 | 0 | old_remaining = http->data_remaining; |
3047 | 0 | http->data_encoding = HTTP_ENCODING_FIELDS; |
3048 | |
|
3049 | 0 | if (httpPrintf(http, "HTTP/%d.%d %d %s\r\n", http->version / 100, http->version % 100, (int)status, _httpStatusString(lang, status)) < 0) |
3050 | 0 | { |
3051 | 0 | http->status = HTTP_STATUS_ERROR; |
3052 | 0 | return (false); |
3053 | 0 | } |
3054 | | |
3055 | 0 | if (status != HTTP_STATUS_CONTINUE) |
3056 | 0 | { |
3057 | | // 100 Continue doesn't have the rest of the response headers... |
3058 | 0 | int i; // Looping var |
3059 | 0 | const char *value; // Field value |
3060 | |
|
3061 | 0 | for (i = 0; i < HTTP_FIELD_MAX; i ++) |
3062 | 0 | { |
3063 | 0 | if ((value = httpGetField(http, i)) != NULL && *value) |
3064 | 0 | { |
3065 | 0 | if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1) |
3066 | 0 | { |
3067 | 0 | http->status = HTTP_STATUS_ERROR; |
3068 | 0 | return (false); |
3069 | 0 | } |
3070 | 0 | } |
3071 | 0 | } |
3072 | | |
3073 | 0 | if (http->cookie) |
3074 | 0 | { |
3075 | 0 | char *start, // Start of cookie |
3076 | 0 | *ptr; // Pointer into cookie |
3077 | |
|
3078 | 0 | for (start = http->cookie; start; start = ptr) |
3079 | 0 | { |
3080 | 0 | if ((ptr = strchr(start, '\n')) != NULL) |
3081 | 0 | *ptr = '\0'; |
3082 | |
|
3083 | 0 | if (strchr(start, ';')) |
3084 | 0 | { |
3085 | 0 | if (httpPrintf(http, "Set-Cookie: %s\r\n", start) < 1) |
3086 | 0 | { |
3087 | 0 | http->status = HTTP_STATUS_ERROR; |
3088 | 0 | if (ptr) |
3089 | 0 | *ptr = '\n'; |
3090 | 0 | return (false); |
3091 | 0 | } |
3092 | 0 | } |
3093 | 0 | else if (httpPrintf(http, "Set-Cookie: %s; path=/; httponly;%s\r\n", start, http->tls ? " secure;" : "") < 1) |
3094 | 0 | { |
3095 | 0 | http->status = HTTP_STATUS_ERROR; |
3096 | 0 | if (ptr) |
3097 | 0 | *ptr = '\n'; |
3098 | 0 | return (false); |
3099 | 0 | } |
3100 | | |
3101 | 0 | if (ptr) |
3102 | 0 | *ptr++ = '\n'; |
3103 | 0 | } |
3104 | 0 | } |
3105 | 0 | } |
3106 | | |
3107 | 0 | if (httpWrite(http, "\r\n", 2) < 2) |
3108 | 0 | { |
3109 | 0 | http->status = HTTP_STATUS_ERROR; |
3110 | 0 | return (false); |
3111 | 0 | } |
3112 | | |
3113 | 0 | if (httpFlushWrite(http) < 0) |
3114 | 0 | { |
3115 | 0 | http->status = HTTP_STATUS_ERROR; |
3116 | 0 | return (false); |
3117 | 0 | } |
3118 | | |
3119 | 0 | if (status == HTTP_STATUS_CONTINUE || status == HTTP_STATUS_SWITCHING_PROTOCOLS) |
3120 | 0 | { |
3121 | | // Restore the old data_encoding and data_length values... |
3122 | 0 | http->data_encoding = old_encoding; |
3123 | 0 | http->data_remaining = old_remaining; |
3124 | 0 | } |
3125 | 0 | else if (http->state == HTTP_STATE_OPTIONS || http->state == HTTP_STATE_HEAD || http->state == HTTP_STATE_PUT || http->state == HTTP_STATE_TRACE || http->state == HTTP_STATE_CONNECT || http->state == HTTP_STATE_STATUS) |
3126 | 0 | { |
3127 | 0 | DEBUG_printf("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, was %s.", httpStateString(http->state)); |
3128 | 0 | http->state = HTTP_STATE_WAITING; |
3129 | 0 | } |
3130 | 0 | else |
3131 | 0 | { |
3132 | | // Force data_encoding and data_length to be set according to the response |
3133 | | // headers... |
3134 | 0 | http_set_length(http); |
3135 | |
|
3136 | 0 | if (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0) |
3137 | 0 | { |
3138 | 0 | DEBUG_printf("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, was %s.", httpStateString(http->state)); |
3139 | 0 | http->state = HTTP_STATE_WAITING; |
3140 | 0 | return (true); |
3141 | 0 | } |
3142 | | |
3143 | 0 | if (http->state == HTTP_STATE_COPY || http->state == HTTP_STATE_DELETE || http->state == HTTP_STATE_GET || http->state == HTTP_STATE_LOCK_RECV || http->state == HTTP_STATE_MOVE || http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_PROPFIND_RECV || http->state == HTTP_STATE_PROPPATCH_RECV) |
3144 | 0 | http->state ++; |
3145 | | |
3146 | | // Then start any content encoding... |
3147 | 0 | DEBUG_puts("1httpWriteResponse: Calling http_content_coding_start."); |
3148 | 0 | http_content_coding_start(http, httpGetField(http, HTTP_FIELD_CONTENT_ENCODING)); |
3149 | 0 | } |
3150 | | |
3151 | 0 | return (true); |
3152 | 0 | } |
3153 | | |
3154 | | |
3155 | | // |
3156 | | // 'http_add_field()' - Add a value for a HTTP field, appending if needed. |
3157 | | // |
3158 | | |
3159 | | static void |
3160 | | http_add_field(http_t *http, // I - HTTP connection |
3161 | | http_field_t field, // I - HTTP field |
3162 | | const char *value, // I - Value string |
3163 | | bool append) // I - Append value? |
3164 | 0 | { |
3165 | 0 | char temp[1024]; // Temporary value string |
3166 | 0 | size_t fieldlen, // Length of existing value |
3167 | 0 | valuelen, // Length of value string |
3168 | 0 | total; // Total length of string |
3169 | | |
3170 | |
|
3171 | 0 | if (field == HTTP_FIELD_HOST) |
3172 | 0 | { |
3173 | | // Special-case for Host: as we don't want a trailing "." on the hostname |
3174 | | // and need to bracket IPv6 numeric addresses. |
3175 | 0 | char *ptr = strchr(value, ':'); |
3176 | |
|
3177 | 0 | if (value[0] != '[' && ptr && strchr(ptr + 1, ':')) |
3178 | 0 | { |
3179 | | // Bracket IPv6 numeric addresses... |
3180 | | // |
3181 | | // This is slightly inefficient (basically copying twice), but is an edge |
3182 | | // case and not worth optimizing... |
3183 | 0 | snprintf(temp, sizeof(temp), "[%s]", value); |
3184 | 0 | value = temp; |
3185 | 0 | } |
3186 | 0 | else if (*value) |
3187 | 0 | { |
3188 | | // Check for a trailing dot on the hostname... |
3189 | 0 | cupsCopyString(temp, value, sizeof(temp)); |
3190 | 0 | value = temp; |
3191 | 0 | ptr = temp + strlen(temp) - 1; |
3192 | |
|
3193 | 0 | if (*ptr == '.') |
3194 | 0 | *ptr = '\0'; |
3195 | 0 | } |
3196 | 0 | } |
3197 | |
|
3198 | 0 | 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) |
3199 | 0 | append = false; |
3200 | |
|
3201 | 0 | if (!append && http->fields[field]) |
3202 | 0 | { |
3203 | 0 | free(http->fields[field]); |
3204 | |
|
3205 | 0 | http->fields[field] = NULL; |
3206 | 0 | } |
3207 | |
|
3208 | 0 | valuelen = strlen(value); |
3209 | |
|
3210 | 0 | if (!valuelen) |
3211 | 0 | return; |
3212 | | |
3213 | 0 | if (http->fields[field]) |
3214 | 0 | { |
3215 | 0 | fieldlen = strlen(http->fields[field]); |
3216 | 0 | total = fieldlen + 2 + valuelen; |
3217 | 0 | } |
3218 | 0 | else |
3219 | 0 | { |
3220 | 0 | fieldlen = 0; |
3221 | 0 | total = valuelen; |
3222 | 0 | } |
3223 | |
|
3224 | 0 | if (fieldlen) |
3225 | 0 | { |
3226 | | // Expand the field value... |
3227 | 0 | char *mcombined; // New value string |
3228 | |
|
3229 | 0 | if ((mcombined = realloc(http->fields[field], total + 1)) != NULL) |
3230 | 0 | { |
3231 | 0 | http->fields[field] = mcombined; |
3232 | 0 | cupsConcatString(mcombined, ", ", total + 1); |
3233 | 0 | cupsConcatString(mcombined, value, total + 1); |
3234 | 0 | } |
3235 | 0 | } |
3236 | 0 | else |
3237 | 0 | { |
3238 | | // Allocate the field value... |
3239 | 0 | http->fields[field] = strdup(value); |
3240 | 0 | } |
3241 | |
|
3242 | 0 | DEBUG_printf("1http_add_field: append=%s, field=%d(%s), value=\"%s\".", append ? "true" : "false", field, http_fields[field], http->fields[field]); |
3243 | |
|
3244 | 0 | if (field == HTTP_FIELD_CONTENT_ENCODING && http->data_encoding != HTTP_ENCODING_FIELDS) |
3245 | 0 | { |
3246 | 0 | DEBUG_puts("1http_add_field: Calling http_content_coding_start."); |
3247 | 0 | http_content_coding_start(http, value); |
3248 | 0 | } |
3249 | 0 | } |
3250 | | |
3251 | | |
3252 | | // |
3253 | | // 'http_content_coding_finish()' - Finish doing any content encoding. |
3254 | | // |
3255 | | |
3256 | | static void |
3257 | | http_content_coding_finish( |
3258 | | http_t *http) // I - HTTP connection |
3259 | 0 | { |
3260 | 0 | int zerr; // Compression status |
3261 | 0 | Byte dummy[1]; // Dummy read buffer |
3262 | 0 | size_t bytes; // Number of bytes to write |
3263 | | |
3264 | |
|
3265 | 0 | DEBUG_printf("http_content_coding_finish(http=%p)", (void *)http); |
3266 | 0 | DEBUG_printf("1http_content_coding_finishing: http->coding=%d", http->coding); |
3267 | |
|
3268 | 0 | switch (http->coding) |
3269 | 0 | { |
3270 | 0 | case _HTTP_CODING_DEFLATE : |
3271 | 0 | case _HTTP_CODING_GZIP : |
3272 | 0 | ((z_stream *)http->stream)->next_in = dummy; |
3273 | 0 | ((z_stream *)http->stream)->avail_in = 0; |
3274 | |
|
3275 | 0 | do |
3276 | 0 | { |
3277 | 0 | zerr = deflate((z_stream *)http->stream, Z_FINISH); |
3278 | 0 | bytes = _HTTP_MAX_SBUFFER - ((z_stream *)http->stream)->avail_out; |
3279 | |
|
3280 | 0 | if (bytes > 0) |
3281 | 0 | { |
3282 | 0 | DEBUG_printf("1http_content_coding_finish: Writing trailing chunk, len=%d", (int)bytes); |
3283 | |
|
3284 | 0 | if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
3285 | 0 | http_write_chunk(http, (char *)http->sbuffer, bytes); |
3286 | 0 | else |
3287 | 0 | http_write(http, (char *)http->sbuffer, bytes); |
3288 | 0 | } |
3289 | |
|
3290 | 0 | ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; |
3291 | 0 | ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; |
3292 | 0 | } |
3293 | 0 | while (zerr == Z_OK); |
3294 | |
|
3295 | 0 | deflateEnd((z_stream *)http->stream); |
3296 | |
|
3297 | 0 | free(http->sbuffer); |
3298 | 0 | free(http->stream); |
3299 | |
|
3300 | 0 | http->sbuffer = NULL; |
3301 | 0 | http->stream = NULL; |
3302 | |
|
3303 | 0 | if (http->wused) |
3304 | 0 | httpFlushWrite(http); |
3305 | 0 | break; |
3306 | | |
3307 | 0 | case _HTTP_CODING_INFLATE : |
3308 | 0 | case _HTTP_CODING_GUNZIP : |
3309 | 0 | inflateEnd((z_stream *)http->stream); |
3310 | |
|
3311 | 0 | free(http->sbuffer); |
3312 | 0 | free(http->stream); |
3313 | |
|
3314 | 0 | http->sbuffer = NULL; |
3315 | 0 | http->stream = NULL; |
3316 | 0 | break; |
3317 | | |
3318 | 0 | default : |
3319 | 0 | break; |
3320 | 0 | } |
3321 | | |
3322 | 0 | http->coding = _HTTP_CODING_IDENTITY; |
3323 | 0 | } |
3324 | | |
3325 | | |
3326 | | // |
3327 | | // 'http_content_coding_start()' - Start doing content encoding. |
3328 | | // |
3329 | | |
3330 | | static void |
3331 | | http_content_coding_start( |
3332 | | http_t *http, // I - HTTP connection |
3333 | | const char *value) // I - Value of Content-Encoding |
3334 | 0 | { |
3335 | 0 | int zerr; // Error/status |
3336 | 0 | _http_coding_t coding; // Content coding value |
3337 | | |
3338 | |
|
3339 | 0 | DEBUG_printf("http_content_coding_start(http=%p, value=\"%s\")", (void *)http, value); |
3340 | |
|
3341 | 0 | if (http->coding != _HTTP_CODING_IDENTITY) |
3342 | 0 | { |
3343 | 0 | DEBUG_printf("1http_content_coding_start: http->coding already %d.", http->coding); |
3344 | 0 | return; |
3345 | 0 | } |
3346 | 0 | else if (!strcmp(value, "x-gzip") || !strcmp(value, "gzip")) |
3347 | 0 | { |
3348 | 0 | if (http->state == HTTP_STATE_GET_SEND || |
3349 | 0 | http->state == HTTP_STATE_POST_SEND) |
3350 | 0 | coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_GZIP : |
3351 | 0 | _HTTP_CODING_GUNZIP; |
3352 | 0 | else if (http->state == HTTP_STATE_POST_RECV || |
3353 | 0 | http->state == HTTP_STATE_PUT_RECV) |
3354 | 0 | coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_GZIP : |
3355 | 0 | _HTTP_CODING_GUNZIP; |
3356 | 0 | else |
3357 | 0 | { |
3358 | 0 | DEBUG_puts("1http_content_coding_start: Not doing content coding."); |
3359 | 0 | return; |
3360 | 0 | } |
3361 | 0 | } |
3362 | 0 | else if (!strcmp(value, "x-deflate") || !strcmp(value, "deflate")) |
3363 | 0 | { |
3364 | 0 | if (http->state == HTTP_STATE_GET_SEND || |
3365 | 0 | http->state == HTTP_STATE_POST_SEND) |
3366 | 0 | coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_DEFLATE : |
3367 | 0 | _HTTP_CODING_INFLATE; |
3368 | 0 | else if (http->state == HTTP_STATE_POST_RECV || |
3369 | 0 | http->state == HTTP_STATE_PUT_RECV) |
3370 | 0 | coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_DEFLATE : |
3371 | 0 | _HTTP_CODING_INFLATE; |
3372 | 0 | else |
3373 | 0 | { |
3374 | 0 | DEBUG_puts("1http_content_coding_start: Not doing content coding."); |
3375 | 0 | return; |
3376 | 0 | } |
3377 | 0 | } |
3378 | 0 | else |
3379 | 0 | { |
3380 | 0 | DEBUG_puts("1http_content_coding_start: Not doing content coding."); |
3381 | 0 | return; |
3382 | 0 | } |
3383 | | |
3384 | 0 | switch (coding) |
3385 | 0 | { |
3386 | 0 | case _HTTP_CODING_DEFLATE : |
3387 | 0 | case _HTTP_CODING_GZIP : |
3388 | 0 | if (http->wused) |
3389 | 0 | httpFlushWrite(http); |
3390 | |
|
3391 | 0 | if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL) |
3392 | 0 | { |
3393 | 0 | http->status = HTTP_STATUS_ERROR; |
3394 | 0 | http->error = errno; |
3395 | 0 | return; |
3396 | 0 | } |
3397 | | |
3398 | | // Window size for compression is 11 bits - optimal based on PWG Raster |
3399 | | // sample files on pwg.org. -11 is raw deflate, 27 is gzip, per ZLIB |
3400 | | // documentation. |
3401 | 0 | if ((http->stream = calloc(1, sizeof(z_stream))) == NULL) |
3402 | 0 | { |
3403 | 0 | free(http->sbuffer); |
3404 | |
|
3405 | 0 | http->sbuffer = NULL; |
3406 | 0 | http->status = HTTP_STATUS_ERROR; |
3407 | 0 | http->error = errno; |
3408 | 0 | return; |
3409 | 0 | } |
3410 | | |
3411 | 0 | if ((zerr = deflateInit2((z_stream *)http->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, coding == _HTTP_CODING_DEFLATE ? -11 : 27, 7, Z_DEFAULT_STRATEGY)) < Z_OK) |
3412 | 0 | { |
3413 | 0 | free(http->sbuffer); |
3414 | 0 | free(http->stream); |
3415 | |
|
3416 | 0 | http->sbuffer = NULL; |
3417 | 0 | http->stream = NULL; |
3418 | 0 | http->status = HTTP_STATUS_ERROR; |
3419 | 0 | http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL; |
3420 | 0 | return; |
3421 | 0 | } |
3422 | | |
3423 | 0 | ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; |
3424 | 0 | ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; |
3425 | 0 | break; |
3426 | | |
3427 | 0 | case _HTTP_CODING_INFLATE : |
3428 | 0 | case _HTTP_CODING_GUNZIP : |
3429 | 0 | if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL) |
3430 | 0 | { |
3431 | 0 | http->status = HTTP_STATUS_ERROR; |
3432 | 0 | http->error = errno; |
3433 | 0 | return; |
3434 | 0 | } |
3435 | | |
3436 | | // Window size for decompression is up to 15 bits (maximum supported). |
3437 | | // -15 is raw inflate, 31 is gunzip, per ZLIB documentation. |
3438 | 0 | if ((http->stream = calloc(1, sizeof(z_stream))) == NULL) |
3439 | 0 | { |
3440 | 0 | free(http->sbuffer); |
3441 | |
|
3442 | 0 | http->sbuffer = NULL; |
3443 | 0 | http->status = HTTP_STATUS_ERROR; |
3444 | 0 | http->error = errno; |
3445 | 0 | return; |
3446 | 0 | } |
3447 | | |
3448 | 0 | if ((zerr = inflateInit2((z_stream *)http->stream, coding == _HTTP_CODING_INFLATE ? -15 : 31)) < Z_OK) |
3449 | 0 | { |
3450 | 0 | free(http->sbuffer); |
3451 | 0 | free(http->stream); |
3452 | |
|
3453 | 0 | http->sbuffer = NULL; |
3454 | 0 | http->stream = NULL; |
3455 | 0 | http->status = HTTP_STATUS_ERROR; |
3456 | 0 | http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL; |
3457 | 0 | return; |
3458 | 0 | } |
3459 | | |
3460 | 0 | ((z_stream *)http->stream)->avail_in = 0; |
3461 | 0 | ((z_stream *)http->stream)->next_in = http->sbuffer; |
3462 | 0 | break; |
3463 | | |
3464 | 0 | default : |
3465 | 0 | break; |
3466 | 0 | } |
3467 | | |
3468 | 0 | http->coding = coding; |
3469 | |
|
3470 | 0 | DEBUG_printf("1http_content_coding_start: http->coding now %d.", http->coding); |
3471 | 0 | } |
3472 | | |
3473 | | |
3474 | | // |
3475 | | // 'http_create()' - Create an unconnected HTTP connection. |
3476 | | // |
3477 | | |
3478 | | static http_t * // O - HTTP connection |
3479 | | http_create( |
3480 | | const char *host, // I - Hostname |
3481 | | int port, // I - Port number |
3482 | | http_addrlist_t *addrlist, // I - Address list or `NULL` |
3483 | | int family, // I - Address family or AF_UNSPEC |
3484 | | http_encryption_t encryption, // I - Encryption to use |
3485 | | bool blocking, // I - `true` for blocking mode |
3486 | | _http_mode_t mode) // I - _HTTP_MODE_CLIENT or _SERVER |
3487 | 0 | { |
3488 | 0 | http_t *http; // New HTTP connection |
3489 | 0 | char service[255]; // Service name |
3490 | 0 | http_addrlist_t *myaddrlist = NULL; // My address list |
3491 | 0 | _cups_globals_t *cg = _cupsGlobals(); // Thread global data |
3492 | | |
3493 | |
|
3494 | 0 | DEBUG_printf("4http_create(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%s, mode=%d)", host, port, (void *)addrlist, family, encryption, blocking ? "true" : "false", mode); |
3495 | |
|
3496 | 0 | if (!host && mode == _HTTP_MODE_CLIENT) |
3497 | 0 | return (NULL); |
3498 | | |
3499 | 0 | httpInitialize(); |
3500 | | |
3501 | | // Lookup the host... |
3502 | 0 | if (addrlist) |
3503 | 0 | { |
3504 | 0 | myaddrlist = httpAddrCopyList(addrlist); |
3505 | 0 | } |
3506 | 0 | else |
3507 | 0 | { |
3508 | 0 | snprintf(service, sizeof(service), "%d", port); |
3509 | |
|
3510 | 0 | myaddrlist = httpAddrGetList(host, family, service); |
3511 | 0 | } |
3512 | |
|
3513 | 0 | if (!myaddrlist) |
3514 | 0 | return (NULL); |
3515 | | |
3516 | | // Allocate memory for the structure... |
3517 | 0 | if ((http = calloc(sizeof(http_t), 1)) == NULL) |
3518 | 0 | { |
3519 | 0 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); |
3520 | 0 | httpAddrFreeList(myaddrlist); |
3521 | 0 | return (NULL); |
3522 | 0 | } |
3523 | | |
3524 | | // Initialize the HTTP data... |
3525 | 0 | http->mode = mode; |
3526 | 0 | http->activity = time(NULL); |
3527 | 0 | http->hostlist = myaddrlist; |
3528 | 0 | http->blocking = blocking; |
3529 | 0 | http->fd = -1; |
3530 | 0 | http->status = HTTP_STATUS_CONTINUE; |
3531 | 0 | http->version = HTTP_VERSION_1_1; |
3532 | |
|
3533 | 0 | if (host) |
3534 | 0 | { |
3535 | 0 | DEBUG_printf("5http_create: host=\"%s\"", host); |
3536 | |
|
3537 | 0 | if (!strncmp(host, "fe80::", 6)) |
3538 | 0 | { |
3539 | | // IPv6 link local address, convert to IPvFuture format... |
3540 | 0 | char *zoneid; // Pointer to zoneid separator |
3541 | |
|
3542 | 0 | snprintf(http->hostname, sizeof(http->hostname), "[v1.%s]", host); |
3543 | 0 | if ((zoneid = strchr(http->hostname, '%')) != NULL) |
3544 | 0 | *zoneid = '+'; |
3545 | 0 | } |
3546 | 0 | else if (isxdigit(host[0]) && isxdigit(host[1]) && isxdigit(host[2]) && isxdigit(host[3]) && host[4] == ':') |
3547 | 0 | { |
3548 | | // IPv6 address, convert to URI format... |
3549 | 0 | snprintf(http->hostname, sizeof(http->hostname), "[%s]", host); |
3550 | 0 | } |
3551 | 0 | else |
3552 | 0 | { |
3553 | | // Not an IPv6 numeric address... |
3554 | 0 | cupsCopyString(http->hostname, host, sizeof(http->hostname)); |
3555 | 0 | } |
3556 | |
|
3557 | 0 | DEBUG_printf("5http_create: http->hostname=\"%s\"", http->hostname); |
3558 | 0 | } |
3559 | |
|
3560 | 0 | if (port == 443) // Always use encryption for https |
3561 | 0 | http->encryption = HTTP_ENCRYPTION_ALWAYS; |
3562 | 0 | else |
3563 | 0 | http->encryption = encryption; |
3564 | |
|
3565 | 0 | http_set_wait(http); |
3566 | | |
3567 | | // Set client credentials... |
3568 | 0 | http->tls_credentials = _httpUseCredentials(cg->credentials); |
3569 | | |
3570 | | // Return the new structure... |
3571 | 0 | return (http); |
3572 | 0 | } |
3573 | | |
3574 | | |
3575 | | #ifdef DEBUG |
3576 | | // |
3577 | | // 'http_debug_hex()' - Do a hex dump of a buffer. |
3578 | | // |
3579 | | |
3580 | | static void |
3581 | | http_debug_hex(const char *prefix, // I - Prefix for line |
3582 | | const char *buffer, // I - Buffer to dump |
3583 | | int bytes) // I - Bytes to dump |
3584 | | { |
3585 | | int i, j, // Looping vars |
3586 | | ch; // Current character |
3587 | | char line[255], // Line buffer |
3588 | | *start, // Start of line after prefix |
3589 | | *ptr; // Pointer into line |
3590 | | |
3591 | | |
3592 | | if (_cups_debug_fd < 0 || _cups_debug_level < 6) |
3593 | | return; |
3594 | | |
3595 | | DEBUG_printf("9%s: %d bytes:", prefix, bytes); |
3596 | | |
3597 | | snprintf(line, sizeof(line), "9%s: ", prefix); |
3598 | | start = line + strlen(line); |
3599 | | |
3600 | | for (i = 0; i < bytes; i += 16) |
3601 | | { |
3602 | | for (j = 0, ptr = start; j < 16 && (i + j) < bytes; j ++, ptr += 2) |
3603 | | snprintf(ptr, 3, "%02X", buffer[i + j] & 255); |
3604 | | |
3605 | | while (j < 16) |
3606 | | { |
3607 | | memcpy(ptr, " ", 3); |
3608 | | ptr += 2; |
3609 | | j ++; |
3610 | | } |
3611 | | |
3612 | | memcpy(ptr, " ", 3); |
3613 | | ptr += 2; |
3614 | | |
3615 | | for (j = 0; j < 16 && (i + j) < bytes; j ++) |
3616 | | { |
3617 | | ch = buffer[i + j] & 255; |
3618 | | |
3619 | | if (ch < ' ' || ch >= 127) |
3620 | | ch = '.'; |
3621 | | |
3622 | | *ptr++ = (char)ch; |
3623 | | } |
3624 | | |
3625 | | *ptr = '\0'; |
3626 | | DEBUG_puts(line); |
3627 | | } |
3628 | | } |
3629 | | #endif // DEBUG |
3630 | | |
3631 | | |
3632 | | // |
3633 | | // 'http_read()' - Read a buffer from a HTTP connection. |
3634 | | // |
3635 | | // This function does the low-level read from the socket, retrying and timing |
3636 | | // out as needed. |
3637 | | // |
3638 | | |
3639 | | static ssize_t // O - Number of bytes read or -1 on error |
3640 | | http_read(http_t *http, // I - HTTP connection |
3641 | | char *buffer, // I - Buffer |
3642 | | size_t length) // I - Maximum bytes to read |
3643 | 0 | { |
3644 | 0 | ssize_t bytes; // Bytes read |
3645 | | |
3646 | |
|
3647 | 0 | DEBUG_printf("7http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length); |
3648 | |
|
3649 | 0 | if (!http->blocking || http->timeout_value > 0.0) |
3650 | 0 | { |
3651 | 0 | while (!httpWait(http, http->wait_value)) |
3652 | 0 | { |
3653 | 0 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
3654 | 0 | continue; |
3655 | | |
3656 | 0 | DEBUG_puts("8http_read: Timeout."); |
3657 | 0 | return (0); |
3658 | 0 | } |
3659 | 0 | } |
3660 | | |
3661 | 0 | DEBUG_printf("8http_read: Reading %d bytes into buffer.", (int)length); |
3662 | |
|
3663 | 0 | do |
3664 | 0 | { |
3665 | 0 | if (http->tls) |
3666 | 0 | bytes = _httpTLSRead(http, buffer, (int)length); |
3667 | 0 | else |
3668 | 0 | bytes = recv(http->fd, buffer, length, 0); |
3669 | |
|
3670 | 0 | if (bytes < 0) |
3671 | 0 | { |
3672 | | #ifdef _WIN32 |
3673 | | if (WSAGetLastError() != WSAEINTR) |
3674 | | { |
3675 | | http->error = WSAGetLastError(); |
3676 | | return (-1); |
3677 | | } |
3678 | | else if (WSAGetLastError() == WSAEWOULDBLOCK) |
3679 | | { |
3680 | | if (!http->timeout_cb || |
3681 | | !(*http->timeout_cb)(http, http->timeout_data)) |
3682 | | { |
3683 | | http->error = WSAEWOULDBLOCK; |
3684 | | return (-1); |
3685 | | } |
3686 | | } |
3687 | | #else |
3688 | 0 | DEBUG_printf("8http_read: %s", strerror(errno)); |
3689 | |
|
3690 | 0 | if (errno == EWOULDBLOCK || errno == EAGAIN) |
3691 | 0 | { |
3692 | 0 | if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) |
3693 | 0 | { |
3694 | 0 | http->error = errno; |
3695 | 0 | return (-1); |
3696 | 0 | } |
3697 | 0 | else if (!http->timeout_cb && errno != EAGAIN) |
3698 | 0 | { |
3699 | 0 | http->error = errno; |
3700 | 0 | return (-1); |
3701 | 0 | } |
3702 | 0 | } |
3703 | 0 | else if (errno != EINTR) |
3704 | 0 | { |
3705 | 0 | http->error = errno; |
3706 | 0 | return (-1); |
3707 | 0 | } |
3708 | 0 | #endif // _WIN32 |
3709 | 0 | } |
3710 | 0 | } |
3711 | 0 | while (bytes < 0); |
3712 | | |
3713 | 0 | DEBUG_printf("8http_read: Read " CUPS_LLFMT " bytes into buffer.", CUPS_LLCAST bytes); |
3714 | | #ifdef DEBUG |
3715 | | if (bytes > 0) |
3716 | | http_debug_hex("http_read", buffer, (int)bytes); |
3717 | | else |
3718 | | #endif // DEBUG |
3719 | 0 | if (bytes == 0) |
3720 | 0 | { |
3721 | 0 | http->error = EPIPE; |
3722 | 0 | return (0); |
3723 | 0 | } |
3724 | | |
3725 | 0 | return (bytes); |
3726 | 0 | } |
3727 | | |
3728 | | |
3729 | | // |
3730 | | // 'http_read_buffered()' - Do a buffered read from a HTTP connection. |
3731 | | // |
3732 | | // This function reads data from the HTTP buffer or from the socket, as needed. |
3733 | | // |
3734 | | |
3735 | | static ssize_t // O - Number of bytes read or -1 on error |
3736 | | http_read_buffered(http_t *http, // I - HTTP connection |
3737 | | char *buffer, // I - Buffer |
3738 | | size_t length) // I - Maximum bytes to read |
3739 | 0 | { |
3740 | 0 | ssize_t bytes; // Bytes read |
3741 | | |
3742 | |
|
3743 | 0 | DEBUG_printf("7http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT ") used=%d", (void *)http, (void *)buffer, CUPS_LLCAST length, http->used); |
3744 | |
|
3745 | 0 | if (http->used > 0) |
3746 | 0 | { |
3747 | 0 | if (length > (size_t)http->used) |
3748 | 0 | bytes = (ssize_t)http->used; |
3749 | 0 | else |
3750 | 0 | bytes = (ssize_t)length; |
3751 | |
|
3752 | 0 | DEBUG_printf("8http_read: Grabbing %d bytes from input buffer.", (int)bytes); |
3753 | |
|
3754 | 0 | memcpy(buffer, http->buffer, (size_t)bytes); |
3755 | 0 | http->used -= (int)bytes; |
3756 | |
|
3757 | 0 | if (http->used > 0) |
3758 | 0 | memmove(http->buffer, http->buffer + bytes, (size_t)http->used); |
3759 | 0 | } |
3760 | 0 | else |
3761 | 0 | bytes = http_read(http, buffer, length); |
3762 | |
|
3763 | 0 | return (bytes); |
3764 | 0 | } |
3765 | | |
3766 | | |
3767 | | // |
3768 | | // 'http_read_chunk()' - Read a chunk from a HTTP connection. |
3769 | | // |
3770 | | // This function reads and validates the chunk length, then does a buffered read |
3771 | | // returning the number of bytes placed in the buffer. |
3772 | | // |
3773 | | |
3774 | | static ssize_t // O - Number of bytes read or -1 on error |
3775 | | http_read_chunk(http_t *http, // I - HTTP connection |
3776 | | char *buffer, // I - Buffer |
3777 | | size_t length) // I - Maximum bytes to read |
3778 | 0 | { |
3779 | 0 | DEBUG_printf("7http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length); |
3780 | |
|
3781 | 0 | if (http->data_remaining <= 0) |
3782 | 0 | { |
3783 | 0 | char len[32]; // Length string |
3784 | |
|
3785 | 0 | if (!httpGets(http, len, sizeof(len))) |
3786 | 0 | { |
3787 | 0 | DEBUG_puts("8http_read_chunk: Could not get chunk length."); |
3788 | 0 | return (0); |
3789 | 0 | } |
3790 | | |
3791 | 0 | if (!len[0]) |
3792 | 0 | { |
3793 | 0 | DEBUG_puts("8http_read_chunk: Blank chunk length, trying again..."); |
3794 | 0 | if (!httpGets(http, len, sizeof(len))) |
3795 | 0 | { |
3796 | 0 | DEBUG_puts("8http_read_chunk: Could not get chunk length."); |
3797 | 0 | return (0); |
3798 | 0 | } |
3799 | 0 | } |
3800 | | |
3801 | 0 | http->data_remaining = strtoll(len, NULL, 16); |
3802 | |
|
3803 | 0 | if (http->data_remaining < 0) |
3804 | 0 | { |
3805 | 0 | DEBUG_printf("8http_read_chunk: Negative chunk length \"%s\" (" CUPS_LLFMT ")", len, CUPS_LLCAST http->data_remaining); |
3806 | 0 | return (0); |
3807 | 0 | } |
3808 | | |
3809 | 0 | DEBUG_printf("8http_read_chunk: Got chunk length \"%s\" (" CUPS_LLFMT ")", len, CUPS_LLCAST http->data_remaining); |
3810 | |
|
3811 | 0 | if (http->data_remaining == 0) |
3812 | 0 | { |
3813 | | // 0-length chunk, grab trailing blank line... |
3814 | 0 | httpGets(http, len, sizeof(len)); |
3815 | 0 | } |
3816 | 0 | } |
3817 | | |
3818 | 0 | DEBUG_printf("8http_read_chunk: data_remaining=" CUPS_LLFMT, CUPS_LLCAST http->data_remaining); |
3819 | |
|
3820 | 0 | if (http->data_remaining <= 0) |
3821 | 0 | return (0); |
3822 | 0 | else if (length > (size_t)http->data_remaining) |
3823 | 0 | length = (size_t)http->data_remaining; |
3824 | | |
3825 | 0 | return (http_read_buffered(http, buffer, length)); |
3826 | 0 | } |
3827 | | |
3828 | | |
3829 | | // |
3830 | | // 'http_send()' - Send a request with all fields and the trailing blank line. |
3831 | | // |
3832 | | |
3833 | | static bool // O - `true` on success, `false` on error |
3834 | | http_send(http_t *http, // I - HTTP connection |
3835 | | http_state_t request, // I - Request code |
3836 | | const char *uri) // I - URI |
3837 | 0 | { |
3838 | 0 | int i; // Looping var |
3839 | 0 | char buf[1024]; // Encoded URI buffer |
3840 | 0 | const char *value; // Field value |
3841 | 0 | static const char * const codes[HTTP_STATE_MAX] = |
3842 | 0 | { // Request code strings |
3843 | 0 | NULL, // WAITING |
3844 | 0 | "CONNECT", |
3845 | 0 | "COPY", |
3846 | 0 | NULL, |
3847 | 0 | "DELETE", |
3848 | 0 | NULL, |
3849 | 0 | "GET", |
3850 | 0 | NULL, |
3851 | 0 | "HEAD", |
3852 | 0 | "LOCK", |
3853 | 0 | NULL, |
3854 | 0 | NULL, |
3855 | 0 | "MKCOL", |
3856 | 0 | "MOVE", |
3857 | 0 | NULL, |
3858 | 0 | "OPTIONS", |
3859 | 0 | "POST", |
3860 | 0 | NULL, |
3861 | 0 | NULL, |
3862 | 0 | "PROPFIND", |
3863 | 0 | NULL, |
3864 | 0 | NULL, |
3865 | 0 | "PROPPATCH", |
3866 | 0 | NULL, |
3867 | 0 | NULL, |
3868 | 0 | "PUT", |
3869 | 0 | NULL, |
3870 | 0 | "TRACE", |
3871 | 0 | "UNLOCK", |
3872 | 0 | NULL, |
3873 | 0 | NULL, |
3874 | 0 | NULL |
3875 | 0 | }; |
3876 | | |
3877 | |
|
3878 | 0 | DEBUG_printf("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http, codes[request], uri); |
3879 | |
|
3880 | 0 | if (http == NULL || uri == NULL) |
3881 | 0 | return (false); |
3882 | | |
3883 | | // Set the User-Agent field if it isn't already... |
3884 | 0 | if (!http->fields[HTTP_FIELD_USER_AGENT]) |
3885 | 0 | { |
3886 | 0 | if (http->default_fields[HTTP_FIELD_USER_AGENT]) |
3887 | 0 | { |
3888 | 0 | DEBUG_puts("5http_send: Using default User-Agent."); |
3889 | 0 | httpSetField(http, HTTP_FIELD_USER_AGENT, http->default_fields[HTTP_FIELD_USER_AGENT]); |
3890 | 0 | } |
3891 | 0 | else |
3892 | 0 | { |
3893 | 0 | DEBUG_puts("5http_send: Using client.conf User-Agent."); |
3894 | 0 | httpSetField(http, HTTP_FIELD_USER_AGENT, cupsGetUserAgent()); |
3895 | 0 | } |
3896 | 0 | } |
3897 | | |
3898 | | // Set the Accept-Encoding field if it isn't already... |
3899 | 0 | if (!http->fields[HTTP_FIELD_ACCEPT_ENCODING] && http->default_fields[HTTP_FIELD_ACCEPT_ENCODING]) |
3900 | 0 | httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, http->default_fields[HTTP_FIELD_ACCEPT_ENCODING]); |
3901 | | |
3902 | | // Set the Authorization field if it isn't already... |
3903 | 0 | if (!http->fields[HTTP_FIELD_AUTHORIZATION] && http->authstring) |
3904 | 0 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); |
3905 | | |
3906 | | // Encode the URI as needed... |
3907 | 0 | _httpEncodeURI(buf, uri, sizeof(buf)); |
3908 | | |
3909 | | // See if we had an error the last time around; if so, reconnect... |
3910 | 0 | if (http->fd < 0 || http->status == HTTP_STATUS_ERROR || http->status >= HTTP_STATUS_BAD_REQUEST) |
3911 | 0 | { |
3912 | 0 | DEBUG_printf("5http_send: Reconnecting, fd=%d, status=%d, tls_upgrade=%d", http->fd, http->status, http->tls_upgrade); |
3913 | |
|
3914 | 0 | if (!httpConnectAgain(http, 30000, NULL)) |
3915 | 0 | return (false); |
3916 | 0 | } |
3917 | | |
3918 | | // Flush any written data that is pending... |
3919 | 0 | if (http->wused) |
3920 | 0 | { |
3921 | 0 | if (httpFlushWrite(http) < 0) |
3922 | 0 | { |
3923 | 0 | if (!httpConnectAgain(http, 30000, NULL)) |
3924 | 0 | return (false); |
3925 | 0 | } |
3926 | 0 | } |
3927 | | |
3928 | | // Send the request header... |
3929 | 0 | http->state = request; |
3930 | 0 | http->data_encoding = HTTP_ENCODING_FIELDS; |
3931 | |
|
3932 | 0 | if (request == HTTP_STATE_LOCK || request == HTTP_STATE_POST || request == HTTP_STATE_PROPFIND || request == HTTP_STATE_PROPPATCH || request == HTTP_STATE_PUT) |
3933 | 0 | http->state ++; |
3934 | |
|
3935 | 0 | http->status = HTTP_STATUS_CONTINUE; |
3936 | |
|
3937 | 0 | if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls) |
3938 | 0 | { |
3939 | 0 | httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); |
3940 | 0 | httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.3,TLS/1.2,TLS/1.1,TLS/1.0"); |
3941 | 0 | } |
3942 | |
|
3943 | 0 | if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1) |
3944 | 0 | { |
3945 | 0 | http->status = HTTP_STATUS_ERROR; |
3946 | 0 | return (false); |
3947 | 0 | } |
3948 | | |
3949 | 0 | for (i = 0; i < HTTP_FIELD_MAX; i ++) |
3950 | 0 | { |
3951 | 0 | if ((value = httpGetField(http, i)) != NULL && *value) |
3952 | 0 | { |
3953 | 0 | DEBUG_printf("5http_send: %s: %s", http_fields[i], value); |
3954 | |
|
3955 | 0 | if (i == HTTP_FIELD_HOST) |
3956 | 0 | { |
3957 | 0 | if (httpPrintf(http, "Host: %s:%d\r\n", value, httpAddrGetPort(http->hostaddr)) < 1) |
3958 | 0 | { |
3959 | 0 | http->status = HTTP_STATUS_ERROR; |
3960 | 0 | return (false); |
3961 | 0 | } |
3962 | 0 | } |
3963 | 0 | else if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1) |
3964 | 0 | { |
3965 | 0 | http->status = HTTP_STATUS_ERROR; |
3966 | 0 | return (false); |
3967 | 0 | } |
3968 | 0 | } |
3969 | 0 | } |
3970 | | |
3971 | 0 | if (http->cookie) |
3972 | 0 | { |
3973 | 0 | if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1) |
3974 | 0 | { |
3975 | 0 | http->status = HTTP_STATUS_ERROR; |
3976 | 0 | return (false); |
3977 | 0 | } |
3978 | 0 | } |
3979 | | |
3980 | 0 | DEBUG_printf("5http_send: expect=%d, mode=%d, state=%d", http->expect, http->mode, http->state); |
3981 | |
|
3982 | 0 | if (http->expect == HTTP_STATUS_CONTINUE && http->mode == _HTTP_MODE_CLIENT && (http->state == HTTP_STATE_LOCK_RECV || http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_PROPFIND_RECV || http->state == HTTP_STATE_PROPPATCH_RECV || http->state == HTTP_STATE_PUT_RECV)) |
3983 | 0 | { |
3984 | 0 | if (httpPrintf(http, "Expect: 100-continue\r\n") < 1) |
3985 | 0 | { |
3986 | 0 | http->status = HTTP_STATUS_ERROR; |
3987 | 0 | return (false); |
3988 | 0 | } |
3989 | 0 | } |
3990 | | |
3991 | 0 | if (httpPrintf(http, "\r\n") < 1) |
3992 | 0 | { |
3993 | 0 | http->status = HTTP_STATUS_ERROR; |
3994 | 0 | return (false); |
3995 | 0 | } |
3996 | | |
3997 | 0 | if (httpFlushWrite(http) < 0) |
3998 | 0 | return (false); |
3999 | | |
4000 | 0 | http_set_length(http); |
4001 | 0 | httpClearFields(http); |
4002 | | |
4003 | | // Some authentication strings can only be used once... |
4004 | 0 | if (http->fields[HTTP_FIELD_AUTHORIZATION] && http->authstring) |
4005 | 0 | { |
4006 | 0 | free(http->authstring); |
4007 | 0 | http->authstring = NULL; |
4008 | 0 | } |
4009 | |
|
4010 | 0 | return (true); |
4011 | 0 | } |
4012 | | |
4013 | | |
4014 | | // |
4015 | | // 'http_set_length()' - Set the data_encoding and data_remaining values. |
4016 | | // |
4017 | | |
4018 | | static off_t // O - Remainder or -1 on error |
4019 | | http_set_length(http_t *http) // I - Connection |
4020 | 0 | { |
4021 | 0 | off_t remaining; // Remainder |
4022 | | |
4023 | |
|
4024 | 0 | DEBUG_printf("4http_set_length(http=%p) mode=%d state=%s", (void *)http, http->mode, httpStateString(http->state)); |
4025 | |
|
4026 | 0 | if ((remaining = httpGetLength(http)) >= 0) |
4027 | 0 | { |
4028 | 0 | if (http->mode == _HTTP_MODE_SERVER && http->state != HTTP_STATE_COPY_SEND && http->state != HTTP_STATE_DELETE_SEND && http->state != HTTP_STATE_GET_SEND && http->state != HTTP_STATE_LOCK && http->state != HTTP_STATE_LOCK_SEND && http->state != HTTP_STATE_MOVE_SEND && http->state != HTTP_STATE_PUT && http->state != HTTP_STATE_POST && http->state != HTTP_STATE_POST_SEND && http->state != HTTP_STATE_PROPFIND && http->state != HTTP_STATE_PROPFIND_SEND && http->state != HTTP_STATE_PROPPATCH && http->state != HTTP_STATE_PROPPATCH_SEND) |
4029 | 0 | { |
4030 | 0 | DEBUG_puts("5http_set_length: Not setting data_encoding/remaining."); |
4031 | 0 | return (remaining); |
4032 | 0 | } |
4033 | | |
4034 | 0 | if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_TRANSFER_ENCODING), "chunked")) |
4035 | 0 | { |
4036 | 0 | DEBUG_puts("5http_set_length: Setting data_encoding to HTTP_ENCODING_CHUNKED."); |
4037 | 0 | http->data_encoding = HTTP_ENCODING_CHUNKED; |
4038 | 0 | } |
4039 | 0 | else |
4040 | 0 | { |
4041 | 0 | DEBUG_puts("5http_set_length: Setting data_encoding to HTTP_ENCODING_LENGTH."); |
4042 | 0 | http->data_encoding = HTTP_ENCODING_LENGTH; |
4043 | 0 | } |
4044 | |
|
4045 | 0 | DEBUG_printf("5http_set_length: Setting data_remaining to " CUPS_LLFMT ".", CUPS_LLCAST remaining); |
4046 | 0 | http->data_remaining = remaining; |
4047 | 0 | } |
4048 | | |
4049 | 0 | return (remaining); |
4050 | 0 | } |
4051 | | |
4052 | | // |
4053 | | // 'http_set_timeout()' - Set the socket timeout values. |
4054 | | // |
4055 | | |
4056 | | static void |
4057 | | http_set_timeout(int fd, // I - File descriptor |
4058 | | double timeout) // I - Timeout in seconds |
4059 | 0 | { |
4060 | | #ifdef _WIN32 |
4061 | | DWORD tv = (DWORD)(timeout * 1000); |
4062 | | // Timeout in milliseconds |
4063 | | |
4064 | | if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, CUPS_SOCAST &tv, sizeof(tv))) |
4065 | | DEBUG_printf("http_set_timeout: setsockopt(SO_RCVTIMEO) failed - %s", strerror(errno)); |
4066 | | |
4067 | | if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, CUPS_SOCAST &tv, sizeof(tv))) |
4068 | | DEBUG_printf("http_set_timeout: setsockopt(SO_SNDTIMEO) failed - %s", strerror(errno)); |
4069 | | |
4070 | | #else |
4071 | 0 | struct timeval tv; // Timeout in secs and usecs |
4072 | |
|
4073 | 0 | tv.tv_sec = (int)timeout; |
4074 | 0 | tv.tv_usec = (int)(1000000 * fmod(timeout, 1.0)); |
4075 | |
|
4076 | 0 | if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, CUPS_SOCAST &tv, sizeof(tv))) |
4077 | 0 | DEBUG_printf("http_set_timeout: setsockopt(SO_RCVTIMEO) failed - %s", strerror(errno)); |
4078 | 0 | if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, CUPS_SOCAST &tv, sizeof(tv))) |
4079 | 0 | DEBUG_printf("http_set_timeout: setsockopt(SO_SNDTIMEO) failed - %s", strerror(errno)); |
4080 | 0 | #endif // _WIN32 |
4081 | 0 | } |
4082 | | |
4083 | | |
4084 | | // |
4085 | | // 'http_set_wait()' - Set the default wait value for reads. |
4086 | | // |
4087 | | |
4088 | | static void |
4089 | | http_set_wait(http_t *http) // I - HTTP connection |
4090 | 0 | { |
4091 | 0 | if (http->blocking) |
4092 | 0 | { |
4093 | 0 | http->wait_value = (int)(http->timeout_value * 1000); |
4094 | |
|
4095 | 0 | if (http->wait_value <= 0) |
4096 | 0 | http->wait_value = 60000; |
4097 | 0 | } |
4098 | 0 | else |
4099 | 0 | http->wait_value = 10000; |
4100 | 0 | } |
4101 | | |
4102 | | |
4103 | | // |
4104 | | // 'http_tls_upgrade()' - Force upgrade to TLS encryption. |
4105 | | // |
4106 | | |
4107 | | static bool // O - `true` on success, `false` on failure |
4108 | | http_tls_upgrade(http_t *http) // I - HTTP connection |
4109 | 0 | { |
4110 | 0 | bool ret; // Return value |
4111 | 0 | http_t myhttp; // Local copy of HTTP data |
4112 | | |
4113 | |
|
4114 | 0 | DEBUG_printf("4http_tls_upgrade(%p)", (void *)http); |
4115 | | |
4116 | | // Flush the connection to make sure any previous "Upgrade" message |
4117 | | // has been read. |
4118 | 0 | httpFlush(http); |
4119 | | |
4120 | | // Copy the HTTP data to a local variable so we can do the OPTIONS |
4121 | | // request without interfering with the existing request data... |
4122 | 0 | memcpy(&myhttp, http, sizeof(myhttp)); |
4123 | | |
4124 | | // Send an OPTIONS request to the server, requiring SSL or TLS |
4125 | | // encryption on the link... |
4126 | 0 | http->tls_upgrade = true; |
4127 | 0 | memset(http->fields, 0, sizeof(http->fields)); |
4128 | 0 | http->expect = (http_status_t)0; |
4129 | |
|
4130 | 0 | if (http->hostname[0] == '/') |
4131 | 0 | httpSetField(http, HTTP_FIELD_HOST, "localhost"); |
4132 | 0 | else |
4133 | 0 | httpSetField(http, HTTP_FIELD_HOST, http->hostname); |
4134 | |
|
4135 | 0 | httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade"); |
4136 | 0 | httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); |
4137 | |
|
4138 | 0 | if ((ret = httpWriteRequest(http, "OPTIONS", "*")) == 0) |
4139 | 0 | { |
4140 | | // Wait for the secure connection... |
4141 | 0 | while (httpUpdate(http) == HTTP_STATUS_CONTINUE) |
4142 | 0 | ; |
4143 | 0 | } |
4144 | | |
4145 | | // Restore the HTTP request data... |
4146 | 0 | memcpy(http->fields, myhttp.fields, sizeof(http->fields)); |
4147 | |
|
4148 | 0 | http->data_encoding = myhttp.data_encoding; |
4149 | 0 | http->data_remaining = myhttp.data_remaining; |
4150 | 0 | http->expect = myhttp.expect; |
4151 | 0 | http->digest_tries = myhttp.digest_tries; |
4152 | 0 | http->tls_upgrade = false; |
4153 | | |
4154 | | // See if we actually went secure... |
4155 | 0 | if (!http->tls) |
4156 | 0 | { |
4157 | | // Server does not support HTTP upgrade... |
4158 | 0 | DEBUG_puts("5http_tls_upgrade: Server does not support HTTP upgrade!"); |
4159 | |
|
4160 | 0 | _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Encryption is not supported."), 1); |
4161 | 0 | httpAddrClose(NULL, http->fd); |
4162 | |
|
4163 | 0 | http->fd = -1; |
4164 | |
|
4165 | 0 | return (false); |
4166 | 0 | } |
4167 | 0 | else |
4168 | 0 | return (ret); |
4169 | 0 | } |
4170 | | |
4171 | | |
4172 | | // |
4173 | | // 'http_write()' - Write a buffer to a HTTP connection. |
4174 | | // |
4175 | | |
4176 | | static ssize_t // O - Number of bytes written |
4177 | | http_write(http_t *http, // I - HTTP connection |
4178 | | const char *buffer, // I - Buffer for data |
4179 | | size_t length) // I - Number of bytes to write |
4180 | 0 | { |
4181 | 0 | ssize_t tbytes, // Total bytes sent |
4182 | 0 | bytes; // Bytes sent |
4183 | | |
4184 | |
|
4185 | 0 | DEBUG_printf("7http_write(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length); |
4186 | 0 | http->error = 0; |
4187 | 0 | tbytes = 0; |
4188 | |
|
4189 | 0 | while (length > 0) |
4190 | 0 | { |
4191 | 0 | DEBUG_printf("8http_write: About to write %d bytes.", (int)length); |
4192 | |
|
4193 | 0 | if (http->timeout_value > 0.0) |
4194 | 0 | { |
4195 | 0 | struct pollfd pfd; // Polled file descriptor |
4196 | 0 | int nfds; // Result from select()/poll() |
4197 | |
|
4198 | 0 | do |
4199 | 0 | { |
4200 | 0 | pfd.fd = http->fd; |
4201 | 0 | pfd.events = POLLOUT; |
4202 | |
|
4203 | 0 | while ((nfds = poll(&pfd, 1, http->wait_value)) < 0) |
4204 | 0 | { |
4205 | | #ifdef _WIN32 |
4206 | | if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) |
4207 | | break; |
4208 | | #else |
4209 | 0 | if (errno == EINTR || errno == EAGAIN) |
4210 | 0 | break; |
4211 | 0 | #endif // _WIN32 |
4212 | 0 | } |
4213 | |
|
4214 | 0 | if (nfds < 0) |
4215 | 0 | { |
4216 | 0 | http->error = errno; |
4217 | 0 | return (-1); |
4218 | 0 | } |
4219 | 0 | else if (nfds == 0 && (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data))) |
4220 | 0 | { |
4221 | | #ifdef _WIN32 |
4222 | | http->error = WSAEWOULDBLOCK; |
4223 | | #else |
4224 | 0 | http->error = EWOULDBLOCK; |
4225 | 0 | #endif // _WIN32 |
4226 | 0 | return (-1); |
4227 | 0 | } |
4228 | 0 | } |
4229 | 0 | while (nfds <= 0); |
4230 | 0 | } |
4231 | | |
4232 | 0 | if (http->tls) |
4233 | 0 | bytes = _httpTLSWrite(http, buffer, (int)length); |
4234 | 0 | else |
4235 | 0 | bytes = send(http->fd, buffer, length, 0); |
4236 | |
|
4237 | 0 | DEBUG_printf("8http_write: Write of " CUPS_LLFMT " bytes returned " CUPS_LLFMT ".", CUPS_LLCAST length, CUPS_LLCAST bytes); |
4238 | |
|
4239 | 0 | if (bytes < 0) |
4240 | 0 | { |
4241 | | #ifdef _WIN32 |
4242 | | if (WSAGetLastError() == WSAEINTR) |
4243 | | continue; |
4244 | | else if (WSAGetLastError() == WSAEWOULDBLOCK) |
4245 | | { |
4246 | | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
4247 | | continue; |
4248 | | |
4249 | | http->error = WSAGetLastError(); |
4250 | | } |
4251 | | else if (WSAGetLastError() != http->error && |
4252 | | WSAGetLastError() != WSAECONNRESET) |
4253 | | { |
4254 | | http->error = WSAGetLastError(); |
4255 | | continue; |
4256 | | } |
4257 | | |
4258 | | #else |
4259 | 0 | if (errno == EINTR) |
4260 | 0 | continue; |
4261 | 0 | else if (errno == EWOULDBLOCK || errno == EAGAIN) |
4262 | 0 | { |
4263 | 0 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
4264 | 0 | continue; |
4265 | 0 | else if (!http->timeout_cb && errno == EAGAIN) |
4266 | 0 | continue; |
4267 | | |
4268 | 0 | http->error = errno; |
4269 | 0 | } |
4270 | 0 | else if (errno != http->error && errno != ECONNRESET) |
4271 | 0 | { |
4272 | 0 | http->error = errno; |
4273 | 0 | continue; |
4274 | 0 | } |
4275 | 0 | #endif // _WIN32 |
4276 | | |
4277 | 0 | DEBUG_printf("8http_write: error writing data (%s).", strerror(http->error)); |
4278 | |
|
4279 | 0 | return (-1); |
4280 | 0 | } |
4281 | | |
4282 | 0 | buffer += bytes; |
4283 | 0 | tbytes += bytes; |
4284 | 0 | length -= (size_t)bytes; |
4285 | 0 | } |
4286 | | |
4287 | | #ifdef DEBUG |
4288 | | http_debug_hex("http_write", buffer - tbytes, (int)tbytes); |
4289 | | #endif // DEBUG |
4290 | | |
4291 | 0 | DEBUG_printf("8http_write: Returning " CUPS_LLFMT ".", CUPS_LLCAST tbytes); |
4292 | |
|
4293 | 0 | return (tbytes); |
4294 | 0 | } |
4295 | | |
4296 | | |
4297 | | // |
4298 | | // 'http_write_chunk()' - Write a chunked buffer. |
4299 | | // |
4300 | | |
4301 | | static ssize_t // O - Number bytes written |
4302 | | http_write_chunk(http_t *http, // I - HTTP connection |
4303 | | const char *buffer, // I - Buffer to write |
4304 | | size_t length) // I - Length of buffer |
4305 | 0 | { |
4306 | 0 | char header[16]; // Chunk header |
4307 | 0 | ssize_t bytes; // Bytes written |
4308 | | |
4309 | |
|
4310 | 0 | DEBUG_printf("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length); |
4311 | | |
4312 | | // Write the chunk header, data, and trailer. |
4313 | 0 | snprintf(header, sizeof(header), "%x\r\n", (unsigned)length); |
4314 | 0 | if (http_write(http, header, strlen(header)) < 0) |
4315 | 0 | { |
4316 | 0 | DEBUG_puts("8http_write_chunk: http_write of length failed."); |
4317 | 0 | return (-1); |
4318 | 0 | } |
4319 | | |
4320 | 0 | if ((bytes = http_write(http, buffer, length)) < 0) |
4321 | 0 | { |
4322 | 0 | DEBUG_puts("8http_write_chunk: http_write of buffer failed."); |
4323 | 0 | return (-1); |
4324 | 0 | } |
4325 | | |
4326 | 0 | if (http_write(http, "\r\n", 2) < 0) |
4327 | 0 | { |
4328 | 0 | DEBUG_puts("8http_write_chunk: http_write of CR LF failed."); |
4329 | 0 | return (-1); |
4330 | 0 | } |
4331 | | |
4332 | 0 | return (bytes); |
4333 | 0 | } |