changeset 25604:e28f1711f402

* parsing HTTP response and emitting HTTP request callback
author Tobias Markmann <tfar@soc.pidgin.im>
date Mon, 11 Aug 2008 21:28:05 +0000
parents 24b97b8359ff
children f747c682a0d9
files libpurple/protocols/jabber/bosh.c libpurple/protocols/jabber/bosh.h
diffstat 2 files changed, 82 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/jabber/bosh.c	Sat Aug 09 23:07:58 2008 +0000
+++ b/libpurple/protocols/jabber/bosh.c	Mon Aug 11 21:28:05 2008 +0000
@@ -80,6 +80,7 @@
 
 void jabber_bosh_connection_login_cb(PurpleHTTPRequest *req, PurpleHTTPResponse *res, void *userdata) {
 	purple_debug_info("jabber", "RECEIVED FIRST HTTP RESPONSE\n");
+	printf("\nDATA\n\n%s\n", res->data);
 }
 
 void jabber_bosh_connection_send(PurpleBOSHConnection *conn, xmlnode *node) {
@@ -104,16 +105,73 @@
 }
 
 static void jabber_bosh_http_connection_receive(gpointer data, gint source, PurpleInputCondition condition) {
+	char buffer[1024];
+	int len;
 	PurpleHTTPConnection *conn = data;
 	PurpleHTTPResponse *response = conn->current_response;
 	
 	purple_debug_info("jabber", "jabber_bosh_http_connection_receive\n");
-	if (response) {
-		// data for current response
-		
-	} else {
+	if (!response) {
 		// new response
 		response = conn->current_response = g_new0(PurpleHTTPResponse, 1);
+		jabber_bosh_http_response_init(response);
+	}
+	
+	len = read(source, buffer, 1024);
+	if (len > 0) {
+		char *found = NULL;
+		if (found = g_strstr_len(buffer, len, "\r\n\r\n")) {
+			char *beginning = buffer;
+			char *field = NULL;
+			char *value = NULL;
+			char *cl = NULL;
+			
+			while (*beginning != 'H') ++beginning;
+			beginning[12] = 0;
+			response->status = atoi(&beginning[9]);
+			beginning = &beginning[13];
+			do {
+				++beginning;
+			} while (*beginning != '\n');
+			++beginning;
+			/* parse HTTP response header */
+			for (;beginning != found; ++beginning) {
+				if (!field) field = beginning;
+				if (*beginning == ':') {
+					*beginning = 0;
+					value = beginning + 1;
+				} else if (*beginning == '\r') {
+					*beginning = 0;
+					g_hash_table_replace(response->header, g_strdup(field), g_strdup(value));
+					value = field = 0;
+					++beginning;
+				}
+			}
+			++found; ++found; ++found; ++found;
+			
+			cl = g_hash_table_lookup(response->header, "Content-Length");
+			if (cl) {
+				PurpleHTTPRequest *request = NULL;
+				response->data_len = atoi(cl);
+				if (response->data <= len - (found - buffer)) response->data = g_memdup(found, response->data_len);
+				else printf("\nDidn't receive complete content");
+				
+				request = g_queue_pop_head(conn->requests);
+				if (request) {
+					request->cb(request, response, conn->userdata);
+					jabber_bosh_http_request_clean(request);
+					jabber_bosh_http_response_clean(response);
+				} else {
+					purple_debug_info("jabber", "received HTTP response but haven't requested anything yet.\n");
+				}
+			} else {
+				printf("\ndidn't receive length.\n");
+			}
+		} else {
+			printf("\nDid not receive complete HTTP header!\n");
+		}
+	} else {
+		purple_debug_info("jabber", "jabber_bosh_http_connection_receive: problem receiving data\n");
 	}
 }
 
@@ -123,6 +181,7 @@
 	conn->port = port;
 	conn->connect_cb = NULL;
 	conn->current_response = NULL;
+	conn->current_data = NULL;
 	conn->requests = g_queue_new();
 }
 
@@ -188,6 +247,18 @@
 }
 
 void jabber_bosh_http_request_clean(PurpleHTTPRequest *req) {
+	g_hash_table_destroy(req->header);
 	g_free(req->method);
 	g_free(req->path);
-}
\ No newline at end of file
+}
+
+void jabber_bosh_http_response_init(PurpleHTTPResponse *res) {
+	res->status = 0;
+	res->header = g_hash_table_new(g_str_hash, g_str_equal);
+}
+
+
+void jabber_bosh_http_response_clean(PurpleHTTPResponse *res) {
+	g_hash_table_destroy(res->header);
+	g_free(res->data);
+}
--- a/libpurple/protocols/jabber/bosh.h	Sat Aug 09 23:07:58 2008 +0000
+++ b/libpurple/protocols/jabber/bosh.h	Mon Aug 11 21:28:05 2008 +0000
@@ -65,7 +65,10 @@
     PurpleConnection *conn;
     PurpleAccount *account;
     GQueue *requests;
+    
     PurpleHTTPResponse *current_response;
+    char *current_data;
+    
     int pih;
     PurpleHTTPConnectionConnectFunction connect_cb;
     void *userdata;
@@ -101,4 +104,7 @@
 void jabber_bosh_http_request_add_to_header(PurpleHTTPRequest *req, const char *field, const char *value);
 void jabber_bosh_http_request_set_data(PurpleHTTPRequest *req, char *data, int len);
 void jabber_bosh_http_request_clean(PurpleHTTPRequest *req);
+
+void jabber_bosh_http_response_init(PurpleHTTPResponse *res);
+void jabber_bosh_http_response_clean(PurpleHTTPResponse *res);
 #endif /* _PURPLE_JABBER_BOSH_H_ */