2 * "$Id: http-support.c 4903 2006-01-10 20:02:46Z mike $"
4 * HTTP support routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * httpAssembleURI() - Assemble a uniform resource identifier from its
30 * httpAssembleURIf() - Assemble a uniform resource identifier from its
31 * components with a formatted resource.
32 * httpDecode64() - Base64-decode a string.
33 * httpDecode64_2() - Base64-decode a string.
34 * httpEncode64() - Base64-encode a string.
35 * httpEncode64_2() - Base64-encode a string.
36 * httpGetDateString() - Get a formatted date/time string from a time value.
37 * httpGetDateString2() - Get a formatted date/time string from a time value.
38 * httpGetDateTime() - Get a time value from a formatted date/time string.
39 * httpSeparate() - Separate a Universal Resource Identifier into its
41 * httpSeparate2() - Separate a Universal Resource Identifier into its
43 * httpSeparateURI() - Separate a Universal Resource Identifier into its
45 * httpStatus() - Return a short string describing a HTTP status code.
46 * _cups_hstrerror() - hstrerror() emulation function for Solaris and
48 * http_copy_decode() - Copy and decode a URI.
49 * http_copy_encode() - Copy and encode a URI.
53 * Include necessary headers...
65 static const char * const http_days
[7] =
74 static const char * const http_months
[12] =
95 static const char *http_copy_decode(char *dst
, const char *src
,
96 int dstsize
, const char *term
);
97 static char *http_copy_encode(char *dst
, const char *src
,
98 char *dstend
, const char *reserved
);
102 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
105 * This function properly escapes all reserved characters in a URI. You
106 * should use this function in place of traditional string functions
107 * whenever you need to create a URI string.
112 http_uri_status_t
/* O - URI status */
113 httpAssembleURI(char *uri
, /* I - URI buffer */
114 int urilen
, /* I - Size of URI buffer */
115 const char *scheme
, /* I - Scheme name */
116 const char *username
, /* I - Username */
117 const char *host
, /* I - Hostname or address */
118 int port
, /* I - Port number */
119 const char *resource
) /* I - Resource */
121 return (httpAssembleURIf(uri
, urilen
, scheme
, username
, host
, port
, "%s",
127 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
128 * components with a formatted resource.
130 * This function creates a formatted version of the resource string
131 * argument "resourcef" and properly escapes all reserved characters
132 * in a URI. You should use this function in place of traditional
133 * string functions whenever you need to create a URI string.
138 http_uri_status_t
/* O - URI status */
139 httpAssembleURIf(char *uri
, /* I - URI buffer */
140 int urilen
, /* I - Size of URI buffer */
141 const char *scheme
, /* I - Scheme name */
142 const char *username
, /* I - Username */
143 const char *host
, /* I - Hostname or address */
144 int port
, /* I - Port number */
145 const char *resourcef
, /* I - Printf-style resource */
146 ...) /* I - Additional arguments as needed */
148 char *ptr
, /* Pointer into URI buffer */
149 *end
; /* End of URI buffer */
150 va_list ap
; /* Pointer to additional arguments */
151 char resource
[1024]; /* Formatted resource string */
152 int bytes
; /* Bytes in formatted string */
156 * Range check input...
159 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
164 return (HTTP_URI_BAD_ARGUMENTS
);
168 * Assemble the URI starting with the scheme...
171 end
= uri
+ urilen
- 1;
172 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
);
175 goto assemble_overflow
;
177 if (!strcmp(scheme
, "mailto:"))
180 * mailto: only has :, no //...
186 goto assemble_overflow
;
191 * Schemes other than mailto: all have //...
201 goto assemble_overflow
;
205 * Next the username and hostname, if any...
210 if (username
&& *username
)
213 * Add username@ first...
216 ptr
= http_copy_encode(ptr
, username
, end
, "/?@");
219 goto assemble_overflow
;
224 goto assemble_overflow
;
228 * Then add the hostname. Since IPv6 is a particular pain to deal
229 * with, we have several special cases to deal with... If we get
230 * an IPv6 address with brackets around it, assume it is already in
234 if (host
[0] != '[' && strchr(host
, ':'))
237 * We have an IPv6 address...
240 if (strchr(host
, '%'))
243 * We have a link-local address, add "[v1." prefix...
254 goto assemble_overflow
;
259 * We have a normal address, add "[" prefix...
265 goto assemble_overflow
;
269 * Copy the rest of the IPv6 address, and terminate with "]".
272 while (ptr
< end
&& *host
)
276 *ptr
++ = '+'; /* Convert zone separator */
284 goto assemble_overflow
;
289 goto assemble_overflow
;
294 * Otherwise, just copy the host string...
297 ptr
= http_copy_encode(ptr
, host
, end
, NULL
);
300 goto assemble_overflow
;
304 * Finish things off with the port number...
309 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
313 goto assemble_overflow
;
318 * Last but not least, add the resource string...
323 va_start(ap
, resourcef
);
324 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
327 if (bytes
>= sizeof(resource
))
328 goto assemble_overflow
;
330 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
);
332 goto assemble_overflow
;
337 goto assemble_overflow
;
340 * Nul-terminate the URI buffer and return with no errors...
345 return (HTTP_URI_OK
);
348 * Clear the URI string and return an overflow error; I don't usually
349 * like goto's, but in this case it makes sense...
355 return (HTTP_URI_OVERFLOW
);
360 * 'httpDecode64()' - Base64-decode a string.
363 char * /* O - Decoded string */
364 httpDecode64(char *out
, /* I - String to write to */
365 const char *in
) /* I - String to read from */
367 int outlen
; /* Output buffer length */
371 * Use the old maximum buffer size for binary compatibility...
376 return (httpDecode64_2(out
, &outlen
, in
));
381 * 'httpDecode64_2()' - Base64-decode a string.
383 * @since CUPS 1.1.21@
386 char * /* O - Decoded string */
387 httpDecode64_2(char *out
, /* I - String to write to */
388 int *outlen
, /* IO - Size of output string */
389 const char *in
) /* I - String to read from */
391 int pos
, /* Bit position */
392 base64
; /* Value of this character */
393 char *outptr
, /* Output pointer */
394 *outend
; /* End of output buffer */
398 * Range check input...
401 if (!out
|| !outlen
|| *outlen
< 1 || !in
|| !*in
)
405 * Convert from base-64 to bytes...
408 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
411 * Decode this character into a number from 0 to 63...
414 if (*in
>= 'A' && *in
<= 'Z')
416 else if (*in
>= 'a' && *in
<= 'z')
417 base64
= *in
- 'a' + 26;
418 else if (*in
>= '0' && *in
<= '9')
419 base64
= *in
- '0' + 52;
430 * Store the result in the appropriate chars...
437 *outptr
= base64
<< 2;
442 *outptr
++ |= (base64
>> 4) & 3;
444 *outptr
= (base64
<< 4) & 255;
449 *outptr
++ |= (base64
>> 2) & 15;
451 *outptr
= (base64
<< 6) & 255;
465 * Return the decoded string and size...
468 *outlen
= (int)(outptr
- out
);
475 * 'httpEncode64()' - Base64-encode a string.
478 char * /* O - Encoded string */
479 httpEncode64(char *out
, /* I - String to write to */
480 const char *in
) /* I - String to read from */
482 return (httpEncode64_2(out
, 512, in
, strlen(in
)));
487 * 'httpEncode64_2()' - Base64-encode a string.
489 * @since CUPS 1.1.21@
492 char * /* O - Encoded string */
493 httpEncode64_2(char *out
, /* I - String to write to */
494 int outlen
, /* I - Size of output string */
495 const char *in
, /* I - String to read from */
496 int inlen
) /* I - Size of input string */
498 char *outptr
, /* Output pointer */
499 *outend
; /* End of output buffer */
500 static const char base64
[] = /* Base64 characters... */
502 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
503 "abcdefghijklmnopqrstuvwxyz"
510 * Range check input...
513 if (!out
|| outlen
< 1 || !in
)
517 * Convert bytes to base-64...
520 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
523 * Encode the up to 3 characters as 4 Base64 numbers...
527 *outptr
++ = base64
[(in
[0] & 255) >> 2];
529 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
543 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
555 *outptr
++ = base64
[in
[0] & 63];
561 * Return the encoded string...
569 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
574 const char * /* O - Date/time string */
575 httpGetDateString(time_t t
) /* I - UNIX time */
577 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
580 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
585 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
590 const char * /* O - Date/time string */
591 httpGetDateString2(time_t t
, /* I - UNIX time */
592 char *s
, /* I - String buffer */
593 int slen
) /* I - Size of string buffer */
595 struct tm
*tdate
; /* UNIX date/time data */
599 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
600 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
601 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
602 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
609 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
612 time_t /* O - UNIX time */
613 httpGetDateTime(const char *s
) /* I - Date/time string */
615 int i
; /* Looping var */
616 char mon
[16]; /* Abbreviated month name */
617 int day
, year
; /* Day of month and year */
618 int hour
, min
, sec
; /* Time */
619 int days
; /* Number of days since 1970 */
620 static const int normal_days
[] = /* Days to a month, normal years */
621 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
622 static const int leap_days
[] = /* Days to a month, leap years */
623 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
626 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
629 * Extract the date and time from the formatted string...
632 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
635 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
636 day
, mon
, year
, hour
, min
, sec
));
639 * Convert the month name to a number from 0 to 11.
642 for (i
= 0; i
< 12; i
++)
643 if (!strcasecmp(mon
, http_months
[i
]))
649 DEBUG_printf((" i=%d\n", i
));
652 * Now convert the date and time to a UNIX time value in seconds since
653 * 1970. We can't use mktime() since the timezone may not be UTC but
654 * the date/time string *is* UTC.
657 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
658 days
= leap_days
[i
] + day
- 1;
660 days
= normal_days
[i
] + day
- 1;
662 DEBUG_printf((" days=%d\n", days
));
664 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
665 ((year
- 1) / 4 - 492) - /* + leap days */
666 ((year
- 1) / 100 - 19) + /* - 100 year days */
667 ((year
- 1) / 400 - 4); /* + 400 year days */
669 DEBUG_printf((" days=%d\n", days
));
671 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
676 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
681 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
682 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
683 char *username
, /* O - Username [1024] */
684 char *host
, /* O - Hostname [1024] */
685 int *port
, /* O - Port number to use */
686 char *resource
) /* O - Resource/filename [1024] */
688 httpSeparateURI(uri
, scheme
, 32, username
, HTTP_MAX_URI
, host
, HTTP_MAX_URI
,
689 port
, resource
, HTTP_MAX_URI
);
694 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
697 * @since CUPS 1.1.21@
701 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
702 char *scheme
, /* O - Scheme (http, https, etc.) */
703 int schemelen
, /* I - Size of scheme buffer */
704 char *username
, /* O - Username */
705 int usernamelen
, /* I - Size of username buffer */
706 char *host
, /* O - Hostname */
707 int hostlen
, /* I - Size of hostname buffer */
708 int *port
, /* O - Port number to use */
709 char *resource
, /* O - Resource/filename */
710 int resourcelen
) /* I - Size of resource buffer */
712 httpSeparateURI(uri
, scheme
, schemelen
, username
, usernamelen
, host
, hostlen
,
713 port
, resource
, resourcelen
);
718 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
724 http_uri_status_t
/* O - Result of separation */
725 httpSeparateURI(const char *uri
, /* I - Universal Resource Identifier */
726 char *scheme
, /* O - Scheme (http, https, etc.) */
727 int schemelen
, /* I - Size of scheme buffer */
728 char *username
, /* O - Username */
729 int usernamelen
, /* I - Size of username buffer */
730 char *host
, /* O - Hostname */
731 int hostlen
, /* I - Size of hostname buffer */
732 int *port
, /* O - Port number to use */
733 char *resource
, /* O - Resource/filename */
734 int resourcelen
) /* I - Size of resource buffer */
736 char *ptr
, /* Pointer into string... */
737 *end
; /* End of string */
738 const char *sep
; /* Separator character */
739 http_uri_status_t status
; /* Result of separation */
743 * Initialize everything to blank...
746 if (scheme
&& schemelen
> 0)
749 if (username
&& usernamelen
> 0)
752 if (host
&& hostlen
> 0)
758 if (resource
&& resourcelen
> 0)
762 * Range check input...
765 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
766 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
768 return (HTTP_URI_BAD_ARGUMENTS
);
771 return (HTTP_URI_BAD_URI
);
774 * Grab the scheme portion of the URI...
777 status
= HTTP_URI_OK
;
779 if (!strncmp(uri
, "//", 2))
782 * Workaround for HP IPP client bug...
785 strlcpy(scheme
, "ipp", schemelen
);
786 status
= HTTP_URI_MISSING_SCHEME
;
788 else if (*uri
== '/')
794 strlcpy(scheme
, "file", schemelen
);
795 status
= HTTP_URI_MISSING_SCHEME
;
800 * Standard URI with scheme...
803 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
804 *uri
&& *uri
!= ':' && ptr
< end
;)
805 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
815 return (HTTP_URI_BAD_SCHEME
);
822 * Set the default port number...
825 if (!strcmp(scheme
, "http"))
827 else if (!strcmp(scheme
, "https"))
829 else if (!strcmp(scheme
, "ipp"))
831 else if (!strcasecmp(scheme
, "lpd"))
833 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
835 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
836 status
= HTTP_URI_UNKNOWN_SCHEME
;
839 * Now see if we have a hostname...
842 if (!strncmp(uri
, "//", 2))
851 * Grab the username, if any...
854 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
857 * Get a username:password combo...
860 uri
= http_copy_decode(username
, uri
, usernamelen
, "@");
865 return (HTTP_URI_BAD_USERNAME
);
872 * Then the hostname/IP address...
878 * Grab IPv6 address...
882 if (!strncmp(uri
, "v1.", 3))
883 uri
+= 3; /* Skip IPvN leader... */
885 uri
= http_copy_decode(host
, uri
, hostlen
, "]");
890 return (HTTP_URI_BAD_HOSTNAME
);
900 return (HTTP_URI_BAD_HOSTNAME
);
905 for (ptr
= host
; *ptr
; ptr
++)
909 * Convert zone separator to % and stop here...
915 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
918 return (HTTP_URI_BAD_HOSTNAME
);
924 * Grab hostname or IPv4 address...
927 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/");
932 return (HTTP_URI_BAD_HOSTNAME
);
939 for (ptr
= host
; *ptr
; ptr
++)
940 if (!strchr("abcdefghijklmnopqrstuvwxyz"
941 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
944 "!$&'()*+,;=", *ptr
))
947 return (HTTP_URI_BAD_HOSTNAME
);
952 * Validate hostname for file scheme - only empty and localhost are
956 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
959 return (HTTP_URI_BAD_HOSTNAME
);
963 * See if we have a port number...
969 * Yes, collect the port number...
972 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
977 return (HTTP_URI_BAD_PORT
);
983 * The remaining portion is the resource string...
986 if (*uri
== '?' || !*uri
)
989 * Hostname but no path...
992 status
= HTTP_URI_MISSING_RESOURCE
;
994 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, "");
997 uri
= http_copy_decode(resource
, uri
, resourcelen
, "");
1002 return (HTTP_URI_BAD_RESOURCE
);
1006 * Return the URI separation status...
1014 * 'httpStatus()' - Return a short string describing a HTTP status code.
1017 const char * /* O - String or NULL */
1018 httpStatus(http_status_t status
) /* I - HTTP status code */
1022 case HTTP_CONTINUE
:
1023 return ("Continue");
1024 case HTTP_SWITCHING_PROTOCOLS
:
1025 return ("Switching Protocols");
1030 case HTTP_ACCEPTED
:
1031 return ("Accepted");
1032 case HTTP_NO_CONTENT
:
1033 return ("No Content");
1034 case HTTP_NOT_MODIFIED
:
1035 return ("Not Modified");
1036 case HTTP_BAD_REQUEST
:
1037 return ("Bad Request");
1038 case HTTP_UNAUTHORIZED
:
1039 return ("Unauthorized");
1040 case HTTP_FORBIDDEN
:
1041 return ("Forbidden");
1042 case HTTP_NOT_FOUND
:
1043 return ("Not Found");
1044 case HTTP_REQUEST_TOO_LARGE
:
1045 return ("Request Entity Too Large");
1046 case HTTP_URI_TOO_LONG
:
1047 return ("URI Too Long");
1048 case HTTP_UPGRADE_REQUIRED
:
1049 return ("Upgrade Required");
1050 case HTTP_NOT_IMPLEMENTED
:
1051 return ("Not Implemented");
1052 case HTTP_NOT_SUPPORTED
:
1053 return ("Not Supported");
1060 #ifndef HAVE_HSTRERROR
1062 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1065 const char * /* O - Error string */
1066 _cups_hstrerror(int error
) /* I - Error number */
1068 static const char * const errors
[] = /* Error strings */
1073 "Unrecoverable lookup error.",
1074 "No data associated with name."
1078 if (error
< 0 || error
> 4)
1079 return ("Unknown hostname lookup error.");
1081 return (errors
[error
]);
1083 #endif /* !HAVE_HSTRERROR */
1087 * 'http_copy_decode()' - Copy and decode a URI.
1090 static const char * /* O - New source pointer or NULL on error */
1091 http_copy_decode(char *dst
, /* O - Destination buffer */
1092 const char *src
, /* I - Source pointer */
1093 int dstsize
, /* I - Destination size */
1094 const char *term
) /* I - Terminating characters */
1096 char *ptr
, /* Pointer into buffer */
1097 *end
; /* End of buffer */
1098 int quoted
; /* Quoted character */
1102 * Copy the src to the destination until we hit a terminating character
1103 * or the end of the string.
1106 for (ptr
= dst
, end
= dst
+ dstsize
- 1; *src
&& !strchr(term
, *src
); src
++)
1111 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1114 * Grab a hex-encoded character...
1119 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1121 quoted
= (*src
- '0') << 4;
1125 quoted
|= tolower(*src
) - 'a' + 10;
1127 quoted
|= *src
- '0';
1134 * Bad hex-encoded character...
1152 * 'http_copy_encode()' - Copy and encode a URI.
1155 static char * /* O - End of current URI */
1156 http_copy_encode(char *dst
, /* O - Destination buffer */
1157 const char *src
, /* I - Source pointer */
1158 char *dstend
, /* I - End of destination buffer */
1159 const char *reserved
) /* I - Extra reserved characters */
1161 static const char *hex
= "0123456789ABCDEF";
1164 while (*src
&& dst
< dstend
)
1166 if (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1167 (reserved
&& strchr(reserved
, *src
)))
1170 * Hex encode reserved characters...
1173 if ((dst
+ 2) >= dstend
)
1177 *dst
++ = hex
[(*src
>> 4) & 15];
1178 *dst
++ = hex
[*src
& 15];
1194 * End of "$Id: http-support.c 4903 2006-01-10 20:02:46Z mike $".