1
0
mirror of https://github.com/lxsang/antd-cgi-plugin synced 2024-12-27 10:28:22 +01:00

fix cgi read output bug

This commit is contained in:
lxsang 2018-10-08 22:30:13 +02:00
parent 4f3d783df2
commit 227f78c777

56
cgi.c
View File

@ -41,7 +41,8 @@ void destroy()
static void add_vars(list *l, char *k, char *v) static void add_vars(list *l, char *k, char *v)
{ {
if(!v || !l || !k) return; if (!v || !l || !k)
return;
char *data = __s("%s=%s", k, v); char *data = __s("%s=%s", k, v);
list_put_s(l, data); list_put_s(l, data);
free(data); free(data);
@ -50,13 +51,15 @@ static void add_vars(list* l, char* k, char* v)
static void write_request_body(antd_request_t *rq, int fd) static void write_request_body(antd_request_t *rq, int fd)
{ {
char *tmp = (char *)dvalue(rq->request, "METHOD"); char *tmp = (char *)dvalue(rq->request, "METHOD");
if(!tmp || EQU(tmp,"GET") || EQU(tmp,"HEAD")) return; if (!tmp || EQU(tmp, "GET") || EQU(tmp, "HEAD"))
return;
int clen = -1; int clen = -1;
dictionary header = (dictionary)dvalue(rq->request, "REQUEST_HEADER"); dictionary header = (dictionary)dvalue(rq->request, "REQUEST_HEADER");
tmp = (char *)dvalue(header, "Content-Length"); tmp = (char *)dvalue(header, "Content-Length");
if (tmp) if (tmp)
clen = atoi(tmp); clen = atoi(tmp);
if(clen == -1) return; if (clen == -1)
return;
// read data and write to the fd // read data and write to the fd
char buf[BUFFLEN]; char buf[BUFFLEN];
int readlen = clen > BUFFLEN ? BUFFLEN : clen; int readlen = clen > BUFFLEN ? BUFFLEN : clen;
@ -75,9 +78,11 @@ static void write_request_body(antd_request_t* rq, int fd)
static char *get_cgi_bin(antd_request_t *rq) static char *get_cgi_bin(antd_request_t *rq)
{ {
char *tmp = (char *)dvalue(rq->request, "RESOURCE_PATH"); char *tmp = (char *)dvalue(rq->request, "RESOURCE_PATH");
if(!tmp) return NULL; if (!tmp)
return NULL;
tmp = ext(tmp); tmp = ext(tmp);
if(!tmp) return NULL; if (!tmp)
return NULL;
char *bin = (char *)dvalue(cgi_bin, tmp); char *bin = (char *)dvalue(cgi_bin, tmp);
free(tmp); free(tmp);
return bin; return bin;
@ -137,7 +142,8 @@ static list get_env_vars(antd_request_t* rq)
{ {
tmp = __s("HTTP_%s", it->key); tmp = __s("HTTP_%s", it->key);
char *s = tmp; char *s = tmp;
while (*s) { while (*s)
{
if (*s == '-') if (*s == '-')
*s = '_'; *s = '_';
else if (*s != '_') else if (*s != '_')
@ -171,7 +177,7 @@ void* handle(void* data)
{ {
antd_request_t *rq = (antd_request_t *)data; antd_request_t *rq = (antd_request_t *)data;
void *cl = (void *)rq->client; void *cl = (void *)rq->client;
pid_t pid = 0, wpid; pid_t pid = 0;
int inpipefd[2]; int inpipefd[2];
int outpipefd[2]; int outpipefd[2];
char buf[BUFFLEN]; char buf[BUFFLEN];
@ -227,10 +233,36 @@ void* handle(void* data)
// Now, we can write to outpipefd[1] and read from inpipefd[0] : // Now, we can write to outpipefd[1] and read from inpipefd[0] :
write_request_body(rq, outpipefd[1]); write_request_body(rq, outpipefd[1]);
LOG("Wait for respond\n");
set_status(cl, 200, "OK");
wpid = 0;
set_status(cl, 200, "OK");
//wpid = 0;
//waitpid(pid, &status, 0); // wait for the child finish
// WNOHANG
while (1)
{
memset(buf, 0, sizeof(buf));
ssize_t count = read(inpipefd[0], buf, BUFFLEN);
if (count == -1)
{
if (errno == EINTR)
{
continue;
}
else
{
break;
}
}
else if (count == 0)
{
break;
}
else
{
antd_send(cl, buf, count);
}
}
/*
do { do {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
int r = read(inpipefd[0], buf, BUFFLEN-1); int r = read(inpipefd[0], buf, BUFFLEN-1);
@ -238,8 +270,10 @@ void* handle(void* data)
{ {
__t(cl, buf); __t(cl, buf);
} }
wpid = waitpid(pid, &status, WNOHANG);
} while(wpid == 0); } while(wpid == 0);
*/
kill(pid, SIGKILL);
waitpid(pid, &status, 0);
free(envs); free(envs);
list_free(&env_vars); list_free(&env_vars);
return task; return task;