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