Refactor for shared library use
This commit is contained in:
360
src/lib/cache.c
Normal file
360
src/lib/cache.c
Normal file
@ -0,0 +1,360 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* File cache implementation
|
||||
* src/lib/cache.c
|
||||
* Lorenz Stechauner, 2020-12-19
|
||||
*/
|
||||
|
||||
#include "cache.h"
|
||||
#include "utils.h"
|
||||
#include "../necronda-server.h"
|
||||
#include <stdio.h>
|
||||
#include <zlib.h>
|
||||
#include <magic.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
int cache_continue = 1;
|
||||
magic_t magic;
|
||||
cache_entry *cache;
|
||||
|
||||
int magic_init() {
|
||||
magic = magic_open(MAGIC_MIME);
|
||||
if (magic == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open magic cookie: %s" CLR_STR "\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (magic_load(magic, MAGIC_FILE) != 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to load magic cookie: %s" CLR_STR "\n", magic_error(magic));
|
||||
return -2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cache_process_term() {
|
||||
cache_continue = 0;
|
||||
}
|
||||
|
||||
int cache_process() {
|
||||
signal(SIGINT, cache_process_term);
|
||||
signal(SIGTERM, cache_process_term);
|
||||
|
||||
int shm_id = shmget(SHM_KEY_CACHE, FILE_CACHE_SIZE * sizeof(cache_entry), 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to create shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
shmdt(cache);
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
FILE *cache_file = fopen("/var/necronda-server/cache", "rb");
|
||||
if (cache_file != NULL) {
|
||||
fread(cache, sizeof(cache_entry), FILE_CACHE_SIZE, cache_file);
|
||||
fclose(cache_file);
|
||||
}
|
||||
|
||||
for (int i = 0; i < FILE_CACHE_SIZE; i++) {
|
||||
cache[i].is_updating = 0;
|
||||
}
|
||||
|
||||
FILE *file;
|
||||
char buf[16384];
|
||||
char comp_buf[16384];
|
||||
char filename_comp[256];
|
||||
unsigned long read;
|
||||
int compress;
|
||||
SHA_CTX ctx;
|
||||
unsigned char hash[SHA_DIGEST_LENGTH];
|
||||
int cache_changed = 0;
|
||||
int p_len;
|
||||
while (cache_continue) {
|
||||
for (int i = 0; i < FILE_CACHE_SIZE; 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);
|
||||
SHA1_Init(&ctx);
|
||||
file = fopen(cache[i].filename, "rb");
|
||||
compress = mime_is_compressible(cache[i].meta.type);
|
||||
|
||||
int level = NECRONDA_ZLIB_LEVEL;
|
||||
z_stream strm;
|
||||
FILE *comp_file = NULL;
|
||||
if (compress) {
|
||||
sprintf(buf, "%.*s/.necronda-server", cache[i].webroot_len, cache[i].filename);
|
||||
mkdir(buf, 0755);
|
||||
sprintf(buf, "%.*s/.necronda-server/cache", cache[i].webroot_len, cache[i].filename);
|
||||
mkdir(buf, 0700);
|
||||
char *rel_path = cache[i].filename + cache[i].webroot_len + 1;
|
||||
for (int j = 0; j < strlen(rel_path); j++) {
|
||||
char ch = rel_path[j];
|
||||
if (ch == '/') {
|
||||
ch = '_';
|
||||
}
|
||||
buf[j] = ch;
|
||||
}
|
||||
buf[strlen(rel_path)] = 0;
|
||||
p_len = snprintf(filename_comp, sizeof(filename_comp), "%.*s/.necronda-server/cache/%s.z",
|
||||
cache[i].webroot_len, cache[i].filename, buf);
|
||||
if (p_len < 0 || p_len >= sizeof(filename_comp)) {
|
||||
fprintf(stderr, ERR_STR "Unable to open cached file: "
|
||||
"File name for compressed file too long" CLR_STR "\n");
|
||||
goto comp_err;
|
||||
}
|
||||
fprintf(stdout, "[cache] Compressing file %s\n", cache[i].filename);
|
||||
comp_file = fopen(filename_comp, "wb");
|
||||
if (comp_file == NULL) {
|
||||
fprintf(stderr, ERR_STR "Unable to open cached file: %s" CLR_STR "\n", strerror(errno));
|
||||
comp_err:
|
||||
compress = 0;
|
||||
} else {
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
if (deflateInit(&strm, level) != Z_OK) {
|
||||
fprintf(stderr, ERR_STR "Unable to init deflate: %s" CLR_STR "\n", strerror(errno));
|
||||
compress = 0;
|
||||
fclose(comp_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while ((read = fread(buf, 1, sizeof(buf), file)) > 0) {
|
||||
SHA1_Update(&ctx, buf, read);
|
||||
if (compress) {
|
||||
strm.avail_in = read;
|
||||
strm.next_in = (unsigned char *) buf;
|
||||
do {
|
||||
strm.avail_out = sizeof(comp_buf);
|
||||
strm.next_out = (unsigned char *) comp_buf;
|
||||
deflate(&strm, feof(file) ? Z_FINISH : Z_NO_FLUSH);
|
||||
fwrite(comp_buf, 1, sizeof(comp_buf) - strm.avail_out, comp_file);
|
||||
strm.avail_in = 0;
|
||||
} while (strm.avail_out == 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (compress) {
|
||||
deflateEnd(&strm);
|
||||
fclose(comp_file);
|
||||
fprintf(stdout, "[cache] Finished compressing file %s\n", cache[i].filename);
|
||||
strcpy(cache[i].meta.filename_comp, filename_comp);
|
||||
} else {
|
||||
memset(cache[i].meta.filename_comp, 0, sizeof(cache[i].meta.filename_comp));
|
||||
}
|
||||
SHA1_Final(hash, &ctx);
|
||||
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
|
||||
for (int j = 0; j < SHA_DIGEST_LENGTH; j++) {
|
||||
sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]);
|
||||
}
|
||||
fclose(file);
|
||||
fprintf(stdout, "[cache] Finished hashing file %s\n", cache[i].filename);
|
||||
cache[i].is_updating = 0;
|
||||
cache_changed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (cache_changed) {
|
||||
cache_changed = 0;
|
||||
cache_file = fopen("/var/necronda-server/cache", "wb");
|
||||
fwrite(cache, sizeof(cache_entry), FILE_CACHE_SIZE, cache_file);
|
||||
fclose(cache_file);
|
||||
} else {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cache_init() {
|
||||
if (magic_init() != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int shm_id = shmget(SHM_KEY_CACHE, FILE_CACHE_SIZE * sizeof(cache_entry), IPC_CREAT | IPC_EXCL | 0600);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to create shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
|
||||
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
|
||||
if (shm == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach shared memory (ro): %s" CLR_STR "\n", strerror(errno));
|
||||
return -3;
|
||||
}
|
||||
cache = shm;
|
||||
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
return -4;
|
||||
}
|
||||
cache = shm_rw;
|
||||
memset(cache, 0, FILE_CACHE_SIZE * sizeof(cache_entry));
|
||||
shmdt(shm_rw);
|
||||
cache = shm;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
// child
|
||||
if (cache_process() == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return -6;
|
||||
}
|
||||
} else if (pid > 0) {
|
||||
// parent
|
||||
fprintf(stderr, "Started child process with PID %i as cache-updater\n", pid);
|
||||
children[0] = pid;
|
||||
} else {
|
||||
fprintf(stderr, ERR_STR "Unable to create child process: %s" CLR_STR "\n", strerror(errno));
|
||||
return -5;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cache_unload() {
|
||||
int shm_id = shmget(SHM_KEY_CACHE, 0, 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to get shared memory id: %s" CLR_STR "\n", strerror(errno));
|
||||
shmdt(cache);
|
||||
return -1;
|
||||
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to configure shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
shmdt(cache);
|
||||
return -1;
|
||||
}
|
||||
shmdt(cache);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cache_update_entry(int entry_num, const char *filename, const char *webroot) {
|
||||
void *cache_ro = cache;
|
||||
int shm_id = shmget(SHM_KEY_CACHE, 0, 0);
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
print(ERR_STR "Unable to attach shared memory (rw): %s" CLR_STR, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
cache = shm_rw;
|
||||
|
||||
struct stat statbuf;
|
||||
stat(filename, &statbuf);
|
||||
memcpy(&cache[entry_num].meta.stat, &statbuf, sizeof(statbuf));
|
||||
|
||||
cache[entry_num].webroot_len = (unsigned char) strlen(webroot);
|
||||
strcpy(cache[entry_num].filename, filename);
|
||||
|
||||
magic_setflags(magic, MAGIC_MIME_TYPE);
|
||||
const char *type = magic_file(magic, filename);
|
||||
char type_new[24];
|
||||
sprintf(type_new, "%s", type);
|
||||
if (strcmp(type, "text/plain") == 0) {
|
||||
if (strcmp(filename + strlen(filename) - 4, ".css") == 0) {
|
||||
sprintf(type_new, "text/css");
|
||||
} else if (strcmp(filename + strlen(filename) - 3, ".js") == 0) {
|
||||
sprintf(type_new, "text/javascript");
|
||||
}
|
||||
}
|
||||
strcpy(cache[entry_num].meta.type, type_new);
|
||||
|
||||
magic_setflags(magic, MAGIC_MIME_ENCODING);
|
||||
strcpy(cache[entry_num].meta.charset, magic_file(magic, filename));
|
||||
|
||||
memset(cache[entry_num].meta.etag, 0, sizeof(cache[entry_num].meta.etag));
|
||||
memset(cache[entry_num].meta.filename_comp, 0, sizeof(cache[entry_num].meta.filename_comp));
|
||||
cache[entry_num].is_updating = 0;
|
||||
|
||||
shmdt(shm_rw);
|
||||
cache = cache_ro;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cache_filename_comp_invalid(const char *filename) {
|
||||
void *cache_ro = cache;
|
||||
int shm_id = shmget(SHM_KEY_CACHE, 0, 0);
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
print(ERR_STR "Unable to attach shared memory (rw): %s" CLR_STR, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
cache = shm_rw;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < FILE_CACHE_SIZE; i++) {
|
||||
if (cache[i].filename[0] != 0 && strlen(cache[i].filename) == strlen(filename) &&
|
||||
strcmp(cache[i].filename, filename) == 0) {
|
||||
if (cache[i].is_updating) {
|
||||
return 0;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
|
||||
memset(cache[i].meta.filename_comp, 0, sizeof(cache[i].meta.filename_comp));
|
||||
cache[i].is_updating = 0;
|
||||
|
||||
shmdt(shm_rw);
|
||||
cache = cache_ro;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uri_cache_init(http_uri *uri) {
|
||||
if (uri->filename == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < FILE_CACHE_SIZE; i++) {
|
||||
if (cache[i].filename[0] != 0 && strlen(cache[i].filename) == strlen(uri->filename) &&
|
||||
strcmp(cache[i].filename, uri->filename) == 0) {
|
||||
uri->meta = &cache[i].meta;
|
||||
if (cache[i].is_updating) {
|
||||
return 0;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (uri->meta == NULL) {
|
||||
for (i = 0; i < FILE_CACHE_SIZE; i++) {
|
||||
if (cache[i].filename[0] == 0) {
|
||||
if (cache_update_entry(i, uri->filename, uri->webroot) != 0) {
|
||||
return -1;
|
||||
}
|
||||
uri->meta = &cache[i].meta;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
struct stat statbuf;
|
||||
stat(uri->filename, &statbuf);
|
||||
if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) {
|
||||
if (cache_update_entry(i, uri->filename, uri->webroot) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
40
src/lib/cache.h
Normal file
40
src/lib/cache.h
Normal file
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* File cache implementation (header file)
|
||||
* src/lib/cache.h
|
||||
* Lorenz Stechauner, 2020-12-19
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_CACHE_H
|
||||
#define NECRONDA_SERVER_CACHE_H
|
||||
|
||||
#include "uri.h"
|
||||
|
||||
typedef struct {
|
||||
char filename[256];
|
||||
unsigned char webroot_len;
|
||||
unsigned char is_updating:1;
|
||||
meta_data meta;
|
||||
} cache_entry;
|
||||
|
||||
extern cache_entry *cache;
|
||||
|
||||
extern int cache_continue;
|
||||
|
||||
int magic_init();
|
||||
|
||||
void cache_process_term();
|
||||
|
||||
int cache_process();
|
||||
|
||||
int cache_init();
|
||||
|
||||
int cache_unload();
|
||||
|
||||
int cache_update_entry(int entry_num, const char *filename, const char *webroot);
|
||||
|
||||
int cache_filename_comp_invalid(const char *filename);
|
||||
|
||||
int uri_cache_init(http_uri *uri);
|
||||
|
||||
#endif //NECRONDA_SERVER_CACHE_H
|
215
src/lib/config.c
Normal file
215
src/lib/config.c
Normal file
@ -0,0 +1,215 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Configuration file loader
|
||||
* src/lib/config.c
|
||||
* Lorenz Stechauner, 2021-01-05
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "../necronda-server.h"
|
||||
#include <stdio.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
host_config *config;
|
||||
char cert_file[256], key_file[256], geoip_dir[256], dns_server[256];
|
||||
|
||||
int config_init() {
|
||||
int shm_id = shmget(SHM_KEY_CONFIG, MAX_HOST_CONFIG * sizeof(host_config), IPC_CREAT | IPC_EXCL | 0640);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to create shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
|
||||
if (shm == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach shared memory (ro): %s" CLR_STR "\n", strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
config = shm;
|
||||
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
fprintf(stderr, ERR_STR "Unable to attach shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
return -3;
|
||||
}
|
||||
config = shm_rw;
|
||||
memset(config, 0, MAX_HOST_CONFIG * sizeof(host_config));
|
||||
shmdt(shm_rw);
|
||||
config = shm;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_unload() {
|
||||
int shm_id = shmget(SHM_KEY_CONFIG, 0, 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to get shared memory id: %s" CLR_STR "\n", strerror(errno));
|
||||
shmdt(config);
|
||||
return -1;
|
||||
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to configure shared memory: %s" CLR_STR "\n", strerror(errno));
|
||||
shmdt(config);
|
||||
return -1;
|
||||
}
|
||||
shmdt(config);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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));
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
unsigned long len = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
char *conf = malloc(len);
|
||||
fread(conf, 1, len, file);
|
||||
fclose(file);
|
||||
|
||||
host_config *tmp_config = malloc(MAX_HOST_CONFIG * sizeof(host_config));
|
||||
memset(tmp_config, 0, MAX_HOST_CONFIG * sizeof(host_config));
|
||||
|
||||
int i = 0;
|
||||
int mode = 0;
|
||||
char *ptr = NULL;
|
||||
char *source, *target;
|
||||
while ((ptr = strtok(ptr == NULL ? conf : NULL, "\n")) != NULL) {
|
||||
char *comment = strchr(ptr, '#');
|
||||
if (comment != NULL) comment[0] = 0;
|
||||
len = strlen(ptr);
|
||||
if (ptr[0] == '[') {
|
||||
if (ptr[len - 1] != ']') goto err;
|
||||
strncpy(tmp_config[i].name, ptr + 1, len - 2);
|
||||
i++;
|
||||
continue;
|
||||
} else if (i == 0) {
|
||||
if (len > 12 && strncmp(ptr, "certificate", 11) == 0 && (ptr[11] == ' ' || ptr[11] == '\t')) {
|
||||
source = ptr + 11;
|
||||
target = cert_file;
|
||||
} else if (len > 12 && strncmp(ptr, "private_key", 11) == 0 && (ptr[11] == ' ' || ptr[11] == '\t')) {
|
||||
source = ptr + 11;
|
||||
target = key_file;
|
||||
} else if (len > 10 && strncmp(ptr, "geoip_dir", 9) == 0 && (ptr[9] == ' ' || ptr[9] == '\t')) {
|
||||
source = ptr + 9;
|
||||
target = geoip_dir;
|
||||
} else if (len > 11 && strncmp(ptr, "dns_server", 10) == 0 && (ptr[10] == ' ' || ptr[10] == '\t')) {
|
||||
source = ptr + 10;
|
||||
target = dns_server;
|
||||
}
|
||||
} else {
|
||||
host_config *hc = &tmp_config[i - 1];
|
||||
if (len > 8 && strncmp(ptr, "webroot", 7) == 0 && (ptr[7] == ' ' || ptr[7] == '\t')) {
|
||||
source = ptr + 7;
|
||||
target = hc->local.webroot;
|
||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_LOCAL) {
|
||||
goto err;
|
||||
} else {
|
||||
hc->type = CONFIG_TYPE_LOCAL;
|
||||
}
|
||||
} else if (len > 9 && strncmp(ptr, "dir_mode", 8) == 0 && (ptr[8] == ' ' || ptr[8] == '\t')) {
|
||||
source = ptr + 8;
|
||||
target = NULL;
|
||||
mode = 1;
|
||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_LOCAL) {
|
||||
goto err;
|
||||
} else {
|
||||
hc->type = CONFIG_TYPE_LOCAL;
|
||||
}
|
||||
} else if (len > 9 && strncmp(ptr, "hostname", 8) == 0 && (ptr[8] == ' ' || ptr[8] == '\t')) {
|
||||
source = ptr + 8;
|
||||
target = hc->rev_proxy.hostname;
|
||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_REVERSE_PROXY) {
|
||||
goto err;
|
||||
} else {
|
||||
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
||||
}
|
||||
} else if (len > 5 && strncmp(ptr, "port", 4) == 0 && (ptr[4] == ' ' || ptr[4] == '\t')) {
|
||||
source = ptr + 4;
|
||||
target = NULL;
|
||||
mode = 2;
|
||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_REVERSE_PROXY) {
|
||||
goto err;
|
||||
} else {
|
||||
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
||||
}
|
||||
} else if (strcmp(ptr, "http") == 0) {
|
||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_REVERSE_PROXY) {
|
||||
goto err;
|
||||
} else {
|
||||
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
||||
hc->rev_proxy.enc = 0;
|
||||
}
|
||||
continue;
|
||||
} else if (strcmp(ptr, "https") == 0) {
|
||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_REVERSE_PROXY) {
|
||||
goto err;
|
||||
} else {
|
||||
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
||||
hc->rev_proxy.enc = 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
char *end_ptr = source + strlen(source) - 1;
|
||||
while (source[0] == ' ' || source[0] == '\t') source++;
|
||||
while (end_ptr[0] == ' ' || end_ptr[0] == '\t') end_ptr--;
|
||||
if (end_ptr <= source) {
|
||||
err:
|
||||
free(conf);
|
||||
free(tmp_config);
|
||||
fprintf(stderr, ERR_STR "Unable to parse config file" CLR_STR "\n");
|
||||
return -2;
|
||||
}
|
||||
end_ptr[1] = 0;
|
||||
if (target != NULL) {
|
||||
strcpy(target, source);
|
||||
} else if (mode == 1) {
|
||||
if (strcmp(source, "forbidden") == 0) {
|
||||
tmp_config[i - 1].local.dir_mode = URI_DIR_MODE_FORBIDDEN;
|
||||
} else if (strcmp(source, "info") == 0) {
|
||||
tmp_config[i - 1].local.dir_mode = URI_DIR_MODE_INFO;
|
||||
} else if (strcmp(source, "list") == 0) {
|
||||
tmp_config[i - 1].local.dir_mode = URI_DIR_MODE_LIST;
|
||||
} else {
|
||||
goto err;
|
||||
}
|
||||
} else if (mode == 2) {
|
||||
tmp_config[i - 1].rev_proxy.port = (unsigned short) strtoul(source, NULL, 10);
|
||||
}
|
||||
}
|
||||
free(conf);
|
||||
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (tmp_config[j].type == CONFIG_TYPE_LOCAL) {
|
||||
char *webroot = tmp_config[j].local.webroot;
|
||||
if (webroot[strlen(webroot) - 1] == '/') {
|
||||
webroot[strlen(webroot) - 1] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int shm_id = shmget(SHM_KEY_CONFIG, 0, 0);
|
||||
if (shm_id < 0) {
|
||||
fprintf(stderr, ERR_STR "Unable to get shared memory id: %s" CLR_STR "\n", strerror(errno));
|
||||
shmdt(config);
|
||||
return -3;
|
||||
}
|
||||
|
||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
||||
if (shm_rw == (void *) -1) {
|
||||
free(tmp_config);
|
||||
fprintf(stderr, ERR_STR "Unable to attach shared memory (rw): %s" CLR_STR "\n", strerror(errno));
|
||||
return -4;
|
||||
}
|
||||
memcpy(shm_rw, tmp_config, MAX_HOST_CONFIG * sizeof(host_config));
|
||||
free(tmp_config);
|
||||
shmdt(shm_rw);
|
||||
return 0;
|
||||
}
|
42
src/lib/config.h
Normal file
42
src/lib/config.h
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Configuration file loader (header file)
|
||||
* src/lib/config.h
|
||||
* Lorenz Stechauner, 2021-01-05
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_CONFIG_H
|
||||
#define NECRONDA_SERVER_CONFIG_H
|
||||
|
||||
#include "uri.h"
|
||||
|
||||
#define CONFIG_TYPE_UNSET 0
|
||||
#define CONFIG_TYPE_LOCAL 1
|
||||
#define CONFIG_TYPE_REVERSE_PROXY 2
|
||||
|
||||
typedef struct {
|
||||
int type;
|
||||
char name[256];
|
||||
union {
|
||||
struct {
|
||||
char hostname[256];
|
||||
unsigned short port;
|
||||
unsigned char enc:1;
|
||||
} rev_proxy;
|
||||
struct {
|
||||
char webroot[256];
|
||||
unsigned char dir_mode:2;
|
||||
} local;
|
||||
};
|
||||
} host_config;
|
||||
|
||||
extern host_config *config;
|
||||
extern char cert_file[256], key_file[256], geoip_dir[256], dns_server[256];
|
||||
|
||||
int config_init();
|
||||
|
||||
int config_load(const char *filename);
|
||||
|
||||
int config_unload();
|
||||
|
||||
#endif //NECRONDA_SERVER_CONFIG_H
|
539
src/lib/fastcgi.c
Normal file
539
src/lib/fastcgi.c
Normal file
@ -0,0 +1,539 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* FastCGI interface implementation
|
||||
* src/lib/fastcgi.c
|
||||
* Lorenz Stechauner, 2020-12-26
|
||||
*/
|
||||
|
||||
#include "fastcgi.h"
|
||||
#include "utils.h"
|
||||
#include "../client.h"
|
||||
#include "../necronda-server.h"
|
||||
#include <sys/un.h>
|
||||
#include <zlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
char *fastcgi_add_param(char *buf, const char *key, const char *value) {
|
||||
char *ptr = buf;
|
||||
unsigned long key_len = strlen(key);
|
||||
unsigned long val_len = strlen(value);
|
||||
|
||||
|
||||
if (key_len <= 127) {
|
||||
ptr[0] = (char) (key_len & 0x7F);
|
||||
ptr++;
|
||||
} else {
|
||||
ptr[0] = (char) (0x80 | (key_len >> 24));
|
||||
ptr[1] = (char) ((key_len >> 16) & 0xFF);
|
||||
ptr[2] = (char) ((key_len >> 8) & 0xFF);
|
||||
ptr[3] = (char) (key_len & 0xFF);
|
||||
ptr += 4;
|
||||
}
|
||||
if (val_len <= 127) {
|
||||
ptr[0] = (char) (val_len & 0x7F);
|
||||
ptr++;
|
||||
} else {
|
||||
ptr[0] = (char) (0x80 | (val_len >> 24));
|
||||
ptr[1] = (char) ((val_len >> 16) & 0xFF);
|
||||
ptr[2] = (char) ((val_len >> 8) & 0xFF);
|
||||
ptr[3] = (char) (val_len & 0xFF);
|
||||
ptr += 4;
|
||||
}
|
||||
|
||||
memcpy(ptr, key, key_len);
|
||||
ptr += key_len;
|
||||
memcpy(ptr, value, val_len);
|
||||
ptr += val_len;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
int fastcgi_init(fastcgi_conn *conn, 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) {
|
||||
req_id |= (req_num + 1) & 0xF;
|
||||
} else {
|
||||
req_id |= req_num & 0xF;
|
||||
}
|
||||
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) {
|
||||
print(ERR_STR "Unable to create unix socket: %s" CLR_STR, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
conn->socket = php_fpm;
|
||||
|
||||
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));
|
||||
return -1;
|
||||
}
|
||||
|
||||
FCGI_Header header = {
|
||||
.version = FCGI_VERSION_1,
|
||||
.requestIdB1 = req_id >> 8,
|
||||
.requestIdB0 = req_id & 0xFF,
|
||||
.paddingLength = 0,
|
||||
.reserved = 0
|
||||
};
|
||||
|
||||
header.type = FCGI_BEGIN_REQUEST;
|
||||
header.contentLengthB1 = 0;
|
||||
header.contentLengthB0 = sizeof(FCGI_BeginRequestBody);
|
||||
FCGI_BeginRequestRecord begin = {
|
||||
header,
|
||||
{.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));
|
||||
return -2;
|
||||
}
|
||||
|
||||
char param_buf[4096];
|
||||
char buf0[256];
|
||||
char *param_ptr = param_buf + sizeof(header);
|
||||
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REDIRECT_STATUS", "CGI");
|
||||
param_ptr = fastcgi_add_param(param_ptr, "DOCUMENT_ROOT", uri->webroot);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "GATEWAY_INTERFACE", "CGI/1.1");
|
||||
param_ptr = fastcgi_add_param(param_ptr, "SERVER_SOFTWARE", SERVER_STR);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "SERVER_PROTOCOL", "HTTP/1.1");
|
||||
param_ptr = fastcgi_add_param(param_ptr, "SERVER_NAME", http_get_header_field(&req->hdr, "Host"));
|
||||
if (client->enc) {
|
||||
param_ptr = fastcgi_add_param(param_ptr, "HTTPS", "on");
|
||||
}
|
||||
|
||||
struct sockaddr_storage addr_storage;
|
||||
struct sockaddr_in6 *addr;
|
||||
socklen_t len = sizeof(addr_storage);
|
||||
getsockname(client->socket, (struct sockaddr *) &addr_storage, &len);
|
||||
addr = (struct sockaddr_in6 *) &addr_storage;
|
||||
sprintf(buf0, "%i", addr->sin6_port);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "SERVER_PORT", buf0);
|
||||
|
||||
len = sizeof(addr_storage);
|
||||
getpeername(client->socket, (struct sockaddr *) &addr_storage, &len);
|
||||
addr = (struct sockaddr_in6 *) &addr_storage;
|
||||
sprintf(buf0, "%i", addr->sin6_port);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_PORT", buf0);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_ADDR", client_addr_str);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_HOST", client_host_str != NULL ? client_host_str : client_addr_str);
|
||||
//param_ptr = fastcgi_add_param(param_ptr, "REMOTE_IDENT", "");
|
||||
//param_ptr = fastcgi_add_param(param_ptr, "REMOTE_USER", "");
|
||||
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REQUEST_METHOD", req->method);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REQUEST_URI", req->uri);
|
||||
param_ptr = fastcgi_add_param(param_ptr, "SCRIPT_NAME", uri->filename + strlen(uri->webroot));
|
||||
param_ptr = fastcgi_add_param(param_ptr, "SCRIPT_FILENAME", uri->filename);
|
||||
//param_ptr = fastcgi_add_param(param_ptr, "PATH_TRANSLATED", uri->filename);
|
||||
|
||||
param_ptr = fastcgi_add_param(param_ptr, "QUERY_STRING", uri->query != NULL ? uri->query : "");
|
||||
if (uri->pathinfo != NULL && strlen(uri->pathinfo) > 0) {
|
||||
sprintf(buf0, "/%s", uri->pathinfo);
|
||||
} else {
|
||||
buf0[0] = 0;
|
||||
}
|
||||
param_ptr = fastcgi_add_param(param_ptr, "PATH_INFO", buf0);
|
||||
|
||||
//param_ptr = fastcgi_add_param(param_ptr, "AUTH_TYPE", "");
|
||||
char *content_length = http_get_header_field(&req->hdr, "Content-Length");
|
||||
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_LENGTH", content_length != NULL ? content_length : "");
|
||||
char *content_type = http_get_header_field(&req->hdr, "Content-Type");
|
||||
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_TYPE", content_type != NULL ? content_type : "");
|
||||
if (client_geoip != NULL) {
|
||||
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_INFO", client_geoip);
|
||||
}
|
||||
|
||||
for (int i = 0; i < req->hdr.field_num; i++) {
|
||||
char *ptr = buf0;
|
||||
ptr += sprintf(ptr, "HTTP_");
|
||||
for (int j = 0; j < strlen(req->hdr.fields[i][0]); j++, ptr++) {
|
||||
char ch = req->hdr.fields[i][0][j];
|
||||
if ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
|
||||
ch = ch;
|
||||
} else if (ch >= 'a' && ch <= 'z') {
|
||||
ch &= 0x5F;
|
||||
} else {
|
||||
ch = '_';
|
||||
}
|
||||
ptr[0] = ch;
|
||||
ptr[1] = 0;
|
||||
}
|
||||
param_ptr = fastcgi_add_param(param_ptr, buf0, req->hdr.fields[i][1]);
|
||||
}
|
||||
|
||||
unsigned short param_len = param_ptr - param_buf - sizeof(header);
|
||||
header.type = FCGI_PARAMS;
|
||||
header.contentLengthB1 = param_len >> 8;
|
||||
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));
|
||||
return -2;
|
||||
}
|
||||
|
||||
header.type = FCGI_PARAMS;
|
||||
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));
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fastcgi_close_stdin(fastcgi_conn *conn) {
|
||||
FCGI_Header header = {
|
||||
.version = FCGI_VERSION_1,
|
||||
.type = FCGI_STDIN,
|
||||
.requestIdB1 = conn->req_id >> 8,
|
||||
.requestIdB0 = conn->req_id & 0xFF,
|
||||
.contentLengthB1 = 0,
|
||||
.contentLengthB0 = 0,
|
||||
.paddingLength = 0,
|
||||
.reserved = 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));
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fastcgi_php_error(const char *msg, int msg_len, char *err_msg) {
|
||||
char *msg_str = malloc(msg_len + 1);
|
||||
char *ptr0 = msg_str;
|
||||
strncpy(msg_str, msg, msg_len);
|
||||
char *ptr1 = NULL;
|
||||
int len;
|
||||
int err = 0;
|
||||
// FIXME *msg is part of a stream, handle fragmented lines
|
||||
while (1) {
|
||||
int msg_type = 0;
|
||||
int msg_pre_len = 0;
|
||||
ptr1 = strstr(ptr0, "PHP message: ");
|
||||
if (ptr1 == NULL) {
|
||||
len = (int) (msg_len - (ptr0 - msg_str));
|
||||
if (ptr0 == msg_str) msg_type = 2;
|
||||
} else {
|
||||
len = (int) (ptr1 - ptr0);
|
||||
}
|
||||
if (len == 0) {
|
||||
goto next;
|
||||
}
|
||||
|
||||
if (len >= 14 && strncmp(ptr0, "PHP Warning: ", 14) == 0) {
|
||||
msg_type = 1;
|
||||
msg_pre_len = 14;
|
||||
} else if (len >= 18 && strncmp(ptr0, "PHP Fatal error: ", 18) == 0) {
|
||||
msg_type = 2;
|
||||
msg_pre_len = 18;
|
||||
} else if (len >= 18 && strncmp(ptr0, "PHP Parse error: ", 18) == 0) {
|
||||
msg_type = 2;
|
||||
msg_pre_len = 18;
|
||||
} else if (len >= 18 && strncmp(ptr0, "PHP Notice: ", 13) == 0) {
|
||||
msg_type = 1;
|
||||
msg_pre_len = 13;
|
||||
}
|
||||
|
||||
char *ptr2 = ptr0;
|
||||
char *ptr3;
|
||||
int len2;
|
||||
while (ptr2 - ptr0 < len) {
|
||||
ptr3 = strchr(ptr2, '\n');
|
||||
len2 = (int) (len - (ptr2 - ptr0));
|
||||
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 : "");
|
||||
if (msg_type == 2 && ptr2 == ptr0) {
|
||||
sprintf(err_msg, "%.*s", len2, ptr2);
|
||||
err = 1;
|
||||
}
|
||||
if (ptr3 == NULL) {
|
||||
break;
|
||||
}
|
||||
ptr2 = ptr3 + 1;
|
||||
}
|
||||
|
||||
next:
|
||||
if (ptr1 == NULL) {
|
||||
break;
|
||||
}
|
||||
ptr0 = ptr1 + 13;
|
||||
}
|
||||
free(msg_str);
|
||||
return err;
|
||||
}
|
||||
|
||||
int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
|
||||
FCGI_Header header;
|
||||
char *content;
|
||||
unsigned short content_len, req_id;
|
||||
int ret;
|
||||
int err = 0;
|
||||
|
||||
while (1) {
|
||||
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));
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
||||
content = malloc(content_len + header.paddingLength);
|
||||
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));
|
||||
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);
|
||||
free(content);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (req_id != conn->req_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (header.type == FCGI_END_REQUEST) {
|
||||
FCGI_EndRequestBody *body = (FCGI_EndRequestBody *) content;
|
||||
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);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "Script 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);
|
||||
} else if (header.type == FCGI_STDOUT) {
|
||||
break;
|
||||
} else {
|
||||
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
|
||||
}
|
||||
|
||||
free(content);
|
||||
}
|
||||
if (err) {
|
||||
res->status = http_get_status(500);
|
||||
return 2;
|
||||
}
|
||||
|
||||
conn->out_buf = content;
|
||||
conn->out_len = content_len;
|
||||
conn->out_off = (unsigned short) (strstr(content, "\r\n\r\n") - content + 4);
|
||||
|
||||
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);
|
||||
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);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
char *ptr = buf;
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = http_parse_header_field(&res->hdr, ptr, pos0);
|
||||
if (ret != 0) return ret;
|
||||
if (pos0[2] == '\r' && pos0[3] == '\n') {
|
||||
return 0;
|
||||
}
|
||||
ptr = pos0 + 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
||||
FCGI_Header header;
|
||||
int ret;
|
||||
char buf0[256];
|
||||
int len;
|
||||
char *content, *ptr;
|
||||
unsigned short req_id, content_len;
|
||||
char comp_out[4096];
|
||||
int finish_comp = 0;
|
||||
|
||||
z_stream strm;
|
||||
if (flags & FASTCGI_COMPRESS) {
|
||||
int level = NECRONDA_ZLIB_LEVEL;
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
if (deflateInit(&strm, level) != Z_OK) {
|
||||
print(ERR_STR "Unable to init deflate: %s" CLR_STR, strerror(errno));
|
||||
flags &= !FASTCGI_COMPRESS;
|
||||
}
|
||||
}
|
||||
|
||||
if (conn->out_buf != NULL && conn->out_len > conn->out_off) {
|
||||
content = conn->out_buf;
|
||||
ptr = content + conn->out_off;
|
||||
content_len = conn->out_len - conn->out_off;
|
||||
goto out;
|
||||
}
|
||||
|
||||
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));
|
||||
return -1;
|
||||
} else if (ret != sizeof(header)) {
|
||||
print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR);
|
||||
return -1;
|
||||
}
|
||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
||||
content = malloc(content_len + header.paddingLength);
|
||||
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));
|
||||
free(content);
|
||||
return -1;
|
||||
} else if (ret != (content_len + header.paddingLength)) {
|
||||
print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR);
|
||||
free(content);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (header.type == FCGI_END_REQUEST) {
|
||||
FCGI_EndRequestBody *body = (FCGI_EndRequestBody *) content;
|
||||
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);
|
||||
}
|
||||
if (app_status != 0) {
|
||||
print(ERR_STR "Script terminated with exit code %i" CLR_STR, app_status);
|
||||
}
|
||||
close(conn->socket);
|
||||
conn->socket = 0;
|
||||
free(content);
|
||||
|
||||
if (flags & FASTCGI_COMPRESS) {
|
||||
finish_comp = 1;
|
||||
content_len = 0;
|
||||
goto out;
|
||||
finish:
|
||||
deflateEnd(&strm);
|
||||
}
|
||||
|
||||
if (flags & FASTCGI_CHUNKED) {
|
||||
sock_send(client, "0\r\n\r\n", 5, 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} else if (header.type == FCGI_STDERR) {
|
||||
fastcgi_php_error(content, content_len, buf0);
|
||||
} else if (header.type == FCGI_STDOUT) {
|
||||
out:
|
||||
if (flags & FASTCGI_COMPRESS) {
|
||||
strm.avail_in = content_len;
|
||||
strm.next_in = (unsigned char *) ptr;
|
||||
}
|
||||
do {
|
||||
int buf_len = content_len;
|
||||
if (flags & FASTCGI_COMPRESS) {
|
||||
strm.avail_out = sizeof(comp_out);
|
||||
strm.next_out = (unsigned char *) comp_out;
|
||||
deflate(&strm, finish_comp ? Z_FINISH : Z_NO_FLUSH);
|
||||
strm.avail_in = 0;
|
||||
ptr = comp_out;
|
||||
buf_len = (int) (sizeof(comp_out) - strm.avail_out);
|
||||
}
|
||||
if (buf_len != 0) {
|
||||
len = sprintf(buf0, "%X\r\n", buf_len);
|
||||
if (flags & FASTCGI_CHUNKED) sock_send(client, buf0, len, 0);
|
||||
sock_send(client, ptr, buf_len, 0);
|
||||
if (flags & FASTCGI_CHUNKED) sock_send(client, "\r\n", 2, 0);
|
||||
}
|
||||
} while ((flags & FASTCGI_COMPRESS) && strm.avail_out == 0);
|
||||
if (finish_comp) goto finish;
|
||||
} else {
|
||||
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
|
||||
}
|
||||
free(content);
|
||||
}
|
||||
}
|
||||
|
||||
int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
|
||||
unsigned long rcv_len = 0;
|
||||
char *buf[16384];
|
||||
long ret;
|
||||
FCGI_Header header = {
|
||||
.version = FCGI_VERSION_1,
|
||||
.type = FCGI_STDIN,
|
||||
.requestIdB1 = conn->req_id >> 8,
|
||||
.requestIdB0 = conn->req_id & 0xFF,
|
||||
.contentLengthB1 = 0,
|
||||
.contentLengthB0 = 0,
|
||||
.paddingLength = 0,
|
||||
.reserved = 0
|
||||
};
|
||||
|
||||
if (client->buf != NULL && client->buf_len - client->buf_off > 0) {
|
||||
ret = (int) (client->buf_len - client->buf_off);
|
||||
memcpy(buf, client->buf + client->buf_off, ret);
|
||||
goto send;
|
||||
}
|
||||
|
||||
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));
|
||||
return -1;
|
||||
}
|
||||
|
||||
send:
|
||||
rcv_len += ret;
|
||||
header.contentLengthB1 = (ret >> 8) & 0xFF;
|
||||
header.contentLengthB0 = ret & 0xFF;
|
||||
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));
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
41
src/lib/fastcgi.h
Normal file
41
src/lib/fastcgi.h
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* FastCGI interface implementation (header file)
|
||||
* src/lib/fastcgi.h
|
||||
* Lorenz Stechauner, 2020-12-26
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_FASTCGI_H
|
||||
#define NECRONDA_SERVER_FASTCGI_H
|
||||
|
||||
#include "include/fastcgi.h"
|
||||
#include "http.h"
|
||||
#include "uri.h"
|
||||
|
||||
#define FASTCGI_CHUNKED 1
|
||||
#define FASTCGI_COMPRESS 2
|
||||
|
||||
typedef struct {
|
||||
int socket;
|
||||
unsigned short req_id;
|
||||
char *out_buf;
|
||||
unsigned short out_len;
|
||||
unsigned short out_off;
|
||||
} fastcgi_conn;
|
||||
|
||||
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,
|
||||
const http_req *req, const http_uri *uri);
|
||||
|
||||
int fastcgi_close_stdin(fastcgi_conn *conn);
|
||||
|
||||
int fastcgi_php_error(const char *msg, int msg_len, char *err_msg);
|
||||
|
||||
int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg);
|
||||
|
||||
int fastcgi_send(fastcgi_conn *conn, sock *client, int flags);
|
||||
|
||||
int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len);
|
||||
|
||||
#endif //NECRONDA_SERVER_FASTCGI_H
|
314
src/lib/http.c
Normal file
314
src/lib/http.c
Normal file
@ -0,0 +1,314 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* HTTP implementation
|
||||
* src/lib/http.c
|
||||
* Lorenz Stechauner, 2020-12-09
|
||||
*/
|
||||
|
||||
#include "http.h"
|
||||
#include "utils.h"
|
||||
#include "../necronda-server.h"
|
||||
#include <string.h>
|
||||
|
||||
void http_to_camel_case(char *str, int mode) {
|
||||
char last = '-';
|
||||
char ch;
|
||||
for (int i = 0; i < strlen(str); i++) {
|
||||
ch = str[i];
|
||||
if (mode == HTTP_CAMEL && last == '-' && ch >= 'a' && ch <= 'z') {
|
||||
str[i] = (char) ((int) ch & 0x5F);
|
||||
} else if (mode == HTTP_LOWER && ch >= 'A' && ch <= 'Z') {
|
||||
str[i] = (char) ((int) ch | 0x20);
|
||||
}
|
||||
last = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
void http_free_hdr(http_hdr *hdr) {
|
||||
for (int i = 0; i < hdr->field_num; i++) {
|
||||
free(hdr->fields[i][0]);
|
||||
free(hdr->fields[i][1]);
|
||||
}
|
||||
hdr->field_num = 0;
|
||||
}
|
||||
|
||||
void http_free_req(http_req *req) {
|
||||
if (req->uri == NULL) free(req->uri);
|
||||
req->uri = NULL;
|
||||
http_free_hdr(&req->hdr);
|
||||
}
|
||||
|
||||
void http_free_res(http_res *res) {
|
||||
http_free_hdr(&res->hdr);
|
||||
}
|
||||
|
||||
int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr) {
|
||||
char *pos1 = memchr(buf, ':', end_ptr - buf);
|
||||
char *pos2;
|
||||
if (pos1 == NULL) {
|
||||
print(ERR_STR "Unable to parse header" CLR_STR);
|
||||
return 3;
|
||||
}
|
||||
|
||||
long len = pos1 - buf;
|
||||
hdr->fields[(int) hdr->field_num][0] = malloc(len + 1);
|
||||
sprintf(hdr->fields[(int) hdr->field_num][0], "%.*s", (int) len, buf);
|
||||
http_to_camel_case(hdr->fields[(int) hdr->field_num][0], HTTP_CAMEL);
|
||||
|
||||
pos1++;
|
||||
pos2 = (char *) end_ptr - 1;
|
||||
while (pos1[0] == ' ') pos1++;
|
||||
while (pos2[0] == ' ') pos2--;
|
||||
len = pos2 - pos1 + 1;
|
||||
|
||||
if (len <= 0) {
|
||||
hdr->fields[(int) hdr->field_num][1] = malloc(1);
|
||||
hdr->fields[(int) hdr->field_num][1][0] = 0;
|
||||
} else {
|
||||
hdr->fields[(int) hdr->field_num][1] = malloc(len + 1);
|
||||
sprintf(hdr->fields[(int) hdr->field_num][1], "%.*s", (int) len, pos1);
|
||||
}
|
||||
hdr->field_num++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_receive_request(sock *client, http_req *req) {
|
||||
long rcv_len, len;
|
||||
char *ptr, *pos0, *pos1, *pos2;
|
||||
char buf[CLIENT_MAX_HEADER_SIZE];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
memset(req->method, 0, sizeof(req->method));
|
||||
memset(req->version, 0, sizeof(req->version));
|
||||
req->uri = NULL;
|
||||
req->hdr.field_num = 0;
|
||||
|
||||
while (1) {
|
||||
rcv_len = sock_recv(client, buf, CLIENT_MAX_HEADER_SIZE, 0);
|
||||
if (rcv_len <= 0) {
|
||||
print("Unable to receive: %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 header: End of header not found" CLR_STR);
|
||||
return 5;
|
||||
}
|
||||
|
||||
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);
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
ptr = buf;
|
||||
while (header_len > (ptr - buf + 2)) {
|
||||
pos0 = strstr(ptr, "\r\n");
|
||||
if (pos0 == NULL) {
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (req->version[0] == 0) {
|
||||
pos1 = memchr(ptr, ' ', rcv_len - (ptr - buf)) + 1;
|
||||
if (pos1 == NULL) goto err_hdr_fmt;
|
||||
|
||||
if (pos1 - ptr - 1 >= sizeof(req->method)) {
|
||||
print(ERR_STR "Unable to parse header: Method name too long" CLR_STR);
|
||||
return 2;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (pos1 - ptr - 1); i++) {
|
||||
if (ptr[i] < 'A' || ptr[i] > 'Z') {
|
||||
print(ERR_STR "Unable to parse header: Invalid method" CLR_STR);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
strncpy(req->method, ptr, pos1 - ptr - 1);
|
||||
|
||||
pos2 = memchr(pos1, ' ', rcv_len - (pos1 - buf)) + 1;
|
||||
if (pos2 == NULL) {
|
||||
err_hdr_fmt:
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (memcmp(pos2, "HTTP/", 5) != 0 || memcmp(pos2 + 8, "\r\n", 2) != 0) {
|
||||
print(ERR_STR "Unable to parse header: Invalid version" CLR_STR);
|
||||
return 3;
|
||||
}
|
||||
|
||||
len = pos2 - pos1 - 1;
|
||||
req->uri = malloc(len + 1);
|
||||
sprintf(req->uri, "%.*s", (int) len, pos1);
|
||||
sprintf(req->version, "%.3s", pos2 + 5);
|
||||
} else {
|
||||
int ret = http_parse_header_field(&req->hdr, ptr, pos0);
|
||||
if (ret != 0) return ret;
|
||||
}
|
||||
ptr = pos0 + 2;
|
||||
}
|
||||
if (pos0[2] == '\r' && pos0[3] == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
client->buf_len = rcv_len - (pos0 - buf + 4);
|
||||
if (client->buf_len > 0) {
|
||||
client->buf = malloc(client->buf_len);
|
||||
client->buf_off = 0;
|
||||
memcpy(client->buf, pos0 + 4, client->buf_len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *http_get_header_field(const http_hdr *hdr, const char *field_name) {
|
||||
char field_name_1[256], field_name_2[256];
|
||||
strcpy(field_name_1, field_name);
|
||||
http_to_camel_case(field_name_1, HTTP_LOWER);
|
||||
for (int i = 0; i < hdr->field_num; i++) {
|
||||
strcpy(field_name_2, hdr->fields[i][0]);
|
||||
http_to_camel_case(field_name_2, HTTP_LOWER);
|
||||
if (strcmp(field_name_1, field_name_2) == 0) {
|
||||
return hdr->fields[i][1];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void http_add_header_field(http_hdr *hdr, const char *field_name, const char *field_value) {
|
||||
size_t len_name = strlen(field_name);
|
||||
size_t len_value = strlen(field_value);
|
||||
char *_field_name = malloc(len_name + 1);
|
||||
char *_field_value = malloc(len_value + 1);
|
||||
strcpy(_field_name, field_name);
|
||||
strcpy(_field_value, field_value);
|
||||
http_to_camel_case(_field_name, HTTP_PRESERVE);
|
||||
hdr->fields[(int) hdr->field_num][0] = _field_name;
|
||||
hdr->fields[(int) hdr->field_num][1] = _field_value;
|
||||
hdr->field_num++;
|
||||
}
|
||||
|
||||
void http_remove_header_field(http_hdr *hdr, const char *field_name, int mode) {
|
||||
char field_name_1[256], field_name_2[256];
|
||||
strcpy(field_name_1, field_name);
|
||||
http_to_camel_case(field_name_1, HTTP_LOWER);
|
||||
|
||||
int i = 0;
|
||||
int diff = 1;
|
||||
if (mode == HTTP_REMOVE_LAST) {
|
||||
i = hdr->field_num - 1;
|
||||
diff = -1;
|
||||
}
|
||||
for (; i < hdr->field_num && i >= 0; i += diff) {
|
||||
strcpy(field_name_2, hdr->fields[i][0]);
|
||||
http_to_camel_case(field_name_2, HTTP_LOWER);
|
||||
if (strcmp(field_name_1, field_name_2) == 0) {
|
||||
for (int j = i; j < hdr->field_num - 1; j++) {
|
||||
memcpy(hdr->fields[j], hdr->fields[j + 1], sizeof(hdr->fields[0]));
|
||||
}
|
||||
hdr->field_num--;
|
||||
if (mode == HTTP_REMOVE_ALL) {
|
||||
i -= diff;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int http_send_response(sock *client, http_res *res) {
|
||||
char buf[CLIENT_MAX_HEADER_SIZE];
|
||||
long off = sprintf(buf, "HTTP/%s %03i %s\r\n", res->version, res->status->code, res->status->msg);
|
||||
for (int i = 0; i < res->hdr.field_num; i++) {
|
||||
off += sprintf(buf + off, "%s: %s\r\n", res->hdr.fields[i][0], res->hdr.fields[i][1]);
|
||||
}
|
||||
off += sprintf(buf + off, "\r\n");
|
||||
if (sock_send(client, buf, off, 0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_send_request(sock *server, http_req *req) {
|
||||
char buf[CLIENT_MAX_HEADER_SIZE];
|
||||
long off = sprintf(buf, "%s %s HTTP/%s\r\n", req->method, req->uri, req->version);
|
||||
for (int i = 0; i < req->hdr.field_num; i++) {
|
||||
off += sprintf(buf + off, "%s: %s\r\n", req->hdr.fields[i][0], req->hdr.fields[i][1]);
|
||||
}
|
||||
off += sprintf(buf + off, "\r\n");
|
||||
long ret = sock_send(server, buf, off, 0);
|
||||
if (ret <= 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const http_status *http_get_status(unsigned short status_code) {
|
||||
for (int i = 0; i < http_statuses_size / sizeof(http_status); i++) {
|
||||
if (http_statuses[i].code == status_code) {
|
||||
return &http_statuses[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const http_status_msg *http_get_error_msg(const http_status *status) {
|
||||
unsigned short code = status->code;
|
||||
for (int i = 0; i < http_status_messages_size / sizeof(http_status_msg); i++) {
|
||||
if (http_status_messages[i].code == code) {
|
||||
return &http_status_messages[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *http_get_status_color(const http_status *status) {
|
||||
unsigned short code = status->code;
|
||||
if (code >= 100 && code < 200) {
|
||||
return HTTP_1XX_STR;
|
||||
} else if ((code >= 200 && code < 300) || code == 304) {
|
||||
return HTTP_2XX_STR;
|
||||
} else if (code >= 300 && code < 400) {
|
||||
return HTTP_3XX_STR;
|
||||
} else if (code >= 400 && code < 500) {
|
||||
return HTTP_4XX_STR;
|
||||
} else if (code >= 500 && code < 600) {
|
||||
return HTTP_5XX_STR;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *http_format_date(time_t time, char *buf, size_t size) {
|
||||
struct tm *timeinfo = gmtime(&time);
|
||||
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", timeinfo);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *http_get_date(char *buf, size_t size) {
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
return http_format_date(rawtime, buf, size);
|
||||
}
|
||||
|
||||
const http_doc_info *http_get_status_info(const http_status *status) {
|
||||
unsigned short code = status->code;
|
||||
static http_doc_info info[] = {
|
||||
{"info", HTTP_COLOR_INFO, http_info_icon, http_info_document},
|
||||
{"success", HTTP_COLOR_SUCCESS, http_success_icon, http_success_document},
|
||||
{"warning", HTTP_COLOR_WARNING, http_warning_icon, http_warning_document},
|
||||
{"error", HTTP_COLOR_ERROR, http_error_icon, http_error_document}
|
||||
};
|
||||
if (code >= 100 && code < 200) {
|
||||
return &info[0];
|
||||
} else if ((code >= 200 && code < 300) || code == 304) {
|
||||
return &info[1];
|
||||
} else if (code >= 300 && code < 400) {
|
||||
return &info[2];
|
||||
} else if (code >= 400 && code < 600) {
|
||||
return &info[3];
|
||||
}
|
||||
return NULL;
|
||||
}
|
111
src/lib/http.h
Normal file
111
src/lib/http.h
Normal file
@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* HTTP implementation (header file)
|
||||
* src/lib/http.h
|
||||
* Lorenz Stechauner, 2020-12-09
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_HTTP_H
|
||||
#define NECRONDA_SERVER_HTTP_H
|
||||
|
||||
#include "sock.h"
|
||||
|
||||
#define HTTP_PRESERVE 0
|
||||
#define HTTP_LOWER 1
|
||||
#define HTTP_CAMEL 2
|
||||
|
||||
#define HTTP_REMOVE_ONE 0
|
||||
#define HTTP_REMOVE_ALL 1
|
||||
#define HTTP_REMOVE_LAST 2
|
||||
|
||||
#define HTTP_COLOR_SUCCESS "#008000"
|
||||
#define HTTP_COLOR_INFO "#606060"
|
||||
#define HTTP_COLOR_WARNING "#E0C000"
|
||||
#define HTTP_COLOR_ERROR "#C00000"
|
||||
|
||||
typedef struct {
|
||||
unsigned short code;
|
||||
char type[16];
|
||||
char msg[32];
|
||||
} http_status;
|
||||
|
||||
typedef struct {
|
||||
unsigned short code;
|
||||
const char *msg;
|
||||
} http_status_msg;
|
||||
|
||||
typedef struct {
|
||||
char mode[8];
|
||||
char color[8];
|
||||
const char *icon;
|
||||
const char *doc;
|
||||
} http_doc_info;
|
||||
|
||||
typedef struct {
|
||||
char field_num;
|
||||
char *fields[64][2];
|
||||
} http_hdr;
|
||||
|
||||
typedef struct {
|
||||
char method[16];
|
||||
char *uri;
|
||||
char version[3];
|
||||
http_hdr hdr;
|
||||
} http_req;
|
||||
|
||||
typedef struct {
|
||||
const http_status *status;
|
||||
char version[3];
|
||||
http_hdr hdr;
|
||||
} http_res;
|
||||
|
||||
extern const http_status http_statuses[];
|
||||
extern const http_status_msg http_status_messages[];
|
||||
extern const int http_statuses_size;
|
||||
extern const int http_status_messages_size;
|
||||
|
||||
extern const char http_default_document[];
|
||||
extern const char http_error_document[];
|
||||
extern const char http_error_icon[];
|
||||
extern const char http_warning_document[];
|
||||
extern const char http_warning_icon[];
|
||||
extern const char http_success_document[];
|
||||
extern const char http_success_icon[];
|
||||
extern const char http_info_document[];
|
||||
extern const char http_info_icon[];
|
||||
|
||||
void http_to_camel_case(char *str, int mode);
|
||||
|
||||
void http_free_hdr(http_hdr *hdr);
|
||||
|
||||
void http_free_req(http_req *req);
|
||||
|
||||
void http_free_res(http_res *res);
|
||||
|
||||
int http_receive_request(sock *client, http_req *req);
|
||||
|
||||
int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr) ;
|
||||
|
||||
char *http_get_header_field(const http_hdr *hdr, const char *field_name);
|
||||
|
||||
void http_add_header_field(http_hdr *hdr, const char *field_name, const char *field_value);
|
||||
|
||||
void http_remove_header_field(http_hdr *hdr, const char *field_name, int mode);
|
||||
|
||||
int http_send_response(sock *client, http_res *res);
|
||||
|
||||
int http_send_request(sock *server, http_req *req);
|
||||
|
||||
const http_status *http_get_status(unsigned short status_code);
|
||||
|
||||
const http_status_msg *http_get_error_msg(const http_status *status);
|
||||
|
||||
const char *http_get_status_color(const http_status *status);
|
||||
|
||||
char *http_format_date(time_t time, char *buf, size_t size);
|
||||
|
||||
char *http_get_date(char *buf, size_t size);
|
||||
|
||||
const http_doc_info *http_get_status_info(const http_status *status);
|
||||
|
||||
#endif //NECRONDA_SERVER_HTTP_H
|
198
src/lib/http_static.c
Normal file
198
src/lib/http_static.c
Normal file
@ -0,0 +1,198 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* HTTP static implementation
|
||||
* src/lib/http_static.c
|
||||
* Lorenz Stechauner, 2021-05-03
|
||||
*/
|
||||
|
||||
#include "http.h"
|
||||
#include "../necronda-server.h"
|
||||
|
||||
const http_status http_statuses[] = {
|
||||
{100, "Informational", "Continue"},
|
||||
{101, "Informational", "Switching Protocols"},
|
||||
|
||||
{200, "Success", "OK"},
|
||||
{201, "Success", "Created"},
|
||||
{202, "Success", "Accepted"},
|
||||
{203, "Success", "Non-Authoritative Information"},
|
||||
{204, "Success", "No Content"},
|
||||
{205, "Success", "Reset Content"},
|
||||
{206, "Success", "Partial Content"},
|
||||
|
||||
{300, "Redirection", "Multiple Choices"},
|
||||
{301, "Redirection", "Moved Permanently"},
|
||||
{302, "Redirection", "Found"},
|
||||
{303, "Redirection", "See Other"},
|
||||
{304, "Success", "Not Modified"},
|
||||
{305, "Redirection", "Use Proxy"},
|
||||
{307, "Redirection", "Temporary Redirect"},
|
||||
{308, "Redirection", "Permanent Redirect"},
|
||||
|
||||
{400, "Client Error", "Bad Request"},
|
||||
{401, "Client Error", "Unauthorized"},
|
||||
{402, "Client Error", "Payment Required"},
|
||||
{403, "Client Error", "Forbidden"},
|
||||
{404, "Client Error", "Not Found"},
|
||||
{405, "Client Error", "Method Not Allowed"},
|
||||
{406, "Client Error", "Not Acceptable"},
|
||||
{407, "Client Error", "Proxy Authentication Required"},
|
||||
{408, "Client Error", "Request Timeout"},
|
||||
{409, "Client Error", "Conflict"},
|
||||
{410, "Client Error", "Gone"},
|
||||
{411, "Client Error", "Length Required"},
|
||||
{412, "Client Error", "Precondition Failed"},
|
||||
{413, "Client Error", "Request Entity Too Large"},
|
||||
{414, "Client Error", "Request-URI Too Long"},
|
||||
{415, "Client Error", "Unsupported Media Type"},
|
||||
{416, "Client Error", "Range Not Satisfiable"},
|
||||
{417, "Client Error", "Expectation Failed"},
|
||||
|
||||
{500, "Server Error", "Internal Server Error"},
|
||||
{501, "Server Error", "Not Implemented"},
|
||||
{502, "Server Error", "Bad Gateway"},
|
||||
{503, "Server Error", "Service Unavailable"},
|
||||
{504, "Server Error", "Gateway Timeout"},
|
||||
{505, "Server Error", "HTTP Version Not Supported"},
|
||||
};
|
||||
|
||||
const http_status_msg http_status_messages[] = {
|
||||
{100, "The client SHOULD continue with its request."},
|
||||
{101, "The server understands and is willing to comply with the clients request, via the Upgrade message header field, for a change in the application protocol being used on this connection."},
|
||||
|
||||
{200, "The request has succeeded."},
|
||||
{201, "The request has been fulfilled and resulted in a new resource being created."},
|
||||
{202, "The request has been accepted for processing, but the processing has not been completed."},
|
||||
{203, "The returned meta information in the entity-header is not the definitive set as available from the origin server, but is gathered from a local or a third-party copy."},
|
||||
{204, "The server has fulfilled the request but does not need to return an entity-body, and might want to return updated meta information."},
|
||||
{205, "The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent."},
|
||||
{206, "The server has fulfilled the partial GET request for the resource."},
|
||||
|
||||
{300, "The requested resource corresponds to any one of a set of representations, each with its own specific location, and agent-driven negotiation information is being provided so that the user (or user agent) can select a preferred representation and redirect its request to that location."},
|
||||
{301, "The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs."},
|
||||
{302, "The requested resource resides temporarily under a different URI."},
|
||||
{303, "The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource."},
|
||||
{304, "The request has been fulfilled and the requested resource has not been modified."},
|
||||
{305, "The requested resource MUST be accessed through the proxy given by the Location field."},
|
||||
{307, "The requested resource resides temporarily under a different URI."},
|
||||
{308, "The requested resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs."},
|
||||
|
||||
{400, "The request could not be understood by the server due to malformed syntax."},
|
||||
{401, "The request requires user authentication."},
|
||||
{403, "The server understood the request, but is refusing to fulfill it."},
|
||||
{404, "The server has not found anything matching the Request-URI."},
|
||||
{405, "The method specified in the Request-Line is not allowed for the resource identified by the Request-URI."},
|
||||
{406, "The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request."},
|
||||
{407, "The request requires user authentication on the proxy."},
|
||||
{408, "The client did not produce a request within the time that the server was prepared to wait."},
|
||||
{409, "The request could not be completed due to a conflict with the current state of the resource."},
|
||||
{410, "The requested resource is no longer available at the server and no forwarding address is known."},
|
||||
{411, "The server refuses to accept the request without a defined Content-Length."},
|
||||
{412, "The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server."},
|
||||
{413, "The server is refusing to process a request because the request entity is larger than the server is willing or able to process."},
|
||||
{414, "The server is refusing to service the request because the Request-URI is longer than the server is willing to interpret."},
|
||||
{415, "The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."},
|
||||
{416, "None of the ranges in the requests Range header field overlap the current extent of the selected resource or that the set of ranges requested has been rejected due to invalid ranges or an excessive request of small or overlapping ranges."},
|
||||
{417, "The expectation given in an Expect request-header field could not be met by this server, or, if the server is a proxy, the server has unambiguous evidence that the request could not be met by the next-hop server."},
|
||||
|
||||
{500, "The server encountered an unexpected condition which prevented it from fulfilling the request."},
|
||||
{501, "The server does not support the functionality required to fulfill the request."},
|
||||
{502, "The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request."},
|
||||
{503, "The server is currently unable to handle the request due to a temporary overloading or maintenance of the server."},
|
||||
{504, "The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI or some other auxiliary server it needed to access in attempting to complete the request."},
|
||||
{505, "The server does not support, or refuses to support, the HTTP protocol version that was used in the request message."}
|
||||
};
|
||||
|
||||
const char http_default_document[] =
|
||||
"<!DOCTYPE html>\n"
|
||||
"<html lang=\"en\">\n"
|
||||
"<head>\n"
|
||||
"\t<title>%1$i %2$s - %7$s</title>\n"
|
||||
"\t<meta charset=\"UTF-8\"/>\n"
|
||||
"\t<meta name=\"theme-color\" content=\"%6$s\"/>\n"
|
||||
"\t<meta name=\"color-scheme\" content=\"light dark\"/>\n"
|
||||
"\t<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black-translucent\"/>\n"
|
||||
"\t<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"/>\n"
|
||||
"\t<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/favicon.ico\"/>\n"
|
||||
"%5$s"
|
||||
"\t<style>\n"
|
||||
"\t\thtml{font-family:\"Arial\",sans-serif;--error:" HTTP_COLOR_ERROR ";--warning:" HTTP_COLOR_WARNING ";--success:" HTTP_COLOR_SUCCESS ";--info:" HTTP_COLOR_INFO ";--color:var(--%4$s);}\n"
|
||||
"\t\tbody{background-color:#F0F0F0;margin:0;}\n"
|
||||
"\t\tmain{max-width:650px;margin:2em auto;}\n"
|
||||
"\t\tsection{margin:1em;background-color:#FFFFFF;border: 1px solid var(--color);border-radius:4px;padding:1em;}\n"
|
||||
"\t\th1,h2,h3,h4,h5,h6,h7{text-align:center;color:var(--color);font-weight:normal;}\n"
|
||||
"\t\th1{font-size:3em;margin:0.125em 0 0.125em 0;}\n"
|
||||
"\t\th2{font-size:1.5em;margin:0.25em 0 1em 0;}\n"
|
||||
"\t\tp{text-align:center;font-size:0.875em;}\n"
|
||||
"\t\tdiv.footer{color:#808080;font-size:0.75em;text-align:center;margin:2em 0 0.5em 0;}\n"
|
||||
"\t\tdiv.footer a{color:#808080;}\n"
|
||||
"\t\t@media(prefers-color-scheme:dark){\n"
|
||||
"\t\t\thtml{color:#FFFFFF;}\n"
|
||||
"\t\t\tbody{background-color:#101010;}\n"
|
||||
"\t\t\tsection{background-color:#181818;}\n"
|
||||
"\t\t}\n"
|
||||
"\t</style>\n"
|
||||
"</head>\n"
|
||||
"<body>\n"
|
||||
"\t<main>\n"
|
||||
"\t\t<section>\n"
|
||||
"%3$s"
|
||||
"\t\t\t<div class=\"footer\"><a href=\"https://%7$s/\">%7$s</a> - Necronda web server " NECRONDA_VERSION "</div>\n"
|
||||
"\t\t</section>\n"
|
||||
"\t</main>\n"
|
||||
"</body>\n"
|
||||
"</html>\n";
|
||||
|
||||
const char http_error_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :(</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char http_error_icon[] =
|
||||
"\t<link rel=\"alternate icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiNDMDAwMDAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjooPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
|
||||
const char http_warning_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :o</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char http_warning_icon[] =
|
||||
"\t<link rel=\"alternate icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiNFMEMwMDAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjpvPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
|
||||
const char http_success_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :)</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char http_success_icon[] =
|
||||
"\t<link rel=\"alternate icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiMwMDgwMDAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjopPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
|
||||
const char http_info_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :)</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char http_info_icon[] =
|
||||
"\t<link rel=\"alternate icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiM2MDYwNjAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjopPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
const int http_statuses_size = sizeof(http_statuses);
|
||||
const int http_status_messages_size = sizeof(http_status_messages);
|
122
src/lib/include/fastcgi.h
Normal file
122
src/lib/include/fastcgi.h
Normal file
@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* FastCGI header file
|
||||
* src/lib/include/fastcgi.h
|
||||
* Lorenz Stechauner, 2021-05-03
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_EXTERN_FASTCGI_H
|
||||
#define NECRONDA_SERVER_EXTERN_FASTCGI_H
|
||||
|
||||
/*
|
||||
* Listening socket file number
|
||||
*/
|
||||
#define FCGI_LISTENSOCK_FILENO 0
|
||||
|
||||
typedef struct {
|
||||
unsigned char version;
|
||||
unsigned char type;
|
||||
unsigned char requestIdB1;
|
||||
unsigned char requestIdB0;
|
||||
unsigned char contentLengthB1;
|
||||
unsigned char contentLengthB0;
|
||||
unsigned char paddingLength;
|
||||
unsigned char reserved;
|
||||
} FCGI_Header;
|
||||
|
||||
/*
|
||||
* Number of bytes in a FCGI_Header. Future versions of the protocol
|
||||
* will not reduce this number.
|
||||
*/
|
||||
#define FCGI_HEADER_LEN 8
|
||||
|
||||
/*
|
||||
* Value for version component of FCGI_Header
|
||||
*/
|
||||
#define FCGI_VERSION_1 1
|
||||
|
||||
/*
|
||||
* Values for type component of FCGI_Header
|
||||
*/
|
||||
#define FCGI_BEGIN_REQUEST 1
|
||||
#define FCGI_ABORT_REQUEST 2
|
||||
#define FCGI_END_REQUEST 3
|
||||
#define FCGI_PARAMS 4
|
||||
#define FCGI_STDIN 5
|
||||
#define FCGI_STDOUT 6
|
||||
#define FCGI_STDERR 7
|
||||
#define FCGI_DATA 8
|
||||
#define FCGI_GET_VALUES 9
|
||||
#define FCGI_GET_VALUES_RESULT 10
|
||||
#define FCGI_UNKNOWN_TYPE 11
|
||||
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
|
||||
|
||||
/*
|
||||
* Value for requestId component of FCGI_Header
|
||||
*/
|
||||
#define FCGI_NULL_REQUEST_ID 0
|
||||
|
||||
typedef struct {
|
||||
unsigned char roleB1;
|
||||
unsigned char roleB0;
|
||||
unsigned char flags;
|
||||
unsigned char reserved[5];
|
||||
} FCGI_BeginRequestBody;
|
||||
|
||||
typedef struct {
|
||||
FCGI_Header header;
|
||||
FCGI_BeginRequestBody body;
|
||||
} FCGI_BeginRequestRecord;
|
||||
|
||||
/*
|
||||
* Mask for flags component of FCGI_BeginRequestBody
|
||||
*/
|
||||
#define FCGI_KEEP_CONN 1
|
||||
|
||||
/*
|
||||
* Values for role component of FCGI_BeginRequestBody
|
||||
*/
|
||||
#define FCGI_RESPONDER 1
|
||||
#define FCGI_AUTHORIZER 2
|
||||
#define FCGI_FILTER 3
|
||||
|
||||
typedef struct {
|
||||
unsigned char appStatusB3;
|
||||
unsigned char appStatusB2;
|
||||
unsigned char appStatusB1;
|
||||
unsigned char appStatusB0;
|
||||
unsigned char protocolStatus;
|
||||
unsigned char reserved[3];
|
||||
} FCGI_EndRequestBody;
|
||||
|
||||
typedef struct {
|
||||
FCGI_Header header;
|
||||
FCGI_EndRequestBody body;
|
||||
} FCGI_EndRequestRecord;
|
||||
|
||||
/*
|
||||
* Values for protocolStatus component of FCGI_EndRequestBody
|
||||
*/
|
||||
#define FCGI_REQUEST_COMPLETE 0
|
||||
#define FCGI_CANT_MPX_CONN 1
|
||||
#define FCGI_OVERLOADED 2
|
||||
#define FCGI_UNKNOWN_ROLE 3
|
||||
|
||||
/*
|
||||
* Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT records
|
||||
*/
|
||||
#define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
|
||||
#define FCGI_MAX_REQS "FCGI_MAX_REQS"
|
||||
#define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
|
||||
|
||||
typedef struct {
|
||||
unsigned char type;
|
||||
unsigned char reserved[7];
|
||||
} FCGI_UnknownTypeBody;
|
||||
|
||||
typedef struct {
|
||||
FCGI_Header header;
|
||||
FCGI_UnknownTypeBody body;
|
||||
} FCGI_UnknownTypeRecord;
|
||||
|
||||
#endif //NECRONDA_SERVER_EXTERN_FASTCGI_H
|
423
src/lib/rev_proxy.c
Normal file
423
src/lib/rev_proxy.c
Normal file
@ -0,0 +1,423 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Reverse proxy
|
||||
* src/lib/rev_proxy.c
|
||||
* Lorenz Stechauner, 2021-01-07
|
||||
*/
|
||||
|
||||
#include "rev_proxy.h"
|
||||
#include "utils.h"
|
||||
#include "../client.h"
|
||||
#include "../necronda-server.h"
|
||||
#include <openssl/ssl.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <openssl/err.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
sock rev_proxy;
|
||||
char *rev_proxy_host = NULL;
|
||||
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
|
||||
|
||||
int rev_proxy_preload() {
|
||||
rev_proxy.buf = NULL;
|
||||
rev_proxy.buf_len = 0;
|
||||
rev_proxy.buf_off = 0;
|
||||
rev_proxy.ctx = SSL_CTX_new(TLS_client_method());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rev_proxy_request_header(http_req *req, int enc) {
|
||||
char buf1[256];
|
||||
char buf2[256];
|
||||
int p_len;
|
||||
http_remove_header_field(&req->hdr, "Connection", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&req->hdr, "Connection", "keep-alive");
|
||||
|
||||
char *via = http_get_header_field(&req->hdr, "Via");
|
||||
sprintf(buf1, "HTTP/%s %s", req->version, DEFAULT_HOST);
|
||||
if (via == NULL) {
|
||||
http_add_header_field(&req->hdr, "Via", buf1);
|
||||
} 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);
|
||||
return -1;
|
||||
}
|
||||
http_remove_header_field(&req->hdr, "Via", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&req->hdr, "Via", buf2);
|
||||
}
|
||||
|
||||
char *host = http_get_header_field(&req->hdr, "Host");
|
||||
char *forwarded = http_get_header_field(&req->hdr, "Forwarded");
|
||||
int client_ipv6 = strchr(client_addr_str, ':') != NULL;
|
||||
int server_ipv6 = strchr(server_addr_str, ':') != NULL;
|
||||
|
||||
p_len = snprintf(buf1, sizeof(buf1), "by=%s%s%s;for=%s%s%s;host=%s;proto=%s",
|
||||
server_ipv6 ? "\"[" : "", server_addr_str, server_ipv6 ? "]\"" : "",
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
if (forwarded == NULL) {
|
||||
http_add_header_field(&req->hdr, "Forwarded", buf1);
|
||||
} 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);
|
||||
return -1;
|
||||
}
|
||||
http_remove_header_field(&req->hdr, "Forwarded", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&req->hdr, "Forwarded", buf2);
|
||||
}
|
||||
|
||||
char *xff = http_get_header_field(&req->hdr, "X-Forwarded-For");
|
||||
if (xff == NULL) {
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-For", client_addr_str);
|
||||
} else {
|
||||
sprintf(buf1, "%s, %s", xff, client_addr_str);
|
||||
http_remove_header_field(&req->hdr, "X-Forwarded-For", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-For", buf1);
|
||||
}
|
||||
|
||||
char *xfh = http_get_header_field(&req->hdr, "X-Forwarded-Host");
|
||||
if (xfh == NULL) {
|
||||
if (forwarded == NULL) {
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-Host", host);
|
||||
} else {
|
||||
char *ptr = strchr(forwarded, ',');
|
||||
unsigned long len;
|
||||
if (ptr != NULL) len = ptr - forwarded;
|
||||
else len = strlen(forwarded);
|
||||
ptr = strstr(forwarded, "host=");
|
||||
if ((ptr - forwarded) < len) {
|
||||
char *end = strchr(ptr, ';');
|
||||
if (end == NULL) len -= (ptr - forwarded);
|
||||
else len = (end - ptr);
|
||||
len -= 5;
|
||||
sprintf(buf1, "%.*s", (int) len, ptr + 5);
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-Host", buf1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *xfp = http_get_header_field(&req->hdr, "X-Forwarded-Proto");
|
||||
if (xfp == NULL) {
|
||||
if (forwarded == NULL) {
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-Proto", enc ? "https" : "http");
|
||||
} else {
|
||||
char *ptr = strchr(forwarded, ',');
|
||||
unsigned long len;
|
||||
if (ptr != NULL) len = ptr - forwarded;
|
||||
else len = strlen(forwarded);
|
||||
ptr = strstr(forwarded, "proto=");
|
||||
if ((ptr - forwarded) < len) {
|
||||
char *end = strchr(ptr, ';');
|
||||
if (end == NULL) len -= (ptr - forwarded);
|
||||
else len = (end - ptr);
|
||||
len -= 6;
|
||||
sprintf(buf1, "%.*s", (int) len, ptr + 6);
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-Proto", buf1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rev_proxy_response_header(http_req *req, http_res *res) {
|
||||
char buf1[256];
|
||||
char buf2[256];
|
||||
int p_len;
|
||||
|
||||
char *via = http_get_header_field(&res->hdr, "Via");
|
||||
p_len = snprintf(buf1, sizeof(buf1), "HTTP/%s %s", req->version, DEFAULT_HOST);
|
||||
if (p_len < 0 || p_len >= sizeof(buf1)) {
|
||||
print(ERR_STR "Appended part of header field 'Via' too long" CLR_STR);
|
||||
return -1;
|
||||
}
|
||||
if (via == NULL) {
|
||||
http_add_header_field(&res->hdr, "Via", buf1);
|
||||
} 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);
|
||||
return -1;
|
||||
}
|
||||
http_remove_header_field(&res->hdr, "Via", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&res->hdr, "Via", buf2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client, http_status *custom_status,
|
||||
char *err_msg) {
|
||||
char buffer[CHUNK_SIZE];
|
||||
long ret;
|
||||
int tries = 0;
|
||||
int retry = 0;
|
||||
|
||||
if (rev_proxy.socket != 0 && strcmp(rev_proxy_host, conf->name) == 0 && sock_check(&rev_proxy) == 0) {
|
||||
goto rev_proxy;
|
||||
}
|
||||
|
||||
retry:
|
||||
if (rev_proxy.socket != 0) {
|
||||
print(BLUE_STR "Closing proxy connection" CLR_STR);
|
||||
sock_close(&rev_proxy);
|
||||
}
|
||||
retry = 0;
|
||||
tries++;
|
||||
|
||||
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));
|
||||
res->status = http_get_status(500);
|
||||
return -1;
|
||||
}
|
||||
|
||||
server_timeout.tv_sec = SERVER_TIMEOUT;
|
||||
server_timeout.tv_usec = 0;
|
||||
if (setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
||||
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);
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
struct sockaddr_in6 address = {.sin6_family = AF_INET6, .sin6_port = htons(conf->rev_proxy.port)};
|
||||
if (host_ent->h_addrtype == AF_INET6) {
|
||||
memcpy(&address.sin6_addr, host_ent->h_addr_list[0], host_ent->h_length);
|
||||
} else if (host_ent->h_addrtype == AF_INET) {
|
||||
unsigned char addr[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0};
|
||||
memcpy(addr + 12, host_ent->h_addr_list[0], host_ent->h_length);
|
||||
memcpy(&address.sin6_addr, addr, 16);
|
||||
}
|
||||
|
||||
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));
|
||||
sprintf(err_msg, "Unable to connect to server: %s.", strerror(errno));
|
||||
goto proxy_err;
|
||||
}
|
||||
|
||||
if (conf->rev_proxy.enc) {
|
||||
rev_proxy.ssl = SSL_new(rev_proxy.ctx);
|
||||
SSL_set_fd(rev_proxy.ssl, rev_proxy.socket);
|
||||
SSL_set_connect_state(rev_proxy.ssl);
|
||||
|
||||
ret = SSL_do_handshake(rev_proxy.ssl);
|
||||
rev_proxy._last_ret = ret;
|
||||
rev_proxy._errno = errno;
|
||||
rev_proxy._ssl_error = ERR_get_error();
|
||||
rev_proxy.enc = 1;
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to perform handshake: %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&rev_proxy));
|
||||
goto proxy_err;
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
ret = rev_proxy_request_header(req, (int) client->enc);
|
||||
if (ret != 0) {
|
||||
res->status = http_get_status(500);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = http_send_request(&rev_proxy, req);
|
||||
if (ret < 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to send request to server (1): %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
}
|
||||
|
||||
char *content_length = http_get_header_field(&req->hdr, "Content-Length");
|
||||
if (content_length != NULL) {
|
||||
unsigned long content_len = strtoul(content_length, NULL, 10);
|
||||
if (client->buf_len - client->buf_off > 0) {
|
||||
unsigned long len = client->buf_len - client->buf_off;
|
||||
if (len > content_len) {
|
||||
len = content_len;
|
||||
}
|
||||
ret = sock_send(&rev_proxy, client->buf, len, 0);
|
||||
if (ret <= 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to send request to server (2): %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
}
|
||||
content_len -= len;
|
||||
}
|
||||
if (content_len > 0) {
|
||||
ret = sock_splice(&rev_proxy, client, buffer, sizeof(buffer), content_len);
|
||||
if (ret <= 0) {
|
||||
if (ret == -1) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to send request to server (3): %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
||||
goto proxy_err;
|
||||
} else if (ret == -2) {
|
||||
res->status = http_get_status(400);
|
||||
print(ERR_STR "Unable to receive request from client: %s" CLR_STR, sock_strerror(client));
|
||||
sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client));
|
||||
return -1;
|
||||
}
|
||||
res->status = http_get_status(500);
|
||||
print(ERR_STR "Unknown Error" CLR_STR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret = sock_recv(&rev_proxy, buffer, sizeof(buffer), MSG_PEEK);
|
||||
if (ret <= 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to receive response from server: %s" CLR_STR, sock_strerror(&rev_proxy));
|
||||
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&rev_proxy));
|
||||
goto proxy_err;
|
||||
}
|
||||
|
||||
char *buf = buffer;
|
||||
unsigned short header_len = (unsigned short) (strstr(buffer, "\r\n\r\n") - buffer + 4);
|
||||
|
||||
if (header_len <= 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to parse header: End of header not found" CLR_STR);
|
||||
sprintf(err_msg, "Unable to parser header: End of header not found.");
|
||||
goto proxy_err;
|
||||
}
|
||||
|
||||
for (int i = 0; i < header_len; i++) {
|
||||
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to parse header: Header contains illegal characters" CLR_STR);
|
||||
sprintf(err_msg, "Unable to parse header: Header contains illegal characters.");
|
||||
goto proxy_err;
|
||||
}
|
||||
}
|
||||
|
||||
char *ptr = buf;
|
||||
while (header_len != (ptr - buf)) {
|
||||
char *pos0 = strstr(ptr, "\r\n");
|
||||
if (pos0 == NULL) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||
goto proxy_err;
|
||||
}
|
||||
if (ptr == buf) {
|
||||
if (strncmp(ptr, "HTTP/", 5) != 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
|
||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||
goto proxy_err;
|
||||
}
|
||||
int status_code = (int) strtol(ptr + 9, NULL, 10);
|
||||
res->status = http_get_status(status_code);
|
||||
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);
|
||||
res->status = custom_status;
|
||||
} else if (res->status == NULL) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to parse header: Invalid or unknown status code" CLR_STR);
|
||||
sprintf(err_msg, "Unable to parse header: Invalid or unknown status code.");
|
||||
goto proxy_err;
|
||||
}
|
||||
} else {
|
||||
ret = http_parse_header_field(&res->hdr, ptr, pos0);
|
||||
if (ret != 0) {
|
||||
res->status = http_get_status(502);
|
||||
print(ERR_STR "Unable to parse header" CLR_STR);
|
||||
sprintf(err_msg, "Unable to parse header.");
|
||||
goto proxy_err;
|
||||
}
|
||||
}
|
||||
if (pos0[2] == '\r' && pos0[3] == '\n') {
|
||||
break;
|
||||
}
|
||||
ptr = pos0 + 2;
|
||||
}
|
||||
sock_recv(&rev_proxy, buffer, header_len, 0);
|
||||
|
||||
ret = rev_proxy_response_header(req, res);
|
||||
if (ret != 0) {
|
||||
res->status = http_get_status(500);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
proxy_err:
|
||||
if (retry) goto retry;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rev_proxy_send(sock *client, int chunked, unsigned long len_to_send) {
|
||||
long ret;
|
||||
char buffer[CHUNK_SIZE];
|
||||
long len, snd_len;
|
||||
// TODO handle websockets
|
||||
do {
|
||||
if (chunked) {
|
||||
ret = sock_recv(&rev_proxy, buffer, 16, MSG_PEEK);
|
||||
if (ret <= 0) {
|
||||
print("Unable to receive: %s", sock_strerror(&rev_proxy));
|
||||
break;
|
||||
}
|
||||
|
||||
len_to_send = strtol(buffer, NULL, 16);
|
||||
char *pos = strstr(buffer, "\r\n");
|
||||
len = pos - buffer + 2;
|
||||
ret = sock_send(client, buffer, len, 0);
|
||||
|
||||
sock_recv(&rev_proxy, buffer, len, 0);
|
||||
if (ret <= 0) break;
|
||||
}
|
||||
snd_len = 0;
|
||||
while (snd_len < len_to_send) {
|
||||
len = sock_recv(&rev_proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
|
||||
ret = sock_send(client, buffer, len, 0);
|
||||
if (ret <= 0) {
|
||||
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
|
||||
break;
|
||||
}
|
||||
snd_len += ret;
|
||||
}
|
||||
if (ret <= 0) break;
|
||||
if (chunked) {
|
||||
sock_recv(&rev_proxy, buffer, 2, 0);
|
||||
ret = sock_send(client, "\r\n", 2, 0);
|
||||
if (ret <= 0) {
|
||||
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (chunked && len_to_send > 0);
|
||||
return 0;
|
||||
}
|
27
src/lib/rev_proxy.h
Normal file
27
src/lib/rev_proxy.h
Normal file
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Reverse proxy (header file)
|
||||
* src/lib/rev_proxy.h
|
||||
* Lorenz Stechauner, 2021-01-07
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_REV_PROXY_H
|
||||
#define NECRONDA_SERVER_REV_PROXY_H
|
||||
|
||||
#include "http.h"
|
||||
#include "config.h"
|
||||
|
||||
extern sock rev_proxy;
|
||||
|
||||
int rev_proxy_preload();
|
||||
|
||||
int rev_proxy_request_header(http_req *req, int enc);
|
||||
|
||||
int rev_proxy_response_header(http_req *req, http_res *res);
|
||||
|
||||
int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client, http_status *custom_status,
|
||||
char *err_msg);
|
||||
|
||||
int rev_proxy_send(sock *client, int chunked, unsigned long len_to_send);
|
||||
|
||||
#endif //NECRONDA_SERVER_REV_PROXY_H
|
114
src/lib/sock.c
Normal file
114
src/lib/sock.c
Normal file
@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Basic TCP and TLS socket
|
||||
* src/lib/sock.c
|
||||
* Lorenz Stechauner, 2021-01-07
|
||||
*/
|
||||
|
||||
#include "sock.h"
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
const char *sock_strerror(sock *s) {
|
||||
if (s->_last_ret == 0) {
|
||||
return "closed";
|
||||
} else if (s->enc) {
|
||||
if (s->_last_ret > 0) {
|
||||
return NULL;
|
||||
}
|
||||
const char *err1 = ERR_reason_error_string(s->_ssl_error);
|
||||
const char *err2 = strerror(errno);
|
||||
switch (SSL_get_error(s->ssl, (int) s->_last_ret)) {
|
||||
case SSL_ERROR_NONE:
|
||||
return NULL;
|
||||
case SSL_ERROR_ZERO_RETURN:
|
||||
return "closed";
|
||||
case SSL_ERROR_WANT_READ:
|
||||
return "want read";
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
return "want write";
|
||||
case SSL_ERROR_WANT_CONNECT:
|
||||
return "want connect";
|
||||
case SSL_ERROR_WANT_ACCEPT:
|
||||
return "want accept";
|
||||
case SSL_ERROR_WANT_X509_LOOKUP:
|
||||
return "want x509 lookup";
|
||||
case SSL_ERROR_SYSCALL:
|
||||
return ((s->_ssl_error == 0) ? ((s->_last_ret == 0) ? "protocol violation" : err2) : err1);
|
||||
case SSL_ERROR_SSL:
|
||||
return err1;
|
||||
default:
|
||||
return "unknown error";
|
||||
}
|
||||
} else {
|
||||
return strerror(s->_errno);
|
||||
}
|
||||
}
|
||||
|
||||
long sock_send(sock *s, void *buf, unsigned long len, int flags) {
|
||||
long ret;
|
||||
if (s->enc) {
|
||||
ret = SSL_write(s->ssl, buf, (int) len);
|
||||
} else {
|
||||
ret = send(s->socket, buf, len, flags);
|
||||
}
|
||||
s->_last_ret = ret;
|
||||
s->_errno = errno;
|
||||
s->_ssl_error = ERR_get_error();
|
||||
return ret >= 0 ? ret : -1;
|
||||
}
|
||||
|
||||
long sock_recv(sock *s, void *buf, unsigned long len, int flags) {
|
||||
long ret;
|
||||
if (s->enc) {
|
||||
if (flags & MSG_PEEK) {
|
||||
ret = SSL_peek(s->ssl, buf, (int) len);
|
||||
} else {
|
||||
ret = SSL_read(s->ssl, buf, (int) len);
|
||||
}
|
||||
} else {
|
||||
ret = recv(s->socket, buf, len, flags);
|
||||
}
|
||||
s->_last_ret = ret;
|
||||
s->_errno = errno;
|
||||
s->_ssl_error = ERR_get_error();
|
||||
return ret >= 0 ? ret : -1;
|
||||
}
|
||||
|
||||
long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigned long len) {
|
||||
long ret;
|
||||
unsigned long send_len = 0;
|
||||
unsigned long next_len;
|
||||
while (send_len < len) {
|
||||
next_len = (buf_len < (len - send_len)) ? buf_len : (len - send_len);
|
||||
ret = sock_recv(src, buf, next_len, 0);
|
||||
if (ret < 0) return -2;
|
||||
next_len = ret;
|
||||
ret = sock_send(dst, buf, next_len, send_len + next_len < len ? MSG_MORE : 0);
|
||||
if (ret < 0) return -1;
|
||||
if (ret != next_len) return -3;
|
||||
send_len += next_len;
|
||||
}
|
||||
return (long) send_len;
|
||||
}
|
||||
|
||||
int sock_close(sock *s) {
|
||||
if ((int) s->enc && s->ssl != NULL) {
|
||||
SSL_shutdown(s->ssl);
|
||||
SSL_free(s->ssl);
|
||||
}
|
||||
shutdown(s->socket, SHUT_RDWR);
|
||||
close(s->socket);
|
||||
s->socket = 0;
|
||||
s->enc = 0;
|
||||
s->ssl = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sock_check(sock *s) {
|
||||
char buf;
|
||||
return recv(s->socket, &buf, 1, MSG_PEEK | MSG_DONTWAIT) == 1;
|
||||
}
|
38
src/lib/sock.h
Normal file
38
src/lib/sock.h
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Basic TCP and TLS socket (header file)
|
||||
* src/lib/sock.h
|
||||
* Lorenz Stechauner, 2021-01-07
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_SOCK_H
|
||||
#define NECRONDA_SERVER_SOCK_H
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
|
||||
typedef struct {
|
||||
unsigned int enc:1;
|
||||
int socket;
|
||||
SSL_CTX *ctx;
|
||||
SSL *ssl;
|
||||
char *buf;
|
||||
unsigned long buf_len;
|
||||
unsigned long buf_off;
|
||||
long _last_ret;
|
||||
int _errno;
|
||||
unsigned long _ssl_error;
|
||||
} sock;
|
||||
|
||||
const char *sock_strerror(sock *s);
|
||||
|
||||
long sock_send(sock *s, void *buf, unsigned long len, int flags);
|
||||
|
||||
long sock_recv(sock *s, void *buf, unsigned long len, int flags);
|
||||
|
||||
long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigned long len);
|
||||
|
||||
int sock_close(sock *s);
|
||||
|
||||
int sock_check(sock *s);
|
||||
|
||||
#endif //NECRONDA_SERVER_SOCK_H
|
200
src/lib/uri.c
Normal file
200
src/lib/uri.c
Normal file
@ -0,0 +1,200 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* URI and path handlers
|
||||
* src/lib/uri.c
|
||||
* Lorenz Stechauner, 2020-12-13
|
||||
*/
|
||||
|
||||
#include "uri.h"
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int path_is_directory(const char *path) {
|
||||
struct stat statbuf;
|
||||
return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode) != 0;
|
||||
}
|
||||
|
||||
int path_is_file(const char *path) {
|
||||
struct stat statbuf;
|
||||
return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode) == 0;
|
||||
}
|
||||
|
||||
int path_exists(const char *path) {
|
||||
struct stat statbuf;
|
||||
return stat(path, &statbuf) == 0;
|
||||
}
|
||||
|
||||
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode) {
|
||||
char buf0[1024];
|
||||
char buf1[1024];
|
||||
char buf2[1024];
|
||||
char buf3[1024];
|
||||
int p_len;
|
||||
uri->webroot = NULL;
|
||||
uri->req_path = NULL;
|
||||
uri->path = NULL;
|
||||
uri->pathinfo = NULL;
|
||||
uri->query = NULL;
|
||||
uri->filename = NULL;
|
||||
uri->uri = NULL;
|
||||
uri->meta = NULL;
|
||||
uri->is_static = 1;
|
||||
uri->is_dir = 0;
|
||||
if (uri_str[0] != '/') {
|
||||
return 1;
|
||||
}
|
||||
uri->webroot = malloc(strlen(webroot) + 1);
|
||||
strcpy(uri->webroot, webroot);
|
||||
|
||||
char *query = strchr(uri_str, '?');
|
||||
if (query == NULL) {
|
||||
uri->query = NULL;
|
||||
} else {
|
||||
query[0] = 0;
|
||||
query++;
|
||||
long size = (long) strlen(query) + 1;
|
||||
uri->query = malloc(size);
|
||||
strcpy(uri->query, query);
|
||||
}
|
||||
|
||||
long size = (long) strlen(uri_str) + 1;
|
||||
uri->req_path = malloc(size);
|
||||
url_decode(uri_str, uri->req_path, &size);
|
||||
if (query != NULL) {
|
||||
query[-1] = '?';
|
||||
}
|
||||
if (strstr(uri->req_path, "/../") != NULL || strstr(uri->req_path, "/./") != NULL) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
size = (long) strlen(uri->req_path) + 1;
|
||||
uri->path = malloc(size);
|
||||
uri->pathinfo = malloc(size);
|
||||
|
||||
char last = 0;
|
||||
for (int i = 0, j = 0; i < size - 1; i++) {
|
||||
char ch = uri->req_path[i];
|
||||
if (last != '/' || ch != '/') {
|
||||
uri->path[j++] = ch;
|
||||
uri->path[j] = 0;
|
||||
}
|
||||
last = ch;
|
||||
}
|
||||
|
||||
if (dir_mode == URI_DIR_MODE_NO_VALIDATION) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uri->path[strlen(uri->path) - 1] == '/') {
|
||||
uri->path[strlen(uri->path) - 1] = 0;
|
||||
strcpy(uri->pathinfo, "/");
|
||||
} else {
|
||||
strcpy(uri->pathinfo, "");
|
||||
}
|
||||
|
||||
if (!path_exists(uri->webroot)) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
sprintf(buf0, "%s%s", uri->webroot, uri->path);
|
||||
p_len = snprintf(buf1, sizeof(buf1), "%s.php", buf0);
|
||||
if (p_len < 0 || p_len >= sizeof(buf1)) return -1;
|
||||
p_len = snprintf(buf2, sizeof(buf2), "%s.html", buf0);
|
||||
if (p_len < 0 || p_len >= sizeof(buf2)) return -1;
|
||||
|
||||
if (strlen(uri->path) <= 1 || path_exists(buf0) || path_is_file(buf1) || path_is_file(buf2)) {
|
||||
break;
|
||||
}
|
||||
|
||||
char *ptr;
|
||||
parent_dir:
|
||||
ptr = strrchr(uri->path, '/');
|
||||
size = (long) strlen(ptr);
|
||||
sprintf(buf3, "%.*s%s", (int) size, ptr, uri->pathinfo);
|
||||
strcpy(uri->pathinfo, buf3);
|
||||
ptr[0] = 0;
|
||||
}
|
||||
if (uri->pathinfo[0] != 0) {
|
||||
sprintf(buf3, "%s", uri->pathinfo + 1);
|
||||
strcpy(uri->pathinfo, buf3);
|
||||
}
|
||||
|
||||
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) {
|
||||
uri->path[len - 4] = 0;
|
||||
uri->is_static = 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->filename = malloc(strlen(buf2) + 1);
|
||||
strcpy(uri->filename, buf2);
|
||||
} 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);
|
||||
if (path_is_file(buf1)) {
|
||||
uri->filename = malloc(strlen(buf1) + 1);
|
||||
strcpy(uri->filename, buf1);
|
||||
uri->is_static = 0;
|
||||
} else if (path_is_file(buf2)) {
|
||||
uri->filename = malloc(strlen(buf2) + 1);
|
||||
strcpy(uri->filename, buf2);
|
||||
} else {
|
||||
if (dir_mode == URI_DIR_MODE_FORBIDDEN) {
|
||||
uri->is_static = 1;
|
||||
} else if (dir_mode == URI_DIR_MODE_LIST) {
|
||||
uri->is_static = 0;
|
||||
} else if (dir_mode == URI_DIR_MODE_INFO) {
|
||||
if (strlen(uri->path) > 1) {
|
||||
uri->path[strlen(uri->path) - 1] = 0;
|
||||
sprintf(buf0, "/%s", uri->pathinfo);
|
||||
strcpy(uri->pathinfo, buf0);
|
||||
goto parent_dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
uri->pathinfo[0] = 0;
|
||||
}
|
||||
|
||||
sprintf(buf0, "%s%s%s%s%s", uri->path,
|
||||
(strlen(uri->pathinfo) == 0 || uri->path[strlen(uri->path) - 1] == '/') ? "" : "/", uri->pathinfo,
|
||||
uri->query != NULL ? "?" : "", uri->query != NULL ? uri->query : "");
|
||||
uri->uri = malloc(strlen(buf0) + 1);
|
||||
strcpy(uri->uri, buf0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void uri_free(http_uri *uri) {
|
||||
if (uri->webroot != NULL) free(uri->webroot);
|
||||
if (uri->req_path != NULL) free(uri->req_path);
|
||||
if (uri->path != NULL) free(uri->path);
|
||||
if (uri->pathinfo != NULL) free(uri->pathinfo);
|
||||
if (uri->query != NULL) free(uri->query);
|
||||
if (uri->filename != NULL) free(uri->filename);
|
||||
if (uri->uri != NULL) free(uri->uri);
|
||||
uri->webroot = NULL;
|
||||
uri->req_path = NULL;
|
||||
uri->path = NULL;
|
||||
uri->pathinfo = NULL;
|
||||
uri->query = NULL;
|
||||
uri->filename = NULL;
|
||||
uri->uri = NULL;
|
||||
}
|
46
src/lib/uri.h
Normal file
46
src/lib/uri.h
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* URI and path handlers (header file)
|
||||
* src/lib/uri.h
|
||||
* Lorenz Stechauner, 2020-12-13
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_URI_H
|
||||
#define NECRONDA_SERVER_URI_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define URI_DIR_MODE_NO_VALIDATION 0
|
||||
#define URI_DIR_MODE_FORBIDDEN 1
|
||||
#define URI_DIR_MODE_LIST 2
|
||||
#define URI_DIR_MODE_INFO 3
|
||||
|
||||
typedef struct {
|
||||
char etag[64];
|
||||
char type[24];
|
||||
char charset[16];
|
||||
char filename_comp[256];
|
||||
struct stat stat;
|
||||
} meta_data;
|
||||
|
||||
typedef struct {
|
||||
char *webroot; // "/srv/www/www.test.org"
|
||||
char *req_path; // "/account/login"
|
||||
char *path; // "/account/"
|
||||
char *pathinfo; // "login"
|
||||
char *query; // "username=test"
|
||||
char *filename; // "/account/index.php"
|
||||
char *uri; // "/account/login?username=test"
|
||||
meta_data *meta;
|
||||
unsigned char is_static:1;
|
||||
unsigned char is_dir:1;
|
||||
} http_uri;
|
||||
|
||||
|
||||
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode);
|
||||
|
||||
int uri_init_cache(http_uri *uri);
|
||||
|
||||
void uri_free(http_uri *uri);
|
||||
|
||||
#endif //NECRONDA_SERVER_URI_H
|
178
src/lib/utils.c
Normal file
178
src/lib/utils.c
Normal file
@ -0,0 +1,178 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Utilities
|
||||
* src/lib/utils.c
|
||||
* Lorenz Stechauner, 2020-12-03
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *log_prefix;
|
||||
|
||||
char *format_duration(unsigned long micros, char *buf) {
|
||||
if (micros < 10000) {
|
||||
sprintf(buf, "%.1f ms", (double) micros / 1000);
|
||||
} else if (micros < 1000000) {
|
||||
sprintf(buf, "%li ms", micros / 1000);
|
||||
} else if (micros < 60000000) {
|
||||
sprintf(buf, "%.1f s", (double) micros / 1000000);
|
||||
} else if (micros < 6000000000) {
|
||||
sprintf(buf, "%.1f min", (double) micros / 1000000 / 60);
|
||||
} else {
|
||||
sprintf(buf, "%li min", micros / 1000000 / 60);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
int url_encode_component(const char *str, char *enc, ssize_t *size) {
|
||||
char *ptr = enc;
|
||||
char ch;
|
||||
memset(enc, 0, *size);
|
||||
for (int i = 0; i < strlen(str); i++, ptr++) {
|
||||
if ((ptr - enc) >= *size) {
|
||||
return -1;
|
||||
}
|
||||
ch = str[i];
|
||||
if (ch == ':' || ch == '/' || ch == '?' || ch == '#' || ch == '[' || ch == ']' || ch == '@' || ch == '!' ||
|
||||
ch == '$' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' ||
|
||||
ch == ';' || ch == '=' || ch < ' ' || ch > '~') {
|
||||
if ((ptr - enc + 2) >= *size) {
|
||||
return -1;
|
||||
}
|
||||
sprintf(ptr, "%%%02X", ch);
|
||||
ptr += 2;
|
||||
} else if (ch == ' ') {
|
||||
ptr[0] = '+';
|
||||
} else {
|
||||
ptr[0] = ch;
|
||||
}
|
||||
}
|
||||
*size = ptr - enc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int url_encode(const char *str, char *enc, ssize_t *size) {
|
||||
char *ptr = enc;
|
||||
unsigned char ch;
|
||||
memset(enc, 0, *size);
|
||||
for (int i = 0; i < strlen(str); i++, ptr++) {
|
||||
if ((ptr - enc) >= *size) {
|
||||
return -1;
|
||||
}
|
||||
ch = str[i];
|
||||
if (ch > 0x7F || ch == ' ') {
|
||||
if ((ptr - enc + 2) >= *size) {
|
||||
return -1;
|
||||
}
|
||||
sprintf(ptr, "%%%02X", ch);
|
||||
ptr += 2;
|
||||
} else {
|
||||
ptr[0] = (char) ch;
|
||||
}
|
||||
}
|
||||
*size = ptr - enc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int url_decode(const char *str, char *dec, ssize_t *size) {
|
||||
char *ptr = dec;
|
||||
char ch, buf[3];
|
||||
memset(dec, 0, *size);
|
||||
for (int i = 0; i < strlen(str); i++, ptr++) {
|
||||
if ((ptr - dec) >= *size) {
|
||||
return -1;
|
||||
}
|
||||
ch = str[i];
|
||||
if (ch == '+') {
|
||||
ch = ' ';
|
||||
} else if (ch == '%') {
|
||||
memcpy(buf, str + i + 1, 2);
|
||||
buf[2] = 0;
|
||||
ch = (char) strtol(buf, NULL, 16);
|
||||
i += 2;
|
||||
} else if (ch == '?') {
|
||||
strcpy(ptr, str + i);
|
||||
break;
|
||||
}
|
||||
ptr[0] = ch;
|
||||
}
|
||||
*size = ptr - dec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mime_is_compressible(const char *type) {
|
||||
return
|
||||
strncmp(type, "text/", 5) == 0 ||
|
||||
strncmp(type, "message/", 7) == 0 ||
|
||||
strstr(type, "+xml") != NULL ||
|
||||
strcmp(type, "application/javascript") == 0 ||
|
||||
strcmp(type, "application/json") == 0 ||
|
||||
strcmp(type, "application/xml") == 0 ||
|
||||
strcmp(type, "application/x-www-form-urlencoded") == 0 ||
|
||||
strcmp(type, "application/x-tex") == 0 ||
|
||||
strcmp(type, "application/x-httpd-php") == 0 ||
|
||||
strcmp(type, "application/x-latex") == 0;
|
||||
}
|
||||
|
||||
MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long *str_off, long str_len) {
|
||||
switch (list->entry_data.type) {
|
||||
case MMDB_DATA_TYPE_MAP:
|
||||
*str_off += sprintf(str + *str_off, "{");
|
||||
break;
|
||||
case MMDB_DATA_TYPE_ARRAY:
|
||||
*str_off += sprintf(str + *str_off, "[");
|
||||
break;
|
||||
case MMDB_DATA_TYPE_UTF8_STRING:
|
||||
*str_off += sprintf(str + *str_off, "\"%.*s\"", list->entry_data.data_size, list->entry_data.utf8_string);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_UINT16:
|
||||
*str_off += sprintf(str + *str_off, "%u", list->entry_data.uint16);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_UINT32:
|
||||
*str_off += sprintf(str + *str_off, "%u", list->entry_data.uint32);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_UINT64:
|
||||
*str_off += sprintf(str + *str_off, "%lu", list->entry_data.uint64);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_UINT128:
|
||||
*str_off += sprintf(str + *str_off, "%llu", (unsigned long long) list->entry_data.uint128);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_INT32:
|
||||
*str_off += sprintf(str + *str_off, "%i", list->entry_data.uint32);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_BOOLEAN:
|
||||
*str_off += sprintf(str + *str_off, "%s", list->entry_data.boolean ? "true" : "false");
|
||||
break;
|
||||
case MMDB_DATA_TYPE_FLOAT:
|
||||
*str_off += sprintf(str + *str_off, "%f", list->entry_data.float_value);
|
||||
break;
|
||||
case MMDB_DATA_TYPE_DOUBLE:
|
||||
*str_off += sprintf(str + *str_off, "%f", list->entry_data.double_value);
|
||||
break;
|
||||
}
|
||||
if (list->entry_data.type != MMDB_DATA_TYPE_MAP && list->entry_data.type != MMDB_DATA_TYPE_ARRAY) {
|
||||
return list->next;
|
||||
}
|
||||
MMDB_entry_data_list_s *next = list->next;
|
||||
int stat = 0;
|
||||
for (int i = 0; i < list->entry_data.data_size; i++) {
|
||||
next = mmdb_json(next, str, str_off, str_len);
|
||||
if (list->entry_data.type == MMDB_DATA_TYPE_MAP) {
|
||||
stat = !stat;
|
||||
if (stat) {
|
||||
i--;
|
||||
*str_off += sprintf(str + *str_off, ":");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (i != list->entry_data.data_size - 1) *str_off += sprintf(str + *str_off, ",");
|
||||
}
|
||||
if (list->entry_data.type == MMDB_DATA_TYPE_MAP) {
|
||||
*str_off += sprintf(str + *str_off, "}");
|
||||
} else {
|
||||
*str_off += sprintf(str + *str_off, "]");
|
||||
}
|
||||
return next;
|
||||
}
|
37
src/lib/utils.h
Normal file
37
src/lib/utils.h
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Necronda Web Server
|
||||
* Utilities (header file)
|
||||
* src/lib/utils.h
|
||||
* Lorenz Stechauner, 2020-12-03
|
||||
*/
|
||||
|
||||
#ifndef NECRONDA_SERVER_UTILS_H
|
||||
#define NECRONDA_SERVER_UTILS_H
|
||||
|
||||
#include <maxminddb.h>
|
||||
|
||||
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 char *str, char *enc, ssize_t *size);
|
||||
|
||||
int url_encode(const char *str, char *enc, ssize_t *size);
|
||||
|
||||
int url_decode(const char *str, char *dec, ssize_t *size);
|
||||
|
||||
int mime_is_compressible(const char *type);
|
||||
|
||||
MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long *str_off, long str_len);
|
||||
|
||||
#endif //NECRONDA_SERVER_UTILS_H
|
Reference in New Issue
Block a user