Compare commits
13 Commits
v4.4
...
69dfc562af
Author | SHA1 | Date | |
---|---|---|---|
69dfc562af
|
|||
a2383825ed
|
|||
55f2318b57
|
|||
eeb0ce7035
|
|||
b5749ae991
|
|||
9adb4791df
|
|||
eba916fad4
|
|||
ac2d17f23a
|
|||
433cc7861f
|
|||
52ea670d36
|
|||
b4b5d77985
|
|||
aebc731c4e
|
|||
4994b4375b
|
22
Makefile
22
Makefile
@@ -1,9 +1,9 @@
|
||||
|
||||
CFLAGS=-std=c11 -Wall
|
||||
INCLUDE=-lssl -lcrypto -lmagic -lz -lmaxminddb -lbrotlienc
|
||||
LIBS=src/lib/*.c
|
||||
CC=gcc
|
||||
CFLAGS=-std=gnu11 -Wall
|
||||
LIBS=-lssl -lcrypto -lmagic -lz -lmaxminddb -lbrotlienc
|
||||
|
||||
DEBIAN_OPTS=-D CACHE_MAGIC_FILE="\"/usr/share/file/magic.mgc\"" -D PHP_FPM_SOCKET="\"/var/run/php/php7.3-fpm.sock\""
|
||||
DEBIAN_OPTS=-D CACHE_MAGIC_FILE="\"/usr/share/file/magic.mgc\"" -D PHP_FPM_SOCKET="\"/var/run/php/php7.4-fpm.sock\""
|
||||
|
||||
packages:
|
||||
@echo "Installing packages..."
|
||||
@@ -15,14 +15,12 @@ permit:
|
||||
|
||||
compile:
|
||||
@mkdir -p bin
|
||||
gcc $(LIBS) -o bin/libnecronda-server.so --shared -fPIC $(CFLAGS) $(INCLUDE)
|
||||
gcc src/necronda-server.c -o bin/necronda-server $(CFLAGS) $(INCLUDE) \
|
||||
$(CC) src/lib/*.c -o bin/libnecrondaserver.so --shared -fPIC $(CFLAGS) $(LIBS)
|
||||
$(CC) src/necronda-server.c -o bin/necronda-server $(CFLAGS) $(LIBS) \
|
||||
-Lbin -lnecronda-server -Wl,-rpath=$(shell pwd)/bin
|
||||
|
||||
compile-debian:
|
||||
compile-prod:
|
||||
@mkdir -p bin
|
||||
gcc $(LIBS) -o bin/libnecronda-server.so --shared -fPIC $(CFLAGS) $(INCLUDE) \
|
||||
$(DEBIAN_OPTS) -O3
|
||||
gcc src/necronda-server.c -o bin/necronda-server $(CFLAGS) $(INCLUDE) \
|
||||
-Lbin -lnecronda-server -Wl,-rpath=$(shell pwd)/bin \
|
||||
$(DEBIAN_OPTS) -O3
|
||||
$(CC) src/lib/*.c -o bin/libnecrondaserver.so --shared -fPIC $(CFLAGS) $(LIBS) $(DEBIAN_OPTS) -O3
|
||||
$(CC) src/necronda-server.c -o bin/necronda-server $(CFLAGS) $(LIBS) $(DEBIAN_OPTS) -O3 \
|
||||
-Lbin -lnecrondaserver -Wl,-rpath=$(shell pwd)/bin
|
||||
|
51
src/client.c
51
src/client.c
@@ -67,7 +67,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
||||
int use_fastcgi = 0;
|
||||
int use_rev_proxy = 0;
|
||||
int p_len;
|
||||
fastcgi_conn php_fpm = {.socket = 0, .req_id = 0};
|
||||
fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0};
|
||||
http_status custom_status;
|
||||
|
||||
http_res res;
|
||||
@@ -138,7 +138,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
||||
strcpy(host, host_ptr);
|
||||
}
|
||||
|
||||
sprintf(log_req_prefix, "[%6i][%s%24s%s]%s ", getpid(), BLD_STR, host, CLR_STR, log_client_prefix);
|
||||
sprintf(log_req_prefix, "[%6i][%s%*s%s]%s ", getpid(), BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
|
||||
log_prefix = log_req_prefix;
|
||||
print(BLD_STR "%s %s" CLR_STR, req.method, req.uri);
|
||||
|
||||
@@ -372,35 +372,46 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
||||
content_length = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
} else {
|
||||
int mode;
|
||||
if (strcmp(uri.filename + strlen(uri.filename) - 4, ".ncr") == 0) {
|
||||
mode = FASTCGI_NECRONDA;
|
||||
} else if (strcmp(uri.filename + strlen(uri.filename) - 4, ".php") == 0) {
|
||||
mode = FASTCGI_PHP;
|
||||
} else {
|
||||
res.status = http_get_status(500);
|
||||
print(ERR_STR "Invalid FastCGI extension: %s" CLR_STR, uri.filename);
|
||||
goto respond;
|
||||
}
|
||||
|
||||
struct stat statbuf;
|
||||
stat(uri.filename, &statbuf);
|
||||
char *last_modified = http_format_date(statbuf.st_mtime, buf0, sizeof(buf0));
|
||||
http_add_header_field(&res.hdr, "Last-Modified", last_modified);
|
||||
|
||||
res.status = http_get_status(200);
|
||||
if (fastcgi_init(&php_fpm, client_num, req_num, client, &req, &uri) != 0) {
|
||||
res.status = http_get_status(502);
|
||||
sprintf(err_msg, "Unable to communicate with PHP-FPM.");
|
||||
if (fastcgi_init(&fcgi_conn, mode, client_num, req_num, client, &req, &uri) != 0) {
|
||||
res.status = http_get_status(503);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
goto respond;
|
||||
}
|
||||
|
||||
char *client_content_length = http_get_header_field(&req.hdr, "Content-Length");
|
||||
if (client_content_length != NULL) {
|
||||
unsigned long client_content_len = strtoul(client_content_length, NULL, 10);
|
||||
ret = fastcgi_receive(&php_fpm, client, client_content_len);
|
||||
ret = fastcgi_receive(&fcgi_conn, client, client_content_len);
|
||||
if (ret != 0) {
|
||||
if (ret < 0) {
|
||||
goto abort;
|
||||
} else {
|
||||
sprintf(err_msg, "Unable to communicate with PHP-FPM.");
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
}
|
||||
res.status = http_get_status(502);
|
||||
goto respond;
|
||||
}
|
||||
}
|
||||
fastcgi_close_stdin(&php_fpm);
|
||||
fastcgi_close_stdin(&fcgi_conn);
|
||||
|
||||
ret = fastcgi_header(&php_fpm, &res, err_msg);
|
||||
ret = fastcgi_header(&fcgi_conn, &res, err_msg);
|
||||
if (ret != 0) {
|
||||
if (ret < 0) {
|
||||
goto abort;
|
||||
@@ -562,7 +573,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
||||
int chunked = transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0;
|
||||
|
||||
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & FASTCGI_COMPRESS);
|
||||
fastcgi_send(&php_fpm, client, flags);
|
||||
ret = fastcgi_send(&fcgi_conn, client, flags);
|
||||
} else if (use_rev_proxy) {
|
||||
char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
|
||||
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
|
||||
@@ -574,7 +585,11 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
||||
}
|
||||
|
||||
int flags = (chunked ? REV_PROXY_CHUNKED : 0) | (use_rev_proxy & REV_PROXY_COMPRESS);
|
||||
rev_proxy_send(client, len_to_send, flags);
|
||||
ret = rev_proxy_send(client, len_to_send, flags);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
client_keep_alive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -589,10 +604,10 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
||||
|
||||
uri_free(&uri);
|
||||
abort:
|
||||
if (php_fpm.socket != 0) {
|
||||
shutdown(php_fpm.socket, SHUT_RDWR);
|
||||
close(php_fpm.socket);
|
||||
php_fpm.socket = 0;
|
||||
if (fcgi_conn.socket != 0) {
|
||||
shutdown(fcgi_conn.socket, SHUT_RDWR);
|
||||
close(fcgi_conn.socket);
|
||||
fcgi_conn.socket = 0;
|
||||
}
|
||||
http_free_req(&req);
|
||||
http_free_res(&res);
|
||||
@@ -682,7 +697,7 @@ int client_connection_handler(sock *client, unsigned long client_num) {
|
||||
if (pos != NULL) {
|
||||
pos = strstr(pos, "\"iso_code\":");
|
||||
pos += 12;
|
||||
strncpy(client_cc, pos, 2);
|
||||
snprintf(client_cc, sizeof(client_cc), "%s", pos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,11 +785,11 @@ int client_handler(sock *client, unsigned long client_num, struct sockaddr_in6 *
|
||||
log_req_prefix = malloc(256);
|
||||
log_client_prefix = malloc(256);
|
||||
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR,
|
||||
ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET_ADDRSTRLEN, client_addr_str,
|
||||
ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET6_ADDRSTRLEN, client_addr_str,
|
||||
ntohs(client_addr->sin6_port), CLR_STR);
|
||||
|
||||
log_conn_prefix = malloc(256);
|
||||
sprintf(log_conn_prefix, "[%6i][%24s]%s ", getpid(), server_addr_str, log_client_prefix);
|
||||
sprintf(log_conn_prefix, "[%6i][%*s]%s ", getpid(), INET6_ADDRSTRLEN, server_addr_str, log_client_prefix);
|
||||
log_prefix = log_conn_prefix;
|
||||
|
||||
print("Started child process with PID %i", getpid());
|
||||
|
@@ -57,14 +57,17 @@ int cache_process() {
|
||||
}
|
||||
cache = shm_rw;
|
||||
|
||||
if (mkdir("/var/necronda-server/", 0755) < 0) {
|
||||
if (errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory '/var/necronda-server/': %s" CLR_STR "\n", strerror(errno));
|
||||
return -3;
|
||||
}
|
||||
if (mkdir("/var/necronda/", 0755) < 0 && errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory '/var/necronda/': %s" CLR_STR "\n", strerror(errno));
|
||||
return -3;
|
||||
}
|
||||
|
||||
FILE *cache_file = fopen("/var/necronda-server/cache", "rb");
|
||||
if (mkdir("/var/necronda/server/", 0755) < 0 && errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory '/var/necronda/server/': %s" CLR_STR "\n", strerror(errno));
|
||||
return -3;
|
||||
}
|
||||
|
||||
FILE *cache_file = fopen("/var/necronda/server/cache", "rb");
|
||||
if (cache_file != NULL) {
|
||||
fread(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
|
||||
fclose(cache_file);
|
||||
@@ -191,7 +194,7 @@ int cache_process() {
|
||||
|
||||
if (cache_changed) {
|
||||
cache_changed = 0;
|
||||
cache_file = fopen("/var/necronda-server/cache", "wb");
|
||||
cache_file = fopen("/var/necronda/server/cache", "wb");
|
||||
if (cache_file == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno));
|
||||
free(buf);
|
||||
|
@@ -18,10 +18,6 @@
|
||||
# define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc"
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_CONFIG_FILE
|
||||
# define DEFAULT_CONFIG_FILE "/etc/necronda-server/necronda-server.conf"
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
char filename[256];
|
||||
|
@@ -85,7 +85,7 @@ int config_load(const char *filename) {
|
||||
len = strlen(ptr);
|
||||
if (ptr[0] == '[') {
|
||||
if (ptr[len - 1] != ']') goto err;
|
||||
strncpy(tmp_config[i].name, ptr + 1, len - 2);
|
||||
snprintf(tmp_config[i].name, sizeof(tmp_config[i].name), "%.*s", (int) len - 2, ptr + 1);
|
||||
i++;
|
||||
continue;
|
||||
} else if (i == 0) {
|
||||
|
@@ -17,6 +17,11 @@
|
||||
#define CONFIG_TYPE_LOCAL 1
|
||||
#define CONFIG_TYPE_REVERSE_PROXY 2
|
||||
|
||||
#ifndef DEFAULT_CONFIG_FILE
|
||||
# define DEFAULT_CONFIG_FILE "/etc/necronda/server.conf"
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
int type;
|
||||
char name[256];
|
||||
|
@@ -49,7 +49,7 @@ char *fastcgi_add_param(char *buf, const char *key, const char *value) {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_num, const sock *client,
|
||||
int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client,
|
||||
const http_req *req, const http_uri *uri) {
|
||||
unsigned short req_id = (client_num & 0xFFF) << 4;
|
||||
if (client_num == 0) {
|
||||
@@ -57,20 +57,27 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n
|
||||
} else {
|
||||
req_id |= req_num & 0xF;
|
||||
}
|
||||
conn->mode = mode;
|
||||
conn->req_id = req_id;
|
||||
conn->out_buf = NULL;
|
||||
conn->out_off = 0;
|
||||
|
||||
int php_fpm = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (php_fpm < 0) {
|
||||
int fcgi_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fcgi_sock < 0) {
|
||||
print(ERR_STR "Unable to create unix socket: %s" CLR_STR, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
conn->socket = php_fpm;
|
||||
conn->socket = fcgi_sock;
|
||||
|
||||
struct sockaddr_un php_fpm_addr = {AF_UNIX, PHP_FPM_SOCKET};
|
||||
if (connect(conn->socket, (struct sockaddr *) &php_fpm_addr, sizeof(php_fpm_addr)) < 0) {
|
||||
print(ERR_STR "Unable to connect to unix socket of PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
struct sockaddr_un sock_addr = {AF_UNIX};
|
||||
if (conn->mode == FASTCGI_NECRONDA) {
|
||||
snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path) - 1, "%s", NECRONDA_BACKEND_SOCKET);
|
||||
} else if (conn->mode == FASTCGI_PHP) {
|
||||
snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path) - 1, "%s", PHP_FPM_SOCKET);
|
||||
}
|
||||
|
||||
if (connect(conn->socket, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) {
|
||||
print(ERR_STR "Unable to connect to unix socket of FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -90,7 +97,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n
|
||||
{.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0}
|
||||
};
|
||||
if (send(conn->socket, &begin, sizeof(begin), 0) != sizeof(begin)) {
|
||||
print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -173,7 +180,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n
|
||||
header.contentLengthB0 = param_len & 0xFF;
|
||||
memcpy(param_buf, &header, sizeof(header));
|
||||
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
|
||||
print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -181,7 +188,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n
|
||||
header.contentLengthB1 = 0;
|
||||
header.contentLengthB0 = 0;
|
||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
||||
print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -201,7 +208,7 @@ int fastcgi_close_stdin(fastcgi_conn *conn) {
|
||||
};
|
||||
|
||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
||||
print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -253,7 +260,7 @@ int fastcgi_php_error(const char *msg, int msg_len, char *err_msg) {
|
||||
if (ptr3 != NULL && (ptr3 - ptr2) < len2) {
|
||||
len2 = (int) (ptr3 - ptr2);
|
||||
}
|
||||
print("%s%.*s%s", msg_type == 1 ? WRN_STR : msg_type == 2 ? ERR_STR : "", len2, ptr2, msg_type != 0 ? CLR_STR : "");
|
||||
print("%s%.*s%s", msg_type == 1 ? WRN_STR : msg_type == 2 ? ERR_STR : "", len2, ptr2, CLR_STR);
|
||||
if (msg_type == 2 && ptr2 == ptr0) {
|
||||
sprintf(err_msg, "%.*s", len2, ptr2);
|
||||
err = 1;
|
||||
@@ -285,13 +292,13 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
ret = recv(conn->socket, &header, sizeof(header), 0);
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(502);
|
||||
sprintf(err_msg, "Unable to communicate with PHP-FPM.");
|
||||
print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return 1;
|
||||
} else if (ret != sizeof(header)) {
|
||||
res->status = http_get_status(502);
|
||||
sprintf(err_msg, "Unable to communicate with PHP-FPM.");
|
||||
print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
|
||||
return 1;
|
||||
}
|
||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
||||
@@ -300,14 +307,14 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(502);
|
||||
sprintf(err_msg, "Unable to communicate with PHP-FPM.");
|
||||
print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
free(content);
|
||||
return 1;
|
||||
} else if (ret != (content_len + header.paddingLength)) {
|
||||
res->status = http_get_status(502);
|
||||
sprintf(err_msg, "Unable to communicate with PHP-FPM.");
|
||||
print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
|
||||
free(content);
|
||||
return 1;
|
||||
}
|
||||
@@ -324,14 +331,17 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "Script terminated with exit code %i" CLR_STR, app_status);
|
||||
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
|
||||
}
|
||||
close(conn->socket);
|
||||
conn->socket = 0;
|
||||
free(content);
|
||||
return 1;
|
||||
} else if (header.type == FCGI_STDERR) {
|
||||
err = err || fastcgi_php_error(content, content_len, err_msg);
|
||||
// TODO implement Necronda backend error handling
|
||||
if (conn->mode == FASTCGI_PHP) {
|
||||
err = err || fastcgi_php_error(content, content_len, err_msg);
|
||||
}
|
||||
} else if (header.type == FCGI_STDOUT) {
|
||||
break;
|
||||
} else {
|
||||
@@ -417,10 +427,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
while (1) {
|
||||
ret = recv(conn->socket, &header, sizeof(header), 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -1;
|
||||
} else if (ret != sizeof(header)) {
|
||||
print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR);
|
||||
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -430,11 +440,11 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
ptr = content;
|
||||
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
free(content);
|
||||
return -1;
|
||||
} else if (ret != (content_len + header.paddingLength)) {
|
||||
print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR);
|
||||
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
|
||||
free(content);
|
||||
return -1;
|
||||
}
|
||||
@@ -447,7 +457,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "Script terminated with exit code %i" CLR_STR, app_status);
|
||||
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
|
||||
}
|
||||
close(conn->socket);
|
||||
conn->socket = 0;
|
||||
@@ -467,7 +477,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
|
||||
return 0;
|
||||
} else if (header.type == FCGI_STDERR) {
|
||||
fastcgi_php_error(content, content_len, buf0);
|
||||
// TODO implement Necronda backend error handling
|
||||
if (conn->mode == FASTCGI_PHP) {
|
||||
fastcgi_php_error(content, content_len, buf0);
|
||||
}
|
||||
} else if (header.type == FCGI_STDOUT) {
|
||||
unsigned long avail_in, avail_out;
|
||||
out:
|
||||
@@ -532,7 +545,7 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
|
||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
|
||||
if (send(conn->socket, buf, ret, 0) != ret) {
|
||||
err:
|
||||
print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno));
|
||||
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
@@ -17,11 +17,17 @@
|
||||
#define FASTCGI_COMPRESS_BR 4
|
||||
#define FASTCGI_COMPRESS 6
|
||||
|
||||
#define FASTCGI_PHP 1
|
||||
#define FASTCGI_NECRONDA 2
|
||||
|
||||
#ifndef PHP_FPM_SOCKET
|
||||
# define PHP_FPM_SOCKET "/var/run/php-fpm/php-fpm.sock"
|
||||
#endif
|
||||
|
||||
#define NECRONDA_BACKEND_SOCKET "/var/run/necronda/necronda-backend.sock"
|
||||
|
||||
typedef struct {
|
||||
int mode;
|
||||
int socket;
|
||||
unsigned short req_id;
|
||||
char *out_buf;
|
||||
@@ -31,7 +37,7 @@ typedef struct {
|
||||
|
||||
char *fastcgi_add_param(char *buf, const char *key, const char *value);
|
||||
|
||||
int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_num, const sock *client,
|
||||
int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client,
|
||||
const http_req *req, const http_uri *uri);
|
||||
|
||||
int fastcgi_close_stdin(fastcgi_conn *conn);
|
||||
|
@@ -125,7 +125,7 @@ int http_receive_request(sock *client, http_req *req) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
strncpy(req->method, ptr, pos1 - ptr - 1);
|
||||
snprintf(req->method, sizeof(req->method), "%.*s", (int) (pos1 - ptr - 1), ptr);
|
||||
|
||||
pos2 = memchr(pos1, ' ', rcv_len - (pos1 - buf)) + 1;
|
||||
if (pos2 == NULL) {
|
||||
|
@@ -42,7 +42,7 @@
|
||||
typedef struct {
|
||||
unsigned short code;
|
||||
char type[16];
|
||||
char msg[32];
|
||||
char msg[64];
|
||||
} http_status;
|
||||
|
||||
typedef struct {
|
||||
|
@@ -186,18 +186,21 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client
|
||||
goto rev_proxy_timeout_err;
|
||||
if (setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0) {
|
||||
rev_proxy_timeout_err:
|
||||
res->status = http_get_status(502);
|
||||
res->status = http_get_status(500);
|
||||
print(ERR_STR "Unable to set timeout for socket: %s" CLR_STR, strerror(errno));
|
||||
sprintf(err_msg, "Unable to set timeout for socket: %s", strerror(errno));
|
||||
goto proxy_err;
|
||||
}
|
||||
|
||||
struct hostent *host_ent = gethostbyname(conf->rev_proxy.hostname);
|
||||
struct hostent *host_ent = gethostbyname2(conf->rev_proxy.hostname, AF_INET6);
|
||||
if (host_ent == NULL) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to connect to server: Name or service not known" CLR_STR);
|
||||
sprintf(err_msg, "Unable to connect to server: Name or service not known.");
|
||||
goto proxy_err;
|
||||
host_ent = gethostbyname2(conf->rev_proxy.hostname, AF_INET);
|
||||
if (host_ent == NULL) {
|
||||
res->status = http_get_status(503);
|
||||
print(ERR_STR "Unable to connect to server: Name or service not known" CLR_STR);
|
||||
sprintf(err_msg, "Unable to connect to server: Name or service not known.");
|
||||
goto proxy_err;
|
||||
}
|
||||
}
|
||||
|
||||
struct sockaddr_in6 address = {.sin6_family = AF_INET6, .sin6_port = htons(conf->rev_proxy.port)};
|
||||
@@ -209,9 +212,18 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client
|
||||
memcpy(&address.sin6_addr, addr, 16);
|
||||
}
|
||||
|
||||
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
|
||||
|
||||
print(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "..." CLR_STR, buffer, conf->rev_proxy.port);
|
||||
if (connect(rev_proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to connect to server: %s" CLR_STR, strerror(errno));
|
||||
if (errno == ETIMEDOUT) {
|
||||
res->status = http_get_status(504);
|
||||
} else if (errno == ECONNREFUSED) {
|
||||
res->status = http_get_status(503);
|
||||
} else {
|
||||
res->status = http_get_status(500);
|
||||
}
|
||||
print(ERR_STR "Unable to connect to [%s]:%i: %s" CLR_STR, buffer, conf->rev_proxy.port, strerror(errno));
|
||||
sprintf(err_msg, "Unable to connect to server: %s.", strerror(errno));
|
||||
goto proxy_err;
|
||||
}
|
||||
@@ -235,7 +247,6 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client
|
||||
}
|
||||
|
||||
rev_proxy_host = conf->name;
|
||||
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
|
||||
print(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i" CLR_STR, buffer, conf->rev_proxy.port);
|
||||
|
||||
rev_proxy:
|
||||
@@ -341,7 +352,8 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client
|
||||
if (res->status == NULL && status_code >= 100 && status_code <= 999) {
|
||||
custom_status->code = status_code;
|
||||
strcpy(custom_status->type, "");
|
||||
strncpy(custom_status->msg, ptr + 13, strchr(ptr, '\r') - ptr - 13);
|
||||
snprintf(custom_status->msg, sizeof(custom_status->msg), "%.*s",
|
||||
(int) (strchr(ptr, '\r') - ptr - 13), ptr + 13);
|
||||
res->status = custom_status;
|
||||
} else if (res->status == NULL) {
|
||||
res->status = http_get_status(502);
|
||||
|
@@ -30,6 +30,7 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo
|
||||
char buf1[1024];
|
||||
char buf2[1024];
|
||||
char buf3[1024];
|
||||
char buf4[1024];
|
||||
int p_len;
|
||||
uri->webroot = NULL;
|
||||
uri->req_path = NULL;
|
||||
@@ -99,12 +100,18 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo
|
||||
|
||||
while (1) {
|
||||
sprintf(buf0, "%s%s", uri->webroot, uri->path);
|
||||
p_len = snprintf(buf1, sizeof(buf1), "%s.php", buf0);
|
||||
p_len = snprintf(buf1, sizeof(buf1), "%s.ncr", buf0);
|
||||
if (p_len < 0 || p_len >= sizeof(buf1)) return -1;
|
||||
p_len = snprintf(buf2, sizeof(buf2), "%s.html", buf0);
|
||||
p_len = snprintf(buf2, sizeof(buf2), "%s.php", buf0);
|
||||
if (p_len < 0 || p_len >= sizeof(buf2)) return -1;
|
||||
p_len = snprintf(buf3, sizeof(buf3), "%s.html", buf0);
|
||||
if (p_len < 0 || p_len >= sizeof(buf3)) return -1;
|
||||
|
||||
if (strlen(uri->path) <= 1 || path_exists(buf0) || path_is_file(buf1) || path_is_file(buf2)) {
|
||||
if (strlen(uri->path) <= 1 ||
|
||||
path_exists(buf0) ||
|
||||
path_is_file(buf1) ||
|
||||
path_is_file(buf2) ||
|
||||
path_is_file(buf3)) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -112,37 +119,42 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo
|
||||
parent_dir:
|
||||
ptr = strrchr(uri->path, '/');
|
||||
size = (long) strlen(ptr);
|
||||
sprintf(buf3, "%.*s%s", (int) size, ptr, uri->pathinfo);
|
||||
strcpy(uri->pathinfo, buf3);
|
||||
sprintf(buf4, "%.*s%s", (int) size, ptr, uri->pathinfo);
|
||||
strcpy(uri->pathinfo, buf4);
|
||||
ptr[0] = 0;
|
||||
}
|
||||
if (uri->pathinfo[0] != 0) {
|
||||
sprintf(buf3, "%s", uri->pathinfo + 1);
|
||||
strcpy(uri->pathinfo, buf3);
|
||||
sprintf(buf4, "%s", uri->pathinfo + 1);
|
||||
strcpy(uri->pathinfo, buf4);
|
||||
}
|
||||
|
||||
if (path_is_file(buf0)) {
|
||||
uri->filename = malloc(strlen(buf0) + 1);
|
||||
strcpy(uri->filename, buf0);
|
||||
long len = (long) strlen(uri->path);
|
||||
if (strcmp(uri->path + len - 5, ".html") == 0) {
|
||||
uri->path[len - 5] = 0;
|
||||
} else if (strcmp(uri->path + len - 4, ".php") == 0) {
|
||||
if (strcmp(uri->path + len - 4, ".ncr") == 0 || strcmp(uri->path + len - 4, ".php") == 0) {
|
||||
uri->path[len - 4] = 0;
|
||||
uri->is_static = 0;
|
||||
} else if (strcmp(uri->path + len - 5, ".html") == 0) {
|
||||
uri->path[len - 5] = 0;
|
||||
}
|
||||
} else if (path_is_file(buf1)) {
|
||||
uri->is_static = 0;
|
||||
uri->filename = malloc(strlen(buf1) + 1);
|
||||
strcpy(uri->filename, buf1);
|
||||
} else if (path_is_file(buf2)) {
|
||||
uri->is_static = 0;
|
||||
uri->filename = malloc(strlen(buf2) + 1);
|
||||
strcpy(uri->filename, buf2);
|
||||
} else if (path_is_file(buf3)) {
|
||||
uri->filename = malloc(strlen(buf3) + 1);
|
||||
strcpy(uri->filename, buf3);
|
||||
} else {
|
||||
uri->is_dir = 1;
|
||||
strcpy(uri->path + strlen(uri->path), "/");
|
||||
sprintf(buf1, "%s%sindex.php", uri->webroot, uri->path);
|
||||
sprintf(buf2, "%s%sindex.html", uri->webroot, uri->path);
|
||||
sprintf(buf1, "%s%sindex.ncr", uri->webroot, uri->path);
|
||||
sprintf(buf2, "%s%sindex.php", uri->webroot, uri->path);
|
||||
sprintf(buf3, "%s%sindex.html", uri->webroot, uri->path);
|
||||
if (path_is_file(buf1)) {
|
||||
uri->filename = malloc(strlen(buf1) + 1);
|
||||
strcpy(uri->filename, buf1);
|
||||
@@ -150,6 +162,10 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo
|
||||
} else if (path_is_file(buf2)) {
|
||||
uri->filename = malloc(strlen(buf2) + 1);
|
||||
strcpy(uri->filename, buf2);
|
||||
uri->is_static = 0;
|
||||
} else if (path_is_file(buf3)) {
|
||||
uri->filename = malloc(strlen(buf3) + 1);
|
||||
strcpy(uri->filename, buf3);
|
||||
} else {
|
||||
if (dir_mode == URI_DIR_MODE_FORBIDDEN) {
|
||||
uri->is_static = 1;
|
||||
@@ -169,7 +185,9 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo
|
||||
if (strcmp(uri->path + strlen(uri->path) - 5, "index") == 0) {
|
||||
uri->path[strlen(uri->path) - 5] = 0;
|
||||
}
|
||||
if (strcmp(uri->pathinfo, "index.php") == 0 || strcmp(uri->pathinfo, "index.html") == 0) {
|
||||
if (strcmp(uri->pathinfo, "index.ncr") == 0 ||
|
||||
strcmp(uri->pathinfo, "index.php") == 0 ||
|
||||
strcmp(uri->pathinfo, "index.html") == 0) {
|
||||
uri->pathinfo[0] = 0;
|
||||
}
|
||||
|
||||
|
@@ -106,7 +106,7 @@ int url_decode(const char *str, char *dec, long *size) {
|
||||
int mime_is_compressible(const char *type) {
|
||||
if (type == NULL) return 0;
|
||||
char type_parsed[64];
|
||||
strncpy(type_parsed, type, sizeof(type_parsed) - 1);
|
||||
snprintf(type_parsed, sizeof(type_parsed), "%s", type);
|
||||
char *pos = strchr(type_parsed, ';');
|
||||
if (pos != NULL) pos[0] = 0;
|
||||
return
|
||||
|
Reference in New Issue
Block a user