From 07692d596b2914c34f02a6ff378367856df6f7f1 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 13 Apr 2024 10:00:51 +0000 Subject: [PATCH] probably works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@20 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Module/http.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/Module/http.c b/Module/http.c index 595690d..e13052b 100644 --- a/Module/http.c +++ b/Module/http.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -58,6 +59,74 @@ int mod_init(struct devforge_config* _config) { return 0; } +#define BUFFER_SIZE 512 + +void http_handler(int sock) { + char* buf = malloc(BUFFER_SIZE); + char* method = NULL; + char* path = NULL; + char cbuf[2]; + cbuf[1] = 0; + while(true){ + int state; + int count; +reset:; + state = 0; + count = 0; + if(method != NULL){ + free(method); + } + if(path != NULL){ + free(path); + } + while(true){ + int len = recv(sock, buf, BUFFER_SIZE, 0); + if(len <= 0) goto goquit; + int i; + for(i = 0; i < len; i++){ + cbuf[0] = buf[i]; + if(state == 0){ + if(buf[i] == ' '){ + state = 1; + }else{ + char* tmp = method; + method = devforge_strcat(tmp == NULL ? "" : tmp, cbuf); + if(tmp != NULL) free(tmp); + } + }else if(state == 1){ + if(buf[i] == ' '){ + state = 2; + }else{ + char* tmp = path; + path = devforge_strcat(tmp == NULL ? "" : tmp, cbuf); + if(tmp != NULL) free(tmp); + } + }else if(state == 2){ + if(buf[i] == '\r'){ + }else if(buf[i] == '\n'){ + state = 3; + } + }else if(state == 3){ + if(buf[i] == '\r'){ + }else if(buf[i] == '\n'){ + count++; + if(count == 2){ + if(strcmp(method, "GET") == 0){ + goto reset; + } + state = 0; + } + }else{ + count = 0; + } + } + } + } + } +goquit:; + free(buf); +} + void* mod_server_thread(void* ptr) { int* ret = malloc(sizeof(*ret)); *ret = 0; @@ -123,6 +192,7 @@ void* mod_server_thread(void* ptr) { int sock = accept(server_socket, (struct sockaddr*)&claddr, &clen); pid_t pid = fork(); if(pid == 0){ + http_handler(sock); _exit(0); }else{ close(sock); -- 2.43.0