Compare commits
No commits in common. "f03453c21b31c240acd8c257d7c49fd0f6e9591f" and "eb1f8af86bdba4915c63597f6ad69316a047369c" have entirely different histories.
f03453c21b
...
eb1f8af86b
@ -321,14 +321,7 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_
|
||||
}
|
||||
else if (mth == HIO_HTTP_OPTIONS)
|
||||
{
|
||||
/* TODO: write proper handler for preflight */
|
||||
//if (hio_svc_htts_dofun(htts, csck, req, options, HIO_NULL, 0) <= -1) goto oops;
|
||||
const hio_bch_t* msg = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: *\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Allow-Credentials: true\r\n\r\n";
|
||||
hio_dev_sck_write(csck, msg, strlen(msg), HIO_NULL, HIO_NULL);
|
||||
}
|
||||
else if (hio_comp_bcstr(qpath_ext, ".cgi", 0) == 0)
|
||||
{
|
||||
if (hio_svc_htts_docgi(htts, csck, req, ext->ai->docroot, qpath, 0) <= -1) goto oops;
|
||||
}
|
||||
else if (hio_comp_bcstr(qpath_ext, ".php", 0) == 0)
|
||||
{
|
||||
|
243
lib/http-cgi.c
243
lib/http-cgi.c
@ -63,11 +63,8 @@ struct cgi_t
|
||||
hio_oow_t num_pending_writes_to_peer;
|
||||
hio_dev_pro_t* peer;
|
||||
hio_htrd_t* peer_htrd;
|
||||
|
||||
hio_dev_sck_t* csck;
|
||||
hio_svc_htts_cli_t* client;
|
||||
hio_http_version_t req_version; /* client request */
|
||||
hio_http_method_t req_method;
|
||||
|
||||
unsigned int over: 4; /* must be large enough to accomodate CGI_OVER_ALL */
|
||||
unsigned int keep_alive: 1;
|
||||
@ -94,56 +91,50 @@ typedef struct cgi_peer_xtn_t cgi_peer_xtn_t;
|
||||
|
||||
static void cgi_halt_participating_devices (cgi_t* cgi)
|
||||
{
|
||||
HIO_ASSERT (cgi->htts->hio, cgi->client != HIO_NULL);
|
||||
HIO_ASSERT (cgi->htts->hio, cgi->csck != HIO_NULL);
|
||||
HIO_ASSERT (cgi->client->htts->hio, cgi->client != HIO_NULL);
|
||||
HIO_ASSERT (cgi->client->htts->hio, cgi->client->sck != HIO_NULL);
|
||||
|
||||
HIO_DEBUG5 (cgi->htts->hio, "HTTS(%p) - cgi(t=%p,c=%p(%d),p=%p) Halting participating devices\n", cgi->htts, cgi, cgi->csck, (cgi->csck? cgi->csck->hnd: -1), cgi->peer);
|
||||
HIO_DEBUG4 (cgi->client->htts->hio, "HTTS(%p) - Halting participating devices in cgi state %p(client=%p,peer=%p)\n", cgi->client->htts, cgi, cgi->client->sck, cgi->peer);
|
||||
|
||||
if (cgi->csck) hio_dev_sck_halt (cgi->csck);
|
||||
|
||||
hio_dev_sck_halt (cgi->client->sck);
|
||||
/* check for peer as it may not have been started */
|
||||
if (cgi->peer) hio_dev_pro_halt (cgi->peer);
|
||||
}
|
||||
|
||||
static int cgi_write_to_client (cgi_t* cgi, const void* data, hio_iolen_t dlen)
|
||||
{
|
||||
if (cgi->csck)
|
||||
cgi->ever_attempted_to_write_to_client = 1;
|
||||
|
||||
cgi->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_write(cgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
|
||||
{
|
||||
cgi->ever_attempted_to_write_to_client = 1;
|
||||
cgi->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cgi->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_write(cgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1)
|
||||
{
|
||||
cgi->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cgi->num_pending_writes_to_client > CGI_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
/* disable reading on the output stream of the peer */
|
||||
if (hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) return -1;
|
||||
}
|
||||
if (cgi->num_pending_writes_to_client > CGI_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
/* disable reading on the output stream of the peer */
|
||||
if (hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cgi_writev_to_client (cgi_t* cgi, hio_iovec_t* iov, hio_iolen_t iovcnt)
|
||||
{
|
||||
if (cgi->csck)
|
||||
cgi->ever_attempted_to_write_to_client = 1;
|
||||
|
||||
cgi->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_writev(cgi->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1)
|
||||
{
|
||||
cgi->ever_attempted_to_write_to_client = 1;
|
||||
cgi->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cgi->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_writev(cgi->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1)
|
||||
{
|
||||
cgi->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cgi->num_pending_writes_to_client > CGI_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
if (hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) return -1;
|
||||
}
|
||||
if (cgi->num_pending_writes_to_client > CGI_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
if (hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -152,26 +143,21 @@ 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_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
|
||||
status_msg = hio_http_status_to_bcstr(status_code);
|
||||
|
||||
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: %s\r\nConnection: %hs\r\nContent-Length: 0\r\n\r\n",
|
||||
cgi->req_version.major, cgi->req_version.minor,
|
||||
status_code, status_msg,
|
||||
status_code, hio_http_status_to_bcstr(status_code),
|
||||
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 (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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static int cgi_write_last_chunk_to_client (cgi_t* cgi)
|
||||
{
|
||||
if (!cgi->ever_attempted_to_write_to_client)
|
||||
@ -190,42 +176,37 @@ static int cgi_write_last_chunk_to_client (cgi_t* cgi)
|
||||
|
||||
static int cgi_write_to_peer (cgi_t* cgi, const void* data, hio_iolen_t dlen)
|
||||
{
|
||||
if (cgi->peer)
|
||||
cgi->num_pending_writes_to_peer++;
|
||||
if (hio_dev_pro_write(cgi->peer, data, dlen, HIO_NULL) <= -1)
|
||||
{
|
||||
cgi->num_pending_writes_to_peer++;
|
||||
if (hio_dev_pro_write(cgi->peer, data, dlen, HIO_NULL) <= -1)
|
||||
{
|
||||
cgi->num_pending_writes_to_peer--;
|
||||
return -1;
|
||||
}
|
||||
cgi->num_pending_writes_to_peer--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TODO: check if it's already finished or something.. */
|
||||
if (cgi->num_pending_writes_to_peer > CGI_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
/* suspend input watching */
|
||||
if (cgi->csck && hio_dev_sck_read(cgi->csck, 0) <= -1) return -1;
|
||||
}
|
||||
/* TODO: check if it's already finished or something.. */
|
||||
if (cgi->num_pending_writes_to_peer > CGI_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
/* suspend input watching */
|
||||
if (hio_dev_sck_read(cgi->client->sck, 0) <= -1) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits)
|
||||
{
|
||||
hio_svc_htts_t* htts = cgi->htts;
|
||||
hio_t* hio = htts->hio;
|
||||
unsigned int old_over;
|
||||
|
||||
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_DEBUG5 (cgi->htts->hio, "HTTS(%p) - client=%p peer=%p new-bits=%x over=%x\n", cgi->htts, cgi->client->sck, cgi->peer, (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)
|
||||
if (hio_dev_sck_read(cgi->client->sck, 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_dev_sck_halt (cgi->csck);
|
||||
HIO_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", cgi->htts, cgi->client->sck);
|
||||
hio_dev_sck_halt (cgi->client->sck);
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,7 +214,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_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting peer(%p) for failure to disable input watching\n", cgi->htts, cgi->peer);
|
||||
hio_dev_pro_halt (cgi->peer);
|
||||
}
|
||||
}
|
||||
@ -243,28 +224,24 @@ 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_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting peer(%p) as it is unneeded\n", cgi->htts, cgi->peer);
|
||||
hio_dev_pro_halt (cgi->peer);
|
||||
}
|
||||
|
||||
if (cgi->csck)
|
||||
if (cgi->keep_alive && !cgi->client_eof_detected)
|
||||
{
|
||||
HIO_ASSERT (hio, cgi->client != HIO_NULL);
|
||||
/* 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);
|
||||
|
||||
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_SVC_HTTS_TASK_UNREF (cgi->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_dev_sck_shutdown (cgi->csck, HIO_DEV_SCK_SHUTDOWN_WRITE);
|
||||
hio_dev_sck_halt (cgi->csck);
|
||||
}
|
||||
/*printf ("DETACHING FROM THE MAIN CLIENT TASK... state -> %p\n", cgi->client->task);*/
|
||||
HIO_SVC_HTTS_TASK_UNREF (cgi->client->task);
|
||||
/* cgi must not be accessed from here down as it could have been destroyed */
|
||||
}
|
||||
else
|
||||
{
|
||||
HIO_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", cgi->htts, cgi->client->sck);
|
||||
hio_dev_sck_shutdown (cgi->client->sck, HIO_DEV_SCK_SHUTDOWN_WRITE);
|
||||
hio_dev_sck_halt (cgi->client->sck);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -274,7 +251,7 @@ 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_DEBUG2 (hio, "HTTS(%p) - killing cgi client(%p)\n", cgi->htts, cgi->client->sck);
|
||||
|
||||
if (cgi->peer)
|
||||
{
|
||||
@ -294,35 +271,44 @@ static void cgi_on_kill (hio_svc_htts_task_t* task)
|
||||
cgi->peer_htrd = HIO_NULL;
|
||||
}
|
||||
|
||||
if (cgi->csck)
|
||||
if (cgi->client_org_on_read)
|
||||
{
|
||||
HIO_ASSERT (hio, cgi->client != HIO_NULL);
|
||||
|
||||
if (cgi->client_org_on_read) cgi->client->sck->on_read = cgi->client_org_on_read;
|
||||
if (cgi->client_org_on_write) cgi->client->sck->on_write = cgi->client_org_on_write;
|
||||
if (cgi->client_org_on_disconnect) cgi->client->sck->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_disconnected)
|
||||
{
|
||||
if (!cgi->keep_alive || hio_dev_sck_read(cgi->client->sck, 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_dev_sck_halt (cgi->client->sck);
|
||||
}
|
||||
}
|
||||
|
||||
cgi->client->sck->on_read = cgi->client_org_on_read;
|
||||
cgi->client_org_on_read = HIO_NULL;
|
||||
cgi->client_org_on_write = HIO_NULL;
|
||||
cgi->client_org_on_disconnect = HIO_NULL;
|
||||
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) - thr(t=%p,c=%p[%d],p=%p) - killed the task\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer);
|
||||
if (cgi->client_org_on_write)
|
||||
{
|
||||
cgi->client->sck->on_write = cgi->client_org_on_write;
|
||||
cgi->client_org_on_write = HIO_NULL;
|
||||
}
|
||||
|
||||
if (cgi->client_org_on_disconnect)
|
||||
{
|
||||
cgi->client->sck->on_disconnect = cgi->client_org_on_disconnect;
|
||||
cgi->client_org_on_disconnect = HIO_NULL;
|
||||
}
|
||||
|
||||
if (cgi->client_htrd_recbs_changed)
|
||||
{
|
||||
/* restore the callbacks */
|
||||
hio_htrd_setrecbs (cgi->client->htrd, &cgi->client_htrd_org_recbs);
|
||||
}
|
||||
|
||||
if (!cgi->client_disconnected)
|
||||
{
|
||||
/*printf ("ENABLING INPUT WATCHING on CLIENT %p. \n", cgi->client->sck);*/
|
||||
if (!cgi->keep_alive || hio_dev_sck_read(cgi->client->sck, 1) <= -1)
|
||||
{
|
||||
HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", cgi->htts, cgi->client->sck);
|
||||
hio_dev_sck_halt (cgi->client->sck);
|
||||
}
|
||||
}
|
||||
|
||||
/*printf ("**** CGI_ON_KILL DONE\n");*/
|
||||
}
|
||||
|
||||
static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
|
||||
static void peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
|
||||
{
|
||||
hio_t* hio = pro->hio;
|
||||
cgi_peer_xtn_t* peer_xtn = hio_dev_pro_getxtn(pro);
|
||||
@ -337,6 +323,7 @@ static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
|
||||
cgi->peer = HIO_NULL; /* clear this peer from the state */
|
||||
|
||||
HIO_ASSERT (hio, peer_xtn->cgi != HIO_NULL);
|
||||
/*printf ("DETACHING FROM CGI PEER DEVICE.....................%p %d\n", peer->cgi, (int)peer->cgi->task_refcnt);*/
|
||||
HIO_SVC_HTTS_TASK_UNREF (peer_xtn->cgi);
|
||||
|
||||
if (cgi->peer_htrd)
|
||||
@ -345,6 +332,7 @@ static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
|
||||
* it's safe to detach the extra information attached on the htrd object. */
|
||||
peer_xtn = hio_htrd_getxtn(cgi->peer_htrd);
|
||||
HIO_ASSERT (hio, peer_xtn->cgi != HIO_NULL);
|
||||
/*printf ("DETACHING FROM CGI PEER HTRD.....................%p %d\n", peer->cgi, (int)peer->cgi->task_refcnt);*/
|
||||
HIO_SVC_HTTS_TASK_UNREF (peer_xtn->cgi);
|
||||
}
|
||||
|
||||
@ -376,7 +364,7 @@ static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
|
||||
}
|
||||
}
|
||||
|
||||
static int cgi_peer_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const void* data, hio_iolen_t dlen)
|
||||
static int peer_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const void* data, hio_iolen_t dlen)
|
||||
{
|
||||
hio_t* hio = pro->hio;
|
||||
cgi_peer_xtn_t* peer = hio_dev_pro_getxtn(pro);
|
||||
@ -394,6 +382,7 @@ static int cgi_peer_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const vo
|
||||
if (dlen == 0)
|
||||
{
|
||||
HIO_DEBUG3 (hio, "HTTS(%p) - EOF from peer %p(pid=%u)\n", cgi->client->htts, pro, (unsigned int)pro->child_pid);
|
||||
cgi->client_eof_detected = 1;
|
||||
|
||||
if (!(cgi->over & CGI_OVER_READ_FROM_PEER))
|
||||
{
|
||||
@ -475,7 +464,7 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req)
|
||||
cgi_t* cgi = peer->cgi;
|
||||
hio_svc_htts_cli_t* cli = cgi->client;
|
||||
hio_bch_t dtbuf[64];
|
||||
int status_code = HIO_HTTP_STATUS_OK;
|
||||
int status_code = 200;
|
||||
|
||||
if (req->attr.content_length)
|
||||
{
|
||||
@ -493,6 +482,7 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req)
|
||||
if (*endptr == '\0' && is_sober && v > 0 && v <= HIO_TYPE_MAX(int)) status_code = v;
|
||||
}
|
||||
|
||||
/*printf ("CGI PEER HTRD PEEK...\n");*/
|
||||
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",
|
||||
@ -528,6 +518,8 @@ static int peer_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req)
|
||||
cgi_peer_xtn_t* peer = hio_htrd_getxtn(htrd);
|
||||
cgi_t* cgi = peer->cgi;
|
||||
|
||||
/*printf (">> PEER RESPONSE COMPLETED\n");*/
|
||||
|
||||
if (cgi_write_last_chunk_to_client(cgi) <= -1) return -1;
|
||||
|
||||
cgi_mark_over (cgi, CGI_OVER_READ_FROM_PEER);
|
||||
@ -598,6 +590,8 @@ static int cgi_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req)
|
||||
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
||||
cgi_t* cgi = (cgi_t*)cli->task;
|
||||
|
||||
/*printf (">> CLIENT REQUEST COMPLETED\n");*/
|
||||
|
||||
/* indicate EOF to the client peer */
|
||||
if (cgi_write_to_peer(cgi, HIO_NULL, 0) <= -1) return -1;
|
||||
|
||||
@ -623,7 +617,7 @@ static hio_htrd_recbs_t cgi_client_htrd_recbs =
|
||||
cgi_client_htrd_push_content
|
||||
};
|
||||
|
||||
static int cgi_peer_on_write (hio_dev_pro_t* pro, hio_iolen_t wrlen, void* wrctx)
|
||||
static int peer_on_write (hio_dev_pro_t* pro, hio_iolen_t wrlen, void* wrctx)
|
||||
{
|
||||
hio_t* hio = pro->hio;
|
||||
cgi_peer_xtn_t* peer = hio_dev_pro_getxtn(pro);
|
||||
@ -677,25 +671,9 @@ oops:
|
||||
static void cgi_client_on_disconnect (hio_dev_sck_t* sck)
|
||||
{
|
||||
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
||||
hio_svc_htts_t* htts = cli->htts;
|
||||
cgi_t* cgi = (cgi_t*)cli->task;
|
||||
hio_t* hio = sck->hio;
|
||||
|
||||
HIO_ASSERT (hio, sck == cgi->csck);
|
||||
HIO_DEBUG4 (hio, "HTTS(%p) - cgi(t=%p,c=%p,csck=%p) - client socket disconnect notified\n", htts, cgi, cli, sck);
|
||||
|
||||
cgi->client_disconnected = 1;
|
||||
cgi->csck = HIO_NULL;
|
||||
cgi->client = HIO_NULL;
|
||||
if (cgi->client_org_on_disconnect)
|
||||
{
|
||||
cgi->client_org_on_disconnect (sck);
|
||||
/* this original callback destroys the associated resource.
|
||||
* cgi must not be accessed from here down */
|
||||
}
|
||||
|
||||
HIO_DEBUG4 (hio, "HTTS(%p) - cgi(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, cgi, cli, sck);
|
||||
/* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */
|
||||
cgi->client_org_on_disconnect (sck);
|
||||
}
|
||||
|
||||
static int cgi_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t len, const hio_skad_t* srcaddr)
|
||||
@ -723,14 +701,11 @@ static int cgi_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
|
||||
{
|
||||
/* 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);
|
||||
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() */
|
||||
{
|
||||
int n;
|
||||
n = cgi_write_to_peer(cgi, HIO_NULL, 0);
|
||||
if (cgi_write_to_peer(cgi, HIO_NULL, 0) <= -1) goto oops;
|
||||
cgi_mark_over (cgi, CGI_OVER_READ_FROM_CLIENT);
|
||||
if (n <= -1) goto oops;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -851,7 +826,7 @@ static int peer_capture_request_header (hio_htre_t* req, const hio_bch_t* key, c
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cgi_peer_on_fork (hio_dev_pro_t* pro, void* fork_ctx)
|
||||
static int peer_on_fork (hio_dev_pro_t* pro, void* fork_ctx)
|
||||
{
|
||||
hio_t* hio = pro->hio; /* in this callback, the pro device is not fully up. however, the hio field is guaranteed to be available */
|
||||
peer_fork_ctx_t* fc = (peer_fork_ctx_t*)fork_ctx;
|
||||
@ -973,21 +948,19 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
|
||||
HIO_MEMSET (&mi, 0, HIO_SIZEOF(mi));
|
||||
mi.flags = HIO_DEV_PRO_READOUT | HIO_DEV_PRO_ERRTONUL | HIO_DEV_PRO_WRITEIN /*| HIO_DEV_PRO_FORGET_CHILD*/;
|
||||
mi.cmd = fc.actual_script;
|
||||
mi.on_read = cgi_peer_on_read;
|
||||
mi.on_write = cgi_peer_on_write;
|
||||
mi.on_close = cgi_peer_on_close;
|
||||
mi.on_fork = cgi_peer_on_fork;
|
||||
mi.on_read = peer_on_read;
|
||||
mi.on_write = peer_on_write;
|
||||
mi.on_close = peer_on_close;
|
||||
mi.on_fork = peer_on_fork;
|
||||
mi.fork_ctx = &fc;
|
||||
|
||||
cgi = (cgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*cgi), cgi_on_kill);
|
||||
if (HIO_UNLIKELY(!cgi)) goto oops;
|
||||
|
||||
cgi->options = options;
|
||||
cgi->csck = csck;
|
||||
cgi->client = cli;
|
||||
/*cgi->num_pending_writes_to_client = 0;
|
||||
cgi->num_pending_writes_to_peer = 0;*/
|
||||
cgi->req_method = hio_htre_getqmethodtype(req);
|
||||
cgi->req_version = *hio_htre_getversion(req);
|
||||
cgi->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &cgi->req_content_length);
|
||||
|
||||
@ -1118,8 +1091,6 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
|
||||
/* TODO: store current input watching state and use it when destroying the cgi data */
|
||||
if (hio_dev_sck_read(csck, !(cgi->over & CGI_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
|
||||
hio_freemem (hio, fc.actual_script);
|
||||
|
||||
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, cgi);
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
|
165
lib/http-file.c
165
lib/http-file.c
@ -67,7 +67,6 @@ struct file_t
|
||||
hio_tmridx_t peer_tmridx;
|
||||
hio_bch_t peer_etag[128];
|
||||
|
||||
hio_dev_sck_t* csck;
|
||||
hio_svc_htts_cli_t* client;
|
||||
hio_http_version_t req_version; /* client request */
|
||||
hio_http_method_t req_method;
|
||||
@ -97,28 +96,25 @@ static int file_send_contents_to_client (file_t* file);
|
||||
|
||||
static void file_halt_participating_devices (file_t* file)
|
||||
{
|
||||
HIO_ASSERT (file->htts->hio, file->client != HIO_NULL);
|
||||
HIO_ASSERT (file->htts->hio, file->client->sck != HIO_NULL);
|
||||
HIO_ASSERT (file->client->htts->hio, file->client != HIO_NULL);
|
||||
HIO_ASSERT (file->client->htts->hio, file->client->sck != HIO_NULL);
|
||||
|
||||
HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) Halting participating devices\n", file->htts, (int)file->csck->hnd, (int)file->peer);
|
||||
HIO_DEBUG3 (file->client->htts->hio, "HTTS(%p) - file(c=%d,p=%d) Halting participating devices\n", file->client->htts, (int)file->client->sck->hnd, (int)file->peer);
|
||||
|
||||
/* only the client socket device.
|
||||
* the peer side is just a file descriptor - no hio-managed device */
|
||||
if (file->csck) hio_dev_sck_halt (file->csck);
|
||||
hio_dev_sck_halt (file->client->sck);
|
||||
}
|
||||
|
||||
static int file_write_to_client (file_t* file, const void* data, hio_iolen_t dlen)
|
||||
{
|
||||
if (file->csck)
|
||||
{
|
||||
file->ever_attempted_to_write_to_client = 1;
|
||||
file->ever_attempted_to_write_to_client = 1;
|
||||
|
||||
file->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_write(file->csck, data, dlen, HIO_NULL, HIO_NULL) <= -1) /* TODO: use sendfile here.. */
|
||||
{
|
||||
file->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
file->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_write(file->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) /* TODO: use sendfile here.. */
|
||||
{
|
||||
file->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -126,16 +122,13 @@ static int file_write_to_client (file_t* file, const void* data, hio_iolen_t dle
|
||||
|
||||
static int file_sendfile_to_client (file_t* file, hio_foff_t foff, hio_iolen_t len)
|
||||
{
|
||||
if (file->csck)
|
||||
{
|
||||
file->ever_attempted_to_write_to_client = 1;
|
||||
file->ever_attempted_to_write_to_client = 1;
|
||||
|
||||
file->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_sendfile(file->csck, file->peer, foff, len, HIO_NULL) <= -1)
|
||||
{
|
||||
file->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
file->num_pending_writes_to_client++;
|
||||
if (hio_dev_sck_sendfile(file->client->sck, file->peer, foff, len, HIO_NULL) <= -1)
|
||||
{
|
||||
file->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -148,7 +141,7 @@ static int file_send_final_status_to_client (file_t* file, int status_code, int
|
||||
const hio_bch_t* status_msg;
|
||||
|
||||
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
|
||||
status_msg = hio_http_status_to_bcstr(status_code);
|
||||
status_msg = hio_http_status_to_bcstr(status_code);
|
||||
|
||||
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",
|
||||
@ -197,14 +190,14 @@ static void file_mark_over (file_t* file, int over_bits)
|
||||
old_over = file->over;
|
||||
file->over |= over_bits;
|
||||
|
||||
HIO_DEBUG6 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) updating mark - old_over=%x | new-bits=%x => over=%x\n", file->htts, (int)file->csck->hnd, file->peer, (int)old_over, (int)over_bits, (int)file->over);
|
||||
HIO_DEBUG6 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) updating mark - old_over=%x | new-bits=%x => over=%x\n", file->htts, (int)file->client->sck->hnd, file->peer, (int)old_over, (int)over_bits, (int)file->over);
|
||||
|
||||
if (!(old_over & FILE_OVER_READ_FROM_CLIENT) && (file->over & FILE_OVER_READ_FROM_CLIENT))
|
||||
{
|
||||
if (file->csck && hio_dev_sck_read(file->csck, 0) <= -1)
|
||||
if (hio_dev_sck_read(file->client->sck, 0) <= -1)
|
||||
{
|
||||
HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting client for failure to disable input watching\n", file->htts, (int)file->csck->hnd, file->peer);
|
||||
hio_dev_sck_halt (file->csck);
|
||||
HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting client for failure to disable input watching\n", file->htts, (int)file->client->sck->hnd, file->peer);
|
||||
hio_dev_sck_halt (file->client->sck);
|
||||
}
|
||||
}
|
||||
|
||||
@ -218,34 +211,31 @@ static void file_mark_over (file_t* file, int over_bits)
|
||||
if (old_over != FILE_OVER_ALL && file->over == FILE_OVER_ALL)
|
||||
{
|
||||
/* ready to stop */
|
||||
HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting peer as it is unneeded\n", file->htts, (int)file->csck->hnd, file->peer);
|
||||
HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting peer as it is unneeded\n", file->htts, (int)file->client->sck->hnd, file->peer);
|
||||
file_close_peer (file);
|
||||
|
||||
if (HIO_LIKELY(file->csck))
|
||||
if (file->keep_alive && !file->client_eof_detected)
|
||||
{
|
||||
if (file->keep_alive && !file->client_eof_detected)
|
||||
{
|
||||
#if defined(TCP_CORK)
|
||||
int tcp_cork = 0;
|
||||
#if defined(SOL_TCP)
|
||||
hio_dev_sck_setsockopt(file->csck, SOL_TCP, TCP_CORK, &tcp_cork, HIO_SIZEOF(tcp_cork));
|
||||
#elif defined(IPPROTO_TCP)
|
||||
hio_dev_sck_setsockopt(file->csck, IPPROTO_TCP, TCP_CORK, &tcp_cork, HIO_SIZEOF(tcp_cork));
|
||||
#endif
|
||||
#if defined(TCP_CORK)
|
||||
int tcp_cork = 0;
|
||||
#if defined(SOL_TCP)
|
||||
hio_dev_sck_setsockopt(file->client->sck, SOL_TCP, TCP_CORK, &tcp_cork, HIO_SIZEOF(tcp_cork));
|
||||
#elif defined(IPPROTO_TCP)
|
||||
hio_dev_sck_setsockopt(file->client->sck, IPPROTO_TCP, TCP_CORK, &tcp_cork, HIO_SIZEOF(tcp_cork));
|
||||
#endif
|
||||
#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);
|
||||
/* the file task must not be accessed from here down as it could have been destroyed */
|
||||
}
|
||||
else
|
||||
{
|
||||
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 */
|
||||
}
|
||||
/* 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);
|
||||
/* the file task must not be accessed from here down as it could have been destroyed */
|
||||
}
|
||||
else
|
||||
{
|
||||
HIO_DEBUG4 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting client for %hs\n", file->htts, (int)file->client->sck->hnd, file->peer, (file->client_eof_detected? "EOF detected": "no keep-alive"));
|
||||
hio_dev_sck_shutdown (file->client->sck, HIO_DEV_SCK_SHUTDOWN_WRITE);
|
||||
hio_dev_sck_halt (file->client->sck);
|
||||
/* the file task will be detached from file->client->task by the upstream disconnect handler in http_svr.c */
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -284,7 +274,7 @@ 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_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) on_kill\n", file->htts, (int)file->client->sck->hnd, file->peer);
|
||||
|
||||
file_close_peer (file);
|
||||
|
||||
@ -294,27 +284,38 @@ static void file_on_kill (hio_svc_htts_task_t* task)
|
||||
file->req_qpath = HIO_NULL;
|
||||
}
|
||||
|
||||
if (file->csck)
|
||||
if (file->client_org_on_read)
|
||||
{
|
||||
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->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_dev_sck_halt (file->csck);
|
||||
}
|
||||
file->client->sck->on_read = file->client_org_on_read;
|
||||
file->client_org_on_read = HIO_NULL;
|
||||
}
|
||||
|
||||
file->client_org_on_read = HIO_NULL;
|
||||
file->client_org_on_write = HIO_NULL;
|
||||
file->client_org_on_disconnect = HIO_NULL;
|
||||
file->client_htrd_recbs_changed = 0;
|
||||
if (file->client_org_on_write)
|
||||
{
|
||||
file->client->sck->on_write = file->client_org_on_write;
|
||||
file->client_org_on_write = HIO_NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
if (file->client_org_on_disconnect)
|
||||
{
|
||||
file->client->sck->on_disconnect = file->client_org_on_disconnect;
|
||||
file->client_org_on_disconnect = HIO_NULL;
|
||||
}
|
||||
|
||||
if (file->client_htrd_recbs_changed)
|
||||
{
|
||||
/* restore the callbacks */
|
||||
hio_htrd_setrecbs (file->client->htrd, &file->client_htrd_org_recbs);
|
||||
}
|
||||
|
||||
if (!file->client_disconnected)
|
||||
{
|
||||
if (!file->keep_alive || hio_dev_sck_read(file->client->sck, 1) <= -1)
|
||||
{
|
||||
HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) halting client for failure to enable input watching\n", file->htts, (int)file->client->sck->hnd, file->peer);
|
||||
hio_dev_sck_halt (file->client->sck);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void file_client_on_disconnect (hio_dev_sck_t* sck)
|
||||
@ -322,26 +323,18 @@ static void file_client_on_disconnect (hio_dev_sck_t* sck)
|
||||
hio_t* hio = sck->hio;
|
||||
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
||||
file_t* file = (file_t*)cli->task;
|
||||
hio_svc_htts_t* htts = file->htts;
|
||||
|
||||
HIO_ASSERT (hio, sck == cli->sck);
|
||||
HIO_ASSERT (hio, sck == file->client->sck);
|
||||
HIO_ASSERT (hio, sck == file->csck);
|
||||
|
||||
HIO_DEBUG4 (hio, "HTTS(%p) - file(t=%p,c=%p,csck=%p) - client socket disconnect notified\n", htts, file, sck, cli);
|
||||
HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) client on_disconnect\n", file->client->htts, (int)sck->hnd, file->peer);
|
||||
|
||||
file->client_disconnected = 1;
|
||||
file->csck = HIO_NULL;
|
||||
file->client = HIO_NULL;
|
||||
if (file->client_org_on_disconnect)
|
||||
{
|
||||
file->client_org_on_disconnect (sck);
|
||||
/* this original callback(listener_on_disconnect in http-svr.c) destroys the associated resource.
|
||||
* the `file` task object must not be accessed from here down */
|
||||
}
|
||||
file->client_org_on_disconnect (sck);
|
||||
|
||||
HIO_DEBUG4 (hio, "HTTS(%p) - file(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, file, sck, cli);
|
||||
/* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */
|
||||
/* the original disconnect handler (listener_on_disconnect in http-svr.c)
|
||||
* frees the file task attached to the client. so it must not be accessed */
|
||||
HIO_ASSERT (hio, cli->task == HIO_NULL);
|
||||
}
|
||||
|
||||
static int file_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t len, const hio_skad_t* srcaddr)
|
||||
@ -352,7 +345,6 @@ static int file_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t
|
||||
|
||||
HIO_ASSERT (hio, sck == cli->sck);
|
||||
HIO_ASSERT (hio, sck == file->client->sck);
|
||||
HIO_ASSERT (hio, sck == file->csck);
|
||||
|
||||
if (len <= -1)
|
||||
{
|
||||
@ -412,7 +404,6 @@ static int file_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wr
|
||||
|
||||
HIO_ASSERT (hio, sck == cli->sck);
|
||||
HIO_ASSERT (hio, sck == file->client->sck);
|
||||
HIO_ASSERT (hio, sck == file->csck);
|
||||
|
||||
if (wrlen <= -1)
|
||||
{
|
||||
@ -797,7 +788,6 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t*
|
||||
|
||||
file->options = options;
|
||||
file->cbs = cbs; /* the given pointer must outlive the lifespan of the while file handling cycle. */
|
||||
file->csck = csck;
|
||||
file->client = cli;
|
||||
file->sendfile_ok = hio_dev_sck_sendfileok(cli->sck);
|
||||
/*file->num_pending_writes_to_client = 0;
|
||||
@ -823,6 +813,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t*
|
||||
HIO_ASSERT (hio, cli->task == HIO_NULL); /* you must not call this function while cli->task is not HIO_NULL */
|
||||
HIO_SVC_HTTS_TASK_REF ((hio_svc_htts_task_t*)file, cli->task); /* cli->task = file with ref-count up */
|
||||
|
||||
|
||||
#if !defined(FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH)
|
||||
if (file->req_content_length_unlimited)
|
||||
{
|
||||
@ -932,7 +923,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t*
|
||||
#if defined(HAVE_POSIX_FADVISE)
|
||||
posix_fadvise (file->peer, file->start_offset, file->end_offset - file->start_offset + 1, POSIX_FADV_SEQUENTIAL);
|
||||
#endif
|
||||
set_tcp_cork (file->csck);
|
||||
set_tcp_cork (file->client->sck);
|
||||
|
||||
if (file_send_header_to_client(file, HIO_HTTP_STATUS_OK, 0, actual_mime_type) <= -1) goto oops;
|
||||
if (file_send_contents_to_client(file) <= -1) goto oops;
|
||||
@ -991,8 +982,6 @@ int hio_svc_htts_dofile (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 file data */
|
||||
if (hio_dev_sck_read(csck, !(file->over & FILE_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
|
||||
hio_freemem (hio, actual_file);
|
||||
|
||||
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, file);
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
|
@ -69,7 +69,6 @@ struct thr_task_t
|
||||
hio_dev_sck_t* csck;
|
||||
hio_svc_htts_cli_t* client;
|
||||
hio_http_version_t req_version; /* client request */
|
||||
hio_http_method_t req_method;
|
||||
|
||||
unsigned int over: 4; /* must be large enough to accomodate THR_TASK_OVER_ALL */
|
||||
unsigned int keep_alive: 1;
|
||||
@ -153,22 +152,16 @@ 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_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
|
||||
status_msg = hio_http_status_to_bcstr(status_code);
|
||||
|
||||
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: %s\r\nConnection: %hs\r\nContent-Length: 0\r\n\r\n",
|
||||
thr_task->req_version.major, thr_task->req_version.minor,
|
||||
status_code, status_msg,
|
||||
status_code, hio_http_status_to_bcstr(status_code),
|
||||
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 (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;
|
||||
|
||||
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;
|
||||
}
|
||||
@ -182,7 +175,7 @@ static int thr_task_write_last_chunk_to_client (thr_task_t* thr_task)
|
||||
else
|
||||
{
|
||||
if (thr_task->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_task, "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;
|
||||
@ -191,20 +184,17 @@ static int thr_task_write_last_chunk_to_client (thr_task_t* thr_task)
|
||||
|
||||
static int thr_task_write_to_peer (thr_task_t* thr_task, const void* data, hio_iolen_t dlen)
|
||||
{
|
||||
if (thr_task->peer)
|
||||
thr_task->num_pending_writes_to_peer++;
|
||||
if (hio_dev_thr_write(thr_task->peer, data, dlen, HIO_NULL) <= -1)
|
||||
{
|
||||
thr_task->num_pending_writes_to_peer++;
|
||||
if (hio_dev_thr_write(thr_task->peer, data, dlen, HIO_NULL) <= -1)
|
||||
{
|
||||
thr_task->num_pending_writes_to_peer--;
|
||||
return -1;
|
||||
}
|
||||
thr_task->num_pending_writes_to_peer--;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TODO: check if it's already finished or something.. */
|
||||
if (thr_task->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_task->num_pending_writes_to_peer > THR_TASK_PENDING_IO_THRESHOLD)
|
||||
{
|
||||
if (hio_dev_sck_read(thr_task->csck, 0) <= -1) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -311,7 +301,7 @@ static void thr_task_on_kill (hio_svc_htts_task_t* task)
|
||||
|
||||
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_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", thr_task->htts, thr_task->csck);
|
||||
hio_dev_sck_halt (thr_task->csck);
|
||||
}
|
||||
}
|
||||
@ -321,7 +311,7 @@ static void thr_task_on_kill (hio_svc_htts_task_t* task)
|
||||
thr_task->client_org_on_disconnect = HIO_NULL;
|
||||
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_SVC_HTTS_TASKL_UNLINK_TASK (thr_task); /* detach from the htts service */
|
||||
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);
|
||||
}
|
||||
|
||||
@ -397,13 +387,11 @@ static int thr_peer_on_read (hio_dev_thr_t* thr, const void* data, hio_iolen_t d
|
||||
|
||||
if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER))
|
||||
{
|
||||
int n;
|
||||
/* the thr script could be misbehaviing.
|
||||
* it still has to read more but EOF is read.
|
||||
* otherwise client_peer_htrd_poke() should have been called */
|
||||
n = thr_task_write_last_chunk_to_client(thr_task);
|
||||
if (thr_task_write_last_chunk_to_client(thr_task) <= -1) goto oops;
|
||||
thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER);
|
||||
if (n <= -1) goto oops;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -562,19 +550,28 @@ 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_len = 2;
|
||||
|
||||
if (thr_task_writev_to_client(thr_task, iov, HIO_COUNTOF(iov)) <= -1) goto oops;
|
||||
if (thr_task_writev_to_client(thr_task, iov, HIO_COUNTOF(iov)) <= -1)
|
||||
{
|
||||
goto oops;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case THR_TASK_RES_MODE_CLOSE:
|
||||
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_task, data, dlen) <= -1)
|
||||
{
|
||||
goto oops;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (thr_task->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_task->peer, 0) <= -1)
|
||||
{
|
||||
goto oops;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -682,7 +679,7 @@ static void thr_client_on_disconnect (hio_dev_sck_t* sck)
|
||||
hio_t* hio = sck->hio;
|
||||
|
||||
HIO_ASSERT (hio, sck = thr_task->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_task, sck, cli);
|
||||
|
||||
thr_task->client_disconnected = 1;
|
||||
thr_task->csck = HIO_NULL;
|
||||
@ -694,7 +691,7 @@ static void thr_client_on_disconnect (hio_dev_sck_t* sck)
|
||||
* 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_task, sck, cli);
|
||||
/* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */
|
||||
}
|
||||
|
||||
@ -923,7 +920,6 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
|
||||
|
||||
/*thr_task->num_pending_writes_to_client = 0;
|
||||
thr_task->num_pending_writes_to_peer = 0;*/
|
||||
thr_task->req_method = hio_htre_getqmethodtype(req);
|
||||
thr_task->req_version = *hio_htre_getversion(req);
|
||||
thr_task->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &thr_task->req_content_length);
|
||||
|
||||
@ -1048,7 +1044,7 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r
|
||||
/* 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;
|
||||
|
||||
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, thr_task);
|
||||
HIO_SVC_HTTS_TASKL_APPEND_TASK(&htts->task, thr_task);
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
|
Loading…
Reference in New Issue
Block a user