Coverage Report

Created: 2025-11-15 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcups/cups/http-support.c
Line
Count
Source
1
//
2
// HTTP support routines for CUPS.
3
//
4
// Copyright © 2020-2025 by OpenPrinting
5
// Copyright © 2007-2019 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 "dnssd.h"
14
15
16
//
17
// Local types...
18
//
19
20
typedef struct _http_uribuf_s   // URI buffer
21
{
22
  cups_dnssd_t    *dnssd;   // DNS-SD context
23
  char      *buffer;  // Pointer to buffer
24
  size_t    bufsize;  // Size of buffer
25
  http_resolve_t  options;  // Options passed to httpResolveURI
26
  const char    *resource;  // Resource from URI
27
  const char    *uuid;    // UUID from URI
28
} _http_uribuf_t;
29
30
31
//
32
// Local globals...
33
//
34
35
static const char * const http_days[7] =// Days of the week
36
{
37
  "Sun",
38
  "Mon",
39
  "Tue",
40
  "Wed",
41
  "Thu",
42
  "Fri",
43
  "Sat"
44
};
45
static const char * const http_months[12] =
46
{         // Months of the year
47
  "Jan",
48
  "Feb",
49
  "Mar",
50
  "Apr",
51
  "May",
52
  "Jun",
53
  "Jul",
54
  "Aug",
55
  "Sep",
56
  "Oct",
57
  "Nov",
58
  "Dec"
59
};
60
static const char * const http_states[] =
61
{         // HTTP state strings
62
  "ERROR",
63
  "WAITING",
64
  "CONNECT",
65
  "COPY",
66
  "COPY-send",
67
  "DELETE",
68
  "DELETE-send",
69
  "GET",
70
  "GET-send",
71
  "HEAD",
72
  "LOCK",
73
  "LOCK-recv",
74
  "LOCK-send",
75
  "MKCOL",
76
  "MOVE",
77
  "MOVE-send",
78
  "OPTIONS",
79
  "POST",
80
  "POST-recv",
81
  "POST-send",
82
  "PROPFIND",
83
  "PROPFIND-recv",
84
  "PROPFIND-send",
85
  "PROPPATCH",
86
  "PROPPATCH-recv",
87
  "PROPPATCH-send",
88
  "PUT",
89
  "PUT-recv",
90
  "TRACE",
91
  "UNLOCK",
92
  "STATUS",
93
  "UNKNOWN_METHOD",
94
  "UNKNOWN_VERSION"
95
};
96
97
98
//
99
// Local functions...
100
//
101
102
static const char *http_copy_decode(char *dst, const char *src, size_t dstsize, const char *term, bool decode);
103
static char   *http_copy_encode(char *dst, const char *src, char *dstend, const char *reserved, const char *term, bool encode);
104
static void     http_resolve_cb(cups_dnssd_resolve_t *res, void *cb_data, cups_dnssd_flags_t flags, uint32_t if_index, const char *fullname, const char *host, uint16_t port, size_t num_txt, cups_option_t *txt);
105
106
107
//
108
// 'httpAssembleURI()' - Assemble a uniform resource identifier from its
109
//                       components.
110
//
111
// This function escapes reserved characters in the URI depending on the
112
// value of the "encoding" argument.  You should use this function in
113
// place of traditional string functions whenever you need to create a
114
// URI string.
115
//
116
117
http_uri_status_t     // O - URI status
118
httpAssembleURI(
119
    http_uri_coding_t encoding,   // I - Encoding flags
120
    char              *uri,   // I - URI buffer
121
    size_t            urilen,   // I - Size of URI buffer
122
    const char        *scheme,    // I - Scheme name
123
    const char        *username,  // I - Username
124
    const char        *host,    // I - Hostname or address
125
    int               port,   // I - Port number
126
    const char        *resource)  // I - Resource
127
0
{
128
0
  char    *ptr,     // Pointer into URI buffer
129
0
    *end;     // End of URI buffer
130
131
132
  // Range check input...
133
0
  if (!uri || urilen < 1 || !scheme || port < 0)
134
0
  {
135
0
    if (uri)
136
0
      *uri = '\0';
137
138
0
    return (HTTP_URI_STATUS_BAD_ARGUMENTS);
139
0
  }
140
141
  // Assemble the URI starting with the scheme...
142
0
  end = uri + urilen - 1;
143
0
  ptr = http_copy_encode(uri, scheme, end, NULL, NULL, false);
144
145
0
  if (!ptr)
146
0
    goto assemble_overflow;
147
148
0
  if (!strcmp(scheme, "geo") || !strcmp(scheme, "mailto") || !strcmp(scheme, "tel"))
149
0
  {
150
    // geo:, mailto:, and tel: only have :, no //...
151
0
    if (ptr < end)
152
0
      *ptr++ = ':';
153
0
    else
154
0
      goto assemble_overflow;
155
0
  }
156
0
  else
157
0
  {
158
    // Schemes other than geo:, mailto:, and tel: typically have //...
159
0
    if ((ptr + 2) < end)
160
0
    {
161
0
      *ptr++ = ':';
162
0
      *ptr++ = '/';
163
0
      *ptr++ = '/';
164
0
    }
165
0
    else
166
0
    {
167
0
      goto assemble_overflow;
168
0
    }
169
0
  }
170
171
  // Next the username and hostname, if any...
172
0
  if (host)
173
0
  {
174
0
    const char  *hostptr;   // Pointer into hostname
175
0
    int   have_ipv6;    // Do we have an IPv6 address?
176
177
0
    if (username && *username)
178
0
    {
179
      // Add username@ first...
180
0
      ptr = http_copy_encode(ptr, username, end, "/?#[]@", NULL, encoding & HTTP_URI_CODING_USERNAME);
181
182
0
      if (!ptr)
183
0
        goto assemble_overflow;
184
185
0
      if (ptr < end)
186
0
  *ptr++ = '@';
187
0
      else
188
0
        goto assemble_overflow;
189
0
    }
190
191
    // Then add the hostname.  Since IPv6 is a particular pain to deal
192
    // with, we have several special cases to deal with.  If we get
193
    // an IPv6 address with brackets around it, assume it is already in
194
    // URI format.  Since DNS-SD service names can sometimes look like
195
    // raw IPv6 addresses, we specifically look for "._tcp" in the name,
196
    // too...
197
0
    for (hostptr = host, have_ipv6 = strchr(host, ':') && !strstr(host, "._tcp"); *hostptr && have_ipv6; hostptr ++)
198
0
    {
199
0
      if (*hostptr != ':' && !isxdigit(*hostptr & 255))
200
0
      {
201
0
        have_ipv6 = *hostptr == '%';
202
0
        break;
203
0
      }
204
0
    }
205
206
0
    if (have_ipv6)
207
0
    {
208
      // We have a raw IPv6 address...
209
0
      if (strchr(host, '%') && !(encoding & HTTP_URI_CODING_RFC6874))
210
0
      {
211
        // We have a link-local address, add "[v1." prefix...
212
0
  if ((ptr + 4) < end)
213
0
  {
214
0
    *ptr++ = '[';
215
0
    *ptr++ = 'v';
216
0
    *ptr++ = '1';
217
0
    *ptr++ = '.';
218
0
  }
219
0
  else
220
0
  {
221
0
    goto assemble_overflow;
222
0
  }
223
0
      }
224
0
      else
225
0
      {
226
        // We have a normal (or RFC 6874 link-local) address, add "[" prefix...
227
0
  if (ptr < end)
228
0
    *ptr++ = '[';
229
0
  else
230
0
          goto assemble_overflow;
231
0
      }
232
233
      // Copy the rest of the IPv6 address, and terminate with "]".
234
0
      while (ptr < end && *host)
235
0
      {
236
0
        if (*host == '%')
237
0
        {
238
          // Convert/encode zone separator
239
0
          if (encoding & HTTP_URI_CODING_RFC6874)
240
0
          {
241
0
            if (ptr >= (end - 2))
242
0
              goto assemble_overflow;
243
244
0
            *ptr++ = '%';
245
0
            *ptr++ = '2';
246
0
            *ptr++ = '5';
247
0
          }
248
0
          else
249
0
          {
250
0
      *ptr++ = '+';
251
0
    }
252
253
0
    host ++;
254
0
  }
255
0
  else
256
0
  {
257
0
    *ptr++ = *host++;
258
0
  }
259
0
      }
260
261
0
      if (*host)
262
0
        goto assemble_overflow;
263
264
0
      if (ptr < end)
265
0
  *ptr++ = ']';
266
0
      else
267
0
        goto assemble_overflow;
268
0
    }
269
0
    else
270
0
    {
271
      // Otherwise, just copy the host string (the extra chars are not in the
272
      // "reg-name" ABNF rule; anything <= SP or >= DEL plus % gets automatically
273
      // percent-encoded.
274
0
      ptr = http_copy_encode(ptr, host, end, "\"#/:<>?@[\\]^`{|}", NULL, encoding & HTTP_URI_CODING_HOSTNAME);
275
276
0
      if (!ptr)
277
0
        goto assemble_overflow;
278
0
    }
279
280
    // Finish things off with the port number...
281
0
    if (!strcmp(scheme, "http") && port == 80)
282
0
      port = 0;
283
0
    else if (!strcmp(scheme, "https") && port == 443)
284
0
      port = 0;
285
0
    else if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && port == 631)
286
0
      port = 0;
287
288
0
    if (port > 0)
289
0
    {
290
0
      snprintf(ptr, (size_t)(end - ptr + 1), ":%d", port);
291
0
      ptr += strlen(ptr);
292
293
0
      if (ptr >= end)
294
0
  goto assemble_overflow;
295
0
    }
296
0
  }
297
298
  // Last but not least, add the resource string...
299
0
  if (resource)
300
0
  {
301
0
    char  *query;     // Pointer to query string
302
303
    // Copy the resource string up to the query string if present...
304
0
    query = strchr(resource, '?');
305
0
    ptr   = http_copy_encode(ptr, resource, end, NULL, "?", encoding & HTTP_URI_CODING_RESOURCE);
306
0
    if (!ptr)
307
0
      goto assemble_overflow;
308
309
0
    if (query)
310
0
    {
311
      // Copy query string without encoding...
312
0
      ptr = http_copy_encode(ptr, query, end, NULL, NULL, encoding & HTTP_URI_CODING_QUERY);
313
0
      if (!ptr)
314
0
  goto assemble_overflow;
315
0
    }
316
0
  }
317
0
  else if (ptr < end)
318
0
  {
319
0
    *ptr++ = '/';
320
0
  }
321
0
  else
322
0
  {
323
0
    goto assemble_overflow;
324
0
  }
325
326
  // Nul-terminate the URI buffer and return with no errors...
327
0
  *ptr = '\0';
328
329
0
  return (HTTP_URI_STATUS_OK);
330
331
  // Clear the URI string and return an overflow error; I don't usually
332
  // like goto's, but in this case it makes sense...
333
0
  assemble_overflow:
334
335
0
  *uri = '\0';
336
0
  return (HTTP_URI_STATUS_OVERFLOW);
337
0
}
338
339
340
//
341
// 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
342
//                        components with a formatted resource.
343
//
344
// This function creates a formatted version of the resource string
345
// argument "resourcef" and escapes reserved characters in the URI
346
// depending on the value of the "encoding" argument.  You should use
347
// this function in place of traditional string functions whenever
348
// you need to create a URI string.
349
//
350
351
http_uri_status_t     // O - URI status
352
httpAssembleURIf(
353
    http_uri_coding_t encoding,   // I - Encoding flags
354
    char              *uri,   // I - URI buffer
355
    size_t            urilen,   // I - Size of URI buffer
356
    const char        *scheme,    // I - Scheme name
357
    const char        *username,  // I - Username
358
    const char        *host,    // I - Hostname or address
359
    int               port,   // I - Port number
360
    const char        *resourcef, // I - Printf-style resource
361
    ...)        // I - Additional arguments as needed
362
0
{
363
0
  va_list ap;     // Pointer to additional arguments
364
0
  char    resource[1024];   // Formatted resource string
365
0
  int   bytes;      // Bytes in formatted string
366
367
368
  // Range check input...
369
0
  if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef)
370
0
  {
371
0
    if (uri)
372
0
      *uri = '\0';
373
374
0
    return (HTTP_URI_STATUS_BAD_ARGUMENTS);
375
0
  }
376
377
  // Format the resource string and assemble the URI...
378
0
  va_start(ap, resourcef);
379
0
  bytes = vsnprintf(resource, sizeof(resource), resourcef, ap);
380
0
  va_end(ap);
381
382
0
  if ((size_t)bytes >= sizeof(resource))
383
0
  {
384
0
    *uri = '\0';
385
0
    return (HTTP_URI_STATUS_OVERFLOW);
386
0
  }
387
0
  else
388
0
  {
389
0
    return (httpAssembleURI(encoding,  uri, urilen, scheme, username, host, port, resource));
390
0
  }
391
0
}
392
393
394
//
395
// 'httpAssembleUUID()' - Assemble a name-based UUID URN conforming to RFC 4122.
396
//
397
// This function creates a unique 128-bit identifying number using the server
398
// name, port number, random data, and optionally an object name and/or object
399
// number.  The result is formatted as a UUID URN as defined in RFC 4122.
400
//
401
// The buffer needs to be at least 46 bytes in size.
402
//
403
404
char *          // I - UUID string
405
httpAssembleUUID(const char *server,  // I - Server name
406
     int        port, // I - Port number
407
     const char *name,  // I - Object name or NULL
408
     int        number, // I - Object number or 0
409
     char       *buffer,  // I - String buffer
410
     size_t     bufsize)  // I - Size of buffer
411
0
{
412
0
  char      data[1024]; // Source string for MD5
413
0
  unsigned char   md5sum[16]; // MD5 digest/sum
414
415
416
  // Build a version 3 UUID conforming to RFC 4122.
417
  //
418
  // Start with the MD5 sum of the server, port, object name and
419
  // number, and some random data on the end.
420
0
  snprintf(data, sizeof(data), "%s:%d:%s:%d:%04x:%04x", server, port, name ? name : server, number, (unsigned)cupsGetRand() & 0xffff, (unsigned)cupsGetRand() & 0xffff);
421
422
0
  cupsHashData("md5", (unsigned char *)data, strlen(data), md5sum, sizeof(md5sum));
423
424
  // Generate the UUID from the MD5...
425
0
  snprintf(buffer, bufsize, "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], md5sum[14], md5sum[15]);
426
427
0
  return (buffer);
428
0
}
429
430
431
//
432
// 'httpDecode64()' - Base64-decode a string.
433
//
434
// This function decodes a Base64 string as defined by RFC 4648.  The caller
435
// must initialize "outlen" to the maximum size of the decoded string.  On
436
// return "outlen" contains the decoded length of the string and "end" (if not
437
// `NULL`) points to the end of the Base64 data that has been decoded.
438
//
439
// This function always reserves one byte in the output buffer for a nul
440
// terminating character, even if the result is not a regular string.  Callers
441
// should ensure that the output buffer is at least one byte larger than the
442
// expected size, for example 33 bytes for a SHA-256 hash which is 32 bytes in
443
// length.
444
//
445
// This function supports both Base64 and Base64url strings.
446
//
447
448
char *          // O  - Decoded string or `NULL` on error
449
httpDecode64(char       *out,   // I  - String to write to
450
       size_t     *outlen,  // IO - Size of output string
451
             const char *in,    // I  - String to read from
452
             const char **end)    // O  - Pointer to end of Base64 data (`NULL` if don't care)
453
0
{
454
0
  int   pos;      // Bit position
455
0
  unsigned  base64;     // Value of this character
456
0
  char    *outptr,    // Output pointer
457
0
    *outend;    // End of output buffer
458
459
460
  // Range check input...
461
0
  if (!out || !outlen || *outlen < 1 || !in)
462
0
    return (NULL);
463
464
0
  if (!*in)
465
0
  {
466
0
    *out    = '\0';
467
0
    *outlen = 0;
468
469
0
    if (end)
470
0
      *end = in;
471
472
0
    return (out);
473
0
  }
474
475
  // Convert from base-64 to bytes...
476
0
  for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++)
477
0
  {
478
    // Decode this character into a number from 0 to 63...
479
0
    if (*in >= 'A' && *in <= 'Z')
480
0
      base64 = (unsigned)(*in - 'A');
481
0
    else if (*in >= 'a' && *in <= 'z')
482
0
      base64 = (unsigned)(*in - 'a' + 26);
483
0
    else if (*in >= '0' && *in <= '9')
484
0
      base64 = (unsigned)(*in - '0' + 52);
485
0
    else if (*in == '+' || *in == '-')
486
0
      base64 = 62;
487
0
    else if (*in == '/' || *in == '_')
488
0
      base64 = 63;
489
0
    else if (*in == '=')
490
0
      break;
491
0
    else if (isspace(*in & 255))
492
0
      continue;
493
0
    else
494
0
      break;
495
496
    // Store the result in the appropriate chars...
497
0
    switch (pos)
498
0
    {
499
0
      case 0 :
500
0
          if (outptr < outend)
501
0
            *outptr = (char)(base64 << 2);
502
0
    pos ++;
503
0
    break;
504
0
      case 1 :
505
0
          if (outptr < outend)
506
0
            *outptr++ |= (char)((base64 >> 4) & 3);
507
0
          if (outptr < outend)
508
0
      *outptr = (char)((base64 << 4) & 255);
509
0
    pos ++;
510
0
    break;
511
0
      case 2 :
512
0
          if (outptr < outend)
513
0
            *outptr++ |= (char)((base64 >> 2) & 15);
514
0
          if (outptr < outend)
515
0
      *outptr = (char)((base64 << 6) & 255);
516
0
    pos ++;
517
0
    break;
518
0
      case 3 :
519
0
          if (outptr < outend)
520
0
            *outptr++ |= (char)base64;
521
0
    pos = 0;
522
0
    break;
523
0
    }
524
0
  }
525
526
  // Add a trailing nul...
527
0
  *outptr = '\0';
528
529
  // Skip trailing '='...
530
0
  while (*in == '=')
531
0
    in ++;
532
533
  // Return the decoded string, next input pointer, and size...
534
0
  *outlen = (size_t)(outptr - out);
535
536
0
  if (end)
537
0
    *end = in;
538
539
0
  return (out);
540
0
}
541
542
543
//
544
// 'httpEncode64()' - Base64-encode a string.
545
//
546
// This function encodes a Base64 string as defined by RFC 4648.  The "url"
547
// argument controls whether the original Base64 ("url" = `false`) or the
548
// Base64url ("url" = `true`) alphabet is used.
549
//
550
551
char *          // O - Encoded string
552
httpEncode64(char       *out,   // I - String to write to
553
       size_t     outlen,   // I - Maximum size of output string
554
             const char *in,    // I - String to read from
555
       size_t     inlen,    // I - Size of input string
556
       bool       url)    // I - `true` for Base64url, `false` for Base64
557
0
{
558
0
  char    *outptr,    // Output pointer
559
0
    *outend;    // End of output buffer
560
0
  const char  *alpha;     // Alphabet
561
0
  static const char *base64 =   // Base64 alphabet
562
0
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
563
0
  static const char *base64url =  // Base64url alphabet
564
0
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
565
566
567
  // Range check input...
568
0
  if (!out || outlen < 1 || !in)
569
0
    return (NULL);
570
571
  // Encode bytes...
572
0
  alpha = url ? base64url : base64;
573
574
0
  for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --)
575
0
  {
576
    // Encode the up to 3 characters as 4 Base64 numbers...
577
0
    if (outptr < outend)
578
0
      *outptr ++ = alpha[(in[0] & 255) >> 2];
579
580
0
    if (outptr < outend)
581
0
    {
582
0
      if (inlen > 1)
583
0
        *outptr ++ = alpha[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63];
584
0
      else
585
0
        *outptr ++ = alpha[((in[0] & 255) << 4) & 63];
586
0
    }
587
588
0
    in ++;
589
0
    inlen --;
590
0
    if (inlen <= 0)
591
0
    {
592
0
      if (!url && outptr < outend)
593
0
  *outptr ++ = '=';
594
0
      if (!url && outptr < outend)
595
0
  *outptr ++ = '=';
596
0
      break;
597
0
    }
598
599
0
    if (outptr < outend)
600
0
    {
601
0
      if (inlen > 1)
602
0
        *outptr ++ = alpha[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63];
603
0
      else
604
0
        *outptr ++ = alpha[((in[0] & 255) << 2) & 63];
605
0
    }
606
607
0
    in ++;
608
0
    inlen --;
609
0
    if (inlen <= 0)
610
0
    {
611
0
      if (!url && outptr < outend)
612
0
        *outptr ++ = '=';
613
0
      break;
614
0
    }
615
616
0
    if (outptr < outend)
617
0
      *outptr ++ = alpha[in[0] & 63];
618
0
  }
619
620
0
  *outptr = '\0';
621
622
  // Return the encoded string...
623
0
  return (out);
624
0
}
625
626
627
//
628
// 'httpGetDateString()' - Get a formatted date/time string from a time value.
629
//
630
631
const char *        // O - Date/time string
632
httpGetDateString(time_t t,   // I - Time in seconds
633
                  char   *s,    // I - String buffer
634
      size_t slen)    // I - Size of string buffer
635
0
{
636
0
  struct tm tdate;      // UNIX date/time data
637
638
639
0
  gmtime_r(&t, &tdate);
640
641
0
  snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT", http_days[tdate.tm_wday], tdate.tm_mday, http_months[tdate.tm_mon], tdate.tm_year + 1900, tdate.tm_hour, tdate.tm_min, tdate.tm_sec);
642
643
0
  return (s);
644
0
}
645
646
647
//
648
// 'httpGetDateTime()' - Get a time value from a formatted date/time string.
649
//
650
651
time_t          // O - Time in seconds
652
httpGetDateTime(const char *s)    // I - Date/time string
653
0
{
654
0
  int   i;      // Looping var
655
0
  char    mon[16];    // Abbreviated month name
656
0
  int   day, year;    // Day of month and year
657
0
  int   hour, min, sec;   // Time
658
0
  long    days;     // Number of days since 1970
659
0
  static const int normal_days[] =  // Days to a month, normal years
660
0
    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
661
0
  static const int leap_days[] =  // Days to a month, leap years
662
0
    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
663
664
665
0
  DEBUG_printf("2httpGetDateTime(s=\"%s\")", s);
666
667
  // Extract the date and time from the formatted string...
668
0
  if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6)
669
0
    return (0);
670
671
0
  DEBUG_printf("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d", day, mon, year, hour, min, sec);
672
673
  // Check for invalid year (RFC 7231 says it's 4DIGIT)
674
0
  if (year > 9999)
675
0
    return (0);
676
677
  // Convert the month name to a number from 0 to 11.
678
0
  for (i = 0; i < 12; i ++)
679
0
  {
680
0
    if (!_cups_strcasecmp(mon, http_months[i]))
681
0
      break;
682
0
  }
683
684
0
  if (i >= 12)
685
0
    return (0);
686
687
0
  DEBUG_printf("4httpGetDateTime: i=%d", i);
688
689
  // Now convert the date and time to a UNIX time value in seconds since
690
  // 1970.  We can't use mktime() since the timezone may not be UTC but
691
  // the date/time string *is* UTC.
692
0
  if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0))
693
0
    days = leap_days[i] + day - 1;
694
0
  else
695
0
    days = normal_days[i] + day - 1;
696
697
0
  DEBUG_printf("4httpGetDateTime: days=%ld", days);
698
699
0
  days += (year - 1970) * 365 +   // 365 days per year (normally)
700
0
          ((year - 1) / 4 - 492) -  // + leap days
701
0
    ((year - 1) / 100 - 19) + // - 100 year days
702
0
          ((year - 1) / 400 - 4); // + 400 year days
703
704
0
  DEBUG_printf("4httpGetDateTime: days=%ld\n", days);
705
706
0
  return (days * 86400 + hour * 3600 + min * 60 + sec);
707
0
}
708
709
710
//
711
// 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
712
//                       components.
713
//
714
715
http_uri_status_t     // O - Result of separation
716
httpSeparateURI(
717
    http_uri_coding_t decoding,   // I - Decoding flags
718
    const char        *uri,   // I - Universal Resource Identifier
719
    char              *scheme,    // O - Scheme (http, https, etc.)
720
    size_t            schemelen,  // I - Size of scheme buffer
721
    char              *username,  // O - Username
722
    size_t            usernamelen,  // I - Size of username buffer
723
    char              *host,    // O - Hostname
724
    size_t            hostlen,    // I - Size of hostname buffer
725
    int               *port,    // O - Port number to use
726
    char              *resource,  // O - Resource/filename
727
    size_t            resourcelen)  // I - Size of resource buffer
728
0
{
729
0
  char      *ptr,   // Pointer into string...
730
0
      *end;   // End of string
731
0
  const char    *sep;   // Separator character
732
0
  http_uri_status_t status;   // Result of separation
733
734
735
  // Initialize everything to blank...
736
0
  if (scheme && schemelen > 0)
737
0
    *scheme = '\0';
738
739
0
  if (username && usernamelen > 0)
740
0
    *username = '\0';
741
742
0
  if (host && hostlen > 0)
743
0
    *host = '\0';
744
745
0
  if (port)
746
0
    *port = 0;
747
748
0
  if (resource && resourcelen > 0)
749
0
    *resource = '\0';
750
751
  // Range check input...
752
0
  if (!uri || !port || !scheme || schemelen == 0 || !username || usernamelen == 0 || !host || hostlen == 0 || !resource || resourcelen == 0)
753
0
    return (HTTP_URI_STATUS_BAD_ARGUMENTS);
754
755
0
  if (!*uri)
756
0
    return (HTTP_URI_STATUS_BAD_URI);
757
758
  // Grab the scheme portion of the URI...
759
0
  status = HTTP_URI_STATUS_OK;
760
761
0
  if (!strncmp(uri, "//", 2))
762
0
  {
763
    // Workaround for HP IPP client bug...
764
0
    cupsCopyString(scheme, "ipp", (size_t)schemelen);
765
0
    status = HTTP_URI_STATUS_MISSING_SCHEME;
766
0
  }
767
0
  else if (*uri == '/')
768
0
  {
769
    // Filename...
770
0
    cupsCopyString(scheme, "file", (size_t)schemelen);
771
0
    status = HTTP_URI_STATUS_MISSING_SCHEME;
772
0
  }
773
0
  else
774
0
  {
775
    // Standard URI with scheme...
776
0
    for (ptr = scheme, end = scheme + schemelen - 1; *uri && *uri != ':' && ptr < end;)
777
0
    {
778
0
      if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+.", *uri) != NULL)
779
0
        *ptr++ = *uri++;
780
0
      else
781
0
        break;
782
0
    }
783
784
0
    *ptr = '\0';
785
786
0
    if (*uri != ':' || *scheme == '.' || !*scheme)
787
0
    {
788
0
      *scheme = '\0';
789
0
      return (HTTP_URI_STATUS_BAD_SCHEME);
790
0
    }
791
792
0
    uri ++;
793
0
  }
794
795
  // Set the default port number...
796
0
  if (!strcmp(scheme, "http"))
797
0
    *port = 80;
798
0
  else if (!strcmp(scheme, "https"))
799
0
    *port = 443;
800
0
  else if (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps"))
801
0
    *port = 631;
802
0
  else if (!_cups_strcasecmp(scheme, "lpd"))
803
0
    *port = 515;
804
0
  else if (!strcmp(scheme, "socket")) // Not yet registered with IANA...
805
0
    *port = 9100;
806
0
  else if (strcmp(scheme, "file") && strcmp(scheme, "mailto") && strcmp(scheme, "tel"))
807
0
    status = HTTP_URI_STATUS_UNKNOWN_SCHEME;
808
809
  // Now see if we have a hostname...
810
0
  if (!strncmp(uri, "//", 2))
811
0
  {
812
    // Yes, extract it...
813
0
    uri += 2;
814
815
    // Grab the username, if any...
816
0
    if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@')
817
0
    {
818
      // Get a username:password combo...
819
0
      uri = http_copy_decode(username, uri, usernamelen, "@", decoding & HTTP_URI_CODING_USERNAME);
820
821
0
      if (!uri)
822
0
      {
823
0
        *username = '\0';
824
0
        return (HTTP_URI_STATUS_BAD_USERNAME);
825
0
      }
826
827
0
      uri ++;
828
0
    }
829
830
    // Then the hostname/IP address...
831
0
    if (*uri == '[')
832
0
    {
833
      // Grab IPv6 address...
834
0
      uri ++;
835
0
      if (*uri == 'v')
836
0
      {
837
        // Skip IPvFuture ("vXXXX.") prefix...
838
0
        uri ++;
839
840
0
        while (isxdigit(*uri & 255))
841
0
          uri ++;
842
843
0
        if (*uri != '.')
844
0
        {
845
0
    *host = '\0';
846
0
    return (HTTP_URI_STATUS_BAD_HOSTNAME);
847
0
        }
848
849
0
        uri ++;
850
0
      }
851
852
0
      uri = http_copy_decode(host, uri, hostlen, "]", decoding & HTTP_URI_CODING_HOSTNAME);
853
854
0
      if (!uri)
855
0
      {
856
0
        *host = '\0';
857
0
        return (HTTP_URI_STATUS_BAD_HOSTNAME);
858
0
      }
859
860
      // Validate value...
861
0
      if (*uri != ']')
862
0
      {
863
0
        *host = '\0';
864
0
        return (HTTP_URI_STATUS_BAD_HOSTNAME);
865
0
      }
866
867
0
      uri ++;
868
869
0
      for (ptr = host; *ptr; ptr ++)
870
0
      {
871
0
        if (*ptr == '+')
872
0
  {
873
    // Convert zone separator to % and stop here...
874
0
    *ptr = '%';
875
0
    break;
876
0
  }
877
0
  else if (*ptr == '%')
878
0
  {
879
    // Stop at zone separator (RFC 6874)
880
0
    break;
881
0
  }
882
0
  else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255))
883
0
  {
884
0
    *host = '\0';
885
0
    return (HTTP_URI_STATUS_BAD_HOSTNAME);
886
0
  }
887
0
      }
888
0
    }
889
0
    else
890
0
    {
891
      // Validate the hostname or IPv4 address first...
892
0
      for (ptr = (char *)uri; *ptr; ptr ++)
893
0
      {
894
0
        if (strchr(":?/", *ptr))
895
0
    break;
896
0
        else if (!strchr("abcdefghijklmnopqrstuvwxyz"  // unreserved
897
0
       "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // unreserved
898
0
       "0123456789"     // unreserved
899
0
             "-._~"       // unreserved
900
0
       "%"        // pct-encoded
901
0
       "!$&'()*+,;="      // sub-delims
902
0
       "\\", *ptr))     // SMB domain
903
0
  {
904
0
    *host = '\0';
905
0
    return (HTTP_URI_STATUS_BAD_HOSTNAME);
906
0
  }
907
0
      }
908
909
      // Then copy the hostname or IPv4 address to the buffer...
910
0
      uri = http_copy_decode(host, uri, hostlen, ":?/", decoding & HTTP_URI_CODING_HOSTNAME);
911
912
0
      if (!uri)
913
0
      {
914
0
        *host = '\0';
915
0
        return (HTTP_URI_STATUS_BAD_HOSTNAME);
916
0
      }
917
0
    }
918
919
    // Validate hostname for file scheme - only empty and localhost are
920
    // acceptable.
921
0
    if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0])
922
0
    {
923
0
      *host = '\0';
924
0
      return (HTTP_URI_STATUS_BAD_HOSTNAME);
925
0
    }
926
927
    // See if we have a port number...
928
0
    if (*uri == ':')
929
0
    {
930
      // Yes, collect the port number...
931
0
      if (!isdigit(uri[1] & 255))
932
0
      {
933
0
        *port = 0;
934
0
        return (HTTP_URI_STATUS_BAD_PORT);
935
0
      }
936
937
0
      *port = (int)strtol(uri + 1, (char **)&uri, 10);
938
939
0
      if (*port <= 0 || *port > 65535)
940
0
      {
941
0
        *port = 0;
942
0
        return (HTTP_URI_STATUS_BAD_PORT);
943
0
      }
944
945
0
      if (*uri != '/' && *uri)
946
0
      {
947
0
        *port = 0;
948
0
        return (HTTP_URI_STATUS_BAD_PORT);
949
0
      }
950
0
    }
951
0
  }
952
953
  // The remaining portion is the resource string...
954
0
  if (*uri == '?' || !*uri)
955
0
  {
956
    // Hostname but no path...
957
0
    status    = HTTP_URI_STATUS_MISSING_RESOURCE;
958
0
    *resource = '/';
959
960
    // Copy any query string...
961
0
    if (*uri == '?')
962
0
      uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL, decoding & HTTP_URI_CODING_QUERY);
963
0
    else
964
0
      resource[1] = '\0';
965
0
  }
966
0
  else
967
0
  {
968
0
    uri = http_copy_decode(resource, uri, resourcelen, "?", decoding & HTTP_URI_CODING_RESOURCE);
969
970
0
    if (uri && *uri == '?')
971
0
    {
972
      // Concatenate any query string...
973
0
      char *resptr = resource + strlen(resource);
974
975
0
      uri = http_copy_decode(resptr, uri, resourcelen - (size_t)(resptr - resource), NULL, decoding & HTTP_URI_CODING_QUERY);
976
0
    }
977
0
  }
978
979
0
  if (!uri)
980
0
  {
981
0
    *resource = '\0';
982
0
    return (HTTP_URI_STATUS_BAD_RESOURCE);
983
0
  }
984
985
  // Return the URI separation status...
986
0
  return (status);
987
0
}
988
989
990
//
991
// '_httpSetDigestAuthString()' - Calculate a Digest authentication response
992
//                                using the appropriate RFC 2068/2617/7616
993
//                                algorithm.
994
//
995
996
bool          // O - `true` on success, `false` on failure
997
_httpSetDigestAuthString(
998
    http_t     *http,     // I - HTTP connection
999
    const char *nonce,      // I - Nonce value
1000
    const char *method,     // I - HTTP method
1001
    const char *resource)   // I - HTTP resource path
1002
0
{
1003
0
  char    kd[65],     // Final MD5/SHA-256 digest
1004
0
    ha1[65],    // Hash of username:realm:password
1005
0
    ha2[65],    // Hash of method:request-uri
1006
0
    username[_HTTP_MAX_VALUE],
1007
          // username:password
1008
0
    *password,    // Pointer to password
1009
0
    temp[1024],   // Temporary string
1010
0
    digest[1024];   // Digest auth data
1011
0
  unsigned char hash[32];   // Hash buffer
1012
0
  size_t  hashsize;   // Size of hash
1013
0
  _cups_globals_t *cg = _cupsGlobals(); // Per-thread globals
1014
1015
1016
0
  DEBUG_printf("2_httpSetDigestAuthString(http=%p, nonce=\"%s\", method=\"%s\", resource=\"%s\")", (void *)http, nonce, method, resource);
1017
1018
0
  if (nonce && *nonce && strcmp(nonce, http->nonce))
1019
0
  {
1020
0
    cupsCopyString(http->nonce, nonce, sizeof(http->nonce));
1021
1022
0
    if (nonce == http->nextnonce)
1023
0
      http->nextnonce[0] = '\0';
1024
1025
0
    http->nonce_count = 1;
1026
0
  }
1027
0
  else
1028
0
  {
1029
0
    http->nonce_count ++;
1030
0
  }
1031
1032
0
  cupsCopyString(username, http->userpass, sizeof(username));
1033
0
  if ((password = strchr(username, ':')) != NULL)
1034
0
    *password++ = '\0';
1035
0
  else
1036
0
    return (false);
1037
1038
0
  if (http->qop[0])
1039
0
  {
1040
    // Follow RFC 2617/7616...
1041
0
    int   i;      // Looping var
1042
0
    char  cnonce[65];   // cnonce value
1043
0
    const char  *hashalg;   // Hashing algorithm
1044
0
    const char  *qop;     // Quality of Protection
1045
1046
0
    for (i = 0; i < 64; i ++)
1047
0
      cnonce[i] = "0123456789ABCDEF"[cupsGetRand() & 15];
1048
0
    cnonce[64] = '\0';
1049
1050
0
    if (!_cups_strcasecmp(http->qop, "auth"))
1051
0
    {
1052
      // RFC 2617: "auth" | "auth-int" | token
1053
0
      qop = "auth";
1054
0
    }
1055
0
    else
1056
0
    {
1057
      // Some other qop we don't support, skip this one...
1058
0
      return (false);
1059
0
    }
1060
1061
0
    if (!http->algorithm[0] || !_cups_strcasecmp(http->algorithm, "MD5"))
1062
0
    {
1063
      // RFC 2617 Digest with MD5
1064
0
      if (cg->digestoptions == _CUPS_DIGESTOPTIONS_DENYMD5)
1065
0
      {
1066
0
  DEBUG_puts("3_httpSetDigestAuthString: MD5 Digest is disabled.");
1067
0
  return (false);
1068
0
      }
1069
1070
0
      hashalg = "md5";
1071
0
    }
1072
0
    else if (!_cups_strcasecmp(http->algorithm, "SHA-256"))
1073
0
    {
1074
      // RFC 7616 Digest with SHA-256
1075
0
      hashalg = "sha2-256";
1076
0
    }
1077
0
    else
1078
0
    {
1079
      // Some other algorithm we don't support, skip this one...
1080
0
      return (false);
1081
0
    }
1082
1083
    // Calculate digest value...
1084
1085
    // H(A1) = H(username:realm:password)
1086
0
    snprintf(temp, sizeof(temp), "%s:%s:%s", username, http->realm, password);
1087
0
    hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
1088
0
    cupsHashString(hash, hashsize, ha1, sizeof(ha1));
1089
1090
    // H(A2) = H(method:uri)
1091
0
    snprintf(temp, sizeof(temp), "%s:%s", method, resource);
1092
0
    hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
1093
0
    cupsHashString(hash, hashsize, ha2, sizeof(ha2));
1094
1095
    // KD = H(H(A1):nonce:nc:cnonce:qop:H(A2))
1096
0
    snprintf(temp, sizeof(temp), "%s:%s:%08x:%s:%s:%s", ha1, http->nonce, http->nonce_count, cnonce, qop, ha2);
1097
0
    hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
1098
0
    cupsHashString(hash, hashsize, kd, sizeof(kd));
1099
1100
    // Pass the RFC 2617/7616 WWW-Authenticate header...
1101
0
    if (http->opaque[0])
1102
0
      snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=%s, opaque=\"%s\", cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsGetUser(), http->realm, http->nonce, http->algorithm, qop, http->opaque, cnonce, http->nonce_count, resource, kd);
1103
0
    else
1104
0
      snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=%s, cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", username, http->realm, http->nonce, http->algorithm, qop, cnonce, http->nonce_count, resource, kd);
1105
0
  }
1106
0
  else
1107
0
  {
1108
    // Use old RFC 2069 Digest method...
1109
0
    if (cg->digestoptions == _CUPS_DIGESTOPTIONS_DENYMD5)
1110
0
    {
1111
0
      DEBUG_puts("3_httpSetDigestAuthString: MD5 Digest is disabled.");
1112
0
      return (0);
1113
0
    }
1114
1115
0
    DEBUG_puts("3_httpSetDigestAuthString: Use old RFC 2069 Digest method...");
1116
1117
    // H(A1) = H(username:realm:password)
1118
0
    snprintf(temp, sizeof(temp), "%s:%s:%s", username, http->realm, password);
1119
0
    hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
1120
0
    cupsHashString(hash, hashsize, ha1, sizeof(ha1));
1121
1122
    // H(A2) = H(method:uri)
1123
0
    snprintf(temp, sizeof(temp), "%s:%s", method, resource);
1124
0
    hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
1125
0
    cupsHashString(hash, hashsize, ha2, sizeof(ha2));
1126
1127
    // KD = H(H(A1):nonce:H(A2))
1128
0
    snprintf(temp, sizeof(temp), "%s:%s:%s", ha1, http->nonce, ha2);
1129
0
    hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
1130
0
    cupsHashString(hash, hashsize, kd, sizeof(kd));
1131
1132
    // Pass the old RFC 2069 WWW-Authenticate header...
1133
0
    snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", username, http->realm, http->nonce, resource, kd);
1134
0
  }
1135
1136
0
  httpSetAuthString(http, "Digest", digest);
1137
1138
0
  return (true);
1139
0
}
1140
1141
1142
//
1143
// 'httpStateString()' - Return the string describing a HTTP state value.
1144
//
1145
1146
const char *        // O - State string
1147
httpStateString(http_state_t state) // I - HTTP state value
1148
0
{
1149
0
  if (state < HTTP_STATE_ERROR || state >= HTTP_STATE_MAX)
1150
0
    return ("HTTP_STATE_???");
1151
0
  else
1152
0
    return (http_states[state - HTTP_STATE_ERROR]);
1153
0
}
1154
1155
1156
//
1157
// '_httpStatusString()' - Return the localized string describing a HTTP
1158
//                         status code.
1159
//
1160
// The returned string is localized using the passed message catalog.
1161
//
1162
1163
const char *        // O - Localized status string
1164
_httpStatusString(
1165
    cups_lang_t   *lang,    // I - Language
1166
    http_status_t status)   // I - HTTP status code
1167
0
{
1168
0
  const char  *s;     // Status string
1169
0
  _cups_globals_t *cg = _cupsGlobals(); // Global data
1170
1171
1172
0
  switch (status)
1173
0
  {
1174
0
    case HTTP_STATUS_ERROR :
1175
0
        s = strerror(errno);
1176
0
        break;
1177
0
    case HTTP_STATUS_CONTINUE :
1178
0
        s = _("Continue");
1179
0
  break;
1180
0
    case HTTP_STATUS_SWITCHING_PROTOCOLS :
1181
0
        s = _("Switching Protocols");
1182
0
  break;
1183
0
    case HTTP_STATUS_OK :
1184
0
        s = _("OK");
1185
0
  break;
1186
0
    case HTTP_STATUS_CREATED :
1187
0
        s = _("Created");
1188
0
  break;
1189
0
    case HTTP_STATUS_ACCEPTED :
1190
0
        s = _("Accepted");
1191
0
  break;
1192
0
    case HTTP_STATUS_NOT_AUTHORITATIVE :
1193
0
        s = _("Not Authoritative");
1194
0
  break;
1195
0
    case HTTP_STATUS_NO_CONTENT :
1196
0
        s = _("No Content");
1197
0
  break;
1198
0
    case HTTP_STATUS_RESET_CONTENT :
1199
0
        s = _("Reset Content");
1200
0
  break;
1201
0
    case HTTP_STATUS_PARTIAL_CONTENT :
1202
0
        s = _("Partial Content");
1203
0
  break;
1204
0
    case HTTP_STATUS_MULTI_STATUS :
1205
0
        s = _("Multi-Status");
1206
0
  break;
1207
0
    case HTTP_STATUS_ALREADY_REPORTED :
1208
0
        s = _("Already Reported");
1209
0
  break;
1210
1211
0
    case HTTP_STATUS_MULTIPLE_CHOICES :
1212
0
        s = _("Multiple Choices");
1213
0
  break;
1214
0
    case HTTP_STATUS_MOVED_PERMANENTLY :
1215
0
        s = _("Moved Permanently");
1216
0
  break;
1217
0
    case HTTP_STATUS_FOUND :
1218
0
        s = _("Found");
1219
0
  break;
1220
0
    case HTTP_STATUS_SEE_OTHER :
1221
0
        s = _("See Other");
1222
0
  break;
1223
0
    case HTTP_STATUS_NOT_MODIFIED :
1224
0
        s = _("Not Modified");
1225
0
  break;
1226
0
    case HTTP_STATUS_USE_PROXY :
1227
0
        s = _("Use Proxy");
1228
0
  break;
1229
0
    case HTTP_STATUS_TEMPORARY_REDIRECT :
1230
0
        s = _("Temporary Redirect");
1231
0
  break;
1232
0
    case HTTP_STATUS_PERMANENT_REDIRECT :
1233
0
        s = _("Permanent Redirect");
1234
0
  break;
1235
1236
0
    case HTTP_STATUS_BAD_REQUEST :
1237
0
        s = _("Bad Request");
1238
0
  break;
1239
0
    case HTTP_STATUS_UNAUTHORIZED :
1240
0
    case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED :
1241
0
        s = _("Unauthorized");
1242
0
  break;
1243
0
    case HTTP_STATUS_PAYMENT_REQUIRED :
1244
0
        s = _("Payment Required");
1245
0
  break;
1246
0
    case HTTP_STATUS_FORBIDDEN :
1247
0
        s = _("Forbidden");
1248
0
  break;
1249
0
    case HTTP_STATUS_NOT_FOUND :
1250
0
        s = _("Not Found");
1251
0
  break;
1252
0
    case HTTP_STATUS_METHOD_NOT_ALLOWED :
1253
0
        s = _("Method Now Allowed");
1254
0
  break;
1255
0
    case HTTP_STATUS_NOT_ACCEPTABLE :
1256
0
        s = _("Not Acceptable");
1257
0
  break;
1258
0
    case HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED :
1259
0
        s = _("Proxy Authentication Required");
1260
0
  break;
1261
0
    case HTTP_STATUS_REQUEST_TIMEOUT :
1262
0
        s = _("Request Timeout");
1263
0
  break;
1264
0
    case HTTP_STATUS_CONFLICT :
1265
0
        s = _("Conflict");
1266
0
  break;
1267
0
    case HTTP_STATUS_GONE :
1268
0
        s = _("Gone");
1269
0
  break;
1270
0
    case HTTP_STATUS_LENGTH_REQUIRED :
1271
0
        s = _("Length Required");
1272
0
  break;
1273
0
    case HTTP_STATUS_PRECONDITION_FAILED :
1274
0
        s = _("Precondition Failed");
1275
0
  break;
1276
0
    case HTTP_STATUS_CONTENT_TOO_LARGE :
1277
0
        s = _("Content Too Large");
1278
0
  break;
1279
0
    case HTTP_STATUS_URI_TOO_LONG :
1280
0
        s = _("URI Too Long");
1281
0
  break;
1282
0
    case HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE :
1283
0
        s = _("Unsupported Media Type");
1284
0
  break;
1285
0
    case HTTP_STATUS_RANGE_NOT_SATISFIABLE :
1286
0
        s = _("Range Not Satisfiable");
1287
0
  break;
1288
0
    case HTTP_STATUS_EXPECTATION_FAILED :
1289
0
        s = _("Expectation Failed");
1290
0
  break;
1291
0
    case HTTP_STATUS_MISDIRECTED_REQUEST :
1292
0
        s = _("Misdirected Request");
1293
0
  break;
1294
0
    case HTTP_STATUS_UNPROCESSABLE_CONTENT :
1295
0
        s = _("Unprocessable Content");
1296
0
  break;
1297
0
    case HTTP_STATUS_LOCKED :
1298
0
        s = _("Locked");
1299
0
  break;
1300
0
    case HTTP_STATUS_FAILED_DEPENDENCY :
1301
0
        s = _("Failed Dependency");
1302
0
  break;
1303
0
    case HTTP_STATUS_TOO_EARLY :
1304
0
        s = _("Too Early");
1305
0
  break;
1306
0
    case HTTP_STATUS_UPGRADE_REQUIRED :
1307
0
        s = _("Upgrade Required");
1308
0
  break;
1309
0
    case HTTP_STATUS_PRECONDITION_REQUIRED :
1310
0
        s = _("Precondition Required");
1311
0
  break;
1312
0
    case HTTP_STATUS_TOO_MANY_REQUESTS :
1313
0
        s = _("Too Many Requests");
1314
0
  break;
1315
0
    case HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE :
1316
0
        s = _("Request Header Fields Too Large");
1317
0
  break;
1318
0
    case HTTP_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS :
1319
0
        s = _("Unavailable for Legal Reasons");
1320
0
  break;
1321
0
    case HTTP_STATUS_SERVER_ERROR :
1322
0
        s = _("Server Error");
1323
0
  break;
1324
0
    case HTTP_STATUS_NOT_IMPLEMENTED :
1325
0
        s = _("Not Implemented");
1326
0
  break;
1327
0
    case HTTP_STATUS_BAD_GATEWAY :
1328
0
        s = _("Bad Gateway");
1329
0
  break;
1330
0
    case HTTP_STATUS_SERVICE_UNAVAILABLE :
1331
0
        s = _("Service Unavailable");
1332
0
  break;
1333
0
    case HTTP_STATUS_GATEWAY_TIMEOUT :
1334
0
        s = _("Gateway Timeout");
1335
0
  break;
1336
0
    case HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED :
1337
0
        s = _("HTTP Version Not Supported");
1338
0
        break;
1339
0
    case HTTP_STATUS_INSUFFICIENT_STORAGE :
1340
0
        s = _("Insufficient Storage");
1341
0
        break;
1342
0
    case HTTP_STATUS_LOOP_DETECTED :
1343
0
        s = _("Loop Detected");
1344
0
        break;
1345
0
    case HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED :
1346
0
        s = _("Network Authentication Required");
1347
0
        break;
1348
0
    case HTTP_STATUS_CUPS_PKI_ERROR :
1349
0
        s = _("TLS Negotiation Error");
1350
0
  break;
1351
0
    default :
1352
0
        snprintf(cg->http_status, sizeof(cg->http_status), "%d", (int)status);
1353
0
        return (cg->http_status);
1354
0
  }
1355
1356
0
  return (cupsLangGetString(lang, s));
1357
0
}
1358
1359
1360
//
1361
// 'httpStatusString()' - Return a short string describing a HTTP status code.
1362
//
1363
// This function returns a short (localized) string describing a HTTP status
1364
// code.  The strings are taken from the IANA HTTP Status Code registry.
1365
//
1366
1367
const char *        // O - Localized status string
1368
httpStatusString(http_status_t status)  // I - HTTP status code
1369
0
{
1370
0
  _cups_globals_t *cg = _cupsGlobals(); // Global data
1371
1372
1373
0
  if (!cg->lang_default)
1374
0
    cg->lang_default = cupsLangDefault();
1375
1376
0
  return (_httpStatusString(cg->lang_default, status));
1377
0
}
1378
1379
1380
//
1381
// 'httpURIStatusString()' - Return a string describing a URI status value.
1382
//
1383
// This function returns a short (localized) string describing a URI status
1384
// value.
1385
//
1386
1387
const char *        // O - Localized status string
1388
httpURIStatusString(
1389
    http_uri_status_t status)   // I - URI status value
1390
0
{
1391
0
  const char  *s;     // Status string
1392
0
  _cups_globals_t *cg = _cupsGlobals(); // Global data
1393
1394
1395
0
  if (!cg->lang_default)
1396
0
    cg->lang_default = cupsLangDefault();
1397
1398
0
  switch (status)
1399
0
  {
1400
0
    case HTTP_URI_STATUS_OVERFLOW :
1401
0
  s = _("URI too large");
1402
0
  break;
1403
0
    case HTTP_URI_STATUS_BAD_ARGUMENTS :
1404
0
  s = _("Bad arguments to function");
1405
0
  break;
1406
0
    case HTTP_URI_STATUS_BAD_RESOURCE :
1407
0
  s = _("Bad resource in URI");
1408
0
  break;
1409
0
    case HTTP_URI_STATUS_BAD_PORT :
1410
0
  s = _("Bad port number in URI");
1411
0
  break;
1412
0
    case HTTP_URI_STATUS_BAD_HOSTNAME :
1413
0
  s = _("Bad hostname/address in URI");
1414
0
  break;
1415
0
    case HTTP_URI_STATUS_BAD_USERNAME :
1416
0
  s = _("Bad username in URI");
1417
0
  break;
1418
0
    case HTTP_URI_STATUS_BAD_SCHEME :
1419
0
  s = _("Bad scheme in URI");
1420
0
  break;
1421
0
    case HTTP_URI_STATUS_BAD_URI :
1422
0
  s = _("Bad/empty URI");
1423
0
  break;
1424
0
    case HTTP_URI_STATUS_OK :
1425
0
  s = _("OK");
1426
0
  break;
1427
0
    case HTTP_URI_STATUS_MISSING_SCHEME :
1428
0
  s = _("Missing scheme in URI");
1429
0
  break;
1430
0
    case HTTP_URI_STATUS_UNKNOWN_SCHEME :
1431
0
  s = _("Unknown scheme in URI");
1432
0
  break;
1433
0
    case HTTP_URI_STATUS_MISSING_RESOURCE :
1434
0
  s = _("Missing resource in URI");
1435
0
  break;
1436
1437
0
    default:
1438
0
        s = _("Unknown");
1439
0
  break;
1440
0
  }
1441
1442
0
  return (cupsLangGetString(cg->lang_default, s));
1443
0
}
1444
1445
1446
#ifndef HAVE_HSTRERROR
1447
//
1448
// '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others.
1449
//
1450
1451
const char *        // O - Error string
1452
_cups_hstrerror(int error)    // I - Error number
1453
{
1454
  static const char * const errors[] =  // Error strings
1455
  {
1456
    "OK",
1457
    "Host not found.",
1458
    "Try again.",
1459
    "Unrecoverable lookup error.",
1460
    "No data associated with name."
1461
  };
1462
1463
1464
  if (error < 0 || error > 4)
1465
    return ("Unknown hostname lookup error.");
1466
  else
1467
    return (errors[error]);
1468
}
1469
#endif // !HAVE_HSTRERROR
1470
1471
1472
//
1473
// '_httpDecodeURI()' - Percent-decode a HTTP request URI.
1474
//
1475
1476
char *          // O - Decoded URI or NULL on error
1477
_httpDecodeURI(char       *dst,   // I - Destination buffer
1478
               const char *src,   // I - Source URI
1479
         size_t     dstsize)  // I - Size of destination buffer
1480
0
{
1481
0
  if (http_copy_decode(dst, src, dstsize, NULL, true))
1482
0
    return (dst);
1483
0
  else
1484
0
    return (NULL);
1485
0
}
1486
1487
1488
//
1489
// '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1490
//
1491
1492
char *          // O - Encoded URI
1493
_httpEncodeURI(char       *dst,   // I - Destination buffer
1494
               const char *src,   // I - Source URI
1495
         size_t     dstsize)  // I - Size of destination buffer
1496
0
{
1497
0
  http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, true);
1498
0
  return (dst);
1499
0
}
1500
1501
1502
//
1503
// 'httpResolveURI()' - Resolve a DNS-SD URI.
1504
//
1505
// This function resolves a DNS-SD URI of the form
1506
// "scheme://service-instance-name._protocol._tcp.domain/...".  The "options"
1507
// argument specifies a bitfield of resolution options including:
1508
//
1509
// - `HTTP_RESOLVE_DEFAULT`: Use default options
1510
// - `HTTP_RESOLVE_FQDN`: Resolve the fully-qualified domain name instead of an IP address
1511
// - `HTTP_RESOLVE_FAXOUT`: Resolve the FaxOut service instead of Print (IPP/IPPS)
1512
//
1513
// The "cb" argument specifies a callback that allows resolution to be
1514
// terminated.  The callback is provided the "cb_data" value and returns a
1515
// `bool` value that is `true` to continue and `false` to stop.  If no callback
1516
// is specified ("cb" is `NULL`), then this function will block up to 90 seconds
1517
// to resolve the specified URI.
1518
//
1519
1520
const char *        // O - Resolved URI
1521
httpResolveURI(
1522
    const char        *uri,   // I - DNS-SD URI
1523
    char              *resolved_uri,  // I - Buffer for resolved URI
1524
    size_t            resolved_size,  // I - Size of URI buffer
1525
    http_resolve_t    options,    // I - Resolve options
1526
    http_resolve_cb_t cb,   // I - Continue callback function
1527
    void              *cb_data)   // I - Context pointer for callback
1528
0
{
1529
0
  char      scheme[32], // URI components...
1530
0
      userpass[256],
1531
0
      hostname[1024],
1532
0
      resource[1024];
1533
0
  int     port;
1534
#ifdef DEBUG
1535
  http_uri_status_t status;   // URI decode status
1536
#endif // DEBUG
1537
1538
1539
0
  DEBUG_printf("httpResolveURI(uri=\"%s\", resolved_uri=%p, resolved_size=" CUPS_LLFMT ", options=0x%x, cb=%p, cb_data=%p)", uri, (void *)resolved_uri, CUPS_LLCAST resolved_size, options, (void *)cb, cb_data);
1540
1541
  // Get the device URI...
1542
#ifdef DEBUG
1543
  if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource))) < HTTP_URI_STATUS_OK)
1544
#else
1545
0
  if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
1546
0
#endif // DEBUG
1547
0
  {
1548
0
    DEBUG_printf("2httpResolveURI: httpSeparateURI returned %d.", status);
1549
0
    DEBUG_puts("1httpResolveURI: Returning NULL");
1550
0
    return (NULL);
1551
0
  }
1552
1553
  // Resolve it as needed...
1554
0
  if (strstr(hostname, "._tcp"))
1555
0
  {
1556
0
    time_t    domain_time,  // Domain lookup time, if any
1557
0
      end_time; // End time for resolve
1558
0
    cups_dnssd_t  *dnssd;   // DNS-SD context
1559
0
    uint32_t    if_index; // Interface index
1560
0
    char    name[256],  // Service instance name
1561
0
      regtype[256], // Registration type
1562
0
      domain[256],  // Domain name
1563
0
      *uuid,    // Pointer to UUID in URI
1564
0
      *uuidend; // Pointer to end of UUID in URI
1565
0
    _http_uribuf_t  uribuf;   // URI buffer
1566
1567
    // Separate the hostname into service name, registration type, and domain...
1568
0
    if (!cupsDNSSDSeparateFullName(hostname, name, sizeof(name), regtype, sizeof(regtype), domain, sizeof(domain)))
1569
0
    {
1570
0
      DEBUG_puts("2httpResolveURI: Bad hostname, returning NULL");
1571
0
      return (NULL);
1572
0
    }
1573
1574
0
    if ((uuid = strstr(resource, "?uuid=")) != NULL)
1575
0
    {
1576
0
      *uuid = '\0';
1577
0
      uuid  += 6;
1578
0
      if ((uuidend = strchr(uuid, '&')) != NULL)
1579
0
        *uuidend = '\0';
1580
0
    }
1581
1582
0
    resolved_uri[0] = '\0';
1583
1584
0
    uribuf.buffer   = resolved_uri;
1585
0
    uribuf.bufsize  = resolved_size;
1586
0
    uribuf.options  = options;
1587
0
    uribuf.resource = resource;
1588
0
    uribuf.uuid     = uuid;
1589
1590
0
    DEBUG_printf("2httpResolveURI: Resolving name=\"%s\", regtype=\"%s\",  domain=\"%s\"\n", name, regtype, domain);
1591
1592
0
    uri = NULL;
1593
1594
0
    if (!strcmp(scheme, "ippusb"))
1595
0
      if_index = CUPS_DNSSD_IF_INDEX_LOCAL;
1596
0
    else
1597
0
      if_index = CUPS_DNSSD_IF_INDEX_ANY;
1598
1599
0
    dnssd = cupsDNSSDNew(NULL, NULL);
1600
1601
0
    if (!cupsDNSSDResolveNew(dnssd, if_index, name, regtype, "local.", http_resolve_cb, &uribuf))
1602
0
    {
1603
0
      cupsDNSSDDelete(dnssd);
1604
0
      return (NULL);
1605
0
    }
1606
1607
0
    domain_time = time(NULL) + 2;
1608
0
    end_time    = time(NULL) + 90;
1609
1610
0
    while (!resolved_uri[0] && time(NULL) < end_time)
1611
0
    {
1612
      // Start the domain resolve as needed...
1613
0
      if (time(NULL) >= domain_time && _cups_strcasecmp(domain, "local."))
1614
0
      {
1615
0
  cupsDNSSDResolveNew(dnssd, if_index, name, regtype, domain, http_resolve_cb, &uribuf);
1616
0
  domain_time = end_time;
1617
0
      }
1618
1619
      // Sleep 1/4 second to allow time for resolve...
1620
0
      usleep(250000);
1621
1622
0
      if (resolved_uri[0])
1623
0
        break;
1624
0
    }
1625
1626
0
    cupsDNSSDDelete(dnssd);
1627
1628
    // Save the results of the resolve...
1629
0
    uri = *resolved_uri ? resolved_uri : NULL;
1630
0
  }
1631
0
  else
1632
0
  {
1633
    // Nothing more to do...
1634
0
    cupsCopyString(resolved_uri, uri, resolved_size);
1635
0
    uri = resolved_uri;
1636
0
  }
1637
1638
0
  DEBUG_printf("2httpResolveURI: Returning \"%s\"", uri);
1639
1640
0
  return (uri);
1641
0
}
1642
1643
1644
//
1645
// 'http_copy_decode()' - Copy and decode a URI.
1646
//
1647
1648
static const char *     // O - New source pointer or NULL on error
1649
http_copy_decode(char       *dst, // O - Destination buffer
1650
                 const char *src, // I - Source pointer
1651
     size_t     dstsize,  // I - Destination size
1652
     const char *term,  // I - Terminating characters
1653
     bool       decode) // I - Decode %-encoded values?
1654
0
{
1655
0
  char  *ptr,       // Pointer into buffer
1656
0
  *end;       // End of buffer
1657
0
  int quoted;       // Quoted character
1658
1659
1660
  // Copy the src to the destination until we hit a terminating character
1661
  // or the end of the string.
1662
0
  for (ptr = dst, end = dst + dstsize - 1; *src && (!term || !strchr(term, *src)); src ++)
1663
0
  {
1664
0
    if (ptr < end)
1665
0
    {
1666
0
      if (*src == '%' && decode)
1667
0
      {
1668
0
        if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255))
1669
0
  {
1670
    // Grab a hex-encoded character...
1671
0
          src ++;
1672
0
    if (isalpha(*src))
1673
0
      quoted = (tolower(*src) - 'a' + 10) << 4;
1674
0
    else
1675
0
      quoted = (*src - '0') << 4;
1676
1677
0
          src ++;
1678
0
    if (isalpha(*src))
1679
0
      quoted |= tolower(*src) - 'a' + 10;
1680
0
    else
1681
0
      quoted |= *src - '0';
1682
1683
0
          *ptr++ = (char)quoted;
1684
0
  }
1685
0
  else
1686
0
  {
1687
    // Bad hex-encoded character...
1688
0
    *ptr = '\0';
1689
0
    return (NULL);
1690
0
  }
1691
0
      }
1692
0
      else if ((*src & 255) <= 0x20 || (*src & 255) >= 0x7f)
1693
0
      {
1694
        // Bad control character...
1695
0
        *ptr = '\0';
1696
0
        return (NULL);
1697
0
      }
1698
0
      else
1699
0
      {
1700
        // Good literal...
1701
0
  *ptr++ = *src;
1702
0
      }
1703
0
    }
1704
0
  }
1705
1706
0
  *ptr = '\0';
1707
1708
0
  return (src);
1709
0
}
1710
1711
1712
//
1713
// 'http_copy_encode()' - Copy and encode a URI.
1714
//
1715
1716
static char *       // O - End of current URI
1717
http_copy_encode(char       *dst, // O - Destination buffer
1718
                 const char *src, // I - Source pointer
1719
     char       *dstend,  // I - End of destination buffer
1720
                 const char *reserved,  // I - Extra reserved characters
1721
     const char *term,  // I - Terminating characters
1722
     bool       encode) // I - %-encode reserved chars?
1723
0
{
1724
0
  static const char hex[] = "0123456789ABCDEF";
1725
1726
1727
0
  while (*src && dst < dstend)
1728
0
  {
1729
0
    if (term && *src == *term)
1730
0
      return (dst);
1731
1732
0
    if (encode && (*src == '%' || *src <= ' ' || *src & 128 || (reserved && strchr(reserved, *src))))
1733
0
    {
1734
      // Hex encode reserved characters...
1735
0
      if ((dst + 2) >= dstend)
1736
0
        break;
1737
1738
0
      *dst++ = '%';
1739
0
      *dst++ = hex[(*src >> 4) & 15];
1740
0
      *dst++ = hex[*src & 15];
1741
1742
0
      src ++;
1743
0
    }
1744
0
    else
1745
0
    {
1746
      // Copy literal...
1747
0
      *dst++ = *src++;
1748
0
    }
1749
0
  }
1750
1751
0
  *dst = '\0';
1752
1753
0
  if (*src)
1754
0
    return (NULL);
1755
0
  else
1756
0
    return (dst);
1757
0
}
1758
1759
1760
//
1761
// 'http_resolve_cb()' - Build a device URI for the given service name.
1762
//
1763
1764
static void
1765
http_resolve_cb(
1766
    cups_dnssd_resolve_t *res,    // I - Resolver
1767
    void                 *cb_data,  // I - Pointer to URI buffer
1768
    cups_dnssd_flags_t   flags,   // I - Results flags
1769
    uint32_t             if_index,  // I - Interface index
1770
    const char           *fullname, // I - Full service name
1771
    const char           *host,   // I - Hostname
1772
    uint16_t             port,    // I - Port number
1773
    size_t               num_txt, // I - Number of TXT key/value pairs
1774
    cups_option_t        *txt)    // I - TXT key/value pairs
1775
0
{
1776
0
  _http_uribuf_t  *uribuf = (_http_uribuf_t *)cb_data;
1777
          // URI buffer
1778
0
  const char    *scheme,  // URI scheme
1779
0
      *hostptr, // Pointer into hostTarget
1780
0
      *reskey,  // "rp" or "rfo"
1781
0
      *resdefault;  // Default path
1782
0
  char      fqdn[256];  // FQDN of the .local name
1783
0
  const char    *value,   // Value from TXT record
1784
0
      *resource;  // Resource path
1785
1786
1787
0
  DEBUG_printf("4http_resolve_cb(res=%p, cb_data=%p, flags=%x, if_index=%u, fullname=\"%s\", host=\"%s\", port=%u, num_txt=%u, txt=%p)", (void *)res, cb_data, flags, if_index, fullname, host, port, (unsigned)num_txt, (void *)txt);
1788
1789
  // If we have a UUID, compare it...
1790
0
  if (uribuf->uuid && (value = cupsGetOption("UUID", num_txt, txt)) != NULL)
1791
0
  {
1792
0
    if (_cups_strcasecmp(value, uribuf->uuid))
1793
0
    {
1794
0
      DEBUG_printf("5http_resolve_cb: Found UUID %s, looking for %s.", value, uribuf->uuid);
1795
0
      return;
1796
0
    }
1797
0
  }
1798
1799
  // Figure out the scheme from the full name...
1800
0
  if (strstr(fullname, "._ipps") || strstr(fullname, "._ipp-tls"))
1801
0
    scheme = "ipps";
1802
0
  else if (strstr(fullname, "._ipp") || strstr(fullname, "._fax-ipp"))
1803
0
    scheme = "ipp";
1804
0
  else if (strstr(fullname, "._http."))
1805
0
    scheme = "http";
1806
0
  else if (strstr(fullname, "._https."))
1807
0
    scheme = "https";
1808
0
  else if (strstr(fullname, "._printer."))
1809
0
    scheme = "lpd";
1810
0
  else if (strstr(fullname, "._pdl-datastream."))
1811
0
    scheme = "socket";
1812
0
  else
1813
0
    scheme = "riousbprint";
1814
1815
  // Extract the "remote printer" key from the TXT record...
1816
0
  if ((uribuf->options & HTTP_RESOLVE_FAXOUT) && (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && !cupsGetOption("printer-type", num_txt, txt))
1817
0
  {
1818
0
    reskey     = "rfo";
1819
0
    resdefault = "ipp/faxout";
1820
0
  }
1821
0
  else
1822
0
  {
1823
0
    reskey     = "rp";
1824
0
    resdefault = "";
1825
0
  }
1826
1827
0
  if ((resource = cupsGetOption(reskey, num_txt, txt)) != NULL)
1828
0
  {
1829
    // Use the resource path from the TXT record...
1830
0
    if (*resource == '/')
1831
0
    {
1832
      // Value (incorrectly) has a leading slash already...
1833
0
      resource ++;
1834
0
    }
1835
0
  }
1836
0
  else
1837
0
  {
1838
    // Use the default resource path...
1839
0
    resource = resdefault;
1840
0
  }
1841
1842
  // Lookup the FQDN if needed...
1843
0
  if ((uribuf->options & HTTP_RESOLVE_FQDN) && (hostptr = host + strlen(host) - 7) > host && !_cups_strcasecmp(hostptr, ".local."))
1844
0
  {
1845
    // OK, we got a .local name but the caller needs a real domain.  Start by
1846
    // getting the IP address of the .local name and then do reverse-lookups...
1847
0
    http_addrlist_t *addrlist,  // List of addresses
1848
0
      *addr;    // Current address
1849
1850
0
    DEBUG_printf("5http_resolve_cb: Looking up \"%s\".", host);
1851
1852
0
    snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
1853
0
    if ((addrlist = httpAddrGetList(host, AF_UNSPEC, fqdn)) != NULL)
1854
0
    {
1855
0
      for (addr = addrlist; addr; addr = addr->next)
1856
0
      {
1857
0
        int error = getnameinfo(&(addr->addr.addr), (socklen_t)httpAddrGetLength(&(addr->addr)), fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
1858
1859
0
        if (!error)
1860
0
  {
1861
0
    DEBUG_printf("5http_resolve_cb: Found \"%s\".", fqdn);
1862
1863
0
    if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn || _cups_strcasecmp(hostptr, ".local"))
1864
0
    {
1865
0
      host = fqdn;
1866
0
      break;
1867
0
    }
1868
0
  }
1869
#ifdef DEBUG
1870
  else
1871
    DEBUG_printf("5http_resolve_cb: \"%s\" did not resolve: %d", httpAddrGetString(&(addr->addr), fqdn, sizeof(fqdn)), error);
1872
#endif // DEBUG
1873
0
      }
1874
1875
0
      httpAddrFreeList(addrlist);
1876
0
    }
1877
0
  }
1878
1879
  // Assemble the final URI...
1880
0
  if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && !strcmp(uribuf->resource, "/cups"))
1881
0
    httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme, NULL, host, port, "/%s?snmp=false", resource);
1882
0
  else
1883
0
    httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme, NULL, host, port, "/%s", resource);
1884
1885
0
  DEBUG_printf("5http_resolve_cb: Resolved URI is \"%s\"...", uribuf->buffer);
1886
0
}