From b8c2411b4c400e0930b6a4ac17cb01268a0a5357 Mon Sep 17 00:00:00 2001 From: Nishi Date: Fri, 13 Sep 2024 10:28:20 +0000 Subject: [PATCH] vhost git-svn-id: file:///raid/svn-personal/tewi/trunk@6 8739d7e6-ffea-ec47-b151-bdff447c6205 --- Common/Makefile | 2 +- Common/log.c | 7 +++++ Common/string.c | 41 +++++++++++++++-------------- Makefile | 2 +- Platform/generic.mk | 1 + Platform/win64.mk | 8 ++++++ Server/Makefile | 2 +- Server/config.c | 63 +++++++++++++++++++++++++++++++++------------ Server/main.c | 11 +++++--- Server/tw_config.h | 7 +++++ 10 files changed, 100 insertions(+), 44 deletions(-) create mode 100644 Platform/win64.mk diff --git a/Common/Makefile b/Common/Makefile index e6eccda..22880a9 100644 --- a/Common/Makefile +++ b/Common/Makefile @@ -10,7 +10,7 @@ OBJS = string.o log.o all: common.a common.a: $(OBJS) - ar rcs $@ $(OBJS) + $(AR) rcs $@ $(OBJS) .c.o: $(CC) $(CFLAGS) -c -o $@ $< diff --git a/Common/log.c b/Common/log.c index 6eb7bed..5d812a6 100644 --- a/Common/log.c +++ b/Common/log.c @@ -39,6 +39,13 @@ void cm_log(const char* name, const char* log, ...) { char* tmp = result; result = cm_strcat(tmp, va_arg(args, char*)); free(tmp); + } else if(log[i] == 'd') { + int a = va_arg(args, int); + char buf[128]; + sprintf(buf, "%d", a); + char* tmp = result; + result = cm_strcat(tmp, buf); + free(tmp); } } else { cbuf[0] = log[i]; diff --git a/Common/string.c b/Common/string.c index d5eccb0..bd34458 100644 --- a/Common/string.c +++ b/Common/string.c @@ -15,21 +15,21 @@ char* cm_strcat(const char* a, const char* b) { char* cm_strdup(const char* str) { return cm_strcat(str, ""); } -char* cm_trimstart(const char* str){ +char* cm_trimstart(const char* str) { int i; - for(i = 0; str[i] != 0; i++){ - if(str[i] != ' ' && str[i] != '\t'){ + for(i = 0; str[i] != 0; i++) { + if(str[i] != ' ' && str[i] != '\t') { return cm_strdup(str + i); } } return cm_strdup(""); } -char* cm_trimend(const char* str){ +char* cm_trimend(const char* str) { char* s = cm_strdup(str); int i; - for(i = strlen(s) - 1; i >= 0; i--){ - if(s[i] != '\t' && s[i] != ' '){ + for(i = strlen(s) - 1; i >= 0; i--) { + if(s[i] != '\t' && s[i] != ' ') { s[i + 1] = 0; break; } @@ -37,14 +37,14 @@ char* cm_trimend(const char* str){ return s; } -char* cm_trim(const char* str){ +char* cm_trim(const char* str) { char* tmp = cm_trimstart(str); char* s = cm_trimend(tmp); free(tmp); return s; } -char** cm_split(const char* str, const char* by){ +char** cm_split(const char* str, const char* by) { int i; char** r = malloc(sizeof(*r)); r[0] = NULL; @@ -54,20 +54,21 @@ char** cm_split(const char* str, const char* by){ cbuf[1] = 0; bool dq = false; bool sq = false; - for(i = 0;; i++){ + for(i = 0;; i++) { int j; bool has = false; - for(j = 0; by[j] != 0; j++){ - if(by[j] == str[i]){ + for(j = 0; by[j] != 0; j++) { + if(by[j] == str[i]) { has = true; break; } } - if(!(dq || sq) && (has || str[i] == 0)){ - if(strlen(b) > 0){ + if(!(dq || sq) && (has || str[i] == 0)) { + if(strlen(b) > 0) { char** old = r; int j; - for(j = 0; old[j] != NULL; j++); + for(j = 0; old[j] != NULL; j++) + ; r = malloc(sizeof(*r) * (j + 2)); for(j = 0; old[j] != NULL; j++) r[j] = old[j]; r[j] = b; @@ -77,12 +78,12 @@ char** cm_split(const char* str, const char* by){ b = malloc(1); b[0] = 0; if(str[i] == 0) break; - }else{ - if(str[i] == '"' && !sq){ + } else { + if(str[i] == '"' && !sq) { dq = !dq; - }else if(str[i] == '\'' && !dq){ + } else if(str[i] == '\'' && !dq) { sq = !sq; - }else{ + } else { cbuf[0] = str[i]; char* tmp = b; b = cm_strcat(tmp, cbuf); @@ -94,12 +95,12 @@ char** cm_split(const char* str, const char* by){ return r; } -bool cm_strcaseequ(const char* a, const char* b){ +bool cm_strcaseequ(const char* a, const char* b) { if(a == NULL) return false; if(b == NULL) return false; if(strlen(a) != strlen(b)) return false; int i; - for(i = 0; a[i] != 0; i++){ + for(i = 0; a[i] != 0; i++) { if(tolower(a[i]) != tolower(b[i])) return false; } return true; diff --git a/Makefile b/Makefile index f5e456d..34e6226 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ all: ./Server $(MAKE) -C $@ $(FLAGS) format: - clang-format --verbose -i `find . -name "*.c" -or -name "*.h"` + clang-format --verbose -i `find ./Server ./Common -name "*.c" -or -name "*.h"` clean: $(MAKE) -C ./Server $(FLAGS) clean diff --git a/Platform/generic.mk b/Platform/generic.mk index d0e4948..aa21474 100644 --- a/Platform/generic.mk +++ b/Platform/generic.mk @@ -5,3 +5,4 @@ AR = ar CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common LDFLAGS = LIBS = +EXEC = diff --git a/Platform/win64.mk b/Platform/win64.mk new file mode 100644 index 0000000..94763f9 --- /dev/null +++ b/Platform/win64.mk @@ -0,0 +1,8 @@ +# $Id$ + +CC = x86_64-w64-mingw32-gcc +AR = x86_64-w64-mingw32-ar +CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I $(PWD)/openssl/include +LDFLAGS = -L $(PWD)/openssl/lib +LIBS = +EXEC = .exe diff --git a/Server/Makefile b/Server/Makefile index bc17049..26a59e1 100644 --- a/Server/Makefile +++ b/Server/Makefile @@ -10,7 +10,7 @@ OBJS = version.o main.o config.o all: tewi$(EXEC) tewi$(EXEC): $(OBJS) ../Common/common.a - $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a -lssl -lcrypto .c.o: $(CC) $(CFLAGS) -c -o $@ $< diff --git a/Server/config.c b/Server/config.c index 96b5b7f..9f6df22 100644 --- a/Server/config.c +++ b/Server/config.c @@ -9,32 +9,61 @@ #include #include -int tw_config_read(const char* path){ +struct tw_config config; + +void tw_config_init(void) {} + +int tw_config_read(const char* path) { cm_log("Config", "Reading %s", path); char cbuf[2]; cbuf[1] = 0; + int ln = 0; FILE* f = fopen(path, "r"); - if(f != NULL){ + if(f != NULL) { char* line = malloc(1); line[0] = 0; - while(1){ + int stop = 0; + char* vhost = NULL; + while(stop == 0) { int c = fread(cbuf, 1, 1, f); - if(cbuf[0] == '\n' || c <= 0){ + if(cbuf[0] == '\n' || c <= 0) { + ln++; char* l = cm_trim(line); - if(strlen(l) > 0 && l[0] != '#'){ + if(strlen(l) > 0 && l[0] != '#') { char** r = cm_split(l, " \t"); int i; - if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")){ - for(i = 1; r[i] != NULL; i++){ - if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")){ - for(i = 0; r[i] != NULL; i++) free(r[i]); - free(r); - free(line); - free(l); - fclose(f); - return 1; + if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) { + for(i = 1; r[i] != NULL; i++) { + if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) { + stop = 1; + break; } } + } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) { + if(vhost != NULL) { + cm_log("Config", "Already in virtual host section"); + stop = 1; + } else { + if(r[1] == NULL) { + cm_log("Config", "Missing virtual host"); + stop = 1; + } else { + vhost = cm_strdup(r[1]); + } + } + } else if(cm_strcaseequ(r[0], "EndVirtualHost")) { + if(vhost == NULL) { + cm_log("Config", "Not in virtual host section"); + stop = 1; + } else { + free(vhost); + vhost = NULL; + } + } else { + if(r[0] != NULL) { + cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln); + } + stop = 1; } for(i = 0; r[i] != NULL; i++) free(r[i]); free(r); @@ -44,7 +73,7 @@ int tw_config_read(const char* path){ line = malloc(1); line[0] = 0; if(c <= 0) break; - }else if(cbuf[0] != '\r'){ + } else if(cbuf[0] != '\r') { char* tmp = line; line = cm_strcat(tmp, cbuf); free(tmp); @@ -52,8 +81,8 @@ int tw_config_read(const char* path){ } free(line); fclose(f); - return 0; - }else{ + return stop; + } else { cm_log("Config", "Could not open the file"); return 1; } diff --git a/Server/main.c b/Server/main.c index 3c30f32..614f5e4 100644 --- a/Server/main.c +++ b/Server/main.c @@ -4,6 +4,8 @@ #include #include +#include + #include #include "tw_config.h" @@ -19,13 +21,13 @@ int main(int argc, char** argv) { if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) { if(!cm_do_log) { cm_do_log = true; - cm_log("", "This is Tewi HTTPd, version %s", tw_get_version()); + cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT); } else { cm_do_log = true; } - } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0){ + } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) { i++; - if(argv[i] == NULL){ + if(argv[i] == NULL) { fprintf(stderr, "Missing argument\n"); return 1; } @@ -36,7 +38,8 @@ int main(int argc, char** argv) { } } } - if(tw_config_read(config) != 0){ + tw_config_init(); + if(tw_config_read(config) != 0) { fprintf(stderr, "Could not read the config\n"); return 1; } diff --git a/Server/tw_config.h b/Server/tw_config.h index b568ab8..51d0c8f 100644 --- a/Server/tw_config.h +++ b/Server/tw_config.h @@ -3,6 +3,13 @@ #ifndef __TW_CONFIG_H__ #define __TW_CONFIG_H__ +struct tw_config_entry {}; + +struct tw_config { + struct tw_config_entry root; +}; + +void tw_config_init(void); int tw_config_read(const char* path); #endif