]> Git repositories of Nishi - tewi.git/commitdiff
vhost
authorNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 10:28:20 +0000 (10:28 +0000)
committerNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 10:28:20 +0000 (10:28 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@6 8739d7e6-ffea-ec47-b151-bdff447c6205

Common/Makefile
Common/log.c
Common/string.c
Makefile
Platform/generic.mk
Platform/win64.mk [new file with mode: 0644]
Server/Makefile
Server/config.c
Server/main.c
Server/tw_config.h

index e6eccda7c2c7714ffd08e0a9ce2cc2d63a3d6802..22880a98f57d40ac3694a60c756d72161d7004d5 100644 (file)
@@ -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 $@ $<
index 6eb7bedf469fe8ad89afab7a54f3178884b31ae8..5d812a60b02c836332a3f21dbef6dd233159d836 100644 (file)
@@ -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];
index d5eccb058648102efa6ee4521ca8734da9991be6..bd344584da5b59157a5207058e85546e1476e105 100644 (file)
@@ -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;
index f5e456df6282720fe1f4fb89701d5ddac3186967..34e6226e349651381efee960b2e54712671d8ef9 100644 (file)
--- 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
index d0e4948e838b2d567415d67b06aa345da69eefe3..aa2147469b160b7f5163f4c8828c888074da1fd7 100644 (file)
@@ -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 (file)
index 0000000..94763f9
--- /dev/null
@@ -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
index bc170491b31ab8d88ce99bd4c212b5c3a34a0bd8..26a59e1d779fbed1017d98a98ecef023f3730160 100644 (file)
@@ -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 $@ $<
index 96b5b7f96d60f42f2a12037777c182dc498a2239..9f6df22c2ddcb1b7813291e1fe2f1e34971adeb0 100644 (file)
@@ -9,32 +9,61 @@
 #include <cm_string.h>
 #include <cm_log.h>
 
-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;
        }
index 3c30f32bdeaf1d740e4ca5e51784ccf3f69b84b4..614f5e4430e9e12beba6845901573dd5ab5f7b0a 100644 (file)
@@ -4,6 +4,8 @@
 #include <stdbool.h>
 #include <string.h>
 
+#include <openssl/opensslv.h>
+
 #include <cm_log.h>
 
 #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;
        }
index b568ab8f18b80c654e60af4002cb0151a2597c18..51d0c8f9126d63d0b9a27a6290048a2a1b630e3e 100644 (file)
@@ -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