More simplly to use

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao committed 2018-07-21 16:41:19 +08:00
1 parent abd7dbb17a
commit 3b1d425d07
12 files changed
+153 -218

No files matched your search

+9 -2
View File
@@ -21,12 +21,17 @@
#include <stdlib.h>
#include <unistd.h>
#include <uhttpd.h>
#include <string.h>
#include <libubox/ulog.h>
static void hello_action(struct uh_client *cl)
static int on_request(struct uh_client *cl)
{
const char *path = cl->get_path(cl);
int body_len = 0;
if (strcmp(path, "/hello"))
return UH_REQUEST_CONTINUE;
cl->send_header(cl, 200, "OK", -1);
cl->append_header(cl, "Myheader", "Hello");
cl->header_end(cl);
@@ -39,6 +44,8 @@ static void hello_action(struct uh_client *cl)
cl->chunk_printf(cl, "<h1>VAR name: %s</h1>", cl->get_var(cl, "name"));
cl->chunk_printf(cl, "<h1>BODY:%s</h1>", cl->get_body(cl, &body_len));
cl->request_done(cl);
return UH_REQUEST_DONE;
}
static void usage(const char *prog)
@@ -96,7 +103,7 @@ int main(int argc, char **argv)
ULOG_INFO("Listen on: %s *:%d\n", srv->ssl ? "https" : "http", port);
srv->add_action(srv, "/hello", hello_action);
srv->request_cb = on_request;
uloop_run();
done:
+7 -1
View File
@@ -51,7 +51,11 @@ srv:set_error404_cb(function(cl, opt)
uh.request_done(cl)
end)
srv:add_action("/lua", function(cl, opt)
srv:set_request_cb(function(cl, opt)
if opt.path ~= "/hello" then
return uh.REQUEST_CONTINUE
end
uh.send_header(cl, 200, "OK", -1)
uh.append_header(cl, "Myheader", "Hello")
uh.header_end(cl)
@@ -74,6 +78,8 @@ srv:add_action("/lua", function(cl, opt)
end
uh.request_done(cl)
return uh.REQUEST_DONE
end)
uloop.run()
+18 -6
View File
@@ -21,10 +21,16 @@
#include <stdlib.h>
#include <unistd.h>
#include <uhttpd.h>
#include <string.h>
#include <libubox/ulog.h>
static void hello_action(struct uh_client *cl)
static int on_request(struct uh_client *cl)
{
const char *path = cl->get_path(cl);
if (strcmp(path, "/template.html"))
return UH_REQUEST_CONTINUE;
#if (UHTTPD_LUA_SUPPORT)
uh_template(cl);
#else
@@ -34,6 +40,8 @@ static void hello_action(struct uh_client *cl)
cl->chunk_printf(cl, "<h1>Lua not enabled when compile</h1>");
cl->request_done(cl);
#endif
return UH_REQUEST_DONE;
}
static void usage(const char *prog)
@@ -72,7 +80,7 @@ int main(int argc, char **argv)
if (!verbose)
ulog_threshold(LOG_ERR);
ULOG_INFO("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
uloop_init();
@@ -91,12 +99,16 @@ int main(int argc, char **argv)
ULOG_INFO("Listen on: %s *:%d\n", srv->ssl ? "https" : "http", port);
srv->add_action(srv, "/template.html", hello_action);
srv->request_cb = on_request;
uloop_run();
done:
uloop_done();
srv->free(srv);
if (srv) {
srv->free(srv);
free(srv);
}
return 0;
}