simplified the directory handling callback
This commit is contained in:
parent
8613cbffc4
commit
34e9e03b8c
131
bin/webs.c
131
bin/webs.c
@ -6,12 +6,18 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct arg_info_t
|
||||
{
|
||||
const char* laddrs;
|
||||
const char* docroot;
|
||||
int file_list_dir;
|
||||
int file_load_index_page;
|
||||
};
|
||||
typedef struct arg_info_t arg_info_t;
|
||||
|
||||
@ -76,43 +82,95 @@ done:
|
||||
}
|
||||
}
|
||||
|
||||
static void bfmt_dir (hio_svc_htts_t* htts, int fd, const hio_bch_t* qpath, hio_svc_htts_file_bfmt_dir_type_t type, const hio_bch_t* name, void* ctx)
|
||||
static const hio_bch_t* file_get_mime_type (hio_svc_htts_t* htts, const hio_bch_t* qpath, const hio_bch_t* file_path, void* ctx)
|
||||
{
|
||||
/* TODO: do bufferring */
|
||||
/* "<a href="urlencoded-name">name</a> */
|
||||
if (type == HIO_SVC_HTTS_FILE_BFMT_DIR_HEADER)
|
||||
{
|
||||
write (fd, "<html><body>", 12);
|
||||
const hio_bch_t* mt = HIO_NULL;
|
||||
const hio_bch_t* dot;
|
||||
dot = hio_rfind_bchar_in_bcstr(file_path, '.');
|
||||
if (dot) mt = hio_get_mime_type_by_ext(dot + 1);
|
||||
return mt;
|
||||
}
|
||||
|
||||
}
|
||||
else if (type == HIO_SVC_HTTS_FILE_BFMT_DIR_FOOTER)
|
||||
{
|
||||
write (fd, "</body></html>", 14);
|
||||
}
|
||||
else
|
||||
{
|
||||
HIO_ASSERT (hio_svc_htts_gethio(htts), name != HIO_NULL);
|
||||
static int file_open_dir_list (hio_svc_htts_t* htts, const hio_bch_t* qpath, const hio_bch_t* dir_path, const hio_bch_t** res_mime_type, void* ctx)
|
||||
{
|
||||
htts_ext_t* ext = hio_svc_htts_getxtn(htts);
|
||||
hio_t* hio = hio_svc_htts_gethio(htts);
|
||||
DIR* dp = HIO_NULL;
|
||||
hio_bch_t file_path[] = "/tmp/.XXXXXX";
|
||||
int fd = -1;
|
||||
struct dirent* de;
|
||||
|
||||
if (name[0] == '.' && name[1] == '\0') return;
|
||||
if (ext->ai->file_load_index_page)
|
||||
{
|
||||
const hio_bch_t* index_path;
|
||||
|
||||
if (qpath[0] == '\0' || (qpath[0] == '/' && qpath[1] == '\0'))
|
||||
index_path = hio_svc_htts_dupmergepaths(htts, dir_path, "index.html");
|
||||
if (HIO_UNLIKELY(!index_path)) goto oops;
|
||||
|
||||
fd = open(index_path, O_RDONLY, 0644);
|
||||
if (fd >= 0)
|
||||
{
|
||||
/* at the root */
|
||||
if (name[0] == '.' && name[1] == '.' && name[2] == '\0') return;
|
||||
if (res_mime_type)
|
||||
{
|
||||
const hio_bch_t* mt;
|
||||
mt = file_get_mime_type(htts, qpath, index_path, ctx);
|
||||
if (mt) *res_mime_type = mt;
|
||||
}
|
||||
hio_freemem (hio, index_path);
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* TODO: get the directory name
|
||||
check if the entry is a directory or something else
|
||||
do escaping...
|
||||
show file size?
|
||||
append / if the file is a directory
|
||||
*/
|
||||
hio_freemem (hio, index_path);
|
||||
}
|
||||
|
||||
if (!ext->ai->file_list_dir) goto oops;
|
||||
|
||||
dp = opendir(dir_path);
|
||||
if (!dp) goto oops;
|
||||
|
||||
/* TOOD: mkostemp instead and specify O_CLOEXEC and O_LARGEFILE? */
|
||||
fd = mkstemp(file_path);
|
||||
if (fd <= -1) goto oops;
|
||||
|
||||
unlink (file_path);
|
||||
|
||||
write (fd, "<html><body>", 12);
|
||||
if (qpath[0] == '\0' || (qpath[0] == '/' && qpath[1] == '\0'))
|
||||
write (fd, "<li><a href=\"..\">..</a>", 23);
|
||||
|
||||
/* TODO: sorting, other informatino like size, */
|
||||
/* TODO: error handling of write() error */
|
||||
while ((de = readdir(dp)))
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
|
||||
(de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')) continue;
|
||||
|
||||
if (stat(de->d_name, &st) <= -1) continue;
|
||||
|
||||
write (fd, "<li><a href=\"", 13);
|
||||
write (fd, name, strlen(name));
|
||||
write (fd, de->d_name, strlen(de->d_name)); /* TOOD: url escaping*/
|
||||
if (S_ISDIR(st.st_mode)) write (fd, "/", 1);
|
||||
write (fd, "\">", 2);
|
||||
write (fd, name, strlen(name));
|
||||
write (fd, de->d_name, strlen(de->d_name));
|
||||
if (S_ISDIR(st.st_mode)) write (fd, "/", 1);
|
||||
write (fd, "</a>", 4);
|
||||
}
|
||||
|
||||
write (fd, "</body></html>\n", 15);
|
||||
|
||||
closedir (dp);
|
||||
lseek (fd, SEEK_SET, 0);
|
||||
|
||||
done:
|
||||
if (res_mime_type) *res_mime_type = "text/html";
|
||||
return fd;
|
||||
|
||||
oops:
|
||||
if (fd >= 0) close (fd);
|
||||
if (dp) closedir (dp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req)
|
||||
@ -122,7 +180,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_
|
||||
hio_http_method_t mth;
|
||||
const hio_bch_t* qpath;
|
||||
|
||||
static hio_svc_htts_file_cbs_t fcbs = { bfmt_dir, HIO_NULL };
|
||||
static hio_svc_htts_file_cbs_t fcbs = { file_get_mime_type, file_open_dir_list, HIO_NULL };
|
||||
|
||||
hio_htre_perdecqpath (req);
|
||||
|
||||
@ -138,9 +196,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_
|
||||
{
|
||||
/* TODO: proper mime-type */
|
||||
/* TODO: make HIO_SVC_HTTS_FILE_DIR a cli option */
|
||||
int fopts = 0;
|
||||
if (ext->ai->file_list_dir) fopts |= HIO_SVC_HTTS_FILE_LIST_DIR;
|
||||
if (hio_svc_htts_dofile(htts, csck, req, ext->ai->docroot, qpath, HIO_NULL, fopts, &fcbs) <= -1) goto oops;
|
||||
if (hio_svc_htts_dofile(htts, csck, req, ext->ai->docroot, qpath, HIO_NULL, 0, &fcbs) <= -1) goto oops;
|
||||
}
|
||||
#if 0
|
||||
else
|
||||
@ -204,7 +260,9 @@ static int process_args (int argc, char* argv[], arg_info_t* ai)
|
||||
{
|
||||
static hio_bopt_lng_t lopt[] =
|
||||
{
|
||||
{ "file-list-dir", '\0' }
|
||||
{ "file-no-list-dir", '\0' },
|
||||
{ "file-no-load-index-page", '\0'},
|
||||
{ HIO_NULL, '\0'}
|
||||
};
|
||||
static hio_bopt_t opt =
|
||||
{
|
||||
@ -222,15 +280,22 @@ static int process_args (int argc, char* argv[], arg_info_t* ai)
|
||||
}
|
||||
|
||||
memset (ai, 0, HIO_SIZEOF(*ai));
|
||||
ai->file_list_dir = 1;
|
||||
ai->file_load_index_page = 1;
|
||||
|
||||
while ((c = hio_getbopt(argc, argv, &opt)) != HIO_BCI_EOF)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '\0':
|
||||
if (strcasecmp(opt.lngopt, "file-list-dir") == 0)
|
||||
if (strcasecmp(opt.lngopt, "file-no-list-dir") == 0)
|
||||
{
|
||||
ai->file_list_dir = 1;
|
||||
ai->file_list_dir = 0;
|
||||
break;
|
||||
}
|
||||
else if (strcasecmp(opt.lngopt, "file-no-load-index-page") == 0)
|
||||
{
|
||||
ai->file_load_index_page = 0;
|
||||
break;
|
||||
}
|
||||
goto print_usage;
|
||||
|
@ -145,8 +145,7 @@ enum hio_svc_htts_cgi_option_t
|
||||
enum hio_svc_htts_file_option_t
|
||||
{
|
||||
HIO_SVC_HTTS_FILE_NO_100_CONTINUE = (1 << 0),
|
||||
HIO_SVC_HTTS_FILE_READ_ONLY = (1 << 1),
|
||||
HIO_SVC_HTTS_FILE_LIST_DIR = (1 << 2)
|
||||
HIO_SVC_HTTS_FILE_READ_ONLY = (1 << 1)
|
||||
};
|
||||
|
||||
enum hio_svc_htts_thr_option_t
|
||||
@ -171,7 +170,8 @@ typedef enum hio_svc_htts_file_bfmt_dir_type_t hio_svc_htts_file_bfmt_dir_type_t
|
||||
|
||||
struct hio_svc_htts_file_cbs_t
|
||||
{
|
||||
int (*bfmt_dir) (hio_svc_htts_t* htts, int fd, const hio_bch_t* qpath, hio_svc_htts_file_bfmt_dir_type_t type, const hio_bch_t* name, void* ctx);
|
||||
const hio_bch_t* (*get_mime_type) (hio_svc_htts_t* htts, const hio_bch_t* qpath, const hio_bch_t* file_path, void* ctx);
|
||||
int (*open_dir_list) (hio_svc_htts_t* htts, const hio_bch_t* qpath, const hio_bch_t* dir_path, const hio_bch_t** res_mime_type, void* ctx);
|
||||
void *ctx;
|
||||
};
|
||||
typedef struct hio_svc_htts_file_cbs_t hio_svc_htts_file_cbs_t;
|
||||
|
106
lib/http-file.c
106
lib/http-file.c
@ -33,9 +33,6 @@
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH
|
||||
|
||||
@ -158,11 +155,11 @@ static int file_send_final_status_to_client (file_t* file, int status_code, int
|
||||
if (dir_redirect)
|
||||
{
|
||||
/* don't send content body when the status code is 3xx. include the Location header only. */
|
||||
if (hio_becs_fcat(cli->sbuf, "Content-Length: 0\r\nLocation: %hs/\r\n\r\n", file->req_qpath) == (hio_oow_t)-1) return -1;
|
||||
if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: 0\r\nLocation: %hs/\r\n\r\n", file->req_qpath) == (hio_oow_t)-1) return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hio_becs_fcat(cli->sbuf, "Content-Length: %zu\r\n\r\n%hs", hio_count_bcstr(status_msg), status_msg) == (hio_oow_t)-1) return -1;
|
||||
if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", hio_count_bcstr(status_msg), status_msg) == (hio_oow_t)-1) return -1;
|
||||
}
|
||||
|
||||
return (file_write_to_client(file, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 ||
|
||||
@ -698,65 +695,9 @@ static HIO_INLINE int process_range_header (file_t* file, hio_htre_t* req, int*
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int open_peer_for_dir_listing (file_t* file, const hio_bch_t* dir_path, int* new_fd, hio_bch_t** new_file)
|
||||
{
|
||||
DIR* dp = HIO_NULL;
|
||||
hio_bch_t* alt_file = HIO_NULL;
|
||||
int alt_fd = -1;
|
||||
struct dirent* de;
|
||||
|
||||
dp = opendir(dir_path);
|
||||
if (!dp) goto oops;
|
||||
|
||||
alt_file = hio_dupbcstr(file->htts->hio, "/tmp/.XXXXXX", HIO_NULL);
|
||||
if (!alt_file) goto oops;
|
||||
|
||||
/* TOOD: mkostemp instead and specify O_CLOEXEC and O_LARGEFILE? */
|
||||
alt_fd = mkstemp(alt_file);
|
||||
if (alt_fd <= -1) goto oops;
|
||||
|
||||
unlink (alt_file);
|
||||
if (file->cbs && file->cbs->bfmt_dir)
|
||||
{
|
||||
/* TODO: can support sorting?? */
|
||||
file->cbs->bfmt_dir (file->htts, alt_fd, file->req_qpath, HIO_SVC_HTTS_FILE_BFMT_DIR_HEADER, HIO_NULL, file->cbs->ctx);
|
||||
while ((de = readdir(dp)))
|
||||
{
|
||||
file->cbs->bfmt_dir (file->htts, alt_fd, file->req_qpath, HIO_SVC_HTTS_FILE_BFMT_DIR_ENTRY, de->d_name, file->cbs->ctx);
|
||||
}
|
||||
file->cbs->bfmt_dir (file->htts, alt_fd, file->req_qpath, HIO_SVC_HTTS_FILE_BFMT_DIR_FOOTER, HIO_NULL, file->cbs->ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((de = readdir(dp)))
|
||||
{
|
||||
if (strcmp(de->d_name, ".") != 0)
|
||||
{
|
||||
write (alt_fd, de->d_name, strlen(de->d_name));
|
||||
write (alt_fd, "\n", 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir (dp);
|
||||
|
||||
lseek (alt_fd, SEEK_SET, 0);
|
||||
*new_fd = alt_fd;
|
||||
*new_file = alt_file;
|
||||
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
if (alt_fd >= 0) close (alt_fd);
|
||||
if (alt_file) hio_freemem (file->htts->hio, alt_file);
|
||||
if (dp) closedir (dp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int flags, int* error_status, const hio_bch_t** actual_mime_type)
|
||||
static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int flags, int* error_status, const hio_bch_t** res_mime_type)
|
||||
{
|
||||
struct stat st;
|
||||
const hio_bch_t* opened_file;
|
||||
|
||||
flags |= O_NONBLOCK;
|
||||
#if defined(O_CLOEXEC)
|
||||
@ -773,10 +714,9 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int
|
||||
return -1;
|
||||
}
|
||||
|
||||
opened_file = actual_file;
|
||||
if ((flags | O_RDONLY) && fstat(file->peer, &st) >= 0 && S_ISDIR(st.st_mode)) /* only for read operation */
|
||||
if (((flags | O_RDONLY) && fstat(file->peer, &st) >= 0 && S_ISDIR(st.st_mode)) && /* only for read operation */
|
||||
(file->cbs && file->cbs->open_dir_list)) /* directory listing is enabled */
|
||||
{
|
||||
hio_bch_t* alt_file;
|
||||
int alt_fd;
|
||||
|
||||
if (!file->req_qpath_ending_with_slash)
|
||||
@ -787,41 +727,23 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int
|
||||
return -1;
|
||||
}
|
||||
|
||||
alt_file = hio_svc_htts_dupmergepaths(file->htts, actual_file, "index.html"); /* TODO: make the default index files configurable */
|
||||
if (alt_file)
|
||||
alt_fd = file->cbs->open_dir_list(file->htts, file->req_qpath, actual_file, res_mime_type, file->cbs->ctx);
|
||||
if (alt_fd >= 0)
|
||||
{
|
||||
alt_fd = open(alt_file, flags, 0644);
|
||||
if (alt_fd <= -1)
|
||||
{
|
||||
hio_freemem (file->htts->hio, alt_file);
|
||||
if ((file->options & HIO_SVC_HTTS_FILE_LIST_DIR) &&
|
||||
open_peer_for_dir_listing(file, actual_file, &alt_fd, &alt_file) >= 0) goto open_ok;
|
||||
}
|
||||
else
|
||||
{
|
||||
open_ok:
|
||||
close (file->peer);
|
||||
file->peer = alt_fd;
|
||||
opened_file = alt_file;
|
||||
}
|
||||
close (file->peer);
|
||||
file->peer = alt_fd;
|
||||
}
|
||||
}
|
||||
|
||||
if (actual_mime_type)
|
||||
else
|
||||
{
|
||||
const hio_bch_t* dot;
|
||||
dot = hio_rfind_bchar_in_bcstr(opened_file, '.');
|
||||
if (dot)
|
||||
if (res_mime_type && file->cbs && file->cbs->get_mime_type)
|
||||
{
|
||||
const hio_bch_t* mt;
|
||||
mt = hio_get_mime_type_by_ext(dot + 1);
|
||||
if (mt) *actual_mime_type = mt;
|
||||
const hio_bch_t* mime_type;
|
||||
mime_type = file->cbs->get_mime_type(file->htts, file->req_qpath, actual_file, file->cbs->ctx);
|
||||
if (mime_type) *res_mime_type = mime_type;
|
||||
}
|
||||
}
|
||||
|
||||
if (opened_file != actual_file)
|
||||
hio_freemem (file->htts->hio, (hio_bch_t*)opened_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -5,14 +5,14 @@
|
||||
|
||||
test_default_index()
|
||||
{
|
||||
local msg="hio-webs default index.html"
|
||||
local msg="hio-webs default index.html under a directory"
|
||||
local srvaddr=127.0.0.1:54321
|
||||
local tmpdir="/tmp/s-001.$$"
|
||||
|
||||
mkdir -p "${tmpdir}"
|
||||
|
||||
## check if index.html is retrieved
|
||||
../bin/hio-webs "${srvaddr}" "${tmpdir}" 2>/dev/null &
|
||||
../bin/hio-webs --file-no-list-dir "${srvaddr}" "${tmpdir}" 2>/dev/null &
|
||||
local jid=$!
|
||||
|
||||
cat >"${tmpdir}/index.html" <<EOF
|
||||
@ -46,7 +46,7 @@ test_file_list_dir()
|
||||
mkdir -p "${tmpdir}"
|
||||
|
||||
## check directory listing against an empty directory
|
||||
../bin/hio-webs --file-list-dir "${srvaddr}" "${tmpdir}" 2>/dev/null &
|
||||
../bin/hio-webs "${srvaddr}" "${tmpdir}" 2>/dev/null &
|
||||
local jid=$!
|
||||
sleep 0.5
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user