/pidgin/main: a5517f235e0f: HTTP: migrate purple_util_fetch_url_...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Wed Jul 3 21:03:05 EDT 2013


Changeset: a5517f235e0f5dd15033adc3d2bda36a342f8410
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-07-04 03:02 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/a5517f235e0f

Description:

HTTP: migrate purple_util_fetch_url_request to new API for GG prpl

diffstat:

 libpurple/http.c                               |   51 ++++++++-
 libpurple/http.h                               |   24 +++-
 libpurple/protocols/gg/avatar.c                |  115 +++++++++++----------
 libpurple/protocols/gg/oauth/oauth-parameter.c |    4 +-
 libpurple/protocols/gg/oauth/oauth-purple.c    |  129 +++++++++++------------
 libpurple/protocols/gg/pubdir-prpl.c           |  132 +++++++++++-------------
 6 files changed, 251 insertions(+), 204 deletions(-)

diffs (truncated from 864 to 300 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -155,6 +155,9 @@ struct _PurpleHttpCookieJar
 
 static time_t purple_http_rfc1123_to_time(const gchar *str);
 
+static gboolean purple_http_request_is_method(PurpleHttpRequest *request,
+	const gchar *method);
+
 static PurpleHttpConnection * purple_http_connection_new(
 	PurpleHttpRequest *request, PurpleConnection *gc);
 static void purple_http_connection_terminate(PurpleHttpConnection *hc);
@@ -513,10 +516,13 @@ static void _purple_http_gen_headers(Pur
 	if (!purple_http_headers_get(hdrs, "accept"))
 		g_string_append(h, "Accept: */*\r\n");
 
-	if (req->contents_length > 0 && !purple_http_headers_get(hdrs,
-		"content-length"))
+	if (!purple_http_headers_get(hdrs, "content-length") && (
+		req->contents_length > 0 ||
+		purple_http_request_is_method(req, "post")))
+	{
 		g_string_append_printf(h, "Content-Length: %u\r\n",
 			req->contents_length);
+	}
 
 	if (proxy_http)
 		g_string_append(h, "Proxy-Connection: close\r\n");
@@ -1260,6 +1266,12 @@ PurpleHttpConnection * purple_http_reque
 
 	g_return_val_if_fail(request != NULL, NULL);
 
+	if (request->url == NULL) {
+		purple_debug_error("http", "Cannot perform new request - "
+			"URL is not set\n");
+		return NULL;
+	}
+
 	hc = purple_http_connection_new(request, gc);
 	hc->callback = callback;
 	hc->user_data = user_data;
@@ -1703,8 +1715,6 @@ PurpleHttpRequest * purple_http_request_
 {
 	PurpleHttpRequest *request;
 
-	g_return_val_if_fail(url != NULL, NULL);
-
 	request = g_new0(PurpleHttpRequest, 1);
 
 	request->ref_count = 1;
@@ -1759,6 +1769,23 @@ void purple_http_request_set_url(PurpleH
 	request->url = g_strdup(url);
 }
 
+void purple_http_request_set_url_printf(PurpleHttpRequest *request,
+	const gchar *format, ...)
+{
+	va_list args;
+	gchar *value;
+
+	g_return_if_fail(request != NULL);
+	g_return_if_fail(format != NULL);
+
+	va_start(args, format);
+	value = g_strdup_vprintf(format, args);
+	va_end(args);
+
+	purple_http_request_set_url(request, value);
+	g_free(value);
+}
+
 const gchar * purple_http_request_get_url(PurpleHttpRequest *request)
 {
 	g_return_val_if_fail(request != NULL, NULL);
@@ -1781,6 +1808,20 @@ const gchar * purple_http_request_get_me
 	return request->method;
 }
 
+static gboolean purple_http_request_is_method(PurpleHttpRequest *request,
+	const gchar *method)
+{
+	const gchar *rmethod;
+
+	g_return_val_if_fail(request != NULL, FALSE);
+	g_return_val_if_fail(method != NULL, FALSE);
+
+	rmethod = purple_http_request_get_method(request);
+	if (rmethod == NULL)
+		return (g_ascii_strcasecmp(method, "get") == 0);
+	return (g_ascii_strcasecmp(method, rmethod) == 0);
+}
+
 void purple_http_request_set_contents(PurpleHttpRequest *request,
 	const gchar *contents, int length)
 {
@@ -2014,7 +2055,7 @@ const gchar * purple_http_response_get_d
 {
 	const gchar *ret = "";
 
-	g_return_val_if_fail(response != NULL, NULL);
+	g_return_val_if_fail(response != NULL, "");
 
 	if (response->contents != NULL) {
 		ret = response->contents->str;
diff --git a/libpurple/http.h b/libpurple/http.h
--- a/libpurple/http.h
+++ b/libpurple/http.h
@@ -403,7 +403,8 @@ gboolean purple_http_cookie_jar_is_empty
 /**
  * Creates the new instance of HTTP request configuration.
  *
- * @param url The URL to request for.
+ * @param url The URL to request for, or NULL to leave empty (to be set with
+ *            purple_http_request_set_url).
  * @return The new instance of HTTP request struct.
  */
 PurpleHttpRequest * purple_http_request_new(const gchar *url);
@@ -434,6 +435,15 @@ PurpleHttpRequest * purple_http_request_
 void purple_http_request_set_url(PurpleHttpRequest *request, const gchar *url);
 
 /**
+ * Constructs and sets an URL for HTTP request.
+ *
+ * @param request The request.
+ * @param format  The format string.
+ */
+void purple_http_request_set_url_printf(PurpleHttpRequest *request,
+	const gchar *format, ...) G_GNUC_PRINTF(2, 3);
+
+/**
  * Gets URL set for the HTTP request.
  *
  * @param request The request.
@@ -580,14 +590,22 @@ int purple_http_request_get_max_len(Purp
 /**
  * Sets (replaces, if exists) specified HTTP request header with provided value.
  *
- * @param key   A header to be set.
- * @param value A value to set, or NULL to remove specified header from request.
+ * @param request The request.
+ * @param key     A header to be set.
+ * @param value   A value to set, or NULL to remove specified header.
  *
  * @see purple_http_request_header_add
  */
 void purple_http_request_header_set(PurpleHttpRequest *request,
 	const gchar *key, const gchar *value);
 
+/**
+ * Constructs and sets (replaces, if exists) specified HTTP request header.
+ *
+ * @param request The request.
+ * @param key     A header to be set.
+ * @param format  The format string.
+ */
 void purple_http_request_header_set_printf(PurpleHttpRequest *request,
 	const gchar *key, const gchar *format, ...) G_GNUC_PRINTF(3, 4);
 
diff --git a/libpurple/protocols/gg/avatar.c b/libpurple/protocols/gg/avatar.c
--- a/libpurple/protocols/gg/avatar.c
+++ b/libpurple/protocols/gg/avatar.c
@@ -31,7 +31,7 @@
 
 #include <debug.h>
 #include <glibcompat.h>
-#include <obsolete.h>
+#include <http.h>
 
 #include "gg.h"
 #include "utils.h"
@@ -55,13 +55,12 @@ typedef struct
 	time_t timestamp;
 	
 	PurpleConnection *gc;
-	PurpleUtilFetchUrlData *request;
+	PurpleHttpConnection *request;
 } ggp_avatar_buddy_update_req;
 
 static gboolean ggp_avatar_buddy_update_next(PurpleConnection *gc);
-static void ggp_avatar_buddy_update_received(PurpleUtilFetchUrlData *url_data,
-	gpointer _pending_update, const gchar *url_text, gsize len,
-	const gchar *error_message);
+static void ggp_avatar_buddy_update_received(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer user_data);
 
 #define GGP_AVATAR_BUDDY_URL "http://avatars.gg.pl/%u/s,big"
 
@@ -74,9 +73,8 @@ typedef struct
 
 static void ggp_avatar_own_got_token(PurpleConnection *gc, const gchar *token,
 	gpointer img);
-static void ggp_avatar_own_sent(PurpleUtilFetchUrlData *url_data,
-	gpointer user_data, const gchar *url_text, gsize len,
-	const gchar *error_message);
+static void ggp_avatar_own_sent(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer user_data);
 
 #define GGP_AVATAR_RESPONSE_MAX 10240
 
@@ -106,7 +104,7 @@ void ggp_avatar_cleanup(PurpleConnection
 		ggp_avatar_buddy_update_req *current_update =
 			avdata->current_update;
 		
-		purple_util_fetch_url_cancel(current_update->request);
+		purple_http_conn_cancel(current_update->request);
 		g_free(current_update);
 	}
 	avdata->current_update = NULL;
@@ -177,6 +175,7 @@ void ggp_avatar_buddy_remove(PurpleConne
    FALSE if we can request another one immediately */
 static gboolean ggp_avatar_buddy_update_next(PurpleConnection *gc)
 {
+	PurpleHttpRequest *req;
 	ggp_avatar_session_data *avdata = ggp_avatar_get_avdata(gc);
 	GList *pending_update_it;
 	ggp_avatar_buddy_update_req *pending_update;
@@ -184,12 +183,11 @@ static gboolean ggp_avatar_buddy_update_
 	PurpleAccount *account = purple_connection_get_account(gc);
 	time_t old_timestamp;
 	const char *old_timestamp_str;
-	gchar *avatar_url;
-	
+
 	pending_update_it = g_list_first(avdata->pending_updates);
 	if (pending_update_it == NULL)
 		return TRUE;
-	
+
 	pending_update = pending_update_it->data;
 	avdata->pending_updates = g_list_remove(avdata->pending_updates,
 		pending_update);
@@ -242,19 +240,21 @@ static gboolean ggp_avatar_buddy_update_
 
 	pending_update->gc = gc;
 	avdata->current_update = pending_update;
-	avatar_url = g_strdup_printf(GGP_AVATAR_BUDDY_URL, pending_update->uin);
-	pending_update->request = purple_util_fetch_url_request(account,
-		avatar_url, FALSE, GGP_AVATAR_USERAGENT, TRUE, NULL, FALSE,
-		GGP_AVATAR_SIZE_MAX, ggp_avatar_buddy_update_received,
-		pending_update);
-	g_free(avatar_url);
-	
+
+	req = purple_http_request_new(NULL);
+	purple_http_request_set_url_printf(req, GGP_AVATAR_BUDDY_URL,
+		pending_update->uin);
+	purple_http_request_header_set(req, "User-Agent", GGP_AVATAR_USERAGENT);
+	purple_http_request_set_max_len(req, GGP_AVATAR_SIZE_MAX);
+	pending_update->request = purple_http_request(gc, req,
+		ggp_avatar_buddy_update_received, pending_update);
+	purple_http_request_unref(req);
+
 	return TRUE;
 }
 
-static void ggp_avatar_buddy_update_received(PurpleUtilFetchUrlData *url_data,
-	gpointer _pending_update, const gchar *url_text, gsize len,
-	const gchar *error_message)
+static void ggp_avatar_buddy_update_received(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer _pending_update)
 {
 	ggp_avatar_buddy_update_req *pending_update = _pending_update;
 	PurpleBuddy *buddy;
@@ -262,6 +262,8 @@ static void ggp_avatar_buddy_update_rece
 	PurpleConnection *gc = pending_update->gc;
 	ggp_avatar_session_data *avdata;
 	gchar timestamp_str[20];
+	const gchar *got_data;
+	size_t got_len;
 
 	if (!PURPLE_CONNECTION_IS_VALID(gc))
 	{
@@ -273,11 +275,12 @@ static void ggp_avatar_buddy_update_rece
 	g_assert(pending_update == avdata->current_update);
 	avdata->current_update = NULL;
 
-	if (len == 0)
+	if (!purple_http_response_is_successfull(response))
 	{
 		purple_debug_error("gg", "ggp_avatar_buddy_update_received: bad"
 			" response while getting avatar for %u: %s\n",
-			pending_update->uin, error_message);
+			pending_update->uin,
+			purple_http_response_get_error(response));
 		g_free(pending_update);
 		return;
 	}
@@ -295,8 +298,9 @@ static void ggp_avatar_buddy_update_rece
 
 	g_snprintf(timestamp_str, sizeof(timestamp_str), "%lu",
 		pending_update->timestamp);
+	got_data = purple_http_response_get_data(response, &got_len);
 	purple_buddy_icons_set_for_user(account, purple_buddy_get_name(buddy),
-		g_memdup(url_text, len), len, timestamp_str);
+		g_memdup(got_data, got_len), got_len, timestamp_str);
 
 	purple_debug_info("gg", "ggp_avatar_buddy_update_received: "
 		"got avatar for buddy %u [ts=%lu]\n", pending_update->uin,
@@ -337,11 +341,12 @@ void ggp_avatar_own_set(PurpleConnection



More information about the Commits mailing list