2 * "$Id: http-support.c 5149 2006-02-22 19:10:22Z 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
,
98 static char *http_copy_encode(char *dst
, const char *src
,
99 char *dstend
, const char *reserved
,
100 const char *term
, int encode
);
104 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
107 * This function escapes reserved characters in the URI depending on the
108 * value of the "encoding" argument. You should use this function in
109 * place of traditional string functions whenever you need to create a
115 http_uri_status_t
/* O - URI status */
117 http_uri_coding_t encoding
, /* I - Encoding flags */
118 char *uri
, /* I - URI buffer */
119 int urilen
, /* I - Size of URI buffer */
120 const char *scheme
, /* I - Scheme name */
121 const char *username
, /* I - Username */
122 const char *host
, /* I - Hostname or address */
123 int port
, /* I - Port number */
124 const char *resource
) /* I - Resource */
126 char *ptr
, /* Pointer into URI buffer */
127 *end
; /* End of URI buffer */
131 * Range check input...
134 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
139 return (HTTP_URI_BAD_ARGUMENTS
);
143 * Assemble the URI starting with the scheme...
146 end
= uri
+ urilen
- 1;
147 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
150 goto assemble_overflow
;
152 if (!strcmp(scheme
, "mailto:"))
155 * mailto: only has :, no //...
161 goto assemble_overflow
;
166 * Schemes other than mailto: all have //...
176 goto assemble_overflow
;
180 * Next the username and hostname, if any...
185 if (username
&& *username
)
188 * Add username@ first...
191 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
192 encoding
& HTTP_URI_CODING_USERNAME
);
195 goto assemble_overflow
;
200 goto assemble_overflow
;
204 * Then add the hostname. Since IPv6 is a particular pain to deal
205 * with, we have several special cases to deal with... If we get
206 * an IPv6 address with brackets around it, assume it is already in
210 if (host
[0] != '[' && strchr(host
, ':'))
213 * We have an IPv6 address...
216 if (strchr(host
, '%'))
219 * We have a link-local address, add "[v1." prefix...
230 goto assemble_overflow
;
235 * We have a normal address, add "[" prefix...
241 goto assemble_overflow
;
245 * Copy the rest of the IPv6 address, and terminate with "]".
248 while (ptr
< end
&& *host
)
252 *ptr
++ = '+'; /* Convert zone separator */
260 goto assemble_overflow
;
265 goto assemble_overflow
;
270 * Otherwise, just copy the host string...
273 ptr
= http_copy_encode(ptr
, host
, end
, NULL
, NULL
,
274 encoding
& HTTP_URI_CODING_HOSTNAME
);
277 goto assemble_overflow
;
281 * Finish things off with the port number...
286 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
290 goto assemble_overflow
;
295 * Last but not least, add the resource string...
300 char *query
; /* Pointer to query string */
304 * Copy the resource string up to the query string if present...
307 query
= strchr(resource
, '?');
308 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
309 encoding
& HTTP_URI_CODING_RESOURCE
);
311 goto assemble_overflow
;
316 * Copy query string without encoding...
319 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
320 encoding
& HTTP_URI_CODING_QUERY
);
322 goto assemble_overflow
;
328 goto assemble_overflow
;
331 * Nul-terminate the URI buffer and return with no errors...
336 return (HTTP_URI_OK
);
339 * Clear the URI string and return an overflow error; I don't usually
340 * like goto's, but in this case it makes sense...
346 return (HTTP_URI_OVERFLOW
);
351 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
352 * components with a formatted resource.
354 * This function creates a formatted version of the resource string
355 * argument "resourcef" and escapes reserved characters in the URI
356 * depending on the value of the "encoding" argument. You should use
357 * this function in place of traditional string functions whenever
358 * you need to create a URI string.
363 http_uri_status_t
/* O - URI status */
365 http_uri_coding_t encoding
, /* I - Encoding flags */
366 char *uri
, /* I - URI buffer */
367 int urilen
, /* I - Size of URI buffer */
368 const char *scheme
, /* I - Scheme name */
369 const char *username
, /* I - Username */
370 const char *host
, /* I - Hostname or address */
371 int port
, /* I - Port number */
372 const char *resourcef
, /* I - Printf-style resource */
373 ...) /* I - Additional arguments as needed */
375 va_list ap
; /* Pointer to additional arguments */
376 char resource
[1024]; /* Formatted resource string */
377 int bytes
; /* Bytes in formatted string */
381 * Range check input...
384 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
389 return (HTTP_URI_BAD_ARGUMENTS
);
393 * Format the resource string and assemble the URI...
396 va_start(ap
, resourcef
);
397 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
400 if (bytes
>= sizeof(resource
))
403 return (HTTP_URI_OVERFLOW
);
406 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
412 * 'httpDecode64()' - Base64-decode a string.
414 * This function is deprecated. Use the httpDecode64_2() function instead
415 * which provides buffer length arguments.
420 char * /* O - Decoded string */
421 httpDecode64(char *out
, /* I - String to write to */
422 const char *in
) /* I - String to read from */
424 int outlen
; /* Output buffer length */
428 * Use the old maximum buffer size for binary compatibility...
433 return (httpDecode64_2(out
, &outlen
, in
));
438 * 'httpDecode64_2()' - Base64-decode a string.
440 * @since CUPS 1.1.21@
443 char * /* O - Decoded string */
444 httpDecode64_2(char *out
, /* I - String to write to */
445 int *outlen
, /* IO - Size of output string */
446 const char *in
) /* I - String to read from */
448 int pos
, /* Bit position */
449 base64
; /* Value of this character */
450 char *outptr
, /* Output pointer */
451 *outend
; /* End of output buffer */
455 * Range check input...
458 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
470 * Convert from base-64 to bytes...
473 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
476 * Decode this character into a number from 0 to 63...
479 if (*in
>= 'A' && *in
<= 'Z')
481 else if (*in
>= 'a' && *in
<= 'z')
482 base64
= *in
- 'a' + 26;
483 else if (*in
>= '0' && *in
<= '9')
484 base64
= *in
- '0' + 52;
495 * Store the result in the appropriate chars...
502 *outptr
= base64
<< 2;
507 *outptr
++ |= (base64
>> 4) & 3;
509 *outptr
= (base64
<< 4) & 255;
514 *outptr
++ |= (base64
>> 2) & 15;
516 *outptr
= (base64
<< 6) & 255;
530 * Return the decoded string and size...
533 *outlen
= (int)(outptr
- out
);
540 * 'httpEncode64()' - Base64-encode a string.
542 * This function is deprecated. Use the httpEncode64_2() function instead
543 * which provides buffer length arguments.
548 char * /* O - Encoded string */
549 httpEncode64(char *out
, /* I - String to write to */
550 const char *in
) /* I - String to read from */
552 return (httpEncode64_2(out
, 512, in
, strlen(in
)));
557 * 'httpEncode64_2()' - Base64-encode a string.
559 * @since CUPS 1.1.21@
562 char * /* O - Encoded string */
563 httpEncode64_2(char *out
, /* I - String to write to */
564 int outlen
, /* I - Size of output string */
565 const char *in
, /* I - String to read from */
566 int inlen
) /* I - Size of input string */
568 char *outptr
, /* Output pointer */
569 *outend
; /* End of output buffer */
570 static const char base64
[] = /* Base64 characters... */
572 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
573 "abcdefghijklmnopqrstuvwxyz"
580 * Range check input...
583 if (!out
|| outlen
< 1 || !in
)
587 * Convert bytes to base-64...
590 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
593 * Encode the up to 3 characters as 4 Base64 numbers...
597 *outptr
++ = base64
[(in
[0] & 255) >> 2];
599 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
613 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
625 *outptr
++ = base64
[in
[0] & 63];
631 * Return the encoded string...
639 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
644 const char * /* O - Date/time string */
645 httpGetDateString(time_t t
) /* I - UNIX time */
647 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
650 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
655 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
660 const char * /* O - Date/time string */
661 httpGetDateString2(time_t t
, /* I - UNIX time */
662 char *s
, /* I - String buffer */
663 int slen
) /* I - Size of string buffer */
665 struct tm
*tdate
; /* UNIX date/time data */
669 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
670 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
671 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
672 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
679 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
682 time_t /* O - UNIX time */
683 httpGetDateTime(const char *s
) /* I - Date/time string */
685 int i
; /* Looping var */
686 char mon
[16]; /* Abbreviated month name */
687 int day
, year
; /* Day of month and year */
688 int hour
, min
, sec
; /* Time */
689 int days
; /* Number of days since 1970 */
690 static const int normal_days
[] = /* Days to a month, normal years */
691 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
692 static const int leap_days
[] = /* Days to a month, leap years */
693 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
696 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
699 * Extract the date and time from the formatted string...
702 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
705 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
706 day
, mon
, year
, hour
, min
, sec
));
709 * Convert the month name to a number from 0 to 11.
712 for (i
= 0; i
< 12; i
++)
713 if (!strcasecmp(mon
, http_months
[i
]))
719 DEBUG_printf((" i=%d\n", i
));
722 * Now convert the date and time to a UNIX time value in seconds since
723 * 1970. We can't use mktime() since the timezone may not be UTC but
724 * the date/time string *is* UTC.
727 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
728 days
= leap_days
[i
] + day
- 1;
730 days
= normal_days
[i
] + day
- 1;
732 DEBUG_printf((" days=%d\n", days
));
734 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
735 ((year
- 1) / 4 - 492) - /* + leap days */
736 ((year
- 1) / 100 - 19) + /* - 100 year days */
737 ((year
- 1) / 400 - 4); /* + 400 year days */
739 DEBUG_printf((" days=%d\n", days
));
741 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
746 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
749 * This function is deprecated; use the httpSeparateURI() function instead.
755 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
756 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
757 char *username
, /* O - Username [1024] */
758 char *host
, /* O - Hostname [1024] */
759 int *port
, /* O - Port number to use */
760 char *resource
) /* O - Resource/filename [1024] */
762 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
763 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
769 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
772 * This function is deprecated; use the httpSeparateURI() function instead.
774 * @since CUPS 1.1.21@
779 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
780 char *scheme
, /* O - Scheme (http, https, etc.) */
781 int schemelen
, /* I - Size of scheme buffer */
782 char *username
, /* O - Username */
783 int usernamelen
, /* I - Size of username buffer */
784 char *host
, /* O - Hostname */
785 int hostlen
, /* I - Size of hostname buffer */
786 int *port
, /* O - Port number to use */
787 char *resource
, /* O - Resource/filename */
788 int resourcelen
) /* I - Size of resource buffer */
790 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
791 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
796 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
802 http_uri_status_t
/* O - Result of separation */
804 http_uri_coding_t decoding
, /* I - Decoding flags */
805 const char *uri
, /* I - Universal Resource Identifier */
806 char *scheme
, /* O - Scheme (http, https, etc.) */
807 int schemelen
, /* I - Size of scheme buffer */
808 char *username
, /* O - Username */
809 int usernamelen
, /* I - Size of username buffer */
810 char *host
, /* O - Hostname */
811 int hostlen
, /* I - Size of hostname buffer */
812 int *port
, /* O - Port number to use */
813 char *resource
, /* O - Resource/filename */
814 int resourcelen
) /* I - Size of resource buffer */
816 char *ptr
, /* Pointer into string... */
817 *end
; /* End of string */
818 const char *sep
; /* Separator character */
819 http_uri_status_t status
; /* Result of separation */
823 * Initialize everything to blank...
826 if (scheme
&& schemelen
> 0)
829 if (username
&& usernamelen
> 0)
832 if (host
&& hostlen
> 0)
838 if (resource
&& resourcelen
> 0)
842 * Range check input...
845 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
846 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
848 return (HTTP_URI_BAD_ARGUMENTS
);
851 return (HTTP_URI_BAD_URI
);
854 * Grab the scheme portion of the URI...
857 status
= HTTP_URI_OK
;
859 if (!strncmp(uri
, "//", 2))
862 * Workaround for HP IPP client bug...
865 strlcpy(scheme
, "ipp", schemelen
);
866 status
= HTTP_URI_MISSING_SCHEME
;
868 else if (*uri
== '/')
874 strlcpy(scheme
, "file", schemelen
);
875 status
= HTTP_URI_MISSING_SCHEME
;
880 * Standard URI with scheme...
883 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
884 *uri
&& *uri
!= ':' && ptr
< end
;)
885 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
895 return (HTTP_URI_BAD_SCHEME
);
902 * Set the default port number...
905 if (!strcmp(scheme
, "http"))
907 else if (!strcmp(scheme
, "https"))
909 else if (!strcmp(scheme
, "ipp"))
911 else if (!strcasecmp(scheme
, "lpd"))
913 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
915 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
916 status
= HTTP_URI_UNKNOWN_SCHEME
;
919 * Now see if we have a hostname...
922 if (!strncmp(uri
, "//", 2))
931 * Grab the username, if any...
934 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
937 * Get a username:password combo...
940 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
941 decoding
& HTTP_URI_CODING_USERNAME
);
946 return (HTTP_URI_BAD_USERNAME
);
953 * Then the hostname/IP address...
959 * Grab IPv6 address...
963 if (!strncmp(uri
, "v1.", 3))
964 uri
+= 3; /* Skip IPvN leader... */
966 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
967 decoding
& HTTP_URI_CODING_HOSTNAME
);
972 return (HTTP_URI_BAD_HOSTNAME
);
982 return (HTTP_URI_BAD_HOSTNAME
);
987 for (ptr
= host
; *ptr
; ptr
++)
991 * Convert zone separator to % and stop here...
997 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1000 return (HTTP_URI_BAD_HOSTNAME
);
1006 * Validate the hostname or IPv4 address first...
1009 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1010 if (strchr(":?/", *ptr
))
1012 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1013 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1017 "!$&'()*+,;=", *ptr
))
1020 return (HTTP_URI_BAD_HOSTNAME
);
1024 * Then copy the hostname or IPv4 address to the buffer...
1027 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1028 decoding
& HTTP_URI_CODING_HOSTNAME
);
1033 return (HTTP_URI_BAD_HOSTNAME
);
1038 * Validate hostname for file scheme - only empty and localhost are
1042 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1045 return (HTTP_URI_BAD_HOSTNAME
);
1049 * See if we have a port number...
1055 * Yes, collect the port number...
1058 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1063 return (HTTP_URI_BAD_PORT
);
1069 * The remaining portion is the resource string...
1072 if (*uri
== '?' || !*uri
)
1075 * Hostname but no path...
1078 status
= HTTP_URI_MISSING_RESOURCE
;
1082 * Copy any query string...
1086 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1087 decoding
& HTTP_URI_CODING_QUERY
);
1093 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1094 decoding
& HTTP_URI_CODING_RESOURCE
);
1096 if (uri
&& *uri
== '?')
1099 * Concatenate any query string...
1102 char *resptr
= resource
+ strlen(resource
);
1104 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (resptr
- resource
),
1105 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1112 return (HTTP_URI_BAD_RESOURCE
);
1116 * Return the URI separation status...
1124 * 'httpStatus()' - Return a short string describing a HTTP status code.
1127 const char * /* O - String or NULL */
1128 httpStatus(http_status_t status
) /* I - HTTP status code */
1132 case HTTP_CONTINUE
:
1133 return ("Continue");
1134 case HTTP_SWITCHING_PROTOCOLS
:
1135 return ("Switching Protocols");
1140 case HTTP_ACCEPTED
:
1141 return ("Accepted");
1142 case HTTP_NO_CONTENT
:
1143 return ("No Content");
1144 case HTTP_NOT_MODIFIED
:
1145 return ("Not Modified");
1146 case HTTP_BAD_REQUEST
:
1147 return ("Bad Request");
1148 case HTTP_UNAUTHORIZED
:
1149 return ("Unauthorized");
1150 case HTTP_FORBIDDEN
:
1151 return ("Forbidden");
1152 case HTTP_NOT_FOUND
:
1153 return ("Not Found");
1154 case HTTP_REQUEST_TOO_LARGE
:
1155 return ("Request Entity Too Large");
1156 case HTTP_URI_TOO_LONG
:
1157 return ("URI Too Long");
1158 case HTTP_UPGRADE_REQUIRED
:
1159 return ("Upgrade Required");
1160 case HTTP_NOT_IMPLEMENTED
:
1161 return ("Not Implemented");
1162 case HTTP_NOT_SUPPORTED
:
1163 return ("Not Supported");
1164 case HTTP_EXPECTATION_FAILED
:
1165 return ("Expectation Failed");
1173 #ifndef HAVE_HSTRERROR
1175 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1178 const char * /* O - Error string */
1179 _cups_hstrerror(int error
) /* I - Error number */
1181 static const char * const errors
[] = /* Error strings */
1186 "Unrecoverable lookup error.",
1187 "No data associated with name."
1191 if (error
< 0 || error
> 4)
1192 return ("Unknown hostname lookup error.");
1194 return (errors
[error
]);
1196 #endif /* !HAVE_HSTRERROR */
1200 * 'http_copy_decode()' - Copy and decode a URI.
1203 static const char * /* O - New source pointer or NULL on error */
1204 http_copy_decode(char *dst
, /* O - Destination buffer */
1205 const char *src
, /* I - Source pointer */
1206 int dstsize
, /* I - Destination size */
1207 const char *term
, /* I - Terminating characters */
1208 int decode
) /* I - Decode %-encoded values */
1210 char *ptr
, /* Pointer into buffer */
1211 *end
; /* End of buffer */
1212 int quoted
; /* Quoted character */
1216 * Copy the src to the destination until we hit a terminating character
1217 * or the end of the string.
1220 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1221 *src
&& (!term
|| !strchr(term
, *src
));
1225 if (*src
== '%' && decode
)
1227 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1230 * Grab a hex-encoded character...
1235 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1237 quoted
= (*src
- '0') << 4;
1241 quoted
|= tolower(*src
) - 'a' + 10;
1243 quoted
|= *src
- '0';
1250 * Bad hex-encoded character...
1268 * 'http_copy_encode()' - Copy and encode a URI.
1271 static char * /* O - End of current URI */
1272 http_copy_encode(char *dst
, /* O - Destination buffer */
1273 const char *src
, /* I - Source pointer */
1274 char *dstend
, /* I - End of destination buffer */
1275 const char *reserved
, /* I - Extra reserved characters */
1276 const char *term
, /* I - Terminating characters */
1277 int encode
) /* I - %-encode reserved chars? */
1279 static const char *hex
= "0123456789ABCDEF";
1282 while (*src
&& dst
< dstend
)
1284 if (term
&& *src
== *term
)
1287 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1288 (reserved
&& strchr(reserved
, *src
))))
1291 * Hex encode reserved characters...
1294 if ((dst
+ 2) >= dstend
)
1298 *dst
++ = hex
[(*src
>> 4) & 15];
1299 *dst
++ = hex
[*src
& 15];
1315 * End of "$Id: http-support.c 5149 2006-02-22 19:10:22Z mike $".