[geocode-glib] Add a default user-agent based on GApplication id



commit 63aa0715f396457e5b127eb2a011ecb390bcf819
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Fri Jan 8 09:06:13 2016 +0100

    Add a default user-agent based on GApplication id
    
    Nominatim’s usage policy requires a User-Agent. They want to be
    able to block batch offenders that misuse the terms of service.
    
    We can set a user-agent based on GApplication (if we are one) to
    make sure at least trusted GNOME applications can be identified.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=756313

 configure.ac                        |    2 +-
 geocode-glib/Makefile.am            |    1 +
 geocode-glib/geocode-forward.c      |    2 +-
 geocode-glib/geocode-glib-private.h |    1 +
 geocode-glib/geocode-glib.c         |   23 +++++++++++++++++++++++
 geocode-glib/geocode-reverse.c      |    2 +-
 6 files changed, 28 insertions(+), 3 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 8ed98a1..91948d0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,7 +62,7 @@ dnl Requires for the library
 PKG_CHECK_MODULES(GEOCODE,
                  gio-2.0 >= 2.34
                  json-glib-1.0 >= 0.99.2
-                 libsoup-2.4)
+                 libsoup-2.4 >= 2.42)
 
 GLIB_GENMARSHAL=`$PKG_CONFIG --variable=glib_genmarshal glib-2.0`
 AC_SUBST(GLIB_GENMARSHAL)
diff --git a/geocode-glib/Makefile.am b/geocode-glib/Makefile.am
index ef94fc3..f62056e 100644
--- a/geocode-glib/Makefile.am
+++ b/geocode-glib/Makefile.am
@@ -45,6 +45,7 @@ gcglib_HEADERS =                                      \
 AM_CPPFLAGS =                                          \
        -I$(top_srcdir)                                 \
        -I$(top_builddir)                               \
+       -DPACKAGE_VERSION=\"@PACKAGE_VERSION \"         \
        -DTEST_SRCDIR=\""$(srcdir)/data/"\"             \
        -DGEOCODE_LOCALEDIR=\"$(localedir)\"
 
diff --git a/geocode-glib/geocode-forward.c b/geocode-glib/geocode-forward.c
index 99b7d24..f931940 100644
--- a/geocode-glib/geocode-forward.c
+++ b/geocode-glib/geocode-forward.c
@@ -202,7 +202,7 @@ geocode_forward_init (GeocodeForward *forward)
        forward->priv = G_TYPE_INSTANCE_GET_PRIVATE ((forward), GEOCODE_TYPE_FORWARD, GeocodeForwardPrivate);
        forward->priv->ht = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                   g_free, g_free);
-        forward->priv->soup_session = soup_session_new ();
+       forward->priv->soup_session = _geocode_glib_build_soup_session ();
        forward->priv->answer_count = DEFAULT_ANSWER_COUNT;
        forward->priv->search_area = NULL;
        forward->priv->bounded = FALSE;
diff --git a/geocode-glib/geocode-glib-private.h b/geocode-glib/geocode-glib-private.h
index 7dde8e7..1be5d2d 100644
--- a/geocode-glib/geocode-glib-private.h
+++ b/geocode-glib/geocode-glib-private.h
@@ -55,6 +55,7 @@ gboolean _geocode_glib_cache_load (SoupMessage *query,
                                    char       **contents);
 GHashTable *_geocode_glib_dup_hash_table (GHashTable *ht);
 gboolean _geocode_object_is_number_after_street (void);
+SoupSession *_geocode_glib_build_soup_session (void);
 
 G_END_DECLS
 
diff --git a/geocode-glib/geocode-glib.c b/geocode-glib/geocode-glib.c
index 0ac4eb5..4e3b3fd 100644
--- a/geocode-glib/geocode-glib.c
+++ b/geocode-glib/geocode-glib.c
@@ -37,6 +37,29 @@
  * <ulink url="http://wiki.openstreetmap.org/wiki/Nominatim";>OSM Nominatim APIs</ulink>
  **/
 
+SoupSession *
+_geocode_glib_build_soup_session (void)
+{
+       GApplication *application;
+       SoupSession *session;
+       char *user_agent;
+
+       application = g_application_get_default ();
+       if (application) {
+               const char *id = g_application_get_application_id (application);
+               user_agent = g_strdup_printf ("geocode-glib/%s (%s)",
+                                             PACKAGE_VERSION, id);
+       } else {
+               user_agent = g_strdup_printf ("geocode-glib/%s",
+                                             PACKAGE_VERSION);
+       }
+
+       session = soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
+                                                user_agent, NULL);
+       g_free (user_agent);
+       return session;
+}
+
 char *
 _geocode_glib_cache_path_for_query (SoupMessage *query)
 {
diff --git a/geocode-glib/geocode-reverse.c b/geocode-glib/geocode-reverse.c
index 7d251f4..b78c381 100644
--- a/geocode-glib/geocode-reverse.c
+++ b/geocode-glib/geocode-reverse.c
@@ -79,7 +79,7 @@ geocode_reverse_init (GeocodeReverse *object)
        object->priv = G_TYPE_INSTANCE_GET_PRIVATE ((object), GEOCODE_TYPE_REVERSE, GeocodeReversePrivate);
        object->priv->ht = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  g_free, g_free);
-        object->priv->soup_session = soup_session_new ();
+       object->priv->soup_session = _geocode_glib_build_soup_session ();
 }
 
 /**


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