2022-10-07 05:08:40 +00:00
|
|
|
#include <hio-http.h>
|
2022-10-25 22:47:40 +00:00
|
|
|
#include <hio-tar.h>
|
2022-12-22 16:25:20 +00:00
|
|
|
#include <hio-opt.h>
|
2022-12-26 17:03:53 +00:00
|
|
|
#include <hio-prv.h>
|
2023-11-17 14:07:59 +00:00
|
|
|
#include <hio-utl.h>
|
2022-10-07 05:08:40 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2022-10-25 22:47:40 +00:00
|
|
|
#include <unistd.h>
|
2022-12-31 15:20:07 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2022-10-07 05:08:40 +00:00
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
struct arg_info_t
|
|
|
|
{
|
2023-11-17 14:07:59 +00:00
|
|
|
const char* logopt;
|
2022-12-22 16:25:20 +00:00
|
|
|
const char* laddrs;
|
|
|
|
const char* docroot;
|
|
|
|
int file_list_dir;
|
2022-12-31 15:20:07 +00:00
|
|
|
int file_load_index_page;
|
2022-12-22 16:25:20 +00:00
|
|
|
};
|
|
|
|
typedef struct arg_info_t arg_info_t;
|
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
struct htts_ext_t
|
|
|
|
{
|
2022-12-31 15:52:38 +00:00
|
|
|
const arg_info_t* ai;
|
2022-10-07 05:08:40 +00:00
|
|
|
};
|
|
|
|
typedef struct htts_ext_t htts_ext_t;
|
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
struct buff_t
|
|
|
|
{
|
|
|
|
hio_uint8_t buf[4096];
|
|
|
|
hio_oow_t len;
|
|
|
|
};
|
|
|
|
typedef struct buff_t buff_t;
|
2022-12-22 16:25:20 +00:00
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2023-01-08 16:09:34 +00:00
|
|
|
static void untar_write_status_code (int fd, int code)
|
|
|
|
{
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case 200:
|
|
|
|
write (fd, "Status: 200\r\n\r\n", 15);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 500:
|
|
|
|
default:
|
|
|
|
write (fd, "Status: 500\r\n\r\n", 15);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
static void untar (hio_svc_htts_t* htts, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx)
|
2022-10-25 22:47:40 +00:00
|
|
|
{
|
2022-12-13 16:17:39 +00:00
|
|
|
FILE* wfp = HIO_NULL;
|
2023-01-11 14:59:41 +00:00
|
|
|
hio_t* hio;
|
2022-10-25 22:47:40 +00:00
|
|
|
htts_ext_t* ext;
|
2022-12-13 16:17:39 +00:00
|
|
|
hio_tar_t* tar = HIO_NULL;
|
|
|
|
hio_uint8_t buf[4096];
|
|
|
|
ssize_t n;
|
2022-10-25 22:47:40 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
hio = hio_svc_htts_gethio(htts);
|
|
|
|
ext = hio_svc_htts_getxtn(htts);
|
2022-10-25 22:47:40 +00:00
|
|
|
|
2022-12-26 17:03:53 +00:00
|
|
|
/* TODO: error handling on write() failure */
|
2022-10-25 22:47:40 +00:00
|
|
|
wfp = fdopen(iop->wfd, "w");
|
|
|
|
if (!wfp)
|
|
|
|
{
|
2023-01-08 16:09:34 +00:00
|
|
|
untar_write_status_code (iop->wfd, 500);
|
2022-10-25 22:47:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:17:39 +00:00
|
|
|
tar = hio_tar_open(hio, 0);
|
|
|
|
if (!tar)
|
2022-10-25 22:47:40 +00:00
|
|
|
{
|
2023-01-08 16:09:34 +00:00
|
|
|
untar_write_status_code (iop->wfd, 500);
|
2022-10-25 22:47:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
hio_tar_setxrootwithbcstr (tar, ext->ai->docroot);
|
2022-12-13 16:17:39 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
n = read(iop->rfd, buf, HIO_COUNTOF(buf));
|
|
|
|
if (n <= 0) break; /* eof or error */
|
|
|
|
|
|
|
|
if (hio_tar_xfeed(tar, buf, n) <= -1)
|
|
|
|
{
|
2023-01-08 16:09:34 +00:00
|
|
|
untar_write_status_code (iop->wfd, 500);
|
2022-12-13 16:17:39 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hio_tar_endxfeed (tar);
|
2023-01-08 16:09:34 +00:00
|
|
|
untar_write_status_code (iop->wfd, 200);
|
2022-10-25 22:47:40 +00:00
|
|
|
|
|
|
|
done:
|
2022-12-13 16:17:39 +00:00
|
|
|
if (tar)
|
|
|
|
{
|
|
|
|
hio_tar_close (tar);
|
|
|
|
}
|
2022-10-25 22:47:40 +00:00
|
|
|
if (wfp)
|
|
|
|
{
|
2022-12-13 16:17:39 +00:00
|
|
|
iop->wfd = HIO_SYSHND_INVALID; /* prevent double close by the main library since this function closes it with fclose() */
|
2022-10-25 22:47:40 +00:00
|
|
|
fclose (wfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static hio_oow_t write_all_to_fd (int fd, const hio_uint8_t* ptr, hio_oow_t len)
|
|
|
|
{
|
|
|
|
hio_oow_t rem = len;
|
|
|
|
|
|
|
|
while (rem > 0)
|
|
|
|
{
|
|
|
|
hio_ooi_t n;
|
|
|
|
n = write(fd, ptr, rem);
|
|
|
|
if (n <= -1) break;
|
|
|
|
rem -= n;
|
|
|
|
ptr += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len - rem; /* return the number of bytes written */
|
|
|
|
}
|
|
|
|
|
|
|
|
static HIO_INLINE void init_buff (buff_t* buf)
|
|
|
|
{
|
|
|
|
buf->len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flush_buff_to_fd (int fd, buff_t* buf)
|
|
|
|
{
|
|
|
|
if (buf->len > 0)
|
|
|
|
{
|
|
|
|
hio_oow_t n;
|
|
|
|
n = write_all_to_fd(fd, buf->buf, buf->len);
|
|
|
|
buf->len -= n;
|
|
|
|
if (buf->len > 0) return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_buff_to_fd (int fd, buff_t* buf, const void* ptr, hio_oow_t len)
|
|
|
|
{
|
|
|
|
hio_oow_t rcapa;
|
|
|
|
|
|
|
|
rcapa = HIO_COUNTOF(buf->buf) - buf->len;
|
|
|
|
if (len >= HIO_COUNTOF(buf->buf) + rcapa)
|
|
|
|
{
|
|
|
|
if (flush_buff_to_fd(fd, buf) <= -1) return -1;
|
|
|
|
if (write_all_to_fd(fd, (const hio_uint8_t*)ptr, len) != len) return -1;
|
|
|
|
}
|
|
|
|
else if (len >= rcapa)
|
|
|
|
{
|
|
|
|
HIO_MEMCPY (&buf->buf[buf->len], ptr, rcapa);
|
|
|
|
buf->len += rcapa;
|
|
|
|
if (flush_buff_to_fd(fd, buf) <= -1) return -1;
|
|
|
|
HIO_MEMCPY (buf->buf, (const hio_uint8_t*)ptr + rcapa, len - rcapa);
|
|
|
|
buf->len = len - rcapa;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HIO_MEMCPY (&buf->buf[buf->len], ptr, len);
|
|
|
|
buf->len += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
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)
|
2022-12-23 14:44:55 +00:00
|
|
|
{
|
2022-12-31 15:20:07 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-12-26 17:03:53 +00:00
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
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;
|
2023-01-02 15:36:14 +00:00
|
|
|
buff_t buf;
|
2022-12-31 15:20:07 +00:00
|
|
|
|
|
|
|
if (ext->ai->file_load_index_page)
|
2022-12-23 14:44:55 +00:00
|
|
|
{
|
2022-12-31 15:52:38 +00:00
|
|
|
hio_bch_t* index_path;
|
2022-12-26 17:03:53 +00:00
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
index_path = hio_svc_htts_dupmergepaths(htts, dir_path, "index.html");
|
|
|
|
if (HIO_UNLIKELY(!index_path)) goto oops;
|
2022-12-30 17:18:28 +00:00
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
fd = open(index_path, O_RDONLY, 0644);
|
|
|
|
if (fd >= 0)
|
2022-12-30 17:18:28 +00:00
|
|
|
{
|
2022-12-31 15:20:07 +00:00
|
|
|
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;
|
2022-12-30 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
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);
|
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
buf.len = 0;
|
|
|
|
init_buff (&buf);
|
|
|
|
|
|
|
|
if (write_buff_to_fd(fd, &buf, "<html><body>", 12) <= -1) goto oops;
|
|
|
|
if (!(qpath[0] == '\0' || (qpath[0] == '/' && qpath[1] == '\0')) &&
|
|
|
|
write_buff_to_fd(fd, &buf,"<li><a href=\"..\">..</a>", 23) <= -1) goto oops;
|
2022-12-31 15:20:07 +00:00
|
|
|
|
|
|
|
while ((de = readdir(dp)))
|
|
|
|
{
|
|
|
|
struct stat st;
|
2023-01-02 15:36:14 +00:00
|
|
|
hio_bch_t* tptr, * dend;
|
|
|
|
hio_bch_t tmp[1024]; /* this must be at least 5 characters large for html escaping */
|
|
|
|
hio_oow_t tml;
|
2022-12-31 15:52:38 +00:00
|
|
|
int n;
|
2022-12-31 15:20:07 +00:00
|
|
|
|
|
|
|
if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
|
|
|
|
(de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')) continue;
|
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
tptr = hio_svc_htts_dupmergepaths(htts, dir_path, de->d_name);
|
|
|
|
if (HIO_UNLIKELY(!tptr)) continue;
|
|
|
|
n = stat(tptr, &st);
|
|
|
|
hio_freemem (hio, tptr);
|
2022-12-31 15:52:38 +00:00
|
|
|
if (HIO_UNLIKELY(n <= -1)) continue;
|
2022-12-31 15:20:07 +00:00
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
dend = de->d_name + strlen(de->d_name);
|
|
|
|
|
|
|
|
if (write_buff_to_fd(fd, &buf, "<li><a href=\"", 13) <= -1) goto oops;
|
|
|
|
|
|
|
|
for (tptr = de->d_name; tptr < dend; )
|
2022-12-31 15:52:38 +00:00
|
|
|
{
|
2023-01-02 15:36:14 +00:00
|
|
|
hio_oow_t dlen = dend - tptr;
|
|
|
|
if (dlen > HIO_COUNTOF(tmp) / 3) dlen = HIO_COUNTOF(tmp) / 3; /* it can grow upto 3 folds */
|
|
|
|
HIO_ASSERT (hio, dlen >= 1);
|
|
|
|
tml = hio_perenc_http_bchars(tptr, dlen, tmp, HIO_COUNTOF(tmp), 0); /* feed a chunk that won't overflow the buffer */
|
|
|
|
HIO_ASSERT (hio, tml <= HIO_COUNTOF(tmp)); /* the buffer 'tmp' must be large enough */
|
|
|
|
if (write_buff_to_fd(fd, &buf, tmp, tml) <= -1) goto oops;
|
|
|
|
tptr += dlen;
|
2022-12-31 15:52:38 +00:00
|
|
|
}
|
2023-01-02 15:36:14 +00:00
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode) && write_buff_to_fd(fd, &buf, "/", 1) <= -1) goto oops;
|
|
|
|
if (write_buff_to_fd(fd, &buf, "\">", 2) <= -1) goto oops;
|
|
|
|
|
|
|
|
dend = de->d_name + strlen(de->d_name);
|
|
|
|
for (tptr = de->d_name; tptr < dend; )
|
|
|
|
{
|
|
|
|
hio_oow_t dlen = dend - tptr;
|
|
|
|
if (dlen > HIO_COUNTOF(tmp) / 5) dlen = HIO_COUNTOF(tmp) / 5; /* it can grow upto 5 folds */
|
|
|
|
HIO_ASSERT (hio, dlen >= 1);
|
|
|
|
tml = hio_escape_html_bchars(tptr, dlen, tmp, HIO_COUNTOF(tmp)); /* feed a chunk that won't overflow the buffer */
|
|
|
|
HIO_ASSERT (hio, tml <= HIO_COUNTOF(tmp)); /* the buffer 'tmp' must be large enough */
|
|
|
|
if (write_buff_to_fd(fd, &buf, tmp, tml) <= -1) goto oops;
|
|
|
|
tptr += dlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode) && write_buff_to_fd(fd, &buf, "/", 1) <= -1) goto oops;
|
|
|
|
if (write_buff_to_fd(fd, &buf, "</a>", 4) <= -1) goto oops;
|
2022-12-23 14:44:55 +00:00
|
|
|
}
|
2022-12-31 15:20:07 +00:00
|
|
|
|
2023-01-02 15:36:14 +00:00
|
|
|
if (write_buff_to_fd(fd, &buf, "</body></html>\n", 15) <= -1) goto oops;
|
|
|
|
if (flush_buff_to_fd(fd, &buf) <= -1) goto oops;
|
2022-12-31 15:20:07 +00:00
|
|
|
|
|
|
|
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;
|
2022-12-23 14:44:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 15:55:15 +00:00
|
|
|
static void htts_task_on_kill (hio_svc_htts_task_t* task)
|
|
|
|
{
|
|
|
|
hio_svc_htts_t* htts = task->htts;
|
|
|
|
hio_t* hio = hio_svc_htts_gethio(htts);
|
|
|
|
|
|
|
|
/* TODO: pretty log message */
|
2023-03-05 18:27:42 +00:00
|
|
|
HIO_INFO3 (hio, "DONE [%hs] [%hs] [%d] ......................................\n", task->task_req_qmth, task->task_req_qpath, (int)task->task_status_code);
|
2023-03-04 15:55:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
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);
|
2022-10-08 07:47:55 +00:00
|
|
|
hio_t* hio = hio_svc_htts_gethio(htts);
|
2022-10-07 05:08:40 +00:00
|
|
|
hio_http_method_t mth;
|
2023-01-08 16:09:34 +00:00
|
|
|
const hio_bch_t* qpath, * qpath_ext;
|
2023-02-20 08:08:41 +00:00
|
|
|
int proto_len;
|
2022-10-07 05:08:40 +00:00
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
static hio_svc_htts_file_cbs_t fcbs = { file_get_mime_type, file_open_dir_list, HIO_NULL };
|
2022-12-23 14:44:55 +00:00
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
hio_htre_perdecqpath (req);
|
|
|
|
|
|
|
|
mth = hio_htre_getqmethodtype(req);
|
|
|
|
qpath = hio_htre_getqpath(req);
|
2023-01-08 16:09:34 +00:00
|
|
|
qpath_ext = hio_rfind_bchar_in_bcstr(qpath, '.');
|
|
|
|
if (!qpath_ext) qpath_ext = "";
|
2022-10-07 05:08:40 +00:00
|
|
|
|
2023-02-20 08:08:41 +00:00
|
|
|
/* TODO: log remote address and other information .... */
|
|
|
|
HIO_INFO3 (hio, "%.*hs %hs\n", hio_htre_getqmethodlen(req), hio_htre_getqmethodname(req), qpath);
|
|
|
|
|
|
|
|
if (((proto_len = 7) && hio_comp_bcstr_limited(qpath, "http://", 7, 1) == 0) ||
|
|
|
|
((proto_len = 8) && hio_comp_bcstr_limited(qpath, "https://", 8, 1) == 0))
|
|
|
|
{
|
|
|
|
const hio_bch_t* tmp;
|
|
|
|
tmp = hio_find_bchar_in_bcstr(qpath + proto_len, '/');
|
|
|
|
if (tmp) qpath = tmp; /* skip http://domain.name */
|
|
|
|
|
|
|
|
/* TODO: support proxy .... */
|
|
|
|
}
|
|
|
|
|
2023-01-08 16:09:34 +00:00
|
|
|
if (mth == HIO_HTTP_OTHER && hio_comp_bcstr(hio_htre_getqmethodname(req), "UNTAR", 1) == 0 && hio_comp_bcstr(qpath_ext, ".tar", 0) == 0)
|
2022-10-25 22:47:40 +00:00
|
|
|
{
|
2022-12-15 15:24:31 +00:00
|
|
|
/* don't care about the path for now. TODO: make this secure and reasonable */
|
2023-03-04 15:55:15 +00:00
|
|
|
if (hio_svc_htts_dothr(htts, csck, req, untar, HIO_NULL, 0, htts_task_on_kill) <= -1) goto oops;
|
2023-01-08 16:09:34 +00:00
|
|
|
}
|
2023-01-11 14:59:41 +00:00
|
|
|
else if (mth == HIO_HTTP_OPTIONS)
|
|
|
|
{
|
2023-01-14 14:54:54 +00:00
|
|
|
/* TODO: write proper handler for preflight */
|
2023-01-11 14:59:41 +00:00
|
|
|
//if (hio_svc_htts_dofun(htts, csck, req, options, HIO_NULL, 0) <= -1) goto oops;
|
2023-01-14 14:54:54 +00:00
|
|
|
const hio_bch_t* msg = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: *\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Allow-Credentials: true\r\n\r\n";
|
|
|
|
hio_dev_sck_write(csck, msg, strlen(msg), HIO_NULL, HIO_NULL);
|
|
|
|
}
|
|
|
|
else if (hio_comp_bcstr(qpath_ext, ".cgi", 0) == 0)
|
|
|
|
{
|
2023-03-04 15:55:15 +00:00
|
|
|
if (hio_svc_htts_docgi(htts, csck, req, ext->ai->docroot, qpath, 0, htts_task_on_kill) <= -1) goto oops;
|
2023-01-11 14:59:41 +00:00
|
|
|
}
|
2023-02-20 08:08:41 +00:00
|
|
|
else if (hio_comp_bcstr(qpath_ext, ".php", 0) == 0 || hio_comp_bcstr(qpath_ext, ".ant", 0) == 0 /*|| hio_comp_bcstr_limited(qpath, "http://", 7, 1) == 0*/)
|
2023-01-08 16:09:34 +00:00
|
|
|
{
|
|
|
|
hio_skad_t skad;
|
|
|
|
hio_bcstrtoskad(hio, "10.30.0.133:9000", &skad);
|
2023-02-20 08:08:41 +00:00
|
|
|
|
|
|
|
HIO_DEBUG2 (hio, "fcgi %hs %hs\n", ext->ai->docroot, qpath);
|
2023-03-09 16:27:08 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, "text/plain", "what the...", 0, htts_task_on_kill) <= -1) goto oops;
|
|
|
|
#else
|
|
|
|
/* TODO: accept a separate document root for the fcgi server and use it below */
|
2023-03-04 15:55:15 +00:00
|
|
|
if (hio_svc_htts_dofcgi(htts, csck, req, &skad, ext->ai->docroot, qpath, 0, htts_task_on_kill) <= -1) goto oops;
|
2023-03-09 16:27:08 +00:00
|
|
|
#endif
|
2022-10-25 22:47:40 +00:00
|
|
|
}
|
2022-12-15 15:24:31 +00:00
|
|
|
else // if (mth == HIO_HTTP_GET || mth == HIO_HTTP_POST)
|
2022-10-07 05:08:40 +00:00
|
|
|
{
|
2022-10-08 07:47:55 +00:00
|
|
|
/* TODO: proper mime-type */
|
2022-12-22 05:21:15 +00:00
|
|
|
/* TODO: make HIO_SVC_HTTS_FILE_DIR a cli option */
|
2023-03-04 15:55:15 +00:00
|
|
|
if (hio_svc_htts_dofile(htts, csck, req, ext->ai->docroot, qpath, HIO_NULL, 0, htts_task_on_kill, &fcbs) <= -1) goto oops;
|
2022-10-07 05:08:40 +00:00
|
|
|
}
|
2022-10-09 16:41:07 +00:00
|
|
|
#if 0
|
2022-10-07 05:08:40 +00:00
|
|
|
else
|
|
|
|
{
|
2022-10-09 16:41:07 +00:00
|
|
|
if (hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_FORBIDDEN, "text/plain", hio_http_status_to_bcstr(403), 0) <= -1) goto oops;
|
2022-10-07 05:08:40 +00:00
|
|
|
}
|
2022-10-09 16:41:07 +00:00
|
|
|
#endif
|
2022-10-07 05:08:40 +00:00
|
|
|
return 0;
|
2022-10-25 22:47:40 +00:00
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
oops:
|
|
|
|
hio_dev_sck_halt (csck);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
int webs_start (hio_t* hio, const arg_info_t* ai)
|
2022-10-07 05:08:40 +00:00
|
|
|
{
|
|
|
|
const hio_bch_t* ptr, * end;
|
|
|
|
hio_bcs_t tok;
|
|
|
|
hio_dev_sck_bind_t bi[100];
|
|
|
|
hio_oow_t bic;
|
|
|
|
hio_svc_htts_t* webs;
|
|
|
|
htts_ext_t* ext;
|
2023-02-20 08:08:41 +00:00
|
|
|
hio_svc_fcgic_tmout_t fcgic_tmout;
|
2022-10-07 05:08:40 +00:00
|
|
|
|
|
|
|
bic = 0;
|
2022-12-22 16:25:20 +00:00
|
|
|
ptr = ai->laddrs;
|
2022-10-07 05:08:40 +00:00
|
|
|
end = ptr + hio_count_bcstr(ptr);
|
|
|
|
while (ptr)
|
|
|
|
{
|
|
|
|
ptr = hio_tokenize_bchars(ptr, end - ptr, ", ", 2, &tok, 0);
|
|
|
|
if (tok.len > 0)
|
|
|
|
{
|
|
|
|
if (hio_bcharstoskad(hio, tok.ptr, tok.len, &bi[bic].localaddr) <= -1)
|
|
|
|
{
|
|
|
|
/* TODO: logging */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bi[bic].options = HIO_DEV_SCK_BIND_REUSEADDR | HIO_DEV_SCK_BIND_REUSEPORT | HIO_DEV_SCK_BIND_IGNERR;
|
|
|
|
bic++;
|
|
|
|
|
|
|
|
if (bic >= HIO_COUNTOF(bi)) break; /* TODO: make 'bi' dynamic */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 08:08:41 +00:00
|
|
|
HIO_MEMSET (&fcgic_tmout, 0, HIO_SIZEOF(fcgic_tmout));
|
2023-11-17 07:57:12 +00:00
|
|
|
HIO_INIT_NTIME(&fcgic_tmout.c, 5, 0);
|
|
|
|
HIO_INIT_NTIME(&fcgic_tmout.r, 60, 0);
|
|
|
|
HIO_INIT_NTIME(&fcgic_tmout.w, -1, 0);
|
2023-02-20 08:08:41 +00:00
|
|
|
|
2023-11-17 07:57:12 +00:00
|
|
|
webs = hio_svc_htts_start(hio, HIO_SIZEOF(htts_ext_t), bi, bic, process_http_request);
|
2022-10-07 05:08:40 +00:00
|
|
|
if (!webs) return -1; /* TODO: logging */
|
|
|
|
|
2023-11-17 07:57:12 +00:00
|
|
|
{
|
|
|
|
hio_oow_t ov;
|
|
|
|
ov = 1000;
|
|
|
|
hio_svc_htts_setoption (webs, HIO_SVC_HTTS_TASK_CGI_MAX, &ov);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hio_svc_htts_enablefcgic(webs, &fcgic_tmout) <= -1)
|
|
|
|
{
|
|
|
|
/* TODO: logging */
|
|
|
|
hio_svc_htts_stop (webs);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
ext = hio_svc_htts_getxtn(webs);
|
2022-12-22 16:25:20 +00:00
|
|
|
ext->ai = ai;
|
2022-10-07 05:08:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-17 14:07:59 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
static hio_t* g_hio = HIO_NULL;
|
|
|
|
|
|
|
|
static void handle_sigint (int sig)
|
|
|
|
{
|
|
|
|
if (g_hio) hio_stop (g_hio, HIO_STOPREQ_TERMINATION);
|
|
|
|
}
|
|
|
|
|
2023-11-17 14:07:59 +00:00
|
|
|
|
|
|
|
static int handle_logopt (hio_t* hio, const hio_bch_t* logstr)
|
|
|
|
{
|
|
|
|
hio_bch_t* cm, * flt;
|
|
|
|
hio_bitmask_t logmask;
|
|
|
|
hio_oow_t tlen, i;
|
|
|
|
hio_bcs_t fname;
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
int op; /* 0: bitwise-OR, 1: bitwise-AND */
|
|
|
|
hio_bitmask_t mask;
|
|
|
|
} xtab[] =
|
|
|
|
{
|
|
|
|
{ "", 0, 0 },
|
|
|
|
|
|
|
|
{ "core", 0, HIO_LOG_CORE },
|
|
|
|
{ "dev", 0, HIO_LOG_DEV },
|
|
|
|
{ "timer", 0, HIO_LOG_TIMER },
|
|
|
|
|
|
|
|
/* select a specific level */
|
|
|
|
{ "fatal", 0, HIO_LOG_FATAL },
|
|
|
|
{ "error", 0, HIO_LOG_ERROR },
|
|
|
|
{ "warn", 0, HIO_LOG_WARN },
|
|
|
|
{ "info", 0, HIO_LOG_INFO },
|
|
|
|
{ "debug", 0, HIO_LOG_DEBUG },
|
|
|
|
|
|
|
|
/* select a specific level or higher */
|
|
|
|
{ "fatal+", 0, HIO_LOG_FATAL },
|
|
|
|
{ "error+", 0, HIO_LOG_FATAL | HIO_LOG_ERROR },
|
|
|
|
{ "warn+", 0, HIO_LOG_FATAL | HIO_LOG_ERROR | HIO_LOG_WARN },
|
|
|
|
{ "info+", 0, HIO_LOG_FATAL | HIO_LOG_ERROR | HIO_LOG_WARN | HIO_LOG_INFO },
|
|
|
|
{ "debug+", 0, HIO_LOG_FATAL | HIO_LOG_ERROR | HIO_LOG_WARN | HIO_LOG_INFO | HIO_LOG_DEBUG },
|
|
|
|
|
|
|
|
/* select a specific level or lower */
|
|
|
|
{ "fatal-", 0, HIO_LOG_FATAL | HIO_LOG_ERROR | HIO_LOG_WARN | HIO_LOG_INFO | HIO_LOG_DEBUG },
|
|
|
|
{ "error-", 0, HIO_LOG_ERROR | HIO_LOG_WARN | HIO_LOG_INFO | HIO_LOG_DEBUG },
|
|
|
|
{ "warn-", 0, HIO_LOG_WARN | HIO_LOG_INFO | HIO_LOG_DEBUG },
|
|
|
|
{ "info-", 0, HIO_LOG_INFO | HIO_LOG_DEBUG },
|
|
|
|
{ "debug-", 0, HIO_LOG_DEBUG },
|
|
|
|
|
|
|
|
/* exclude a specific level */
|
|
|
|
{ "-fatal", 1, ~HIO_LOG_FATAL },
|
|
|
|
{ "-error", 1, ~HIO_LOG_ERROR },
|
|
|
|
{ "-warn", 1, ~HIO_LOG_WARN },
|
|
|
|
{ "-info", 1, ~HIO_LOG_INFO },
|
|
|
|
{ "-debug", 1, ~HIO_LOG_DEBUG },
|
|
|
|
};
|
|
|
|
|
|
|
|
cm = hio_find_bchar_in_bcstr(logstr, ',');
|
|
|
|
if (cm)
|
|
|
|
{
|
|
|
|
fname.len = cm - logstr;
|
|
|
|
logmask = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
flt = cm + 1;
|
|
|
|
|
|
|
|
cm = hio_find_bchar_in_bcstr(flt, ',');
|
|
|
|
tlen = (cm)? (cm - flt): hio_count_bcstr(flt);
|
|
|
|
|
|
|
|
for (i = 0; i < HIO_COUNTOF(xtab); i++)
|
|
|
|
{
|
|
|
|
if (hio_comp_bchars_bcstr(flt, tlen, xtab[i].name, 0) == 0)
|
|
|
|
{
|
|
|
|
if (xtab[i].op) logmask &= xtab[i].mask;
|
|
|
|
else logmask |= xtab[i].mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= HIO_COUNTOF(xtab))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "ERROR: unrecognized value - [%.*s] - [%s]\n", (int)tlen, flt, logstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (cm);
|
|
|
|
|
|
|
|
|
|
|
|
if (!(logmask & HIO_LOG_ALL_TYPES)) logmask |= HIO_LOG_ALL_TYPES; /* no types specified. force to all types */
|
|
|
|
if (!(logmask & HIO_LOG_ALL_LEVELS)) logmask |= HIO_LOG_ALL_LEVELS; /* no levels specified. force to all levels */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logmask = HIO_LOG_ALL_LEVELS | HIO_LOG_ALL_TYPES;
|
|
|
|
fname.len = hio_count_bcstr(logstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fname.ptr = (hio_bch_t*)logstr;
|
|
|
|
hio_setoption (hio, HIO_LOG_TARGET_BCS, &fname);
|
|
|
|
|
|
|
|
logmask |= HIO_LOG_GUARDED;
|
|
|
|
hio_setoption (hio, HIO_LOG_MASK, &logmask);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HIO_BUILD_DEBUG)
|
|
|
|
#if 0
|
|
|
|
static int handle_dbgopt (hio_t* hio, const hio_bch_t* str)
|
|
|
|
{
|
|
|
|
/*xtn_t* xtn = (xtn_t*)hio_getxtn(hio);*/
|
|
|
|
const hio_bch_t* cm, * flt;
|
|
|
|
hio_oow_t len;
|
|
|
|
hio_bitmask_t trait, dbgopt = 0;
|
|
|
|
|
|
|
|
cm = str - 1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
flt = cm + 1;
|
|
|
|
|
|
|
|
cm = hio_find_bchar_in_bcstr(flt, ',');
|
|
|
|
len = cm? (cm - flt): hio_count_bcstr(flt);
|
|
|
|
if (hio_comp_bchars_bcstr(flt, len, "htts") == 0) dbgopt |= HIO_TRAIT_DEBUG_HTTS;
|
|
|
|
else if (hio_comp_bchars_bcstr(flt, len, "bigint") == 0) dbgopt |= HIO_TRAIT_DEBUG_BIGINT;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "ERROR: unknown debug option value - %.*s\n", (int)len, flt);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (cm);
|
|
|
|
|
|
|
|
hio_getoption (hio, HIO_TRAIT, &trait);
|
|
|
|
trait |= dbgopt;
|
|
|
|
hio_setoption (hio, HIO_TRAIT, &trait);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
static int process_args (int argc, char* argv[], arg_info_t* ai)
|
2022-10-07 05:08:40 +00:00
|
|
|
{
|
2022-12-22 16:25:20 +00:00
|
|
|
static hio_bopt_lng_t lopt[] =
|
|
|
|
{
|
2022-12-31 15:20:07 +00:00
|
|
|
{ "file-no-list-dir", '\0' },
|
|
|
|
{ "file-no-load-index-page", '\0'},
|
2023-11-17 14:07:59 +00:00
|
|
|
{ ":log", 'l' },
|
2022-12-31 15:20:07 +00:00
|
|
|
{ HIO_NULL, '\0'}
|
2022-12-22 16:25:20 +00:00
|
|
|
};
|
|
|
|
static hio_bopt_t opt =
|
|
|
|
{
|
2023-11-17 14:07:59 +00:00
|
|
|
"l:",
|
2022-12-26 17:03:53 +00:00
|
|
|
lopt
|
2022-12-22 16:25:20 +00:00
|
|
|
};
|
2022-10-07 05:08:40 +00:00
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
hio_bci_t c;
|
2023-11-17 14:07:59 +00:00
|
|
|
const char* logopt = HIO_NULL;
|
2022-12-16 16:24:10 +00:00
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
if (argc < 3)
|
|
|
|
{
|
2022-12-22 16:25:20 +00:00
|
|
|
print_usage:
|
|
|
|
fprintf (stderr, "Usage: %s [options] listen-address:port docroot-dir\n", argv[0]);
|
2022-10-07 05:08:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2022-12-22 16:25:20 +00:00
|
|
|
|
|
|
|
memset (ai, 0, HIO_SIZEOF(*ai));
|
2022-12-31 15:20:07 +00:00
|
|
|
ai->file_list_dir = 1;
|
|
|
|
ai->file_load_index_page = 1;
|
2022-12-22 16:25:20 +00:00
|
|
|
|
|
|
|
while ((c = hio_getbopt(argc, argv, &opt)) != HIO_BCI_EOF)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2023-11-17 14:07:59 +00:00
|
|
|
case 'l':
|
|
|
|
logopt = opt.arg;
|
|
|
|
break;
|
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
case '\0':
|
2022-12-31 15:20:07 +00:00
|
|
|
if (strcasecmp(opt.lngopt, "file-no-list-dir") == 0)
|
|
|
|
{
|
|
|
|
ai->file_list_dir = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (strcasecmp(opt.lngopt, "file-no-load-index-page") == 0)
|
2022-12-22 16:25:20 +00:00
|
|
|
{
|
2022-12-31 15:20:07 +00:00
|
|
|
ai->file_load_index_page = 0;
|
2022-12-22 16:25:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto print_usage;
|
|
|
|
|
2023-11-17 14:07:59 +00:00
|
|
|
|
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
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;
|
|
|
|
|
2023-11-17 14:07:59 +00:00
|
|
|
ai->logopt = logopt;
|
2022-12-22 16:25:20 +00:00
|
|
|
ai->laddrs = argv[opt.ind++];
|
|
|
|
ai->docroot = argv[opt.ind++];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
hio_t* hio = HIO_NULL;
|
|
|
|
struct sigaction sigact;
|
|
|
|
arg_info_t ai;
|
|
|
|
int xret = -1;
|
|
|
|
|
|
|
|
if (process_args(argc, argv, &ai) <= -1) return -1;
|
2022-10-07 05:08:40 +00:00
|
|
|
|
|
|
|
memset (&sigact, 0, HIO_SIZEOF(sigact));
|
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
sigaction (SIGPIPE, &sigact, HIO_NULL);
|
|
|
|
|
|
|
|
memset (&sigact, 0, HIO_SIZEOF(sigact));
|
|
|
|
sigact.sa_handler = handle_sigint;
|
|
|
|
sigaction (SIGINT, &sigact, HIO_NULL);
|
|
|
|
|
|
|
|
hio = hio_open(HIO_NULL, 0, HIO_NULL, HIO_FEATURE_ALL, 512, HIO_NULL);
|
|
|
|
if (!hio)
|
|
|
|
{
|
|
|
|
printf ("Cannot open hio\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2023-11-17 14:07:59 +00:00
|
|
|
if (ai.logopt && handle_logopt(hio, ai.logopt) <= -1) goto oops;
|
|
|
|
|
2022-12-13 16:17:39 +00:00
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
g_hio = hio;
|
|
|
|
|
2022-12-22 16:25:20 +00:00
|
|
|
if (webs_start(hio, &ai) <= -1) goto oops;
|
2022-10-07 05:08:40 +00:00
|
|
|
|
|
|
|
hio_loop (hio);
|
|
|
|
|
|
|
|
g_hio = HIO_NULL;
|
|
|
|
xret = 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
|
|
|
|
memset (&sigact, 0, HIO_SIZEOF(sigact));
|
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
sigaction (SIGINT, &sigact, HIO_NULL);
|
|
|
|
|
|
|
|
if (hio) hio_close (hio);
|
|
|
|
return xret;
|
|
|
|
}
|
|
|
|
|