From 257c6a09a693a23a8c27a0c9cef165c88f090358 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 27 Nov 2017 13:03:47 +0800 Subject: [PATCH] New function: uh_register_default_hook Signed-off-by: Jianhui Zhao --- src/include/internal.h | 1 + src/include/uhttp/uhttp.h | 6 ++++++ src/uhttp.c | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/src/include/internal.h b/src/include/internal.h index 8fef1f3..e8a9428 100755 --- a/src/include/internal.h +++ b/src/include/internal.h @@ -37,6 +37,7 @@ struct uh_server { #endif ev_io read_watcher; struct ev_loop *loop; + uh_hookfn_t default_cb; struct list_head hooks; struct list_head connections; }; diff --git a/src/include/uhttp/uhttp.h b/src/include/uhttp/uhttp.h index 784de2f..22551d1 100755 --- a/src/include/uhttp/uhttp.h +++ b/src/include/uhttp/uhttp.h @@ -80,6 +80,12 @@ int uh_printf_chunk(struct uh_connection *con, const char *fmt, ...); /* Registers a callback to be executed on a specific path */ int uh_register_hook(struct uh_server *srv, const char *path, uh_hookfn_t cb); +/* + * Registers a callback to be executed when don't find a callback function + * that is specific to a path. + */ +void uh_register_default_hook(struct uh_server *srv, uh_hookfn_t cb); + struct uh_str *uh_get_url(struct uh_connection *con); struct uh_str *uh_get_path(struct uh_connection *con); struct uh_str *uh_get_query(struct uh_connection *con); diff --git a/src/uhttp.c b/src/uhttp.c index 2987972..d1b8a44 100755 --- a/src/uhttp.c +++ b/src/uhttp.c @@ -188,6 +188,11 @@ static int on_message_complete(http_parser *parser) } } + if (con->srv->default_cb) { + con->srv->default_cb(con); + return 0; + } + uh_send_error(con, HTTP_STATUS_NOT_FOUND, NULL); return 0; @@ -539,6 +544,11 @@ int uh_register_hook(struct uh_server *srv, const char *path, uh_hookfn_t cb) return 0; } +void uh_register_default_hook(struct uh_server *srv, uh_hookfn_t cb) +{ + srv->default_cb = cb; +} + inline struct uh_str *uh_get_url(struct uh_connection *con) { return &con->req.url;