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