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