[libsoup] soup-tld: do not consider non suffixed domains as public



commit cbae89f4176a96ed16641acbd1bb43717bf35284
Author: Dan Winship <danw gnome org>
Date:   Mon Jul 16 18:03:24 2012 -0400

    soup-tld: do not consider non suffixed domains as public
    
    Non suffixed hostnames (used in many intranet applications) should not be
    considered as public.
    
    Based on a patch from Sergio Villar Senin.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=679230

 libsoup/soup-tld.c |   15 ++++++++-------
 libsoup/soup-tld.h |    3 ++-
 tests/tld-test.c   |   25 ++++++++++++++-----------
 3 files changed, 24 insertions(+), 19 deletions(-)
---
diff --git a/libsoup/soup-tld.c b/libsoup/soup-tld.c
index 4dce00b..73654b0 100644
--- a/libsoup/soup-tld.c
+++ b/libsoup/soup-tld.c
@@ -105,10 +105,7 @@ soup_tld_domain_is_public_suffix (const char *domain)
 	if (*domain == '.' && !(++domain))
 		g_return_val_if_reached (FALSE);
 
-	/* By asking for one additional domain we instruct the search
-	 * not to exactly match any registered public domain.
-	 */
-	base_domain = soup_tld_get_base_domain_internal (domain, 1, &error);
+	base_domain = soup_tld_get_base_domain_internal (domain, 0, &error);
 	if (base_domain)
 		return FALSE;
 
@@ -193,10 +190,14 @@ soup_tld_get_base_domain_internal (const char *hostname, guint additional_domain
 			}
 		}
 
-		/* We hit the top domain, use it if it's listed as valid. */
+		/* If we hit the top and haven't matched yet, then it
+		 * has no public suffix.
+		 */
 		if (!next_dot) {
-			tld = cur_domain;
-			break;
+			g_set_error_literal (error, SOUP_TLD_ERROR,
+					     SOUP_TLD_ERROR_NO_BASE_DOMAIN,
+					     _("Hostname has no base domain"));
+			return NULL;
 		}
 
 		prev_domain = cur_domain;
diff --git a/libsoup/soup-tld.h b/libsoup/soup-tld.h
index 2d1eebd..38de46a 100644
--- a/libsoup/soup-tld.h
+++ b/libsoup/soup-tld.h
@@ -22,7 +22,8 @@ GQuark soup_tld_error_quark (void);
 typedef enum {
 	SOUP_TLD_ERROR_INVALID_HOSTNAME,
 	SOUP_TLD_ERROR_IS_IP_ADDRESS,
-	SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS
+	SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS,
+	SOUP_TLD_ERROR_NO_BASE_DOMAIN
 } SoupTLDError;
 
 G_END_DECLS
diff --git a/tests/tld-test.c b/tests/tld-test.c
index 5952444..5f66c68 100644
--- a/tests/tld-test.c
+++ b/tests/tld-test.c
@@ -21,16 +21,16 @@ static struct {
   { ".example", NULL },
   { ".example.com", NULL },
   { ".example.example", NULL },
-  /* Unlisted TLD. Not checked because we do not want to force every URL to have a public suffix.*/
-  /* { "example", NULL }, */
-  /* { "example.example", NULL }, */
-  /* { "b.example.example", NULL }, */
-  /* { "a.b.example.example", NULL }, */
+  /* Unlisted TLD.*/
+  { "example", NULL },
+  { "example.example", NULL },
+  { "b.example.example", NULL },
+  { "a.b.example.example", NULL },
   /* Listed, but non-Internet, TLD. */
-  /*{ "local", NULL }, */
-  /*{ "example.local", NULL }, */
-  /*{ "b.example.local", NULL }, */
-  /*{ "a.b.example.local", NULL }, */
+  { "local", NULL },
+  { "example.local", NULL },
+  { "b.example.local", NULL },
+  { "a.b.example.local", NULL },
   /* TLD with only 1 rule. */
   { "biz", NULL },
   { "domain.biz", "domain.biz" },
@@ -102,13 +102,15 @@ main (int argc, char **argv)
                gboolean is_public = soup_tld_domain_is_public_suffix (tld_tests[i].hostname);
                const char *base_domain = soup_tld_get_base_domain (tld_tests[i].hostname, NULL);
 
+	       debug_printf (1, "Testing %s: ", tld_tests[i].hostname);
                if (tld_tests[i].result) {
                        /* Public domains have NULL expected results. */
                        if (is_public || g_strcmp0 (tld_tests[i].result, base_domain)) {
                                debug_printf (1, "ERROR: %s got %s (%s expected)\n",
                                              tld_tests[i].hostname, base_domain, tld_tests[i].result);
                                ++errors;
-                       }
+                       } else
+			       debug_printf (1, "OK\n");
                } else {
                        /* If there is no expected result then either the domain is public or
                         * the hostname invalid (for example starts with a leading dot).
@@ -117,7 +119,8 @@ main (int argc, char **argv)
                                debug_printf (1, "ERROR: public domain %s got %s (none expected)\n",
                                              tld_tests[i].hostname, base_domain);
                                ++errors;
-                       }
+                       } else
+			       debug_printf (1, "OK\n");
 	       }
 	}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]