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