1
0
mirror of https://github.com/lxsang/antd-cgi-plugin synced 2024-12-26 18:08:21 +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)
{
if(!v || !l || !k) return;
if (!v || !l || !k)
return;
char *data = __s("%s=%s", k, v);
list_put_s(l, 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)
{
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;
dictionary header = (dictionary)dvalue(rq->request, "REQUEST_HEADER");
tmp = (char *)dvalue(header, "Content-Length");
if (tmp)
clen = atoi(tmp);
if(clen == -1) return;
if (clen == -1)
return;
// read data and write to the fd
char buf[BUFFLEN];
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)
{
char *tmp = (char *)dvalue(rq->request, "RESOURCE_PATH");
if(!tmp) return NULL;
if (!tmp)
return NULL;
tmp = ext(tmp);
if(!tmp) return NULL;
if (!tmp)
return NULL;
char *bin = (char *)dvalue(cgi_bin, tmp);
free(tmp);
return bin;
@ -137,7 +142,8 @@ static list get_env_vars(antd_request_t* rq)
{
tmp = __s("HTTP_%s", it->key);
char *s = tmp;
while (*s) {
while (*s)
{
if (*s == '-')
*s = '_';
else if (*s != '_')
@ -171,7 +177,7 @@ void* handle(void* data)
{
antd_request_t *rq = (antd_request_t *)data;
void *cl = (void *)rq->client;
pid_t pid = 0, wpid;
pid_t pid = 0;
int inpipefd[2];
int outpipefd[2];
char buf[BUFFLEN];
@ -227,10 +233,36 @@ void* handle(void* data)
// Now, we can write to outpipefd[1] and read from inpipefd[0] :
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 {
memset(buf, 0, sizeof(buf));
int r = read(inpipefd[0], buf, BUFFLEN-1);
@ -238,8 +270,10 @@ void* handle(void* data)
{
__t(cl, buf);
}
wpid = waitpid(pid, &status, WNOHANG);
} while(wpid == 0);
*/
kill(pid, SIGKILL);
waitpid(pid, &status, 0);
free(envs);
list_free(&env_vars);
return task;