diff options
author | Christophe Fergeau <cfergeau@redhat.com> | 2013-09-16 13:13:52 +0200 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2014-09-03 12:17:28 +0200 |
commit | f655f0702d949149bff2608ab8f8ffe31d840a12 (patch) | |
tree | f3da0315e7d6bcd853b16df3744ac35d281cce32 | |
parent | 3e6598acbaceac2353848d860a8b8e7c692e1a4f (diff) |
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
(cherry picked from commit c66b6df501a0b40210ab2f23882ce3f57fdb1f5f)
-rw-r--r-- | rest/rest-proxy-call.c | 60 | ||||
-rw-r--r-- | 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 03c1874..9c3c9f3 100644 --- a/rest/rest-proxy-call.c +++ b/rest/rest-proxy-call.c @@ -734,23 +734,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); @@ -760,6 +750,8 @@ prepare_message (RestProxyCall *call, GError **error_out) return FALSE; } + g_free (priv->url); + /* FIXME: Perhaps excessive memory duplication */ if (priv->function) { @@ -774,6 +766,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 */ @@ -798,6 +811,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); @@ -815,6 +838,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, @@ -852,6 +880,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); |