]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c07d5b2d | 2 | * "$Id: http.c 181 2006-06-22 20:01:18Z jlovell $" |
ef416fc2 | 3 | * |
4 | * HTTP routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
fa73b229 | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
ecdc0628 | 28 | * httpBlocking() - Set blocking/non-blocking behavior on a connection. |
ef416fc2 | 29 | * httpCheck() - Check to see if there is a pending response from |
30 | * the server. | |
31 | * httpClearCookie() - Clear the cookie value(s). | |
ecdc0628 | 32 | * httpClearFields() - Clear HTTP request fields. |
ef416fc2 | 33 | * httpClose() - Close an HTTP connection... |
34 | * httpConnect() - Connect to a HTTP server. | |
35 | * httpConnectEncrypt() - Connect to a HTTP server using encryption. | |
36 | * httpDelete() - Send a DELETE request to the server. | |
37 | * httpEncryption() - Set the required encryption on the link. | |
ecdc0628 | 38 | * httpError() - Get the last error on a connection. |
ef416fc2 | 39 | * httpFlush() - Flush data from a HTTP connection. |
40 | * httpFlushWrite() - Flush data in write buffer. | |
41 | * httpGet() - Send a GET request to the server. | |
ecdc0628 | 42 | * httpGetBlocking() - Get the blocking/non-block state of a connection. |
43 | * httpGetCookie() - Get any cookie data from the response. | |
44 | * httpGetFd() - Get the file descriptor associated with a | |
45 | * connection. | |
46 | * httpGetField() - Get a field value from a request/response. | |
ef416fc2 | 47 | * httpGetLength() - Get the amount of data remaining from the |
48 | * content-length or transfer-encoding fields. | |
49 | * httpGetLength2() - Get the amount of data remaining from the | |
50 | * content-length or transfer-encoding fields. | |
ecdc0628 | 51 | * httpGetStatus() - Get the status of the last HTTP request. |
ef416fc2 | 52 | * httpGetSubField() - Get a sub-field value. |
53 | * httpGets() - Get a line of text from a HTTP connection. | |
54 | * httpHead() - Send a HEAD request to the server. | |
55 | * httpInitialize() - Initialize the HTTP interface library and set the | |
56 | * default HTTP proxy (if any). | |
57 | * httpOptions() - Send an OPTIONS request to the server. | |
58 | * httpPost() - Send a POST request to the server. | |
59 | * httpPrintf() - Print a formatted string to a HTTP connection. | |
60 | * httpPut() - Send a PUT request to the server. | |
61 | * httpRead() - Read data from a HTTP connection. | |
a4d04587 | 62 | * httpRead2() - Read data from a HTTP connection. |
ef416fc2 | 63 | * _httpReadCDSA() - Read function for CDSA decryption code. |
64 | * httpReconnect() - Reconnect to a HTTP server... | |
65 | * httpSetCookie() - Set the cookie value(s)... | |
b423cd4c | 66 | * httpSetExpect() - Set the Expect: header in a request. |
ef416fc2 | 67 | * httpSetField() - Set the value of an HTTP header. |
68 | * httpSetLength() - Set the content-length and transfer-encoding. | |
69 | * httpTrace() - Send an TRACE request to the server. | |
70 | * httpUpdate() - Update the current HTTP state for incoming data. | |
71 | * httpWait() - Wait for data available on a connection. | |
72 | * httpWrite() - Write data to a HTTP connection. | |
a4d04587 | 73 | * httpWrite2() - Write data to a HTTP connection. |
ef416fc2 | 74 | * _httpWriteCDSA() - Write function for CDSA encryption code. |
75 | * http_field() - Return the field index for a field name. | |
76 | * http_read_ssl() - Read from a SSL/TLS connection. | |
77 | * http_send() - Send a request with all fields and the trailing | |
78 | * blank line. | |
79 | * http_setup_ssl() - Set up SSL/TLS on a connection. | |
80 | * http_shutdown_ssl() - Shut down SSL/TLS on a connection. | |
81 | * http_upgrade() - Force upgrade to TLS encryption. | |
82 | * http_wait() - Wait for data available on a connection. | |
83 | * http_write() - Write data to a connection. | |
84 | * http_write_ssl() - Write to a SSL/TLS connection. | |
85 | */ | |
86 | ||
87 | /* | |
88 | * Include necessary headers... | |
89 | */ | |
90 | ||
91 | #include "http-private.h" | |
92 | #include "globals.h" | |
93 | #include "debug.h" | |
94 | #include <stdlib.h> | |
95 | #include <fcntl.h> | |
96 | #include <errno.h> | |
97 | #ifndef WIN32 | |
98 | # include <signal.h> | |
99 | # include <sys/time.h> | |
100 | # include <sys/resource.h> | |
101 | #endif /* !WIN32 */ | |
102 | ||
103 | ||
104 | /* | |
105 | * Some operating systems have done away with the Fxxxx constants for | |
106 | * the fcntl() call; this works around that "feature"... | |
107 | */ | |
108 | ||
109 | #ifndef FNONBLK | |
110 | # define FNONBLK O_NONBLOCK | |
111 | #endif /* !FNONBLK */ | |
112 | ||
113 | ||
114 | /* | |
115 | * Local functions... | |
116 | */ | |
117 | ||
118 | static http_field_t http_field(const char *name); | |
119 | static int http_send(http_t *http, http_state_t request, | |
120 | const char *uri); | |
121 | static int http_wait(http_t *http, int msec); | |
122 | static int http_write(http_t *http, const char *buffer, | |
123 | int length); | |
124 | static int http_write_chunk(http_t *http, const char *buffer, | |
125 | int length); | |
126 | #ifdef HAVE_SSL | |
127 | static int http_read_ssl(http_t *http, char *buf, int len); | |
128 | static int http_setup_ssl(http_t *http); | |
129 | static void http_shutdown_ssl(http_t *http); | |
130 | static int http_upgrade(http_t *http); | |
131 | static int http_write_ssl(http_t *http, const char *buf, int len); | |
132 | #endif /* HAVE_SSL */ | |
133 | ||
134 | ||
135 | /* | |
136 | * Local globals... | |
137 | */ | |
138 | ||
139 | static const char * const http_fields[] = | |
140 | { | |
141 | "Accept-Language", | |
142 | "Accept-Ranges", | |
143 | "Authorization", | |
144 | "Connection", | |
145 | "Content-Encoding", | |
146 | "Content-Language", | |
147 | "Content-Length", | |
148 | "Content-Location", | |
149 | "Content-MD5", | |
150 | "Content-Range", | |
151 | "Content-Type", | |
152 | "Content-Version", | |
153 | "Date", | |
154 | "Host", | |
155 | "If-Modified-Since", | |
156 | "If-Unmodified-since", | |
157 | "Keep-Alive", | |
158 | "Last-Modified", | |
159 | "Link", | |
160 | "Location", | |
161 | "Range", | |
162 | "Referer", | |
163 | "Retry-After", | |
164 | "Transfer-Encoding", | |
165 | "Upgrade", | |
166 | "User-Agent", | |
167 | "WWW-Authenticate" | |
168 | }; | |
169 | ||
170 | ||
ecdc0628 | 171 | /* |
172 | * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection. | |
173 | */ | |
174 | ||
175 | void | |
176 | httpBlocking(http_t *http, /* I - HTTP connection */ | |
177 | int b) /* I - 1 = blocking, 0 = non-blocking */ | |
178 | { | |
179 | if (http) | |
180 | http->blocking = b; | |
181 | } | |
182 | ||
183 | ||
ef416fc2 | 184 | /* |
185 | * 'httpCheck()' - Check to see if there is a pending response from the server. | |
186 | */ | |
187 | ||
ecdc0628 | 188 | int /* O - 0 = no data, 1 = data available */ |
189 | httpCheck(http_t *http) /* I - HTTP connection */ | |
ef416fc2 | 190 | { |
191 | return (httpWait(http, 0)); | |
192 | } | |
193 | ||
194 | ||
195 | /* | |
196 | * 'httpClearCookie()' - Clear the cookie value(s). | |
197 | * | |
198 | * @since CUPS 1.1.19@ | |
199 | */ | |
200 | ||
201 | void | |
ecdc0628 | 202 | httpClearCookie(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 203 | { |
204 | if (!http) | |
205 | return; | |
206 | ||
207 | if (http->cookie) | |
208 | { | |
209 | free(http->cookie); | |
210 | http->cookie = NULL; | |
211 | } | |
212 | } | |
213 | ||
214 | ||
ecdc0628 | 215 | /* |
216 | * 'httpClearFields()' - Clear HTTP request fields. | |
217 | */ | |
218 | ||
219 | void | |
220 | httpClearFields(http_t *http) /* I - HTTP connection */ | |
221 | { | |
222 | if (http) | |
223 | { | |
224 | memset(http->fields, 0, sizeof(http->fields)); | |
225 | httpSetField(http, HTTP_FIELD_HOST, http->hostname); | |
b423cd4c | 226 | |
227 | http->expect = (http_status_t)0; | |
ecdc0628 | 228 | } |
229 | } | |
230 | ||
231 | ||
ef416fc2 | 232 | /* |
233 | * 'httpClose()' - Close an HTTP connection... | |
234 | */ | |
235 | ||
236 | void | |
ecdc0628 | 237 | httpClose(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 238 | { |
239 | DEBUG_printf(("httpClose(http=%p)\n", http)); | |
240 | ||
241 | if (!http) | |
242 | return; | |
243 | ||
244 | httpAddrFreeList(http->addrlist); | |
245 | ||
246 | if (http->input_set) | |
247 | free(http->input_set); | |
248 | ||
249 | if (http->cookie) | |
250 | free(http->cookie); | |
251 | ||
252 | #ifdef HAVE_SSL | |
253 | if (http->tls) | |
254 | http_shutdown_ssl(http); | |
255 | #endif /* HAVE_SSL */ | |
256 | ||
257 | #ifdef WIN32 | |
258 | closesocket(http->fd); | |
259 | #else | |
260 | close(http->fd); | |
261 | #endif /* WIN32 */ | |
262 | ||
263 | free(http); | |
264 | } | |
265 | ||
266 | ||
267 | /* | |
268 | * 'httpConnect()' - Connect to a HTTP server. | |
269 | */ | |
270 | ||
271 | http_t * /* O - New HTTP connection */ | |
272 | httpConnect(const char *host, /* I - Host to connect to */ | |
273 | int port) /* I - Port number */ | |
274 | { | |
275 | http_encryption_t encryption; /* Type of encryption to use */ | |
276 | ||
277 | ||
278 | /* | |
279 | * Set the default encryption status... | |
280 | */ | |
281 | ||
282 | if (port == 443) | |
283 | encryption = HTTP_ENCRYPT_ALWAYS; | |
284 | else | |
285 | encryption = HTTP_ENCRYPT_IF_REQUESTED; | |
286 | ||
287 | return (httpConnectEncrypt(host, port, encryption)); | |
288 | } | |
289 | ||
290 | ||
291 | /* | |
292 | * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption. | |
293 | */ | |
294 | ||
295 | http_t * /* O - New HTTP connection */ | |
296 | httpConnectEncrypt( | |
297 | const char *host, /* I - Host to connect to */ | |
298 | int port, /* I - Port number */ | |
299 | http_encryption_t encryption) /* I - Type of encryption to use */ | |
300 | { | |
301 | http_t *http; /* New HTTP connection */ | |
302 | http_addrlist_t *addrlist; /* Host address data */ | |
303 | char service[255]; /* Service name */ | |
304 | ||
305 | ||
306 | DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n", | |
307 | host ? host : "(null)", port, encryption)); | |
308 | ||
309 | if (!host) | |
310 | return (NULL); | |
311 | ||
312 | httpInitialize(); | |
313 | ||
314 | /* | |
315 | * Lookup the host... | |
316 | */ | |
317 | ||
318 | sprintf(service, "%d", port); | |
319 | ||
320 | if ((addrlist = httpAddrGetList(host, AF_UNSPEC, service)) == NULL) | |
321 | return (NULL); | |
322 | ||
323 | /* | |
324 | * Allocate memory for the structure... | |
325 | */ | |
326 | ||
327 | http = calloc(sizeof(http_t), 1); | |
328 | if (http == NULL) | |
329 | return (NULL); | |
330 | ||
331 | http->version = HTTP_1_1; | |
332 | http->blocking = 1; | |
333 | http->activity = time(NULL); | |
334 | http->fd = -1; | |
335 | ||
336 | /* | |
337 | * Set the encryption status... | |
338 | */ | |
339 | ||
340 | if (port == 443) /* Always use encryption for https */ | |
341 | http->encryption = HTTP_ENCRYPT_ALWAYS; | |
342 | else | |
343 | http->encryption = encryption; | |
344 | ||
345 | /* | |
346 | * Loop through the addresses we have until one of them connects... | |
347 | */ | |
348 | ||
349 | strlcpy(http->hostname, host, sizeof(http->hostname)); | |
350 | ||
351 | /* | |
352 | * Connect to the remote system... | |
353 | */ | |
354 | ||
355 | http->addrlist = addrlist; | |
356 | ||
357 | if (!httpReconnect(http)) | |
358 | return (http); | |
359 | ||
360 | /* | |
361 | * Could not connect to any known address - bail out! | |
362 | */ | |
363 | ||
364 | httpAddrFreeList(addrlist); | |
365 | ||
366 | free(http); | |
367 | ||
368 | return (NULL); | |
369 | } | |
370 | ||
371 | ||
372 | /* | |
373 | * 'httpDelete()' - Send a DELETE request to the server. | |
374 | */ | |
375 | ||
376 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 377 | httpDelete(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 378 | const char *uri) /* I - URI to delete */ |
379 | { | |
380 | return (http_send(http, HTTP_DELETE, uri)); | |
381 | } | |
382 | ||
383 | ||
384 | /* | |
385 | * 'httpEncryption()' - Set the required encryption on the link. | |
386 | */ | |
387 | ||
388 | int /* O - -1 on error, 0 on success */ | |
ecdc0628 | 389 | httpEncryption(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 390 | http_encryption_t e) /* I - New encryption preference */ |
391 | { | |
392 | DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http, e)); | |
393 | ||
394 | #ifdef HAVE_SSL | |
395 | if (!http) | |
396 | return (0); | |
397 | ||
398 | http->encryption = e; | |
399 | ||
400 | if ((http->encryption == HTTP_ENCRYPT_ALWAYS && !http->tls) || | |
401 | (http->encryption == HTTP_ENCRYPT_NEVER && http->tls)) | |
402 | return (httpReconnect(http)); | |
403 | else if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls) | |
404 | return (http_upgrade(http)); | |
405 | else | |
406 | return (0); | |
407 | #else | |
408 | if (e == HTTP_ENCRYPT_ALWAYS || e == HTTP_ENCRYPT_REQUIRED) | |
409 | return (-1); | |
410 | else | |
411 | return (0); | |
412 | #endif /* HAVE_SSL */ | |
413 | } | |
414 | ||
415 | ||
ecdc0628 | 416 | /* |
417 | * 'httpError()' - Get the last error on a connection. | |
418 | */ | |
419 | ||
420 | int /* O - Error code (errno) value */ | |
421 | httpError(http_t *http) /* I - HTTP connection */ | |
422 | { | |
423 | if (http) | |
424 | return (http->error); | |
425 | else | |
426 | return (EINVAL); | |
427 | } | |
428 | ||
429 | ||
ef416fc2 | 430 | /* |
431 | * 'httpFlush()' - Flush data from a HTTP connection. | |
432 | */ | |
433 | ||
434 | void | |
ecdc0628 | 435 | httpFlush(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 436 | { |
437 | char buffer[8192]; /* Junk buffer */ | |
fa73b229 | 438 | int blocking; /* To block or not to block */ |
ef416fc2 | 439 | |
440 | ||
441 | DEBUG_printf(("httpFlush(http=%p), state=%d\n", http, http->state)); | |
442 | ||
fa73b229 | 443 | /* |
444 | * Temporarily set non-blocking mode so we don't get stuck in httpRead()... | |
445 | */ | |
446 | ||
447 | blocking = http->blocking; | |
448 | http->blocking = 0; | |
449 | ||
450 | /* | |
451 | * Read any data we can... | |
452 | */ | |
453 | ||
a4d04587 | 454 | while (httpRead2(http, buffer, sizeof(buffer)) > 0); |
fa73b229 | 455 | |
456 | /* | |
457 | * Restore blocking and reset the connection if we didn't get all of | |
458 | * the remaining data... | |
459 | */ | |
460 | ||
461 | http->blocking = blocking; | |
462 | ||
463 | if (http->state != HTTP_WAITING && http->fd >= 0) | |
464 | { | |
465 | /* | |
466 | * Didn't get the data back, so close the current connection. | |
467 | */ | |
468 | ||
469 | http->state = HTTP_WAITING; | |
470 | ||
471 | #ifdef HAVE_SSL | |
472 | if (http->tls) | |
473 | http_shutdown_ssl(http); | |
474 | #endif /* HAVE_SSL */ | |
475 | ||
476 | #ifdef WIN32 | |
477 | closesocket(http->fd); | |
478 | #else | |
479 | close(http->fd); | |
480 | #endif /* WIN32 */ | |
481 | ||
482 | http->fd = -1; | |
483 | } | |
ef416fc2 | 484 | } |
485 | ||
486 | ||
487 | /* | |
488 | * 'httpFlushWrite()' - Flush data in write buffer. | |
489 | * | |
490 | * @since CUPS 1.2@ | |
491 | */ | |
492 | ||
493 | int /* O - Bytes written or -1 on error */ | |
ecdc0628 | 494 | httpFlushWrite(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 495 | { |
496 | int bytes; /* Bytes written */ | |
497 | ||
498 | ||
499 | DEBUG_printf(("httpFlushWrite(http=%p)\n", http)); | |
500 | ||
501 | if (!http || !http->wused) | |
502 | return (0); | |
503 | ||
504 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
505 | bytes = http_write_chunk(http, http->wbuffer, http->wused); | |
506 | else | |
507 | bytes = http_write(http, http->wbuffer, http->wused); | |
508 | ||
509 | http->wused = 0; | |
510 | ||
511 | return (bytes); | |
512 | } | |
513 | ||
514 | ||
515 | /* | |
516 | * 'httpGet()' - Send a GET request to the server. | |
517 | */ | |
518 | ||
519 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 520 | httpGet(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 521 | const char *uri) /* I - URI to get */ |
522 | { | |
523 | return (http_send(http, HTTP_GET, uri)); | |
524 | } | |
525 | ||
526 | ||
ecdc0628 | 527 | /* |
528 | * 'httpGetBlocking()' - Get the blocking/non-block state of a connection. | |
529 | * | |
530 | * @since CUPS 1.2@ | |
531 | */ | |
532 | ||
533 | int /* O - 1 if blocking, 0 if non-blocking */ | |
534 | httpGetBlocking(http_t *http) /* I - HTTP connection */ | |
535 | { | |
536 | return (http ? http->blocking : 0); | |
537 | } | |
538 | ||
539 | ||
540 | /* | |
541 | * 'httpGetCookie()' - Get any cookie data from the response. | |
757d2cad | 542 | * |
543 | * @since CUPS 1.1.19@ | |
ecdc0628 | 544 | */ |
545 | ||
546 | const char * /* O - Cookie data or NULL */ | |
547 | httpGetCookie(http_t *http) /* I - HTTP connecion */ | |
548 | { | |
549 | return (http ? http->cookie : NULL); | |
550 | } | |
551 | ||
552 | ||
553 | /* | |
554 | * 'httpGetFd()' - Get the file descriptor associated with a connection. | |
555 | * | |
556 | * @since CUPS 1.2@ | |
557 | */ | |
558 | ||
559 | int /* O - File descriptor or -1 if none */ | |
560 | httpGetFd(http_t *http) /* I - HTTP connection */ | |
561 | { | |
562 | return (http ? http->fd : -1); | |
563 | } | |
564 | ||
565 | ||
566 | /* | |
567 | * 'httpGetField()' - Get a field value from a request/response. | |
568 | */ | |
569 | ||
570 | const char * /* O - Field value */ | |
571 | httpGetField(http_t *http, /* I - HTTP connection */ | |
572 | http_field_t field) /* I - Field to get */ | |
573 | { | |
574 | if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) | |
575 | return (NULL); | |
576 | else | |
577 | return (http->fields[field]); | |
578 | } | |
579 | ||
580 | ||
581 | /* | |
582 | * 'httpGetLength()' - Get the amount of data remaining from the | |
583 | * content-length or transfer-encoding fields. | |
584 | * | |
585 | * This function is deprecated and will not return lengths larger than | |
586 | * 2^31 - 1; use httpGetLength2() instead. | |
587 | * | |
588 | * @deprecated@ | |
589 | */ | |
590 | ||
591 | int /* O - Content length */ | |
592 | httpGetLength(http_t *http) /* I - HTTP connection */ | |
593 | { | |
594 | /* | |
595 | * Get the read content length and return the 32-bit value. | |
596 | */ | |
597 | ||
598 | if (http) | |
599 | { | |
600 | httpGetLength2(http); | |
601 | ||
602 | return (http->_data_remaining); | |
603 | } | |
604 | else | |
605 | return (-1); | |
606 | } | |
607 | ||
608 | ||
609 | /* | |
610 | * 'httpGetLength2()' - Get the amount of data remaining from the | |
611 | * content-length or transfer-encoding fields. | |
612 | * | |
613 | * This function returns the complete content length, even for | |
614 | * content larger than 2^31 - 1. | |
615 | * | |
616 | * @since CUPS 1.2@ | |
617 | */ | |
618 | ||
619 | off_t /* O - Content length */ | |
620 | httpGetLength2(http_t *http) /* I - HTTP connection */ | |
621 | { | |
622 | DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http, http->state)); | |
623 | ||
624 | if (!http) | |
625 | return (-1); | |
626 | ||
627 | if (!strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked")) | |
628 | { | |
629 | DEBUG_puts("httpGetLength2: chunked request!"); | |
630 | ||
631 | http->data_encoding = HTTP_ENCODE_CHUNKED; | |
632 | http->data_remaining = 0; | |
633 | } | |
634 | else | |
635 | { | |
636 | http->data_encoding = HTTP_ENCODE_LENGTH; | |
637 | ||
638 | /* | |
639 | * The following is a hack for HTTP servers that don't send a | |
640 | * content-length or transfer-encoding field... | |
641 | * | |
642 | * If there is no content-length then the connection must close | |
643 | * after the transfer is complete... | |
644 | */ | |
645 | ||
646 | if (http->fields[HTTP_FIELD_CONTENT_LENGTH][0] == '\0') | |
647 | http->data_remaining = 2147483647; | |
648 | else | |
649 | http->data_remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH], | |
650 | NULL, 10); | |
651 | ||
652 | DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT "\n", | |
653 | CUPS_LLCAST http->data_remaining)); | |
654 | } | |
655 | ||
656 | if (http->data_remaining <= INT_MAX) | |
657 | http->_data_remaining = (int)http->data_remaining; | |
658 | else | |
659 | http->_data_remaining = INT_MAX; | |
660 | ||
661 | return (http->data_remaining); | |
662 | } | |
663 | ||
664 | ||
665 | /* | |
666 | * 'httpGetStatus()' - Get the status of the last HTTP request. | |
667 | * | |
668 | * @since CUPS 1.2@ | |
669 | */ | |
670 | ||
671 | http_status_t /* O - HTTP status */ | |
672 | httpGetStatus(http_t *http) /* I - HTTP connection */ | |
673 | { | |
674 | return (http ? http->status : HTTP_ERROR); | |
675 | } | |
676 | ||
677 | ||
ef416fc2 | 678 | /* |
679 | * 'httpGetSubField()' - Get a sub-field value. | |
680 | * | |
681 | * @deprecated@ | |
682 | */ | |
683 | ||
684 | char * /* O - Value or NULL */ | |
ecdc0628 | 685 | httpGetSubField(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 686 | http_field_t field, /* I - Field index */ |
687 | const char *name, /* I - Name of sub-field */ | |
688 | char *value) /* O - Value string */ | |
689 | { | |
690 | return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE)); | |
691 | } | |
692 | ||
693 | ||
694 | /* | |
695 | * 'httpGetSubField2()' - Get a sub-field value. | |
696 | * | |
697 | * @since CUPS 1.2@ | |
698 | */ | |
699 | ||
700 | char * /* O - Value or NULL */ | |
ecdc0628 | 701 | httpGetSubField2(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 702 | http_field_t field, /* I - Field index */ |
703 | const char *name, /* I - Name of sub-field */ | |
704 | char *value, /* O - Value string */ | |
705 | int valuelen) /* I - Size of value buffer */ | |
706 | { | |
707 | const char *fptr; /* Pointer into field */ | |
708 | char temp[HTTP_MAX_VALUE], /* Temporary buffer for name */ | |
709 | *ptr, /* Pointer into string buffer */ | |
710 | *end; /* End of value buffer */ | |
711 | ||
712 | DEBUG_printf(("httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)\n", | |
713 | http, field, name, value, valuelen)); | |
714 | ||
715 | if (!http || !name || !value || valuelen < 2 || | |
ecdc0628 | 716 | field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) |
ef416fc2 | 717 | return (NULL); |
718 | ||
719 | end = value + valuelen - 1; | |
720 | ||
721 | for (fptr = http->fields[field]; *fptr;) | |
722 | { | |
723 | /* | |
724 | * Skip leading whitespace... | |
725 | */ | |
726 | ||
727 | while (isspace(*fptr & 255)) | |
728 | fptr ++; | |
729 | ||
730 | if (*fptr == ',') | |
731 | { | |
732 | fptr ++; | |
733 | continue; | |
734 | } | |
735 | ||
736 | /* | |
737 | * Get the sub-field name... | |
738 | */ | |
739 | ||
740 | for (ptr = temp; | |
741 | *fptr && *fptr != '=' && !isspace(*fptr & 255) && | |
742 | ptr < (temp + sizeof(temp) - 1); | |
743 | *ptr++ = *fptr++); | |
744 | ||
745 | *ptr = '\0'; | |
746 | ||
747 | DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp)); | |
748 | ||
749 | /* | |
750 | * Skip trailing chars up to the '='... | |
751 | */ | |
752 | ||
753 | while (isspace(*fptr & 255)) | |
754 | fptr ++; | |
755 | ||
756 | if (!*fptr) | |
757 | break; | |
758 | ||
759 | if (*fptr != '=') | |
760 | continue; | |
761 | ||
762 | /* | |
763 | * Skip = and leading whitespace... | |
764 | */ | |
765 | ||
766 | fptr ++; | |
767 | ||
768 | while (isspace(*fptr & 255)) | |
769 | fptr ++; | |
770 | ||
771 | if (*fptr == '\"') | |
772 | { | |
773 | /* | |
774 | * Read quoted string... | |
775 | */ | |
776 | ||
777 | for (ptr = value, fptr ++; | |
778 | *fptr && *fptr != '\"' && ptr < end; | |
779 | *ptr++ = *fptr++); | |
780 | ||
781 | *ptr = '\0'; | |
782 | ||
783 | while (*fptr && *fptr != '\"') | |
784 | fptr ++; | |
785 | ||
786 | if (*fptr) | |
787 | fptr ++; | |
788 | } | |
789 | else | |
790 | { | |
791 | /* | |
792 | * Read unquoted string... | |
793 | */ | |
794 | ||
795 | for (ptr = value; | |
796 | *fptr && !isspace(*fptr & 255) && *fptr != ',' && ptr < end; | |
797 | *ptr++ = *fptr++); | |
798 | ||
799 | *ptr = '\0'; | |
800 | ||
801 | while (*fptr && !isspace(*fptr & 255) && *fptr != ',') | |
802 | fptr ++; | |
803 | } | |
804 | ||
805 | DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value)); | |
806 | ||
807 | /* | |
808 | * See if this is the one... | |
809 | */ | |
810 | ||
811 | if (!strcmp(name, temp)) | |
812 | return (value); | |
813 | } | |
814 | ||
815 | value[0] = '\0'; | |
816 | ||
817 | return (NULL); | |
818 | } | |
819 | ||
820 | ||
ef416fc2 | 821 | /* |
822 | * 'httpGets()' - Get a line of text from a HTTP connection. | |
823 | */ | |
824 | ||
825 | char * /* O - Line or NULL */ | |
826 | httpGets(char *line, /* I - Line to read into */ | |
827 | int length, /* I - Max length of buffer */ | |
ecdc0628 | 828 | http_t *http) /* I - HTTP connection */ |
ef416fc2 | 829 | { |
830 | char *lineptr, /* Pointer into line */ | |
831 | *lineend, /* End of line */ | |
832 | *bufptr, /* Pointer into input buffer */ | |
833 | *bufend; /* Pointer to end of buffer */ | |
834 | int bytes, /* Number of bytes read */ | |
835 | eol; /* End-of-line? */ | |
836 | ||
837 | ||
838 | DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line, length, http)); | |
839 | ||
840 | if (http == NULL || line == NULL) | |
841 | return (NULL); | |
842 | ||
843 | /* | |
844 | * Read a line from the buffer... | |
845 | */ | |
846 | ||
847 | lineptr = line; | |
848 | lineend = line + length - 1; | |
849 | eol = 0; | |
850 | ||
851 | while (lineptr < lineend) | |
852 | { | |
853 | /* | |
854 | * Pre-load the buffer as needed... | |
855 | */ | |
856 | ||
857 | #ifdef WIN32 | |
858 | WSASetLastError(0); | |
859 | #else | |
860 | errno = 0; | |
861 | #endif /* WIN32 */ | |
862 | ||
863 | while (http->used == 0) | |
864 | { | |
865 | /* | |
866 | * No newline; see if there is more data to be read... | |
867 | */ | |
868 | ||
ed486911 | 869 | if (!http->blocking && !http_wait(http, 10000)) |
89d46774 | 870 | { |
871 | DEBUG_puts("httpGets: Timed out!"); | |
872 | http->error = ETIMEDOUT; | |
ef416fc2 | 873 | return (NULL); |
89d46774 | 874 | } |
ef416fc2 | 875 | |
876 | #ifdef HAVE_SSL | |
877 | if (http->tls) | |
878 | bytes = http_read_ssl(http, http->buffer + http->used, | |
879 | HTTP_MAX_BUFFER - http->used); | |
880 | else | |
881 | #endif /* HAVE_SSL */ | |
882 | bytes = recv(http->fd, http->buffer + http->used, | |
883 | HTTP_MAX_BUFFER - http->used, 0); | |
884 | ||
885 | DEBUG_printf(("httpGets: read %d bytes...\n", bytes)); | |
886 | ||
887 | if (bytes < 0) | |
888 | { | |
889 | /* | |
890 | * Nope, can't get a line this time... | |
891 | */ | |
892 | ||
893 | #ifdef WIN32 | |
894 | if (WSAGetLastError() != http->error) | |
895 | { | |
896 | http->error = WSAGetLastError(); | |
897 | continue; | |
898 | } | |
899 | ||
900 | DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError())); | |
901 | #else | |
902 | DEBUG_printf(("httpGets: recv() error %d!\n", errno)); | |
903 | ||
904 | if (errno == EINTR) | |
905 | continue; | |
906 | else if (errno != http->error) | |
907 | { | |
908 | http->error = errno; | |
909 | continue; | |
910 | } | |
911 | #endif /* WIN32 */ | |
912 | ||
913 | return (NULL); | |
914 | } | |
915 | else if (bytes == 0) | |
916 | { | |
917 | http->error = EPIPE; | |
918 | ||
919 | return (NULL); | |
920 | } | |
921 | ||
922 | /* | |
923 | * Yup, update the amount used... | |
924 | */ | |
925 | ||
926 | http->used += bytes; | |
927 | } | |
928 | ||
929 | /* | |
930 | * Now copy as much of the current line as possible... | |
931 | */ | |
932 | ||
933 | for (bufptr = http->buffer, bufend = http->buffer + http->used; | |
934 | lineptr < lineend && bufptr < bufend;) | |
935 | { | |
936 | if (*bufptr == 0x0a) | |
937 | { | |
938 | eol = 1; | |
939 | bufptr ++; | |
940 | break; | |
941 | } | |
942 | else if (*bufptr == 0x0d) | |
943 | bufptr ++; | |
944 | else | |
945 | *lineptr++ = *bufptr++; | |
946 | } | |
947 | ||
948 | http->used -= bufptr - http->buffer; | |
949 | if (http->used > 0) | |
950 | memmove(http->buffer, bufptr, http->used); | |
951 | ||
952 | if (eol) | |
953 | { | |
954 | /* | |
955 | * End of line... | |
956 | */ | |
957 | ||
958 | http->activity = time(NULL); | |
959 | ||
960 | *lineptr = '\0'; | |
961 | ||
962 | DEBUG_printf(("httpGets: Returning \"%s\"\n", line)); | |
963 | ||
964 | return (line); | |
965 | } | |
966 | } | |
967 | ||
968 | DEBUG_puts("httpGets: No new line available!"); | |
969 | ||
970 | return (NULL); | |
971 | } | |
972 | ||
973 | ||
974 | /* | |
975 | * 'httpHead()' - Send a HEAD request to the server. | |
976 | */ | |
977 | ||
978 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 979 | httpHead(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 980 | const char *uri) /* I - URI for head */ |
981 | { | |
982 | return (http_send(http, HTTP_HEAD, uri)); | |
983 | } | |
984 | ||
985 | ||
986 | /* | |
987 | * 'httpInitialize()' - Initialize the HTTP interface library and set the | |
988 | * default HTTP proxy (if any). | |
989 | */ | |
990 | ||
991 | void | |
992 | httpInitialize(void) | |
993 | { | |
994 | #ifdef HAVE_LIBSSL | |
995 | # ifndef WIN32 | |
996 | struct timeval curtime; /* Current time in microseconds */ | |
997 | # endif /* !WIN32 */ | |
998 | int i; /* Looping var */ | |
999 | unsigned char data[1024]; /* Seed data */ | |
1000 | #endif /* HAVE_LIBSSL */ | |
1001 | ||
1002 | #ifdef WIN32 | |
1003 | WSADATA winsockdata; /* WinSock data */ | |
1004 | static int initialized = 0; /* Has WinSock been initialized? */ | |
1005 | ||
1006 | ||
1007 | if (!initialized) | |
1008 | WSAStartup(MAKEWORD(1,1), &winsockdata); | |
fa73b229 | 1009 | #elif !defined(SO_NOSIGPIPE) |
ef416fc2 | 1010 | /* |
1011 | * Ignore SIGPIPE signals... | |
1012 | */ | |
1013 | ||
fa73b229 | 1014 | # ifdef HAVE_SIGSET |
1015 | sigset(SIGPIPE, SIG_IGN); | |
1016 | # elif defined(HAVE_SIGACTION) | |
1017 | struct sigaction action; /* POSIX sigaction data */ | |
1018 | ||
1019 | ||
ef416fc2 | 1020 | memset(&action, 0, sizeof(action)); |
1021 | action.sa_handler = SIG_IGN; | |
1022 | sigaction(SIGPIPE, &action, NULL); | |
fa73b229 | 1023 | # else |
ef416fc2 | 1024 | signal(SIGPIPE, SIG_IGN); |
fa73b229 | 1025 | # endif /* !SO_NOSIGPIPE */ |
ef416fc2 | 1026 | #endif /* WIN32 */ |
1027 | ||
1028 | #ifdef HAVE_GNUTLS | |
1029 | gnutls_global_init(); | |
1030 | #endif /* HAVE_GNUTLS */ | |
1031 | ||
1032 | #ifdef HAVE_LIBSSL | |
1033 | SSL_load_error_strings(); | |
1034 | SSL_library_init(); | |
1035 | ||
1036 | /* | |
1037 | * Using the current time is a dubious random seed, but on some systems | |
1038 | * it is the best we can do (on others, this seed isn't even used...) | |
1039 | */ | |
1040 | ||
1041 | #ifdef WIN32 | |
1042 | #else | |
1043 | gettimeofday(&curtime, NULL); | |
1044 | srand(curtime.tv_sec + curtime.tv_usec); | |
1045 | #endif /* WIN32 */ | |
1046 | ||
1047 | for (i = 0; i < sizeof(data); i ++) | |
1048 | data[i] = rand(); /* Yes, this is a poor source of random data... */ | |
1049 | ||
1050 | RAND_seed(&data, sizeof(data)); | |
1051 | #endif /* HAVE_LIBSSL */ | |
1052 | } | |
1053 | ||
1054 | ||
1055 | /* | |
1056 | * 'httpOptions()' - Send an OPTIONS request to the server. | |
1057 | */ | |
1058 | ||
1059 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1060 | httpOptions(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1061 | const char *uri) /* I - URI for options */ |
1062 | { | |
1063 | return (http_send(http, HTTP_OPTIONS, uri)); | |
1064 | } | |
1065 | ||
1066 | ||
1067 | /* | |
1068 | * 'httpPost()' - Send a POST request to the server. | |
1069 | */ | |
1070 | ||
1071 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1072 | httpPost(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1073 | const char *uri) /* I - URI for post */ |
1074 | { | |
1075 | return (http_send(http, HTTP_POST, uri)); | |
1076 | } | |
1077 | ||
1078 | ||
1079 | /* | |
1080 | * 'httpPrintf()' - Print a formatted string to a HTTP connection. | |
ecdc0628 | 1081 | * |
1082 | * @private@ | |
ef416fc2 | 1083 | */ |
1084 | ||
1085 | int /* O - Number of bytes written */ | |
ecdc0628 | 1086 | httpPrintf(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1087 | const char *format, /* I - printf-style format string */ |
1088 | ...) /* I - Additional args as needed */ | |
1089 | { | |
1090 | int bytes; /* Number of bytes to write */ | |
1091 | char buf[16384]; /* Buffer for formatted string */ | |
1092 | va_list ap; /* Variable argument pointer */ | |
1093 | ||
1094 | ||
1095 | DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http, format)); | |
1096 | ||
1097 | va_start(ap, format); | |
1098 | bytes = vsnprintf(buf, sizeof(buf), format, ap); | |
1099 | va_end(ap); | |
1100 | ||
1101 | DEBUG_printf(("httpPrintf: %s", buf)); | |
1102 | ||
1103 | if (http->wused) | |
1104 | { | |
1105 | DEBUG_puts(" flushing existing data..."); | |
1106 | ||
1107 | if (httpFlushWrite(http) < 0) | |
1108 | return (-1); | |
1109 | } | |
1110 | ||
1111 | return (http_write(http, buf, bytes)); | |
1112 | } | |
1113 | ||
1114 | ||
1115 | /* | |
1116 | * 'httpPut()' - Send a PUT request to the server. | |
1117 | */ | |
1118 | ||
1119 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1120 | httpPut(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1121 | const char *uri) /* I - URI to put */ |
1122 | { | |
1123 | return (http_send(http, HTTP_PUT, uri)); | |
1124 | } | |
1125 | ||
1126 | ||
1127 | /* | |
1128 | * 'httpRead()' - Read data from a HTTP connection. | |
a4d04587 | 1129 | * |
1130 | * This function is deprecated. Use the httpRead2() function which can | |
1131 | * read more than 2GB of data. | |
1132 | * | |
1133 | * @deprecated@ | |
ef416fc2 | 1134 | */ |
1135 | ||
1136 | int /* O - Number of bytes read */ | |
ecdc0628 | 1137 | httpRead(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1138 | char *buffer, /* I - Buffer for data */ |
1139 | int length) /* I - Maximum number of bytes */ | |
1140 | { | |
a4d04587 | 1141 | return ((int)httpRead2(http, buffer, length)); |
1142 | } | |
1143 | ||
1144 | ||
1145 | /* | |
1146 | * 'httpRead2()' - Read data from a HTTP connection. | |
ecdc0628 | 1147 | * |
1148 | * @since CUPS 1.2@ | |
a4d04587 | 1149 | */ |
1150 | ||
1151 | ssize_t /* O - Number of bytes read */ | |
ecdc0628 | 1152 | httpRead2(http_t *http, /* I - HTTP connection */ |
a4d04587 | 1153 | char *buffer, /* I - Buffer for data */ |
1154 | size_t length) /* I - Maximum number of bytes */ | |
1155 | { | |
1156 | ssize_t bytes; /* Bytes read */ | |
ef416fc2 | 1157 | char len[32]; /* Length string */ |
1158 | ||
1159 | ||
1160 | DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n", | |
1161 | http, buffer, length)); | |
1162 | ||
1163 | if (http == NULL || buffer == NULL) | |
1164 | return (-1); | |
1165 | ||
1166 | http->activity = time(NULL); | |
1167 | ||
1168 | if (length <= 0) | |
1169 | return (0); | |
1170 | ||
1171 | if (http->data_encoding == HTTP_ENCODE_CHUNKED && | |
1172 | http->data_remaining <= 0) | |
1173 | { | |
a4d04587 | 1174 | DEBUG_puts("httpRead2: Getting chunk length..."); |
ef416fc2 | 1175 | |
1176 | if (httpGets(len, sizeof(len), http) == NULL) | |
1177 | { | |
a4d04587 | 1178 | DEBUG_puts("httpRead2: Could not get length!"); |
ef416fc2 | 1179 | return (0); |
1180 | } | |
1181 | ||
1182 | http->data_remaining = strtoll(len, NULL, 16); | |
1183 | if (http->data_remaining < 0) | |
1184 | { | |
a4d04587 | 1185 | DEBUG_puts("httpRead2: Negative chunk length!"); |
ef416fc2 | 1186 | return (0); |
1187 | } | |
1188 | } | |
1189 | ||
a4d04587 | 1190 | DEBUG_printf(("httpRead2: data_remaining=" CUPS_LLFMT "\n", |
ef416fc2 | 1191 | CUPS_LLCAST http->data_remaining)); |
1192 | ||
1193 | if (http->data_remaining <= 0) | |
1194 | { | |
1195 | /* | |
1196 | * A zero-length chunk ends a transfer; unless we are reading POST | |
1197 | * data, go idle... | |
1198 | */ | |
1199 | ||
1200 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
1201 | httpGets(len, sizeof(len), http); | |
1202 | ||
1203 | if (http->state == HTTP_POST_RECV) | |
1204 | http->state ++; | |
1205 | else | |
1206 | http->state = HTTP_WAITING; | |
1207 | ||
1208 | /* | |
1209 | * Prevent future reads for this request... | |
1210 | */ | |
1211 | ||
1212 | http->data_encoding = HTTP_ENCODE_LENGTH; | |
1213 | ||
1214 | return (0); | |
1215 | } | |
1216 | else if (length > http->data_remaining) | |
1217 | length = http->data_remaining; | |
1218 | ||
1219 | if (http->used == 0 && length <= 256) | |
1220 | { | |
1221 | /* | |
1222 | * Buffer small reads for better performance... | |
1223 | */ | |
1224 | ||
ed486911 | 1225 | if (!http->blocking && !httpWait(http, 10000)) |
ef416fc2 | 1226 | return (0); |
1227 | ||
1228 | if (http->data_remaining > sizeof(http->buffer)) | |
1229 | bytes = sizeof(http->buffer); | |
1230 | else | |
1231 | bytes = http->data_remaining; | |
1232 | ||
1233 | #ifdef HAVE_SSL | |
1234 | if (http->tls) | |
1235 | bytes = http_read_ssl(http, http->buffer, bytes); | |
1236 | else | |
1237 | #endif /* HAVE_SSL */ | |
1238 | { | |
a4d04587 | 1239 | DEBUG_printf(("httpRead2: reading %d bytes from socket into buffer...\n", |
ef416fc2 | 1240 | bytes)); |
1241 | ||
1242 | bytes = recv(http->fd, http->buffer, bytes, 0); | |
1243 | ||
a4d04587 | 1244 | DEBUG_printf(("httpRead2: read %d bytes from socket into buffer...\n", |
ef416fc2 | 1245 | bytes)); |
1246 | } | |
1247 | ||
1248 | if (bytes > 0) | |
1249 | http->used = bytes; | |
1250 | else if (bytes < 0) | |
1251 | { | |
1252 | #ifdef WIN32 | |
1253 | http->error = WSAGetLastError(); | |
1254 | return (-1); | |
1255 | #else | |
1256 | if (errno != EINTR) | |
1257 | { | |
1258 | http->error = errno; | |
1259 | return (-1); | |
1260 | } | |
1261 | #endif /* WIN32 */ | |
1262 | } | |
1263 | else | |
1264 | { | |
1265 | http->error = EPIPE; | |
1266 | return (0); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | if (http->used > 0) | |
1271 | { | |
1272 | if (length > http->used) | |
1273 | length = http->used; | |
1274 | ||
1275 | bytes = length; | |
1276 | ||
a4d04587 | 1277 | DEBUG_printf(("httpRead2: grabbing %d bytes from input buffer...\n", bytes)); |
ef416fc2 | 1278 | |
1279 | memcpy(buffer, http->buffer, length); | |
1280 | http->used -= length; | |
1281 | ||
1282 | if (http->used > 0) | |
1283 | memmove(http->buffer, http->buffer + length, http->used); | |
1284 | } | |
1285 | #ifdef HAVE_SSL | |
1286 | else if (http->tls) | |
1287 | { | |
ed486911 | 1288 | if (!http->blocking && !httpWait(http, 10000)) |
ef416fc2 | 1289 | return (0); |
1290 | ||
1291 | bytes = http_read_ssl(http, buffer, length); | |
1292 | } | |
1293 | #endif /* HAVE_SSL */ | |
1294 | else | |
1295 | { | |
ed486911 | 1296 | if (!http->blocking && !httpWait(http, 10000)) |
ef416fc2 | 1297 | return (0); |
1298 | ||
a4d04587 | 1299 | DEBUG_printf(("httpRead2: reading %d bytes from socket...\n", length)); |
ef416fc2 | 1300 | |
1301 | while ((bytes = recv(http->fd, buffer, length, 0)) < 0) | |
1302 | if (errno != EINTR) | |
1303 | break; | |
1304 | ||
a4d04587 | 1305 | DEBUG_printf(("httpRead2: read %d bytes from socket...\n", bytes)); |
ef416fc2 | 1306 | } |
1307 | ||
1308 | if (bytes > 0) | |
1309 | { | |
1310 | http->data_remaining -= bytes; | |
1311 | ||
1312 | if (http->data_remaining <= INT_MAX) | |
1313 | http->_data_remaining = (int)http->data_remaining; | |
1314 | else | |
1315 | http->_data_remaining = INT_MAX; | |
1316 | } | |
1317 | else if (bytes < 0) | |
1318 | { | |
1319 | #ifdef WIN32 | |
1320 | http->error = WSAGetLastError(); | |
1321 | #else | |
1322 | if (errno == EINTR) | |
1323 | bytes = 0; | |
1324 | else | |
1325 | http->error = errno; | |
1326 | #endif /* WIN32 */ | |
1327 | } | |
1328 | else | |
1329 | { | |
1330 | http->error = EPIPE; | |
1331 | return (0); | |
1332 | } | |
1333 | ||
1334 | if (http->data_remaining == 0) | |
1335 | { | |
1336 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
1337 | httpGets(len, sizeof(len), http); | |
1338 | ||
1339 | if (http->data_encoding != HTTP_ENCODE_CHUNKED) | |
1340 | { | |
1341 | if (http->state == HTTP_POST_RECV) | |
1342 | http->state ++; | |
1343 | else | |
1344 | http->state = HTTP_WAITING; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | #ifdef DEBUG | |
1349 | { | |
1350 | int i, j, ch; | |
a4d04587 | 1351 | printf("httpRead2: Read %d bytes:\n", bytes); |
ef416fc2 | 1352 | for (i = 0; i < bytes; i += 16) |
1353 | { | |
1354 | printf(" "); | |
1355 | ||
1356 | for (j = 0; j < 16 && (i + j) < bytes; j ++) | |
1357 | printf(" %02X", buffer[i + j] & 255); | |
1358 | ||
1359 | while (j < 16) | |
1360 | { | |
1361 | printf(" "); | |
1362 | j ++; | |
1363 | } | |
1364 | ||
1365 | printf(" "); | |
1366 | for (j = 0; j < 16 && (i + j) < bytes; j ++) | |
1367 | { | |
1368 | ch = buffer[i + j] & 255; | |
1369 | ||
fa73b229 | 1370 | if (ch < ' ' || ch >= 127) |
ef416fc2 | 1371 | ch = '.'; |
1372 | ||
1373 | putchar(ch); | |
1374 | } | |
1375 | putchar('\n'); | |
1376 | } | |
1377 | } | |
1378 | #endif /* DEBUG */ | |
1379 | ||
1380 | return (bytes); | |
1381 | } | |
1382 | ||
1383 | ||
1384 | #if defined(HAVE_SSL) && defined(HAVE_CDSASSL) | |
1385 | /* | |
1386 | * '_httpReadCDSA()' - Read function for CDSA decryption code. | |
1387 | */ | |
1388 | ||
1389 | OSStatus /* O - -1 on error, 0 on success */ | |
1390 | _httpReadCDSA( | |
1391 | SSLConnectionRef connection, /* I - SSL/TLS connection */ | |
1392 | void *data, /* I - Data buffer */ | |
1393 | size_t *dataLength) /* IO - Number of bytes */ | |
1394 | { | |
e53920b9 | 1395 | OSStatus result; /* Return value */ |
1396 | ssize_t bytes; /* Number of bytes read */ | |
1397 | cdsa_conn_ref_t u; /* Connection reference union */ | |
1398 | ||
1399 | ||
1400 | u.connection = connection; | |
ef416fc2 | 1401 | |
b423cd4c | 1402 | do |
e53920b9 | 1403 | bytes = recv(u.sock, data, *dataLength, 0); |
b423cd4c | 1404 | while (bytes == -1 && errno == EINTR); |
fa73b229 | 1405 | |
b423cd4c | 1406 | if (bytes == *dataLength) |
1407 | result = 0; | |
1408 | else if (bytes > 0) | |
1409 | { | |
1410 | *dataLength = bytes; | |
1411 | result = errSSLWouldBlock; | |
1412 | } | |
1413 | else | |
1414 | { | |
1415 | *dataLength = 0; | |
fa73b229 | 1416 | |
1417 | if (bytes == 0) | |
ed486911 | 1418 | result = errSSLClosedGraceful; |
b423cd4c | 1419 | else if (errno == EAGAIN) |
1420 | result = errSSLWouldBlock; | |
b423cd4c | 1421 | else |
ed486911 | 1422 | result = errSSLClosedAbort; |
ef416fc2 | 1423 | } |
b423cd4c | 1424 | |
1425 | return result; | |
ef416fc2 | 1426 | } |
1427 | #endif /* HAVE_SSL && HAVE_CDSASSL */ | |
1428 | ||
1429 | ||
1430 | /* | |
ecdc0628 | 1431 | * 'httpReconnect()' - Reconnect to a HTTP server. |
ef416fc2 | 1432 | */ |
1433 | ||
1434 | int /* O - 0 on success, non-zero on failure */ | |
ecdc0628 | 1435 | httpReconnect(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 1436 | { |
1437 | http_addrlist_t *addr; /* Connected address */ | |
1438 | ||
1439 | ||
1440 | DEBUG_printf(("httpReconnect(http=%p)\n", http)); | |
1441 | ||
1442 | if (!http) | |
1443 | return (-1); | |
1444 | ||
1445 | #ifdef HAVE_SSL | |
1446 | if (http->tls) | |
1447 | http_shutdown_ssl(http); | |
1448 | #endif /* HAVE_SSL */ | |
1449 | ||
1450 | /* | |
1451 | * Close any previously open socket... | |
1452 | */ | |
1453 | ||
1454 | if (http->fd >= 0) | |
bd7854cb | 1455 | { |
ef416fc2 | 1456 | #ifdef WIN32 |
1457 | closesocket(http->fd); | |
1458 | #else | |
1459 | close(http->fd); | |
1460 | #endif /* WIN32 */ | |
1461 | ||
bd7854cb | 1462 | http->fd = -1; |
1463 | } | |
1464 | ||
ef416fc2 | 1465 | /* |
1466 | * Connect to the server... | |
1467 | */ | |
1468 | ||
1469 | if ((addr = httpAddrConnect(http->addrlist, &(http->fd))) == NULL) | |
1470 | { | |
1471 | /* | |
1472 | * Unable to connect... | |
1473 | */ | |
1474 | ||
1475 | #ifdef WIN32 | |
1476 | http->error = WSAGetLastError(); | |
1477 | #else | |
1478 | http->error = errno; | |
1479 | #endif /* WIN32 */ | |
1480 | http->status = HTTP_ERROR; | |
1481 | ||
1482 | return (-1); | |
1483 | } | |
1484 | ||
1485 | http->hostaddr = &(addr->addr); | |
1486 | http->error = 0; | |
1487 | http->status = HTTP_CONTINUE; | |
1488 | ||
1489 | #ifdef HAVE_SSL | |
1490 | if (http->encryption == HTTP_ENCRYPT_ALWAYS) | |
1491 | { | |
1492 | /* | |
1493 | * Always do encryption via SSL. | |
1494 | */ | |
1495 | ||
1496 | if (http_setup_ssl(http) != 0) | |
1497 | { | |
1498 | #ifdef WIN32 | |
1499 | closesocket(http->fd); | |
1500 | #else | |
1501 | close(http->fd); | |
1502 | #endif /* WIN32 */ | |
1503 | ||
1504 | return (-1); | |
1505 | } | |
1506 | } | |
1507 | else if (http->encryption == HTTP_ENCRYPT_REQUIRED) | |
1508 | return (http_upgrade(http)); | |
1509 | #endif /* HAVE_SSL */ | |
1510 | ||
1511 | return (0); | |
1512 | } | |
1513 | ||
1514 | ||
1515 | /* | |
1516 | * 'httpSetCookie()' - Set the cookie value(s)... | |
1517 | * | |
1518 | * @since CUPS 1.1.19@ | |
1519 | */ | |
1520 | ||
1521 | void | |
1522 | httpSetCookie(http_t *http, /* I - Connection */ | |
1523 | const char *cookie) /* I - Cookie string */ | |
1524 | { | |
1525 | if (!http) | |
1526 | return; | |
1527 | ||
1528 | if (http->cookie) | |
1529 | free(http->cookie); | |
1530 | ||
1531 | if (cookie) | |
1532 | http->cookie = strdup(cookie); | |
1533 | else | |
1534 | http->cookie = NULL; | |
1535 | } | |
1536 | ||
1537 | ||
b423cd4c | 1538 | /* |
1539 | * 'httpSetExpect()' - Set the Expect: header in a request. | |
1540 | * | |
1541 | * Currently only HTTP_CONTINUE is supported for the "expect" argument. | |
1542 | * | |
1543 | * @since CUPS 1.2@ | |
1544 | */ | |
1545 | ||
1546 | void | |
1547 | httpSetExpect(http_t *http, /* I - HTTP connection */ | |
1548 | http_status_t expect) /* I - HTTP status to expect (HTTP_CONTINUE) */ | |
1549 | { | |
1550 | if (http) | |
1551 | http->expect = expect; | |
1552 | } | |
1553 | ||
1554 | ||
ef416fc2 | 1555 | /* |
1556 | * 'httpSetField()' - Set the value of an HTTP header. | |
1557 | */ | |
1558 | ||
1559 | void | |
ecdc0628 | 1560 | httpSetField(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1561 | http_field_t field, /* I - Field index */ |
1562 | const char *value) /* I - Value */ | |
1563 | { | |
1564 | if (http == NULL || | |
1565 | field < HTTP_FIELD_ACCEPT_LANGUAGE || | |
1566 | field > HTTP_FIELD_WWW_AUTHENTICATE || | |
1567 | value == NULL) | |
1568 | return; | |
1569 | ||
1570 | strlcpy(http->fields[field], value, HTTP_MAX_VALUE); | |
1571 | } | |
1572 | ||
1573 | ||
1574 | /* | |
1575 | * 'httpSetLength()' - Set the content-length and content-encoding. | |
1576 | * | |
1577 | * @since CUPS 1.2@ | |
1578 | */ | |
1579 | ||
1580 | void | |
ecdc0628 | 1581 | httpSetLength(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1582 | size_t length) /* I - Length (0 for chunked) */ |
1583 | { | |
1584 | if (!http) | |
1585 | return; | |
1586 | ||
1587 | if (!length) | |
1588 | { | |
1589 | strcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"); | |
1590 | http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0'; | |
1591 | } | |
1592 | else | |
1593 | { | |
1594 | http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0'; | |
1595 | snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE, | |
1596 | CUPS_LLFMT, CUPS_LLCAST length); | |
1597 | } | |
1598 | } | |
1599 | ||
1600 | ||
1601 | /* | |
1602 | * 'httpTrace()' - Send an TRACE request to the server. | |
1603 | */ | |
1604 | ||
1605 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1606 | httpTrace(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1607 | const char *uri) /* I - URI for trace */ |
1608 | { | |
1609 | return (http_send(http, HTTP_TRACE, uri)); | |
1610 | } | |
1611 | ||
1612 | ||
1613 | /* | |
1614 | * 'httpUpdate()' - Update the current HTTP state for incoming data. | |
1615 | */ | |
1616 | ||
1617 | http_status_t /* O - HTTP status */ | |
ecdc0628 | 1618 | httpUpdate(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 1619 | { |
1620 | char line[32768], /* Line from connection... */ | |
1621 | *value; /* Pointer to value on line */ | |
1622 | http_field_t field; /* Field index */ | |
1623 | int major, minor, /* HTTP version numbers */ | |
1624 | status; /* Request status */ | |
1625 | ||
1626 | ||
1627 | DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http, http->state)); | |
1628 | ||
1629 | /* | |
1630 | * Flush pending data, if any... | |
1631 | */ | |
1632 | ||
1633 | if (http->wused) | |
1634 | { | |
1635 | DEBUG_puts(" flushing buffer..."); | |
1636 | ||
1637 | if (httpFlushWrite(http) < 0) | |
1638 | return (HTTP_ERROR); | |
1639 | } | |
1640 | ||
1641 | /* | |
1642 | * If we haven't issued any commands, then there is nothing to "update"... | |
1643 | */ | |
1644 | ||
1645 | if (http->state == HTTP_WAITING) | |
1646 | return (HTTP_CONTINUE); | |
1647 | ||
1648 | /* | |
1649 | * Grab all of the lines we can from the connection... | |
1650 | */ | |
1651 | ||
1652 | while (httpGets(line, sizeof(line), http) != NULL) | |
1653 | { | |
1654 | DEBUG_printf(("httpUpdate: Got \"%s\"\n", line)); | |
1655 | ||
1656 | if (line[0] == '\0') | |
1657 | { | |
1658 | /* | |
1659 | * Blank line means the start of the data section (if any). Return | |
1660 | * the result code, too... | |
1661 | * | |
1662 | * If we get status 100 (HTTP_CONTINUE), then we *don't* change states. | |
1663 | * Instead, we just return HTTP_CONTINUE to the caller and keep on | |
1664 | * tryin'... | |
1665 | */ | |
1666 | ||
1667 | if (http->status == HTTP_CONTINUE) | |
1668 | return (http->status); | |
1669 | ||
1670 | if (http->status < HTTP_BAD_REQUEST) | |
1671 | http->digest_tries = 0; | |
1672 | ||
1673 | #ifdef HAVE_SSL | |
1674 | if (http->status == HTTP_SWITCHING_PROTOCOLS && !http->tls) | |
1675 | { | |
1676 | if (http_setup_ssl(http) != 0) | |
1677 | { | |
1678 | # ifdef WIN32 | |
1679 | closesocket(http->fd); | |
1680 | # else | |
1681 | close(http->fd); | |
1682 | # endif /* WIN32 */ | |
1683 | ||
1684 | return (HTTP_ERROR); | |
1685 | } | |
1686 | ||
1687 | return (HTTP_CONTINUE); | |
1688 | } | |
1689 | #endif /* HAVE_SSL */ | |
1690 | ||
1691 | httpGetLength2(http); | |
1692 | ||
1693 | switch (http->state) | |
1694 | { | |
1695 | case HTTP_GET : | |
1696 | case HTTP_POST : | |
1697 | case HTTP_POST_RECV : | |
1698 | case HTTP_PUT : | |
1699 | http->state ++; | |
1700 | case HTTP_POST_SEND : | |
e1d6a774 | 1701 | case HTTP_HEAD : |
ef416fc2 | 1702 | break; |
1703 | ||
1704 | default : | |
1705 | http->state = HTTP_WAITING; | |
1706 | break; | |
1707 | } | |
1708 | ||
1709 | return (http->status); | |
1710 | } | |
1711 | else if (strncmp(line, "HTTP/", 5) == 0) | |
1712 | { | |
1713 | /* | |
1714 | * Got the beginning of a response... | |
1715 | */ | |
1716 | ||
1717 | if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &status) != 3) | |
1718 | return (HTTP_ERROR); | |
1719 | ||
1720 | http->version = (http_version_t)(major * 100 + minor); | |
1721 | http->status = (http_status_t)status; | |
1722 | } | |
1723 | else if ((value = strchr(line, ':')) != NULL) | |
1724 | { | |
1725 | /* | |
1726 | * Got a value... | |
1727 | */ | |
1728 | ||
1729 | *value++ = '\0'; | |
1730 | while (isspace(*value & 255)) | |
1731 | value ++; | |
1732 | ||
1733 | /* | |
1734 | * Be tolerants of servers that send unknown attribute fields... | |
1735 | */ | |
1736 | ||
1737 | if (!strcasecmp(line, "expect")) | |
1738 | { | |
1739 | /* | |
1740 | * "Expect: 100-continue" or similar... | |
1741 | */ | |
1742 | ||
1743 | http->expect = (http_status_t)atoi(value); | |
1744 | } | |
1745 | else if (!strcasecmp(line, "cookie")) | |
1746 | { | |
1747 | /* | |
1748 | * "Cookie: name=value[; name=value ...]" - replaces previous cookies... | |
1749 | */ | |
1750 | ||
1751 | httpSetCookie(http, value); | |
1752 | } | |
1753 | else if ((field = http_field(line)) == HTTP_FIELD_UNKNOWN) | |
1754 | { | |
1755 | DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line)); | |
1756 | continue; | |
1757 | } | |
1758 | else | |
1759 | httpSetField(http, field, value); | |
1760 | } | |
1761 | else | |
1762 | { | |
1763 | http->status = HTTP_ERROR; | |
1764 | return (HTTP_ERROR); | |
1765 | } | |
1766 | } | |
1767 | ||
1768 | /* | |
1769 | * See if there was an error... | |
1770 | */ | |
1771 | ||
1772 | if (http->error == EPIPE && http->status > HTTP_CONTINUE) | |
1773 | return (http->status); | |
1774 | ||
1775 | if (http->error) | |
1776 | { | |
1777 | DEBUG_printf(("httpUpdate: socket error %d - %s\n", http->error, | |
1778 | strerror(http->error))); | |
1779 | http->status = HTTP_ERROR; | |
1780 | return (HTTP_ERROR); | |
1781 | } | |
1782 | ||
1783 | /* | |
1784 | * If we haven't already returned, then there is nothing new... | |
1785 | */ | |
1786 | ||
1787 | return (HTTP_CONTINUE); | |
1788 | } | |
1789 | ||
1790 | ||
1791 | /* | |
1792 | * 'httpWait()' - Wait for data available on a connection. | |
1793 | * | |
1794 | * @since CUPS 1.1.19@ | |
1795 | */ | |
1796 | ||
1797 | int /* O - 1 if data is available, 0 otherwise */ | |
ecdc0628 | 1798 | httpWait(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1799 | int msec) /* I - Milliseconds to wait */ |
1800 | { | |
1801 | /* | |
1802 | * First see if there is data in the buffer... | |
1803 | */ | |
1804 | ||
1805 | if (http == NULL) | |
1806 | return (0); | |
1807 | ||
1808 | if (http->used) | |
1809 | return (1); | |
1810 | ||
1811 | /* | |
1812 | * If not, check the SSL/TLS buffers and do a select() on the connection... | |
1813 | */ | |
1814 | ||
1815 | return (http_wait(http, msec)); | |
1816 | } | |
1817 | ||
1818 | ||
1819 | /* | |
1820 | * 'httpWrite()' - Write data to a HTTP connection. | |
a4d04587 | 1821 | * |
1822 | * This function is deprecated. Use the httpWrite2() function which can | |
1823 | * write more than 2GB of data. | |
1824 | * | |
1825 | * @deprecated@ | |
ef416fc2 | 1826 | */ |
1827 | ||
1828 | int /* O - Number of bytes written */ | |
ecdc0628 | 1829 | httpWrite(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1830 | const char *buffer, /* I - Buffer for data */ |
1831 | int length) /* I - Number of bytes to write */ | |
1832 | { | |
a4d04587 | 1833 | return ((int)httpWrite2(http, buffer, length)); |
1834 | } | |
1835 | ||
1836 | ||
1837 | /* | |
1838 | * 'httpWrite2()' - Write data to a HTTP connection. | |
ecdc0628 | 1839 | * |
1840 | * @since CUPS 1.2@ | |
a4d04587 | 1841 | */ |
1842 | ||
1843 | ssize_t /* O - Number of bytes written */ | |
ecdc0628 | 1844 | httpWrite2(http_t *http, /* I - HTTP connection */ |
a4d04587 | 1845 | const char *buffer, /* I - Buffer for data */ |
1846 | size_t length) /* I - Number of bytes to write */ | |
1847 | { | |
1848 | ssize_t bytes; /* Bytes written */ | |
ef416fc2 | 1849 | |
1850 | ||
1851 | DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http, | |
1852 | buffer, length)); | |
1853 | ||
1854 | /* | |
1855 | * Range check input... | |
1856 | */ | |
1857 | ||
1858 | if (http == NULL || buffer == NULL) | |
1859 | return (-1); | |
1860 | ||
1861 | /* | |
1862 | * Mark activity on the connection... | |
1863 | */ | |
1864 | ||
1865 | http->activity = time(NULL); | |
1866 | ||
1867 | /* | |
1868 | * Buffer small writes for better performance... | |
1869 | */ | |
1870 | ||
1871 | if (length > 0) | |
1872 | { | |
1873 | if (http->wused && (length + http->wused) > sizeof(http->wbuffer)) | |
1874 | { | |
1875 | DEBUG_printf((" flushing buffer (wused=%d, length=%d)\n", | |
1876 | http->wused, length)); | |
1877 | ||
1878 | httpFlushWrite(http); | |
1879 | } | |
1880 | ||
1881 | if ((length + http->wused) <= sizeof(http->wbuffer)) | |
1882 | { | |
1883 | /* | |
1884 | * Write to buffer... | |
1885 | */ | |
1886 | ||
1887 | DEBUG_printf((" copying %d bytes to wbuffer...\n", length)); | |
1888 | ||
1889 | memcpy(http->wbuffer + http->wused, buffer, length); | |
1890 | http->wused += length; | |
1891 | bytes = length; | |
1892 | } | |
1893 | else | |
1894 | { | |
1895 | /* | |
1896 | * Otherwise write the data directly... | |
1897 | */ | |
1898 | ||
1899 | DEBUG_printf((" writing %d bytes to socket...\n", length)); | |
1900 | ||
1901 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
1902 | bytes = http_write_chunk(http, buffer, length); | |
1903 | else | |
1904 | bytes = http_write(http, buffer, length); | |
1905 | ||
1906 | DEBUG_printf((" wrote %d bytes...\n", bytes)); | |
1907 | } | |
1908 | ||
1909 | if (http->data_encoding == HTTP_ENCODE_LENGTH) | |
1910 | http->data_remaining -= bytes; | |
1911 | } | |
1912 | else | |
1913 | bytes = 0; | |
1914 | ||
1915 | /* | |
1916 | * Handle end-of-request processing... | |
1917 | */ | |
1918 | ||
1919 | if ((http->data_encoding == HTTP_ENCODE_CHUNKED && length == 0) || | |
1920 | (http->data_encoding == HTTP_ENCODE_LENGTH && http->data_remaining == 0)) | |
1921 | { | |
1922 | /* | |
1923 | * Finished with the transfer; unless we are sending POST or PUT | |
1924 | * data, go idle... | |
1925 | */ | |
1926 | ||
1927 | DEBUG_puts("httpWrite: changing states..."); | |
1928 | ||
1929 | if (http->wused) | |
1930 | httpFlushWrite(http); | |
1931 | ||
1932 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
1933 | { | |
1934 | /* | |
1935 | * Send a 0-length chunk at the end of the request... | |
1936 | */ | |
1937 | ||
1938 | http_write(http, "0\r\n\r\n", 5); | |
1939 | ||
1940 | /* | |
1941 | * Reset the data state... | |
1942 | */ | |
1943 | ||
1944 | http->data_encoding = HTTP_ENCODE_LENGTH; | |
1945 | http->data_remaining = 0; | |
1946 | } | |
1947 | ||
1948 | if (http->state == HTTP_POST_RECV) | |
1949 | http->state ++; | |
1950 | else if (http->state == HTTP_PUT_RECV) | |
1951 | http->state = HTTP_STATUS; | |
1952 | else | |
1953 | http->state = HTTP_WAITING; | |
1954 | } | |
1955 | ||
1956 | return (bytes); | |
1957 | } | |
1958 | ||
1959 | ||
1960 | #if defined(HAVE_SSL) && defined(HAVE_CDSASSL) | |
1961 | /* | |
1962 | * '_httpWriteCDSA()' - Write function for CDSA encryption code. | |
1963 | */ | |
1964 | ||
1965 | OSStatus /* O - -1 on error, 0 on success */ | |
1966 | _httpWriteCDSA( | |
1967 | SSLConnectionRef connection, /* I - SSL/TLS connection */ | |
1968 | const void *data, /* I - Data buffer */ | |
1969 | size_t *dataLength) /* IO - Number of bytes */ | |
1970 | { | |
e53920b9 | 1971 | OSStatus result; /* Return value */ |
1972 | ssize_t bytes; /* Number of bytes read */ | |
1973 | cdsa_conn_ref_t u; /* Connection reference union */ | |
1974 | ||
1975 | ||
1976 | u.connection = connection; | |
ef416fc2 | 1977 | |
b423cd4c | 1978 | do |
e53920b9 | 1979 | bytes = write(u.sock, data, *dataLength); |
b423cd4c | 1980 | while (bytes == -1 && errno == EINTR); |
fa73b229 | 1981 | |
b423cd4c | 1982 | if (bytes == *dataLength) |
1983 | result = 0; | |
1984 | else if (bytes >= 0) | |
1985 | { | |
1986 | *dataLength = bytes; | |
1987 | result = errSSLWouldBlock; | |
1988 | } | |
1989 | else | |
1990 | { | |
1991 | *dataLength = 0; | |
1992 | ||
fa73b229 | 1993 | if (errno == EAGAIN) |
b423cd4c | 1994 | result = errSSLWouldBlock; |
b423cd4c | 1995 | else |
ed486911 | 1996 | result = errSSLClosedAbort; |
ef416fc2 | 1997 | } |
b423cd4c | 1998 | |
1999 | return result; | |
ef416fc2 | 2000 | } |
2001 | #endif /* HAVE_SSL && HAVE_CDSASSL */ | |
2002 | ||
2003 | ||
2004 | /* | |
2005 | * 'http_field()' - Return the field index for a field name. | |
2006 | */ | |
2007 | ||
2008 | static http_field_t /* O - Field index */ | |
2009 | http_field(const char *name) /* I - String name */ | |
2010 | { | |
2011 | int i; /* Looping var */ | |
2012 | ||
2013 | ||
2014 | for (i = 0; i < HTTP_FIELD_MAX; i ++) | |
2015 | if (strcasecmp(name, http_fields[i]) == 0) | |
2016 | return ((http_field_t)i); | |
2017 | ||
2018 | return (HTTP_FIELD_UNKNOWN); | |
2019 | } | |
2020 | ||
2021 | ||
2022 | #ifdef HAVE_SSL | |
2023 | /* | |
2024 | * 'http_read_ssl()' - Read from a SSL/TLS connection. | |
2025 | */ | |
2026 | ||
2027 | static int /* O - Bytes read */ | |
ecdc0628 | 2028 | http_read_ssl(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2029 | char *buf, /* I - Buffer to store data */ |
2030 | int len) /* I - Length of buffer */ | |
2031 | { | |
2032 | # if defined(HAVE_LIBSSL) | |
2033 | return (SSL_read((SSL *)(http->tls), buf, len)); | |
2034 | ||
2035 | # elif defined(HAVE_GNUTLS) | |
2036 | return (gnutls_record_recv(((http_tls_t *)(http->tls))->session, buf, len)); | |
2037 | ||
2038 | # elif defined(HAVE_CDSASSL) | |
fa73b229 | 2039 | int result; /* Return value */ |
ef416fc2 | 2040 | OSStatus error; /* Error info */ |
2041 | size_t processed; /* Number of bytes processed */ | |
2042 | ||
2043 | ||
89d46774 | 2044 | error = SSLRead(((http_tls_t *)http->tls)->session, buf, len, &processed); |
ef416fc2 | 2045 | |
fa73b229 | 2046 | switch (error) |
ef416fc2 | 2047 | { |
fa73b229 | 2048 | case 0 : |
2049 | result = (int)processed; | |
2050 | break; | |
2051 | case errSSLClosedGraceful : | |
2052 | result = 0; | |
2053 | break; | |
2054 | case errSSLWouldBlock : | |
b423cd4c | 2055 | if (processed) |
2056 | result = (int)processed; | |
2057 | else | |
2058 | { | |
2059 | result = -1; | |
2060 | errno = EINTR; | |
2061 | } | |
fa73b229 | 2062 | break; |
2063 | default : | |
2064 | errno = EPIPE; | |
2065 | result = -1; | |
2066 | break; | |
ef416fc2 | 2067 | } |
fa73b229 | 2068 | |
2069 | return (result); | |
ef416fc2 | 2070 | # endif /* HAVE_LIBSSL */ |
2071 | } | |
2072 | #endif /* HAVE_SSL */ | |
2073 | ||
2074 | ||
2075 | /* | |
2076 | * 'http_send()' - Send a request with all fields and the trailing blank line. | |
2077 | */ | |
2078 | ||
2079 | static int /* O - 0 on success, non-zero on error */ | |
ecdc0628 | 2080 | http_send(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2081 | http_state_t request, /* I - Request code */ |
2082 | const char *uri) /* I - URI */ | |
2083 | { | |
2084 | int i; /* Looping var */ | |
2085 | char *ptr, /* Pointer in buffer */ | |
2086 | buf[1024]; /* Encoded URI buffer */ | |
2087 | static const char * const codes[] = | |
2088 | { /* Request code strings */ | |
2089 | NULL, | |
2090 | "OPTIONS", | |
2091 | "GET", | |
2092 | NULL, | |
2093 | "HEAD", | |
2094 | "POST", | |
2095 | NULL, | |
2096 | NULL, | |
2097 | "PUT", | |
2098 | NULL, | |
2099 | "DELETE", | |
2100 | "TRACE", | |
2101 | "CLOSE" | |
2102 | }; | |
2103 | static const char hex[] = "0123456789ABCDEF"; | |
2104 | /* Hex digits */ | |
2105 | ||
2106 | ||
2107 | DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n", | |
2108 | http, codes[request], uri)); | |
2109 | ||
2110 | if (http == NULL || uri == NULL) | |
2111 | return (-1); | |
2112 | ||
2113 | /* | |
2114 | * Set the User-Agent field if it isn't already... | |
2115 | */ | |
2116 | ||
2117 | if (!http->fields[HTTP_FIELD_USER_AGENT][0]) | |
2118 | httpSetField(http, HTTP_FIELD_USER_AGENT, CUPS_MINIMAL); | |
2119 | ||
2120 | /* | |
2121 | * Encode the URI as needed... | |
2122 | */ | |
2123 | ||
2124 | for (ptr = buf; *uri != '\0' && ptr < (buf + sizeof(buf) - 1); uri ++) | |
2125 | if (*uri <= ' ' || *uri >= 127) | |
2126 | { | |
2127 | if (ptr < (buf + sizeof(buf) - 1)) | |
2128 | *ptr ++ = '%'; | |
2129 | if (ptr < (buf + sizeof(buf) - 1)) | |
2130 | *ptr ++ = hex[(*uri >> 4) & 15]; | |
2131 | if (ptr < (buf + sizeof(buf) - 1)) | |
2132 | *ptr ++ = hex[*uri & 15]; | |
2133 | } | |
2134 | else | |
2135 | *ptr ++ = *uri; | |
2136 | ||
2137 | *ptr = '\0'; | |
2138 | ||
2139 | /* | |
2140 | * See if we had an error the last time around; if so, reconnect... | |
2141 | */ | |
2142 | ||
2143 | if (http->status == HTTP_ERROR || http->status >= HTTP_BAD_REQUEST) | |
fa73b229 | 2144 | if (httpReconnect(http)) |
2145 | return (-1); | |
ef416fc2 | 2146 | |
2147 | /* | |
2148 | * Send the request header... | |
2149 | */ | |
2150 | ||
2151 | http->state = request; | |
2152 | if (request == HTTP_POST || request == HTTP_PUT) | |
2153 | http->state ++; | |
2154 | ||
2155 | http->status = HTTP_CONTINUE; | |
2156 | ||
2157 | #ifdef HAVE_SSL | |
2158 | if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls) | |
2159 | { | |
2160 | httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); | |
2161 | httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0,SSL/2.0,SSL/3.0"); | |
2162 | } | |
2163 | #endif /* HAVE_SSL */ | |
2164 | ||
2165 | if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1) | |
2166 | { | |
2167 | http->status = HTTP_ERROR; | |
2168 | return (-1); | |
2169 | } | |
2170 | ||
2171 | for (i = 0; i < HTTP_FIELD_MAX; i ++) | |
2172 | if (http->fields[i][0] != '\0') | |
2173 | { | |
2174 | DEBUG_printf(("%s: %s\n", http_fields[i], http->fields[i])); | |
2175 | ||
2176 | if (httpPrintf(http, "%s: %s\r\n", http_fields[i], http->fields[i]) < 1) | |
2177 | { | |
2178 | http->status = HTTP_ERROR; | |
2179 | return (-1); | |
2180 | } | |
2181 | } | |
2182 | ||
2183 | if (http->cookie) | |
2184 | if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1) | |
2185 | { | |
2186 | http->status = HTTP_ERROR; | |
2187 | return (-1); | |
2188 | } | |
2189 | ||
b423cd4c | 2190 | if (http->expect == HTTP_CONTINUE && |
2191 | (http->state == HTTP_POST_RECV || http->state == HTTP_PUT_RECV)) | |
2192 | if (httpPrintf(http, "Expect: 100-continue\r\n") < 1) | |
2193 | { | |
2194 | http->status = HTTP_ERROR; | |
2195 | return (-1); | |
2196 | } | |
2197 | ||
ef416fc2 | 2198 | if (httpPrintf(http, "\r\n") < 1) |
2199 | { | |
2200 | http->status = HTTP_ERROR; | |
2201 | return (-1); | |
2202 | } | |
2203 | ||
2204 | httpGetLength2(http); | |
2205 | httpClearFields(http); | |
2206 | ||
2207 | return (0); | |
2208 | } | |
2209 | ||
2210 | ||
2211 | #ifdef HAVE_SSL | |
2212 | /* | |
2213 | * 'http_setup_ssl()' - Set up SSL/TLS support on a connection. | |
2214 | */ | |
2215 | ||
2216 | static int /* O - Status of connection */ | |
ecdc0628 | 2217 | http_setup_ssl(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 2218 | { |
2219 | # ifdef HAVE_LIBSSL | |
89d46774 | 2220 | SSL_CTX *context; /* Context for encryption */ |
2221 | SSL *conn; /* Connection for encryption */ | |
ef416fc2 | 2222 | # elif defined(HAVE_GNUTLS) |
89d46774 | 2223 | http_tls_t *conn; /* TLS session object */ |
ef416fc2 | 2224 | gnutls_certificate_client_credentials *credentials; |
89d46774 | 2225 | /* TLS credentials */ |
ef416fc2 | 2226 | # elif defined(HAVE_CDSASSL) |
89d46774 | 2227 | OSStatus error; /* Error code */ |
2228 | http_tls_t *conn; /* CDSA connection information */ | |
2229 | cdsa_conn_ref_t u; /* Connection reference union */ | |
ef416fc2 | 2230 | # endif /* HAVE_LIBSSL */ |
2231 | ||
2232 | ||
2233 | DEBUG_printf(("http_setup_ssl(http=%p)\n", http)); | |
2234 | ||
2235 | # ifdef HAVE_LIBSSL | |
2236 | context = SSL_CTX_new(SSLv23_client_method()); | |
2237 | ||
2238 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */ | |
2239 | ||
2240 | conn = SSL_new(context); | |
2241 | ||
2242 | SSL_set_fd(conn, http->fd); | |
2243 | if (SSL_connect(conn) != 1) | |
2244 | { | |
2245 | # ifdef DEBUG | |
2246 | unsigned long error; /* Error code */ | |
2247 | ||
2248 | while ((error = ERR_get_error()) != 0) | |
2249 | printf("http_setup_ssl: %s\n", ERR_error_string(error, NULL)); | |
2250 | # endif /* DEBUG */ | |
2251 | ||
2252 | SSL_CTX_free(context); | |
2253 | SSL_free(conn); | |
2254 | ||
2255 | # ifdef WIN32 | |
2256 | http->error = WSAGetLastError(); | |
2257 | # else | |
2258 | http->error = errno; | |
2259 | # endif /* WIN32 */ | |
2260 | http->status = HTTP_ERROR; | |
2261 | ||
2262 | return (HTTP_ERROR); | |
2263 | } | |
2264 | ||
2265 | # elif defined(HAVE_GNUTLS) | |
89d46774 | 2266 | if ((conn = (http_tls_t *)malloc(sizeof(http_tls_t))) == NULL) |
ef416fc2 | 2267 | { |
2268 | http->error = errno; | |
2269 | http->status = HTTP_ERROR; | |
2270 | ||
2271 | return (-1); | |
2272 | } | |
2273 | ||
2274 | credentials = (gnutls_certificate_client_credentials *) | |
2275 | malloc(sizeof(gnutls_certificate_client_credentials)); | |
2276 | if (credentials == NULL) | |
2277 | { | |
2278 | free(conn); | |
2279 | ||
2280 | http->error = errno; | |
2281 | http->status = HTTP_ERROR; | |
2282 | ||
2283 | return (-1); | |
2284 | } | |
2285 | ||
2286 | gnutls_certificate_allocate_credentials(credentials); | |
2287 | ||
2288 | gnutls_init(&(conn->session), GNUTLS_CLIENT); | |
2289 | gnutls_set_default_priority(conn->session); | |
2290 | gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials); | |
e1d6a774 | 2291 | gnutls_transport_set_ptr(conn->session, |
2292 | (gnutls_transport_ptr)((long)http->fd)); | |
ef416fc2 | 2293 | |
2294 | if ((gnutls_handshake(conn->session)) != GNUTLS_E_SUCCESS) | |
2295 | { | |
2296 | http->error = errno; | |
2297 | http->status = HTTP_ERROR; | |
2298 | ||
2299 | return (-1); | |
2300 | } | |
2301 | ||
2302 | conn->credentials = credentials; | |
2303 | ||
2304 | # elif defined(HAVE_CDSASSL) | |
89d46774 | 2305 | conn = (http_tls_t *)calloc(1, sizeof(http_tls_t)); |
e53920b9 | 2306 | |
89d46774 | 2307 | if (conn == NULL) |
2308 | return (-1); | |
e53920b9 | 2309 | |
89d46774 | 2310 | if ((error = SSLNewContext(false, &conn->session))) |
e53920b9 | 2311 | { |
89d46774 | 2312 | http->error = error; |
2313 | http->status = HTTP_ERROR; | |
e53920b9 | 2314 | |
89d46774 | 2315 | free(conn); |
2316 | return (-1); | |
e53920b9 | 2317 | } |
ef416fc2 | 2318 | |
89d46774 | 2319 | /* |
2320 | * Use a union to resolve warnings about int/pointer size mismatches... | |
2321 | */ | |
2322 | ||
2323 | u.connection = NULL; | |
2324 | u.sock = http->fd; | |
2325 | error = SSLSetConnection(conn->session, u.connection); | |
2326 | ||
2327 | if (!error) | |
2328 | error = SSLSetIOFuncs(conn->session, _httpReadCDSA, _httpWriteCDSA); | |
2329 | ||
ef416fc2 | 2330 | if (!error) |
89d46774 | 2331 | error = SSLSetAllowsExpiredCerts(conn->session, true); |
ef416fc2 | 2332 | |
2333 | if (!error) | |
89d46774 | 2334 | error = SSLSetAllowsAnyRoot(conn->session, true); |
ef416fc2 | 2335 | |
ed486911 | 2336 | if (!error) |
2337 | error = SSLSetProtocolVersionEnabled(conn->session, kSSLProtocol2, false); | |
2338 | ||
ef416fc2 | 2339 | if (!error) |
b423cd4c | 2340 | { |
89d46774 | 2341 | while ((error = SSLHandshake(conn->session)) == errSSLWouldBlock) |
b423cd4c | 2342 | usleep(1000); |
2343 | } | |
ef416fc2 | 2344 | |
89d46774 | 2345 | if (error) |
ef416fc2 | 2346 | { |
2347 | http->error = error; | |
2348 | http->status = HTTP_ERROR; | |
2349 | ||
89d46774 | 2350 | SSLDisposeContext(conn->session); |
ef416fc2 | 2351 | |
89d46774 | 2352 | free(conn); |
ef416fc2 | 2353 | |
2354 | return (-1); | |
2355 | } | |
2356 | # endif /* HAVE_CDSASSL */ | |
2357 | ||
2358 | http->tls = conn; | |
2359 | return (0); | |
2360 | } | |
2361 | #endif /* HAVE_SSL */ | |
2362 | ||
2363 | ||
2364 | #ifdef HAVE_SSL | |
2365 | /* | |
2366 | * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection. | |
2367 | */ | |
2368 | ||
2369 | static void | |
89d46774 | 2370 | http_shutdown_ssl(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 2371 | { |
2372 | # ifdef HAVE_LIBSSL | |
89d46774 | 2373 | SSL_CTX *context; /* Context for encryption */ |
2374 | SSL *conn; /* Connection for encryption */ | |
ef416fc2 | 2375 | |
2376 | ||
2377 | conn = (SSL *)(http->tls); | |
2378 | context = SSL_get_SSL_CTX(conn); | |
2379 | ||
2380 | SSL_shutdown(conn); | |
2381 | SSL_CTX_free(context); | |
2382 | SSL_free(conn); | |
2383 | ||
2384 | # elif defined(HAVE_GNUTLS) | |
89d46774 | 2385 | http_tls_t *conn; /* Encryption session */ |
ef416fc2 | 2386 | gnutls_certificate_client_credentials *credentials; |
89d46774 | 2387 | /* TLS credentials */ |
ef416fc2 | 2388 | |
2389 | ||
2390 | conn = (http_tls_t *)(http->tls); | |
2391 | credentials = (gnutls_certificate_client_credentials *)(conn->credentials); | |
2392 | ||
2393 | gnutls_bye(conn->session, GNUTLS_SHUT_RDWR); | |
2394 | gnutls_deinit(conn->session); | |
2395 | gnutls_certificate_free_credentials(*credentials); | |
2396 | free(credentials); | |
2397 | free(conn); | |
2398 | ||
2399 | # elif defined(HAVE_CDSASSL) | |
89d46774 | 2400 | http_tls_t *conn; /* CDSA connection information */ |
2401 | ||
2402 | ||
2403 | conn = (http_tls_t *)(http->tls); | |
2404 | ||
2405 | while (SSLClose(conn->session) == errSSLWouldBlock) | |
b423cd4c | 2406 | usleep(1000); |
2407 | ||
89d46774 | 2408 | SSLDisposeContext(conn->session); |
2409 | ||
2410 | if (conn->certsArray) | |
2411 | CFRelease(conn->certsArray); | |
2412 | ||
2413 | free(conn); | |
ef416fc2 | 2414 | # endif /* HAVE_LIBSSL */ |
2415 | ||
2416 | http->tls = NULL; | |
2417 | } | |
2418 | #endif /* HAVE_SSL */ | |
2419 | ||
2420 | ||
2421 | #ifdef HAVE_SSL | |
2422 | /* | |
2423 | * 'http_upgrade()' - Force upgrade to TLS encryption. | |
2424 | */ | |
2425 | ||
89d46774 | 2426 | static int /* O - Status of connection */ |
2427 | http_upgrade(http_t *http) /* I - HTTP connection */ | |
ef416fc2 | 2428 | { |
89d46774 | 2429 | int ret; /* Return value */ |
2430 | http_t myhttp; /* Local copy of HTTP data */ | |
ef416fc2 | 2431 | |
2432 | ||
2433 | DEBUG_printf(("http_upgrade(%p)\n", http)); | |
2434 | ||
2435 | /* | |
2436 | * Copy the HTTP data to a local variable so we can do the OPTIONS | |
2437 | * request without interfering with the existing request data... | |
2438 | */ | |
2439 | ||
2440 | memcpy(&myhttp, http, sizeof(myhttp)); | |
2441 | ||
2442 | /* | |
2443 | * Send an OPTIONS request to the server, requiring SSL or TLS | |
2444 | * encryption on the link... | |
2445 | */ | |
2446 | ||
2447 | httpClearFields(&myhttp); | |
2448 | httpSetField(&myhttp, HTTP_FIELD_CONNECTION, "upgrade"); | |
2449 | httpSetField(&myhttp, HTTP_FIELD_UPGRADE, "TLS/1.0, SSL/2.0, SSL/3.0"); | |
2450 | ||
2451 | if ((ret = httpOptions(&myhttp, "*")) == 0) | |
2452 | { | |
2453 | /* | |
2454 | * Wait for the secure connection... | |
2455 | */ | |
2456 | ||
2457 | while (httpUpdate(&myhttp) == HTTP_CONTINUE); | |
2458 | } | |
2459 | ||
2460 | httpFlush(&myhttp); | |
2461 | ||
2462 | /* | |
2463 | * Copy the HTTP data back over, if any... | |
2464 | */ | |
2465 | ||
2466 | http->fd = myhttp.fd; | |
2467 | http->error = myhttp.error; | |
2468 | http->activity = myhttp.activity; | |
2469 | http->status = myhttp.status; | |
2470 | http->version = myhttp.version; | |
2471 | http->keep_alive = myhttp.keep_alive; | |
2472 | http->used = myhttp.used; | |
2473 | ||
2474 | if (http->used) | |
2475 | memcpy(http->buffer, myhttp.buffer, http->used); | |
2476 | ||
2477 | http->auth_type = myhttp.auth_type; | |
2478 | http->nonce_count = myhttp.nonce_count; | |
2479 | ||
2480 | memcpy(http->nonce, myhttp.nonce, sizeof(http->nonce)); | |
2481 | ||
2482 | http->tls = myhttp.tls; | |
2483 | http->encryption = myhttp.encryption; | |
2484 | ||
2485 | /* | |
2486 | * See if we actually went secure... | |
2487 | */ | |
2488 | ||
2489 | if (!http->tls) | |
2490 | { | |
2491 | /* | |
2492 | * Server does not support HTTP upgrade... | |
2493 | */ | |
2494 | ||
2495 | DEBUG_puts("Server does not support HTTP upgrade!"); | |
2496 | ||
2497 | # ifdef WIN32 | |
2498 | closesocket(http->fd); | |
2499 | # else | |
2500 | close(http->fd); | |
2501 | # endif | |
2502 | ||
2503 | http->fd = -1; | |
2504 | ||
2505 | return (-1); | |
2506 | } | |
2507 | else | |
2508 | return (ret); | |
2509 | } | |
2510 | #endif /* HAVE_SSL */ | |
2511 | ||
2512 | ||
2513 | /* | |
2514 | * 'http_wait()' - Wait for data available on a connection. | |
2515 | */ | |
2516 | ||
2517 | static int /* O - 1 if data is available, 0 otherwise */ | |
ecdc0628 | 2518 | http_wait(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2519 | int msec) /* I - Milliseconds to wait */ |
2520 | { | |
2521 | #ifndef WIN32 | |
2522 | struct rlimit limit; /* Runtime limit */ | |
2523 | int set_size; /* Size of select set */ | |
2524 | #endif /* !WIN32 */ | |
2525 | struct timeval timeout; /* Timeout */ | |
2526 | int nfds; /* Result from select() */ | |
2527 | ||
2528 | ||
2529 | DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http, msec)); | |
2530 | ||
fa73b229 | 2531 | if (http->fd < 0) |
2532 | return (0); | |
2533 | ||
ef416fc2 | 2534 | /* |
2535 | * Check the SSL/TLS buffers for data first... | |
2536 | */ | |
2537 | ||
2538 | #ifdef HAVE_SSL | |
2539 | if (http->tls) | |
2540 | { | |
2541 | # ifdef HAVE_LIBSSL | |
2542 | if (SSL_pending((SSL *)(http->tls))) | |
2543 | return (1); | |
2544 | # elif defined(HAVE_GNUTLS) | |
2545 | if (gnutls_record_check_pending(((http_tls_t *)(http->tls))->session)) | |
2546 | return (1); | |
2547 | # elif defined(HAVE_CDSASSL) | |
2548 | size_t bytes; /* Bytes that are available */ | |
2549 | ||
89d46774 | 2550 | if (!SSLGetBufferedReadSize(((http_tls_t *)http->tls)->session, &bytes) && bytes > 0) |
ef416fc2 | 2551 | return (1); |
2552 | # endif /* HAVE_LIBSSL */ | |
2553 | } | |
2554 | #endif /* HAVE_SSL */ | |
2555 | ||
2556 | /* | |
2557 | * Then try doing a select() to poll the socket... | |
2558 | */ | |
2559 | ||
2560 | if (!http->input_set) | |
2561 | { | |
2562 | #ifdef WIN32 | |
2563 | /* | |
2564 | * Windows has a fixed-size select() structure, different (surprise, | |
2565 | * surprise!) from all UNIX implementations. Just allocate this | |
2566 | * fixed structure... | |
2567 | */ | |
2568 | ||
2569 | http->input_set = calloc(1, sizeof(fd_set)); | |
2570 | #else | |
2571 | /* | |
2572 | * Allocate the select() input set based upon the max number of file | |
2573 | * descriptors available for this process... | |
2574 | */ | |
2575 | ||
2576 | getrlimit(RLIMIT_NOFILE, &limit); | |
2577 | ||
2578 | set_size = (limit.rlim_cur + 31) / 8 + 4; | |
2579 | if (set_size < sizeof(fd_set)) | |
2580 | set_size = sizeof(fd_set); | |
2581 | ||
2582 | http->input_set = calloc(1, set_size); | |
2583 | #endif /* WIN32 */ | |
2584 | ||
2585 | if (!http->input_set) | |
2586 | return (0); | |
2587 | } | |
2588 | ||
2589 | do | |
2590 | { | |
2591 | FD_SET(http->fd, http->input_set); | |
2592 | ||
89d46774 | 2593 | DEBUG_printf(("http_wait: msec=%d, http->fd=%d\n", msec, http->fd)); |
2594 | ||
ef416fc2 | 2595 | if (msec >= 0) |
2596 | { | |
2597 | timeout.tv_sec = msec / 1000; | |
2598 | timeout.tv_usec = (msec % 1000) * 1000; | |
2599 | ||
2600 | nfds = select(http->fd + 1, http->input_set, NULL, NULL, &timeout); | |
2601 | } | |
2602 | else | |
2603 | nfds = select(http->fd + 1, http->input_set, NULL, NULL, NULL); | |
89d46774 | 2604 | |
2605 | DEBUG_printf(("http_wait: select() returned %d...\n", nfds)); | |
ef416fc2 | 2606 | } |
2607 | #ifdef WIN32 | |
2608 | while (nfds < 0 && WSAGetLastError() == WSAEINTR); | |
2609 | #else | |
2610 | while (nfds < 0 && errno == EINTR); | |
2611 | #endif /* WIN32 */ | |
2612 | ||
2613 | FD_CLR(http->fd, http->input_set); | |
2614 | ||
89d46774 | 2615 | DEBUG_printf(("http_wait: returning with nfds=%d...\n", nfds)); |
2616 | ||
ef416fc2 | 2617 | return (nfds > 0); |
2618 | } | |
2619 | ||
2620 | ||
2621 | /* | |
2622 | * 'http_write()' - Write a buffer to a HTTP connection. | |
2623 | */ | |
2624 | ||
2625 | static int /* O - Number of bytes written */ | |
ecdc0628 | 2626 | http_write(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2627 | const char *buffer, /* I - Buffer for data */ |
2628 | int length) /* I - Number of bytes to write */ | |
2629 | { | |
2630 | int tbytes, /* Total bytes sent */ | |
2631 | bytes; /* Bytes sent */ | |
2632 | ||
2633 | ||
2634 | tbytes = 0; | |
2635 | ||
2636 | while (length > 0) | |
2637 | { | |
2638 | #ifdef HAVE_SSL | |
2639 | if (http->tls) | |
2640 | bytes = http_write_ssl(http, buffer, length); | |
2641 | else | |
2642 | #endif /* HAVE_SSL */ | |
2643 | bytes = send(http->fd, buffer, length, 0); | |
2644 | ||
2645 | if (bytes < 0) | |
2646 | { | |
2647 | #ifdef WIN32 | |
2648 | if (WSAGetLastError() != http->error) | |
2649 | { | |
2650 | http->error = WSAGetLastError(); | |
2651 | continue; | |
2652 | } | |
2653 | #else | |
2654 | if (errno == EINTR) | |
2655 | continue; | |
2656 | else if (errno != http->error && errno != ECONNRESET) | |
2657 | { | |
2658 | http->error = errno; | |
2659 | continue; | |
2660 | } | |
2661 | #endif /* WIN32 */ | |
2662 | ||
2663 | DEBUG_puts("http_write: error writing data...\n"); | |
2664 | ||
2665 | return (-1); | |
2666 | } | |
2667 | ||
2668 | buffer += bytes; | |
2669 | tbytes += bytes; | |
2670 | length -= bytes; | |
2671 | } | |
2672 | ||
2673 | #ifdef DEBUG | |
2674 | { | |
2675 | int i, j, ch; | |
2676 | printf("http_write: wrote %d bytes: \n", tbytes); | |
2677 | for (i = 0, buffer -= tbytes; i < tbytes; i += 16) | |
2678 | { | |
2679 | printf(" "); | |
2680 | ||
2681 | for (j = 0; j < 16 && (i + j) < tbytes; j ++) | |
2682 | printf(" %02X", buffer[i + j] & 255); | |
2683 | ||
2684 | while (j < 16) | |
2685 | { | |
2686 | printf(" "); | |
2687 | j ++; | |
2688 | } | |
2689 | ||
2690 | printf(" "); | |
2691 | for (j = 0; j < 16 && (i + j) < tbytes; j ++) | |
2692 | { | |
2693 | ch = buffer[i + j] & 255; | |
2694 | ||
2695 | if (ch < ' ' || ch == 127) | |
2696 | ch = '.'; | |
2697 | ||
2698 | putchar(ch); | |
2699 | } | |
2700 | putchar('\n'); | |
2701 | } | |
2702 | } | |
2703 | #endif /* DEBUG */ | |
2704 | ||
2705 | return (tbytes); | |
2706 | } | |
2707 | ||
2708 | ||
2709 | /* | |
2710 | * 'http_write_chunk()' - Write a chunked buffer. | |
2711 | */ | |
2712 | ||
2713 | static int /* O - Number bytes written */ | |
ecdc0628 | 2714 | http_write_chunk(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2715 | const char *buffer, /* I - Buffer to write */ |
2716 | int length) /* I - Length of buffer */ | |
2717 | { | |
2718 | char header[255]; /* Chunk header */ | |
2719 | int bytes; /* Bytes written */ | |
2720 | ||
2721 | DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n", | |
2722 | http, buffer, length)); | |
2723 | ||
2724 | /* | |
2725 | * Write the chunk header, data, and trailer. | |
2726 | */ | |
2727 | ||
2728 | sprintf(header, "%x\r\n", length); | |
2729 | if (http_write(http, header, strlen(header)) < 0) | |
2730 | { | |
2731 | DEBUG_puts(" http_write of length failed!"); | |
2732 | return (-1); | |
2733 | } | |
2734 | ||
2735 | if ((bytes = http_write(http, buffer, length)) < 0) | |
2736 | { | |
2737 | DEBUG_puts(" http_write of buffer failed!"); | |
2738 | return (-1); | |
2739 | } | |
2740 | ||
2741 | if (http_write(http, "\r\n", 2) < 0) | |
2742 | { | |
2743 | DEBUG_puts(" http_write of CR LF failed!"); | |
2744 | return (-1); | |
2745 | } | |
2746 | ||
2747 | return (bytes); | |
2748 | } | |
2749 | ||
2750 | ||
2751 | #ifdef HAVE_SSL | |
2752 | /* | |
2753 | * 'http_write_ssl()' - Write to a SSL/TLS connection. | |
2754 | */ | |
2755 | ||
2756 | static int /* O - Bytes written */ | |
ecdc0628 | 2757 | http_write_ssl(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2758 | const char *buf, /* I - Buffer holding data */ |
2759 | int len) /* I - Length of buffer */ | |
2760 | { | |
2761 | # if defined(HAVE_LIBSSL) | |
2762 | return (SSL_write((SSL *)(http->tls), buf, len)); | |
2763 | ||
2764 | # elif defined(HAVE_GNUTLS) | |
2765 | return (gnutls_record_send(((http_tls_t *)(http->tls))->session, buf, len)); | |
2766 | # elif defined(HAVE_CDSASSL) | |
fa73b229 | 2767 | int result; /* Return value */ |
ef416fc2 | 2768 | OSStatus error; /* Error info */ |
2769 | size_t processed; /* Number of bytes processed */ | |
2770 | ||
2771 | ||
89d46774 | 2772 | error = SSLWrite(((http_tls_t *)http->tls)->session, buf, len, &processed); |
ef416fc2 | 2773 | |
fa73b229 | 2774 | switch (error) |
ef416fc2 | 2775 | { |
fa73b229 | 2776 | case 0 : |
2777 | result = (int)processed; | |
2778 | break; | |
2779 | case errSSLClosedGraceful : | |
2780 | result = 0; | |
2781 | break; | |
2782 | case errSSLWouldBlock : | |
b423cd4c | 2783 | if (processed) |
2784 | result = (int)processed; | |
2785 | else | |
2786 | { | |
2787 | result = -1; | |
2788 | errno = EINTR; | |
2789 | } | |
fa73b229 | 2790 | break; |
2791 | default : | |
2792 | errno = EPIPE; | |
2793 | result = -1; | |
2794 | break; | |
ef416fc2 | 2795 | } |
fa73b229 | 2796 | |
2797 | return (result); | |
ef416fc2 | 2798 | # endif /* HAVE_LIBSSL */ |
2799 | } | |
2800 | #endif /* HAVE_SSL */ | |
2801 | ||
2802 | ||
2803 | /* | |
c07d5b2d | 2804 | * End of "$Id: http.c 181 2006-06-22 20:01:18Z jlovell $". |
ef416fc2 | 2805 | */ |