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