2019-08-27 02:21:37 +00:00
|
|
|
/*
|
|
|
|
* MIT License
|
|
|
|
*
|
2019-09-03 07:03:35 +00:00
|
|
|
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
|
2019-08-27 02:21:37 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2020-12-16 16:53:36 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2019-08-27 02:21:37 +00:00
|
|
|
|
|
|
|
#include "uhttpd.h"
|
|
|
|
|
2020-07-02 02:32:19 +00:00
|
|
|
static bool serve_file = false;
|
|
|
|
|
2020-12-16 16:53:36 +00:00
|
|
|
static void default_handler(struct uh_connection *conn, int event)
|
2019-08-27 02:21:37 +00:00
|
|
|
{
|
2020-12-16 16:53:36 +00:00
|
|
|
if (event != UH_EV_COMPLETE)
|
|
|
|
return;
|
|
|
|
|
2020-07-02 02:32:19 +00:00
|
|
|
if (!serve_file) {
|
2020-09-07 06:31:21 +00:00
|
|
|
struct uh_str path = conn->get_path(conn);
|
|
|
|
struct uh_str query = conn->get_query(conn);
|
|
|
|
struct uh_str ua = conn->get_header(conn, "User-Agent");
|
|
|
|
struct uh_str body = conn->get_body(conn);
|
2020-07-02 02:32:19 +00:00
|
|
|
|
|
|
|
conn->send_head(conn, HTTP_STATUS_OK, -1, NULL);
|
|
|
|
conn->chunk_printf(conn, "I'm Libuhttpd: %s\n", UHTTPD_VERSION_STRING);
|
|
|
|
conn->chunk_printf(conn, "Method: %s\n", conn->get_method_str(conn));
|
2020-09-07 02:03:32 +00:00
|
|
|
conn->chunk_printf(conn, "Path: %.*s\n", path.len ,path.p);
|
|
|
|
conn->chunk_printf(conn, "Query: %.*s\n", query.len, query.p);
|
|
|
|
conn->chunk_printf(conn, "User-Agent: %.*s\n", ua.len, ua.p);
|
|
|
|
conn->chunk_printf(conn, "Body: %.*s\n", body.len, body.p);
|
2020-07-02 02:32:19 +00:00
|
|
|
conn->chunk_end(conn);
|
2020-09-08 14:37:36 +00:00
|
|
|
conn->done(conn);
|
2020-07-02 02:32:19 +00:00
|
|
|
} else {
|
2021-01-01 08:53:14 +00:00
|
|
|
conn->serve_file(conn);
|
2020-07-02 02:32:19 +00:00
|
|
|
}
|
2019-08-27 02:21:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:53:36 +00:00
|
|
|
static void upload_handler(struct uh_connection *conn, int event)
|
|
|
|
{
|
|
|
|
static int fd = -1;
|
|
|
|
|
2020-12-26 09:30:28 +00:00
|
|
|
if (event == UH_EV_HEAD_COMPLETE) {
|
|
|
|
struct uh_str str = conn->get_header(conn, "Content-Length");
|
|
|
|
int content_length;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
sprintf(buf, "%.*s\n", (int)str.len, str.p);
|
|
|
|
|
|
|
|
content_length = atoi(buf);
|
|
|
|
|
|
|
|
if (content_length > 1024 * 1024 * 1024) {
|
|
|
|
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, "Too big");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} if (event == UH_EV_BODY) {
|
2020-12-16 16:53:36 +00:00
|
|
|
struct uh_str body = conn->extract_body(conn);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
fd = open("upload.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, body.p, body.len) < 0) {
|
|
|
|
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2020-12-26 09:30:28 +00:00
|
|
|
} else if (event == UH_EV_COMPLETE) {
|
2020-12-16 16:53:36 +00:00
|
|
|
struct stat st;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
conn->send_head(conn, HTTP_STATUS_OK, -1, NULL);
|
|
|
|
|
|
|
|
if (fd > 0) {
|
|
|
|
fstat(fd, &st);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = -1;
|
|
|
|
size = st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->chunk_printf(conn, "Upload size: %zd\n", size);
|
|
|
|
conn->chunk_end(conn);
|
|
|
|
conn->done(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:21:37 +00:00
|
|
|
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
|
|
|
|
{
|
|
|
|
if (w->signum == SIGINT) {
|
|
|
|
ev_break(loop, EVBREAK_ALL);
|
|
|
|
uh_log_info("Normal quit\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(const char *prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [option]\n"
|
2021-01-01 08:53:14 +00:00
|
|
|
" -h docroot # Document root, default is .\n"
|
|
|
|
" -i index_page # Index page, default is index.html\n"
|
|
|
|
" -a addr # Default addr is localhost\n"
|
|
|
|
" -p port # Default port is 8080\n"
|
|
|
|
" -s # SSl on\n"
|
|
|
|
" -f # Serve file\n"
|
|
|
|
" -P # plugin path\n"
|
|
|
|
" -v # verbose\n", prog);
|
2019-08-27 02:21:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct ev_loop *loop = EV_DEFAULT;
|
|
|
|
struct ev_signal signal_watcher;
|
|
|
|
struct uh_server *srv = NULL;
|
2020-12-19 16:13:29 +00:00
|
|
|
const char *plugin_path = NULL;
|
2019-08-27 02:21:37 +00:00
|
|
|
bool verbose = false;
|
|
|
|
bool ssl = false;
|
2021-01-01 08:53:14 +00:00
|
|
|
const char *docroot = ".";
|
|
|
|
const char *index_page = "index.html";
|
2020-12-19 16:13:29 +00:00
|
|
|
const char *addr = "localhost";
|
2019-08-27 02:21:37 +00:00
|
|
|
int port = 8080;
|
|
|
|
int opt;
|
|
|
|
|
2021-01-01 08:53:14 +00:00
|
|
|
while ((opt = getopt(argc, argv, "h:i:a:p:sfP:v")) != -1) {
|
2020-03-13 15:09:48 +00:00
|
|
|
switch (opt) {
|
2021-01-01 08:53:14 +00:00
|
|
|
case 'h':
|
|
|
|
docroot = optarg;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
index_page = optarg;
|
|
|
|
break;
|
2020-12-19 16:13:29 +00:00
|
|
|
case 'a':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
2019-08-27 02:21:37 +00:00
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
2020-03-13 15:09:48 +00:00
|
|
|
break;
|
2019-08-27 02:21:37 +00:00
|
|
|
case 's':
|
|
|
|
ssl = true;
|
2020-07-02 02:09:12 +00:00
|
|
|
break;
|
2020-07-02 02:32:19 +00:00
|
|
|
case 'f':
|
|
|
|
serve_file = true;
|
|
|
|
break;
|
2020-12-19 16:13:29 +00:00
|
|
|
case 'P':
|
|
|
|
plugin_path = optarg;
|
|
|
|
break;
|
2019-08-27 02:21:37 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default: /* '?' */
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!verbose)
|
|
|
|
uh_log_threshold(LOG_ERR);
|
2020-03-13 15:09:48 +00:00
|
|
|
|
2019-08-27 02:21:37 +00:00
|
|
|
uh_log_info("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
|
|
|
|
|
2020-09-08 14:41:03 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2020-12-19 16:13:29 +00:00
|
|
|
srv = uh_server_new(loop, addr, port);
|
2019-08-27 02:21:37 +00:00
|
|
|
if (!srv)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#if UHTTPD_SSL_SUPPORT
|
|
|
|
if (ssl && srv->ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
|
|
|
|
goto err;
|
|
|
|
#endif
|
|
|
|
|
2021-01-03 08:19:24 +00:00
|
|
|
srv->start_worker(srv, -1); /* -1 means automatically to available CPUs */
|
|
|
|
|
2021-01-01 08:53:14 +00:00
|
|
|
srv->set_docroot(srv, docroot);
|
|
|
|
srv->set_index_page(srv, index_page);
|
|
|
|
|
2021-01-01 10:16:07 +00:00
|
|
|
srv->set_default_handler(srv, default_handler);
|
2020-12-16 16:53:36 +00:00
|
|
|
|
|
|
|
srv->add_path_handler(srv, "/upload", upload_handler);
|
2019-08-27 02:21:37 +00:00
|
|
|
|
2020-12-19 16:13:29 +00:00
|
|
|
if (plugin_path)
|
|
|
|
srv->load_plugin(srv, plugin_path);
|
2019-08-27 02:21:37 +00:00
|
|
|
|
|
|
|
ev_signal_init(&signal_watcher, signal_cb, SIGINT);
|
|
|
|
ev_signal_start(loop, &signal_watcher);
|
|
|
|
|
|
|
|
ev_run(loop, 0);
|
|
|
|
|
|
|
|
err:
|
|
|
|
srv->free(srv);
|
|
|
|
free(srv);
|
2020-03-13 15:09:48 +00:00
|
|
|
|
2020-10-06 09:56:01 +00:00
|
|
|
ev_loop_destroy(loop);
|
|
|
|
|
2019-08-27 02:21:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|