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"
55 # elif defined(HAVE_POLL)
58 # include <sys/select.h>
60 #endif /* HAVE_DNSSD */
67 typedef struct _http_uribuf_s
/* URI buffer */
69 char *buffer
; /* Pointer to buffer */
70 size_t bufsize
; /* Size of buffer */
78 static const char * const http_days
[7] =
88 static const char * const http_months
[12] =
109 static const char *http_copy_decode(char *dst
, const char *src
,
110 int dstsize
, const char *term
,
112 static char *http_copy_encode(char *dst
, const char *src
,
113 char *dstend
, const char *reserved
,
114 const char *term
, int encode
);
116 static void DNSSD_API
resolve_callback(DNSServiceRef sdRef
,
117 DNSServiceFlags flags
,
118 uint32_t interfaceIndex
,
119 DNSServiceErrorType errorCode
,
120 const char *fullName
,
121 const char *hostTarget
,
122 uint16_t port
, uint16_t txtLen
,
123 const unsigned char *txtRecord
,
125 #endif /* HAVE_DNSSD */
129 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
132 * This function escapes reserved characters in the URI depending on the
133 * value of the "encoding" argument. You should use this function in
134 * place of traditional string functions whenever you need to create a
137 * @since CUPS 1.2/Mac OS X 10.5@
140 http_uri_status_t
/* O - URI status */
142 http_uri_coding_t encoding
, /* I - Encoding flags */
143 char *uri
, /* I - URI buffer */
144 int urilen
, /* I - Size of URI buffer */
145 const char *scheme
, /* I - Scheme name */
146 const char *username
, /* I - Username */
147 const char *host
, /* I - Hostname or address */
148 int port
, /* I - Port number */
149 const char *resource
) /* I - Resource */
151 char *ptr
, /* Pointer into URI buffer */
152 *end
; /* End of URI buffer */
156 * Range check input...
159 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
164 return (HTTP_URI_BAD_ARGUMENTS
);
168 * Assemble the URI starting with the scheme...
171 end
= uri
+ urilen
- 1;
172 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
175 goto assemble_overflow
;
177 if (!strcmp(scheme
, "mailto"))
180 * mailto: only has :, no //...
186 goto assemble_overflow
;
191 * Schemes other than mailto: all have //...
201 goto assemble_overflow
;
205 * Next the username and hostname, if any...
210 if (username
&& *username
)
213 * Add username@ first...
216 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
217 encoding
& HTTP_URI_CODING_USERNAME
);
220 goto assemble_overflow
;
225 goto assemble_overflow
;
229 * Then add the hostname. Since IPv6 is a particular pain to deal
230 * with, we have several special cases to deal with. If we get
231 * an IPv6 address with brackets around it, assume it is already in
232 * URI format. Since DNS-SD service names can sometimes look like
233 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
237 if (host
[0] != '[' && strchr(host
, ':') && !strstr(host
, "._tcp"))
240 * We have a raw IPv6 address...
243 if (strchr(host
, '%'))
246 * We have a link-local address, add "[v1." prefix...
257 goto assemble_overflow
;
262 * We have a normal address, add "[" prefix...
268 goto assemble_overflow
;
272 * Copy the rest of the IPv6 address, and terminate with "]".
275 while (ptr
< end
&& *host
)
279 *ptr
++ = '+'; /* Convert zone separator */
287 goto assemble_overflow
;
292 goto assemble_overflow
;
297 * Otherwise, just copy the host string...
300 ptr
= http_copy_encode(ptr
, host
, end
, ":/?#[]@\\", NULL
,
301 encoding
& HTTP_URI_CODING_HOSTNAME
);
304 goto assemble_overflow
;
308 * Finish things off with the port number...
313 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
317 goto assemble_overflow
;
322 * Last but not least, add the resource string...
327 char *query
; /* Pointer to query string */
331 * Copy the resource string up to the query string if present...
334 query
= strchr(resource
, '?');
335 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
336 encoding
& HTTP_URI_CODING_RESOURCE
);
338 goto assemble_overflow
;
343 * Copy query string without encoding...
346 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
347 encoding
& HTTP_URI_CODING_QUERY
);
349 goto assemble_overflow
;
355 goto assemble_overflow
;
358 * Nul-terminate the URI buffer and return with no errors...
363 return (HTTP_URI_OK
);
366 * Clear the URI string and return an overflow error; I don't usually
367 * like goto's, but in this case it makes sense...
373 return (HTTP_URI_OVERFLOW
);
378 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
379 * components with a formatted resource.
381 * This function creates a formatted version of the resource string
382 * argument "resourcef" and escapes reserved characters in the URI
383 * depending on the value of the "encoding" argument. You should use
384 * this function in place of traditional string functions whenever
385 * you need to create a URI string.
387 * @since CUPS 1.2/Mac OS X 10.5@
390 http_uri_status_t
/* O - URI status */
392 http_uri_coding_t encoding
, /* I - Encoding flags */
393 char *uri
, /* I - URI buffer */
394 int urilen
, /* I - Size of URI buffer */
395 const char *scheme
, /* I - Scheme name */
396 const char *username
, /* I - Username */
397 const char *host
, /* I - Hostname or address */
398 int port
, /* I - Port number */
399 const char *resourcef
, /* I - Printf-style resource */
400 ...) /* I - Additional arguments as needed */
402 va_list ap
; /* Pointer to additional arguments */
403 char resource
[1024]; /* Formatted resource string */
404 int bytes
; /* Bytes in formatted string */
408 * Range check input...
411 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
416 return (HTTP_URI_BAD_ARGUMENTS
);
420 * Format the resource string and assemble the URI...
423 va_start(ap
, resourcef
);
424 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
427 if (bytes
>= sizeof(resource
))
430 return (HTTP_URI_OVERFLOW
);
433 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
439 * 'httpDecode64()' - Base64-decode a string.
441 * This function is deprecated. Use the httpDecode64_2() function instead
442 * which provides buffer length arguments.
447 char * /* O - Decoded string */
448 httpDecode64(char *out
, /* I - String to write to */
449 const char *in
) /* I - String to read from */
451 int outlen
; /* Output buffer length */
455 * Use the old maximum buffer size for binary compatibility...
460 return (httpDecode64_2(out
, &outlen
, in
));
465 * 'httpDecode64_2()' - Base64-decode a string.
467 * @since CUPS 1.1.21/Mac OS X 10.4@
470 char * /* O - Decoded string */
471 httpDecode64_2(char *out
, /* I - String to write to */
472 int *outlen
, /* IO - Size of output string */
473 const char *in
) /* I - String to read from */
475 int pos
, /* Bit position */
476 base64
; /* Value of this character */
477 char *outptr
, /* Output pointer */
478 *outend
; /* End of output buffer */
482 * Range check input...
485 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
497 * Convert from base-64 to bytes...
500 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
503 * Decode this character into a number from 0 to 63...
506 if (*in
>= 'A' && *in
<= 'Z')
508 else if (*in
>= 'a' && *in
<= 'z')
509 base64
= *in
- 'a' + 26;
510 else if (*in
>= '0' && *in
<= '9')
511 base64
= *in
- '0' + 52;
522 * Store the result in the appropriate chars...
529 *outptr
= base64
<< 2;
534 *outptr
++ |= (base64
>> 4) & 3;
536 *outptr
= (base64
<< 4) & 255;
541 *outptr
++ |= (base64
>> 2) & 15;
543 *outptr
= (base64
<< 6) & 255;
557 * Return the decoded string and size...
560 *outlen
= (int)(outptr
- out
);
567 * 'httpEncode64()' - Base64-encode a string.
569 * This function is deprecated. Use the httpEncode64_2() function instead
570 * which provides buffer length arguments.
575 char * /* O - Encoded string */
576 httpEncode64(char *out
, /* I - String to write to */
577 const char *in
) /* I - String to read from */
579 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
584 * 'httpEncode64_2()' - Base64-encode a string.
586 * @since CUPS 1.1.21/Mac OS X 10.4@
589 char * /* O - Encoded string */
590 httpEncode64_2(char *out
, /* I - String to write to */
591 int outlen
, /* I - Size of output string */
592 const char *in
, /* I - String to read from */
593 int inlen
) /* I - Size of input string */
595 char *outptr
, /* Output pointer */
596 *outend
; /* End of output buffer */
597 static const char base64
[] = /* Base64 characters... */
599 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
600 "abcdefghijklmnopqrstuvwxyz"
607 * Range check input...
610 if (!out
|| outlen
< 1 || !in
)
614 * Convert bytes to base-64...
617 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
620 * Encode the up to 3 characters as 4 Base64 numbers...
624 *outptr
++ = base64
[(in
[0] & 255) >> 2];
629 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
631 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
648 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
650 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
663 *outptr
++ = base64
[in
[0] & 63];
669 * Return the encoded string...
677 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
682 const char * /* O - Date/time string */
683 httpGetDateString(time_t t
) /* I - UNIX time */
685 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
688 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
693 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
695 * @since CUPS 1.2/Mac OS X 10.5@
698 const char * /* O - Date/time string */
699 httpGetDateString2(time_t t
, /* I - UNIX time */
700 char *s
, /* I - String buffer */
701 int slen
) /* I - Size of string buffer */
703 struct tm
*tdate
; /* UNIX date/time data */
707 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
708 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
709 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
710 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
717 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
720 time_t /* O - UNIX time */
721 httpGetDateTime(const char *s
) /* I - Date/time string */
723 int i
; /* Looping var */
724 char mon
[16]; /* Abbreviated month name */
725 int day
, year
; /* Day of month and year */
726 int hour
, min
, sec
; /* Time */
727 int days
; /* Number of days since 1970 */
728 static const int normal_days
[] = /* Days to a month, normal years */
729 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
730 static const int leap_days
[] = /* Days to a month, leap years */
731 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
734 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s
));
737 * Extract the date and time from the formatted string...
740 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
743 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
744 "min=%d, sec=%d", day
, mon
, year
, hour
, min
, sec
));
747 * Convert the month name to a number from 0 to 11.
750 for (i
= 0; i
< 12; i
++)
751 if (!strcasecmp(mon
, http_months
[i
]))
757 DEBUG_printf(("4httpGetDateTime: i=%d", i
));
760 * Now convert the date and time to a UNIX time value in seconds since
761 * 1970. We can't use mktime() since the timezone may not be UTC but
762 * the date/time string *is* UTC.
765 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
766 days
= leap_days
[i
] + day
- 1;
768 days
= normal_days
[i
] + day
- 1;
770 DEBUG_printf(("4httpGetDateTime: days=%d", days
));
772 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
773 ((year
- 1) / 4 - 492) - /* + leap days */
774 ((year
- 1) / 100 - 19) + /* - 100 year days */
775 ((year
- 1) / 400 - 4); /* + 400 year days */
777 DEBUG_printf(("4httpGetDateTime: days=%d\n", days
));
779 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
784 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
787 * This function is deprecated; use the httpSeparateURI() function instead.
793 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
794 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
795 char *username
, /* O - Username [1024] */
796 char *host
, /* O - Hostname [1024] */
797 int *port
, /* O - Port number to use */
798 char *resource
) /* O - Resource/filename [1024] */
800 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
801 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
807 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
810 * This function is deprecated; use the httpSeparateURI() function instead.
812 * @since CUPS 1.1.21/Mac OS X 10.4@
817 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
818 char *scheme
, /* O - Scheme (http, https, etc.) */
819 int schemelen
, /* I - Size of scheme buffer */
820 char *username
, /* O - Username */
821 int usernamelen
, /* I - Size of username buffer */
822 char *host
, /* O - Hostname */
823 int hostlen
, /* I - Size of hostname buffer */
824 int *port
, /* O - Port number to use */
825 char *resource
, /* O - Resource/filename */
826 int resourcelen
) /* I - Size of resource buffer */
828 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
829 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
834 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
837 * @since CUPS 1.2/Mac OS X 10.5@
840 http_uri_status_t
/* O - Result of separation */
842 http_uri_coding_t decoding
, /* I - Decoding flags */
843 const char *uri
, /* I - Universal Resource Identifier */
844 char *scheme
, /* O - Scheme (http, https, etc.) */
845 int schemelen
, /* I - Size of scheme buffer */
846 char *username
, /* O - Username */
847 int usernamelen
, /* I - Size of username buffer */
848 char *host
, /* O - Hostname */
849 int hostlen
, /* I - Size of hostname buffer */
850 int *port
, /* O - Port number to use */
851 char *resource
, /* O - Resource/filename */
852 int resourcelen
) /* I - Size of resource buffer */
854 char *ptr
, /* Pointer into string... */
855 *end
; /* End of string */
856 const char *sep
; /* Separator character */
857 http_uri_status_t status
; /* Result of separation */
861 * Initialize everything to blank...
864 if (scheme
&& schemelen
> 0)
867 if (username
&& usernamelen
> 0)
870 if (host
&& hostlen
> 0)
876 if (resource
&& resourcelen
> 0)
880 * Range check input...
883 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
884 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
886 return (HTTP_URI_BAD_ARGUMENTS
);
889 return (HTTP_URI_BAD_URI
);
892 * Grab the scheme portion of the URI...
895 status
= HTTP_URI_OK
;
897 if (!strncmp(uri
, "//", 2))
900 * Workaround for HP IPP client bug...
903 strlcpy(scheme
, "ipp", schemelen
);
904 status
= HTTP_URI_MISSING_SCHEME
;
906 else if (*uri
== '/')
912 strlcpy(scheme
, "file", schemelen
);
913 status
= HTTP_URI_MISSING_SCHEME
;
918 * Standard URI with scheme...
921 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
922 *uri
&& *uri
!= ':' && ptr
< end
;)
923 if (isalnum(*uri
& 255) || *uri
== '-' || *uri
== '+' || *uri
== '.')
933 return (HTTP_URI_BAD_SCHEME
);
940 * Set the default port number...
943 if (!strcmp(scheme
, "http"))
945 else if (!strcmp(scheme
, "https"))
947 else if (!strcmp(scheme
, "ipp"))
949 else if (!strcasecmp(scheme
, "lpd"))
951 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
953 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
954 status
= HTTP_URI_UNKNOWN_SCHEME
;
957 * Now see if we have a hostname...
960 if (!strncmp(uri
, "//", 2))
969 * Grab the username, if any...
972 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
975 * Get a username:password combo...
978 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
979 decoding
& HTTP_URI_CODING_USERNAME
);
984 return (HTTP_URI_BAD_USERNAME
);
991 * Then the hostname/IP address...
997 * Grab IPv6 address...
1001 if (!strncmp(uri
, "v1.", 3))
1002 uri
+= 3; /* Skip IPvN leader... */
1004 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
1005 decoding
& HTTP_URI_CODING_HOSTNAME
);
1010 return (HTTP_URI_BAD_HOSTNAME
);
1020 return (HTTP_URI_BAD_HOSTNAME
);
1025 for (ptr
= host
; *ptr
; ptr
++)
1029 * Convert zone separator to % and stop here...
1035 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1038 return (HTTP_URI_BAD_HOSTNAME
);
1044 * Validate the hostname or IPv4 address first...
1047 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1048 if (strchr(":?/", *ptr
))
1050 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1051 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1055 "!$&'()*+,;=\\", *ptr
))
1058 return (HTTP_URI_BAD_HOSTNAME
);
1062 * Then copy the hostname or IPv4 address to the buffer...
1065 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1066 decoding
& HTTP_URI_CODING_HOSTNAME
);
1071 return (HTTP_URI_BAD_HOSTNAME
);
1076 * Validate hostname for file scheme - only empty and localhost are
1080 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1083 return (HTTP_URI_BAD_HOSTNAME
);
1087 * See if we have a port number...
1093 * Yes, collect the port number...
1096 if (!isdigit(uri
[1] & 255))
1099 return (HTTP_URI_BAD_PORT
);
1102 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1104 if (*uri
!= '/' && *uri
)
1107 return (HTTP_URI_BAD_PORT
);
1113 * The remaining portion is the resource string...
1116 if (*uri
== '?' || !*uri
)
1119 * Hostname but no path...
1122 status
= HTTP_URI_MISSING_RESOURCE
;
1126 * Copy any query string...
1130 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1131 decoding
& HTTP_URI_CODING_QUERY
);
1137 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1138 decoding
& HTTP_URI_CODING_RESOURCE
);
1140 if (uri
&& *uri
== '?')
1143 * Concatenate any query string...
1146 char *resptr
= resource
+ strlen(resource
);
1148 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1149 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1156 return (HTTP_URI_BAD_RESOURCE
);
1160 * Return the URI separation status...
1168 * 'httpStatus()' - Return a short string describing a HTTP status code.
1170 * The returned string is localized to the current POSIX locale and is based
1171 * on the status strings defined in RFC 2616.
1174 const char * /* O - Localized status string */
1175 httpStatus(http_status_t status
) /* I - HTTP status code */
1177 const char *s
; /* Status string */
1178 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1181 if (!cg
->lang_default
)
1182 cg
->lang_default
= cupsLangDefault();
1186 case HTTP_CONTINUE
:
1189 case HTTP_SWITCHING_PROTOCOLS
:
1190 s
= _("Switching Protocols");
1198 case HTTP_ACCEPTED
:
1201 case HTTP_NO_CONTENT
:
1202 s
= _("No Content");
1204 case HTTP_MOVED_PERMANENTLY
:
1205 s
= _("Moved Permanently");
1207 case HTTP_SEE_OTHER
:
1210 case HTTP_NOT_MODIFIED
:
1211 s
= _("Not Modified");
1213 case HTTP_BAD_REQUEST
:
1214 s
= _("Bad Request");
1216 case HTTP_UNAUTHORIZED
:
1217 case HTTP_AUTHORIZATION_CANCELED
:
1218 s
= _("Unauthorized");
1220 case HTTP_FORBIDDEN
:
1223 case HTTP_NOT_FOUND
:
1226 case HTTP_REQUEST_TOO_LARGE
:
1227 s
= _("Request Entity Too Large");
1229 case HTTP_URI_TOO_LONG
:
1230 s
= _("URI Too Long");
1232 case HTTP_UPGRADE_REQUIRED
:
1233 s
= _("Upgrade Required");
1235 case HTTP_NOT_IMPLEMENTED
:
1236 s
= _("Not Implemented");
1238 case HTTP_NOT_SUPPORTED
:
1239 s
= _("Not Supported");
1241 case HTTP_EXPECTATION_FAILED
:
1242 s
= _("Expectation Failed");
1244 case HTTP_SERVICE_UNAVAILABLE
:
1245 s
= _("Service Unavailable");
1247 case HTTP_SERVER_ERROR
:
1248 s
= _("Internal Server Error");
1256 return (_cupsLangString(cg
->lang_default
, s
));
1260 #ifndef HAVE_HSTRERROR
1262 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1265 const char * /* O - Error string */
1266 _cups_hstrerror(int error
) /* I - Error number */
1268 static const char * const errors
[] = /* Error strings */
1273 "Unrecoverable lookup error.",
1274 "No data associated with name."
1278 if (error
< 0 || error
> 4)
1279 return ("Unknown hostname lookup error.");
1281 return (errors
[error
]);
1283 #endif /* !HAVE_HSTRERROR */
1287 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1290 char * /* O - Encoded URI */
1291 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1292 const char *src
, /* I - Source URI */
1293 size_t dstsize
) /* I - Size of destination buffer */
1295 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1301 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1304 const char * /* O - Resolved URI */
1306 const char *uri
, /* I - DNS-SD URI */
1307 char *resolved_uri
, /* I - Buffer for resolved URI */
1308 size_t resolved_size
, /* I - Size of URI buffer */
1309 int logit
) /* I - Log progress to stderr? */
1311 char scheme
[32], /* URI components... */
1317 http_uri_status_t status
; /* URI decode status */
1321 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1322 "resolved_size=" CUPS_LLFMT
")", uri
, resolved_uri
,
1323 CUPS_LLCAST resolved_size
));
1326 * Get the device URI...
1330 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1331 sizeof(scheme
), userpass
, sizeof(userpass
),
1332 hostname
, sizeof(hostname
), &port
, resource
,
1333 sizeof(resource
))) < HTTP_URI_OK
)
1335 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1336 sizeof(scheme
), userpass
, sizeof(userpass
),
1337 hostname
, sizeof(hostname
), &port
, resource
,
1338 sizeof(resource
)) < HTTP_URI_OK
)
1342 _cupsLangPrintf(stderr
, _("Bad device URI \"%s\"\n"), uri
);
1344 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status
));
1345 DEBUG_puts("5_httpResolveURI: Returning NULL");
1350 * Resolve it as needed...
1353 if (strstr(hostname
, "._tcp"))
1356 DNSServiceRef ref
, /* DNS-SD master service reference */
1357 domainref
, /* DNS-SD service reference for domain */
1358 localref
; /* DNS-SD service reference for .local */
1359 int domainsent
= 0; /* Send the domain resolve? */
1360 char *regtype
, /* Pointer to type in hostname */
1361 *domain
; /* Pointer to domain in hostname */
1362 _http_uribuf_t uribuf
; /* URI buffer */
1364 struct pollfd polldata
; /* Polling data */
1365 #else /* select() */
1366 fd_set input_set
; /* Input set for select() */
1367 struct timeval stimeout
; /* Timeout value for select() */
1368 #endif /* HAVE_POLL */
1371 fprintf(stderr
, "DEBUG: Resolving \"%s\"...\n", hostname
);
1374 * Separate the hostname into service name, registration type, and domain...
1377 for (regtype
= strstr(hostname
, "._tcp") - 2;
1380 if (regtype
[0] == '.' && regtype
[1] == '_')
1383 * Found ._servicetype in front of ._tcp...
1390 if (regtype
<= hostname
)
1392 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1396 for (domain
= strchr(regtype
, '.');
1398 domain
= strchr(domain
+ 1, '.'))
1399 if (domain
[1] != '_')
1405 uribuf
.buffer
= resolved_uri
;
1406 uribuf
.bufsize
= resolved_size
;
1408 resolved_uri
[0] = '\0';
1410 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1411 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1414 fputs("STATE: +connecting-to-device\n", stderr
);
1415 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1416 "domain=\"local.\"...\n", hostname
, regtype
);
1421 if (DNSServiceCreateConnection(&ref
) == kDNSServiceErr_NoError
)
1424 if (DNSServiceResolve(&localref
, kDNSServiceFlagsShareConnection
, 0,
1425 hostname
, regtype
, "local.", resolve_callback
,
1426 &uribuf
) == kDNSServiceErr_NoError
)
1428 int fds
; /* Number of ready descriptors */
1429 time_t timeout
, /* Poll timeout */
1430 start_time
= time(NULL
);/* Start time */
1435 _cupsLangPuts(stderr
, _("INFO: Looking for printer...\n"));
1438 * For the first minute, wakeup every 2 seconds to emit a
1439 * "looking for printer" message...
1442 timeout
= (time(NULL
) < (start_time
+ 60)) ? 2000 : -1;
1445 polldata
.fd
= DNSServiceRefSockFD(ref
);
1446 polldata
.events
= POLLIN
;
1448 fds
= poll(&polldata
, 1, timeout
);
1450 #else /* select() */
1451 FD_ZERO(&input_set
);
1452 FD_SET(DNSServiceRefSockFD(ref
), &input_set
);
1454 stimeout
.tv_sec
= ((int)timeout
) / 1000;
1455 stimeout
.tv_usec
= ((int)(timeout
) * 1000) % 1000000;
1457 fds
= select(DNSServiceRefSockFD(ref
)+1, &input_set
, NULL
, NULL
,
1458 timeout
< 0.0 ? NULL
: &stimeout
);
1459 #endif /* HAVE_POLL */
1463 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1465 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno
)));
1472 * Wait 2 seconds for a response to the local resolve; if nothing
1473 * comes in, do an additional domain resolution...
1476 if (domainsent
== 0 && strcasecmp(domain
, "local."))
1480 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1481 "domain=\"%s\"...\n", hostname
, regtype
, domain
);
1484 if (DNSServiceResolve(&domainref
, kDNSServiceFlagsShareConnection
, 0,
1485 hostname
, regtype
, domain
, resolve_callback
,
1486 &uribuf
) == kDNSServiceErr_NoError
)
1492 if (DNSServiceProcessResult(ref
) == kDNSServiceErr_NoError
)
1501 DNSServiceRefDeallocate(domainref
);
1503 DNSServiceRefDeallocate(localref
);
1506 DNSServiceRefDeallocate(ref
);
1512 fprintf(stderr
, "DEBUG: Resolved as \"%s\"...\n", uri
);
1514 fputs("DEBUG: Unable to resolve URI\n", stderr
);
1516 fputs("STATE: -connecting-to-device\n", stderr
);
1521 * No DNS-SD support...
1525 #endif /* HAVE_DNSSD */
1528 _cupsLangPuts(stderr
, _("Unable to find printer\n"));
1531 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri
));
1538 * 'http_copy_decode()' - Copy and decode a URI.
1541 static const char * /* O - New source pointer or NULL on error */
1542 http_copy_decode(char *dst
, /* O - Destination buffer */
1543 const char *src
, /* I - Source pointer */
1544 int dstsize
, /* I - Destination size */
1545 const char *term
, /* I - Terminating characters */
1546 int decode
) /* I - Decode %-encoded values */
1548 char *ptr
, /* Pointer into buffer */
1549 *end
; /* End of buffer */
1550 int quoted
; /* Quoted character */
1554 * Copy the src to the destination until we hit a terminating character
1555 * or the end of the string.
1558 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1559 *src
&& (!term
|| !strchr(term
, *src
));
1563 if (*src
== '%' && decode
)
1565 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1568 * Grab a hex-encoded character...
1573 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1575 quoted
= (*src
- '0') << 4;
1579 quoted
|= tolower(*src
) - 'a' + 10;
1581 quoted
|= *src
- '0';
1588 * Bad hex-encoded character...
1606 * 'http_copy_encode()' - Copy and encode a URI.
1609 static char * /* O - End of current URI */
1610 http_copy_encode(char *dst
, /* O - Destination buffer */
1611 const char *src
, /* I - Source pointer */
1612 char *dstend
, /* I - End of destination buffer */
1613 const char *reserved
, /* I - Extra reserved characters */
1614 const char *term
, /* I - Terminating characters */
1615 int encode
) /* I - %-encode reserved chars? */
1617 static const char hex
[] = "0123456789ABCDEF";
1620 while (*src
&& dst
< dstend
)
1622 if (term
&& *src
== *term
)
1625 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1626 (reserved
&& strchr(reserved
, *src
))))
1629 * Hex encode reserved characters...
1632 if ((dst
+ 2) >= dstend
)
1636 *dst
++ = hex
[(*src
>> 4) & 15];
1637 *dst
++ = hex
[*src
& 15];
1656 * 'resolve_callback()' - Build a device URI for the given service name.
1659 static void DNSSD_API
1661 DNSServiceRef sdRef
, /* I - Service reference */
1662 DNSServiceFlags flags
, /* I - Results flags */
1663 uint32_t interfaceIndex
, /* I - Interface number */
1664 DNSServiceErrorType errorCode
, /* I - Error, if any */
1665 const char *fullName
, /* I - Full service name */
1666 const char *hostTarget
, /* I - Hostname */
1667 uint16_t port
, /* I - Port number */
1668 uint16_t txtLen
, /* I - Length of TXT record */
1669 const unsigned char *txtRecord
, /* I - TXT record data */
1670 void *context
) /* I - Pointer to URI buffer */
1672 const char *scheme
; /* URI scheme */
1673 char rp
[257]; /* Remote printer */
1674 const void *value
; /* Value from TXT record */
1675 uint8_t valueLen
; /* Length of value */
1676 _http_uribuf_t
*uribuf
; /* URI buffer */
1679 DEBUG_printf(("7resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1680 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1681 "txtLen=%u, txtRecord=%p, context=%p)", sdRef
, flags
,
1682 interfaceIndex
, errorCode
, fullName
, hostTarget
, port
, txtLen
,
1683 txtRecord
, context
));
1686 * Figure out the scheme from the full name...
1689 if (strstr(fullName
, "._ipp") || strstr(fullName
, "._fax-ipp"))
1691 else if (strstr(fullName
, "._printer."))
1693 else if (strstr(fullName
, "._pdl-datastream."))
1696 scheme
= "riousbprint";
1699 * Extract the "remote printer" key from the TXT record...
1702 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "rp",
1703 &valueLen
)) != NULL
)
1706 * Convert to resource by concatenating with a leading "/"...
1710 memcpy(rp
+ 1, value
, valueLen
);
1711 rp
[valueLen
+ 1] = '\0';
1717 * Assemble the final device URI...
1720 uribuf
= (_http_uribuf_t
*)context
;
1722 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, uribuf
->bufsize
, scheme
,
1723 NULL
, hostTarget
, ntohs(port
), rp
);
1725 DEBUG_printf(("8resolve_callback: Resolved URI is \"%s\"...",
1728 #endif /* HAVE_DNSSD */
1732 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".