fixed wrong length types in mio_copy_bchars_to_bcstr() and mio_copy_uchars_to_ucstr()

fixed the wrong value defined for MIO_SKAD_TO_OOCSTR_PORT
set various environment variables before starting a cgi script
This commit is contained in:
hyung-hwan 2020-05-20 14:53:05 +00:00
parent d95bc9bc92
commit f373f367b3
7 changed files with 74 additions and 46 deletions

View File

@ -393,7 +393,6 @@ mio_oow_t FN(vfcat) (str_t* str, const char_t* fmt, va_list ap)
mio_fmtout_t fo; mio_fmtout_t fo;
MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo)); MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo));
//fo.mmgr = str->mio->_mmgr;
fo.putbchars = FN(put_bchars); fo.putbchars = FN(put_bchars);
fo.putuchars = FN(put_uchars); fo.putuchars = FN(put_uchars);
fo.ctx = str; fo.ctx = str;
@ -423,7 +422,6 @@ mio_oow_t FN(vfmt) (str_t* str, const char_t* fmt, va_list ap)
mio_fmtout_t fo; mio_fmtout_t fo;
MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo)); MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo));
//fo.mmgr = str->mio->_mmgr;
fo.putbchars = FN(put_bchars); fo.putbchars = FN(put_bchars);
fo.putuchars = FN(put_uchars); fo.putuchars = FN(put_uchars);
fo.ctx = str; fo.ctx = str;

View File

@ -2,6 +2,7 @@
#include "mio-htrd.h" #include "mio-htrd.h"
#include "mio-pro.h" /* for cgi */ #include "mio-pro.h" /* for cgi */
#include "mio-fmt.h" #include "mio-fmt.h"
#include "mio-chr.h"
#include "mio-prv.h" #include "mio-prv.h"
@ -9,7 +10,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> /* setenv */ #include <stdlib.h> /* setenv, clearenv */
#define CGI_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH #define CGI_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH
@ -24,9 +25,6 @@ struct mio_svc_htts_cli_t
mio_svc_htts_t* htts; mio_svc_htts_t* htts;
mio_dev_sck_t* sck; mio_dev_sck_t* sck;
mio_skad_t remote_addr;
mio_skad_t local_addr;
mio_skad_t orgdst_addr;
mio_htrd_t* htrd; mio_htrd_t* htrd;
mio_becs_t* sbuf; /* temporary buffer for status line formatting */ mio_becs_t* sbuf; /* temporary buffer for status line formatting */
@ -951,7 +949,7 @@ oops:
return 0; return 0;
} }
static int cgi_peer_capture_header (mio_htre_t* req, const mio_bch_t* key, const mio_htre_hdrval_t* val, void* ctx) static int cgi_peer_capture_response_header (mio_htre_t* req, const mio_bch_t* key, const mio_htre_hdrval_t* val, void* ctx)
{ {
mio_svc_htts_cli_t* cli = (mio_svc_htts_cli_t*)ctx; mio_svc_htts_cli_t* cli = (mio_svc_htts_cli_t*)ctx;
@ -1011,7 +1009,7 @@ printf ("CGI PEER HTRD PEEK...\n");
status_code, mio_http_status_to_bcstr(status_code), status_code, mio_http_status_to_bcstr(status_code),
cli->htts->server_name, dtbuf) == (mio_oow_t)-1) return -1; cli->htts->server_name, dtbuf) == (mio_oow_t)-1) return -1;
if (mio_htre_walkheaders(req, cgi_peer_capture_header, cli) <= -1) return -1; if (mio_htre_walkheaders(req, cgi_peer_capture_response_header, cli) <= -1) return -1;
switch (cgi_state->res_mode_to_cli) switch (cgi_state->res_mode_to_cli)
{ {
@ -1100,18 +1098,6 @@ static mio_htrd_recbs_t cgi_peer_htrd_recbs =
cgi_peer_htrd_push_content cgi_peer_htrd_push_content
}; };
static int walk_client_req_trailer (mio_htre_t* req, const mio_bch_t* key, const mio_htre_hdrval_t* val, void* ctx)
{
printf ("\t%s=");
while (val)
{
printf ("%s ", val->ptr);
val = val->next;
}
printf ("\n");
return 0;
}
static int cgi_client_htrd_poke (mio_htrd_t* htrd, mio_htre_t* req) static int cgi_client_htrd_poke (mio_htrd_t* htrd, mio_htre_t* req)
{ {
/* client request got completed */ /* client request got completed */
@ -1121,8 +1107,6 @@ static int cgi_client_htrd_poke (mio_htrd_t* htrd, mio_htre_t* req)
cgi_state_t* cgi_state = (cgi_state_t*)cli->rsrc; cgi_state_t* cgi_state = (cgi_state_t*)cli->rsrc;
printf (">> CLIENT REQUEST COMPLETED\n"); printf (">> CLIENT REQUEST COMPLETED\n");
//mio_htre_walkheaders (req, walk_client_req_trailer, MIO_NULL);
mio_htre_walktrailers (req, walk_client_req_trailer, MIO_NULL); // MIO_HTRD_TRAILERS must be set for this to work
/* indicate EOF to the client peer */ /* indicate EOF to the client peer */
if (cgi_state_write_to_peer(cgi_state, MIO_NULL, 0) <= -1) return -1; if (cgi_state_write_to_peer(cgi_state, MIO_NULL, 0) <= -1) return -1;
@ -1333,25 +1317,64 @@ struct cgi_peer_fork_ctx_t
}; };
typedef struct cgi_peer_fork_ctx_t cgi_peer_fork_ctx_t; typedef struct cgi_peer_fork_ctx_t cgi_peer_fork_ctx_t;
static int cgi_peer_capture_request_header (mio_htre_t* req, const mio_bch_t* key, const mio_htre_hdrval_t* val, void* ctx)
{
mio_becs_t* dbuf = (mio_becs_t*)ctx;
if (mio_comp_bcstr(key, "Connection", 1) != 0 &&
mio_comp_bcstr(key, "Transfer-Encoding", 1) != 0 &&
mio_comp_bcstr(key, "Content-Length", 1) != 0 &&
mio_comp_bcstr(key, "Expect", 1) != 0)
{
mio_oow_t val_offset;
mio_bch_t* ptr;
mio_becs_clear (dbuf);
if (mio_becs_cpy(dbuf, "HTTP_") == (mio_oow_t)-1 ||
mio_becs_cat(dbuf, key) == (mio_oow_t)-1 ||
mio_becs_ccat(dbuf, '\0') == (mio_oow_t)-1) return -1;
for (ptr = MIO_BECS_PTR(dbuf); *ptr; ptr++)
{
*ptr = mio_to_bch_upper(*ptr);
if (*ptr =='-') *ptr = '_';
}
val_offset = MIO_BECS_LEN(dbuf);
if (mio_becs_cat(dbuf, val->ptr) == (mio_oow_t)-1) return -1;
val = val->next;
while (val)
{
if (mio_becs_cat(dbuf, ",") == (mio_oow_t)-1 ||
mio_becs_cat (dbuf, val->ptr) == (mio_oow_t)-1) return -1;
val = val->next;
}
setenv (MIO_BECS_PTR(dbuf), MIO_BECS_CPTR(dbuf, val_offset), 1);
}
return 0;
}
static int cgi_peer_on_fork (mio_dev_pro_t* pro, void* fork_ctx) static int cgi_peer_on_fork (mio_dev_pro_t* pro, void* fork_ctx)
{ {
/*mio_t* mio = pro->mio;*/ /* in this callback, the pro device is not fully up. however, the mio field is guaranteed to be available */ /*mio_t* mio = pro->mio;*/ /* in this callback, the pro device is not fully up. however, the mio field is guaranteed to be available */
cgi_peer_fork_ctx_t* fc = (cgi_peer_fork_ctx_t*)fork_ctx; cgi_peer_fork_ctx_t* fc = (cgi_peer_fork_ctx_t*)fork_ctx;
mio_oow_t content_length; mio_oow_t content_length;
mio_bch_t tmp[128];
const mio_bch_t* qparam; const mio_bch_t* qparam;
const char* path;
mio_bch_t tmp[256];
mio_becs_t dbuf;
qparam = mio_htre_getqparam(fc->req); qparam = mio_htre_getqparam(fc->req);
path = getenv("PATH");
clearenv (); clearenv ();
if (path) setenv ("PATH", path, 1);
setenv ("GATEWAY_INTERFACE", "CGI/1.1", 1); setenv ("GATEWAY_INTERFACE", "CGI/1.1", 1);
///////// mio_fmttobcstr (pro->mio, tmp, MIO_COUNTOF(tmp), "HTTP/%d.%d", (int)mio_htre_getmajorversion(fc->req), (int)mio_htre_getminorversion(fc->req));
printf (">>>>>>>>>> %d pro->mio %p\n",
mio_fmttobcstr (pro->mio, tmp, MIO_COUNTOF(tmp), "HTTP/%d.%d", (int)mio_htre_getmajorversion(fc->req), (int)mio_htre_getminorversion(fc->req)), pro->mio);
////////
setenv ("SERVER_PROTOCOL", tmp, 1); setenv ("SERVER_PROTOCOL", tmp, 1);
//setenv ("SCRIPT_FILENAME", //setenv ("SCRIPT_FILENAME",
@ -1364,7 +1387,6 @@ printf (">>>>>>>>>> %d pro->mio %p\n",
if (get_request_content_length(fc->req, &content_length) == 0) if (get_request_content_length(fc->req, &content_length) == 0)
{ {
mio_fmt_uintmax_to_bcstr(tmp, MIO_COUNTOF(tmp), content_length, 10, 0, '\0', MIO_NULL); mio_fmt_uintmax_to_bcstr(tmp, MIO_COUNTOF(tmp), content_length, 10, 0, '\0', MIO_NULL);
setenv ("CONTENT_LENGTH", tmp, 1); setenv ("CONTENT_LENGTH", tmp, 1);
} }
@ -1374,17 +1396,28 @@ printf (">>>>>>>>>> %d pro->mio %p\n",
setenv ("CONTENT_LENGTH", "-1", 1); setenv ("CONTENT_LENGTH", "-1", 1);
} }
setenv ("SERVER_SOFTWARE", fc->cli->htts->server_name, 1); setenv ("SERVER_SOFTWARE", fc->cli->htts->server_name, 1);
#if 0
setenv ("SERVER_PORT",
setenv ("SERVER_ADDR",
setenv ("SERVER_NAME", /* server host name */
setenv ("REMOTE_PORT", mio_skadtobcstr (pro->mio, &fc->cli->sck->localaddr, tmp, MIO_COUNTOF(tmp), MIO_SKAD_TO_BCSTR_ADDR);
setenv ("REMOTE_ADDR", setenv ("SERVER_ADDR", tmp, 1);
#endif
//mio_htre_walkheaders(req, gethostname (tmp, MIO_COUNTOF(tmp)); /* if this fails, i assume tmp contains the ip address set by mio_skadtobcstr() above */
/* [NOTE] trailers are not available when this cgi resource is started. let's not call mio_htre_walktrailers() */ setenv ("SERVER_NAME", tmp, 1);
mio_skadtobcstr (pro->mio, &fc->cli->sck->localaddr, tmp, MIO_COUNTOF(tmp), MIO_SKAD_TO_BCSTR_PORT);
setenv ("SERVER_PORT", tmp, 1);
mio_skadtobcstr (pro->mio, &fc->cli->sck->remoteaddr, tmp, MIO_COUNTOF(tmp), MIO_SKAD_TO_BCSTR_ADDR);
setenv ("REMOTE_ADDR", tmp, 1);
mio_skadtobcstr (pro->mio, &fc->cli->sck->remoteaddr, tmp, MIO_COUNTOF(tmp), MIO_SKAD_TO_BCSTR_PORT);
setenv ("REMOTE_PORT", tmp, 1);
if (mio_becs_init(&dbuf, pro->mio, 256) >= 0)
{
mio_htre_walkheaders (fc->req, cgi_peer_capture_request_header, &dbuf);
/* [NOTE] trailers are not available when this cgi resource is started. let's not call mio_htre_walktrailers() */
mio_becs_fini (&dbuf);
}
return 0; return 0;
} }

View File

@ -63,7 +63,6 @@ struct mio_fmtout_t
{ {
mio_oow_t count; /* out */ mio_oow_t count; /* out */
//mio_mmgr_t* mmgr;
mio_fmtout_putbchars_t putbchars; /* in */ mio_fmtout_putbchars_t putbchars; /* in */
mio_fmtout_putuchars_t putuchars; /* in */ mio_fmtout_putuchars_t putuchars; /* in */
mio_bitmask_t mask; /* in */ mio_bitmask_t mask; /* in */

View File

@ -56,7 +56,7 @@ typedef struct mio_skad_t mio_skad_t;
#define MIO_SKAD_TO_OOCSTR_ADDR (1 << 0) #define MIO_SKAD_TO_OOCSTR_ADDR (1 << 0)
#define MIO_SKAD_TO_OOCSTR_PORT (1 << 0) #define MIO_SKAD_TO_OOCSTR_PORT (1 << 1)
#define MIO_SKAD_TO_UCSTR_ADDR MIO_SKAD_TO_OOCSTR_ADDR #define MIO_SKAD_TO_UCSTR_ADDR MIO_SKAD_TO_OOCSTR_ADDR
#define MIO_SKAD_TO_UCSTR_PORT MIO_SKAD_TO_OOCSTR_PORT #define MIO_SKAD_TO_UCSTR_PORT MIO_SKAD_TO_OOCSTR_PORT
#define MIO_SKAD_TO_BCSTR_ADDR MIO_SKAD_TO_OOCSTR_ADDR #define MIO_SKAD_TO_BCSTR_ADDR MIO_SKAD_TO_OOCSTR_ADDR

View File

@ -371,14 +371,14 @@ MIO_EXPORT mio_oow_t mio_copy_bcstr (
MIO_EXPORT mio_oow_t mio_copy_uchars_to_ucstr ( MIO_EXPORT mio_oow_t mio_copy_uchars_to_ucstr (
mio_uch_t* dst, mio_uch_t* dst,
mio_uch_t dlen, mio_oow_t dlen,
const mio_uch_t* src, const mio_uch_t* src,
mio_oow_t slen mio_oow_t slen
); );
MIO_EXPORT mio_oow_t mio_copy_bchars_to_bcstr ( MIO_EXPORT mio_oow_t mio_copy_bchars_to_bcstr (
mio_bch_t* dst, mio_bch_t* dst,
mio_bch_t dlen, mio_oow_t dlen,
const mio_bch_t* src, const mio_bch_t* src,
mio_oow_t slen mio_oow_t slen
); );

View File

@ -1720,7 +1720,6 @@ mio_oow_t mio_vfmttoucstr (mio_t* mio, mio_uch_t* buf, mio_oow_t bufsz, const mi
if (bufsz <= 0) return 0; if (bufsz <= 0) return 0;
MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo)); MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo));
//fo.mmgr = mio->mmgr;
fo.putbchars = fmt_put_bchars_to_uch_buf; fo.putbchars = fmt_put_bchars_to_uch_buf;
fo.putuchars = fmt_put_uchars_to_uch_buf; fo.putuchars = fmt_put_uchars_to_uch_buf;
fo.ctx = &fb; fo.ctx = &fb;
@ -1812,7 +1811,6 @@ mio_oow_t mio_vfmttobcstr (mio_t* mio, mio_bch_t* buf, mio_oow_t bufsz, const mi
if (bufsz <= 0) return 0; if (bufsz <= 0) return 0;
MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo)); MIO_MEMSET (&fo, 0, MIO_SIZEOF(fo));
//fo.mmgr = mio->mmgr;
fo.putbchars = fmt_put_bchars_to_bch_buf; fo.putbchars = fmt_put_bchars_to_bch_buf;
fo.putuchars = fmt_put_uchars_to_bch_buf; fo.putuchars = fmt_put_uchars_to_bch_buf;
fo.ctx = &fb; fo.ctx = &fb;

View File

@ -398,7 +398,7 @@ void mio_copy_uchars_to_bchars (mio_bch_t* dst, const mio_uch_t* src, mio_oow_t
for (i = 0; i < len; i++) dst[i] = src[i]; for (i = 0; i < len; i++) dst[i] = src[i];
} }
mio_oow_t mio_copy_uchars_to_ucstr (mio_uch_t* dst, mio_uch_t dlen, const mio_uch_t* src, mio_oow_t slen) mio_oow_t mio_copy_uchars_to_ucstr (mio_uch_t* dst, mio_oow_t dlen, const mio_uch_t* src, mio_oow_t slen)
{ {
mio_oow_t i; mio_oow_t i;
if (dlen <= 0) return 0; if (dlen <= 0) return 0;
@ -408,7 +408,7 @@ mio_oow_t mio_copy_uchars_to_ucstr (mio_uch_t* dst, mio_uch_t dlen, const mio_uc
return i; return i;
} }
mio_oow_t mio_copy_bchars_to_bcstr (mio_bch_t* dst, mio_bch_t dlen, const mio_bch_t* src, mio_oow_t slen) mio_oow_t mio_copy_bchars_to_bcstr (mio_bch_t* dst, mio_oow_t dlen, const mio_bch_t* src, mio_oow_t slen)
{ {
mio_oow_t i; mio_oow_t i;
if (dlen <= 0) return 0; if (dlen <= 0) return 0;