Compare commits

...

2 Commits

Author SHA1 Message Date
9e285bcc1e adding directory name formatter callback to hio_svc_htts_dofile() - not finished yet 2022-12-21 14:57:52 +00:00
85616b1658 added HIO_SVC_HTTS_FILE_LIST_DIR
updated the file handler to list directory if the option is set
2022-12-21 14:57:51 +00:00
8 changed files with 100 additions and 41 deletions

View File

@ -976,7 +976,7 @@ if (hio_htre_getcontentlen(req) > 0)
else if (hio_comp_bcstr_limited(qpath, "/cgi/", 5, 1) == 0) else if (hio_comp_bcstr_limited(qpath, "/cgi/", 5, 1) == 0)
x = hio_svc_htts_docgi(htts, csck, req, "", qpath + 4, 0); x = hio_svc_htts_docgi(htts, csck, req, "", qpath + 4, 0);
else else
x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0); x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL);
if (x <= -1) goto oops; if (x <= -1) goto oops;
} }
#if 0 #if 0

View File

@ -211,7 +211,7 @@ if (hio_htre_getcontentlen(req) > 0)
x = hio_svc_htts_dofcgi(htts, csck, req, &fcgis_addr, 0); x = hio_svc_htts_dofcgi(htts, csck, req, &fcgis_addr, 0);
} }
else else
x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0); x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL);
if (x <= -1) goto oops; if (x <= -1) goto oops;
} }
#if 0 #if 0

View File

@ -84,7 +84,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_
else // if (mth == HIO_HTTP_GET || mth == HIO_HTTP_POST) else // if (mth == HIO_HTTP_GET || mth == HIO_HTTP_POST)
{ {
/* TODO: proper mime-type */ /* TODO: proper mime-type */
if (hio_svc_htts_dofile(htts, csck, req, ext->docroot, qpath, HIO_NULL, 0) <= -1) goto oops; if (hio_svc_htts_dofile(htts, csck, req, ext->docroot, qpath, HIO_NULL, 0, HIO_NULL) <= -1) goto oops;
} }
#if 0 #if 0
else else

View File

@ -137,7 +137,6 @@ typedef void (*hio_svc_htts_thr_func_t) (
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
enum hio_svc_htts_cgi_option_t enum hio_svc_htts_cgi_option_t
{ {
HIO_SVC_HTTS_CGI_NO_100_CONTINUE = (1 << 0) HIO_SVC_HTTS_CGI_NO_100_CONTINUE = (1 << 0)
@ -145,8 +144,9 @@ enum hio_svc_htts_cgi_option_t
enum hio_svc_htts_file_option_t enum hio_svc_htts_file_option_t
{ {
HIO_SVC_HTTS_FILE_NO_100_CONTINUE = (1 << 0), HIO_SVC_HTTS_FILE_NO_100_CONTINUE = (1 << 0),
HIO_SVC_HTTS_FILE_READ_ONLY = (1 << 1) HIO_SVC_HTTS_FILE_READ_ONLY = (1 << 1),
HIO_SVC_HTTS_FILE_LIST_DIR = (1 << 2)
}; };
enum hio_svc_htts_thr_option_t enum hio_svc_htts_thr_option_t
@ -161,6 +161,12 @@ enum hio_svc_htts_txt_option_t
}; };
#endif #endif
struct hio_svc_htts_file_cbs_t
{
int (*bfmt_dir) (hio_svc_htts_t* htts, const hio_bch_t* name);
};
typedef struct hio_svc_htts_file_cbs_t hio_svc_htts_file_cbs_t;
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
@ -340,17 +346,18 @@ HIO_EXPORT int hio_svc_htts_dofcgi (
hio_dev_sck_t* csck, hio_dev_sck_t* csck,
hio_htre_t* req, hio_htre_t* req,
const hio_skad_t* fcgis_addr, const hio_skad_t* fcgis_addr,
int options int options /**< 0 or bitwise-Ored of #hio_svc_htts_file_option_t enumerators */
); );
HIO_EXPORT int hio_svc_htts_dofile ( HIO_EXPORT int hio_svc_htts_dofile (
hio_svc_htts_t* htts, hio_svc_htts_t* htts,
hio_dev_sck_t* csck, hio_dev_sck_t* csck,
hio_htre_t* req, hio_htre_t* req,
const hio_bch_t* docroot, const hio_bch_t* docroot,
const hio_bch_t* filepath, const hio_bch_t* filepath,
const hio_bch_t* mime_type, const hio_bch_t* mime_type,
int options int options,
hio_svc_htts_file_cbs_t* cbs
); );
HIO_EXPORT int hio_svc_htts_dothr ( HIO_EXPORT int hio_svc_htts_dothr (

View File

@ -73,18 +73,18 @@ struct hio_bopt_lng_t
struct hio_bopt_t struct hio_bopt_t
{ {
/* input */ /* input */
const hio_bch_t* str; /* option string */ const hio_bch_t* str; /**< option string */
hio_bopt_lng_t* lng; /* long options */ hio_bopt_lng_t* lng; /**< long options */
/* output */ /* output */
hio_bci_t opt; /* character checked for validity */ hio_bci_t opt; /**< character checked for validity */
hio_bch_t* arg; /* argument associated with an option */ hio_bch_t* arg; /**< argument associated with an option */
/* output */ /* output */
const hio_bch_t* lngopt; const hio_bch_t* lngopt;
/* input + output */ /* input + output */
int ind; /* index into parent argv vector */ int ind; /**< index into parent argv vector */
/* input + output - internal*/ /* input + output - internal*/
hio_bch_t* cur; hio_bch_t* cur;
@ -95,7 +95,7 @@ extern "C" {
#endif #endif
/** /**
* The hio_getopt() function processes the \a argc command-line arguments * The hio_getuopt() function processes the \a argc command-line arguments
* pointed to by \a argv as configured in \a opt. It can process two * pointed to by \a argv as configured in \a opt. It can process two
* different option styles: a single character starting with '-', and a * different option styles: a single character starting with '-', and a
* long name starting with '--'. * long name starting with '--'.
@ -107,7 +107,7 @@ extern "C" {
* - \b ? indicates a bad option stored in the \a opt->opt field. * - \b ? indicates a bad option stored in the \a opt->opt field.
* - \b : indicates a bad parameter for an option stored in the \a opt->opt field. * - \b : indicates a bad parameter for an option stored in the \a opt->opt field.
* *
* @return an option character on success, HIO_CHAR_EOF on no more options. * @return an option character on success, #HIO_UCI_EOF on no more options.
*/ */
HIO_EXPORT hio_uci_t hio_getuopt ( HIO_EXPORT hio_uci_t hio_getuopt (
int argc, /* argument count */ int argc, /* argument count */
@ -115,6 +115,10 @@ HIO_EXPORT hio_uci_t hio_getuopt (
hio_uopt_t* opt /* option configuration */ hio_uopt_t* opt /* option configuration */
); );
/**
* The hio_getbopt() function is analogous to hio_getuopt() except that
* it accepts character strings of the #hio_bch_t type.
*/
HIO_EXPORT hio_bci_t hio_getbopt ( HIO_EXPORT hio_bci_t hio_getbopt (
int argc, /* argument count */ int argc, /* argument count */
hio_bch_t* const* argv, /* argument array */ hio_bch_t* const* argv, /* argument array */

View File

@ -25,6 +25,10 @@
#ifndef _HIO_H_ #ifndef _HIO_H_
#define _HIO_H_ #define _HIO_H_
/** \file
* The file defines basic data types and functions common through the entire
* library.
*/
#include <hio-cmn.h> #include <hio-cmn.h>
#include <stdarg.h> #include <stdarg.h>
@ -259,6 +263,10 @@ struct hio_dev_evcb_t
int (*on_write) (hio_dev_t* dev, hio_iolen_t wrlen, void* wrctx, const hio_devaddr_t* dstaddr); int (*on_write) (hio_dev_t* dev, hio_iolen_t wrlen, void* wrctx, const hio_devaddr_t* dstaddr);
}; };
/**
* The #hio_q_t type is a basic structure that holds two pointers to the previous
* item and the next to form a circular doubly linked list to used as a queue.
*/
struct hio_q_t struct hio_q_t
{ {
hio_q_t* q_next; hio_q_t* q_next;
@ -297,14 +305,14 @@ struct hio_q_t
__nn->q_prev->q_next = __nn; \ __nn->q_prev->q_next = __nn; \
} while (0) } while (0)
/* insert an item at the back of the queue */ /** The #HIO_Q_ENQ macro inserts an item at the back of the queue. */
/*#define HIO_Q_ENQ(wq,x) HIO_Q_LINK(HIO_Q_TAIL(wq), x, HIO_Q_TAIL(wq)->next)*/ /*#define HIO_Q_ENQ(wq,x) HIO_Q_LINK(HIO_Q_TAIL(wq), x, HIO_Q_TAIL(wq)->next)*/
#define HIO_Q_ENQ(wq,x) HIO_Q_LINK(HIO_Q_TAIL(wq), x, wq) #define HIO_Q_ENQ(wq,x) HIO_Q_LINK(HIO_Q_TAIL(wq), x, wq)
/* remove an item in the front from the queue */ /** The #HIO_Q_DEQ macro removes an item in the front from the queue. */
#define HIO_Q_DEQ(wq) HIO_Q_UNLINK(HIO_Q_HEAD(wq)) #define HIO_Q_DEQ(wq) HIO_Q_UNLINK(HIO_Q_HEAD(wq))
/* completed write queue */ /** The #hio_cwq_t type defines a queue of completed writes. */
struct hio_cwq_t struct hio_cwq_t
{ {
hio_cwq_t* q_next; hio_cwq_t* q_next;
@ -331,7 +339,7 @@ struct hio_cwq_t
#define HIO_CWQ_ENQ(cwq,x) HIO_CWQ_LINK(HIO_CWQ_TAIL(cwq), (hio_q_t*)x, cwq) #define HIO_CWQ_ENQ(cwq,x) HIO_CWQ_LINK(HIO_CWQ_TAIL(cwq), (hio_q_t*)x, cwq)
#define HIO_CWQ_DEQ(cwq) HIO_CWQ_UNLINK(HIO_CWQ_HEAD(cwq)) #define HIO_CWQ_DEQ(cwq) HIO_CWQ_UNLINK(HIO_CWQ_HEAD(cwq))
/* write queue */ /** The #hio_wq_t type defines a queue of pending writes */
struct hio_wq_t struct hio_wq_t
{ {
hio_wq_t* q_next; hio_wq_t* q_next;

View File

@ -33,6 +33,9 @@
#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#define FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH #define FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH
@ -54,6 +57,7 @@ struct file_t
HIO_SVC_HTTS_RSRC_HEADER; HIO_SVC_HTTS_RSRC_HEADER;
int options; int options;
hio_svc_htts_file_cbs_t* cbs;
hio_oow_t num_pending_writes_to_client; hio_oow_t num_pending_writes_to_client;
hio_oow_t num_pending_writes_to_peer; hio_oow_t num_pending_writes_to_peer;
int sendfile_ok; int sendfile_ok;
@ -85,13 +89,11 @@ struct file_t
hio_dev_sck_on_write_t client_org_on_write; hio_dev_sck_on_write_t client_org_on_write;
hio_dev_sck_on_disconnect_t client_org_on_disconnect; hio_dev_sck_on_disconnect_t client_org_on_disconnect;
hio_htrd_recbs_t client_htrd_org_recbs; hio_htrd_recbs_t client_htrd_org_recbs;
}; };
typedef struct file_t file_t; typedef struct file_t file_t;
static int file_send_contents_to_client (file_t* file); static int file_send_contents_to_client (file_t* file);
static void file_halt_participating_devices (file_t* file) static void file_halt_participating_devices (file_t* file)
{ {
HIO_ASSERT (file->client->htts->hio, file->client != HIO_NULL); HIO_ASSERT (file->client->htts->hio, file->client != HIO_NULL);
@ -554,7 +556,7 @@ static int file_send_contents_to_client (file_t* file)
} }
else if (n == 0) else if (n == 0)
{ {
/* no more data to read - this must not happend unless file size changed while the file is open. */ /* no more data to read - this must not happen unless file size changed while the file is open. */
/* TODO: I probably must close the connection by force??? */ /* TODO: I probably must close the connection by force??? */
file_mark_over (file, FILE_OVER_READ_FROM_PEER); file_mark_over (file, FILE_OVER_READ_FROM_PEER);
return -1; return -1;
@ -701,7 +703,6 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int
hio_bch_t* alt_file; hio_bch_t* alt_file;
int alt_fd; int alt_fd;
/* TODO: create an option to support directory listing */
alt_file = hio_svc_htts_dupmergepaths(file->htts, actual_file, "index.html"); /* TODO: make the default index files configurable */ alt_file = hio_svc_htts_dupmergepaths(file->htts, actual_file, "index.html"); /* TODO: make the default index files configurable */
if (alt_file) if (alt_file)
{ {
@ -715,16 +716,56 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int
else else
{ {
hio_freemem (file->htts->hio, alt_file); hio_freemem (file->htts->hio, alt_file);
#if 0
/* TODO: switch to directory listing if it's enabled */ if (file->options & HIO_SVC_HTTS_FILE_LIST_DIR)
DIR* dp;
dp = opendir(actual_file);
if (dp)
{ {
close (file->peer); /* switch to directory listing */
file->peer = dp; DIR* dp;
dp = opendir(actual_file);
if (dp)
{
alt_file = hio_dupbcstr(file->htts->hio, "/tmp/.XXXXXX", HIO_NULL);
if (alt_file)
{
/* TOOD: mkostemp instead and specify O_CLOEXEC and O_LARGEFILE? */
alt_fd = mkstemp(alt_file);
if (alt_fd >= 0)
{
struct dirent* de;
unlink (alt_file);
while ((de = readdir(dp)))
{
/* TODO: do buffering ... */
#if 0
/* TODO: call a directory entry formatter callback?? */
if (file->cbs && file->cbs->bfmt_dir)
{
file->cbs->bfmt_dir(file->htts, de->d_name);
}
#endif
if (strcmp(de->d_name, ".") != 0)
{
write (alt_fd, de->d_name, strlen(de->d_name));
write (alt_fd, "\n", 1);
}
}
lseek (alt_fd, SEEK_SET, 0);
close (file->peer);
file->peer = alt_fd;
opened_file = alt_file;
}
else
{
hio_freemem (file->htts->hio, alt_file);
}
}
closedir (dp);
}
} }
#endif
} }
} }
} }
@ -759,10 +800,8 @@ static HIO_INLINE void set_tcp_cork (hio_dev_sck_t* sck)
#endif #endif
} }
int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* filepath, const hio_bch_t* mime_type, int options) int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* filepath, const hio_bch_t* mime_type, int options, hio_svc_htts_file_cbs_t* cbs)
{ {
/* TODO: ETag, Last-Modified... */
hio_t* hio = htts->hio; hio_t* hio = htts->hio;
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck);
file_t* file = HIO_NULL; file_t* file = HIO_NULL;
@ -782,6 +821,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t*
if (HIO_UNLIKELY(!file)) goto oops; if (HIO_UNLIKELY(!file)) goto oops;
file->options = options; file->options = options;
file->cbs = cbs; /* the given pointer must outlive the lifespan of the while file handling cycle. */
file->client = cli; file->client = cli;
file->sendfile_ok = hio_dev_sck_sendfileok(cli->sck); file->sendfile_ok = hio_dev_sck_sendfileok(cli->sck);
/*file->num_pending_writes_to_client = 0; /*file->num_pending_writes_to_client = 0;

View File

@ -7,7 +7,7 @@
test_default_index() test_default_index()
{ {
local msg="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.$$"
../bin/hio-webs "${srvaddr}" "${tmpdir}" 2>/dev/null & ../bin/hio-webs "${srvaddr}" "${tmpdir}" 2>/dev/null &