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