#include <unistd.h>
#include <string.h>
#include <signal.h>
+#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
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;
int sock = accept(server_socket, (struct sockaddr*)&claddr, &clen);
pid_t pid = fork();
if(pid == 0){
+ http_handler(sock);
_exit(0);
}else{
close(sock);