pidgin/pidgin

Cut down on FbHttpParams code using libsoup.

2019-10-12, Elliott Sales de Andrade
d59a2f6c170f
Parents 07bc15fd4417
Children 0b2985712d3b
Cut down on FbHttpParams code using libsoup.
--- a/libpurple/protocols/facebook/api.c Sat Oct 12 04:38:00 2019 -0400
+++ b/libpurple/protocols/facebook/api.c Sat Oct 12 22:32:50 2019 -0400
@@ -704,8 +704,6 @@
fb_http_params_set_str(params, "locale", val);
g_free(val);
- msg = soup_message_new("POST", url);
-
/* Ensure an old signature is not computed */
g_hash_table_remove(params, "sig");
@@ -727,6 +725,9 @@
g_list_free(keys);
g_free(data);
+ msg = soup_form_request_new_from_hash("POST", url, params);
+ fb_http_params_free(params);
+
if (priv->token != NULL) {
data = g_strdup_printf("OAuth %s", priv->token);
soup_message_headers_replace(msg->request_headers, "Authorization",
@@ -734,10 +735,6 @@
g_free(data);
}
- data = fb_http_params_close(params, NULL);
- soup_message_set_request(msg,
- "application/x-www-form-urlencoded; charset=utf-8",
- SOUP_MEMORY_TAKE, data, -1);
soup_session_queue_message(priv->cons, msg, callback, api);
fb_util_debug(FB_UTIL_DEBUG_INFO, "HTTP Request (%p):", msg);
--- a/libpurple/protocols/facebook/http.c Sat Oct 12 04:38:00 2019 -0400
+++ b/libpurple/protocols/facebook/http.c Sat Oct 12 22:32:50 2019 -0400
@@ -58,56 +58,29 @@
FbHttpParams *
fb_http_params_new_parse(const gchar *data, gboolean isurl)
{
- const gchar *tail;
- gchar *key;
- gchar **ps;
- gchar *val;
- guint i;
+ SoupURI *uri = NULL;
FbHttpParams *params;
- params = fb_http_params_new();
-
if (data == NULL) {
- return params;
+ return fb_http_params_new();
}
if (isurl) {
- data = strchr(data, '?');
+ uri = soup_uri_new(data);
- if (data == NULL) {
- return params;
+ if (uri == NULL) {
+ return fb_http_params_new();
}
- tail = strchr(++data, '#');
-
- if (tail != NULL) {
- data = g_strndup(data, tail - data);
- } else {
- data = g_strdup(data);
- }
+ data = uri->query;
}
- ps = g_strsplit(data, "&", 0);
-
- for (i = 0; ps[i] != NULL; i++) {
- key = ps[i];
- val = strchr(ps[i], '=');
+ params = soup_form_decode(data);
- if (val == NULL) {
- continue;
- }
-
- *(val++) = 0;
- key = g_uri_unescape_string(key, NULL);
- val = g_uri_unescape_string(val, NULL);
- g_hash_table_replace(params, key, val);
+ if (isurl) {
+ soup_uri_free(uri);
}
- if (isurl) {
- g_free((gchar *) data);
- }
-
- g_strfreev(ps);
return params;
}
@@ -117,41 +90,6 @@
g_hash_table_destroy(params);
}
-gchar *
-fb_http_params_close(FbHttpParams *params, const gchar *url)
-{
- GHashTableIter iter;
- gpointer key;
- gpointer val;
- GString *ret;
-
- g_hash_table_iter_init(&iter, params);
- ret = g_string_new(NULL);
-
- while (g_hash_table_iter_next(&iter, &key, &val)) {
- if (val == NULL) {
- g_hash_table_iter_remove(&iter);
- continue;
- }
-
- if (ret->len > 0) {
- g_string_append_c(ret, '&');
- }
-
- g_string_append_uri_escaped(ret, key, NULL, TRUE);
- g_string_append_c(ret, '=');
- g_string_append_uri_escaped(ret, val, NULL, TRUE);
- }
-
- if (url != NULL) {
- g_string_prepend_c(ret, '?');
- g_string_prepend(ret, url);
- }
-
- fb_http_params_free(params);
- return g_string_free(ret, FALSE);
-}
-
static const gchar *
fb_http_params_get(FbHttpParams *params, const gchar *name, GError **error)
{
--- a/libpurple/protocols/facebook/http.h Sat Oct 12 04:38:00 2019 -0400
+++ b/libpurple/protocols/facebook/http.h Sat Oct 12 22:32:50 2019 -0400
@@ -124,21 +124,6 @@
fb_http_params_free(FbHttpParams *params);
/**
- * fb_http_params_close:
- * @params: The #FbHttpParams.
- * @url: The URL or #NULL.
- *
- * Closes the #FbHttpParams by returning a string representing the HTTP
- * parameters. If @url is non-#NULL, then the parameters are appended
- * to the value of @url. This frees the #FbHttpParams. The returned
- * string should be freed with #g_free() when no longer needed.
- *
- * Returns: The string representation of the HTTP parameters.
- */
-gchar *
-fb_http_params_close(FbHttpParams *params, const gchar *url);
-
-/**
* fb_http_params_get_bool:
* @params: The #FbHttpParams.
* @name: The parameter name.