Skip to content

Commit

Permalink
Support set a callback for connection closed
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Jan 15, 2021
1 parent 6cf45fb commit afeee7e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example/simple_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

#include "handler.h"

static void conn_closed_cb(struct uh_connection *conn)
{
}

static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
if (w->signum == SIGINT) {
Expand Down Expand Up @@ -113,6 +117,7 @@ int main(int argc, char **argv)
srv->set_docroot(srv, docroot);
srv->set_index_page(srv, index_page);

srv->set_conn_closed_cb(srv, conn_closed_cb);
srv->set_default_handler(srv, default_handler);
srv->add_path_handler(srv, "/echo", echo_handler);
srv->add_path_handler(srv, "/upload", upload_handler);
Expand Down
3 changes: 3 additions & 0 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ void conn_free(struct uh_connection_internal *conn)
uh_ssl_free(conn->ssl);
#endif

if (conn->srv->conn_closed_cb)
conn->srv->conn_closed_cb(&conn->com);

if (conn->sock > 0)
close(conn->sock);

Expand Down
8 changes: 8 additions & 0 deletions src/uhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ static int uh_add_path_handler(struct uh_server *srv, const char *path, uh_path_
return 0;
}

static void uh_set_conn_abort_cb(struct uh_server *srv, uh_con_closed_cb_prototype cb)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;

srvi->conn_closed_cb = cb;
}

static void uh_set_default_handler(struct uh_server *srv, uh_path_handler_prototype handler)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;
Expand Down Expand Up @@ -365,6 +372,7 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host

srv->load_plugin = uh_load_plugin;

srv->set_conn_closed_cb = uh_set_conn_abort_cb;
srv->set_default_handler = uh_set_default_handler;
srv->add_path_handler = uh_add_path_handler;

Expand Down
2 changes: 2 additions & 0 deletions src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct uh_connection {
void *userdata;
};

typedef void (*uh_con_closed_cb_prototype)(struct uh_connection *conn);
typedef void (*uh_path_handler_prototype)(struct uh_connection *conn, int event);

struct uh_server {
Expand All @@ -90,6 +91,7 @@ struct uh_server {
int (*ssl_init)(struct uh_server *srv, const char *cert, const char *key);
#endif
int (*load_plugin)(struct uh_server *srv, const char *path);
void (*set_conn_closed_cb)(struct uh_server *srv, uh_con_closed_cb_prototype cb);
void (*set_default_handler)(struct uh_server *srv, uh_path_handler_prototype handler);
int (*add_path_handler)(struct uh_server *srv, const char *path, uh_path_handler_prototype handler);
int (*set_docroot)(struct uh_server *srv, const char *path);
Expand Down
1 change: 1 addition & 0 deletions src/uhttpd_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct uh_server_internal {
struct ev_loop *loop;
struct ev_io ior;
struct uh_connection_internal *conns;
void (*conn_closed_cb)(struct uh_connection *conn);
void (*default_handler)(struct uh_connection *conn, int event);
#if UHTTPD_SSL_SUPPORT
void *ssl_ctx;
Expand Down

0 comments on commit afeee7e

Please sign in to comment.