Change the names of variables and functions to make them more readable.
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>main
parent
2f8176bf14
commit
7ec4490eae
|
@ -8,7 +8,7 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
|
||||||
ev_break(loop, EVBREAK_ALL);
|
ev_break(loop, EVBREAK_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void route_test(struct uh_connection *con)
|
void hook_test(struct uh_connection *con)
|
||||||
{
|
{
|
||||||
struct uh_str *url = uh_get_url(con);
|
struct uh_str *url = uh_get_url(con);
|
||||||
struct uh_str *path = uh_get_path(con);
|
struct uh_str *path = uh_get_path(con);
|
||||||
|
@ -64,7 +64,7 @@ int main(int argc, char **argv)
|
||||||
goto err;
|
goto err;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uh_register_route(srv, "/test", route_test);
|
uh_register_hook(srv, "/test", hook_test);
|
||||||
|
|
||||||
uh_log_info("Listen on 8000...\n");
|
uh_log_info("Listen on 8000...\n");
|
||||||
|
|
||||||
|
|
|
@ -24,9 +24,9 @@
|
||||||
ev_timer_start(l, w); \
|
ev_timer_start(l, w); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
struct uh_route {
|
struct uh_hook {
|
||||||
char *path;
|
char *path;
|
||||||
uh_route_handler_t cb;
|
uh_hookfn_t cb;
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ struct uh_server {
|
||||||
#endif
|
#endif
|
||||||
ev_io read_watcher;
|
ev_io read_watcher;
|
||||||
struct ev_loop *loop;
|
struct ev_loop *loop;
|
||||||
struct list_head routes;
|
struct list_head hooks;
|
||||||
struct list_head connections;
|
struct list_head connections;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
struct uh_server;
|
struct uh_server;
|
||||||
struct uh_connection;
|
struct uh_connection;
|
||||||
|
|
||||||
typedef void (*uh_route_handler_t)(struct uh_connection *con);
|
typedef void (*uh_hookfn_t)(struct uh_connection *con);
|
||||||
|
|
||||||
const char *uh_version();
|
const char *uh_version();
|
||||||
|
|
||||||
|
@ -77,8 +77,8 @@ int uh_send_chunk(struct uh_connection *con, const char *buf, int len);
|
||||||
*/
|
*/
|
||||||
int uh_printf_chunk(struct uh_connection *con, const char *fmt, ...);
|
int uh_printf_chunk(struct uh_connection *con, const char *fmt, ...);
|
||||||
|
|
||||||
/* sets a callback to be executed on a specific path */
|
/* Registers a callback to be executed on a specific path */
|
||||||
int uh_register_route(struct uh_server *srv, const char *path, uh_route_handler_t cb);
|
int uh_register_hook(struct uh_server *srv, const char *path, uh_hookfn_t cb);
|
||||||
|
|
||||||
struct uh_str *uh_get_url(struct uh_connection *con);
|
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_path(struct uh_connection *con);
|
||||||
|
|
38
src/uhttp.c
38
src/uhttp.c
|
@ -177,11 +177,11 @@ static int uh_str_cmp(struct uh_str *uv, const char *str)
|
||||||
static int on_message_complete(http_parser *parser)
|
static int on_message_complete(http_parser *parser)
|
||||||
{
|
{
|
||||||
struct uh_connection *con = container_of(parser, struct uh_connection, parser);
|
struct uh_connection *con = container_of(parser, struct uh_connection, parser);
|
||||||
struct uh_route *r;
|
struct uh_hook *h;
|
||||||
|
|
||||||
list_for_each_entry(r, &con->srv->routes, list) {
|
list_for_each_entry(h, &con->srv->hooks, list) {
|
||||||
if (uh_str_cmp(&con->req.path, r->path)) {
|
if (uh_str_cmp(&con->req.path, h->path)) {
|
||||||
r->cb(con);
|
h->cb(con);
|
||||||
if (!(con->flags & UH_CON_CLOSE))
|
if (!(con->flags & UH_CON_CLOSE))
|
||||||
con->flags |= UH_CON_REUSE;
|
con->flags |= UH_CON_REUSE;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -338,7 +338,7 @@ struct uh_server *uh_server_new(struct ev_loop *loop, const char *ipaddr, int po
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
INIT_LIST_HEAD(&srv->routes);
|
INIT_LIST_HEAD(&srv->hooks);
|
||||||
INIT_LIST_HEAD(&srv->connections);
|
INIT_LIST_HEAD(&srv->connections);
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||||||
|
@ -377,7 +377,7 @@ void uh_server_free(struct uh_server *srv)
|
||||||
{
|
{
|
||||||
if (srv) {
|
if (srv) {
|
||||||
struct uh_connection *con, *tmp_c;
|
struct uh_connection *con, *tmp_c;
|
||||||
struct uh_route *r, *tmp_r;
|
struct uh_hook *h, *tmp_h;
|
||||||
|
|
||||||
if (srv->sock > 0)
|
if (srv->sock > 0)
|
||||||
close(srv->sock);
|
close(srv->sock);
|
||||||
|
@ -388,10 +388,10 @@ void uh_server_free(struct uh_server *srv)
|
||||||
uh_connection_destroy(con);
|
uh_connection_destroy(con);
|
||||||
}
|
}
|
||||||
|
|
||||||
list_for_each_entry_safe(r, tmp_r, &srv->routes, list) {
|
list_for_each_entry_safe(h, tmp_h, &srv->hooks, list) {
|
||||||
list_del(&r->list);
|
list_del(&h->list);
|
||||||
free(r->path);
|
free(h->path);
|
||||||
free(r);
|
free(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
uh_ssl_ctx_free(srv);
|
uh_ssl_ctx_free(srv);
|
||||||
|
@ -514,27 +514,27 @@ int uh_printf_chunk(struct uh_connection *con, const char *fmt, ...)
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int uh_register_route(struct uh_server *srv, const char *path, uh_route_handler_t cb)
|
int uh_register_hook(struct uh_server *srv, const char *path, uh_hookfn_t cb)
|
||||||
{
|
{
|
||||||
struct uh_route *r;
|
struct uh_hook *h;
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
|
|
||||||
r = calloc(1, sizeof(struct uh_route));
|
h = calloc(1, sizeof(struct uh_hook));
|
||||||
if (!r) {
|
if (!h) {
|
||||||
uh_log_err("calloc");
|
uh_log_err("calloc");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
r->path = strdup(path);
|
h->path = strdup(path);
|
||||||
if (!r->path) {
|
if (!h->path) {
|
||||||
uh_log_err("strdup");
|
uh_log_err("strdup");
|
||||||
free(r);
|
free(h);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
r->cb = cb;
|
h->cb = cb;
|
||||||
list_add(&r->list, &srv->routes);
|
list_add(&h->list, &srv->hooks);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue