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