shorted code a bit by creating a new function hio_svc_htts_task_buildfinalres()

This commit is contained in:
2023-03-10 01:27:08 +09:00
parent bafad8ceab
commit 933d03fe27
8 changed files with 160 additions and 318 deletions

View File

@@ -35,7 +35,6 @@ struct fcgi_t
hio_htrd_t* peer_htrd;
unsigned int over: 4; /* must be large enough to accomodate FCGI_OVER_ALL */
unsigned int keep_alive: 1;
unsigned int req_content_length_unlimited: 1;
unsigned int ever_attempted_to_write_to_client: 1;
unsigned int last_chunk_sent: 1;
@@ -130,32 +129,8 @@ static int fcgi_writev_to_client (fcgi_t* fcgi, hio_iovec_t* iov, hio_iolen_t io
static int fcgi_send_final_status_to_client (fcgi_t* fcgi, int status_code, int force_close)
{
hio_svc_htts_cli_t* cli = fcgi->task_client;
hio_bch_t dtbuf[64];
const hio_bch_t* status_msg;
hio_oow_t content_len;
if (!cli) return 0; /* client unbound or no binding client */
hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf));
status_msg = hio_http_status_to_bcstr(status_code);
content_len = hio_count_bcstr(status_msg);
if (!force_close) force_close = !fcgi->keep_alive;
if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\nConnection: %hs\r\n",
fcgi->task_req_version.major, fcgi->task_req_version.minor,
status_code, status_msg,
cli->htts->server_name, dtbuf,
(force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1;
if (fcgi->task_req_method == HIO_HTTP_HEAD)
{
if (status_code != HIO_HTTP_STATUS_OK) content_len = 0;
status_msg = "";
}
if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", content_len, status_msg) == (hio_oow_t)-1) return -1;
fcgi->task_status_code = status_code;
if (!cli) return 0; /* client disconnected probably */
if (hio_svc_htts_task_buildfinalres(fcgi, status_code, HIO_NULL, HIO_NULL, force_close) <= -1) return -1;
return (fcgi_write_to_client(fcgi, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 ||
(force_close && fcgi_write_to_client(fcgi, HIO_NULL, 0) <= -1))? -1: 0;
}
@@ -176,7 +151,7 @@ static int fcgi_write_last_chunk_to_client (fcgi_t* fcgi)
fcgi_write_to_client(fcgi, "0\r\n\r\n", 5) <= -1) return -1;
}
if (!fcgi->keep_alive && fcgi_write_to_client(fcgi, HIO_NULL, 0) <= -1) return -1;
if (!fcgi->task_keep_client_alive && fcgi_write_to_client(fcgi, HIO_NULL, 0) <= -1) return -1;
}
return 0;
}
@@ -246,7 +221,7 @@ static HIO_INLINE void fcgi_mark_over (fcgi_t* fcgi, int over_bits)
if (fcgi->task_csck)
{
HIO_DEBUG2 (hio, "HTTS(%p) - ALL OVER keeping client(%p) alive\n", fcgi->htts, fcgi->task_csck);
if (fcgi->keep_alive && !fcgi->client_eof_detected)
if (fcgi->task_keep_client_alive && !fcgi->client_eof_detected)
{
HIO_DEBUG2 (hio, "HTTS(%p) - keeping client(%p) alive\n", fcgi->htts, fcgi->task_csck);
HIO_ASSERT (fcgi->htts->hio, fcgi->task_client->task == (hio_svc_htts_task_t*)fcgi);
@@ -464,7 +439,7 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req)
break;
case FCGI_RES_MODE_LENGTH:
if (hio_becs_cat(cli->sbuf, (fcgi->keep_alive? "Connection: keep-alive\r\n": "Connection: close\r\n")) == (hio_oow_t)-1) return -1;
if (hio_becs_cat(cli->sbuf, (fcgi->task_keep_client_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;
@@ -894,7 +869,7 @@ HIO_DEBUG2 (csck->hio, "UNBINDING CLEINT FROM TASK... client=%p csck=%p\n", fcgi
fcgi->task_client = HIO_NULL;
fcgi->task_csck = HIO_NULL;
if (!fcgi->client_disconnected && (!fcgi->keep_alive || hio_dev_sck_read(csck, 1) <= -1))
if (!fcgi->client_disconnected && (!fcgi->task_keep_client_alive || hio_dev_sck_read(csck, 1) <= -1))
{
HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", fcgi->htts, csck);
hio_dev_sck_halt (csck);
@@ -1046,18 +1021,8 @@ static int setup_for_content_length(fcgi_t* fcgi, hio_htre_t* req)
}
#endif
/* this may change later if Content-Length is included in the fcgi output */
if (req->flags & HIO_HTRE_ATTR_KEEPALIVE)
{
fcgi->keep_alive = 1;
fcgi->res_mode_to_cli = FCGI_RES_MODE_CHUNKED;
/* the mode still can get switched to FCGI_RES_MODE_LENGTH if the fcgi script emits Content-Length */
}
else
{
fcgi->keep_alive = 0;
fcgi->res_mode_to_cli = FCGI_RES_MODE_CLOSE;
}
fcgi->res_mode_to_cli = fcgi->task_keep_client_alive? FCGI_RES_MODE_CHUNKED: FCGI_RES_MODE_CLOSE;
/* the mode still can get switched from FCGI_RES_MODE_CHUNKED to FCGI_RES_MODE_LENGTH if the fcgi script emits Content-Length */
return 0;
}