115 lines
2.7 KiB
C
115 lines
2.7 KiB
C
/*
|
|
* 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 Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 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
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
* USA
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <uhttpd.h>
|
|
#include <string.h>
|
|
#include <libubox/ulog.h>
|
|
|
|
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
|
|
cl->send_header(cl, 200, "OK", -1);
|
|
cl->header_end(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)
|
|
{
|
|
fprintf(stderr, "Usage: %s [option]\n"
|
|
" -p port # Default port is 8080\n"
|
|
" -s # SSl on\n"
|
|
" -v # verbose\n", prog);
|
|
exit(1);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct uh_server *srv = NULL;
|
|
bool verbose = false;
|
|
bool ssl = false;
|
|
int port = 8080;
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "p:vs")) != -1) {
|
|
switch (opt)
|
|
{
|
|
case 'p':
|
|
port = atoi(optarg);
|
|
break;
|
|
case 's':
|
|
ssl = true;
|
|
break;
|
|
case 'v':
|
|
verbose = true;
|
|
break;
|
|
default: /* '?' */
|
|
usage(argv[0]);
|
|
}
|
|
}
|
|
|
|
if (!verbose)
|
|
ulog_threshold(LOG_ERR);
|
|
|
|
ULOG_INFO("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
|
|
|
|
uloop_init();
|
|
|
|
srv = uh_server_new("0.0.0.0", port);
|
|
if (!srv)
|
|
goto done;
|
|
|
|
#if (!UHTTPD_SSL_SUPPORT)
|
|
if (ssl)
|
|
ULOG_ERR("SSl is not compiled in\n");
|
|
#else
|
|
if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
|
|
goto done;
|
|
#endif
|
|
|
|
ULOG_INFO("Listen on: %s *:%d\n", srv->ssl ? "https" : "http", port);
|
|
|
|
srv->request_cb = on_request;
|
|
|
|
uloop_run();
|
|
done:
|
|
uloop_done();
|
|
|
|
if (srv) {
|
|
srv->free(srv);
|
|
free(srv);
|
|
}
|
|
|
|
return 0;
|
|
}
|