[librest] Allow to modify REST function in serialize_params vfunc



commit c66b6df501a0b40210ab2f23882ce3f57fdb1f5f
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Mon Sep 16 13:13:52 2013 +0200

    Allow to modify REST function in serialize_params vfunc
    
    RestProxyCall::prepare_message() calls the serialize_params vfunc
    so that child classes can serialize the call parameters in whichever
    way they want.
    One way of doing that could be to append the parameters to the URI
    that is called (http://example.com?param1=value1;param2=value2).
    However, the URI to call is determined at the beginning of
    prepare_message(), and is not refreshed after calling
    RestProxyCall::serialize_params(), so it's not possible to
    append parameters to the URI that is going to be called.
    
    This commit rebuilds the URI to call after calling serialize_params()
    in case it has changed.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=708359

 rest/rest-proxy-call.c   |   60 ++++++++++++++++++++++++++++++++++++----------
 tests/custom-serialize.c |    3 +-
 2 files changed, 49 insertions(+), 14 deletions(-)
---
diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c
index e94bba4..bfe580b 100644
--- a/rest/rest-proxy-call.c
+++ b/rest/rest-proxy-call.c
@@ -742,23 +742,13 @@ set_header (gpointer key, gpointer value, gpointer user_data)
   soup_message_headers_replace (headers, name, value);
 }
 
-static SoupMessage *
-prepare_message (RestProxyCall *call, GError **error_out)
+static gboolean
+set_url (RestProxyCall *call)
 {
   RestProxyCallPrivate *priv;
-  RestProxyCallClass *call_class;
-  const gchar *bound_url, *user_agent;
-  SoupMessage *message;
-  GError *error = NULL;
+  const gchar *bound_url;
 
   priv = GET_PRIVATE (call);
-  call_class = REST_PROXY_CALL_GET_CLASS (call);
-
-  /* Emit a warning if the caller is re-using RestProxyCall objects */
-  if (priv->url)
-  {
-    g_warning (G_STRLOC ": re-use of RestProxyCall %p, don't do this", call);
-  }
 
   bound_url =_rest_proxy_get_bound_url (priv->proxy);
 
@@ -768,6 +758,8 @@ prepare_message (RestProxyCall *call, GError **error_out)
     return FALSE;
   }
 
+  g_free (priv->url);
+
   /* FIXME: Perhaps excessive memory duplication */
   if (priv->function)
   {
@@ -782,6 +774,27 @@ prepare_message (RestProxyCall *call, GError **error_out)
     priv->url = g_strdup (bound_url);
   }
 
+  return TRUE;
+}
+
+static SoupMessage *
+prepare_message (RestProxyCall *call, GError **error_out)
+{
+  RestProxyCallPrivate *priv;
+  RestProxyCallClass *call_class;
+  const gchar *user_agent;
+  SoupMessage *message;
+  GError *error = NULL;
+
+  priv = GET_PRIVATE (call);
+  call_class = REST_PROXY_CALL_GET_CLASS (call);
+
+  /* Emit a warning if the caller is re-using RestProxyCall objects */
+  if (priv->url)
+  {
+    g_warning (G_STRLOC ": re-use of RestProxyCall %p, don't do this", call);
+  }
+
   /* Allow an overrideable prepare function that is called before every
    * invocation so subclasses can do magic
    */
@@ -806,6 +819,16 @@ prepare_message (RestProxyCall *call, GError **error_out)
       return NULL;
     }
 
+    /* Reset priv->url as the serialize_params vcall may have called
+     * rest_proxy_call_set_function()
+     */
+    if (!set_url (call))
+    {
+        g_free (content);
+        g_free (content_type);
+        return NULL;
+    }
+
     message = soup_message_new (priv->method, priv->url);
     if (message == NULL) {
         g_free (content);
@@ -823,6 +846,11 @@ prepare_message (RestProxyCall *call, GError **error_out)
   } else if (rest_params_are_strings (priv->params)) {
     GHashTable *hash;
 
+    if (!set_url (call))
+    {
+        return NULL;
+    }
+
     hash = rest_params_as_string_hash_table (priv->params);
 
     message = soup_form_request_new_from_hash (priv->method,
@@ -860,6 +888,12 @@ prepare_message (RestProxyCall *call, GError **error_out)
       }
     }
 
+    if (!set_url (call))
+    {
+        soup_multipart_free (mp);
+        return NULL;
+    }
+
     message = soup_form_request_new_from_multipart (priv->url, mp);
 
     soup_multipart_free (mp);
diff --git a/tests/custom-serialize.c b/tests/custom-serialize.c
index 3f8cb80..c6b801f 100644
--- a/tests/custom-serialize.c
+++ b/tests/custom-serialize.c
@@ -70,6 +70,7 @@ custom_proxy_call_serialize (RestProxyCall *self,
   *content_type = g_strdup ("application/json");
   *content = g_strdup ("{}");
   *content_len = strlen (*content);
+  rest_proxy_call_set_function (self, "ping");
 
   return TRUE;
 }
@@ -137,7 +138,7 @@ main (int argc, char **argv)
   proxy = rest_proxy_new (url, FALSE);
   call = g_object_new (REST_TYPE_CUSTOM_PROXY_CALL, "proxy", proxy, NULL);
 
-  rest_proxy_call_set_function (call, "ping");
+  rest_proxy_call_set_function (call, "wrong-function");
 
   if (!rest_proxy_call_sync (call, &error)) {
     g_printerr ("Call failed: %s\n", error->message);


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