]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/testhttp.c
Update Windows DLL exports file.
[thirdparty/cups.git] / cups / testhttp.c
index 3599cea47d1104d94a7138e7c3b394fabb1d3d71..dfb767c89e7be994d3a462b45bd55df7c57286cf 100644 (file)
@@ -1,30 +1,18 @@
 /*
- * "$Id$"
+ * HTTP test program for CUPS.
  *
- *   HTTP test program for CUPS.
+ * Copyright © 2007-2018 by Apple Inc.
+ * Copyright © 1997-2006 by Easy Software Products.
  *
- *   Copyright 2007-2013 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products.
- *
- *   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:
- *
- *   main() - Main entry.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include "string-private.h"
-#include "http-private.h"
+#include "cups-private.h"
 
 
 /*
@@ -40,8 +28,8 @@ typedef struct uri_test_s             /**** URI test cases ****/
                        *hostname,      /* Hostname string */
                        *resource;      /* Resource string */
   int                  port,           /* Port number */
-                       assemble_port,  /* Port number for httpAssembleURI() */
-                       assemble_coding;/* Coding for httpAssembleURI() */
+                       assemble_port;  /* Port number for httpAssembleURI() */
+  http_uri_coding_t    assemble_coding;/* Coding for httpAssembleURI() */
 } uri_test_t;
 
 
@@ -106,12 +94,15 @@ static uri_test_t  uri_tests[] =   /* URI test data */
                          { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/",
                            "socket", "", "192.168.1.1", "/", 9101, 9101,
                            HTTP_URI_CODING_MOST  },
+                         { HTTP_URI_STATUS_OK, "tel:8005551212",
+                           "tel", "", "", "8005551212", 0, 0,
+                           HTTP_URI_CODING_MOST  },
                          { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
                            "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
                            HTTP_URI_CODING_MOST  },
                          { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp",
                            "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
-                           HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874 },
+                           (http_uri_coding_t)(HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874) },
                          { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
                            "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
                            HTTP_URI_CODING_MOST  },
@@ -121,6 +112,12 @@ static uri_test_t  uri_tests[] =   /* URI test data */
                          { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
                            "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
                            HTTP_URI_CODING_MOST  },
+                         { HTTP_URI_STATUS_OK, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/",
+                           "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0,
+                           HTTP_URI_CODING_MOST  },
+                         { HTTP_URI_STATUS_UNKNOWN_SCHEME, "smb://server/Some%20Printer",
+                           "smb", "", "server", "/Some Printer", 0, 0,
+                           HTTP_URI_CODING_ALL },
 
                          /* Missing scheme */
                          { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html",
@@ -149,6 +146,9 @@ static uri_test_t   uri_tests[] =   /* URI test data */
                            HTTP_URI_CODING_MOST  },
 
                          /* Bad scheme */
+                         { HTTP_URI_STATUS_BAD_SCHEME, "://server/ipp",
+                           "", "", "", "", 0, 0,
+                           HTTP_URI_CODING_MOST  },
                          { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource",
                            "", "", "", "", 0, 0,
                            HTTP_URI_CODING_MOST  },
@@ -171,6 +171,9 @@ static uri_test_t   uri_tests[] =   /* URI test data */
                          { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html",
                            "http", "", "", "", 80, 0,
                            HTTP_URI_CODING_MOST  },
+                         { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/",
+                           "ipp", "", "", "", 631, 0,
+                           HTTP_URI_CODING_MOST  },
 
                          /* Bad port number */
                          { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html",
@@ -178,6 +181,9 @@ static uri_test_t   uri_tests[] =   /* URI test data */
                            HTTP_URI_CODING_MOST  },
 
                          /* Bad resource */
+                         { HTTP_URI_STATUS_BAD_RESOURCE, "mailto:\r\nbla",
+                           "mailto", "", "", "", 0, 0,
+                           HTTP_URI_CODING_MOST  },
                          { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%",
                            "http", "", "server", "", 80, 0,
                            HTTP_URI_CODING_MOST  },
@@ -330,6 +336,40 @@ main(int  argc,                            /* I - Number of command-line arguments */
     if (!j)
       puts("PASS");
 
+#if 0
+   /*
+    * _httpDigest()
+    */
+
+    fputs("_httpDigest(MD5): ", stdout);
+    if (!_httpDigest(buffer, sizeof(buffer), "MD5", "Mufasa", "http-auth@example.org", "Circle of Life", "7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v", 1, "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ", "auth", "GET", "/dir/index.html"))
+    {
+      failures ++;
+      puts("FAIL (unable to calculate hash)");
+    }
+    else if (strcmp(buffer, "8ca523f5e9506fed4657c9700eebdbec"))
+    {
+      failures ++;
+      printf("FAIL (got \"%s\", expected \"8ca523f5e9506fed4657c9700eebdbec\")\n", buffer);
+    }
+    else
+      puts("PASS");
+
+    fputs("_httpDigest(SHA-256): ", stdout);
+    if (!_httpDigest(buffer, sizeof(buffer), "SHA-256", "Mufasa", "http-auth@example.org", "Circle of Life", "7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v", 1, "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ", "auth", "GET", "/dir/index.html"))
+    {
+      failures ++;
+      puts("FAIL (unable to calculate hash)");
+    }
+    else if (strcmp(buffer, "753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1"))
+    {
+      failures ++;
+      printf("FAIL (got \"%s\", expected \"753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1\")\n", buffer);
+    }
+    else
+      puts("PASS");
+#endif /* 0 */
+
    /*
     * httpGetHostname()
     */
@@ -588,6 +628,8 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
   for (i = 1; i < argc; i ++)
   {
+    int new_auth;
+
     if (!strcmp(argv[i], "-o"))
     {
       i ++;
@@ -609,18 +651,158 @@ main(int  argc,                          /* I - Number of command-line arguments */
     else
       encryption = HTTP_ENCRYPTION_IF_REQUESTED;
 
-    http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000,
-                        NULL);
+    http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL);
     if (http == NULL)
     {
       perror(hostname);
       continue;
     }
+
+    if (httpIsEncrypted(http))
+    {
+      cups_array_t *creds;
+      char info[1024];
+      static const char *trusts[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
+      if (!httpCopyCredentials(http, &creds))
+      {
+       cups_array_t *lcreds;
+        http_trust_t trust = httpCredentialsGetTrust(creds, hostname);
+
+        httpCredentialsString(creds, info, sizeof(info));
+
+       printf("Count: %d\n", cupsArrayCount(creds));
+        printf("Trust: %s\n", trusts[trust]);
+        printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds)));
+        printf("IsValidName: %d\n", httpCredentialsAreValidForName(creds, hostname));
+        printf("String: \"%s\"\n", info);
+
+       printf("LoadCredentials: %d\n", httpLoadCredentials(NULL, &lcreds, hostname));
+       httpCredentialsString(lcreds, info, sizeof(info));
+       printf("    Count: %d\n", cupsArrayCount(lcreds));
+       printf("    String: \"%s\"\n", info);
+
+        if (lcreds && cupsArrayCount(creds) == cupsArrayCount(lcreds))
+        {
+          http_credential_t    *cred, *lcred;
+
+          for (i = 1, cred = (http_credential_t *)cupsArrayFirst(creds), lcred = (http_credential_t *)cupsArrayFirst(lcreds);
+               cred && lcred;
+               i ++, cred = (http_credential_t *)cupsArrayNext(creds), lcred = (http_credential_t *)cupsArrayNext(lcreds))
+          {
+            if (cred->datalen != lcred->datalen)
+              printf("    Credential #%d: Different lengths (saved=%d, current=%d)\n", i, (int)cred->datalen, (int)lcred->datalen);
+            else if (memcmp(cred->data, lcred->data, cred->datalen))
+              printf("    Credential #%d: Different data\n", i);
+            else
+              printf("    Credential #%d: Matches\n", i);
+          }
+        }
+
+        if (trust != HTTP_TRUST_OK)
+       {
+         printf("SaveCredentials: %d\n", httpSaveCredentials(NULL, creds, hostname));
+         trust = httpCredentialsGetTrust(creds, hostname);
+         printf("New Trust: %s\n", trusts[trust]);
+       }
+
+        httpFreeCredentials(creds);
+      }
+      else
+        puts("No credentials!");
+    }
+
     printf("Checking file \"%s\"...\n", resource);
-    httpClearFields(http);
-    httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
-    httpHead(http, resource);
-    while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
+
+    new_auth = 0;
+
+    do
+    {
+      if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+      {
+       httpClearFields(http);
+       if (httpReconnect2(http, 30000, NULL))
+       {
+          status = HTTP_STATUS_ERROR;
+          break;
+       }
+      }
+
+      if (http->authstring && !strncmp(http->authstring, "Digest ", 7) && !new_auth)
+        _httpSetDigestAuthString(http, http->nextnonce, "HEAD", resource);
+
+      httpClearFields(http);
+      httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
+      httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
+
+      if (httpHead(http, resource))
+      {
+        if (httpReconnect2(http, 30000, NULL))
+        {
+          status = HTTP_STATUS_ERROR;
+          break;
+        }
+        else
+        {
+          status = HTTP_STATUS_UNAUTHORIZED;
+          continue;
+        }
+      }
+
+      while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
+
+      new_auth = 0;
+
+      if (status == HTTP_STATUS_UNAUTHORIZED)
+      {
+       /*
+       * Flush any error message...
+       */
+
+       httpFlush(http);
+
+       /*
+       * See if we can do authentication...
+       */
+
+        new_auth = 1;
+
+       if (cupsDoAuthentication(http, "HEAD", resource))
+       {
+         status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
+         break;
+       }
+
+       if (httpReconnect2(http, 30000, NULL))
+       {
+         status = HTTP_STATUS_ERROR;
+         break;
+       }
+
+       continue;
+      }
+#ifdef HAVE_SSL
+      else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
+      {
+       /* Flush any error message... */
+       httpFlush(http);
+
+       /* Reconnect... */
+       if (httpReconnect2(http, 30000, NULL))
+       {
+         status = HTTP_STATUS_ERROR;
+         break;
+       }
+
+       /* Upgrade with encryption... */
+       httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
+
+       /* Try again, this time with encryption enabled... */
+       continue;
+      }
+#endif /* HAVE_SSL */
+    }
+    while (status == HTTP_STATUS_UNAUTHORIZED ||
+           status == HTTP_STATUS_UPGRADE_REQUIRED);
 
     if (status == HTTP_STATUS_OK)
       puts("HEAD OK:");
@@ -631,11 +813,97 @@ main(int  argc,                           /* I - Number of command-line arguments */
 
     printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource,
            encoding ? encoding : "identity");
-    httpClearFields(http);
-    httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
-    httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
-    httpGet(http, resource);
-    while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
+
+    new_auth = 0;
+
+    do
+    {
+      if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+      {
+       httpClearFields(http);
+       if (httpReconnect2(http, 30000, NULL))
+       {
+          status = HTTP_STATUS_ERROR;
+          break;
+       }
+      }
+
+      if (http->authstring && !strncmp(http->authstring, "Digest ", 7) && !new_auth)
+        _httpSetDigestAuthString(http, http->nextnonce, "GET", resource);
+
+      httpClearFields(http);
+      httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
+      httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
+      httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
+
+      if (httpGet(http, resource))
+      {
+        if (httpReconnect2(http, 30000, NULL))
+        {
+          status = HTTP_STATUS_ERROR;
+          break;
+        }
+        else
+        {
+          status = HTTP_STATUS_UNAUTHORIZED;
+          continue;
+        }
+      }
+
+      while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
+
+      new_auth = 0;
+
+      if (status == HTTP_STATUS_UNAUTHORIZED)
+      {
+       /*
+       * Flush any error message...
+       */
+
+       httpFlush(http);
+
+       /*
+       * See if we can do authentication...
+       */
+
+        new_auth = 1;
+
+       if (cupsDoAuthentication(http, "GET", resource))
+       {
+         status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
+         break;
+       }
+
+       if (httpReconnect2(http, 30000, NULL))
+       {
+         status = HTTP_STATUS_ERROR;
+         break;
+       }
+
+       continue;
+      }
+#ifdef HAVE_SSL
+      else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
+      {
+       /* Flush any error message... */
+       httpFlush(http);
+
+       /* Reconnect... */
+       if (httpReconnect2(http, 30000, NULL))
+       {
+         status = HTTP_STATUS_ERROR;
+         break;
+       }
+
+       /* Upgrade with encryption... */
+       httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
+
+       /* Try again, this time with encryption enabled... */
+       continue;
+      }
+#endif /* HAVE_SSL */
+    }
+    while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED);
 
     if (status == HTTP_STATUS_OK)
       puts("GET OK:");
@@ -649,11 +917,13 @@ main(int  argc,                           /* I - Number of command-line arguments */
     while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
     {
       total += bytes;
-      fwrite(buffer, bytes, 1, out);
+      fwrite(buffer, (size_t)bytes, 1, out);
       if (out != stdout)
       {
         current = time(NULL);
-        if (current == start) current ++;
+        if (current == start)
+          current ++;
+
         printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
               CUPS_LLFMT " bytes/sec)      ", CUPS_LLCAST total,
               CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
@@ -662,6 +932,9 @@ main(int  argc,                             /* I - Number of command-line arguments */
     }
   }
 
+  if (out != stdout)
+    putchar('\n');
+
   puts("Closing connection to server...");
   httpClose(http);
 
@@ -670,8 +943,3 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
   return (0);
 }
-
-
-/*
- * End of "$Id$".
- */