]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/http-support.c
Support new launch_activate_socket API on OS X (<rdar://problem/16069952>)
[thirdparty/cups.git] / cups / http-support.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
5ec1fd3d 4 * HTTP support routines for CUPS.
ef416fc2 5 *
7e86f2f6 6 * Copyright 2007-2014 by Apple Inc.
5ec1fd3d 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
5ec1fd3d
MS
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
5ec1fd3d 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 */
17
18/*
19 * Include necessary headers...
20 */
21
71e16022 22#include "cups-private.h"
5eb9da71
MS
23#ifdef HAVE_DNSSD
24# include <dns_sd.h>
6d2f911b
MS
25# ifdef WIN32
26# include <io.h>
27# elif defined(HAVE_POLL)
28# include <poll.h>
29# else
30# include <sys/select.h>
31# endif /* WIN32 */
f3c17241
MS
32#elif defined(HAVE_AVAHI)
33# include <avahi-client/client.h>
34# include <avahi-client/lookup.h>
35# include <avahi-common/simple-watch.h>
5eb9da71
MS
36#endif /* HAVE_DNSSD */
37
38
39/*
40 * Local types...
41 */
42
43typedef struct _http_uribuf_s /* URI buffer */
44{
f3c17241
MS
45#ifdef HAVE_AVAHI
46 AvahiSimplePoll *poll; /* Poll state */
47#endif /* HAVE_AVAHI */
48 char *buffer; /* Pointer to buffer */
49 size_t bufsize; /* Size of buffer */
50 int options; /* Options passed to _httpResolveURI */
5a9febac 51 const char *resource; /* Resource from URI */
2c3f5865 52 const char *uuid; /* UUID from URI */
5eb9da71 53} _http_uribuf_t;
ef416fc2 54
55
56/*
57 * Local globals...
58 */
59
5ec1fd3d 60static const char * const http_days[7] =/* Days of the week */
ef416fc2 61 {
0a682745
MS
62 "Sun",
63 "Mon",
ef416fc2 64 "Tue",
65 "Wed",
66 "Thu",
67 "Fri",
68 "Sat"
69 };
70static const char * const http_months[12] =
5ec1fd3d 71 { /* Months of the year */
ef416fc2 72 "Jan",
73 "Feb",
74 "Mar",
75 "Apr",
76 "May",
77 "Jun",
78 "Jul",
79 "Aug",
80 "Sep",
81 "Oct",
82 "Nov",
83 "Dec"
84 };
5ec1fd3d
MS
85static const char * const http_states[] =
86 { /* HTTP state strings */
87 "HTTP_STATE_ERROR",
88 "HTTP_STATE_WAITING",
89 "HTTP_STATE_OPTIONS",
90 "HTTP_STATE_GET",
91 "HTTP_STATE_GET_SEND",
92 "HTTP_STATE_HEAD",
93 "HTTP_STATE_POST",
94 "HTTP_STATE_POST_RECV",
95 "HTTP_STATE_POST_SEND",
96 "HTTP_STATE_PUT",
97 "HTTP_STATE_PUT_RECV",
98 "HTTP_STATE_DELETE",
99 "HTTP_STATE_TRACE",
100 "HTTP_STATE_CONNECT",
101 "HTTP_STATE_STATUS",
102 "HTTP_STATE_UNKNOWN_METHOD",
103 "HTTP_STATE_UNKNOWN_VERSION"
104 };
ef416fc2 105
106
107/*
108 * Local functions...
109 */
110
111static const char *http_copy_decode(char *dst, const char *src,
a4d04587 112 int dstsize, const char *term,
113 int decode);
ef416fc2 114static char *http_copy_encode(char *dst, const char *src,
a4d04587 115 char *dstend, const char *reserved,
116 const char *term, int encode);
5eb9da71 117#ifdef HAVE_DNSSD
eac3a0a0
MS
118static void DNSSD_API http_resolve_cb(DNSServiceRef sdRef,
119 DNSServiceFlags flags,
120 uint32_t interfaceIndex,
121 DNSServiceErrorType errorCode,
122 const char *fullName,
123 const char *hostTarget,
124 uint16_t port, uint16_t txtLen,
125 const unsigned char *txtRecord,
126 void *context);
5eb9da71 127#endif /* HAVE_DNSSD */
ef416fc2 128
f3c17241
MS
129#ifdef HAVE_AVAHI
130static void http_client_cb(AvahiClient *client,
131 AvahiClientState state, void *simple_poll);
132static int http_poll_cb(struct pollfd *pollfds, unsigned int num_pollfds,
133 int timeout, void *context);
134static void http_resolve_cb(AvahiServiceResolver *resolver,
135 AvahiIfIndex interface,
136 AvahiProtocol protocol,
137 AvahiResolverEvent event,
138 const char *name, const char *type,
139 const char *domain, const char *host_name,
140 const AvahiAddress *address, uint16_t port,
141 AvahiStringList *txt,
142 AvahiLookupResultFlags flags, void *context);
143#endif /* HAVE_AVAHI */
144
ef416fc2 145
146/*
147 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
148 * components.
149 *
ecdc0628 150 * This function escapes reserved characters in the URI depending on the
151 * value of the "encoding" argument. You should use this function in
152 * place of traditional string functions whenever you need to create a
153 * URI string.
ef416fc2 154 *
f3c17241 155 * @since CUPS 1.2/OS X 10.5@
ef416fc2 156 */
157
158http_uri_status_t /* O - URI status */
a4d04587 159httpAssembleURI(
160 http_uri_coding_t encoding, /* I - Encoding flags */
161 char *uri, /* I - URI buffer */
162 int urilen, /* I - Size of URI buffer */
163 const char *scheme, /* I - Scheme name */
164 const char *username, /* I - Username */
165 const char *host, /* I - Hostname or address */
166 int port, /* I - Port number */
167 const char *resource) /* I - Resource */
ef416fc2 168{
169 char *ptr, /* Pointer into URI buffer */
170 *end; /* End of URI buffer */
ef416fc2 171
172
173 /*
174 * Range check input...
175 */
176
177 if (!uri || urilen < 1 || !scheme || port < 0)
178 {
179 if (uri)
180 *uri = '\0';
181
cb7f98ee 182 return (HTTP_URI_STATUS_BAD_ARGUMENTS);
ef416fc2 183 }
184
185 /*
186 * Assemble the URI starting with the scheme...
187 */
188
189 end = uri + urilen - 1;
a4d04587 190 ptr = http_copy_encode(uri, scheme, end, NULL, NULL, 0);
ef416fc2 191
192 if (!ptr)
193 goto assemble_overflow;
194
f4047845 195 if (!strcmp(scheme, "mailto") || !strcmp(scheme, "tel"))
ef416fc2 196 {
197 /*
f4047845 198 * mailto: and tel: only have :, no //...
ef416fc2 199 */
200
201 if (ptr < end)
202 *ptr++ = ':';
203 else
204 goto assemble_overflow;
205 }
206 else
207 {
208 /*
f4047845 209 * Schemes other than mailto: and tel: all have //...
ef416fc2 210 */
211
212 if ((ptr + 2) < end)
213 {
214 *ptr++ = ':';
215 *ptr++ = '/';
216 *ptr++ = '/';
217 }
218 else
219 goto assemble_overflow;
220 }
221
222 /*
223 * Next the username and hostname, if any...
224 */
225
226 if (host)
227 {
ff862e50
MS
228 const char *hostptr; /* Pointer into hostname */
229 int have_ipv6; /* Do we have an IPv6 address? */
230
ef416fc2 231 if (username && *username)
232 {
233 /*
234 * Add username@ first...
235 */
236
85dda01c 237 ptr = http_copy_encode(ptr, username, end, "/?#[]@", NULL,
a4d04587 238 encoding & HTTP_URI_CODING_USERNAME);
ef416fc2 239
240 if (!ptr)
241 goto assemble_overflow;
242
243 if (ptr < end)
244 *ptr++ = '@';
245 else
246 goto assemble_overflow;
247 }
248
249 /*
250 * Then add the hostname. Since IPv6 is a particular pain to deal
c9fc04c6 251 * with, we have several special cases to deal with. If we get
ef416fc2 252 * an IPv6 address with brackets around it, assume it is already in
c9fc04c6
MS
253 * URI format. Since DNS-SD service names can sometimes look like
254 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
255 * too...
ef416fc2 256 */
257
ff862e50
MS
258 for (hostptr = host,
259 have_ipv6 = strchr(host, ':') && !strstr(host, "._tcp");
260 *hostptr && have_ipv6;
261 hostptr ++)
262 if (*hostptr != ':' && !isxdigit(*hostptr & 255))
263 {
264 have_ipv6 = *hostptr == '%';
265 break;
266 }
267
268 if (have_ipv6)
ef416fc2 269 {
270 /*
c9fc04c6 271 * We have a raw IPv6 address...
ef416fc2 272 */
273
86c809d9 274 if (strchr(host, '%') && !(encoding & HTTP_URI_CODING_RFC6874))
ef416fc2 275 {
276 /*
277 * We have a link-local address, add "[v1." prefix...
278 */
279
280 if ((ptr + 4) < end)
281 {
282 *ptr++ = '[';
283 *ptr++ = 'v';
284 *ptr++ = '1';
285 *ptr++ = '.';
286 }
287 else
288 goto assemble_overflow;
289 }
290 else
291 {
292 /*
ff862e50 293 * We have a normal (or RFC 6874 link-local) address, add "[" prefix...
ef416fc2 294 */
295
296 if (ptr < end)
297 *ptr++ = '[';
298 else
299 goto assemble_overflow;
300 }
301
302 /*
303 * Copy the rest of the IPv6 address, and terminate with "]".
304 */
305
306 while (ptr < end && *host)
307 {
308 if (*host == '%')
86c809d9
MS
309 {
310 /*
311 * Convert/encode zone separator
312 */
313
314 if (encoding & HTTP_URI_CODING_RFC6874)
315 {
316 if (ptr >= (end - 2))
317 goto assemble_overflow;
318
319 *ptr++ = '%';
320 *ptr++ = '2';
321 *ptr++ = '5';
322 }
323 else
324 *ptr++ = '+';
325
ef416fc2 326 host ++;
327 }
328 else
329 *ptr++ = *host++;
330 }
331
332 if (*host)
333 goto assemble_overflow;
334
335 if (ptr < end)
336 *ptr++ = ']';
337 else
338 goto assemble_overflow;
339 }
340 else
341 {
342 /*
ff862e50
MS
343 * Otherwise, just copy the host string (the extra chars are not in the
344 * "reg-name" ABNF rule; anything <= SP or >= DEL plus % gets automatically
345 * percent-encoded.
ef416fc2 346 */
ff862e50
MS
347
348 ptr = http_copy_encode(ptr, host, end, "\"#/:<>?@[\\]^`{|}", NULL,
a4d04587 349 encoding & HTTP_URI_CODING_HOSTNAME);
ef416fc2 350
351 if (!ptr)
352 goto assemble_overflow;
353 }
354
355 /*
356 * Finish things off with the port number...
357 */
358
359 if (port > 0)
360 {
361 snprintf(ptr, end - ptr + 1, ":%d", port);
362 ptr += strlen(ptr);
363
364 if (ptr >= end)
365 goto assemble_overflow;
366 }
367 }
368
369 /*
370 * Last but not least, add the resource string...
371 */
372
a4d04587 373 if (resource)
ef416fc2 374 {
fa73b229 375 char *query; /* Pointer to query string */
376
377
fa73b229 378 /*
a4d04587 379 * Copy the resource string up to the query string if present...
fa73b229 380 */
381
a4d04587 382 query = strchr(resource, '?');
383 ptr = http_copy_encode(ptr, resource, end, NULL, "?",
384 encoding & HTTP_URI_CODING_RESOURCE);
ef416fc2 385 if (!ptr)
386 goto assemble_overflow;
fa73b229 387
388 if (query)
389 {
390 /*
391 * Copy query string without encoding...
392 */
393
a4d04587 394 ptr = http_copy_encode(ptr, query, end, NULL, NULL,
395 encoding & HTTP_URI_CODING_QUERY);
396 if (!ptr)
397 goto assemble_overflow;
fa73b229 398 }
ef416fc2 399 }
400 else if (ptr < end)
401 *ptr++ = '/';
402 else
403 goto assemble_overflow;
404
405 /*
406 * Nul-terminate the URI buffer and return with no errors...
407 */
408
409 *ptr = '\0';
410
cb7f98ee 411 return (HTTP_URI_STATUS_OK);
ef416fc2 412
413 /*
414 * Clear the URI string and return an overflow error; I don't usually
415 * like goto's, but in this case it makes sense...
416 */
417
418 assemble_overflow:
419
420 *uri = '\0';
cb7f98ee 421 return (HTTP_URI_STATUS_OVERFLOW);
ef416fc2 422}
423
424
a4d04587 425/*
426 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
427 * components with a formatted resource.
428 *
429 * This function creates a formatted version of the resource string
ecdc0628 430 * argument "resourcef" and escapes reserved characters in the URI
431 * depending on the value of the "encoding" argument. You should use
432 * this function in place of traditional string functions whenever
433 * you need to create a URI string.
a4d04587 434 *
f3c17241 435 * @since CUPS 1.2/OS X 10.5@
a4d04587 436 */
437
438http_uri_status_t /* O - URI status */
439httpAssembleURIf(
440 http_uri_coding_t encoding, /* I - Encoding flags */
441 char *uri, /* I - URI buffer */
442 int urilen, /* I - Size of URI buffer */
443 const char *scheme, /* I - Scheme name */
444 const char *username, /* I - Username */
445 const char *host, /* I - Hostname or address */
446 int port, /* I - Port number */
447 const char *resourcef, /* I - Printf-style resource */
448 ...) /* I - Additional arguments as needed */
449{
450 va_list ap; /* Pointer to additional arguments */
451 char resource[1024]; /* Formatted resource string */
452 int bytes; /* Bytes in formatted string */
453
454
455 /*
456 * Range check input...
457 */
458
459 if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef)
460 {
461 if (uri)
462 *uri = '\0';
463
cb7f98ee 464 return (HTTP_URI_STATUS_BAD_ARGUMENTS);
a4d04587 465 }
466
467 /*
468 * Format the resource string and assemble the URI...
469 */
470
471 va_start(ap, resourcef);
472 bytes = vsnprintf(resource, sizeof(resource), resourcef, ap);
473 va_end(ap);
474
7e86f2f6 475 if ((size_t)bytes >= sizeof(resource))
a4d04587 476 {
477 *uri = '\0';
cb7f98ee 478 return (HTTP_URI_STATUS_OVERFLOW);
a4d04587 479 }
480 else
481 return (httpAssembleURI(encoding, uri, urilen, scheme, username, host,
482 port, resource));
483}
484
485
07ed0e9a 486/*
0fa6c7fa
MS
487 * 'httpAssembleUUID()' - Assemble a name-based UUID URN conforming to RFC 4122.
488 *
489 * This function creates a unique 128-bit identifying number using the server
490 * name, port number, random data, and optionally an object name and/or object
491 * number. The result is formatted as a UUID URN as defined in RFC 4122.
07ed0e9a
MS
492 *
493 * The buffer needs to be at least 46 bytes in size.
6961465f 494 *
9c0e8e5d 495 * @since CUPS 1.7/OS X 10.9@
07ed0e9a
MS
496 */
497
498char * /* I - UUID string */
0fa6c7fa
MS
499httpAssembleUUID(const char *server, /* I - Server name */
500 int port, /* I - Port number */
501 const char *name, /* I - Object name or NULL */
502 int number, /* I - Object number or 0 */
503 char *buffer, /* I - String buffer */
504 size_t bufsize) /* I - Size of buffer */
07ed0e9a
MS
505{
506 char data[1024]; /* Source string for MD5 */
507 _cups_md5_state_t md5state; /* MD5 state */
508 unsigned char md5sum[16]; /* MD5 digest/sum */
509
510
511 /*
512 * Build a version 3 UUID conforming to RFC 4122.
513 *
514 * Start with the MD5 sum of the server, port, object name and
515 * number, and some random data on the end.
516 */
517
518 snprintf(data, sizeof(data), "%s:%d:%s:%d:%04x:%04x", server,
519 port, name ? name : server, number,
f99f3698 520 (unsigned)CUPS_RAND() & 0xffff, (unsigned)CUPS_RAND() & 0xffff);
07ed0e9a
MS
521
522 _cupsMD5Init(&md5state);
7e86f2f6 523 _cupsMD5Append(&md5state, (unsigned char *)data, (int)strlen(data));
07ed0e9a
MS
524 _cupsMD5Finish(&md5state, md5sum);
525
526 /*
527 * Generate the UUID from the MD5...
528 */
529
530 snprintf(buffer, bufsize,
531 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
532 "%02x%02x%02x%02x%02x%02x",
533 md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5],
534 (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40,
535 md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13],
536 md5sum[14], md5sum[15]);
537
538 return (buffer);
539}
540
541
ef416fc2 542/*
543 * 'httpDecode64()' - Base64-decode a string.
ecdc0628 544 *
545 * This function is deprecated. Use the httpDecode64_2() function instead
546 * which provides buffer length arguments.
547 *
548 * @deprecated@
ef416fc2 549 */
550
551char * /* O - Decoded string */
552httpDecode64(char *out, /* I - String to write to */
553 const char *in) /* I - String to read from */
554{
555 int outlen; /* Output buffer length */
556
557
558 /*
559 * Use the old maximum buffer size for binary compatibility...
560 */
561
562 outlen = 512;
563
564 return (httpDecode64_2(out, &outlen, in));
565}
566
567
568/*
569 * 'httpDecode64_2()' - Base64-decode a string.
570 *
f3c17241 571 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 572 */
573
574char * /* O - Decoded string */
575httpDecode64_2(char *out, /* I - String to write to */
576 int *outlen, /* IO - Size of output string */
577 const char *in) /* I - String to read from */
578{
7e86f2f6
MS
579 int pos; /* Bit position */
580 unsigned base64; /* Value of this character */
581 char *outptr, /* Output pointer */
582 *outend; /* End of output buffer */
ef416fc2 583
584
585 /*
586 * Range check input...
587 */
588
fa73b229 589 if (!out || !outlen || *outlen < 1 || !in)
ef416fc2 590 return (NULL);
591
fa73b229 592 if (!*in)
593 {
594 *out = '\0';
595 *outlen = 0;
596
597 return (out);
598 }
599
ef416fc2 600 /*
601 * Convert from base-64 to bytes...
602 */
603
604 for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++)
605 {
606 /*
607 * Decode this character into a number from 0 to 63...
608 */
609
610 if (*in >= 'A' && *in <= 'Z')
7e86f2f6 611 base64 = (unsigned)(*in - 'A');
ef416fc2 612 else if (*in >= 'a' && *in <= 'z')
7e86f2f6 613 base64 = (unsigned)(*in - 'a' + 26);
ef416fc2 614 else if (*in >= '0' && *in <= '9')
7e86f2f6 615 base64 = (unsigned)(*in - '0' + 52);
ef416fc2 616 else if (*in == '+')
617 base64 = 62;
618 else if (*in == '/')
619 base64 = 63;
620 else if (*in == '=')
621 break;
622 else
623 continue;
624
625 /*
626 * Store the result in the appropriate chars...
627 */
628
629 switch (pos)
630 {
631 case 0 :
632 if (outptr < outend)
7e86f2f6 633 *outptr = (char)(base64 << 2);
ef416fc2 634 pos ++;
635 break;
636 case 1 :
637 if (outptr < outend)
7e86f2f6 638 *outptr++ |= (char)((base64 >> 4) & 3);
ef416fc2 639 if (outptr < outend)
7e86f2f6 640 *outptr = (char)((base64 << 4) & 255);
ef416fc2 641 pos ++;
642 break;
643 case 2 :
644 if (outptr < outend)
7e86f2f6 645 *outptr++ |= (char)((base64 >> 2) & 15);
ef416fc2 646 if (outptr < outend)
7e86f2f6 647 *outptr = (char)((base64 << 6) & 255);
ef416fc2 648 pos ++;
649 break;
650 case 3 :
651 if (outptr < outend)
652 *outptr++ |= base64;
653 pos = 0;
654 break;
655 }
656 }
657
658 *outptr = '\0';
659
660 /*
661 * Return the decoded string and size...
662 */
663
664 *outlen = (int)(outptr - out);
665
666 return (out);
667}
668
669
670/*
671 * 'httpEncode64()' - Base64-encode a string.
ecdc0628 672 *
673 * This function is deprecated. Use the httpEncode64_2() function instead
674 * which provides buffer length arguments.
675 *
676 * @deprecated@
ef416fc2 677 */
678
679char * /* O - Encoded string */
680httpEncode64(char *out, /* I - String to write to */
681 const char *in) /* I - String to read from */
682{
b86bc4cf 683 return (httpEncode64_2(out, 512, in, (int)strlen(in)));
ef416fc2 684}
685
686
687/*
688 * 'httpEncode64_2()' - Base64-encode a string.
689 *
f3c17241 690 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 691 */
692
693char * /* O - Encoded string */
694httpEncode64_2(char *out, /* I - String to write to */
695 int outlen, /* I - Size of output string */
696 const char *in, /* I - String to read from */
697 int inlen) /* I - Size of input string */
698{
699 char *outptr, /* Output pointer */
700 *outend; /* End of output buffer */
701 static const char base64[] = /* Base64 characters... */
702 {
703 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
704 "abcdefghijklmnopqrstuvwxyz"
705 "0123456789"
706 "+/"
707 };
708
709
710 /*
711 * Range check input...
712 */
713
714 if (!out || outlen < 1 || !in)
715 return (NULL);
716
717 /*
718 * Convert bytes to base-64...
719 */
720
721 for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --)
722 {
723 /*
724 * Encode the up to 3 characters as 4 Base64 numbers...
725 */
726
727 if (outptr < outend)
728 *outptr ++ = base64[(in[0] & 255) >> 2];
26d47ec6 729
ef416fc2 730 if (outptr < outend)
26d47ec6 731 {
732 if (inlen > 1)
733 *outptr ++ = base64[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63];
734 else
735 *outptr ++ = base64[((in[0] & 255) << 4) & 63];
736 }
ef416fc2 737
738 in ++;
739 inlen --;
740 if (inlen <= 0)
741 {
742 if (outptr < outend)
743 *outptr ++ = '=';
744 if (outptr < outend)
745 *outptr ++ = '=';
746 break;
747 }
748
749 if (outptr < outend)
26d47ec6 750 {
751 if (inlen > 1)
752 *outptr ++ = base64[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63];
753 else
754 *outptr ++ = base64[((in[0] & 255) << 2) & 63];
755 }
ef416fc2 756
757 in ++;
758 inlen --;
759 if (inlen <= 0)
760 {
761 if (outptr < outend)
762 *outptr ++ = '=';
763 break;
764 }
765
766 if (outptr < outend)
767 *outptr ++ = base64[in[0] & 63];
768 }
769
770 *outptr = '\0';
771
772 /*
773 * Return the encoded string...
774 */
775
776 return (out);
777}
778
779
780/*
781 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
782 *
783 * @deprecated@
784 */
785
786const char * /* O - Date/time string */
787httpGetDateString(time_t t) /* I - UNIX time */
788{
789 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
790
791
792 return (httpGetDateString2(t, cg->http_date, sizeof(cg->http_date)));
793}
794
795
796/*
797 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
798 *
f3c17241 799 * @since CUPS 1.2/OS X 10.5@
ef416fc2 800 */
801
802const char * /* O - Date/time string */
803httpGetDateString2(time_t t, /* I - UNIX time */
804 char *s, /* I - String buffer */
805 int slen) /* I - Size of string buffer */
806{
807 struct tm *tdate; /* UNIX date/time data */
808
809
810 tdate = gmtime(&t);
3e7fe0ca
MS
811 if (tdate)
812 snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT",
813 http_days[tdate->tm_wday], tdate->tm_mday,
814 http_months[tdate->tm_mon], tdate->tm_year + 1900,
815 tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
816 else
817 s[0] = '\0';
ef416fc2 818
819 return (s);
820}
821
822
823/*
824 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
825 */
826
827time_t /* O - UNIX time */
828httpGetDateTime(const char *s) /* I - Date/time string */
829{
830 int i; /* Looping var */
831 char mon[16]; /* Abbreviated month name */
832 int day, year; /* Day of month and year */
833 int hour, min, sec; /* Time */
834 int days; /* Number of days since 1970 */
835 static const int normal_days[] = /* Days to a month, normal years */
836 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
837 static const int leap_days[] = /* Days to a month, leap years */
838 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
839
840
e07d4801 841 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s));
ef416fc2 842
843 /*
844 * Extract the date and time from the formatted string...
845 */
846
847 if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6)
848 return (0);
849
e07d4801
MS
850 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
851 "min=%d, sec=%d", day, mon, year, hour, min, sec));
ef416fc2 852
853 /*
854 * Convert the month name to a number from 0 to 11.
855 */
856
857 for (i = 0; i < 12; i ++)
88f9aafc 858 if (!_cups_strcasecmp(mon, http_months[i]))
ef416fc2 859 break;
860
861 if (i >= 12)
862 return (0);
863
e07d4801 864 DEBUG_printf(("4httpGetDateTime: i=%d", i));
ef416fc2 865
866 /*
867 * Now convert the date and time to a UNIX time value in seconds since
868 * 1970. We can't use mktime() since the timezone may not be UTC but
869 * the date/time string *is* UTC.
870 */
871
872 if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0))
873 days = leap_days[i] + day - 1;
874 else
875 days = normal_days[i] + day - 1;
876
e07d4801 877 DEBUG_printf(("4httpGetDateTime: days=%d", days));
ef416fc2 878
879 days += (year - 1970) * 365 + /* 365 days per year (normally) */
880 ((year - 1) / 4 - 492) - /* + leap days */
881 ((year - 1) / 100 - 19) + /* - 100 year days */
882 ((year - 1) / 400 - 4); /* + 400 year days */
883
e07d4801 884 DEBUG_printf(("4httpGetDateTime: days=%d\n", days));
ef416fc2 885
886 return (days * 86400 + hour * 3600 + min * 60 + sec);
887}
888
889
890/*
891 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
892 * components.
ecdc0628 893 *
894 * This function is deprecated; use the httpSeparateURI() function instead.
895 *
896 * @deprecated@
ef416fc2 897 */
898
899void
900httpSeparate(const char *uri, /* I - Universal Resource Identifier */
901 char *scheme, /* O - Scheme [32] (http, https, etc.) */
902 char *username, /* O - Username [1024] */
903 char *host, /* O - Hostname [1024] */
904 int *port, /* O - Port number to use */
905 char *resource) /* O - Resource/filename [1024] */
906{
a4d04587 907 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, 32, username,
908 HTTP_MAX_URI, host, HTTP_MAX_URI, port, resource,
909 HTTP_MAX_URI);
ef416fc2 910}
911
912
913/*
914 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
915 * components.
916 *
ecdc0628 917 * This function is deprecated; use the httpSeparateURI() function instead.
918 *
f3c17241 919 * @since CUPS 1.1.21/OS X 10.4@
ecdc0628 920 * @deprecated@
ef416fc2 921 */
922
923void
924httpSeparate2(const char *uri, /* I - Universal Resource Identifier */
925 char *scheme, /* O - Scheme (http, https, etc.) */
926 int schemelen, /* I - Size of scheme buffer */
927 char *username, /* O - Username */
928 int usernamelen, /* I - Size of username buffer */
929 char *host, /* O - Hostname */
930 int hostlen, /* I - Size of hostname buffer */
931 int *port, /* O - Port number to use */
932 char *resource, /* O - Resource/filename */
933 int resourcelen) /* I - Size of resource buffer */
934{
a4d04587 935 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, schemelen, username,
936 usernamelen, host, hostlen, port, resource, resourcelen);
ef416fc2 937}
938
939
940/*
941 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
942 * components.
943 *
f3c17241 944 * @since CUPS 1.2/OS X 10.5@
ef416fc2 945 */
946
947http_uri_status_t /* O - Result of separation */
a4d04587 948httpSeparateURI(
949 http_uri_coding_t decoding, /* I - Decoding flags */
950 const char *uri, /* I - Universal Resource Identifier */
951 char *scheme, /* O - Scheme (http, https, etc.) */
952 int schemelen, /* I - Size of scheme buffer */
953 char *username, /* O - Username */
954 int usernamelen, /* I - Size of username buffer */
955 char *host, /* O - Hostname */
956 int hostlen, /* I - Size of hostname buffer */
957 int *port, /* O - Port number to use */
958 char *resource, /* O - Resource/filename */
959 int resourcelen) /* I - Size of resource buffer */
ef416fc2 960{
961 char *ptr, /* Pointer into string... */
962 *end; /* End of string */
963 const char *sep; /* Separator character */
964 http_uri_status_t status; /* Result of separation */
965
966
967 /*
968 * Initialize everything to blank...
969 */
970
971 if (scheme && schemelen > 0)
972 *scheme = '\0';
973
974 if (username && usernamelen > 0)
975 *username = '\0';
976
977 if (host && hostlen > 0)
978 *host = '\0';
979
980 if (port)
981 *port = 0;
982
983 if (resource && resourcelen > 0)
984 *resource = '\0';
985
986 /*
987 * Range check input...
988 */
989
990 if (!uri || !port || !scheme || schemelen <= 0 || !username ||
991 usernamelen <= 0 || !host || hostlen <= 0 || !resource ||
992 resourcelen <= 0)
cb7f98ee 993 return (HTTP_URI_STATUS_BAD_ARGUMENTS);
ef416fc2 994
995 if (!*uri)
cb7f98ee 996 return (HTTP_URI_STATUS_BAD_URI);
ef416fc2 997
998 /*
999 * Grab the scheme portion of the URI...
1000 */
1001
cb7f98ee 1002 status = HTTP_URI_STATUS_OK;
ef416fc2 1003
1004 if (!strncmp(uri, "//", 2))
1005 {
1006 /*
1007 * Workaround for HP IPP client bug...
1008 */
1009
1010 strlcpy(scheme, "ipp", schemelen);
cb7f98ee 1011 status = HTTP_URI_STATUS_MISSING_SCHEME;
ef416fc2 1012 }
1013 else if (*uri == '/')
1014 {
1015 /*
1016 * Filename...
1017 */
1018
1019 strlcpy(scheme, "file", schemelen);
cb7f98ee 1020 status = HTTP_URI_STATUS_MISSING_SCHEME;
ef416fc2 1021 }
1022 else
1023 {
1024 /*
1025 * Standard URI with scheme...
1026 */
1027
1028 for (ptr = scheme, end = scheme + schemelen - 1;
1029 *uri && *uri != ':' && ptr < end;)
7cf5915e
MS
1030 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1031 "abcdefghijklmnopqrstuvwxyz"
1032 "0123456789-+.", *uri) != NULL)
ef416fc2 1033 *ptr++ = *uri++;
1034 else
1035 break;
1036
1037 *ptr = '\0';
1038
1039 if (*uri != ':')
1040 {
1041 *scheme = '\0';
cb7f98ee 1042 return (HTTP_URI_STATUS_BAD_SCHEME);
ef416fc2 1043 }
1044
1045 uri ++;
1046 }
1047
1048 /*
1049 * Set the default port number...
1050 */
1051
1052 if (!strcmp(scheme, "http"))
1053 *port = 80;
1054 else if (!strcmp(scheme, "https"))
1055 *port = 443;
7cf5915e 1056 else if (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps"))
ef416fc2 1057 *port = 631;
88f9aafc 1058 else if (!_cups_strcasecmp(scheme, "lpd"))
ef416fc2 1059 *port = 515;
1060 else if (!strcmp(scheme, "socket")) /* Not yet registered with IANA... */
1061 *port = 9100;
f4047845 1062 else if (strcmp(scheme, "file") && strcmp(scheme, "mailto") && strcmp(scheme, "tel"))
cb7f98ee 1063 status = HTTP_URI_STATUS_UNKNOWN_SCHEME;
ef416fc2 1064
1065 /*
1066 * Now see if we have a hostname...
1067 */
1068
1069 if (!strncmp(uri, "//", 2))
1070 {
1071 /*
1072 * Yes, extract it...
1073 */
1074
1075 uri += 2;
1076
1077 /*
1078 * Grab the username, if any...
1079 */
1080
1081 if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@')
1082 {
1083 /*
1084 * Get a username:password combo...
1085 */
1086
a4d04587 1087 uri = http_copy_decode(username, uri, usernamelen, "@",
1088 decoding & HTTP_URI_CODING_USERNAME);
ef416fc2 1089
1090 if (!uri)
1091 {
1092 *username = '\0';
cb7f98ee 1093 return (HTTP_URI_STATUS_BAD_USERNAME);
ef416fc2 1094 }
1095
1096 uri ++;
1097 }
1098
1099 /*
1100 * Then the hostname/IP address...
1101 */
1102
1103 if (*uri == '[')
1104 {
1105 /*
1106 * Grab IPv6 address...
1107 */
1108
1109 uri ++;
86c809d9
MS
1110 if (*uri == 'v')
1111 {
1112 /*
1113 * Skip IPvFuture ("vXXXX.") prefix...
1114 */
1115
1116 uri ++;
1117
1118 while (isxdigit(*uri & 255))
1119 uri ++;
1120
1121 if (*uri != '.')
1122 {
1123 *host = '\0';
1124 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1125 }
1126
1127 uri ++;
1128 }
ef416fc2 1129
a4d04587 1130 uri = http_copy_decode(host, uri, hostlen, "]",
1131 decoding & HTTP_URI_CODING_HOSTNAME);
ef416fc2 1132
1133 if (!uri)
1134 {
1135 *host = '\0';
cb7f98ee 1136 return (HTTP_URI_STATUS_BAD_HOSTNAME);
ef416fc2 1137 }
1138
1139 /*
1140 * Validate value...
1141 */
1142
1143 if (*uri != ']')
1144 {
1145 *host = '\0';
cb7f98ee 1146 return (HTTP_URI_STATUS_BAD_HOSTNAME);
ef416fc2 1147 }
1148
1149 uri ++;
1150
1151 for (ptr = host; *ptr; ptr ++)
1152 if (*ptr == '+')
1153 {
1154 /*
1155 * Convert zone separator to % and stop here...
1156 */
1157
1158 *ptr = '%';
1159 break;
1160 }
86c809d9
MS
1161 else if (*ptr == '%')
1162 {
1163 /*
1164 * Stop at zone separator (RFC 6874)
1165 */
1166
1167 break;
1168 }
ef416fc2 1169 else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255))
1170 {
1171 *host = '\0';
cb7f98ee 1172 return (HTTP_URI_STATUS_BAD_HOSTNAME);
ef416fc2 1173 }
1174 }
1175 else
1176 {
1177 /*
b423cd4c 1178 * Validate the hostname or IPv4 address first...
1179 */
1180
1181 for (ptr = (char *)uri; *ptr; ptr ++)
1182 if (strchr(":?/", *ptr))
1183 break;
ff862e50
MS
1184 else if (!strchr("abcdefghijklmnopqrstuvwxyz" /* unreserved */
1185 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* unreserved */
1186 "0123456789" /* unreserved */
1187 "-._~" /* unreserved */
1188 "%" /* pct-encoded */
1189 "!$&'()*+,;=" /* sub-delims */
1190 "\\", *ptr)) /* SMB domain */
b423cd4c 1191 {
1192 *host = '\0';
cb7f98ee 1193 return (HTTP_URI_STATUS_BAD_HOSTNAME);
b423cd4c 1194 }
1195
1196 /*
1197 * Then copy the hostname or IPv4 address to the buffer...
ef416fc2 1198 */
1199
a4d04587 1200 uri = http_copy_decode(host, uri, hostlen, ":?/",
1201 decoding & HTTP_URI_CODING_HOSTNAME);
ef416fc2 1202
1203 if (!uri)
1204 {
1205 *host = '\0';
cb7f98ee 1206 return (HTTP_URI_STATUS_BAD_HOSTNAME);
ef416fc2 1207 }
ef416fc2 1208 }
1209
1210 /*
1211 * Validate hostname for file scheme - only empty and localhost are
1212 * acceptable.
1213 */
1214
1215 if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0])
1216 {
1217 *host = '\0';
cb7f98ee 1218 return (HTTP_URI_STATUS_BAD_HOSTNAME);
ef416fc2 1219 }
1220
1221 /*
1222 * See if we have a port number...
1223 */
1224
1225 if (*uri == ':')
1226 {
1227 /*
1228 * Yes, collect the port number...
1229 */
1230
dfd5680b
MS
1231 if (!isdigit(uri[1] & 255))
1232 {
1233 *port = 0;
cb7f98ee 1234 return (HTTP_URI_STATUS_BAD_PORT);
dfd5680b
MS
1235 }
1236
7e86f2f6 1237 *port = (int)strtol(uri + 1, (char **)&uri, 10);
ef416fc2 1238
d6ae789d 1239 if (*uri != '/' && *uri)
ef416fc2 1240 {
1241 *port = 0;
cb7f98ee 1242 return (HTTP_URI_STATUS_BAD_PORT);
ef416fc2 1243 }
1244 }
1245 }
1246
1247 /*
1248 * The remaining portion is the resource string...
1249 */
1250
1251 if (*uri == '?' || !*uri)
1252 {
1253 /*
1254 * Hostname but no path...
1255 */
1256
cb7f98ee 1257 status = HTTP_URI_STATUS_MISSING_RESOURCE;
ef416fc2 1258 *resource = '/';
fa73b229 1259
1260 /*
a4d04587 1261 * Copy any query string...
fa73b229 1262 */
1263
1264 if (*uri == '?')
a4d04587 1265 uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL,
1266 decoding & HTTP_URI_CODING_QUERY);
fa73b229 1267 else
1268 resource[1] = '\0';
ef416fc2 1269 }
1270 else
fa73b229 1271 {
a4d04587 1272 uri = http_copy_decode(resource, uri, resourcelen, "?",
1273 decoding & HTTP_URI_CODING_RESOURCE);
fa73b229 1274
1275 if (uri && *uri == '?')
1276 {
1277 /*
a4d04587 1278 * Concatenate any query string...
fa73b229 1279 */
1280
a4d04587 1281 char *resptr = resource + strlen(resource);
1282
ff862e50
MS
1283 uri = http_copy_decode(resptr, uri,
1284 resourcelen - (int)(resptr - resource), NULL,
1285 decoding & HTTP_URI_CODING_QUERY);
fa73b229 1286 }
1287 }
ef416fc2 1288
1289 if (!uri)
1290 {
1291 *resource = '\0';
cb7f98ee 1292 return (HTTP_URI_STATUS_BAD_RESOURCE);
ef416fc2 1293 }
1294
1295 /*
1296 * Return the URI separation status...
1297 */
1298
1299 return (status);
1300}
1301
1302
5ec1fd3d
MS
1303/*
1304 * 'httpStateString()' - Return the string describing a HTTP state value.
1305 *
1306 * @since CUPS 2.0@
1307 */
1308
1309const char * /* O - State string */
1310httpStateString(http_state_t state) /* I - HTTP state value */
1311{
1312 if (state < HTTP_STATE_ERROR || state > HTTP_STATE_UNKNOWN_VERSION)
1313 return ("HTTP_STATE_???");
1314 else
1315 return (http_states[state - HTTP_STATE_ERROR]);
1316}
1317
1318
ef416fc2 1319/*
41e6c1f1 1320 * '_httpStatus()' - Return the localized string describing a HTTP status code.
9f5eb9be 1321 *
41e6c1f1 1322 * The returned string is localized using the passed message catalog.
ef416fc2 1323 */
1324
9f5eb9be 1325const char * /* O - Localized status string */
41e6c1f1
MS
1326_httpStatus(cups_lang_t *lang, /* I - Language */
1327 http_status_t status) /* I - HTTP status code */
ef416fc2 1328{
9f5eb9be 1329 const char *s; /* Status string */
9f5eb9be 1330
9f5eb9be 1331
ef416fc2 1332 switch (status)
1333 {
b63a0d90
MS
1334 case HTTP_STATUS_ERROR :
1335 s = strerror(errno);
1336 break;
cb7f98ee 1337 case HTTP_STATUS_CONTINUE :
9f5eb9be
MS
1338 s = _("Continue");
1339 break;
cb7f98ee 1340 case HTTP_STATUS_SWITCHING_PROTOCOLS :
9f5eb9be
MS
1341 s = _("Switching Protocols");
1342 break;
cb7f98ee 1343 case HTTP_STATUS_OK :
9f5eb9be
MS
1344 s = _("OK");
1345 break;
cb7f98ee 1346 case HTTP_STATUS_CREATED :
9f5eb9be
MS
1347 s = _("Created");
1348 break;
cb7f98ee 1349 case HTTP_STATUS_ACCEPTED :
9f5eb9be
MS
1350 s = _("Accepted");
1351 break;
cb7f98ee 1352 case HTTP_STATUS_NO_CONTENT :
9f5eb9be
MS
1353 s = _("No Content");
1354 break;
cb7f98ee 1355 case HTTP_STATUS_MOVED_PERMANENTLY :
9f5eb9be
MS
1356 s = _("Moved Permanently");
1357 break;
cb7f98ee 1358 case HTTP_STATUS_SEE_OTHER :
9f5eb9be
MS
1359 s = _("See Other");
1360 break;
cb7f98ee 1361 case HTTP_STATUS_NOT_MODIFIED :
9f5eb9be
MS
1362 s = _("Not Modified");
1363 break;
cb7f98ee 1364 case HTTP_STATUS_BAD_REQUEST :
9f5eb9be
MS
1365 s = _("Bad Request");
1366 break;
cb7f98ee
MS
1367 case HTTP_STATUS_UNAUTHORIZED :
1368 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED :
9f5eb9be
MS
1369 s = _("Unauthorized");
1370 break;
cb7f98ee 1371 case HTTP_STATUS_FORBIDDEN :
9f5eb9be
MS
1372 s = _("Forbidden");
1373 break;
cb7f98ee 1374 case HTTP_STATUS_NOT_FOUND :
9f5eb9be
MS
1375 s = _("Not Found");
1376 break;
cb7f98ee 1377 case HTTP_STATUS_REQUEST_TOO_LARGE :
9f5eb9be
MS
1378 s = _("Request Entity Too Large");
1379 break;
cb7f98ee 1380 case HTTP_STATUS_URI_TOO_LONG :
9f5eb9be
MS
1381 s = _("URI Too Long");
1382 break;
cb7f98ee 1383 case HTTP_STATUS_UPGRADE_REQUIRED :
9f5eb9be
MS
1384 s = _("Upgrade Required");
1385 break;
cb7f98ee 1386 case HTTP_STATUS_NOT_IMPLEMENTED :
9f5eb9be
MS
1387 s = _("Not Implemented");
1388 break;
cb7f98ee 1389 case HTTP_STATUS_NOT_SUPPORTED :
9f5eb9be
MS
1390 s = _("Not Supported");
1391 break;
cb7f98ee 1392 case HTTP_STATUS_EXPECTATION_FAILED :
9f5eb9be
MS
1393 s = _("Expectation Failed");
1394 break;
cb7f98ee 1395 case HTTP_STATUS_SERVICE_UNAVAILABLE :
9f5eb9be
MS
1396 s = _("Service Unavailable");
1397 break;
cb7f98ee 1398 case HTTP_STATUS_SERVER_ERROR :
94da7e34
MS
1399 s = _("Internal Server Error");
1400 break;
cb7f98ee 1401 case HTTP_STATUS_CUPS_PKI_ERROR :
7cf5915e
MS
1402 s = _("SSL/TLS Negotiation Error");
1403 break;
cb7f98ee 1404 case HTTP_STATUS_CUPS_WEBIF_DISABLED :
229681c1
MS
1405 s = _("Web Interface is Disabled");
1406 break;
b423cd4c 1407
ef416fc2 1408 default :
9f5eb9be
MS
1409 s = _("Unknown");
1410 break;
ef416fc2 1411 }
9f5eb9be 1412
41e6c1f1
MS
1413 return (_cupsLangString(lang, s));
1414}
1415
1416
1417/*
1418 * 'httpStatus()' - Return a short string describing a HTTP status code.
1419 *
1420 * The returned string is localized to the current POSIX locale and is based
1421 * on the status strings defined in RFC 2616.
1422 */
1423
1424const char * /* O - Localized status string */
1425httpStatus(http_status_t status) /* I - HTTP status code */
1426{
1427 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1428
1429
1430 if (!cg->lang_default)
1431 cg->lang_default = cupsLangDefault();
1432
1433 return (_httpStatus(cg->lang_default, status));
ef416fc2 1434}
1435
7e86f2f6
MS
1436/*
1437 * 'httpURIStatusString()' - Return a string describing a URI status code.
1438 *
1439 * @since CUPS 2.0@
1440 */
1441
1442const char * /* O - Localized status string */
1443httpURIStatusString(
1444 http_uri_status_t status) /* I - URI status code */
1445{
1446 const char *s; /* Status string */
1447 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1448
1449
1450 if (!cg->lang_default)
1451 cg->lang_default = cupsLangDefault();
1452
1453 switch (status)
1454 {
1455 case HTTP_URI_STATUS_OVERFLOW :
1456 s = _("URI too large");
1457 break;
1458 case HTTP_URI_STATUS_BAD_ARGUMENTS :
1459 s = _("Bad arguments to function");
1460 break;
1461 case HTTP_URI_STATUS_BAD_RESOURCE :
1462 s = _("Bad resource in URI");
1463 break;
1464 case HTTP_URI_STATUS_BAD_PORT :
1465 s = _("Bad port number in URI");
1466 break;
1467 case HTTP_URI_STATUS_BAD_HOSTNAME :
1468 s = _("Bad hostname/address in URI");
1469 break;
1470 case HTTP_URI_STATUS_BAD_USERNAME :
1471 s = _("Bad username in URI");
1472 break;
1473 case HTTP_URI_STATUS_BAD_SCHEME :
1474 s = _("Bad scheme in URI");
1475 break;
1476 case HTTP_URI_STATUS_BAD_URI :
1477 s = _("Bad/empty URI");
1478 break;
1479 case HTTP_URI_STATUS_OK :
1480 s = _("OK");
1481 break;
1482 case HTTP_URI_STATUS_MISSING_SCHEME :
1483 s = _("Missing scheme in URI");
1484 break;
1485 case HTTP_URI_STATUS_UNKNOWN_SCHEME :
1486 s = _("Unknown scheme in URI");
1487 break;
1488 case HTTP_URI_STATUS_MISSING_RESOURCE :
1489 s = _("Missing resource in URI");
1490 break;
1491
1492 default:
1493 s = _("Unknown");
1494 break;
1495 }
1496
1497 return (_cupsLangString(cg->lang_default, s));
1498}
1499
ef416fc2 1500
1501#ifndef HAVE_HSTRERROR
1502/*
eac3a0a0 1503 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others.
ef416fc2 1504 */
1505
1506const char * /* O - Error string */
1507_cups_hstrerror(int error) /* I - Error number */
1508{
1509 static const char * const errors[] = /* Error strings */
1510 {
1511 "OK",
1512 "Host not found.",
1513 "Try again.",
1514 "Unrecoverable lookup error.",
1515 "No data associated with name."
1516 };
1517
1518
1519 if (error < 0 || error > 4)
1520 return ("Unknown hostname lookup error.");
1521 else
1522 return (errors[error]);
1523}
1524#endif /* !HAVE_HSTRERROR */
1525
1526
1106b00e
MS
1527/*
1528 * '_httpDecodeURI()' - Percent-decode a HTTP request URI.
1529 */
1530
1531char * /* O - Decoded URI or NULL on error */
1532_httpDecodeURI(char *dst, /* I - Destination buffer */
1533 const char *src, /* I - Source URI */
1534 size_t dstsize) /* I - Size of destination buffer */
1535{
1536 if (http_copy_decode(dst, src, (int)dstsize, NULL, 1))
1537 return (dst);
1538 else
1539 return (NULL);
1540}
1541
1542
839a51c8
MS
1543/*
1544 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1545 */
1546
1547char * /* O - Encoded URI */
1548_httpEncodeURI(char *dst, /* I - Destination buffer */
1549 const char *src, /* I - Source URI */
1550 size_t dstsize) /* I - Size of destination buffer */
1551{
1552 http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, 1);
1553 return (dst);
1554}
1555
1556
5eb9da71
MS
1557/*
1558 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1559 */
1560
1561const char * /* O - Resolved URI */
1562_httpResolveURI(
1563 const char *uri, /* I - DNS-SD URI */
1564 char *resolved_uri, /* I - Buffer for resolved URI */
1f0275e3 1565 size_t resolved_size, /* I - Size of URI buffer */
eac3a0a0 1566 int options, /* I - Resolve options */
07ed0e9a
MS
1567 int (*cb)(void *context), /* I - Continue callback function */
1568 void *context) /* I - Context pointer for callback */
5eb9da71
MS
1569{
1570 char scheme[32], /* URI components... */
1571 userpass[256],
1572 hostname[1024],
1573 resource[1024];
1574 int port;
b19ccc9e 1575#ifdef DEBUG
1f0275e3 1576 http_uri_status_t status; /* URI decode status */
b19ccc9e 1577#endif /* DEBUG */
5eb9da71
MS
1578
1579
e07d4801
MS
1580 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1581 "resolved_size=" CUPS_LLFMT ")", uri, resolved_uri,
1f0275e3
MS
1582 CUPS_LLCAST resolved_size));
1583
5eb9da71
MS
1584 /*
1585 * Get the device URI...
1586 */
1587
b19ccc9e 1588#ifdef DEBUG
1f0275e3
MS
1589 if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1590 sizeof(scheme), userpass, sizeof(userpass),
1591 hostname, sizeof(hostname), &port, resource,
cb7f98ee 1592 sizeof(resource))) < HTTP_URI_STATUS_OK)
b19ccc9e
MS
1593#else
1594 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1595 sizeof(scheme), userpass, sizeof(userpass),
1596 hostname, sizeof(hostname), &port, resource,
cb7f98ee 1597 sizeof(resource)) < HTTP_URI_STATUS_OK)
b19ccc9e 1598#endif /* DEBUG */
1f0275e3 1599 {
eac3a0a0 1600 if (options & _HTTP_RESOLVE_STDERR)
84315f46 1601 _cupsLangPrintFilter(stderr, "ERROR", _("Bad device-uri \"%s\"."), uri);
1f0275e3 1602
e07d4801
MS
1603 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status));
1604 DEBUG_puts("5_httpResolveURI: Returning NULL");
5eb9da71 1605 return (NULL);
1f0275e3 1606 }
5eb9da71
MS
1607
1608 /*
1609 * Resolve it as needed...
1610 */
1611
1612 if (strstr(hostname, "._tcp"))
1613 {
f3c17241 1614#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
5eb9da71 1615 char *regtype, /* Pointer to type in hostname */
2c3f5865
MS
1616 *domain, /* Pointer to domain in hostname */
1617 *uuid, /* Pointer to UUID in URI */
1618 *uuidend; /* Pointer to end of UUID in URI */
5eb9da71 1619 _http_uribuf_t uribuf; /* URI buffer */
f3c17241
MS
1620 int offline = 0; /* offline-report state set? */
1621# ifdef HAVE_DNSSD
1622# ifdef WIN32
1623# pragma comment(lib, "dnssd.lib")
1624# endif /* WIN32 */
1625 DNSServiceRef ref, /* DNS-SD master service reference */
7e86f2f6 1626 domainref = NULL,/* DNS-SD service reference for domain */
f3c17241
MS
1627 localref; /* DNS-SD service reference for .local */
1628 int domainsent = 0; /* Send the domain resolve? */
1629# ifdef HAVE_POLL
38e73f87 1630 struct pollfd polldata; /* Polling data */
f3c17241 1631# else /* select() */
6d2f911b
MS
1632 fd_set input_set; /* Input set for select() */
1633 struct timeval stimeout; /* Timeout value for select() */
f3c17241
MS
1634# endif /* HAVE_POLL */
1635# elif defined(HAVE_AVAHI)
1636 AvahiClient *client; /* Client information */
1637 int error; /* Status */
1638# endif /* HAVE_DNSSD */
38e73f87 1639
eac3a0a0 1640 if (options & _HTTP_RESOLVE_STDERR)
38e73f87 1641 fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
5eb9da71
MS
1642
1643 /*
1644 * Separate the hostname into service name, registration type, and domain...
1645 */
1646
1f0275e3
MS
1647 for (regtype = strstr(hostname, "._tcp") - 2;
1648 regtype > hostname;
1649 regtype --)
1650 if (regtype[0] == '.' && regtype[1] == '_')
1651 {
1652 /*
1653 * Found ._servicetype in front of ._tcp...
1654 */
1655
1656 *regtype++ = '\0';
1657 break;
1658 }
1659
1660 if (regtype <= hostname)
1661 {
e07d4801 1662 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1f0275e3
MS
1663 return (NULL);
1664 }
5eb9da71 1665
5eb9da71
MS
1666 for (domain = strchr(regtype, '.');
1667 domain;
1668 domain = strchr(domain + 1, '.'))
1669 if (domain[1] != '_')
1670 break;
1671
1672 if (domain)
1673 *domain++ = '\0';
1674
2c3f5865
MS
1675 if ((uuid = strstr(resource, "?uuid=")) != NULL)
1676 {
1677 *uuid = '\0';
1678 uuid += 6;
1679 if ((uuidend = strchr(uuid, '&')) != NULL)
1680 *uuidend = '\0';
1681 }
1682
1683 resolved_uri[0] = '\0';
1684
eac3a0a0
MS
1685 uribuf.buffer = resolved_uri;
1686 uribuf.bufsize = resolved_size;
1687 uribuf.options = options;
5a9febac 1688 uribuf.resource = resource;
2c3f5865 1689 uribuf.uuid = uuid;
5eb9da71 1690
e07d4801 1691 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1f0275e3 1692 "domain=\"%s\"\n", hostname, regtype, domain));
eac3a0a0 1693 if (options & _HTTP_RESOLVE_STDERR)
1f0275e3
MS
1694 {
1695 fputs("STATE: +connecting-to-device\n", stderr);
38e73f87
MS
1696 fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1697 "domain=\"local.\"...\n", hostname, regtype);
1f0275e3
MS
1698 }
1699
38e73f87
MS
1700 uri = NULL;
1701
f3c17241 1702# ifdef HAVE_DNSSD
38e73f87 1703 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
5eb9da71 1704 {
7e86f2f6 1705 uint32_t myinterface = kDNSServiceInterfaceIndexAny;
a469f8a5
MS
1706 /* Lookup on any interface */
1707
1708 if (!strcmp(scheme, "ippusb"))
db8b865d 1709 myinterface = kDNSServiceInterfaceIndexLocalOnly;
a469f8a5 1710
38e73f87 1711 localref = ref;
3e7fe0ca 1712 if (DNSServiceResolve(&localref,
db8b865d 1713 kDNSServiceFlagsShareConnection, myinterface,
a469f8a5 1714 hostname, regtype, "local.", http_resolve_cb,
38e73f87
MS
1715 &uribuf) == kDNSServiceErr_NoError)
1716 {
acb056cb
MS
1717 int fds; /* Number of ready descriptors */
1718 time_t timeout, /* Poll timeout */
3e7fe0ca
MS
1719 start_time = time(NULL),/* Start time */
1720 end_time = start_time + 90;
1721 /* End time */
acb056cb 1722
3e7fe0ca 1723 while (time(NULL) < end_time)
38e73f87 1724 {
eac3a0a0 1725 if (options & _HTTP_RESOLVE_STDERR)
0837b7e8 1726 _cupsLangPrintFilter(stderr, "INFO", _("Looking for printer."));
acb056cb 1727
07ed0e9a
MS
1728 if (cb && !(*cb)(context))
1729 {
1730 DEBUG_puts("5_httpResolveURI: callback returned 0 (stop)");
1731 break;
1732 }
1733
38e73f87 1734 /*
3e7fe0ca 1735 * Wakeup every 2 seconds to emit a "looking for printer" message...
38e73f87
MS
1736 */
1737
3e7fe0ca
MS
1738 if ((timeout = end_time - time(NULL)) > 2)
1739 timeout = 2;
acb056cb 1740
f3c17241 1741# ifdef HAVE_POLL
38e73f87
MS
1742 polldata.fd = DNSServiceRefSockFD(ref);
1743 polldata.events = POLLIN;
1744
7e86f2f6 1745 fds = poll(&polldata, 1, (int)(1000 * timeout));
acb056cb 1746
f3c17241 1747# else /* select() */
6d2f911b
MS
1748 FD_ZERO(&input_set);
1749 FD_SET(DNSServiceRefSockFD(ref), &input_set);
1750
f3c17241 1751# ifdef WIN32
82cc1f9a 1752 stimeout.tv_sec = (long)timeout;
f3c17241 1753# else
3e7fe0ca 1754 stimeout.tv_sec = timeout;
f3c17241 1755# endif /* WIN32 */
3e7fe0ca 1756 stimeout.tv_usec = 0;
6d2f911b 1757
88f9aafc 1758 fds = select(DNSServiceRefSockFD(ref)+1, &input_set, NULL, NULL,
3e7fe0ca 1759 &stimeout);
f3c17241 1760# endif /* HAVE_POLL */
6d2f911b 1761
acb056cb
MS
1762 if (fds < 0)
1763 {
1764 if (errno != EINTR && errno != EAGAIN)
1765 {
1766 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno)));
1767 break;
1768 }
1769 }
1770 else if (fds == 0)
38e73f87
MS
1771 {
1772 /*
acb056cb
MS
1773 * Wait 2 seconds for a response to the local resolve; if nothing
1774 * comes in, do an additional domain resolution...
38e73f87
MS
1775 */
1776
3e7fe0ca 1777 if (domainsent == 0 && domain && _cups_strcasecmp(domain, "local."))
acb056cb 1778 {
eac3a0a0 1779 if (options & _HTTP_RESOLVE_STDERR)
acb056cb
MS
1780 fprintf(stderr,
1781 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
eac3a0a0
MS
1782 "domain=\"%s\"...\n", hostname, regtype,
1783 domain ? domain : "");
88f9aafc 1784
acb056cb 1785 domainref = ref;
3e7fe0ca 1786 if (DNSServiceResolve(&domainref,
82cc1f9a 1787 kDNSServiceFlagsShareConnection,
db8b865d 1788 myinterface, hostname, regtype, domain,
3e7fe0ca
MS
1789 http_resolve_cb,
1790 &uribuf) == kDNSServiceErr_NoError)
acb056cb
MS
1791 domainsent = 1;
1792 }
6c48a6ca
MS
1793
1794 /*
1795 * If it hasn't resolved within 5 seconds set the offline-report
1796 * printer-state-reason...
1797 */
1798
eac3a0a0
MS
1799 if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 &&
1800 time(NULL) > (start_time + 5))
6c48a6ca
MS
1801 {
1802 fputs("STATE: +offline-report\n", stderr);
1803 offline = 1;
1804 }
38e73f87 1805 }
acb056cb
MS
1806 else
1807 {
2c3f5865
MS
1808 if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError &&
1809 resolved_uri[0])
acb056cb
MS
1810 {
1811 uri = resolved_uri;
1812 break;
1813 }
1814 }
1815 }
38e73f87
MS
1816
1817 if (domainsent)
1818 DNSServiceRefDeallocate(domainref);
1819
1820 DNSServiceRefDeallocate(localref);
1821 }
5eb9da71
MS
1822
1823 DNSServiceRefDeallocate(ref);
1824 }
f3c17241
MS
1825# else /* HAVE_AVAHI */
1826 if ((uribuf.poll = avahi_simple_poll_new()) != NULL)
1827 {
1828 avahi_simple_poll_set_func(uribuf.poll, http_poll_cb, NULL);
1829
1830 if ((client = avahi_client_new(avahi_simple_poll_get(uribuf.poll),
1831 0, http_client_cb,
1832 &uribuf, &error)) != NULL)
1833 {
1834 if (avahi_service_resolver_new(client, AVAHI_IF_UNSPEC,
1835 AVAHI_PROTO_UNSPEC, hostname,
1836 regtype, "local.", AVAHI_PROTO_UNSPEC, 0,
1837 http_resolve_cb, &uribuf) != NULL)
1838 {
1839 time_t start_time = time(NULL),
1840 /* Start time */
1841 end_time = start_time + 90;
1842 /* End time */
1843 int pstatus; /* Poll status */
1844
1845 pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000);
1846
1847 if (pstatus == 0 && !resolved_uri[0] && domain &&
1848 _cups_strcasecmp(domain, "local."))
1849 {
1850 /*
1851 * Resolve for .local hasn't returned anything, try the listed
1852 * domain...
1853 */
1854
1855 avahi_service_resolver_new(client, AVAHI_IF_UNSPEC,
1856 AVAHI_PROTO_UNSPEC, hostname,
1857 regtype, domain, AVAHI_PROTO_UNSPEC, 0,
1858 http_resolve_cb, &uribuf);
1859 }
1860
1861 while (!pstatus && !resolved_uri[0] && time(NULL) < end_time)
1862 {
1863 if ((pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000)) != 0)
1864 break;
1865
1866 /*
1867 * If it hasn't resolved within 5 seconds set the offline-report
1868 * printer-state-reason...
1869 */
1870
1871 if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 &&
1872 time(NULL) > (start_time + 5))
1873 {
1874 fputs("STATE: +offline-report\n", stderr);
1875 offline = 1;
1876 }
1877 }
1878
1879 /*
1880 * Collect the result (if we got one).
1881 */
1882
1883 if (resolved_uri[0])
1884 uri = resolved_uri;
1885 }
1886
1887 avahi_client_free(client);
1888 }
1889
1890 avahi_simple_poll_free(uribuf.poll);
1891 }
1892# endif /* HAVE_DNSSD */
1f0275e3 1893
eac3a0a0 1894 if (options & _HTTP_RESOLVE_STDERR)
38e73f87
MS
1895 {
1896 if (uri)
3e7fe0ca 1897 {
38e73f87 1898 fprintf(stderr, "DEBUG: Resolved as \"%s\"...\n", uri);
3e7fe0ca
MS
1899 fputs("STATE: -connecting-to-device,offline-report\n", stderr);
1900 }
f11a948a 1901 else
3e7fe0ca 1902 {
4d301e69 1903 fputs("DEBUG: Unable to resolve URI\n", stderr);
3e7fe0ca
MS
1904 fputs("STATE: -connecting-to-device\n", stderr);
1905 }
38e73f87 1906 }
1f0275e3 1907
f3c17241 1908#else /* HAVE_DNSSD || HAVE_AVAHI */
1f0275e3
MS
1909 /*
1910 * No DNS-SD support...
1911 */
5eb9da71
MS
1912
1913 uri = NULL;
f3c17241 1914#endif /* HAVE_DNSSD || HAVE_AVAHI */
1f0275e3 1915
eac3a0a0 1916 if ((options & _HTTP_RESOLVE_STDERR) && !uri)
a469f8a5 1917 _cupsLangPrintFilter(stderr, "INFO", _("Unable to find printer."));
5eb9da71 1918 }
eac3a0a0
MS
1919 else
1920 {
1921 /*
1922 * Nothing more to do...
1923 */
1924
1925 strlcpy(resolved_uri, uri, resolved_size);
1926 uri = resolved_uri;
1927 }
5eb9da71 1928
e07d4801 1929 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri));
1f0275e3 1930
5eb9da71
MS
1931 return (uri);
1932}
1933
1934
f3c17241
MS
1935#ifdef HAVE_AVAHI
1936/*
1937 * 'http_client_cb()' - Client callback for resolving URI.
1938 */
1939
1940static void
1941http_client_cb(
1942 AvahiClient *client, /* I - Client information */
1943 AvahiClientState state, /* I - Current state */
1944 void *context) /* I - Pointer to URI buffer */
1945{
1946 DEBUG_printf(("7http_client_cb(client=%p, state=%d, context=%p)", client,
1947 state, context));
1948
1949 /*
1950 * If the connection drops, quit.
1951 */
1952
1953 if (state == AVAHI_CLIENT_FAILURE)
1954 {
1955 _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
1956 /* URI buffer */
1957
1958 avahi_simple_poll_quit(uribuf->poll);
1959 }
1960}
1961#endif /* HAVE_AVAHI */
1962
1963
ef416fc2 1964/*
1965 * 'http_copy_decode()' - Copy and decode a URI.
1966 */
1967
1968static const char * /* O - New source pointer or NULL on error */
88f9aafc 1969http_copy_decode(char *dst, /* O - Destination buffer */
ef416fc2 1970 const char *src, /* I - Source pointer */
1971 int dstsize, /* I - Destination size */
a4d04587 1972 const char *term, /* I - Terminating characters */
1973 int decode) /* I - Decode %-encoded values */
ef416fc2 1974{
1975 char *ptr, /* Pointer into buffer */
1976 *end; /* End of buffer */
1977 int quoted; /* Quoted character */
1978
1979
1980 /*
1981 * Copy the src to the destination until we hit a terminating character
1982 * or the end of the string.
1983 */
1984
e00b005a 1985 for (ptr = dst, end = dst + dstsize - 1;
1986 *src && (!term || !strchr(term, *src));
1987 src ++)
ef416fc2 1988 if (ptr < end)
1989 {
a4d04587 1990 if (*src == '%' && decode)
ef416fc2 1991 {
1992 if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255))
1993 {
1994 /*
1995 * Grab a hex-encoded character...
1996 */
1997
1998 src ++;
1999 if (isalpha(*src))
2000 quoted = (tolower(*src) - 'a' + 10) << 4;
2001 else
2002 quoted = (*src - '0') << 4;
2003
2004 src ++;
2005 if (isalpha(*src))
2006 quoted |= tolower(*src) - 'a' + 10;
2007 else
2008 quoted |= *src - '0';
2009
7e86f2f6 2010 *ptr++ = (char)quoted;
ef416fc2 2011 }
2012 else
2013 {
2014 /*
2015 * Bad hex-encoded character...
2016 */
2017
2018 *ptr = '\0';
2019 return (NULL);
2020 }
2021 }
6961465f
MS
2022 else if ((*src & 255) <= 0x20 || (*src & 255) >= 0x7f)
2023 {
2024 *ptr = '\0';
2025 return (NULL);
2026 }
ef416fc2 2027 else
2028 *ptr++ = *src;
2029 }
2030
2031 *ptr = '\0';
2032
2033 return (src);
2034}
2035
2036
2037/*
2038 * 'http_copy_encode()' - Copy and encode a URI.
2039 */
2040
2041static char * /* O - End of current URI */
88f9aafc 2042http_copy_encode(char *dst, /* O - Destination buffer */
ef416fc2 2043 const char *src, /* I - Source pointer */
2044 char *dstend, /* I - End of destination buffer */
a4d04587 2045 const char *reserved, /* I - Extra reserved characters */
2046 const char *term, /* I - Terminating characters */
2047 int encode) /* I - %-encode reserved chars? */
ef416fc2 2048{
ac884b6a 2049 static const char hex[] = "0123456789ABCDEF";
ef416fc2 2050
2051
2052 while (*src && dst < dstend)
2053 {
a4d04587 2054 if (term && *src == *term)
2055 return (dst);
2056
2057 if (encode && (*src == '%' || *src <= ' ' || *src & 128 ||
2058 (reserved && strchr(reserved, *src))))
ef416fc2 2059 {
2060 /*
2061 * Hex encode reserved characters...
2062 */
2063
2064 if ((dst + 2) >= dstend)
2065 break;
2066
2067 *dst++ = '%';
2068 *dst++ = hex[(*src >> 4) & 15];
2069 *dst++ = hex[*src & 15];
2070
2071 src ++;
2072 }
2073 else
2074 *dst++ = *src++;
2075 }
2076
839a51c8
MS
2077 *dst = '\0';
2078
ef416fc2 2079 if (*src)
2080 return (NULL);
2081 else
2082 return (dst);
2083}
2084
2085
5eb9da71
MS
2086#ifdef HAVE_DNSSD
2087/*
eac3a0a0 2088 * 'http_resolve_cb()' - Build a device URI for the given service name.
5eb9da71
MS
2089 */
2090
6d2f911b 2091static void DNSSD_API
eac3a0a0 2092http_resolve_cb(
5eb9da71
MS
2093 DNSServiceRef sdRef, /* I - Service reference */
2094 DNSServiceFlags flags, /* I - Results flags */
2095 uint32_t interfaceIndex, /* I - Interface number */
2096 DNSServiceErrorType errorCode, /* I - Error, if any */
2097 const char *fullName, /* I - Full service name */
2098 const char *hostTarget, /* I - Hostname */
2099 uint16_t port, /* I - Port number */
2100 uint16_t txtLen, /* I - Length of TXT record */
2101 const unsigned char *txtRecord, /* I - TXT record data */
2102 void *context) /* I - Pointer to URI buffer */
2103{
f3c17241
MS
2104 _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
2105 /* URI buffer */
eac3a0a0 2106 const char *scheme, /* URI scheme */
f3c17241
MS
2107 *hostptr, /* Pointer into hostTarget */
2108 *reskey, /* "rp" or "rfo" */
2109 *resdefault; /* Default path */
2110 char resource[257], /* Remote path */
eac3a0a0 2111 fqdn[256]; /* FQDN of the .local name */
5eb9da71
MS
2112 const void *value; /* Value from TXT record */
2113 uint8_t valueLen; /* Length of value */
5eb9da71
MS
2114
2115
eac3a0a0 2116 DEBUG_printf(("7http_resolve_cb(sdRef=%p, flags=%x, interfaceIndex=%u, "
5eb9da71 2117 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
e07d4801 2118 "txtLen=%u, txtRecord=%p, context=%p)", sdRef, flags,
5eb9da71
MS
2119 interfaceIndex, errorCode, fullName, hostTarget, port, txtLen,
2120 txtRecord, context));
2121
2c3f5865
MS
2122 /*
2123 * If we have a UUID, compare it...
2124 */
2125
2126 if (uribuf->uuid &&
2127 (value = TXTRecordGetValuePtr(txtLen, txtRecord, "UUID",
2128 &valueLen)) != NULL)
2129 {
2130 char uuid[256]; /* UUID value */
2131
2132 memcpy(uuid, value, valueLen);
2133 uuid[valueLen] = '\0';
2134
2135 if (_cups_strcasecmp(uuid, uribuf->uuid))
2136 {
2137 if (uribuf->options & _HTTP_RESOLVE_STDERR)
2c3f5865
MS
2138 fprintf(stderr, "DEBUG: Found UUID %s, looking for %s.", uuid,
2139 uribuf->uuid);
2c3f5865
MS
2140
2141 DEBUG_printf(("7http_resolve_cb: Found UUID %s, looking for %s.", uuid,
2142 uribuf->uuid));
2143 return;
2144 }
2145 }
2146
5eb9da71
MS
2147 /*
2148 * Figure out the scheme from the full name...
2149 */
2150
eac3a0a0
MS
2151 if (strstr(fullName, "._ipps") || strstr(fullName, "._ipp-tls"))
2152 scheme = "ipps";
2153 else if (strstr(fullName, "._ipp") || strstr(fullName, "._fax-ipp"))
5eb9da71 2154 scheme = "ipp";
eac3a0a0
MS
2155 else if (strstr(fullName, "._http."))
2156 scheme = "http";
2157 else if (strstr(fullName, "._https."))
2158 scheme = "https";
5eb9da71
MS
2159 else if (strstr(fullName, "._printer."))
2160 scheme = "lpd";
2161 else if (strstr(fullName, "._pdl-datastream."))
2162 scheme = "socket";
2163 else
2164 scheme = "riousbprint";
2165
2166 /*
2167 * Extract the "remote printer" key from the TXT record...
2168 */
2169
f3c17241 2170 if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) &&
0d4d48ad
MS
2171 (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
2172 !TXTRecordGetValuePtr(txtLen, txtRecord, "printer-type", &valueLen))
f3c17241
MS
2173 {
2174 reskey = "rfo";
2175 resdefault = "/ipp/faxout";
2176 }
2177 else
2178 {
2179 reskey = "rp";
2180 resdefault = "/";
2181 }
2182
2183 if ((value = TXTRecordGetValuePtr(txtLen, txtRecord, reskey,
5eb9da71 2184 &valueLen)) != NULL)
9b66acc5
MS
2185 {
2186 if (((char *)value)[0] == '/')
2187 {
2188 /*
f3c17241 2189 * Value (incorrectly) has a leading slash already...
9b66acc5
MS
2190 */
2191
f3c17241
MS
2192 memcpy(resource, value, valueLen);
2193 resource[valueLen] = '\0';
9b66acc5
MS
2194 }
2195 else
2196 {
2197 /*
2198 * Convert to resource by concatenating with a leading "/"...
2199 */
2200
f3c17241
MS
2201 resource[0] = '/';
2202 memcpy(resource + 1, value, valueLen);
2203 resource[valueLen + 1] = '\0';
9b66acc5
MS
2204 }
2205 }
2206 else
5eb9da71
MS
2207 {
2208 /*
f3c17241 2209 * Use the default value...
5eb9da71
MS
2210 */
2211
f3c17241 2212 strlcpy(resource, resdefault, sizeof(resource));
5eb9da71 2213 }
5eb9da71
MS
2214
2215 /*
eac3a0a0 2216 * Lookup the FQDN if needed...
5eb9da71
MS
2217 */
2218
eac3a0a0
MS
2219 if ((uribuf->options & _HTTP_RESOLVE_FQDN) &&
2220 (hostptr = hostTarget + strlen(hostTarget) - 7) > hostTarget &&
88f9aafc 2221 !_cups_strcasecmp(hostptr, ".local."))
eac3a0a0
MS
2222 {
2223 /*
2224 * OK, we got a .local name but the caller needs a real domain. Start by
2225 * getting the IP address of the .local name and then do reverse-lookups...
2226 */
2227
2228 http_addrlist_t *addrlist, /* List of addresses */
2229 *addr; /* Current address */
2230
2231 DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget));
2232
2233 snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
2234 if ((addrlist = httpAddrGetList(hostTarget, AF_UNSPEC, fqdn)) != NULL)
2235 {
2236 for (addr = addrlist; addr; addr = addr->next)
2237 {
7e86f2f6 2238 int error = getnameinfo(&(addr->addr.addr), (socklen_t)httpAddrLength(&(addr->addr)), fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
eac3a0a0
MS
2239
2240 if (!error)
2241 {
2242 DEBUG_printf(("8http_resolve_cb: Found \"%s\".", fqdn));
2243
2244 if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn ||
88f9aafc 2245 _cups_strcasecmp(hostptr, ".local"))
eac3a0a0
MS
2246 {
2247 hostTarget = fqdn;
2248 break;
2249 }
2250 }
2251#ifdef DEBUG
2252 else
2253 DEBUG_printf(("8http_resolve_cb: \"%s\" did not resolve: %d",
2254 httpAddrString(&(addr->addr), fqdn, sizeof(fqdn)),
2255 error));
2256#endif /* DEBUG */
2257 }
0fa6c7fa
MS
2258
2259 httpAddrFreeList(addrlist);
eac3a0a0
MS
2260 }
2261 }
2262
2263 /*
2264 * Assemble the final device URI...
2265 */
5eb9da71 2266
5a9febac
MS
2267 if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
2268 !strcmp(uribuf->resource, "/cups"))
7e86f2f6 2269 httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, (int)uribuf->bufsize, scheme, NULL, hostTarget, ntohs(port), "%s?snmp=false", resource);
5a9febac 2270 else
7e86f2f6 2271 httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, (int)uribuf->bufsize, scheme, NULL, hostTarget, ntohs(port), resource);
5eb9da71 2272
eac3a0a0 2273 DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\"...", uribuf->buffer));
5eb9da71 2274}
f3c17241
MS
2275
2276#elif defined(HAVE_AVAHI)
2277/*
2278 * 'http_poll_cb()' - Wait for input on the specified file descriptors.
2279 *
2280 * Note: This function is needed because avahi_simple_poll_iterate is broken
2281 * and always uses a timeout of 0 (!) milliseconds.
2282 * (Avahi Ticket #364)
2283 */
2284
2285static int /* O - Number of file descriptors matching */
2286http_poll_cb(
2287 struct pollfd *pollfds, /* I - File descriptors */
2288 unsigned int num_pollfds, /* I - Number of file descriptors */
2289 int timeout, /* I - Timeout in milliseconds (used) */
2290 void *context) /* I - User data (unused) */
2291{
2292 (void)timeout;
2293 (void)context;
2294
2295 return (poll(pollfds, num_pollfds, 2000));
2296}
2297
2298
2299/*
2300 * 'http_resolve_cb()' - Build a device URI for the given service name.
2301 */
2302
2303static void
2304http_resolve_cb(
2305 AvahiServiceResolver *resolver, /* I - Resolver (unused) */
2306 AvahiIfIndex interface, /* I - Interface index (unused) */
2307 AvahiProtocol protocol, /* I - Network protocol (unused) */
2308 AvahiResolverEvent event, /* I - Event (found, etc.) */
2309 const char *name, /* I - Service name */
2310 const char *type, /* I - Registration type */
2311 const char *domain, /* I - Domain (unused) */
2312 const char *hostTarget, /* I - Hostname */
2313 const AvahiAddress *address, /* I - Address (unused) */
2314 uint16_t port, /* I - Port number */
2315 AvahiStringList *txt, /* I - TXT record */
2316 AvahiLookupResultFlags flags, /* I - Lookup flags (unused) */
2317 void *context) /* I - Pointer to URI buffer */
2318{
2319 _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
2320 /* URI buffer */
2321 const char *scheme, /* URI scheme */
2322 *hostptr, /* Pointer into hostTarget */
2323 *reskey, /* "rp" or "rfo" */
2324 *resdefault; /* Default path */
2325 char resource[257], /* Remote path */
2326 fqdn[256]; /* FQDN of the .local name */
2327 AvahiStringList *pair; /* Current TXT record key/value pair */
2328 char *value; /* Value for "rp" key */
2329 size_t valueLen = 0; /* Length of "rp" key */
2330
2331
2332 DEBUG_printf(("7http_resolve_cb(resolver=%p, "
2333 "interface=%d, protocol=%d, event=%d, name=\"%s\", "
2334 "type=\"%s\", domain=\"%s\", hostTarget=\"%s\", address=%p, "
2335 "port=%d, txt=%p, flags=%d, context=%p)",
2336 resolver, interface, protocol, event, name, type, domain,
2337 hostTarget, address, port, txt, flags, context));
2338
2339 if (event != AVAHI_RESOLVER_FOUND)
2340 {
2341 avahi_service_resolver_free(resolver);
2342 avahi_simple_poll_quit(uribuf->poll);
2343 return;
2344 }
2345
2c3f5865
MS
2346 /*
2347 * If we have a UUID, compare it...
2348 */
2349
2350 if (uribuf->uuid && (pair = avahi_string_list_find(txt, "UUID")) != NULL)
2351 {
2352 char uuid[256]; /* UUID value */
2353
2354 avahi_string_list_get_pair(pair, NULL, &value, &valueLen);
2355
2356 memcpy(uuid, value, valueLen);
2357 uuid[valueLen] = '\0';
2358
2359 if (_cups_strcasecmp(uuid, uribuf->uuid))
2360 {
2361 if (uribuf->options & _HTTP_RESOLVE_STDERR)
2c3f5865
MS
2362 fprintf(stderr, "DEBUG: Found UUID %s, looking for %s.", uuid,
2363 uribuf->uuid);
2c3f5865
MS
2364
2365 DEBUG_printf(("7http_resolve_cb: Found UUID %s, looking for %s.", uuid,
2366 uribuf->uuid));
2367 return;
2368 }
2369 }
2370
f3c17241
MS
2371 /*
2372 * Figure out the scheme from the full name...
2373 */
2374
2375 if (strstr(type, "_ipp."))
2376 scheme = "ipp";
2377 else if (strstr(type, "_printer."))
2378 scheme = "lpd";
2379 else if (strstr(type, "_pdl-datastream."))
2380 scheme = "socket";
2381 else
2382 scheme = "riousbprint";
2383
2384 if (!strncmp(type, "_ipps.", 6) || !strncmp(type, "_ipp-tls.", 9))
2385 scheme = "ipps";
2386 else if (!strncmp(type, "_ipp.", 5) || !strncmp(type, "_fax-ipp.", 9))
2387 scheme = "ipp";
2388 else if (!strncmp(type, "_http.", 6))
2389 scheme = "http";
2390 else if (!strncmp(type, "_https.", 7))
2391 scheme = "https";
2392 else if (!strncmp(type, "_printer.", 9))
2393 scheme = "lpd";
2394 else if (!strncmp(type, "_pdl-datastream.", 16))
2395 scheme = "socket";
2396 else
2397 {
2398 avahi_service_resolver_free(resolver);
2399 avahi_simple_poll_quit(uribuf->poll);
2400 return;
2401 }
2402
2403 /*
2404 * Extract the remote resource key from the TXT record...
2405 */
2406
2407 if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) &&
2c3f5865
MS
2408 (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
2409 !avahi_string_list_find(txt, "printer-type"))
f3c17241
MS
2410 {
2411 reskey = "rfo";
2412 resdefault = "/ipp/faxout";
2413 }
2414 else
2415 {
2416 reskey = "rp";
2417 resdefault = "/";
2418 }
2419
2420 if ((pair = avahi_string_list_find(txt, reskey)) != NULL)
2421 {
2422 avahi_string_list_get_pair(pair, NULL, &value, &valueLen);
2423
2424 if (value[0] == '/')
2425 {
2426 /*
2427 * Value (incorrectly) has a leading slash already...
2428 */
2429
2430 memcpy(resource, value, valueLen);
2431 resource[valueLen] = '\0';
2432 }
2433 else
2434 {
2435 /*
2436 * Convert to resource by concatenating with a leading "/"...
2437 */
2438
2439 resource[0] = '/';
2440 memcpy(resource + 1, value, valueLen);
2441 resource[valueLen + 1] = '\0';
2442 }
2443 }
2444 else
2445 {
2446 /*
2447 * Use the default value...
2448 */
2449
2450 strlcpy(resource, resdefault, sizeof(resource));
2451 }
2452
2453 /*
2454 * Lookup the FQDN if needed...
2455 */
2456
2457 if ((uribuf->options & _HTTP_RESOLVE_FQDN) &&
2458 (hostptr = hostTarget + strlen(hostTarget) - 6) > hostTarget &&
2459 !_cups_strcasecmp(hostptr, ".local"))
2460 {
2461 /*
2462 * OK, we got a .local name but the caller needs a real domain. Start by
2463 * getting the IP address of the .local name and then do reverse-lookups...
2464 */
2465
2466 http_addrlist_t *addrlist, /* List of addresses */
2467 *addr; /* Current address */
2468
2469 DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget));
2470
2471 snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
2472 if ((addrlist = httpAddrGetList(hostTarget, AF_UNSPEC, fqdn)) != NULL)
2473 {
2474 for (addr = addrlist; addr; addr = addr->next)
2475 {
2476 int error = getnameinfo(&(addr->addr.addr),
2477 httpAddrLength(&(addr->addr)),
2478 fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
2479
2480 if (!error)
2481 {
2482 DEBUG_printf(("8http_resolve_cb: Found \"%s\".", fqdn));
2483
2484 if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn ||
2485 _cups_strcasecmp(hostptr, ".local"))
2486 {
2487 hostTarget = fqdn;
2488 break;
2489 }
2490 }
2491#ifdef DEBUG
2492 else
2493 DEBUG_printf(("8http_resolve_cb: \"%s\" did not resolve: %d",
2494 httpAddrString(&(addr->addr), fqdn, sizeof(fqdn)),
2495 error));
2496#endif /* DEBUG */
2497 }
0fa6c7fa
MS
2498
2499 httpAddrFreeList(addrlist);
f3c17241
MS
2500 }
2501 }
2502
2503 /*
2504 * Assemble the final device URI using the resolved hostname...
2505 */
2506
2507 httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme,
2508 NULL, hostTarget, port, resource);
2509 DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\".", uribuf->buffer));
2510
2511 avahi_simple_poll_quit(uribuf->poll);
2512}
5eb9da71
MS
2513#endif /* HAVE_DNSSD */
2514
2515
ef416fc2 2516/*
f2d18633 2517 * End of "$Id$".
ef416fc2 2518 */