1
0
mirror of https://github.com/lxsang/antd-cgi-plugin synced 2024-12-28 02:48:20 +01:00

improvement code

This commit is contained in:
lxsang 2021-01-02 19:45:14 +01:00
parent a3bd60338a
commit 8c31338a96
2 changed files with 116 additions and 59 deletions

83
cgi.c
View File

@ -12,8 +12,8 @@
dictionary_t cgi_bin = NULL; dictionary_t cgi_bin = NULL;
typedef struct
typedef struct { {
int size; int size;
char *env[MAX_ENV_SIZE]; char *env[MAX_ENV_SIZE];
} envar_arr_t; } envar_arr_t;
@ -58,6 +58,54 @@ void destroy()
freedict(cgi_bin); freedict(cgi_bin);
} }
static int guard_read(int fd, void *buffer, size_t size)
{
int n = 0;
int read_len;
int st;
while (n != (int)size)
{
read_len = (int)size - n;
st = read(fd, buffer + n, read_len);
if (st == -1)
{
ERROR("Unable to read from #%d: %s", fd, strerror(errno));
return -1;
}
if (st == 0)
{
ERROR("Endpoint %d is closed", fd);
return -1;
}
n += st;
}
return n;
}
static int guard_write(int fd, void *buffer, size_t size)
{
int n = 0;
int write_len;
int st;
while (n != (int)size)
{
write_len = (int)size - n;
st = write(fd, buffer + n, write_len);
if (st == -1)
{
ERROR("Unable to write to #%d: %s", fd, strerror(errno));
return -1;
}
if (st == 0)
{
ERROR("Endpoint %d is closed", fd);
return -1;
}
n += st;
}
return n;
}
static void add_vars(envar_arr_t *l, char *k, char *v) static void add_vars(envar_arr_t *l, char *k, char *v)
{ {
if (!v || !l || !k) if (!v || !l || !k)
@ -73,6 +121,7 @@ static void add_vars(envar_arr_t *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");
int ret;
if (!tmp || EQU(tmp, "GET") || EQU(tmp, "HEAD")) if (!tmp || EQU(tmp, "GET") || EQU(tmp, "HEAD"))
return; return;
int clen = -1; int clen = -1;
@ -93,9 +142,10 @@ static void write_request_body(antd_request_t *rq, int fd)
{ {
read += stat; read += stat;
readlen = (clen - read) > BUFFLEN ? BUFFLEN : (clen - read); readlen = (clen - read) > BUFFLEN ? BUFFLEN : (clen - read);
UNUSED(write(fd, buf, stat)); ret = guard_write(fd, buf, stat);
} }
} }
UNUSED(ret);
} }
static char *get_cgi_bin(antd_request_t *rq) static char *get_cgi_bin(antd_request_t *rq)
{ {
@ -153,7 +203,8 @@ static void get_env_vars(antd_request_t *rq, envar_arr_t* env_vars)
if (tmp) if (tmp)
{ {
sub = tmp; sub = tmp;
while(*sub == '/') sub++; while (*sub == '/')
sub++;
if (sub) if (sub)
{ {
add_vars(env_vars, "PATH_INFO", sub); add_vars(env_vars, "PATH_INFO", sub);
@ -207,7 +258,6 @@ static void get_env_vars(antd_request_t *rq, envar_arr_t* env_vars)
add_vars(env_vars, "REDIRECT_STATUS", "200"); add_vars(env_vars, "REDIRECT_STATUS", "200");
} }
int read_line(int fn, char *buf, int size) int read_line(int fn, char *buf, int size)
{ {
int i = 0; int i = 0;
@ -245,15 +295,23 @@ void *handle(void *data)
antd_task_t *task = NULL; antd_task_t *task = NULL;
if (!bin || ws_enable(rq->request)) if (!bin || ws_enable(rq->request))
{ {
LOG("No cgi bin found or connection is websocket"); ERROR("No cgi bin found or connection is websocket");
antd_error(cl, 503, "Service unavailable"); antd_error(cl, 503, "Service unavailable");
task = antd_create_task(NULL, data, NULL,rq->client->last_io); return antd_create_task(NULL, data, NULL, rq->client->last_io);
task->priority++;
return task;
} }
// PIPE // PIPE
UNUSED(pipe(inpipefd)); if (pipe(inpipefd) == -1)
UNUSED(pipe(outpipefd)); {
ERROR("Can not create inpipe: %s", strerror(errno));
antd_error(cl, 503, "Service unavailable");
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
if (pipe(outpipefd) == -1)
{
ERROR("Can not create outpipe: %s", strerror(errno));
antd_error(cl, 503, "Service unavailable");
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
@ -347,7 +405,7 @@ void *handle(void *data)
// send out the rest of data // send out the rest of data
while (1) while (1)
{ {
count = read(inpipefd[0], buf, BUFFLEN); count = guard_read(inpipefd[0], buf, BUFFLEN);
if (count == -1) if (count == -1)
{ {
@ -379,6 +437,5 @@ void *handle(void *data)
kill(pid, SIGKILL); kill(pid, SIGKILL);
//waitpid(pid, &status, 0); //waitpid(pid, &status, 0);
task = antd_create_task(NULL, data, NULL, rq->client->last_io); task = antd_create_task(NULL, data, NULL, rq->client->last_io);
task->priority++;
return task; return task;
} }

BIN
dist/cgi-1.0.0b.tar.gz vendored

Binary file not shown.