Coverage Report

Created: 2026-03-19 06:49

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