configure the docroot and index page globally.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-01 16:53:14 +08:00
parent 8931fb63e1
commit 2fd658c347
6 changed files with 69 additions and 14 deletions

View File

@ -32,8 +32,6 @@
#include "uhttpd.h" #include "uhttpd.h"
static bool serve_file = false; static bool serve_file = false;
static const char *docroot = ".";
static const char *index_page = "index.html";
static void default_handler(struct uh_connection *conn, int event) static void default_handler(struct uh_connection *conn, int event)
{ {
@ -56,7 +54,7 @@ static void default_handler(struct uh_connection *conn, int event)
conn->chunk_end(conn); conn->chunk_end(conn);
conn->done(conn); conn->done(conn);
} else { } else {
conn->serve_file(conn, docroot, index_page); conn->serve_file(conn);
} }
} }
@ -126,6 +124,8 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
static void usage(const char *prog) static void usage(const char *prog)
{ {
fprintf(stderr, "Usage: %s [option]\n" fprintf(stderr, "Usage: %s [option]\n"
" -h docroot # Document root, default is .\n"
" -i index_page # Index page, default is index.html\n"
" -a addr # Default addr is localhost\n" " -a addr # Default addr is localhost\n"
" -p port # Default port is 8080\n" " -p port # Default port is 8080\n"
" -s # SSl on\n" " -s # SSl on\n"
@ -143,12 +143,20 @@ int main(int argc, char **argv)
const char *plugin_path = NULL; const char *plugin_path = NULL;
bool verbose = false; bool verbose = false;
bool ssl = false; bool ssl = false;
const char *docroot = ".";
const char *index_page = "index.html";
const char *addr = "localhost"; const char *addr = "localhost";
int port = 8080; int port = 8080;
int opt; int opt;
while ((opt = getopt(argc, argv, "a:p:sfP:v")) != -1) { while ((opt = getopt(argc, argv, "h:i:a:p:sfP:v")) != -1) {
switch (opt) { switch (opt) {
case 'h':
docroot = optarg;
break;
case 'i':
index_page = optarg;
break;
case 'a': case 'a':
addr = optarg; addr = optarg;
break; break;
@ -188,6 +196,9 @@ int main(int argc, char **argv)
goto err; goto err;
#endif #endif
srv->set_docroot(srv, docroot);
srv->set_index_page(srv, index_page);
srv->default_handler = default_handler; srv->default_handler = default_handler;
srv->add_path_handler(srv, "/upload", upload_handler); srv->add_path_handler(srv, "/upload", upload_handler);

View File

@ -114,7 +114,7 @@ struct uh_connection {
void (*send_head)(struct uh_connection *conn, int code, int content_length, const char *extra_headers); void (*send_head)(struct uh_connection *conn, int code, int content_length, const char *extra_headers);
void (*error)(struct uh_connection *conn, int code, const char *reason); void (*error)(struct uh_connection *conn, int code, const char *reason);
void (*redirect)(struct uh_connection *conn, int code, const char *location, ...); void (*redirect)(struct uh_connection *conn, int code, const char *location, ...);
void (*serve_file)(struct uh_connection *conn, const char *docroot, const char *index_page); void (*serve_file)(struct uh_connection *conn);
void (*chunk_send)(struct uh_connection *conn, const void *data, ssize_t len); void (*chunk_send)(struct uh_connection *conn, const void *data, ssize_t len);
void (*chunk_printf)(struct uh_connection *conn, const char *format, ...); void (*chunk_printf)(struct uh_connection *conn, const char *format, ...);
void (*chunk_vprintf)(struct uh_connection *conn, const char *format, va_list arg); void (*chunk_vprintf)(struct uh_connection *conn, const char *format, va_list arg);

View File

@ -35,8 +35,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include "connection.h"
#include "mimetypes.h" #include "mimetypes.h"
#include "uhttpd.h"
#include "log.h" #include "log.h"
static const char *file_mktag(struct stat *s, char *buf, int len) static const char *file_mktag(struct stat *s, char *buf, int len)
@ -152,9 +152,12 @@ static void file_if_gzip(struct uh_connection *conn, const char *path, const cha
conn->printf(conn, "Content-Encoding: gzip\r\n"); conn->printf(conn, "Content-Encoding: gzip\r\n");
} }
void serve_file(struct uh_connection *conn, const char *docroot, const char *index_page) void serve_file(struct uh_connection *conn)
{ {
const struct uh_str path = conn->get_path(conn); const struct uh_str path = conn->get_path(conn);
struct uh_server *srv = conn->srv;
const char *docroot = srv->docroot;
const char *index_page = srv->index_page;
static char fullpath[512]; static char fullpath[512];
const char *mime; const char *mime;
struct stat st; struct stat st;

View File

@ -27,6 +27,6 @@
#include "connection.h" #include "connection.h"
void serve_file(struct uh_connection *conn, const char *docroot, const char *index_page); void serve_file(struct uh_connection *conn);
#endif #endif

View File

@ -53,6 +53,12 @@ static void uh_server_free(struct uh_server *srv)
if (srv->sock > 0) if (srv->sock > 0)
close(srv->sock); close(srv->sock);
if (srv->docroot)
free(srv->docroot);
if (srv->index_page)
free(srv->index_page);
while (conn) { while (conn) {
struct uh_connection *next = conn->next; struct uh_connection *next = conn->next;
conn_free(conn); conn_free(conn);
@ -218,6 +224,34 @@ static int uh_add_path_handler(struct uh_server *srv, const char *path, uh_path_
return 0; return 0;
} }
static int uh_set_docroot(struct uh_server *srv, const char *path)
{
if (srv->docroot)
free(srv->docroot);
srv->docroot = strdup(path);
if (!srv->docroot) {
uh_log_err("strdup: %s\n", strerror(errno));
return -1;
}
return 0;
}
static int uh_set_index_page(struct uh_server *srv, const char *name)
{
if (srv->index_page)
free(srv->index_page);
srv->index_page = strdup(name);
if (!srv->index_page) {
uh_log_err("strdup: %s\n", strerror(errno));
return -1;
}
return 0;
}
int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host, int port) int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host, int port)
{ {
union { union {
@ -306,6 +340,9 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host
srv->add_path_handler = uh_add_path_handler; srv->add_path_handler = uh_add_path_handler;
srv->set_docroot = uh_set_docroot;
srv->set_index_page = uh_set_index_page;
ev_io_init(&srv->ior, uh_accept_cb, sock, EV_READ); ev_io_init(&srv->ior, uh_accept_cb, sock, EV_READ);
ev_io_start(srv->loop, &srv->ior); ev_io_start(srv->loop, &srv->ior);

View File

@ -58,6 +58,8 @@ struct uh_path_handler {
struct uh_server { struct uh_server {
int sock; int sock;
char *docroot;
char *index_page;
struct ev_loop *loop; struct ev_loop *loop;
struct ev_io ior; struct ev_io ior;
struct uh_connection *conns; struct uh_connection *conns;
@ -71,6 +73,8 @@ struct uh_server {
int (*load_plugin)(struct uh_server *srv, const char *path); int (*load_plugin)(struct uh_server *srv, const char *path);
struct uh_path_handler *handlers; struct uh_path_handler *handlers;
int (*add_path_handler)(struct uh_server *srv, const char *path, uh_path_handler_prototype handler); int (*add_path_handler)(struct uh_server *srv, const char *path, uh_path_handler_prototype handler);
int (*set_docroot)(struct uh_server *srv, const char *path);
int (*set_index_page)(struct uh_server *srv, const char *name);
}; };
/* /*