Compare commits

...

2 Commits

Author SHA1 Message Date
47b1ac034d renamed thr_task to thr in http-thr.c 2023-03-05 01:13:19 +09:00
6ee1c2d66c renamed thr to peer in http-thr.c 2023-03-05 01:08:53 +09:00

View File

@ -94,60 +94,60 @@ struct thr_peer_xtn_t
}; };
typedef struct thr_peer_xtn_t thr_peer_xtn_t; typedef struct thr_peer_xtn_t thr_peer_xtn_t;
static void thr_task_halt_participating_devices (thr_task_t* thr_task) static void thr_task_halt_participating_devices (thr_task_t* thr)
{ {
HIO_DEBUG4 (thr_task->htts->hio, "HTTS(%p) - Halting participating devices in thr task %p(csck=%p,peer=%p)\n", thr_task->htts, thr_task, thr_task->csck, thr_task->peer); HIO_DEBUG4 (thr->htts->hio, "HTTS(%p) - Halting participating devices in thr task %p(csck=%p,peer=%p)\n", thr->htts, thr, thr->csck, thr->peer);
if (thr_task->csck) hio_dev_sck_halt (thr_task->csck); if (thr->csck) hio_dev_sck_halt (thr->csck);
/* check for peer as it may not have been started */ /* check for peer as it may not have been started */
if (thr_task->peer) hio_dev_thr_halt (thr_task->peer); if (thr->peer) hio_dev_thr_halt (thr->peer);
} }
static int thr_task_write_to_client (thr_task_t* thr_task, const void* data, hio_iolen_t dlen) static int thr_task_write_to_client (thr_task_t* thr, const void* data, hio_iolen_t dlen)
{ {
if (thr_task->csck) if (thr->csck)
{ {
thr_task->ever_attempted_to_write_to_client = 1; thr->ever_attempted_to_write_to_client = 1;
thr_task->num_pending_writes_to_client++; thr->num_pending_writes_to_client++;
if (hio_dev_sck_write(thr_task->csck, data, dlen, HIO_NULL, HIO_NULL) <= -1) if (hio_dev_sck_write(thr->csck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
{ {
thr_task->num_pending_writes_to_client--; thr->num_pending_writes_to_client--;
return -1; return -1;
} }
if (thr_task->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD) if (thr->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD)
{ {
if (hio_dev_thr_read(thr_task->peer, 0) <= -1) return -1; if (hio_dev_thr_read(thr->peer, 0) <= -1) return -1;
} }
} }
return 0; return 0;
} }
static int thr_task_writev_to_client (thr_task_t* thr_task, hio_iovec_t* iov, hio_iolen_t iovcnt) static int thr_task_writev_to_client (thr_task_t* thr, hio_iovec_t* iov, hio_iolen_t iovcnt)
{ {
if (thr_task->csck) if (thr->csck)
{ {
thr_task->ever_attempted_to_write_to_client = 1; thr->ever_attempted_to_write_to_client = 1;
thr_task->num_pending_writes_to_client++; thr->num_pending_writes_to_client++;
if (hio_dev_sck_writev(thr_task->csck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) if (hio_dev_sck_writev(thr->csck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1)
{ {
thr_task->num_pending_writes_to_client--; thr->num_pending_writes_to_client--;
return -1; return -1;
} }
if (thr_task->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD) if (thr->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD)
{ {
if (hio_dev_thr_read(thr_task->peer, 0) <= -1) return -1; if (hio_dev_thr_read(thr->peer, 0) <= -1) return -1;
} }
} }
return 0; return 0;
} }
static int thr_task_send_final_status_to_client (thr_task_t* thr_task, int status_code, int force_close) static int thr_task_send_final_status_to_client (thr_task_t* thr, int status_code, int force_close)
{ {
hio_svc_htts_cli_t* cli = thr_task->task_client; hio_svc_htts_cli_t* cli = thr->task_client;
hio_bch_t dtbuf[64]; hio_bch_t dtbuf[64];
const hio_bch_t* status_msg; const hio_bch_t* status_msg;
hio_oow_t content_len; hio_oow_t content_len;
@ -156,14 +156,14 @@ static int thr_task_send_final_status_to_client (thr_task_t* thr_task, int statu
status_msg = hio_http_status_to_bcstr(status_code); status_msg = hio_http_status_to_bcstr(status_code);
content_len = hio_count_bcstr(status_msg); content_len = hio_count_bcstr(status_msg);
if (!force_close) force_close = !thr_task->keep_alive; if (!force_close) force_close = !thr->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\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->task_req_version.major, thr_task->task_req_version.minor, thr->task_req_version.major, thr->task_req_version.minor,
status_code, status_msg, status_code, status_msg,
cli->htts->server_name, dtbuf, cli->htts->server_name, dtbuf,
(force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1; (force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1;
if (thr_task->task_req_method == HIO_HTTP_HEAD) if (thr->task_req_method == HIO_HTTP_HEAD)
{ {
if (status_code != HIO_HTTP_STATUS_OK) content_len = 0; if (status_code != HIO_HTTP_STATUS_OK) content_len = 0;
status_msg = ""; status_msg = "";
@ -171,101 +171,101 @@ 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; 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; thr->task_status_code = status_code;
return (thr_task_write_to_client(thr_task, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || return (thr_task_write_to_client(thr, 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; (force_close && thr_task_write_to_client(thr, HIO_NULL, 0) <= -1))? -1: 0;
} }
static int thr_task_write_last_chunk_to_client (thr_task_t* thr_task) static int thr_task_write_last_chunk_to_client (thr_task_t* thr)
{ {
if (!thr_task->ever_attempted_to_write_to_client) if (!thr->ever_attempted_to_write_to_client)
{ {
if (thr_task_send_final_status_to_client(thr_task, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 0) <= -1) return -1; if (thr_task_send_final_status_to_client(thr, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 0) <= -1) return -1;
} }
else else
{ {
if (thr_task->res_mode_to_cli == THR_TASK_RES_MODE_CHUNKED && if (thr->res_mode_to_cli == THR_TASK_RES_MODE_CHUNKED &&
thr_task_write_to_client(thr_task, "0\r\n\r\n", 5) <= -1) return -1; thr_task_write_to_client(thr, "0\r\n\r\n", 5) <= -1) return -1;
} }
if (!thr_task->keep_alive && thr_task_write_to_client(thr_task, HIO_NULL, 0) <= -1) return -1; if (!thr->keep_alive && thr_task_write_to_client(thr, HIO_NULL, 0) <= -1) return -1;
return 0; return 0;
} }
static int thr_task_write_to_peer (thr_task_t* thr_task, const void* data, hio_iolen_t dlen) static int thr_task_write_to_peer (thr_task_t* thr, const void* data, hio_iolen_t dlen)
{ {
if (thr_task->peer) if (thr->peer)
{ {
thr_task->num_pending_writes_to_peer++; thr->num_pending_writes_to_peer++;
if (hio_dev_thr_write(thr_task->peer, data, dlen, HIO_NULL) <= -1) if (hio_dev_thr_write(thr->peer, data, dlen, HIO_NULL) <= -1)
{ {
thr_task->num_pending_writes_to_peer--; thr->num_pending_writes_to_peer--;
return -1; return -1;
} }
/* TODO: check if it's already finished or something.. */ /* TODO: check if it's already finished or something.. */
if (thr_task->num_pending_writes_to_peer > THR_TASK_PENDING_IO_THRESHOLD) if (thr->num_pending_writes_to_peer > THR_TASK_PENDING_IO_THRESHOLD)
{ {
if (thr_task->csck && hio_dev_sck_read(thr_task->csck, 0) <= -1) return -1; if (thr->csck && hio_dev_sck_read(thr->csck, 0) <= -1) return -1;
} }
} }
return 0; return 0;
} }
static HIO_INLINE void thr_task_mark_over (thr_task_t* thr_task, int over_bits) static HIO_INLINE void thr_task_mark_over (thr_task_t* thr, int over_bits)
{ {
hio_svc_htts_t* htts = thr_task->htts; hio_svc_htts_t* htts = thr->htts;
hio_t* hio = htts->hio; hio_t* hio = htts->hio;
unsigned int old_over; unsigned int old_over;
old_over = thr_task->over; old_over = thr->over;
thr_task->over |= over_bits; thr->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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer, (int)old_over, (int)over_bits, (int)thr->over);
if (!(old_over & THR_TASK_OVER_READ_FROM_CLIENT) && (thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) if (!(old_over & THR_TASK_OVER_READ_FROM_CLIENT) && (thr->over & THR_TASK_OVER_READ_FROM_CLIENT))
{ {
if (thr_task->csck && hio_dev_sck_read(thr_task->csck, 0) <= -1) if (thr->csck && hio_dev_sck_read(thr->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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
hio_dev_sck_halt (thr_task->csck); hio_dev_sck_halt (thr->csck);
} }
} }
if (!(old_over & THR_TASK_OVER_READ_FROM_PEER) && (thr_task->over & THR_TASK_OVER_READ_FROM_PEER)) if (!(old_over & THR_TASK_OVER_READ_FROM_PEER) && (thr->over & THR_TASK_OVER_READ_FROM_PEER))
{ {
if (thr_task->peer && hio_dev_thr_read(thr_task->peer, 0) <= -1) if (thr->peer && hio_dev_thr_read(thr->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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
hio_dev_thr_halt (thr_task->peer); hio_dev_thr_halt (thr->peer);
} }
} }
if (old_over != THR_TASK_OVER_ALL && thr_task->over == THR_TASK_OVER_ALL) if (old_over != THR_TASK_OVER_ALL && thr->over == THR_TASK_OVER_ALL)
{ {
/* ready to stop */ /* ready to stop */
if (thr_task->peer) if (thr->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_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer as it is unneeded\n", thr->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
hio_dev_thr_halt (thr_task->peer); hio_dev_thr_halt (thr->peer);
} }
if (thr_task->csck) if (thr->csck)
{ {
HIO_ASSERT (hio, thr_task->task_client != HIO_NULL); HIO_ASSERT (hio, thr->task_client != HIO_NULL);
if (thr_task->keep_alive && !thr_task->client_eof_detected) if (thr->keep_alive && !thr->client_eof_detected)
{ {
/* how to arrange to delete this thr_task object and put the socket back to the normal waiting state??? */ /* 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->task_client->task == (hio_svc_htts_task_t*)thr_task); HIO_ASSERT (thr->htts->hio, thr->task_client->task == (hio_svc_htts_task_t*)thr);
HIO_SVC_HTTS_TASK_UNREF (thr_task->task_client->task); HIO_SVC_HTTS_TASK_UNREF (thr->task_client->task);
/* IMPORTANT: thr_task must not be accessed from here down as it could have been destroyed */ /* IMPORTANT: thr_task must not be accessed from here down as it could have been destroyed */
} }
else 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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
hio_dev_sck_shutdown (thr_task->csck, HIO_DEV_SCK_SHUTDOWN_WRITE); hio_dev_sck_shutdown (thr->csck, HIO_DEV_SCK_SHUTDOWN_WRITE);
hio_dev_sck_halt (thr_task->csck); hio_dev_sck_halt (thr->csck);
} }
} }
} }
@ -273,141 +273,141 @@ static HIO_INLINE void thr_task_mark_over (thr_task_t* thr_task, int over_bits)
static void thr_task_on_kill (hio_svc_htts_task_t* task) static void thr_task_on_kill (hio_svc_htts_task_t* task)
{ {
thr_task_t* thr_task = (thr_task_t*)task; thr_task_t* thr = (thr_task_t*)task;
hio_t* hio = thr_task->htts->hio; hio_t* hio = thr->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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
if (thr_task->on_kill) thr_task->on_kill (task); if (thr->on_kill) thr->on_kill (task);
if (thr_task->peer) if (thr->peer)
{ {
thr_peer_xtn_t* thr_peer = hio_dev_thr_getxtn(thr_task->peer); thr_peer_xtn_t* peer_xtn = hio_dev_thr_getxtn(thr->peer);
if (thr_peer->task) if (peer_xtn->task)
{ {
/* thr_peer->task may not be NULL if the resource is killed regardless of the reference count. /* peer_xtn->task may not be NULL if the resource is killed regardless of the reference count.
* anyway, don't use HIO_SVC_HTTS_TASK_UNREF (thr_peer->task) because the resource itself * anyway, don't use HIO_SVC_HTTS_TASK_UNREF (peer_xtn->task) because the resource itself
* is already being killed. */ * is already being killed. */
thr_peer->task = HIO_NULL; peer_xtn->task = HIO_NULL;
} }
hio_dev_thr_kill (thr_task->peer); hio_dev_thr_kill (thr->peer);
thr_task->peer = HIO_NULL; thr->peer = HIO_NULL;
} }
if (thr_task->peer_htrd) if (thr->peer_htrd)
{ {
thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(thr_task->peer_htrd); thr_peer_xtn_t* peer_xtn = hio_htrd_getxtn(thr->peer_htrd);
if (thr_peer->task) thr_peer->task = HIO_NULL; // no HIO_SVC_HTTS_TASK_UNREF() for the same reason above if (peer_xtn->task) peer_xtn->task = HIO_NULL; // no HIO_SVC_HTTS_TASK_UNREF() for the same reason above
hio_htrd_close (thr_task->peer_htrd); hio_htrd_close (thr->peer_htrd);
thr_task->peer_htrd = HIO_NULL; thr->peer_htrd = HIO_NULL;
} }
if (thr_task->csck) if (thr->csck)
{ {
HIO_ASSERT (hio, thr_task->task_client != HIO_NULL); HIO_ASSERT (hio, thr->task_client != HIO_NULL);
/* restore callbacks */ /* restore callbacks */
if (thr_task->client_org_on_read) thr_task->csck->on_read = thr_task->client_org_on_read; if (thr->client_org_on_read) thr->csck->on_read = thr->client_org_on_read;
if (thr_task->client_org_on_write) thr_task->csck->on_write = thr_task->client_org_on_write; if (thr->client_org_on_write) thr->csck->on_write = thr->client_org_on_write;
if (thr_task->client_org_on_disconnect) thr_task->csck->on_disconnect = thr_task->client_org_on_disconnect; if (thr->client_org_on_disconnect) thr->csck->on_disconnect = thr->client_org_on_disconnect;
if (thr_task->client_htrd_recbs_changed) hio_htrd_setrecbs (thr_task->task_client->htrd, &thr_task->client_htrd_org_recbs); if (thr->client_htrd_recbs_changed) hio_htrd_setrecbs (thr->task_client->htrd, &thr->client_htrd_org_recbs);
if (!thr_task->keep_alive || hio_dev_sck_read(thr_task->csck, 1) <= -1) if (!thr->keep_alive || hio_dev_sck_read(thr->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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
hio_dev_sck_halt (thr_task->csck); hio_dev_sck_halt (thr->csck);
} }
} }
thr_task->client_org_on_read = HIO_NULL; thr->client_org_on_read = HIO_NULL;
thr_task->client_org_on_write = HIO_NULL; thr->client_org_on_write = HIO_NULL;
thr_task->client_org_on_disconnect = HIO_NULL; thr->client_org_on_disconnect = HIO_NULL;
thr_task->client_htrd_recbs_changed = 0; thr->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 */ if (thr->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (thr); /* 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->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->htts, thr, thr->task_client, (thr->csck? thr->csck->hnd: -1), thr->peer);
} }
static void thr_peer_on_close (hio_dev_thr_t* thr, hio_dev_thr_sid_t sid) static void thr_peer_on_close (hio_dev_thr_t* peer, hio_dev_thr_sid_t sid)
{ {
hio_t* hio = thr->hio; hio_t* hio = peer->hio;
thr_peer_xtn_t* thr_peer = (thr_peer_xtn_t*)hio_dev_thr_getxtn(thr); thr_peer_xtn_t* peer_xtn = (thr_peer_xtn_t*)hio_dev_thr_getxtn(peer);
thr_task_t* thr_task = thr_peer->task; thr_task_t* thr = peer_xtn->task;
if (!thr_task) return; /* thr task already gone */ if (!thr) return; /* thr task already gone */
switch (sid) switch (sid)
{ {
case HIO_DEV_THR_MASTER: case HIO_DEV_THR_MASTER:
HIO_DEBUG2 (hio, "HTTS(%p) - peer %p closing master\n", thr_task->htts, thr); HIO_DEBUG2 (hio, "HTTS(%p) - peer %p closing master\n", thr->htts, peer);
thr_task->peer = HIO_NULL; /* clear this peer from the state */ thr->peer = HIO_NULL; /* clear this peer from the state */
HIO_ASSERT (hio, thr_peer->task != HIO_NULL); HIO_ASSERT (hio, peer_xtn->task != HIO_NULL);
HIO_SVC_HTTS_TASK_UNREF (thr_peer->task); HIO_SVC_HTTS_TASK_UNREF (peer_xtn->task);
if (thr_task->peer_htrd) if (thr->peer_htrd)
{ {
/* once this peer device is closed, peer's htrd is also never used. /* once this peer device is closed, peer's htrd is also never used.
* it's safe to detach the extra information attached on the htrd object. */ * it's safe to detach the extra information attached on the htrd object. */
thr_peer = hio_htrd_getxtn(thr_task->peer_htrd); peer_xtn = hio_htrd_getxtn(thr->peer_htrd);
HIO_ASSERT (hio, thr_peer->task != HIO_NULL); HIO_ASSERT (hio, peer_xtn->task != HIO_NULL);
HIO_SVC_HTTS_TASK_UNREF (thr_peer->task); HIO_SVC_HTTS_TASK_UNREF (peer_xtn->task);
} }
break; break;
case HIO_DEV_THR_OUT: case HIO_DEV_THR_OUT:
HIO_ASSERT (hio, thr_task->peer == thr); HIO_ASSERT (hio, thr->peer == peer);
HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr_task->htts, thr, sid); HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr->htts, peer, sid);
if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER)) if (!(thr->over & THR_TASK_OVER_READ_FROM_PEER))
{ {
if (thr_task_write_last_chunk_to_client(thr_task) <= -1) if (thr_task_write_last_chunk_to_client(thr) <= -1)
thr_task_halt_participating_devices (thr_task); thr_task_halt_participating_devices (thr);
else else
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER); thr_task_mark_over (thr, THR_TASK_OVER_READ_FROM_PEER);
} }
break; break;
case HIO_DEV_THR_IN: case HIO_DEV_THR_IN:
thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_PEER); thr_task_mark_over (thr, THR_TASK_OVER_WRITE_TO_PEER);
break; break;
default: default:
HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr_task->htts, thr, sid); HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr->htts, peer, sid);
/* do nothing */ /* do nothing */
break; break;
} }
} }
static int thr_peer_on_read (hio_dev_thr_t* thr, const void* data, hio_iolen_t dlen) static int thr_peer_on_read (hio_dev_thr_t* peer, const void* data, hio_iolen_t dlen)
{ {
hio_t* hio = thr->hio; hio_t* hio = peer->hio;
thr_peer_xtn_t* thr_peer = (thr_peer_xtn_t*)hio_dev_thr_getxtn(thr); thr_peer_xtn_t* peer_xtn = (thr_peer_xtn_t*)hio_dev_thr_getxtn(peer);
thr_task_t* thr_task = thr_peer->task; thr_task_t* thr = peer_xtn->task;
HIO_ASSERT (hio, thr_task != HIO_NULL); HIO_ASSERT (hio, thr != HIO_NULL);
if (dlen <= -1) if (dlen <= -1)
{ {
HIO_DEBUG2 (hio, "HTTPS(%p) - read error from peer %p\n", thr_task->htts, thr); HIO_DEBUG2 (hio, "HTTPS(%p) - read error from peer %p\n", thr->htts, peer);
goto oops; goto oops;
} }
if (dlen == 0) if (dlen == 0)
{ {
HIO_DEBUG2 (hio, "HTTPS(%p) - EOF from peer %p\n", thr_task->htts, thr); HIO_DEBUG2 (hio, "HTTPS(%p) - EOF from peer %p\n", thr->htts, peer);
if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER)) if (!(thr->over & THR_TASK_OVER_READ_FROM_PEER))
{ {
int n; int n;
/* the thr script could be misbehaviing. /* the thr script could be misbehaviing.
* it still has to read more but EOF is read. * it still has to read more but EOF is read.
* otherwise client_peer_htrd_poke() should have been called */ * otherwise client_peer_htrd_poke() should have been called */
n = thr_task_write_last_chunk_to_client(thr_task); n = thr_task_write_last_chunk_to_client(thr);
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER); thr_task_mark_over (thr, THR_TASK_OVER_READ_FROM_PEER);
if (n <= -1) goto oops; if (n <= -1) goto oops;
} }
} }
@ -415,16 +415,16 @@ static int thr_peer_on_read (hio_dev_thr_t* thr, const void* data, hio_iolen_t d
{ {
hio_oow_t rem; hio_oow_t rem;
HIO_ASSERT (hio, !(thr_task->over & THR_TASK_OVER_READ_FROM_PEER)); HIO_ASSERT (hio, !(thr->over & THR_TASK_OVER_READ_FROM_PEER));
if (hio_htrd_feed(thr_task->peer_htrd, data, dlen, &rem) <= -1) if (hio_htrd_feed(thr->peer_htrd, data, dlen, &rem) <= -1)
{ {
HIO_DEBUG2 (hio, "HTTPS(%p) - unable to feed peer htrd - peer %p\n", thr_task->htts, thr); HIO_DEBUG2 (hio, "HTTPS(%p) - unable to feed peer htrd - peer %p\n", thr->htts, peer);
if (!thr_task->ever_attempted_to_write_to_client && if (!thr->ever_attempted_to_write_to_client &&
!(thr_task->over & THR_TASK_OVER_WRITE_TO_CLIENT)) !(thr->over & THR_TASK_OVER_WRITE_TO_CLIENT))
{ {
thr_task_send_final_status_to_client (thr_task, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 1); /* don't care about error because it jumps to oops below anyway */ thr_task_send_final_status_to_client (thr, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 1); /* don't care about error because it jumps to oops below anyway */
} }
goto oops; goto oops;
@ -441,7 +441,7 @@ static int thr_peer_on_read (hio_dev_thr_t* thr, const void* data, hio_iolen_t d
return 0; return 0;
oops: oops:
thr_task_halt_participating_devices (thr_task); thr_task_halt_participating_devices (thr);
return 0; return 0;
} }
@ -476,16 +476,16 @@ static int thr_peer_capture_response_header (hio_htre_t* req, const hio_bch_t* k
static int thr_peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) 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_peer_xtn_t* peer_xtn = hio_htrd_getxtn(htrd);
thr_task_t* thr_task = thr_peer->task; thr_task_t* thr = peer_xtn->task;
hio_svc_htts_cli_t* cli = thr_task->task_client; hio_svc_htts_cli_t* cli = thr->task_client;
hio_bch_t dtbuf[64]; hio_bch_t dtbuf[64];
int status_code = HIO_HTTP_STATUS_OK; int status_code = HIO_HTTP_STATUS_OK;
if (req->attr.content_length) if (req->attr.content_length)
{ {
// TOOD: remove content_length if content_length is negative or not numeric. // TOOD: remove content_length if content_length is negative or not numeric.
thr_task->res_mode_to_cli = THR_TASK_RES_MODE_LENGTH; thr->res_mode_to_cli = THR_TASK_RES_MODE_LENGTH;
} }
if (req->attr.status) if (req->attr.status)
@ -501,13 +501,13 @@ static int thr_peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req)
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf)); hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\n", if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\n",
thr_task->task_req_version.major, thr_task->task_req_version.minor, thr->task_req_version.major, thr->task_req_version.minor,
status_code, hio_http_status_to_bcstr(status_code), status_code, hio_http_status_to_bcstr(status_code),
cli->htts->server_name, dtbuf) == (hio_oow_t)-1) return -1; cli->htts->server_name, dtbuf) == (hio_oow_t)-1) return -1;
if (hio_htre_walkheaders(req, thr_peer_capture_response_header, cli) <= -1) return -1; if (hio_htre_walkheaders(req, thr_peer_capture_response_header, cli) <= -1) return -1;
switch (thr_task->res_mode_to_cli) switch (thr->res_mode_to_cli)
{ {
case THR_TASK_RES_MODE_CHUNKED: case THR_TASK_RES_MODE_CHUNKED:
if (hio_becs_cat(cli->sbuf, "Transfer-Encoding: chunked\r\n") == (hio_oow_t)-1) return -1; if (hio_becs_cat(cli->sbuf, "Transfer-Encoding: chunked\r\n") == (hio_oow_t)-1) return -1;
@ -519,34 +519,34 @@ static int thr_peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req)
break; break;
case THR_TASK_RES_MODE_LENGTH: case THR_TASK_RES_MODE_LENGTH:
if (hio_becs_cat(cli->sbuf, (thr_task->keep_alive? "Connection: keep-alive\r\n": "Connection: close\r\n")) == (hio_oow_t)-1) return -1; if (hio_becs_cat(cli->sbuf, (thr->keep_alive? "Connection: keep-alive\r\n": "Connection: close\r\n")) == (hio_oow_t)-1) return -1;
} }
if (hio_becs_cat(cli->sbuf, "\r\n") == (hio_oow_t)-1) return -1; if (hio_becs_cat(cli->sbuf, "\r\n") == (hio_oow_t)-1) return -1;
return thr_task_write_to_client(thr_task, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)); return thr_task_write_to_client(thr, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf));
} }
static int thr_peer_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req) static int thr_peer_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req)
{ {
/* client request got completed */ /* client request got completed */
thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(htrd); thr_peer_xtn_t* peer_xtn = hio_htrd_getxtn(htrd);
thr_task_t* thr_task = thr_peer->task; thr_task_t* thr = peer_xtn->task;
if (thr_task_write_last_chunk_to_client(thr_task) <= -1) return -1; if (thr_task_write_last_chunk_to_client(thr) <= -1) return -1;
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER); thr_task_mark_over (thr, THR_TASK_OVER_READ_FROM_PEER);
return 0; return 0;
} }
static int thr_peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const hio_bch_t* data, hio_oow_t dlen) static int thr_peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const hio_bch_t* data, hio_oow_t dlen)
{ {
thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(htrd); thr_peer_xtn_t* peer_xtn = hio_htrd_getxtn(htrd);
thr_task_t* thr_task = thr_peer->task; thr_task_t* thr = peer_xtn->task;
HIO_ASSERT (thr_task->htts->hio, htrd == thr_task->peer_htrd); HIO_ASSERT (thr->htts->hio, htrd == thr->peer_htrd);
switch (thr_task->res_mode_to_cli) switch (thr->res_mode_to_cli)
{ {
case THR_TASK_RES_MODE_CHUNKED: case THR_TASK_RES_MODE_CHUNKED:
{ {
@ -567,19 +567,19 @@ static int thr_peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const
iov[2].iov_ptr = "\r\n"; iov[2].iov_ptr = "\r\n";
iov[2].iov_len = 2; iov[2].iov_len = 2;
if (thr_task_writev_to_client(thr_task, iov, HIO_COUNTOF(iov)) <= -1) goto oops; if (thr_task_writev_to_client(thr, iov, HIO_COUNTOF(iov)) <= -1) goto oops;
break; break;
} }
case THR_TASK_RES_MODE_CLOSE: case THR_TASK_RES_MODE_CLOSE:
case THR_TASK_RES_MODE_LENGTH: case THR_TASK_RES_MODE_LENGTH:
if (thr_task_write_to_client(thr_task, data, dlen) <= -1) goto oops; if (thr_task_write_to_client(thr, data, dlen) <= -1) goto oops;
break; break;
} }
if (thr_task->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD) if (thr->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD)
{ {
if (hio_dev_thr_read(thr_task->peer, 0) <= -1) goto oops; if (hio_dev_thr_read(thr->peer, 0) <= -1) goto oops;
} }
return 0; return 0;
@ -601,12 +601,12 @@ static int thr_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req)
hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd); hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd);
hio_dev_sck_t* sck = htrdxtn->sck; hio_dev_sck_t* sck = htrdxtn->sck;
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
thr_task_t* thr_task = (thr_task_t*)cli->task; thr_task_t* thr = (thr_task_t*)cli->task;
/* indicate EOF to the client peer */ /* indicate EOF to the client peer */
if (thr_task_write_to_peer(thr_task, HIO_NULL, 0) <= -1) return -1; if (thr_task_write_to_peer(thr, HIO_NULL, 0) <= -1) return -1;
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_CLIENT); thr_task_mark_over (thr, THR_TASK_OVER_READ_FROM_CLIENT);
return 0; return 0;
} }
@ -615,10 +615,10 @@ static int thr_client_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, cons
hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd); hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd);
hio_dev_sck_t* sck = htrdxtn->sck; hio_dev_sck_t* sck = htrdxtn->sck;
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
thr_task_t* thr_task = (thr_task_t*)cli->task; thr_task_t* thr = (thr_task_t*)cli->task;
HIO_ASSERT (sck->hio, cli->sck == sck); HIO_ASSERT (sck->hio, cli->sck == sck);
return thr_task_write_to_peer(thr_task, data, dlen); return thr_task_write_to_peer(thr, data, dlen);
} }
static hio_htrd_recbs_t thr_client_htrd_recbs = static hio_htrd_recbs_t thr_client_htrd_recbs =
@ -628,19 +628,19 @@ static hio_htrd_recbs_t thr_client_htrd_recbs =
thr_client_htrd_push_content thr_client_htrd_push_content
}; };
static int thr_peer_on_write (hio_dev_thr_t* thr, hio_iolen_t wrlen, void* wrctx) static int thr_peer_on_write (hio_dev_thr_t* peer, hio_iolen_t wrlen, void* wrctx)
{ {
hio_t* hio = thr->hio; hio_t* hio = peer->hio;
thr_peer_xtn_t* thr_peer = (thr_peer_xtn_t*)hio_dev_thr_getxtn(thr); thr_peer_xtn_t* peer_xtn = (thr_peer_xtn_t*)hio_dev_thr_getxtn(peer);
thr_task_t* thr_task = thr_peer->task; thr_task_t* thr = peer_xtn->task;
if (!thr_task) return 0; /* there is nothing i can do. the thr_task is being cleared or has been cleared already. */ if (!thr) return 0; /* there is nothing i can do. the thr_task is being cleared or has been cleared already. */
HIO_ASSERT (hio, thr_task->peer == thr); HIO_ASSERT (hio, thr->peer == peer);
if (wrlen <= -1) if (wrlen <= -1)
{ {
HIO_DEBUG2 (hio, "HTTS(%p) - unable to write to peer %p\n", thr_task->htts, thr); HIO_DEBUG2 (hio, "HTTS(%p) - unable to write to peer %p\n", thr->htts, peer);
goto oops; goto oops;
} }
else if (wrlen == 0) else if (wrlen == 0)
@ -648,58 +648,58 @@ static int thr_peer_on_write (hio_dev_thr_t* thr, hio_iolen_t wrlen, void* wrctx
/* indicated EOF */ /* indicated EOF */
/* do nothing here as i didn't incremented num_pending_writes_to_peer when making the write request */ /* do nothing here as i didn't incremented num_pending_writes_to_peer when making the write request */
thr_task->num_pending_writes_to_peer--; thr->num_pending_writes_to_peer--;
HIO_ASSERT (hio, thr_task->num_pending_writes_to_peer == 0); HIO_ASSERT (hio, thr->num_pending_writes_to_peer == 0);
HIO_DEBUG2 (hio, "HTTS(%p) - indicated EOF to peer %p\n", thr_task->htts, thr); HIO_DEBUG2 (hio, "HTTS(%p) - indicated EOF to peer %p\n", thr->htts, peer);
/* indicated EOF to the peer side. i need no more data from the client side. /* 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 */ * i don't need to enable input watching in the client side either */
thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_PEER); thr_task_mark_over (thr, THR_TASK_OVER_WRITE_TO_PEER);
} }
else else
{ {
HIO_ASSERT (hio, thr_task->num_pending_writes_to_peer > 0); HIO_ASSERT (hio, thr->num_pending_writes_to_peer > 0);
thr_task->num_pending_writes_to_peer--; thr->num_pending_writes_to_peer--;
if (thr_task->num_pending_writes_to_peer == THR_TASK_PENDING_IO_THRESHOLD) if (thr->num_pending_writes_to_peer == THR_TASK_PENDING_IO_THRESHOLD)
{ {
if (!(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT) && if (!(thr->over & THR_TASK_OVER_READ_FROM_CLIENT) &&
hio_dev_sck_read(thr_task->csck, 1) <= -1) goto oops; hio_dev_sck_read(thr->csck, 1) <= -1) goto oops;
} }
if ((thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT) && thr_task->num_pending_writes_to_peer <= 0) if ((thr->over & THR_TASK_OVER_READ_FROM_CLIENT) && thr->num_pending_writes_to_peer <= 0)
{ {
thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_PEER); thr_task_mark_over (thr, THR_TASK_OVER_WRITE_TO_PEER);
} }
} }
return 0; return 0;
oops: oops:
thr_task_halt_participating_devices (thr_task); thr_task_halt_participating_devices (thr);
return 0; return 0;
} }
static void thr_client_on_disconnect (hio_dev_sck_t* sck) static void thr_client_on_disconnect (hio_dev_sck_t* sck)
{ {
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
thr_task_t* thr_task = (thr_task_t*)cli->task; thr_task_t* thr = (thr_task_t*)cli->task;
hio_svc_htts_t* htts = thr_task->htts; hio_svc_htts_t* htts = thr->htts;
hio_t* hio = sck->hio; hio_t* hio = sck->hio;
HIO_ASSERT (hio, sck = thr_task->csck); HIO_ASSERT (hio, sck = thr->csck);
HIO_DEBUG4 (hio, "HTTS(%p) - thr(t=%p,c=%p,csck=%p) - client socket disconnect notified\n", htts, thr_task, cli, sck); HIO_DEBUG4 (hio, "HTTS(%p) - thr(t=%p,c=%p,csck=%p) - client socket disconnect notified\n", htts, thr, cli, sck);
thr_task->client_disconnected = 1; thr->client_disconnected = 1;
thr_task->csck = HIO_NULL; thr->csck = HIO_NULL;
thr_task->task_client = HIO_NULL; thr->task_client = HIO_NULL;
if (thr_task->client_org_on_disconnect) if (thr->client_org_on_disconnect)
{ {
thr_task->client_org_on_disconnect (sck); thr->client_org_on_disconnect (sck);
/* this original callback destroys the associated resource. /* this original callback destroys the associated resource.
* thr_task must not be accessed from here down */ * thr_task must not be accessed from here down */
} }
HIO_DEBUG4 (hio, "HTTS(%p) - thr(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, thr_task, cli, sck); HIO_DEBUG4 (hio, "HTTS(%p) - thr(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, thr, cli, sck);
/* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */ /* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */
} }
@ -707,7 +707,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
{ {
hio_t* hio = sck->hio; hio_t* hio = sck->hio;
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
thr_task_t* thr_task = (thr_task_t*)cli->task; thr_task_t* thr = (thr_task_t*)cli->task;
HIO_ASSERT (hio, sck == cli->sck); HIO_ASSERT (hio, sck == cli->sck);
@ -718,7 +718,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
goto oops; goto oops;
} }
if (!thr_task->peer) if (!thr->peer)
{ {
/* the peer is gone */ /* the peer is gone */
goto oops; /* do what? just return 0? */ goto oops; /* do what? just return 0? */
@ -727,14 +727,14 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
if (len == 0) if (len == 0)
{ {
/* EOF on the client side. arrange to close */ /* EOF on the client side. arrange to close */
HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", thr_task->htts, sck, (int)sck->hnd); HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", thr->htts, sck, (int)sck->hnd);
thr_task->client_eof_detected = 1; thr->client_eof_detected = 1;
if (!(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without thr_client_htrd_poke() */ if (!(thr->over & THR_TASK_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without thr_client_htrd_poke() */
{ {
int n; int n;
n = thr_task_write_to_peer(thr_task, HIO_NULL, 0); n = thr_task_write_to_peer(thr, HIO_NULL, 0);
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_CLIENT); thr_task_mark_over (thr, THR_TASK_OVER_READ_FROM_CLIENT);
if (n <= -1) goto oops; if (n <= -1) goto oops;
} }
} }
@ -742,7 +742,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
{ {
hio_oow_t rem; hio_oow_t rem;
HIO_ASSERT (hio, !(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)); HIO_ASSERT (hio, !(thr->over & THR_TASK_OVER_READ_FROM_CLIENT));
if (hio_htrd_feed(cli->htrd, buf, len, &rem) <= -1) goto oops; if (hio_htrd_feed(cli->htrd, buf, len, &rem) <= -1) goto oops;
@ -756,7 +756,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
return 0; return 0;
oops: oops:
thr_task_halt_participating_devices (thr_task); thr_task_halt_participating_devices (thr);
return 0; return 0;
} }
@ -764,7 +764,7 @@ static int thr_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrc
{ {
hio_t* hio = sck->hio; hio_t* hio = sck->hio;
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
thr_task_t* thr_task = (thr_task_t*)cli->task; thr_task_t* thr = (thr_task_t*)cli->task;
if (wrlen <= -1) if (wrlen <= -1)
{ {
@ -775,35 +775,35 @@ static int thr_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrc
if (wrlen == 0) if (wrlen == 0)
{ {
/* if the connect is keep-alive, this part may not be called */ /* if the connect is keep-alive, this part may not be called */
thr_task->num_pending_writes_to_client--; thr->num_pending_writes_to_client--;
HIO_ASSERT (hio, thr_task->num_pending_writes_to_client == 0); HIO_ASSERT (hio, thr->num_pending_writes_to_client == 0);
HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", thr_task->htts, sck, (int)sck->hnd); HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", thr->htts, sck, (int)sck->hnd);
/* since EOF has been indicated to the client, it must not write to the client any further. /* 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. * 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 */ * i don't need to enable input watching on the peer side */
thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_CLIENT); thr_task_mark_over (thr, THR_TASK_OVER_WRITE_TO_CLIENT);
} }
else else
{ {
HIO_ASSERT (hio, thr_task->num_pending_writes_to_client > 0); HIO_ASSERT (hio, thr->num_pending_writes_to_client > 0);
thr_task->num_pending_writes_to_client--; thr->num_pending_writes_to_client--;
if (thr_task->peer && thr_task->num_pending_writes_to_client == THR_TASK_PENDING_IO_THRESHOLD) if (thr->peer && thr->num_pending_writes_to_client == THR_TASK_PENDING_IO_THRESHOLD)
{ {
if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER) && if (!(thr->over & THR_TASK_OVER_READ_FROM_PEER) &&
hio_dev_thr_read(thr_task->peer, 1) <= -1) goto oops; hio_dev_thr_read(thr->peer, 1) <= -1) goto oops;
} }
if ((thr_task->over & THR_TASK_OVER_READ_FROM_PEER) && thr_task->num_pending_writes_to_client <= 0) if ((thr->over & THR_TASK_OVER_READ_FROM_PEER) && thr->num_pending_writes_to_client <= 0)
{ {
thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_CLIENT); thr_task_mark_over (thr, THR_TASK_OVER_WRITE_TO_CLIENT);
} }
} }
return 0; return 0;
oops: oops:
thr_task_halt_participating_devices (thr_task); thr_task_halt_participating_devices (thr);
return 0; return 0;
} }
@ -879,8 +879,8 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
{ {
hio_t* hio = htts->hio; hio_t* hio = htts->hio;
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck);
thr_task_t* thr_task = HIO_NULL; thr_task_t* thr = HIO_NULL;
thr_peer_xtn_t* thr_peer; thr_peer_xtn_t* peer_xtn;
hio_dev_thr_make_t mi; hio_dev_thr_make_t mi;
thr_func_start_t* tfs; thr_func_start_t* tfs;
int have_content; int have_content;
@ -919,31 +919,31 @@ 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_write = thr_peer_on_write;
mi.on_close = thr_peer_on_close; 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, cli); thr = (thr_task_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*thr), thr_task_on_kill, req, cli);
if (HIO_UNLIKELY(!thr_task)) goto oops; if (HIO_UNLIKELY(!thr)) goto oops;
thr_task->on_kill = on_kill; thr->on_kill = on_kill;
thr_task->options = options; thr->options = options;
thr_task->csck = csck; thr->csck = csck;
thr_task->task_client = cli; /* for faster access without going through csck. */ thr->task_client = cli; /* for faster access without going through csck. */
/*thr_task->num_pending_writes_to_client = 0; /*thr->num_pending_writes_to_client = 0;
thr_task->num_pending_writes_to_peer = 0;*/ thr->num_pending_writes_to_peer = 0;*/
thr_task->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &thr_task->req_content_length); thr->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &thr->req_content_length);
thr_task->client_org_on_read = csck->on_read; thr->client_org_on_read = csck->on_read;
thr_task->client_org_on_write = csck->on_write; thr->client_org_on_write = csck->on_write;
thr_task->client_org_on_disconnect = csck->on_disconnect; thr->client_org_on_disconnect = csck->on_disconnect;
csck->on_read = thr_client_on_read; csck->on_read = thr_client_on_read;
csck->on_write = thr_client_on_write; csck->on_write = thr_client_on_write;
csck->on_disconnect = thr_client_on_disconnect; csck->on_disconnect = thr_client_on_disconnect;
/* attach the thr task to the client socket via the task field in the extended space of the socket */ /* attach the thr task to the client socket via the task field in the extended space of the socket */
HIO_ASSERT (hio, cli->task == HIO_NULL); HIO_ASSERT (hio, cli->task == HIO_NULL);
HIO_SVC_HTTS_TASK_REF ((hio_svc_htts_task_t*)thr_task, cli->task); HIO_SVC_HTTS_TASK_REF ((hio_svc_htts_task_t*)thr, cli->task);
thr_task->peer = hio_dev_thr_make(hio, HIO_SIZEOF(*thr_peer), &mi); thr->peer = hio_dev_thr_make(hio, HIO_SIZEOF(*peer_xtn), &mi);
if (HIO_UNLIKELY(!thr_task->peer)) if (HIO_UNLIKELY(!thr->peer))
{ {
/* no need to detach the attached task here because that is handled /* no need to detach the attached task here because that is handled
* in the kill/disconnect callbacks of relevant devices */ * in the kill/disconnect callbacks of relevant devices */
@ -954,20 +954,20 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
tfs = HIO_NULL; /* mark that tfs is delegated to the thread */ tfs = HIO_NULL; /* mark that tfs is delegated to the thread */
/* attach the thr task to the peer thread device */ /* attach the thr task to the peer thread device */
thr_peer = hio_dev_thr_getxtn(thr_task->peer); peer_xtn = hio_dev_thr_getxtn(thr->peer);
HIO_SVC_HTTS_TASK_REF (thr_task, thr_peer->task); HIO_SVC_HTTS_TASK_REF (thr, peer_xtn->task);
thr_task->peer_htrd = hio_htrd_open(hio, HIO_SIZEOF(*thr_peer)); thr->peer_htrd = hio_htrd_open(hio, HIO_SIZEOF(*peer_xtn));
if (HIO_UNLIKELY(!thr_task->peer_htrd)) goto oops; if (HIO_UNLIKELY(!thr->peer_htrd)) goto oops;
hio_htrd_setoption (thr_task->peer_htrd, HIO_HTRD_SKIP_INITIAL_LINE | HIO_HTRD_RESPONSE); hio_htrd_setoption (thr->peer_htrd, HIO_HTRD_SKIP_INITIAL_LINE | HIO_HTRD_RESPONSE);
hio_htrd_setrecbs (thr_task->peer_htrd, &thr_peer_htrd_recbs); hio_htrd_setrecbs (thr->peer_htrd, &thr_peer_htrd_recbs);
/* attach the thr task to the htrd parser set on the peer thread device */ /* attach the thr task to the htrd parser set on the peer thread device */
thr_peer = hio_htrd_getxtn(thr_task->peer_htrd); peer_xtn = hio_htrd_getxtn(thr->peer_htrd);
HIO_SVC_HTTS_TASK_REF (thr_task, thr_peer->task); HIO_SVC_HTTS_TASK_REF (thr, peer_xtn->task);
#if !defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) #if !defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH)
if (thr_task->req_content_length_unlimited) if (thr->req_content_length_unlimited)
{ {
/* Transfer-Encoding is chunked. no content-length is known in advance. */ /* Transfer-Encoding is chunked. no content-length is known in advance. */
@ -975,7 +975,7 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
* option 2. send 411 Length Required immediately * 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] */ * option 3. set Content-Length to -1 and use EOF to indicate the end of content [Non-Standard] */
if (thr_task_send_final_status_to_client(thr_task, HIO_HTTP_STATUS_LENGTH_REQUIRED, 1) <= -1) goto oops; if (thr_task_send_final_status_to_client(thr, HIO_HTTP_STATUS_LENGTH_REQUIRED, 1) <= -1) goto oops;
} }
#endif #endif
@ -985,7 +985,7 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
/* CAN I LET the thr SCRIPT handle this? */ /* CAN I LET the thr SCRIPT handle this? */
if (!(options & HIO_SVC_HTTS_THR_NO_100_CONTINUE) && if (!(options & HIO_SVC_HTTS_THR_NO_100_CONTINUE) &&
hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 &&
(thr_task->req_content_length_unlimited || thr_task->req_content_length > 0)) (thr->req_content_length_unlimited || thr->req_content_length > 0))
{ {
/* /*
* Don't send 100 Continue if http verions is lower than 1.1 * Don't send 100 Continue if http verions is lower than 1.1
@ -1003,61 +1003,61 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
hio_bch_t msgbuf[64]; hio_bch_t msgbuf[64];
hio_oow_t msglen; hio_oow_t msglen;
msglen = hio_fmttobcstr(hio, msgbuf, HIO_COUNTOF(msgbuf), "HTTP/%d.%d %d %hs\r\n\r\n", thr_task->task_req_version.major, thr_task->task_req_version.minor, HIO_HTTP_STATUS_CONTINUE, hio_http_status_to_bcstr(HIO_HTTP_STATUS_CONTINUE)); msglen = hio_fmttobcstr(hio, msgbuf, HIO_COUNTOF(msgbuf), "HTTP/%d.%d %d %hs\r\n\r\n", thr->task_req_version.major, thr->task_req_version.minor, HIO_HTTP_STATUS_CONTINUE, hio_http_status_to_bcstr(HIO_HTTP_STATUS_CONTINUE));
if (thr_task_write_to_client(thr_task, msgbuf, msglen) <= -1) goto oops; if (thr_task_write_to_client(thr, msgbuf, msglen) <= -1) goto oops;
thr_task->ever_attempted_to_write_to_client = 0; /* reset this as it's polluted for 100 continue */ thr->ever_attempted_to_write_to_client = 0; /* reset this as it's polluted for 100 continue */
} }
} }
else if (req->flags & HIO_HTRE_ATTR_EXPECT) else if (req->flags & HIO_HTRE_ATTR_EXPECT)
{ {
/* 417 Expectation Failed */ /* 417 Expectation Failed */
thr_task_send_final_status_to_client(thr_task, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1); thr_task_send_final_status_to_client(thr, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1);
goto oops; goto oops;
} }
#if defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) #if defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH)
have_content = thr_task->req_content_length > 0 || thr_task->req_content_length_unlimited; have_content = thr->req_content_length > 0 || thr->req_content_length_unlimited;
#else #else
have_content = thr_task->req_content_length > 0; have_content = thr->req_content_length > 0;
#endif #endif
if (have_content) if (have_content)
{ {
/* change the callbacks to subscribe to contents to be uploaded */ /* change the callbacks to subscribe to contents to be uploaded */
thr_task->client_htrd_org_recbs = *hio_htrd_getrecbs(thr_task->task_client->htrd); thr->client_htrd_org_recbs = *hio_htrd_getrecbs(thr->task_client->htrd);
thr_client_htrd_recbs.peek = thr_task->client_htrd_org_recbs.peek; thr_client_htrd_recbs.peek = thr->client_htrd_org_recbs.peek;
hio_htrd_setrecbs (thr_task->task_client->htrd, &thr_client_htrd_recbs); hio_htrd_setrecbs (thr->task_client->htrd, &thr_client_htrd_recbs);
thr_task->client_htrd_recbs_changed = 1; thr->client_htrd_recbs_changed = 1;
} }
else else
{ {
/* no content to be uploaded from the client */ /* no content to be uploaded from the client */
/* indicate EOF to the peer and disable input wathching from the client */ /* indicate EOF to the peer and disable input wathching from the client */
if (thr_task_write_to_peer(thr_task, HIO_NULL, 0) <= -1) goto oops; if (thr_task_write_to_peer(thr, HIO_NULL, 0) <= -1) goto oops;
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_CLIENT | THR_TASK_OVER_WRITE_TO_PEER); thr_task_mark_over (thr, THR_TASK_OVER_READ_FROM_CLIENT | THR_TASK_OVER_WRITE_TO_PEER);
} }
/* this may change later if Content-Length is included in the thr output */ /* this may change later if Content-Length is included in the thr output */
if (req->flags & HIO_HTRE_ATTR_KEEPALIVE) if (req->flags & HIO_HTRE_ATTR_KEEPALIVE)
{ {
thr_task->keep_alive = 1; thr->keep_alive = 1;
thr_task->res_mode_to_cli = THR_TASK_RES_MODE_CHUNKED; thr->res_mode_to_cli = THR_TASK_RES_MODE_CHUNKED;
/* the mode still can get switched to THR_TASK_RES_MODE_LENGTH if the thr script emits Content-Length */ /* the mode still can get switched to THR_TASK_RES_MODE_LENGTH if the thr script emits Content-Length */
} }
else else
{ {
thr_task->keep_alive = 0; thr->keep_alive = 0;
thr_task->res_mode_to_cli = THR_TASK_RES_MODE_CLOSE; thr->res_mode_to_cli = THR_TASK_RES_MODE_CLOSE;
} }
/* TODO: store current input watching state and use it when destroying the thr_task data */ /* TODO: store current input watching state and use it when destroying the thr_task data */
if (hio_dev_sck_read(csck, !(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) <= -1) goto oops; if (hio_dev_sck_read(csck, !(thr->over & THR_TASK_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, (hio_svc_htts_task_t*)thr_task); HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, (hio_svc_htts_task_t*)thr);
return 0; return 0;
oops: oops:
HIO_DEBUG2 (hio, "HTTS(%p) - FAILURE in dothr - socket(%p)\n", htts, csck); HIO_DEBUG2 (hio, "HTTS(%p) - FAILURE in dothr - socket(%p)\n", htts, csck);
if (tfs) free_thr_start_info (tfs); if (tfs) free_thr_start_info (tfs);
if (thr_task) thr_task_halt_participating_devices (thr_task); if (thr) thr_task_halt_participating_devices (thr);
return -1; return -1;
} }