/*
* "$Id$"
*
- * HTTP support routines for CUPS.
+ * HTTP support routines for CUPS.
*
- * Copyright 2007-2013 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2015 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law. Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file. If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
- * This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- * httpAssembleURI() - Assemble a uniform resource identifier from its
- * components.
- * httpAssembleURIf() - Assemble a uniform resource identifier from its
- * components with a formatted resource.
- * httpAssembleUUID() - Assemble a name-based UUID URN conforming to RFC
- * 4122.
- * httpDecode64() - Base64-decode a string.
- * httpDecode64_2() - Base64-decode a string.
- * httpEncode64() - Base64-encode a string.
- * httpEncode64_2() - Base64-encode a string.
- * httpGetDateString() - Get a formatted date/time string from a time value.
- * httpGetDateString2() - Get a formatted date/time string from a time value.
- * httpGetDateTime() - Get a time value from a formatted date/time string.
- * httpSeparate() - Separate a Universal Resource Identifier into its
- * components.
- * httpSeparate2() - Separate a Universal Resource Identifier into its
- * components.
- * httpSeparateURI() - Separate a Universal Resource Identifier into its
- * components.
- * httpStatus() - Return a short string describing a HTTP status
- * code.
- * _cups_hstrerror() - hstrerror() emulation function for Solaris and
- * others.
- * _httpDecodeURI() - Percent-decode a HTTP request URI.
- * _httpEncodeURI() - Percent-encode a HTTP request URI.
- * _httpResolveURI() - Resolve a DNS-SD URI.
- * http_client_cb() - Client callback for resolving URI.
- * http_copy_decode() - Copy and decode a URI.
- * http_copy_encode() - Copy and encode a URI.
- * http_poll_cb() - Wait for input on the specified file descriptors.
- * http_resolve_cb() - Build a device URI for the given service name.
- * http_resolve_cb() - Build a device URI for the given service name.
+ * This file is subject to the Apple OS-Developed Software exception.
*/
/*
size_t bufsize; /* Size of buffer */
int options; /* Options passed to _httpResolveURI */
const char *resource; /* Resource from URI */
+ const char *uuid; /* UUID from URI */
} _http_uribuf_t;
* Local globals...
*/
-static const char * const http_days[7] =
+static const char * const http_days[7] =/* Days of the week */
{
"Sun",
"Mon",
"Sat"
};
static const char * const http_months[12] =
- {
+ { /* Months of the year */
"Jan",
"Feb",
"Mar",
"Nov",
"Dec"
};
+static const char * const http_states[] =
+ { /* HTTP state strings */
+ "HTTP_STATE_ERROR",
+ "HTTP_STATE_WAITING",
+ "HTTP_STATE_OPTIONS",
+ "HTTP_STATE_GET",
+ "HTTP_STATE_GET_SEND",
+ "HTTP_STATE_HEAD",
+ "HTTP_STATE_POST",
+ "HTTP_STATE_POST_RECV",
+ "HTTP_STATE_POST_SEND",
+ "HTTP_STATE_PUT",
+ "HTTP_STATE_PUT_RECV",
+ "HTTP_STATE_DELETE",
+ "HTTP_STATE_TRACE",
+ "HTTP_STATE_CONNECT",
+ "HTTP_STATE_STATUS",
+ "HTTP_STATE_UNKNOWN_METHOD",
+ "HTTP_STATE_UNKNOWN_VERSION"
+ };
/*
if (!ptr)
goto assemble_overflow;
- if (!strcmp(scheme, "mailto"))
+ if (!strcmp(scheme, "geo") || !strcmp(scheme, "mailto") || !strcmp(scheme, "tel"))
{
/*
- * mailto: only has :, no //...
+ * geo:, mailto:, and tel: only have :, no //...
*/
if (ptr < end)
else
{
/*
- * Schemes other than mailto: all have //...
+ * Schemes other than geo:, mailto:, and tel: typically have //...
*/
if ((ptr + 2) < end)
if (port > 0)
{
- snprintf(ptr, end - ptr + 1, ":%d", port);
+ snprintf(ptr, (size_t)(end - ptr + 1), ":%d", port);
ptr += strlen(ptr);
if (ptr >= end)
bytes = vsnprintf(resource, sizeof(resource), resourcef, ap);
va_end(ap);
- if (bytes >= sizeof(resource))
+ if ((size_t)bytes >= sizeof(resource))
{
*uri = '\0';
return (HTTP_URI_STATUS_OVERFLOW);
(unsigned)CUPS_RAND() & 0xffff, (unsigned)CUPS_RAND() & 0xffff);
_cupsMD5Init(&md5state);
- _cupsMD5Append(&md5state, (unsigned char *)data, strlen(data));
+ _cupsMD5Append(&md5state, (unsigned char *)data, (int)strlen(data));
_cupsMD5Finish(&md5state, md5sum);
/*
return (buffer);
}
-/* For OS X 10.8 and earlier */
-char *_httpAssembleUUID(const char *server, int port, const char *name,
- int number, char *buffer, size_t bufsize)
-{
- return (httpAssembleUUID(server, port, name, number, buffer, bufsize));
-}
-
/*
* 'httpDecode64()' - Base64-decode a string.
int *outlen, /* IO - Size of output string */
const char *in) /* I - String to read from */
{
- int pos, /* Bit position */
- base64; /* Value of this character */
- char *outptr, /* Output pointer */
- *outend; /* End of output buffer */
+ int pos; /* Bit position */
+ unsigned base64; /* Value of this character */
+ char *outptr, /* Output pointer */
+ *outend; /* End of output buffer */
/*
*/
if (*in >= 'A' && *in <= 'Z')
- base64 = *in - 'A';
+ base64 = (unsigned)(*in - 'A');
else if (*in >= 'a' && *in <= 'z')
- base64 = *in - 'a' + 26;
+ base64 = (unsigned)(*in - 'a' + 26);
else if (*in >= '0' && *in <= '9')
- base64 = *in - '0' + 52;
+ base64 = (unsigned)(*in - '0' + 52);
else if (*in == '+')
base64 = 62;
else if (*in == '/')
{
case 0 :
if (outptr < outend)
- *outptr = base64 << 2;
+ *outptr = (char)(base64 << 2);
pos ++;
break;
case 1 :
if (outptr < outend)
- *outptr++ |= (base64 >> 4) & 3;
+ *outptr++ |= (char)((base64 >> 4) & 3);
if (outptr < outend)
- *outptr = (base64 << 4) & 255;
+ *outptr = (char)((base64 << 4) & 255);
pos ++;
break;
case 2 :
if (outptr < outend)
- *outptr++ |= (base64 >> 2) & 15;
+ *outptr++ |= (char)((base64 >> 2) & 15);
if (outptr < outend)
- *outptr = (base64 << 6) & 255;
+ *outptr = (char)((base64 << 6) & 255);
pos ++;
break;
case 3 :
if (outptr < outend)
- *outptr++ |= base64;
+ *outptr++ |= (char)base64;
pos = 0;
break;
}
tdate = gmtime(&t);
if (tdate)
- snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT",
- http_days[tdate->tm_wday], tdate->tm_mday,
- http_months[tdate->tm_mon], tdate->tm_year + 1900,
- tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
+ snprintf(s, (size_t)slen, "%s, %02d %s %d %02d:%02d:%02d GMT", http_days[tdate->tm_wday], tdate->tm_mday, http_months[tdate->tm_mon], tdate->tm_year + 1900, tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
else
s[0] = '\0';
* Workaround for HP IPP client bug...
*/
- strlcpy(scheme, "ipp", schemelen);
+ strlcpy(scheme, "ipp", (size_t)schemelen);
status = HTTP_URI_STATUS_MISSING_SCHEME;
}
else if (*uri == '/')
* Filename...
*/
- strlcpy(scheme, "file", schemelen);
+ strlcpy(scheme, "file", (size_t)schemelen);
status = HTTP_URI_STATUS_MISSING_SCHEME;
}
else
*port = 515;
else if (!strcmp(scheme, "socket")) /* Not yet registered with IANA... */
*port = 9100;
- else if (strcmp(scheme, "file") && strcmp(scheme, "mailto"))
+ else if (strcmp(scheme, "file") && strcmp(scheme, "mailto") && strcmp(scheme, "tel"))
status = HTTP_URI_STATUS_UNKNOWN_SCHEME;
/*
return (HTTP_URI_STATUS_BAD_PORT);
}
- *port = strtol(uri + 1, (char **)&uri, 10);
+ *port = (int)strtol(uri + 1, (char **)&uri, 10);
if (*uri != '/' && *uri)
{
/*
- * 'httpStatus()' - Return a short string describing a HTTP status code.
+ * 'httpStateString()' - Return the string describing a HTTP state value.
*
- * The returned string is localized to the current POSIX locale and is based
- * on the status strings defined in RFC 2616.
+ * @since CUPS 2.0/OS 10.10@
+ */
+
+const char * /* O - State string */
+httpStateString(http_state_t state) /* I - HTTP state value */
+{
+ if (state < HTTP_STATE_ERROR || state > HTTP_STATE_UNKNOWN_VERSION)
+ return ("HTTP_STATE_???");
+ else
+ return (http_states[state - HTTP_STATE_ERROR]);
+}
+
+
+/*
+ * '_httpStatus()' - Return the localized string describing a HTTP status code.
+ *
+ * The returned string is localized using the passed message catalog.
*/
const char * /* O - Localized status string */
-httpStatus(http_status_t status) /* I - HTTP status code */
+_httpStatus(cups_lang_t *lang, /* I - Language */
+ http_status_t status) /* I - HTTP status code */
{
const char *s; /* Status string */
- _cups_globals_t *cg = _cupsGlobals(); /* Global data */
-
- if (!cg->lang_default)
- cg->lang_default = cupsLangDefault();
switch (status)
{
break;
}
+ return (_cupsLangString(lang, s));
+}
+
+
+/*
+ * 'httpStatus()' - Return a short string describing a HTTP status code.
+ *
+ * The returned string is localized to the current POSIX locale and is based
+ * on the status strings defined in RFC 2616.
+ */
+
+const char * /* O - Localized status string */
+httpStatus(http_status_t status) /* I - HTTP status code */
+{
+ _cups_globals_t *cg = _cupsGlobals(); /* Global data */
+
+
+ if (!cg->lang_default)
+ cg->lang_default = cupsLangDefault();
+
+ return (_httpStatus(cg->lang_default, status));
+}
+
+/*
+ * 'httpURIStatusString()' - Return a string describing a URI status code.
+ *
+ * @since CUPS 2.0/OS 10.10@
+ */
+
+const char * /* O - Localized status string */
+httpURIStatusString(
+ http_uri_status_t status) /* I - URI status code */
+{
+ const char *s; /* Status string */
+ _cups_globals_t *cg = _cupsGlobals(); /* Global data */
+
+
+ if (!cg->lang_default)
+ cg->lang_default = cupsLangDefault();
+
+ switch (status)
+ {
+ case HTTP_URI_STATUS_OVERFLOW :
+ s = _("URI too large");
+ break;
+ case HTTP_URI_STATUS_BAD_ARGUMENTS :
+ s = _("Bad arguments to function");
+ break;
+ case HTTP_URI_STATUS_BAD_RESOURCE :
+ s = _("Bad resource in URI");
+ break;
+ case HTTP_URI_STATUS_BAD_PORT :
+ s = _("Bad port number in URI");
+ break;
+ case HTTP_URI_STATUS_BAD_HOSTNAME :
+ s = _("Bad hostname/address in URI");
+ break;
+ case HTTP_URI_STATUS_BAD_USERNAME :
+ s = _("Bad username in URI");
+ break;
+ case HTTP_URI_STATUS_BAD_SCHEME :
+ s = _("Bad scheme in URI");
+ break;
+ case HTTP_URI_STATUS_BAD_URI :
+ s = _("Bad/empty URI");
+ break;
+ case HTTP_URI_STATUS_OK :
+ s = _("OK");
+ break;
+ case HTTP_URI_STATUS_MISSING_SCHEME :
+ s = _("Missing scheme in URI");
+ break;
+ case HTTP_URI_STATUS_UNKNOWN_SCHEME :
+ s = _("Unknown scheme in URI");
+ break;
+ case HTTP_URI_STATUS_MISSING_RESOURCE :
+ s = _("Missing resource in URI");
+ break;
+
+ default:
+ s = _("Unknown");
+ break;
+ }
+
return (_cupsLangString(cg->lang_default, s));
}
{
#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
char *regtype, /* Pointer to type in hostname */
- *domain; /* Pointer to domain in hostname */
+ *domain, /* Pointer to domain in hostname */
+ *uuid, /* Pointer to UUID in URI */
+ *uuidend; /* Pointer to end of UUID in URI */
_http_uribuf_t uribuf; /* URI buffer */
int offline = 0; /* offline-report state set? */
# ifdef HAVE_DNSSD
# pragma comment(lib, "dnssd.lib")
# endif /* WIN32 */
DNSServiceRef ref, /* DNS-SD master service reference */
- domainref, /* DNS-SD service reference for domain */
+ domainref = NULL,/* DNS-SD service reference for domain */
+ ippref = NULL, /* DNS-SD service reference for network IPP */
+ ippsref = NULL, /* DNS-SD service reference for network IPPS */
localref; /* DNS-SD service reference for .local */
- int domainsent = 0; /* Send the domain resolve? */
+ int extrasent = 0; /* Send the domain/IPP/IPPS resolves? */
# ifdef HAVE_POLL
struct pollfd polldata; /* Polling data */
# else /* select() */
if (domain)
*domain++ = '\0';
+ if ((uuid = strstr(resource, "?uuid=")) != NULL)
+ {
+ *uuid = '\0';
+ uuid += 6;
+ if ((uuidend = strchr(uuid, '&')) != NULL)
+ *uuidend = '\0';
+ }
+
+ resolved_uri[0] = '\0';
+
uribuf.buffer = resolved_uri;
uribuf.bufsize = resolved_size;
uribuf.options = options;
uribuf.resource = resource;
-
- resolved_uri[0] = '\0';
+ uribuf.uuid = uuid;
DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
"domain=\"%s\"\n", hostname, regtype, domain));
# ifdef HAVE_DNSSD
if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
{
- int myinterface = kDNSServiceInterfaceIndexAny;
+ uint32_t myinterface = kDNSServiceInterfaceIndexAny;
/* Lookup on any interface */
if (!strcmp(scheme, "ippusb"))
while (time(NULL) < end_time)
{
if (options & _HTTP_RESOLVE_STDERR)
- _cupsLangPrintFilter(stderr, "INFO", _("Looking for printer."));
+ _cupsLangPrintFilter(stderr, "INFO", _("Looking for printer..."));
if (cb && !(*cb)(context))
{
polldata.fd = DNSServiceRefSockFD(ref);
polldata.events = POLLIN;
- fds = poll(&polldata, 1, 1000 * timeout);
+ fds = poll(&polldata, 1, (int)(1000 * timeout));
# else /* select() */
FD_ZERO(&input_set);
* comes in, do an additional domain resolution...
*/
- if (domainsent == 0 && domain && _cups_strcasecmp(domain, "local."))
+ if (extrasent == 0 && domain && _cups_strcasecmp(domain, "local."))
{
if (options & _HTTP_RESOLVE_STDERR)
fprintf(stderr,
myinterface, hostname, regtype, domain,
http_resolve_cb,
&uribuf) == kDNSServiceErr_NoError)
- domainsent = 1;
+ extrasent = 1;
+ }
+ else if (extrasent == 0 && !strcmp(scheme, "ippusb"))
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"_ipps._tcp\", domain=\"local.\"...\n", hostname);
+
+ ippsref = ref;
+ if (DNSServiceResolve(&ippsref,
+ kDNSServiceFlagsShareConnection,
+ kDNSServiceInterfaceIndexAny, hostname,
+ "_ipps._tcp", domain, http_resolve_cb,
+ &uribuf) == kDNSServiceErr_NoError)
+ extrasent = 1;
+ }
+ else if (extrasent == 1 && !strcmp(scheme, "ippusb"))
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"_ipp._tcp\", domain=\"local.\"...\n", hostname);
+
+ ippref = ref;
+ if (DNSServiceResolve(&ippref,
+ kDNSServiceFlagsShareConnection,
+ kDNSServiceInterfaceIndexAny, hostname,
+ "_ipp._tcp", domain, http_resolve_cb,
+ &uribuf) == kDNSServiceErr_NoError)
+ extrasent = 2;
}
/*
}
else
{
- if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError)
+ if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError &&
+ resolved_uri[0])
{
uri = resolved_uri;
break;
}
}
- if (domainsent)
- DNSServiceRefDeallocate(domainref);
+ if (extrasent)
+ {
+ if (domainref)
+ DNSServiceRefDeallocate(domainref);
+ if (ippref)
+ DNSServiceRefDeallocate(ippref);
+ if (ippsref)
+ DNSServiceRefDeallocate(ippsref);
+ }
DNSServiceRefDeallocate(localref);
}
else
quoted |= *src - '0';
- *ptr++ = quoted;
+ *ptr++ = (char)quoted;
}
else
{
interfaceIndex, errorCode, fullName, hostTarget, port, txtLen,
txtRecord, context));
+ /*
+ * If we have a UUID, compare it...
+ */
+
+ if (uribuf->uuid &&
+ (value = TXTRecordGetValuePtr(txtLen, txtRecord, "UUID",
+ &valueLen)) != NULL)
+ {
+ char uuid[256]; /* UUID value */
+
+ memcpy(uuid, value, valueLen);
+ uuid[valueLen] = '\0';
+
+ if (_cups_strcasecmp(uuid, uribuf->uuid))
+ {
+ if (uribuf->options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Found UUID %s, looking for %s.", uuid,
+ uribuf->uuid);
+
+ DEBUG_printf(("7http_resolve_cb: Found UUID %s, looking for %s.", uuid,
+ uribuf->uuid));
+ return;
+ }
+ }
+
/*
* Figure out the scheme from the full name...
*/
*/
if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) &&
- (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")))
+ (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
+ !TXTRecordGetValuePtr(txtLen, txtRecord, "printer-type", &valueLen))
{
reskey = "rfo";
resdefault = "/ipp/faxout";
{
for (addr = addrlist; addr; addr = addr->next)
{
- int error = getnameinfo(&(addr->addr.addr),
- httpAddrLength(&(addr->addr)),
- fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
+ int error = getnameinfo(&(addr->addr.addr), (socklen_t)httpAddrLength(&(addr->addr)), fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
if (!error)
{
if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
!strcmp(uribuf->resource, "/cups"))
- httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize,
- scheme, NULL, hostTarget, ntohs(port), "%s?snmp=false",
- resource);
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, (int)uribuf->bufsize, scheme, NULL, hostTarget, ntohs(port), "%s?snmp=false", resource);
else
- httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize,
- scheme, NULL, hostTarget, ntohs(port), resource);
+ httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, (int)uribuf->bufsize, scheme, NULL, hostTarget, ntohs(port), resource);
DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\"...", uribuf->buffer));
}
return;
}
+ /*
+ * If we have a UUID, compare it...
+ */
+
+ if (uribuf->uuid && (pair = avahi_string_list_find(txt, "UUID")) != NULL)
+ {
+ char uuid[256]; /* UUID value */
+
+ avahi_string_list_get_pair(pair, NULL, &value, &valueLen);
+
+ memcpy(uuid, value, valueLen);
+ uuid[valueLen] = '\0';
+
+ if (_cups_strcasecmp(uuid, uribuf->uuid))
+ {
+ if (uribuf->options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Found UUID %s, looking for %s.", uuid,
+ uribuf->uuid);
+
+ DEBUG_printf(("7http_resolve_cb: Found UUID %s, looking for %s.", uuid,
+ uribuf->uuid));
+ return;
+ }
+ }
+
/*
* Figure out the scheme from the full name...
*/
*/
if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) &&
- (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")))
+ (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
+ !avahi_string_list_find(txt, "printer-type"))
{
reskey = "rfo";
resdefault = "/ipp/faxout";
{
for (addr = addrlist; addr; addr = addr->next)
{
- int error = getnameinfo(&(addr->addr.addr),
- httpAddrLength(&(addr->addr)),
- fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
+ int error = getnameinfo(&(addr->addr.addr), (socklen_t)httpAddrLength(&(addr->addr)), fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
if (!error)
{