From e6dee4a116f67a877e5b1f14a4dc1cc895fec71f Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 25 Aug 2003 17:59:33 +0000 Subject: [PATCH] Add a "user_data" arg, pass it to the callbacks. * libsoup/soup-message-io.c (soup_message_read, soup_message_write, soup_message_write_simple): Add a "user_data" arg, pass it to the callbacks. * libsoup/soup-message.c (soup_message_requeue, requeue_read_finished, requeue_read_error): Update for that * libsoup/soup-queue.c: Likewise * libsoup/soup-server.c: Likewise --- ChangeLog | 13 ++++++++++ libsoup/soup-message-io.c | 58 +++++++++++++++++++++++++++--------------- libsoup/soup-message-private.h | 36 +++++++++++++++++--------- libsoup/soup-message.c | 7 ++--- libsoup/soup-queue.c | 32 ++++++++++++----------- libsoup/soup-server.c | 37 ++++++++++++--------------- 6 files changed, 112 insertions(+), 71 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1575bcb..06c4f03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2003-08-25 Dan Winship + * libsoup/soup-message-io.c (soup_message_read, + soup_message_write, soup_message_write_simple): Add a "user_data" + arg, pass it to the callbacks. + + * libsoup/soup-message.c (soup_message_requeue, + requeue_read_finished, requeue_read_error): Update for that + + * libsoup/soup-queue.c: Likewise + + * libsoup/soup-server.c: Likewise + +2003-08-25 Dan Winship + * libsoup/soup-message.c (soup_message_new): Take a uri string instead of a context. Also, swap the args (so the method comes before the URI, just like in the protocol). diff --git a/libsoup/soup-message-io.c b/libsoup/soup-message-io.c index e1c9a23..7be5556 100644 --- a/libsoup/soup-message-io.c +++ b/libsoup/soup-message-io.c @@ -32,11 +32,13 @@ typedef struct { SoupMessageReadChunkFn read_chunk_cb; SoupMessageReadBodyFn read_body_cb; SoupMessageReadErrorFn error_cb; + gpointer user_data; } SoupMessageReadState; /* Put these around callback invocation if there is code afterward * that depends on the read not having been cancelled. */ +#define dummy_to_make_emacs_happy { #define SOUP_MESSAGE_READ_PREPARE_FOR_CALLBACK { gboolean cancelled; g_object_ref (msg); #define SOUP_MESSAGE_READ_RETURN_IF_CANCELLED cancelled = (msg->priv->read_state != r); g_object_unref (msg); if (cancelled) return; } #define SOUP_MESSAGE_READ_RETURN_VAL_IF_CANCELLED(val) cancelled = (msg->priv->read_state != r); g_object_unref (msg); if (cancelled) return val; } @@ -71,7 +73,8 @@ soup_message_read_set_callbacks (SoupMessage *msg, SoupMessageReadHeadersFn read_headers_cb, SoupMessageReadChunkFn read_chunk_cb, SoupMessageReadBodyFn read_body_cb, - SoupMessageReadErrorFn error_cb) + SoupMessageReadErrorFn error_cb, + gpointer user_data) { SoupMessageReadState *r = msg->priv->read_state; @@ -79,6 +82,7 @@ soup_message_read_set_callbacks (SoupMessage *msg, r->read_chunk_cb = read_chunk_cb; r->read_body_cb = read_body_cb; r->error_cb = error_cb; + r->user_data = user_data; } static void @@ -86,6 +90,7 @@ issue_final_callback (SoupMessage *msg) { SoupMessageReadState *r = msg->priv->read_state; SoupMessageReadBodyFn read_body_cb = r->read_body_cb; + gpointer user_data = r->user_data; char *body; guint len; @@ -101,7 +106,7 @@ issue_final_callback (SoupMessage *msg) } soup_message_read_cancel (msg); - read_body_cb (msg, body, len); + read_body_cb (msg, body, len, user_data); } static void @@ -109,6 +114,7 @@ failed_read (SoupSocket *sock, SoupMessage *msg) { SoupMessageReadState *r = msg->priv->read_state; SoupMessageReadErrorFn error_cb = r->error_cb; + gpointer user_data = r->user_data; /* Closing the connection to signify EOF is valid if content * length is unknown, but only if headers have been sent. @@ -120,7 +126,7 @@ failed_read (SoupSocket *sock, SoupMessage *msg) } soup_message_read_cancel (msg); - error_cb (msg); + error_cb (msg, user_data); } static gboolean @@ -178,7 +184,7 @@ read_body_chunk (SoupMessage *msg, guint *size) if (r->read_chunk_cb) { SOUP_MESSAGE_READ_PREPARE_FOR_CALLBACK; - r->read_chunk_cb (msg, read_buf, nread); + r->read_chunk_cb (msg, read_buf, nread, r->user_data); SOUP_MESSAGE_READ_RETURN_VAL_IF_CANCELLED (FALSE); } @@ -229,7 +235,8 @@ do_read (SoupSocket *sock, SoupMessage *msg) r->meta_buf->data, r->meta_buf->len, &r->encoding, - &r->read_length); + &r->read_length, + r->user_data); SOUP_MESSAGE_READ_RETURN_IF_CANCELLED; } g_byte_array_set_size (r->meta_buf, 0); @@ -325,11 +332,12 @@ idle_read (gpointer user_data) } void -soup_message_read (SoupMessage *msg, - SoupMessageReadHeadersFn read_headers_cb, - SoupMessageReadChunkFn read_chunk_cb, - SoupMessageReadBodyFn read_body_cb, - SoupMessageReadErrorFn error_cb) +soup_message_read (SoupMessage *msg, + SoupMessageReadHeadersFn read_headers_cb, + SoupMessageReadChunkFn read_chunk_cb, + SoupMessageReadBodyFn read_body_cb, + SoupMessageReadErrorFn error_cb, + gpointer user_data) { SoupMessageReadState *r; @@ -342,6 +350,7 @@ soup_message_read (SoupMessage *msg, r->read_chunk_cb = read_chunk_cb; r->read_body_cb = read_body_cb; r->error_cb = error_cb; + r->user_data = user_data; r->encoding = SOUP_TRANSFER_UNKNOWN; r->meta_buf = g_byte_array_new (); @@ -376,6 +385,7 @@ typedef struct { SoupMessageWriteGetChunkFn get_chunk_cb; SoupMessageWriteDoneFn write_done_cb; SoupMessageWriteErrorFn error_cb; + gpointer user_data; } SoupMessageWriteState; /* Put these around callback invocation if there is code afterward @@ -411,9 +421,10 @@ failed_write (SoupSocket *sock, SoupMessage *msg) { SoupMessageWriteState *w = msg->priv->write_state; SoupMessageWriteErrorFn error_cb = w->error_cb; + gpointer user_data = w->user_data; soup_message_write_cancel (msg); - error_cb (msg); + error_cb (msg, user_data); } static gboolean @@ -451,13 +462,14 @@ do_write (SoupSocket *sock, SoupMessage *msg) { SoupMessageWriteState *w = msg->priv->write_state; SoupMessageWriteDoneFn write_done_cb = w->write_done_cb; + gpointer user_data = w->user_data; while (1) { switch (msg->priv->status) { case SOUP_MESSAGE_STATUS_WRITING_HEADERS: if (w->get_header_cb) { SOUP_MESSAGE_WRITE_PREPARE_FOR_CALLBACK; - w->get_header_cb (msg, w->buf); + w->get_header_cb (msg, w->buf, user_data); SOUP_MESSAGE_WRITE_RETURN_IF_CANCELLED; w->get_header_cb = NULL; @@ -490,8 +502,9 @@ do_write (SoupSocket *sock, SoupMessage *msg) gboolean got_chunk; SOUP_MESSAGE_WRITE_PREPARE_FOR_CALLBACK; - got_chunk = - w->get_chunk_cb (msg, &w->chunk); + got_chunk = w->get_chunk_cb (msg, + &w->chunk, + user_data); SOUP_MESSAGE_WRITE_RETURN_IF_CANCELLED; if (!got_chunk) { @@ -560,7 +573,7 @@ do_write (SoupSocket *sock, SoupMessage *msg) done: msg->priv->status = SOUP_MESSAGE_STATUS_FINISHED_WRITING; soup_message_write_cancel (msg); - write_done_cb (msg); + write_done_cb (msg, user_data); } static gboolean @@ -579,7 +592,8 @@ create_writer (SoupMessage *msg, SoupTransferEncoding encoding, SoupMessageWriteGetHeaderFn get_header_cb, SoupMessageWriteDoneFn write_done_cb, - SoupMessageWriteErrorFn error_cb) + SoupMessageWriteErrorFn error_cb, + gpointer user_data) { SoupMessageWriteState *w; @@ -589,6 +603,7 @@ create_writer (SoupMessage *msg, w->get_header_cb = get_header_cb; w->write_done_cb = write_done_cb; w->error_cb = error_cb; + w->user_data = user_data; w->write_tag = g_signal_connect (msg->priv->socket, "writable", @@ -610,12 +625,14 @@ soup_message_write_simple (SoupMessage *msg, const SoupDataBuffer *body, SoupMessageWriteGetHeaderFn get_header_cb, SoupMessageWriteDoneFn write_done_cb, - SoupMessageWriteErrorFn error_cb) + SoupMessageWriteErrorFn error_cb, + gpointer user_data) { SoupMessageWriteState *w; w = create_writer (msg, SOUP_TRANSFER_CONTENT_LENGTH, - get_header_cb, write_done_cb, error_cb); + get_header_cb, write_done_cb, + error_cb, user_data); w->body = body; } @@ -626,12 +643,13 @@ soup_message_write (SoupMessage *msg, SoupMessageWriteGetHeaderFn get_header_cb, SoupMessageWriteGetChunkFn get_chunk_cb, SoupMessageWriteDoneFn write_done_cb, - SoupMessageWriteErrorFn error_cb) + SoupMessageWriteErrorFn error_cb, + gpointer user_data) { SoupMessageWriteState *w; w = create_writer (msg, encoding, get_header_cb, - write_done_cb, error_cb); + write_done_cb, error_cb, user_data); w->get_chunk_cb = get_chunk_cb; } diff --git a/libsoup/soup-message-private.h b/libsoup/soup-message-private.h index f2a5df8..ba56a83 100644 --- a/libsoup/soup-message-private.h +++ b/libsoup/soup-message-private.h @@ -81,53 +81,65 @@ typedef void (*SoupMessageReadHeadersFn) (SoupMessage *msg, char *headers, guint header_len, SoupTransferEncoding *encoding, - int *content_len); + int *content_len, + gpointer user_data); typedef void (*SoupMessageReadChunkFn) (SoupMessage *msg, const char *chunk, - guint len); + guint len, + gpointer user_data); typedef void (*SoupMessageReadBodyFn) (SoupMessage *msg, char *body, - guint len); + guint len, + gpointer user_data); -typedef void (*SoupMessageReadErrorFn) (SoupMessage *msg); +typedef void (*SoupMessageReadErrorFn) (SoupMessage *msg, + gpointer user_data); void soup_message_read (SoupMessage *msg, SoupMessageReadHeadersFn read_headers_cb, SoupMessageReadChunkFn read_chunk_cb, SoupMessageReadBodyFn read_body_cb, - SoupMessageReadErrorFn error_cb); + SoupMessageReadErrorFn error_cb, + gpointer user_data); void soup_message_read_set_callbacks (SoupMessage *msg, SoupMessageReadHeadersFn read_headers_cb, SoupMessageReadChunkFn read_chunk_cb, SoupMessageReadBodyFn read_body_cb, - SoupMessageReadErrorFn error_cb); + SoupMessageReadErrorFn error_cb, + gpointer user_data); void soup_message_read_cancel (SoupMessage *msg); typedef void (*SoupMessageWriteGetHeaderFn) (SoupMessage *msg, - GString *out_hdr); + GString *out_hdr, + gpointer user_data); typedef gboolean (*SoupMessageWriteGetChunkFn) (SoupMessage *msg, - SoupDataBuffer *out_next); + SoupDataBuffer *out_next, + gpointer user_data); -typedef void (*SoupMessageWriteDoneFn) (SoupMessage *msg); +typedef void (*SoupMessageWriteDoneFn) (SoupMessage *msg, + gpointer user_data); -typedef void (*SoupMessageWriteErrorFn) (SoupMessage *msg); +typedef void (*SoupMessageWriteErrorFn) (SoupMessage *msg, + gpointer user_data); void soup_message_write (SoupMessage *msg, SoupTransferEncoding encoding, SoupMessageWriteGetHeaderFn get_header_cb, SoupMessageWriteGetChunkFn get_chunk_cb, SoupMessageWriteDoneFn write_done_cb, - SoupMessageWriteErrorFn error_cb); + SoupMessageWriteErrorFn error_cb, + gpointer user_data); void soup_message_write_simple (SoupMessage *msg, const SoupDataBuffer *body, SoupMessageWriteGetHeaderFn get_header_cb, SoupMessageWriteDoneFn write_done_cb, - SoupMessageWriteErrorFn error_cb); + SoupMessageWriteErrorFn error_cb, + gpointer user_data); void soup_message_write_cancel (SoupMessage *msg); void soup_message_write_pause (SoupMessage *msg); diff --git a/libsoup/soup-message.c b/libsoup/soup-message.c index 0984697..2d6ef30 100644 --- a/libsoup/soup-message.c +++ b/libsoup/soup-message.c @@ -482,14 +482,15 @@ soup_message_queue (SoupMessage *req, } static void -requeue_read_error (SoupMessage *msg) +requeue_read_error (SoupMessage *msg, gpointer user_data) { soup_message_disconnect (msg); queue_message (msg); } static void -requeue_read_finished (SoupMessage *msg, char *body, guint len) +requeue_read_finished (SoupMessage *msg, char *body, guint len, + gpointer user_data) { SoupConnection *conn = msg->priv->connection; @@ -524,7 +525,7 @@ soup_message_requeue (SoupMessage *req) if (req->priv->connection && req->priv->read_state) { soup_message_read_set_callbacks (req, NULL, NULL, requeue_read_finished, - requeue_read_error); + requeue_read_error, NULL); if (req->priv->write_state) soup_message_write_cancel (req); diff --git a/libsoup/soup-queue.c b/libsoup/soup-queue.c index 0fc55bc..0928a73 100644 --- a/libsoup/soup-queue.c +++ b/libsoup/soup-queue.c @@ -54,7 +54,7 @@ soup_debug_print_headers (SoupMessage *req) } static void -soup_queue_error_cb (SoupMessage *req) +soup_queue_error_cb (SoupMessage *req, gpointer user_data) { SoupConnection *conn = soup_message_get_connection (req); const SoupUri *uri; @@ -110,11 +110,9 @@ soup_queue_error_cb (SoupMessage *req) } static void -soup_queue_read_headers_cb (SoupMessage *req, - char *headers, - guint headers_len, - SoupTransferEncoding *encoding, - int *content_len) +soup_queue_read_headers_cb (SoupMessage *req, char *headers, guint headers_len, + SoupTransferEncoding *encoding, int *content_len, + gpointer user_data) { const char *length, *enc; SoupHttpVersion version; @@ -203,9 +201,8 @@ soup_queue_read_headers_cb (SoupMessage *req, } static void -soup_queue_read_chunk_cb (SoupMessage *req, - const char *chunk, - guint len) +soup_queue_read_chunk_cb (SoupMessage *req, const char *chunk, guint len, + gpointer user_data) { req->response.owner = SOUP_BUFFER_STATIC; req->response.length = len; @@ -217,7 +214,8 @@ soup_queue_read_chunk_cb (SoupMessage *req, } static void -soup_queue_read_done_cb (SoupMessage *req, char *body, guint len) +soup_queue_read_done_cb (SoupMessage *req, char *body, guint len, + gpointer user_data) { if (soup_message_is_keepalive (req)) soup_connection_mark_old (soup_message_get_connection (req)); @@ -233,7 +231,8 @@ soup_queue_read_done_cb (SoupMessage *req, char *body, guint len) soup_queue_read_headers_cb, soup_queue_read_chunk_cb, soup_queue_read_done_cb, - soup_queue_error_cb); + soup_queue_error_cb, + NULL); } else req->priv->status = SOUP_MESSAGE_STATUS_FINISHED; @@ -323,7 +322,8 @@ soup_check_used_headers (gchar *key, } static void -soup_queue_get_request_header_cb (SoupMessage *req, GString *header) +soup_queue_get_request_header_cb (SoupMessage *req, GString *header, + gpointer user_data) { char *uri; SoupContext *proxy; @@ -405,13 +405,14 @@ soup_queue_get_request_header_cb (SoupMessage *req, GString *header) } static void -soup_queue_write_done_cb (SoupMessage *req) +soup_queue_write_done_cb (SoupMessage *req, gpointer user_data) { soup_message_read (req, soup_queue_read_headers_cb, soup_queue_read_chunk_cb, soup_queue_read_done_cb, - soup_queue_error_cb); + soup_queue_error_cb, + NULL); } static void @@ -449,7 +450,8 @@ start_request (SoupContext *ctx, SoupMessage *req) soup_message_write_simple (req, &req->request, soup_queue_get_request_header_cb, soup_queue_write_done_cb, - soup_queue_error_cb); + soup_queue_error_cb, + NULL); } static void diff --git a/libsoup/soup-server.c b/libsoup/soup-server.c index 403b261..6a46a71 100644 --- a/libsoup/soup-server.c +++ b/libsoup/soup-server.c @@ -181,13 +181,13 @@ soup_server_get_protocol (SoupServer *server) static void start_request (SoupServer *, SoupSocket *); static void -error_cb (SoupMessage *msg) +error_cb (SoupMessage *msg, gpointer user_data) { g_object_unref (msg); } static void -write_done_cb (SoupMessage *msg) +write_done_cb (SoupMessage *msg, gpointer user_data) { SoupServerMessage *smsg = SOUP_SERVER_MESSAGE (msg); @@ -207,8 +207,8 @@ write_header (gpointer name, gpointer value, gpointer headers) } static void -get_response_header_cb (SoupMessage *msg, - GString *headers) +get_response_header_cb (SoupMessage *msg, GString *headers, + gpointer user_data) { SoupServerMessage *smsg = SOUP_SERVER_MESSAGE (msg); SoupTransferEncoding encoding = @@ -235,10 +235,7 @@ get_response_header_cb (SoupMessage *msg, } static inline void -set_response_error (SoupMessage *req, - guint code, - char *phrase, - char *body) +set_response_error (SoupMessage *req, guint code, char *phrase, char *body) { if (phrase) soup_message_set_error_full (req, code, phrase); @@ -262,15 +259,14 @@ issue_bad_request (SoupMessage *msg) soup_message_write_simple (msg, &msg->response, get_response_header_cb, - write_done_cb, error_cb); + write_done_cb, error_cb, + NULL); } static void -read_headers_cb (SoupMessage *msg, - char *headers, - guint headers_len, - SoupTransferEncoding *encoding, - int *content_len) +read_headers_cb (SoupMessage *msg, char *headers, guint headers_len, + SoupTransferEncoding *encoding, int *content_len, + gpointer user_data) { SoupContext *ctx; char *req_path = NULL, *url; @@ -435,7 +431,7 @@ call_handler (SoupMessage *req) } static gboolean -get_chunk_cb (SoupMessage *msg, SoupDataBuffer *out_next) +get_chunk_cb (SoupMessage *msg, SoupDataBuffer *out_next, gpointer user_data) { SoupServerMessage *smsg = SOUP_SERVER_MESSAGE (msg); SoupDataBuffer *next; @@ -456,9 +452,7 @@ get_chunk_cb (SoupMessage *msg, SoupDataBuffer *out_next) } static void -read_done_cb (SoupMessage *req, - char *body, - guint len) +read_done_cb (SoupMessage *req, char *body, guint len, gpointer user_data) { SoupServerMessage *smsg = SOUP_SERVER_MESSAGE (req); SoupTransferEncoding encoding; @@ -473,12 +467,13 @@ read_done_cb (SoupMessage *req, if (encoding == SOUP_TRANSFER_CONTENT_LENGTH) { soup_message_write_simple (req, &req->response, get_response_header_cb, - write_done_cb, error_cb); + write_done_cb, error_cb, + NULL); soup_server_message_start (smsg); } else { soup_message_write (req, encoding, get_response_header_cb, get_chunk_cb, - write_done_cb, error_cb); + write_done_cb, error_cb, NULL); } return; @@ -492,7 +487,7 @@ start_request (SoupServer *server, SoupSocket *server_sock) /* Listen for another request on this connection */ msg = (SoupMessage *)soup_server_message_new (server, server_sock); soup_message_read (msg, read_headers_cb, NULL, - read_done_cb, error_cb); + read_done_cb, error_cb, NULL); } static void -- 2.7.4