[libgovirt] Cookie



commit b88a8e7f4c6b95707a71fb5a2cb56e70f8469312
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Tue Dec 3 10:11:19 2013 +0100

    Cookie

 govirt/ovirt-action-rest-call.c |   11 +++++++++-
 govirt/ovirt-proxy-private.h    |    1 +
 govirt/ovirt-proxy.c            |   41 +++++++++++++++++++++++++++++++++++++++
 govirt/ovirt-rest-call.c        |   15 +++++++++++++-
 4 files changed, 66 insertions(+), 2 deletions(-)
---
diff --git a/govirt/ovirt-action-rest-call.c b/govirt/ovirt-action-rest-call.c
index a4bf7c4..afd623d 100644
--- a/govirt/ovirt-action-rest-call.c
+++ b/govirt/ovirt-action-rest-call.c
@@ -87,16 +87,25 @@ OvirtActionRestCall *ovirt_action_rest_call_new(RestProxy *proxy)
 {
     OvirtActionRestCall *call;
     gboolean admin;
+    char *session_id;
 
     g_return_val_if_fail(OVIRT_IS_PROXY(proxy), NULL);
     call = OVIRT_ACTION_REST_CALL(g_object_new(OVIRT_TYPE_ACTION_REST_CALL, "proxy", proxy, NULL));
     g_return_val_if_fail(call != NULL, NULL);
-    g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
+    g_object_get(G_OBJECT(proxy), "admin", &admin, "session-id", &session_id, NULL);
     if (admin) {
         rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "false");
     } else {
         rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "true");
     }
+    rest_proxy_call_add_header(REST_PROXY_CALL(call), "Prefer", "persistent-auth");
+    if (session_id != NULL) {
+        char *header;
+        header = g_strdup_printf("JSESSIONID=%s", session_id);
+        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Cookie", header);
+        g_free(header);
+        g_free(session_id);
+    }
 
     return call;
 }
diff --git a/govirt/ovirt-proxy-private.h b/govirt/ovirt-proxy-private.h
index 1053ce8..a45e7fe 100644
--- a/govirt/ovirt-proxy-private.h
+++ b/govirt/ovirt-proxy-private.h
@@ -34,6 +34,7 @@ struct _OvirtProxyPrivate {
     char *tmp_ca_file;
     gboolean admin_mode;
     OvirtApi *api;
+    char *jsessionid;
 
     gulong ssl_ca_file_changed_id;
 };
diff --git a/govirt/ovirt-proxy.c b/govirt/ovirt-proxy.c
index b4ee32f..7a6c667 100644
--- a/govirt/ovirt-proxy.c
+++ b/govirt/ovirt-proxy.c
@@ -46,6 +46,7 @@ enum {
     PROP_0,
     PROP_CA_CERT,
     PROP_ADMIN,
+    PROP_SESSION_ID
 };
 
 #define CA_CERT_FILENAME "ca.crt"
@@ -162,6 +163,29 @@ RestXmlNode *ovirt_proxy_get_collection_xml(OvirtProxy *proxy,
         return NULL;
     }
 
+#if 0
+    GHashTable *headers_hash;
+    headers_hash = rest_proxy_call_get_response_headers(call);
+    if (headers_hash != NULL) {
+        GList *headers;
+        GList *it;
+        headers = g_hash_table_get_keys(headers_hash);
+        for (it = headers; it != NULL; it = it->next) {
+            g_warning("HEADER: %s -- %s", it->data,
+                    g_hash_table_lookup(headers_hash, it->data));
+        }
+        g_list_free(headers);
+        g_hash_table_unref(headers_hash);
+    }
+#endif
+
+    gchar *session_id;
+    session_id = rest_proxy_call_lookup_response_header(call, "JSESSIONID");
+    if ((session_id != NULL) && (g_strcmp0(session_id, proxy->priv->jsessionid) != 0)) {
+        g_object_set(G_OBJECT(proxy), "session-id", session_id);
+    }
+    g_warning("COOKIE: %s", rest_proxy_call_lookup_response_header(call, "Set-Cookie"));
+    g_warning("JSESSIONID: %s", rest_proxy_call_lookup_response_header(call, "JSESSIONID"));
     root = ovirt_rest_xml_node_from_call(call);
     g_object_unref(G_OBJECT(call));
 
@@ -690,6 +714,9 @@ static void ovirt_proxy_get_property(GObject *object,
     case PROP_ADMIN:
         g_value_set_boolean(value, proxy->priv->admin_mode);
         break;
+    case PROP_SESSION_ID:
+        g_value_set_string(value, proxy->priv->jsessionid);
+        break;
 
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
@@ -715,6 +742,11 @@ static void ovirt_proxy_set_property(GObject *object,
         proxy->priv->admin_mode = g_value_get_boolean(value);
         break;
 
+    case PROP_SESSION_ID:
+        g_free(proxy->priv->jsessionid);
+        proxy->priv->jsessionid = g_value_dup_string(value);
+        break;
+
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     }
@@ -739,6 +771,7 @@ ovirt_proxy_finalize(GObject *obj)
     OvirtProxy *proxy = OVIRT_PROXY(obj);
 
     ovirt_proxy_set_tmp_ca_file(proxy, NULL);
+    g_free(proxy->priv->jsessionid);
 
     G_OBJECT_CLASS(ovirt_proxy_parent_class)->finalize(obj);
 }
@@ -770,6 +803,14 @@ ovirt_proxy_class_init(OvirtProxyClass *klass)
                                                          FALSE,
                                                          G_PARAM_READWRITE |
                                                          G_PARAM_STATIC_STRINGS));
+    g_object_class_install_property(oclass,
+                                    PROP_SESSION_ID,
+                                    g_param_spec_string("session-id",
+                                                        "session-id",
+                                                        "oVirt/RHEV JSESSIONID",
+                                                        NULL,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
 
     g_type_class_add_private(klass, sizeof(OvirtProxyPrivate));
 }
diff --git a/govirt/ovirt-rest-call.c b/govirt/ovirt-rest-call.c
index 42c6a51..076c849 100644
--- a/govirt/ovirt-rest-call.c
+++ b/govirt/ovirt-rest-call.c
@@ -118,18 +118,31 @@ static void ovirt_rest_call_finalize(GObject *object)
 static void ovirt_rest_call_constructed(GObject *object)
 {
     OvirtProxy *proxy;
+    OvirtRestCall *call = OVIRT_REST_CALL(object);
 
     G_OBJECT_CLASS(ovirt_rest_call_parent_class)->constructed(object);
 
     g_object_get(object, "proxy", &proxy, NULL);
     if (proxy != NULL) {
         gboolean admin;
-        g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
+        char *session_id;
+        g_object_get(G_OBJECT(proxy),
+                     "admin", &admin,
+                     "session-id", &session_id,
+                     NULL);
         if (admin) {
             rest_proxy_call_add_header(REST_PROXY_CALL(object), "Filter", "false");
         } else {
             rest_proxy_call_add_header(REST_PROXY_CALL(object), "Filter", "true");
         }
+        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Prefer", "persistent-auth");
+        if (session_id != NULL) {
+            char *header;
+            header = g_strdup_printf("JSESSIONID=%s", session_id);
+            rest_proxy_call_add_header(REST_PROXY_CALL(call), "Cookie", header);
+            g_free(header);
+            g_free(session_id);
+        }
         g_object_unref(proxy);
     }
 }


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