soc.2008.xmpp: 34f9361d: * parsing HTTP response and emitting HTT...

tfar at soc.pidgin.im tfar at soc.pidgin.im
Fri Aug 15 10:11:00 EDT 2008


-----------------------------------------------------------------
Revision: 34f9361d412cd8909b9f3ec6addea37752c7530b
Ancestor: 889c6080de6c279c7deab72951807b17cdeba58f
Author: tfar at soc.pidgin.im
Date: 2008-08-11T21:28:05
Branch: im.pidgin.soc.2008.xmpp
URL: http://d.pidgin.im/viewmtn/revision/info/34f9361d412cd8909b9f3ec6addea37752c7530b

Modified files:
        libpurple/protocols/jabber/bosh.c
        libpurple/protocols/jabber/bosh.h

ChangeLog: 

* parsing HTTP response and emitting HTTP request callback

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/bosh.c	07a22aa1b65e69750d45fae673d2ad2761c75be0
+++ libpurple/protocols/jabber/bosh.c	00de3e0a027da37a61303bfc78ef58001f837a9a
@@ -80,6 +80,7 @@ void jabber_bosh_connection_login_cb(Pur
 
 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,17 +105,74 @@ static void jabber_bosh_http_connection_
 }
 
 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");
+	}
 }
 
 void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port) {
@@ -123,6 +181,7 @@ void jabber_bosh_http_connection_init(Pu
 	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(Purp
 }
 
 void jabber_bosh_http_request_clean(PurpleHTTPRequest *req) {
+	g_hash_table_destroy(req->header);
 	g_free(req->method);
 	g_free(req->path);
 }
+
+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);
+}
============================================================
--- libpurple/protocols/jabber/bosh.h	b7456af0c899a280bdd1964e95fb37dcb9a20a5e
+++ libpurple/protocols/jabber/bosh.h	1c911c687265fc0f984ff62770fb78f15f3dde7f
@@ -65,7 +65,10 @@ struct _PurpleHTTPConnection {
     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_clean(Purp
 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_ */


More information about the Commits mailing list