# HG changeset patch
# Node ID d0281b7eb22c
# Parent  0cbfd6c63e62
diff --git a/libpurple/protocols/facebook/api.c b/libpurple/protocols/facebook/api.c
--- a/libpurple/protocols/facebook/api.c
+++ b/libpurple/protocols/facebook/api.c
@@ -625,36 +625,41 @@ fb_api_json_chk(FbApi *api, gconstpointe
 }
 
 static gboolean
-fb_api_http_chk(FbApi *api, SoupMessage *res, JsonNode **root)
+fb_api_http_chk(FbApi *api, SoupSession *session, GAsyncResult *result,
+                SoupMessage *msg, JsonNode **root)
 {
-	const gchar *data;
-	const gchar *msg;
+	GBytes *response_body = NULL;
+	const gchar *reason = NULL;
+	const gchar *data = NULL;
 	GError *err = NULL;
 	gint code;
-	gsize size;
-
-	msg = soup_message_get_reason_phrase(res);
-	code = soup_message_get_status(res);
-	data = res->response_body->data;
-	size = res->response_body->length;
-
-	fb_util_debug(FB_UTIL_DEBUG_INFO, "HTTP Response (%p):", res);
-	if (msg != NULL) {
-		fb_util_debug(FB_UTIL_DEBUG_INFO, "  Response Error: %s (%d)", msg,
+	gsize size = 0;
+
+	reason = soup_message_get_reason_phrase(msg);
+	code = soup_message_get_status(msg);
+
+	fb_util_debug(FB_UTIL_DEBUG_INFO, "HTTP Response (%p):", msg);
+	if (reason != NULL) {
+		fb_util_debug(FB_UTIL_DEBUG_INFO, "  Response Error: %s (%d)", reason,
 		              code);
 	} else {
 		fb_util_debug(FB_UTIL_DEBUG_INFO, "  Response Error: %d", code);
 	}
 
+	if (fb_http_error_chk(msg, &err) && (root == NULL)) {
+		return TRUE;
+	}
+
+	response_body = soup_session_send_and_read_finish(session, result, &err);
+	if(response_body != NULL) {
+		data = g_bytes_get_data(response_body, &size);
+	}
+
 	if (G_LIKELY(size > 0)) {
 		fb_util_debug(FB_UTIL_DEBUG_INFO, "  Response Data: %.*s",
 		              (gint) size, data);
 	}
 
-	if (fb_http_error_chk(res, &err) && (root == NULL)) {
-		return TRUE;
-	}
-
 	/* Rudimentary check to prevent wrongful error parsing */
 	if ((size < 2) || (data[0] != '{') || (data[size - 1] != '}')) {
 		FB_API_ERROR_EMIT(api, err, return FALSE);
@@ -675,7 +680,7 @@ fb_api_http_chk(FbApi *api, SoupMessage 
 static SoupMessage *
 fb_api_http_req(FbApi *api, const gchar *url, const gchar *name,
                 const gchar *method, FbHttpParams *params,
-                SoupSessionCallback callback)
+                GAsyncReadyCallback callback)
 {
 	gchar *data;
 	gchar *key;
@@ -726,7 +731,9 @@ fb_api_http_req(FbApi *api, const gchar 
 		g_free(data);
 	}
 
-	soup_session_queue_message(api->cons, msg, callback, api);
+	g_object_set_data(G_OBJECT(msg), "facebook-api", api);
+	soup_session_send_and_read_async(api->cons, msg, G_PRIORITY_DEFAULT, NULL,
+	                                 callback, msg);
 
 	fb_util_debug(FB_UTIL_DEBUG_INFO, "HTTP Request (%p):", msg);
 	fb_util_debug(FB_UTIL_DEBUG_INFO, "  Request URL: %s", url);
@@ -736,7 +743,7 @@ fb_api_http_req(FbApi *api, const gchar 
 
 static SoupMessage *
 fb_api_http_query(FbApi *api, gint64 query, JsonBuilder *builder,
-                  SoupSessionCallback hcb)
+                  GAsyncReadyCallback hcb)
 {
 	const gchar *name;
 	FbHttpParams *prms;
@@ -784,19 +791,22 @@ fb_api_http_query(FbApi *api, gint64 que
 }
 
 static void
-fb_api_cb_http_bool(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                    gpointer data)
-{
-	FbApi *api = data;
-
-	if (!fb_api_http_chk(api, res, NULL)) {
+fb_api_cb_http_bool(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
+	JsonNode *root;
+
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
-	if (!purple_strequal(res->response_body->data, "true")) {
+	if (!json_node_get_boolean(root)) {
 		fb_api_error_literal(api, FB_API_ERROR,
 		                     _("Failed generic API operation"));
 	}
+
+	json_node_free(root);
 }
 
 static void
@@ -969,16 +979,16 @@ fb_api_connect_queue(FbApi *api)
 }
 
 static void
-fb_api_cb_seqid(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                gpointer data)
-{
+fb_api_cb_seqid(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *str;
-	FbApi *api = data;
 	FbJsonValues *values;
 	GError *err = NULL;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -1996,11 +2006,11 @@ fb_api_error_emit(FbApi *api, GError *er
 }
 
 static void
-fb_api_cb_attach(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                 gpointer data)
-{
+fb_api_cb_attach(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *str;
-	FbApi *api = data;
 	FbApiMessage *msg;
 	FbJsonValues *values;
 	gchar *name;
@@ -2011,7 +2021,7 @@ fb_api_cb_attach(G_GNUC_UNUSED SoupSessi
 
 	static const gchar *imgexts[] = {".jpg", ".png", ".gif"};
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2026,7 +2036,7 @@ fb_api_cb_attach(G_GNUC_UNUSED SoupSessi
 		return;
 	);
 
-	msg = g_object_steal_data(G_OBJECT(res), "fb-api-msg");
+	msg = g_object_steal_data(G_OBJECT(soupmsg), "fb-api-msg");
 	str = fb_json_values_next_str(values, NULL);
 	name = g_ascii_strdown(str, -1);
 
@@ -2066,15 +2076,15 @@ fb_api_attach(FbApi *api, FbId aid, cons
 }
 
 static void
-fb_api_cb_auth(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-               gpointer data)
-{
-	FbApi *api = data;
+fb_api_cb_auth(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	FbJsonValues *values;
 	GError *err = NULL;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2133,18 +2143,18 @@ fb_api_user_icon_checksum(gchar *icon)
 }
 
 static void
-fb_api_cb_contact(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                  gpointer data)
-{
+fb_api_cb_contact(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *str;
-	FbApi *api = data;
 	FbApiUser user;
 	FbJsonValues *values;
 	GError *err = NULL;
 	JsonNode *node;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2285,12 +2295,12 @@ fb_api_cb_contacts_parse_removed(FbApi *
 }
 
 static void
-fb_api_cb_contacts(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                   gpointer data)
-{
+fb_api_cb_contacts(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *cursor;
 	const gchar *delta_cursor;
-	FbApi *api = data;
 	FbJsonValues *values;
 	gboolean complete;
 	gboolean is_delta;
@@ -2301,7 +2311,7 @@ fb_api_cb_contacts(G_GNUC_UNUSED SoupSes
 	JsonNode *croot;
 	JsonNode *node;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2597,12 +2607,12 @@ fb_api_cb_unread_parse_attach(FbApi *api
 }
 
 static void
-fb_api_cb_unread_msgs(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                      gpointer data)
-{
+fb_api_cb_unread_msgs(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *body;
 	const gchar *str;
-	FbApi *api = data;
 	FbApiMessage *dmsg;
 	FbApiMessage msg;
 	FbId id;
@@ -2615,7 +2625,7 @@ fb_api_cb_unread_msgs(G_GNUC_UNUSED Soup
 	JsonNode *root;
 	JsonNode *xode;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2729,18 +2739,18 @@ fb_api_cb_unread_msgs(G_GNUC_UNUSED Soup
 }
 
 static void
-fb_api_cb_unread(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                 gpointer data)
-{
+fb_api_cb_unread(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *id;
-	FbApi *api = data;
 	FbJsonValues *values;
 	GError *err = NULL;
 	gint64 count;
 	JsonBuilder *bldr;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2808,10 +2818,10 @@ fb_api_unread(FbApi *api)
 }
 
 static void
-fb_api_cb_sticker(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                  gpointer data)
-{
-	FbApi *api = data;
+fb_api_cb_sticker(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	FbApiMessage *msg;
 	FbJsonValues *values;
 	GError *err = NULL;
@@ -2819,7 +2829,7 @@ fb_api_cb_sticker(G_GNUC_UNUSED SoupSess
 	JsonNode *node;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2835,7 +2845,7 @@ fb_api_cb_sticker(G_GNUC_UNUSED SoupSess
 		return;
 	);
 
-	msg = g_object_steal_data(G_OBJECT(res), "fb-api-msg");
+	msg = g_object_steal_data(G_OBJECT(soupmsg), "fb-api-msg");
 	msg->flags |= FB_API_MESSAGE_FLAG_IMAGE;
 	msg->text = fb_json_values_next_str_dup(values, NULL);
 	msgs = g_slist_prepend(msgs, msg);
@@ -2937,16 +2947,16 @@ fb_api_thread_parse(FbApi *api, FbApiThr
 }
 
 static void
-fb_api_cb_thread(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                 gpointer data)
-{
-	FbApi *api = data;
+fb_api_cb_thread(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	FbApiThread thrd;
 	GError *err = NULL;
 	JsonNode *node;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -2997,17 +3007,17 @@ fb_api_thread(FbApi *api, FbId tid)
 }
 
 static void
-fb_api_cb_thread_create(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                        gpointer data)
-{
+fb_api_cb_thread_create(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	const gchar *str;
-	FbApi *api = data;
 	FbId tid;
 	FbJsonValues *values;
 	GError *err = NULL;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
@@ -3127,10 +3137,10 @@ fb_api_thread_topic(FbApi *api, FbId tid
 }
 
 static void
-fb_api_cb_threads(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                  gpointer data)
-{
-	FbApi *api = data;
+fb_api_cb_threads(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupSession *session = SOUP_SESSION(source);
+	SoupMessage *soupmsg = data;
+	FbApi *api = g_object_get_data(G_OBJECT(soupmsg), "facebook-api");
 	FbApiThread *dthrd;
 	FbApiThread thrd;
 	GError *err = NULL;
@@ -3140,7 +3150,7 @@ fb_api_cb_threads(G_GNUC_UNUSED SoupSess
 	JsonArray *arr;
 	JsonNode *root;
 
-	if (!fb_api_http_chk(api, res, &root)) {
+	if (!fb_api_http_chk(api, session, result, soupmsg, &root)) {
 		return;
 	}
 
diff --git a/libpurple/protocols/facebook/data.c b/libpurple/protocols/facebook/data.c
--- a/libpurple/protocols/facebook/data.c
+++ b/libpurple/protocols/facebook/data.c
@@ -24,6 +24,7 @@
 
 #include <purple.h>
 #include "libpurple/glibcompat.h"
+#include "libpurple/soupcompat.h"
 
 #include "api.h"
 #include "data.h"
@@ -486,16 +487,21 @@ fb_data_image_get_url(FbDataImage *img)
 }
 
 static void
-fb_data_image_cb(G_GNUC_UNUSED SoupSession *session, SoupMessage *res,
-                 gpointer data)
-{
-	FbDataImage *img = data;
+fb_data_image_cb(GObject *source, GAsyncResult *result, gpointer data) {
+	SoupMessage *msg = data;
+	FbDataImage *img = g_object_get_data(G_OBJECT(msg), "facebook-data-image");
 	GError *err = NULL;
 
-	fb_http_error_chk(res, &err);
+	if(fb_http_error_chk(msg, &err)) {
+		GBytes *bytes = NULL;
 
-	img->image = (guint8 *)res->response_body->data;
-	img->size = res->response_body->length;
+		bytes = soup_session_send_and_read_finish(SOUP_SESSION(source),
+		                                          result, &err);
+		if(bytes != NULL) {
+			img->image = g_bytes_unref_to_data(bytes, &img->size);
+		}
+	}
+
 	img->func(img, err);
 
 	if (G_LIKELY(err == NULL)) {
@@ -541,8 +547,10 @@ fb_data_image_queue(FbData *fata)
 		url = fb_data_image_get_url(img);
 
 		msg = soup_message_new("GET", url);
+		g_object_set_data(G_OBJECT(msg), "facebook-data-image", img);
 		// purple_http_request_set_max_len(req, FB_DATA_ICON_SIZE_MAX);
-		soup_session_queue_message(fata->cons, msg, fb_data_image_cb, img);
+		soup_session_send_and_read_async(fata->cons, msg, G_PRIORITY_DEFAULT,
+		                                 NULL, fb_data_image_cb, msg);
 
 		if (++active >= FB_DATA_ICON_MAX) {
 			break;
