Coverage Report

Created: 2026-04-12 06:23

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