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-2008 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
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.
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@
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@
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.
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@
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
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 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1093 if (*uri
!= '/' && *uri
)
1096 return (HTTP_URI_BAD_PORT
);
1102 * The remaining portion is the resource string...
1105 if (*uri
== '?' || !*uri
)
1108 * Hostname but no path...
1111 status
= HTTP_URI_MISSING_RESOURCE
;
1115 * Copy any query string...
1119 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1120 decoding
& HTTP_URI_CODING_QUERY
);
1126 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1127 decoding
& HTTP_URI_CODING_RESOURCE
);
1129 if (uri
&& *uri
== '?')
1132 * Concatenate any query string...
1135 char *resptr
= resource
+ strlen(resource
);
1137 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1138 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1145 return (HTTP_URI_BAD_RESOURCE
);
1149 * Return the URI separation status...
1157 * 'httpStatus()' - Return a short string describing a HTTP status code.
1159 * The returned string is localized to the current POSIX locale and is based
1160 * on the status strings defined in RFC 2616.
1163 const char * /* O - Localized status string */
1164 httpStatus(http_status_t status
) /* I - HTTP status code */
1166 const char *s
; /* Status string */
1167 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1170 if (!cg
->lang_default
)
1171 cg
->lang_default
= cupsLangDefault();
1175 case HTTP_CONTINUE
:
1178 case HTTP_SWITCHING_PROTOCOLS
:
1179 s
= _("Switching Protocols");
1187 case HTTP_ACCEPTED
:
1190 case HTTP_NO_CONTENT
:
1191 s
= _("No Content");
1193 case HTTP_MOVED_PERMANENTLY
:
1194 s
= _("Moved Permanently");
1196 case HTTP_SEE_OTHER
:
1199 case HTTP_NOT_MODIFIED
:
1200 s
= _("Not Modified");
1202 case HTTP_BAD_REQUEST
:
1203 s
= _("Bad Request");
1205 case HTTP_UNAUTHORIZED
:
1206 s
= _("Unauthorized");
1208 case HTTP_FORBIDDEN
:
1211 case HTTP_NOT_FOUND
:
1214 case HTTP_REQUEST_TOO_LARGE
:
1215 s
= _("Request Entity Too Large");
1217 case HTTP_URI_TOO_LONG
:
1218 s
= _("URI Too Long");
1220 case HTTP_UPGRADE_REQUIRED
:
1221 s
= _("Upgrade Required");
1223 case HTTP_NOT_IMPLEMENTED
:
1224 s
= _("Not Implemented");
1226 case HTTP_NOT_SUPPORTED
:
1227 s
= _("Not Supported");
1229 case HTTP_EXPECTATION_FAILED
:
1230 s
= _("Expectation Failed");
1232 case HTTP_SERVICE_UNAVAILABLE
:
1233 s
= _("Service Unavailable");
1241 return (_cupsLangString(cg
->lang_default
, s
));
1245 #ifndef HAVE_HSTRERROR
1247 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1250 const char * /* O - Error string */
1251 _cups_hstrerror(int error
) /* I - Error number */
1253 static const char * const errors
[] = /* Error strings */
1258 "Unrecoverable lookup error.",
1259 "No data associated with name."
1263 if (error
< 0 || error
> 4)
1264 return ("Unknown hostname lookup error.");
1266 return (errors
[error
]);
1268 #endif /* !HAVE_HSTRERROR */
1272 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1275 char * /* O - Encoded URI */
1276 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1277 const char *src
, /* I - Source URI */
1278 size_t dstsize
) /* I - Size of destination buffer */
1280 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1286 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1289 const char * /* O - Resolved URI */
1291 const char *uri
, /* I - DNS-SD URI */
1292 char *resolved_uri
, /* I - Buffer for resolved URI */
1293 size_t resolved_size
, /* I - Size of URI buffer */
1294 int log
) /* I - Log progress to stderr? */
1296 char scheme
[32], /* URI components... */
1302 http_uri_status_t status
; /* URI decode status */
1306 DEBUG_printf(("_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1307 "resolved_size=" CUPS_LLFMT
")\n", uri
, resolved_uri
,
1308 CUPS_LLCAST resolved_size
));
1311 * Get the device URI...
1315 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1316 sizeof(scheme
), userpass
, sizeof(userpass
),
1317 hostname
, sizeof(hostname
), &port
, resource
,
1318 sizeof(resource
))) < HTTP_URI_OK
)
1320 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1321 sizeof(scheme
), userpass
, sizeof(userpass
),
1322 hostname
, sizeof(hostname
), &port
, resource
,
1323 sizeof(resource
)) < HTTP_URI_OK
)
1327 _cupsLangPrintf(stderr
, _("Bad device URI \"%s\"!\n"), uri
);
1329 DEBUG_printf(("_httpResolveURI: httpSeparateURI returned %d!\n", status
));
1330 DEBUG_puts("_httpResolveURI: Returning NULL");
1335 * Resolve it as needed...
1338 if (strstr(hostname
, "._tcp"))
1341 DNSServiceRef ref
; /* DNS-SD service reference */
1342 char *regtype
, /* Pointer to type in hostname */
1343 *domain
; /* Pointer to domain in hostname */
1344 _http_uribuf_t uribuf
; /* URI buffer */
1347 * Separate the hostname into service name, registration type, and domain...
1350 for (regtype
= strstr(hostname
, "._tcp") - 2;
1353 if (regtype
[0] == '.' && regtype
[1] == '_')
1356 * Found ._servicetype in front of ._tcp...
1363 if (regtype
<= hostname
)
1365 DEBUG_puts("_httpResolveURI: Bad hostname, returning NULL");
1369 domain
= regtype
+ strlen(regtype
) - 1;
1370 if (domain
> regtype
&& *domain
== '.')
1373 for (domain
= strchr(regtype
, '.');
1375 domain
= strchr(domain
+ 1, '.'))
1376 if (domain
[1] != '_')
1382 uribuf
.buffer
= resolved_uri
;
1383 uribuf
.bufsize
= resolved_size
;
1385 resolved_uri
[0] = '\0';
1387 DEBUG_printf(("_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1388 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1391 fputs("STATE: +connecting-to-device\n", stderr
);
1392 _cupsLangPrintf(stderr
, _("INFO: Looking for \"%s\"...\n"), hostname
);
1395 if (DNSServiceResolve(&ref
, 0, 0, hostname
, regtype
, domain
,
1397 &uribuf
) == kDNSServiceErr_NoError
)
1399 if (DNSServiceProcessResult(ref
) != kDNSServiceErr_NoError
&&
1405 DNSServiceRefDeallocate(ref
);
1411 fputs("STATE: -connecting-to-device\n", stderr
);
1415 * No DNS-SD support...
1419 #endif /* HAVE_DNSSD */
1422 _cupsLangPuts(stderr
, _("Unable to find printer!\n"));
1425 DEBUG_printf(("_httpResolveURI: Returning \"%s\"\n", uri
));
1432 * 'http_copy_decode()' - Copy and decode a URI.
1435 static const char * /* O - New source pointer or NULL on error */
1436 http_copy_decode(char *dst
, /* O - Destination buffer */
1437 const char *src
, /* I - Source pointer */
1438 int dstsize
, /* I - Destination size */
1439 const char *term
, /* I - Terminating characters */
1440 int decode
) /* I - Decode %-encoded values */
1442 char *ptr
, /* Pointer into buffer */
1443 *end
; /* End of buffer */
1444 int quoted
; /* Quoted character */
1448 * Copy the src to the destination until we hit a terminating character
1449 * or the end of the string.
1452 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1453 *src
&& (!term
|| !strchr(term
, *src
));
1457 if (*src
== '%' && decode
)
1459 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1462 * Grab a hex-encoded character...
1467 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1469 quoted
= (*src
- '0') << 4;
1473 quoted
|= tolower(*src
) - 'a' + 10;
1475 quoted
|= *src
- '0';
1482 * Bad hex-encoded character...
1500 * 'http_copy_encode()' - Copy and encode a URI.
1503 static char * /* O - End of current URI */
1504 http_copy_encode(char *dst
, /* O - Destination buffer */
1505 const char *src
, /* I - Source pointer */
1506 char *dstend
, /* I - End of destination buffer */
1507 const char *reserved
, /* I - Extra reserved characters */
1508 const char *term
, /* I - Terminating characters */
1509 int encode
) /* I - %-encode reserved chars? */
1511 static const char hex
[] = "0123456789ABCDEF";
1514 while (*src
&& dst
< dstend
)
1516 if (term
&& *src
== *term
)
1519 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1520 (reserved
&& strchr(reserved
, *src
))))
1523 * Hex encode reserved characters...
1526 if ((dst
+ 2) >= dstend
)
1530 *dst
++ = hex
[(*src
>> 4) & 15];
1531 *dst
++ = hex
[*src
& 15];
1550 * 'resolve_callback()' - Build a device URI for the given service name.
1555 DNSServiceRef sdRef
, /* I - Service reference */
1556 DNSServiceFlags flags
, /* I - Results flags */
1557 uint32_t interfaceIndex
, /* I - Interface number */
1558 DNSServiceErrorType errorCode
, /* I - Error, if any */
1559 const char *fullName
, /* I - Full service name */
1560 const char *hostTarget
, /* I - Hostname */
1561 uint16_t port
, /* I - Port number */
1562 uint16_t txtLen
, /* I - Length of TXT record */
1563 const unsigned char *txtRecord
, /* I - TXT record data */
1564 void *context
) /* I - Pointer to URI buffer */
1566 const char *scheme
; /* URI scheme */
1567 char rp
[257]; /* Remote printer */
1568 const void *value
; /* Value from TXT record */
1569 uint8_t valueLen
; /* Length of value */
1570 _http_uribuf_t
*uribuf
; /* URI buffer */
1573 DEBUG_printf(("resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1574 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1575 "txtLen=%u, txtRecord=%p, context=%p)\n", sdRef
, flags
,
1576 interfaceIndex
, errorCode
, fullName
, hostTarget
, port
, txtLen
,
1577 txtRecord
, context
));
1580 * Figure out the scheme from the full name...
1583 if (strstr(fullName
, "._ipp"))
1585 else if (strstr(fullName
, "._printer."))
1587 else if (strstr(fullName
, "._pdl-datastream."))
1590 scheme
= "riousbprint";
1593 * Extract the "remote printer" key from the TXT record...
1596 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "rp",
1597 &valueLen
)) != NULL
)
1600 * Convert to resource by concatenating with a leading "/"...
1604 memcpy(rp
+ 1, value
, valueLen
);
1605 rp
[valueLen
+ 1] = '\0';
1611 * Assemble the final device URI...
1614 uribuf
= (_http_uribuf_t
*)context
;
1616 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, uribuf
->bufsize
, scheme
,
1617 NULL
, hostTarget
, ntohs(port
), rp
);
1619 DEBUG_printf(("resolve_callback: Resolved URI is \"%s\"...\n",
1622 #endif /* HAVE_DNSSD */
1626 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".