From: nishi Date: Sat, 17 Feb 2024 01:27:06 +0000 (+0000) Subject: httpd X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=bb83dae3b98729d6defb9970643d35cbd0306478;p=libw3.git httpd git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@235 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/httpd/Makefile b/Example/httpd/Makefile index 13fb04e..81b59a3 100644 --- a/Example/httpd/Makefile +++ b/Example/httpd/Makefile @@ -2,7 +2,7 @@ .PHONY: clean install ./httpd$(SUFFIX): ./httpd.c $(RESFILE) - $(CC) -g -o $@ -I ../../Library -L ../../Library $^ -lw3 + $(CC) -g -o $@ -I ../../Library -L ../../Library $^ -lw3 -lserver ../libw3.res: $(MAKE) -C .. ./libw3.res WINDRES=$(WINDRES) diff --git a/Example/httpd/httpd.c b/Example/httpd/httpd.c index 7dc4603..131ee29 100644 --- a/Example/httpd/httpd.c +++ b/Example/httpd/httpd.c @@ -5,13 +5,168 @@ */ #include +#include +#include #include #include +#include +#include +#include +#include + +#include + +#define BUFFER_SIZE 512 + +char* badreq; +char* badreq_header; + +void http_handler(int sock){ + char* buf = malloc(BUFFER_SIZE); + int phase = 0; + /* 0: Method + 1: Path + 2: Version + 3: Header + 4: Body + */ + char* method = malloc(1); + method[0] = 0; + char* path = malloc(1); + path[0] = 0; + char* version = malloc(1); + version[0] = 0; + char* cbuf = malloc(2); + cbuf[1] = 0; + char* line = malloc(1); + line[0] = 0; + while(true){ + int len = recv(sock, buf, BUFFER_SIZE, 0); + if(len <= 0) break; + int i; + for(i = 0; i < len; i++){ + if(phase == 0){ + if(buf[i] == ' '){ + phase++; + }else{ + cbuf[0] = buf[i]; + char* tmp = method; + method = __W3_Concat(tmp, cbuf); + free(tmp); + } + }else if(phase == 1){ + if(buf[i] == ' '){ + phase++; + }else{ + cbuf[0] = buf[i]; + char* tmp = path; + path = __W3_Concat(tmp, cbuf); + free(tmp); + } + }else if(phase == 2){ + if(buf[i] == '\n'){ + phase++; + if(strlen(version) < 8){ + send(sock, badreq_header, strlen(badreq_header), 0); + send(sock, badreq, strlen(badreq), 0); + goto quit; + }else{ + if(strcmp(version + 5, "1.1") == 0 || strcmp(version + 5, "1.0") == 0){ + }else{ + send(sock, badreq_header, strlen(badreq_header), 0); + send(sock, badreq, strlen(badreq), 0); + goto quit; + } + } + }else if(buf[i] != '\r'){ + cbuf[0] = buf[i]; + char* tmp = version; + version = __W3_Concat(tmp, cbuf); + free(tmp); + } + }else if(phase == 3){ + if(buf[i] == '\n'){ + if(strcmp(line, "") == 0) phase++; + free(line); + line = malloc(1); + line[0] = 0; + if(phase == 4){ + if(strcmp(method, "GET") == 0) goto quit; + } + }else if(buf[i] != '\r'){ + cbuf[0] = buf[i]; + char* tmp = line; + line = __W3_Concat(tmp, cbuf); + free(tmp); + } + }else if(phase == 4){ + } + } + } +quit:; + free(line); + free(method); + free(path); + free(version); + free(cbuf); +} int main(int argc, char** argv) { - if(argv[1] != NULL && strcmp(argv[1], "--version") == 0) { - printf("LibW3 %s\n", LIBW3_VERSION); - return 0; + int i; + char* portstr = NULL; + char* configfile = "/etc/httpd.conf"; + for(i = 1; argv[i] != NULL; i++){ + if(strcmp(argv[i], "--version") == 0) { + printf("LibW3 %s\n", LIBW3_VERSION); + return 0; + }else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-c") == 0) { + configfile = argv[i + 1]; + }else if(strcmp(argv[i], "--port") == 0 || strcmp(argv[i], "-p") == 0) { + portstr = argv[i + 1]; + }else if(argv[i][0] == '-'){ + fprintf(stderr, "%s: invalid option: %s\n", argv[0], argv[i]); + return 1; + } + } + badreq = __W3_Strdup( +"" \ +" " \ +" " \ +" " \ +" " \ +"

Bad request

" \ +"
" \ +" LibW3/" LIBW3_VERSION "" \ +" " \ +"" +); + badreq_header = malloc(2048); + sprintf(badreq_header, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n", strlen(badreq)); + + FILE* f = fopen(configfile, "r"); + if(f != NULL){ + struct stat s; + stat(configfile, &s); + char* file = malloc(s.st_size + 1); + file[s.st_size] = 0; + fread(file, s.st_size, 1, f); + + /* TODO: Parse the config file */ + + free(file); + fclose(f); + }else{ + free(badreq_header); + free(badreq); + fprintf(stderr, "%s: failed to load the config file\n", argv[0]); + return 1; + } + + W3_Library_Init(); + int st = ls_start_server(portstr == NULL ? 80 : atoi(portstr), http_handler); + if(st == -1){ + fprintf(stderr, "%s: failed to start the server\n", argv[0]); + return 1; } } diff --git a/Example/httpd/httpd.conf b/Example/httpd/httpd.conf new file mode 100644 index 0000000..98fd1a6 --- /dev/null +++ b/Example/httpd/httpd.conf @@ -0,0 +1,2 @@ +# $Id$ +Root /var/www diff --git a/W3Version.h.m4 b/W3Version.h.m4 index 7e9a7e4..0e0de23 100644 --- a/W3Version.h.m4 +++ b/W3Version.h.m4 @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.16" \ +#define LIBW3_VERSION "2.16A" \ SUFFIX ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `')