]> Nishi Git Mirror - mandshurica.git/commitdiff
probably works
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 10:00:51 +0000 (10:00 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 10:00:51 +0000 (10:00 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@20 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Module/http.c

index 595690d4defb86bb9eb649d57beddd666f46b9d0..e13052bebcb40b87d515c1f092b70ab4a4dd6e77 100644 (file)
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <signal.h>
+#include <stdbool.h>
 #include <sys/types.h>
 
 #include <sys/socket.h>
@@ -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);