the server is now useable

This commit is contained in:
overflowerror 2019-03-13 17:18:15 +01:00
parent d9f1f709df
commit 4bfe1dc39c
2 changed files with 89 additions and 101 deletions

17
demo.conf Normal file
View file

@ -0,0 +1,17 @@
bind 0.0.0.0:1337 {
site {
root = ./home/
handler / {
type = file
index = index.html
}
handler /cgi-bin/ {
type = cgi
}
}
}
logging {
access = access.log
server = server.log
verbosity = info
}

View file

@ -2,37 +2,35 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "networking.h" #include "networking.h"
#include "logging.h" #include "logging.h"
#include "headers.h" #include "headers.h"
#include "files.h"
#include "cgi.h"
#include "util.h" #include "util.h"
#include "signals.h" #include "signals.h"
#include "config.h"
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
#include "ssl.h" #include "ssl.h"
#endif #endif
struct headers headers; struct networkingConfig networkingConfig;
char* documentRoot = NULL; struct config* config;
FILE* accesslog;
const char* configFile = NULL;
void shutdownHandler() { void shutdownHandler() {
info("main: shutting down"); info("main: shutting down");
headers_free(&headers); headers_free(&(networkingConfig.defaultHeaders));
if (documentRoot != NULL) config_destroy(config);
free(documentRoot);
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
ssl_destroy(); ssl_destroy();
#endif #endif
fclose(accesslog);
exit(0); exit(0);
} }
@ -41,113 +39,86 @@ void sigHandler(int signo) {
shutdownHandler(); shutdownHandler();
} }
struct handlerSettings { void setup() {
struct fileSettings fileSettings; setLogging(stdout, ERROR, true);
struct cgiSettings cgiSettings; setCriticalHandler(&shutdownHandler);
const char* cgiBin;
};
struct handler handlerGetter(struct metaData metaData, const char* host, struct bind* bind) {
struct handlerSettings* settings = (struct handlerSettings*) bind->settings.ptr;
info("%s", metaData.path);
union userData data;
if (isInDir(metaData.path, settings->cgiBin)) {
data.ptr = &(settings->cgiSettings);
return (struct handler) {
.handler = &cgiHandler,
.data = data
};
} else {
data.ptr = &(settings->fileSettings);
return (struct handler) {
.handler = &fileHandler,
.data = data
};
}
}
int main(int argc, char** argv) {
accesslog = fopen("access.log", "a");
setbuf(accesslog, NULL);
setLogging(stderr, DEBUG, true);
setLogging(accesslog, HTTP_ACCESS, false);
setCriticalHandler(NULL);
signal_setup(SIGINT, &sigHandler); signal_setup(SIGINT, &sigHandler);
signal_setup(SIGTERM, &sigHandler); signal_setup(SIGTERM, &sigHandler);
documentRoot = realpath("./home/", NULL); networkingConfig.defaultHeaders.number = 0;
struct handlerSettings handlerSettings = {
.fileSettings = {
.documentRoot = documentRoot,
.index = true,
.indexfiles = {
.number = 2,
.files = (const char* []) {
"index.html",
"index.htm"
}
}
},
.cgiSettings = {
.documentRoot = documentRoot
},
.cgiBin = "/cgi-bin/"
};
union userData settingsData;
settingsData.ptr = &handlerSettings;
headers = headers_create();
headers_mod(&headers, "Server", "CFloor 0.1");
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
ssl_init(); ssl_init();
struct ssl_settings ssl_settings = (struct ssl_settings) {
.privateKey = "certs/hiro.key",
.certificate = "certs/hiro.crt"
};
if (ssl_initSettings(&(ssl_settings)) < 0) {
error("main: error setting up ssl settings");
return 1;
}
#endif #endif
}
void help(const char* progname) {
printf("Usage: %s -c CONFIG_FILE\n", progname);
}
struct networkingConfig config = { int parseArguments(int argc, char** argv) {
.maxConnections = 1024, int opt;
.connectionTimeout = 2000, while((opt = getopt(argc, argv, "c:")) != -1) {
.binds = { switch(opt) {
.number = 1, case 'c':
.binds = (struct bind[]) { configFile = optarg;
{ break;
.address = "0.0.0.0", default:
.port = "1337", help(argv[0]);
.settings = settingsData, return -1;
}
}
if (optind < argc) {
help(argv[0]);
return -1;
}
#ifdef SSL_SUPPORT if (configFile == NULL) {
.ssl_settings = &ssl_settings help(argv[0]);
#endif return -1;
} }
}
},
.defaultHeaders = headers,
.getHandler = &handlerGetter
};
networking_init(config); return 0;
}
int main(int argc, char** argv) {
setup();
if (parseArguments(argc, argv) < 0) {
shutdownHandler();
return 0;
}
FILE* file = fopen(configFile, "r");
if (file == NULL) {
error("main: couldn't open config file: %s", strerror(errno));
shutdownHandler();
return 0;
}
config = config_parse(file);
fclose(file);
if (config == NULL) {
shutdownHandler();
return 0;
}
config_setLogging(config);
if (config_getNetworkingConfig(config, &networkingConfig) == NULL) {
shutdownHandler();
return 0;
}
networking_init(networkingConfig);
while(true) { while(true) {
sleep(0xffff); sleep(0xffff);
} }
shutdownHandler(); shutdownHandler();
return 0;
} }