2017-12-30 03:36:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Jianhui Zhao <jianhuizhao329@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-01-05 13:46:03 +00:00
|
|
|
#include <uhttpd.h>
|
|
|
|
|
|
|
|
//#define EXAMPLE_SSL
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2018-01-06 06:38:34 +00:00
|
|
|
#define port 8000
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
static void hello_action(struct uh_client *cl)
|
2017-11-09 04:54:28 +00:00
|
|
|
{
|
2017-12-29 09:48:06 +00:00
|
|
|
int body_len = 0;
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
cl->send_header(cl, 200, "OK", -1);
|
|
|
|
cl->append_header(cl, "Myheader", "Hello");
|
|
|
|
cl->header_end(cl);
|
|
|
|
|
|
|
|
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));
|
2018-01-01 03:44:46 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>URL: %s</h1>", cl->get_url(cl));
|
2017-12-29 06:40:58 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>PATH: %s</h1>", cl->get_path(cl));
|
|
|
|
cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
|
2018-01-01 03:44:46 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>VAR name: %s</h1>", cl->get_var(cl, "name"));
|
2017-12-29 09:48:06 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>BODY:%s</h1>", cl->get_body(cl, &body_len));
|
2017-12-29 06:40:58 +00:00
|
|
|
cl->request_done(cl);
|
2017-11-09 04:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-11-13 12:51:42 +00:00
|
|
|
struct uh_server *srv = NULL;
|
2017-12-29 06:40:58 +00:00
|
|
|
|
|
|
|
uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
uloop_init();
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
srv = uh_server_new("0.0.0.0", port);
|
|
|
|
if (!srv)
|
|
|
|
goto done;
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
uh_log_debug("Listen on: *:%s", port);
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2018-01-05 13:46:03 +00:00
|
|
|
#ifdef EXAMPLE_SSL
|
2017-12-29 06:40:58 +00:00
|
|
|
#if (UHTTPD_SSL_SUPPORT)
|
2018-01-01 01:37:59 +00:00
|
|
|
if (srv->ssl_init(srv, "/etc/wifidog/wifidog.key", "/etc/wifidog/wifidog.crt") < 0)
|
2017-12-29 06:40:58 +00:00
|
|
|
goto done;
|
2018-01-05 13:46:03 +00:00
|
|
|
#endif
|
2017-11-12 07:11:39 +00:00
|
|
|
#endif
|
|
|
|
|
2018-01-01 02:30:41 +00:00
|
|
|
srv->add_action(srv, "/hello", hello_action);
|
2017-11-13 12:51:42 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
uloop_run();
|
|
|
|
done:
|
|
|
|
uloop_done();
|
|
|
|
srv->free(srv);
|
2017-11-13 12:51:42 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-11-09 04:54:28 +00:00
|
|
|
}
|