[libshumate] Add global user agent option



commit 19555a98da93a82a8ad81606bd57a7ac4769d2ba
Author: James Westman <james jwestman net>
Date:   Mon Feb 21 16:40:58 2022 -0500

    Add global user agent option
    
    Add methods to get and set a global user agent for libshumate.
    
    Fixes #32.

 shumate/meson.build               |  2 ++
 shumate/shumate-tile-downloader.c |  4 +--
 shumate/shumate-user-agent.c      | 59 +++++++++++++++++++++++++++++++++++++++
 shumate/shumate-user-agent.h      | 21 ++++++++++++++
 shumate/shumate.h                 |  1 +
 5 files changed, 85 insertions(+), 2 deletions(-)
---
diff --git a/shumate/meson.build b/shumate/meson.build
index 89c7ef4..b432f64 100644
--- a/shumate/meson.build
+++ b/shumate/meson.build
@@ -20,6 +20,7 @@ libshumate_public_h = [
   'shumate-raster-renderer.h',
   'shumate-tile.h',
   'shumate-tile-downloader.h',
+  'shumate-user-agent.h',
   'shumate-vector-renderer.h',
   'shumate-viewport.h',
   'shumate.h',
@@ -66,6 +67,7 @@ libshumate_sources = [
   'shumate-raster-renderer.c',
   'shumate-tile.c',
   'shumate-tile-downloader.c',
+  'shumate-user-agent.c',
   'shumate-vector-renderer.c',
   'shumate-viewport.c',
 ]
diff --git a/shumate/shumate-tile-downloader.c b/shumate/shumate-tile-downloader.c
index 41b3b8d..56efc6a 100644
--- a/shumate/shumate-tile-downloader.c
+++ b/shumate/shumate-tile-downloader.c
@@ -18,6 +18,7 @@
 #include <libsoup/soup.h>
 #include "shumate-tile-downloader.h"
 #include "shumate-file-cache.h"
+#include "shumate-user-agent.h"
 
 struct _ShumateTileDownloader
 {
@@ -386,8 +387,7 @@ fetch_from_network (GTask *task)
                                          NULL);
 
       g_object_set (G_OBJECT (data->self->soup_session),
-                    "user-agent",
-                    "libshumate/" SHUMATE_VERSION,
+                    "user-agent", shumate_get_user_agent (),
                     "max-conns-per-host", MAX_CONNS_DEFAULT,
                     "max-conns", MAX_CONNS_DEFAULT,
                     NULL);
diff --git a/shumate/shumate-user-agent.c b/shumate/shumate-user-agent.c
new file mode 100644
index 0000000..b17ec4a
--- /dev/null
+++ b/shumate/shumate-user-agent.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2022 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+
+#include "shumate-user-agent.h"
+#include "shumate-version.h"
+
+
+static char *user_agent = NULL;
+
+
+/**
+ * shumate_get_user_agent:
+ *
+ * Gets the user agent libshumate will use for all requests.
+ *
+ * This API is not thread-safe and should only be called from the main thread.
+ *
+ * Returns: (transfer none): the user agent
+ */
+const char *
+shumate_get_user_agent (void)
+{
+  if (user_agent == NULL)
+    return "libshumate/" SHUMATE_VERSION;
+  else
+    return user_agent;
+}
+
+
+/**
+ * shumate_set_user_agent:
+ * @new_user_agent: (nullable): the new user agent, or %NULL to reset to the default
+ *
+ * Sets the user agent that libshumate uses for all requests.
+ *
+ * This API is not thread-safe and should only be called from the main thread.
+ */
+void
+shumate_set_user_agent (const char *new_user_agent)
+{
+  g_clear_pointer (&user_agent, g_free);
+  user_agent = g_strdup (new_user_agent);
+}
diff --git a/shumate/shumate-user-agent.h b/shumate/shumate-user-agent.h
new file mode 100644
index 0000000..0bd9098
--- /dev/null
+++ b/shumate/shumate-user-agent.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2022 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+const char *shumate_get_user_agent (void);
+void        shumate_set_user_agent (const char *new_user_agent);
diff --git a/shumate/shumate.h b/shumate/shumate.h
index 56118b5..070193a 100644
--- a/shumate/shumate.h
+++ b/shumate/shumate.h
@@ -47,6 +47,7 @@
 #include "shumate/shumate-map-source.h"
 #include "shumate/shumate-map-source-registry.h"
 #include "shumate/shumate-tile-downloader.h"
+#include "shumate/shumate-user-agent.h"
 
 #include "shumate/shumate-memory-cache.h"
 #include "shumate/shumate-file-cache.h"


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