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] =
66 static const char * const http_months
[12] =
87 static const char *http_copy_decode(char *dst
, const char *src
,
88 int dstsize
, const char *term
,
90 static char *http_copy_encode(char *dst
, const char *src
,
91 char *dstend
, const char *reserved
,
92 const char *term
, int encode
);
96 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
99 * This function escapes reserved characters in the URI depending on the
100 * value of the "encoding" argument. You should use this function in
101 * place of traditional string functions whenever you need to create a
107 http_uri_status_t
/* O - URI status */
109 http_uri_coding_t encoding
, /* I - Encoding flags */
110 char *uri
, /* I - URI buffer */
111 int urilen
, /* I - Size of URI buffer */
112 const char *scheme
, /* I - Scheme name */
113 const char *username
, /* I - Username */
114 const char *host
, /* I - Hostname or address */
115 int port
, /* I - Port number */
116 const char *resource
) /* I - Resource */
118 char *ptr
, /* Pointer into URI buffer */
119 *end
; /* End of URI buffer */
123 * Range check input...
126 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
131 return (HTTP_URI_BAD_ARGUMENTS
);
135 * Assemble the URI starting with the scheme...
138 end
= uri
+ urilen
- 1;
139 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
142 goto assemble_overflow
;
144 if (!strcmp(scheme
, "mailto"))
147 * mailto: only has :, no //...
153 goto assemble_overflow
;
158 * Schemes other than mailto: all have //...
168 goto assemble_overflow
;
172 * Next the username and hostname, if any...
177 if (username
&& *username
)
180 * Add username@ first...
183 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
184 encoding
& HTTP_URI_CODING_USERNAME
);
187 goto assemble_overflow
;
192 goto assemble_overflow
;
196 * Then add the hostname. Since IPv6 is a particular pain to deal
197 * with, we have several special cases to deal with... If we get
198 * an IPv6 address with brackets around it, assume it is already in
202 if (host
[0] != '[' && strchr(host
, ':'))
205 * We have an IPv6 address...
208 if (strchr(host
, '%'))
211 * We have a link-local address, add "[v1." prefix...
222 goto assemble_overflow
;
227 * We have a normal address, add "[" prefix...
233 goto assemble_overflow
;
237 * Copy the rest of the IPv6 address, and terminate with "]".
240 while (ptr
< end
&& *host
)
244 *ptr
++ = '+'; /* Convert zone separator */
252 goto assemble_overflow
;
257 goto assemble_overflow
;
262 * Otherwise, just copy the host string...
265 ptr
= http_copy_encode(ptr
, host
, end
, NULL
, NULL
,
266 encoding
& HTTP_URI_CODING_HOSTNAME
);
269 goto assemble_overflow
;
273 * Finish things off with the port number...
278 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
282 goto assemble_overflow
;
287 * Last but not least, add the resource string...
292 char *query
; /* Pointer to query string */
296 * Copy the resource string up to the query string if present...
299 query
= strchr(resource
, '?');
300 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
301 encoding
& HTTP_URI_CODING_RESOURCE
);
303 goto assemble_overflow
;
308 * Copy query string without encoding...
311 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
312 encoding
& HTTP_URI_CODING_QUERY
);
314 goto assemble_overflow
;
320 goto assemble_overflow
;
323 * Nul-terminate the URI buffer and return with no errors...
328 return (HTTP_URI_OK
);
331 * Clear the URI string and return an overflow error; I don't usually
332 * like goto's, but in this case it makes sense...
338 return (HTTP_URI_OVERFLOW
);
343 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
344 * components with a formatted resource.
346 * This function creates a formatted version of the resource string
347 * argument "resourcef" and escapes reserved characters in the URI
348 * depending on the value of the "encoding" argument. You should use
349 * this function in place of traditional string functions whenever
350 * you need to create a URI string.
355 http_uri_status_t
/* O - URI status */
357 http_uri_coding_t encoding
, /* I - Encoding flags */
358 char *uri
, /* I - URI buffer */
359 int urilen
, /* I - Size of URI buffer */
360 const char *scheme
, /* I - Scheme name */
361 const char *username
, /* I - Username */
362 const char *host
, /* I - Hostname or address */
363 int port
, /* I - Port number */
364 const char *resourcef
, /* I - Printf-style resource */
365 ...) /* I - Additional arguments as needed */
367 va_list ap
; /* Pointer to additional arguments */
368 char resource
[1024]; /* Formatted resource string */
369 int bytes
; /* Bytes in formatted string */
373 * Range check input...
376 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
381 return (HTTP_URI_BAD_ARGUMENTS
);
385 * Format the resource string and assemble the URI...
388 va_start(ap
, resourcef
);
389 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
392 if (bytes
>= sizeof(resource
))
395 return (HTTP_URI_OVERFLOW
);
398 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
404 * 'httpDecode64()' - Base64-decode a string.
406 * This function is deprecated. Use the httpDecode64_2() function instead
407 * which provides buffer length arguments.
412 char * /* O - Decoded string */
413 httpDecode64(char *out
, /* I - String to write to */
414 const char *in
) /* I - String to read from */
416 int outlen
; /* Output buffer length */
420 * Use the old maximum buffer size for binary compatibility...
425 return (httpDecode64_2(out
, &outlen
, in
));
430 * 'httpDecode64_2()' - Base64-decode a string.
432 * @since CUPS 1.1.21@
435 char * /* O - Decoded string */
436 httpDecode64_2(char *out
, /* I - String to write to */
437 int *outlen
, /* IO - Size of output string */
438 const char *in
) /* I - String to read from */
440 int pos
, /* Bit position */
441 base64
; /* Value of this character */
442 char *outptr
, /* Output pointer */
443 *outend
; /* End of output buffer */
447 * Range check input...
450 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
462 * Convert from base-64 to bytes...
465 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
468 * Decode this character into a number from 0 to 63...
471 if (*in
>= 'A' && *in
<= 'Z')
473 else if (*in
>= 'a' && *in
<= 'z')
474 base64
= *in
- 'a' + 26;
475 else if (*in
>= '0' && *in
<= '9')
476 base64
= *in
- '0' + 52;
487 * Store the result in the appropriate chars...
494 *outptr
= base64
<< 2;
499 *outptr
++ |= (base64
>> 4) & 3;
501 *outptr
= (base64
<< 4) & 255;
506 *outptr
++ |= (base64
>> 2) & 15;
508 *outptr
= (base64
<< 6) & 255;
522 * Return the decoded string and size...
525 *outlen
= (int)(outptr
- out
);
532 * 'httpEncode64()' - Base64-encode a string.
534 * This function is deprecated. Use the httpEncode64_2() function instead
535 * which provides buffer length arguments.
540 char * /* O - Encoded string */
541 httpEncode64(char *out
, /* I - String to write to */
542 const char *in
) /* I - String to read from */
544 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
549 * 'httpEncode64_2()' - Base64-encode a string.
551 * @since CUPS 1.1.21@
554 char * /* O - Encoded string */
555 httpEncode64_2(char *out
, /* I - String to write to */
556 int outlen
, /* I - Size of output string */
557 const char *in
, /* I - String to read from */
558 int inlen
) /* I - Size of input string */
560 char *outptr
, /* Output pointer */
561 *outend
; /* End of output buffer */
562 static const char base64
[] = /* Base64 characters... */
564 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
565 "abcdefghijklmnopqrstuvwxyz"
572 * Range check input...
575 if (!out
|| outlen
< 1 || !in
)
579 * Convert bytes to base-64...
582 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
585 * Encode the up to 3 characters as 4 Base64 numbers...
589 *outptr
++ = base64
[(in
[0] & 255) >> 2];
594 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
596 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
613 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
615 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
628 *outptr
++ = base64
[in
[0] & 63];
634 * Return the encoded string...
642 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
647 const char * /* O - Date/time string */
648 httpGetDateString(time_t t
) /* I - UNIX time */
650 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
653 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
658 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
663 const char * /* O - Date/time string */
664 httpGetDateString2(time_t t
, /* I - UNIX time */
665 char *s
, /* I - String buffer */
666 int slen
) /* I - Size of string buffer */
668 struct tm
*tdate
; /* UNIX date/time data */
672 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
673 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
674 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
675 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
682 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
685 time_t /* O - UNIX time */
686 httpGetDateTime(const char *s
) /* I - Date/time string */
688 int i
; /* Looping var */
689 char mon
[16]; /* Abbreviated month name */
690 int day
, year
; /* Day of month and year */
691 int hour
, min
, sec
; /* Time */
692 int days
; /* Number of days since 1970 */
693 static const int normal_days
[] = /* Days to a month, normal years */
694 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
695 static const int leap_days
[] = /* Days to a month, leap years */
696 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
699 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
702 * Extract the date and time from the formatted string...
705 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
708 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
709 day
, mon
, year
, hour
, min
, sec
));
712 * Convert the month name to a number from 0 to 11.
715 for (i
= 0; i
< 12; i
++)
716 if (!strcasecmp(mon
, http_months
[i
]))
722 DEBUG_printf((" i=%d\n", i
));
725 * Now convert the date and time to a UNIX time value in seconds since
726 * 1970. We can't use mktime() since the timezone may not be UTC but
727 * the date/time string *is* UTC.
730 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
731 days
= leap_days
[i
] + day
- 1;
733 days
= normal_days
[i
] + day
- 1;
735 DEBUG_printf((" days=%d\n", days
));
737 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
738 ((year
- 1) / 4 - 492) - /* + leap days */
739 ((year
- 1) / 100 - 19) + /* - 100 year days */
740 ((year
- 1) / 400 - 4); /* + 400 year days */
742 DEBUG_printf((" days=%d\n", days
));
744 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
749 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
752 * This function is deprecated; use the httpSeparateURI() function instead.
758 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
759 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
760 char *username
, /* O - Username [1024] */
761 char *host
, /* O - Hostname [1024] */
762 int *port
, /* O - Port number to use */
763 char *resource
) /* O - Resource/filename [1024] */
765 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
766 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
772 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
775 * This function is deprecated; use the httpSeparateURI() function instead.
777 * @since CUPS 1.1.21@
782 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
783 char *scheme
, /* O - Scheme (http, https, etc.) */
784 int schemelen
, /* I - Size of scheme buffer */
785 char *username
, /* O - Username */
786 int usernamelen
, /* I - Size of username buffer */
787 char *host
, /* O - Hostname */
788 int hostlen
, /* I - Size of hostname buffer */
789 int *port
, /* O - Port number to use */
790 char *resource
, /* O - Resource/filename */
791 int resourcelen
) /* I - Size of resource buffer */
793 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
794 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
799 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
805 http_uri_status_t
/* O - Result of separation */
807 http_uri_coding_t decoding
, /* I - Decoding flags */
808 const char *uri
, /* I - Universal Resource Identifier */
809 char *scheme
, /* O - Scheme (http, https, etc.) */
810 int schemelen
, /* I - Size of scheme buffer */
811 char *username
, /* O - Username */
812 int usernamelen
, /* I - Size of username buffer */
813 char *host
, /* O - Hostname */
814 int hostlen
, /* I - Size of hostname buffer */
815 int *port
, /* O - Port number to use */
816 char *resource
, /* O - Resource/filename */
817 int resourcelen
) /* I - Size of resource buffer */
819 char *ptr
, /* Pointer into string... */
820 *end
; /* End of string */
821 const char *sep
; /* Separator character */
822 http_uri_status_t status
; /* Result of separation */
826 * Initialize everything to blank...
829 if (scheme
&& schemelen
> 0)
832 if (username
&& usernamelen
> 0)
835 if (host
&& hostlen
> 0)
841 if (resource
&& resourcelen
> 0)
845 * Range check input...
848 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
849 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
851 return (HTTP_URI_BAD_ARGUMENTS
);
854 return (HTTP_URI_BAD_URI
);
857 * Grab the scheme portion of the URI...
860 status
= HTTP_URI_OK
;
862 if (!strncmp(uri
, "//", 2))
865 * Workaround for HP IPP client bug...
868 strlcpy(scheme
, "ipp", schemelen
);
869 status
= HTTP_URI_MISSING_SCHEME
;
871 else if (*uri
== '/')
877 strlcpy(scheme
, "file", schemelen
);
878 status
= HTTP_URI_MISSING_SCHEME
;
883 * Standard URI with scheme...
886 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
887 *uri
&& *uri
!= ':' && ptr
< end
;)
888 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
898 return (HTTP_URI_BAD_SCHEME
);
905 * Set the default port number...
908 if (!strcmp(scheme
, "http"))
910 else if (!strcmp(scheme
, "https"))
912 else if (!strcmp(scheme
, "ipp"))
914 else if (!strcasecmp(scheme
, "lpd"))
916 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
918 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
919 status
= HTTP_URI_UNKNOWN_SCHEME
;
922 * Now see if we have a hostname...
925 if (!strncmp(uri
, "//", 2))
934 * Grab the username, if any...
937 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
940 * Get a username:password combo...
943 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
944 decoding
& HTTP_URI_CODING_USERNAME
);
949 return (HTTP_URI_BAD_USERNAME
);
956 * Then the hostname/IP address...
962 * Grab IPv6 address...
966 if (!strncmp(uri
, "v1.", 3))
967 uri
+= 3; /* Skip IPvN leader... */
969 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
970 decoding
& HTTP_URI_CODING_HOSTNAME
);
975 return (HTTP_URI_BAD_HOSTNAME
);
985 return (HTTP_URI_BAD_HOSTNAME
);
990 for (ptr
= host
; *ptr
; ptr
++)
994 * Convert zone separator to % and stop here...
1000 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1003 return (HTTP_URI_BAD_HOSTNAME
);
1009 * Validate the hostname or IPv4 address first...
1012 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1013 if (strchr(":?/", *ptr
))
1015 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1016 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1020 "!$&'()*+,;=\\", *ptr
))
1023 return (HTTP_URI_BAD_HOSTNAME
);
1027 * Then copy the hostname or IPv4 address to the buffer...
1030 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1031 decoding
& HTTP_URI_CODING_HOSTNAME
);
1036 return (HTTP_URI_BAD_HOSTNAME
);
1041 * Validate hostname for file scheme - only empty and localhost are
1045 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1048 return (HTTP_URI_BAD_HOSTNAME
);
1052 * See if we have a port number...
1058 * Yes, collect the port number...
1061 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1063 if (*uri
!= '/' && *uri
)
1066 return (HTTP_URI_BAD_PORT
);
1072 * The remaining portion is the resource string...
1075 if (*uri
== '?' || !*uri
)
1078 * Hostname but no path...
1081 status
= HTTP_URI_MISSING_RESOURCE
;
1085 * Copy any query string...
1089 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1090 decoding
& HTTP_URI_CODING_QUERY
);
1096 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1097 decoding
& HTTP_URI_CODING_RESOURCE
);
1099 if (uri
&& *uri
== '?')
1102 * Concatenate any query string...
1105 char *resptr
= resource
+ strlen(resource
);
1107 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1108 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1115 return (HTTP_URI_BAD_RESOURCE
);
1119 * Return the URI separation status...
1127 * 'httpStatus()' - Return a short string describing a HTTP status code.
1130 const char * /* O - String or NULL */
1131 httpStatus(http_status_t status
) /* I - HTTP status code */
1135 case HTTP_CONTINUE
:
1136 return ("Continue");
1137 case HTTP_SWITCHING_PROTOCOLS
:
1138 return ("Switching Protocols");
1143 case HTTP_ACCEPTED
:
1144 return ("Accepted");
1145 case HTTP_NO_CONTENT
:
1146 return ("No Content");
1147 case HTTP_MOVED_PERMANENTLY
:
1148 return ("Moved Permanently");
1149 case HTTP_SEE_OTHER
:
1150 return ("See Other");
1151 case HTTP_NOT_MODIFIED
:
1152 return ("Not Modified");
1153 case HTTP_BAD_REQUEST
:
1154 return ("Bad Request");
1155 case HTTP_UNAUTHORIZED
:
1156 return ("Unauthorized");
1157 case HTTP_FORBIDDEN
:
1158 return ("Forbidden");
1159 case HTTP_NOT_FOUND
:
1160 return ("Not Found");
1161 case HTTP_REQUEST_TOO_LARGE
:
1162 return ("Request Entity Too Large");
1163 case HTTP_URI_TOO_LONG
:
1164 return ("URI Too Long");
1165 case HTTP_UPGRADE_REQUIRED
:
1166 return ("Upgrade Required");
1167 case HTTP_NOT_IMPLEMENTED
:
1168 return ("Not Implemented");
1169 case HTTP_NOT_SUPPORTED
:
1170 return ("Not Supported");
1171 case HTTP_EXPECTATION_FAILED
:
1172 return ("Expectation Failed");
1180 #ifndef HAVE_HSTRERROR
1182 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1185 const char * /* O - Error string */
1186 _cups_hstrerror(int error
) /* I - Error number */
1188 static const char * const errors
[] = /* Error strings */
1193 "Unrecoverable lookup error.",
1194 "No data associated with name."
1198 if (error
< 0 || error
> 4)
1199 return ("Unknown hostname lookup error.");
1201 return (errors
[error
]);
1203 #endif /* !HAVE_HSTRERROR */
1207 * 'http_copy_decode()' - Copy and decode a URI.
1210 static const char * /* O - New source pointer or NULL on error */
1211 http_copy_decode(char *dst
, /* O - Destination buffer */
1212 const char *src
, /* I - Source pointer */
1213 int dstsize
, /* I - Destination size */
1214 const char *term
, /* I - Terminating characters */
1215 int decode
) /* I - Decode %-encoded values */
1217 char *ptr
, /* Pointer into buffer */
1218 *end
; /* End of buffer */
1219 int quoted
; /* Quoted character */
1223 * Copy the src to the destination until we hit a terminating character
1224 * or the end of the string.
1227 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1228 *src
&& (!term
|| !strchr(term
, *src
));
1232 if (*src
== '%' && decode
)
1234 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1237 * Grab a hex-encoded character...
1242 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1244 quoted
= (*src
- '0') << 4;
1248 quoted
|= tolower(*src
) - 'a' + 10;
1250 quoted
|= *src
- '0';
1257 * Bad hex-encoded character...
1275 * 'http_copy_encode()' - Copy and encode a URI.
1278 static char * /* O - End of current URI */
1279 http_copy_encode(char *dst
, /* O - Destination buffer */
1280 const char *src
, /* I - Source pointer */
1281 char *dstend
, /* I - End of destination buffer */
1282 const char *reserved
, /* I - Extra reserved characters */
1283 const char *term
, /* I - Terminating characters */
1284 int encode
) /* I - %-encode reserved chars? */
1286 static const char *hex
= "0123456789ABCDEF";
1289 while (*src
&& dst
< dstend
)
1291 if (term
&& *src
== *term
)
1294 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1295 (reserved
&& strchr(reserved
, *src
))))
1298 * Hex encode reserved characters...
1301 if ((dst
+ 2) >= dstend
)
1305 *dst
++ = hex
[(*src
>> 4) & 15];
1306 *dst
++ = hex
[*src
& 15];
1322 * End of "$Id: http-support.c 6649 2007-07-11 21:46:42Z mike $".