/cpw/tomkiewicz/http: 4042907bcdf1: Timeout support

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Wed Oct 17 04:04:29 EDT 2012


Changeset: 4042907bcdf125f324c4cda925ce4c282d9d28d5
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-10-17 10:04 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/4042907bcdf1

Description:

Timeout support

diffstat:

 libpurple/http.c            |  49 ++++++++++++++++++++++++++++++++++++++++++--
 libpurple/http.h            |  17 +++++++++++++++
 libpurple/protocols/gg/gg.c |  12 ++++++----
 3 files changed, 70 insertions(+), 8 deletions(-)

diffs (174 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -33,6 +33,7 @@
 #define PURPLE_HTTP_MAX_RECV_BUFFER_LEN 10240
 
 #define PURPLE_HTTP_REQUEST_DEFAULT_MAX_REDIRECTS 20
+#define PURPLE_HTTP_REQUEST_DEFAULT_TIMEOUT 30
 
 typedef struct _PurpleHttpURL PurpleHttpURL;
 
@@ -56,6 +57,7 @@ struct _PurpleHttpRequest
 	gchar *url;
 	PurpleHttpHeaders *headers;
 
+	int timeout;
 	int max_redirects;
 	gboolean http11;
 	int max_length;
@@ -86,6 +88,8 @@ struct _PurpleHttpConnection
 	int chunk_length, chunk_got;
 
 	GList *link_global, *link_gc;
+
+	guint timeout_handle;
 };
 
 struct _PurpleHttpResponse
@@ -546,8 +550,13 @@ static gboolean _purple_http_recv_body_c
 		line_len = eol - line;
 
 		if (1 != sscanf(line, "%x", &hc->chunk_length)) {
-			purple_debug_warning("http",
-				"Chunk length not found\n");
+			if (purple_debug_is_unsafe())
+				purple_debug_warning("http",
+					"Chunk length not found in [%s]\n",
+					line);
+			else
+				purple_debug_warning("http",
+					"Chunk length not found\n");
 			_purple_http_error(hc, _("Error parsing HTTP"));
 			return FALSE;
 		}
@@ -915,6 +924,17 @@ static gboolean _purple_http_reconnect(P
 
 /*** Performing HTTP requests *************************************************/
 
+static gboolean purple_http_request_timeout(gpointer _hc)
+{
+	PurpleHttpConnection *hc = _hc;
+
+	purple_debug_warning("http", "Timeout reached for request %p\n", hc);
+
+	purple_http_conn_cancel(hc);
+
+	return FALSE;
+}
+
 PurpleHttpConnection * purple_http_get(PurpleConnection *gc, const gchar *url,
 	PurpleHttpCallback callback, gpointer user_data)
 {
@@ -957,7 +977,8 @@ PurpleHttpConnection * purple_http_reque
 
 	_purple_http_reconnect(hc);
 
-	/* TODO: timeout */
+	hc->timeout_handle = purple_timeout_add_seconds(request->timeout,
+		purple_http_request_timeout, hc);
 
 	return hc;
 }
@@ -991,6 +1012,9 @@ static PurpleHttpConnection * purple_htt
 
 static void purple_http_connection_free(PurpleHttpConnection *hc)
 {
+	if (hc->timeout_handle)
+		purple_timeout_remove(hc->timeout_handle);
+
 	purple_http_url_free(hc->url);
 	purple_http_request_unref(hc->request);
 	purple_http_response_free(hc->response);
@@ -1078,8 +1102,10 @@ PurpleHttpRequest * purple_http_request_
 	request->url = g_strdup(url);
 	request->headers = purple_http_headers_new();
 
+	request->timeout = PURPLE_HTTP_REQUEST_DEFAULT_TIMEOUT;
 	request->max_redirects = PURPLE_HTTP_REQUEST_DEFAULT_MAX_REDIRECTS;
 	request->http11 = TRUE;
+	request->max_length = -1;
 
 	return request;
 }
@@ -1113,6 +1139,23 @@ PurpleHttpRequest * purple_http_request_
 	return NULL;
 }
 
+void purple_http_request_set_timeout(PurpleHttpRequest *request, int timeout)
+{
+	g_return_if_fail(request != NULL);
+
+	if (timeout < -1)
+		timeout = -1;
+
+	request->timeout = timeout;
+}
+
+int purple_http_request_get_timeout(PurpleHttpRequest *request)
+{
+	g_return_val_if_fail(request != NULL, -1);
+
+	return request->timeout;
+}
+
 void purple_http_request_set_max_redirects(PurpleHttpRequest *request,
 	int max_redirects)
 {
diff --git a/libpurple/http.h b/libpurple/http.h
--- a/libpurple/http.h
+++ b/libpurple/http.h
@@ -262,6 +262,23 @@ void purple_http_request_set_response_wr
 	PurpleHttpContentWriter writer, gpointer user_data);
 
 /**
+ * Set maximum amount of time, that request is allowed to run.
+ *
+ * @param request The request.
+ * @param timeout Time (in seconds) after that timeout will be cancelled,
+ *                -1 for infinite time.
+ */
+void purple_http_request_set_timeout(PurpleHttpRequest *request, int timeout);
+
+/**
+ * Get maximum amount of time, that request is allowed to run.
+ *
+ * @param request The request.
+ * @return        Timeout currently set (-1 for infinite).
+ */
+int purple_http_request_get_timeout(PurpleHttpRequest *request);
+
+/**
  * Sets maximum amount of redirects.
  *
  * @param request       The request.
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
@@ -114,6 +114,7 @@ static void ggp_action_test_http(PurpleP
 	//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);
@@ -137,12 +138,13 @@ static void ggp_action_test_http(PurpleP
 //	purple_util_fetch_url("https://www.google.pl",
 	purple_util_fetch_url("http://wp.pl",
 		TRUE, "My Browser", 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_set_max_len(request, 0);
+	request = purple_http_request_new("http://www.wasilczyk.pl/x_ip_simple.htm");
+//	purple_http_request_set_http11(request, FALSE);
+//	purple_http_request_set_max_redirects(request, 1);
+//	purple_http_request_set_max_len(request, -1);
+	purple_http_request_set_timeout(request, 3);
 	purple_http_request(gc, request, ggp_test_http_cb, NULL);
 	purple_http_request_unref(request);
 



More information about the Commits mailing list