diff options
author | Ross Burton <ross@linux.intel.com> | 2010-07-06 11:24:52 +0100 |
---|---|---|
committer | Ross Burton <ross@linux.intel.com> | 2010-07-08 21:17:06 +0100 |
commit | 77903df0256907073e0d8cadd9bd6acf4ff3d2d4 (patch) | |
tree | 824a0efa1885b538c374cdb31c0e0b5d3a63b7f5 /rest | |
parent | 23de181bdb4eae9fe769a8f20d5359cbf443e52a (diff) |
Port the proxies to use RestParam
Diffstat (limited to 'rest')
-rw-r--r-- | rest/oauth-proxy-call.c | 46 | ||||
-rw-r--r-- | rest/rest-proxy-call-private.h | 3 | ||||
-rw-r--r-- | rest/rest-proxy-call.c | 99 | ||||
-rw-r--r-- | rest/rest-proxy-call.h | 16 |
4 files changed, 117 insertions, 47 deletions
diff --git a/rest/oauth-proxy-call.c b/rest/oauth-proxy-call.c index 4d824c6..ab2ba5d 100644 --- a/rest/oauth-proxy-call.c +++ b/rest/oauth-proxy-call.c @@ -96,7 +96,21 @@ merge_hashes (GHashTable *hash, GHashTable *from) g_hash_table_iter_init (&iter, from); while (g_hash_table_iter_next (&iter, &key, &value)) { - g_hash_table_insert (hash, key, g_strdup (value)); + g_hash_table_insert (hash, key, value); + } +} + +static void +merge_params (GHashTable *hash, RestParams *params) +{ + RestParamsIter iter; + const char *name; + RestParam *param; + + rest_params_iter_init (&iter, params); + while (rest_params_iter_next (&iter, &name, ¶m)) { + if (rest_param_is_string (param)) + g_hash_table_insert (hash, (gpointer)name, (gpointer)rest_param_get_content (param)); } } @@ -119,9 +133,9 @@ sign_hmac (OAuthProxy *proxy, RestProxyCall *call, GHashTable *oauth_params) g_string_append_c (text, '&'); /* Merge the OAuth parameters with the query parameters */ - all_params = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free); + all_params = g_hash_table_new (g_str_hash, g_str_equal); merge_hashes (all_params, oauth_params); - merge_hashes (all_params, callpriv->params); + merge_params (all_params, callpriv->params); ep = encode_params (all_params); eep = OAUTH_ENCODE_STRING (ep); @@ -167,31 +181,33 @@ make_authorized_header (GHashTable *oauth_params) return g_string_free (auth, FALSE); } +/* + * Remove any OAuth parameters from the @call parameters and add them to + * @oauth_params for building an Authorized header with. + */ static void steal_oauth_params (RestProxyCall *call, GHashTable *oauth_params) { - GHashTable *params; - GHashTableIter iter; - char *key, *value; + RestParams *params; + RestParamsIter iter; + const char *name; + RestParam *param; GList *to_remove = NULL; params = rest_proxy_call_get_params (call); - g_hash_table_iter_init (&iter, params); - while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value)) { - if (g_str_has_prefix (key, "oauth_")) { - g_hash_table_insert (oauth_params, key, value); - to_remove = g_list_prepend (to_remove, key); - /* TODO: key will be leaked */ + rest_params_iter_init (&iter, params); + while (rest_params_iter_next (&iter, &name, ¶m)) { + if (rest_param_is_string (param) && g_str_has_prefix (name, "oauth_")) { + g_hash_table_insert (oauth_params, g_strdup (name), (gpointer)rest_param_get_content (param)); + to_remove = g_list_prepend (to_remove, g_strdup (name)); } } while (to_remove) { - g_hash_table_steal (params, to_remove->data); + rest_params_remove (params, to_remove->data); to_remove = g_list_delete_link (to_remove, to_remove); } - - g_hash_table_unref (params); } static gboolean diff --git a/rest/rest-proxy-call-private.h b/rest/rest-proxy-call-private.h index f244f16..50a2a0b 100644 --- a/rest/rest-proxy-call-private.h +++ b/rest/rest-proxy-call-private.h @@ -25,6 +25,7 @@ #include <rest/rest-proxy.h> #include <rest/rest-proxy-call.h> +#include <rest/rest-params.h> G_BEGIN_DECLS @@ -34,7 +35,7 @@ struct _RestProxyCallPrivate { gchar *method; gchar *function; GHashTable *headers; - GHashTable *params; + RestParams *params; /* The real URL we're about to invoke */ gchar *url; diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c index a550606..b5777b7 100644 --- a/rest/rest-proxy-call.c +++ b/rest/rest-proxy-call.c @@ -22,6 +22,7 @@ #include <rest/rest-proxy.h> #include <rest/rest-proxy-call.h> +#include <rest/rest-params.h> #include <libsoup/soup.h> #include "rest-private.h" @@ -93,7 +94,7 @@ rest_proxy_call_dispose (GObject *object) if (priv->params) { - g_hash_table_unref (priv->params); + rest_params_free (priv->params); priv->params = NULL; } @@ -162,10 +163,8 @@ rest_proxy_call_init (RestProxyCall *self) priv->method = g_strdup ("GET"); - priv->params = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, - g_free); + priv->params = rest_params_new (); + priv->headers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, @@ -356,26 +355,39 @@ rest_proxy_call_remove_header (RestProxyCall *call, /** * rest_proxy_call_add_param: * @call: The #RestProxyCall - * @param: The name of the parameter to set + * @name: The name of the parameter to set * @value: The value of the parameter * - * Add a query parameter called @param with the value @value to the call. If a - * parameter with this name already exists, the new value will replace the old. + * Add a query parameter called @param with the string value @value to the call. + * If a parameter with this name already exists, the new value will replace the + * old. */ void rest_proxy_call_add_param (RestProxyCall *call, - const gchar *param, + const gchar *name, const gchar *value) { RestProxyCallPrivate *priv; + RestParam *param; g_return_if_fail (REST_IS_PROXY_CALL (call)); priv = GET_PRIVATE (call); - g_hash_table_insert (priv->params, - g_strdup (param), - g_strdup (value)); + param = rest_param_new_string (name, REST_MEMORY_COPY, value); + rest_params_add (priv->params, param); +} + +void +rest_proxy_call_add_param_full (RestProxyCall *call, RestParam *param) +{ + RestProxyCallPrivate *priv; + + g_return_if_fail (REST_IS_PROXY_CALL (call)); + g_return_if_fail (param); + + priv = GET_PRIVATE (call); + rest_params_add (priv->params, param); } /** @@ -426,16 +438,16 @@ rest_proxy_call_add_params_from_valist (RestProxyCall *call, /** * rest_proxy_call_lookup_param: * @call: The #RestProxyCall - * @param: The paramter name + * @name: The paramter name * * Get the value of the parameter called @name. * * Returns: The parameter value, or %NULL if it does not exist. This string is * owned by the #RestProxyCall and should not be freed. */ -const gchar * +RestParam * rest_proxy_call_lookup_param (RestProxyCall *call, - const gchar *param) + const gchar *name) { RestProxyCallPrivate *priv; @@ -443,19 +455,19 @@ rest_proxy_call_lookup_param (RestProxyCall *call, priv = GET_PRIVATE (call); - return g_hash_table_lookup (priv->params, param); + return rest_params_get (priv->params, name); } /** * rest_proxy_call_remove_param: * @call: The #RestProxyCall - * @param: The paramter name + * @name: The paramter name * - * Remove the parameter named @param from the call. + * Remove the parameter named @name from the call. */ void rest_proxy_call_remove_param (RestProxyCall *call, - const gchar *param) + const gchar *name) { RestProxyCallPrivate *priv; @@ -463,7 +475,7 @@ rest_proxy_call_remove_param (RestProxyCall *call, priv = GET_PRIVATE (call); - g_hash_table_remove (priv->params, param); + rest_params_remove (priv->params, name); } /** @@ -475,7 +487,7 @@ rest_proxy_call_remove_param (RestProxyCall *call, * * Returns: A #GHashTable. */ -GHashTable * +RestParams * rest_proxy_call_get_params (RestProxyCall *call) { RestProxyCallPrivate *priv; @@ -484,7 +496,7 @@ rest_proxy_call_get_params (RestProxyCall *call) priv = GET_PRIVATE (call); - return g_hash_table_ref (priv->params); + return priv->params; } @@ -681,9 +693,46 @@ prepare_message (RestProxyCall *call, GError **error_out) } } - message = soup_form_request_new_from_hash (priv->method, - priv->url, - priv->params); + if (rest_params_are_strings (priv->params)) { + GHashTable *hash; + + hash = rest_params_as_string_hash_table (priv->params); + + message = soup_form_request_new_from_hash (priv->method, + priv->url, + hash); + + g_hash_table_unref (hash); + } else { + SoupMultipart *mp; + RestParamsIter iter; + const char *name; + RestParam *param; + + mp = soup_multipart_new (SOUP_FORM_MIME_TYPE_MULTIPART); + + rest_params_iter_init (&iter, priv->params); + + while (rest_params_iter_next (&iter, &name, ¶m)) { + if (rest_param_is_string (param)) { + soup_multipart_append_form_string (mp, name, rest_param_get_content (param)); + } else { + SoupBuffer *sb; + + sb = soup_buffer_new_with_owner (rest_param_get_content (param), + rest_param_get_content_length (param), + rest_param_ref (param), + (GDestroyNotify)rest_param_unref); + + soup_multipart_append_form_file (mp, name, + rest_param_get_file_name (param), + rest_param_get_content_type (param), + sb); + } + } + + message = soup_form_request_new_from_multipart (priv->url, mp); + } /* Set the user agent, if one was set in the proxy */ user_agent = rest_proxy_get_user_agent (priv->proxy); diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h index 43bcfa9..5c5ac99 100644 --- a/rest/rest-proxy-call.h +++ b/rest/rest-proxy-call.h @@ -4,7 +4,7 @@ * * Authors: Rob Bradford <rob@linux.intel.com> * Ross Burton <ross@linux.intel.com> - * + * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU Lesser General Public License, * version 2.1, as published by the Free Software Foundation. @@ -24,6 +24,7 @@ #define _REST_PROXY_CALL #include <glib-object.h> +#include <rest/rest-params.h> G_BEGIN_DECLS @@ -112,9 +113,12 @@ void rest_proxy_call_remove_header (RestProxyCall *call, const gchar *header); void rest_proxy_call_add_param (RestProxyCall *call, - const gchar *param, + const gchar *name, const gchar *value); +void rest_proxy_call_add_param_full (RestProxyCall *call, + RestParam *param); + G_GNUC_NULL_TERMINATED void rest_proxy_call_add_params (RestProxyCall *call, ...); @@ -122,13 +126,13 @@ void rest_proxy_call_add_params (RestProxyCall *call, void rest_proxy_call_add_params_from_valist (RestProxyCall *call, va_list params); -const gchar *rest_proxy_call_lookup_param (RestProxyCall *call, - const gchar *param); +RestParam *rest_proxy_call_lookup_param (RestProxyCall *call, + const gchar *name); void rest_proxy_call_remove_param (RestProxyCall *call, - const gchar *param); + const gchar *name); -GHashTable *rest_proxy_call_get_params (RestProxyCall *call); +RestParams *rest_proxy_call_get_params (RestProxyCall *call); gboolean rest_proxy_call_run (RestProxyCall *call, GMainLoop **loop, |