enhanced cmdline arguments in hio-webs.c

This commit is contained in:
hyung-hwan 2022-12-22 16:25:20 +00:00
parent 9e135dbbd7
commit b4cadfeed3
2 changed files with 107 additions and 37 deletions

View File

@ -1,16 +1,26 @@
#include <hio-http.h> #include <hio-http.h>
#include <hio-tar.h> #include <hio-tar.h>
#include <hio-opt.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
struct arg_info_t
{
const char* laddrs;
const char* docroot;
int file_list_dir;
};
typedef struct arg_info_t arg_info_t;
struct htts_ext_t struct htts_ext_t
{ {
const hio_bch_t* docroot; arg_info_t* ai;
}; };
typedef struct htts_ext_t htts_ext_t; typedef struct htts_ext_t htts_ext_t;
void untar (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) void untar (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx)
{ {
FILE* wfp = HIO_NULL; FILE* wfp = HIO_NULL;
@ -35,7 +45,7 @@ void untar (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t*
goto done; goto done;
} }
hio_tar_setxrootwithbcstr (tar, ext->docroot); hio_tar_setxrootwithbcstr (tar, ext->ai->docroot);
while (1) while (1)
{ {
@ -85,7 +95,9 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_
{ {
/* TODO: proper mime-type */ /* TODO: proper mime-type */
/* TODO: make HIO_SVC_HTTS_FILE_DIR a cli option */ /* TODO: make HIO_SVC_HTTS_FILE_DIR a cli option */
if (hio_svc_htts_dofile(htts, csck, req, ext->docroot, qpath, HIO_NULL, HIO_SVC_HTTS_FILE_LIST_DIR, HIO_NULL) <= -1) goto oops; 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, HIO_NULL) <= -1) goto oops;
} }
#if 0 #if 0
else else
@ -100,7 +112,7 @@ oops:
return 0; return 0;
} }
int webs_start (hio_t* hio, const hio_bch_t* addrs, const hio_bch_t* docroot) int webs_start (hio_t* hio, const arg_info_t* ai)
{ {
const hio_bch_t* ptr, * end; const hio_bch_t* ptr, * end;
hio_bcs_t tok; hio_bcs_t tok;
@ -110,7 +122,7 @@ int webs_start (hio_t* hio, const hio_bch_t* addrs, const hio_bch_t* docroot)
htts_ext_t* ext; htts_ext_t* ext;
bic = 0; bic = 0;
ptr = addrs; ptr = ai->laddrs;
end = ptr + hio_count_bcstr(ptr); end = ptr + hio_count_bcstr(ptr);
while (ptr) while (ptr)
{ {
@ -133,7 +145,7 @@ int webs_start (hio_t* hio, const hio_bch_t* addrs, const hio_bch_t* docroot)
if (!webs) return -1; /* TODO: logging */ if (!webs) return -1; /* TODO: logging */
ext = hio_svc_htts_getxtn(webs); ext = hio_svc_htts_getxtn(webs);
ext->docroot = docroot; ext->ai = ai;
return 0; return 0;
} }
@ -145,37 +157,68 @@ static void handle_sigint (int sig)
if (g_hio) hio_stop (g_hio, HIO_STOPREQ_TERMINATION); if (g_hio) hio_stop (g_hio, HIO_STOPREQ_TERMINATION);
} }
static int process_args (int argc, char* argv[], arg_info_t* ai)
{
static hio_bopt_lng_t lopt[] =
{
{ "file-list-dir", '\0' }
};
static hio_bopt_t opt =
{
"",
&lopt
};
hio_bci_t c;
if (argc < 3)
{
print_usage:
fprintf (stderr, "Usage: %s [options] listen-address:port docroot-dir\n", argv[0]);
return -1;
}
memset (ai, 0, HIO_SIZEOF(*ai));
while ((c = hio_getbopt(argc, argv, &opt)) != HIO_BCI_EOF)
{
switch (c)
{
case '\0':
if (strcasecmp(opt.lngopt, "file-list-dir") == 0)
{
ai->file_list_dir = 1;
break;
}
goto print_usage;
case ':':
if (opt.lngopt)
fprintf (stderr, "bad argument for '%s'\n", opt.lngopt);
else
fprintf (stderr, "bad argument for '%c'\n", opt.opt);
return -1;
default:
goto print_usage;
}
}
if (argc - opt.ind != 2) goto print_usage;
ai->laddrs = argv[opt.ind++];
ai->docroot = argv[opt.ind++];
return 0;
}
int main (int argc, char* argv[]) int main (int argc, char* argv[])
{ {
hio_t* hio = HIO_NULL; hio_t* hio = HIO_NULL;
struct sigaction sigact; struct sigaction sigact;
arg_info_t ai;
int xret = -1; int xret = -1;
#if 0 if (process_args(argc, argv, &ai) <= -1) return -1;
hio_oow_t i;
// TODO: use getopt() or something similar
for (i = 1; i < argc; )
{
if (strcmp(argv[i], "-s") == 0)
{
i++;
g_dev_type4 = HIO_DEV_SCK_SCTP4;
g_dev_type6 = HIO_DEV_SCK_SCTP6;
}
else
{
printf ("Error: invalid argument %s\n", argv[i]);
return -1;
}
}
#else
if (argc < 3)
{
printf ("Error: %s listen-address doc-root\n", hio_get_base_name_bcstr(argv[0]));
return -1;
}
#endif
memset (&sigact, 0, HIO_SIZEOF(sigact)); memset (&sigact, 0, HIO_SIZEOF(sigact));
sigact.sa_handler = SIG_IGN; sigact.sa_handler = SIG_IGN;
@ -202,7 +245,7 @@ int main (int argc, char* argv[])
g_hio = hio; g_hio = hio;
if (webs_start(hio, argv[1], argv[2]) <= -1) goto oops; if (webs_start(hio, &ai) <= -1) goto oops;
hio_loop (hio); hio_loop (hio);

View File

@ -1,8 +1,6 @@
#!/bin/sh #!/bin/sh
##srcdir=$(dirname "$0") [ -z "$srcdir" ] && srcdir=$(dirname "$0")
##srcdir="${0%/*}"
[ -z "$srcdir" ]
. "${srcdir}/tap.inc" . "${srcdir}/tap.inc"
test_default_index() test_default_index()
@ -10,10 +8,13 @@ test_default_index()
local msg="hi-webs default index.html" local msg="hi-webs default index.html"
local srvaddr=127.0.0.1:54321 local srvaddr=127.0.0.1:54321
local tmpdir="/tmp/s-001.$$" 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 "${srvaddr}" "${tmpdir}" 2>/dev/null &
local jid=$! local jid=$!
mkdir -p "${tmpdir}"
cat >"${tmpdir}/index.html" <<EOF cat >"${tmpdir}/index.html" <<EOF
<html> <html>
<body> <body>
@ -21,10 +22,12 @@ hello
</body> </body>
</html> </html>
EOF EOF
sleep 0.5 sleep 0.5
local hc=$(curl -s -w '%{http_code}\n' -o /dev/null "http://${srvaddr}") local hc=$(curl -s -w '%{http_code}\n' -o /dev/null "http://${srvaddr}")
tap_ensure "$hc" "200" "$msg - got $hc" tap_ensure "$hc" "200" "$msg - got $hc"
## check if 404 is returned if the document root is not found
rm -rf "${tmpdir}" rm -rf "${tmpdir}"
local hc=$(curl -s -w '%{http_code}\n' -o /dev/null "http://${srvaddr}") local hc=$(curl -s -w '%{http_code}\n' -o /dev/null "http://${srvaddr}")
tap_ensure "$hc" "404" "$msg - got $hc" tap_ensure "$hc" "404" "$msg - got $hc"
@ -33,5 +36,29 @@ EOF
wait ${jid} wait ${jid}
} }
test_file_list_dir()
{
local msg="hi-webs file-list-dir"
local srvaddr=127.0.0.1:54321
local tmpdir="/tmp/s-001.$$"
mkdir -p "${tmpdir}"
## check directory listing against an empty directory
../bin/hio-webs --file-list-dir "${srvaddr}" "${tmpdir}" 2>/dev/null &
local jid=$!
sleep 0.5
local hc=$(curl -s -w '%{http_code}\n' -o /dev/null "http://${srvaddr}")
tap_ensure "$hc" "200" "$msg - got $hc"
rm -rf "${tmpdir}"
kill -TERM ${jid}
wait ${jid}
}
test_default_index test_default_index
tap_end test_file_list_dir
tap_end