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 * http_copy_decode() - Copy and decode a URI.
40 * http_copy_encode() - Copy and encode a URI.
44 * Include necessary headers...
56 static const char * const http_days
[7] =
65 static const char * const http_months
[12] =
86 static const char *http_copy_decode(char *dst
, const char *src
,
87 int dstsize
, const char *term
,
89 static char *http_copy_encode(char *dst
, const char *src
,
90 char *dstend
, const char *reserved
,
91 const char *term
, int encode
);
95 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
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
106 http_uri_status_t
/* O - URI status */
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 */
117 char *ptr
, /* Pointer into URI buffer */
118 *end
; /* End of URI buffer */
122 * Range check input...
125 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
130 return (HTTP_URI_BAD_ARGUMENTS
);
134 * Assemble the URI starting with the scheme...
137 end
= uri
+ urilen
- 1;
138 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
141 goto assemble_overflow
;
143 if (!strcmp(scheme
, "mailto"))
146 * mailto: only has :, no //...
152 goto assemble_overflow
;
157 * Schemes other than mailto: all have //...
167 goto assemble_overflow
;
171 * Next the username and hostname, if any...
176 if (username
&& *username
)
179 * Add username@ first...
182 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
183 encoding
& HTTP_URI_CODING_USERNAME
);
186 goto assemble_overflow
;
191 goto assemble_overflow
;
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
201 if (host
[0] != '[' && strchr(host
, ':'))
204 * We have an IPv6 address...
207 if (strchr(host
, '%'))
210 * We have a link-local address, add "[v1." prefix...
221 goto assemble_overflow
;
226 * We have a normal address, add "[" prefix...
232 goto assemble_overflow
;
236 * Copy the rest of the IPv6 address, and terminate with "]".
239 while (ptr
< end
&& *host
)
243 *ptr
++ = '+'; /* Convert zone separator */
251 goto assemble_overflow
;
256 goto assemble_overflow
;
261 * Otherwise, just copy the host string...
264 ptr
= http_copy_encode(ptr
, host
, end
, NULL
, NULL
,
265 encoding
& HTTP_URI_CODING_HOSTNAME
);
268 goto assemble_overflow
;
272 * Finish things off with the port number...
277 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
281 goto assemble_overflow
;
286 * Last but not least, add the resource string...
291 char *query
; /* Pointer to query string */
295 * Copy the resource string up to the query string if present...
298 query
= strchr(resource
, '?');
299 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
300 encoding
& HTTP_URI_CODING_RESOURCE
);
302 goto assemble_overflow
;
307 * Copy query string without encoding...
310 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
311 encoding
& HTTP_URI_CODING_QUERY
);
313 goto assemble_overflow
;
319 goto assemble_overflow
;
322 * Nul-terminate the URI buffer and return with no errors...
327 return (HTTP_URI_OK
);
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...
337 return (HTTP_URI_OVERFLOW
);
342 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
343 * components with a formatted resource.
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.
354 http_uri_status_t
/* O - URI status */
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 */
366 va_list ap
; /* Pointer to additional arguments */
367 char resource
[1024]; /* Formatted resource string */
368 int bytes
; /* Bytes in formatted string */
372 * Range check input...
375 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
380 return (HTTP_URI_BAD_ARGUMENTS
);
384 * Format the resource string and assemble the URI...
387 va_start(ap
, resourcef
);
388 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
391 if (bytes
>= sizeof(resource
))
394 return (HTTP_URI_OVERFLOW
);
397 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
403 * 'httpDecode64()' - Base64-decode a string.
405 * This function is deprecated. Use the httpDecode64_2() function instead
406 * which provides buffer length arguments.
411 char * /* O - Decoded string */
412 httpDecode64(char *out
, /* I - String to write to */
413 const char *in
) /* I - String to read from */
415 int outlen
; /* Output buffer length */
419 * Use the old maximum buffer size for binary compatibility...
424 return (httpDecode64_2(out
, &outlen
, in
));
429 * 'httpDecode64_2()' - Base64-decode a string.
431 * @since CUPS 1.1.21@
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 */
439 int pos
, /* Bit position */
440 base64
; /* Value of this character */
441 char *outptr
, /* Output pointer */
442 *outend
; /* End of output buffer */
446 * Range check input...
449 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
461 * Convert from base-64 to bytes...
464 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
467 * Decode this character into a number from 0 to 63...
470 if (*in
>= 'A' && *in
<= 'Z')
472 else if (*in
>= 'a' && *in
<= 'z')
473 base64
= *in
- 'a' + 26;
474 else if (*in
>= '0' && *in
<= '9')
475 base64
= *in
- '0' + 52;
486 * Store the result in the appropriate chars...
493 *outptr
= base64
<< 2;
498 *outptr
++ |= (base64
>> 4) & 3;
500 *outptr
= (base64
<< 4) & 255;
505 *outptr
++ |= (base64
>> 2) & 15;
507 *outptr
= (base64
<< 6) & 255;
521 * Return the decoded string and size...
524 *outlen
= (int)(outptr
- out
);
531 * 'httpEncode64()' - Base64-encode a string.
533 * This function is deprecated. Use the httpEncode64_2() function instead
534 * which provides buffer length arguments.
539 char * /* O - Encoded string */
540 httpEncode64(char *out
, /* I - String to write to */
541 const char *in
) /* I - String to read from */
543 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
548 * 'httpEncode64_2()' - Base64-encode a string.
550 * @since CUPS 1.1.21@
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 */
559 char *outptr
, /* Output pointer */
560 *outend
; /* End of output buffer */
561 static const char base64
[] = /* Base64 characters... */
563 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
564 "abcdefghijklmnopqrstuvwxyz"
571 * Range check input...
574 if (!out
|| outlen
< 1 || !in
)
578 * Convert bytes to base-64...
581 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
584 * Encode the up to 3 characters as 4 Base64 numbers...
588 *outptr
++ = base64
[(in
[0] & 255) >> 2];
593 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
595 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
612 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
614 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
627 *outptr
++ = base64
[in
[0] & 63];
633 * Return the encoded string...
641 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
646 const char * /* O - Date/time string */
647 httpGetDateString(time_t t
) /* I - UNIX time */
649 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
652 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
657 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
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 */
667 struct tm
*tdate
; /* UNIX date/time data */
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
);
681 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
684 time_t /* O - UNIX time */
685 httpGetDateTime(const char *s
) /* I - Date/time string */
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 };
698 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
701 * Extract the date and time from the formatted string...
704 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
707 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
708 day
, mon
, year
, hour
, min
, sec
));
711 * Convert the month name to a number from 0 to 11.
714 for (i
= 0; i
< 12; i
++)
715 if (!strcasecmp(mon
, http_months
[i
]))
721 DEBUG_printf((" i=%d\n", i
));
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.
729 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
730 days
= leap_days
[i
] + day
- 1;
732 days
= normal_days
[i
] + day
- 1;
734 DEBUG_printf((" days=%d\n", days
));
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 */
741 DEBUG_printf((" days=%d\n", days
));
743 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
748 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
751 * This function is deprecated; use the httpSeparateURI() function instead.
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] */
764 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
765 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
771 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
774 * This function is deprecated; use the httpSeparateURI() function instead.
776 * @since CUPS 1.1.21@
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 */
792 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
793 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
798 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
804 http_uri_status_t
/* O - Result of separation */
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 */
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 */
825 * Initialize everything to blank...
828 if (scheme
&& schemelen
> 0)
831 if (username
&& usernamelen
> 0)
834 if (host
&& hostlen
> 0)
840 if (resource
&& resourcelen
> 0)
844 * Range check input...
847 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
848 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
850 return (HTTP_URI_BAD_ARGUMENTS
);
853 return (HTTP_URI_BAD_URI
);
856 * Grab the scheme portion of the URI...
859 status
= HTTP_URI_OK
;
861 if (!strncmp(uri
, "//", 2))
864 * Workaround for HP IPP client bug...
867 strlcpy(scheme
, "ipp", schemelen
);
868 status
= HTTP_URI_MISSING_SCHEME
;
870 else if (*uri
== '/')
876 strlcpy(scheme
, "file", schemelen
);
877 status
= HTTP_URI_MISSING_SCHEME
;
882 * Standard URI with scheme...
885 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
886 *uri
&& *uri
!= ':' && ptr
< end
;)
887 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
897 return (HTTP_URI_BAD_SCHEME
);
904 * Set the default port number...
907 if (!strcmp(scheme
, "http"))
909 else if (!strcmp(scheme
, "https"))
911 else if (!strcmp(scheme
, "ipp"))
913 else if (!strcasecmp(scheme
, "lpd"))
915 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
917 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
918 status
= HTTP_URI_UNKNOWN_SCHEME
;
921 * Now see if we have a hostname...
924 if (!strncmp(uri
, "//", 2))
933 * Grab the username, if any...
936 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
939 * Get a username:password combo...
942 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
943 decoding
& HTTP_URI_CODING_USERNAME
);
948 return (HTTP_URI_BAD_USERNAME
);
955 * Then the hostname/IP address...
961 * Grab IPv6 address...
965 if (!strncmp(uri
, "v1.", 3))
966 uri
+= 3; /* Skip IPvN leader... */
968 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
969 decoding
& HTTP_URI_CODING_HOSTNAME
);
974 return (HTTP_URI_BAD_HOSTNAME
);
984 return (HTTP_URI_BAD_HOSTNAME
);
989 for (ptr
= host
; *ptr
; ptr
++)
993 * Convert zone separator to % and stop here...
999 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1002 return (HTTP_URI_BAD_HOSTNAME
);
1008 * Validate the hostname or IPv4 address first...
1011 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1012 if (strchr(":?/", *ptr
))
1014 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1015 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1019 "!$&'()*+,;=", *ptr
))
1022 return (HTTP_URI_BAD_HOSTNAME
);
1026 * Then copy the hostname or IPv4 address to the buffer...
1029 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1030 decoding
& HTTP_URI_CODING_HOSTNAME
);
1035 return (HTTP_URI_BAD_HOSTNAME
);
1040 * Validate hostname for file scheme - only empty and localhost are
1044 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1047 return (HTTP_URI_BAD_HOSTNAME
);
1051 * See if we have a port number...
1057 * Yes, collect the port number...
1060 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1062 if (*uri
!= '/' && *uri
)
1065 return (HTTP_URI_BAD_PORT
);
1071 * The remaining portion is the resource string...
1074 if (*uri
== '?' || !*uri
)
1077 * Hostname but no path...
1080 status
= HTTP_URI_MISSING_RESOURCE
;
1084 * Copy any query string...
1088 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1089 decoding
& HTTP_URI_CODING_QUERY
);
1095 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1096 decoding
& HTTP_URI_CODING_RESOURCE
);
1098 if (uri
&& *uri
== '?')
1101 * Concatenate any query string...
1104 char *resptr
= resource
+ strlen(resource
);
1106 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1107 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1114 return (HTTP_URI_BAD_RESOURCE
);
1118 * Return the URI separation status...
1126 * 'httpStatus()' - Return a short string describing a HTTP status code.
1129 const char * /* O - String or NULL */
1130 httpStatus(http_status_t status
) /* I - HTTP status code */
1134 case HTTP_CONTINUE
:
1135 return ("Continue");
1136 case HTTP_SWITCHING_PROTOCOLS
:
1137 return ("Switching Protocols");
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");
1179 #ifndef HAVE_HSTRERROR
1181 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1184 const char * /* O - Error string */
1185 _cups_hstrerror(int error
) /* I - Error number */
1187 static const char * const errors
[] = /* Error strings */
1192 "Unrecoverable lookup error.",
1193 "No data associated with name."
1197 if (error
< 0 || error
> 4)
1198 return ("Unknown hostname lookup error.");
1200 return (errors
[error
]);
1202 #endif /* !HAVE_HSTRERROR */
1206 * 'http_copy_decode()' - Copy and decode a URI.
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 */
1216 char *ptr
, /* Pointer into buffer */
1217 *end
; /* End of buffer */
1218 int quoted
; /* Quoted character */
1222 * Copy the src to the destination until we hit a terminating character
1223 * or the end of the string.
1226 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1227 *src
&& (!term
|| !strchr(term
, *src
));
1231 if (*src
== '%' && decode
)
1233 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1236 * Grab a hex-encoded character...
1241 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1243 quoted
= (*src
- '0') << 4;
1247 quoted
|= tolower(*src
) - 'a' + 10;
1249 quoted
|= *src
- '0';
1256 * Bad hex-encoded character...
1274 * 'http_copy_encode()' - Copy and encode a URI.
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? */
1285 static const char *hex
= "0123456789ABCDEF";
1288 while (*src
&& dst
< dstend
)
1290 if (term
&& *src
== *term
)
1293 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1294 (reserved
&& strchr(reserved
, *src
))))
1297 * Hex encode reserved characters...
1300 if ((dst
+ 2) >= dstend
)
1304 *dst
++ = hex
[(*src
>> 4) & 15];
1305 *dst
++ = hex
[*src
& 15];
1321 * End of "$Id: http-support.c 6649 2007-07-11 21:46:42Z mike $".