example/template.c: dos2unix

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-07-21 16:09:19 +08:00
parent 4ae06b8150
commit abd7dbb17a
1 changed files with 102 additions and 102 deletions

View File

@ -1,102 +1,102 @@
/* /*
* Copyright (C) 2017 Jianhui Zhao <jianhuizhao329@gmail.com> * Copyright (C) 2017 Jianhui Zhao <jianhuizhao329@gmail.com>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * 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, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA * USA
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <uhttpd.h> #include <uhttpd.h>
#include <libubox/ulog.h> #include <libubox/ulog.h>
static void hello_action(struct uh_client *cl) static void hello_action(struct uh_client *cl)
{ {
#if (UHTTPD_LUA_SUPPORT) #if (UHTTPD_LUA_SUPPORT)
uh_template(cl); uh_template(cl);
#else #else
cl->send_header(cl, 200, "OK", -1); cl->send_header(cl, 200, "OK", -1);
cl->header_end(cl); cl->header_end(cl);
cl->chunk_printf(cl, "<h1>Lua not enabled when compile</h1>"); cl->chunk_printf(cl, "<h1>Lua not enabled when compile</h1>");
cl->request_done(cl); cl->request_done(cl);
#endif #endif
} }
static void usage(const char *prog) static void usage(const char *prog)
{ {
fprintf(stderr, "Usage: %s [option]\n" fprintf(stderr, "Usage: %s [option]\n"
" -p port # Default port is 8080\n" " -p port # Default port is 8080\n"
" -s # SSl on\n" " -s # SSl on\n"
" -v # verbose\n", prog); " -v # verbose\n", prog);
exit(1); exit(1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct uh_server *srv = NULL; struct uh_server *srv = NULL;
bool verbose = false; bool verbose = false;
bool ssl = false; bool ssl = false;
int port = 8080; int port = 8080;
int opt; int opt;
while ((opt = getopt(argc, argv, "p:vs")) != -1) { while ((opt = getopt(argc, argv, "p:vs")) != -1) {
switch (opt) switch (opt)
{ {
case 'p': case 'p':
port = atoi(optarg); port = atoi(optarg);
break; break;
case 's': case 's':
ssl = true; ssl = true;
break; break;
case 'v': case 'v':
verbose = true; verbose = true;
break; break;
default: /* '?' */ default: /* '?' */
usage(argv[0]); usage(argv[0]);
} }
} }
if (!verbose) if (!verbose)
ulog_threshold(LOG_ERR); ulog_threshold(LOG_ERR);
ULOG_INFO("libuhttpd version: %s\n", UHTTPD_VERSION_STRING); ULOG_INFO("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
uloop_init(); uloop_init();
srv = uh_server_new("0.0.0.0", port); srv = uh_server_new("0.0.0.0", port);
if (!srv) if (!srv)
goto done; goto done;
#if (!UHTTPD_SSL_SUPPORT) #if (!UHTTPD_SSL_SUPPORT)
if (ssl) if (ssl)
ULOG_ERR("SSl is not compiled in\n"); ULOG_ERR("SSl is not compiled in\n");
#else #else
if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
goto done; goto done;
#endif #endif
ULOG_INFO("Listen on: %s *:%d\n", srv->ssl ? "https" : "http", port); ULOG_INFO("Listen on: %s *:%d\n", srv->ssl ? "https" : "http", port);
srv->add_action(srv, "/template.html", hello_action); srv->add_action(srv, "/template.html", hello_action);
uloop_run(); uloop_run();
done: done:
uloop_done(); uloop_done();
srv->free(srv); srv->free(srv);
return 0; return 0;
} }