can return response now

git-svn-id: file:///raid/svn-personal/tewi/trunk@18 8739d7e6-ffea-ec47-b151-bdff447c6205
This commit is contained in:
Nishi 2024-09-14 00:42:40 +00:00
parent 219b8523ce
commit 3da7713d32
10 changed files with 170 additions and 6 deletions

View 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 $@ $<

View 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;
}

View 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;

View File

@ -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);

View 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");

View 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) {

View 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);

View 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);

View File

@ -4,5 +4,6 @@
#define __TW_VERSION_H__
const char* tw_get_version(void);
const char* tw_get_platform(void);
#endif

View 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; }