1
0
mirror of https://github.com/lxsang/antd-cgi-plugin synced 2024-12-27 10:28:22 +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

169
cgi.c
View File

@ -12,10 +12,10 @@
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;
static int ini_handle(void *user_data, const char *section, const char *name, static int ini_handle(void *user_data, const char *section, const char *name,
@ -58,11 +58,59 @@ 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)
return; return;
if(l->size >= MAX_ENV_SIZE-1) if (l->size >= MAX_ENV_SIZE - 1)
return; return;
char *data = __s("%s=%s", k, v); char *data = __s("%s=%s", k, v);
l->env[l->size] = data; l->env[l->size] = data;
@ -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)
{ {
@ -110,16 +160,16 @@ static char *get_cgi_bin(antd_request_t *rq)
free(tmp); free(tmp);
return bin; return bin;
} }
static void get_env_vars(antd_request_t *rq, envar_arr_t* env_vars) static void get_env_vars(antd_request_t *rq, envar_arr_t *env_vars)
{ {
char *tmp = NULL; char *tmp = NULL;
char *sub = NULL; char *sub = NULL;
char* root; char *root;
dictionary_t request = (dictionary_t)rq->request; dictionary_t request = (dictionary_t)rq->request;
dictionary_t header = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER"); dictionary_t header = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
add_vars(env_vars, "GATEWAY_INTERFACE", "CGI/1.1"); add_vars(env_vars, "GATEWAY_INTERFACE", "CGI/1.1");
add_vars(env_vars, "SERVER_SOFTWARE", SERVER_NAME); add_vars(env_vars, "SERVER_SOFTWARE", SERVER_NAME);
root = (char*)dvalue(header, "SERVER_WWW_ROOT"); root = (char *)dvalue(header, "SERVER_WWW_ROOT");
tmp = (char *)dvalue(request, "REQUEST_QUERY"); tmp = (char *)dvalue(request, "REQUEST_QUERY");
if (!tmp) if (!tmp)
add_vars(env_vars, "QUERY_STRING", ""); add_vars(env_vars, "QUERY_STRING", "");
@ -153,8 +203,9 @@ 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 == '/')
if(sub) sub++;
if (sub)
{ {
add_vars(env_vars, "PATH_INFO", sub); add_vars(env_vars, "PATH_INFO", sub);
} }
@ -207,31 +258,30 @@ 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;
char c = '\0'; char c = '\0';
int n; int n;
while ((i < size - 1) && (c != '\n')) while ((i < size - 1) && (c != '\n'))
{ {
n = read(fn, &c,1); n = read(fn, &c, 1);
if (n > 0) if (n > 0)
{
//LOG("Data : %c\n", c);
buf[i] = c;
i++;
}
else
{ {
if(i == 0) //LOG("Data : %c\n", c);
buf[i] = c;
i++;
}
else
{
if (i == 0)
i = n; i = n;
c = '\n'; c = '\n';
} }
} }
if(i >= 0) if (i >= 0)
buf[i] = '\0'; buf[i] = '\0';
return i; return i;
} }
void *handle(void *data) void *handle(void *data)
@ -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)
{ {
@ -266,7 +324,7 @@ void *handle(void *data)
LOG("Execute the cgi bin"); LOG("Execute the cgi bin");
envar_arr_t envs; envar_arr_t envs;
envs.size = 0; envs.size = 0;
for(int i = 0; i < MAX_ENV_SIZE; i++) for (int i = 0; i < MAX_ENV_SIZE; i++)
{ {
envs.env[i] = NULL; envs.env[i] = NULL;
} }
@ -299,38 +357,38 @@ void *handle(void *data)
rhd.header = dict(); rhd.header = dict();
rhd.cookie = list_init(); rhd.cookie = list_init();
rhd.status = 200; rhd.status = 200;
char* k; char *k;
char* v; char *v;
int len; int len;
ssize_t count; ssize_t count;
while( read_line(inpipefd[0], buf, BUFFLEN) > 0 && strcmp(buf,"\r\n") != 0) while (read_line(inpipefd[0], buf, BUFFLEN) > 0 && strcmp(buf, "\r\n") != 0)
{ {
trim(buf,'\n'); trim(buf, '\n');
trim(buf,'\r'); trim(buf, '\r');
if(regex_match("\\s*Status\\s*:\\s+([0-9]{3})\\s+([a-zA-Z0-9 ]*)",buf,3,matches)) if (regex_match("\\s*Status\\s*:\\s+([0-9]{3})\\s+([a-zA-Z0-9 ]*)", buf, 3, matches))
{ {
len = matches[1].rm_eo - matches[1].rm_so; len = matches[1].rm_eo - matches[1].rm_so;
k = (char*)malloc(len); k = (char *)malloc(len);
memset(k, 0, len); memset(k, 0, len);
memcpy(k, buf + matches[1].rm_so, len); memcpy(k, buf + matches[1].rm_so, len);
rhd.status = atoi(k); rhd.status = atoi(k);
free(k); free(k);
} }
else if(regex_match("^([a-zA-Z0-9\\-]+)\\s*:\\s*(.*)$",buf,3,matches)) else if (regex_match("^([a-zA-Z0-9\\-]+)\\s*:\\s*(.*)$", buf, 3, matches))
{ {
len = matches[1].rm_eo - matches[1].rm_so; len = matches[1].rm_eo - matches[1].rm_so;
k = (char*)malloc(len+1); k = (char *)malloc(len + 1);
memcpy(k, buf + matches[1].rm_so, len); memcpy(k, buf + matches[1].rm_so, len);
k[len] = '\0'; k[len] = '\0';
verify_header(k); verify_header(k);
len = matches[2].rm_eo - matches[2].rm_so ; len = matches[2].rm_eo - matches[2].rm_so;
v = (char*)malloc(len+1); v = (char *)malloc(len + 1);
memcpy(v, buf + matches[2].rm_so, len); memcpy(v, buf + matches[2].rm_so, len);
v[len] = '\0'; v[len] = '\0';
if(strcmp(k,"Set-Cookie") == 0) if (strcmp(k, "Set-Cookie") == 0)
{ {
list_put_ptr(&rhd.cookie,v); list_put_ptr(&rhd.cookie, v);
} }
else else
{ {
@ -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)
{ {
@ -364,11 +422,11 @@ void *handle(void *data)
else if (count == 0) else if (count == 0)
{ {
if(waitpid(pid, &status, WNOHANG) != 0) if (waitpid(pid, &status, WNOHANG) != 0)
{ {
break; break;
} }
continue; continue;
} }
else else
{ {
@ -378,7 +436,6 @@ 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.