Restructure with libubox

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao committed 2017-12-29 14:40:58 +08:00
1 parent ef94e04328
commit 568a287feb
38 files changed
+1815 -5578

No files matched your search

-4
View File
@@ -1,6 +1,2 @@
add_executable(helloworld helloworld.c)
target_link_libraries(helloworld uhttpd)
add_executable(lua_template lua_template.c)
target_link_libraries(lua_template uhttpd)
+27 -62
View File
@@ -1,80 +1,45 @@
#include <ev.h>
#include <stdio.h>
#include <uhttpd.h>
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
#define port "8000"
static void hello_action(struct uh_client *cl)
{
printf("Got signal: %d\n", w->signum);
ev_break(loop, EVBREAK_ALL);
}
cl->send_header(cl, 200, "OK", -1);
cl->append_header(cl, "Myheader", "Hello");
cl->header_end(cl);
void hook_test(struct uh_connection *con)
{
struct uh_str *url = uh_get_url(con);
struct uh_str *path = uh_get_path(con);
struct uh_str name = uh_get_var(con, "name");
struct uh_str *header_host = uh_get_header(con, "Host");
struct uh_str *header_ua = uh_get_header(con, "User-Agent");
char unescaped_name[128];
uh_send_head(con, HTTP_STATUS_OK, -1, NULL);
uh_printf_chunk(con, "<h1>Hello World</h1>");
uh_printf_chunk(con, "<h1>Libuhttp v%s</h1>", uh_version());
uh_printf_chunk(con, "<h1>Url: %.*s</h1>", (int)url->len, url->at);
uh_printf_chunk(con, "<h1>Path: %.*s</h1>", (int)path->len, path->at);
uh_printf_chunk(con, "<h1>Name: %.*s</h1>", (int)name.len, name.at);
if (name.at) {
uh_unescape(name.at, name.len, unescaped_name, sizeof(unescaped_name));
uh_printf_chunk(con, "<h1>Unescaped Name: %s</h1>", unescaped_name);
}
if (header_host)
uh_printf_chunk(con, "<h1>Host: %.*s</h1>", (int)header_host->len, header_host->at);
if (header_ua)
uh_printf_chunk(con, "<h1>User-Agent: %.*s</h1>", (int)header_ua->len, header_ua->at);
uh_send_chunk(con, NULL, 0);
cl->chunk_printf(cl, "<h1>Hello Libuhttpd %s</h1>", UHTTPD_VERSION_STRING);
cl->chunk_printf(cl, "<h1>REMOTE_ADDR: %s</h1>", cl->get_peer_addr(cl));
cl->chunk_printf(cl, "<h1>PATH: %s</h1>", cl->get_path(cl));
cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
cl->request_done(cl);
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
ev_signal *sig_watcher = NULL;
struct uh_server *srv = NULL;
uh_log_info("libuhttp version: %s\n", uh_version());
sig_watcher = calloc(1, sizeof(ev_signal));
if (!sig_watcher)
return -1;
ev_signal_init(sig_watcher, signal_cb, SIGINT);
ev_signal_start(loop, sig_watcher);
uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
srv = uh_server_new(loop, "0.0.0.0", 8000);
if (!srv) {
uh_log_err("uh_server_new failed\n");
goto err;
}
uloop_init();
#if (UHTTP_SSL_ENABLED)
if (uh_ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
goto err;
srv = uh_server_new("0.0.0.0", port);
if (!srv)
goto done;
uh_log_debug("Listen on: *:%s", port);
#if (UHTTPD_SSL_SUPPORT)
if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
goto done;
#endif
uh_register_hook(srv, "/test", hook_test);
uh_add_action(srv, "/hello", hello_action);
uh_log_info("Listen on 8000...\n");
ev_run(loop, 0);
err:
free(sig_watcher);
uh_server_free(srv);
uloop_run();
done:
uloop_done();
srv->free(srv);
return 0;
}
-56
View File
@@ -1,56 +0,0 @@
#include <ev.h>
#include <stdio.h>
#include <uhttpd.h>
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
printf("Got signal: %d\n", w->signum);
ev_break(loop, EVBREAK_ALL);
}
static void lua_template(struct uh_connection *con)
{
uh_template(con);
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
ev_signal *sig_watcher = NULL;
struct uh_server *srv = NULL;
uh_log_info("libuhttp version: %s", uh_version());
sig_watcher = calloc(1, sizeof(ev_signal));
if (!sig_watcher)
return -1;
ev_signal_init(sig_watcher, signal_cb, SIGINT);
ev_signal_start(loop, sig_watcher);
srv = uh_server_new(loop, "0.0.0.0", 8000);
if (!srv) {
uh_log_err("uh_server_new failed");
goto err;
}
#if (UHTTP_SSL_ENABLED)
if (uh_ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
goto err;
#endif
uh_register_default_hook(srv, lua_template);
uh_log_info("Listen on 8000...");
ev_run(loop, 0);
err:
free(sig_watcher);
uh_server_free(srv);
return 0;
}
-16
View File
@@ -1,16 +0,0 @@
<h1>Lua Template Test</h1>
<h1>Date: <%=os.date()%></h1>
<h1>Method: <%=_UHTTP["HTTP_METHOD"]%></h1>
<h1>Remote Host: <%=_UHTTP["REMOTE_HOST"]%></h1>
<h1>Url: <%=_UHTTP["HTTP_URL"]%></h1>
<h1>Path: <%=_UHTTP["HTTP_PATH"]%></h1>
<h1>------------Http Headers-------------</h1>
<%for k, v in pairs(_UHTTP["HEADERS"]) do%>
<h1><%=k%>: <%=v%></h1>
<%end%>
<h1>------------Http Variables-------------</h1>
<%for k, v in pairs(_UHTTP["VARIABLES"]) do%>
<h1><%=k%>: <%=v%></h1>
<%end%>