fixed more issue in multiple http task handlers

This commit is contained in:
hyung-hwan 2023-01-15 00:59:43 +09:00
parent f03453c21b
commit a7dde21b4d
6 changed files with 162 additions and 120 deletions

View File

@ -145,7 +145,7 @@ printf ("DISCONNECT SOCKET .................. sck->%p conn->%p\n", sck, conn);
for (i = 0; i < conn->sess.capa; i++)
{
hio_svc_fcgic_sess_t* sess;
sess = &conn->sess.ptr[i + 1];
sess = &conn->sess.ptr[i];
if (sess->active)
{
/* TODO: release the session???*/
@ -410,6 +410,7 @@ static hio_svc_fcgic_sess_t* new_session (hio_svc_fcgic_t* fcgic, const hio_skad
* but application records have a nonzero requestId. */
newptr[i].sid = i + 1;
newptr[i].conn = conn;
newptr[i].active = 0;
}
newptr[i - 1].sid = INVALID_SID;
conn->sess.free = conn->sess.capa;

View File

@ -153,20 +153,26 @@ static int cgi_send_final_status_to_client (cgi_t* cgi, int status_code, int for
hio_svc_htts_cli_t* cli = cgi->client;
hio_bch_t dtbuf[64];
const hio_bch_t* status_msg;
hio_oow_t content_len;
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
status_msg = hio_http_status_to_bcstr(status_code);
content_len = hio_count_bcstr(status_msg);
if (!force_close) force_close = !cgi->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\n",
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n",
cgi->req_version.major, cgi->req_version.minor,
status_code, status_msg,
cli->htts->server_name, dtbuf,
(force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1;
if (cgi->req_method == HIO_HTTP_HEAD && status_code != HIO_HTTP_STATUS_OK) status_msg = "";
if (cgi->req_method == HIO_HTTP_HEAD)
{
if (status_code != HIO_HTTP_STATUS_OK) content_len = 0;
status_msg = "";
}
if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", hio_count_bcstr(status_msg), status_msg) == (hio_oow_t)-1) return -1;
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;
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;
@ -333,7 +339,7 @@ static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
switch (sid)
{
case HIO_DEV_PRO_MASTER:
HIO_DEBUG3 (hio, "HTTS(%p) - peer %p(pid=%d) closing master\n", cgi->client->htts, pro, (int)pro->child_pid);
HIO_DEBUG3 (hio, "HTTS(%p) - peer %p(pid=%d) closing master\n", cgi->htts, pro, (int)pro->child_pid);
cgi->peer = HIO_NULL; /* clear this peer from the state */
HIO_ASSERT (hio, peer_xtn->cgi != HIO_NULL);
@ -352,7 +358,7 @@ static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
case HIO_DEV_PRO_OUT:
HIO_ASSERT (hio, cgi->peer == pro);
HIO_DEBUG4 (hio, "HTTS(%p) - peer %p(pid=%d) closing slave[%d]\n", cgi->client->htts, pro, (int)pro->child_pid, sid);
HIO_DEBUG4 (hio, "HTTS(%p) - peer %p(pid=%d) closing slave[%d]\n", cgi->htts, pro, (int)pro->child_pid, sid);
if (!(cgi->over & CGI_OVER_READ_FROM_PEER))
{
@ -364,13 +370,13 @@ static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
break;
case HIO_DEV_PRO_IN:
HIO_DEBUG4 (hio, "HTTS(%p) - peer %p(pid=%d) closing slave[%d]\n", cgi->client->htts, pro, (int)pro->child_pid, sid);
HIO_DEBUG4 (hio, "HTTS(%p) - peer %p(pid=%d) closing slave[%d]\n", cgi->htts, pro, (int)pro->child_pid, sid);
cgi_mark_over (cgi, CGI_OVER_WRITE_TO_PEER);
break;
case HIO_DEV_PRO_ERR:
default:
HIO_DEBUG4 (hio, "HTTS(%p) - peer %p(pid=%d) closing slave[%d]\n", cgi->client->htts, pro, (int)pro->child_pid, sid);
HIO_DEBUG4 (hio, "HTTS(%p) - peer %p(pid=%d) closing slave[%d]\n", cgi->htts, pro, (int)pro->child_pid, sid);
/* do nothing */
break;
}
@ -387,13 +393,13 @@ static int cgi_peer_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const vo
if (dlen <= -1)
{
HIO_DEBUG3 (hio, "HTTS(%p) - read error from peer %p(pid=%u)\n", cgi->client->htts, pro, (unsigned int)pro->child_pid);
HIO_DEBUG3 (hio, "HTTS(%p) - read error from peer %p(pid=%u)\n", cgi->htts, pro, (unsigned int)pro->child_pid);
goto oops;
}
if (dlen == 0)
{
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from peer %p(pid=%u)\n", cgi->client->htts, pro, (unsigned int)pro->child_pid);
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from peer %p(pid=%u)\n", cgi->htts, pro, (unsigned int)pro->child_pid);
if (!(cgi->over & CGI_OVER_READ_FROM_PEER))
{
@ -539,7 +545,7 @@ static int peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const hio_
cgi_peer_xtn_t* peer = hio_htrd_getxtn(htrd);
cgi_t* cgi = peer->cgi;
HIO_ASSERT (cgi->client->htts->hio, htrd == cgi->peer_htrd);
HIO_ASSERT (cgi->htts->hio, htrd == cgi->peer_htrd);
switch (cgi->res_mode_to_cli)
{
@ -635,7 +641,7 @@ static int cgi_peer_on_write (hio_dev_pro_t* pro, hio_iolen_t wrlen, void* wrctx
if (wrlen <= -1)
{
HIO_DEBUG3 (hio, "HTTS(%p) - unable to write to peer %p(pid=%u)\n", cgi->client->htts, pro, (int)pro->child_pid);
HIO_DEBUG3 (hio, "HTTS(%p) - unable to write to peer %p(pid=%u)\n", cgi->htts, pro, (int)pro->child_pid);
goto oops;
}
else if (wrlen == 0)
@ -645,7 +651,7 @@ static int cgi_peer_on_write (hio_dev_pro_t* pro, hio_iolen_t wrlen, void* wrctx
cgi->num_pending_writes_to_peer--;
HIO_ASSERT (hio, cgi->num_pending_writes_to_peer == 0);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to peer %p(pid=%u)\n", cgi->client->htts, pro, (int)pro->child_pid);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to peer %p(pid=%u)\n", cgi->htts, pro, (int)pro->child_pid);
/* indicated EOF to the peer side. i need no more data from the client side.
* i don't need to enable input watching in the client side either */
cgi_mark_over (cgi, CGI_OVER_WRITE_TO_PEER);
@ -722,7 +728,7 @@ static int cgi_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
if (len == 0)
{
/* EOF on the client side. arrange to close */
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from client %p(hnd=%d)\n", cgi->client->htts, sck, (int)sck->hnd);
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from client %p(hnd=%d)\n", cgi->htts, sck, (int)sck->hnd);
cgi->client_eof_detected = 1;
if (!(cgi->over & CGI_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without cgi_client_htrd_poke() */
@ -772,7 +778,7 @@ static int cgi_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrc
/* if the connect is keep-alive, this part may not be called */
cgi->num_pending_writes_to_client--;
HIO_ASSERT (hio, cgi->num_pending_writes_to_client == 0);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", cgi->client->htts, sck, (int)sck->hnd);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", cgi->htts, sck, (int)sck->hnd);
/* 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 */

View File

@ -29,7 +29,10 @@ struct fcgi_t
hio_oow_t num_pending_writes_to_client;
hio_oow_t num_pending_writes_to_peer;
hio_svc_fcgic_sess_t* peer;
hio_dev_sck_t* csck;
hio_svc_htts_cli_t* client;
hio_http_method_t req_method;
hio_http_version_t req_version; /* client request */
unsigned int over: 4; /* must be large enough to accomodate FCGI_OVER_ALL */
@ -81,13 +84,13 @@ static int begin_request ()
static void fcgi_halt_participating_devices (fcgi_t* fcgi)
{
HIO_ASSERT (fcgi->client->htts->hio, fcgi->client != HIO_NULL);
HIO_ASSERT (fcgi->client->htts->hio, fcgi->client->sck != HIO_NULL);
HIO_ASSERT (fcgi->htts->hio, fcgi->client != HIO_NULL);
HIO_ASSERT (fcgi->htts->hio, fcgi->csck != HIO_NULL);
/* TODO: include fcgi session id in the output in place of peer??? */
HIO_DEBUG3 (fcgi->client->htts->hio, "HTTS(%p) - Halting participating devices in fcgi state %p(client=%p)\n", fcgi->client->htts, fcgi, fcgi->client->sck);
HIO_DEBUG3 (fcgi->htts->hio, "HTTS(%p) - Halting participating devices in fcgi state %p(client=%p)\n", fcgi->htts, fcgi, fcgi->csck);
hio_dev_sck_halt (fcgi->client->sck);
hio_dev_sck_halt (fcgi->csck);
#if 0
/* check for peer as it may not have been started */
@ -111,7 +114,7 @@ static int fcgi_write_to_peer (fcgi_t* fcgi, const void* data, hio_iolen_t dlen)
if (fcgi->num_pending_writes_to_peer > FCGI_PENDING_IO_THRESHOLD)
{
/* disable input watching */
if (hio_dev_sck_read(fcgi->client->sck, 0) <= -1) return -1;
if (hio_dev_sck_read(fcgi->csck, 0) <= -1) return -1;
}
return 0;
}
@ -123,15 +126,15 @@ static HIO_INLINE void fcgi_mark_over (fcgi_t* fcgi, int over_bits)
old_over = fcgi->over;
fcgi->over |= over_bits;
HIO_DEBUG4 (fcgi->htts->hio, "HTTS(%p) - client=%p new-bits=%x over=%x\n", fcgi->htts, fcgi->client->sck, (int)over_bits, (int)fcgi->over);
HIO_DEBUG4 (fcgi->htts->hio, "HTTS(%p) - client=%p new-bits=%x over=%x\n", fcgi->htts, fcgi->csck, (int)over_bits, (int)fcgi->over);
if (!(old_over & FCGI_OVER_READ_FROM_CLIENT) && (fcgi->over & FCGI_OVER_READ_FROM_CLIENT))
{
printf (">>>>>>>>>>>> disableing client read watching ...................\n");
if (hio_dev_sck_read(fcgi->client->sck, 0) <= -1)
if (hio_dev_sck_read(fcgi->csck, 0) <= -1)
{
HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", fcgi->htts, fcgi->client->sck);
hio_dev_sck_halt (fcgi->client->sck);
HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", fcgi->htts, fcgi->csck);
hio_dev_sck_halt (fcgi->csck);
}
}
@ -168,9 +171,9 @@ printf (">>>>>>>>>>>> disableing client read watching ...................\n");
}
else
{
HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", fcgi->htts, fcgi->client->sck);
hio_dev_sck_shutdown (fcgi->client->sck, HIO_DEV_SCK_SHUTDOWN_WRITE);
hio_dev_sck_halt (fcgi->client->sck);
HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", fcgi->htts, fcgi->csck);
hio_dev_sck_shutdown (fcgi->csck, HIO_DEV_SCK_SHUTDOWN_WRITE);
hio_dev_sck_halt (fcgi->csck);
}
}
}
@ -180,7 +183,7 @@ static int fcgi_write_to_client (fcgi_t* fcgi, const void* data, hio_iolen_t dle
fcgi->ever_attempted_to_write_to_client = 1;
fcgi->num_pending_writes_to_client++;
if (hio_dev_sck_write(fcgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
if (hio_dev_sck_write(fcgi->csck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
{
fcgi->num_pending_writes_to_client--;
return -1;
@ -200,16 +203,28 @@ static int fcgi_send_final_status_to_client (fcgi_t* fcgi, int status_code, int
{
hio_svc_htts_cli_t* cli = fcgi->client;
hio_bch_t dtbuf[64];
const hio_bch_t* status_msg;
hio_oow_t content_len;
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
status_msg = hio_http_status_to_bcstr(status_code);
content_len = hio_count_bcstr(status_msg);
if (!force_close) force_close = !fcgi->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\nContent-Length: 0\r\n\r\n",
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n",
fcgi->req_version.major, fcgi->req_version.minor,
status_code, hio_http_status_to_bcstr(status_code),
status_code, status_msg,
cli->htts->server_name, dtbuf,
(force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1;
if (fcgi->req_method == HIO_HTTP_HEAD)
{
if (status_code != HIO_HTTP_STATUS_OK) content_len = 0;
status_msg = "";
}
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;
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;
}
@ -285,7 +300,7 @@ static int fcgi_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
if (len == 0)
{
/* EOF on the client side. arrange to close */
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from client %p(hnd=%d)\n", fcgi->client->htts, sck, (int)sck->hnd);
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from client %p(hnd=%d)\n", fcgi->htts, sck, (int)sck->hnd);
if (!(fcgi->over & FCGI_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without fcgi_client_htrd_poke() */
{
@ -333,7 +348,7 @@ static int fcgi_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 */
fcgi->num_pending_writes_to_client--;
HIO_ASSERT (hio, fcgi->num_pending_writes_to_client == 0);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", fcgi->client->htts, sck, (int)sck->hnd);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", fcgi->htts, sck, (int)sck->hnd);
/* 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 */
@ -376,7 +391,7 @@ static void fcgi_on_kill (hio_svc_htts_task_t* task)
fcgi_t* fcgi = (fcgi_t*)task;
hio_t* hio = fcgi->htts->hio;
HIO_DEBUG2 (hio, "HTTS(%p) - killing fcgi client(%p)\n", fcgi->htts, fcgi->client->sck);
HIO_DEBUG2 (hio, "HTTS(%p) - killing fcgi client(%p)\n", fcgi->htts, fcgi->csck);
#if 0
if (fcgi->peer)
@ -389,38 +404,31 @@ static void fcgi_on_kill (hio_svc_htts_task_t* task)
}
#endif
if (fcgi->client_org_on_read)
if (fcgi->csck)
{
fcgi->client->sck->on_read = fcgi->client_org_on_read;
fcgi->client_org_on_read = HIO_NULL;
}
HIO_ASSERT (hio, fcgi->client != HIO_NULL);
if (fcgi->client_org_on_write)
{
fcgi->client->sck->on_write = fcgi->client_org_on_write;
fcgi->client_org_on_write = HIO_NULL;
}
if (fcgi->client_org_on_disconnect)
{
fcgi->client->sck->on_disconnect = fcgi->client_org_on_disconnect;
fcgi->client_org_on_disconnect = HIO_NULL;
}
if (fcgi->client_htrd_recbs_changed)
{
/* restore the callbacks */
hio_htrd_setrecbs (fcgi->client->htrd, &fcgi->client_htrd_org_recbs);
}
if (fcgi->client_org_on_read) fcgi->csck->on_read = fcgi->client_org_on_read;
if (fcgi->client_org_on_write) fcgi->csck->on_write = fcgi->client_org_on_write;
if (fcgi->client_org_on_disconnect) fcgi->csck->on_disconnect = fcgi->client_org_on_disconnect;
if (fcgi->client_htrd_recbs_changed) hio_htrd_setrecbs (fcgi->client->htrd, &fcgi->client_htrd_org_recbs);
if (!fcgi->client_disconnected)
{
if (!fcgi->keep_alive || hio_dev_sck_read(fcgi->client->sck, 1) <= -1)
if (!fcgi->keep_alive || hio_dev_sck_read(fcgi->csck, 1) <= -1)
{
HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", fcgi->htts, fcgi->client->sck);
hio_dev_sck_halt (fcgi->client->sck);
HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", fcgi->htts, fcgi->csck);
hio_dev_sck_halt (fcgi->csck);
}
}
}
fcgi->client_org_on_read = HIO_NULL;
fcgi->client_org_on_write = HIO_NULL;
fcgi->client_org_on_disconnect = HIO_NULL;
fcgi->client_htrd_recbs_changed = 0;
if (fcgi->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (fcgi); /* detach from the htts service only if it's attached */
}
static int write_params (fcgi_t* fcgi, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* script)
@ -432,7 +440,7 @@ static int write_params (fcgi_t* fcgi, hio_dev_sck_t* csck, hio_htre_t* req, con
hio_oow_t content_length;
hio_bch_t* actual_script = HIO_NULL;
HIO_ASSERT (hio, fcgi->client->sck == csck);
HIO_ASSERT (hio, fcgi->csck == csck);
actual_script = hio_svc_htts_dupmergepaths(fcgi->htts, docroot, script);
if (!actual_script) goto oops;
@ -479,6 +487,7 @@ static int write_params (fcgi_t* fcgi, hio_dev_sck_t* csck, hio_htre_t* req, con
//hio_htre_walkheaders (req,)
hio_freemem (hio, actual_script);
return 0;
oops:
@ -504,9 +513,11 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t*
fcgi = (fcgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*fcgi), fcgi_on_kill);
if (HIO_UNLIKELY(!fcgi)) goto oops;
fcgi->csck = csck;
fcgi->client = cli;
/*fcgi->num_pending_writes_to_client = 0;
fcgi->num_pending_writes_to_peer = 0;*/
fcgi->req_method = hio_htre_getqmethodtype(req);
fcgi->req_version = *hio_htre_getversion(req);
fcgi->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &fcgi->req_content_length);
@ -627,6 +638,8 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t*
/* TODO: store current input watching state and use it when destroying the cgi data */
if (hio_dev_sck_read(csck, !(fcgi->over & FCGI_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, fcgi);
return 0;
oops:

View File

@ -146,18 +146,24 @@ static int file_send_final_status_to_client (file_t* file, int status_code, int
hio_svc_htts_cli_t* cli = file->client;
hio_bch_t dtbuf[64];
const hio_bch_t* status_msg;
hio_oow_t content_len;
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
status_msg = hio_http_status_to_bcstr(status_code);
content_len = hio_count_bcstr(status_msg);
if (!force_close) force_close = !file->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\n",
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n",
file->req_version.major, file->req_version.minor,
status_code, status_msg,
cli->htts->server_name, dtbuf,
(force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1;
if (file->req_method == HIO_HTTP_HEAD && status_code != HIO_HTTP_STATUS_OK) status_msg = "";
if (file->req_method == HIO_HTTP_HEAD)
{
if (status_code != HIO_HTTP_STATUS_OK) content_len = 0;
status_msg = "";
}
if (dir_redirect)
{
@ -166,7 +172,7 @@ static int file_send_final_status_to_client (file_t* file, int status_code, int
}
else
{
if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", hio_count_bcstr(status_msg), status_msg) == (hio_oow_t)-1) return -1;
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;
}
return (file_write_to_client(file, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 ||

View File

@ -154,20 +154,26 @@ static int thr_task_send_final_status_to_client (thr_task_t* thr_task, int statu
hio_svc_htts_cli_t* cli = thr_task->client;
hio_bch_t dtbuf[64];
const hio_bch_t* status_msg;
hio_oow_t content_len;
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
status_msg = hio_http_status_to_bcstr(status_code);
content_len = hio_count_bcstr(status_msg);
if (!force_close) force_close = !thr_task->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\n",
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n",
thr_task->req_version.major, thr_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 (thr_task->req_method == HIO_HTTP_HEAD && status_code != HIO_HTTP_STATUS_OK) status_msg = "";
if (thr_task->req_method == HIO_HTTP_HEAD)
{
if (status_code != HIO_HTTP_STATUS_OK) content_len = 0;
status_msg = "";
}
if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", hio_count_bcstr(status_msg), status_msg) == (hio_oow_t)-1) return -1;
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;
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;

View File

@ -35,7 +35,10 @@ struct txt_t
int options;
hio_oow_t num_pending_writes_to_client;
hio_dev_sck_t* csck;
hio_svc_htts_cli_t* client;
hio_http_method_t req_method;
hio_http_version_t req_version; /* client request */
unsigned int over: 2; /* must be large enough to accomodate TXT_OVER_ALL */
@ -55,37 +58,43 @@ typedef struct txt_t txt_t;
static void txt_halt_participating_devices (txt_t* txt)
{
HIO_ASSERT (txt->client->htts->hio, txt->client != HIO_NULL);
HIO_ASSERT (txt->client->htts->hio, txt->client->sck != HIO_NULL);
HIO_DEBUG3 (txt->client->htts->hio, "HTTS(%p) - Halting participating devices in txt state %p(client=%p)\n", txt->client->htts, txt, txt->client->sck);
hio_dev_sck_halt (txt->client->sck);
HIO_ASSERT (txt->htts->hio, txt->client != HIO_NULL);
HIO_ASSERT (txt->htts->hio, txt->csck != HIO_NULL);
HIO_DEBUG3 (txt->htts->hio, "HTTS(%p) - Halting participating devices in txt state %p(client=%p)\n", txt->htts, txt, txt->csck);
hio_dev_sck_halt (txt->csck);
}
static int txt_write_to_client (txt_t* txt, const void* data, hio_iolen_t dlen)
{
if (txt->csck)
{
txt->num_pending_writes_to_client++;
if (hio_dev_sck_write(txt->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
if (hio_dev_sck_write(txt->csck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
{
txt->num_pending_writes_to_client--;
return -1;
}
}
return 0;
}
#if 0
static int txt_writev_to_client (txt_t* txt, hio_iovec_t* iov, hio_iolen_t iovcnt)
{
if (txt->csck)
{
txt->num_pending_writes_to_client++;
if (hio_dev_sck_writev(txt->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1)
if (hio_dev_sck_writev(txt->csck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1)
{
txt->num_pending_writes_to_client--;
return -1;
}
}
return 0;
}
#endif
static int txt_send_final_status_to_client (txt_t* txt, int status_code, const char* content_type, const char* content_text, int force_close)
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_bch_t dtbuf[64];
@ -95,22 +104,28 @@ static int txt_send_final_status_to_client (txt_t* txt, int status_code, const c
if (!force_close) force_close = !txt->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\n",
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n",
txt->req_version.major, txt->req_version.minor,
status_code, hio_http_status_to_bcstr(status_code),
cli->htts->server_name, dtbuf,
(force_close? "close": "keep-alive"),
(content_text? hio_count_bcstr(content_text): 0), (content_text? content_text: "")) == (hio_oow_t)-1) return -1;
(force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1;
if (content_text)
{
content_text_len = hio_count_bcstr(content_text);
if (txt->req_method == HIO_HTTP_HEAD)
{
if (status_code != HIO_HTTP_STATUS_OK) content_text_len = 0;
content_text = "";
}
if (content_type && hio_becs_fcat(cli->sbuf, "Content-Type: %hs\r\n", content_type) == (hio_oow_t)-1) return -1;
}
if (hio_becs_fcat(cli->sbuf, "Content-Length: %zu\r\n\r\n", content_text_len) == (hio_oow_t)-1) return -1;
return (txt_write_to_client(txt, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 ||
(content_text && txt_write_to_client(txt, content_text, content_text_len) <= -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;
}
@ -121,14 +136,14 @@ static HIO_INLINE void txt_mark_over (txt_t* txt, int over_bits)
old_over = txt->over;
txt->over |= over_bits;
HIO_DEBUG4 (txt->htts->hio, "HTTS(%p) - client=%p new-bits=%x over=%x\n", txt->htts, txt->client->sck, (int)over_bits, (int)txt->over);
HIO_DEBUG4 (txt->htts->hio, "HTTS(%p) - client=%p new-bits=%x over=%x\n", txt->htts, txt->csck, (int)over_bits, (int)txt->over);
if (!(old_over & TXT_OVER_READ_FROM_CLIENT) && (txt->over & TXT_OVER_READ_FROM_CLIENT))
{
if (hio_dev_sck_read(txt->client->sck, 0) <= -1)
if (hio_dev_sck_read(txt->csck, 0) <= -1)
{
HIO_DEBUG2 (txt->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", txt->htts, txt->client->sck);
hio_dev_sck_halt (txt->client->sck);
HIO_DEBUG2 (txt->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", txt->htts, txt->csck);
hio_dev_sck_halt (txt->csck);
}
}
@ -146,9 +161,9 @@ static HIO_INLINE void txt_mark_over (txt_t* txt, int over_bits)
}
else
{
HIO_DEBUG2 (txt->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", txt->htts, txt->client->sck);
hio_dev_sck_shutdown (txt->client->sck, HIO_DEV_SCK_SHUTDOWN_WRITE);
hio_dev_sck_halt (txt->client->sck);
HIO_DEBUG2 (txt->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", txt->htts, txt->csck);
hio_dev_sck_shutdown (txt->csck, HIO_DEV_SCK_SHUTDOWN_WRITE);
hio_dev_sck_halt (txt->csck);
}
}
}
@ -158,43 +173,34 @@ static void txt_on_kill (hio_svc_htts_task_t* task)
txt_t* txt = (txt_t*)task;
hio_t* hio = txt->htts->hio;
HIO_DEBUG2 (hio, "HTTS(%p) - killing txt client(%p)\n", txt->htts, txt->client->sck);
HIO_DEBUG2 (hio, "HTTS(%p) - killing txt client(%p)\n", txt->htts, txt->csck);
if (txt->client_org_on_read)
if (txt->csck)
{
txt->client->sck->on_read = txt->client_org_on_read;
txt->client_org_on_read = HIO_NULL;
}
if (txt->client_org_on_write)
{
txt->client->sck->on_write = txt->client_org_on_write;
txt->client_org_on_write = HIO_NULL;
}
if (txt->client_org_on_disconnect)
{
txt->client->sck->on_disconnect = txt->client_org_on_disconnect;
txt->client_org_on_disconnect = HIO_NULL;
}
HIO_ASSERT (hio, txt->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)
{
/* restore the callbacks */
hio_htrd_setrecbs (txt->client->htrd, &txt->client_htrd_org_recbs);
}
if (!txt->client_disconnected)
{
/*printf ("ENABLING INPUT WATCHING on CLIENT %p. \n", txt->client->sck);*/
if (!txt->keep_alive || hio_dev_sck_read(txt->client->sck, 1) <= -1)
if (!txt->keep_alive || hio_dev_sck_read(txt->csck, 1) <= -1)
{
HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", txt->htts, txt->client->sck);
hio_dev_sck_halt (txt->client->sck);
HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", txt->htts, txt->csck);
hio_dev_sck_halt (txt->csck);
}
}
}
/*printf ("**** TXT_ON_KILL DONE\n");*/
txt->client_org_on_read = HIO_NULL;
txt->client_org_on_write = HIO_NULL;
txt->client_org_on_disconnect = HIO_NULL;
txt->client_htrd_recbs_changed = 0;
if (txt->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (txt); /* detach from the htts service only if it's attached */
}
static int txt_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req)
@ -250,7 +256,7 @@ static int txt_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
if (len == 0)
{
/* EOF on the client side. arrange to close */
HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", txt->client->htts, sck, (int)sck->hnd);
HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", txt->htts, sck, (int)sck->hnd);
txt->client_eof_detected = 1;
if (!(txt->over & TXT_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without txt_client_htrd_poke() */
@ -297,7 +303,7 @@ static int txt_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrc
/* if the connect is keep-alive, this part may not be called */
txt->num_pending_writes_to_client--;
HIO_ASSERT (hio, txt->num_pending_writes_to_client == 0);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", txt->client->htts, sck, (int)sck->hnd);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", txt->htts, sck, (int)sck->hnd);
/* 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 */
@ -333,8 +339,10 @@ int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
if (HIO_UNLIKELY(!txt)) goto oops;
txt->options = options;
txt->csck = csck;
txt->client = cli;
/*txt->num_pending_writes_to_client = 0;*/
txt->req_method = hio_htre_getqmethodtype(req);
txt->req_version = *hio_htre_getversion(req);
txt->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &txt->req_content_length);
@ -381,6 +389,8 @@ int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
if (hio_dev_sck_read(csck, !(txt->over & TXT_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
if (txt_send_final_status_to_client(txt, status_code, content_type, content_text, 0) <= -1) goto oops;
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, txt);
return 0;
oops: