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
203 if (host
[0] != '[' && strchr(host
, ':'))
206 * We have an IPv6 address...
209 if (strchr(host
, '%'))
212 * We have a link-local address, add "[v1." prefix...
223 goto assemble_overflow
;
228 * We have a normal address, add "[" prefix...
234 goto assemble_overflow
;
238 * Copy the rest of the IPv6 address, and terminate with "]".
241 while (ptr
< end
&& *host
)
245 *ptr
++ = '+'; /* Convert zone separator */
253 goto assemble_overflow
;
258 goto assemble_overflow
;
263 * Otherwise, just copy the host string...
266 ptr
= http_copy_encode(ptr
, host
, end
, NULL
, NULL
,
267 encoding
& HTTP_URI_CODING_HOSTNAME
);
270 goto assemble_overflow
;
274 * Finish things off with the port number...
279 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
283 goto assemble_overflow
;
288 * Last but not least, add the resource string...
293 char *query
; /* Pointer to query string */
297 * Copy the resource string up to the query string if present...
300 query
= strchr(resource
, '?');
301 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
302 encoding
& HTTP_URI_CODING_RESOURCE
);
304 goto assemble_overflow
;
309 * Copy query string without encoding...
312 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
313 encoding
& HTTP_URI_CODING_QUERY
);
315 goto assemble_overflow
;
321 goto assemble_overflow
;
324 * Nul-terminate the URI buffer and return with no errors...
329 return (HTTP_URI_OK
);
332 * Clear the URI string and return an overflow error; I don't usually
333 * like goto's, but in this case it makes sense...
339 return (HTTP_URI_OVERFLOW
);
344 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
345 * components with a formatted resource.
347 * This function creates a formatted version of the resource string
348 * argument "resourcef" and escapes reserved characters in the URI
349 * depending on the value of the "encoding" argument. You should use
350 * this function in place of traditional string functions whenever
351 * you need to create a URI string.
356 http_uri_status_t
/* O - URI status */
358 http_uri_coding_t encoding
, /* I - Encoding flags */
359 char *uri
, /* I - URI buffer */
360 int urilen
, /* I - Size of URI buffer */
361 const char *scheme
, /* I - Scheme name */
362 const char *username
, /* I - Username */
363 const char *host
, /* I - Hostname or address */
364 int port
, /* I - Port number */
365 const char *resourcef
, /* I - Printf-style resource */
366 ...) /* I - Additional arguments as needed */
368 va_list ap
; /* Pointer to additional arguments */
369 char resource
[1024]; /* Formatted resource string */
370 int bytes
; /* Bytes in formatted string */
374 * Range check input...
377 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
382 return (HTTP_URI_BAD_ARGUMENTS
);
386 * Format the resource string and assemble the URI...
389 va_start(ap
, resourcef
);
390 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
393 if (bytes
>= sizeof(resource
))
396 return (HTTP_URI_OVERFLOW
);
399 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
405 * 'httpDecode64()' - Base64-decode a string.
407 * This function is deprecated. Use the httpDecode64_2() function instead
408 * which provides buffer length arguments.
413 char * /* O - Decoded string */
414 httpDecode64(char *out
, /* I - String to write to */
415 const char *in
) /* I - String to read from */
417 int outlen
; /* Output buffer length */
421 * Use the old maximum buffer size for binary compatibility...
426 return (httpDecode64_2(out
, &outlen
, in
));
431 * 'httpDecode64_2()' - Base64-decode a string.
433 * @since CUPS 1.1.21@
436 char * /* O - Decoded string */
437 httpDecode64_2(char *out
, /* I - String to write to */
438 int *outlen
, /* IO - Size of output string */
439 const char *in
) /* I - String to read from */
441 int pos
, /* Bit position */
442 base64
; /* Value of this character */
443 char *outptr
, /* Output pointer */
444 *outend
; /* End of output buffer */
448 * Range check input...
451 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
463 * Convert from base-64 to bytes...
466 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
469 * Decode this character into a number from 0 to 63...
472 if (*in
>= 'A' && *in
<= 'Z')
474 else if (*in
>= 'a' && *in
<= 'z')
475 base64
= *in
- 'a' + 26;
476 else if (*in
>= '0' && *in
<= '9')
477 base64
= *in
- '0' + 52;
488 * Store the result in the appropriate chars...
495 *outptr
= base64
<< 2;
500 *outptr
++ |= (base64
>> 4) & 3;
502 *outptr
= (base64
<< 4) & 255;
507 *outptr
++ |= (base64
>> 2) & 15;
509 *outptr
= (base64
<< 6) & 255;
523 * Return the decoded string and size...
526 *outlen
= (int)(outptr
- out
);
533 * 'httpEncode64()' - Base64-encode a string.
535 * This function is deprecated. Use the httpEncode64_2() function instead
536 * which provides buffer length arguments.
541 char * /* O - Encoded string */
542 httpEncode64(char *out
, /* I - String to write to */
543 const char *in
) /* I - String to read from */
545 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
550 * 'httpEncode64_2()' - Base64-encode a string.
552 * @since CUPS 1.1.21@
555 char * /* O - Encoded string */
556 httpEncode64_2(char *out
, /* I - String to write to */
557 int outlen
, /* I - Size of output string */
558 const char *in
, /* I - String to read from */
559 int inlen
) /* I - Size of input string */
561 char *outptr
, /* Output pointer */
562 *outend
; /* End of output buffer */
563 static const char base64
[] = /* Base64 characters... */
565 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
566 "abcdefghijklmnopqrstuvwxyz"
573 * Range check input...
576 if (!out
|| outlen
< 1 || !in
)
580 * Convert bytes to base-64...
583 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
586 * Encode the up to 3 characters as 4 Base64 numbers...
590 *outptr
++ = base64
[(in
[0] & 255) >> 2];
595 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
597 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
614 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
616 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
629 *outptr
++ = base64
[in
[0] & 63];
635 * Return the encoded string...
643 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
648 const char * /* O - Date/time string */
649 httpGetDateString(time_t t
) /* I - UNIX time */
651 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
654 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
659 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
664 const char * /* O - Date/time string */
665 httpGetDateString2(time_t t
, /* I - UNIX time */
666 char *s
, /* I - String buffer */
667 int slen
) /* I - Size of string buffer */
669 struct tm
*tdate
; /* UNIX date/time data */
673 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
674 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
675 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
676 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
683 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
686 time_t /* O - UNIX time */
687 httpGetDateTime(const char *s
) /* I - Date/time string */
689 int i
; /* Looping var */
690 char mon
[16]; /* Abbreviated month name */
691 int day
, year
; /* Day of month and year */
692 int hour
, min
, sec
; /* Time */
693 int days
; /* Number of days since 1970 */
694 static const int normal_days
[] = /* Days to a month, normal years */
695 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
696 static const int leap_days
[] = /* Days to a month, leap years */
697 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
700 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
703 * Extract the date and time from the formatted string...
706 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
709 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
710 day
, mon
, year
, hour
, min
, sec
));
713 * Convert the month name to a number from 0 to 11.
716 for (i
= 0; i
< 12; i
++)
717 if (!strcasecmp(mon
, http_months
[i
]))
723 DEBUG_printf((" i=%d\n", i
));
726 * Now convert the date and time to a UNIX time value in seconds since
727 * 1970. We can't use mktime() since the timezone may not be UTC but
728 * the date/time string *is* UTC.
731 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
732 days
= leap_days
[i
] + day
- 1;
734 days
= normal_days
[i
] + day
- 1;
736 DEBUG_printf((" days=%d\n", days
));
738 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
739 ((year
- 1) / 4 - 492) - /* + leap days */
740 ((year
- 1) / 100 - 19) + /* - 100 year days */
741 ((year
- 1) / 400 - 4); /* + 400 year days */
743 DEBUG_printf((" days=%d\n", days
));
745 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
750 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
753 * This function is deprecated; use the httpSeparateURI() function instead.
759 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
760 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
761 char *username
, /* O - Username [1024] */
762 char *host
, /* O - Hostname [1024] */
763 int *port
, /* O - Port number to use */
764 char *resource
) /* O - Resource/filename [1024] */
766 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
767 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
773 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
776 * This function is deprecated; use the httpSeparateURI() function instead.
778 * @since CUPS 1.1.21@
783 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
784 char *scheme
, /* O - Scheme (http, https, etc.) */
785 int schemelen
, /* I - Size of scheme buffer */
786 char *username
, /* O - Username */
787 int usernamelen
, /* I - Size of username buffer */
788 char *host
, /* O - Hostname */
789 int hostlen
, /* I - Size of hostname buffer */
790 int *port
, /* O - Port number to use */
791 char *resource
, /* O - Resource/filename */
792 int resourcelen
) /* I - Size of resource buffer */
794 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
795 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
800 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
806 http_uri_status_t
/* O - Result of separation */
808 http_uri_coding_t decoding
, /* I - Decoding flags */
809 const char *uri
, /* I - Universal Resource Identifier */
810 char *scheme
, /* O - Scheme (http, https, etc.) */
811 int schemelen
, /* I - Size of scheme buffer */
812 char *username
, /* O - Username */
813 int usernamelen
, /* I - Size of username buffer */
814 char *host
, /* O - Hostname */
815 int hostlen
, /* I - Size of hostname buffer */
816 int *port
, /* O - Port number to use */
817 char *resource
, /* O - Resource/filename */
818 int resourcelen
) /* I - Size of resource buffer */
820 char *ptr
, /* Pointer into string... */
821 *end
; /* End of string */
822 const char *sep
; /* Separator character */
823 http_uri_status_t status
; /* Result of separation */
827 * Initialize everything to blank...
830 if (scheme
&& schemelen
> 0)
833 if (username
&& usernamelen
> 0)
836 if (host
&& hostlen
> 0)
842 if (resource
&& resourcelen
> 0)
846 * Range check input...
849 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
850 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
852 return (HTTP_URI_BAD_ARGUMENTS
);
855 return (HTTP_URI_BAD_URI
);
858 * Grab the scheme portion of the URI...
861 status
= HTTP_URI_OK
;
863 if (!strncmp(uri
, "//", 2))
866 * Workaround for HP IPP client bug...
869 strlcpy(scheme
, "ipp", schemelen
);
870 status
= HTTP_URI_MISSING_SCHEME
;
872 else if (*uri
== '/')
878 strlcpy(scheme
, "file", schemelen
);
879 status
= HTTP_URI_MISSING_SCHEME
;
884 * Standard URI with scheme...
887 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
888 *uri
&& *uri
!= ':' && ptr
< end
;)
889 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
899 return (HTTP_URI_BAD_SCHEME
);
906 * Set the default port number...
909 if (!strcmp(scheme
, "http"))
911 else if (!strcmp(scheme
, "https"))
913 else if (!strcmp(scheme
, "ipp"))
915 else if (!strcasecmp(scheme
, "lpd"))
917 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
919 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
920 status
= HTTP_URI_UNKNOWN_SCHEME
;
923 * Now see if we have a hostname...
926 if (!strncmp(uri
, "//", 2))
935 * Grab the username, if any...
938 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
941 * Get a username:password combo...
944 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
945 decoding
& HTTP_URI_CODING_USERNAME
);
950 return (HTTP_URI_BAD_USERNAME
);
957 * Then the hostname/IP address...
963 * Grab IPv6 address...
967 if (!strncmp(uri
, "v1.", 3))
968 uri
+= 3; /* Skip IPvN leader... */
970 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
971 decoding
& HTTP_URI_CODING_HOSTNAME
);
976 return (HTTP_URI_BAD_HOSTNAME
);
986 return (HTTP_URI_BAD_HOSTNAME
);
991 for (ptr
= host
; *ptr
; ptr
++)
995 * Convert zone separator to % and stop here...
1001 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1004 return (HTTP_URI_BAD_HOSTNAME
);
1010 * Validate the hostname or IPv4 address first...
1013 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1014 if (strchr(":?/", *ptr
))
1016 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1017 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1021 "!$&'()*+,;=\\", *ptr
))
1024 return (HTTP_URI_BAD_HOSTNAME
);
1028 * Then copy the hostname or IPv4 address to the buffer...
1031 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1032 decoding
& HTTP_URI_CODING_HOSTNAME
);
1037 return (HTTP_URI_BAD_HOSTNAME
);
1042 * Validate hostname for file scheme - only empty and localhost are
1046 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1049 return (HTTP_URI_BAD_HOSTNAME
);
1053 * See if we have a port number...
1059 * Yes, collect the port number...
1062 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1064 if (*uri
!= '/' && *uri
)
1067 return (HTTP_URI_BAD_PORT
);
1073 * The remaining portion is the resource string...
1076 if (*uri
== '?' || !*uri
)
1079 * Hostname but no path...
1082 status
= HTTP_URI_MISSING_RESOURCE
;
1086 * Copy any query string...
1090 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1091 decoding
& HTTP_URI_CODING_QUERY
);
1097 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1098 decoding
& HTTP_URI_CODING_RESOURCE
);
1100 if (uri
&& *uri
== '?')
1103 * Concatenate any query string...
1106 char *resptr
= resource
+ strlen(resource
);
1108 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1109 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1116 return (HTTP_URI_BAD_RESOURCE
);
1120 * Return the URI separation status...
1128 * 'httpStatus()' - Return a short string describing a HTTP status code.
1131 const char * /* O - String or NULL */
1132 httpStatus(http_status_t status
) /* I - HTTP status code */
1136 case HTTP_CONTINUE
:
1137 return ("Continue");
1138 case HTTP_SWITCHING_PROTOCOLS
:
1139 return ("Switching Protocols");
1144 case HTTP_ACCEPTED
:
1145 return ("Accepted");
1146 case HTTP_NO_CONTENT
:
1147 return ("No Content");
1148 case HTTP_MOVED_PERMANENTLY
:
1149 return ("Moved Permanently");
1150 case HTTP_SEE_OTHER
:
1151 return ("See Other");
1152 case HTTP_NOT_MODIFIED
:
1153 return ("Not Modified");
1154 case HTTP_BAD_REQUEST
:
1155 return ("Bad Request");
1156 case HTTP_UNAUTHORIZED
:
1157 return ("Unauthorized");
1158 case HTTP_FORBIDDEN
:
1159 return ("Forbidden");
1160 case HTTP_NOT_FOUND
:
1161 return ("Not Found");
1162 case HTTP_REQUEST_TOO_LARGE
:
1163 return ("Request Entity Too Large");
1164 case HTTP_URI_TOO_LONG
:
1165 return ("URI Too Long");
1166 case HTTP_UPGRADE_REQUIRED
:
1167 return ("Upgrade Required");
1168 case HTTP_NOT_IMPLEMENTED
:
1169 return ("Not Implemented");
1170 case HTTP_NOT_SUPPORTED
:
1171 return ("Not Supported");
1172 case HTTP_EXPECTATION_FAILED
:
1173 return ("Expectation Failed");
1181 #ifndef HAVE_HSTRERROR
1183 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1186 const char * /* O - Error string */
1187 _cups_hstrerror(int error
) /* I - Error number */
1189 static const char * const errors
[] = /* Error strings */
1194 "Unrecoverable lookup error.",
1195 "No data associated with name."
1199 if (error
< 0 || error
> 4)
1200 return ("Unknown hostname lookup error.");
1202 return (errors
[error
]);
1204 #endif /* !HAVE_HSTRERROR */
1208 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1211 char * /* O - Encoded URI */
1212 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1213 const char *src
, /* I - Source URI */
1214 size_t dstsize
) /* I - Size of destination buffer */
1216 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1222 * 'http_copy_decode()' - Copy and decode a URI.
1225 static const char * /* O - New source pointer or NULL on error */
1226 http_copy_decode(char *dst
, /* O - Destination buffer */
1227 const char *src
, /* I - Source pointer */
1228 int dstsize
, /* I - Destination size */
1229 const char *term
, /* I - Terminating characters */
1230 int decode
) /* I - Decode %-encoded values */
1232 char *ptr
, /* Pointer into buffer */
1233 *end
; /* End of buffer */
1234 int quoted
; /* Quoted character */
1238 * Copy the src to the destination until we hit a terminating character
1239 * or the end of the string.
1242 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1243 *src
&& (!term
|| !strchr(term
, *src
));
1247 if (*src
== '%' && decode
)
1249 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1252 * Grab a hex-encoded character...
1257 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1259 quoted
= (*src
- '0') << 4;
1263 quoted
|= tolower(*src
) - 'a' + 10;
1265 quoted
|= *src
- '0';
1272 * Bad hex-encoded character...
1290 * 'http_copy_encode()' - Copy and encode a URI.
1293 static char * /* O - End of current URI */
1294 http_copy_encode(char *dst
, /* O - Destination buffer */
1295 const char *src
, /* I - Source pointer */
1296 char *dstend
, /* I - End of destination buffer */
1297 const char *reserved
, /* I - Extra reserved characters */
1298 const char *term
, /* I - Terminating characters */
1299 int encode
) /* I - %-encode reserved chars? */
1301 static const char hex
[] = "0123456789ABCDEF";
1304 while (*src
&& dst
< dstend
)
1306 if (term
&& *src
== *term
)
1309 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1310 (reserved
&& strchr(reserved
, *src
))))
1313 * Hex encode reserved characters...
1316 if ((dst
+ 2) >= dstend
)
1320 *dst
++ = hex
[(*src
>> 4) & 15];
1321 *dst
++ = hex
[*src
& 15];
1339 * End of "$Id: http-support.c 6649 2007-07-11 21:46:42Z mike $".