[libsoup/hsts-policies-api: 5/5] HSTS: Add API to retrieve a list of policies in an enforcer
- From: Claudio Saavedra <csaavedra src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsoup/hsts-policies-api: 5/5] HSTS: Add API to retrieve a list of policies in an enforcer
- Date: Mon, 5 Aug 2019 14:19:30 +0000 (UTC)
commit d0f97bd67f21a214d396b01fa8b293d0d89cb5ab
Author: Claudio Saavedra <csaavedra igalia com>
Date: Mon Aug 5 15:47:07 2019 +0300
HSTS: Add API to retrieve a list of policies in an enforcer
Add soup_hsts_enforcer_get_policies(). This API is needed for WebKit's
website data manager to handle HSTS policies.
docs/reference/libsoup-2.4-sections.txt | 1 +
libsoup/soup-hsts-enforcer.c | 38 ++++++++++++++++++++++++++++
libsoup/soup-hsts-enforcer.h | 4 +++
tests/hsts-test.c | 45 +++++++++++++++++++++++++++++++++
4 files changed, 88 insertions(+)
---
diff --git a/docs/reference/libsoup-2.4-sections.txt b/docs/reference/libsoup-2.4-sections.txt
index be87fb9d..ea20cfb2 100644
--- a/docs/reference/libsoup-2.4-sections.txt
+++ b/docs/reference/libsoup-2.4-sections.txt
@@ -1403,6 +1403,7 @@ soup_hsts_enforcer_has_valid_policy
soup_hsts_enforcer_set_policy
soup_hsts_enforcer_set_session_policy
soup_hsts_enforcer_get_domains
+soup_hsts_enforcer_get_policies
<SUBSECTION>
SoupHSTSPolicy
soup_hsts_policy_new
diff --git a/libsoup/soup-hsts-enforcer.c b/libsoup/soup-hsts-enforcer.c
index 882bbb87..f57ef079 100644
--- a/libsoup/soup-hsts-enforcer.c
+++ b/libsoup/soup-hsts-enforcer.c
@@ -686,3 +686,41 @@ soup_hsts_enforcer_get_domains (SoupHSTSEnforcer *hsts_enforcer,
return domains;
}
+
+static void
+add_policy_to_list (gpointer key,
+ gpointer value,
+ gpointer data)
+{
+ GList **policies = (GList **) data;
+ *policies = g_list_prepend (*policies, soup_hsts_policy_copy ((SoupHSTSPolicy*)value));
+}
+
+/**
+ * soup_hsts_enforcer_all_policies:
+ * @hsts_enforcer: a #SoupHSTSEnforcer
+ * @session_policies: whether to include session policies
+ *
+ * Gets a list with the policies in @enforcer.
+ *
+ * Returns: (element-type SoupHSTSPolicy) (transfer full): a newly
+ * allocated list of policies. Use g_list_free_full() and
+ * soup_hsts_policy_free() to free the list.
+ *
+ * Since: 2.68
+ *
+ **/
+GList*
+soup_hsts_enforcer_get_policies (SoupHSTSEnforcer *hsts_enforcer,
+ gboolean session_policies)
+{
+ GList *policies = NULL;
+
+ g_return_val_if_fail (SOUP_IS_HSTS_ENFORCER (hsts_enforcer), NULL);
+
+ g_hash_table_foreach (hsts_enforcer->priv->host_policies, add_policy_to_list, &policies);
+ if (session_policies)
+ g_hash_table_foreach (hsts_enforcer->priv->session_policies, add_policy_to_list, &policies);
+
+ return policies;
+}
diff --git a/libsoup/soup-hsts-enforcer.h b/libsoup/soup-hsts-enforcer.h
index 2cad1d1c..c9beaa41 100644
--- a/libsoup/soup-hsts-enforcer.h
+++ b/libsoup/soup-hsts-enforcer.h
@@ -80,6 +80,10 @@ SOUP_AVAILABLE_IN_2_68
GList *soup_hsts_enforcer_get_domains (SoupHSTSEnforcer *hsts_enforcer,
gboolean session_policies);
+SOUP_AVAILABLE_IN_2_68
+GList *soup_hsts_enforcer_get_policies (SoupHSTSEnforcer *hsts_enforcer,
+ gboolean session_policies);
+
G_END_DECLS
#endif /* __SOUP_HSTS_ENFORCER_H__ */
diff --git a/tests/hsts-test.c b/tests/hsts-test.c
index 5918ee39..ba919c81 100644
--- a/tests/hsts-test.c
+++ b/tests/hsts-test.c
@@ -503,6 +503,50 @@ do_hsts_get_domains_test (void)
g_object_unref (enforcer);
}
+static void
+do_hsts_get_policies_test (void)
+{
+ SoupHSTSEnforcer *enforcer = soup_hsts_enforcer_new ();
+ SoupHSTSPolicy *policy;
+ GList* policies;
+
+ g_assert_null (soup_hsts_enforcer_get_policies (enforcer, TRUE));
+ g_assert_null (soup_hsts_enforcer_get_policies (enforcer, FALSE));
+
+ policy = soup_hsts_policy_new ("gnome.org", 3600, FALSE);
+ g_assert_nonnull (policy);
+ soup_hsts_enforcer_set_policy (enforcer, policy);
+ soup_hsts_policy_free (policy);
+
+ policy = soup_hsts_policy_new_session_policy ("freedesktop.org", FALSE);
+ g_assert_nonnull (policy);
+ soup_hsts_enforcer_set_policy (enforcer, policy);
+ soup_hsts_policy_free (policy);
+
+ policies = soup_hsts_enforcer_get_policies (enforcer, TRUE);
+ g_assert_nonnull (policies);
+ g_assert_cmpint (g_list_length (policies), ==, 2);
+ g_list_free_full (policies, (GDestroyNotify)soup_hsts_policy_free);
+
+ policies = soup_hsts_enforcer_get_policies (enforcer, FALSE);
+ g_assert_nonnull (policies);
+ g_assert_cmpint (g_list_length (policies), ==, 1);
+ policy = (SoupHSTSPolicy*)policies->data;
+ g_assert_cmpstr (policy->domain, ==, "gnome.org");
+ g_list_free_full (policies, (GDestroyNotify)soup_hsts_policy_free);
+
+ policy = soup_hsts_policy_new ("gnome.org", SOUP_HSTS_POLICY_MAX_AGE_PAST, FALSE);
+ soup_hsts_enforcer_set_policy (enforcer, policy);
+ soup_hsts_policy_free (policy);
+
+ policies = soup_hsts_enforcer_get_policies (enforcer, TRUE);
+ g_assert_cmpint (g_list_length (policies), ==, 1);
+ policy = (SoupHSTSPolicy*)policies->data;
+ g_assert_cmpstr (policy->domain, ==, "freedesktop.org");
+ g_list_free_full (policies, (GDestroyNotify)soup_hsts_policy_free);
+ g_object_unref(enforcer);
+}
+
int
main (int argc, char **argv)
{
@@ -549,6 +593,7 @@ main (int argc, char **argv)
g_test_add_func ("/hsts/session-policy", do_hsts_session_policy_test);
g_test_add_func ("/hsts/idna-addresses", do_hsts_idna_addresses_test);
g_test_add_func ("/hsts/get-domains", do_hsts_get_domains_test);
+ g_test_add_func ("/hsts/get-policies", do_hsts_get_policies_test);
ret = g_test_run ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]