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...
57 #endif /* HAVE_DNSSD */
64 typedef struct _http_uribuf_s
/* URI buffer */
66 char *buffer
; /* Pointer to buffer */
67 size_t bufsize
; /* Size of buffer */
75 static const char * const http_days
[7] =
85 static const char * const http_months
[12] =
106 static const char *http_copy_decode(char *dst
, const char *src
,
107 int dstsize
, const char *term
,
109 static char *http_copy_encode(char *dst
, const char *src
,
110 char *dstend
, const char *reserved
,
111 const char *term
, int encode
);
113 static void resolve_callback(DNSServiceRef sdRef
,
114 DNSServiceFlags flags
,
115 uint32_t interfaceIndex
,
116 DNSServiceErrorType errorCode
,
117 const char *fullName
,
118 const char *hostTarget
,
119 uint16_t port
, uint16_t txtLen
,
120 const unsigned char *txtRecord
,
122 #endif /* HAVE_DNSSD */
126 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
129 * This function escapes reserved characters in the URI depending on the
130 * value of the "encoding" argument. You should use this function in
131 * place of traditional string functions whenever you need to create a
134 * @since CUPS 1.2/Mac OS X 10.5@
137 http_uri_status_t
/* O - URI status */
139 http_uri_coding_t encoding
, /* I - Encoding flags */
140 char *uri
, /* I - URI buffer */
141 int urilen
, /* I - Size of URI buffer */
142 const char *scheme
, /* I - Scheme name */
143 const char *username
, /* I - Username */
144 const char *host
, /* I - Hostname or address */
145 int port
, /* I - Port number */
146 const char *resource
) /* I - Resource */
148 char *ptr
, /* Pointer into URI buffer */
149 *end
; /* End of URI buffer */
153 * Range check input...
156 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
161 return (HTTP_URI_BAD_ARGUMENTS
);
165 * Assemble the URI starting with the scheme...
168 end
= uri
+ urilen
- 1;
169 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
172 goto assemble_overflow
;
174 if (!strcmp(scheme
, "mailto"))
177 * mailto: only has :, no //...
183 goto assemble_overflow
;
188 * Schemes other than mailto: all have //...
198 goto assemble_overflow
;
202 * Next the username and hostname, if any...
207 if (username
&& *username
)
210 * Add username@ first...
213 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
214 encoding
& HTTP_URI_CODING_USERNAME
);
217 goto assemble_overflow
;
222 goto assemble_overflow
;
226 * Then add the hostname. Since IPv6 is a particular pain to deal
227 * with, we have several special cases to deal with. If we get
228 * an IPv6 address with brackets around it, assume it is already in
229 * URI format. Since DNS-SD service names can sometimes look like
230 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
234 if (host
[0] != '[' && strchr(host
, ':') && !strstr(host
, "._tcp"))
237 * We have a raw IPv6 address...
240 if (strchr(host
, '%'))
243 * We have a link-local address, add "[v1." prefix...
254 goto assemble_overflow
;
259 * We have a normal address, add "[" prefix...
265 goto assemble_overflow
;
269 * Copy the rest of the IPv6 address, and terminate with "]".
272 while (ptr
< end
&& *host
)
276 *ptr
++ = '+'; /* Convert zone separator */
284 goto assemble_overflow
;
289 goto assemble_overflow
;
294 * Otherwise, just copy the host string...
297 ptr
= http_copy_encode(ptr
, host
, end
, ":/?#[]@\\", NULL
,
298 encoding
& HTTP_URI_CODING_HOSTNAME
);
301 goto assemble_overflow
;
305 * Finish things off with the port number...
310 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
314 goto assemble_overflow
;
319 * Last but not least, add the resource string...
324 char *query
; /* Pointer to query string */
328 * Copy the resource string up to the query string if present...
331 query
= strchr(resource
, '?');
332 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
333 encoding
& HTTP_URI_CODING_RESOURCE
);
335 goto assemble_overflow
;
340 * Copy query string without encoding...
343 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
344 encoding
& HTTP_URI_CODING_QUERY
);
346 goto assemble_overflow
;
352 goto assemble_overflow
;
355 * Nul-terminate the URI buffer and return with no errors...
360 return (HTTP_URI_OK
);
363 * Clear the URI string and return an overflow error; I don't usually
364 * like goto's, but in this case it makes sense...
370 return (HTTP_URI_OVERFLOW
);
375 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
376 * components with a formatted resource.
378 * This function creates a formatted version of the resource string
379 * argument "resourcef" and escapes reserved characters in the URI
380 * depending on the value of the "encoding" argument. You should use
381 * this function in place of traditional string functions whenever
382 * you need to create a URI string.
384 * @since CUPS 1.2/Mac OS X 10.5@
387 http_uri_status_t
/* O - URI status */
389 http_uri_coding_t encoding
, /* I - Encoding flags */
390 char *uri
, /* I - URI buffer */
391 int urilen
, /* I - Size of URI buffer */
392 const char *scheme
, /* I - Scheme name */
393 const char *username
, /* I - Username */
394 const char *host
, /* I - Hostname or address */
395 int port
, /* I - Port number */
396 const char *resourcef
, /* I - Printf-style resource */
397 ...) /* I - Additional arguments as needed */
399 va_list ap
; /* Pointer to additional arguments */
400 char resource
[1024]; /* Formatted resource string */
401 int bytes
; /* Bytes in formatted string */
405 * Range check input...
408 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
413 return (HTTP_URI_BAD_ARGUMENTS
);
417 * Format the resource string and assemble the URI...
420 va_start(ap
, resourcef
);
421 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
424 if (bytes
>= sizeof(resource
))
427 return (HTTP_URI_OVERFLOW
);
430 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
436 * 'httpDecode64()' - Base64-decode a string.
438 * This function is deprecated. Use the httpDecode64_2() function instead
439 * which provides buffer length arguments.
444 char * /* O - Decoded string */
445 httpDecode64(char *out
, /* I - String to write to */
446 const char *in
) /* I - String to read from */
448 int outlen
; /* Output buffer length */
452 * Use the old maximum buffer size for binary compatibility...
457 return (httpDecode64_2(out
, &outlen
, in
));
462 * 'httpDecode64_2()' - Base64-decode a string.
464 * @since CUPS 1.1.21/Mac OS X 10.4@
467 char * /* O - Decoded string */
468 httpDecode64_2(char *out
, /* I - String to write to */
469 int *outlen
, /* IO - Size of output string */
470 const char *in
) /* I - String to read from */
472 int pos
, /* Bit position */
473 base64
; /* Value of this character */
474 char *outptr
, /* Output pointer */
475 *outend
; /* End of output buffer */
479 * Range check input...
482 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
494 * Convert from base-64 to bytes...
497 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
500 * Decode this character into a number from 0 to 63...
503 if (*in
>= 'A' && *in
<= 'Z')
505 else if (*in
>= 'a' && *in
<= 'z')
506 base64
= *in
- 'a' + 26;
507 else if (*in
>= '0' && *in
<= '9')
508 base64
= *in
- '0' + 52;
519 * Store the result in the appropriate chars...
526 *outptr
= base64
<< 2;
531 *outptr
++ |= (base64
>> 4) & 3;
533 *outptr
= (base64
<< 4) & 255;
538 *outptr
++ |= (base64
>> 2) & 15;
540 *outptr
= (base64
<< 6) & 255;
554 * Return the decoded string and size...
557 *outlen
= (int)(outptr
- out
);
564 * 'httpEncode64()' - Base64-encode a string.
566 * This function is deprecated. Use the httpEncode64_2() function instead
567 * which provides buffer length arguments.
572 char * /* O - Encoded string */
573 httpEncode64(char *out
, /* I - String to write to */
574 const char *in
) /* I - String to read from */
576 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
581 * 'httpEncode64_2()' - Base64-encode a string.
583 * @since CUPS 1.1.21/Mac OS X 10.4@
586 char * /* O - Encoded string */
587 httpEncode64_2(char *out
, /* I - String to write to */
588 int outlen
, /* I - Size of output string */
589 const char *in
, /* I - String to read from */
590 int inlen
) /* I - Size of input string */
592 char *outptr
, /* Output pointer */
593 *outend
; /* End of output buffer */
594 static const char base64
[] = /* Base64 characters... */
596 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
597 "abcdefghijklmnopqrstuvwxyz"
604 * Range check input...
607 if (!out
|| outlen
< 1 || !in
)
611 * Convert bytes to base-64...
614 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
617 * Encode the up to 3 characters as 4 Base64 numbers...
621 *outptr
++ = base64
[(in
[0] & 255) >> 2];
626 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
628 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
645 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
647 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
660 *outptr
++ = base64
[in
[0] & 63];
666 * Return the encoded string...
674 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
679 const char * /* O - Date/time string */
680 httpGetDateString(time_t t
) /* I - UNIX time */
682 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
685 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
690 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
692 * @since CUPS 1.2/Mac OS X 10.5@
695 const char * /* O - Date/time string */
696 httpGetDateString2(time_t t
, /* I - UNIX time */
697 char *s
, /* I - String buffer */
698 int slen
) /* I - Size of string buffer */
700 struct tm
*tdate
; /* UNIX date/time data */
704 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
705 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
706 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
707 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
714 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
717 time_t /* O - UNIX time */
718 httpGetDateTime(const char *s
) /* I - Date/time string */
720 int i
; /* Looping var */
721 char mon
[16]; /* Abbreviated month name */
722 int day
, year
; /* Day of month and year */
723 int hour
, min
, sec
; /* Time */
724 int days
; /* Number of days since 1970 */
725 static const int normal_days
[] = /* Days to a month, normal years */
726 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
727 static const int leap_days
[] = /* Days to a month, leap years */
728 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
731 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s
));
734 * Extract the date and time from the formatted string...
737 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
740 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
741 "min=%d, sec=%d", day
, mon
, year
, hour
, min
, sec
));
744 * Convert the month name to a number from 0 to 11.
747 for (i
= 0; i
< 12; i
++)
748 if (!strcasecmp(mon
, http_months
[i
]))
754 DEBUG_printf(("4httpGetDateTime: i=%d", i
));
757 * Now convert the date and time to a UNIX time value in seconds since
758 * 1970. We can't use mktime() since the timezone may not be UTC but
759 * the date/time string *is* UTC.
762 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
763 days
= leap_days
[i
] + day
- 1;
765 days
= normal_days
[i
] + day
- 1;
767 DEBUG_printf(("4httpGetDateTime: days=%d", days
));
769 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
770 ((year
- 1) / 4 - 492) - /* + leap days */
771 ((year
- 1) / 100 - 19) + /* - 100 year days */
772 ((year
- 1) / 400 - 4); /* + 400 year days */
774 DEBUG_printf(("4httpGetDateTime: days=%d\n", days
));
776 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
781 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
784 * This function is deprecated; use the httpSeparateURI() function instead.
790 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
791 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
792 char *username
, /* O - Username [1024] */
793 char *host
, /* O - Hostname [1024] */
794 int *port
, /* O - Port number to use */
795 char *resource
) /* O - Resource/filename [1024] */
797 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
798 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
804 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
807 * This function is deprecated; use the httpSeparateURI() function instead.
809 * @since CUPS 1.1.21/Mac OS X 10.4@
814 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
815 char *scheme
, /* O - Scheme (http, https, etc.) */
816 int schemelen
, /* I - Size of scheme buffer */
817 char *username
, /* O - Username */
818 int usernamelen
, /* I - Size of username buffer */
819 char *host
, /* O - Hostname */
820 int hostlen
, /* I - Size of hostname buffer */
821 int *port
, /* O - Port number to use */
822 char *resource
, /* O - Resource/filename */
823 int resourcelen
) /* I - Size of resource buffer */
825 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
826 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
831 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
834 * @since CUPS 1.2/Mac OS X 10.5@
837 http_uri_status_t
/* O - Result of separation */
839 http_uri_coding_t decoding
, /* I - Decoding flags */
840 const char *uri
, /* I - Universal Resource Identifier */
841 char *scheme
, /* O - Scheme (http, https, etc.) */
842 int schemelen
, /* I - Size of scheme buffer */
843 char *username
, /* O - Username */
844 int usernamelen
, /* I - Size of username buffer */
845 char *host
, /* O - Hostname */
846 int hostlen
, /* I - Size of hostname buffer */
847 int *port
, /* O - Port number to use */
848 char *resource
, /* O - Resource/filename */
849 int resourcelen
) /* I - Size of resource buffer */
851 char *ptr
, /* Pointer into string... */
852 *end
; /* End of string */
853 const char *sep
; /* Separator character */
854 http_uri_status_t status
; /* Result of separation */
858 * Initialize everything to blank...
861 if (scheme
&& schemelen
> 0)
864 if (username
&& usernamelen
> 0)
867 if (host
&& hostlen
> 0)
873 if (resource
&& resourcelen
> 0)
877 * Range check input...
880 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
881 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
883 return (HTTP_URI_BAD_ARGUMENTS
);
886 return (HTTP_URI_BAD_URI
);
889 * Grab the scheme portion of the URI...
892 status
= HTTP_URI_OK
;
894 if (!strncmp(uri
, "//", 2))
897 * Workaround for HP IPP client bug...
900 strlcpy(scheme
, "ipp", schemelen
);
901 status
= HTTP_URI_MISSING_SCHEME
;
903 else if (*uri
== '/')
909 strlcpy(scheme
, "file", schemelen
);
910 status
= HTTP_URI_MISSING_SCHEME
;
915 * Standard URI with scheme...
918 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
919 *uri
&& *uri
!= ':' && ptr
< end
;)
920 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
930 return (HTTP_URI_BAD_SCHEME
);
937 * Set the default port number...
940 if (!strcmp(scheme
, "http"))
942 else if (!strcmp(scheme
, "https"))
944 else if (!strcmp(scheme
, "ipp"))
946 else if (!strcasecmp(scheme
, "lpd"))
948 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
950 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
951 status
= HTTP_URI_UNKNOWN_SCHEME
;
954 * Now see if we have a hostname...
957 if (!strncmp(uri
, "//", 2))
966 * Grab the username, if any...
969 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
972 * Get a username:password combo...
975 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
976 decoding
& HTTP_URI_CODING_USERNAME
);
981 return (HTTP_URI_BAD_USERNAME
);
988 * Then the hostname/IP address...
994 * Grab IPv6 address...
998 if (!strncmp(uri
, "v1.", 3))
999 uri
+= 3; /* Skip IPvN leader... */
1001 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
1002 decoding
& HTTP_URI_CODING_HOSTNAME
);
1007 return (HTTP_URI_BAD_HOSTNAME
);
1017 return (HTTP_URI_BAD_HOSTNAME
);
1022 for (ptr
= host
; *ptr
; ptr
++)
1026 * Convert zone separator to % and stop here...
1032 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1035 return (HTTP_URI_BAD_HOSTNAME
);
1041 * Validate the hostname or IPv4 address first...
1044 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1045 if (strchr(":?/", *ptr
))
1047 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1048 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1052 "!$&'()*+,;=\\", *ptr
))
1055 return (HTTP_URI_BAD_HOSTNAME
);
1059 * Then copy the hostname or IPv4 address to the buffer...
1062 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1063 decoding
& HTTP_URI_CODING_HOSTNAME
);
1068 return (HTTP_URI_BAD_HOSTNAME
);
1073 * Validate hostname for file scheme - only empty and localhost are
1077 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1080 return (HTTP_URI_BAD_HOSTNAME
);
1084 * See if we have a port number...
1090 * Yes, collect the port number...
1093 if (!isdigit(uri
[1] & 255))
1096 return (HTTP_URI_BAD_PORT
);
1099 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1101 if (*uri
!= '/' && *uri
)
1104 return (HTTP_URI_BAD_PORT
);
1110 * The remaining portion is the resource string...
1113 if (*uri
== '?' || !*uri
)
1116 * Hostname but no path...
1119 status
= HTTP_URI_MISSING_RESOURCE
;
1123 * Copy any query string...
1127 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1128 decoding
& HTTP_URI_CODING_QUERY
);
1134 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1135 decoding
& HTTP_URI_CODING_RESOURCE
);
1137 if (uri
&& *uri
== '?')
1140 * Concatenate any query string...
1143 char *resptr
= resource
+ strlen(resource
);
1145 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1146 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1153 return (HTTP_URI_BAD_RESOURCE
);
1157 * Return the URI separation status...
1165 * 'httpStatus()' - Return a short string describing a HTTP status code.
1167 * The returned string is localized to the current POSIX locale and is based
1168 * on the status strings defined in RFC 2616.
1171 const char * /* O - Localized status string */
1172 httpStatus(http_status_t status
) /* I - HTTP status code */
1174 const char *s
; /* Status string */
1175 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1178 if (!cg
->lang_default
)
1179 cg
->lang_default
= cupsLangDefault();
1183 case HTTP_CONTINUE
:
1186 case HTTP_SWITCHING_PROTOCOLS
:
1187 s
= _("Switching Protocols");
1195 case HTTP_ACCEPTED
:
1198 case HTTP_NO_CONTENT
:
1199 s
= _("No Content");
1201 case HTTP_MOVED_PERMANENTLY
:
1202 s
= _("Moved Permanently");
1204 case HTTP_SEE_OTHER
:
1207 case HTTP_NOT_MODIFIED
:
1208 s
= _("Not Modified");
1210 case HTTP_BAD_REQUEST
:
1211 s
= _("Bad Request");
1213 case HTTP_UNAUTHORIZED
:
1214 case HTTP_AUTHORIZATION_CANCELED
:
1215 s
= _("Unauthorized");
1217 case HTTP_FORBIDDEN
:
1220 case HTTP_NOT_FOUND
:
1223 case HTTP_REQUEST_TOO_LARGE
:
1224 s
= _("Request Entity Too Large");
1226 case HTTP_URI_TOO_LONG
:
1227 s
= _("URI Too Long");
1229 case HTTP_UPGRADE_REQUIRED
:
1230 s
= _("Upgrade Required");
1232 case HTTP_NOT_IMPLEMENTED
:
1233 s
= _("Not Implemented");
1235 case HTTP_NOT_SUPPORTED
:
1236 s
= _("Not Supported");
1238 case HTTP_EXPECTATION_FAILED
:
1239 s
= _("Expectation Failed");
1241 case HTTP_SERVICE_UNAVAILABLE
:
1242 s
= _("Service Unavailable");
1244 case HTTP_SERVER_ERROR
:
1245 s
= _("Internal Server Error");
1253 return (_cupsLangString(cg
->lang_default
, s
));
1257 #ifndef HAVE_HSTRERROR
1259 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1262 const char * /* O - Error string */
1263 _cups_hstrerror(int error
) /* I - Error number */
1265 static const char * const errors
[] = /* Error strings */
1270 "Unrecoverable lookup error.",
1271 "No data associated with name."
1275 if (error
< 0 || error
> 4)
1276 return ("Unknown hostname lookup error.");
1278 return (errors
[error
]);
1280 #endif /* !HAVE_HSTRERROR */
1284 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1287 char * /* O - Encoded URI */
1288 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1289 const char *src
, /* I - Source URI */
1290 size_t dstsize
) /* I - Size of destination buffer */
1292 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1298 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1301 const char * /* O - Resolved URI */
1303 const char *uri
, /* I - DNS-SD URI */
1304 char *resolved_uri
, /* I - Buffer for resolved URI */
1305 size_t resolved_size
, /* I - Size of URI buffer */
1306 int logit
) /* I - Log progress to stderr? */
1308 char scheme
[32], /* URI components... */
1314 http_uri_status_t status
; /* URI decode status */
1318 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1319 "resolved_size=" CUPS_LLFMT
")", uri
, resolved_uri
,
1320 CUPS_LLCAST resolved_size
));
1323 * Get the device URI...
1327 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1328 sizeof(scheme
), userpass
, sizeof(userpass
),
1329 hostname
, sizeof(hostname
), &port
, resource
,
1330 sizeof(resource
))) < HTTP_URI_OK
)
1332 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1333 sizeof(scheme
), userpass
, sizeof(userpass
),
1334 hostname
, sizeof(hostname
), &port
, resource
,
1335 sizeof(resource
)) < HTTP_URI_OK
)
1339 _cupsLangPrintf(stderr
, _("Bad device URI \"%s\"\n"), uri
);
1341 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status
));
1342 DEBUG_puts("5_httpResolveURI: Returning NULL");
1347 * Resolve it as needed...
1350 if (strstr(hostname
, "._tcp"))
1353 DNSServiceRef ref
, /* DNS-SD master service reference */
1354 domainref
, /* DNS-SD service reference for domain */
1355 localref
; /* DNS-SD service reference for .local */
1356 int domainsent
= 0; /* Send the domain resolve? */
1357 char *regtype
, /* Pointer to type in hostname */
1358 *domain
; /* Pointer to domain in hostname */
1359 _http_uribuf_t uribuf
; /* URI buffer */
1360 struct pollfd polldata
; /* Polling data */
1364 fprintf(stderr
, "DEBUG: Resolving \"%s\"...\n", hostname
);
1367 * Separate the hostname into service name, registration type, and domain...
1370 for (regtype
= strstr(hostname
, "._tcp") - 2;
1373 if (regtype
[0] == '.' && regtype
[1] == '_')
1376 * Found ._servicetype in front of ._tcp...
1383 if (regtype
<= hostname
)
1385 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1389 for (domain
= strchr(regtype
, '.');
1391 domain
= strchr(domain
+ 1, '.'))
1392 if (domain
[1] != '_')
1398 uribuf
.buffer
= resolved_uri
;
1399 uribuf
.bufsize
= resolved_size
;
1401 resolved_uri
[0] = '\0';
1403 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1404 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1407 fputs("STATE: +connecting-to-device\n", stderr
);
1408 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1409 "domain=\"local.\"...\n", hostname
, regtype
);
1414 if (DNSServiceCreateConnection(&ref
) == kDNSServiceErr_NoError
)
1417 if (DNSServiceResolve(&localref
, kDNSServiceFlagsShareConnection
, 0,
1418 hostname
, regtype
, "local.", resolve_callback
,
1419 &uribuf
) == kDNSServiceErr_NoError
)
1421 int fds
; /* Number of ready descriptors */
1422 time_t timeout
, /* Poll timeout */
1423 start_time
= time(NULL
);/* Start time */
1428 _cupsLangPuts(stderr
, _("INFO: Looking for printer...\n"));
1431 * For the first minute, wakeup every 2 seconds to emit a
1432 * "looking for printer" message...
1435 timeout
= (time(NULL
) < (start_time
+ 60)) ? 2000 : -1;
1437 polldata
.fd
= DNSServiceRefSockFD(ref
);
1438 polldata
.events
= POLLIN
;
1440 fds
= poll(&polldata
, 1, timeout
);
1444 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1446 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno
)));
1453 * Wait 2 seconds for a response to the local resolve; if nothing
1454 * comes in, do an additional domain resolution...
1457 if (domainsent
== 0 && strcasecmp(domain
, "local."))
1461 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1462 "domain=\"%s\"...\n", hostname
, regtype
, domain
);
1465 if (DNSServiceResolve(&domainref
, kDNSServiceFlagsShareConnection
, 0,
1466 hostname
, regtype
, domain
, resolve_callback
,
1467 &uribuf
) == kDNSServiceErr_NoError
)
1473 if (DNSServiceProcessResult(ref
) == kDNSServiceErr_NoError
)
1482 DNSServiceRefDeallocate(domainref
);
1484 DNSServiceRefDeallocate(localref
);
1487 DNSServiceRefDeallocate(ref
);
1493 fprintf(stderr
, "DEBUG: Resolved as \"%s\"...\n", uri
);
1495 fputs("DEBUG: Unable to resolve URI\n", stderr
);
1497 fputs("STATE: -connecting-to-device\n", stderr
);
1502 * No DNS-SD support...
1506 #endif /* HAVE_DNSSD */
1509 _cupsLangPuts(stderr
, _("Unable to find printer\n"));
1512 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri
));
1519 * 'http_copy_decode()' - Copy and decode a URI.
1522 static const char * /* O - New source pointer or NULL on error */
1523 http_copy_decode(char *dst
, /* O - Destination buffer */
1524 const char *src
, /* I - Source pointer */
1525 int dstsize
, /* I - Destination size */
1526 const char *term
, /* I - Terminating characters */
1527 int decode
) /* I - Decode %-encoded values */
1529 char *ptr
, /* Pointer into buffer */
1530 *end
; /* End of buffer */
1531 int quoted
; /* Quoted character */
1535 * Copy the src to the destination until we hit a terminating character
1536 * or the end of the string.
1539 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1540 *src
&& (!term
|| !strchr(term
, *src
));
1544 if (*src
== '%' && decode
)
1546 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1549 * Grab a hex-encoded character...
1554 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1556 quoted
= (*src
- '0') << 4;
1560 quoted
|= tolower(*src
) - 'a' + 10;
1562 quoted
|= *src
- '0';
1569 * Bad hex-encoded character...
1587 * 'http_copy_encode()' - Copy and encode a URI.
1590 static char * /* O - End of current URI */
1591 http_copy_encode(char *dst
, /* O - Destination buffer */
1592 const char *src
, /* I - Source pointer */
1593 char *dstend
, /* I - End of destination buffer */
1594 const char *reserved
, /* I - Extra reserved characters */
1595 const char *term
, /* I - Terminating characters */
1596 int encode
) /* I - %-encode reserved chars? */
1598 static const char hex
[] = "0123456789ABCDEF";
1601 while (*src
&& dst
< dstend
)
1603 if (term
&& *src
== *term
)
1606 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1607 (reserved
&& strchr(reserved
, *src
))))
1610 * Hex encode reserved characters...
1613 if ((dst
+ 2) >= dstend
)
1617 *dst
++ = hex
[(*src
>> 4) & 15];
1618 *dst
++ = hex
[*src
& 15];
1637 * 'resolve_callback()' - Build a device URI for the given service name.
1642 DNSServiceRef sdRef
, /* I - Service reference */
1643 DNSServiceFlags flags
, /* I - Results flags */
1644 uint32_t interfaceIndex
, /* I - Interface number */
1645 DNSServiceErrorType errorCode
, /* I - Error, if any */
1646 const char *fullName
, /* I - Full service name */
1647 const char *hostTarget
, /* I - Hostname */
1648 uint16_t port
, /* I - Port number */
1649 uint16_t txtLen
, /* I - Length of TXT record */
1650 const unsigned char *txtRecord
, /* I - TXT record data */
1651 void *context
) /* I - Pointer to URI buffer */
1653 const char *scheme
; /* URI scheme */
1654 char rp
[257]; /* Remote printer */
1655 const void *value
; /* Value from TXT record */
1656 uint8_t valueLen
; /* Length of value */
1657 _http_uribuf_t
*uribuf
; /* URI buffer */
1660 DEBUG_printf(("7resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1661 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1662 "txtLen=%u, txtRecord=%p, context=%p)", sdRef
, flags
,
1663 interfaceIndex
, errorCode
, fullName
, hostTarget
, port
, txtLen
,
1664 txtRecord
, context
));
1667 * Figure out the scheme from the full name...
1670 if (strstr(fullName
, "._ipp") || strstr(fullName
, "._fax-ipp"))
1672 else if (strstr(fullName
, "._printer."))
1674 else if (strstr(fullName
, "._pdl-datastream."))
1677 scheme
= "riousbprint";
1680 * Extract the "remote printer" key from the TXT record...
1683 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "rp",
1684 &valueLen
)) != NULL
)
1687 * Convert to resource by concatenating with a leading "/"...
1691 memcpy(rp
+ 1, value
, valueLen
);
1692 rp
[valueLen
+ 1] = '\0';
1698 * Assemble the final device URI...
1701 uribuf
= (_http_uribuf_t
*)context
;
1703 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, uribuf
->bufsize
, scheme
,
1704 NULL
, hostTarget
, ntohs(port
), rp
);
1706 DEBUG_printf(("8resolve_callback: Resolved URI is \"%s\"...",
1709 #endif /* HAVE_DNSSD */
1713 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".