[grilo] net: Add possibility to add arbitrary HTTP headers



commit bf186d0626220b8515cbf2ce2809e404ce6f0d28
Author: Jens Georg <jensg openismus com>
Date:   Thu Jul 12 22:44:47 2012 +0200

    net: Add possibility to add arbitrary HTTP headers
    
    Some webservices need custom HTTP headers for requests; this patch adds
    a function to schedule requests with those special headers.
    
    Under contract for Canonical Ltd.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680436
    
    Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>

 libs/net/grl-net-private.h       |    1 +
 libs/net/grl-net-soup-stable.c   |   13 +++++
 libs/net/grl-net-soup-unstable.c |   19 +++++++
 libs/net/grl-net-wc.c            |   97 ++++++++++++++++++++++++++++++++++++-
 libs/net/grl-net-wc.h            |   16 ++++++
 5 files changed, 143 insertions(+), 3 deletions(-)
---
diff --git a/libs/net/grl-net-private.h b/libs/net/grl-net-private.h
index 184fb16..3d15a0c 100644
--- a/libs/net/grl-net-private.h
+++ b/libs/net/grl-net-private.h
@@ -51,6 +51,7 @@ void parse_error (guint status,
 
 void get_url_now (GrlNetWc *self,
                   const char *url,
+                  GHashTable *headers,
                   GAsyncResult *result,
                   GCancellable *cancellable);
 
diff --git a/libs/net/grl-net-soup-stable.c b/libs/net/grl-net-soup-stable.c
index d1e8926..70ae7ec 100644
--- a/libs/net/grl-net-soup-stable.c
+++ b/libs/net/grl-net-soup-stable.c
@@ -1,10 +1,12 @@
 /*
  * Copyright (C) 2012 Igalia S.L.
+ * Copyright (C) 2012 Canonical Ltd.
  *
  * Contact: Iago Toral Quiroga <itoral igalia com>
  *
  * Authors: VÃctor M. JÃquez L. <vjaquez igalia com>
  *          Juan A. Suarez Romero <jasuarez igalia com>
+ *          Jens Georg <jensg openismus com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -74,6 +76,7 @@ message_cancel_cb (GCancellable *cancellable,
 void
 get_url_now (GrlNetWc *self,
 	     const char *url,
+	     GHashTable *headers,
 	     GAsyncResult *result,
 	     GCancellable *cancellable)
 {
@@ -93,6 +96,16 @@ get_url_now (GrlNetWc *self,
     return;
   }
 
+  if (headers != NULL) {
+    GHashTableIter iter;
+    const char *key, *value;
+
+    g_hash_table_iter_init (&iter, headers);
+    while (g_hash_table_iter_next (&iter, &key, &value)) {
+      soup_message_headers_append (msg->request_headers, key, value);
+    }
+  }
+
   g_simple_async_result_set_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result),
                                              msg, NULL);
 
diff --git a/libs/net/grl-net-soup-unstable.c b/libs/net/grl-net-soup-unstable.c
index 275aa10..cc81c38 100644
--- a/libs/net/grl-net-soup-unstable.c
+++ b/libs/net/grl-net-soup-unstable.c
@@ -1,10 +1,12 @@
 /*
  * Copyright (C) 2012 Igalia S.L.
+ * Copyright (C) 2012 Canonical Ltd.
  *
  * Contact: Iago Toral Quiroga <itoral igalia com>
  *
  * Authors: VÃctor M. JÃquez L. <vjaquez igalia com>
  *          Juan A. Suarez Romero <jasuarez igalia com>
+ *          Jens Georg <jensg openismus com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -245,6 +247,7 @@ reply_cb (GObject *source,
 void
 get_url_now (GrlNetWc *self,
              const char *url,
+             GHashTable *headers,
              GAsyncResult *result,
              GCancellable *cancellable)
 {
@@ -256,6 +259,22 @@ get_url_now (GrlNetWc *self,
                                              NULL);
 
   rr->request = soup_requester_request (priv->requester, url, NULL);
+  if (headers != NULL) {
+    SoupMessage *message;
+    GHashTableIter iter;
+    const char *key, *value;
+
+    message = soup_request_http_get_message (SOUP_REQUEST_HTTP (rr->request));
+
+    if (message) {
+      g_hash_table_iter_init (&iter, headers);
+      while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *)&value)) {
+        soup_message_headers_append (message->request_headers, key, value);
+      }
+      g_object_unref (message);
+    }
+  }
+
   soup_request_send_async (rr->request, cancellable, reply_cb, result);
 }
 
diff --git a/libs/net/grl-net-wc.c b/libs/net/grl-net-wc.c
index 6d1fb32..3ee7471 100644
--- a/libs/net/grl-net-wc.c
+++ b/libs/net/grl-net-wc.c
@@ -1,10 +1,12 @@
 /*
  * Copyright (C) 2010, 2011 Igalia S.L.
+ * Copyright (C) 2012 Canonical Ltd.
  *
  * Contact: Iago Toral Quiroga <itoral igalia com>
  *
  * Authors: VÃctor M. JÃquez L. <vjaquez igalia com>
  *          Juan A. Suarez Romero <jasuarez igalia com>
+ *          Jens Georg <jensg openismus com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -277,6 +279,7 @@ struct request_clos {
   char *url;
   GAsyncResult *result;
   GCancellable *cancellable;
+  GHashTable *headers;
   guint source_id;
 };
 
@@ -292,9 +295,10 @@ get_url_delayed (gpointer user_data)
     g_assert (c == d);
   }
 
-  get_url_now (c->self, c->url, c->result, c->cancellable);
+  get_url_now (c->self, c->url, c->headers, c->result, c->cancellable);
 
   g_free (c->url);
+  g_hash_table_unref (c->headers);
   g_free (c);
 
   return FALSE;
@@ -303,6 +307,7 @@ get_url_delayed (gpointer user_data)
 static void
 get_url (GrlNetWc *self,
          const char *url,
+         GHashTable *headers,
          GAsyncResult *result,
          GCancellable *cancellable)
 {
@@ -314,7 +319,7 @@ get_url (GrlNetWc *self,
   g_get_current_time (&now);
 
   if ((now.tv_sec - priv->last_request.tv_sec) > priv->throttling) {
-    get_url_now (self, url, result, cancellable);
+    get_url_now (self, url, headers, result, cancellable);
     g_get_current_time (&priv->last_request);
 
     return;
@@ -326,6 +331,7 @@ get_url (GrlNetWc *self,
   c = g_new (struct request_clos, 1);
   c->self = self;
   c->url = g_strdup (url);
+  c->headers = g_hash_table_ref (headers);
   c->result = result;
   c->cancellable = cancellable;
 
@@ -370,6 +376,91 @@ grl_net_wc_request_async (GrlNetWc *self,
                           GAsyncReadyCallback callback,
                           gpointer user_data)
 {
+  grl_net_wc_request_with_headers_hash_async (self,
+                                              uri,
+                                              NULL,
+                                              cancellable,
+                                              callback,
+                                              user_data);
+}
+
+/**
+ * grl_net_wc_request_with_headers_async:
+ * @self: a #GrlNetWc instance
+ * @uri: The URI of the resource to request
+ * @cancellable: (allow-none): a #GCancellable instance or %NULL to ignore
+ * @callback: The callback when the result is ready
+ * @user_data: User data set for the @callback
+ * @Varargs: List of tuples of header name and header value, terminated by
+ * %NULL.
+ *
+ * Request the fetching of a web resource given the @uri. This request is
+ * asynchronous, thus the result will be returned within the @callback.
+ */
+void grl_net_wc_request_with_headers_async (GrlNetWc *self,
+                                            const char *uri,
+                                            GCancellable *cancellable,
+                                            GAsyncReadyCallback callback,
+                                            gpointer user_data,
+                                            ...)
+{
+  va_list va_args;
+  const gchar *header_name = NULL, *header_value = NULL;
+  GHashTable *headers = NULL;
+
+  va_start (va_args, user_data);
+
+  header_name = va_arg (va_args, const gchar *);
+  while (header_name) {
+    header_value = va_arg (va_args, const gchar *);
+    if (header_value) {
+      if (headers == NULL) {
+        headers = g_hash_table_new_full (g_str_hash,
+                                         g_str_equal,
+                                         g_free,
+                                         g_free);
+      }
+      g_hash_table_insert (headers, g_strdup (header_name), g_strdup (header_value));
+    }
+    header_name = va_arg (va_args, const gchar *);
+  }
+
+  va_end (va_args);
+
+  grl_net_wc_request_with_headers_hash_async (self,
+                                              uri,
+                                              headers,
+                                              cancellable,
+                                              callback,
+                                              user_data);
+
+  if (headers)
+    g_hash_table_unref (headers);
+}
+
+
+/**
+ * grl_net_wc_request_with_headers_hash_async:
+ * @self: a #GrlNetWc instance
+ * @uri: The URI of the resource to request
+ * @headers: (allow-none) (element-type utf8 utf8): a set of additional HTTP
+ * headers for this request or %NULL to ignore
+ * @cancellable: (allow-none): a #GCancellable instance or %NULL to ignore
+ * @callback: The callback when the result is ready
+ * @user_data: User data set for the @callback
+ *
+ * Request the fetching of a web resource given the @uri. This request is
+ * asynchronous, thus the result will be returned within the @callback.
+ * Rename to: grl_net_wc_request_with_headers_async
+ */
+void
+grl_net_wc_request_with_headers_hash_async (GrlNetWc *self,
+                                            const char *uri,
+                                            GHashTable *headers,
+                                            GCancellable *cancellable,
+                                            GAsyncReadyCallback callback,
+                                            gpointer user_data)
+{
   GSimpleAsyncResult *result;
 
   result = g_simple_async_result_new (G_OBJECT (self),
@@ -377,7 +468,7 @@ grl_net_wc_request_async (GrlNetWc *self,
                                       user_data,
                                       grl_net_wc_request_async);
 
-  get_url (self, uri, G_ASYNC_RESULT (result), cancellable);
+  get_url (self, uri, headers, G_ASYNC_RESULT (result), cancellable);
 }
 
 /**
diff --git a/libs/net/grl-net-wc.h b/libs/net/grl-net-wc.h
index 05db8fa..becad74 100644
--- a/libs/net/grl-net-wc.h
+++ b/libs/net/grl-net-wc.h
@@ -1,9 +1,11 @@
 /*
  * Copyright (C) 2010, 2011 Igalia S.L.
+ * Copyright (C) 2012 Canonical Ltd.
  *
  * Contact: Iago Toral Quiroga <itoral igalia com>
  *
  * Authors: VÃctor M. JÃquez L. <vjaquez igalia com>
+ *          Jens Georg <jensg openismus com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -119,6 +121,20 @@ void grl_net_wc_request_async (GrlNetWc *self,
 			       GAsyncReadyCallback callback,
 			       gpointer user_data);
 
+void grl_net_wc_request_with_headers_hash_async (GrlNetWc *self,
+                                                 const char *uri,
+                                                 GHashTable *headers,
+                                                 GCancellable *cancellable,
+                                                 GAsyncReadyCallback callback,
+                                                 gpointer user_data);
+
+void grl_net_wc_request_with_headers_async (GrlNetWc *self,
+                                            const char *uri,
+                                            GCancellable *cancellable,
+                                            GAsyncReadyCallback callback,
+                                            gpointer user_data,
+                                            ...) G_GNUC_NULL_TERMINATED;
+
 gboolean grl_net_wc_request_finish (GrlNetWc *self,
 				    GAsyncResult *result,
 				    gchar **content,



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