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 * _httpDecodeURI() - Percent-decode a HTTP request URI.
40 * _httpEncodeURI() - Percent-encode a HTTP request URI.
41 * _httpResolveURI() - Resolve a DNS-SD URI.
42 * http_copy_decode() - Copy and decode a URI.
43 * http_copy_encode() - Copy and encode a URI.
44 * resolve_callback() - Build a device URI for the given service name.
48 * Include necessary headers...
51 #include "cups-private.h"
56 # elif defined(HAVE_POLL)
59 # include <sys/select.h>
61 #endif /* HAVE_DNSSD */
68 typedef struct _http_uribuf_s
/* URI buffer */
70 char *buffer
; /* Pointer to buffer */
71 size_t bufsize
; /* Size of buffer */
79 static const char * const http_days
[7] =
89 static const char * const http_months
[12] =
110 static const char *http_copy_decode(char *dst
, const char *src
,
111 int dstsize
, const char *term
,
113 static char *http_copy_encode(char *dst
, const char *src
,
114 char *dstend
, const char *reserved
,
115 const char *term
, int encode
);
117 static void DNSSD_API
resolve_callback(DNSServiceRef sdRef
,
118 DNSServiceFlags flags
,
119 uint32_t interfaceIndex
,
120 DNSServiceErrorType errorCode
,
121 const char *fullName
,
122 const char *hostTarget
,
123 uint16_t port
, uint16_t txtLen
,
124 const unsigned char *txtRecord
,
126 #endif /* HAVE_DNSSD */
130 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
133 * This function escapes reserved characters in the URI depending on the
134 * value of the "encoding" argument. You should use this function in
135 * place of traditional string functions whenever you need to create a
138 * @since CUPS 1.2/Mac OS X 10.5@
141 http_uri_status_t
/* O - URI status */
143 http_uri_coding_t encoding
, /* I - Encoding flags */
144 char *uri
, /* I - URI buffer */
145 int urilen
, /* I - Size of URI buffer */
146 const char *scheme
, /* I - Scheme name */
147 const char *username
, /* I - Username */
148 const char *host
, /* I - Hostname or address */
149 int port
, /* I - Port number */
150 const char *resource
) /* I - Resource */
152 char *ptr
, /* Pointer into URI buffer */
153 *end
; /* End of URI buffer */
157 * Range check input...
160 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
165 return (HTTP_URI_BAD_ARGUMENTS
);
169 * Assemble the URI starting with the scheme...
172 end
= uri
+ urilen
- 1;
173 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
176 goto assemble_overflow
;
178 if (!strcmp(scheme
, "mailto"))
181 * mailto: only has :, no //...
187 goto assemble_overflow
;
192 * Schemes other than mailto: all have //...
202 goto assemble_overflow
;
206 * Next the username and hostname, if any...
211 if (username
&& *username
)
214 * Add username@ first...
217 ptr
= http_copy_encode(ptr
, username
, end
, "/?@", NULL
,
218 encoding
& HTTP_URI_CODING_USERNAME
);
221 goto assemble_overflow
;
226 goto assemble_overflow
;
230 * Then add the hostname. Since IPv6 is a particular pain to deal
231 * with, we have several special cases to deal with. If we get
232 * an IPv6 address with brackets around it, assume it is already in
233 * URI format. Since DNS-SD service names can sometimes look like
234 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
238 if (host
[0] != '[' && strchr(host
, ':') && !strstr(host
, "._tcp"))
241 * We have a raw IPv6 address...
244 if (strchr(host
, '%'))
247 * We have a link-local address, add "[v1." prefix...
258 goto assemble_overflow
;
263 * We have a normal address, add "[" prefix...
269 goto assemble_overflow
;
273 * Copy the rest of the IPv6 address, and terminate with "]".
276 while (ptr
< end
&& *host
)
280 *ptr
++ = '+'; /* Convert zone separator */
288 goto assemble_overflow
;
293 goto assemble_overflow
;
298 * Otherwise, just copy the host string...
301 ptr
= http_copy_encode(ptr
, host
, end
, ":/?#[]@\\\"", NULL
,
302 encoding
& HTTP_URI_CODING_HOSTNAME
);
305 goto assemble_overflow
;
309 * Finish things off with the port number...
314 snprintf(ptr
, end
- ptr
+ 1, ":%d", port
);
318 goto assemble_overflow
;
323 * Last but not least, add the resource string...
328 char *query
; /* Pointer to query string */
332 * Copy the resource string up to the query string if present...
335 query
= strchr(resource
, '?');
336 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
337 encoding
& HTTP_URI_CODING_RESOURCE
);
339 goto assemble_overflow
;
344 * Copy query string without encoding...
347 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
348 encoding
& HTTP_URI_CODING_QUERY
);
350 goto assemble_overflow
;
356 goto assemble_overflow
;
359 * Nul-terminate the URI buffer and return with no errors...
364 return (HTTP_URI_OK
);
367 * Clear the URI string and return an overflow error; I don't usually
368 * like goto's, but in this case it makes sense...
374 return (HTTP_URI_OVERFLOW
);
379 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
380 * components with a formatted resource.
382 * This function creates a formatted version of the resource string
383 * argument "resourcef" and escapes reserved characters in the URI
384 * depending on the value of the "encoding" argument. You should use
385 * this function in place of traditional string functions whenever
386 * you need to create a URI string.
388 * @since CUPS 1.2/Mac OS X 10.5@
391 http_uri_status_t
/* O - URI status */
393 http_uri_coding_t encoding
, /* I - Encoding flags */
394 char *uri
, /* I - URI buffer */
395 int urilen
, /* I - Size of URI buffer */
396 const char *scheme
, /* I - Scheme name */
397 const char *username
, /* I - Username */
398 const char *host
, /* I - Hostname or address */
399 int port
, /* I - Port number */
400 const char *resourcef
, /* I - Printf-style resource */
401 ...) /* I - Additional arguments as needed */
403 va_list ap
; /* Pointer to additional arguments */
404 char resource
[1024]; /* Formatted resource string */
405 int bytes
; /* Bytes in formatted string */
409 * Range check input...
412 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
417 return (HTTP_URI_BAD_ARGUMENTS
);
421 * Format the resource string and assemble the URI...
424 va_start(ap
, resourcef
);
425 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
428 if (bytes
>= sizeof(resource
))
431 return (HTTP_URI_OVERFLOW
);
434 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
440 * 'httpDecode64()' - Base64-decode a string.
442 * This function is deprecated. Use the httpDecode64_2() function instead
443 * which provides buffer length arguments.
448 char * /* O - Decoded string */
449 httpDecode64(char *out
, /* I - String to write to */
450 const char *in
) /* I - String to read from */
452 int outlen
; /* Output buffer length */
456 * Use the old maximum buffer size for binary compatibility...
461 return (httpDecode64_2(out
, &outlen
, in
));
466 * 'httpDecode64_2()' - Base64-decode a string.
468 * @since CUPS 1.1.21/Mac OS X 10.4@
471 char * /* O - Decoded string */
472 httpDecode64_2(char *out
, /* I - String to write to */
473 int *outlen
, /* IO - Size of output string */
474 const char *in
) /* I - String to read from */
476 int pos
, /* Bit position */
477 base64
; /* Value of this character */
478 char *outptr
, /* Output pointer */
479 *outend
; /* End of output buffer */
483 * Range check input...
486 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
498 * Convert from base-64 to bytes...
501 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
504 * Decode this character into a number from 0 to 63...
507 if (*in
>= 'A' && *in
<= 'Z')
509 else if (*in
>= 'a' && *in
<= 'z')
510 base64
= *in
- 'a' + 26;
511 else if (*in
>= '0' && *in
<= '9')
512 base64
= *in
- '0' + 52;
523 * Store the result in the appropriate chars...
530 *outptr
= base64
<< 2;
535 *outptr
++ |= (base64
>> 4) & 3;
537 *outptr
= (base64
<< 4) & 255;
542 *outptr
++ |= (base64
>> 2) & 15;
544 *outptr
= (base64
<< 6) & 255;
558 * Return the decoded string and size...
561 *outlen
= (int)(outptr
- out
);
568 * 'httpEncode64()' - Base64-encode a string.
570 * This function is deprecated. Use the httpEncode64_2() function instead
571 * which provides buffer length arguments.
576 char * /* O - Encoded string */
577 httpEncode64(char *out
, /* I - String to write to */
578 const char *in
) /* I - String to read from */
580 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
585 * 'httpEncode64_2()' - Base64-encode a string.
587 * @since CUPS 1.1.21/Mac OS X 10.4@
590 char * /* O - Encoded string */
591 httpEncode64_2(char *out
, /* I - String to write to */
592 int outlen
, /* I - Size of output string */
593 const char *in
, /* I - String to read from */
594 int inlen
) /* I - Size of input string */
596 char *outptr
, /* Output pointer */
597 *outend
; /* End of output buffer */
598 static const char base64
[] = /* Base64 characters... */
600 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
601 "abcdefghijklmnopqrstuvwxyz"
608 * Range check input...
611 if (!out
|| outlen
< 1 || !in
)
615 * Convert bytes to base-64...
618 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
621 * Encode the up to 3 characters as 4 Base64 numbers...
625 *outptr
++ = base64
[(in
[0] & 255) >> 2];
630 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
632 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
649 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
651 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
664 *outptr
++ = base64
[in
[0] & 63];
670 * Return the encoded string...
678 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
683 const char * /* O - Date/time string */
684 httpGetDateString(time_t t
) /* I - UNIX time */
686 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
689 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
694 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
696 * @since CUPS 1.2/Mac OS X 10.5@
699 const char * /* O - Date/time string */
700 httpGetDateString2(time_t t
, /* I - UNIX time */
701 char *s
, /* I - String buffer */
702 int slen
) /* I - Size of string buffer */
704 struct tm
*tdate
; /* UNIX date/time data */
708 snprintf(s
, slen
, "%s, %02d %s %d %02d:%02d:%02d GMT",
709 http_days
[tdate
->tm_wday
], tdate
->tm_mday
,
710 http_months
[tdate
->tm_mon
], tdate
->tm_year
+ 1900,
711 tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
718 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
721 time_t /* O - UNIX time */
722 httpGetDateTime(const char *s
) /* I - Date/time string */
724 int i
; /* Looping var */
725 char mon
[16]; /* Abbreviated month name */
726 int day
, year
; /* Day of month and year */
727 int hour
, min
, sec
; /* Time */
728 int days
; /* Number of days since 1970 */
729 static const int normal_days
[] = /* Days to a month, normal years */
730 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
731 static const int leap_days
[] = /* Days to a month, leap years */
732 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
735 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s
));
738 * Extract the date and time from the formatted string...
741 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
744 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
745 "min=%d, sec=%d", day
, mon
, year
, hour
, min
, sec
));
748 * Convert the month name to a number from 0 to 11.
751 for (i
= 0; i
< 12; i
++)
752 if (!strcasecmp(mon
, http_months
[i
]))
758 DEBUG_printf(("4httpGetDateTime: i=%d", i
));
761 * Now convert the date and time to a UNIX time value in seconds since
762 * 1970. We can't use mktime() since the timezone may not be UTC but
763 * the date/time string *is* UTC.
766 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
767 days
= leap_days
[i
] + day
- 1;
769 days
= normal_days
[i
] + day
- 1;
771 DEBUG_printf(("4httpGetDateTime: days=%d", days
));
773 days
+= (year
- 1970) * 365 + /* 365 days per year (normally) */
774 ((year
- 1) / 4 - 492) - /* + leap days */
775 ((year
- 1) / 100 - 19) + /* - 100 year days */
776 ((year
- 1) / 400 - 4); /* + 400 year days */
778 DEBUG_printf(("4httpGetDateTime: days=%d\n", days
));
780 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
785 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
788 * This function is deprecated; use the httpSeparateURI() function instead.
794 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
795 char *scheme
, /* O - Scheme [32] (http, https, etc.) */
796 char *username
, /* O - Username [1024] */
797 char *host
, /* O - Hostname [1024] */
798 int *port
, /* O - Port number to use */
799 char *resource
) /* O - Resource/filename [1024] */
801 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
802 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
808 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
811 * This function is deprecated; use the httpSeparateURI() function instead.
813 * @since CUPS 1.1.21/Mac OS X 10.4@
818 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
819 char *scheme
, /* O - Scheme (http, https, etc.) */
820 int schemelen
, /* I - Size of scheme buffer */
821 char *username
, /* O - Username */
822 int usernamelen
, /* I - Size of username buffer */
823 char *host
, /* O - Hostname */
824 int hostlen
, /* I - Size of hostname buffer */
825 int *port
, /* O - Port number to use */
826 char *resource
, /* O - Resource/filename */
827 int resourcelen
) /* I - Size of resource buffer */
829 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
830 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
835 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
838 * @since CUPS 1.2/Mac OS X 10.5@
841 http_uri_status_t
/* O - Result of separation */
843 http_uri_coding_t decoding
, /* I - Decoding flags */
844 const char *uri
, /* I - Universal Resource Identifier */
845 char *scheme
, /* O - Scheme (http, https, etc.) */
846 int schemelen
, /* I - Size of scheme buffer */
847 char *username
, /* O - Username */
848 int usernamelen
, /* I - Size of username buffer */
849 char *host
, /* O - Hostname */
850 int hostlen
, /* I - Size of hostname buffer */
851 int *port
, /* O - Port number to use */
852 char *resource
, /* O - Resource/filename */
853 int resourcelen
) /* I - Size of resource buffer */
855 char *ptr
, /* Pointer into string... */
856 *end
; /* End of string */
857 const char *sep
; /* Separator character */
858 http_uri_status_t status
; /* Result of separation */
862 * Initialize everything to blank...
865 if (scheme
&& schemelen
> 0)
868 if (username
&& usernamelen
> 0)
871 if (host
&& hostlen
> 0)
877 if (resource
&& resourcelen
> 0)
881 * Range check input...
884 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
885 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
887 return (HTTP_URI_BAD_ARGUMENTS
);
890 return (HTTP_URI_BAD_URI
);
893 * Grab the scheme portion of the URI...
896 status
= HTTP_URI_OK
;
898 if (!strncmp(uri
, "//", 2))
901 * Workaround for HP IPP client bug...
904 strlcpy(scheme
, "ipp", schemelen
);
905 status
= HTTP_URI_MISSING_SCHEME
;
907 else if (*uri
== '/')
913 strlcpy(scheme
, "file", schemelen
);
914 status
= HTTP_URI_MISSING_SCHEME
;
919 * Standard URI with scheme...
922 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
923 *uri
&& *uri
!= ':' && ptr
< end
;)
924 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
925 "abcdefghijklmnopqrstuvwxyz"
926 "0123456789-+.", *uri
) != NULL
)
936 return (HTTP_URI_BAD_SCHEME
);
943 * Set the default port number...
946 if (!strcmp(scheme
, "http"))
948 else if (!strcmp(scheme
, "https"))
950 else if (!strcmp(scheme
, "ipp") || !strcmp(scheme
, "ipps"))
952 else if (!strcasecmp(scheme
, "lpd"))
954 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
956 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto"))
957 status
= HTTP_URI_UNKNOWN_SCHEME
;
960 * Now see if we have a hostname...
963 if (!strncmp(uri
, "//", 2))
972 * Grab the username, if any...
975 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
978 * Get a username:password combo...
981 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
982 decoding
& HTTP_URI_CODING_USERNAME
);
987 return (HTTP_URI_BAD_USERNAME
);
994 * Then the hostname/IP address...
1000 * Grab IPv6 address...
1004 if (!strncmp(uri
, "v1.", 3))
1005 uri
+= 3; /* Skip IPvN leader... */
1007 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
1008 decoding
& HTTP_URI_CODING_HOSTNAME
);
1013 return (HTTP_URI_BAD_HOSTNAME
);
1023 return (HTTP_URI_BAD_HOSTNAME
);
1028 for (ptr
= host
; *ptr
; ptr
++)
1032 * Convert zone separator to % and stop here...
1038 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1041 return (HTTP_URI_BAD_HOSTNAME
);
1047 * Validate the hostname or IPv4 address first...
1050 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1051 if (strchr(":?/", *ptr
))
1053 else if (!strchr("abcdefghijklmnopqrstuvwxyz"
1054 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1058 "!$&'()*+,;=\\", *ptr
))
1061 return (HTTP_URI_BAD_HOSTNAME
);
1065 * Then copy the hostname or IPv4 address to the buffer...
1068 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1069 decoding
& HTTP_URI_CODING_HOSTNAME
);
1074 return (HTTP_URI_BAD_HOSTNAME
);
1079 * Validate hostname for file scheme - only empty and localhost are
1083 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1086 return (HTTP_URI_BAD_HOSTNAME
);
1090 * See if we have a port number...
1096 * Yes, collect the port number...
1099 if (!isdigit(uri
[1] & 255))
1102 return (HTTP_URI_BAD_PORT
);
1105 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
1107 if (*uri
!= '/' && *uri
)
1110 return (HTTP_URI_BAD_PORT
);
1116 * The remaining portion is the resource string...
1119 if (*uri
== '?' || !*uri
)
1122 * Hostname but no path...
1125 status
= HTTP_URI_MISSING_RESOURCE
;
1129 * Copy any query string...
1133 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1134 decoding
& HTTP_URI_CODING_QUERY
);
1140 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1141 decoding
& HTTP_URI_CODING_RESOURCE
);
1143 if (uri
&& *uri
== '?')
1146 * Concatenate any query string...
1149 char *resptr
= resource
+ strlen(resource
);
1151 uri
= http_copy_decode(resptr
, uri
, resourcelen
- (int)(resptr
- resource
),
1152 NULL
, decoding
& HTTP_URI_CODING_QUERY
);
1159 return (HTTP_URI_BAD_RESOURCE
);
1163 * Return the URI separation status...
1171 * 'httpStatus()' - Return a short string describing a HTTP status code.
1173 * The returned string is localized to the current POSIX locale and is based
1174 * on the status strings defined in RFC 2616.
1177 const char * /* O - Localized status string */
1178 httpStatus(http_status_t status
) /* I - HTTP status code */
1180 const char *s
; /* Status string */
1181 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1184 if (!cg
->lang_default
)
1185 cg
->lang_default
= cupsLangDefault();
1189 case HTTP_CONTINUE
:
1192 case HTTP_SWITCHING_PROTOCOLS
:
1193 s
= _("Switching Protocols");
1201 case HTTP_ACCEPTED
:
1204 case HTTP_NO_CONTENT
:
1205 s
= _("No Content");
1207 case HTTP_MOVED_PERMANENTLY
:
1208 s
= _("Moved Permanently");
1210 case HTTP_SEE_OTHER
:
1213 case HTTP_NOT_MODIFIED
:
1214 s
= _("Not Modified");
1216 case HTTP_BAD_REQUEST
:
1217 s
= _("Bad Request");
1219 case HTTP_UNAUTHORIZED
:
1220 case HTTP_AUTHORIZATION_CANCELED
:
1221 s
= _("Unauthorized");
1223 case HTTP_FORBIDDEN
:
1226 case HTTP_NOT_FOUND
:
1229 case HTTP_REQUEST_TOO_LARGE
:
1230 s
= _("Request Entity Too Large");
1232 case HTTP_URI_TOO_LONG
:
1233 s
= _("URI Too Long");
1235 case HTTP_UPGRADE_REQUIRED
:
1236 s
= _("Upgrade Required");
1238 case HTTP_NOT_IMPLEMENTED
:
1239 s
= _("Not Implemented");
1241 case HTTP_NOT_SUPPORTED
:
1242 s
= _("Not Supported");
1244 case HTTP_EXPECTATION_FAILED
:
1245 s
= _("Expectation Failed");
1247 case HTTP_SERVICE_UNAVAILABLE
:
1248 s
= _("Service Unavailable");
1250 case HTTP_SERVER_ERROR
:
1251 s
= _("Internal Server Error");
1253 case HTTP_PKI_ERROR
:
1254 s
= _("SSL/TLS Negotiation Error");
1256 case HTTP_WEBIF_DISABLED
:
1257 s
= _("Web Interface is Disabled");
1265 return (_cupsLangString(cg
->lang_default
, s
));
1269 #ifndef HAVE_HSTRERROR
1271 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
1274 const char * /* O - Error string */
1275 _cups_hstrerror(int error
) /* I - Error number */
1277 static const char * const errors
[] = /* Error strings */
1282 "Unrecoverable lookup error.",
1283 "No data associated with name."
1287 if (error
< 0 || error
> 4)
1288 return ("Unknown hostname lookup error.");
1290 return (errors
[error
]);
1292 #endif /* !HAVE_HSTRERROR */
1296 * '_httpDecodeURI()' - Percent-decode a HTTP request URI.
1299 char * /* O - Decoded URI or NULL on error */
1300 _httpDecodeURI(char *dst
, /* I - Destination buffer */
1301 const char *src
, /* I - Source URI */
1302 size_t dstsize
) /* I - Size of destination buffer */
1304 if (http_copy_decode(dst
, src
, (int)dstsize
, NULL
, 1))
1312 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1315 char * /* O - Encoded URI */
1316 _httpEncodeURI(char *dst
, /* I - Destination buffer */
1317 const char *src
, /* I - Source URI */
1318 size_t dstsize
) /* I - Size of destination buffer */
1320 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1326 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1329 const char * /* O - Resolved URI */
1331 const char *uri
, /* I - DNS-SD URI */
1332 char *resolved_uri
, /* I - Buffer for resolved URI */
1333 size_t resolved_size
, /* I - Size of URI buffer */
1334 int logit
) /* I - Log progress to stderr? */
1336 char scheme
[32], /* URI components... */
1342 http_uri_status_t status
; /* URI decode status */
1346 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1347 "resolved_size=" CUPS_LLFMT
")", uri
, resolved_uri
,
1348 CUPS_LLCAST resolved_size
));
1351 * Get the device URI...
1355 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1356 sizeof(scheme
), userpass
, sizeof(userpass
),
1357 hostname
, sizeof(hostname
), &port
, resource
,
1358 sizeof(resource
))) < HTTP_URI_OK
)
1360 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1361 sizeof(scheme
), userpass
, sizeof(userpass
),
1362 hostname
, sizeof(hostname
), &port
, resource
,
1363 sizeof(resource
)) < HTTP_URI_OK
)
1367 _cupsLangPrintFilter(stderr
, "ERROR", _("Bad device URI \"%s\"."), uri
);
1369 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status
));
1370 DEBUG_puts("5_httpResolveURI: Returning NULL");
1375 * Resolve it as needed...
1378 if (strstr(hostname
, "._tcp"))
1382 # pragma comment(lib, "dnssd.lib")
1384 DNSServiceRef ref
, /* DNS-SD master service reference */
1385 domainref
, /* DNS-SD service reference for domain */
1386 localref
; /* DNS-SD service reference for .local */
1387 int domainsent
= 0, /* Send the domain resolve? */
1388 offline
= 0; /* offline-report state set? */
1389 char *regtype
, /* Pointer to type in hostname */
1390 *domain
; /* Pointer to domain in hostname */
1391 _http_uribuf_t uribuf
; /* URI buffer */
1393 struct pollfd polldata
; /* Polling data */
1394 #else /* select() */
1395 fd_set input_set
; /* Input set for select() */
1396 struct timeval stimeout
; /* Timeout value for select() */
1397 #endif /* HAVE_POLL */
1400 fprintf(stderr
, "DEBUG: Resolving \"%s\"...\n", hostname
);
1403 * Separate the hostname into service name, registration type, and domain...
1406 for (regtype
= strstr(hostname
, "._tcp") - 2;
1409 if (regtype
[0] == '.' && regtype
[1] == '_')
1412 * Found ._servicetype in front of ._tcp...
1419 if (regtype
<= hostname
)
1421 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1425 for (domain
= strchr(regtype
, '.');
1427 domain
= strchr(domain
+ 1, '.'))
1428 if (domain
[1] != '_')
1434 uribuf
.buffer
= resolved_uri
;
1435 uribuf
.bufsize
= resolved_size
;
1437 resolved_uri
[0] = '\0';
1439 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1440 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1443 fputs("STATE: +connecting-to-device\n", stderr
);
1444 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1445 "domain=\"local.\"...\n", hostname
, regtype
);
1450 if (DNSServiceCreateConnection(&ref
) == kDNSServiceErr_NoError
)
1453 if (DNSServiceResolve(&localref
, kDNSServiceFlagsShareConnection
, 0,
1454 hostname
, regtype
, "local.", resolve_callback
,
1455 &uribuf
) == kDNSServiceErr_NoError
)
1457 int fds
; /* Number of ready descriptors */
1458 time_t timeout
, /* Poll timeout */
1459 start_time
= time(NULL
);/* Start time */
1464 _cupsLangPrintFilter(stderr
, "INFO", _("Looking for printer."));
1467 * For the first minute, wakeup every 2 seconds to emit a
1468 * "looking for printer" message...
1471 timeout
= (time(NULL
) < (start_time
+ 60)) ? 2000 : -1;
1474 polldata
.fd
= DNSServiceRefSockFD(ref
);
1475 polldata
.events
= POLLIN
;
1477 fds
= poll(&polldata
, 1, timeout
);
1479 #else /* select() */
1480 FD_ZERO(&input_set
);
1481 FD_SET(DNSServiceRefSockFD(ref
), &input_set
);
1483 stimeout
.tv_sec
= ((int)timeout
) / 1000;
1484 stimeout
.tv_usec
= ((int)(timeout
) * 1000) % 1000000;
1486 fds
= select(DNSServiceRefSockFD(ref
)+1, &input_set
, NULL
, NULL
,
1487 timeout
< 0.0 ? NULL
: &stimeout
);
1488 #endif /* HAVE_POLL */
1492 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1494 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno
)));
1501 * Wait 2 seconds for a response to the local resolve; if nothing
1502 * comes in, do an additional domain resolution...
1505 if (domainsent
== 0 && strcasecmp(domain
, "local."))
1509 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1510 "domain=\"%s\"...\n", hostname
, regtype
, domain
);
1513 if (DNSServiceResolve(&domainref
, kDNSServiceFlagsShareConnection
, 0,
1514 hostname
, regtype
, domain
, resolve_callback
,
1515 &uribuf
) == kDNSServiceErr_NoError
)
1520 * If it hasn't resolved within 5 seconds set the offline-report
1521 * printer-state-reason...
1524 if (logit
&& offline
== 0 && time(NULL
) > (start_time
+ 5))
1526 fputs("STATE: +offline-report\n", stderr
);
1532 if (DNSServiceProcessResult(ref
) == kDNSServiceErr_NoError
)
1541 DNSServiceRefDeallocate(domainref
);
1543 DNSServiceRefDeallocate(localref
);
1546 DNSServiceRefDeallocate(ref
);
1552 fprintf(stderr
, "DEBUG: Resolved as \"%s\"...\n", uri
);
1554 fputs("DEBUG: Unable to resolve URI\n", stderr
);
1556 fputs("STATE: -connecting-to-device,offline-report\n", stderr
);
1561 * No DNS-SD support...
1565 #endif /* HAVE_DNSSD */
1568 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to find printer."));
1571 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri
));
1578 * 'http_copy_decode()' - Copy and decode a URI.
1581 static const char * /* O - New source pointer or NULL on error */
1582 http_copy_decode(char *dst
, /* O - Destination buffer */
1583 const char *src
, /* I - Source pointer */
1584 int dstsize
, /* I - Destination size */
1585 const char *term
, /* I - Terminating characters */
1586 int decode
) /* I - Decode %-encoded values */
1588 char *ptr
, /* Pointer into buffer */
1589 *end
; /* End of buffer */
1590 int quoted
; /* Quoted character */
1594 * Copy the src to the destination until we hit a terminating character
1595 * or the end of the string.
1598 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
1599 *src
&& (!term
|| !strchr(term
, *src
));
1603 if (*src
== '%' && decode
)
1605 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
1608 * Grab a hex-encoded character...
1613 quoted
= (tolower(*src
) - 'a' + 10) << 4;
1615 quoted
= (*src
- '0') << 4;
1619 quoted
|= tolower(*src
) - 'a' + 10;
1621 quoted
|= *src
- '0';
1628 * Bad hex-encoded character...
1646 * 'http_copy_encode()' - Copy and encode a URI.
1649 static char * /* O - End of current URI */
1650 http_copy_encode(char *dst
, /* O - Destination buffer */
1651 const char *src
, /* I - Source pointer */
1652 char *dstend
, /* I - End of destination buffer */
1653 const char *reserved
, /* I - Extra reserved characters */
1654 const char *term
, /* I - Terminating characters */
1655 int encode
) /* I - %-encode reserved chars? */
1657 static const char hex
[] = "0123456789ABCDEF";
1660 while (*src
&& dst
< dstend
)
1662 if (term
&& *src
== *term
)
1665 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
1666 (reserved
&& strchr(reserved
, *src
))))
1669 * Hex encode reserved characters...
1672 if ((dst
+ 2) >= dstend
)
1676 *dst
++ = hex
[(*src
>> 4) & 15];
1677 *dst
++ = hex
[*src
& 15];
1696 * 'resolve_callback()' - Build a device URI for the given service name.
1699 static void DNSSD_API
1701 DNSServiceRef sdRef
, /* I - Service reference */
1702 DNSServiceFlags flags
, /* I - Results flags */
1703 uint32_t interfaceIndex
, /* I - Interface number */
1704 DNSServiceErrorType errorCode
, /* I - Error, if any */
1705 const char *fullName
, /* I - Full service name */
1706 const char *hostTarget
, /* I - Hostname */
1707 uint16_t port
, /* I - Port number */
1708 uint16_t txtLen
, /* I - Length of TXT record */
1709 const unsigned char *txtRecord
, /* I - TXT record data */
1710 void *context
) /* I - Pointer to URI buffer */
1712 const char *scheme
; /* URI scheme */
1713 char rp
[257]; /* Remote printer */
1714 const void *value
; /* Value from TXT record */
1715 uint8_t valueLen
; /* Length of value */
1716 _http_uribuf_t
*uribuf
; /* URI buffer */
1719 DEBUG_printf(("7resolve_callback(sdRef=%p, flags=%x, interfaceIndex=%u, "
1720 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
1721 "txtLen=%u, txtRecord=%p, context=%p)", sdRef
, flags
,
1722 interfaceIndex
, errorCode
, fullName
, hostTarget
, port
, txtLen
,
1723 txtRecord
, context
));
1726 * Figure out the scheme from the full name...
1729 if (strstr(fullName
, "._ipp") || strstr(fullName
, "._fax-ipp"))
1731 else if (strstr(fullName
, "._printer."))
1733 else if (strstr(fullName
, "._pdl-datastream."))
1736 scheme
= "riousbprint";
1739 * Extract the "remote printer" key from the TXT record...
1742 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "rp",
1743 &valueLen
)) != NULL
)
1746 * Convert to resource by concatenating with a leading "/"...
1750 memcpy(rp
+ 1, value
, valueLen
);
1751 rp
[valueLen
+ 1] = '\0';
1757 * Assemble the final device URI...
1760 uribuf
= (_http_uribuf_t
*)context
;
1762 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, uribuf
->bufsize
, scheme
,
1763 NULL
, hostTarget
, ntohs(port
), rp
);
1765 DEBUG_printf(("8resolve_callback: Resolved URI is \"%s\"...",
1768 #endif /* HAVE_DNSSD */
1772 * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $".