/cpw/tomkiewicz/http: 1ce1faf4e675: Extend URL parsing

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Fri Oct 12 20:17:32 EDT 2012


Changeset: 1ce1faf4e675a40ffef49c2a4c56c0c09a095ed5
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-10-13 02:17 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/1ce1faf4e675

Description:

Extend URL parsing

diffstat:

 libpurple/http.c |  100 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 libpurple/util.c |    2 +-
 2 files changed, 95 insertions(+), 7 deletions(-)

diffs (180 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -29,6 +29,8 @@
 #include "internal.h"
 #include "debug.h"
 
+typedef struct _PurpleHttpURL PurpleHttpURL;
+
 struct _PurpleHttpRequest
 {
 	int ref_count;
@@ -42,6 +44,8 @@ struct _PurpleHttpConnection
 	PurpleHttpCallback callback;
 	gpointer user_data;
 
+	PurpleHttpURL *url;
+	PurpleHttpRequest *request;
 	PurpleHttpResponse *response;
 };
 
@@ -53,12 +57,28 @@ struct _PurpleHttpResponse
 	gsize data_len;
 };
 
-static PurpleHttpConnection * purple_http_connection_new(void);
+struct _PurpleHttpURL
+{
+	gchar *protocol;
+	gchar *host;
+	int port;
+	gchar *path;
+	gchar *user;
+	gchar *password;
+};
+
+static PurpleHttpConnection * purple_http_connection_new(
+	PurpleHttpRequest *request);
 static void purple_http_connection_terminate(PurpleHttpConnection *hc);
 
 static PurpleHttpResponse * purple_http_response_new(void);
 static void purple_http_response_free(PurpleHttpResponse *response);
 
+static PurpleHttpURL * purple_http_url_parse(const char *url);
+static void purple_http_url_free(PurpleHttpURL *parsed_url);
+
+static const gchar * purple_http_url_debug(PurpleHttpURL *parsed_url);
+
 /*** Performing HTTP requests *************************************************/
 
 PurpleHttpConnection * purple_http_get(PurpleConnection *gc, const gchar *url,
@@ -82,7 +102,7 @@ static gboolean purple_http_request_dumm
 	PurpleHttpResponse *response = hc->response;
 
 	response->code = 200;
-	response->data = g_strdup("[dummy reply]");
+	response->data = g_strdup(purple_http_url_debug(hc->url));
 	response->data_len = strlen(response->data);
 
 	purple_http_connection_terminate(hc);
@@ -98,13 +118,23 @@ PurpleHttpConnection * purple_http_reque
 
 	g_return_val_if_fail(request != NULL, NULL);
 
-	hc = purple_http_connection_new();
+	hc = purple_http_connection_new(request);
 	hc->gc = gc;
 	hc->callback = callback;
 	hc->user_data = user_data;
 
-	purple_debug_misc("http", "Performing new request %p for %s.\n",
-		hc, request->url);
+	if (purple_debug_is_unsafe())
+		purple_debug_misc("http", "Performing new request %p for %s.\n",
+			hc, request->url);
+	else
+		purple_debug_misc("http", "Performing new request %p.\n", hc);
+
+	hc->url = purple_http_url_parse(request->url);
+	if (!hc->url) {
+		purple_debug_error("http", "Invalid URL requested.\n");
+		purple_http_connection_terminate(hc);
+		return NULL;
+	}
 
 	purple_timeout_add_seconds(1, purple_http_request_dummy_timeout, hc);
 
@@ -115,10 +145,12 @@ PurpleHttpConnection * purple_http_reque
 
 static void purple_http_connection_free(PurpleHttpConnection *hc);
 
-static PurpleHttpConnection * purple_http_connection_new(void)
+static PurpleHttpConnection * purple_http_connection_new(PurpleHttpRequest *request)
 {
 	PurpleHttpConnection *hc = g_new0(PurpleHttpConnection, 1);
 
+	hc->request = request;
+	purple_http_request_ref(request);
 	hc->response = purple_http_response_new();
 
 	return hc;
@@ -126,6 +158,8 @@ static PurpleHttpConnection * purple_htt
 
 static void purple_http_connection_free(PurpleHttpConnection *hc)
 {
+	purple_http_url_free(hc->url);
+	purple_http_request_unref(hc->request);
 	purple_http_response_free(hc->response);
 	g_free(hc);
 }
@@ -237,3 +271,57 @@ const gchar * purple_http_response_get_d
 
 	return response->data;
 }
+
+/*** URL functions ************************************************************/
+
+static PurpleHttpURL * purple_http_url_parse(const char *url)
+{
+	PurpleHttpURL *parsed_url;
+
+	g_return_val_if_fail(url != NULL, NULL);
+
+	parsed_url = g_new0(PurpleHttpURL, 1);
+
+	if (!purple_url_parse(url,
+		&parsed_url->protocol,
+		&parsed_url->host,
+		&parsed_url->port,
+		&parsed_url->path,
+		&parsed_url->user,
+		&parsed_url->password)) {
+		g_free(parsed_url);
+		return NULL;
+	}
+
+	return parsed_url;
+}
+
+static void purple_http_url_free(PurpleHttpURL *parsed_url)
+{
+	if (parsed_url == NULL)
+		return;
+
+	g_free(parsed_url->protocol);
+	g_free(parsed_url->host);
+	g_free(parsed_url->path);
+	g_free(parsed_url->user);
+	g_free(parsed_url->password);
+	g_free(parsed_url);
+}
+
+static const gchar * purple_http_url_debug(PurpleHttpURL *parsed_url)
+{
+	static gchar buff[512];
+
+	g_snprintf(buff, sizeof(buff), "%s://%s:%d [%s] [%s / %s]",
+		parsed_url->protocol,
+		parsed_url->host,
+		parsed_url->port,
+		parsed_url->path,
+		parsed_url->user,
+		parsed_url->password);
+
+	buff[sizeof(buff) - 1] = '\0';
+
+	return buff;
+}
diff --git a/libpurple/util.c b/libpurple/util.c
--- a/libpurple/util.c
+++ b/libpurple/util.c
@@ -3880,7 +3880,7 @@ purple_url_parse(const char *url, char *
 #define PASSWD_CTRL "A-Za-z0-9.,~_/*!&%%?=+^-"
 
 	g_return_val_if_fail(url != NULL, FALSE);
-	g_return_val_if_fail(strlen(url) >= 256, FALSE);
+	g_return_val_if_fail(strlen(url) < 256, FALSE);
 
 	colon = strchr(url, ':');
 	if (colon) {



More information about the Commits mailing list