[libsoup] soup-tld: improve handling of the psl context



commit abe6ad3957cfafcee64ea3d02ba84cd3d4856c95
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Fri Apr 6 16:14:20 2018 +0300

    soup-tld: improve handling of the psl context
    
    Use the most recently available psl data from libpsl. If none is
    available, log a warning. This is extremely rare and shouldn't
    happen but let's handle it gracefully in any case.

 libsoup/soup-tld.c |   29 +++++++++++++++++++++++------
 libsoup/soup-tld.h |    3 ++-
 2 files changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/libsoup/soup-tld.c b/libsoup/soup-tld.c
index 61bfbe0..3455e22 100644
--- a/libsoup/soup-tld.c
+++ b/libsoup/soup-tld.c
@@ -62,6 +62,17 @@ soup_tld_get_base_domain (const char *hostname, GError **error)
        return soup_tld_get_base_domain_internal (hostname, error);
 }
 
+static psl_ctx_t *
+soup_psl_context (void)
+{
+       static psl_ctx_t *psl = NULL;
+
+       if (!psl)
+               psl = psl_latest (NULL);
+
+       return psl;
+}
+
 /**
  * soup_tld_domain_is_public_suffix:
  * @domain: a domain name
@@ -80,12 +91,14 @@ soup_tld_get_base_domain (const char *hostname, GError **error)
 gboolean
 soup_tld_domain_is_public_suffix (const char *domain)
 {
-       const psl_ctx_t* psl = psl_builtin ();
+       const psl_ctx_t* psl = soup_psl_context ();
 
        g_return_val_if_fail (domain, FALSE);
 
-       /* This will fail if libpsl's built-in data was disabled during compilation. */
-       g_assert (psl);
+       if (!psl) {
+               g_warning ("soup-tld: There is no public-suffix data available.");
+               return FALSE;
+       }
 
        return psl_is_public_suffix2 (psl, domain, PSL_TYPE_ANY | PSL_TYPE_NO_STAR_RULE);
 }
@@ -128,11 +141,15 @@ static const char *
 soup_tld_get_base_domain_internal (const char *hostname, GError **error)
 {
        char *utf8_hostname = NULL;
-       const psl_ctx_t* psl = psl_builtin ();
+       const psl_ctx_t* psl = soup_psl_context ();
        const char *registrable_domain, *unregistrable_domain;
 
-       /* This will fail if libpsl's built-in data was disabled during compilation. */
-       g_assert (psl);
+       if (!psl) {
+               g_set_error_literal (error, SOUP_TLD_ERROR,
+                                    SOUP_TLD_ERROR_NO_PSL_DATA,
+                                    _("No public-suffix list available."));
+               return NULL;
+       }
 
        /* Valid hostnames neither start with a dot nor have more than one
         * dot together.
diff --git a/libsoup/soup-tld.h b/libsoup/soup-tld.h
index 4b099a2..df88b55 100644
--- a/libsoup/soup-tld.h
+++ b/libsoup/soup-tld.h
@@ -26,7 +26,8 @@ typedef enum {
        SOUP_TLD_ERROR_INVALID_HOSTNAME,
        SOUP_TLD_ERROR_IS_IP_ADDRESS,
        SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS,
-       SOUP_TLD_ERROR_NO_BASE_DOMAIN
+       SOUP_TLD_ERROR_NO_BASE_DOMAIN,
+       SOUP_TLD_ERROR_NO_PSL_DATA,
 } SoupTLDError;
 
 G_END_DECLS


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