cleaned up code a bit
This commit is contained in:
parent
2a561ad46b
commit
330135bb26
File diff suppressed because it is too large
Load Diff
86
mio/lib/http-fcgi.c
Normal file
86
mio/lib/http-fcgi.c
Normal file
@ -0,0 +1,86 @@
|
||||
|
||||
#define FCGI_PADDING_SIZE 255
|
||||
#define FCGI_RECORD_SIZE \
|
||||
(sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
|
||||
|
||||
#define FCGI_BEGIN_REQUEST 1
|
||||
#define FCGI_ABORT_REQUEST 2
|
||||
#define FCGI_END_REQUEST 3
|
||||
#define FCGI_PARAMS 4
|
||||
#define FCGI_STDIN 5
|
||||
#define FCGI_STDOUT 6
|
||||
#define FCGI_STDERR 7
|
||||
#define FCGI_DATA 8
|
||||
#define FCGI_GET_VALUES 9
|
||||
#define FCGI_GET_VALUES_RESULT 10
|
||||
#define FCGI_UNKNOWN_TYPE 11
|
||||
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
|
||||
|
||||
/* role in fcgi_begin_request_body */
|
||||
#define FCGI_RESPONDER 1
|
||||
#define FCGI_AUTHORIZER 2
|
||||
#define FCGI_FILTER 3
|
||||
|
||||
/* flag in fcgi_begin_request_body */
|
||||
#define FCGI_KEEP_CONN 1
|
||||
|
||||
/* proto in fcgi_end_request_body */
|
||||
#define FCGI_REQUEST_COMPLETE 0
|
||||
#define FCGI_CANT_MPX_CONN 1
|
||||
#define FCGI_OVERLOADED 2
|
||||
#define FCGI_UNKNOWN_ROLE 3
|
||||
|
||||
#include "mio-pac1.h"
|
||||
struct fcgi_record_header
|
||||
{
|
||||
mio_uint8_t version;
|
||||
mio_uint8_t type;
|
||||
mio_uint16_t id;
|
||||
mio_uint16_t content_len;
|
||||
mio_uint8_t padding_len;
|
||||
mio_uint8_t reserved;
|
||||
};
|
||||
|
||||
struct fcgi_begin_request_body
|
||||
{
|
||||
mio_uint16_t role;
|
||||
mio_uint8_t flags;
|
||||
mio_uint8_t reserved[5];
|
||||
};
|
||||
|
||||
struct fcgi_end_request_body
|
||||
{
|
||||
mio_uint32_t app_status;
|
||||
mio_uint8_t proto_status;
|
||||
mio_uint8_t reserved[3];
|
||||
};
|
||||
#include "mio-upac.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int begin_request ()
|
||||
{
|
||||
struct fcgi_record_header* h;
|
||||
struct fcgi_begin_request_body* br;
|
||||
|
||||
h->version = 1;
|
||||
h->type = FCGI_BEGIN_REQUEST;
|
||||
h->id = MIO_CONST_HTON16(1);
|
||||
h->content_len = MIO_HTON16(MIO_SIZEOF(struct fcgi_begin_request_body));
|
||||
h->padding_len = 0;
|
||||
|
||||
br->role = MIO_CONST_HTON16(FCGI_RESPONDER);
|
||||
br->flags = 0;
|
||||
|
||||
|
||||
h->type = FCGI_PARAMS;
|
||||
h->content_len = 0;
|
||||
|
||||
|
||||
/*
|
||||
h->type = FCGI_STDIN;
|
||||
*/
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -27,11 +27,11 @@
|
||||
#include <mio-fmt.h>
|
||||
#include <mio-chr.h>
|
||||
|
||||
#define TXT_STATE_OVER_READ_FROM_CLIENT (1 << 0)
|
||||
#define TXT_STATE_OVER_WRITE_TO_CLIENT (1 << 1)
|
||||
#define TXT_STATE_OVER_ALL (TXT_STATE_OVER_READ_FROM_CLIENT | TXT_STATE_OVER_WRITE_TO_CLIENT)
|
||||
#define TXT_OVER_READ_FROM_CLIENT (1 << 0)
|
||||
#define TXT_OVER_WRITE_TO_CLIENT (1 << 1)
|
||||
#define TXT_OVER_ALL (TXT_OVER_READ_FROM_CLIENT | TXT_OVER_WRITE_TO_CLIENT)
|
||||
|
||||
struct txt_state_t
|
||||
struct txt_t
|
||||
{
|
||||
MIO_SVC_HTTS_RSRC_HEADER;
|
||||
|
||||
@ -39,7 +39,7 @@ struct txt_state_t
|
||||
mio_svc_htts_cli_t* client;
|
||||
mio_http_version_t req_version; /* client request */
|
||||
|
||||
unsigned int over: 2; /* must be large enough to accomodate TXT_STATE_OVER_ALL */
|
||||
unsigned int over: 2; /* must be large enough to accomodate TXT_OVER_ALL */
|
||||
unsigned int keep_alive: 1;
|
||||
unsigned int req_content_length_unlimited: 1;
|
||||
unsigned int client_disconnected: 1;
|
||||
@ -51,52 +51,52 @@ struct txt_state_t
|
||||
mio_dev_sck_on_disconnect_t client_org_on_disconnect;
|
||||
mio_htrd_recbs_t client_htrd_org_recbs;
|
||||
};
|
||||
typedef struct txt_state_t txt_state_t;
|
||||
typedef struct txt_t txt_t;
|
||||
|
||||
static void txt_state_halt_participating_devices (txt_state_t* txt_state)
|
||||
static void txt_halt_participating_devices (txt_t* txt)
|
||||
{
|
||||
MIO_ASSERT (txt_state->client->htts->mio, txt_state->client != MIO_NULL);
|
||||
MIO_ASSERT (txt_state->client->htts->mio, txt_state->client->sck != MIO_NULL);
|
||||
MIO_DEBUG3 (txt_state->client->htts->mio, "HTTS(%p) - Halting participating devices in txt state %p(client=%p)\n", txt_state->client->htts, txt_state, txt_state->client->sck);
|
||||
mio_dev_sck_halt (txt_state->client->sck);
|
||||
MIO_ASSERT (txt->client->htts->mio, txt->client != MIO_NULL);
|
||||
MIO_ASSERT (txt->client->htts->mio, txt->client->sck != MIO_NULL);
|
||||
MIO_DEBUG3 (txt->client->htts->mio, "HTTS(%p) - Halting participating devices in txt state %p(client=%p)\n", txt->client->htts, txt, txt->client->sck);
|
||||
mio_dev_sck_halt (txt->client->sck);
|
||||
}
|
||||
|
||||
static int txt_state_write_to_client (txt_state_t* txt_state, const void* data, mio_iolen_t dlen)
|
||||
static int txt_write_to_client (txt_t* txt, const void* data, mio_iolen_t dlen)
|
||||
{
|
||||
txt_state->num_pending_writes_to_client++;
|
||||
if (mio_dev_sck_write(txt_state->client->sck, data, dlen, MIO_NULL, MIO_NULL) <= -1)
|
||||
txt->num_pending_writes_to_client++;
|
||||
if (mio_dev_sck_write(txt->client->sck, data, dlen, MIO_NULL, MIO_NULL) <= -1)
|
||||
{
|
||||
txt_state->num_pending_writes_to_client--;
|
||||
txt->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static int txt_state_writev_to_client (txt_state_t* txt_state, mio_iovec_t* iov, mio_iolen_t iovcnt)
|
||||
static int txt_writev_to_client (txt_t* txt, mio_iovec_t* iov, mio_iolen_t iovcnt)
|
||||
{
|
||||
txt_state->num_pending_writes_to_client++;
|
||||
if (mio_dev_sck_writev(txt_state->client->sck, iov, iovcnt, MIO_NULL, MIO_NULL) <= -1)
|
||||
txt->num_pending_writes_to_client++;
|
||||
if (mio_dev_sck_writev(txt->client->sck, iov, iovcnt, MIO_NULL, MIO_NULL) <= -1)
|
||||
{
|
||||
txt_state->num_pending_writes_to_client--;
|
||||
txt->num_pending_writes_to_client--;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int txt_state_send_final_status_to_client (txt_state_t* txt_state, int status_code, const char* content_type, const char* content_text, int force_close)
|
||||
static int txt_send_final_status_to_client (txt_t* txt, int status_code, const char* content_type, const char* content_text, int force_close)
|
||||
{
|
||||
mio_svc_htts_cli_t* cli = txt_state->client;
|
||||
mio_svc_htts_cli_t* cli = txt->client;
|
||||
mio_bch_t dtbuf[64];
|
||||
mio_oow_t content_text_len = 0;
|
||||
|
||||
mio_svc_htts_fmtgmtime (cli->htts, MIO_NULL, dtbuf, MIO_COUNTOF(dtbuf));
|
||||
|
||||
if (!force_close) force_close = !txt_state->keep_alive;
|
||||
if (!force_close) force_close = !txt->keep_alive;
|
||||
|
||||
if (mio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\n",
|
||||
txt_state->req_version.major, txt_state->req_version.minor,
|
||||
txt->req_version.major, txt->req_version.minor,
|
||||
status_code, mio_http_status_to_bcstr(status_code),
|
||||
cli->htts->server_name, dtbuf,
|
||||
(force_close? "close": "keep-alive"),
|
||||
@ -109,91 +109,91 @@ static int txt_state_send_final_status_to_client (txt_state_t* txt_state, int st
|
||||
}
|
||||
if (mio_becs_fcat(cli->sbuf, "Content-Length: %zu\r\n\r\n", content_text_len) == (mio_oow_t)-1) return -1;
|
||||
|
||||
return (txt_state_write_to_client(txt_state, MIO_BECS_PTR(cli->sbuf), MIO_BECS_LEN(cli->sbuf)) <= -1 ||
|
||||
(content_text && txt_state_write_to_client(txt_state, content_text, content_text_len) <= -1) ||
|
||||
(force_close && txt_state_write_to_client(txt_state, MIO_NULL, 0) <= -1))? -1: 0;
|
||||
return (txt_write_to_client(txt, MIO_BECS_PTR(cli->sbuf), MIO_BECS_LEN(cli->sbuf)) <= -1 ||
|
||||
(content_text && txt_write_to_client(txt, content_text, content_text_len) <= -1) ||
|
||||
(force_close && txt_write_to_client(txt, MIO_NULL, 0) <= -1))? -1: 0;
|
||||
}
|
||||
|
||||
static MIO_INLINE void txt_state_mark_over (txt_state_t* txt_state, int over_bits)
|
||||
static MIO_INLINE void txt_mark_over (txt_t* txt, int over_bits)
|
||||
{
|
||||
unsigned int old_over;
|
||||
|
||||
old_over = txt_state->over;
|
||||
txt_state->over |= over_bits;
|
||||
old_over = txt->over;
|
||||
txt->over |= over_bits;
|
||||
|
||||
MIO_DEBUG4 (txt_state->htts->mio, "HTTS(%p) - client=%p new-bits=%x over=%x\n", txt_state->htts, txt_state->client->sck, (int)over_bits, (int)txt_state->over);
|
||||
MIO_DEBUG4 (txt->htts->mio, "HTTS(%p) - client=%p new-bits=%x over=%x\n", txt->htts, txt->client->sck, (int)over_bits, (int)txt->over);
|
||||
|
||||
if (!(old_over & TXT_STATE_OVER_READ_FROM_CLIENT) && (txt_state->over & TXT_STATE_OVER_READ_FROM_CLIENT))
|
||||
if (!(old_over & TXT_OVER_READ_FROM_CLIENT) && (txt->over & TXT_OVER_READ_FROM_CLIENT))
|
||||
{
|
||||
if (mio_dev_sck_read(txt_state->client->sck, 0) <= -1)
|
||||
if (mio_dev_sck_read(txt->client->sck, 0) <= -1)
|
||||
{
|
||||
MIO_DEBUG2 (txt_state->htts->mio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", txt_state->htts, txt_state->client->sck);
|
||||
mio_dev_sck_halt (txt_state->client->sck);
|
||||
MIO_DEBUG2 (txt->htts->mio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", txt->htts, txt->client->sck);
|
||||
mio_dev_sck_halt (txt->client->sck);
|
||||
}
|
||||
}
|
||||
|
||||
if (old_over != TXT_STATE_OVER_ALL && txt_state->over == TXT_STATE_OVER_ALL)
|
||||
if (old_over != TXT_OVER_ALL && txt->over == TXT_OVER_ALL)
|
||||
{
|
||||
/* ready to stop */
|
||||
if (txt_state->keep_alive)
|
||||
if (txt->keep_alive)
|
||||
{
|
||||
/* how to arrange to delete this txt_state object and put the socket back to the normal waiting state??? */
|
||||
MIO_ASSERT (txt_state->htts->mio, txt_state->client->rsrc == (mio_svc_htts_rsrc_t*)txt_state);
|
||||
/* how to arrange to delete this txt object and put the socket back to the normal waiting state??? */
|
||||
MIO_ASSERT (txt->htts->mio, txt->client->rsrc == (mio_svc_htts_rsrc_t*)txt);
|
||||
|
||||
printf ("DETACHING FROM THE MAIN CLIENT RSRC... state -> %p\n", txt_state->client->rsrc);
|
||||
MIO_SVC_HTTS_RSRC_DETACH (txt_state->client->rsrc);
|
||||
/* txt_state must not be access from here down as it could have been destroyed */
|
||||
printf ("DETACHING FROM THE MAIN CLIENT RSRC... state -> %p\n", txt->client->rsrc);
|
||||
MIO_SVC_HTTS_RSRC_DETACH (txt->client->rsrc);
|
||||
/* txt must not be access from here down as it could have been destroyed */
|
||||
}
|
||||
else
|
||||
{
|
||||
MIO_DEBUG2 (txt_state->htts->mio, "HTTS(%p) - halting client(%p) for no keep-alive\n", txt_state->htts, txt_state->client->sck);
|
||||
mio_dev_sck_shutdown (txt_state->client->sck, MIO_DEV_SCK_SHUTDOWN_WRITE);
|
||||
mio_dev_sck_halt (txt_state->client->sck);
|
||||
MIO_DEBUG2 (txt->htts->mio, "HTTS(%p) - halting client(%p) for no keep-alive\n", txt->htts, txt->client->sck);
|
||||
mio_dev_sck_shutdown (txt->client->sck, MIO_DEV_SCK_SHUTDOWN_WRITE);
|
||||
mio_dev_sck_halt (txt->client->sck);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void txt_state_on_kill (txt_state_t* txt_state)
|
||||
static void txt_on_kill (txt_t* txt)
|
||||
{
|
||||
mio_t* mio = txt_state->htts->mio;
|
||||
mio_t* mio = txt->htts->mio;
|
||||
|
||||
MIO_DEBUG2 (mio, "HTTS(%p) - killing txt_state client(%p)\n", txt_state->htts, txt_state->client->sck);
|
||||
MIO_DEBUG2 (mio, "HTTS(%p) - killing txt client(%p)\n", txt->htts, txt->client->sck);
|
||||
|
||||
if (txt_state->client_org_on_read)
|
||||
if (txt->client_org_on_read)
|
||||
{
|
||||
txt_state->client->sck->on_read = txt_state->client_org_on_read;
|
||||
txt_state->client_org_on_read = MIO_NULL;
|
||||
txt->client->sck->on_read = txt->client_org_on_read;
|
||||
txt->client_org_on_read = MIO_NULL;
|
||||
}
|
||||
|
||||
if (txt_state->client_org_on_write)
|
||||
if (txt->client_org_on_write)
|
||||
{
|
||||
txt_state->client->sck->on_write = txt_state->client_org_on_write;
|
||||
txt_state->client_org_on_write = MIO_NULL;
|
||||
txt->client->sck->on_write = txt->client_org_on_write;
|
||||
txt->client_org_on_write = MIO_NULL;
|
||||
}
|
||||
|
||||
if (txt_state->client_org_on_disconnect)
|
||||
if (txt->client_org_on_disconnect)
|
||||
{
|
||||
txt_state->client->sck->on_disconnect = txt_state->client_org_on_disconnect;
|
||||
txt_state->client_org_on_disconnect = MIO_NULL;
|
||||
txt->client->sck->on_disconnect = txt->client_org_on_disconnect;
|
||||
txt->client_org_on_disconnect = MIO_NULL;
|
||||
}
|
||||
|
||||
if (txt_state->client_htrd_recbs_changed)
|
||||
if (txt->client_htrd_recbs_changed)
|
||||
{
|
||||
/* restore the callbacks */
|
||||
mio_htrd_setrecbs (txt_state->client->htrd, &txt_state->client_htrd_org_recbs);
|
||||
mio_htrd_setrecbs (txt->client->htrd, &txt->client_htrd_org_recbs);
|
||||
}
|
||||
|
||||
if (!txt_state->client_disconnected)
|
||||
if (!txt->client_disconnected)
|
||||
{
|
||||
/*printf ("ENABLING INPUT WATCHING on CLIENT %p. \n", txt_state->client->sck);*/
|
||||
if (!txt_state->keep_alive || mio_dev_sck_read(txt_state->client->sck, 1) <= -1)
|
||||
/*printf ("ENABLING INPUT WATCHING on CLIENT %p. \n", txt->client->sck);*/
|
||||
if (!txt->keep_alive || mio_dev_sck_read(txt->client->sck, 1) <= -1)
|
||||
{
|
||||
MIO_DEBUG2 (mio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", txt_state->htts, txt_state->client->sck);
|
||||
mio_dev_sck_halt (txt_state->client->sck);
|
||||
MIO_DEBUG2 (mio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", txt->htts, txt->client->sck);
|
||||
mio_dev_sck_halt (txt->client->sck);
|
||||
}
|
||||
}
|
||||
|
||||
/*printf ("**** TXT_STATE_ON_KILL DONE\n");*/
|
||||
/*printf ("**** TXT_ON_KILL DONE\n");*/
|
||||
}
|
||||
|
||||
static int txt_client_htrd_poke (mio_htrd_t* htrd, mio_htre_t* req)
|
||||
@ -202,11 +202,11 @@ static int txt_client_htrd_poke (mio_htrd_t* htrd, mio_htre_t* req)
|
||||
mio_svc_htts_cli_htrd_xtn_t* htrdxtn = (mio_svc_htts_cli_htrd_xtn_t*)mio_htrd_getxtn(htrd);
|
||||
mio_dev_sck_t* sck = htrdxtn->sck;
|
||||
mio_svc_htts_cli_t* cli = mio_dev_sck_getxtn(sck);
|
||||
txt_state_t* txt_state = (txt_state_t*)cli->rsrc;
|
||||
txt_t* txt = (txt_t*)cli->rsrc;
|
||||
|
||||
printf (">> CLIENT REQUEST COMPLETED\n");
|
||||
|
||||
txt_state_mark_over (txt_state, TXT_STATE_OVER_READ_FROM_CLIENT);
|
||||
txt_mark_over (txt, TXT_OVER_READ_FROM_CLIENT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -226,16 +226,16 @@ static mio_htrd_recbs_t txt_client_htrd_recbs =
|
||||
static void txt_client_on_disconnect (mio_dev_sck_t* sck)
|
||||
{
|
||||
mio_svc_htts_cli_t* cli = mio_dev_sck_getxtn(sck);
|
||||
txt_state_t* txt_state = (txt_state_t*)cli->rsrc;
|
||||
txt_state->client_disconnected = 1;
|
||||
txt_state->client_org_on_disconnect (sck);
|
||||
txt_t* txt = (txt_t*)cli->rsrc;
|
||||
txt->client_disconnected = 1;
|
||||
txt->client_org_on_disconnect (sck);
|
||||
}
|
||||
|
||||
static int txt_client_on_read (mio_dev_sck_t* sck, const void* buf, mio_iolen_t len, const mio_skad_t* srcaddr)
|
||||
{
|
||||
mio_t* mio = sck->mio;
|
||||
mio_svc_htts_cli_t* cli = mio_dev_sck_getxtn(sck);
|
||||
txt_state_t* txt_state = (txt_state_t*)cli->rsrc;
|
||||
txt_t* txt = (txt_t*)cli->rsrc;
|
||||
|
||||
MIO_ASSERT (mio, sck == cli->sck);
|
||||
|
||||
@ -249,18 +249,18 @@ static int txt_client_on_read (mio_dev_sck_t* sck, const void* buf, mio_iolen_t
|
||||
if (len == 0)
|
||||
{
|
||||
/* EOF on the client side. arrange to close */
|
||||
MIO_DEBUG3 (mio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", txt_state->client->htts, sck, (int)sck->hnd);
|
||||
MIO_DEBUG3 (mio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", txt->client->htts, sck, (int)sck->hnd);
|
||||
|
||||
if (!(txt_state->over & TXT_STATE_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without txt_client_htrd_poke() */
|
||||
if (!(txt->over & TXT_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without txt_client_htrd_poke() */
|
||||
{
|
||||
txt_state_mark_over (txt_state, TXT_STATE_OVER_READ_FROM_CLIENT);
|
||||
txt_mark_over (txt, TXT_OVER_READ_FROM_CLIENT);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mio_oow_t rem;
|
||||
|
||||
MIO_ASSERT (mio, !(txt_state->over & TXT_STATE_OVER_READ_FROM_CLIENT));
|
||||
MIO_ASSERT (mio, !(txt->over & TXT_OVER_READ_FROM_CLIENT));
|
||||
|
||||
if (mio_htrd_feed(cli->htrd, buf, len, &rem) <= -1) goto oops;
|
||||
|
||||
@ -274,7 +274,7 @@ printf ("UUUUUUUUUUUUUUUUUUUUUUUUUUGGGGGHHHHHHHHHHHH .......... TXT CLIENT GIVIN
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
txt_state_halt_participating_devices (txt_state);
|
||||
txt_halt_participating_devices (txt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -282,7 +282,7 @@ static int txt_client_on_write (mio_dev_sck_t* sck, mio_iolen_t wrlen, void* wrc
|
||||
{
|
||||
mio_t* mio = sck->mio;
|
||||
mio_svc_htts_cli_t* cli = mio_dev_sck_getxtn(sck);
|
||||
txt_state_t* txt_state = (txt_state_t*)cli->rsrc;
|
||||
txt_t* txt = (txt_t*)cli->rsrc;
|
||||
|
||||
if (wrlen <= -1)
|
||||
{
|
||||
@ -293,28 +293,28 @@ static int txt_client_on_write (mio_dev_sck_t* sck, mio_iolen_t wrlen, void* wrc
|
||||
if (wrlen == 0)
|
||||
{
|
||||
/* if the connect is keep-alive, this part may not be called */
|
||||
txt_state->num_pending_writes_to_client--;
|
||||
MIO_ASSERT (mio, txt_state->num_pending_writes_to_client == 0);
|
||||
MIO_DEBUG3 (mio, "HTTS(%p) - indicated EOF to client %p(%d)\n", txt_state->client->htts, sck, (int)sck->hnd);
|
||||
txt->num_pending_writes_to_client--;
|
||||
MIO_ASSERT (mio, txt->num_pending_writes_to_client == 0);
|
||||
MIO_DEBUG3 (mio, "HTTS(%p) - indicated EOF to client %p(%d)\n", txt->client->htts, sck, (int)sck->hnd);
|
||||
/* since EOF has been indicated to the client, it must not write to the client any further.
|
||||
* this also means that i don't need any data from the peer side either.
|
||||
* i don't need to enable input watching on the peer side */
|
||||
txt_state_mark_over (txt_state, TXT_STATE_OVER_WRITE_TO_CLIENT);
|
||||
txt_mark_over (txt, TXT_OVER_WRITE_TO_CLIENT);
|
||||
}
|
||||
else
|
||||
{
|
||||
MIO_ASSERT (mio, txt_state->num_pending_writes_to_client > 0);
|
||||
txt_state->num_pending_writes_to_client--;
|
||||
if (txt_state->num_pending_writes_to_client <= 0)
|
||||
MIO_ASSERT (mio, txt->num_pending_writes_to_client > 0);
|
||||
txt->num_pending_writes_to_client--;
|
||||
if (txt->num_pending_writes_to_client <= 0)
|
||||
{
|
||||
txt_state_mark_over (txt_state, TXT_STATE_OVER_WRITE_TO_CLIENT);
|
||||
txt_mark_over (txt, TXT_OVER_WRITE_TO_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
txt_state_halt_participating_devices (txt_state);
|
||||
txt_halt_participating_devices (txt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -322,28 +322,28 @@ int mio_svc_htts_dotxt (mio_svc_htts_t* htts, mio_dev_sck_t* csck, mio_htre_t* r
|
||||
{
|
||||
mio_t* mio = htts->mio;
|
||||
mio_svc_htts_cli_t* cli = mio_dev_sck_getxtn(csck);
|
||||
txt_state_t* txt_state = MIO_NULL;
|
||||
txt_t* txt = MIO_NULL;
|
||||
|
||||
/* ensure that you call this function before any contents is received */
|
||||
MIO_ASSERT (mio, mio_htre_getcontentlen(req) == 0);
|
||||
|
||||
txt_state = (txt_state_t*)mio_svc_htts_rsrc_make(htts, MIO_SIZEOF(*txt_state), txt_state_on_kill);
|
||||
if (MIO_UNLIKELY(!txt_state)) goto oops;
|
||||
txt = (txt_t*)mio_svc_htts_rsrc_make(htts, MIO_SIZEOF(*txt), txt_on_kill);
|
||||
if (MIO_UNLIKELY(!txt)) goto oops;
|
||||
|
||||
txt_state->client = cli;
|
||||
/*txt_state->num_pending_writes_to_client = 0;*/
|
||||
txt_state->req_version = *mio_htre_getversion(req);
|
||||
txt_state->req_content_length_unlimited = mio_htre_getreqcontentlen(req, &txt_state->req_content_length);
|
||||
txt->client = cli;
|
||||
/*txt->num_pending_writes_to_client = 0;*/
|
||||
txt->req_version = *mio_htre_getversion(req);
|
||||
txt->req_content_length_unlimited = mio_htre_getreqcontentlen(req, &txt->req_content_length);
|
||||
|
||||
txt_state->client_org_on_read = csck->on_read;
|
||||
txt_state->client_org_on_write = csck->on_write;
|
||||
txt_state->client_org_on_disconnect = csck->on_disconnect;
|
||||
txt->client_org_on_read = csck->on_read;
|
||||
txt->client_org_on_write = csck->on_write;
|
||||
txt->client_org_on_disconnect = csck->on_disconnect;
|
||||
csck->on_read = txt_client_on_read;
|
||||
csck->on_write = txt_client_on_write;
|
||||
csck->on_disconnect = txt_client_on_disconnect;
|
||||
|
||||
MIO_ASSERT (mio, cli->rsrc == MIO_NULL);
|
||||
MIO_SVC_HTTS_RSRC_ATTACH (txt_state, cli->rsrc);
|
||||
MIO_SVC_HTTS_RSRC_ATTACH (txt, cli->rsrc);
|
||||
|
||||
if (req->flags & MIO_HTRE_ATTR_EXPECT100)
|
||||
{
|
||||
@ -352,36 +352,36 @@ int mio_svc_htts_dotxt (mio_svc_htts_t* htts, mio_dev_sck_t* csck, mio_htre_t* r
|
||||
else if (req->flags & MIO_HTRE_ATTR_EXPECT)
|
||||
{
|
||||
/* 417 Expectation Failed */
|
||||
txt_state_send_final_status_to_client(txt_state, 417, MIO_NULL, MIO_NULL, 1);
|
||||
txt_send_final_status_to_client(txt, 417, MIO_NULL, MIO_NULL, 1);
|
||||
goto oops;
|
||||
}
|
||||
|
||||
if (txt_state->req_content_length_unlimited || txt_state->req_content_length > 0)
|
||||
if (txt->req_content_length_unlimited || txt->req_content_length > 0)
|
||||
{
|
||||
/* change the callbacks to subscribe to contents to be uploaded */
|
||||
txt_state->client_htrd_org_recbs = *mio_htrd_getrecbs(txt_state->client->htrd);
|
||||
txt_client_htrd_recbs.peek = txt_state->client_htrd_org_recbs.peek;
|
||||
mio_htrd_setrecbs (txt_state->client->htrd, &txt_client_htrd_recbs);
|
||||
txt_state->client_htrd_recbs_changed = 1;
|
||||
txt->client_htrd_org_recbs = *mio_htrd_getrecbs(txt->client->htrd);
|
||||
txt_client_htrd_recbs.peek = txt->client_htrd_org_recbs.peek;
|
||||
mio_htrd_setrecbs (txt->client->htrd, &txt_client_htrd_recbs);
|
||||
txt->client_htrd_recbs_changed = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* no content to be uploaded from the client */
|
||||
/* indicate EOF to the peer and disable input wathching from the client */
|
||||
txt_state_mark_over (txt_state, TXT_STATE_OVER_READ_FROM_CLIENT);
|
||||
txt_mark_over (txt, TXT_OVER_READ_FROM_CLIENT);
|
||||
}
|
||||
|
||||
/* this may change later if Content-Length is included in the txt output */
|
||||
txt_state->keep_alive = !!(req->flags & MIO_HTRE_ATTR_KEEPALIVE);
|
||||
txt->keep_alive = !!(req->flags & MIO_HTRE_ATTR_KEEPALIVE);
|
||||
|
||||
/* TODO: store current input watching state and use it when destroying the txt_state data */
|
||||
if (mio_dev_sck_read(csck, !(txt_state->over & TXT_STATE_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
|
||||
/* TODO: store current input watching state and use it when destroying the txt data */
|
||||
if (mio_dev_sck_read(csck, !(txt->over & TXT_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
|
||||
|
||||
if (txt_state_send_final_status_to_client(txt_state, status_code, content_type, content_text, 0) <= -1) goto oops;
|
||||
if (txt_send_final_status_to_client(txt, status_code, content_type, content_text, 0) <= -1) goto oops;
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
MIO_DEBUG2 (mio, "HTTS(%p) - FAILURE in dotxt - socket(%p)\n", htts, csck);
|
||||
if (txt_state) txt_state_halt_participating_devices (txt_state);
|
||||
if (txt) txt_halt_participating_devices (txt);
|
||||
return -1;
|
||||
}
|
||||
|
@ -288,7 +288,7 @@ MIO_EXPORT int mio_svc_htts_dofile (
|
||||
mio_dev_sck_t* csck,
|
||||
mio_htre_t* req,
|
||||
const mio_bch_t* docroot,
|
||||
const mio_bch_t* file,
|
||||
const mio_bch_t* filepath,
|
||||
const mio_bch_t* mime_type
|
||||
);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user