summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomas Popela <tpopela@redhat.com>2016-02-10 17:02:37 +0100
committerTomas Popela <tpopela@redhat.com>2016-02-16 11:55:51 +0100
commite34d0bc0b0139a82986a226b8181ed3506ac98ae (patch)
tree9a036cf6b7fda3dfb0d1cf8f6aa66f0f3ba95393
parent6b8c865a68e87dce6f8c07cd4a05a547de35cf04 (diff)
downloadlibsoup-e34d0bc0b0139a82986a226b8181ed3506ac98ae.tar.gz
soup-misc: Introduce the soup_host_matches_host function
Refactor the soup_cookie_domain_matches function's code to the soup_host_matches_host function to avoid code duplication between soup-cookie and soup-auth-negotiate.
-rw-r--r--libsoup/soup-auth-negotiate.c38
-rw-r--r--libsoup/soup-cookie.c20
-rw-r--r--libsoup/soup-misc.c35
-rw-r--r--libsoup/soup-misc.h2
4 files changed, 44 insertions, 51 deletions
diff --git a/libsoup/soup-auth-negotiate.c b/libsoup/soup-auth-negotiate.c
index 3a800c9e..d0fc3afe 100644
--- a/libsoup/soup-auth-negotiate.c
+++ b/libsoup/soup-auth-negotiate.c
@@ -305,44 +305,18 @@ remove_server_response_handler (SoupMessage *msg, gpointer state)
state);
}
-/* check if scheme://host:port from msg matches the trusted uri */
+/* Check if scheme://host:port from message matches the given URI. */
static gint
-match_base_uri (SoupURI *trusted_uri, SoupURI *msg_uri)
+match_base_uri (SoupURI *list_uri, SoupURI *msg_uri)
{
- if (msg_uri->scheme != trusted_uri->scheme)
+ if (msg_uri->scheme != list_uri->scheme)
return 1;
- if (trusted_uri->port && (msg_uri->port != trusted_uri->port))
+ if (list_uri->port && (msg_uri->port != list_uri->port))
return 1;
- if (trusted_uri->host) {
- const gchar *msg_host = NULL;
- const gchar *trusted_host = NULL;
-
- msg_host = soup_uri_get_host (msg_uri);
- trusted_host = soup_uri_get_host (trusted_uri);
-
- if (g_str_has_suffix (msg_host, trusted_host)) {
- /* if the msg host ends with host from the trusted uri, then make
- * sure it is either an exact match, or prefixed with a dot. We
- * don't want "foobar.com" to match "bar.com"
- */
- if (g_ascii_strcasecmp (msg_host, trusted_host) == 0) {
- return 0;
- } else {
- gint trusted_host_len, msg_host_len;
-
- /* we don't want example.com to match fooexample.com */
- trusted_host_len = strlen (trusted_host);
- msg_host_len = strlen (msg_host);
- if (msg_host[msg_host_len - trusted_host_len - 1] == '.') {
- return 0;
- }
- }
- }
-
- return 1;
- }
+ if (list_uri->host)
+ return !soup_host_matches_host (msg_uri->host, list_uri->host);
return 0;
}
diff --git a/libsoup/soup-cookie.c b/libsoup/soup-cookie.c
index 5af36e91..63f98aa4 100644
--- a/libsoup/soup-cookie.c
+++ b/libsoup/soup-cookie.c
@@ -107,28 +107,10 @@ soup_cookie_copy (SoupCookie *cookie)
gboolean
soup_cookie_domain_matches (SoupCookie *cookie, const char *host)
{
- char *match;
- int dlen;
- const char *domain;
-
g_return_val_if_fail (cookie != NULL, FALSE);
g_return_val_if_fail (host != NULL, FALSE);
- domain = cookie->domain;
-
- if (!g_ascii_strcasecmp (domain, host))
- return TRUE;
- if (*domain != '.')
- return FALSE;
- if (!g_ascii_strcasecmp (domain + 1, host))
- return TRUE;
- dlen = strlen (domain);
- while ((match = strstr (host, domain))) {
- if (!match[dlen])
- return TRUE;
- host = match + 1;
- }
- return FALSE;
+ return soup_host_matches_host (cookie->domain, host);
}
static inline const char *
diff --git a/libsoup/soup-misc.c b/libsoup/soup-misc.c
index 073e895f..03f58d2a 100644
--- a/libsoup/soup-misc.c
+++ b/libsoup/soup-misc.c
@@ -241,3 +241,38 @@ const char soup_char_attributes[] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
};
+
+/**
+ * soup_host_matches_host
+ * @host: a URI
+ * @compare_with: a URI
+ *
+ * Checks if the @host and @compare_with exactly match or prefixed with a dot.
+ *
+ * Return value: %TRUE if the hosts match, %FALSE otherwise
+ *
+ * Since: 2.53
+ **/
+gboolean
+soup_host_matches_host (const gchar *host, const gchar *compare_with)
+{
+ char *match;
+ int dlen;
+
+ g_return_val_if_fail (host != NULL, FALSE);
+ g_return_val_if_fail (compare_with != NULL, FALSE);
+
+ if (!g_ascii_strcasecmp (host, compare_with))
+ return TRUE;
+ if (*host != '.')
+ return FALSE;
+ if (!g_ascii_strcasecmp (host + 1, compare_with))
+ return TRUE;
+ dlen = strlen (host);
+ while ((match = strstr (compare_with, host))) {
+ if (!match[dlen])
+ return TRUE;
+ compare_with = match + 1;
+ }
+ return FALSE;
+}
diff --git a/libsoup/soup-misc.h b/libsoup/soup-misc.h
index ecb09a86..5e818358 100644
--- a/libsoup/soup-misc.h
+++ b/libsoup/soup-misc.h
@@ -38,6 +38,8 @@ guint soup_str_case_hash (gconstpointer key);
SOUP_AVAILABLE_IN_2_4
gboolean soup_str_case_equal (gconstpointer v1,
gconstpointer v2);
+gboolean soup_host_matches_host (const gchar *host,
+ const gchar *compare_with);
#define _SOUP_ATOMIC_INTERN_STRING(variable, value) ((const char *)(g_atomic_pointer_get (&(variable)) ? (variable) : (g_atomic_pointer_set (&(variable), (gpointer)g_intern_static_string (value)), (variable))))