]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http-support.c
Add ellipsis to "Looking for printer" message.
[thirdparty/cups.git] / cups / http-support.c
index 8f5c67d8082c5bf90e383a590295d0c5aaf13905..aae75e12d761ab44cb11c4ab88fd0c70b8f9c325 100644 (file)
@@ -1,55 +1,18 @@
 /*
  * "$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 the localized string describing a HTTP
- *                          status code.
- *   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.
  */
 
 /*
@@ -94,7 +57,7 @@ typedef struct _http_uribuf_s         /* URI buffer */
  * Local globals...
  */
 
-static const char * const http_days[7] =
+static const char * const http_days[7] =/* Days of the week */
                        {
                          "Sun",
                          "Mon",
@@ -105,7 +68,7 @@ static const char * const http_days[7] =
                          "Sat"
                        };
 static const char * const http_months[12] =
-                       {
+                       {               /* Months of the year */
                          "Jan",
                          "Feb",
                          "Mar",
@@ -119,6 +82,26 @@ static const char * const http_months[12] =
                          "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"
+                       };
 
 
 /*
@@ -209,10 +192,10 @@ httpAssembleURI(
   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)
@@ -223,7 +206,7 @@ httpAssembleURI(
   else
   {
    /*
-    * Schemes other than mailto: all have //...
+    * Schemes other than geo:, mailto:, and tel: typically have //...
     */
 
     if ((ptr + 2) < end)
@@ -375,7 +358,7 @@ httpAssembleURI(
 
     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)
@@ -489,7 +472,7 @@ httpAssembleURIf(
   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);
@@ -537,7 +520,7 @@ httpAssembleUUID(const char *server,        /* I - Server name */
           (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);
 
  /*
@@ -555,13 +538,6 @@ httpAssembleUUID(const char *server,       /* I - Server name */
   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.
@@ -600,10 +576,10 @@ httpDecode64_2(char       *out,           /* I  - String to write to */
               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 */
 
 
  /*
@@ -632,11 +608,11 @@ httpDecode64_2(char       *out,           /* I  - String to write to */
     */
 
     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 == '/')
@@ -654,26 +630,26 @@ httpDecode64_2(char       *out,           /* I  - String to write to */
     {
       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;
     }
@@ -833,10 +809,7 @@ httpGetDateString2(time_t t,               /* I - UNIX time */
 
   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';
 
@@ -1031,7 +1004,7 @@ httpSeparateURI(
     * 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 == '/')
@@ -1040,7 +1013,7 @@ httpSeparateURI(
     * Filename...
     */
 
-    strlcpy(scheme, "file", schemelen);
+    strlcpy(scheme, "file", (size_t)schemelen);
     status = HTTP_URI_STATUS_MISSING_SCHEME;
   }
   else
@@ -1083,7 +1056,7 @@ httpSeparateURI(
     *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;
 
  /*
@@ -1258,7 +1231,7 @@ httpSeparateURI(
         return (HTTP_URI_STATUS_BAD_PORT);
       }
 
-      *port = strtol(uri + 1, (char **)&uri, 10);
+      *port = (int)strtol(uri + 1, (char **)&uri, 10);
 
       if (*uri != '/' && *uri)
       {
@@ -1324,6 +1297,22 @@ httpSeparateURI(
 }
 
 
+/*
+ * 'httpStateString()' - Return the string describing a HTTP state value.
+ *
+ * @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.
  *
@@ -1441,6 +1430,70 @@ httpStatus(http_status_t status) /* I - HTTP status code */
   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));
+}
+
 
 #ifndef HAVE_HSTRERROR
 /*
@@ -1567,9 +1620,11 @@ _httpResolveURI(
 #      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() */
@@ -1646,7 +1701,7 @@ _httpResolveURI(
 #  ifdef HAVE_DNSSD
     if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
     {
-      int myinterface = kDNSServiceInterfaceIndexAny;
+      uint32_t myinterface = kDNSServiceInterfaceIndexAny;
                                        /* Lookup on any interface */
 
       if (!strcmp(scheme, "ippusb"))
@@ -1667,7 +1722,7 @@ _httpResolveURI(
        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))
          {
@@ -1686,7 +1741,7 @@ _httpResolveURI(
          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);
@@ -1718,7 +1773,7 @@ _httpResolveURI(
            * 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,
@@ -1732,7 +1787,33 @@ _httpResolveURI(
                                    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;
            }
 
           /*
@@ -1758,8 +1839,15 @@ _httpResolveURI(
          }
        }
 
-       if (domainsent)
-         DNSServiceRefDeallocate(domainref);
+       if (extrasent)
+       {
+         if (domainref)
+           DNSServiceRefDeallocate(domainref);
+         if (ippref)
+           DNSServiceRefDeallocate(ippref);
+         if (ippsref)
+           DNSServiceRefDeallocate(ippsref);
+       }
 
        DNSServiceRefDeallocate(localref);
       }
@@ -1951,7 +2039,7 @@ http_copy_decode(char       *dst, /* O - Destination buffer */
          else
            quoted |= *src - '0';
 
-          *ptr++ = quoted;
+          *ptr++ = (char)quoted;
        }
        else
        {
@@ -2179,9 +2267,7 @@ http_resolve_cb(
     {
       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)
        {
@@ -2212,12 +2298,9 @@ http_resolve_cb(
 
   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));
 }
@@ -2422,9 +2505,7 @@ http_resolve_cb(
     {
       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)
        {