#include "http.h" #include "native.h" #include #include #include #include #include #include const uint16_t port = 8201; int server_socket = 0; void int_handler(int a) { (void)a; printf("=== terminated ===\n"); if (server_socket != 0) close(server_socket); exit(1); } int main(void) { init_sockets(); signal(SIGINT, &int_handler); printf("starting server...\n"); server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket < 0) { printf("error: could not open socket\n"); return 1; } struct sockaddr_in server; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = AF_INET; server.sin_port = htons(port); if (bind(server_socket, (struct sockaddr*)&server, sizeof(server)) > 0) { printf("error: could not bind socket\n"); return 1; } if (listen(server_socket, SOMAXCONN) < 0) { printf("error: could not listen on server"); return 1; } printf("listening on port %d\n", port); while (true) { struct sockaddr_in client; socklen_t client_size; printf("waiting for client...\n"); int client_socket = accept(server_socket, (struct sockaddr*)&client, &client_size); if (client_socket < 0) { printf("error: could not accept connection\n"); continue; } printf("client connected\n"); while (true) { uint8_t buffer[8192] = { 0 }; ssize_t read_size = recv(client_socket, buffer, 8192, 0); if (read_size < 0) { printf("error: could not recieve\n"); close_socket(client_socket); break; } else if (read_size == 0) { printf("client disconnected\n"); close_socket(client_socket); break; } printf("=== recieved start ===\n%s\n=== recieved end ===\n", buffer); HttpRequestHeader header = parse_http_request_header((char*)buffer, strlen((char*)buffer)); printf("method: %d\n", header.method); printf("path_index: %ld\n", header.path_index); printf("path_length: %ld\n", header.path_length); printf("content_length: %ld\n", header.content_length); printf("body_index: %ld\n", header.body_index); char* path = calloc(header.path_length + 1, sizeof(char)); strncpy(path, (char*)&buffer[header.path_index], header.path_length); printf("path: %s\n", path); if (strncmp(path, "/api", 4) == 0) { // something something api } else { if (strstr(path, "..") != NULL) { uint8_t send_buffer[] = "HTTP/1.1 400 BAD\r\n" "Content-Type: text/html\r\n" "\r\n" "Bad " "request

Fuck you!

\r\n"; ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0); if (written < 0) { printf("error: could not write\n"); close_socket(client_socket); break; } } else if (header.path_length == 0 || strncmp(path, "/", header.path_length) == 0) { FILE* file = fopen("../frontend/index.html", "r"); if (file == NULL) { printf("error: could not open file\n"); close_socket(client_socket); break; } uint8_t send_buffer[] = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "\r\n"; ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0); if (written < 0) { printf("error: could not write\n"); close_socket(client_socket); break; } char char_read; while ((char_read = (char)fgetc(file)) != EOF) { write(client_socket, &char_read, sizeof(char)); } } else { char rootpath[] = "../frontend"; size_t filepath_size = sizeof(rootpath) + header.path_length + 1; char* filepath = calloc(filepath_size, sizeof(char)); snprintf(filepath, filepath_size, "%s%s", rootpath, path); char* dot = strrchr(path, '.'); char mime_type[20] = { 0 }; char file_flag[3] = { 'r', 0 }; if (dot != NULL && strncmp(dot, ".html", 5) == 0) { snprintf(mime_type, 20, "text/html"); } else if (dot != NULL && strncmp(dot, ".css", 4) == 0) { snprintf(mime_type, 20, "text/css"); } else if (dot != NULL && strncmp(dot, ".js", 3) == 0) { snprintf(mime_type, 20, "text/javascript"); } else if (dot != NULL && strncmp(dot, ".ico", 4) == 0) { snprintf(mime_type, 20, "image/x-icon"); } else if (dot != NULL && strncmp(dot, ".jpg", 4) == 0) { snprintf(mime_type, 20, "image/jpeg"); file_flag[1] = 'b'; } else if (dot != NULL && strncmp(dot, ".png", 4) == 0) { snprintf(mime_type, 20, "image/png"); file_flag[1] = 'b'; } else if (dot != NULL && strncmp(dot, ".woff2", 6) == 0) { snprintf(mime_type, 20, "font/woff2"); file_flag[1] = 'b'; } else { printf("error: unknown file type\n"); close_socket(client_socket); break; } printf("\n %s | %s \n", file_flag, filepath); FILE* file = fopen(filepath, file_flag); if (file == NULL) { printf("error: could not open file\n"); close_socket(client_socket); break; } char send_buffer_1[] = "HTTP/1.1 200 OK\r\n" "Content-Type: "; char send_buffer_2[] = "\r\n" "\r\n"; ssize_t written = send(client_socket, send_buffer_1, strlen(send_buffer_1) - 1, 0); if (written < 0) { printf("error: could not write\n"); close_socket(client_socket); break; } written = send(client_socket, mime_type, strlen(mime_type), 0); if (written < 0) { printf("error: could not write\n"); close_socket(client_socket); break; } written = send(client_socket, send_buffer_2, strlen(send_buffer_2), 0); if (written < 0) { printf("error: could not write\n"); close_socket(client_socket); break; } int char_read; while ((char_read = fgetc(file)) != EOF) { write(client_socket, (char*)&char_read, sizeof(char)); } free(filepath); fclose(file); } } free(path); /* uint8_t send_buffer[] = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "\r\n" "hej med " "dig

fuck c hashtag

\r\n" "\r\n"; ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0); if (written < 0) { printf("error: could not write\n"); close_socket(client_socket); break; } */ printf("disconnecting client\n"); close_socket(client_socket); break; } } return 0; }