Add logger
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
* @date 2020-12-19
|
||||
*/
|
||||
|
||||
#include "../logger.h"
|
||||
#include "cache.h"
|
||||
#include "utils.h"
|
||||
#include "compress.h"
|
||||
@ -27,11 +28,11 @@ cache_entry *cache;
|
||||
int magic_init(void) {
|
||||
magic = magic_open(MAGIC_MIME);
|
||||
if (magic == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open magic cookie: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to open magic cookie");
|
||||
return -1;
|
||||
}
|
||||
if (magic_load(magic, CACHE_MAGIC_FILE) != 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to load magic cookie: %s" CLR_STR "\n", magic_error(magic));
|
||||
critical("Unable to load magic cookie: %s", magic_error(magic));
|
||||
return -2;
|
||||
}
|
||||
return 0;
|
||||
@ -42,30 +43,34 @@ void cache_process_term(int _) {
|
||||
}
|
||||
|
||||
int cache_process(void) {
|
||||
errno = 0;
|
||||
signal(SIGINT, cache_process_term);
|
||||
signal(SIGTERM, cache_process_term);
|
||||
|
||||
logger_set_name("cache");
|
||||
|
||||
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to create cache shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to create cache shared memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
shmdt(cache);
|
||||
errno = 0;
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to attach cache shared memory (rw)");
|
||||
return -2;
|
||||
}
|
||||
cache = shm_rw;
|
||||
|
||||
if (mkdir("/var/sesimos/", 0755) < 0 && errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory '/var/sesimos/': %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to create directory '/var/sesimos/'");
|
||||
return -3;
|
||||
}
|
||||
|
||||
if (mkdir("/var/sesimos/server/", 0755) < 0 && errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory '/var/sesimos/server/': %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to create directory '/var/sesimos/server/'");
|
||||
return -3;
|
||||
}
|
||||
|
||||
@ -75,6 +80,8 @@ int cache_process(void) {
|
||||
fclose(cache_file);
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
|
||||
for (int i = 0; i < CACHE_ENTRIES; i++) {
|
||||
cache[i].is_updating = 0;
|
||||
}
|
||||
@ -93,7 +100,7 @@ int cache_process(void) {
|
||||
for (int i = 0; i < CACHE_ENTRIES; i++) {
|
||||
if (cache[i].filename[0] != 0 && cache[i].meta.etag[0] == 0 && !cache[i].is_updating) {
|
||||
cache[i].is_updating = 1;
|
||||
fprintf(stdout, "[cache] Hashing file %s\n", cache[i].filename);
|
||||
info("Hashing file %s", cache[i].filename);
|
||||
|
||||
ctx = EVP_MD_CTX_new();
|
||||
EVP_DigestInit(ctx, EVP_sha1());
|
||||
@ -106,15 +113,16 @@ int cache_process(void) {
|
||||
if (compress) {
|
||||
sprintf(buf, "%.*s/.sesimos", cache[i].webroot_len, cache[i].filename);
|
||||
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory %s: %s" CLR_STR "\n", buf, strerror(errno));
|
||||
error("Unable to create directory %s", buf);
|
||||
goto comp_err;
|
||||
}
|
||||
|
||||
sprintf(buf, "%.*s/.sesimos/cache", cache[i].webroot_len, cache[i].filename);
|
||||
if (mkdir(buf, 0700) != 0 && errno != EEXIST) {
|
||||
fprintf(stderr, ERR_STR "Unable to create directory %s: %s" CLR_STR "\n", buf, strerror(errno));
|
||||
error("Unable to create directory %s", buf);
|
||||
goto comp_err;
|
||||
}
|
||||
errno = 0;
|
||||
|
||||
char *rel_path = cache[i].filename + cache[i].webroot_len + 1;
|
||||
for (int j = 0; j < strlen(rel_path); j++) {
|
||||
@ -131,22 +139,22 @@ int cache_process(void) {
|
||||
"%.*s/.sesimos/cache/%s.br",
|
||||
cache[i].webroot_len, cache[i].filename, buf);
|
||||
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
|
||||
fprintf(stderr, ERR_STR "Unable to open cached file: File name for compressed file too long" CLR_STR "\n");
|
||||
error("Unable to open cached file: File name for compressed file too long");
|
||||
goto comp_err;
|
||||
}
|
||||
|
||||
fprintf(stdout, "[cache] Compressing file %s\n", cache[i].filename);
|
||||
info("Compressing file %s", cache[i].filename);
|
||||
|
||||
comp_file_gz = fopen(filename_comp_gz, "wb");
|
||||
comp_file_br = fopen(filename_comp_br, "wb");
|
||||
if (comp_file_gz == NULL || comp_file_br == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open cached file: %s" CLR_STR "\n", strerror(errno));
|
||||
error("Unable to open cached file");
|
||||
comp_err:
|
||||
compress = 0;
|
||||
} else {
|
||||
ret = compress_init(&comp_ctx, COMPRESS_GZ | COMPRESS_BR);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to init compression: %s" CLR_STR "\n", strerror(errno));
|
||||
error("Unable to init compression");
|
||||
compress = 0;
|
||||
fclose(comp_file_gz);
|
||||
fclose(comp_file_br);
|
||||
@ -177,7 +185,7 @@ int cache_process(void) {
|
||||
compress_free(&comp_ctx);
|
||||
fclose(comp_file_gz);
|
||||
fclose(comp_file_br);
|
||||
fprintf(stdout, "[cache] Finished compressing file %s\n", cache[i].filename);
|
||||
info("Finished compressing file %s", cache[i].filename);
|
||||
strcpy(cache[i].meta.filename_comp_gz, filename_comp_gz);
|
||||
strcpy(cache[i].meta.filename_comp_br, filename_comp_br);
|
||||
} else {
|
||||
@ -193,7 +201,7 @@ int cache_process(void) {
|
||||
sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]);
|
||||
}
|
||||
fclose(file);
|
||||
fprintf(stdout, "[cache] Finished hashing file %s\n", cache[i].filename);
|
||||
info("Finished hashing file %s", cache[i].filename);
|
||||
cache[i].is_updating = 0;
|
||||
cache_changed = 1;
|
||||
}
|
||||
@ -203,7 +211,7 @@ int cache_process(void) {
|
||||
cache_changed = 0;
|
||||
cache_file = fopen("/var/sesimos/server/cache", "wb");
|
||||
if (cache_file == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to open cache file");
|
||||
return -1;
|
||||
}
|
||||
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
|
||||
@ -217,32 +225,34 @@ int cache_process(void) {
|
||||
}
|
||||
|
||||
int cache_init(void) {
|
||||
errno = 0;
|
||||
if (magic_init() != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), IPC_CREAT | IPC_EXCL | 0600);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to create cache shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to create cache shared memory");
|
||||
return -2;
|
||||
}
|
||||
|
||||
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
|
||||
if (shm == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach cache shared memory (ro): %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to attach cache shared memory (ro)");
|
||||
return -3;
|
||||
}
|
||||
cache = shm;
|
||||
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to attach cache shared memory (rw)");
|
||||
return -4;
|
||||
}
|
||||
cache = shm_rw;
|
||||
memset(cache, 0, CACHE_ENTRIES * sizeof(cache_entry));
|
||||
shmdt(shm_rw);
|
||||
cache = shm;
|
||||
errno = 0;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
@ -254,10 +264,10 @@ int cache_init(void) {
|
||||
}
|
||||
} else if (pid > 0) {
|
||||
// parent
|
||||
fprintf(stderr, "Started child process with PID %i as cache-updater\n", pid);
|
||||
info("Started child process with PID %i as cache-updater", pid);
|
||||
return pid;
|
||||
} else {
|
||||
fprintf(stderr, ERR_STR "Unable to create child process: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to create child process");
|
||||
return -5;
|
||||
}
|
||||
}
|
||||
@ -265,15 +275,16 @@ int cache_init(void) {
|
||||
int cache_unload(void) {
|
||||
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to get cache shared memory id: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to get cache shared memory id");
|
||||
shmdt(cache);
|
||||
return -1;
|
||||
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to configure cache shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to configure cache shared memory");
|
||||
shmdt(cache);
|
||||
return -1;
|
||||
}
|
||||
shmdt(cache);
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -282,7 +293,7 @@ int cache_update_entry(int entry_num, const char *filename, const char *webroot)
|
||||
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
print(ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR, strerror(errno));
|
||||
error("Unable to attach cache shared memory (rw)");
|
||||
return -1;
|
||||
}
|
||||
cache = shm_rw;
|
||||
@ -317,6 +328,7 @@ int cache_update_entry(int entry_num, const char *filename, const char *webroot)
|
||||
|
||||
shmdt(shm_rw);
|
||||
cache = cache_ro;
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -325,7 +337,7 @@ int cache_filename_comp_invalid(const char *filename) {
|
||||
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
print(ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR, strerror(errno));
|
||||
error("Unable to attach cache shared memory (rw)");
|
||||
return -1;
|
||||
}
|
||||
cache = shm_rw;
|
||||
|
@ -6,6 +6,7 @@
|
||||
* @date 2021-01-05
|
||||
*/
|
||||
|
||||
#include "../logger.h"
|
||||
#include "config.h"
|
||||
#include "utils.h"
|
||||
|
||||
@ -23,20 +24,20 @@ char geoip_dir[256], dns_server[256];
|
||||
int config_init(void) {
|
||||
int shm_id = shmget(CONFIG_SHM_KEY, sizeof(t_config), IPC_CREAT | IPC_EXCL | 0640);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to create config shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to create config shared memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
|
||||
if (shm == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach config shared memory (ro): %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to attach config shared memory (ro)");
|
||||
return -2;
|
||||
}
|
||||
config = shm;
|
||||
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach config shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to attach config shared memory (rw)");
|
||||
return -3;
|
||||
}
|
||||
config = shm_rw;
|
||||
@ -49,11 +50,11 @@ int config_init(void) {
|
||||
int config_unload(void) {
|
||||
int shm_id = shmget(CONFIG_SHM_KEY, 0, 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to get config shared memory id: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to get config shared memory id");
|
||||
shmdt(config);
|
||||
return -1;
|
||||
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to configure config shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to configure config shared memory");
|
||||
shmdt(config);
|
||||
return -1;
|
||||
}
|
||||
@ -64,7 +65,7 @@ int config_unload(void) {
|
||||
int config_load(const char *filename) {
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open config file: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to open config file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -211,7 +212,7 @@ int config_load(const char *filename) {
|
||||
if (strlen(source) == 0) {
|
||||
err:
|
||||
free(tmp_config);
|
||||
fprintf(stderr, ERR_STR "Unable to parse config file (line %i)" CLR_STR "\n", line);
|
||||
critical("Unable to parse config file (line %i)", line);
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -252,14 +253,14 @@ int config_load(const char *filename) {
|
||||
if (!found) {
|
||||
err2:
|
||||
free(tmp_config);
|
||||
fprintf(stderr, ERR_STR "Unable to parse config file" CLR_STR "\n");
|
||||
critical("Unable to parse config file");
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
int shm_id = shmget(CONFIG_SHM_KEY, 0, 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to get config shared memory id: %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to get config shared memory id");
|
||||
shmdt(config);
|
||||
return -3;
|
||||
}
|
||||
@ -267,7 +268,7 @@ int config_load(const char *filename) {
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
free(tmp_config);
|
||||
fprintf(stderr, ERR_STR "Unable to attach config shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
critical("Unable to attach config shared memory (rw)");
|
||||
return -4;
|
||||
}
|
||||
memcpy(shm_rw, tmp_config, sizeof(t_config));
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "utils.h"
|
||||
#include "compress.h"
|
||||
#include "../server.h"
|
||||
#include "../logger.h"
|
||||
|
||||
#include <sys/un.h>
|
||||
#include <sys/socket.h>
|
||||
@ -68,7 +69,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
|
||||
|
||||
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));
|
||||
error("Unable to create unix socket");
|
||||
return -1;
|
||||
}
|
||||
conn->socket = fcgi_sock;
|
||||
@ -81,7 +82,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
|
||||
}
|
||||
|
||||
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));
|
||||
error("Unable to connect to unix socket of FastCGI socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -101,7 +102,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
|
||||
{.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 FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to send to FastCGI socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -186,7 +187,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
|
||||
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 FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to send to FastCGI socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -194,7 +195,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
|
||||
header.contentLengthB1 = 0;
|
||||
header.contentLengthB0 = 0;
|
||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
||||
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to send to FastCGI socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -214,7 +215,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 FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to send to FastCGI socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -231,7 +232,7 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
|
||||
int err = 0;
|
||||
// FIXME *msg is part of a stream, handle fragmented lines
|
||||
while (1) {
|
||||
int msg_type = 0;
|
||||
log_lvl_t msg_type = LOG_INFO;
|
||||
int msg_pre_len = 0;
|
||||
ptr1 = strstr(ptr0, "PHP message: ");
|
||||
if (ptr1 == NULL) {
|
||||
@ -245,16 +246,16 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
|
||||
}
|
||||
|
||||
if (len >= 14 && strncmp(ptr0, "PHP Warning: ", 14) == 0) {
|
||||
msg_type = 1;
|
||||
msg_type = LOG_WARNING;
|
||||
msg_pre_len = 14;
|
||||
} else if (len >= 18 && strncmp(ptr0, "PHP Fatal error: ", 18) == 0) {
|
||||
msg_type = 2;
|
||||
msg_type = LOG_ERROR;
|
||||
msg_pre_len = 18;
|
||||
} else if (len >= 18 && strncmp(ptr0, "PHP Parse error: ", 18) == 0) {
|
||||
msg_type = 2;
|
||||
msg_type = LOG_ERROR;
|
||||
msg_pre_len = 18;
|
||||
} else if (len >= 18 && strncmp(ptr0, "PHP Notice: ", 13) == 0) {
|
||||
msg_type = 1;
|
||||
msg_type = LOG_NOTICE;
|
||||
msg_pre_len = 13;
|
||||
}
|
||||
|
||||
@ -267,7 +268,7 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
|
||||
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, CLR_STR);
|
||||
logmsgf(msg_type, "%.*s", len2, ptr2);
|
||||
if (msg_type == 2 && ptr2 == ptr0) {
|
||||
strcpy_rem_webroot(err_msg, ptr2, len2, conn->webroot);
|
||||
err = 1;
|
||||
@ -300,12 +301,12 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(500);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from FastCGI socket");
|
||||
return 1;
|
||||
} else if (ret != sizeof(header)) {
|
||||
res->status = http_get_status(500);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
|
||||
error("Unable to receive from FastCGI socket");
|
||||
return 1;
|
||||
}
|
||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
||||
@ -315,13 +316,13 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(500);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from FastCGI socket");
|
||||
free(content);
|
||||
return 1;
|
||||
} else if (ret != (content_len + header.paddingLength)) {
|
||||
res->status = http_get_status(500);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
|
||||
error("Unable to receive from FastCGI socket");
|
||||
free(content);
|
||||
return 1;
|
||||
}
|
||||
@ -335,10 +336,10 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
int app_status = (body->appStatusB3 << 24) | (body->appStatusB2 << 16) | (body->appStatusB1 << 8) |
|
||||
body->appStatusB0;
|
||||
if (body->protocolStatus != FCGI_REQUEST_COMPLETE) {
|
||||
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
|
||||
error("FastCGI protocol error: %i", body->protocolStatus);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
|
||||
error("FastCGI app terminated with exit code %i", app_status);
|
||||
}
|
||||
close(conn->socket);
|
||||
conn->socket = 0;
|
||||
@ -352,7 +353,7 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
} else if (header.type == FCGI_STDOUT) {
|
||||
break;
|
||||
} else {
|
||||
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
|
||||
error("Unknown FastCGI type: %i", header.type);
|
||||
}
|
||||
|
||||
free(content);
|
||||
@ -369,13 +370,13 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
char *buf = content;
|
||||
unsigned short header_len = conn->out_off;
|
||||
if (header_len <= 0) {
|
||||
print(ERR_STR "Unable to parse header: End of header not found" CLR_STR);
|
||||
error("Unable to parse header: End of header not found");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < header_len; i++) {
|
||||
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
|
||||
print(ERR_STR "Unable to parse header: Header contains illegal characters" CLR_STR);
|
||||
error("Unable to parse header: Header contains illegal characters");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@ -384,7 +385,7 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
while (header_len != (ptr - buf)) {
|
||||
char *pos0 = strstr(ptr, "\r\n");
|
||||
if (pos0 == NULL) {
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
error("Unable to parse header: Invalid header format");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -413,13 +414,13 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
if (flags & FASTCGI_COMPRESS_BR) {
|
||||
flags &= ~FASTCGI_COMPRESS_GZ;
|
||||
if (compress_init(&comp_ctx, COMPRESS_BR) != 0) {
|
||||
print(ERR_STR "Unable to init brotli: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to init brotli");
|
||||
flags &= ~FASTCGI_COMPRESS_BR;
|
||||
}
|
||||
} else if (flags & FASTCGI_COMPRESS_GZ) {
|
||||
flags &= ~FASTCGI_COMPRESS_BR;
|
||||
if (compress_init(&comp_ctx, COMPRESS_GZ) != 0) {
|
||||
print(ERR_STR "Unable to init gzip: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to init gzip");
|
||||
flags &= ~FASTCGI_COMPRESS_GZ;
|
||||
}
|
||||
}
|
||||
@ -434,11 +435,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 FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from FastCGI socket");
|
||||
return -1;
|
||||
} else if (ret != sizeof(header)) {
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: received len (%li) != header len (%li)" CLR_STR,
|
||||
ret, sizeof(header));
|
||||
error("Unable to receive from FastCGI socket: received len (%li) != header len (%li)", ret, sizeof(header));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -451,7 +451,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
while (rcv_len < content_len + header.paddingLength) {
|
||||
ret = recv(conn->socket, content + rcv_len, content_len + header.paddingLength - rcv_len, 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from FastCGI socket");
|
||||
free(content);
|
||||
return -1;
|
||||
}
|
||||
@ -463,10 +463,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
int app_status = (body->appStatusB3 << 24) | (body->appStatusB2 << 16) | (body->appStatusB1 << 8) |
|
||||
body->appStatusB0;
|
||||
if (body->protocolStatus != FCGI_REQUEST_COMPLETE) {
|
||||
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
|
||||
error("FastCGI protocol error: %i", body->protocolStatus);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
|
||||
error("FastCGI app terminated with exit code %i", app_status);
|
||||
}
|
||||
close(conn->socket);
|
||||
conn->socket = 0;
|
||||
@ -513,7 +513,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
} while ((flags & FASTCGI_COMPRESS) && (avail_in != 0 || avail_out != sizeof(comp_out)));
|
||||
if (finish_comp) goto finish;
|
||||
} else {
|
||||
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
|
||||
error("Unknown FastCGI type: %i", header.type);
|
||||
}
|
||||
free(content);
|
||||
}
|
||||
@ -533,11 +533,10 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
|
||||
while (1) {
|
||||
ret = recv(conn->socket, &header, sizeof(header), 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from FastCGI socket");
|
||||
return -1;
|
||||
} else if (ret != sizeof(header)) {
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: received len (%li) != header len (%li)" CLR_STR,
|
||||
ret, sizeof(header));
|
||||
error("Unable to receive from FastCGI socket: received len (%li) != header len (%li)", ret, sizeof(header));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -549,7 +548,7 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
|
||||
while (rcv_len < content_len + header.paddingLength) {
|
||||
ret = recv(conn->socket, content + rcv_len, content_len + header.paddingLength - rcv_len, 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from FastCGI socket");
|
||||
free(content);
|
||||
return -1;
|
||||
}
|
||||
@ -561,10 +560,10 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
|
||||
int app_status = (body->appStatusB3 << 24) | (body->appStatusB2 << 16) | (body->appStatusB1 << 8) |
|
||||
body->appStatusB0;
|
||||
if (body->protocolStatus != FCGI_REQUEST_COMPLETE) {
|
||||
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
|
||||
error("FastCGI protocol error: %i", body->protocolStatus);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
|
||||
error("FastCGI app terminated with exit code %i", app_status);
|
||||
}
|
||||
close(conn->socket);
|
||||
conn->socket = 0;
|
||||
@ -579,7 +578,7 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
|
||||
} else if (header.type == FCGI_STDOUT) {
|
||||
ptr += snprintf(ptr, len - (ptr - buf), "%.*s", content_len, content);
|
||||
} else {
|
||||
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
|
||||
error("Unknown FastCGI type: %i", header.type);
|
||||
}
|
||||
free(content);
|
||||
}
|
||||
@ -603,7 +602,7 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
|
||||
while (rcv_len < len) {
|
||||
ret = sock_recv(client, buf, sizeof(buf), 0);
|
||||
if (ret <= 0) {
|
||||
print(ERR_STR "Unable to receive: %s" CLR_STR, sock_strerror(client));
|
||||
error("Unable to receive: %s", sock_strerror(client));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -613,7 +612,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 FastCGI socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to send to FastCGI socket");
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
* @date 2020-12-09
|
||||
*/
|
||||
|
||||
#include "../logger.h"
|
||||
#include "http.h"
|
||||
#include "utils.h"
|
||||
#include "compress.h"
|
||||
@ -87,14 +88,14 @@ void http_free_res(http_res *res) {
|
||||
|
||||
int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr, int flags) {
|
||||
if (hdr->last_field_num > hdr->field_num) {
|
||||
print(ERR_STR "Unable to parse header: Invalid state" CLR_STR);
|
||||
error("Unable to parse header: Invalid state");
|
||||
return 3;
|
||||
}
|
||||
|
||||
char *pos1 = (char *) buf, *pos2 = (char *) end_ptr;
|
||||
if (buf[0] == ' ' || buf[0] == '\t') {
|
||||
if (hdr->last_field_num == -1) {
|
||||
print(ERR_STR "Unable to parse header" CLR_STR);
|
||||
error("Unable to parse header");
|
||||
return 3;
|
||||
}
|
||||
http_field *f = &hdr->fields[(int) hdr->last_field_num];
|
||||
@ -107,7 +108,7 @@ int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr,
|
||||
|
||||
pos1 = memchr(buf, ':', end_ptr - buf);
|
||||
if (pos1 == NULL) {
|
||||
print(ERR_STR "Unable to parse header" CLR_STR);
|
||||
error("Unable to parse header");
|
||||
return 3;
|
||||
}
|
||||
long len1 = pos1 - buf;
|
||||
@ -120,7 +121,7 @@ int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr,
|
||||
int found = http_get_header_field_num_len(hdr, buf, len1);
|
||||
if (!(flags & HTTP_MERGE_FIELDS) || found == -1) {
|
||||
if (http_add_header_field_len(hdr, buf, len1, pos1, len2 < 0 ? 0 : len2) != 0) {
|
||||
print(ERR_STR "Unable to parse header: Too many header fields" CLR_STR);
|
||||
error("Unable to parse header: Too many header fields");
|
||||
return 3;
|
||||
}
|
||||
} else {
|
||||
@ -147,13 +148,13 @@ int http_receive_request(sock *client, http_req *req) {
|
||||
while (1) {
|
||||
rcv_len = sock_recv(client, buf, CLIENT_MAX_HEADER_SIZE, MSG_PEEK);
|
||||
if (rcv_len <= 0) {
|
||||
print("Unable to receive http header: %s", sock_strerror(client));
|
||||
error("Unable to receive http header: %s", sock_strerror(client));
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned long header_len = strstr(buf, "\r\n\r\n") - buf + 4;
|
||||
if (header_len <= 0) {
|
||||
print(ERR_STR "Unable to parse http header: End of header not found" CLR_STR);
|
||||
error("Unable to parse http header: End of header not found");
|
||||
return 5;
|
||||
} else {
|
||||
rcv_len = sock_recv(client, buf, header_len, 0);
|
||||
@ -161,7 +162,7 @@ int http_receive_request(sock *client, http_req *req) {
|
||||
|
||||
for (int i = 0; i < header_len; i++) {
|
||||
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
|
||||
print(ERR_STR "Unable to parse http header: Header contains illegal characters" CLR_STR);
|
||||
error("Unable to parse http header: Header contains illegal characters");
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
@ -170,7 +171,7 @@ int http_receive_request(sock *client, http_req *req) {
|
||||
while (header_len > (ptr - buf + 2)) {
|
||||
pos0 = strstr(ptr, "\r\n");
|
||||
if (pos0 == NULL) {
|
||||
print(ERR_STR "Unable to parse http header: Invalid header format" CLR_STR);
|
||||
error("Unable to parse http header: Invalid header format");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -179,13 +180,13 @@ int http_receive_request(sock *client, http_req *req) {
|
||||
if (pos1 == NULL) goto err_hdr_fmt;
|
||||
|
||||
if (pos1 - ptr - 1 >= sizeof(req->method)) {
|
||||
print(ERR_STR "Unable to parse http header: Method name too long" CLR_STR);
|
||||
error("Unable to parse http header: Method name too long");
|
||||
return 2;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (pos1 - ptr - 1); i++) {
|
||||
if (ptr[i] < 'A' || ptr[i] > 'Z') {
|
||||
print(ERR_STR "Unable to parse http header: Invalid method" CLR_STR);
|
||||
error("Unable to parse http header: Invalid method");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@ -194,12 +195,12 @@ int http_receive_request(sock *client, http_req *req) {
|
||||
pos2 = memchr(pos1, ' ', rcv_len - (pos1 - buf)) + 1;
|
||||
if (pos2 == NULL) {
|
||||
err_hdr_fmt:
|
||||
print(ERR_STR "Unable to parse http header: Invalid header format" CLR_STR);
|
||||
error("Unable to parse http header: Invalid header format");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (memcmp(pos2, "HTTP/", 5) != 0 || memcmp(pos2 + 8, "\r\n", 2) != 0) {
|
||||
print(ERR_STR "Unable to parse http header: Invalid version" CLR_STR);
|
||||
error("Unable to parse http header: Invalid version");
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "../defs.h"
|
||||
#include "../server.h"
|
||||
#include "../logger.h"
|
||||
#include "rev_proxy.h"
|
||||
#include "utils.h"
|
||||
#include "compress.h"
|
||||
@ -40,7 +41,7 @@ int rev_proxy_request_header(http_req *req, int enc) {
|
||||
} else {
|
||||
p_len = snprintf(buf2, sizeof(buf2), "%s, %s", via, buf1);
|
||||
if (p_len < 0 || p_len >= sizeof(buf2)) {
|
||||
print(ERR_STR "Header field 'Via' too long" CLR_STR);
|
||||
error("Header field 'Via' too long");
|
||||
return -1;
|
||||
}
|
||||
http_remove_header_field(&req->hdr, "Via", HTTP_REMOVE_ALL);
|
||||
@ -57,7 +58,7 @@ int rev_proxy_request_header(http_req *req, int enc) {
|
||||
client_ipv6 ? "\"[" : "", client_addr_str, client_ipv6 ? "]\"" : "",
|
||||
host, enc ? "https" : "http");
|
||||
if (p_len < 0 || p_len >= sizeof(buf1)) {
|
||||
print(ERR_STR "Appended part of header field 'Forwarded' too long" CLR_STR);
|
||||
error("Appended part of header field 'Forwarded' too long");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -66,7 +67,7 @@ int rev_proxy_request_header(http_req *req, int enc) {
|
||||
} else {
|
||||
p_len = snprintf(buf2, sizeof(buf2), "%s, %s", forwarded, buf1);
|
||||
if (p_len < 0 || p_len >= sizeof(buf2)) {
|
||||
print(ERR_STR "Header field 'Forwarded' too long" CLR_STR);
|
||||
error("Header field 'Forwarded' too long");
|
||||
return -1;
|
||||
}
|
||||
http_remove_header_field(&req->hdr, "Forwarded", HTTP_REMOVE_ALL);
|
||||
@ -134,7 +135,7 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
|
||||
const char *via = http_get_header_field(&res->hdr, "Via");
|
||||
p_len = snprintf(buf1, sizeof(buf1), "HTTP/%s %s", req->version, SERVER_NAME);
|
||||
if (p_len < 0 || p_len >= sizeof(buf1)) {
|
||||
print(ERR_STR "Appended part of header field 'Via' too long" CLR_STR);
|
||||
error("Appended part of header field 'Via' too long");
|
||||
return -1;
|
||||
}
|
||||
if (via == NULL) {
|
||||
@ -142,7 +143,7 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
|
||||
} else {
|
||||
p_len = snprintf(buf2, sizeof(buf2), "%s, %s", via, buf1);
|
||||
if (p_len < 0 || p_len >= sizeof(buf2)) {
|
||||
print(ERR_STR "Header field 'Via' too long" CLR_STR);
|
||||
error("Header field 'Via' too long");
|
||||
return -1;
|
||||
}
|
||||
http_remove_header_field(&res->hdr, "Via", HTTP_REMOVE_ALL);
|
||||
@ -190,7 +191,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
|
||||
retry:
|
||||
if (rev_proxy.socket != 0) {
|
||||
print(BLUE_STR "Closing proxy connection" CLR_STR);
|
||||
info(BLUE_STR "Closing proxy connection");
|
||||
sock_close(&rev_proxy);
|
||||
}
|
||||
retry = 0;
|
||||
@ -198,7 +199,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
|
||||
rev_proxy.socket = socket(AF_INET6, SOCK_STREAM, 0);
|
||||
if (rev_proxy.socket < 0) {
|
||||
print(ERR_STR "Unable to create socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to create socket");
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
return -1;
|
||||
@ -217,7 +218,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (host_ent == NULL) {
|
||||
res->status = http_get_status(503);
|
||||
ctx->origin = SERVER_REQ;
|
||||
print(ERR_STR "Unable to connect to server: Name or service not known" CLR_STR);
|
||||
error("Unable to connect to server: Name or service not known");
|
||||
sprintf(err_msg, "Unable to connect to server: Name or service not known.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -234,7 +235,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
|
||||
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);
|
||||
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", buffer, conf->rev_proxy.port);
|
||||
if (connect(rev_proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
|
||||
if (errno == ETIMEDOUT || errno == EINPROGRESS) {
|
||||
res->status = http_get_status(504);
|
||||
@ -246,7 +247,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
}
|
||||
print(ERR_STR "Unable to connect to [%s]:%i: %s" CLR_STR, buffer, conf->rev_proxy.port, strerror(errno));
|
||||
error("Unable to connect to [%s]:%i: %s", buffer, conf->rev_proxy.port, strerror(errno));
|
||||
sprintf(err_msg, "Unable to connect to server: %s.", strerror(errno));
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -259,7 +260,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
rev_proxy_timeout_err:
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
print(ERR_STR "Unable to set timeout for reverse proxy socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to set timeout for reverse proxy socket");
|
||||
sprintf(err_msg, "Unable to set timeout for reverse proxy socket: %s", strerror(errno));
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -277,14 +278,14 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_REQ;
|
||||
print(ERR_STR "Unable to perform handshake: %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
error("Unable to perform handshake: %s", sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&rev_proxy));
|
||||
goto proxy_err;
|
||||
}
|
||||
}
|
||||
|
||||
rev_proxy_host = conf->name;
|
||||
print(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i" CLR_STR, buffer, conf->rev_proxy.port);
|
||||
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", buffer, conf->rev_proxy.port);
|
||||
|
||||
rev_proxy:
|
||||
connection = http_get_header_field(&req->hdr, "Connection");
|
||||
@ -314,7 +315,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_REQ;
|
||||
print(ERR_STR "Unable to send request to server (1): %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
error("Unable to send request to server (1): %s", sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
@ -335,20 +336,20 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (ret == -1) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_REQ;
|
||||
print(ERR_STR "Unable to send request to server (2): %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
error("Unable to send request to server (2): %s", sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
} else if (ret == -2) {
|
||||
res->status = http_get_status(400);
|
||||
ctx->origin = CLIENT_REQ;
|
||||
print(ERR_STR "Unable to receive request from client: %s" CLR_STR, sock_strerror(client));
|
||||
error("Unable to receive request from client: %s", sock_strerror(client));
|
||||
sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client));
|
||||
return -1;
|
||||
}
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
print(ERR_STR "Unknown Error" CLR_STR);
|
||||
error("Unknown Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -364,7 +365,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
}
|
||||
print(ERR_STR "Unable to receive response from server: %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
error("Unable to receive response from server: %s", sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&rev_proxy));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
@ -376,7 +377,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (header_len <= 0) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
print(ERR_STR "Unable to parse header: End of header not found" CLR_STR);
|
||||
error("Unable to parse header: End of header not found");
|
||||
sprintf(err_msg, "Unable to parser header: End of header not found.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -385,7 +386,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
print(ERR_STR "Unable to parse header: Header contains illegal characters" CLR_STR);
|
||||
error("Unable to parse header: Header contains illegal characters");
|
||||
sprintf(err_msg, "Unable to parse header: Header contains illegal characters.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -397,7 +398,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (pos0 == NULL) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
error("Unable to parse header: Invalid header format");
|
||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -405,7 +406,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (strncmp(ptr, "HTTP/", 5) != 0) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
error("Unable to parse header: Invalid header format");
|
||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -420,7 +421,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
} else if (res->status == NULL) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
print(ERR_STR "Unable to parse header: Invalid or unknown status code" CLR_STR);
|
||||
error("Unable to parse header: Invalid or unknown status code");
|
||||
sprintf(err_msg, "Unable to parse header: Invalid or unknown status code.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -429,7 +430,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
||||
if (ret != 0) {
|
||||
res->status = http_get_status(502);
|
||||
ctx->origin = SERVER_RES;
|
||||
print(ERR_STR "Unable to parse header" CLR_STR);
|
||||
error("Unable to parse header");
|
||||
sprintf(err_msg, "Unable to parse header.");
|
||||
goto proxy_err;
|
||||
}
|
||||
@ -464,13 +465,13 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
||||
if (flags & REV_PROXY_COMPRESS_BR) {
|
||||
flags &= ~REV_PROXY_COMPRESS_GZ;
|
||||
if (compress_init(&comp_ctx, COMPRESS_BR) != 0) {
|
||||
print(ERR_STR "Unable to init brotli: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to init brotli");
|
||||
flags &= ~REV_PROXY_COMPRESS_BR;
|
||||
}
|
||||
} else if (flags & REV_PROXY_COMPRESS_GZ) {
|
||||
flags &= ~REV_PROXY_COMPRESS_BR;
|
||||
if (compress_init(&comp_ctx, COMPRESS_GZ) != 0) {
|
||||
print(ERR_STR "Unable to init gzip: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to init gzip");
|
||||
flags &= ~REV_PROXY_COMPRESS_GZ;
|
||||
}
|
||||
}
|
||||
@ -481,9 +482,9 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
||||
ret = sock_get_chunk_header(&rev_proxy);
|
||||
if (ret < 0) {
|
||||
if (ret == -1) {
|
||||
print("Unable to receive from server: Malformed chunk header");
|
||||
error("Unable to receive from server: Malformed chunk header");
|
||||
} else {
|
||||
print("Unable to receive from server: %s", sock_strerror(&rev_proxy));
|
||||
error("Unable to receive from server: %s", sock_strerror(&rev_proxy));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -503,7 +504,7 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
||||
unsigned long avail_in, avail_out;
|
||||
ret = sock_recv(&rev_proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
|
||||
if (ret <= 0) {
|
||||
print("Unable to receive from server: %s", sock_strerror(&rev_proxy));
|
||||
error("Unable to receive from server: %s", sock_strerror(&rev_proxy));
|
||||
break;
|
||||
}
|
||||
len = ret;
|
||||
@ -534,7 +535,7 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
||||
if (flags & REV_PROXY_CHUNKED) ret = sock_send(client, "\r\n", 2, 0);
|
||||
if (ret <= 0) {
|
||||
err:
|
||||
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
|
||||
error("Unable to send: %s", sock_strerror(client));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -551,7 +552,7 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
||||
if (flags & REV_PROXY_CHUNKED) {
|
||||
ret = sock_send(client, "0\r\n\r\n", 5, 0);
|
||||
if (ret <= 0) {
|
||||
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
|
||||
error("Unable to send: %s", sock_strerror(client));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -188,7 +188,6 @@ long sock_get_chunk_header(sock *s) {
|
||||
char buf[16];
|
||||
|
||||
do {
|
||||
print("debug1"); // TODO remove
|
||||
ret = sock_recv(s, buf, sizeof(buf), MSG_PEEK);
|
||||
if (ret <= 0) return -2;
|
||||
else if (ret < 2) continue;
|
||||
|
@ -13,8 +13,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
char *log_prefix;
|
||||
|
||||
static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static const int base64_mod_table[3] = {0, 2, 1};
|
||||
|
||||
|
@ -19,19 +19,6 @@
|
||||
#define HTTP_STR "\x1B[1;31m"
|
||||
#define HTTPS_STR "\x1B[1;32m"
|
||||
|
||||
extern char *log_prefix;
|
||||
|
||||
|
||||
#define out_1(fmt) fprintf(stdout, "%s" fmt "\n", log_prefix)
|
||||
#define out_2(fmt, args...) fprintf(stdout, "%s" fmt "\n", log_prefix, args)
|
||||
|
||||
#define out_x(x, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, FUNC, ...) FUNC
|
||||
|
||||
#define print(...) out_x(, ##__VA_ARGS__, out_2(__VA_ARGS__), out_2(__VA_ARGS__), out_2(__VA_ARGS__), \
|
||||
out_2(__VA_ARGS__), out_2(__VA_ARGS__), out_2(__VA_ARGS__), out_2(__VA_ARGS__), \
|
||||
out_2(__VA_ARGS__), out_1(__VA_ARGS__))
|
||||
|
||||
|
||||
char *format_duration(unsigned long micros, char *buf);
|
||||
|
||||
int url_encode_component(const void *in, size_t size_in, char *out, size_t size_out);
|
||||
|
@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
#include "../defs.h"
|
||||
#include "../logger.h"
|
||||
#include "websocket.h"
|
||||
#include "utils.h"
|
||||
|
||||
@ -45,10 +46,10 @@ int ws_recv_frame_header(sock *s, ws_frame *frame) {
|
||||
|
||||
long ret = sock_recv(s, buf, 2, 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from socket");
|
||||
return -1;
|
||||
} else if (ret != 2) {
|
||||
print(ERR_STR "Unable to receive 2 bytes from socket" CLR_STR);
|
||||
error("Unable to receive 2 bytes from socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -70,10 +71,10 @@ int ws_recv_frame_header(sock *s, ws_frame *frame) {
|
||||
|
||||
ret = sock_recv(s, buf, remaining, 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to receive from socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to receive from socket");
|
||||
return -1;
|
||||
} else if (ret != remaining) {
|
||||
print(ERR_STR "Unable to receive correct number of bytes from socket" CLR_STR);
|
||||
error("Unable to receive correct number of bytes from socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -134,10 +135,10 @@ int ws_send_frame_header(sock *s, ws_frame *frame) {
|
||||
|
||||
long ret = sock_send(s, buf, ptr - buf, frame->len != 0 ? MSG_MORE : 0);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to send to socket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to send to socket");
|
||||
return -1;
|
||||
} else if (ret != ptr - buf) {
|
||||
print(ERR_STR "Unable to send to socket" CLR_STR);
|
||||
error("Unable to send to socket");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -161,13 +162,13 @@ int ws_handle_connection(sock *s1, sock *s2) {
|
||||
if (terminate) {
|
||||
break;
|
||||
} else if (ret < 0) {
|
||||
print(ERR_STR "Unable to poll sockets: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to poll sockets");
|
||||
return -1;
|
||||
} else if (n_readable == 0) {
|
||||
print(ERR_STR "Connection timed out" CLR_STR);
|
||||
error("Connection timed out");
|
||||
return -2;
|
||||
} else if (n_error > 0) {
|
||||
print(ERR_STR "Peer closed connection" CLR_STR);
|
||||
error("Peer closed connection");
|
||||
return -3;
|
||||
}
|
||||
|
||||
@ -176,7 +177,7 @@ int ws_handle_connection(sock *s1, sock *s2) {
|
||||
sock *o = (s == s1) ? s2 : s1;
|
||||
if (ws_recv_frame_header(s, &frame) != 0) return -3;
|
||||
|
||||
// print("WebSocket: Peer %s, Opcode=0x%X, Len=%li", (s == s1) ? "1" : "2", frame.opcode, frame.len);
|
||||
// debug("WebSocket: Peer %s, Opcode=0x%X, Len=%li", (s == s1) ? "1" : "2", frame.opcode, frame.len);
|
||||
|
||||
if (frame.opcode == 0x8) {
|
||||
n_sock--;
|
||||
@ -193,10 +194,10 @@ int ws_handle_connection(sock *s1, sock *s2) {
|
||||
if (frame.len > 0) {
|
||||
ret = sock_splice(o, s, buf, sizeof(buf), frame.len);
|
||||
if (ret < 0) {
|
||||
print(ERR_STR "Unable to forward data in WebSocket: %s" CLR_STR, strerror(errno));
|
||||
error("Unable to forward data in WebSocket");
|
||||
return -4;
|
||||
} else if (ret != frame.len) {
|
||||
print(ERR_STR "Unable to forward correct number of bytes in WebSocket" CLR_STR);
|
||||
error("Unable to forward correct number of bytes in WebSocket");
|
||||
return -4;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user