cpw.darkrain42.xmpp.bosh: 914a150e: Don't track a cb-per-POST and remove the...
paul at darkrain42.org
paul at darkrain42.org
Fri Jan 23 22:27:44 EST 2009
-----------------------------------------------------------------
Revision: 914a150e0946a7b03298e5a93bf322f2e869e47a
Ancestor: 87e5b267c7251b68c2860428f4f13fa14f63feec
Author: paul at darkrain42.org
Date: 2009-01-21T00:19:33
Branch: im.pidgin.cpw.darkrain42.xmpp.bosh
URL: http://d.pidgin.im/viewmtn/revision/info/914a150e0946a7b03298e5a93bf322f2e869e47a
Modified files:
libpurple/protocols/jabber/bosh.c
ChangeLog:
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.
-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/bosh.c fa85d205b46830dac602507a058f9bf168c4d4f8
+++ libpurple/protocols/jabber/bosh.c 427fa4b4a4faf51a80224d53eec19b62abd85d9f
@@ -29,12 +29,10 @@ typedef struct _PurpleHTTPRequest Purple
#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 @@ struct _PurpleHTTPConnection {
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 {
};
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 @@ jabber_bosh_http_request_destroy(PurpleH
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 @@ jabber_bosh_http_connection_init(const c
conn->host = g_strdup(host);
conn->port = port;
conn->fd = -1;
- conn->requests = g_queue_new();
return conn;
}
@@ -161,15 +143,9 @@ jabber_bosh_http_connection_destroy(Purp
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 @@ static void jabber_bosh_connection_boot(
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 @@ static void jabber_bosh_connection_send_
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(Purp
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 @@ jabber_bosh_http_connection_process(Purp
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);
+ http_received_cb(conn->buf->str + conn->handled_len, conn->body_len,
+ conn->userdata);
- cb(conn->current_response, conn->userdata);
- } else {
- purple_debug_warning("jabber", "Received HTTP response before POST\n");
- }
-
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 @@ jabber_bosh_http_connection_send_request
* 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)
More information about the Commits
mailing list