From b0bed674343af92421be84c1d0da353bcde6c0a6 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Wed, 6 Jan 2021 00:49:32 +0800 Subject: [PATCH] Add some api Signed-off-by: Jianhui Zhao --- src/connection.c | 16 ++++++++++++++++ src/uhttpd.c | 8 ++++++++ src/uhttpd.h | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/src/connection.c b/src/connection.c index 6c9c31e..b640923 100644 --- a/src/connection.c +++ b/src/connection.c @@ -706,8 +706,24 @@ static void keepalive_cb(struct ev_loop *loop, struct ev_timer *w, int revents) conn_error(&conn->com, HTTP_STATUS_REQUEST_TIMEOUT, NULL); } +static struct uh_server *conn_get_server(struct uh_connection *conn) +{ + struct uh_connection_internal *conni = (struct uh_connection_internal *)conn; + + return &conni->srv->com; +} + +static struct ev_loop *conn_get_loop(struct uh_connection *conn) +{ + struct uh_connection_internal *conni = (struct uh_connection_internal *)conn; + + return conni->srv->loop; +} + static void conn_init_cb(struct uh_connection *conn) { + conn->get_server = conn_get_server; + conn->get_loop = conn_get_loop; conn->done = conn_done; conn->send = conn_send; conn->send_file = conn_send_file; diff --git a/src/uhttpd.c b/src/uhttpd.c index 1753482..bee0024 100644 --- a/src/uhttpd.c +++ b/src/uhttpd.c @@ -371,6 +371,13 @@ static int uh_set_index_page(struct uh_server *srv, const char *name) return 0; } +static struct ev_loop *uh_get_loop(struct uh_server *srv) +{ + struct uh_server_internal *srvi = (struct uh_server_internal *)srv; + + return srvi->loop; +} + int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host, int port) { struct uh_server_internal *srvi = (struct uh_server_internal *)srv; @@ -446,6 +453,7 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host uh_log_debug("Listen on: %s %d\n", addr_str, port); } + srv->get_loop = uh_get_loop; srv->free = uh_server_free; srv->start_worker = uh_start_worker; diff --git a/src/uhttpd.h b/src/uhttpd.h index d747b98..3bf1cb9 100644 --- a/src/uhttpd.h +++ b/src/uhttpd.h @@ -45,7 +45,11 @@ enum { UH_EV_COMPLETE }; +struct uh_server; + struct uh_connection { + struct uh_server *(*get_server)(struct uh_connection *conn); + struct ev_loop *(*get_loop)(struct uh_connection *conn); /* ** Indicates the end of request processing ** Must be called at last, if not call 'error', 'redirect' and 'serve_file' @@ -78,6 +82,7 @@ struct uh_connection { typedef void (*uh_path_handler_prototype)(struct uh_connection *conn, int event); struct uh_server { + struct ev_loop *(*get_loop)(struct uh_server *srv); void (*free)(struct uh_server *srv); /* ** Start n worker processes to process the requests