From 5d881d0f3ac1e391ee45a03ab8df43a028e21165 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 27 Dec 2009 22:51:06 +0100 Subject: [PATCH] [MINOR] new function stream_int_cond_close() This one will be used to conditionally send a message upon a close on a stream interface. It will not overwrite any existing data. --- include/proto/stream_interface.h | 1 + src/stream_interface.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/proto/stream_interface.h b/include/proto/stream_interface.h index 4e7f734195..e63e136180 100644 --- a/include/proto/stream_interface.h +++ b/include/proto/stream_interface.h @@ -32,6 +32,7 @@ int stream_int_check_timeouts(struct stream_interface *si); void stream_int_report_error(struct stream_interface *si); void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg); +void stream_int_cond_close(struct stream_interface *si, const struct chunk *msg); /* functions used when running a stream interface as a task */ void stream_int_update(struct stream_interface *si); diff --git a/src/stream_interface.c b/src/stream_interface.c index fcfa117b8b..9b7277fad6 100644 --- a/src/stream_interface.c +++ b/src/stream_interface.c @@ -80,6 +80,28 @@ void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) buffer_auto_close(si->ob); } +/* This function aborts all of the client's requests and stops the server's + * response with the data that are already present. If the response buffer + * empty, then the message in arguments will be sent. This ensures that + * we won't mix an HTTP response with pending data. The buffer is marked for + * read shutdown on the server side to protect the message or any pending + * data, and the buffer write is enabled. The message is contained in a + * "chunk". If it is null, then an empty message is used. The reply buffer + * doesn't need to be empty before this. The goal of this function is to + * return error messages to a client but only when it is possible. + */ +void stream_int_cond_close(struct stream_interface *si, const struct chunk *msg) +{ + buffer_abort(si->ib); + buffer_erase(si->ib); /* remove any pending request */ + buffer_shutr_now(si->ob); + if (!si->ob->l && msg && msg->len) + buffer_write(si->ob, msg->str, msg->len); + + si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); + buffer_auto_close(si->ob); +} + /* default update function for scheduled tasks, not used for embedded tasks */ void stream_int_update(struct stream_interface *si) { -- 2.47.3