From 68a60f11670c243283ea337ba85a962997167d2e Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 10 Aug 2020 17:03:54 +0000 Subject: [PATCH] some code changes for easier testing --- mio/bin/t06.c | 46 ++++++++++++++++++++++++++++++++++++++-------- mio/lib/htrd.c | 2 +- mio/lib/http-fil.c | 9 ++++++++- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/mio/bin/t06.c b/mio/bin/t06.c index 5de7541..9db27bf 100644 --- a/mio/bin/t06.c +++ b/mio/bin/t06.c @@ -9,9 +9,10 @@ #include #include -#define NUM_THRS 8 +#define MAX_NUM_THRS 256 static int g_reuse_port = 0; -static mio_svc_htts_t* g_htts[NUM_THRS]; +static int g_num_thrs = 2; +static mio_svc_htts_t* g_htts[MAX_NUM_THRS]; static int g_htts_no = 0; static pthread_mutex_t g_htts_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -243,7 +244,7 @@ void* thr_func (void* arg) pthread_mutex_lock (&g_htts_mutex); g_htts[g_htts_no] = htts; printf ("starting the loop for %d\n", g_htts_no); - g_htts_no = (g_htts_no + 1) % MIO_COUNTOF(g_htts); + g_htts_no = (g_htts_no + 1) % g_num_thrs; pthread_mutex_unlock (&g_htts_mutex); mio_loop (mio); @@ -371,7 +372,7 @@ static int try_to_accept (mio_dev_sck_t* sck, mio_dev_sck_qxmsg_t* qxmsg, int in pthread_mutex_lock (&g_htts_mutex); htts = g_htts[g_htts_no]; - g_htts_no = (g_htts_no + 1) % MIO_COUNTOF(g_htts); + g_htts_no = (g_htts_no + 1) % g_num_thrs; pthread_mutex_unlock (&g_htts_mutex); if (mio_svc_htts_writetosidechan(htts, qxmsg, MIO_SIZEOF(*qxmsg)) <= -1) @@ -512,13 +513,42 @@ static int add_listener (mio_t* mio, mio_bch_t* addrstr) int main (int argc, char* argv[]) { mio_t* mio = MIO_NULL; - pthread_t t[NUM_THRS]; + pthread_t t[MAX_NUM_THRS]; mio_oow_t i; struct sigaction sigact; - if (argc >= 2 && strcmp(argv[1], "-r") == 0) + +// TODO: use getopt() or something similar + for (i = 1; i < argc; ) { - g_reuse_port = 1; + if (strcmp(argv[i], "-r") == 0) + { + g_reuse_port = 1; + i++; + } + else if (strcmp(argv[i], "-t") == 0) + { + i++; + if (i < argc) + { + g_num_thrs = atoi(argv[i]); + if (g_num_thrs < 1 || g_num_thrs > MAX_NUM_THRS) + { + printf ("Error: %s not allowed for -t\n", argv[i]); + return -1; + } + i++; + } + else + { + g_num_thrs = 2; + } + } + else + { + printf ("Error: invalid argument %s\n", argv[i]); + return -1; + } } memset (&sigact, 0, MIO_SIZEOF(sigact)); @@ -535,7 +565,7 @@ int main (int argc, char* argv[]) goto oops; } - for (i = 0; i < MIO_COUNTOF(t); i++) + for (i = 0; i < g_num_thrs; i++) pthread_create (&t[i], MIO_NULL, thr_func, mio); sleep (1); /* TODO: use pthread_cond_wait()/pthread_cond_signal() or a varialble to see if all threads are up */ diff --git a/mio/lib/htrd.c b/mio/lib/htrd.c index 293011c..bbfe04d 100644 --- a/mio/lib/htrd.c +++ b/mio/lib/htrd.c @@ -1147,7 +1147,7 @@ int mio_htrd_feed (mio_htrd_t* htrd, const mio_bch_t* req, mio_oow_t len, mio_oo /* reset the raw request length */ htrd->fed.s.plen = 0; - if (parse_initial_line_and_headers (htrd, req, ptr - req) <= -1) + if (parse_initial_line_and_headers(htrd, req, ptr - req) <= -1) { return -1; } diff --git a/mio/lib/http-fil.c b/mio/lib/http-fil.c index 0013461..dd6c7ad 100644 --- a/mio/lib/http-fil.c +++ b/mio/lib/http-fil.c @@ -463,6 +463,13 @@ static int file_state_send_header_to_client (file_state_t* file_state, int statu if (file_state->req_method == MIO_HTTP_GET && mio_becs_fcat(cli->sbuf, "ETag: %hs\r\n", file_state->peer_etag) == (mio_oow_t)-1) return -1; if (status_code == 206 && mio_becs_fcat(cli->sbuf, "Content-Ranges: bytes %ju-%ju/%ju\r\n", (mio_uintmax_t)file_state->start_offset, (mio_uintmax_t)file_state->end_offset, (mio_uintmax_t)file_state->total_size) == (mio_oow_t)-1) return -1; + +/* ----- */ +// TODO: Allow-Contents +// Allow-Headers... support custom headers... + if (mio_becs_fcat(cli->sbuf, "Access-Control-Allow-Origin: *\r\n", (mio_uintmax_t)content_length) == (mio_oow_t)-1) return -1; +/* ----- */ + if (mio_becs_fcat(cli->sbuf, "Content-Length: %ju\r\n\r\n", (mio_uintmax_t)content_length) == (mio_oow_t)-1) return -1; return file_state_write_to_client(file_state, MIO_BECS_PTR(cli->sbuf), MIO_BECS_LEN(cli->sbuf)); @@ -548,7 +555,7 @@ static MIO_INLINE int process_range_header (file_state_t* file_state, mio_htre_t return -1; } - if ((st.st_mode & S_IFMT) == S_IFREG) + if ((st.st_mode & S_IFMT) != S_IFREG) { /* TODO: support directory listing if S_IFDIR? still disallow special files. */ file_state_send_final_status_to_client (file_state, 403, 1); /* forbidden */