/cpw/tomkiewicz/http: 95fda782966c: New feature: setting maximum...
Tomasz Wasilczyk
tomkiewicz at cpw.pidgin.im
Tue Oct 16 07:42:20 EDT 2012
Changeset: 95fda782966c2c59de49ed93a92aa11bcdcb7535
Author: Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date: 2012-10-16 13:42 +0200
Branch: default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/95fda782966c
Description:
New feature: setting maximum length of response
diffstat:
libpurple/http.c | 31 +++++++++++++++++++++++++++++++
libpurple/http.h | 17 +++++++++++++++--
libpurple/protocols/gg/gg.c | 3 ++-
3 files changed, 48 insertions(+), 3 deletions(-)
diffs (110 lines):
diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -57,6 +57,7 @@ struct _PurpleHttpRequest
int max_redirects;
gboolean http11;
+ int max_length;
};
struct _PurpleHttpConnection
@@ -76,6 +77,7 @@ struct _PurpleHttpConnection
GString *response_buffer;
int redirects_count;
+ int data_length_got;
int length_expected, length_got;
@@ -416,6 +418,17 @@ static gboolean _purple_http_recv_header
static void _purple_http_recv_body_data(PurpleHttpConnection *hc,
const gchar *buf, int len)
{
+ if (hc->request->max_length >= 0) {
+ if (hc->data_length_got + len > hc->request->max_length) {
+ len = hc->request->max_length - hc->data_length_got;
+ hc->length_expected = hc->length_got;
+ }
+ hc->data_length_got += len;
+ }
+
+ if (len == 0)
+ return;
+
g_string_append_len(hc->response->contents, buf, len);
}
@@ -831,6 +844,7 @@ static gboolean _purple_http_reconnect(P
g_string_free(hc->response->contents, TRUE);
hc->response->contents = NULL;
hc->length_got = 0;
+ hc->data_length_got = 0;
hc->length_expected = -1;
hc->is_chunked = FALSE;
hc->in_chunk = FALSE;
@@ -1024,6 +1038,23 @@ gboolean purple_http_request_is_http11(P
return request->http11;
}
+void purple_http_request_set_max_len(PurpleHttpRequest *request, int max_len)
+{
+ g_return_if_fail(request != NULL);
+
+ if (max_len < -1)
+ max_len = -1;
+
+ request->max_length = max_len;
+}
+
+int purple_http_request_get_max_len(PurpleHttpRequest *request)
+{
+ g_return_val_if_fail(request != NULL, -1);
+
+ return request->max_length;
+}
+
/*** HTTP response API ********************************************************/
static PurpleHttpResponse * purple_http_response_new(void)
diff --git a/libpurple/http.h b/libpurple/http.h
--- a/libpurple/http.h
+++ b/libpurple/http.h
@@ -293,9 +293,22 @@ void purple_http_request_set_http11(Purp
gboolean purple_http_request_is_http11(PurpleHttpRequest *request);
/**
- * -1 for unlimited
+ * Sets maximum length of response content to read.
+ *
+ * Headers length doesn't count here.
+ *
+ * @param request The request.
+ * @param max_len Maximum length of response to read (-1 for unlimited).
*/
-void purple_http_request_set_max_len(PurpleHttpRequest *request, int max_len); // +get
+void purple_http_request_set_max_len(PurpleHttpRequest *request, int max_len);
+
+/**
+ * Gets maximum length of response content to read.
+ *
+ * @param request The request.
+ * @return Maximum length of response to read, or -1 if unlimited.
+ */
+int purple_http_request_get_max_len(PurpleHttpRequest *request);
/**
* Sets (replaces, if exists) specified HTTP request header with provided value.
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
@@ -146,7 +146,8 @@ static void ggp_action_test_http(PurpleP
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_redirects(request, 1);
+// purple_http_request_set_max_len(request, 0);
purple_http_request(gc, request, ggp_test_http_cb, NULL);
purple_http_request_unref(request);
More information about the Commits
mailing list