]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-support.c
Merge changes from CUPS 1.5svn-r9105.
[thirdparty/cups.git] / cups / http-support.c
1 /*
2 * "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $"
3 *
4 * HTTP support routines for CUPS.
5 *
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
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/".
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...
39 * _httpEncodeURI() - Percent-encode a HTTP request URI.
40 * _httpResolveURI() - Resolve a DNS-SD URI.
41 * http_copy_decode() - Copy and decode a URI.
42 * http_copy_encode() - Copy and encode a URI.
43 * resolve_callback() - Build a device URI for the given service name.
44 */
45
46 /*
47 * Include necessary headers...
48 */
49
50 #include "cups-private.h"
51 #ifdef HAVE_DNSSD
52 # include <dns_sd.h>
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 */
60 #endif /* HAVE_DNSSD */
61
62
63 /*
64 * Local types...
65 */
66
67 typedef struct _http_uribuf_s /* URI buffer */
68 {
69 char *buffer; /* Pointer to buffer */
70 size_t bufsize; /* Size of buffer */
71 } _http_uribuf_t;
72
73
74 /*
75 * Local globals...
76 */
77
78 static const char * const http_days[7] =
79 {
80 "Sun",
81 "Mon",
82 "Tue",
83 "Wed",
84 "Thu",
85 "Fri",
86 "Sat"
87 };
88 static 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
109 static const char *http_copy_decode(char *dst, const char *src,
110 int dstsize, const char *term,
111 int decode);
112 static char *http_copy_encode(char *dst, const char *src,
113 char *dstend, const char *reserved,
114 const char *term, int encode);
115 #ifdef HAVE_DNSSD
116 static void DNSSD_API resolve_callback(DNSServiceRef sdRef,
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 */
126
127
128 /*
129 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
130 * components.
131 *
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.
136 *
137 * @since CUPS 1.2/Mac OS X 10.5@
138 */
139
140 http_uri_status_t /* O - URI status */
141 httpAssembleURI(
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 */
150 {
151 char *ptr, /* Pointer into URI buffer */
152 *end; /* End of URI buffer */
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;
172 ptr = http_copy_encode(uri, scheme, end, NULL, NULL, 0);
173
174 if (!ptr)
175 goto assemble_overflow;
176
177 if (!strcmp(scheme, "mailto"))
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
216 ptr = http_copy_encode(ptr, username, end, "/?@", NULL,
217 encoding & HTTP_URI_CODING_USERNAME);
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
230 * with, we have several special cases to deal with. If we get
231 * an IPv6 address with brackets around it, assume it is already in
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...
235 */
236
237 if (host[0] != '[' && strchr(host, ':') && !strstr(host, "._tcp"))
238 {
239 /*
240 * We have a raw IPv6 address...
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
300 ptr = http_copy_encode(ptr, host, end, ":/?#[]@\\", NULL,
301 encoding & HTTP_URI_CODING_HOSTNAME);
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
325 if (resource)
326 {
327 char *query; /* Pointer to query string */
328
329
330 /*
331 * Copy the resource string up to the query string if present...
332 */
333
334 query = strchr(resource, '?');
335 ptr = http_copy_encode(ptr, resource, end, NULL, "?",
336 encoding & HTTP_URI_CODING_RESOURCE);
337 if (!ptr)
338 goto assemble_overflow;
339
340 if (query)
341 {
342 /*
343 * Copy query string without encoding...
344 */
345
346 ptr = http_copy_encode(ptr, query, end, NULL, NULL,
347 encoding & HTTP_URI_CODING_QUERY);
348 if (!ptr)
349 goto assemble_overflow;
350 }
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
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
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.
386 *
387 * @since CUPS 1.2/Mac OS X 10.5@
388 */
389
390 http_uri_status_t /* O - URI status */
391 httpAssembleURIf(
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
438 /*
439 * 'httpDecode64()' - Base64-decode a string.
440 *
441 * This function is deprecated. Use the httpDecode64_2() function instead
442 * which provides buffer length arguments.
443 *
444 * @deprecated@
445 */
446
447 char * /* O - Decoded string */
448 httpDecode64(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 *
467 * @since CUPS 1.1.21/Mac OS X 10.4@
468 */
469
470 char * /* O - Decoded string */
471 httpDecode64_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
485 if (!out || !outlen || *outlen < 1 || !in)
486 return (NULL);
487
488 if (!*in)
489 {
490 *out = '\0';
491 *outlen = 0;
492
493 return (out);
494 }
495
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.
568 *
569 * This function is deprecated. Use the httpEncode64_2() function instead
570 * which provides buffer length arguments.
571 *
572 * @deprecated@
573 */
574
575 char * /* O - Encoded string */
576 httpEncode64(char *out, /* I - String to write to */
577 const char *in) /* I - String to read from */
578 {
579 return (httpEncode64_2(out, 512, in, (int)strlen(in)));
580 }
581
582
583 /*
584 * 'httpEncode64_2()' - Base64-encode a string.
585 *
586 * @since CUPS 1.1.21/Mac OS X 10.4@
587 */
588
589 char * /* O - Encoded string */
590 httpEncode64_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];
625
626 if (outptr < outend)
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 }
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)
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 }
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
682 const char * /* O - Date/time string */
683 httpGetDateString(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 *
695 * @since CUPS 1.2/Mac OS X 10.5@
696 */
697
698 const char * /* O - Date/time string */
699 httpGetDateString2(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
720 time_t /* O - UNIX time */
721 httpGetDateTime(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
734 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s));
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
743 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
744 "min=%d, sec=%d", day, mon, year, hour, min, sec));
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
757 DEBUG_printf(("4httpGetDateTime: i=%d", i));
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
770 DEBUG_printf(("4httpGetDateTime: days=%d", days));
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
777 DEBUG_printf(("4httpGetDateTime: days=%d\n", days));
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.
786 *
787 * This function is deprecated; use the httpSeparateURI() function instead.
788 *
789 * @deprecated@
790 */
791
792 void
793 httpSeparate(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 {
800 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, 32, username,
801 HTTP_MAX_URI, host, HTTP_MAX_URI, port, resource,
802 HTTP_MAX_URI);
803 }
804
805
806 /*
807 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
808 * components.
809 *
810 * This function is deprecated; use the httpSeparateURI() function instead.
811 *
812 * @since CUPS 1.1.21/Mac OS X 10.4@
813 * @deprecated@
814 */
815
816 void
817 httpSeparate2(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 {
828 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, schemelen, username,
829 usernamelen, host, hostlen, port, resource, resourcelen);
830 }
831
832
833 /*
834 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
835 * components.
836 *
837 * @since CUPS 1.2/Mac OS X 10.5@
838 */
839
840 http_uri_status_t /* O - Result of separation */
841 httpSeparateURI(
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 */
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;)
923 if (isalnum(*uri & 255) || *uri == '-' || *uri == '+' || *uri == '.')
924 *ptr++ = *uri++;
925 else
926 break;
927
928 *ptr = '\0';
929
930 if (*uri != ':')
931 {
932 *scheme = '\0';
933 return (HTTP_URI_BAD_SCHEME);
934 }
935
936 uri ++;
937 }
938
939 /*
940 * Set the default port number...
941 */
942
943 if (!strcmp(scheme, "http"))
944 *port = 80;
945 else if (!strcmp(scheme, "https"))
946 *port = 443;
947 else if (!strcmp(scheme, "ipp"))
948 *port = 631;
949 else if (!strcasecmp(scheme, "lpd"))
950 *port = 515;
951 else if (!strcmp(scheme, "socket")) /* Not yet registered with IANA... */
952 *port = 9100;
953 else if (strcmp(scheme, "file") && strcmp(scheme, "mailto"))
954 status = HTTP_URI_UNKNOWN_SCHEME;
955
956 /*
957 * Now see if we have a hostname...
958 */
959
960 if (!strncmp(uri, "//", 2))
961 {
962 /*
963 * Yes, extract it...
964 */
965
966 uri += 2;
967
968 /*
969 * Grab the username, if any...
970 */
971
972 if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@')
973 {
974 /*
975 * Get a username:password combo...
976 */
977
978 uri = http_copy_decode(username, uri, usernamelen, "@",
979 decoding & HTTP_URI_CODING_USERNAME);
980
981 if (!uri)
982 {
983 *username = '\0';
984 return (HTTP_URI_BAD_USERNAME);
985 }
986
987 uri ++;
988 }
989
990 /*
991 * Then the hostname/IP address...
992 */
993
994 if (*uri == '[')
995 {
996 /*
997 * Grab IPv6 address...
998 */
999
1000 uri ++;
1001 if (!strncmp(uri, "v1.", 3))
1002 uri += 3; /* Skip IPvN leader... */
1003
1004 uri = http_copy_decode(host, uri, hostlen, "]",
1005 decoding & HTTP_URI_CODING_HOSTNAME);
1006
1007 if (!uri)
1008 {
1009 *host = '\0';
1010 return (HTTP_URI_BAD_HOSTNAME);
1011 }
1012
1013 /*
1014 * Validate value...
1015 */
1016
1017 if (*uri != ']')
1018 {
1019 *host = '\0';
1020 return (HTTP_URI_BAD_HOSTNAME);
1021 }
1022
1023 uri ++;
1024
1025 for (ptr = host; *ptr; ptr ++)
1026 if (*ptr == '+')
1027 {
1028 /*
1029 * Convert zone separator to % and stop here...
1030 */
1031
1032 *ptr = '%';
1033 break;
1034 }
1035 else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255))
1036 {
1037 *host = '\0';
1038 return (HTTP_URI_BAD_HOSTNAME);
1039 }
1040 }
1041 else
1042 {
1043 /*
1044 * Validate the hostname or IPv4 address first...
1045 */
1046
1047 for (ptr = (char *)uri; *ptr; ptr ++)
1048 if (strchr(":?/", *ptr))
1049 break;
1050 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1051 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1052 "0123456789"
1053 "-._~"
1054 "%"
1055 "!$&'()*+,;=\\", *ptr))
1056 {
1057 *host = '\0';
1058 return (HTTP_URI_BAD_HOSTNAME);
1059 }
1060
1061 /*
1062 * Then copy the hostname or IPv4 address to the buffer...
1063 */
1064
1065 uri = http_copy_decode(host, uri, hostlen, ":?/",
1066 decoding & HTTP_URI_CODING_HOSTNAME);
1067
1068 if (!uri)
1069 {
1070 *host = '\0';
1071 return (HTTP_URI_BAD_HOSTNAME);
1072 }
1073 }
1074
1075 /*
1076 * Validate hostname for file scheme - only empty and localhost are
1077 * acceptable.
1078 */
1079
1080 if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0])
1081 {
1082 *host = '\0';
1083 return (HTTP_URI_BAD_HOSTNAME);
1084 }
1085
1086 /*
1087 * See if we have a port number...
1088 */
1089
1090 if (*uri == ':')
1091 {
1092 /*
1093 * Yes, collect the port number...
1094 */
1095
1096 if (!isdigit(uri[1] & 255))
1097 {
1098 *port = 0;
1099 return (HTTP_URI_BAD_PORT);
1100 }
1101
1102 *port = strtol(uri + 1, (char **)&uri, 10);
1103
1104 if (*uri != '/' && *uri)
1105 {
1106 *port = 0;
1107 return (HTTP_URI_BAD_PORT);
1108 }
1109 }
1110 }
1111
1112 /*
1113 * The remaining portion is the resource string...
1114 */
1115
1116 if (*uri == '?' || !*uri)
1117 {
1118 /*
1119 * Hostname but no path...
1120 */
1121
1122 status = HTTP_URI_MISSING_RESOURCE;
1123 *resource = '/';
1124
1125 /*
1126 * Copy any query string...
1127 */
1128
1129 if (*uri == '?')
1130 uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL,
1131 decoding & HTTP_URI_CODING_QUERY);
1132 else
1133 resource[1] = '\0';
1134 }
1135 else
1136 {
1137 uri = http_copy_decode(resource, uri, resourcelen, "?",
1138 decoding & HTTP_URI_CODING_RESOURCE);
1139
1140 if (uri && *uri == '?')
1141 {
1142 /*
1143 * Concatenate any query string...
1144 */
1145
1146 char *resptr = resource + strlen(resource);
1147
1148 uri = http_copy_decode(resptr, uri, resourcelen - (int)(resptr - resource),
1149 NULL, decoding & HTTP_URI_CODING_QUERY);
1150 }
1151 }
1152
1153 if (!uri)
1154 {
1155 *resource = '\0';
1156 return (HTTP_URI_BAD_RESOURCE);
1157 }
1158
1159 /*
1160 * Return the URI separation status...
1161 */
1162
1163 return (status);
1164 }
1165
1166
1167 /*
1168 * 'httpStatus()' - Return a short string describing a HTTP status code.
1169 *
1170 * The returned string is localized to the current POSIX locale and is based
1171 * on the status strings defined in RFC 2616.
1172 */
1173
1174 const char * /* O - Localized status string */
1175 httpStatus(http_status_t status) /* I - HTTP status code */
1176 {
1177 const char *s; /* Status string */
1178 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1179
1180
1181 if (!cg->lang_default)
1182 cg->lang_default = cupsLangDefault();
1183
1184 switch (status)
1185 {
1186 case HTTP_CONTINUE :
1187 s = _("Continue");
1188 break;
1189 case HTTP_SWITCHING_PROTOCOLS :
1190 s = _("Switching Protocols");
1191 break;
1192 case HTTP_OK :
1193 s = _("OK");
1194 break;
1195 case HTTP_CREATED :
1196 s = _("Created");
1197 break;
1198 case HTTP_ACCEPTED :
1199 s = _("Accepted");
1200 break;
1201 case HTTP_NO_CONTENT :
1202 s = _("No Content");
1203 break;
1204 case HTTP_MOVED_PERMANENTLY :
1205 s = _("Moved Permanently");
1206 break;
1207 case HTTP_SEE_OTHER :
1208 s = _("See Other");
1209 break;
1210 case HTTP_NOT_MODIFIED :
1211 s = _("Not Modified");
1212 break;
1213 case HTTP_BAD_REQUEST :
1214 s = _("Bad Request");
1215 break;
1216 case HTTP_UNAUTHORIZED :
1217 case HTTP_AUTHORIZATION_CANCELED :
1218 s = _("Unauthorized");
1219 break;
1220 case HTTP_FORBIDDEN :
1221 s = _("Forbidden");
1222 break;
1223 case HTTP_NOT_FOUND :
1224 s = _("Not Found");
1225 break;
1226 case HTTP_REQUEST_TOO_LARGE :
1227 s = _("Request Entity Too Large");
1228 break;
1229 case HTTP_URI_TOO_LONG :
1230 s = _("URI Too Long");
1231 break;
1232 case HTTP_UPGRADE_REQUIRED :
1233 s = _("Upgrade Required");
1234 break;
1235 case HTTP_NOT_IMPLEMENTED :
1236 s = _("Not Implemented");
1237 break;
1238 case HTTP_NOT_SUPPORTED :
1239 s = _("Not Supported");
1240 break;
1241 case HTTP_EXPECTATION_FAILED :
1242 s = _("Expectation Failed");
1243 break;
1244 case HTTP_SERVICE_UNAVAILABLE :
1245 s = _("Service Unavailable");
1246 break;
1247 case HTTP_SERVER_ERROR :
1248 s = _("Internal Server Error");
1249 break;
1250
1251 default :
1252 s = _("Unknown");
1253 break;
1254 }
1255
1256 return (_cupsLangString(cg->lang_default, s));
1257 }
1258
1259
1260 #ifndef HAVE_HSTRERROR
1261 /*
1262 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1263 */
1264
1265 const char * /* O - Error string */
1266 _cups_hstrerror(int error) /* I - Error number */
1267 {
1268 static const char * const errors[] = /* Error strings */
1269 {
1270 "OK",
1271 "Host not found.",
1272 "Try again.",
1273 "Unrecoverable lookup error.",
1274 "No data associated with name."
1275 };
1276
1277
1278 if (error < 0 || error > 4)
1279 return ("Unknown hostname lookup error.");
1280 else
1281 return (errors[error]);
1282 }
1283 #endif /* !HAVE_HSTRERROR */
1284
1285
1286 /*
1287 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1288 */
1289
1290 char * /* O - Encoded URI */
1291 _httpEncodeURI(char *dst, /* I - Destination buffer */
1292 const char *src, /* I - Source URI */
1293 size_t dstsize) /* I - Size of destination buffer */
1294 {
1295 http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, 1);
1296 return (dst);
1297 }
1298
1299
1300 /*
1301 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1302 */
1303
1304 const char * /* O - Resolved URI */
1305 _httpResolveURI(
1306 const char *uri, /* I - DNS-SD URI */
1307 char *resolved_uri, /* I - Buffer for resolved URI */
1308 size_t resolved_size, /* I - Size of URI buffer */
1309 int logit) /* I - Log progress to stderr? */
1310 {
1311 char scheme[32], /* URI components... */
1312 userpass[256],
1313 hostname[1024],
1314 resource[1024];
1315 int port;
1316 #ifdef DEBUG
1317 http_uri_status_t status; /* URI decode status */
1318 #endif /* DEBUG */
1319
1320
1321 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1322 "resolved_size=" CUPS_LLFMT ")", uri, resolved_uri,
1323 CUPS_LLCAST resolved_size));
1324
1325 /*
1326 * Get the device URI...
1327 */
1328
1329 #ifdef DEBUG
1330 if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1331 sizeof(scheme), userpass, sizeof(userpass),
1332 hostname, sizeof(hostname), &port, resource,
1333 sizeof(resource))) < HTTP_URI_OK)
1334 #else
1335 if (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)
1339 #endif /* DEBUG */
1340 {
1341 if (logit)
1342 _cupsLangPrintf(stderr, _("Bad device URI \"%s\"\n"), uri);
1343
1344 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status));
1345 DEBUG_puts("5_httpResolveURI: Returning NULL");
1346 return (NULL);
1347 }
1348
1349 /*
1350 * Resolve it as needed...
1351 */
1352
1353 if (strstr(hostname, "._tcp"))
1354 {
1355 #ifdef HAVE_DNSSD
1356 DNSServiceRef ref, /* DNS-SD master service reference */
1357 domainref, /* DNS-SD service reference for domain */
1358 localref; /* DNS-SD service reference for .local */
1359 int domainsent = 0; /* Send the domain resolve? */
1360 char *regtype, /* Pointer to type in hostname */
1361 *domain; /* Pointer to domain in hostname */
1362 _http_uribuf_t uribuf; /* URI buffer */
1363 #ifdef HAVE_POLL
1364 struct pollfd polldata; /* Polling data */
1365 #else /* select() */
1366 fd_set input_set; /* Input set for select() */
1367 struct timeval stimeout; /* Timeout value for select() */
1368 #endif /* HAVE_POLL */
1369
1370 if (logit)
1371 fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
1372
1373 /*
1374 * Separate the hostname into service name, registration type, and domain...
1375 */
1376
1377 for (regtype = strstr(hostname, "._tcp") - 2;
1378 regtype > hostname;
1379 regtype --)
1380 if (regtype[0] == '.' && regtype[1] == '_')
1381 {
1382 /*
1383 * Found ._servicetype in front of ._tcp...
1384 */
1385
1386 *regtype++ = '\0';
1387 break;
1388 }
1389
1390 if (regtype <= hostname)
1391 {
1392 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1393 return (NULL);
1394 }
1395
1396 for (domain = strchr(regtype, '.');
1397 domain;
1398 domain = strchr(domain + 1, '.'))
1399 if (domain[1] != '_')
1400 break;
1401
1402 if (domain)
1403 *domain++ = '\0';
1404
1405 uribuf.buffer = resolved_uri;
1406 uribuf.bufsize = resolved_size;
1407
1408 resolved_uri[0] = '\0';
1409
1410 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1411 "domain=\"%s\"\n", hostname, regtype, domain));
1412 if (logit)
1413 {
1414 fputs("STATE: +connecting-to-device\n", stderr);
1415 fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1416 "domain=\"local.\"...\n", hostname, regtype);
1417 }
1418
1419 uri = NULL;
1420
1421 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
1422 {
1423 localref = ref;
1424 if (DNSServiceResolve(&localref, kDNSServiceFlagsShareConnection, 0,
1425 hostname, regtype, "local.", resolve_callback,
1426 &uribuf) == kDNSServiceErr_NoError)
1427 {
1428 int fds; /* Number of ready descriptors */
1429 time_t timeout, /* Poll timeout */
1430 start_time = time(NULL);/* Start time */
1431
1432 for (;;)
1433 {
1434 if (logit)
1435 _cupsLangPuts(stderr, _("INFO: Looking for printer...\n"));
1436
1437 /*
1438 * For the first minute, wakeup every 2 seconds to emit a
1439 * "looking for printer" message...
1440 */
1441
1442 timeout = (time(NULL) < (start_time + 60)) ? 2000 : -1;
1443
1444 #ifdef HAVE_POLL
1445 polldata.fd = DNSServiceRefSockFD(ref);
1446 polldata.events = POLLIN;
1447
1448 fds = poll(&polldata, 1, timeout);
1449
1450 #else /* select() */
1451 FD_ZERO(&input_set);
1452 FD_SET(DNSServiceRefSockFD(ref), &input_set);
1453
1454 stimeout.tv_sec = ((int)timeout) / 1000;
1455 stimeout.tv_usec = ((int)(timeout) * 1000) % 1000000;
1456
1457 fds = select(DNSServiceRefSockFD(ref)+1, &input_set, NULL, NULL,
1458 timeout < 0.0 ? NULL : &stimeout);
1459 #endif /* HAVE_POLL */
1460
1461 if (fds < 0)
1462 {
1463 if (errno != EINTR && errno != EAGAIN)
1464 {
1465 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno)));
1466 break;
1467 }
1468 }
1469 else if (fds == 0)
1470 {
1471 /*
1472 * Wait 2 seconds for a response to the local resolve; if nothing
1473 * comes in, do an additional domain resolution...
1474 */
1475
1476 if (domainsent == 0 && strcasecmp(domain, "local."))
1477 {
1478 if (logit)
1479 fprintf(stderr,
1480 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1481 "domain=\"%s\"...\n", hostname, regtype, domain);
1482
1483 domainref = ref;
1484 if (DNSServiceResolve(&domainref, kDNSServiceFlagsShareConnection, 0,
1485 hostname, regtype, domain, resolve_callback,
1486 &uribuf) == kDNSServiceErr_NoError)
1487 domainsent = 1;
1488 }
1489 }
1490 else
1491 {
1492 if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError)
1493 {
1494 uri = resolved_uri;
1495 break;
1496 }
1497 }
1498 }
1499
1500 if (domainsent)
1501 DNSServiceRefDeallocate(domainref);
1502
1503 DNSServiceRefDeallocate(localref);
1504 }
1505
1506 DNSServiceRefDeallocate(ref);
1507 }
1508
1509 if (logit)
1510 {
1511 if (uri)
1512 fprintf(stderr, "DEBUG: Resolved as \"%s\"...\n", uri);
1513 else
1514 fputs("DEBUG: Unable to resolve URI\n", stderr);
1515
1516 fputs("STATE: -connecting-to-device\n", stderr);
1517 }
1518
1519 #else
1520 /*
1521 * No DNS-SD support...
1522 */
1523
1524 uri = NULL;
1525 #endif /* HAVE_DNSSD */
1526
1527 if (logit && !uri)
1528 _cupsLangPuts(stderr, _("Unable to find printer\n"));
1529 }
1530
1531 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri));
1532
1533 return (uri);
1534 }
1535
1536
1537 /*
1538 * 'http_copy_decode()' - Copy and decode a URI.
1539 */
1540
1541 static const char * /* O - New source pointer or NULL on error */
1542 http_copy_decode(char *dst, /* O - Destination buffer */
1543 const char *src, /* I - Source pointer */
1544 int dstsize, /* I - Destination size */
1545 const char *term, /* I - Terminating characters */
1546 int decode) /* I - Decode %-encoded values */
1547 {
1548 char *ptr, /* Pointer into buffer */
1549 *end; /* End of buffer */
1550 int quoted; /* Quoted character */
1551
1552
1553 /*
1554 * Copy the src to the destination until we hit a terminating character
1555 * or the end of the string.
1556 */
1557
1558 for (ptr = dst, end = dst + dstsize - 1;
1559 *src && (!term || !strchr(term, *src));
1560 src ++)
1561 if (ptr < end)
1562 {
1563 if (*src == '%' && decode)
1564 {
1565 if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255))
1566 {
1567 /*
1568 * Grab a hex-encoded character...
1569 */
1570
1571 src ++;
1572 if (isalpha(*src))
1573 quoted = (tolower(*src) - 'a' + 10) << 4;
1574 else
1575 quoted = (*src - '0') << 4;
1576
1577 src ++;
1578 if (isalpha(*src))
1579 quoted |= tolower(*src) - 'a' + 10;
1580 else
1581 quoted |= *src - '0';
1582
1583 *ptr++ = quoted;
1584 }
1585 else
1586 {
1587 /*
1588 * Bad hex-encoded character...
1589 */
1590
1591 *ptr = '\0';
1592 return (NULL);
1593 }
1594 }
1595 else
1596 *ptr++ = *src;
1597 }
1598
1599 *ptr = '\0';
1600
1601 return (src);
1602 }
1603
1604
1605 /*
1606 * 'http_copy_encode()' - Copy and encode a URI.
1607 */
1608
1609 static char * /* O - End of current URI */
1610 http_copy_encode(char *dst, /* O - Destination buffer */
1611 const char *src, /* I - Source pointer */
1612 char *dstend, /* I - End of destination buffer */
1613 const char *reserved, /* I - Extra reserved characters */
1614 const char *term, /* I - Terminating characters */
1615 int encode) /* I - %-encode reserved chars? */
1616 {
1617 static const char hex[] = "0123456789ABCDEF";
1618
1619
1620 while (*src && dst < dstend)
1621 {
1622 if (term && *src == *term)
1623 return (dst);
1624
1625 if (encode && (*src == '%' || *src <= ' ' || *src & 128 ||
1626 (reserved && strchr(reserved, *src))))
1627 {
1628 /*
1629 * Hex encode reserved characters...
1630 */
1631
1632 if ((dst + 2) >= dstend)
1633 break;
1634
1635 *dst++ = '%';
1636 *dst++ = hex[(*src >> 4) & 15];
1637 *dst++ = hex[*src & 15];
1638
1639 src ++;
1640 }
1641 else
1642 *dst++ = *src++;
1643 }
1644
1645 *dst = '\0';
1646
1647 if (*src)
1648 return (NULL);
1649 else
1650 return (dst);
1651 }
1652
1653
1654 #ifdef HAVE_DNSSD
1655 /*
1656 * 'resolve_callback()' - Build a device URI for the given service name.
1657 */
1658
1659 static void DNSSD_API
1660 resolve_callback(
1661 DNSServiceRef sdRef, /* I - Service reference */
1662 DNSServiceFlags flags, /* I - Results flags */
1663 uint32_t interfaceIndex, /* I - Interface number */
1664 DNSServiceErrorType errorCode, /* I - Error, if any */
1665 const char *fullName, /* I - Full service name */
1666 const char *hostTarget, /* I - Hostname */
1667 uint16_t port, /* I - Port number */
1668 uint16_t txtLen, /* I - Length of TXT record */
1669 const unsigned char *txtRecord, /* I - TXT record data */
1670 void *context) /* I - Pointer to URI buffer */
1671 {
1672 const char *scheme; /* URI scheme */
1673 char rp[257]; /* Remote printer */
1674 const void *value; /* Value from TXT record */
1675 uint8_t valueLen; /* Length of value */
1676 _http_uribuf_t *uribuf; /* URI buffer */
1677
1678
1679 DEBUG_printf(("7resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1680 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1681 "txtLen=%u, txtRecord=%p, context=%p)", sdRef, flags,
1682 interfaceIndex, errorCode, fullName, hostTarget, port, txtLen,
1683 txtRecord, context));
1684
1685 /*
1686 * Figure out the scheme from the full name...
1687 */
1688
1689 if (strstr(fullName, "._ipp") || strstr(fullName, "._fax-ipp"))
1690 scheme = "ipp";
1691 else if (strstr(fullName, "._printer."))
1692 scheme = "lpd";
1693 else if (strstr(fullName, "._pdl-datastream."))
1694 scheme = "socket";
1695 else
1696 scheme = "riousbprint";
1697
1698 /*
1699 * Extract the "remote printer" key from the TXT record...
1700 */
1701
1702 if ((value = TXTRecordGetValuePtr(txtLen, txtRecord, "rp",
1703 &valueLen)) != NULL)
1704 {
1705 /*
1706 * Convert to resource by concatenating with a leading "/"...
1707 */
1708
1709 rp[0] = '/';
1710 memcpy(rp + 1, value, valueLen);
1711 rp[valueLen + 1] = '\0';
1712 }
1713 else
1714 rp[0] = '\0';
1715
1716 /*
1717 * Assemble the final device URI...
1718 */
1719
1720 uribuf = (_http_uribuf_t *)context;
1721
1722 httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme,
1723 NULL, hostTarget, ntohs(port), rp);
1724
1725 DEBUG_printf(("8resolve_callback: Resolved URI is \"%s\"...",
1726 uribuf->buffer));
1727 }
1728 #endif /* HAVE_DNSSD */
1729
1730
1731 /*
1732 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".
1733 */