configure the docroot and index page globally.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao committed 2021-01-01 16:53:14 +08:00
1 parent 8931fb63e1
commit 2fd658c347
6 files changed
+69 -14

No files matched your search

+1 -1
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 (*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);
+5 -2
View File
@@ -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)
@@ -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;
+1 -1
View File
@@ -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
View File
@@ -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);
@@ -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 {
@@ -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);
+4
View File
@@ -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;
@@ -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);
};
/*