directory formatting code in http server file handler

This commit is contained in:
hyung-hwan 2022-12-23 14:44:55 +00:00
parent b4cadfeed3
commit 56bd1af653
3 changed files with 46 additions and 14 deletions

View File

@ -74,6 +74,32 @@ done:
}
}
static void bfmt_dir (hio_svc_htts_t* htts, int fd, const hio_bch_t* name, int type, void* ctx)
{
/* TODO: do bufferring */
/* "<a href="urlencoded-name">name</a> */
if (name)
{
/* TODO: get the directory name
check if the entry is a directory or something else */
write (fd, "<li><a href=\"", 13);
write (fd, name, strlen(name));
write (fd, "\">", 2);
write (fd, name, strlen(name));
write (fd, "</a>", 4);
}
else if (type == 0)
{
write (fd, "<html><body>", 12);
}
else
{
write (fd, "</body></html>", 14);
}
}
static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req)
{
htts_ext_t* ext = hio_svc_htts_getxtn(htts);
@ -81,6 +107,8 @@ 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 };
hio_htre_perdecqpath (req);
mth = hio_htre_getqmethodtype(req);
@ -97,7 +125,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_
/* 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, HIO_NULL) <= -1) goto oops;
if (hio_svc_htts_dofile(htts, csck, req, ext->ai->docroot, qpath, HIO_NULL, fopts, &fcbs) <= -1) goto oops;
}
#if 0
else

View File

@ -163,7 +163,8 @@ enum hio_svc_htts_txt_option_t
struct hio_svc_htts_file_cbs_t
{
int (*bfmt_dir) (hio_svc_htts_t* htts, const hio_bch_t* name);
int (*bfmt_dir) (hio_svc_htts_t* htts, int fd, const hio_bch_t* name, int type, void* ctx);
void *ctx;
};
typedef struct hio_svc_htts_file_cbs_t hio_svc_htts_file_cbs_t;

View File

@ -735,25 +735,28 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int
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);
file->cbs->bfmt_dir (file->htts, alt_fd, HIO_NULL, 0, file->cbs->ctx);
while ((de = readdir(dp)))
{
file->cbs->bfmt_dir (file->htts, alt_fd, de->d_name, 1, file->cbs->ctx);
}
#endif
file->cbs->bfmt_dir (file->htts, alt_fd, HIO_NULL, 2, 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);
}
}
}
lseek (alt_fd, SEEK_SET, 0);
close (file->peer);
file->peer = alt_fd;
opened_file = alt_file;