diff options
author | Christophe Fergeau <cfergeau@redhat.com> | 2015-04-08 17:52:58 +0200 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2016-04-08 17:04:24 +0200 |
commit | 66749deffa4c61fc83531ee8d4fabfa6df796aca (patch) | |
tree | c011479652a463b70f74811690c9080110be2162 | |
parent | 070bbd9894a9436090173d006d600246be011b34 (diff) |
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.
-rw-r--r-- | govirt/ovirt-proxy-private.h | 3 | ||||
-rw-r--r-- | govirt/ovirt-proxy.c | 95 | ||||
-rw-r--r-- | govirt/ovirt-proxy.h | 3 | ||||
-rw-r--r-- | govirt/ovirt-rest-call.c | 3 |
4 files changed, 104 insertions, 0 deletions
diff --git a/govirt/ovirt-proxy-private.h b/govirt/ovirt-proxy-private.h index 1aff423..67f4146 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; gboolean setting_ca_file; gulong ssl_ca_file_changed_id; @@ -72,6 +73,8 @@ gboolean ovirt_rest_call_finish(GAsyncResult *result, GError **err); /* Work around G_GNUC_DEPRECATED attribute on ovirt_proxy_get_vms() */ GList *ovirt_proxy_get_vms_internal(OvirtProxy *proxy); +void ovirt_proxy_append_additional_headers(OvirtProxy *proxy, + RestProxyCall *call); G_END_DECLS diff --git a/govirt/ovirt-proxy.c b/govirt/ovirt-proxy.c index 19bd94b..0604165 100644 --- a/govirt/ovirt-proxy.c +++ b/govirt/ovirt-proxy.c @@ -831,6 +831,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; + if (proxy->priv->api != NULL) { g_object_unref(proxy->priv->api); proxy->priv->api = NULL; @@ -960,6 +964,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 @@ -1032,6 +1040,93 @@ static void vm_collection_changed(GObject *gobject, } +/** + * 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 2f9c2be..7a2fe8b 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); } } |