Skip to content

Commit

Permalink
configure the docroot and index page globally.
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Jan 1, 2021
1 parent 8931fb6 commit 2fd658c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
31 changes: 21 additions & 10 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
#include "uhttpd.h"

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)
{
Expand All @@ -56,7 +54,7 @@ static void default_handler(struct uh_connection *conn, int event)
conn->chunk_end(conn);
conn->done(conn);
} else {
conn->serve_file(conn, docroot, index_page);
conn->serve_file(conn);
}
}

Expand Down Expand Up @@ -126,12 +124,14 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\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);
" -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);
exit(1);
}

Expand All @@ -143,12 +143,20 @@ int main(int argc, char **argv)
const char *plugin_path = NULL;
bool verbose = false;
bool ssl = false;
const char *docroot = ".";
const char *index_page = "index.html";
const char *addr = "localhost";
int port = 8080;
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) {
case 'h':
docroot = optarg;
break;
case 'i':
index_page = optarg;
break;
case 'a':
addr = optarg;
break;
Expand Down Expand Up @@ -188,6 +196,9 @@ int main(int argc, char **argv)
goto err;
#endif

srv->set_docroot(srv, docroot);
srv->set_index_page(srv, index_page);

srv->default_handler = default_handler;

srv->add_path_handler(srv, "/upload", upload_handler);
Expand Down
2 changes: 1 addition & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct uh_connection {
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 (*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_printf)(struct uh_connection *conn, const char *format, ...);
void (*chunk_vprintf)(struct uh_connection *conn, const char *format, va_list arg);
Expand Down
7 changes: 5 additions & 2 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#include <fcntl.h>
#include <inttypes.h>

#include "connection.h"
#include "mimetypes.h"
#include "uhttpd.h"
#include "log.h"

static const char *file_mktag(struct stat *s, char *buf, int len)
Expand Down Expand Up @@ -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");
}

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);
struct uh_server *srv = conn->srv;
const char *docroot = srv->docroot;
const char *index_page = srv->index_page;
static char fullpath[512];
const char *mime;
struct stat st;
Expand Down
2 changes: 1 addition & 1 deletion src/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@

#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
37 changes: 37 additions & 0 deletions src/uhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ static void uh_server_free(struct uh_server *srv)
if (srv->sock > 0)
close(srv->sock);

if (srv->docroot)
free(srv->docroot);

if (srv->index_page)
free(srv->index_page);

while (conn) {
struct uh_connection *next = conn->next;
conn_free(conn);
Expand Down Expand Up @@ -218,6 +224,34 @@ static int uh_add_path_handler(struct uh_server *srv, const char *path, uh_path_
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)
{
union {
Expand Down Expand Up @@ -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->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_start(srv->loop, &srv->ior);

Expand Down
4 changes: 4 additions & 0 deletions src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct uh_path_handler {

struct uh_server {
int sock;
char *docroot;
char *index_page;
struct ev_loop *loop;
struct ev_io ior;
struct uh_connection *conns;
Expand All @@ -71,6 +73,8 @@ struct uh_server {
int (*load_plugin)(struct uh_server *srv, const char *path);
struct uh_path_handler *handlers;
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);
};

/*
Expand Down

0 comments on commit 2fd658c

Please sign in to comment.