New function: uh_register_default_hook

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2017-11-27 13:03:47 +08:00
parent 7ec4490eae
commit 257c6a09a6
3 changed files with 17 additions and 0 deletions

View File

@ -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;
};

View File

@ -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);

View File

@ -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;