From 0ed8b1325942e5158f105e7c9bb2408c70af3706 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 5 Mar 2023 00:55:15 +0900 Subject: [PATCH] moved more htts task fields to the header side --- bin/t01.c | 8 ++-- bin/t06.c | 12 +++--- bin/webs.c | 17 ++++++-- lib/hio-htre.h | 1 + lib/hio-http.h | 85 +++++++++++++++++++++++----------------- lib/http-cgi.c | 53 +++++++++++++------------ lib/http-fcgi.c | 57 +++++++++++++++------------ lib/http-file.c | 101 ++++++++++++++++++++---------------------------- lib/http-prv.h | 1 - lib/http-svr.c | 66 ++++++++++++++++++++++++++++++- lib/http-thr.c | 49 ++++++++++++----------- lib/http-txt.c | 29 ++++++++------ lib/http.c | 8 ++-- 13 files changed, 287 insertions(+), 200 deletions(-) diff --git a/bin/t01.c b/bin/t01.c index f43f6dd..326d49c 100644 --- a/bin/t01.c +++ b/bin/t01.c @@ -970,13 +970,13 @@ if (hio_htre_getcontentlen(req) > 0) const hio_bch_t* qpath = hio_htre_getqpath(req); int x; if (hio_comp_bcstr_limited(qpath, "/thr/", 5, 1) == 0) - x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr_request, HIO_NULL, 0); + x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr_request, HIO_NULL, 0, HIO_NULL); else if (hio_comp_bcstr_limited(qpath, "/txt/", 5, 1) == 0) - x = hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_OK, "text/plain", qpath, 0); + x = hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_OK, "text/plain", qpath, 0, HIO_NULL); 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, HIO_NULL); else - x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL); + x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL, HIO_NULL); if (x <= -1) goto oops; } #if 0 diff --git a/bin/t06.c b/bin/t06.c index 93b6ec4..2accc2c 100644 --- a/bin/t06.c +++ b/bin/t06.c @@ -197,21 +197,21 @@ if (hio_htre_getcontentlen(req) > 0) const hio_bch_t* qpath = hio_htre_getqpath(req); int x; if (hio_comp_bcstr_limited(qpath, "/thr/", 5, 1) == 0) - x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr_request, HIO_NULL, HIO_SVC_HTTS_THR_NO_100_CONTINUE); + x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr_request, HIO_NULL, HIO_SVC_HTTS_THR_NO_100_CONTINUE, HIO_NULL); else if (hio_comp_bcstr_limited(qpath, "/thr2/", 6, 1) == 0) - x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr2_request, HIO_NULL, HIO_SVC_HTTS_THR_NO_100_CONTINUE); + x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr2_request, HIO_NULL, HIO_SVC_HTTS_THR_NO_100_CONTINUE, HIO_NULL); else if (hio_comp_bcstr_limited(qpath, "/txt/", 5, 1) == 0) - x = hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_OK, "text/plain", qpath, 0); + x = hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_OK, "text/plain", qpath, 0, HIO_NULL); 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, HIO_NULL); else if (hio_comp_bcstr_limited(qpath, "/fcgi/", 5, 1) == 0) { hio_skad_t fcgis_addr; hio_bcstrtoskad(hio, "127.0.0.1:9000", &fcgis_addr); - x = hio_svc_htts_dofcgi(htts, csck, req, &fcgis_addr, "", qpath + 5, 0); + x = hio_svc_htts_dofcgi(htts, csck, req, &fcgis_addr, "", qpath + 5, 0, HIO_NULL); } else - x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL); + x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL, HIO_NULL); if (x <= -1) goto oops; } #if 0 diff --git a/bin/webs.c b/bin/webs.c index 0eb5930..0040758 100644 --- a/bin/webs.c +++ b/bin/webs.c @@ -298,6 +298,15 @@ oops: return -1; } +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 */ + HIO_INFO3 (hio, "DONE [%hs] [%hs] [%hs] ......................................\n", task->task_req_qmth, task->task_req_qpath, hio_http_status_to_bcstr(task->task_status_code)); +} + 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); @@ -331,7 +340,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_ if (mth == HIO_HTTP_OTHER && hio_comp_bcstr(hio_htre_getqmethodname(req), "UNTAR", 1) == 0 && hio_comp_bcstr(qpath_ext, ".tar", 0) == 0) { /* don't care about the path for now. TODO: make this secure and reasonable */ - if (hio_svc_htts_dothr(htts, csck, req, untar, HIO_NULL, 0) <= -1) goto oops; + if (hio_svc_htts_dothr(htts, csck, req, untar, HIO_NULL, 0, htts_task_on_kill) <= -1) goto oops; } else if (mth == HIO_HTTP_OPTIONS) { @@ -342,7 +351,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_ } else if (hio_comp_bcstr(qpath_ext, ".cgi", 0) == 0) { - if (hio_svc_htts_docgi(htts, csck, req, ext->ai->docroot, qpath, 0) <= -1) goto oops; + if (hio_svc_htts_docgi(htts, csck, req, ext->ai->docroot, qpath, 0, htts_task_on_kill) <= -1) goto oops; } 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*/) { @@ -354,14 +363,14 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_ /* if the document root is relative, it is hard to gurantee that the same document is * true to the fcgi server which is a different process. so map it a blank string for now. * TODO: accept a separate document root for the fcgi server and use it below */ - if (hio_svc_htts_dofcgi(htts, csck, req, &skad, ext->ai->docroot, qpath, 0) <= -1) goto oops; + if (hio_svc_htts_dofcgi(htts, csck, req, &skad, ext->ai->docroot, qpath, 0, htts_task_on_kill) <= -1) goto oops; /*if (hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, "text/plain", "what the...", 0) <= -1) goto oops;*/ } else // if (mth == HIO_HTTP_GET || mth == HIO_HTTP_POST) { /* TODO: proper mime-type */ /* TODO: make HIO_SVC_HTTS_FILE_DIR a cli option */ - if (hio_svc_htts_dofile(htts, csck, req, ext->ai->docroot, qpath, HIO_NULL, 0, &fcbs) <= -1) goto oops; + if (hio_svc_htts_dofile(htts, csck, req, ext->ai->docroot, qpath, HIO_NULL, 0, htts_task_on_kill, &fcbs) <= -1) goto oops; } #if 0 else diff --git a/lib/hio-htre.h b/lib/hio-htre.h index affdbe8..0ae1782 100644 --- a/lib/hio-htre.h +++ b/lib/hio-htre.h @@ -112,6 +112,7 @@ enum hio_http_status_t HIO_HTTP_STATUS_NOT_MODIFIED = 304, HIO_HTTP_STATUS_BAD_REQUEST = 400, + HIO_HTTP_STATUS_UNAUTHORIZED = 401, HIO_HTTP_STATUS_FORBIDDEN = 403, HIO_HTTP_STATUS_NOT_FOUND = 404, HIO_HTTP_STATUS_METHOD_NOT_ALLOWED = 405, diff --git a/lib/hio-http.h b/lib/hio-http.h index ee29975..285b055 100644 --- a/lib/hio-http.h +++ b/lib/hio-http.h @@ -82,6 +82,7 @@ typedef struct hio_svc_httc_t hio_svc_httc_t; /* -------------------------------------------------------------- */ typedef struct hio_svc_htts_task_t hio_svc_htts_task_t; +typedef struct hio_svc_htts_cli_t hio_svc_htts_cli_t; typedef void (*hio_svc_htts_task_on_kill_t) ( hio_svc_htts_task_t* task @@ -95,7 +96,13 @@ typedef void (*hio_svc_htts_task_on_kill_t) ( hio_svc_htts_task_t* task_next; \ hio_svc_htts_task_on_kill_t task_on_kill; \ hio_http_version_t task_req_version; \ - hio_http_method_t task_req_method + hio_http_method_t task_req_method; \ + unsigned int task_req_qpath_ending_with_slash: 1; \ + unsigned int task_req_qpath_is_root: 1; \ + hio_bch_t* task_req_qmth; \ + hio_bch_t* task_req_qpath; \ + hio_http_status_t task_status_code; \ + hio_svc_htts_cli_t* task_client struct hio_svc_htts_task_t { @@ -410,59 +417,65 @@ HIO_EXPORT int hio_svc_htts_getsockaddr ( ); HIO_EXPORT int hio_svc_htts_docgi ( - hio_svc_htts_t* htts, - hio_dev_sck_t* csck, - hio_htre_t* req, - const hio_bch_t* docroot, - const hio_bch_t* script, - int options + hio_svc_htts_t* htts, + hio_dev_sck_t* csck, + hio_htre_t* req, + const hio_bch_t* docroot, + const hio_bch_t* script, + int options, + hio_svc_htts_task_on_kill_t on_kill ); HIO_EXPORT int hio_svc_htts_dofcgi ( - hio_svc_htts_t* htts, - hio_dev_sck_t* csck, - hio_htre_t* req, - const hio_skad_t* fcgis_addr, - const hio_bch_t* docroot, - const hio_bch_t* script, - int options /**< 0 or bitwise-Ored of #hio_svc_htts_file_option_t enumerators */ + hio_svc_htts_t* htts, + hio_dev_sck_t* csck, + hio_htre_t* req, + const hio_skad_t* fcgis_addr, + const hio_bch_t* docroot, + const hio_bch_t* script, + int options, /**< 0 or bitwise-Ored of #hio_svc_htts_file_option_t enumerators */ + hio_svc_htts_task_on_kill_t on_kill ); HIO_EXPORT 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 + 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_task_on_kill_t on_kill, + hio_svc_htts_file_cbs_t* cbs ); HIO_EXPORT int hio_svc_htts_dothr ( - hio_svc_htts_t* htts, - hio_dev_sck_t* csck, - hio_htre_t* req, - hio_svc_htts_thr_func_t func, - void* ctx, - int options + hio_svc_htts_t* htts, + hio_dev_sck_t* csck, + hio_htre_t* req, + hio_svc_htts_thr_func_t func, + void* ctx, + int options, + hio_svc_htts_task_on_kill_t on_kill ); HIO_EXPORT int hio_svc_htts_dotxt ( - hio_svc_htts_t* htts, - hio_dev_sck_t* csck, - hio_htre_t* req, - int status_code, - const hio_bch_t* content_type, - const hio_bch_t* content_text, - int options + hio_svc_htts_t* htts, + hio_dev_sck_t* csck, + hio_htre_t* req, + int status_code, + const hio_bch_t* content_type, + const hio_bch_t* content_text, + int options, + hio_svc_htts_task_on_kill_t on_kill ); HIO_EXPORT hio_svc_htts_task_t* hio_svc_htts_task_make ( hio_svc_htts_t* htts, hio_oow_t task_size, hio_svc_htts_task_on_kill_t on_kill, - hio_htre_t* req + hio_htre_t* req, + hio_svc_htts_cli_t* client ); HIO_EXPORT void hio_svc_htts_task_kill ( diff --git a/lib/http-cgi.c b/lib/http-cgi.c index 304438a..dafd2f1 100644 --- a/lib/http-cgi.c +++ b/lib/http-cgi.c @@ -58,6 +58,8 @@ struct cgi_t { HIO_SVC_HTTS_TASK_HEADER; + hio_svc_htts_task_on_kill_t on_kill; /* user-provided on_kill callback */ + int options; hio_oow_t num_pending_writes_to_client; hio_oow_t num_pending_writes_to_peer; @@ -65,7 +67,6 @@ struct cgi_t hio_htrd_t* peer_htrd; hio_dev_sck_t* csck; - hio_svc_htts_cli_t* client; unsigned int over: 4; /* must be large enough to accomodate CGI_OVER_ALL */ unsigned int keep_alive: 1; @@ -145,7 +146,7 @@ static int cgi_writev_to_client (cgi_t* cgi, hio_iovec_t* iov, hio_iolen_t iovcn static int cgi_send_final_status_to_client (cgi_t* cgi, int status_code, int force_close) { - hio_svc_htts_cli_t* cli = cgi->client; + hio_svc_htts_cli_t* cli = cgi->task_client; hio_bch_t dtbuf[64]; const hio_bch_t* status_msg; hio_oow_t content_len; @@ -169,6 +170,7 @@ static int cgi_send_final_status_to_client (cgi_t* cgi, int status_code, int for if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", content_len, status_msg) == (hio_oow_t)-1) return -1; + cgi->task_status_code = status_code; return (cgi_write_to_client(cgi, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || (force_close && cgi_write_to_client(cgi, HIO_NULL, 0) <= -1))? -1: 0; } @@ -219,13 +221,13 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) old_over = cgi->over; cgi->over |= over_bits; - HIO_DEBUG8 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer, (int)old_over, (int)over_bits, (int)cgi->over); + HIO_DEBUG8 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer, (int)old_over, (int)over_bits, (int)cgi->over); if (!(old_over & CGI_OVER_READ_FROM_CLIENT) && (cgi->over & CGI_OVER_READ_FROM_CLIENT)) { if (cgi->csck && hio_dev_sck_read(cgi->csck, 0) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting client for failure to disable input watching\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting client for failure to disable input watching\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); hio_dev_sck_halt (cgi->csck); } } @@ -234,7 +236,7 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) { if (cgi->peer && hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting peer for failure to disable input watching\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting peer for failure to disable input watching\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); hio_dev_pro_halt (cgi->peer); } } @@ -244,25 +246,25 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) /* ready to stop */ if (cgi->peer) { - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting unneeded peer\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting unneeded peer\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); hio_dev_pro_halt (cgi->peer); } if (cgi->csck) { - HIO_ASSERT (hio, cgi->client != HIO_NULL); + HIO_ASSERT (hio, cgi->task_client != HIO_NULL); if (cgi->keep_alive && !cgi->client_eof_detected) { /* how to arrange to delete this cgi object and put the socket back to the normal waiting state??? */ - HIO_ASSERT (cgi->htts->hio, cgi->client->task == (hio_svc_htts_task_t*)cgi); + HIO_ASSERT (cgi->htts->hio, cgi->task_client->task == (hio_svc_htts_task_t*)cgi); - HIO_SVC_HTTS_TASK_UNREF (cgi->client->task); + HIO_SVC_HTTS_TASK_UNREF (cgi->task_client->task); /* cgi must not be accessed from here down as it could have been destroyed */ } else { - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting peer for no keep-alive\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting peer for no keep-alive\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); hio_dev_sck_shutdown (cgi->csck, HIO_DEV_SCK_SHUTDOWN_WRITE); hio_dev_sck_halt (cgi->csck); } @@ -275,7 +277,9 @@ static void cgi_on_kill (hio_svc_htts_task_t* task) cgi_t* cgi = (cgi_t*)task; hio_t* hio = cgi->htts->hio; - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - killing the task\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - killing the task\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + + if (cgi->on_kill) cgi->on_kill (task); if (cgi->peer) { @@ -297,18 +301,18 @@ static void cgi_on_kill (hio_svc_htts_task_t* task) if (cgi->csck) { - HIO_ASSERT (hio, cgi->client != HIO_NULL); + HIO_ASSERT (hio, cgi->task_client != HIO_NULL); if (cgi->client_org_on_read) cgi->csck->on_read = cgi->client_org_on_read; if (cgi->client_org_on_write) cgi->csck->on_write = cgi->client_org_on_write; if (cgi->client_org_on_disconnect) cgi->csck->on_disconnect = cgi->client_org_on_disconnect; - if (cgi->client_htrd_recbs_changed) hio_htrd_setrecbs (cgi->client->htrd, &cgi->client_htrd_org_recbs); + if (cgi->client_htrd_recbs_changed) hio_htrd_setrecbs (cgi->task_client->htrd, &cgi->client_htrd_org_recbs); if (!cgi->client_disconnected) { if (!cgi->keep_alive || hio_dev_sck_read(cgi->csck, 1) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting client for failure to enable input watching\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - halting client for failure to enable input watching\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); hio_dev_sck_halt (cgi->csck); } } @@ -320,7 +324,7 @@ static void cgi_on_kill (hio_svc_htts_task_t* task) cgi->client_htrd_recbs_changed = 0; if (cgi->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (cgi); /* detach from the htts service only if it's attached */ - HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - killed the task\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - killed the task\n", cgi->htts, cgi, cgi->task_client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); } static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid) @@ -526,7 +530,7 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) { cgi_peer_xtn_t* peer = hio_htrd_getxtn(htrd); cgi_t* cgi = peer->cgi; - hio_svc_htts_cli_t* cli = cgi->client; + hio_svc_htts_cli_t* cli = cgi->task_client; hio_bch_t dtbuf[64]; int status_code = HIO_HTTP_STATUS_OK; const hio_bch_t* status_line = HIO_NULL; @@ -719,7 +723,7 @@ static void cgi_client_on_disconnect (hio_dev_sck_t* sck) cgi->client_disconnected = 1; cgi->csck = HIO_NULL; - cgi->client = HIO_NULL; + cgi->task_client = HIO_NULL; if (cgi->client_org_on_disconnect) { cgi->client_org_on_disconnect (sck); @@ -987,7 +991,7 @@ static int cgi_peer_on_fork (hio_dev_pro_t* pro, void* fork_ctx) return 0; } -int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* script, int options) +int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* script, int options, hio_svc_htts_task_on_kill_t on_kill) { hio_t* hio = htts->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); @@ -1018,16 +1022,17 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r mi.on_fork = cgi_peer_on_fork; mi.fork_ctx = &fc; - cgi = (cgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*cgi), cgi_on_kill, req); + cgi = (cgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*cgi), cgi_on_kill, req, cli); if (HIO_UNLIKELY(!cgi)) goto oops; + cgi->on_kill = on_kill; cgi->options = options; /*cgi->num_pending_writes_to_client = 0; cgi->num_pending_writes_to_peer = 0;*/ cgi->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &cgi->req_content_length); cgi->csck = csck; - cgi->client = cli; + cgi->task_client = cli; /* remember the client socket's io event handlers */ cgi->client_org_on_read = csck->on_read; cgi->client_org_on_write = csck->on_write; @@ -1112,9 +1117,9 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r if (cgi->req_content_length_unlimited) { /* change the callbacks to subscribe to contents to be uploaded */ - cgi->client_htrd_org_recbs = *hio_htrd_getrecbs(cgi->client->htrd); + cgi->client_htrd_org_recbs = *hio_htrd_getrecbs(cgi->task_client->htrd); cgi_client_htrd_recbs.peek = cgi->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (cgi->client->htrd, &cgi_client_htrd_recbs); + hio_htrd_setrecbs (cgi->task_client->htrd, &cgi_client_htrd_recbs); cgi->client_htrd_recbs_changed = 1; } else @@ -1123,9 +1128,9 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r if (cgi->req_content_length > 0) { /* change the callbacks to subscribe to contents to be uploaded */ - cgi->client_htrd_org_recbs = *hio_htrd_getrecbs(cgi->client->htrd); + cgi->client_htrd_org_recbs = *hio_htrd_getrecbs(cgi->task_client->htrd); cgi_client_htrd_recbs.peek = cgi->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (cgi->client->htrd, &cgi_client_htrd_recbs); + hio_htrd_setrecbs (cgi->task_client->htrd, &cgi_client_htrd_recbs); cgi->client_htrd_recbs_changed = 1; } else diff --git a/lib/http-fcgi.c b/lib/http-fcgi.c index 16e2945..b123188 100644 --- a/lib/http-fcgi.c +++ b/lib/http-fcgi.c @@ -27,13 +27,14 @@ struct fcgi_t { HIO_SVC_HTTS_TASK_HEADER; + hio_svc_htts_task_on_kill_t on_kill; /* user-provided on_kill callback */ + hio_oow_t num_pending_writes_to_client; hio_oow_t num_pending_writes_to_peer; hio_svc_fcgic_sess_t* peer; hio_htrd_t* peer_htrd; hio_dev_sck_t* csck; - hio_svc_htts_cli_t* client; unsigned int over: 4; /* must be large enough to accomodate FCGI_OVER_ALL */ unsigned int keep_alive: 1; @@ -130,7 +131,7 @@ static int fcgi_writev_to_client (fcgi_t* fcgi, hio_iovec_t* iov, hio_iolen_t io static int fcgi_send_final_status_to_client (fcgi_t* fcgi, int status_code, int force_close) { - hio_svc_htts_cli_t* cli = fcgi->client; + hio_svc_htts_cli_t* cli = fcgi->task_client; hio_bch_t dtbuf[64]; const hio_bch_t* status_msg; hio_oow_t content_len; @@ -156,6 +157,7 @@ static int fcgi_send_final_status_to_client (fcgi_t* fcgi, int status_code, int if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", content_len, status_msg) == (hio_oow_t)-1) return -1; + fcgi->task_status_code = status_code; return (fcgi_write_to_client(fcgi, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || (force_close && fcgi_write_to_client(fcgi, HIO_NULL, 0) <= -1))? -1: 0; } @@ -213,7 +215,7 @@ static HIO_INLINE void fcgi_mark_over (fcgi_t* fcgi, int over_bits) old_over = fcgi->over; fcgi->over |= over_bits; - HIO_DEBUG8 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", fcgi->htts, fcgi, fcgi->client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer, (int)old_over, (int)over_bits, (int)fcgi->over); + HIO_DEBUG8 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", fcgi->htts, fcgi, fcgi->task_client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer, (int)old_over, (int)over_bits, (int)fcgi->over); if (!(old_over & FCGI_OVER_READ_FROM_CLIENT) && (fcgi->over & FCGI_OVER_READ_FROM_CLIENT)) { @@ -249,7 +251,7 @@ HIO_DEBUG2 (hio, "HTTS(%p) - ALL OVER keeping client(%p) alive\n", fcgi->htts, f if (fcgi->keep_alive && !fcgi->client_eof_detected) { HIO_DEBUG2 (hio, "HTTS(%p) - keeping client(%p) alive\n", fcgi->htts, fcgi->csck); - HIO_ASSERT (fcgi->htts->hio, fcgi->client->task == (hio_svc_htts_task_t*)fcgi); + HIO_ASSERT (fcgi->htts->hio, fcgi->task_client->task == (hio_svc_htts_task_t*)fcgi); unbind_task_from_client (fcgi, 1); /* fcgi must not be accessed from here down as it could have been destroyed in unbind_task_from_client() */ } @@ -268,11 +270,13 @@ static void fcgi_on_kill (hio_svc_htts_task_t* task) fcgi_t* fcgi = (fcgi_t*)task; hio_t* hio = fcgi->htts->hio; - HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - killing the task\n", fcgi->htts, fcgi, fcgi->client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - killing the task\n", fcgi->htts, fcgi, fcgi->task_client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); + + if (fcgi->on_kill) fcgi->on_kill (task); /* [NOTE] * 1. if hio_svc_htts_task_kill() is called, fcgi->peer, fcgi->peer_htrd, fcgi->csck, - * fcgi->client may not not null. + * fcgi->task_client may not not null. * 2. this callback function doesn't decrement the reference count on fcgi because * this is the fcgi destruction call-back. */ @@ -290,14 +294,14 @@ static void fcgi_on_kill (hio_svc_htts_task_t* task) if (fcgi->csck) { - HIO_ASSERT (hio, fcgi->client != HIO_NULL); + HIO_ASSERT (hio, fcgi->task_client != HIO_NULL); unbind_task_from_client (fcgi, 0); } /* detach from the htts service only if it's attached */ if (fcgi->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (fcgi); - HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - killed the task\n", fcgi->htts, fcgi, fcgi->client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - killed the task\n", fcgi->htts, fcgi, fcgi->task_client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); } static void fcgi_peer_on_untie (hio_svc_fcgic_sess_t* peer, void* ctx) @@ -309,7 +313,7 @@ static void fcgi_peer_on_untie (hio_svc_fcgic_sess_t* peer, void* ctx) * fcgi_halt_participating_devices() calls hio_svc_fcgi_untie() again * to cause an infinite loop if we don't reset fcgi->peer to HIO_NULL here */ - HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - peer untied\n", fcgi->htts, fcgi, fcgi->client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - peer untied\n", fcgi->htts, fcgi, fcgi->task_client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); fcgi->peer = HIO_NULL; fcgi_write_last_chunk_to_client (fcgi); @@ -429,7 +433,7 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) { fcgi_peer_xtn_t* peer = hio_htrd_getxtn(htrd); fcgi_t* fcgi = peer->fcgi; - hio_svc_htts_cli_t* cli = fcgi->client; + hio_svc_htts_cli_t* cli = fcgi->task_client; hio_bch_t dtbuf[64]; int status_code = HIO_HTTP_STATUS_OK; const hio_bch_t* status_line = HIO_NULL; @@ -463,6 +467,7 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) * the value may contain more than numbers */ if (code_len > 0 && desc_len > 0) { + /* use the whole line as it is - e.g. Status: 302 Moved*/ status_line = req->attr.status; } else @@ -873,7 +878,7 @@ static void bind_task_to_client (fcgi_t* fcgi, hio_dev_sck_t* csck) HIO_ASSERT (fcgi->htts->hio, cli->task == HIO_NULL); fcgi->csck = csck; - fcgi->client = cli; + fcgi->task_client = cli; /* remember the client socket's io event handlers */ fcgi->client_org_on_read = csck->on_read; @@ -893,14 +898,14 @@ static void unbind_task_from_client (fcgi_t* fcgi, int rcdown) { hio_dev_sck_t* csck = fcgi->csck; - HIO_ASSERT (fcgi->htts->hio, fcgi->client != HIO_NULL); + HIO_ASSERT (fcgi->htts->hio, fcgi->task_client != HIO_NULL); HIO_ASSERT (fcgi->htts->hio, fcgi->csck != HIO_NULL); - HIO_ASSERT (fcgi->htts->hio, fcgi->client->task == (hio_svc_htts_task_t*)fcgi); - HIO_ASSERT (fcgi->htts->hio, fcgi->client->htrd != HIO_NULL); + HIO_ASSERT (fcgi->htts->hio, fcgi->task_client->task == (hio_svc_htts_task_t*)fcgi); + HIO_ASSERT (fcgi->htts->hio, fcgi->task_client->htrd != HIO_NULL); if (fcgi->client_htrd_recbs_changed) { - hio_htrd_setrecbs (fcgi->client->htrd, &fcgi->client_htrd_org_recbs); + hio_htrd_setrecbs (fcgi->task_client->htrd, &fcgi->client_htrd_org_recbs); fcgi->client_htrd_recbs_changed = 0; } @@ -922,13 +927,13 @@ static void unbind_task_from_client (fcgi_t* fcgi, int rcdown) fcgi->client_org_on_disconnect = HIO_NULL; } -HIO_DEBUG2 (csck->hio, "UNBINDING CLEINT FROM TASK... client=%p csck=%p\n", fcgi->client, csck); +HIO_DEBUG2 (csck->hio, "UNBINDING CLEINT FROM TASK... client=%p csck=%p\n", fcgi->task_client, csck); /* there is some ordering issue in using HIO_SVC_HTTS_TASK_UNREF() - * because it can destroy the fcgi itself. so reset fcgi->client->task + * because it can destroy the fcgi itself. so reset fcgi->task_client->task * to null and call RCDOWN() later */ - fcgi->client->task = HIO_NULL; - fcgi->client = HIO_NULL; + fcgi->task_client->task = HIO_NULL; + fcgi->task_client = HIO_NULL; fcgi->csck = HIO_NULL; if (!fcgi->client_disconnected && (!fcgi->keep_alive || hio_dev_sck_read(csck, 1) <= -1)) @@ -1056,9 +1061,9 @@ static int setup_for_content_length(fcgi_t* fcgi, hio_htre_t* req) if (fcgi->req_content_length_unlimited) { /* change the callbacks to subscribe to contents to be uploaded */ - fcgi->client_htrd_org_recbs = *hio_htrd_getrecbs(fcgi->client->htrd); + fcgi->client_htrd_org_recbs = *hio_htrd_getrecbs(fcgi->task_client->htrd); fcgi_client_htrd_recbs.peek = fcgi->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (fcgi->client->htrd, &fcgi_client_htrd_recbs); + hio_htrd_setrecbs (fcgi->task_client->htrd, &fcgi_client_htrd_recbs); fcgi->client_htrd_recbs_changed = 1; } else @@ -1067,9 +1072,9 @@ static int setup_for_content_length(fcgi_t* fcgi, hio_htre_t* req) if (fcgi->req_content_length > 0) { /* change the callbacks to subscribe to contents to be uploaded */ - fcgi->client_htrd_org_recbs = *hio_htrd_getrecbs(fcgi->client->htrd); + fcgi->client_htrd_org_recbs = *hio_htrd_getrecbs(fcgi->task_client->htrd); fcgi_client_htrd_recbs.peek = fcgi->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (fcgi->client->htrd, &fcgi_client_htrd_recbs); + hio_htrd_setrecbs (fcgi->task_client->htrd, &fcgi_client_htrd_recbs); fcgi->client_htrd_recbs_changed = 1; } else @@ -1099,9 +1104,10 @@ static int setup_for_content_length(fcgi_t* fcgi, hio_htre_t* req) return 0; } -int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_skad_t* fcgis_addr, const hio_bch_t* docroot, const hio_bch_t* script, int options) +int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_skad_t* fcgis_addr, const hio_bch_t* docroot, const hio_bch_t* script, int options, hio_svc_htts_task_on_kill_t on_kill) { hio_t* hio = htts->hio; + hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); fcgi_t* fcgi = HIO_NULL; /* ensure that you call this function before any contents is received */ @@ -1113,9 +1119,10 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* goto oops; } - fcgi = (fcgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*fcgi), fcgi_on_kill, req); + fcgi = (fcgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*fcgi), fcgi_on_kill, req, cli); if (HIO_UNLIKELY(!fcgi)) goto oops; + fcgi->on_kill = on_kill; /*fcgi->num_pending_writes_to_client = 0; fcgi->num_pending_writes_to_peer = 0;*/ fcgi->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &fcgi->req_content_length); diff --git a/lib/http-file.c b/lib/http-file.c index 4e73609..81cebda 100644 --- a/lib/http-file.c +++ b/lib/http-file.c @@ -53,6 +53,8 @@ struct file_t { HIO_SVC_HTTS_TASK_HEADER; + hio_svc_htts_task_on_kill_t on_kill; /* user-provided on_kill callback */ + int options; hio_svc_htts_file_cbs_t* cbs; hio_oow_t num_pending_writes_to_client; @@ -68,14 +70,10 @@ struct file_t hio_bch_t peer_etag[128]; hio_dev_sck_t* csck; - hio_svc_htts_cli_t* client; - hio_bch_t* req_qpath; unsigned int over: 4; /* must be large enough to accomodate FILE_OVER_ALL */ unsigned int keep_alive: 1; unsigned int req_content_length_unlimited: 1; - unsigned int req_qpath_ending_with_slash: 1; - unsigned int req_qpath_is_root: 1; unsigned int ever_attempted_to_write_to_client: 1; unsigned int client_eof_detected: 1; unsigned int client_disconnected: 1; @@ -136,9 +134,9 @@ static int file_sendfile_to_client (file_t* file, hio_foff_t foff, hio_iolen_t l return 0; } -static int file_send_final_status_to_client (file_t* file, int status_code, int force_close, int dir_redirect) +static int file_send_final_status_to_client (file_t* file, int status_code, int force_close) { - hio_svc_htts_cli_t* cli = file->client; + hio_svc_htts_cli_t* cli = file->task_client; hio_bch_t dtbuf[64]; const hio_bch_t* status_msg; hio_oow_t content_len; @@ -160,16 +158,17 @@ static int file_send_final_status_to_client (file_t* file, int status_code, int status_msg = ""; } - if (dir_redirect) + if (status_code == HIO_HTTP_STATUS_MOVED_PERMANENTLY || status_code == HIO_HTTP_STATUS_MOVED_TEMPORARILY) { /* don't send content body when the status code is 3xx. include the Location header only. */ - if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: 0\r\nLocation: %hs/\r\n\r\n", file->req_qpath) == (hio_oow_t)-1) return -1; + if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: 0\r\nLocation: %hs%hs\r\n\r\n", file->task_req_qpath, (file->task_req_qpath_ending_with_slash? "": "/")) == (hio_oow_t)-1) return -1; } else { if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", content_len, status_msg) == (hio_oow_t)-1) return -1; } + file->task_status_code = status_code; return (file_write_to_client(file, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || (force_close && file_write_to_client(file, HIO_NULL, 0) <= -1))? -1: 0; } @@ -236,8 +235,8 @@ static void file_mark_over (file_t* file, int over_bits) #endif /* how to arrange to delete this file object and put the socket back to the normal waiting state??? */ - HIO_ASSERT (file->htts->hio, file->client->task == (hio_svc_htts_task_t*)file); - HIO_SVC_HTTS_TASK_UNREF (file->client->task); + HIO_ASSERT (file->htts->hio, file->task_client->task == (hio_svc_htts_task_t*)file); + HIO_SVC_HTTS_TASK_UNREF (file->task_client->task); /* the file task must not be accessed from here down as it could have been destroyed */ } else @@ -245,7 +244,7 @@ static void file_mark_over (file_t* file, int over_bits) HIO_DEBUG4 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting client for %hs\n", file->htts, (int)file->csck->hnd, file->peer, (file->client_eof_detected? "EOF detected": "no keep-alive")); hio_dev_sck_shutdown (file->csck, HIO_DEV_SCK_SHUTDOWN_WRITE); hio_dev_sck_halt (file->csck); - /* the file task will be detached from file->client->task by the upstream disconnect handler in http_svr.c */ + /* the file task will be detached from file->task_client->task by the upstream disconnect handler in http_svr.c */ } } } @@ -285,26 +284,22 @@ static void file_on_kill (hio_svc_htts_task_t* task) file_t* file = (file_t*)task; hio_t* hio = file->htts->hio; - HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - killing the task\n", file->htts, file, file->client, (file->csck? file->csck->hnd: -1), file->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - killing the task\n", file->htts, file, file->task_client, (file->csck? file->csck->hnd: -1), file->peer); + + if (file->on_kill) file->on_kill (task); file_close_peer (file); - if (file->req_qpath) - { - hio_freemem (hio, file->req_qpath); - file->req_qpath = HIO_NULL; - } - if (file->csck) { if (file->client_org_on_read) file->csck->on_read = file->client_org_on_read; if (file->client_org_on_write) file->csck->on_write = file->client_org_on_write; if (file->client_org_on_disconnect) file->csck->on_disconnect = file->client_org_on_disconnect; - if (file->client_htrd_recbs_changed) hio_htrd_setrecbs (file->client->htrd, &file->client_htrd_org_recbs); + if (file->client_htrd_recbs_changed) hio_htrd_setrecbs (file->task_client->htrd, &file->client_htrd_org_recbs); if (!file->keep_alive || hio_dev_sck_read(file->csck, 1) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - halting client for failure to enable input watching\n", file->htts, file, file->client, (file->csck? file->csck->hnd: -1), file->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - halting client for failure to enable input watching\n", file->htts, file, file->task_client, (file->csck? file->csck->hnd: -1), file->peer); hio_dev_sck_halt (file->csck); } } @@ -315,7 +310,7 @@ static void file_on_kill (hio_svc_htts_task_t* task) file->client_htrd_recbs_changed = 0; if (file->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (file); /* detach from the htts service only if it's attached */ - HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - killed the task\n", file->htts, file, file->client, (file->csck? file->csck->hnd: -1), file->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - killed the task\n", file->htts, file, file->task_client, (file->csck? file->csck->hnd: -1), file->peer); } static void file_client_on_disconnect (hio_dev_sck_t* sck) @@ -332,7 +327,7 @@ static void file_client_on_disconnect (hio_dev_sck_t* sck) file->client_disconnected = 1; file->csck = HIO_NULL; - file->client = HIO_NULL; + file->task_client = HIO_NULL; if (file->client_org_on_disconnect) { file->client_org_on_disconnect (sck); @@ -356,21 +351,21 @@ static int file_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t if (len <= -1) { /* read error */ - HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) read error on client\n", file->client->htts, (int)sck->hnd, file->peer); + HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) read error on client\n", file->task_client->htts, (int)sck->hnd, file->peer); goto oops; } if (file->peer <= -1) { /* the peer is gone or not even opened */ - HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) read on client, no peer to write\n", file->client->htts, (int)sck->hnd, file->peer); + HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) read on client, no peer to write\n", file->task_client->htts, (int)sck->hnd, file->peer); goto oops; /* do what? just return 0? */ } if (len == 0) { /* EOF on the client side. arrange to close */ - HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) EOF detected on client\n", file->client->htts, (int)sck->hnd, file->peer); + HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) EOF detected on client\n", file->task_client->htts, (int)sck->hnd, file->peer); file->client_eof_detected = 1; if (!(file->over & FILE_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without file_client_htrd_poke() */ @@ -392,7 +387,7 @@ static int file_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t if (rem > 0) { /* TODO store this to client buffer. once the current resource is completed, arrange to call on_read() with it */ - HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) excessive data after contents on client\n", file->client->htts, (int)sck->hnd, file->peer); + HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) excessive data after contents on client\n", file->task_client->htts, (int)sck->hnd, file->peer); } } @@ -414,7 +409,7 @@ static int file_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wr if (wrlen <= -1) { - HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) unable to write to client\n", file->client->htts, (int)sck->hnd, file->peer); + HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) unable to write to client\n", file->task_client->htts, (int)sck->hnd, file->peer); goto oops; } @@ -423,7 +418,7 @@ static int file_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wr /* if the connect is keep-alive, this part may not be called */ file->num_pending_writes_to_client--; HIO_ASSERT (hio, file->num_pending_writes_to_client == 0); - HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) indicated EOF to client\n", file->client->htts, (int)sck->hnd, file->peer); + HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) indicated EOF to client\n", file->task_client->htts, (int)sck->hnd, file->peer); /* since EOF has been indicated to the client, it must not write to the client any further. * this also means that i don't need any data from the peer side either. * i don't need to enable input watching on the peer side */ @@ -466,7 +461,7 @@ static int file_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req) if (file->task_req_method != HIO_HTTP_GET) { - if (file_send_final_status_to_client(file, HIO_HTTP_STATUS_OK, 0, 0) <= -1) return -1; + if (file_send_final_status_to_client(file, HIO_HTTP_STATUS_OK, 0) <= -1) return -1; } file_mark_over (file, FILE_OVER_READ_FROM_CLIENT); @@ -495,7 +490,7 @@ static hio_htrd_recbs_t file_client_htrd_recbs = static int file_send_header_to_client (file_t* file, int status_code, int force_close, const hio_bch_t* mime_type) { - hio_svc_htts_cli_t* cli = file->client; + hio_svc_htts_cli_t* cli = file->task_client; hio_bch_t dtbuf[64]; hio_foff_t content_length; @@ -727,7 +722,7 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int { int alt_fd; - if (!file->req_qpath_ending_with_slash) + if (!file->task_req_qpath_ending_with_slash) { *error_status = HIO_HTTP_STATUS_MOVED_PERMANENTLY; close (file->peer); @@ -735,7 +730,7 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int return -1; } - alt_fd = file->cbs->open_dir_list(file->htts, file->req_qpath, actual_file, res_mime_type, file->cbs->ctx); + alt_fd = file->cbs->open_dir_list(file->htts, file->task_req_qpath, actual_file, res_mime_type, file->cbs->ctx); if (alt_fd >= 0) { close (file->peer); @@ -747,7 +742,7 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int if (res_mime_type && file->cbs && file->cbs->get_mime_type) { const hio_bch_t* mime_type; - mime_type = file->cbs->get_mime_type(file->htts, file->req_qpath, actual_file, file->cbs->ctx); + mime_type = file->cbs->get_mime_type(file->htts, file->task_req_qpath, actual_file, file->cbs->ctx); if (mime_type) *res_mime_type = mime_type; } } @@ -767,14 +762,12 @@ static HIO_INLINE void set_tcp_cork (hio_dev_sck_t* sck) #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, hio_svc_htts_file_cbs_t* cbs) +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_task_on_kill_t on_kill, hio_svc_htts_file_cbs_t* cbs) { hio_t* hio = htts->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); file_t* file = HIO_NULL; hio_bch_t* actual_file = HIO_NULL; - hio_bch_t* req_qpath = HIO_NULL; - int dir_redirect = 0; int status_code; /* ensure that you call this function before any contents is received */ @@ -783,29 +776,22 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* HIO_DEBUG5 (hio, "HTTS(%p) - file(c=%d) - [%hs] %hs%hs\n", htts, (int)csck->hnd, cli->cli_addr_bcstr, (docroot[0] == '/' && docroot[1] == '\0' && filepath[0] == '/'? "": docroot), filepath); -/* TODO: may have to use getorgqpath() for the percent-decoding... */ - req_qpath = hio_dupbcstr(hio, hio_htre_getqpath(req), HIO_NULL); - if (HIO_UNLIKELY(!req_qpath)) goto oops; - actual_file = hio_svc_htts_dupmergepaths(htts, docroot, filepath); if (HIO_UNLIKELY(!actual_file)) goto oops; - file = (file_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*file), file_on_kill, req); + file = (file_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*file), file_on_kill, req, cli); if (HIO_UNLIKELY(!file)) goto oops; + file->on_kill = on_kill; file->options = options; file->cbs = cbs; /* the given pointer must outlive the lifespan of the while file handling cycle. */ file->sendfile_ok = hio_dev_sck_sendfileok(csck); /*file->num_pending_writes_to_client = 0; file->num_pending_writes_to_peer = 0;*/ file->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &file->req_content_length); - file->req_qpath = req_qpath; - req_qpath = HIO_NULL; /* delegated to the file task */ - file->req_qpath_ending_with_slash = (hio_htre_getqpathlen(req) > 0 && hio_htre_getqpath(req)[hio_htre_getqpathlen(req) - 1] == '/'); - file->req_qpath_is_root = (hio_htre_getqpathlen(req) == 1 && hio_htre_getqpath(req)[0] == '/'); file->csck = csck; - file->client = cli; + file->task_client = cli; file->client_org_on_read = csck->on_read; file->client_org_on_write = csck->on_write; file->client_org_on_disconnect = csck->on_disconnect; @@ -827,7 +813,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* /* option 1. buffer contents. if it gets too large, send 413 Request Entity Too Large. * option 2. send 411 Length Required immediately * option 3. set Content-Length to -1 and use EOF to indicate the end of content [Non-Standard] */ - if (file_send_final_status_to_client(file, HIO_HTTP_STATUS_LENGTH_REQUIRED, 1, 0) <= -1) goto oops; + if (file_send_final_status_to_client(file, HIO_HTTP_STATUS_LENGTH_REQUIRED, 1) <= -1) goto oops; } #endif @@ -849,7 +835,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* else if (req->flags & HIO_HTRE_ATTR_EXPECT) { /* 417 Expectation Failed */ - file_send_final_status_to_client (file, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1, 0); + file_send_final_status_to_client (file, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1); goto oops; } @@ -857,9 +843,9 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* if (file->req_content_length_unlimited) { /* change the callbacks to subscribe to contents to be uploaded */ - file->client_htrd_org_recbs = *hio_htrd_getrecbs(file->client->htrd); + file->client_htrd_org_recbs = *hio_htrd_getrecbs(file->task_client->htrd); file_client_htrd_recbs.peek = file->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (file->client->htrd, &file_client_htrd_recbs); + hio_htrd_setrecbs (file->task_client->htrd, &file_client_htrd_recbs); file->client_htrd_recbs_changed = 1; } else @@ -868,9 +854,9 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* if (file->req_content_length > 0) { /* change the callbacks to subscribe to contents to be uploaded */ - file->client_htrd_org_recbs = *hio_htrd_getrecbs(file->client->htrd); + file->client_htrd_org_recbs = *hio_htrd_getrecbs(file->task_client->htrd); file_client_htrd_recbs.peek = file->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (file->client->htrd, &file_client_htrd_recbs); + hio_htrd_setrecbs (file->task_client->htrd, &file_client_htrd_recbs); file->client_htrd_recbs_changed = 1; } else @@ -909,12 +895,8 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* { const hio_bch_t* actual_mime_type = mime_type; - if (open_peer_with_mode(file, actual_file, O_RDONLY, &status_code, (mime_type? HIO_NULL: &actual_mime_type)) <= -1) - { - dir_redirect = (status_code == HIO_HTTP_STATUS_MOVED_PERMANENTLY); /* to handle the special rediret of a directory not ending with a slash in the request url */ - goto done_with_status_code; - } - if (process_range_header(file, req, &status_code) <= -1) goto done_with_status_code; + if (open_peer_with_mode(file, actual_file, O_RDONLY, &status_code, (mime_type? HIO_NULL: &actual_mime_type)) <= -1 || + process_range_header(file, req, &status_code) <= -1) goto done_with_status_code; if (HIO_LIKELY(file->task_req_method == HIO_HTTP_GET)) { @@ -978,7 +960,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* default: status_code = HIO_HTTP_STATUS_METHOD_NOT_ALLOWED; done_with_status_code: - if (file_send_final_status_to_client(file, status_code, 0, dir_redirect) <= -1) goto oops; + if (file_send_final_status_to_client(file, status_code, 0) <= -1) goto oops; done_with_status_code_2: file_mark_over (file, FILE_OVER_READ_FROM_PEER | FILE_OVER_WRITE_TO_PEER); break; @@ -995,6 +977,5 @@ oops: HIO_DEBUG2 (hio, "HTTS(%p) - file(c=%d) failure\n", htts, csck->hnd); if (file) file_halt_participating_devices (file); if (actual_file) hio_freemem (hio, actual_file); - if (req_qpath) hio_freemem (hio, req_qpath); return -1; } diff --git a/lib/http-prv.h b/lib/http-prv.h index 7c04fd9..3c5e79e 100644 --- a/lib/http-prv.h +++ b/lib/http-prv.h @@ -30,7 +30,6 @@ #include #include "hio-prv.h" -typedef struct hio_svc_htts_cli_t hio_svc_htts_cli_t; struct hio_svc_htts_cli_t { hio_svc_htts_cli_t* cli_prev; diff --git a/lib/http-svr.c b/lib/http-svr.c index d65bdce..0f1e8d4 100644 --- a/lib/http-svr.c +++ b/lib/http-svr.c @@ -653,14 +653,18 @@ int hio_svc_htts_getsockaddr (hio_svc_htts_t* htts, hio_oow_t idx, hio_skad_t* s * * you can pass sizeof(my_task_t) to hio_svc_htts_task_make() */ -hio_svc_htts_task_t* hio_svc_htts_task_make (hio_svc_htts_t* htts, hio_oow_t task_size, hio_svc_htts_task_on_kill_t on_kill, hio_htre_t* req) +hio_svc_htts_task_t* hio_svc_htts_task_make (hio_svc_htts_t* htts, hio_oow_t task_size, hio_svc_htts_task_on_kill_t on_kill, hio_htre_t* req, hio_svc_htts_cli_t* cli) { hio_t* hio = htts->hio; hio_svc_htts_task_t* task; + hio_oow_t qpath_len, qmth_len; HIO_DEBUG1 (hio, "HTTS(%p) - allocating task\n", htts); - task = hio_callocmem(hio, task_size); + qpath_len = hio_htre_getqpathlen(req); + qmth_len = hio_htre_getqmethodlen(req); + + task = hio_callocmem(hio, task_size + qmth_len + 1 + qpath_len + 1); if (HIO_UNLIKELY(!task)) { HIO_DEBUG1 (hio, "HTTS(%p) - failed to allocate task\n", htts); @@ -673,6 +677,14 @@ hio_svc_htts_task_t* hio_svc_htts_task_make (hio_svc_htts_t* htts, hio_oow_t tas task->task_on_kill = on_kill; task->task_req_method = hio_htre_getqmethodtype(req); task->task_req_version = *hio_htre_getversion(req); + task->task_req_qpath_ending_with_slash = (hio_htre_getqpathlen(req) > 0 && hio_htre_getqpath(req)[hio_htre_getqpathlen(req) - 1] == '/'); + task->task_req_qpath_is_root = (hio_htre_getqpathlen(req) == 1 && hio_htre_getqpath(req)[0] == '/'); + task->task_req_qmth = (hio_bch_t*)((hio_uint8_t*)task + task_size); + task->task_req_qpath = task->task_req_qmth + qmth_len + 1; + task->task_client = cli; + + HIO_MEMCPY (task->task_req_qmth, hio_htre_getqmethodname(req),qmth_len + 1); + HIO_MEMCPY (task->task_req_qpath, hio_htre_getqpath(req), qpath_len + 1); HIO_DEBUG2 (hio, "HTTS(%p) - allocated task %p\n", htts, task); return task; @@ -691,6 +703,56 @@ void hio_svc_htts_task_kill (hio_svc_htts_task_t* task) HIO_DEBUG2 (hio, "HTTS(%p) - destroyed task %p\n", htts, task); } +#if 0 +int hio_svc_https_task_sendfinal (hio_svc_htts_task_t* task, int status_code, const hio_bch_t* content_type, const hio_bch_t* content_text, int force_close) +{ + hio_svc_htts_cli_t* cli = task->task_client; + hio_bch_t dtbuf[64]; + hio_oow_t content_len; + const hio_bch_t* status_msg; + + status_msg = hio_http_status_to_bcstr(status_code); + hio_svc_htts_fmtgmtime (task->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf)); + + if (!force_close) force_close = !task->task_keep_alive; + if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n", + task->task_req_version.major, task->task_req_version.minor, + status_code, status_msg, + cli->htts->server_name, dtbuf, + (force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1; + + if (!content_text) content_text = status_msg; + + content_len = hio_count_bcstr(content_text); + if (task->task_req_method == HIO_HTTP_HEAD) + { + /* if status code is 200, the content is retained but the actual content is discarded. */ + if (status_code != HIO_HTTP_STATUS_OK) content_len = 0; + content_text = ""; + } + else if (status_code == HIO_HTTP_STATUS_MOVED_PERMANENTLY || status_code == HIO_HTTP_STATUS_MOVED_TEMPORARILY) + { + content_len = 0; + } + + if (content_type && hio_becs_fcat(cli->sbuf, "Content-Type: %hs\r\n", content_type) == (hio_oow_t)-1) return -1; + + if (status_code == HIO_HTTP_STATUS_MOVED_PERMANENTLY || status_code == HIO_HTTP_STATUS_MOVED_TEMPORARILY) + { + /* don't send content body when the status code is 3xx. include the Location header only. */ + if (hio_becs_fcat(cli->sbuf, "Content-Length: 0\r\nLocation: %hs%hs\r\n\r\n", task->task_req_qpath, (task->task_req_qpath_ending_with_slash? "": "/")) == (hio_oow_t)-1) return -1; + } + else + { + if (hio_becs_fcat(cli->sbuf, "nContent-Length: %zu\r\n\r\n%hs", content_len, content_text) == (hio_oow_t)-1) return -1; + } + + task->task_status_code = status_code; + return (task_write_to_client(task, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || + (force_close && task_write_to_client(file, HIO_NULL, 0) <= -1))? -1: 0; +} +#endif + /* ----------------------------------------------------------------- */ int hio_svc_htts_doproxy (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* upstream) diff --git a/lib/http-thr.c b/lib/http-thr.c index 1e750a7..f0da146 100644 --- a/lib/http-thr.c +++ b/lib/http-thr.c @@ -61,13 +61,14 @@ struct thr_task_t { HIO_SVC_HTTS_TASK_HEADER; + hio_svc_htts_task_on_kill_t on_kill; /* user-provided on_kill callback */ + int options; hio_oow_t num_pending_writes_to_client; hio_oow_t num_pending_writes_to_peer; hio_dev_thr_t* peer; hio_htrd_t* peer_htrd; hio_dev_sck_t* csck; - hio_svc_htts_cli_t* client; unsigned int over: 4; /* must be large enough to accomodate THR_TASK_OVER_ALL */ unsigned int keep_alive: 1; @@ -146,7 +147,7 @@ static int thr_task_writev_to_client (thr_task_t* thr_task, hio_iovec_t* iov, hi static int thr_task_send_final_status_to_client (thr_task_t* thr_task, int status_code, int force_close) { - hio_svc_htts_cli_t* cli = thr_task->client; + hio_svc_htts_cli_t* cli = thr_task->task_client; hio_bch_t dtbuf[64]; const hio_bch_t* status_msg; hio_oow_t content_len; @@ -170,6 +171,7 @@ static int thr_task_send_final_status_to_client (thr_task_t* thr_task, int statu if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", content_len, status_msg) == (hio_oow_t)-1) return -1; + thr_task->task_status_code = status_code; return (thr_task_write_to_client(thr_task, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || (force_close && thr_task_write_to_client(thr_task, HIO_NULL, 0) <= -1))? -1: 0; } @@ -219,13 +221,13 @@ static HIO_INLINE void thr_task_mark_over (thr_task_t* thr_task, int over_bits) old_over = thr_task->over; thr_task->over |= over_bits; - HIO_DEBUG8 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer, (int)old_over, (int)over_bits, (int)thr_task->over); + HIO_DEBUG8 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer, (int)old_over, (int)over_bits, (int)thr_task->over); if (!(old_over & THR_TASK_OVER_READ_FROM_CLIENT) && (thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) { if (thr_task->csck && hio_dev_sck_read(thr_task->csck, 0) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for failure to disable input watching\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for failure to disable input watching\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); hio_dev_sck_halt (thr_task->csck); } } @@ -234,7 +236,7 @@ static HIO_INLINE void thr_task_mark_over (thr_task_t* thr_task, int over_bits) { if (thr_task->peer && hio_dev_thr_read(thr_task->peer, 0) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer for failure to disable input watching\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer for failure to disable input watching\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); hio_dev_thr_halt (thr_task->peer); } } @@ -244,24 +246,24 @@ static HIO_INLINE void thr_task_mark_over (thr_task_t* thr_task, int over_bits) /* ready to stop */ if (thr_task->peer) { - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer as it is unneeded\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer as it is unneeded\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); hio_dev_thr_halt (thr_task->peer); } if (thr_task->csck) { - HIO_ASSERT (hio, thr_task->client != HIO_NULL); + HIO_ASSERT (hio, thr_task->task_client != HIO_NULL); if (thr_task->keep_alive && !thr_task->client_eof_detected) { /* how to arrange to delete this thr_task object and put the socket back to the normal waiting state??? */ - HIO_ASSERT (thr_task->htts->hio, thr_task->client->task == (hio_svc_htts_task_t*)thr_task); - HIO_SVC_HTTS_TASK_UNREF (thr_task->client->task); + HIO_ASSERT (thr_task->htts->hio, thr_task->task_client->task == (hio_svc_htts_task_t*)thr_task); + HIO_SVC_HTTS_TASK_UNREF (thr_task->task_client->task); /* IMPORTANT: thr_task must not be accessed from here down as it could have been destroyed */ } else { - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for no keep-alive\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for no keep-alive\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); hio_dev_sck_shutdown (thr_task->csck, HIO_DEV_SCK_SHUTDOWN_WRITE); hio_dev_sck_halt (thr_task->csck); } @@ -274,7 +276,9 @@ static void thr_task_on_kill (hio_svc_htts_task_t* task) thr_task_t* thr_task = (thr_task_t*)task; hio_t* hio = thr_task->htts->hio; - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killing the task\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killing the task\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + + if (thr_task->on_kill) thr_task->on_kill (task); if (thr_task->peer) { @@ -302,17 +306,17 @@ static void thr_task_on_kill (hio_svc_htts_task_t* task) if (thr_task->csck) { - HIO_ASSERT (hio, thr_task->client != HIO_NULL); + HIO_ASSERT (hio, thr_task->task_client != HIO_NULL); /* restore callbacks */ if (thr_task->client_org_on_read) thr_task->csck->on_read = thr_task->client_org_on_read; if (thr_task->client_org_on_write) thr_task->csck->on_write = thr_task->client_org_on_write; if (thr_task->client_org_on_disconnect) thr_task->csck->on_disconnect = thr_task->client_org_on_disconnect; - if (thr_task->client_htrd_recbs_changed) hio_htrd_setrecbs (thr_task->client->htrd, &thr_task->client_htrd_org_recbs); + if (thr_task->client_htrd_recbs_changed) hio_htrd_setrecbs (thr_task->task_client->htrd, &thr_task->client_htrd_org_recbs); if (!thr_task->keep_alive || hio_dev_sck_read(thr_task->csck, 1) <= -1) { - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for failure to enable input watching\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for failure to enable input watching\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); hio_dev_sck_halt (thr_task->csck); } } @@ -323,7 +327,7 @@ static void thr_task_on_kill (hio_svc_htts_task_t* task) thr_task->client_htrd_recbs_changed = 0; if (thr_task->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (thr_task); /* detach from the htts service only if it's attached */ - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killed the task\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killed the task\n", thr_task->htts, thr_task, thr_task->task_client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); } static void thr_peer_on_close (hio_dev_thr_t* thr, hio_dev_thr_sid_t sid) @@ -474,7 +478,7 @@ static int thr_peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) { thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(htrd); thr_task_t* thr_task = thr_peer->task; - hio_svc_htts_cli_t* cli = thr_task->client; + hio_svc_htts_cli_t* cli = thr_task->task_client; hio_bch_t dtbuf[64]; int status_code = HIO_HTTP_STATUS_OK; @@ -687,7 +691,7 @@ static void thr_client_on_disconnect (hio_dev_sck_t* sck) thr_task->client_disconnected = 1; thr_task->csck = HIO_NULL; - thr_task->client = HIO_NULL; + thr_task->task_client = HIO_NULL; if (thr_task->client_org_on_disconnect) { thr_task->client_org_on_disconnect (sck); @@ -871,7 +875,7 @@ static int thr_capture_request_header (hio_htre_t* req, const hio_bch_t* key, co return 0; } -int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, hio_svc_htts_thr_func_t func, void* ctx, int options) +int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, hio_svc_htts_thr_func_t func, void* ctx, int options, hio_svc_htts_task_on_kill_t on_kill) { hio_t* hio = htts->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); @@ -915,12 +919,13 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r mi.on_write = thr_peer_on_write; mi.on_close = thr_peer_on_close; - thr_task = (thr_task_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*thr_task), thr_task_on_kill, req); + thr_task = (thr_task_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*thr_task), thr_task_on_kill, req, cli); if (HIO_UNLIKELY(!thr_task)) goto oops; + thr_task->on_kill = on_kill; thr_task->options = options; thr_task->csck = csck; - thr_task->client = cli; /* for faster access without going through csck. */ + thr_task->task_client = cli; /* for faster access without going through csck. */ /*thr_task->num_pending_writes_to_client = 0; thr_task->num_pending_writes_to_peer = 0;*/ @@ -1018,9 +1023,9 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r if (have_content) { /* change the callbacks to subscribe to contents to be uploaded */ - thr_task->client_htrd_org_recbs = *hio_htrd_getrecbs(thr_task->client->htrd); + thr_task->client_htrd_org_recbs = *hio_htrd_getrecbs(thr_task->task_client->htrd); thr_client_htrd_recbs.peek = thr_task->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (thr_task->client->htrd, &thr_client_htrd_recbs); + hio_htrd_setrecbs (thr_task->task_client->htrd, &thr_client_htrd_recbs); thr_task->client_htrd_recbs_changed = 1; } else diff --git a/lib/http-txt.c b/lib/http-txt.c index 5dcbf16..9acdb40 100644 --- a/lib/http-txt.c +++ b/lib/http-txt.c @@ -33,11 +33,12 @@ struct txt_t { HIO_SVC_HTTS_TASK_HEADER; + hio_svc_htts_task_on_kill_t on_kill; /* user-provided on_kill callback */ + int options; hio_oow_t num_pending_writes_to_client; hio_dev_sck_t* csck; - hio_svc_htts_cli_t* client; unsigned int over: 2; /* must be large enough to accomodate TXT_OVER_ALL */ unsigned int keep_alive: 1; @@ -92,7 +93,7 @@ static int txt_writev_to_client (txt_t* txt, hio_iovec_t* iov, hio_iolen_t iovcn static int txt_send_final_status_to_client (txt_t* txt, int status_code, const hio_bch_t* content_type, const hio_bch_t* content_text, int force_close) { - hio_svc_htts_cli_t* cli = txt->client; + hio_svc_htts_cli_t* cli = txt->task_client; hio_bch_t dtbuf[64]; hio_oow_t content_text_len = 0; @@ -120,6 +121,7 @@ static int txt_send_final_status_to_client (txt_t* txt, int status_code, const h if (hio_becs_fcat(cli->sbuf, "Content-Length: %zu\r\n\r\n", content_text_len) == (hio_oow_t)-1) return -1; + txt->task_status_code = status_code; return (txt_write_to_client(txt, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || (content_text_len > 0 && txt_write_to_client(txt, content_text, content_text_len) <= -1) || (force_close && txt_write_to_client(txt, HIO_NULL, 0) <= -1))? -1: 0; @@ -149,10 +151,10 @@ static HIO_INLINE void txt_mark_over (txt_t* txt, int over_bits) if (txt->keep_alive && !txt->client_eof_detected) { /* how to arrange to delete this txt object and put the socket back to the normal waiting state??? */ - HIO_ASSERT (txt->htts->hio, txt->client->task == (hio_svc_htts_task_t*)txt); + HIO_ASSERT (txt->htts->hio, txt->task_client->task == (hio_svc_htts_task_t*)txt); -/*printf ("DETACHING FROM THE MAIN CLIENT TASK... state -> %p\n", txt->client->task);*/ - HIO_SVC_HTTS_TASK_UNREF (txt->client->task); +/*printf ("DETACHING FROM THE MAIN CLIENT TASK... state -> %p\n", txt->task_client->task);*/ + HIO_SVC_HTTS_TASK_UNREF (txt->task_client->task); /* txt must not be access from here down as it could have been destroyed */ } else @@ -171,15 +173,17 @@ static void txt_on_kill (hio_svc_htts_task_t* task) HIO_DEBUG2 (hio, "HTTS(%p) - killing txt client(%p)\n", txt->htts, txt->csck); + if (txt->on_kill) txt->on_kill (task); + if (txt->csck) { - HIO_ASSERT (hio, txt->client != HIO_NULL); + HIO_ASSERT (hio, txt->task_client != HIO_NULL); if (txt->client_org_on_read) txt->csck->on_read = txt->client_org_on_read; if (txt->client_org_on_write) txt->csck->on_write = txt->client_org_on_write; if (txt->client_org_on_disconnect) txt->csck->on_disconnect = txt->client_org_on_disconnect; if (txt->client_htrd_recbs_changed) - hio_htrd_setrecbs (txt->client->htrd, &txt->client_htrd_org_recbs); + hio_htrd_setrecbs (txt->task_client->htrd, &txt->client_htrd_org_recbs); if (!txt->client_disconnected) { @@ -322,7 +326,7 @@ oops: return 0; } -int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, int status_code, const hio_bch_t* content_type, const hio_bch_t* content_text, int options) +int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, int status_code, const hio_bch_t* content_type, const hio_bch_t* content_text, int options, hio_svc_htts_task_on_kill_t on_kill) { hio_t* hio = htts->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); @@ -332,15 +336,16 @@ int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r HIO_ASSERT (hio, hio_htre_getcontentlen(req) == 0); HIO_ASSERT (hio, cli->sck == csck); - txt = (txt_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*txt), txt_on_kill, req); + txt = (txt_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*txt), txt_on_kill, req, cli); if (HIO_UNLIKELY(!txt)) goto oops; + txt->on_kill = on_kill; txt->options = options; /*txt->num_pending_writes_to_client = 0;*/ txt->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &txt->req_content_length); txt->csck = csck; - txt->client = cli; + txt->task_client = cli; txt->client_org_on_read = csck->on_read; txt->client_org_on_write = csck->on_write; txt->client_org_on_disconnect = csck->on_disconnect; @@ -365,9 +370,9 @@ int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r if (txt->req_content_length_unlimited || txt->req_content_length > 0) { /* change the callbacks to subscribe to contents to be uploaded */ - txt->client_htrd_org_recbs = *hio_htrd_getrecbs(txt->client->htrd); + txt->client_htrd_org_recbs = *hio_htrd_getrecbs(txt->task_client->htrd); txt_client_htrd_recbs.peek = txt->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (txt->client->htrd, &txt_client_htrd_recbs); + hio_htrd_setrecbs (txt->task_client->htrd, &txt_client_htrd_recbs); txt->client_htrd_recbs_changed = 1; } else diff --git a/lib/http.c b/lib/http.c index f828a1d..f347727 100644 --- a/lib/http.c +++ b/lib/http.c @@ -59,7 +59,7 @@ const hio_bch_t* hio_http_status_to_bcstr (int code) case 300: msg = "Multiple Choices"; break; case HIO_HTTP_STATUS_MOVED_PERMANENTLY: msg = "Moved Permanently"; break; - case 302: msg = "Found"; break; + case HIO_HTTP_STATUS_MOVED_TEMPORARILY: msg = "Moved Temporarily"; break; case 303: msg = "See Other"; break; case HIO_HTTP_STATUS_NOT_MODIFIED: msg = "Not Modified"; break; case 305: msg = "Use Proxy"; break; @@ -67,11 +67,11 @@ const hio_bch_t* hio_http_status_to_bcstr (int code) case 308: msg = "Permanent Redirect"; break; case HIO_HTTP_STATUS_BAD_REQUEST: msg = "Bad Request"; break; - case 401: msg = "Unauthorized"; break; + case HIO_HTTP_STATUS_UNAUTHORIZED: msg = "Unauthorized"; break; case 402: msg = "Payment Required"; break; case HIO_HTTP_STATUS_FORBIDDEN: msg = "Forbidden"; break; case HIO_HTTP_STATUS_NOT_FOUND: msg = "Not Found"; break; - case HIO_HTTP_STATUS_METHOD_NOT_ALLOWED: msg = "Method Not Allowed"; break; + case HIO_HTTP_STATUS_METHOD_NOT_ALLOWED: msg = "Method Not Allowed"; break; case 406: msg = "Not Acceptable"; break; case 407: msg = "Proxy Authentication Required"; break; case 408: msg = "Request Timeout"; break; @@ -96,7 +96,7 @@ const hio_bch_t* hio_http_status_to_bcstr (int code) case 504: msg = "Gateway Timeout"; break; case 505: msg = "HTTP Version Not Supported"; break; - default: msg = "Unknown Error"; break; + default: msg = "Unknown"; break; } return msg;