FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX)
-.PHONY: all format clean ./Server ./Common
+.PHONY: all format clean ./Server ./Common ./Module
-all: ./Server
+all: ./Server ./Module
./Server:: ./Common
$(MAKE) -C $@ $(FLAGS)
+./Module:: ./Common
+ $(MAKE) -C $@ $(FLAGS)
+
./Common::
$(MAKE) -C $@ $(FLAGS)
format:
- clang-format --verbose -i `find ./Server ./Common -name "*.c" -or -name "*.h"`
+ clang-format --verbose -i `find ./Server ./Common ./Module -name "*.c" -or -name "*.h"`
clean:
$(MAKE) -C ./Server $(FLAGS) clean
+ $(MAKE) -C ./Module $(FLAGS) clean
$(MAKE) -C ./Common $(FLAGS) clean
--- /dev/null
+# $Id$
+
+include $(PWD)/Platform/$(PLATFORM).mk
+
+.PHONY: all clean
+.SUFFIXES: .c .o .so
+
+all: mod_example.so
+
+.o.so:
+ $(CC) $(LDFLAGS) -shared -o $@ $< $(LIBS)
+
+.c.o:
+ $(CC) $(CFLAGS) -fPIC -c -o $@ $<
+
+clean:
+ rm -f *.o *.so *.a
--- /dev/null
+/* $Id$ */
+
+#include "../Server/tw_module.h"
+
+int mod_init(struct tw_config* config, struct tw_tool* tools) {
+ tools->log("Example", "This is an example module");
+ return 0;
+}
LDFLAGS =
LIBS =
EXEC =
+LIB = .so
LDFLAGS = -L $(PWD)/openssl/lib
LIBS = -lws2_32
EXEC = .exe
+LIB = .dll
.PHONY: all clean
.SUFFIXES: .c .o
-OBJS = version.o main.o config.o server.o ssl.o http.o
+OBJS = version.o main.o config.o server.o ssl.o http.o module.o
all: tewi$(EXEC)
#define SOURCE
#include "tw_config.h"
+#include "tw_module.h"
#include <stdio.h>
#include <stdint.h>
config.root.sslkey = NULL;
config.root.sslcert = NULL;
config.vhost_count = 0;
+ config.server_root = cm_strdup(PREFIX);
gethostname(config.hostname, 1024);
}
if(current->sslcert != NULL) free(current->sslcert);
current->sslcert = cm_strdup(r[1]);
}
+ } else if(cm_strcaseequ(r[0], "ServerRoot")) {
+ if(r[1] == NULL) {
+ cm_log("Config", "Missing path at line %d", ln);
+ stop = 1;
+ } else {
+ if(config.server_root != NULL) free(config.server_root);
+ config.server_root = cm_strdup(r[1]);
+ }
+ } else if(cm_strcaseequ(r[0], "LoadModule")) {
+ for(i = 1; r[i] != NULL; i++) {
+ void* mod = tw_module_load(r[i]);
+ if(mod != NULL) {
+ if(tw_module_init(mod) != 0) {
+ stop = 1;
+ break;
+ }
+ } else {
+ stop = 1;
+ break;
+ }
+ }
} else {
if(r[0] != NULL) {
cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
#include <stdbool.h>
#include <stdlib.h>
-#include <sys/socket.h>
+#include <string.h>
+
+#ifdef __MINGW32__
+#include <winsock2.h>
+#else
+#include <sys/select.h>
+#endif
void tw_free_request(struct tw_http_request* req) {
if(req->method != NULL) free(req->method);
--- /dev/null
+/* $Id$ */
+
+#include "tw_module.h"
+
+#include "tw_config.h"
+
+#include <cm_string.h>
+#include <cm_log.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+
+#ifdef __MINGW32__
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
+
+extern struct tw_config config;
+
+void* tw_module_load(const char* path) {
+ char* p = getcwd(NULL, 0);
+ chdir(config.server_root);
+ void* lib;
+#ifdef __MINGW32__
+ lib = LoadLibraryA(path);
+#else
+ lib = dlopen(path, DL_LAZY);
+#endif
+ if(lib == NULL) {
+ cm_log("Module", "Could not load %s", path);
+ }
+ chdir(p);
+ free(p);
+ return lib;
+}
+
+void* tw_module_symbol(void* mod, const char* sym) {
+#ifdef __MINGW32__
+ return GetProcAddress(mod, sym);
+#else
+ return dlsym(mod, sym);
+#endif
+}
+
+void tw_init_tools(struct tw_tool* tools) { tools->log = cm_log; }
+
+int tw_module_init(void* mod) {
+ tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
+ if(mod_init == NULL) {
+ cm_log("Module", "Could not init a module");
+ return 1;
+ } else {
+ struct tw_tool tools;
+ tw_init_tools(&tools);
+ return mod_init(&config, &tools);
+ }
+}
}
struct tw_http_request req;
int ret = tw_http_parse(s, sock, &req);
+ if(ret == 0) {
+ }
cleanup:
if(sslworks) {
SSL_shutdown(s);
struct tw_config_entry root;
struct tw_config_entry vhosts[MAX_VHOSTS];
int vhost_count;
+ char* server_root;
};
void tw_config_init(void);
char** headers;
};
-struct tw_http_tool {};
-
#ifdef SOURCE
#include <openssl/ssl.h>
int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req);
--- /dev/null
+/* $Id$ */
+
+#ifndef __TW_MODULE_H__
+#define __TW_MODULE_H__
+
+#include "tw_config.h"
+
+struct tw_tool {
+ void (*log)(const char* name, const char* log, ...);
+};
+
+typedef int (*tw_mod_init_t)(struct tw_config* config, struct tw_tool* tools);
+
+void* tw_module_load(const char* path);
+void* tw_module_symbol(void* mod, const char* sym);
+void tw_init_tools(struct tw_tool* tools);
+int tw_module_init(void* mod);
+
+#endif
# $Id$
# This is an example config
+LoadModule /home/nishi/SVN/tewi/trunk/Module/mod_example.so
+
Listen 8000 8001 8002 8003 8004
ListenSSL 8443 8444 8445 8446 8447