/pidgin/main: 59b2fc93f2c4: HTTP: migrate purple_util_fetch_url_...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Fri Jul 5 08:59:57 EDT 2013


Changeset: 59b2fc93f2c4834cf506e0ba6aa727473170d752
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-07-05 14:59 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/59b2fc93f2c4

Description:

HTTP: migrate purple_util_fetch_url_request to new API for upnp

diffstat:

 libpurple/upnp.c |  165 +++++++++++++++++++++----------------------------------
 1 files changed, 63 insertions(+), 102 deletions(-)

diffs (296 lines):

diff --git a/libpurple/upnp.c b/libpurple/upnp.c
--- a/libpurple/upnp.c
+++ b/libpurple/upnp.c
@@ -35,7 +35,6 @@
 #include "signals.h"
 #include "util.h"
 #include "xmlnode.h"
-#include "obsolete.h"
 
 /***************************************************************
 ** General Defines                                             *
@@ -71,13 +70,6 @@
 /******************************************************************
 ** Action Defines                                                 *
 *******************************************************************/
-#define HTTP_HEADER_ACTION \
-	"POST /%s HTTP/1.1\r\n" \
-	"HOST: %s:%d\r\n" \
-	"SOAPACTION: \"urn:schemas-upnp-org:service:%s#%s\"\r\n" \
-	"CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n" \
-	"CONTENT-LENGTH: %" G_GSIZE_FORMAT "\r\n\r\n"
-
 #define SOAP_ACTION \
 	"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
 	"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " \
@@ -146,7 +138,7 @@ struct _UPnPMappingAddRemove
 	gpointer cb_data;
 	gboolean success;
 	guint tima; /* purple_timeout_add handle */
-	PurpleUtilFetchUrlData *gfud;
+	PurpleHttpConnection *hc;
 };
 
 static PurpleUPnPControlInfo control_info = {
@@ -231,27 +223,12 @@ static gchar*
 purple_upnp_parse_description_response(const gchar* httpResponse, gsize len,
 	const gchar* httpURL, const gchar* serviceType)
 {
-	gchar *xmlRoot, *baseURL, *controlURL, *service;
+	gchar *baseURL, *controlURL, *service;
 	xmlnode *xmlRootNode, *serviceTypeNode, *controlURLNode, *baseURLNode;
 	char *tmp;
 
-	/* make sure we have a valid http response */
-	if(g_strstr_len(httpResponse, len, HTTP_OK) == NULL) {
-		purple_debug_error("upnp",
-			"parse_description_response(): Failed In HTTP_OK\n");
-		return NULL;
-	}
-
-	/* find the root of the xml document */
-	if((xmlRoot = g_strstr_len(httpResponse, len, "<root")) == NULL) {
-		purple_debug_error("upnp",
-			"parse_description_response(): Failed finding root\n");
-		return NULL;
-	}
-
 	/* create the xml root node */
-	if((xmlRootNode = xmlnode_from_str(xmlRoot,
-			len - (xmlRoot - httpResponse))) == NULL) {
+	if ((xmlRootNode = xmlnode_from_str(httpResponse, len)) == NULL) {
 		purple_debug_error("upnp",
 			"parse_description_response(): Could not parse xml root node\n");
 		return NULL;
@@ -390,15 +367,20 @@ purple_upnp_parse_description_response(c
 }
 
 static void
-upnp_parse_description_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-		const gchar *httpResponse, gsize len, const gchar *error_message)
+upnp_parse_description_cb(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer _dd)
 {
-	UPnPDiscoveryData *dd = user_data;
+	UPnPDiscoveryData *dd = _dd;
 	gchar *control_url = NULL;
 
-	if (len > 0)
+	if (response && purple_http_response_is_successfull(response)) {
+		const gchar *got_data;
+		size_t got_len;
+
+		got_data = purple_http_response_get_data(response, &got_len);
 		control_url = purple_upnp_parse_description_response(
-			httpResponse, len, dd->full_url, dd->service_type);
+			got_data, got_len, dd->full_url, dd->service_type);
+	}
 
 	g_free(dd->full_url);
 
@@ -433,41 +415,31 @@ upnp_parse_description_cb(PurpleUtilFetc
 static void
 purple_upnp_parse_description(const gchar* descriptionURL, UPnPDiscoveryData *dd)
 {
+	PurpleHttpRequest *req;
 	PurpleHttpURL *url;
-	gchar* httpRequest;
-
-	/* parse the 4 above variables out of the descriptionURL
-	   example description URL: http://192.168.1.1:5678/rootDesc.xml */
-
-	url = purple_http_url_parse(descriptionURL);
-	if (!url)
-		return;
-
-	/* for example...
-	   GET /rootDesc.xml HTTP/1.1\r\nHost: 192.168.1.1:5678\r\n\r\n */
-	httpRequest = g_strdup_printf(
-		"GET %s HTTP/1.1\r\n"
-		"Connection: close\r\n"
-		"Host: %s:%d\r\n\r\n",
-		purple_http_url_get_path(url),
-		purple_http_url_get_host(url),
-		purple_http_url_get_port(url));
-
-	dd->full_url = g_strdup_printf("http://%s:%d",
-		purple_http_url_get_host(url),
-		purple_http_url_get_port(url));
-	purple_http_url_free(url);
 
 	/* Remove the timeout because everything it is waiting for has
 	 * successfully completed */
 	purple_timeout_remove(dd->tima);
 	dd->tima = 0;
 
-	purple_util_fetch_url_request(NULL, descriptionURL, TRUE, NULL, TRUE, httpRequest,
-			TRUE, MAX_UPNP_DOWNLOAD, upnp_parse_description_cb, dd);
+	/* Extract base url out of the descriptionURL.
+	 * Example description URL: http://192.168.1.1:5678/rootDesc.xml
+	 */
+	url = purple_http_url_parse(descriptionURL);
+	if (!url) {
+		upnp_parse_description_cb(NULL, NULL, dd);
+		return;
+	}
+	dd->full_url = g_strdup_printf("http://%s:%d",
+		purple_http_url_get_host(url),
+		purple_http_url_get_port(url));
+	purple_http_url_free(url);
 
-	g_free(httpRequest);
-
+	req = purple_http_request_new(descriptionURL);
+	purple_http_request_set_max_len(req, MAX_UPNP_DOWNLOAD);
+	purple_http_request(NULL, req, upnp_parse_description_cb, dd);
+	purple_http_request_unref(req);
 }
 
 static void
@@ -691,46 +663,34 @@ purple_upnp_discover(PurpleUPnPCallback 
 	purple_upnp_discover_send_broadcast(dd);
 }
 
-static PurpleUtilFetchUrlData*
+static PurpleHttpConnection*
 purple_upnp_generate_action_message_and_send(const gchar* actionName,
-		const gchar* actionParams, PurpleUtilFetchUrlCallback cb,
+		const gchar* actionParams, PurpleHttpCallback cb,
 		gpointer cb_data)
 {
-	PurpleUtilFetchUrlData* gfud;
-	PurpleHttpURL *url;
+	PurpleHttpConnection *hc;
+	PurpleHttpRequest *req;
 	gchar* soapMessage;
-	gchar* totalSendMessage;
-
-	url = purple_http_url_parse(control_info.control_url);
-	if (!url) {
-		purple_debug_error("upnp", "generate_action_message_and_send():"
-			" Failed In Parse URL\n");
-		/* XXX: This should probably be async */
-		if(cb)
-			cb(NULL, cb_data, NULL, 0, NULL);
-		return NULL;
-	}
 
 	/* set the soap message */
 	soapMessage = g_strdup_printf(SOAP_ACTION, actionName,
 		control_info.service_type, actionParams, actionName);
 
-	/* set the HTTP Header, and append the body to it */
-	totalSendMessage = g_strdup_printf(HTTP_HEADER_ACTION "%s",
-		purple_http_url_get_path(url),
-		purple_http_url_get_host(url),
-		purple_http_url_get_port(url),
-		control_info.service_type, actionName,
-		strlen(soapMessage), soapMessage);
+	req = purple_http_request_new(control_info.control_url);
+	purple_http_request_set_max_len(req, MAX_UPNP_DOWNLOAD);
+	purple_http_request_set_method(req, "POST");
+	purple_http_request_header_set_printf(req, "SOAPAction",
+		"\"urn:schemas-upnp-org:service:%s#%s\"",
+		control_info.service_type, actionName);
+	purple_http_request_header_set(req, "Content-Type",
+		"text/xml; charset=utf-8");
+	purple_http_request_set_contents(req, soapMessage, -1);
+	hc = purple_http_request(NULL, req, cb, cb_data);
+	purple_http_request_unref(req);
+
 	g_free(soapMessage);
 
-	gfud = purple_util_fetch_url_request(NULL, control_info.control_url, FALSE, NULL, TRUE,
-				totalSendMessage, TRUE, MAX_UPNP_DOWNLOAD, cb, cb_data);
-
-	g_free(totalSendMessage);
-	purple_http_url_free(url);
-
-	return gfud;
+	return hc;
 }
 
 const gchar *
@@ -750,27 +710,30 @@ purple_upnp_get_public_ip()
 }
 
 static void
-looked_up_public_ip_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-		const gchar *httpResponse, gsize len, const gchar *error_message)
+looked_up_public_ip_cb(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer user_data)
 {
 	gchar* temp, *temp2;
+	const gchar *got_data;
+	size_t got_len;
 
-	if ((error_message != NULL) || (httpResponse == NULL))
+	if (!purple_http_response_is_successfull(response))
 		return;
 
 	/* extract the ip, or see if there is an error */
-	if((temp = g_strstr_len(httpResponse, len,
+	got_data = purple_http_response_get_data(response, &got_len);
+	if((temp = g_strstr_len(got_data, got_len,
 			"<NewExternalIPAddress")) == NULL) {
 		purple_debug_error("upnp",
 			"looked_up_public_ip_cb(): Failed Finding <NewExternalIPAddress\n");
 		return;
 	}
-	if(!(temp = g_strstr_len(temp, len - (temp - httpResponse), ">"))) {
+	if(!(temp = g_strstr_len(temp, got_len - (temp - got_data), ">"))) {
 		purple_debug_error("upnp",
 			"looked_up_public_ip_cb(): Failed In Finding >\n");
 		return;
 	}
-	if(!(temp2 = g_strstr_len(temp, len - (temp - httpResponse), "<"))) {
+	if(!(temp2 = g_strstr_len(temp, got_len - (temp - got_data), "<"))) {
 		purple_debug_error("upnp",
 			"looked_up_public_ip_cb(): Failed In Finding <\n");
 		return;
@@ -848,20 +811,19 @@ lookup_internal_ip()
 }
 
 static void
-done_port_mapping_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-		const gchar *httpResponse, gsize len, const gchar *error_message)
+done_port_mapping_cb(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer user_data)
 {
 	UPnPMappingAddRemove *ar = user_data;
 
 	gboolean success = TRUE;
 
 	/* determine if port mapping was a success */
-	if ((error_message != NULL) || (httpResponse == NULL) ||
-		(g_strstr_len(httpResponse, len, HTTP_OK) == NULL))
+	if (!purple_http_response_is_successfull(response))
 	{
 		purple_debug_error("upnp",
 			"purple_upnp_set_port_mapping(): Failed HTTP_OK\n%s\n",
-			httpResponse ? httpResponse : "(null)");
+			purple_http_response_get_error(response));
 		success =  FALSE;
 	} else
 		purple_debug_info("upnp", "Successfully completed port mapping operation\n");
@@ -901,8 +863,8 @@ do_port_mapping_cb(gboolean has_control_
 				ar->portmap, ar->protocol);
 		}
 
-		ar->gfud = purple_upnp_generate_action_message_and_send(action_name,
-						action_params, done_port_mapping_cb, ar);
+		ar->hc = purple_upnp_generate_action_message_and_send(
+			action_name, action_params, done_port_mapping_cb, ar);
 
 		g_free(action_params);
 		return;
@@ -946,8 +908,7 @@ void purple_upnp_cancel_port_mapping(UPn
 	if (ar->tima > 0)
 		purple_timeout_remove(ar->tima);
 
-	if (ar->gfud)
-		purple_util_fetch_url_cancel(ar->gfud);
+	purple_http_conn_cancel(ar->hc);
 
 	g_free(ar);
 }



More information about the Commits mailing list