Skip to content

Commit

Permalink
New function: uh_register_default_hook
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Nov 27, 2017
1 parent 7ec4490 commit 257c6a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/include/internal.h
Expand Up @@ -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;
};
Expand Down
6 changes: 6 additions & 0 deletions src/include/uhttp/uhttp.h
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/uhttp.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 257c6a0

Please sign in to comment.