mirror of
https://github.com/lxsang/antd-cgi-plugin
synced 2025-05-14 00:51:50 +02:00
improvement code
This commit is contained in:
parent
a3bd60338a
commit
8c31338a96
83
cgi.c
83
cgi.c
@ -12,8 +12,8 @@
|
||||
|
||||
dictionary_t cgi_bin = NULL;
|
||||
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
int size;
|
||||
char *env[MAX_ENV_SIZE];
|
||||
} envar_arr_t;
|
||||
@ -58,6 +58,54 @@ void destroy()
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
char *tmp = (char *)dvalue(rq->request, "METHOD");
|
||||
int ret;
|
||||
if (!tmp || EQU(tmp, "GET") || EQU(tmp, "HEAD"))
|
||||
return;
|
||||
int clen = -1;
|
||||
@ -93,9 +142,10 @@ static void write_request_body(antd_request_t *rq, int fd)
|
||||
{
|
||||
read += stat;
|
||||
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)
|
||||
{
|
||||
@ -153,7 +203,8 @@ static void get_env_vars(antd_request_t *rq, envar_arr_t* env_vars)
|
||||
if (tmp)
|
||||
{
|
||||
sub = tmp;
|
||||
while(*sub == '/') sub++;
|
||||
while (*sub == '/')
|
||||
sub++;
|
||||
if (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");
|
||||
}
|
||||
|
||||
|
||||
int read_line(int fn, char *buf, int size)
|
||||
{
|
||||
int i = 0;
|
||||
@ -245,15 +295,23 @@ void *handle(void *data)
|
||||
antd_task_t *task = NULL;
|
||||
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");
|
||||
task = antd_create_task(NULL, data, NULL,rq->client->last_io);
|
||||
task->priority++;
|
||||
return task;
|
||||
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
}
|
||||
// PIPE
|
||||
UNUSED(pipe(inpipefd));
|
||||
UNUSED(pipe(outpipefd));
|
||||
if (pipe(inpipefd) == -1)
|
||||
{
|
||||
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();
|
||||
if (pid == 0)
|
||||
{
|
||||
@ -347,7 +405,7 @@ void *handle(void *data)
|
||||
// send out the rest of data
|
||||
while (1)
|
||||
{
|
||||
count = read(inpipefd[0], buf, BUFFLEN);
|
||||
count = guard_read(inpipefd[0], buf, BUFFLEN);
|
||||
|
||||
if (count == -1)
|
||||
{
|
||||
@ -379,6 +437,5 @@ void *handle(void *data)
|
||||
kill(pid, SIGKILL);
|
||||
//waitpid(pid, &status, 0);
|
||||
task = antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
task->priority++;
|
||||
return task;
|
||||
}
|
||||
|
BIN
dist/cgi-1.0.0b.tar.gz
vendored
BIN
dist/cgi-1.0.0b.tar.gz
vendored
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user