2023-02-09 22:02:54 +00:00
|
|
|
#include "http.h"
|
2023-02-10 16:39:04 +00:00
|
|
|
#include "tcp.h"
|
2023-02-10 10:11:06 +00:00
|
|
|
#include <signal.h>
|
2023-02-09 09:25:15 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2023-02-09 12:35:05 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-02-09 12:37:00 +00:00
|
|
|
|
2023-02-10 16:39:04 +00:00
|
|
|
const uint16_t port = 8000;
|
2023-02-10 10:11:06 +00:00
|
|
|
|
2023-02-10 16:39:04 +00:00
|
|
|
TcpServer* server = NULL;
|
2023-02-10 10:11:06 +00:00
|
|
|
|
2023-02-10 16:39:04 +00:00
|
|
|
void interrupt_handler(int a)
|
2023-02-10 10:11:06 +00:00
|
|
|
{
|
|
|
|
(void)a;
|
2023-02-10 16:39:04 +00:00
|
|
|
printf("\nShutting down gracefully...\n");
|
|
|
|
if (server != NULL)
|
|
|
|
tcp_server_destroy(server);
|
2023-02-10 10:11:06 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2023-02-09 09:25:15 +00:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2023-02-10 16:39:04 +00:00
|
|
|
tcp_global_initialize_sockets();
|
2023-02-09 12:37:00 +00:00
|
|
|
|
2023-02-10 16:39:04 +00:00
|
|
|
signal(SIGINT, &interrupt_handler);
|
2023-02-10 10:11:06 +00:00
|
|
|
|
2023-02-09 09:25:15 +00:00
|
|
|
printf("starting server...\n");
|
2023-02-10 16:39:04 +00:00
|
|
|
server = tcp_server_create("127.0.0.1", port);
|
|
|
|
if (server == NULL)
|
2023-02-09 09:25:15 +00:00
|
|
|
return 1;
|
|
|
|
printf("listening on port %d\n", port);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
printf("waiting for client...\n");
|
2023-02-10 16:39:04 +00:00
|
|
|
TcpConnection* client_connection = tcp_server_accept(server);
|
|
|
|
if (client_connection == NULL) {
|
|
|
|
printf("error: could not accept client\n");
|
2023-02-09 09:25:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
printf("client connected\n");
|
|
|
|
while (true) {
|
|
|
|
uint8_t buffer[8192] = { 0 };
|
2023-02-10 16:39:04 +00:00
|
|
|
ssize_t recieved = tcp_recieve(client_connection, buffer, 8192);
|
|
|
|
if (recieved < 0) {
|
2023-02-09 09:25:15 +00:00
|
|
|
printf("error: could not recieve\n");
|
|
|
|
break;
|
2023-02-10 16:39:04 +00:00
|
|
|
} else if (recieved == 0) {
|
2023-02-09 09:25:15 +00:00
|
|
|
printf("client disconnected\n");
|
|
|
|
break;
|
|
|
|
}
|
2023-02-09 22:02:54 +00:00
|
|
|
HttpRequestHeader header
|
|
|
|
= parse_http_request_header((char*)buffer, strlen((char*)buffer));
|
2023-02-10 10:11:06 +00:00
|
|
|
char* path = calloc(header.path_length + 1, sizeof(char));
|
|
|
|
strncpy(path, (char*)&buffer[header.path_index], header.path_length);
|
|
|
|
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"
|
|
|
|
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Bad "
|
|
|
|
"request</title></head><body><h1>Fuck you!</h1></body></html>\r\n";
|
2023-02-10 16:39:04 +00:00
|
|
|
ssize_t written = tcp_send(client_connection, send_buffer, sizeof(send_buffer));
|
2023-02-10 10:11:06 +00:00
|
|
|
if (written < 0) {
|
|
|
|
printf("error: could not write\n");
|
|
|
|
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");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint8_t send_buffer[] = "HTTP/1.1 200 OK\r\n"
|
|
|
|
"Content-Type: text/html\r\n"
|
|
|
|
"\r\n";
|
2023-02-10 16:39:04 +00:00
|
|
|
ssize_t written = tcp_send(client_connection, send_buffer, sizeof(send_buffer));
|
2023-02-10 10:11:06 +00:00
|
|
|
if (written < 0) {
|
|
|
|
printf("error: could not write\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char char_read;
|
|
|
|
while ((char_read = (char)fgetc(file)) != EOF) {
|
2023-02-10 16:39:04 +00:00
|
|
|
tcp_send(client_connection, (uint8_t*)&char_read, sizeof(char));
|
2023-02-10 10:11:06 +00:00
|
|
|
}
|
|
|
|
} 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);
|
2023-02-10 16:39:04 +00:00
|
|
|
|
2023-02-10 10:11:06 +00:00
|
|
|
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");
|
2023-02-10 16:39:04 +00:00
|
|
|
} else if (dot != NULL && strncmp(dot, ".map", 4) == 0) {
|
|
|
|
snprintf(mime_type, 20, "application/json");
|
2023-02-10 10:11:06 +00:00
|
|
|
} 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");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* file = fopen(filepath, file_flag);
|
|
|
|
if (file == NULL) {
|
|
|
|
printf("error: could not open file\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char send_buffer_1[] = "HTTP/1.1 200 OK\r\n"
|
|
|
|
"Content-Type: ";
|
|
|
|
char send_buffer_2[] = "\r\n"
|
|
|
|
"\r\n";
|
2023-02-10 16:39:04 +00:00
|
|
|
ssize_t written = tcp_send(
|
|
|
|
client_connection, (uint8_t*)send_buffer_1, strlen(send_buffer_1));
|
2023-02-10 10:11:06 +00:00
|
|
|
if (written < 0) {
|
|
|
|
printf("error: could not write\n");
|
|
|
|
break;
|
|
|
|
}
|
2023-02-10 16:39:04 +00:00
|
|
|
written = tcp_send(client_connection, (uint8_t*)mime_type, strlen(mime_type));
|
2023-02-10 10:11:06 +00:00
|
|
|
if (written < 0) {
|
|
|
|
printf("error: could not write\n");
|
|
|
|
break;
|
|
|
|
}
|
2023-02-10 16:39:04 +00:00
|
|
|
written = tcp_send(
|
|
|
|
client_connection, (uint8_t*)send_buffer_2, strlen(send_buffer_2));
|
2023-02-10 10:11:06 +00:00
|
|
|
if (written < 0) {
|
|
|
|
printf("error: could not write\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int char_read;
|
|
|
|
while ((char_read = fgetc(file)) != EOF) {
|
2023-02-10 16:39:04 +00:00
|
|
|
tcp_send(client_connection, (uint8_t*)&char_read, sizeof(char));
|
2023-02-10 10:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(filepath);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(path);
|
2023-02-09 22:02:54 +00:00
|
|
|
break;
|
2023-02-09 09:25:15 +00:00
|
|
|
}
|
2023-02-10 16:39:04 +00:00
|
|
|
printf("disconnecting client\n");
|
|
|
|
tcp_connection_destroy(client_connection);
|
2023-02-09 09:25:15 +00:00
|
|
|
}
|
2023-02-10 16:39:04 +00:00
|
|
|
tcp_server_destroy(server);
|
2023-02-09 09:25:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|