2 * "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $"
4 * HTTP support routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2009 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 * _httpResolveURI() - Resolve a DNS-SD URI.
41 * http_copy_decode() - Copy and decode a URI.
42 * http_copy_encode() - Copy and encode a URI.
43 * resolve_callback() - Build a device URI for the given service name.
47 * Include necessary headers...
55 #endif /* HAVE_DNSSD */
62 typedef struct _http_uribuf_s
/* URI buffer */
64 char *buffer
; /* Pointer to buffer */
65 size_t bufsize
; /* Size of buffer */
73 static const char * const http_days
[7] =
83 static const char * const http_months
[12] =
104 static const char *http_copy_decode(char *dst
, const char *src
,
105 int dstsize
, const char *term
,
107 static char *http_copy_encode(char *dst
, const char *src
,
108 char *dstend
, const char *reserved
,
109 const char *term
, int encode
);
111 static void resolve_callback(DNSServiceRef sdRef
,
112 DNSServiceFlags flags
,
113 uint32_t interfaceIndex
,
114 DNSServiceErrorType errorCode
,
115 const char *fullName
,
116 const char *hostTarget
,
117 uint16_t port
, uint16_t txtLen
,
118 const unsigned char *txtRecord
,
120 #endif /* HAVE_DNSSD */
124 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
127 * This function escapes reserved characters in the URI depending on the
128 * value of the "encoding" argument. You should use this function in
129 * place of traditional string functions whenever you need to create a
132 * @since CUPS 1.2/Mac OS X 10.5@
135 http_uri_status_t
/* O - URI status */
137 http_uri_coding_t encoding
, /* I - Encoding flags */
138 char *uri
, /* I - URI buffer */
139 int urilen
, /* I - Size of URI buffer */
140 const char *scheme
, /* I - Scheme name */
141 const char *username
, /* I - Username */
142 const char *host
, /* I - Hostname or address */
143 int port
, /* I - Port number */
144 const char *resource
) /* I - Resource */
146 char *ptr
, /* Pointer into URI buffer */
147 *end
; /* End of URI buffer */
151 * Range check input...
154 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
159 return (HTTP_URI_BAD_ARGUMENTS
);
163 * Assemble the URI starting with the scheme...
166 end
= uri
+ urilen
- 1;
167 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
170 goto assemble_overflow
;
172 if (!strcmp(scheme
, "mailto"))
175 * mailto: only has :, no //...
181 goto assemble_overflow
;
186 * Schemes other than mailto: all have //...
196 goto assemble_overflow
;
200 * Next the username and hostname, if any...
205 if (username
&& *username
)
208 * Add username@ first...
211 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
212 encoding
& HTTP_URI_CODING_USERNAME
);
215 goto assemble_overflow
;
220 goto assemble_overflow
;
224 * Then add the hostname. Since IPv6 is a particular pain to deal
225 * with, we have several special cases to deal with. If we get
226 * an IPv6 address with brackets around it, assume it is already in
227 * URI format. Since DNS-SD service names can sometimes look like
228 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
232 if (host
[0] != '[' && strchr(host
, ':') && !strstr(host
, "._tcp"))
235 * We have a raw IPv6 address...
238 if (strchr(host
, '%'))
241 * We have a link-local address, add "[v1." prefix...
252 goto assemble_overflow
;
257 * We have a normal address, add "[" prefix...
263 goto assemble_overflow
;
267 * Copy the rest of the IPv6 address, and terminate with "]".
270 while (ptr
< end
&& *host
)
274 *ptr
++ = '+'; /* Convert zone separator */
282 goto assemble_overflow
;
287 goto assemble_overflow
;
292 * Otherwise, just copy the host string...
295 ptr
= http_copy_encode(ptr
, host
, end
, ":/?#[]@\\", NULL
,
296 encoding
& HTTP_URI_CODING_HOSTNAME
);
299 goto assemble_overflow
;
303 * Finish things off with the port number...
308 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
312 goto assemble_overflow
;
317 * Last but not least, add the resource string...
322 char *query
; /* Pointer to query string */
326 * Copy the resource string up to the query string if present...
329 query
= strchr(resource
, '?');
330 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
331 encoding
& HTTP_URI_CODING_RESOURCE
);
333 goto assemble_overflow
;
338 * Copy query string without encoding...
341 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
342 encoding
& HTTP_URI_CODING_QUERY
);
344 goto assemble_overflow
;
350 goto assemble_overflow
;
353 * Nul-terminate the URI buffer and return with no errors...
358 return (HTTP_URI_OK
);
361 * Clear the URI string and return an overflow error; I don't usually
362 * like goto's, but in this case it makes sense...
368 return (HTTP_URI_OVERFLOW
);
373 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
374 * components with a formatted resource.
376 * This function creates a formatted version of the resource string
377 * argument "resourcef" and escapes reserved characters in the URI
378 * depending on the value of the "encoding" argument. You should use
379 * this function in place of traditional string functions whenever
380 * you need to create a URI string.
382 * @since CUPS 1.2/Mac OS X 10.5@
385 http_uri_status_t
/* O - URI status */
387 http_uri_coding_t encoding
, /* I - Encoding flags */
388 char *uri
, /* I - URI buffer */
389 int urilen
, /* I - Size of URI buffer */
390 const char *scheme
, /* I - Scheme name */
391 const char *username
, /* I - Username */
392 const char *host
, /* I - Hostname or address */
393 int port
, /* I - Port number */
394 const char *resourcef
, /* I - Printf-style resource */
395 ...) /* I - Additional arguments as needed */
397 va_list ap
; /* Pointer to additional arguments */
398 char resource
[1024]; /* Formatted resource string */
399 int bytes
; /* Bytes in formatted string */
403 * Range check input...
406 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
411 return (HTTP_URI_BAD_ARGUMENTS
);
415 * Format the resource string and assemble the URI...
418 va_start(ap
, resourcef
);
419 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
422 if (bytes
>= sizeof(resource
))
425 return (HTTP_URI_OVERFLOW
);
428 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
434 * 'httpDecode64()' - Base64-decode a string.
436 * This function is deprecated. Use the httpDecode64_2() function instead
437 * which provides buffer length arguments.
442 char * /* O - Decoded string */
443 httpDecode64(char *out
, /* I - String to write to */
444 const char *in
) /* I - String to read from */
446 int outlen
; /* Output buffer length */
450 * Use the old maximum buffer size for binary compatibility...
455 return (httpDecode64_2(out
, &outlen
, in
));
460 * 'httpDecode64_2()' - Base64-decode a string.
462 * @since CUPS 1.1.21/Mac OS X 10.4@
465 char * /* O - Decoded string */
466 httpDecode64_2(char *out
, /* I - String to write to */
467 int *outlen
, /* IO - Size of output string */
468 const char *in
) /* I - String to read from */
470 int pos
, /* Bit position */
471 base64
; /* Value of this character */
472 char *outptr
, /* Output pointer */
473 *outend
; /* End of output buffer */
477 * Range check input...
480 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
492 * Convert from base-64 to bytes...
495 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
498 * Decode this character into a number from 0 to 63...
501 if (*in
>= 'A' && *in
<= 'Z')
503 else if (*in
>= 'a' && *in
<= 'z')
504 base64
= *in
- 'a' + 26;
505 else if (*in
>= '0' && *in
<= '9')
506 base64
= *in
- '0' + 52;
517 * Store the result in the appropriate chars...
524 *outptr
= base64
<< 2;
529 *outptr
++ |= (base64
>> 4) & 3;
531 *outptr
= (base64
<< 4) & 255;
536 *outptr
++ |= (base64
>> 2) & 15;
538 *outptr
= (base64
<< 6) & 255;
552 * Return the decoded string and size...
555 *outlen
= (int)(outptr
- out
);
562 * 'httpEncode64()' - Base64-encode a string.
564 * This function is deprecated. Use the httpEncode64_2() function instead
565 * which provides buffer length arguments.
570 char * /* O - Encoded string */
571 httpEncode64(char *out
, /* I - String to write to */
572 const char *in
) /* I - String to read from */
574 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
579 * 'httpEncode64_2()' - Base64-encode a string.
581 * @since CUPS 1.1.21/Mac OS X 10.4@
584 char * /* O - Encoded string */
585 httpEncode64_2(char *out
, /* I - String to write to */
586 int outlen
, /* I - Size of output string */
587 const char *in
, /* I - String to read from */
588 int inlen
) /* I - Size of input string */
590 char *outptr
, /* Output pointer */
591 *outend
; /* End of output buffer */
592 static const char base64
[] = /* Base64 characters... */
594 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
595 "abcdefghijklmnopqrstuvwxyz"
602 * Range check input...
605 if (!out
|| outlen
< 1 || !in
)
609 * Convert bytes to base-64...
612 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
615 * Encode the up to 3 characters as 4 Base64 numbers...
619 *outptr
++ = base64
[(in
[0] & 255) >> 2];
624 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
626 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
643 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
645 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
658 *outptr
++ = base64
[in
[0] & 63];
664 * Return the encoded string...
672 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
677 const char * /* O - Date/time string */
678 httpGetDateString(time_t t
) /* I - UNIX time */
680 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
683 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
688 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
690 * @since CUPS 1.2/Mac OS X 10.5@
693 const char * /* O - Date/time string */
694 httpGetDateString2(time_t t
, /* I - UNIX time */
695 char *s
, /* I - String buffer */
696 int slen
) /* I - Size of string buffer */
698 struct tm
*tdate
; /* UNIX date/time data */
702 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
703 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
704 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
705 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
712 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
715 time_t /* O - UNIX time */
716 httpGetDateTime(const char *s
) /* I - Date/time string */
718 int i
; /* Looping var */
719 char mon
[16]; /* Abbreviated month name */
720 int day
, year
; /* Day of month and year */
721 int hour
, min
, sec
; /* Time */
722 int days
; /* Number of days since 1970 */
723 static const int normal_days
[] = /* Days to a month, normal years */
724 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
725 static const int leap_days
[] = /* Days to a month, leap years */
726 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
729 DEBUG_printf(("httpGetDateTime(s=\"%s\")\n", s
));
732 * Extract the date and time from the formatted string...
735 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
738 DEBUG_printf((" day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d\n",
739 day
, mon
, year
, hour
, min
, sec
));
742 * Convert the month name to a number from 0 to 11.
745 for (i
= 0; i
< 12; i
++)
746 if (!strcasecmp(mon
, http_months
[i
]))
752 DEBUG_printf((" i=%d\n", i
));
755 * Now convert the date and time to a UNIX time value in seconds since
756 * 1970. We can't use mktime() since the timezone may not be UTC but
757 * the date/time string *is* UTC.
760 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
761 days
= leap_days
[i
] + day
- 1;
763 days
= normal_days
[i
] + day
- 1;
765 DEBUG_printf((" days=%d\n", days
));
767 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
768 ((year
- 1) / 4 - 492) - /* + leap days */
769 ((year
- 1) / 100 - 19) + /* - 100 year days */
770 ((year
- 1) / 400 - 4); /* + 400 year days */
772 DEBUG_printf((" days=%d\n", days
));
774 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
779 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
782 * This function is deprecated; use the httpSeparateURI() function instead.
788 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
789 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
790 char *username
, /* O - Username [1024] */
791 char *host
, /* O - Hostname [1024] */
792 int *port
, /* O - Port number to use */
793 char *resource
) /* O - Resource/filename [1024] */
795 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
796 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
802 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
805 * This function is deprecated; use the httpSeparateURI() function instead.
807 * @since CUPS 1.1.21/Mac OS X 10.4@
812 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
813 char *scheme
, /* O - Scheme (http, https, etc.) */
814 int schemelen
, /* I - Size of scheme buffer */
815 char *username
, /* O - Username */
816 int usernamelen
, /* I - Size of username buffer */
817 char *host
, /* O - Hostname */
818 int hostlen
, /* I - Size of hostname buffer */
819 int *port
, /* O - Port number to use */
820 char *resource
, /* O - Resource/filename */
821 int resourcelen
) /* I - Size of resource buffer */
823 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
824 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
829 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
832 * @since CUPS 1.2/Mac OS X 10.5@
835 http_uri_status_t
/* O - Result of separation */
837 http_uri_coding_t decoding
, /* I - Decoding flags */
838 const char *uri
, /* I - Universal Resource Identifier */
839 char *scheme
, /* O - Scheme (http, https, etc.) */
840 int schemelen
, /* I - Size of scheme buffer */
841 char *username
, /* O - Username */
842 int usernamelen
, /* I - Size of username buffer */
843 char *host
, /* O - Hostname */
844 int hostlen
, /* I - Size of hostname buffer */
845 int *port
, /* O - Port number to use */
846 char *resource
, /* O - Resource/filename */
847 int resourcelen
) /* I - Size of resource buffer */
849 char *ptr
, /* Pointer into string... */
850 *end
; /* End of string */
851 const char *sep
; /* Separator character */
852 http_uri_status_t status
; /* Result of separation */
856 * Initialize everything to blank...
859 if (scheme
&& schemelen
> 0)
862 if (username
&& usernamelen
> 0)
865 if (host
&& hostlen
> 0)
871 if (resource
&& resourcelen
> 0)
875 * Range check input...
878 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
879 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
881 return (HTTP_URI_BAD_ARGUMENTS
);
884 return (HTTP_URI_BAD_URI
);
887 * Grab the scheme portion of the URI...
890 status
= HTTP_URI_OK
;
892 if (!strncmp(uri
, "//", 2))
895 * Workaround for HP IPP client bug...
898 strlcpy(scheme
, "ipp", schemelen
);
899 status
= HTTP_URI_MISSING_SCHEME
;
901 else if (*uri
== '/')
907 strlcpy(scheme
, "file", schemelen
);
908 status
= HTTP_URI_MISSING_SCHEME
;
913 * Standard URI with scheme...
916 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
917 *uri
&& *uri
!= ':' && ptr
< end
;)
918 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
928 return (HTTP_URI_BAD_SCHEME
);
935 * Set the default port number...
938 if (!strcmp(scheme
, "http"))
940 else if (!strcmp(scheme
, "https"))
942 else if (!strcmp(scheme
, "ipp"))
944 else if (!strcasecmp(scheme
, "lpd"))
946 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
948 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
949 status
= HTTP_URI_UNKNOWN_SCHEME
;
952 * Now see if we have a hostname...
955 if (!strncmp(uri
, "//", 2))
964 * Grab the username, if any...
967 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
970 * Get a username:password combo...
973 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
974 decoding
& HTTP_URI_CODING_USERNAME
);
979 return (HTTP_URI_BAD_USERNAME
);
986 * Then the hostname/IP address...
992 * Grab IPv6 address...
996 if (!strncmp(uri
, "v1.", 3))
997 uri
+= 3; /* Skip IPvN leader... */
999 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
1000 decoding
& HTTP_URI_CODING_HOSTNAME
);
1005 return (HTTP_URI_BAD_HOSTNAME
);
1015 return (HTTP_URI_BAD_HOSTNAME
);
1020 for (ptr
= host
; *ptr
; ptr
++)
1024 * Convert zone separator to % and stop here...
1030 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1033 return (HTTP_URI_BAD_HOSTNAME
);
1039 * Validate the hostname or IPv4 address first...
1042 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1043 if (strchr(":?/", *ptr
))
1045 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1046 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1050 "!$&'()*+,;=\\", *ptr
))
1053 return (HTTP_URI_BAD_HOSTNAME
);
1057 * Then copy the hostname or IPv4 address to the buffer...
1060 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1061 decoding
& HTTP_URI_CODING_HOSTNAME
);
1066 return (HTTP_URI_BAD_HOSTNAME
);
1071 * Validate hostname for file scheme - only empty and localhost are
1075 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1078 return (HTTP_URI_BAD_HOSTNAME
);
1082 * See if we have a port number...
1088 * Yes, collect the port number...
1091 if (!isdigit(uri
[1] & 255))
1094 return (HTTP_URI_BAD_PORT
);
1097 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1099 if (*uri
!= '/' && *uri
)
1102 return (HTTP_URI_BAD_PORT
);
1108 * The remaining portion is the resource string...
1111 if (*uri
== '?' || !*uri
)
1114 * Hostname but no path...
1117 status
= HTTP_URI_MISSING_RESOURCE
;
1121 * Copy any query string...
1125 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1126 decoding
& HTTP_URI_CODING_QUERY
);
1132 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1133 decoding
& HTTP_URI_CODING_RESOURCE
);
1135 if (uri
&& *uri
== '?')
1138 * Concatenate any query string...
1141 char *resptr
= resource
+ strlen(resource
);
1143 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1144 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1151 return (HTTP_URI_BAD_RESOURCE
);
1155 * Return the URI separation status...
1163 * 'httpStatus()' - Return a short string describing a HTTP status code.
1165 * The returned string is localized to the current POSIX locale and is based
1166 * on the status strings defined in RFC 2616.
1169 const char * /* O - Localized status string */
1170 httpStatus(http_status_t status
) /* I - HTTP status code */
1172 const char *s
; /* Status string */
1173 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1176 if (!cg
->lang_default
)
1177 cg
->lang_default
= cupsLangDefault();
1181 case HTTP_CONTINUE
:
1184 case HTTP_SWITCHING_PROTOCOLS
:
1185 s
= _("Switching Protocols");
1193 case HTTP_ACCEPTED
:
1196 case HTTP_NO_CONTENT
:
1197 s
= _("No Content");
1199 case HTTP_MOVED_PERMANENTLY
:
1200 s
= _("Moved Permanently");
1202 case HTTP_SEE_OTHER
:
1205 case HTTP_NOT_MODIFIED
:
1206 s
= _("Not Modified");
1208 case HTTP_BAD_REQUEST
:
1209 s
= _("Bad Request");
1211 case HTTP_UNAUTHORIZED
:
1212 s
= _("Unauthorized");
1214 case HTTP_FORBIDDEN
:
1217 case HTTP_NOT_FOUND
:
1220 case HTTP_REQUEST_TOO_LARGE
:
1221 s
= _("Request Entity Too Large");
1223 case HTTP_URI_TOO_LONG
:
1224 s
= _("URI Too Long");
1226 case HTTP_UPGRADE_REQUIRED
:
1227 s
= _("Upgrade Required");
1229 case HTTP_NOT_IMPLEMENTED
:
1230 s
= _("Not Implemented");
1232 case HTTP_NOT_SUPPORTED
:
1233 s
= _("Not Supported");
1235 case HTTP_EXPECTATION_FAILED
:
1236 s
= _("Expectation Failed");
1238 case HTTP_SERVICE_UNAVAILABLE
:
1239 s
= _("Service Unavailable");
1247 return (_cupsLangString(cg
->lang_default
, s
));
1251 #ifndef HAVE_HSTRERROR
1253 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1256 const char * /* O - Error string */
1257 _cups_hstrerror(int error
) /* I - Error number */
1259 static const char * const errors
[] = /* Error strings */
1264 "Unrecoverable lookup error.",
1265 "No data associated with name."
1269 if (error
< 0 || error
> 4)
1270 return ("Unknown hostname lookup error.");
1272 return (errors
[error
]);
1274 #endif /* !HAVE_HSTRERROR */
1278 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1281 char * /* O - Encoded URI */
1282 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1283 const char *src
, /* I - Source URI */
1284 size_t dstsize
) /* I - Size of destination buffer */
1286 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1292 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1295 const char * /* O - Resolved URI */
1297 const char *uri
, /* I - DNS-SD URI */
1298 char *resolved_uri
, /* I - Buffer for resolved URI */
1299 size_t resolved_size
, /* I - Size of URI buffer */
1300 int logit
) /* I - Log progress to stderr? */
1302 char scheme
[32], /* URI components... */
1308 http_uri_status_t status
; /* URI decode status */
1312 DEBUG_printf(("_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1313 "resolved_size=" CUPS_LLFMT
")\n", uri
, resolved_uri
,
1314 CUPS_LLCAST resolved_size
));
1317 * Get the device URI...
1321 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1322 sizeof(scheme
), userpass
, sizeof(userpass
),
1323 hostname
, sizeof(hostname
), &port
, resource
,
1324 sizeof(resource
))) < HTTP_URI_OK
)
1326 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1327 sizeof(scheme
), userpass
, sizeof(userpass
),
1328 hostname
, sizeof(hostname
), &port
, resource
,
1329 sizeof(resource
)) < HTTP_URI_OK
)
1333 _cupsLangPrintf(stderr
, _("Bad device URI \"%s\"!\n"), uri
);
1335 DEBUG_printf(("_httpResolveURI: httpSeparateURI returned %d!\n", status
));
1336 DEBUG_puts("_httpResolveURI: Returning NULL");
1341 * Resolve it as needed...
1344 if (strstr(hostname
, "._tcp"))
1347 DNSServiceRef ref
; /* DNS-SD service reference */
1348 char *regtype
, /* Pointer to type in hostname */
1349 *domain
; /* Pointer to domain in hostname */
1350 _http_uribuf_t uribuf
; /* URI buffer */
1353 * Separate the hostname into service name, registration type, and domain...
1356 for (regtype
= strstr(hostname
, "._tcp") - 2;
1359 if (regtype
[0] == '.' && regtype
[1] == '_')
1362 * Found ._servicetype in front of ._tcp...
1369 if (regtype
<= hostname
)
1371 DEBUG_puts("_httpResolveURI: Bad hostname, returning NULL");
1375 domain
= regtype
+ strlen(regtype
) - 1;
1376 if (domain
> regtype
&& *domain
== '.')
1379 for (domain
= strchr(regtype
, '.');
1381 domain
= strchr(domain
+ 1, '.'))
1382 if (domain
[1] != '_')
1388 uribuf
.buffer
= resolved_uri
;
1389 uribuf
.bufsize
= resolved_size
;
1391 resolved_uri
[0] = '\0';
1393 DEBUG_printf(("_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1394 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1397 fputs("STATE: +connecting-to-device\n", stderr
);
1398 fprintf(stderr
, "DEBUG: Resolving %s, regtype=%s, domain=%s...\n",
1399 hostname
, regtype
, domain
);
1400 _cupsLangPuts(stderr
, _("INFO: Looking for printer...\n"));
1403 if (DNSServiceResolve(&ref
, 0, 0, hostname
, regtype
, domain
,
1405 &uribuf
) == kDNSServiceErr_NoError
)
1407 if (DNSServiceProcessResult(ref
) != kDNSServiceErr_NoError
&&
1413 DNSServiceRefDeallocate(ref
);
1419 fputs("STATE: -connecting-to-device\n", stderr
);
1423 * No DNS-SD support...
1427 #endif /* HAVE_DNSSD */
1430 _cupsLangPuts(stderr
, _("Unable to find printer!\n"));
1433 DEBUG_printf(("_httpResolveURI: Returning \"%s\"\n", uri
));
1440 * 'http_copy_decode()' - Copy and decode a URI.
1443 static const char * /* O - New source pointer or NULL on error */
1444 http_copy_decode(char *dst
, /* O - Destination buffer */
1445 const char *src
, /* I - Source pointer */
1446 int dstsize
, /* I - Destination size */
1447 const char *term
, /* I - Terminating characters */
1448 int decode
) /* I - Decode %-encoded values */
1450 char *ptr
, /* Pointer into buffer */
1451 *end
; /* End of buffer */
1452 int quoted
; /* Quoted character */
1456 * Copy the src to the destination until we hit a terminating character
1457 * or the end of the string.
1460 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1461 *src
&& (!term
|| !strchr(term
, *src
));
1465 if (*src
== '%' && decode
)
1467 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1470 * Grab a hex-encoded character...
1475 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1477 quoted
= (*src
- '0') << 4;
1481 quoted
|= tolower(*src
) - 'a' + 10;
1483 quoted
|= *src
- '0';
1490 * Bad hex-encoded character...
1508 * 'http_copy_encode()' - Copy and encode a URI.
1511 static char * /* O - End of current URI */
1512 http_copy_encode(char *dst
, /* O - Destination buffer */
1513 const char *src
, /* I - Source pointer */
1514 char *dstend
, /* I - End of destination buffer */
1515 const char *reserved
, /* I - Extra reserved characters */
1516 const char *term
, /* I - Terminating characters */
1517 int encode
) /* I - %-encode reserved chars? */
1519 static const char hex
[] = "0123456789ABCDEF";
1522 while (*src
&& dst
< dstend
)
1524 if (term
&& *src
== *term
)
1527 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1528 (reserved
&& strchr(reserved
, *src
))))
1531 * Hex encode reserved characters...
1534 if ((dst
+ 2) >= dstend
)
1538 *dst
++ = hex
[(*src
>> 4) & 15];
1539 *dst
++ = hex
[*src
& 15];
1558 * 'resolve_callback()' - Build a device URI for the given service name.
1563 DNSServiceRef sdRef
, /* I - Service reference */
1564 DNSServiceFlags flags
, /* I - Results flags */
1565 uint32_t interfaceIndex
, /* I - Interface number */
1566 DNSServiceErrorType errorCode
, /* I - Error, if any */
1567 const char *fullName
, /* I - Full service name */
1568 const char *hostTarget
, /* I - Hostname */
1569 uint16_t port
, /* I - Port number */
1570 uint16_t txtLen
, /* I - Length of TXT record */
1571 const unsigned char *txtRecord
, /* I - TXT record data */
1572 void *context
) /* I - Pointer to URI buffer */
1574 const char *scheme
; /* URI scheme */
1575 char rp
[257]; /* Remote printer */
1576 const void *value
; /* Value from TXT record */
1577 uint8_t valueLen
; /* Length of value */
1578 _http_uribuf_t
*uribuf
; /* URI buffer */
1581 DEBUG_printf(("resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1582 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1583 "txtLen=%u, txtRecord=%p, context=%p)\n", sdRef
, flags
,
1584 interfaceIndex
, errorCode
, fullName
, hostTarget
, port
, txtLen
,
1585 txtRecord
, context
));
1588 * Figure out the scheme from the full name...
1591 if (strstr(fullName
, "._ipp"))
1593 else if (strstr(fullName
, "._printer."))
1595 else if (strstr(fullName
, "._pdl-datastream."))
1598 scheme
= "riousbprint";
1601 * Extract the "remote printer" key from the TXT record...
1604 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "rp",
1605 &valueLen
)) != NULL
)
1608 * Convert to resource by concatenating with a leading "/"...
1612 memcpy(rp
+ 1, value
, valueLen
);
1613 rp
[valueLen
+ 1] = '\0';
1619 * Assemble the final device URI...
1622 uribuf
= (_http_uribuf_t
*)context
;
1624 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, uribuf
->bufsize
, scheme
,
1625 NULL
, hostTarget
, ntohs(port
), rp
);
1627 DEBUG_printf(("resolve_callback: Resolved URI is \"%s\"...\n",
1630 #endif /* HAVE_DNSSD */
1634 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".