/cpw/tomkiewicz/http: 81f92a310aff: Writing request contents

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Thu Oct 18 07:45:13 EDT 2012


Changeset: 81f92a310aff85c6e5ba673589423ffc3e82609c
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-10-18 13:45 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/81f92a310aff

Description:

Writing request contents

diffstat:

 libpurple/http.c            |  68 +++++++++++++++++++++++++++++++++++++++-----
 libpurple/protocols/gg/gg.c |  43 +++++++++++++++++++++++++++-
 2 files changed, 100 insertions(+), 11 deletions(-)

diffs (195 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -61,6 +61,9 @@ struct _PurpleHttpRequest
 	PurpleHttpHeaders *headers;
 	PurpleHttpCookieJar *cookie_jar;
 
+	gchar *contents;
+	int contents_length;
+
 	int timeout;
 	int max_redirects;
 	gboolean http11;
@@ -79,7 +82,7 @@ struct _PurpleHttpConnection
 
 	PurpleHttpSocket socket;
 	GString *request_header;
-	int request_header_written;
+	int request_header_written, request_contents_written;
 	gboolean main_header_got, headers_got;
 	GString *response_buffer;
 
@@ -410,6 +413,7 @@ static void _purple_http_gen_headers(Pur
 
 	hc->request_header = h = g_string_new("");
 	hc->request_header_written = 0;
+	hc->request_contents_written = 0;
 
 	if (proxy_http)
 		request_url = tmp_url = purple_http_url_print(url);
@@ -431,6 +435,11 @@ static void _purple_http_gen_headers(Pur
 	if (!purple_http_headers_get(hdrs, "accept"))
 		g_string_append(h, "Accept: */*\r\n");
 
+	if (req->contents_length > 0 && !purple_http_headers_get(hdrs,
+		"content-length"))
+		g_string_append_printf(h, "Content-Length: %u\r\n",
+			req->contents_length);
+
 	if (proxy_http)
 		g_string_append(h, "Proxy-Connection: close\r\n");
 
@@ -838,13 +847,29 @@ static void _purple_http_send(gpointer _
 	PurpleHttpSocket *hs = &hc->socket;
 	int written, write_len;
 	const gchar *write_from;
+	gboolean writing_headers;
 
 	_purple_http_gen_headers(hc);
 
-	write_from = hc->request_header->str + hc->request_header_written;
-	write_len = hc->request_header->len - hc->request_header_written;
+	writing_headers =
+		(hc->request_header_written < hc->request_header->len);
+	if (writing_headers) {
+		write_from = hc->request_header->str +
+			hc->request_header_written;
+		write_len = hc->request_header->len -
+			hc->request_header_written;
+	} else {
+		/* TODO: write_from - other write methods */
+		write_from = hc->request->contents +
+			hc->request_contents_written;
+		write_len = hc->request->contents_length -
+			hc->request_contents_written;
+	}
 
-	if (hs->is_ssl)
+	if (write_len == 0) {
+		purple_debug_warning("http", "Nothing to write\n");
+		written = 0;
+	} else if (hs->is_ssl)
 		written = purple_ssl_write(hs->ssl_connection,
 			write_from, write_len);
 	else
@@ -859,11 +884,17 @@ static void _purple_http_send(gpointer _
 		return;
 	}
 
-	hc->request_header_written += written;
-	if (hc->request_header_written < hc->request_header->len)
-		return;
-
-	/* TODO: write contents */
+	if (writing_headers) {
+		hc->request_header_written += written;
+		if (hc->request_header_written < hc->request_header->len)
+			return;
+		if (hc->request->contents_length > 0)
+			return;
+	} else {
+		hc->request_contents_written += written;
+		if (hc->request_contents_written < hc->request->contents_length)
+			return;
+	}
 
 	/* request is completely written, let's read the response */
 	purple_input_remove(hs->inpa);
@@ -1424,6 +1455,25 @@ const gchar * purple_http_request_get_me
 	return request->method;
 }
 
+void purple_http_request_set_contents(PurpleHttpRequest *request,
+	const gchar *contents, int length)
+{
+	g_return_if_fail(request != NULL);
+	g_return_if_fail(length >= -1);
+
+	g_free(request->contents);
+	if (contents == NULL || length == 0) {
+		request->contents = NULL;
+		request->contents_length = 0;
+		return;
+	}
+
+	if (length == -1)
+		length = strlen(contents);
+	request->contents = g_memdup(contents, length);
+	request->contents_length = length;
+}
+
 void purple_http_request_set_timeout(PurpleHttpRequest *request, int timeout)
 {
 	g_return_if_fail(request != NULL);
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
@@ -89,10 +89,29 @@ static void ggp_file_transfer_test_signe
 }
 */
 
+uin_t ggp_file_transfer_test_recipient = 38522810;
+const gchar *ggp_file_transfer_test_filename = "plik.txt";
+const gchar *ggp_file_transfer_test_file = "ala ma kota";
+
+static void ggp_file_transfer_test_offered(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer user_data)
+{
+	if (!purple_http_response_is_successfull(response))
+	{
+		purple_debug_error("gg-test", "offer failed\n");
+		return;
+	}
+
+	purple_debug_info("gg-test", "offered: %s\n",
+		purple_http_response_get_data(response));
+}
+
 static void ggp_file_transfer_test_signedin(PurpleHttpConnection *http_conn,
 	PurpleHttpResponse *response, gpointer user_data)
 {
+	PurpleHttpRequest *req;
 	const gchar *security_token;
+	gchar *data;
 
 	if (!purple_http_response_is_successfull(response))
 	{
@@ -119,6 +138,28 @@ static void ggp_file_transfer_test_signe
 	if (purple_debug_is_unsafe())
 		purple_debug_misc("gg-test", "security token=%s\n",
 			security_token);
+
+	//req = purple_http_request_new("https://drive.mpa.gg.pl/send_ticket");
+	req = purple_http_request_new("http://drive.mpa.gg.pl/send_ticket");
+	purple_http_request_set_method(req, "PUT");
+
+	purple_http_request_header_set(req, "X-gged-api-version", "6");
+	purple_http_request_header_set(req, "X-gged-security-token", security_token);
+
+	data = g_strdup_printf("{\"send_ticket\":{"
+		"\"recipient\":\"%u\","
+		"\"file_name\":\"%s\","
+		"\"file_size\":\"%u\""
+		"}}",
+		ggp_file_transfer_test_recipient,
+		ggp_file_transfer_test_filename,
+		strlen(ggp_file_transfer_test_file));
+	purple_http_request_set_contents(req, data, -1);
+	g_free(data);
+
+	purple_http_request(purple_http_conn_get_purple_connection(http_conn),
+		req, ggp_file_transfer_test_offered, NULL);
+	purple_http_request_unref(req);
 }
 
 static void ggp_file_transfer_test(PurpleConnection *gc)
@@ -154,8 +195,6 @@ static void ggp_file_transfer_test(Purpl
 	purple_http_request_header_set(req, "X-gged-client-metadata", metadata);
 	purple_http_request_header_set(req, "X-gged-api-version", "6");
 
-	purple_http_request_header_set(req, "Connection", "keep-alive");
-
 	purple_http_request(gc, req, ggp_file_transfer_test_signedin, NULL);
 	purple_http_request_unref(req);
 }



More information about the Commits mailing list