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