2 * "$Id: http-support.c 6649 2007-07-11 21:46:42Z mike $"
4 * HTTP support routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * httpAssembleURI() - Assemble a uniform resource identifier from its
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
32 * httpSeparate2() - Separate a Universal Resource Identifier into its
34 * httpSeparateURI() - Separate a Universal Resource Identifier into its
36 * httpStatus() - Return a short string describing a HTTP status code.
37 * _cups_hstrerror() - hstrerror() emulation function for Solaris and
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.
45 * Include necessary headers...
57 static const char * const http_days
[7] =
67 static const char * const http_months
[12] =
88 static const char *http_copy_decode(char *dst
, const char *src
,
89 int dstsize
, const char *term
,
91 static char *http_copy_encode(char *dst
, const char *src
,
92 char *dstend
, const char *reserved
,
93 const char *term
, int encode
);
97 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
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
108 http_uri_status_t
/* O - URI status */
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 */
119 char *ptr
, /* Pointer into URI buffer */
120 *end
; /* End of URI buffer */
124 * Range check input...
127 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
132 return (HTTP_URI_BAD_ARGUMENTS
);
136 * Assemble the URI starting with the scheme...
139 end
= uri
+ urilen
- 1;
140 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
143 goto assemble_overflow
;
145 if (!strcmp(scheme
, "mailto"))
148 * mailto: only has :, no //...
154 goto assemble_overflow
;
159 * Schemes other than mailto: all have //...
169 goto assemble_overflow
;
173 * Next the username and hostname, if any...
178 if (username
&& *username
)
181 * Add username@ first...
184 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
185 encoding
& HTTP_URI_CODING_USERNAME
);
188 goto assemble_overflow
;
193 goto assemble_overflow
;
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,
205 if (host
[0] != '[' && strchr(host
, ':') && !strstr(host
, "._tcp"))
208 * We have a raw IPv6 address...
211 if (strchr(host
, '%'))
214 * We have a link-local address, add "[v1." prefix...
225 goto assemble_overflow
;
230 * We have a normal address, add "[" prefix...
236 goto assemble_overflow
;
240 * Copy the rest of the IPv6 address, and terminate with "]".
243 while (ptr
< end
&& *host
)
247 *ptr
++ = '+'; /* Convert zone separator */
255 goto assemble_overflow
;
260 goto assemble_overflow
;
265 * Otherwise, just copy the host string...
268 ptr
= http_copy_encode(ptr
, host
, end
, ":/?#[]@", NULL
,
269 encoding
& HTTP_URI_CODING_HOSTNAME
);
272 goto assemble_overflow
;
276 * Finish things off with the port number...
281 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
285 goto assemble_overflow
;
290 * Last but not least, add the resource string...
295 char *query
; /* Pointer to query string */
299 * Copy the resource string up to the query string if present...
302 query
= strchr(resource
, '?');
303 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
304 encoding
& HTTP_URI_CODING_RESOURCE
);
306 goto assemble_overflow
;
311 * Copy query string without encoding...
314 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
315 encoding
& HTTP_URI_CODING_QUERY
);
317 goto assemble_overflow
;
323 goto assemble_overflow
;
326 * Nul-terminate the URI buffer and return with no errors...
331 return (HTTP_URI_OK
);
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...
341 return (HTTP_URI_OVERFLOW
);
346 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
347 * components with a formatted resource.
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.
358 http_uri_status_t
/* O - URI status */
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 */
370 va_list ap
; /* Pointer to additional arguments */
371 char resource
[1024]; /* Formatted resource string */
372 int bytes
; /* Bytes in formatted string */
376 * Range check input...
379 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
384 return (HTTP_URI_BAD_ARGUMENTS
);
388 * Format the resource string and assemble the URI...
391 va_start(ap
, resourcef
);
392 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
395 if (bytes
>= sizeof(resource
))
398 return (HTTP_URI_OVERFLOW
);
401 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
407 * 'httpDecode64()' - Base64-decode a string.
409 * This function is deprecated. Use the httpDecode64_2() function instead
410 * which provides buffer length arguments.
415 char * /* O - Decoded string */
416 httpDecode64(char *out
, /* I - String to write to */
417 const char *in
) /* I - String to read from */
419 int outlen
; /* Output buffer length */
423 * Use the old maximum buffer size for binary compatibility...
428 return (httpDecode64_2(out
, &outlen
, in
));
433 * 'httpDecode64_2()' - Base64-decode a string.
435 * @since CUPS 1.1.21@
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 */
443 int pos
, /* Bit position */
444 base64
; /* Value of this character */
445 char *outptr
, /* Output pointer */
446 *outend
; /* End of output buffer */
450 * Range check input...
453 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
465 * Convert from base-64 to bytes...
468 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
471 * Decode this character into a number from 0 to 63...
474 if (*in
>= 'A' && *in
<= 'Z')
476 else if (*in
>= 'a' && *in
<= 'z')
477 base64
= *in
- 'a' + 26;
478 else if (*in
>= '0' && *in
<= '9')
479 base64
= *in
- '0' + 52;
490 * Store the result in the appropriate chars...
497 *outptr
= base64
<< 2;
502 *outptr
++ |= (base64
>> 4) & 3;
504 *outptr
= (base64
<< 4) & 255;
509 *outptr
++ |= (base64
>> 2) & 15;
511 *outptr
= (base64
<< 6) & 255;
525 * Return the decoded string and size...
528 *outlen
= (int)(outptr
- out
);
535 * 'httpEncode64()' - Base64-encode a string.
537 * This function is deprecated. Use the httpEncode64_2() function instead
538 * which provides buffer length arguments.
543 char * /* O - Encoded string */
544 httpEncode64(char *out
, /* I - String to write to */
545 const char *in
) /* I - String to read from */
547 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
552 * 'httpEncode64_2()' - Base64-encode a string.
554 * @since CUPS 1.1.21@
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 */
563 char *outptr
, /* Output pointer */
564 *outend
; /* End of output buffer */
565 static const char base64
[] = /* Base64 characters... */
567 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
568 "abcdefghijklmnopqrstuvwxyz"
575 * Range check input...
578 if (!out
|| outlen
< 1 || !in
)
582 * Convert bytes to base-64...
585 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
588 * Encode the up to 3 characters as 4 Base64 numbers...
592 *outptr
++ = base64
[(in
[0] & 255) >> 2];
597 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
599 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
616 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
618 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
631 *outptr
++ = base64
[in
[0] & 63];
637 * Return the encoded string...
645 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
650 const char * /* O - Date/time string */
651 httpGetDateString(time_t t
) /* I - UNIX time */
653 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
656 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
661 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
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 */
671 struct tm
*tdate
; /* UNIX date/time data */
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
);
685 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
688 time_t /* O - UNIX time */
689 httpGetDateTime(const char *s
) /* I - Date/time string */
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 };
702 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
705 * Extract the date and time from the formatted string...
708 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
711 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
712 day
, mon
, year
, hour
, min
, sec
));
715 * Convert the month name to a number from 0 to 11.
718 for (i
= 0; i
< 12; i
++)
719 if (!strcasecmp(mon
, http_months
[i
]))
725 DEBUG_printf((" i=%d\n", i
));
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.
733 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
734 days
= leap_days
[i
] + day
- 1;
736 days
= normal_days
[i
] + day
- 1;
738 DEBUG_printf((" days=%d\n", days
));
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 */
745 DEBUG_printf((" days=%d\n", days
));
747 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
752 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
755 * This function is deprecated; use the httpSeparateURI() function instead.
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] */
768 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
769 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
775 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
778 * This function is deprecated; use the httpSeparateURI() function instead.
780 * @since CUPS 1.1.21@
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 */
796 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
797 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
802 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
808 http_uri_status_t
/* O - Result of separation */
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 */
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 */
829 * Initialize everything to blank...
832 if (scheme
&& schemelen
> 0)
835 if (username
&& usernamelen
> 0)
838 if (host
&& hostlen
> 0)
844 if (resource
&& resourcelen
> 0)
848 * Range check input...
851 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
852 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
854 return (HTTP_URI_BAD_ARGUMENTS
);
857 return (HTTP_URI_BAD_URI
);
860 * Grab the scheme portion of the URI...
863 status
= HTTP_URI_OK
;
865 if (!strncmp(uri
, "//", 2))
868 * Workaround for HP IPP client bug...
871 strlcpy(scheme
, "ipp", schemelen
);
872 status
= HTTP_URI_MISSING_SCHEME
;
874 else if (*uri
== '/')
880 strlcpy(scheme
, "file", schemelen
);
881 status
= HTTP_URI_MISSING_SCHEME
;
886 * Standard URI with scheme...
889 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
890 *uri
&& *uri
!= ':' && ptr
< end
;)
891 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
901 return (HTTP_URI_BAD_SCHEME
);
908 * Set the default port number...
911 if (!strcmp(scheme
, "http"))
913 else if (!strcmp(scheme
, "https"))
915 else if (!strcmp(scheme
, "ipp"))
917 else if (!strcasecmp(scheme
, "lpd"))
919 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
921 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
922 status
= HTTP_URI_UNKNOWN_SCHEME
;
925 * Now see if we have a hostname...
928 if (!strncmp(uri
, "//", 2))
937 * Grab the username, if any...
940 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
943 * Get a username:password combo...
946 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
947 decoding
& HTTP_URI_CODING_USERNAME
);
952 return (HTTP_URI_BAD_USERNAME
);
959 * Then the hostname/IP address...
965 * Grab IPv6 address...
969 if (!strncmp(uri
, "v1.", 3))
970 uri
+= 3; /* Skip IPvN leader... */
972 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
973 decoding
& HTTP_URI_CODING_HOSTNAME
);
978 return (HTTP_URI_BAD_HOSTNAME
);
988 return (HTTP_URI_BAD_HOSTNAME
);
993 for (ptr
= host
; *ptr
; ptr
++)
997 * Convert zone separator to % and stop here...
1003 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1006 return (HTTP_URI_BAD_HOSTNAME
);
1012 * Validate the hostname or IPv4 address first...
1015 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1016 if (strchr(":?/", *ptr
))
1018 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1019 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1023 "!$&'()*+,;=\\", *ptr
))
1026 return (HTTP_URI_BAD_HOSTNAME
);
1030 * Then copy the hostname or IPv4 address to the buffer...
1033 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1034 decoding
& HTTP_URI_CODING_HOSTNAME
);
1039 return (HTTP_URI_BAD_HOSTNAME
);
1044 * Validate hostname for file scheme - only empty and localhost are
1048 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1051 return (HTTP_URI_BAD_HOSTNAME
);
1055 * See if we have a port number...
1061 * Yes, collect the port number...
1064 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1066 if (*uri
!= '/' && *uri
)
1069 return (HTTP_URI_BAD_PORT
);
1075 * The remaining portion is the resource string...
1078 if (*uri
== '?' || !*uri
)
1081 * Hostname but no path...
1084 status
= HTTP_URI_MISSING_RESOURCE
;
1088 * Copy any query string...
1092 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1093 decoding
& HTTP_URI_CODING_QUERY
);
1099 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1100 decoding
& HTTP_URI_CODING_RESOURCE
);
1102 if (uri
&& *uri
== '?')
1105 * Concatenate any query string...
1108 char *resptr
= resource
+ strlen(resource
);
1110 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1111 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1118 return (HTTP_URI_BAD_RESOURCE
);
1122 * Return the URI separation status...
1130 * 'httpStatus()' - Return a short string describing a HTTP status code.
1133 const char * /* O - String or NULL */
1134 httpStatus(http_status_t status
) /* I - HTTP status code */
1138 case HTTP_CONTINUE
:
1139 return ("Continue");
1140 case HTTP_SWITCHING_PROTOCOLS
:
1141 return ("Switching Protocols");
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");
1183 #ifndef HAVE_HSTRERROR
1185 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1188 const char * /* O - Error string */
1189 _cups_hstrerror(int error
) /* I - Error number */
1191 static const char * const errors
[] = /* Error strings */
1196 "Unrecoverable lookup error.",
1197 "No data associated with name."
1201 if (error
< 0 || error
> 4)
1202 return ("Unknown hostname lookup error.");
1204 return (errors
[error
]);
1206 #endif /* !HAVE_HSTRERROR */
1210 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
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 */
1218 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1224 * 'http_copy_decode()' - Copy and decode a URI.
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 */
1234 char *ptr
, /* Pointer into buffer */
1235 *end
; /* End of buffer */
1236 int quoted
; /* Quoted character */
1240 * Copy the src to the destination until we hit a terminating character
1241 * or the end of the string.
1244 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1245 *src
&& (!term
|| !strchr(term
, *src
));
1249 if (*src
== '%' && decode
)
1251 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1254 * Grab a hex-encoded character...
1259 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1261 quoted
= (*src
- '0') << 4;
1265 quoted
|= tolower(*src
) - 'a' + 10;
1267 quoted
|= *src
- '0';
1274 * Bad hex-encoded character...
1292 * 'http_copy_encode()' - Copy and encode a URI.
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? */
1303 static const char hex
[] = "0123456789ABCDEF";
1306 while (*src
&& dst
< dstend
)
1308 if (term
&& *src
== *term
)
1311 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1312 (reserved
&& strchr(reserved
, *src
))))
1315 * Hex encode reserved characters...
1318 if ((dst
+ 2) >= dstend
)
1322 *dst
++ = hex
[(*src
>> 4) & 15];
1323 *dst
++ = hex
[*src
& 15];
1341 * End of "$Id: http-support.c 6649 2007-07-11 21:46:42Z mike $".