- Timestamp:
- Sep 14, 2024, 9:42:40 AM (2 months ago)
- Location:
- trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Module/Makefile
r17 r18 9 9 10 10 .o.so: 11 $(CC) $(LDFLAGS) -shared -o $@ $< $(LIBS)11 $(CC) $(LDFLAGS) -shared -o $@ $< ../Common/common.a $(LIBS) 12 12 13 13 .c.o: -
trunk/Module/mod_example.c
r17 r18 5 5 int mod_init(struct tw_config* config, struct tw_tool* tools) { 6 6 tools->log("Example", "This is an example module"); 7 tools->add_version("Example/0.0"); 7 8 return 0; 8 9 } -
trunk/Server/config.c
r17 r18 39 39 config.root.sslcert = NULL; 40 40 config.vhost_count = 0; 41 config.module_count = 0; 42 config.extension = NULL; 41 43 config.server_root = cm_strdup(PREFIX); 42 44 gethostname(config.hostname, 1024); … … 140 142 void* mod = tw_module_load(r[i]); 141 143 if(mod != NULL) { 144 config.modules[config.module_count++] = mod; 142 145 if(tw_module_init(mod) != 0) { 143 146 stop = 1; -
trunk/Server/main.c
r16 r18 17 17 18 18 extern bool cm_do_log; 19 extern struct tw_config config; 20 21 char tw_server[2048]; 19 22 20 23 int main(int argc, char** argv) { 21 24 int i; 22 const char* conf ig= PREFIX "/etc/tewi.conf";25 const char* confpath = PREFIX "/etc/tewi.conf"; 23 26 for(i = 1; i < argc; i++) { 24 27 if(argv[i][0] == '-') { … … 36 39 return 1; 37 40 } 38 conf ig= argv[i];41 confpath = argv[i]; 39 42 } else { 40 43 fprintf(stderr, "Unknown option: %s\n", argv[i]); … … 44 47 } 45 48 tw_config_init(); 46 if(tw_config_read(conf ig) != 0) {49 if(tw_config_read(confpath) != 0) { 47 50 fprintf(stderr, "Could not read the config\n"); 48 51 return 1; … … 52 55 return 1; 53 56 } 57 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension); 54 58 cm_log("Daemon", "Ready"); 55 59 #ifndef __MINGW32__ -
trunk/Server/module.c
r17 r18 1 1 /* $Id$ */ 2 3 #define SOURCE 2 4 3 5 #include "tw_module.h" … … 44 46 } 45 47 46 void tw_init_tools(struct tw_tool* tools) { tools->log = cm_log; } 48 void tw_add_version(const char* string) { 49 if(config.extension == NULL) { 50 config.extension = cm_strcat(" ", string); 51 } else { 52 char* tmp = config.extension; 53 config.extension = cm_strcat3(tmp, " ", string); 54 free(tmp); 55 } 56 } 57 58 void tw_init_tools(struct tw_tool* tools) { 59 tools->log = cm_log; 60 tools->add_version = tw_add_version; 61 } 47 62 48 63 int tw_module_init(void* mod) { -
trunk/Server/server.c
r17 r18 8 8 #include "tw_config.h" 9 9 #include "tw_http.h" 10 #include "tw_module.h" 11 #include "tw_version.h" 10 12 11 13 #include <unistd.h> … … 13 15 #include <stdbool.h> 14 16 17 #include <cm_string.h> 15 18 #include <cm_log.h> 16 19 … … 28 31 29 32 extern struct tw_config config; 33 extern char tw_server[]; 30 34 31 35 fd_set fdset; … … 130 134 return SSL_write(ssl, data, len); 131 135 } 136 } 137 138 #define ERROR_400 \ 139 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 140 "<html>\n" \ 141 " <head>\n" \ 142 " <title>400 Bad Request</title>" \ 143 " </head>\n" \ 144 " <body>\n" \ 145 " <h1>Bad Request</h1>\n" \ 146 " <hr>\n" \ 147 " ", \ 148 address, \ 149 "\n" \ 150 " </body>\n" \ 151 "</html>\n" 152 153 #define ERROR_401 \ 154 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 155 "<html>\n" \ 156 " <head>\n" \ 157 " <title>401 Unauthorized</title>" \ 158 " </head>\n" \ 159 " <body>\n" \ 160 " <h1>Unauthorized</h1>\n" \ 161 " <hr>\n" \ 162 " ", \ 163 address, \ 164 "\n" \ 165 " </body>\n" \ 166 "</html>\n" 167 168 #define ERROR_403 \ 169 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 170 "<html>\n" \ 171 " <head>\n" \ 172 " <title>403 Forbidden</title>" \ 173 " </head>\n" \ 174 " <body>\n" \ 175 " <h1>Forbidden</h1>\n" \ 176 " <hr>\n" \ 177 " ", \ 178 address, \ 179 "\n" \ 180 " </body>\n" \ 181 "</html>\n" 182 183 #define ERROR_404 \ 184 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 185 "<html>\n" \ 186 " <head>\n" \ 187 " <title>404 Not Found</title>" \ 188 " </head>\n" \ 189 " <body>\n" \ 190 " <h1>Not Found</h1>\n" \ 191 " <hr>\n" \ 192 " ", \ 193 address, \ 194 "\n" \ 195 " </body>\n" \ 196 "</html>\n" 197 198 void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, const unsigned char* doc, size_t size) { 199 char construct[512]; 200 sprintf(construct, "%llu", (unsigned long long)size); 201 tw_write(ssl, sock, "HTTP/1.1 ", 9); 202 tw_write(ssl, sock, (char*)status, strlen(status)); 203 tw_write(ssl, sock, "\r\n", 2); 204 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2); 205 tw_write(ssl, sock, (char*)type, strlen(type)); 206 tw_write(ssl, sock, "\r\n", 2); 207 tw_write(ssl, sock, "Server: ", 6 + 2); 208 tw_write(ssl, sock, tw_server, strlen(tw_server)); 209 tw_write(ssl, sock, "\r\n", 2); 210 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2); 211 tw_write(ssl, sock, construct, strlen(construct)); 212 tw_write(ssl, sock, "\r\n", 2); 213 tw_write(ssl, sock, "\r\n", 2); 214 size_t incr = 0; 215 while(1) { 216 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128); 217 incr += 128; 218 size -= 128; 219 if(size <= 0) break; 220 } 221 } 222 223 const char* tw_http_status(int code) { 224 if(code == 400) { 225 return "400 Bad Request"; 226 } else { 227 return "400 Bad Request"; 228 } 229 } 230 231 char* tw_http_default_error(int code, char* name, int port) { 232 char address[1024]; 233 sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port); 234 if(code == 400) { 235 return cm_strcat3(ERROR_400); 236 } else { 237 return cm_strcat3(ERROR_400); 238 } 239 } 240 241 void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) { 242 char* str = tw_http_default_error(error, name, port); 243 tw_process_page(ssl, sock, tw_http_status(error), "text/html", str, strlen(str)); 244 free(str); 132 245 } 133 246 … … 162 275 int ret = tw_http_parse(s, sock, &req); 163 276 if(ret == 0) { 277 } else { 278 tw_http_error(s, sock, 400, name, port); 164 279 } 165 280 cleanup: -
trunk/Server/tw_config.h
r17 r18 7 7 8 8 #define MAX_PORTS 1024 9 10 9 #define MAX_VHOSTS 1024 10 #define MAX_MODULES 1024 11 11 12 12 struct tw_config_entry { … … 22 22 struct tw_config_entry root; 23 23 struct tw_config_entry vhosts[MAX_VHOSTS]; 24 void* modules[MAX_MODULES]; 25 int module_count; 24 26 int vhost_count; 25 27 char* server_root; 28 char* extension; 26 29 }; 27 30 -
trunk/Server/tw_module.h
r17 r18 5 5 6 6 #include "tw_config.h" 7 #include "tw_http.h" 7 8 8 9 struct tw_tool { 9 10 void (*log)(const char* name, const char* log, ...); 11 void (*add_version)(const char* string); 12 }; 13 14 enum TW_MODULE_RETURN { 15 TW_MODULE_PASS = 0, /* Pass to the next module. */ 16 TW_MODULE_STOP, /* Do not pass to the next module. */ 17 TW_MODULE_ERROR /* Error, and do not pass to the next module. */ 10 18 }; 11 19 12 20 typedef int (*tw_mod_init_t)(struct tw_config* config, struct tw_tool* tools); 21 typedef int (*tw_mod_request_t)(struct tw_tool* tools, struct tw_http_request* req, struct tw_http_response* res); 13 22 14 23 void* tw_module_load(const char* path); -
trunk/Server/tw_version.h
r3 r18 5 5 6 6 const char* tw_get_version(void); 7 const char* tw_get_platform(void); 7 8 8 9 #endif -
trunk/Server/version.c
r16 r18 7 7 const char* tw_version = "0.00"; 8 8 9 const char* tw_platform = 10 #if defined(PLATFORM) 11 PLATFORM 12 #elif defined(__NetBSD__) 13 "NetBSD" 14 #elif defined(__MINGW32__) 15 "Windows" 16 #else 17 "Unix" 18 #endif 19 ; 20 9 21 const char* tw_get_version(void) { return tw_version; } 22 const char* tw_get_platform(void) { return tw_platform; }
Note:
See TracChangeset
for help on using the changeset viewer.