]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/http-support.c
Merge changes from CUPS 1.5svn-r9352.
[thirdparty/cups.git] / cups / http-support.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $"
ef416fc2 3 *
71e16022 4 * HTTP support routines for CUPS.
ef416fc2 5 *
71e16022 6 * Copyright 2007-2010 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * httpAssembleURI() - Assemble a uniform resource identifier from its
20 * components.
21 * httpAssembleURIf() - Assemble a uniform resource identifier from its
22 * components with a formatted resource.
23 * httpDecode64() - Base64-decode a string.
24 * httpDecode64_2() - Base64-decode a string.
25 * httpEncode64() - Base64-encode a string.
26 * httpEncode64_2() - Base64-encode a string.
27 * httpGetDateString() - Get a formatted date/time string from a time value.
28 * httpGetDateString2() - Get a formatted date/time string from a time value.
29 * httpGetDateTime() - Get a time value from a formatted date/time string.
30 * httpSeparate() - Separate a Universal Resource Identifier into its
31 * components.
32 * httpSeparate2() - Separate a Universal Resource Identifier into its
33 * components.
34 * httpSeparateURI() - Separate a Universal Resource Identifier into its
35 * components.
36 * httpStatus() - Return a short string describing a HTTP status code.
37 * _cups_hstrerror() - hstrerror() emulation function for Solaris and
38 * others...
839a51c8 39 * _httpEncodeURI() - Percent-encode a HTTP request URI.
5eb9da71 40 * _httpResolveURI() - Resolve a DNS-SD URI.
ef416fc2 41 * http_copy_decode() - Copy and decode a URI.
42 * http_copy_encode() - Copy and encode a URI.
5eb9da71 43 * resolve_callback() - Build a device URI for the given service name.
ef416fc2 44 */
45
46/*
47 * Include necessary headers...
48 */
49
71e16022 50#include "cups-private.h"
5eb9da71
MS
51#ifdef HAVE_DNSSD
52# include <dns_sd.h>
6d2f911b
MS
53# ifdef WIN32
54# include <io.h>
55# elif defined(HAVE_POLL)
56# include <poll.h>
57# else
58# include <sys/select.h>
59# endif /* WIN32 */
5eb9da71
MS
60#endif /* HAVE_DNSSD */
61
62
63/*
64 * Local types...
65 */
66
67typedef struct _http_uribuf_s /* URI buffer */
68{
69 char *buffer; /* Pointer to buffer */
70 size_t bufsize; /* Size of buffer */
71} _http_uribuf_t;
ef416fc2 72
73
74/*
75 * Local globals...
76 */
77
78static const char * const http_days[7] =
79 {
0a682745
MS
80 "Sun",
81 "Mon",
ef416fc2 82 "Tue",
83 "Wed",
84 "Thu",
85 "Fri",
86 "Sat"
87 };
88static const char * const http_months[12] =
89 {
90 "Jan",
91 "Feb",
92 "Mar",
93 "Apr",
94 "May",
95 "Jun",
96 "Jul",
97 "Aug",
98 "Sep",
99 "Oct",
100 "Nov",
101 "Dec"
102 };
103
104
105/*
106 * Local functions...
107 */
108
109static const char *http_copy_decode(char *dst, const char *src,
a4d04587 110 int dstsize, const char *term,
111 int decode);
ef416fc2 112static char *http_copy_encode(char *dst, const char *src,
a4d04587 113 char *dstend, const char *reserved,
114 const char *term, int encode);
5eb9da71 115#ifdef HAVE_DNSSD
6d2f911b 116static void DNSSD_API resolve_callback(DNSServiceRef sdRef,
5eb9da71
MS
117 DNSServiceFlags flags,
118 uint32_t interfaceIndex,
119 DNSServiceErrorType errorCode,
120 const char *fullName,
121 const char *hostTarget,
122 uint16_t port, uint16_t txtLen,
123 const unsigned char *txtRecord,
124 void *context);
125#endif /* HAVE_DNSSD */
ef416fc2 126
127
128/*
129 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
130 * components.
131 *
ecdc0628 132 * This function escapes reserved characters in the URI depending on the
133 * value of the "encoding" argument. You should use this function in
134 * place of traditional string functions whenever you need to create a
135 * URI string.
ef416fc2 136 *
426c6a59 137 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 138 */
139
140http_uri_status_t /* O - URI status */
a4d04587 141httpAssembleURI(
142 http_uri_coding_t encoding, /* I - Encoding flags */
143 char *uri, /* I - URI buffer */
144 int urilen, /* I - Size of URI buffer */
145 const char *scheme, /* I - Scheme name */
146 const char *username, /* I - Username */
147 const char *host, /* I - Hostname or address */
148 int port, /* I - Port number */
149 const char *resource) /* I - Resource */
ef416fc2 150{
151 char *ptr, /* Pointer into URI buffer */
152 *end; /* End of URI buffer */
ef416fc2 153
154
155 /*
156 * Range check input...
157 */
158
159 if (!uri || urilen < 1 || !scheme || port < 0)
160 {
161 if (uri)
162 *uri = '\0';
163
164 return (HTTP_URI_BAD_ARGUMENTS);
165 }
166
167 /*
168 * Assemble the URI starting with the scheme...
169 */
170
171 end = uri + urilen - 1;
a4d04587 172 ptr = http_copy_encode(uri, scheme, end, NULL, NULL, 0);
ef416fc2 173
174 if (!ptr)
175 goto assemble_overflow;
176
f7deaa1a 177 if (!strcmp(scheme, "mailto"))
ef416fc2 178 {
179 /*
180 * mailto: only has :, no //...
181 */
182
183 if (ptr < end)
184 *ptr++ = ':';
185 else
186 goto assemble_overflow;
187 }
188 else
189 {
190 /*
191 * Schemes other than mailto: all have //...
192 */
193
194 if ((ptr + 2) < end)
195 {
196 *ptr++ = ':';
197 *ptr++ = '/';
198 *ptr++ = '/';
199 }
200 else
201 goto assemble_overflow;
202 }
203
204 /*
205 * Next the username and hostname, if any...
206 */
207
208 if (host)
209 {
210 if (username && *username)
211 {
212 /*
213 * Add username@ first...
214 */
215
a4d04587 216 ptr = http_copy_encode(ptr, username, end, "/?@", NULL,
217 encoding & HTTP_URI_CODING_USERNAME);
ef416fc2 218
219 if (!ptr)
220 goto assemble_overflow;
221
222 if (ptr < end)
223 *ptr++ = '@';
224 else
225 goto assemble_overflow;
226 }
227
228 /*
229 * Then add the hostname. Since IPv6 is a particular pain to deal
c9fc04c6 230 * with, we have several special cases to deal with. If we get
ef416fc2 231 * an IPv6 address with brackets around it, assume it is already in
c9fc04c6
MS
232 * URI format. Since DNS-SD service names can sometimes look like
233 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
234 * too...
ef416fc2 235 */
236
c9fc04c6 237 if (host[0] != '[' && strchr(host, ':') && !strstr(host, "._tcp"))
ef416fc2 238 {
239 /*
c9fc04c6 240 * We have a raw IPv6 address...
ef416fc2 241 */
242
243 if (strchr(host, '%'))
244 {
245 /*
246 * We have a link-local address, add "[v1." prefix...
247 */
248
249 if ((ptr + 4) < end)
250 {
251 *ptr++ = '[';
252 *ptr++ = 'v';
253 *ptr++ = '1';
254 *ptr++ = '.';
255 }
256 else
257 goto assemble_overflow;
258 }
259 else
260 {
261 /*
262 * We have a normal address, add "[" prefix...
263 */
264
265 if (ptr < end)
266 *ptr++ = '[';
267 else
268 goto assemble_overflow;
269 }
270
271 /*
272 * Copy the rest of the IPv6 address, and terminate with "]".
273 */
274
275 while (ptr < end && *host)
276 {
277 if (*host == '%')
278 {
279 *ptr++ = '+'; /* Convert zone separator */
280 host ++;
281 }
282 else
283 *ptr++ = *host++;
284 }
285
286 if (*host)
287 goto assemble_overflow;
288
289 if (ptr < end)
290 *ptr++ = ']';
291 else
292 goto assemble_overflow;
293 }
294 else
295 {
296 /*
297 * Otherwise, just copy the host string...
298 */
299
7cf5915e 300 ptr = http_copy_encode(ptr, host, end, ":/?#[]@\\\"", NULL,
a4d04587 301 encoding & HTTP_URI_CODING_HOSTNAME);
ef416fc2 302
303 if (!ptr)
304 goto assemble_overflow;
305 }
306
307 /*
308 * Finish things off with the port number...
309 */
310
311 if (port > 0)
312 {
313 snprintf(ptr, end - ptr + 1, ":%d", port);
314 ptr += strlen(ptr);
315
316 if (ptr >= end)
317 goto assemble_overflow;
318 }
319 }
320
321 /*
322 * Last but not least, add the resource string...
323 */
324
a4d04587 325 if (resource)
ef416fc2 326 {
fa73b229 327 char *query; /* Pointer to query string */
328
329
fa73b229 330 /*
a4d04587 331 * Copy the resource string up to the query string if present...
fa73b229 332 */
333
a4d04587 334 query = strchr(resource, '?');
335 ptr = http_copy_encode(ptr, resource, end, NULL, "?",
336 encoding & HTTP_URI_CODING_RESOURCE);
ef416fc2 337 if (!ptr)
338 goto assemble_overflow;
fa73b229 339
340 if (query)
341 {
342 /*
343 * Copy query string without encoding...
344 */
345
a4d04587 346 ptr = http_copy_encode(ptr, query, end, NULL, NULL,
347 encoding & HTTP_URI_CODING_QUERY);
348 if (!ptr)
349 goto assemble_overflow;
fa73b229 350 }
ef416fc2 351 }
352 else if (ptr < end)
353 *ptr++ = '/';
354 else
355 goto assemble_overflow;
356
357 /*
358 * Nul-terminate the URI buffer and return with no errors...
359 */
360
361 *ptr = '\0';
362
363 return (HTTP_URI_OK);
364
365 /*
366 * Clear the URI string and return an overflow error; I don't usually
367 * like goto's, but in this case it makes sense...
368 */
369
370 assemble_overflow:
371
372 *uri = '\0';
373 return (HTTP_URI_OVERFLOW);
374}
375
376
a4d04587 377/*
378 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
379 * components with a formatted resource.
380 *
381 * This function creates a formatted version of the resource string
ecdc0628 382 * argument "resourcef" and escapes reserved characters in the URI
383 * depending on the value of the "encoding" argument. You should use
384 * this function in place of traditional string functions whenever
385 * you need to create a URI string.
a4d04587 386 *
426c6a59 387 * @since CUPS 1.2/Mac OS X 10.5@
a4d04587 388 */
389
390http_uri_status_t /* O - URI status */
391httpAssembleURIf(
392 http_uri_coding_t encoding, /* I - Encoding flags */
393 char *uri, /* I - URI buffer */
394 int urilen, /* I - Size of URI buffer */
395 const char *scheme, /* I - Scheme name */
396 const char *username, /* I - Username */
397 const char *host, /* I - Hostname or address */
398 int port, /* I - Port number */
399 const char *resourcef, /* I - Printf-style resource */
400 ...) /* I - Additional arguments as needed */
401{
402 va_list ap; /* Pointer to additional arguments */
403 char resource[1024]; /* Formatted resource string */
404 int bytes; /* Bytes in formatted string */
405
406
407 /*
408 * Range check input...
409 */
410
411 if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef)
412 {
413 if (uri)
414 *uri = '\0';
415
416 return (HTTP_URI_BAD_ARGUMENTS);
417 }
418
419 /*
420 * Format the resource string and assemble the URI...
421 */
422
423 va_start(ap, resourcef);
424 bytes = vsnprintf(resource, sizeof(resource), resourcef, ap);
425 va_end(ap);
426
427 if (bytes >= sizeof(resource))
428 {
429 *uri = '\0';
430 return (HTTP_URI_OVERFLOW);
431 }
432 else
433 return (httpAssembleURI(encoding, uri, urilen, scheme, username, host,
434 port, resource));
435}
436
437
ef416fc2 438/*
439 * 'httpDecode64()' - Base64-decode a string.
ecdc0628 440 *
441 * This function is deprecated. Use the httpDecode64_2() function instead
442 * which provides buffer length arguments.
443 *
444 * @deprecated@
ef416fc2 445 */
446
447char * /* O - Decoded string */
448httpDecode64(char *out, /* I - String to write to */
449 const char *in) /* I - String to read from */
450{
451 int outlen; /* Output buffer length */
452
453
454 /*
455 * Use the old maximum buffer size for binary compatibility...
456 */
457
458 outlen = 512;
459
460 return (httpDecode64_2(out, &outlen, in));
461}
462
463
464/*
465 * 'httpDecode64_2()' - Base64-decode a string.
466 *
426c6a59 467 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 468 */
469
470char * /* O - Decoded string */
471httpDecode64_2(char *out, /* I - String to write to */
472 int *outlen, /* IO - Size of output string */
473 const char *in) /* I - String to read from */
474{
475 int pos, /* Bit position */
476 base64; /* Value of this character */
477 char *outptr, /* Output pointer */
478 *outend; /* End of output buffer */
479
480
481 /*
482 * Range check input...
483 */
484
fa73b229 485 if (!out || !outlen || *outlen < 1 || !in)
ef416fc2 486 return (NULL);
487
fa73b229 488 if (!*in)
489 {
490 *out = '\0';
491 *outlen = 0;
492
493 return (out);
494 }
495
ef416fc2 496 /*
497 * Convert from base-64 to bytes...
498 */
499
500 for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++)
501 {
502 /*
503 * Decode this character into a number from 0 to 63...
504 */
505
506 if (*in >= 'A' && *in <= 'Z')
507 base64 = *in - 'A';
508 else if (*in >= 'a' && *in <= 'z')
509 base64 = *in - 'a' + 26;
510 else if (*in >= '0' && *in <= '9')
511 base64 = *in - '0' + 52;
512 else if (*in == '+')
513 base64 = 62;
514 else if (*in == '/')
515 base64 = 63;
516 else if (*in == '=')
517 break;
518 else
519 continue;
520
521 /*
522 * Store the result in the appropriate chars...
523 */
524
525 switch (pos)
526 {
527 case 0 :
528 if (outptr < outend)
529 *outptr = base64 << 2;
530 pos ++;
531 break;
532 case 1 :
533 if (outptr < outend)
534 *outptr++ |= (base64 >> 4) & 3;
535 if (outptr < outend)
536 *outptr = (base64 << 4) & 255;
537 pos ++;
538 break;
539 case 2 :
540 if (outptr < outend)
541 *outptr++ |= (base64 >> 2) & 15;
542 if (outptr < outend)
543 *outptr = (base64 << 6) & 255;
544 pos ++;
545 break;
546 case 3 :
547 if (outptr < outend)
548 *outptr++ |= base64;
549 pos = 0;
550 break;
551 }
552 }
553
554 *outptr = '\0';
555
556 /*
557 * Return the decoded string and size...
558 */
559
560 *outlen = (int)(outptr - out);
561
562 return (out);
563}
564
565
566/*
567 * 'httpEncode64()' - Base64-encode a string.
ecdc0628 568 *
569 * This function is deprecated. Use the httpEncode64_2() function instead
570 * which provides buffer length arguments.
571 *
572 * @deprecated@
ef416fc2 573 */
574
575char * /* O - Encoded string */
576httpEncode64(char *out, /* I - String to write to */
577 const char *in) /* I - String to read from */
578{
b86bc4cf 579 return (httpEncode64_2(out, 512, in, (int)strlen(in)));
ef416fc2 580}
581
582
583/*
584 * 'httpEncode64_2()' - Base64-encode a string.
585 *
426c6a59 586 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 587 */
588
589char * /* O - Encoded string */
590httpEncode64_2(char *out, /* I - String to write to */
591 int outlen, /* I - Size of output string */
592 const char *in, /* I - String to read from */
593 int inlen) /* I - Size of input string */
594{
595 char *outptr, /* Output pointer */
596 *outend; /* End of output buffer */
597 static const char base64[] = /* Base64 characters... */
598 {
599 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
600 "abcdefghijklmnopqrstuvwxyz"
601 "0123456789"
602 "+/"
603 };
604
605
606 /*
607 * Range check input...
608 */
609
610 if (!out || outlen < 1 || !in)
611 return (NULL);
612
613 /*
614 * Convert bytes to base-64...
615 */
616
617 for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --)
618 {
619 /*
620 * Encode the up to 3 characters as 4 Base64 numbers...
621 */
622
623 if (outptr < outend)
624 *outptr ++ = base64[(in[0] & 255) >> 2];
26d47ec6 625
ef416fc2 626 if (outptr < outend)
26d47ec6 627 {
628 if (inlen > 1)
629 *outptr ++ = base64[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63];
630 else
631 *outptr ++ = base64[((in[0] & 255) << 4) & 63];
632 }
ef416fc2 633
634 in ++;
635 inlen --;
636 if (inlen <= 0)
637 {
638 if (outptr < outend)
639 *outptr ++ = '=';
640 if (outptr < outend)
641 *outptr ++ = '=';
642 break;
643 }
644
645 if (outptr < outend)
26d47ec6 646 {
647 if (inlen > 1)
648 *outptr ++ = base64[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63];
649 else
650 *outptr ++ = base64[((in[0] & 255) << 2) & 63];
651 }
ef416fc2 652
653 in ++;
654 inlen --;
655 if (inlen <= 0)
656 {
657 if (outptr < outend)
658 *outptr ++ = '=';
659 break;
660 }
661
662 if (outptr < outend)
663 *outptr ++ = base64[in[0] & 63];
664 }
665
666 *outptr = '\0';
667
668 /*
669 * Return the encoded string...
670 */
671
672 return (out);
673}
674
675
676/*
677 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
678 *
679 * @deprecated@
680 */
681
682const char * /* O - Date/time string */
683httpGetDateString(time_t t) /* I - UNIX time */
684{
685 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
686
687
688 return (httpGetDateString2(t, cg->http_date, sizeof(cg->http_date)));
689}
690
691
692/*
693 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
694 *
426c6a59 695 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 696 */
697
698const char * /* O - Date/time string */
699httpGetDateString2(time_t t, /* I - UNIX time */
700 char *s, /* I - String buffer */
701 int slen) /* I - Size of string buffer */
702{
703 struct tm *tdate; /* UNIX date/time data */
704
705
706 tdate = gmtime(&t);
707 snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT",
708 http_days[tdate->tm_wday], tdate->tm_mday,
709 http_months[tdate->tm_mon], tdate->tm_year + 1900,
710 tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
711
712 return (s);
713}
714
715
716/*
717 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
718 */
719
720time_t /* O - UNIX time */
721httpGetDateTime(const char *s) /* I - Date/time string */
722{
723 int i; /* Looping var */
724 char mon[16]; /* Abbreviated month name */
725 int day, year; /* Day of month and year */
726 int hour, min, sec; /* Time */
727 int days; /* Number of days since 1970 */
728 static const int normal_days[] = /* Days to a month, normal years */
729 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
730 static const int leap_days[] = /* Days to a month, leap years */
731 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
732
733
e07d4801 734 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s));
ef416fc2 735
736 /*
737 * Extract the date and time from the formatted string...
738 */
739
740 if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6)
741 return (0);
742
e07d4801
MS
743 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
744 "min=%d, sec=%d", day, mon, year, hour, min, sec));
ef416fc2 745
746 /*
747 * Convert the month name to a number from 0 to 11.
748 */
749
750 for (i = 0; i < 12; i ++)
751 if (!strcasecmp(mon, http_months[i]))
752 break;
753
754 if (i >= 12)
755 return (0);
756
e07d4801 757 DEBUG_printf(("4httpGetDateTime: i=%d", i));
ef416fc2 758
759 /*
760 * Now convert the date and time to a UNIX time value in seconds since
761 * 1970. We can't use mktime() since the timezone may not be UTC but
762 * the date/time string *is* UTC.
763 */
764
765 if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0))
766 days = leap_days[i] + day - 1;
767 else
768 days = normal_days[i] + day - 1;
769
e07d4801 770 DEBUG_printf(("4httpGetDateTime: days=%d", days));
ef416fc2 771
772 days += (year - 1970) * 365 + /* 365 days per year (normally) */
773 ((year - 1) / 4 - 492) - /* + leap days */
774 ((year - 1) / 100 - 19) + /* - 100 year days */
775 ((year - 1) / 400 - 4); /* + 400 year days */
776
e07d4801 777 DEBUG_printf(("4httpGetDateTime: days=%d\n", days));
ef416fc2 778
779 return (days * 86400 + hour * 3600 + min * 60 + sec);
780}
781
782
783/*
784 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
785 * components.
ecdc0628 786 *
787 * This function is deprecated; use the httpSeparateURI() function instead.
788 *
789 * @deprecated@
ef416fc2 790 */
791
792void
793httpSeparate(const char *uri, /* I - Universal Resource Identifier */
794 char *scheme, /* O - Scheme [32] (http, https, etc.) */
795 char *username, /* O - Username [1024] */
796 char *host, /* O - Hostname [1024] */
797 int *port, /* O - Port number to use */
798 char *resource) /* O - Resource/filename [1024] */
799{
a4d04587 800 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, 32, username,
801 HTTP_MAX_URI, host, HTTP_MAX_URI, port, resource,
802 HTTP_MAX_URI);
ef416fc2 803}
804
805
806/*
807 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
808 * components.
809 *
ecdc0628 810 * This function is deprecated; use the httpSeparateURI() function instead.
811 *
426c6a59 812 * @since CUPS 1.1.21/Mac OS X 10.4@
ecdc0628 813 * @deprecated@
ef416fc2 814 */
815
816void
817httpSeparate2(const char *uri, /* I - Universal Resource Identifier */
818 char *scheme, /* O - Scheme (http, https, etc.) */
819 int schemelen, /* I - Size of scheme buffer */
820 char *username, /* O - Username */
821 int usernamelen, /* I - Size of username buffer */
822 char *host, /* O - Hostname */
823 int hostlen, /* I - Size of hostname buffer */
824 int *port, /* O - Port number to use */
825 char *resource, /* O - Resource/filename */
826 int resourcelen) /* I - Size of resource buffer */
827{
a4d04587 828 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, schemelen, username,
829 usernamelen, host, hostlen, port, resource, resourcelen);
ef416fc2 830}
831
832
833/*
834 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
835 * components.
836 *
426c6a59 837 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 838 */
839
840http_uri_status_t /* O - Result of separation */
a4d04587 841httpSeparateURI(
842 http_uri_coding_t decoding, /* I - Decoding flags */
843 const char *uri, /* I - Universal Resource Identifier */
844 char *scheme, /* O - Scheme (http, https, etc.) */
845 int schemelen, /* I - Size of scheme buffer */
846 char *username, /* O - Username */
847 int usernamelen, /* I - Size of username buffer */
848 char *host, /* O - Hostname */
849 int hostlen, /* I - Size of hostname buffer */
850 int *port, /* O - Port number to use */
851 char *resource, /* O - Resource/filename */
852 int resourcelen) /* I - Size of resource buffer */
ef416fc2 853{
854 char *ptr, /* Pointer into string... */
855 *end; /* End of string */
856 const char *sep; /* Separator character */
857 http_uri_status_t status; /* Result of separation */
858
859
860 /*
861 * Initialize everything to blank...
862 */
863
864 if (scheme && schemelen > 0)
865 *scheme = '\0';
866
867 if (username && usernamelen > 0)
868 *username = '\0';
869
870 if (host && hostlen > 0)
871 *host = '\0';
872
873 if (port)
874 *port = 0;
875
876 if (resource && resourcelen > 0)
877 *resource = '\0';
878
879 /*
880 * Range check input...
881 */
882
883 if (!uri || !port || !scheme || schemelen <= 0 || !username ||
884 usernamelen <= 0 || !host || hostlen <= 0 || !resource ||
885 resourcelen <= 0)
886 return (HTTP_URI_BAD_ARGUMENTS);
887
888 if (!*uri)
889 return (HTTP_URI_BAD_URI);
890
891 /*
892 * Grab the scheme portion of the URI...
893 */
894
895 status = HTTP_URI_OK;
896
897 if (!strncmp(uri, "//", 2))
898 {
899 /*
900 * Workaround for HP IPP client bug...
901 */
902
903 strlcpy(scheme, "ipp", schemelen);
904 status = HTTP_URI_MISSING_SCHEME;
905 }
906 else if (*uri == '/')
907 {
908 /*
909 * Filename...
910 */
911
912 strlcpy(scheme, "file", schemelen);
913 status = HTTP_URI_MISSING_SCHEME;
914 }
915 else
916 {
917 /*
918 * Standard URI with scheme...
919 */
920
921 for (ptr = scheme, end = scheme + schemelen - 1;
922 *uri && *uri != ':' && ptr < end;)
7cf5915e
MS
923 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
924 "abcdefghijklmnopqrstuvwxyz"
925 "0123456789-+.", *uri) != NULL)
ef416fc2 926 *ptr++ = *uri++;
927 else
928 break;
929
930 *ptr = '\0';
931
932 if (*uri != ':')
933 {
934 *scheme = '\0';
935 return (HTTP_URI_BAD_SCHEME);
936 }
937
938 uri ++;
939 }
940
941 /*
942 * Set the default port number...
943 */
944
945 if (!strcmp(scheme, "http"))
946 *port = 80;
947 else if (!strcmp(scheme, "https"))
948 *port = 443;
7cf5915e 949 else if (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps"))
ef416fc2 950 *port = 631;
951 else if (!strcasecmp(scheme, "lpd"))
952 *port = 515;
953 else if (!strcmp(scheme, "socket")) /* Not yet registered with IANA... */
954 *port = 9100;
955 else if (strcmp(scheme, "file") && strcmp(scheme, "mailto"))
956 status = HTTP_URI_UNKNOWN_SCHEME;
957
958 /*
959 * Now see if we have a hostname...
960 */
961
962 if (!strncmp(uri, "//", 2))
963 {
964 /*
965 * Yes, extract it...
966 */
967
968 uri += 2;
969
970 /*
971 * Grab the username, if any...
972 */
973
974 if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@')
975 {
976 /*
977 * Get a username:password combo...
978 */
979
a4d04587 980 uri = http_copy_decode(username, uri, usernamelen, "@",
981 decoding & HTTP_URI_CODING_USERNAME);
ef416fc2 982
983 if (!uri)
984 {
985 *username = '\0';
986 return (HTTP_URI_BAD_USERNAME);
987 }
988
989 uri ++;
990 }
991
992 /*
993 * Then the hostname/IP address...
994 */
995
996 if (*uri == '[')
997 {
998 /*
999 * Grab IPv6 address...
1000 */
1001
1002 uri ++;
1003 if (!strncmp(uri, "v1.", 3))
1004 uri += 3; /* Skip IPvN leader... */
1005
a4d04587 1006 uri = http_copy_decode(host, uri, hostlen, "]",
1007 decoding & HTTP_URI_CODING_HOSTNAME);
ef416fc2 1008
1009 if (!uri)
1010 {
1011 *host = '\0';
1012 return (HTTP_URI_BAD_HOSTNAME);
1013 }
1014
1015 /*
1016 * Validate value...
1017 */
1018
1019 if (*uri != ']')
1020 {
1021 *host = '\0';
1022 return (HTTP_URI_BAD_HOSTNAME);
1023 }
1024
1025 uri ++;
1026
1027 for (ptr = host; *ptr; ptr ++)
1028 if (*ptr == '+')
1029 {
1030 /*
1031 * Convert zone separator to % and stop here...
1032 */
1033
1034 *ptr = '%';
1035 break;
1036 }
1037 else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255))
1038 {
1039 *host = '\0';
1040 return (HTTP_URI_BAD_HOSTNAME);
1041 }
1042 }
1043 else
1044 {
1045 /*
b423cd4c 1046 * Validate the hostname or IPv4 address first...
1047 */
1048
1049 for (ptr = (char *)uri; *ptr; ptr ++)
1050 if (strchr(":?/", *ptr))
1051 break;
1052 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1053 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1054 "0123456789"
1055 "-._~"
1056 "%"
2fb76298 1057 "!$&'()*+,;=\\", *ptr))
b423cd4c 1058 {
1059 *host = '\0';
1060 return (HTTP_URI_BAD_HOSTNAME);
1061 }
1062
1063 /*
1064 * Then copy the hostname or IPv4 address to the buffer...
ef416fc2 1065 */
1066
a4d04587 1067 uri = http_copy_decode(host, uri, hostlen, ":?/",
1068 decoding & HTTP_URI_CODING_HOSTNAME);
ef416fc2 1069
1070 if (!uri)
1071 {
1072 *host = '\0';
1073 return (HTTP_URI_BAD_HOSTNAME);
1074 }
ef416fc2 1075 }
1076
1077 /*
1078 * Validate hostname for file scheme - only empty and localhost are
1079 * acceptable.
1080 */
1081
1082 if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0])
1083 {
1084 *host = '\0';
1085 return (HTTP_URI_BAD_HOSTNAME);
1086 }
1087
1088 /*
1089 * See if we have a port number...
1090 */
1091
1092 if (*uri == ':')
1093 {
1094 /*
1095 * Yes, collect the port number...
1096 */
1097
dfd5680b
MS
1098 if (!isdigit(uri[1] & 255))
1099 {
1100 *port = 0;
1101 return (HTTP_URI_BAD_PORT);
1102 }
1103
ef416fc2 1104 *port = strtol(uri + 1, (char **)&uri, 10);
1105
d6ae789d 1106 if (*uri != '/' && *uri)
ef416fc2 1107 {
1108 *port = 0;
1109 return (HTTP_URI_BAD_PORT);
1110 }
1111 }
1112 }
1113
1114 /*
1115 * The remaining portion is the resource string...
1116 */
1117
1118 if (*uri == '?' || !*uri)
1119 {
1120 /*
1121 * Hostname but no path...
1122 */
1123
1124 status = HTTP_URI_MISSING_RESOURCE;
1125 *resource = '/';
fa73b229 1126
1127 /*
a4d04587 1128 * Copy any query string...
fa73b229 1129 */
1130
1131 if (*uri == '?')
a4d04587 1132 uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL,
1133 decoding & HTTP_URI_CODING_QUERY);
fa73b229 1134 else
1135 resource[1] = '\0';
ef416fc2 1136 }
1137 else
fa73b229 1138 {
a4d04587 1139 uri = http_copy_decode(resource, uri, resourcelen, "?",
1140 decoding & HTTP_URI_CODING_RESOURCE);
fa73b229 1141
1142 if (uri && *uri == '?')
1143 {
1144 /*
a4d04587 1145 * Concatenate any query string...
fa73b229 1146 */
1147
a4d04587 1148 char *resptr = resource + strlen(resource);
1149
b86bc4cf 1150 uri = http_copy_decode(resptr, uri, resourcelen - (int)(resptr - resource),
a4d04587 1151 NULL, decoding & HTTP_URI_CODING_QUERY);
fa73b229 1152 }
1153 }
ef416fc2 1154
1155 if (!uri)
1156 {
1157 *resource = '\0';
1158 return (HTTP_URI_BAD_RESOURCE);
1159 }
1160
1161 /*
1162 * Return the URI separation status...
1163 */
1164
1165 return (status);
1166}
1167
1168
1169/*
1170 * 'httpStatus()' - Return a short string describing a HTTP status code.
9f5eb9be
MS
1171 *
1172 * The returned string is localized to the current POSIX locale and is based
1173 * on the status strings defined in RFC 2616.
ef416fc2 1174 */
1175
9f5eb9be 1176const char * /* O - Localized status string */
ef416fc2 1177httpStatus(http_status_t status) /* I - HTTP status code */
1178{
9f5eb9be
MS
1179 const char *s; /* Status string */
1180 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1181
1182
1183 if (!cg->lang_default)
1184 cg->lang_default = cupsLangDefault();
1185
ef416fc2 1186 switch (status)
1187 {
1188 case HTTP_CONTINUE :
9f5eb9be
MS
1189 s = _("Continue");
1190 break;
ef416fc2 1191 case HTTP_SWITCHING_PROTOCOLS :
9f5eb9be
MS
1192 s = _("Switching Protocols");
1193 break;
ef416fc2 1194 case HTTP_OK :
9f5eb9be
MS
1195 s = _("OK");
1196 break;
ef416fc2 1197 case HTTP_CREATED :
9f5eb9be
MS
1198 s = _("Created");
1199 break;
ef416fc2 1200 case HTTP_ACCEPTED :
9f5eb9be
MS
1201 s = _("Accepted");
1202 break;
ef416fc2 1203 case HTTP_NO_CONTENT :
9f5eb9be
MS
1204 s = _("No Content");
1205 break;
d6ae789d 1206 case HTTP_MOVED_PERMANENTLY :
9f5eb9be
MS
1207 s = _("Moved Permanently");
1208 break;
d6ae789d 1209 case HTTP_SEE_OTHER :
9f5eb9be
MS
1210 s = _("See Other");
1211 break;
ef416fc2 1212 case HTTP_NOT_MODIFIED :
9f5eb9be
MS
1213 s = _("Not Modified");
1214 break;
ef416fc2 1215 case HTTP_BAD_REQUEST :
9f5eb9be
MS
1216 s = _("Bad Request");
1217 break;
ef416fc2 1218 case HTTP_UNAUTHORIZED :
f11a948a 1219 case HTTP_AUTHORIZATION_CANCELED :
9f5eb9be
MS
1220 s = _("Unauthorized");
1221 break;
ef416fc2 1222 case HTTP_FORBIDDEN :
9f5eb9be
MS
1223 s = _("Forbidden");
1224 break;
ef416fc2 1225 case HTTP_NOT_FOUND :
9f5eb9be
MS
1226 s = _("Not Found");
1227 break;
ef416fc2 1228 case HTTP_REQUEST_TOO_LARGE :
9f5eb9be
MS
1229 s = _("Request Entity Too Large");
1230 break;
ef416fc2 1231 case HTTP_URI_TOO_LONG :
9f5eb9be
MS
1232 s = _("URI Too Long");
1233 break;
ef416fc2 1234 case HTTP_UPGRADE_REQUIRED :
9f5eb9be
MS
1235 s = _("Upgrade Required");
1236 break;
ef416fc2 1237 case HTTP_NOT_IMPLEMENTED :
9f5eb9be
MS
1238 s = _("Not Implemented");
1239 break;
ef416fc2 1240 case HTTP_NOT_SUPPORTED :
9f5eb9be
MS
1241 s = _("Not Supported");
1242 break;
b423cd4c 1243 case HTTP_EXPECTATION_FAILED :
9f5eb9be
MS
1244 s = _("Expectation Failed");
1245 break;
1246 case HTTP_SERVICE_UNAVAILABLE :
1247 s = _("Service Unavailable");
1248 break;
94da7e34
MS
1249 case HTTP_SERVER_ERROR :
1250 s = _("Internal Server Error");
1251 break;
7cf5915e
MS
1252 case HTTP_PKI_ERROR :
1253 s = _("SSL/TLS Negotiation Error");
1254 break;
b423cd4c 1255
ef416fc2 1256 default :
9f5eb9be
MS
1257 s = _("Unknown");
1258 break;
ef416fc2 1259 }
9f5eb9be
MS
1260
1261 return (_cupsLangString(cg->lang_default, s));
ef416fc2 1262}
1263
1264
1265#ifndef HAVE_HSTRERROR
1266/*
1267 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1268 */
1269
1270const char * /* O - Error string */
1271_cups_hstrerror(int error) /* I - Error number */
1272{
1273 static const char * const errors[] = /* Error strings */
1274 {
1275 "OK",
1276 "Host not found.",
1277 "Try again.",
1278 "Unrecoverable lookup error.",
1279 "No data associated with name."
1280 };
1281
1282
1283 if (error < 0 || error > 4)
1284 return ("Unknown hostname lookup error.");
1285 else
1286 return (errors[error]);
1287}
1288#endif /* !HAVE_HSTRERROR */
1289
1290
839a51c8
MS
1291/*
1292 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1293 */
1294
1295char * /* O - Encoded URI */
1296_httpEncodeURI(char *dst, /* I - Destination buffer */
1297 const char *src, /* I - Source URI */
1298 size_t dstsize) /* I - Size of destination buffer */
1299{
1300 http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, 1);
1301 return (dst);
1302}
1303
1304
5eb9da71
MS
1305/*
1306 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1307 */
1308
1309const char * /* O - Resolved URI */
1310_httpResolveURI(
1311 const char *uri, /* I - DNS-SD URI */
1312 char *resolved_uri, /* I - Buffer for resolved URI */
1f0275e3 1313 size_t resolved_size, /* I - Size of URI buffer */
dfd5680b 1314 int logit) /* I - Log progress to stderr? */
5eb9da71
MS
1315{
1316 char scheme[32], /* URI components... */
1317 userpass[256],
1318 hostname[1024],
1319 resource[1024];
1320 int port;
b19ccc9e 1321#ifdef DEBUG
1f0275e3 1322 http_uri_status_t status; /* URI decode status */
b19ccc9e 1323#endif /* DEBUG */
5eb9da71
MS
1324
1325
e07d4801
MS
1326 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1327 "resolved_size=" CUPS_LLFMT ")", uri, resolved_uri,
1f0275e3
MS
1328 CUPS_LLCAST resolved_size));
1329
5eb9da71
MS
1330 /*
1331 * Get the device URI...
1332 */
1333
b19ccc9e 1334#ifdef DEBUG
1f0275e3
MS
1335 if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1336 sizeof(scheme), userpass, sizeof(userpass),
1337 hostname, sizeof(hostname), &port, resource,
1338 sizeof(resource))) < HTTP_URI_OK)
b19ccc9e
MS
1339#else
1340 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1341 sizeof(scheme), userpass, sizeof(userpass),
1342 hostname, sizeof(hostname), &port, resource,
1343 sizeof(resource)) < HTTP_URI_OK)
1344#endif /* DEBUG */
1f0275e3 1345 {
dfd5680b 1346 if (logit)
4d301e69 1347 _cupsLangPrintf(stderr, _("Bad device URI \"%s\"\n"), uri);
1f0275e3 1348
e07d4801
MS
1349 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status));
1350 DEBUG_puts("5_httpResolveURI: Returning NULL");
5eb9da71 1351 return (NULL);
1f0275e3 1352 }
5eb9da71
MS
1353
1354 /*
1355 * Resolve it as needed...
1356 */
1357
1358 if (strstr(hostname, "._tcp"))
1359 {
1360#ifdef HAVE_DNSSD
c7017ecc
MS
1361# ifdef WIN32
1362# pragma comment(lib, "dnssd.lib")
1363# endif /* WIN32 */
38e73f87
MS
1364 DNSServiceRef ref, /* DNS-SD master service reference */
1365 domainref, /* DNS-SD service reference for domain */
1366 localref; /* DNS-SD service reference for .local */
6c48a6ca
MS
1367 int domainsent = 0, /* Send the domain resolve? */
1368 offline = 0; /* offline-report state set? */
5eb9da71
MS
1369 char *regtype, /* Pointer to type in hostname */
1370 *domain; /* Pointer to domain in hostname */
1371 _http_uribuf_t uribuf; /* URI buffer */
6d2f911b 1372#ifdef HAVE_POLL
38e73f87 1373 struct pollfd polldata; /* Polling data */
6d2f911b
MS
1374#else /* select() */
1375 fd_set input_set; /* Input set for select() */
1376 struct timeval stimeout; /* Timeout value for select() */
1377#endif /* HAVE_POLL */
38e73f87
MS
1378
1379 if (logit)
1380 fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
5eb9da71
MS
1381
1382 /*
1383 * Separate the hostname into service name, registration type, and domain...
1384 */
1385
1f0275e3
MS
1386 for (regtype = strstr(hostname, "._tcp") - 2;
1387 regtype > hostname;
1388 regtype --)
1389 if (regtype[0] == '.' && regtype[1] == '_')
1390 {
1391 /*
1392 * Found ._servicetype in front of ._tcp...
1393 */
1394
1395 *regtype++ = '\0';
1396 break;
1397 }
1398
1399 if (regtype <= hostname)
1400 {
e07d4801 1401 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1f0275e3
MS
1402 return (NULL);
1403 }
5eb9da71 1404
5eb9da71
MS
1405 for (domain = strchr(regtype, '.');
1406 domain;
1407 domain = strchr(domain + 1, '.'))
1408 if (domain[1] != '_')
1409 break;
1410
1411 if (domain)
1412 *domain++ = '\0';
1413
1414 uribuf.buffer = resolved_uri;
1415 uribuf.bufsize = resolved_size;
1416
1417 resolved_uri[0] = '\0';
1418
e07d4801 1419 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1f0275e3 1420 "domain=\"%s\"\n", hostname, regtype, domain));
dfd5680b 1421 if (logit)
1f0275e3
MS
1422 {
1423 fputs("STATE: +connecting-to-device\n", stderr);
38e73f87
MS
1424 fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1425 "domain=\"local.\"...\n", hostname, regtype);
1f0275e3
MS
1426 }
1427
38e73f87
MS
1428 uri = NULL;
1429
1430 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
5eb9da71 1431 {
38e73f87
MS
1432 localref = ref;
1433 if (DNSServiceResolve(&localref, kDNSServiceFlagsShareConnection, 0,
1434 hostname, regtype, "local.", resolve_callback,
1435 &uribuf) == kDNSServiceErr_NoError)
1436 {
acb056cb
MS
1437 int fds; /* Number of ready descriptors */
1438 time_t timeout, /* Poll timeout */
1439 start_time = time(NULL);/* Start time */
1440
1441 for (;;)
38e73f87 1442 {
acb056cb
MS
1443 if (logit)
1444 _cupsLangPuts(stderr, _("INFO: Looking for printer...\n"));
1445
38e73f87 1446 /*
acb056cb
MS
1447 * For the first minute, wakeup every 2 seconds to emit a
1448 * "looking for printer" message...
38e73f87
MS
1449 */
1450
acb056cb
MS
1451 timeout = (time(NULL) < (start_time + 60)) ? 2000 : -1;
1452
6d2f911b 1453#ifdef HAVE_POLL
38e73f87
MS
1454 polldata.fd = DNSServiceRefSockFD(ref);
1455 polldata.events = POLLIN;
1456
acb056cb
MS
1457 fds = poll(&polldata, 1, timeout);
1458
6d2f911b
MS
1459#else /* select() */
1460 FD_ZERO(&input_set);
1461 FD_SET(DNSServiceRefSockFD(ref), &input_set);
1462
1463 stimeout.tv_sec = ((int)timeout) / 1000;
1464 stimeout.tv_usec = ((int)(timeout) * 1000) % 1000000;
1465
1466 fds = select(DNSServiceRefSockFD(ref)+1, &input_set, NULL, NULL,
1467 timeout < 0.0 ? NULL : &stimeout);
1468#endif /* HAVE_POLL */
1469
acb056cb
MS
1470 if (fds < 0)
1471 {
1472 if (errno != EINTR && errno != EAGAIN)
1473 {
1474 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno)));
1475 break;
1476 }
1477 }
1478 else if (fds == 0)
38e73f87
MS
1479 {
1480 /*
acb056cb
MS
1481 * Wait 2 seconds for a response to the local resolve; if nothing
1482 * comes in, do an additional domain resolution...
38e73f87
MS
1483 */
1484
acb056cb
MS
1485 if (domainsent == 0 && strcasecmp(domain, "local."))
1486 {
1487 if (logit)
1488 fprintf(stderr,
1489 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1490 "domain=\"%s\"...\n", hostname, regtype, domain);
1491
1492 domainref = ref;
1493 if (DNSServiceResolve(&domainref, kDNSServiceFlagsShareConnection, 0,
1494 hostname, regtype, domain, resolve_callback,
1495 &uribuf) == kDNSServiceErr_NoError)
1496 domainsent = 1;
1497 }
6c48a6ca
MS
1498
1499 /*
1500 * If it hasn't resolved within 5 seconds set the offline-report
1501 * printer-state-reason...
1502 */
1503
1504 if (logit && offline == 0 && time(NULL) > (start_time + 5))
1505 {
1506 fputs("STATE: +offline-report\n", stderr);
1507 offline = 1;
1508 }
38e73f87 1509 }
acb056cb
MS
1510 else
1511 {
1512 if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError)
1513 {
1514 uri = resolved_uri;
1515 break;
1516 }
1517 }
1518 }
38e73f87
MS
1519
1520 if (domainsent)
1521 DNSServiceRefDeallocate(domainref);
1522
1523 DNSServiceRefDeallocate(localref);
1524 }
5eb9da71
MS
1525
1526 DNSServiceRefDeallocate(ref);
1527 }
1f0275e3 1528
dfd5680b 1529 if (logit)
38e73f87
MS
1530 {
1531 if (uri)
38e73f87 1532 fprintf(stderr, "DEBUG: Resolved as \"%s\"...\n", uri);
f11a948a 1533 else
4d301e69 1534 fputs("DEBUG: Unable to resolve URI\n", stderr);
38e73f87 1535
6c48a6ca 1536 fputs("STATE: -connecting-to-device,offline-report\n", stderr);
38e73f87 1537 }
1f0275e3
MS
1538
1539#else
1540 /*
1541 * No DNS-SD support...
1542 */
5eb9da71
MS
1543
1544 uri = NULL;
1f0275e3
MS
1545#endif /* HAVE_DNSSD */
1546
dfd5680b 1547 if (logit && !uri)
4d301e69 1548 _cupsLangPuts(stderr, _("Unable to find printer\n"));
5eb9da71
MS
1549 }
1550
e07d4801 1551 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri));
1f0275e3 1552
5eb9da71
MS
1553 return (uri);
1554}
1555
1556
ef416fc2 1557/*
1558 * 'http_copy_decode()' - Copy and decode a URI.
1559 */
1560
1561static const char * /* O - New source pointer or NULL on error */
1562http_copy_decode(char *dst, /* O - Destination buffer */
1563 const char *src, /* I - Source pointer */
1564 int dstsize, /* I - Destination size */
a4d04587 1565 const char *term, /* I - Terminating characters */
1566 int decode) /* I - Decode %-encoded values */
ef416fc2 1567{
1568 char *ptr, /* Pointer into buffer */
1569 *end; /* End of buffer */
1570 int quoted; /* Quoted character */
1571
1572
1573 /*
1574 * Copy the src to the destination until we hit a terminating character
1575 * or the end of the string.
1576 */
1577
e00b005a 1578 for (ptr = dst, end = dst + dstsize - 1;
1579 *src && (!term || !strchr(term, *src));
1580 src ++)
ef416fc2 1581 if (ptr < end)
1582 {
a4d04587 1583 if (*src == '%' && decode)
ef416fc2 1584 {
1585 if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255))
1586 {
1587 /*
1588 * Grab a hex-encoded character...
1589 */
1590
1591 src ++;
1592 if (isalpha(*src))
1593 quoted = (tolower(*src) - 'a' + 10) << 4;
1594 else
1595 quoted = (*src - '0') << 4;
1596
1597 src ++;
1598 if (isalpha(*src))
1599 quoted |= tolower(*src) - 'a' + 10;
1600 else
1601 quoted |= *src - '0';
1602
1603 *ptr++ = quoted;
1604 }
1605 else
1606 {
1607 /*
1608 * Bad hex-encoded character...
1609 */
1610
1611 *ptr = '\0';
1612 return (NULL);
1613 }
1614 }
1615 else
1616 *ptr++ = *src;
1617 }
1618
1619 *ptr = '\0';
1620
1621 return (src);
1622}
1623
1624
1625/*
1626 * 'http_copy_encode()' - Copy and encode a URI.
1627 */
1628
1629static char * /* O - End of current URI */
1630http_copy_encode(char *dst, /* O - Destination buffer */
1631 const char *src, /* I - Source pointer */
1632 char *dstend, /* I - End of destination buffer */
a4d04587 1633 const char *reserved, /* I - Extra reserved characters */
1634 const char *term, /* I - Terminating characters */
1635 int encode) /* I - %-encode reserved chars? */
ef416fc2 1636{
ac884b6a 1637 static const char hex[] = "0123456789ABCDEF";
ef416fc2 1638
1639
1640 while (*src && dst < dstend)
1641 {
a4d04587 1642 if (term && *src == *term)
1643 return (dst);
1644
1645 if (encode && (*src == '%' || *src <= ' ' || *src & 128 ||
1646 (reserved && strchr(reserved, *src))))
ef416fc2 1647 {
1648 /*
1649 * Hex encode reserved characters...
1650 */
1651
1652 if ((dst + 2) >= dstend)
1653 break;
1654
1655 *dst++ = '%';
1656 *dst++ = hex[(*src >> 4) & 15];
1657 *dst++ = hex[*src & 15];
1658
1659 src ++;
1660 }
1661 else
1662 *dst++ = *src++;
1663 }
1664
839a51c8
MS
1665 *dst = '\0';
1666
ef416fc2 1667 if (*src)
1668 return (NULL);
1669 else
1670 return (dst);
1671}
1672
1673
5eb9da71
MS
1674#ifdef HAVE_DNSSD
1675/*
1676 * 'resolve_callback()' - Build a device URI for the given service name.
1677 */
1678
6d2f911b 1679static void DNSSD_API
5eb9da71
MS
1680resolve_callback(
1681 DNSServiceRef sdRef, /* I - Service reference */
1682 DNSServiceFlags flags, /* I - Results flags */
1683 uint32_t interfaceIndex, /* I - Interface number */
1684 DNSServiceErrorType errorCode, /* I - Error, if any */
1685 const char *fullName, /* I - Full service name */
1686 const char *hostTarget, /* I - Hostname */
1687 uint16_t port, /* I - Port number */
1688 uint16_t txtLen, /* I - Length of TXT record */
1689 const unsigned char *txtRecord, /* I - TXT record data */
1690 void *context) /* I - Pointer to URI buffer */
1691{
1692 const char *scheme; /* URI scheme */
1693 char rp[257]; /* Remote printer */
1694 const void *value; /* Value from TXT record */
1695 uint8_t valueLen; /* Length of value */
1696 _http_uribuf_t *uribuf; /* URI buffer */
1697
1698
e07d4801 1699 DEBUG_printf(("7resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
5eb9da71 1700 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
e07d4801 1701 "txtLen=%u, txtRecord=%p, context=%p)", sdRef, flags,
5eb9da71
MS
1702 interfaceIndex, errorCode, fullName, hostTarget, port, txtLen,
1703 txtRecord, context));
1704
1705 /*
1706 * Figure out the scheme from the full name...
1707 */
1708
f11a948a 1709 if (strstr(fullName, "._ipp") || strstr(fullName, "._fax-ipp"))
5eb9da71
MS
1710 scheme = "ipp";
1711 else if (strstr(fullName, "._printer."))
1712 scheme = "lpd";
1713 else if (strstr(fullName, "._pdl-datastream."))
1714 scheme = "socket";
1715 else
1716 scheme = "riousbprint";
1717
1718 /*
1719 * Extract the "remote printer" key from the TXT record...
1720 */
1721
1722 if ((value = TXTRecordGetValuePtr(txtLen, txtRecord, "rp",
1723 &valueLen)) != NULL)
1724 {
1725 /*
1726 * Convert to resource by concatenating with a leading "/"...
1727 */
1728
1729 rp[0] = '/';
1730 memcpy(rp + 1, value, valueLen);
1731 rp[valueLen + 1] = '\0';
1732 }
1733 else
1734 rp[0] = '\0';
1735
1736 /*
1737 * Assemble the final device URI...
1738 */
1739
1740 uribuf = (_http_uribuf_t *)context;
1741
1742 httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme,
1743 NULL, hostTarget, ntohs(port), rp);
1744
e07d4801 1745 DEBUG_printf(("8resolve_callback: Resolved URI is \"%s\"...",
5eb9da71
MS
1746 uribuf->buffer));
1747}
1748#endif /* HAVE_DNSSD */
1749
1750
ef416fc2 1751/*
b19ccc9e 1752 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".
ef416fc2 1753 */