Blame SOURCES/0003-proxy-Readd-additional-header-API.patch

51ca4a
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
51ca4a
From: Christophe Fergeau <cfergeau@redhat.com>
51ca4a
Date: Wed, 8 Apr 2015 17:52:58 +0200
51ca4a
Subject: [PATCH] proxy: Readd additional header API
51ca4a
51ca4a
This API will be needed in order to set a custom Authorization: header
51ca4a
for authentication with oVirt. However this API can be internal only for
51ca4a
now.
51ca4a
---
51ca4a
 govirt/ovirt-proxy-private.h |  5 +++
51ca4a
 govirt/ovirt-proxy.c         | 95 ++++++++++++++++++++++++++++++++++++++++++++
51ca4a
 govirt/ovirt-proxy.h         |  3 ++
51ca4a
 govirt/ovirt-rest-call.c     |  3 ++
51ca4a
 4 files changed, 106 insertions(+)
51ca4a
51ca4a
diff --git a/govirt/ovirt-proxy-private.h b/govirt/ovirt-proxy-private.h
51ca4a
index b9cc153..6bbfa91 100644
51ca4a
--- a/govirt/ovirt-proxy-private.h
51ca4a
+++ b/govirt/ovirt-proxy-private.h
51ca4a
@@ -38,6 +38,7 @@ struct _OvirtProxyPrivate {
51ca4a
     OvirtApi *api;
51ca4a
     char *jsessionid;
51ca4a
     SoupCookieJar *cookie_jar;
51ca4a
+    GHashTable *additional_headers;
51ca4a
 
51ca4a
     gulong ssl_ca_file_changed_id;
51ca4a
 };
51ca4a
@@ -69,6 +70,10 @@ void ovirt_rest_call_async(OvirtRestCall *call,
51ca4a
                            GDestroyNotify destroy_func);
51ca4a
 gboolean ovirt_rest_call_finish(GAsyncResult *result, GError **err);
51ca4a
 
51ca4a
+void ovirt_proxy_append_additional_headers(OvirtProxy *proxy,
51ca4a
+                                           RestProxyCall *call);
51ca4a
+
51ca4a
+
51ca4a
 G_END_DECLS
51ca4a
 
51ca4a
 #endif /* __OVIRT_PROXY_H__ */
51ca4a
diff --git a/govirt/ovirt-proxy.c b/govirt/ovirt-proxy.c
51ca4a
index cea1042..9e559bc 100644
51ca4a
--- a/govirt/ovirt-proxy.c
51ca4a
+++ b/govirt/ovirt-proxy.c
51ca4a
@@ -774,6 +774,10 @@ ovirt_proxy_dispose(GObject *obj)
51ca4a
         proxy->priv->cookie_jar = NULL;
51ca4a
     }
51ca4a
 
51ca4a
+    g_warn_if_fail(proxy->priv->additional_headers != NULL);
51ca4a
+    g_hash_table_unref(proxy->priv->additional_headers);
51ca4a
+    proxy->priv->additional_headers = NULL;
51ca4a
+
51ca4a
     G_OBJECT_CLASS(ovirt_proxy_parent_class)->dispose(obj);
51ca4a
 }
51ca4a
 
51ca4a
@@ -888,6 +892,10 @@ ovirt_proxy_init(OvirtProxy *self)
51ca4a
     self->priv->cookie_jar = soup_cookie_jar_new();
51ca4a
     rest_proxy_add_soup_feature(REST_PROXY(self),
51ca4a
                                 SOUP_SESSION_FEATURE(self->priv->cookie_jar));
51ca4a
+    self->priv->additional_headers = g_hash_table_new_full(g_str_hash,
51ca4a
+                                                           g_str_equal,
51ca4a
+                                                           g_free,
51ca4a
+                                                           g_free);
51ca4a
 }
51ca4a
 
51ca4a
 /* FIXME : "uri" should just be a base domain, foo.example.com/some/path
51ca4a
@@ -952,6 +960,93 @@ OvirtProxy *ovirt_proxy_new(const char *hostname)
51ca4a
 }
51ca4a
 
51ca4a
 
51ca4a
+/**
51ca4a
+ * ovirt_proxy_add_header:
51ca4a
+ * @proxy: a #OvirtProxy
51ca4a
+ * @header: name of the header to add to each request
51ca4a
+ * @value: value of the header to add to each request
51ca4a
+ *
51ca4a
+ * Add a http header called @header with the value @value to each oVirt REST
51ca4a
+ * API call. If a header with this name already exists, the new value will
51ca4a
+ * replace the old. If @value is NULL then the header will be removed.
51ca4a
+ */
51ca4a
+void ovirt_proxy_add_header(OvirtProxy *proxy, const char *header, const char *value)
51ca4a
+{
51ca4a
+    g_return_if_fail(OVIRT_IS_PROXY(proxy));
51ca4a
+
51ca4a
+    if (value != NULL) {
51ca4a
+        g_hash_table_replace(proxy->priv->additional_headers,
51ca4a
+                             g_strdup(header),
51ca4a
+                             g_strdup(value));
51ca4a
+    } else {
51ca4a
+        g_hash_table_remove(proxy->priv->additional_headers, header);
51ca4a
+    }
51ca4a
+}
51ca4a
+
51ca4a
+
51ca4a
+/**
51ca4a
+ * ovirt_proxy_add_headers:
51ca4a
+ * @proxy: a #OvirtProxy
51ca4a
+ * @...: header name and value pairs, followed by %NULL
51ca4a
+ *
51ca4a
+ * Add the specified http header and value pairs to @proxy. These headers will
51ca4a
+ * be sent with each oVirt REST API call. If a header already exists, the new
51ca4a
+ * value will replace the old.
51ca4a
+ */
51ca4a
+void ovirt_proxy_add_headers(OvirtProxy *proxy, ...)
51ca4a
+{
51ca4a
+    va_list headers;
51ca4a
+
51ca4a
+    g_return_if_fail(OVIRT_IS_PROXY(proxy));
51ca4a
+
51ca4a
+    va_start(headers, proxy);
51ca4a
+    ovirt_proxy_add_headers_from_valist(proxy, headers);
51ca4a
+    va_end(headers);
51ca4a
+}
51ca4a
+
51ca4a
+
51ca4a
+/**
51ca4a
+ * ovirt_proxy_add_headers_from_valist:
51ca4a
+ * @proxy: a #OvirtProxy
51ca4a
+ * @headers: header name and value pairs
51ca4a
+ *
51ca4a
+ * Add the specified http header and value pairs to @proxy. These headers will
51ca4a
+ * be sent with each oVirt REST API call. If a header already exists, the new
51ca4a
+ * value will replace the old.
51ca4a
+ */
51ca4a
+void ovirt_proxy_add_headers_from_valist(OvirtProxy *proxy, va_list headers)
51ca4a
+{
51ca4a
+    const char *header = NULL;
51ca4a
+    const char *value;
51ca4a
+
51ca4a
+    g_return_if_fail(OVIRT_IS_PROXY(proxy));
51ca4a
+
51ca4a
+    header = va_arg(headers, const char *);
51ca4a
+    while (header != NULL) {
51ca4a
+        value = va_arg(headers, const char *);
51ca4a
+        ovirt_proxy_add_header(proxy, header, value);
51ca4a
+        header = va_arg(headers, const char *);
51ca4a
+    }
51ca4a
+}
51ca4a
+
51ca4a
+
51ca4a
+void ovirt_proxy_append_additional_headers(OvirtProxy *proxy,
51ca4a
+                                           RestProxyCall *call)
51ca4a
+{
51ca4a
+    GHashTableIter iter;
51ca4a
+    gpointer key;
51ca4a
+    gpointer value;
51ca4a
+
51ca4a
+    g_return_if_fail(OVIRT_IS_PROXY(proxy));
51ca4a
+    g_return_if_fail(REST_IS_PROXY_CALL(call));
51ca4a
+
51ca4a
+    g_hash_table_iter_init(&iter, proxy->priv->additional_headers);
51ca4a
+    while (g_hash_table_iter_next (&iter, &key, &value)) {
51ca4a
+        rest_proxy_call_add_header(call, key, value);
51ca4a
+    }
51ca4a
+}
51ca4a
+
51ca4a
+
51ca4a
 static void ovirt_proxy_set_api_from_xml(OvirtProxy *proxy,
51ca4a
                                          RestXmlNode *node,
51ca4a
                                          GError **error)
51ca4a
diff --git a/govirt/ovirt-proxy.h b/govirt/ovirt-proxy.h
51ca4a
index d011209..71fa4e0 100644
51ca4a
--- a/govirt/ovirt-proxy.h
51ca4a
+++ b/govirt/ovirt-proxy.h
51ca4a
@@ -66,6 +66,9 @@ struct _OvirtProxyClass {
51ca4a
 GType ovirt_proxy_get_type(void);
51ca4a
 
51ca4a
 OvirtProxy *ovirt_proxy_new(const char *host);
51ca4a
+void ovirt_proxy_add_header(OvirtProxy *proxy, const char *header, const char *value);
51ca4a
+void ovirt_proxy_add_headers(OvirtProxy *proxy, ...);
51ca4a
+void ovirt_proxy_add_headers_from_valist(OvirtProxy *proxy, va_list headers);
51ca4a
 
51ca4a
 G_DEPRECATED_FOR(ovirt_collection_lookup_resource)
51ca4a
 OvirtVm *ovirt_proxy_lookup_vm(OvirtProxy *proxy, const char *vm_name);
51ca4a
diff --git a/govirt/ovirt-rest-call.c b/govirt/ovirt-rest-call.c
51ca4a
index 4095014..9e1d9c8 100644
51ca4a
--- a/govirt/ovirt-rest-call.c
51ca4a
+++ b/govirt/ovirt-rest-call.c
51ca4a
@@ -25,6 +25,7 @@
51ca4a
 #include <rest/rest-params.h>
51ca4a
 
51ca4a
 #include "ovirt-proxy.h"
51ca4a
+#include "ovirt-proxy-private.h"
51ca4a
 #include "ovirt-rest-call.h"
51ca4a
 #include "ovirt-rest-call-error.h"
51ca4a
 #include "ovirt-utils.h"
51ca4a
@@ -128,6 +129,8 @@ static void ovirt_rest_call_constructed(GObject *object)
51ca4a
         }
51ca4a
         rest_proxy_call_add_header(REST_PROXY_CALL(object),
51ca4a
                                    "Prefer", "persistent-auth");
51ca4a
+        ovirt_proxy_append_additional_headers(proxy, REST_PROXY_CALL(object));
51ca4a
+
51ca4a
         g_object_unref(proxy);
51ca4a
     }
51ca4a
 }