From 3da7713d323c2d46d1d0daeec008ecc70aa86562 Mon Sep 17 00:00:00 2001 From: Nishi Date: Sat, 14 Sep 2024 00:42:40 +0000 Subject: [PATCH] can return response now git-svn-id: file:///raid/svn-personal/tewi/trunk@18 8739d7e6-ffea-ec47-b151-bdff447c6205 --- Module/Makefile | 2 +- Module/mod_example.c | 1 + Server/config.c | 3 ++ Server/main.c | 10 ++-- Server/module.c | 17 ++++++- Server/server.c | 115 +++++++++++++++++++++++++++++++++++++++++++ Server/tw_config.h | 5 +- Server/tw_module.h | 9 ++++ Server/tw_version.h | 1 + Server/version.c | 13 +++++ 10 files changed, 170 insertions(+), 6 deletions(-) diff --git a/Module/Makefile b/Module/Makefile index acc6102..abf0b73 100644 --- a/Module/Makefile +++ b/Module/Makefile @@ -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 $@ $< diff --git a/Module/mod_example.c b/Module/mod_example.c index d170fd0..a26569f 100644 --- a/Module/mod_example.c +++ b/Module/mod_example.c @@ -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; } diff --git a/Server/config.c b/Server/config.c index df91aaa..5f5f78f 100644 --- a/Server/config.c +++ b/Server/config.c @@ -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; diff --git a/Server/main.c b/Server/main.c index e0cccb2..c55a03a 100644 --- a/Server/main.c +++ b/Server/main.c @@ -16,10 +16,13 @@ #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); diff --git a/Server/module.c b/Server/module.c index ec793ed..c7fda36 100644 --- a/Server/module.c +++ b/Server/module.c @@ -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"); diff --git a/Server/server.c b/Server/server.c index 6f9cd01..9ce3542 100644 --- a/Server/server.c +++ b/Server/server.c @@ -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 #include #include +#include #include #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 \ + "\n" \ + "\n" \ + " \n" \ + " 400 Bad Request" \ + " \n" \ + " \n" \ + "

Bad Request

\n" \ + "
\n" \ + " ", \ + address, \ + "\n" \ + " \n" \ + "\n" + +#define ERROR_401 \ + "\n" \ + "\n" \ + " \n" \ + " 401 Unauthorized" \ + " \n" \ + " \n" \ + "

Unauthorized

\n" \ + "
\n" \ + " ", \ + address, \ + "\n" \ + " \n" \ + "\n" + +#define ERROR_403 \ + "\n" \ + "\n" \ + " \n" \ + " 403 Forbidden" \ + " \n" \ + " \n" \ + "

Forbidden

\n" \ + "
\n" \ + " ", \ + address, \ + "\n" \ + " \n" \ + "\n" + +#define ERROR_404 \ + "\n" \ + "\n" \ + " \n" \ + " 404 Not Found" \ + " \n" \ + " \n" \ + "

Not Found

\n" \ + "
\n" \ + " ", \ + address, \ + "\n" \ + " \n" \ + "\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, "
%s Server at %s Port %d
", 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) { diff --git a/Server/tw_config.h b/Server/tw_config.h index 030d06e..86957b3 100644 --- a/Server/tw_config.h +++ b/Server/tw_config.h @@ -6,8 +6,8 @@ #include #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); diff --git a/Server/tw_module.h b/Server/tw_module.h index fe2a4c4..8379703 100644 --- a/Server/tw_module.h +++ b/Server/tw_module.h @@ -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); diff --git a/Server/tw_version.h b/Server/tw_version.h index 4711481..3dc7dbd 100644 --- a/Server/tw_version.h +++ b/Server/tw_version.h @@ -4,5 +4,6 @@ #define __TW_VERSION_H__ const char* tw_get_version(void); +const char* tw_get_platform(void); #endif diff --git a/Server/version.c b/Server/version.c index 4805770..eeb9d08 100644 --- a/Server/version.c +++ b/Server/version.c @@ -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; }