changeset 25813:828640306e31

Don't track a cb-per-POST and remove the PurpleHTTPResponse structure. The only callback ever used is http_received_cb and the ordering of responses from the server is not guaranteed to match the order of our requests, so the metaphor of matching them doesn't make sense. Instead of that, just track the number of requests (to ensure there is always a request outstanding). Additionally, pass the data const-ified instead of copying it. It's just fed to an XML parser anyway.
author Paul Aurich <paul@darkrain42.org>
date Wed, 21 Jan 2009 00:19:33 +0000
parents 8c58f31f41eb
children c11c14dde641
files libpurple/protocols/jabber/bosh.c
diffstat 1 files changed, 15 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/jabber/bosh.c	Tue Jan 20 06:10:42 2009 +0000
+++ b/libpurple/protocols/jabber/bosh.c	Wed Jan 21 00:19:33 2009 +0000
@@ -29,12 +29,10 @@
 #include "bosh.h"
 
 typedef struct _PurpleHTTPRequest PurpleHTTPRequest;
-typedef struct _PurpleHTTPResponse PurpleHTTPResponse;
 typedef struct _PurpleHTTPConnection PurpleHTTPConnection;
 
 typedef void (*PurpleHTTPConnectionConnectFunction)(PurpleHTTPConnection *conn);
 typedef void (*PurpleHTTPConnectionDisconnectFunction)(PurpleHTTPConnection *conn);
-typedef void (*PurpleHTTPRequestCallback)(PurpleHTTPResponse *res, void *userdata);
 typedef void (*PurpleBOSHConnectionConnectFunction)(PurpleBOSHConnection *conn);
 typedef void (*PurpleBOSHConnectionReceiveFunction)(PurpleBOSHConnection *conn, xmlnode *node);
 
@@ -66,9 +64,8 @@
     char *host;
     int port;
     int ie_handle;
-    GQueue *requests; /* Queue of PurpleHTTPRequestCallbacks */
-    
-    PurpleHTTPResponse *current_response;
+    int requests; /* number of outstanding HTTP requests */
+
     GString *buf;
     gboolean headers_done;
     gsize handled_len;
@@ -81,22 +78,15 @@
 };
 
 struct _PurpleHTTPRequest {
-    PurpleHTTPRequestCallback cb;
     const char *path;
     char *data;
     int data_len;
     void *userdata;
 };
 
-struct _PurpleHTTPResponse {
-    char *data;
-    int data_len;
-};
-
 static void jabber_bosh_connection_stream_restart(PurpleBOSHConnection *conn);
 static gboolean jabber_bosh_connection_error_check(PurpleBOSHConnection *conn, xmlnode *node);
 static void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node);
-static void jabber_bosh_connection_http_received_cb(PurpleHTTPResponse *res, void *userdata);
 static void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, xmlnode *node);
 
 static void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn);
@@ -134,13 +124,6 @@
 	g_free(req);
 }
 
-static void
-jabber_bosh_http_response_destroy(PurpleHTTPResponse *res)
-{
-	g_free(res->data);
-	g_free(res);
-}
-
 static PurpleHTTPConnection*
 jabber_bosh_http_connection_init(const char *host, int port)
 {
@@ -148,7 +131,6 @@
 	conn->host = g_strdup(host);
 	conn->port = port;
 	conn->fd = -1;
-	conn->requests = g_queue_new();
 
 	return conn;
 }
@@ -161,15 +143,9 @@
 	if (conn->buf)
 		g_string_free(conn->buf, TRUE);
 
-	if (conn->requests)
-		g_queue_free(conn->requests);
-
-	if (conn->current_response)
-		jabber_bosh_http_response_destroy(conn->current_response);
-
 	if (conn->ie_handle)
 		purple_input_remove(conn->ie_handle);
-	if (conn->fd > 0)
+	if (conn->fd >= 0)
 		close(conn->fd);
 
 	g_free(conn);
@@ -396,13 +372,15 @@
 	xmlnode_free(init);
 }
 
-static void jabber_bosh_connection_http_received_cb(PurpleHTTPResponse *res, void *userdata) {
+static void
+http_received_cb(const char *data, int len, void *userdata)
+{
 	PurpleBOSHConnection *conn = userdata;
 	if (conn->receive_cb) {
-		xmlnode *node = xmlnode_from_str(res->data, res->data_len);
+		xmlnode *node = xmlnode_from_str(data, len);
 		if (node) {
 			char *txt = xmlnode_to_formatted_str(node, NULL);
-			printf("\njabber_bosh_connection_http_received_cb\n%s\n", txt);
+			printf("\nhttp_received_cb\n%s\n", txt);
 			g_free(txt);
 			conn->receive_cb(conn, node);
 			xmlnode_free(node);
@@ -456,7 +434,6 @@
 	
 	request = g_new0(PurpleHTTPRequest, 1);
 	request->path     = conn->path;
-	request->cb       = jabber_bosh_connection_http_received_cb;
 	request->userdata = conn;
 
 	request->data = xmlnode_to_str(node, &(request->data_len));
@@ -493,12 +470,8 @@
 jabber_bosh_http_connection_process(PurpleHTTPConnection *conn)
 {
 	PurpleBOSHConnection *bosh_conn = conn->userdata;
-	PurpleHTTPRequestCallback cb;
 	const char *cursor;
 
-	if (!conn->current_response)
-		conn->current_response = g_new0(PurpleHTTPResponse, 1);
-
 	cursor = conn->buf->str + conn->handled_len;
 
 	if (!conn->headers_done) {
@@ -533,28 +506,21 @@
 	if (conn->buf->len - conn->handled_len < conn->body_len)
 		return;
 
-	cb = g_queue_pop_head(conn->requests);
+	--conn->requests;
 
 #warning For a pure HTTP 1.1 stack, this would need to be handled elsewhere.
-	if (bosh_conn->ready && g_queue_is_empty(conn->requests)) {
+	if (bosh_conn->ready && conn->requests == 0) {
 		jabber_bosh_connection_send(bosh_conn, NULL);
 		purple_debug_misc("jabber", "BOSH: Sending an empty request\n");
 	}
 
-	if (cb) {
-		conn->current_response->data_len = conn->body_len;
-		conn->current_response->data = g_memdup(conn->buf->str + conn->handled_len, conn->body_len + 1);
-
-		cb(conn->current_response, conn->userdata);
-	} else {
-		purple_debug_warning("jabber", "Received HTTP response before POST\n");
-	}
+	http_received_cb(conn->buf->str + conn->handled_len, conn->body_len,
+	                 conn->userdata);
 
 	g_string_free(conn->buf, TRUE);
 	conn->buf = NULL;
-	jabber_bosh_http_response_destroy(conn->current_response);
-	conn->current_response = NULL;
-	conn->headers_done = conn->handled_len = conn->body_len = 0;
+	conn->headers_done = FALSE;
+	conn->handled_len = conn->body_len = 0;
 }
 
 static void
@@ -662,8 +628,8 @@
 	 * low-level code in jabber.c */
 	ret = write(conn->fd, packet->str, packet->len);
 
+	++conn->requests;
 	g_string_free(packet, TRUE);
-	g_queue_push_tail(conn->requests, req->cb);
 	jabber_bosh_http_request_destroy(req);
 
 	if (ret < 0 && errno == EAGAIN)