2023-01-12 01:05:48 +01:00
|
|
|
#define PLUGIN_IMPLEMENT 1
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <antd/plugin.h>
|
|
|
|
#include <antd/scheduler.h>
|
|
|
|
#include <antd/utils.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
//#include <libgen.h>
|
|
|
|
#include "proto.h"
|
|
|
|
|
2023-01-17 01:03:26 +01:00
|
|
|
#ifdef MAX_PATH_LEN
|
|
|
|
#undef MAX_PATH_LEN
|
|
|
|
#endif
|
2023-01-12 01:05:48 +01:00
|
|
|
#define MAX_PATH_LEN 108
|
2023-01-17 01:03:26 +01:00
|
|
|
#define MAX_BACK_LOG 64
|
2023-01-12 01:05:48 +01:00
|
|
|
#define PROCESS_TIMEOUT 200u
|
|
|
|
|
|
|
|
#define FCGI_CLIENT_REQUEST_SENT (0)
|
|
|
|
#define FCGI_CLIENT_WAIT_FOR_RESPONSE_HEADER (-1)
|
|
|
|
#define FCGI_CLIENT_WAIT_FOR_RESPONSE_DATA (-2)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// FastCGI application path
|
|
|
|
char app_bin[MAX_PATH_LEN];
|
|
|
|
// TCP host or Unix domain socket
|
|
|
|
char address[MAX_PATH_LEN];
|
|
|
|
// only for TCP socket
|
|
|
|
int port;
|
|
|
|
// server fd
|
2023-01-17 01:03:26 +01:00
|
|
|
int fd;
|
2023-01-12 01:05:48 +01:00
|
|
|
// pid of the application process
|
2023-01-17 01:03:26 +01:00
|
|
|
pid_t pid;
|
2023-01-12 01:05:48 +01:00
|
|
|
} fcgi_config_t;
|
|
|
|
|
|
|
|
static fcgi_config_t g_config;
|
|
|
|
|
2023-01-17 01:03:26 +01:00
|
|
|
static int mk_socket();
|
|
|
|
static int read_config();
|
|
|
|
static int open_un_socket();
|
|
|
|
static int open_tcp_socket();
|
|
|
|
static int open_socket();
|
|
|
|
static char* read_line(char** buff, int* size);
|
|
|
|
static int read_header(antd_client_t* cl, antd_request_t* rq);
|
|
|
|
static int read_data(antd_client_t* cl, antd_request_t* rq);
|
|
|
|
static void *process(void *data);
|
|
|
|
static int send_request(antd_client_t *cl, antd_request_t* rq);
|
|
|
|
void* handle(void* data);
|
|
|
|
static int mk_un_socket();
|
|
|
|
static int mk_tcp_socket();
|
|
|
|
|
2023-01-12 01:05:48 +01:00
|
|
|
static int read_config()
|
|
|
|
{
|
|
|
|
char * tmp;
|
|
|
|
(void*) memset(g_config.app_bin, 0, MAX_PATH_LEN);
|
|
|
|
(void*) memset(g_config.address, 0, MAX_PATH_LEN);
|
|
|
|
g_config.port = -1;
|
2023-01-17 01:03:26 +01:00
|
|
|
g_config.fd = -1;
|
|
|
|
g_config.pid = -1;
|
2023-01-12 01:05:48 +01:00
|
|
|
regmatch_t regex_matches[3];
|
|
|
|
// read plugin configuration
|
|
|
|
if(!__plugin__.config)
|
|
|
|
{
|
2023-01-17 01:03:26 +01:00
|
|
|
PLUGIN_PANIC("No plugin configuration found. Please specify it in server config file");
|
2023-01-12 01:05:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp = (char*) dvalue(__plugin__.config, "socket");
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("No socket configuration found (socket)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(strncmp(tmp,"unix:", 5) == 0)
|
|
|
|
{
|
|
|
|
if(strlen(tmp + 5) > MAX_PATH_LEN - 1)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("socket configuration is too long: %s", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
snprintf(g_config.address, MAX_PATH_LEN,"%s", tmp+5);
|
|
|
|
LOG("Found Unix domain socket configuration: %s", g_config.address);
|
|
|
|
}
|
|
|
|
else if(regex_match("^([a-zA-Z0-9\\-_\\.]+):([0-9]+)$", tmp,3, regex_matches))
|
|
|
|
{
|
|
|
|
if(regex_matches[1].rm_eo - regex_matches[1].rm_so > MAX_PATH_LEN - 1)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("socket configuration is too long: %s", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(g_config.address, tmp + regex_matches[2].rm_so, regex_matches[2].rm_eo - regex_matches[2].rm_so);
|
|
|
|
g_config.port = atoi(g_config.address);
|
|
|
|
(void*) memset(g_config.address, 0, MAX_PATH_LEN);
|
|
|
|
memcpy(g_config.address, tmp + regex_matches[1].rm_so, regex_matches[1].rm_eo - regex_matches[1].rm_so);
|
|
|
|
LOG("Found TCP socket configuration: %s:%d", g_config.address, g_config.port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("Unknown socket configuration: %s", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp = (char*) dvalue(__plugin__.config, "bin");
|
2023-01-17 01:03:26 +01:00
|
|
|
if(tmp)
|
|
|
|
{
|
|
|
|
if(strlen(tmp) > MAX_PATH_LEN - 1)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("Bin application configuration is too long: %s", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
snprintf(g_config.app_bin, MAX_PATH_LEN,"%s", tmp);
|
|
|
|
LOG("Binary application configuration: %s", g_config.app_bin);
|
|
|
|
// create the server socket then launched it
|
|
|
|
g_config.fd = mk_socket();
|
|
|
|
if(g_config.fd == -1)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("Unable to create FastCGI server socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// launch the application
|
|
|
|
g_config.pid = fork();
|
|
|
|
if(g_config.pid == -1)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("Unable to create FastCGI server socket");
|
|
|
|
close(g_config.fd);
|
|
|
|
g_config.fd = -1;
|
|
|
|
g_config.pid = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(g_config.pid == 0)
|
|
|
|
{
|
|
|
|
// child
|
|
|
|
// close the original stdin
|
|
|
|
close(0);
|
|
|
|
// redirect the stdin to the socket
|
|
|
|
dup2(g_config.fd, 0);
|
|
|
|
char *argv[] = {g_config.app_bin, 0};
|
|
|
|
char *env[] = {NULL, NULL};
|
|
|
|
env[0] = getenv("ANTD_DEBUG");
|
|
|
|
if(env[0] && (
|
|
|
|
(strcmp(env[0], "1") == 0) || (strcmp(env[0], "true") == 0)) )
|
|
|
|
{
|
|
|
|
env[0] = "debug=1";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
env[0] = "debug=0";
|
|
|
|
}
|
|
|
|
execve(argv[0], &argv[0], &env[0]);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
// parent
|
|
|
|
LOG("FastCGI process (%d) created", g_config.pid);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mk_un_socket()
|
|
|
|
{
|
|
|
|
struct sockaddr_un address;
|
|
|
|
address.sun_family = AF_UNIX;
|
|
|
|
//remove socket file if exists
|
|
|
|
(void) remove(g_config.address);
|
|
|
|
// create the socket
|
|
|
|
(void)strncpy(address.sun_path, g_config.address, sizeof(address.sun_path));
|
|
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
ERROR("Unable to create Unix domain socket: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bind(fd, (struct sockaddr *)(&address), sizeof(address)) == -1)
|
2023-01-12 01:05:48 +01:00
|
|
|
{
|
2023-01-17 01:03:26 +01:00
|
|
|
ERROR("Unable to bind name: %s to a socket: %s", address.sun_path, strerror(errno));
|
|
|
|
close(fd);
|
2023-01-12 01:05:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2023-01-17 01:03:26 +01:00
|
|
|
// mark the socket as passive mode
|
|
|
|
|
|
|
|
if (listen(fd, MAX_BACK_LOG) == -1)
|
2023-01-12 01:05:48 +01:00
|
|
|
{
|
2023-01-17 01:03:26 +01:00
|
|
|
ERROR("Unable to listen to socket: %d (%s): %s", fd, g_config.address, strerror(errno));
|
|
|
|
close(fd);
|
2023-01-12 01:05:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2023-01-17 01:03:26 +01:00
|
|
|
LOG("Socket %s is created successfully: %d", g_config.address, fd);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
static int mk_tcp_socket()
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
struct sockaddr_in name;
|
|
|
|
fd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
ERROR("Unable to create TCP socket socket: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) == -1)
|
|
|
|
{
|
|
|
|
ERROR("Unable to set reuse address on port %d - setsockopt: %s", g_config.port, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&name, 0, sizeof(name));
|
|
|
|
name.sin_family = AF_INET;
|
|
|
|
name.sin_port = htons(g_config.port);
|
|
|
|
name.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (bind(fd, (struct sockaddr *)&name, sizeof(name)) < 0)
|
|
|
|
{
|
|
|
|
ERROR("Unable to bind TCP socket at port %d -bind: %s", g_config.port, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, MAX_BACK_LOG) < 0)
|
|
|
|
{
|
|
|
|
ERROR("Unable to listen on Port %d - listen: %s", g_config.port, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
2023-01-12 01:05:48 +01:00
|
|
|
}
|
2023-01-17 01:03:26 +01:00
|
|
|
|
2023-01-12 01:05:48 +01:00
|
|
|
static int open_un_socket()
|
|
|
|
{
|
|
|
|
struct sockaddr_un address;
|
|
|
|
address.sun_family = AF_UNIX;
|
|
|
|
// create the socket
|
|
|
|
(void)strncpy(address.sun_path, g_config.address, sizeof(address.sun_path));
|
|
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
ERROR("Unable to create Unix domain socket: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(connect(fd, (struct sockaddr*)(&address), sizeof(address)) == -1)
|
|
|
|
{
|
|
|
|
ERROR( "Unable to connect to socket '%s': %s", address.sun_path, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOG("Connected to FastCGI server at %s: %d", g_config.address, fd);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_tcp_socket()
|
|
|
|
{
|
|
|
|
struct sockaddr_in servaddr;
|
|
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("Cannot create TCP socket %s:d: %s",g_config.address, g_config.port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
|
|
|
|
|
|
// assign IP, PORT
|
|
|
|
servaddr.sin_family = AF_INET;
|
|
|
|
servaddr.sin_addr.s_addr = inet_addr(g_config.address);
|
|
|
|
servaddr.sin_port = htons(g_config.port);
|
|
|
|
|
|
|
|
// connect the client socket to server socket
|
|
|
|
if (connect(fd, (struct sockaddr*)&servaddr, sizeof(servaddr))!= 0) {
|
|
|
|
ERROR( "Unable to connect to socket '%s:%d': %s", g_config.address, g_config.port, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOG("Connected to server: %s:%d at [%d]", g_config.address, g_config.port, fd);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_socket()
|
|
|
|
{
|
|
|
|
if(g_config.port != -1)
|
|
|
|
{
|
|
|
|
return open_tcp_socket();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return open_un_socket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 01:03:26 +01:00
|
|
|
static int mk_socket()
|
|
|
|
{
|
|
|
|
if(g_config.port != -1)
|
|
|
|
{
|
|
|
|
return mk_tcp_socket();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return mk_un_socket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:05:48 +01:00
|
|
|
void init()
|
|
|
|
{
|
|
|
|
use_raw_body();
|
|
|
|
if(read_config() != 0)
|
|
|
|
return;
|
|
|
|
// create the socket
|
|
|
|
//if(create_socket() != 0)
|
|
|
|
// return;
|
|
|
|
LOG("FastCGI init successful");
|
|
|
|
|
|
|
|
}
|
|
|
|
void destroy()
|
|
|
|
{
|
2023-01-17 01:03:26 +01:00
|
|
|
if(g_config.pid > 0)
|
|
|
|
{
|
|
|
|
LOG("Process killed: %d", g_config.pid);
|
|
|
|
(void)kill(g_config.pid, SIGKILL);
|
|
|
|
g_config.pid = -1;
|
|
|
|
}
|
|
|
|
if(g_config.fd > 0)
|
2023-01-12 01:05:48 +01:00
|
|
|
{
|
2023-01-17 01:03:26 +01:00
|
|
|
LOG("Close server socket: %d", g_config.fd);
|
2023-01-12 01:05:48 +01:00
|
|
|
close(g_config.fd);
|
2023-01-17 01:03:26 +01:00
|
|
|
g_config.fd = -1;
|
|
|
|
}
|
2023-01-12 01:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-17 01:03:26 +01:00
|
|
|
static char* read_line(char** buff, int* size)
|
2023-01-12 01:05:48 +01:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while(i <= *size-1 && (*buff)[i] != '\n') i++;
|
|
|
|
if(i > 0 && i <= *size - 1)
|
|
|
|
(*buff)[i] = '\0';
|
|
|
|
char* line = *buff;
|
|
|
|
*size = *size - i - 1;
|
|
|
|
*buff = *buff + i+1;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_header(antd_client_t* cl, antd_request_t* rq)
|
|
|
|
{
|
|
|
|
FCGI_Header header;
|
|
|
|
antd_response_header_t rhd;
|
|
|
|
rhd.header = dict();
|
|
|
|
rhd.cookie = list_init();
|
|
|
|
rhd.status = 200;
|
|
|
|
char *k;
|
|
|
|
char *v;
|
|
|
|
int len, ret;
|
|
|
|
regmatch_t matches[3];
|
|
|
|
uint8_t * payload;
|
|
|
|
char* line;
|
|
|
|
char* ptr;
|
|
|
|
while(cl->state == FCGI_CLIENT_WAIT_FOR_RESPONSE_HEADER)
|
|
|
|
{
|
|
|
|
ret = fcgi_read_header(cl,&header);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
LOG("fcgi_read_header() on %d: %s", cl->sock, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
payload = fcgi_read_payload(cl, &header, &ret);
|
|
|
|
switch(header.type)
|
|
|
|
{
|
|
|
|
case FCGI_STDOUT:
|
|
|
|
// write data to the other side
|
|
|
|
if(payload && ret > 0)
|
|
|
|
{
|
|
|
|
ptr = (char*)payload;
|
|
|
|
while(ret > 0)
|
|
|
|
{
|
|
|
|
line = read_line(&ptr, &ret);
|
|
|
|
trim(line, '\r');
|
|
|
|
if(strlen(line) == 0)
|
|
|
|
{
|
|
|
|
cl->state = FCGI_CLIENT_WAIT_FOR_RESPONSE_DATA;
|
|
|
|
// write out header and the rest of the data
|
|
|
|
antd_send_header(rq->client, &rhd);
|
|
|
|
if(ret > 0)
|
|
|
|
{
|
2023-01-26 00:15:35 +01:00
|
|
|
if(antd_send(rq->client,ptr, ret) != ret && rq->client->z_level == ANTD_CNONE)
|
2023-01-12 01:05:48 +01:00
|
|
|
{
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
ERROR("Error atnd_send(): %s", strerror(errno));
|
|
|
|
free(payload);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(ret < 0) break;
|
|
|
|
if (regex_match("\\s*Status\\s*:\\s+([0-9]{3})\\s+([a-zA-Z0-9 ]*)", line, 3, matches))
|
|
|
|
{
|
|
|
|
len = matches[1].rm_eo - matches[1].rm_so;
|
|
|
|
k = (char *)malloc(len + 1);
|
|
|
|
memset(k, 0, len + 1);
|
|
|
|
memcpy(k, line + matches[1].rm_so, len);
|
|
|
|
rhd.status = atoi(k);
|
|
|
|
LOG("Response status %d", rhd.status);
|
|
|
|
free(k);
|
|
|
|
}
|
|
|
|
else if (regex_match("^([a-zA-Z0-9\\-]+)\\s*:\\s*(.*)$", line, 3, matches))
|
|
|
|
{
|
|
|
|
len = matches[1].rm_eo - matches[1].rm_so;
|
|
|
|
k = (char *)malloc(len + 1);
|
|
|
|
memset(k, 0, len + 1);
|
|
|
|
memcpy(k, line + matches[1].rm_so, len);
|
|
|
|
verify_header(k);
|
|
|
|
len = matches[2].rm_eo - matches[2].rm_so;
|
|
|
|
v = (char *)malloc(len + 1);
|
|
|
|
memset(v, 0, len + 1);
|
|
|
|
memcpy(v, line + matches[2].rm_so, len);
|
|
|
|
LOG("Header [%s] -> [%s]", k, v);
|
|
|
|
if (strcmp(k, "Set-Cookie") == 0)
|
|
|
|
{
|
|
|
|
list_put_ptr(&rhd.cookie, v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dput(rhd.header, k, v);
|
|
|
|
}
|
|
|
|
free(k);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("Ignore invalid header: %s", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FCGI_STDERR:
|
|
|
|
if(payload && ret > 0)
|
|
|
|
{
|
|
|
|
ERROR("%s", (char*) payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FCGI_END_REQUEST:
|
|
|
|
LOG("End request received, this should not happen %d", cl->sock);
|
|
|
|
// FCGI_EndRequestBody* body = (FCGI_EndRequestBody*) payload;
|
|
|
|
if(payload) free(payload);
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
LOG("Unsupported record type: 0x%02x", header.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(payload) free(payload);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_data(antd_client_t* cl, antd_request_t* rq)
|
|
|
|
{
|
|
|
|
FCGI_Header header;
|
|
|
|
int ret = fcgi_read_header(cl,&header);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
LOG("fcgi_read_header() on %d: %s", cl->sock, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t * payload = fcgi_read_payload(cl, &header, &ret);
|
|
|
|
switch(header.type)
|
|
|
|
{
|
|
|
|
case FCGI_STDOUT:
|
|
|
|
// write data to the other side
|
|
|
|
if(payload && ret > 0)
|
|
|
|
{
|
2023-01-26 00:15:35 +01:00
|
|
|
/**
|
|
|
|
* antd_send may return fewer bytes than `ret`
|
|
|
|
* this does not mean error, as the output stream
|
|
|
|
* maybe compressed by gzip
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if(antd_send(rq->client,payload, ret) != ret && rq->client->z_level == ANTD_CNONE)
|
2023-01-12 01:05:48 +01:00
|
|
|
{
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
ERROR("Error atnd_send(): %s", strerror(errno));
|
|
|
|
free(payload);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FCGI_STDERR:
|
|
|
|
if(payload && ret > 0)
|
|
|
|
{
|
|
|
|
ERROR("%s", (char*) payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FCGI_END_REQUEST:
|
|
|
|
LOG("End request received, close connection %d", cl->sock);
|
|
|
|
if(payload) free(payload);
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
LOG("Unsupported record type: 0x%02x", header.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(payload) free(payload);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *process(void *data)
|
|
|
|
{
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
antd_client_t* cl = (antd_client_t* ) dvalue(rq->request, "FCGI_CL_DATA");
|
|
|
|
struct pollfd pfds[2];
|
2023-01-17 01:03:26 +01:00
|
|
|
int status;
|
|
|
|
if(g_config.pid > 0)
|
|
|
|
{
|
|
|
|
if(waitpid(g_config.pid, &status, WNOHANG) > 0)
|
|
|
|
{
|
|
|
|
PLUGIN_PANIC("FastCGI process exits unexpectedly");
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:05:48 +01:00
|
|
|
pfds[0].fd = rq->client->sock;
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
if(rq->client->ssl)
|
|
|
|
{
|
|
|
|
pfds[0].events = POLLIN | POLLOUT;
|
|
|
|
}
|
|
|
|
pfds[1].fd = cl->sock;
|
|
|
|
pfds[1].events = POLLIN;
|
|
|
|
if(cl->state == FCGI_CLIENT_REQUEST_SENT)
|
|
|
|
{
|
|
|
|
(void)fcgi_send_stdin(cl, cl->sock, NULL, 0, 0);
|
|
|
|
if (ws_enable(rq->request))
|
|
|
|
{
|
|
|
|
cl->state = FCGI_CLIENT_WAIT_FOR_RESPONSE_DATA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cl->state = FCGI_CLIENT_WAIT_FOR_RESPONSE_HEADER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = poll(pfds, 2, PROCESS_TIMEOUT);
|
|
|
|
antd_task_t* task;
|
|
|
|
uint8_t buff[BUFFLEN];
|
|
|
|
int ret;
|
|
|
|
switch (rc)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
ERROR("Error on poll(): %s", strerror(errno));
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
case 0:
|
|
|
|
// time out
|
|
|
|
task = antd_create_task(process, (void *)rq, NULL, time(NULL));
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
|
|
|
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_READABLE);
|
|
|
|
return task;
|
|
|
|
// we have data
|
|
|
|
default:
|
|
|
|
// If data is on webserver
|
|
|
|
if ((pfds[0].revents & POLLIN) || (rq->client->ssl && (pfds[0].revents & POLLOUT)) )
|
|
|
|
{
|
|
|
|
while((ret = antd_recv_upto(rq->client,buff, BUFFLEN)) > 0)
|
|
|
|
{
|
|
|
|
// write data to the application stdin
|
|
|
|
if(fcgi_send_stdin(cl, cl->sock,buff, ret, (ret % 8 == 0)? 0 : 8 - (ret % 8) ) != 0)
|
|
|
|
{
|
|
|
|
ERROR("Error on fcgi_send_stdin(): %s", strerror(errno));
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
if(cl->state > 0)
|
|
|
|
cl->state -= ret;
|
2023-01-26 15:23:45 +01:00
|
|
|
//LOG("sending %s: %d", buff, cl->state);
|
2023-01-12 01:05:48 +01:00
|
|
|
}
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
LOG("antd_recv_upto() on %d: %s",rq->client->sock, strerror(errno));
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(pfds[0].revents &(POLLERR | POLLHUP))
|
|
|
|
{
|
|
|
|
ERROR("POLLERR or POLLHUP received on %d", rq->client->sock);
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
if(pfds[1].revents & POLLIN)
|
|
|
|
{
|
|
|
|
if(cl->state == FCGI_CLIENT_WAIT_FOR_RESPONSE_HEADER)
|
|
|
|
{
|
|
|
|
if(read_header(cl, rq) != 0)
|
|
|
|
{
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(read_data(cl,rq) != 0)
|
|
|
|
{
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(pfds[1].revents &(POLLERR | POLLHUP))
|
|
|
|
{
|
|
|
|
ERROR("POLLERR or POLLHUP received on %d", cl->sock);
|
|
|
|
//(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
antd_close(cl);
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", NULL);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
task = antd_create_task(process, (void *)rq, NULL, time(NULL));
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
|
|
|
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_READABLE);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_request(antd_client_t *cl, antd_request_t* rq)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
char *tmp = NULL;
|
|
|
|
char *root;
|
|
|
|
dictionary_t request = (dictionary_t)rq->request;
|
|
|
|
dictionary_t header = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
ret += fcgi_begin_request(cl, cl->sock, FCGI_RESPONDER, 0);
|
|
|
|
//ret += fcgi_send_param(cl, cl->sock, "", "");
|
2023-01-17 01:03:26 +01:00
|
|
|
// ANTD specific params
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "TMP_DIR", __plugin__.tmpdir);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "DB_DIR", __plugin__.dbpath);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "LIB_DIR", __plugin__.pdir);
|
|
|
|
// CGI parms
|
2023-01-12 01:05:48 +01:00
|
|
|
ret += fcgi_send_param(cl, cl->sock, "GATEWAY_INTERFACE", "CGI/1.1");
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SERVER_SOFTWARE", SERVER_NAME);
|
|
|
|
root = (char *)dvalue(request, "SERVER_WWW_ROOT");
|
2023-01-25 22:40:10 +01:00
|
|
|
tmp = (char *)dvalue(request, "REQUEST_URI");
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "PATH_INFO", "");
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "REQUEST_URI", "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "PATH_INFO", tmp);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "REQUEST_URI", tmp);
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:05:48 +01:00
|
|
|
tmp = (char *)dvalue(request, "REQUEST_QUERY");
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "QUERY_STRING", "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-25 22:40:10 +01:00
|
|
|
ret += fcgi_send_param(cl, cl->sock, "QUERY_STRING", tmp);
|
2023-01-12 01:05:48 +01:00
|
|
|
}
|
2023-01-25 22:40:10 +01:00
|
|
|
|
2023-01-12 01:05:48 +01:00
|
|
|
tmp = (char *)dvalue(request, "METHOD");
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "REQUEST_METHOD", tmp);
|
|
|
|
}
|
|
|
|
tmp = (char *)dvalue(header, "Content-Type");
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "CONTENT_TYPE", tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "CONTENT_TYPE", "");
|
|
|
|
}
|
|
|
|
tmp = (char *)dvalue(header, "Content-Length");
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
cl->state = atoi(tmp);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "CONTENT_LENGTH", tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "CONTENT_LENGTH", "");
|
|
|
|
}
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "DOCUMENT_ROOT", root);
|
|
|
|
tmp = (char *)dvalue(request, "REMOTE_ADDR");
|
|
|
|
if(tmp)
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "REMOTE_ADDR", tmp);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "REMOTE_HOST", tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SERVER_NAME", SERVER_NAME);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SERVER_PORT", (char *)dvalue(request, "SERVER_PORT"));
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SERVER_PROTOCOL", "HTTP/1.1");
|
|
|
|
// add remaining header to the vars
|
|
|
|
chain_t it;
|
|
|
|
for_each_assoc(it, header)
|
|
|
|
{
|
|
|
|
tmp = __s("HTTP_%s", it->key);
|
|
|
|
char *s = tmp;
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
if (*s == '-')
|
|
|
|
*s = '_';
|
|
|
|
else if (*s != '_')
|
|
|
|
*s = toupper((char)*s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, tmp, (char *)it->value);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
tmp = (char *)dvalue(request, "RESOURCE_PATH");
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SCRIPT_NAME", basename(tmp));
|
2023-01-25 22:40:10 +01:00
|
|
|
//tmp = __s("%s/%s", root, tmp);
|
2023-01-12 01:05:48 +01:00
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SCRIPT_FILENAME", tmp);
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "PATH_TRANSLATED", tmp);
|
2023-01-25 22:40:10 +01:00
|
|
|
//free(tmp);
|
2023-01-12 01:05:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SCRIPT_FILENAME", "");
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "PATH_TRANSLATED", "");
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "SCRIPT_NAME", "");
|
|
|
|
}
|
|
|
|
// redirect status for php
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "REDIRECT_STATUS", "200");
|
|
|
|
ret += fcgi_send_param(cl, cl->sock, "", "");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* handle(void* data)
|
|
|
|
{
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
// connect to socket
|
|
|
|
int fd = open_socket();
|
|
|
|
if(fd < 0)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 503, "Service unavailable");
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
set_nonblock(fd);
|
|
|
|
|
|
|
|
// write all header to fastCGI server via params
|
|
|
|
antd_client_t* cl = (antd_client_t*) malloc(sizeof(antd_client_t));
|
|
|
|
(void)memset(cl, 0, sizeof(antd_client_t));
|
|
|
|
cl->sock = fd;
|
|
|
|
time(&cl->last_io);
|
|
|
|
cl->ssl = NULL;
|
|
|
|
// state is used to store content lenth of the current request
|
|
|
|
cl->state = FCGI_CLIENT_REQUEST_SENT;
|
|
|
|
cl->z_status = 0;
|
|
|
|
cl->z_level = ANTD_CNONE;
|
|
|
|
cl->zstream = NULL;
|
2023-01-26 00:15:35 +01:00
|
|
|
//rq->client->z_level = ANTD_CNONE;
|
2023-01-12 01:05:48 +01:00
|
|
|
|
|
|
|
// start the request
|
|
|
|
|
|
|
|
if(send_request(cl,rq) != 0)
|
|
|
|
{
|
|
|
|
ERROR("Unable to send request to application: %d", fd);
|
|
|
|
antd_error(rq->client, 500, "Internal server error");
|
|
|
|
(void)fcgi_abort_request(cl, cl->sock);
|
|
|
|
antd_close(cl);
|
|
|
|
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
}
|
|
|
|
|
|
|
|
dput(rq->request, "FCGI_CL_DATA", cl);
|
|
|
|
|
|
|
|
antd_task_t* task = antd_create_task(process, (void *)rq, NULL, time(NULL));
|
|
|
|
//antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
|
|
|
//antd_task_bind_event(task, fd, 0, TASK_EVT_ON_READABLE);
|
|
|
|
return task;
|
|
|
|
}
|