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