]> Git repositories of Nishi - tewi.git/commitdiff
can return response now
authorNishi <nishi@nishi.boats>
Sat, 14 Sep 2024 00:42:40 +0000 (00:42 +0000)
committerNishi <nishi@nishi.boats>
Sat, 14 Sep 2024 00:42:40 +0000 (00:42 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@18 8739d7e6-ffea-ec47-b151-bdff447c6205

Module/Makefile
Module/mod_example.c
Server/config.c
Server/main.c
Server/module.c
Server/server.c
Server/tw_config.h
Server/tw_module.h
Server/tw_version.h
Server/version.c

index acc6102a2166459d0bcc6517f0f7ec2445fe8c0b..abf0b73372ca97da649c815cf23c14e6287e383b 100644 (file)
@@ -8,7 +8,7 @@ include $(PWD)/Platform/$(PLATFORM).mk
 all: mod_example.so
 
 .o.so:
-       $(CC) $(LDFLAGS) -shared -o $@ $< $(LIBS)
+       $(CC) $(LDFLAGS) -shared -o $@ $< ../Common/common.a $(LIBS)
 
 .c.o:
        $(CC) $(CFLAGS) -fPIC -c -o $@ $<
index d170fd05d38cf10798ec1e2eda3203c26a8f98f9..a26569f5a4dc488c55ae8d9877c42565a374ff6d 100644 (file)
@@ -4,5 +4,6 @@
 
 int mod_init(struct tw_config* config, struct tw_tool* tools) {
        tools->log("Example", "This is an example module");
+       tools->add_version("Example/0.0");
        return 0;
 }
index df91aaa999bb97038c6b14879a3852786dc625d1..5f5f78f8734c6fe1c76db361f6e83ef0d7073d6e 100644 (file)
@@ -38,6 +38,8 @@ void tw_config_init(void) {
        config.root.sslkey = NULL;
        config.root.sslcert = NULL;
        config.vhost_count = 0;
+       config.module_count = 0;
+       config.extension = NULL;
        config.server_root = cm_strdup(PREFIX);
        gethostname(config.hostname, 1024);
 }
@@ -139,6 +141,7 @@ int tw_config_read(const char* path) {
                                                for(i = 1; r[i] != NULL; i++) {
                                                        void* mod = tw_module_load(r[i]);
                                                        if(mod != NULL) {
+                                                               config.modules[config.module_count++] = mod;
                                                                if(tw_module_init(mod) != 0) {
                                                                        stop = 1;
                                                                        break;
index e0cccb2c924d32ca18704513fec7bd878bf44e2d..c55a03adcbb78e2f488b0c2f108688706bb86963 100644 (file)
 #include "tw_version.h"
 
 extern bool cm_do_log;
+extern struct tw_config config;
+
+char tw_server[2048];
 
 int main(int argc, char** argv) {
        int i;
-       const char* config = PREFIX "/etc/tewi.conf";
+       const char* confpath = PREFIX "/etc/tewi.conf";
        for(i = 1; i < argc; i++) {
                if(argv[i][0] == '-') {
                        if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
@@ -35,7 +38,7 @@ int main(int argc, char** argv) {
                                        fprintf(stderr, "Missing argument\n");
                                        return 1;
                                }
-                               config = argv[i];
+                               confpath = argv[i];
                        } else {
                                fprintf(stderr, "Unknown option: %s\n", argv[i]);
                                return 1;
@@ -43,7 +46,7 @@ int main(int argc, char** argv) {
                }
        }
        tw_config_init();
-       if(tw_config_read(config) != 0) {
+       if(tw_config_read(confpath) != 0) {
                fprintf(stderr, "Could not read the config\n");
                return 1;
        }
@@ -51,6 +54,7 @@ int main(int argc, char** argv) {
                fprintf(stderr, "Could not initialize the server\n");
                return 1;
        }
+       sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
        cm_log("Daemon", "Ready");
 #ifndef __MINGW32__
        signal(SIGCHLD, SIG_IGN);
index ec793ed9991c73abe79a3200b1f42237e177bf79..c7fda36779d80baa0190ae0c02d22e7178e30ef0 100644 (file)
@@ -1,5 +1,7 @@
 /* $Id$ */
 
+#define SOURCE
+
 #include "tw_module.h"
 
 #include "tw_config.h"
@@ -43,7 +45,20 @@ void* tw_module_symbol(void* mod, const char* sym) {
 #endif
 }
 
-void tw_init_tools(struct tw_tool* tools) { tools->log = cm_log; }
+void tw_add_version(const char* string) {
+       if(config.extension == NULL) {
+               config.extension = cm_strcat(" ", string);
+       } else {
+               char* tmp = config.extension;
+               config.extension = cm_strcat3(tmp, " ", string);
+               free(tmp);
+       }
+}
+
+void tw_init_tools(struct tw_tool* tools) {
+       tools->log = cm_log;
+       tools->add_version = tw_add_version;
+}
 
 int tw_module_init(void* mod) {
        tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
index 6f9cd016bbd7cafc0ec2b4903a3ce21a955fb42f..9ce354290c204047dda1af91d552450162cfce8c 100644 (file)
@@ -7,11 +7,14 @@
 #include "tw_ssl.h"
 #include "tw_config.h"
 #include "tw_http.h"
+#include "tw_module.h"
+#include "tw_version.h"
 
 #include <unistd.h>
 #include <string.h>
 #include <stdbool.h>
 
+#include <cm_string.h>
 #include <cm_log.h>
 
 #ifdef __MINGW32__
@@ -27,6 +30,7 @@
 #endif
 
 extern struct tw_config config;
+extern char tw_server[];
 
 fd_set fdset;
 int sockcount = 0;
@@ -131,6 +135,115 @@ size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
        }
 }
 
+#define ERROR_400 \
+       "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
+       "<html>\n" \
+       "       <head>\n" \
+       "               <title>400 Bad Request</title>" \
+       "       </head>\n" \
+       "       <body>\n" \
+       "               <h1>Bad Request</h1>\n" \
+       "               <hr>\n" \
+       "               ", \
+           address, \
+           "\n" \
+           "   </body>\n" \
+           "</html>\n"
+
+#define ERROR_401 \
+       "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
+       "<html>\n" \
+       "       <head>\n" \
+       "               <title>401 Unauthorized</title>" \
+       "       </head>\n" \
+       "       <body>\n" \
+       "               <h1>Unauthorized</h1>\n" \
+       "               <hr>\n" \
+       "               ", \
+           address, \
+           "\n" \
+           "   </body>\n" \
+           "</html>\n"
+
+#define ERROR_403 \
+       "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
+       "<html>\n" \
+       "       <head>\n" \
+       "               <title>403 Forbidden</title>" \
+       "       </head>\n" \
+       "       <body>\n" \
+       "               <h1>Forbidden</h1>\n" \
+       "               <hr>\n" \
+       "               ", \
+           address, \
+           "\n" \
+           "   </body>\n" \
+           "</html>\n"
+
+#define ERROR_404 \
+       "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
+       "<html>\n" \
+       "       <head>\n" \
+       "               <title>404 Not Found</title>" \
+       "       </head>\n" \
+       "       <body>\n" \
+       "               <h1>Not Found</h1>\n" \
+       "               <hr>\n" \
+       "               ", \
+           address, \
+           "\n" \
+           "   </body>\n" \
+           "</html>\n"
+
+void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, const unsigned char* doc, size_t size) {
+       char construct[512];
+       sprintf(construct, "%llu", (unsigned long long)size);
+       tw_write(ssl, sock, "HTTP/1.1 ", 9);
+       tw_write(ssl, sock, (char*)status, strlen(status));
+       tw_write(ssl, sock, "\r\n", 2);
+       tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
+       tw_write(ssl, sock, (char*)type, strlen(type));
+       tw_write(ssl, sock, "\r\n", 2);
+       tw_write(ssl, sock, "Server: ", 6 + 2);
+       tw_write(ssl, sock, tw_server, strlen(tw_server));
+       tw_write(ssl, sock, "\r\n", 2);
+       tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
+       tw_write(ssl, sock, construct, strlen(construct));
+       tw_write(ssl, sock, "\r\n", 2);
+       tw_write(ssl, sock, "\r\n", 2);
+       size_t incr = 0;
+       while(1) {
+               tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
+               incr += 128;
+               size -= 128;
+               if(size <= 0) break;
+       }
+}
+
+const char* tw_http_status(int code) {
+       if(code == 400) {
+               return "400 Bad Request";
+       } else {
+               return "400 Bad Request";
+       }
+}
+
+char* tw_http_default_error(int code, char* name, int port) {
+       char address[1024];
+       sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
+       if(code == 400) {
+               return cm_strcat3(ERROR_400);
+       } else {
+               return cm_strcat3(ERROR_400);
+       }
+}
+
+void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) {
+       char* str = tw_http_default_error(error, name, port);
+       tw_process_page(ssl, sock, tw_http_status(error), "text/html", str, strlen(str));
+       free(str);
+}
+
 #ifdef __MINGW32__
 struct pass_entry {
        int sock;
@@ -161,6 +274,8 @@ void tw_server_pass(int sock, bool ssl, int port) {
        struct tw_http_request req;
        int ret = tw_http_parse(s, sock, &req);
        if(ret == 0) {
+       } else {
+               tw_http_error(s, sock, 400, name, port);
        }
 cleanup:
        if(sslworks) {
index 030d06ed5efa96e2178d954e33eb1059b8476a75..86957b344c7d3df7527fabacc350bd75a144d670 100644 (file)
@@ -6,8 +6,8 @@
 #include <stdint.h>
 
 #define MAX_PORTS 1024
-
 #define MAX_VHOSTS 1024
+#define MAX_MODULES 1024
 
 struct tw_config_entry {
        char* name;
@@ -21,8 +21,11 @@ struct tw_config {
        char hostname[1025];
        struct tw_config_entry root;
        struct tw_config_entry vhosts[MAX_VHOSTS];
+       void* modules[MAX_MODULES];
+       int module_count;
        int vhost_count;
        char* server_root;
+       char* extension;
 };
 
 void tw_config_init(void);
index fe2a4c44373995b99454c2b1015b27ab190d84bf..8379703bdcb9fb89b01653c14c60b4998bc4ad1f 100644 (file)
@@ -4,12 +4,21 @@
 #define __TW_MODULE_H__
 
 #include "tw_config.h"
+#include "tw_http.h"
 
 struct tw_tool {
        void (*log)(const char* name, const char* log, ...);
+       void (*add_version)(const char* string);
+};
+
+enum TW_MODULE_RETURN {
+       TW_MODULE_PASS = 0, /* Pass to the next module. */
+       TW_MODULE_STOP,     /* Do not pass to the next module. */
+       TW_MODULE_ERROR     /* Error, and do not pass to the next module. */
 };
 
 typedef int (*tw_mod_init_t)(struct tw_config* config, struct tw_tool* tools);
+typedef int (*tw_mod_request_t)(struct tw_tool* tools, struct tw_http_request* req, struct tw_http_response* res);
 
 void* tw_module_load(const char* path);
 void* tw_module_symbol(void* mod, const char* sym);
index 47114817dd957b220487a20c4f632180cd054abb..3dc7dbd5beb7189da11b97afac43a1285f6209a1 100644 (file)
@@ -4,5 +4,6 @@
 #define __TW_VERSION_H__
 
 const char* tw_get_version(void);
+const char* tw_get_platform(void);
 
 #endif
index 480577016a1d64e7d6eb4256f702aca276d106af..eeb9d0876415d564331ace7f3c04f5e2f59daacd 100644 (file)
@@ -6,4 +6,17 @@
 
 const char* tw_version = "0.00";
 
+const char* tw_platform =
+#if defined(PLATFORM)
+    PLATFORM
+#elif defined(__NetBSD__)
+    "NetBSD"
+#elif defined(__MINGW32__)
+    "Windows"
+#else
+    "Unix"
+#endif
+    ;
+
 const char* tw_get_version(void) { return tw_version; }
+const char* tw_get_platform(void) { return tw_platform; }