2 * "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $"
4 * HTTP support routines for CUPS.
6 * Copyright 2007-2010 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...
50 #include "cups-private.h"
54 #endif /* HAVE_DNSSD */
61 typedef struct _http_uribuf_s
/* URI buffer */
63 char *buffer
; /* Pointer to buffer */
64 size_t bufsize
; /* Size of buffer */
72 static const char * const http_days
[7] =
82 static const char * const http_months
[12] =
103 static const char *http_copy_decode(char *dst
, const char *src
,
104 int dstsize
, const char *term
,
106 static char *http_copy_encode(char *dst
, const char *src
,
107 char *dstend
, const char *reserved
,
108 const char *term
, int encode
);
110 static void resolve_callback(DNSServiceRef sdRef
,
111 DNSServiceFlags flags
,
112 uint32_t interfaceIndex
,
113 DNSServiceErrorType errorCode
,
114 const char *fullName
,
115 const char *hostTarget
,
116 uint16_t port
, uint16_t txtLen
,
117 const unsigned char *txtRecord
,
119 #endif /* HAVE_DNSSD */
123 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
126 * This function escapes reserved characters in the URI depending on the
127 * value of the "encoding" argument. You should use this function in
128 * place of traditional string functions whenever you need to create a
131 * @since CUPS 1.2/Mac OS X 10.5@
134 http_uri_status_t
/* O - URI status */
136 http_uri_coding_t encoding
, /* I - Encoding flags */
137 char *uri
, /* I - URI buffer */
138 int urilen
, /* I - Size of URI buffer */
139 const char *scheme
, /* I - Scheme name */
140 const char *username
, /* I - Username */
141 const char *host
, /* I - Hostname or address */
142 int port
, /* I - Port number */
143 const char *resource
) /* I - Resource */
145 char *ptr
, /* Pointer into URI buffer */
146 *end
; /* End of URI buffer */
150 * Range check input...
153 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
158 return (HTTP_URI_BAD_ARGUMENTS
);
162 * Assemble the URI starting with the scheme...
165 end
= uri
+ urilen
- 1;
166 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
169 goto assemble_overflow
;
171 if (!strcmp(scheme
, "mailto"))
174 * mailto: only has :, no //...
180 goto assemble_overflow
;
185 * Schemes other than mailto: all have //...
195 goto assemble_overflow
;
199 * Next the username and hostname, if any...
204 if (username
&& *username
)
207 * Add username@ first...
210 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
211 encoding
& HTTP_URI_CODING_USERNAME
);
214 goto assemble_overflow
;
219 goto assemble_overflow
;
223 * Then add the hostname. Since IPv6 is a particular pain to deal
224 * with, we have several special cases to deal with. If we get
225 * an IPv6 address with brackets around it, assume it is already in
226 * URI format. Since DNS-SD service names can sometimes look like
227 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
231 if (host
[0] != '[' && strchr(host
, ':') && !strstr(host
, "._tcp"))
234 * We have a raw IPv6 address...
237 if (strchr(host
, '%'))
240 * We have a link-local address, add "[v1." prefix...
251 goto assemble_overflow
;
256 * We have a normal address, add "[" prefix...
262 goto assemble_overflow
;
266 * Copy the rest of the IPv6 address, and terminate with "]".
269 while (ptr
< end
&& *host
)
273 *ptr
++ = '+'; /* Convert zone separator */
281 goto assemble_overflow
;
286 goto assemble_overflow
;
291 * Otherwise, just copy the host string...
294 ptr
= http_copy_encode(ptr
, host
, end
, ":/?#[]@\\", NULL
,
295 encoding
& HTTP_URI_CODING_HOSTNAME
);
298 goto assemble_overflow
;
302 * Finish things off with the port number...
307 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
311 goto assemble_overflow
;
316 * Last but not least, add the resource string...
321 char *query
; /* Pointer to query string */
325 * Copy the resource string up to the query string if present...
328 query
= strchr(resource
, '?');
329 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
330 encoding
& HTTP_URI_CODING_RESOURCE
);
332 goto assemble_overflow
;
337 * Copy query string without encoding...
340 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
341 encoding
& HTTP_URI_CODING_QUERY
);
343 goto assemble_overflow
;
349 goto assemble_overflow
;
352 * Nul-terminate the URI buffer and return with no errors...
357 return (HTTP_URI_OK
);
360 * Clear the URI string and return an overflow error; I don't usually
361 * like goto's, but in this case it makes sense...
367 return (HTTP_URI_OVERFLOW
);
372 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
373 * components with a formatted resource.
375 * This function creates a formatted version of the resource string
376 * argument "resourcef" and escapes reserved characters in the URI
377 * depending on the value of the "encoding" argument. You should use
378 * this function in place of traditional string functions whenever
379 * you need to create a URI string.
381 * @since CUPS 1.2/Mac OS X 10.5@
384 http_uri_status_t
/* O - URI status */
386 http_uri_coding_t encoding
, /* I - Encoding flags */
387 char *uri
, /* I - URI buffer */
388 int urilen
, /* I - Size of URI buffer */
389 const char *scheme
, /* I - Scheme name */
390 const char *username
, /* I - Username */
391 const char *host
, /* I - Hostname or address */
392 int port
, /* I - Port number */
393 const char *resourcef
, /* I - Printf-style resource */
394 ...) /* I - Additional arguments as needed */
396 va_list ap
; /* Pointer to additional arguments */
397 char resource
[1024]; /* Formatted resource string */
398 int bytes
; /* Bytes in formatted string */
402 * Range check input...
405 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
410 return (HTTP_URI_BAD_ARGUMENTS
);
414 * Format the resource string and assemble the URI...
417 va_start(ap
, resourcef
);
418 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
421 if (bytes
>= sizeof(resource
))
424 return (HTTP_URI_OVERFLOW
);
427 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
433 * 'httpDecode64()' - Base64-decode a string.
435 * This function is deprecated. Use the httpDecode64_2() function instead
436 * which provides buffer length arguments.
441 char * /* O - Decoded string */
442 httpDecode64(char *out
, /* I - String to write to */
443 const char *in
) /* I - String to read from */
445 int outlen
; /* Output buffer length */
449 * Use the old maximum buffer size for binary compatibility...
454 return (httpDecode64_2(out
, &outlen
, in
));
459 * 'httpDecode64_2()' - Base64-decode a string.
461 * @since CUPS 1.1.21/Mac OS X 10.4@
464 char * /* O - Decoded string */
465 httpDecode64_2(char *out
, /* I - String to write to */
466 int *outlen
, /* IO - Size of output string */
467 const char *in
) /* I - String to read from */
469 int pos
, /* Bit position */
470 base64
; /* Value of this character */
471 char *outptr
, /* Output pointer */
472 *outend
; /* End of output buffer */
476 * Range check input...
479 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
491 * Convert from base-64 to bytes...
494 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
497 * Decode this character into a number from 0 to 63...
500 if (*in
>= 'A' && *in
<= 'Z')
502 else if (*in
>= 'a' && *in
<= 'z')
503 base64
= *in
- 'a' + 26;
504 else if (*in
>= '0' && *in
<= '9')
505 base64
= *in
- '0' + 52;
516 * Store the result in the appropriate chars...
523 *outptr
= base64
<< 2;
528 *outptr
++ |= (base64
>> 4) & 3;
530 *outptr
= (base64
<< 4) & 255;
535 *outptr
++ |= (base64
>> 2) & 15;
537 *outptr
= (base64
<< 6) & 255;
551 * Return the decoded string and size...
554 *outlen
= (int)(outptr
- out
);
561 * 'httpEncode64()' - Base64-encode a string.
563 * This function is deprecated. Use the httpEncode64_2() function instead
564 * which provides buffer length arguments.
569 char * /* O - Encoded string */
570 httpEncode64(char *out
, /* I - String to write to */
571 const char *in
) /* I - String to read from */
573 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
578 * 'httpEncode64_2()' - Base64-encode a string.
580 * @since CUPS 1.1.21/Mac OS X 10.4@
583 char * /* O - Encoded string */
584 httpEncode64_2(char *out
, /* I - String to write to */
585 int outlen
, /* I - Size of output string */
586 const char *in
, /* I - String to read from */
587 int inlen
) /* I - Size of input string */
589 char *outptr
, /* Output pointer */
590 *outend
; /* End of output buffer */
591 static const char base64
[] = /* Base64 characters... */
593 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
594 "abcdefghijklmnopqrstuvwxyz"
601 * Range check input...
604 if (!out
|| outlen
< 1 || !in
)
608 * Convert bytes to base-64...
611 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
614 * Encode the up to 3 characters as 4 Base64 numbers...
618 *outptr
++ = base64
[(in
[0] & 255) >> 2];
623 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
625 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
642 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
644 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
657 *outptr
++ = base64
[in
[0] & 63];
663 * Return the encoded string...
671 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
676 const char * /* O - Date/time string */
677 httpGetDateString(time_t t
) /* I - UNIX time */
679 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
682 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
687 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
689 * @since CUPS 1.2/Mac OS X 10.5@
692 const char * /* O - Date/time string */
693 httpGetDateString2(time_t t
, /* I - UNIX time */
694 char *s
, /* I - String buffer */
695 int slen
) /* I - Size of string buffer */
697 struct tm
*tdate
; /* UNIX date/time data */
701 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
702 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
703 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
704 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
711 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
714 time_t /* O - UNIX time */
715 httpGetDateTime(const char *s
) /* I - Date/time string */
717 int i
; /* Looping var */
718 char mon
[16]; /* Abbreviated month name */
719 int day
, year
; /* Day of month and year */
720 int hour
, min
, sec
; /* Time */
721 int days
; /* Number of days since 1970 */
722 static const int normal_days
[] = /* Days to a month, normal years */
723 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
724 static const int leap_days
[] = /* Days to a month, leap years */
725 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
728 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s
));
731 * Extract the date and time from the formatted string...
734 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
737 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
738 "min=%d, sec=%d", day
, mon
, year
, hour
, min
, sec
));
741 * Convert the month name to a number from 0 to 11.
744 for (i
= 0; i
< 12; i
++)
745 if (!strcasecmp(mon
, http_months
[i
]))
751 DEBUG_printf(("4httpGetDateTime: i=%d", i
));
754 * Now convert the date and time to a UNIX time value in seconds since
755 * 1970. We can't use mktime() since the timezone may not be UTC but
756 * the date/time string *is* UTC.
759 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
760 days
= leap_days
[i
] + day
- 1;
762 days
= normal_days
[i
] + day
- 1;
764 DEBUG_printf(("4httpGetDateTime: days=%d", days
));
766 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
767 ((year
- 1) / 4 - 492) - /* + leap days */
768 ((year
- 1) / 100 - 19) + /* - 100 year days */
769 ((year
- 1) / 400 - 4); /* + 400 year days */
771 DEBUG_printf(("4httpGetDateTime: days=%d\n", days
));
773 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
778 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
781 * This function is deprecated; use the httpSeparateURI() function instead.
787 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
788 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
789 char *username
, /* O - Username [1024] */
790 char *host
, /* O - Hostname [1024] */
791 int *port
, /* O - Port number to use */
792 char *resource
) /* O - Resource/filename [1024] */
794 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
795 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
801 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
804 * This function is deprecated; use the httpSeparateURI() function instead.
806 * @since CUPS 1.1.21/Mac OS X 10.4@
811 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
812 char *scheme
, /* O - Scheme (http, https, etc.) */
813 int schemelen
, /* I - Size of scheme buffer */
814 char *username
, /* O - Username */
815 int usernamelen
, /* I - Size of username buffer */
816 char *host
, /* O - Hostname */
817 int hostlen
, /* I - Size of hostname buffer */
818 int *port
, /* O - Port number to use */
819 char *resource
, /* O - Resource/filename */
820 int resourcelen
) /* I - Size of resource buffer */
822 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
823 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
828 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
831 * @since CUPS 1.2/Mac OS X 10.5@
834 http_uri_status_t
/* O - Result of separation */
836 http_uri_coding_t decoding
, /* I - Decoding flags */
837 const char *uri
, /* I - Universal Resource Identifier */
838 char *scheme
, /* O - Scheme (http, https, etc.) */
839 int schemelen
, /* I - Size of scheme buffer */
840 char *username
, /* O - Username */
841 int usernamelen
, /* I - Size of username buffer */
842 char *host
, /* O - Hostname */
843 int hostlen
, /* I - Size of hostname buffer */
844 int *port
, /* O - Port number to use */
845 char *resource
, /* O - Resource/filename */
846 int resourcelen
) /* I - Size of resource buffer */
848 char *ptr
, /* Pointer into string... */
849 *end
; /* End of string */
850 const char *sep
; /* Separator character */
851 http_uri_status_t status
; /* Result of separation */
855 * Initialize everything to blank...
858 if (scheme
&& schemelen
> 0)
861 if (username
&& usernamelen
> 0)
864 if (host
&& hostlen
> 0)
870 if (resource
&& resourcelen
> 0)
874 * Range check input...
877 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
878 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
880 return (HTTP_URI_BAD_ARGUMENTS
);
883 return (HTTP_URI_BAD_URI
);
886 * Grab the scheme portion of the URI...
889 status
= HTTP_URI_OK
;
891 if (!strncmp(uri
, "//", 2))
894 * Workaround for HP IPP client bug...
897 strlcpy(scheme
, "ipp", schemelen
);
898 status
= HTTP_URI_MISSING_SCHEME
;
900 else if (*uri
== '/')
906 strlcpy(scheme
, "file", schemelen
);
907 status
= HTTP_URI_MISSING_SCHEME
;
912 * Standard URI with scheme...
915 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
916 *uri
&& *uri
!= ':' && ptr
< end
;)
917 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
927 return (HTTP_URI_BAD_SCHEME
);
934 * Set the default port number...
937 if (!strcmp(scheme
, "http"))
939 else if (!strcmp(scheme
, "https"))
941 else if (!strcmp(scheme
, "ipp"))
943 else if (!strcasecmp(scheme
, "lpd"))
945 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
947 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
948 status
= HTTP_URI_UNKNOWN_SCHEME
;
951 * Now see if we have a hostname...
954 if (!strncmp(uri
, "//", 2))
963 * Grab the username, if any...
966 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
969 * Get a username:password combo...
972 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
973 decoding
& HTTP_URI_CODING_USERNAME
);
978 return (HTTP_URI_BAD_USERNAME
);
985 * Then the hostname/IP address...
991 * Grab IPv6 address...
995 if (!strncmp(uri
, "v1.", 3))
996 uri
+= 3; /* Skip IPvN leader... */
998 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
999 decoding
& HTTP_URI_CODING_HOSTNAME
);
1004 return (HTTP_URI_BAD_HOSTNAME
);
1014 return (HTTP_URI_BAD_HOSTNAME
);
1019 for (ptr
= host
; *ptr
; ptr
++)
1023 * Convert zone separator to % and stop here...
1029 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1032 return (HTTP_URI_BAD_HOSTNAME
);
1038 * Validate the hostname or IPv4 address first...
1041 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1042 if (strchr(":?/", *ptr
))
1044 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1045 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1049 "!$&'()*+,;=\\", *ptr
))
1052 return (HTTP_URI_BAD_HOSTNAME
);
1056 * Then copy the hostname or IPv4 address to the buffer...
1059 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1060 decoding
& HTTP_URI_CODING_HOSTNAME
);
1065 return (HTTP_URI_BAD_HOSTNAME
);
1070 * Validate hostname for file scheme - only empty and localhost are
1074 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1077 return (HTTP_URI_BAD_HOSTNAME
);
1081 * See if we have a port number...
1087 * Yes, collect the port number...
1090 if (!isdigit(uri
[1] & 255))
1093 return (HTTP_URI_BAD_PORT
);
1096 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1098 if (*uri
!= '/' && *uri
)
1101 return (HTTP_URI_BAD_PORT
);
1107 * The remaining portion is the resource string...
1110 if (*uri
== '?' || !*uri
)
1113 * Hostname but no path...
1116 status
= HTTP_URI_MISSING_RESOURCE
;
1120 * Copy any query string...
1124 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1125 decoding
& HTTP_URI_CODING_QUERY
);
1131 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1132 decoding
& HTTP_URI_CODING_RESOURCE
);
1134 if (uri
&& *uri
== '?')
1137 * Concatenate any query string...
1140 char *resptr
= resource
+ strlen(resource
);
1142 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1143 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1150 return (HTTP_URI_BAD_RESOURCE
);
1154 * Return the URI separation status...
1162 * 'httpStatus()' - Return a short string describing a HTTP status code.
1164 * The returned string is localized to the current POSIX locale and is based
1165 * on the status strings defined in RFC 2616.
1168 const char * /* O - Localized status string */
1169 httpStatus(http_status_t status
) /* I - HTTP status code */
1171 const char *s
; /* Status string */
1172 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1175 if (!cg
->lang_default
)
1176 cg
->lang_default
= cupsLangDefault();
1180 case HTTP_CONTINUE
:
1183 case HTTP_SWITCHING_PROTOCOLS
:
1184 s
= _("Switching Protocols");
1192 case HTTP_ACCEPTED
:
1195 case HTTP_NO_CONTENT
:
1196 s
= _("No Content");
1198 case HTTP_MOVED_PERMANENTLY
:
1199 s
= _("Moved Permanently");
1201 case HTTP_SEE_OTHER
:
1204 case HTTP_NOT_MODIFIED
:
1205 s
= _("Not Modified");
1207 case HTTP_BAD_REQUEST
:
1208 s
= _("Bad Request");
1210 case HTTP_UNAUTHORIZED
:
1211 case HTTP_AUTHORIZATION_CANCELED
:
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");
1241 case HTTP_SERVER_ERROR
:
1242 s
= _("Internal Server Error");
1250 return (_cupsLangString(cg
->lang_default
, s
));
1254 #ifndef HAVE_HSTRERROR
1256 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1259 const char * /* O - Error string */
1260 _cups_hstrerror(int error
) /* I - Error number */
1262 static const char * const errors
[] = /* Error strings */
1267 "Unrecoverable lookup error.",
1268 "No data associated with name."
1272 if (error
< 0 || error
> 4)
1273 return ("Unknown hostname lookup error.");
1275 return (errors
[error
]);
1277 #endif /* !HAVE_HSTRERROR */
1281 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1284 char * /* O - Encoded URI */
1285 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1286 const char *src
, /* I - Source URI */
1287 size_t dstsize
) /* I - Size of destination buffer */
1289 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1295 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1298 const char * /* O - Resolved URI */
1300 const char *uri
, /* I - DNS-SD URI */
1301 char *resolved_uri
, /* I - Buffer for resolved URI */
1302 size_t resolved_size
, /* I - Size of URI buffer */
1303 int logit
) /* I - Log progress to stderr? */
1305 char scheme
[32], /* URI components... */
1311 http_uri_status_t status
; /* URI decode status */
1315 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1316 "resolved_size=" CUPS_LLFMT
")", uri
, resolved_uri
,
1317 CUPS_LLCAST resolved_size
));
1320 * Get the device URI...
1324 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1325 sizeof(scheme
), userpass
, sizeof(userpass
),
1326 hostname
, sizeof(hostname
), &port
, resource
,
1327 sizeof(resource
))) < HTTP_URI_OK
)
1329 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1330 sizeof(scheme
), userpass
, sizeof(userpass
),
1331 hostname
, sizeof(hostname
), &port
, resource
,
1332 sizeof(resource
)) < HTTP_URI_OK
)
1336 _cupsLangPrintf(stderr
, _("Bad device URI \"%s\"\n"), uri
);
1338 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status
));
1339 DEBUG_puts("5_httpResolveURI: Returning NULL");
1344 * Resolve it as needed...
1347 if (strstr(hostname
, "._tcp"))
1350 DNSServiceRef ref
, /* DNS-SD master service reference */
1351 domainref
, /* DNS-SD service reference for domain */
1352 localref
; /* DNS-SD service reference for .local */
1353 int domainsent
= 0; /* Send the domain resolve? */
1354 char *regtype
, /* Pointer to type in hostname */
1355 *domain
; /* Pointer to domain in hostname */
1356 _http_uribuf_t uribuf
; /* URI buffer */
1357 struct pollfd polldata
; /* Polling data */
1361 fprintf(stderr
, "DEBUG: Resolving \"%s\"...\n", hostname
);
1364 * Separate the hostname into service name, registration type, and domain...
1367 for (regtype
= strstr(hostname
, "._tcp") - 2;
1370 if (regtype
[0] == '.' && regtype
[1] == '_')
1373 * Found ._servicetype in front of ._tcp...
1380 if (regtype
<= hostname
)
1382 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1386 for (domain
= strchr(regtype
, '.');
1388 domain
= strchr(domain
+ 1, '.'))
1389 if (domain
[1] != '_')
1395 uribuf
.buffer
= resolved_uri
;
1396 uribuf
.bufsize
= resolved_size
;
1398 resolved_uri
[0] = '\0';
1400 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1401 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1404 fputs("STATE: +connecting-to-device\n", stderr
);
1405 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1406 "domain=\"local.\"...\n", hostname
, regtype
);
1411 if (DNSServiceCreateConnection(&ref
) == kDNSServiceErr_NoError
)
1414 if (DNSServiceResolve(&localref
, kDNSServiceFlagsShareConnection
, 0,
1415 hostname
, regtype
, "local.", resolve_callback
,
1416 &uribuf
) == kDNSServiceErr_NoError
)
1418 int fds
; /* Number of ready descriptors */
1419 time_t timeout
, /* Poll timeout */
1420 start_time
= time(NULL
);/* Start time */
1425 _cupsLangPuts(stderr
, _("INFO: Looking for printer...\n"));
1428 * For the first minute, wakeup every 2 seconds to emit a
1429 * "looking for printer" message...
1432 timeout
= (time(NULL
) < (start_time
+ 60)) ? 2000 : -1;
1434 polldata
.fd
= DNSServiceRefSockFD(ref
);
1435 polldata
.events
= POLLIN
;
1437 fds
= poll(&polldata
, 1, timeout
);
1441 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1443 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno
)));
1450 * Wait 2 seconds for a response to the local resolve; if nothing
1451 * comes in, do an additional domain resolution...
1454 if (domainsent
== 0 && strcasecmp(domain
, "local."))
1458 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1459 "domain=\"%s\"...\n", hostname
, regtype
, domain
);
1462 if (DNSServiceResolve(&domainref
, kDNSServiceFlagsShareConnection
, 0,
1463 hostname
, regtype
, domain
, resolve_callback
,
1464 &uribuf
) == kDNSServiceErr_NoError
)
1470 if (DNSServiceProcessResult(ref
) == kDNSServiceErr_NoError
)
1479 DNSServiceRefDeallocate(domainref
);
1481 DNSServiceRefDeallocate(localref
);
1484 DNSServiceRefDeallocate(ref
);
1490 fprintf(stderr
, "DEBUG: Resolved as \"%s\"...\n", uri
);
1492 fputs("DEBUG: Unable to resolve URI\n", stderr
);
1494 fputs("STATE: -connecting-to-device\n", stderr
);
1499 * No DNS-SD support...
1503 #endif /* HAVE_DNSSD */
1506 _cupsLangPuts(stderr
, _("Unable to find printer\n"));
1509 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri
));
1516 * 'http_copy_decode()' - Copy and decode a URI.
1519 static const char * /* O - New source pointer or NULL on error */
1520 http_copy_decode(char *dst
, /* O - Destination buffer */
1521 const char *src
, /* I - Source pointer */
1522 int dstsize
, /* I - Destination size */
1523 const char *term
, /* I - Terminating characters */
1524 int decode
) /* I - Decode %-encoded values */
1526 char *ptr
, /* Pointer into buffer */
1527 *end
; /* End of buffer */
1528 int quoted
; /* Quoted character */
1532 * Copy the src to the destination until we hit a terminating character
1533 * or the end of the string.
1536 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1537 *src
&& (!term
|| !strchr(term
, *src
));
1541 if (*src
== '%' && decode
)
1543 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1546 * Grab a hex-encoded character...
1551 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1553 quoted
= (*src
- '0') << 4;
1557 quoted
|= tolower(*src
) - 'a' + 10;
1559 quoted
|= *src
- '0';
1566 * Bad hex-encoded character...
1584 * 'http_copy_encode()' - Copy and encode a URI.
1587 static char * /* O - End of current URI */
1588 http_copy_encode(char *dst
, /* O - Destination buffer */
1589 const char *src
, /* I - Source pointer */
1590 char *dstend
, /* I - End of destination buffer */
1591 const char *reserved
, /* I - Extra reserved characters */
1592 const char *term
, /* I - Terminating characters */
1593 int encode
) /* I - %-encode reserved chars? */
1595 static const char hex
[] = "0123456789ABCDEF";
1598 while (*src
&& dst
< dstend
)
1600 if (term
&& *src
== *term
)
1603 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1604 (reserved
&& strchr(reserved
, *src
))))
1607 * Hex encode reserved characters...
1610 if ((dst
+ 2) >= dstend
)
1614 *dst
++ = hex
[(*src
>> 4) & 15];
1615 *dst
++ = hex
[*src
& 15];
1634 * 'resolve_callback()' - Build a device URI for the given service name.
1639 DNSServiceRef sdRef
, /* I - Service reference */
1640 DNSServiceFlags flags
, /* I - Results flags */
1641 uint32_t interfaceIndex
, /* I - Interface number */
1642 DNSServiceErrorType errorCode
, /* I - Error, if any */
1643 const char *fullName
, /* I - Full service name */
1644 const char *hostTarget
, /* I - Hostname */
1645 uint16_t port
, /* I - Port number */
1646 uint16_t txtLen
, /* I - Length of TXT record */
1647 const unsigned char *txtRecord
, /* I - TXT record data */
1648 void *context
) /* I - Pointer to URI buffer */
1650 const char *scheme
; /* URI scheme */
1651 char rp
[257]; /* Remote printer */
1652 const void *value
; /* Value from TXT record */
1653 uint8_t valueLen
; /* Length of value */
1654 _http_uribuf_t
*uribuf
; /* URI buffer */
1657 DEBUG_printf(("7resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1658 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1659 "txtLen=%u, txtRecord=%p, context=%p)", sdRef
, flags
,
1660 interfaceIndex
, errorCode
, fullName
, hostTarget
, port
, txtLen
,
1661 txtRecord
, context
));
1664 * Figure out the scheme from the full name...
1667 if (strstr(fullName
, "._ipp") || strstr(fullName
, "._fax-ipp"))
1669 else if (strstr(fullName
, "._printer."))
1671 else if (strstr(fullName
, "._pdl-datastream."))
1674 scheme
= "riousbprint";
1677 * Extract the "remote printer" key from the TXT record...
1680 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "rp",
1681 &valueLen
)) != NULL
)
1684 * Convert to resource by concatenating with a leading "/"...
1688 memcpy(rp
+ 1, value
, valueLen
);
1689 rp
[valueLen
+ 1] = '\0';
1695 * Assemble the final device URI...
1698 uribuf
= (_http_uribuf_t
*)context
;
1700 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, uribuf
->bufsize
, scheme
,
1701 NULL
, hostTarget
, ntohs(port
), rp
);
1703 DEBUG_printf(("8resolve_callback: Resolved URI is \"%s\"...",
1706 #endif /* HAVE_DNSSD */
1710 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".