/cpw/tomkiewicz/http: b3afec292014: New features: http version s...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Tue Oct 16 07:27:05 EDT 2012


Changeset: b3afec2920143eff4c1d9bfcb2d339cf9b5e68cd
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-10-16 13:26 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/b3afec292014

Description:

New features: http version switching and limits for redirects

diffstat:

 libpurple/http.c            |  53 ++++++++++++++++++++++++++++++++++++++++----
 libpurple/http.h            |  33 ++++++++++++++++++++++-----
 libpurple/protocols/gg/gg.c |  40 ++++++++++++++++++++++++++-------
 3 files changed, 105 insertions(+), 21 deletions(-)

diffs (239 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -32,6 +32,8 @@
 #define PURPLE_HTTP_URL_CREDENTIALS_CHARS "a-z0-9.,~_/*!&%?=+\\^-"
 #define PURPLE_HTTP_MAX_RECV_BUFFER_LEN 10240
 
+#define PURPLE_HTTP_REQUEST_DEFAULT_MAX_REDIRECTS 20
+
 typedef struct _PurpleHttpURL PurpleHttpURL;
 
 typedef struct _PurpleHttpSocket PurpleHttpSocket;
@@ -52,6 +54,9 @@ struct _PurpleHttpRequest
 	int ref_count;
 
 	gchar *url;
+
+	int max_redirects;
+	gboolean http11;
 };
 
 struct _PurpleHttpConnection
@@ -70,6 +75,8 @@ struct _PurpleHttpConnection
 	gboolean main_header_got, headers_got;
 	GString *response_buffer;
 
+	int redirects_count;
+
 	int length_expected, length_got;
 
 	gboolean is_chunked, in_chunk, chunks_done;
@@ -307,7 +314,8 @@ static void _purple_http_gen_headers(Pur
 	hc->request_header = h = g_string_new("");
 	hc->request_header_written = 0;
 
-	g_string_append_printf(h, "GET %s HTTP/%s\r\n", url->path, "1.1");
+	g_string_append_printf(h, "GET %s HTTP/%s\r\n", url->path,
+		hc->request->http11 ? "1.1" : "1.0");
 	g_string_append_printf(h, "Host: %s\r\n", url->host);
 	g_string_append_printf(h, "Connection: close\r\n");
 
@@ -599,9 +607,12 @@ static void _purple_http_recv(gpointer _
 
 		redirect = purple_http_headers_get(hc->response->headers,
 			"location");
-		if (redirect) {
+		if (redirect &&
+			hc->request->max_redirects > hc->redirects_count) {
 			PurpleHttpURL *url = purple_http_url_parse(redirect);
 
+			hc->redirects_count++;
+
 			if (!url) {
 				if (purple_debug_is_unsafe())
 					purple_debug_warning("http",
@@ -947,6 +958,9 @@ PurpleHttpRequest * purple_http_request_
 	request->ref_count = 1;
 	request->url = g_strdup(url);
 
+	request->max_redirects = PURPLE_HTTP_REQUEST_DEFAULT_MAX_REDIRECTS;
+	request->http11 = TRUE;
+
 	return request;
 }
 
@@ -978,6 +992,38 @@ PurpleHttpRequest * purple_http_request_
 	return NULL;
 }
 
+void purple_http_request_set_max_redirects(PurpleHttpRequest *request,
+	int max_redirects)
+{
+	g_return_if_fail(request != NULL);
+
+	if (max_redirects < -1)
+		max_redirects = -1;
+
+	request->max_redirects = max_redirects;
+}
+
+int purple_http_request_get_max_redirects(PurpleHttpRequest *request)
+{
+	g_return_val_if_fail(request != NULL, -1);
+
+	return request->max_redirects;
+}
+
+void purple_http_request_set_http11(PurpleHttpRequest *request, gboolean http11)
+{
+	g_return_if_fail(request != NULL);
+
+	request->http11 = http11;
+}
+
+gboolean purple_http_request_is_http11(PurpleHttpRequest *request)
+{
+	g_return_val_if_fail(request != NULL, FALSE);
+
+	return request->http11;
+}
+
 /*** HTTP response API ********************************************************/
 
 static PurpleHttpResponse * purple_http_response_new(void)
@@ -1010,9 +1056,6 @@ gboolean purple_http_response_is_success
 	if (code == 200)
 		return TRUE;
 
-	if (code / 100 == 3) /* 3xx */
-		return TRUE;
-
 	return FALSE;
 }
 
diff --git a/libpurple/http.h b/libpurple/http.h
--- a/libpurple/http.h
+++ b/libpurple/http.h
@@ -253,10 +253,21 @@ void purple_http_request_set_response_wr
 	PurpleHttpContentWriter writer, gpointer user_data);
 
 /**
- * -1 for unlimited
+ * Sets maximum amount of redirects.
+ *
+ * @param request       The request.
+ * @param max_redirects Maximum amount of redirects, or -1 for unlimited.
  */
 void purple_http_request_set_max_redirects(PurpleHttpRequest *request,
-	int max_redirects); // +get
+	int max_redirects);
+
+/**
+ * Gets maximum amount of redirects.
+ *
+ * @param request The request.
+ * @return        Current maximum amount of redirects (-1 for unlimited).
+ */
+int purple_http_request_get_max_redirects(PurpleHttpRequest *request);
 
 /**
  * NULL for disabling cookie support
@@ -265,13 +276,21 @@ void purple_http_request_set_cookie_jar(
 	PurpleHTTPCookieJar *cookie_jar); // +get
 
 /**
- * NULL for default
+ * Sets HTTP version to use.
+ *
+ * @param request The request.
+ * @param http11  TRUE for HTTP/1.1, FALSE for HTTP/1.0.
  */
-void purple_http_request_set_user_agent(PurpleHttpRequest *request,
-	const gchar *user_agent); // +get
+void purple_http_request_set_http11(PurpleHttpRequest *request,
+	gboolean http11);
 
-void purple_http_request_set_http11(PurpleHttpRequest *request,
-	gboolean http11); // +is
+/**
+ * Gets used HTTP version.
+ *
+ * @param request The request.
+ * @return        TRUE, if we use HTTP/1.1, FALSE for HTTP/1.0.
+ */
+gboolean purple_http_request_is_http11(PurpleHttpRequest *request);
 
 /**
  * -1 for unlimited
diff --git a/libpurple/protocols/gg/gg.c b/libpurple/protocols/gg/gg.c
--- a/libpurple/protocols/gg/gg.c
+++ b/libpurple/protocols/gg/gg.c
@@ -64,13 +64,13 @@ static void ggp_test_http_cb(PurpleHttpC
 	const gchar *data;
 	gchar *data_front, *data_tail;
 
-	purple_debug_info("http-test", "Testing http done %s.\n",
-		purple_http_response_is_successfull(response) ?
-		"successfully" : "without success");
-	purple_debug_info("http-test", "Returned http code: %d.\n",
-		purple_http_response_get_code(response));
-	purple_debug_info("http-test", "Returned error: %s.\n",
-		purple_http_response_get_error(response));
+//	purple_debug_info("http-test", "Testing http done %s.\n",
+//		purple_http_response_is_successfull(response) ?
+//		"successfully" : "without success");
+//	purple_debug_info("http-test", "Returned http code: %d.\n",
+//		purple_http_response_get_code(response));
+//	purple_debug_info("http-test", "Returned error: %s.\n",
+//		purple_http_response_get_error(response));
 
 	data = purple_http_response_get_data(response);
 	if (strlen(data) < 200)
@@ -112,15 +112,30 @@ static void ggp_test_http_cb2(PurpleUtil
 
 static void ggp_action_test_http(PurplePluginAction *action)
 {
+	PurpleHttpRequest *request;
+	//PurpleHttpConnection *hc;
 	PurpleConnection *gc = (PurpleConnection *)action->context;
 
 	purple_debug_info("http-test", "Testing http...\n");
 //	purple_http_get(gc, "http://www.wasilczyk.pl/x_ip_simple.htm",
 //		ggp_test_http_cb, NULL);
+/*
+	purple_http_get(gc, "http://www.wasilczyk.pl/x_ip_simple.htm",
+		ggp_test_http_cb, NULL);
+	purple_http_get(gc, "http://www.wasilczyk.pl/x_ip_simple.htm",
+		ggp_test_http_cb, NULL);
+	purple_http_get(gc, "http://www.wasilczyk.pl/x_ip_simple.htm",
+		ggp_test_http_cb, NULL);
+	purple_http_get(gc, "http://www.wasilczyk.pl/x_ip_simple.htm",
+		ggp_test_http_cb, NULL);
+	purple_http_get(gc, "http://www.wasilczyk.pl/x_ip_simple.htm",
+		ggp_test_http_cb, NULL);
+*/
+
 //	purple_http_get(gc, "http://google.com",
 //		ggp_test_http_cb, NULL);
-	purple_http_get(gc, "http://wp.pl",
-		ggp_test_http_cb, NULL);
+//	purple_http_get(gc, "http://wp.pl",
+//		ggp_test_http_cb, NULL);
 
 //	purple_http_get(gc, "https://www.google.pl",
 //		ggp_test_http_cb, NULL);
@@ -128,6 +143,13 @@ static void ggp_action_test_http(PurpleP
 //	purple_util_fetch_url("https://www.google.pl",
 //		TRUE, NULL, TRUE, -1, ggp_test_http_cb2, NULL);
 
+
+	request = purple_http_request_new("http://wp.pl");
+//	purple_http_request_set_http11(request, FALSE);
+	purple_http_request_set_max_redirects(request, 1);
+	purple_http_request(gc, request, ggp_test_http_cb, NULL);
+	purple_http_request_unref(request);
+
 	purple_debug_info("http-test", "Testing http started.\n");
 }
 



More information about the Commits mailing list