backend: fixies
This commit is contained in:
parent
28a91d9f6b
commit
d9ee82597e
@ -1,9 +1,11 @@
|
||||
#include "tcp.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
@ -36,10 +38,12 @@ TcpServer* tcp_server_create(const char* ip, uint16_t port)
|
||||
server_address.sin_port = htons(port);
|
||||
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
|
||||
printf("error: tcp: could not bind socket\n");
|
||||
close(server_socket);
|
||||
return NULL;
|
||||
}
|
||||
if (listen(server_socket, SOMAXCONN) < 0) {
|
||||
if (listen(server_socket, SOMAXCONN - 1) < 0) {
|
||||
printf("error: tcp: could not listen on server\n");
|
||||
close(server_socket);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -59,12 +63,13 @@ void tcp_server_destroy(TcpServer* server)
|
||||
|
||||
TcpConnection* tcp_server_accept(TcpServer* server)
|
||||
{
|
||||
struct sockaddr_in client_address;
|
||||
socklen_t client_size;
|
||||
struct sockaddr_in client_address = { 0 };
|
||||
socklen_t client_size = 0;
|
||||
int client_socket
|
||||
= accept(server->server_socket, (struct sockaddr*)&client_address, &client_size);
|
||||
if (client_socket < 0) {
|
||||
printf("error: tcp: could not accept connection\n");
|
||||
printf("errno: %d %s\n", errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const uint16_t port = 8000;
|
||||
|
||||
TcpServer* server = NULL;
|
||||
|
||||
void interrupt_handler(int a)
|
||||
@ -20,38 +18,19 @@ void interrupt_handler(int a)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
void handle_client_connection(TcpConnection* connection)
|
||||
{
|
||||
tcp_global_initialize_sockets();
|
||||
|
||||
signal(SIGINT, &interrupt_handler);
|
||||
|
||||
printf("starting server...\n");
|
||||
server = tcp_server_create("127.0.0.1", port);
|
||||
if (server == NULL)
|
||||
return 1;
|
||||
printf("listening on port %d\n", port);
|
||||
|
||||
while (true) {
|
||||
printf("waiting for client...\n");
|
||||
TcpConnection* client_connection = tcp_server_accept(server);
|
||||
if (client_connection == NULL) {
|
||||
printf("error: could not accept client\n");
|
||||
continue;
|
||||
}
|
||||
printf("client connected\n");
|
||||
while (true) {
|
||||
uint8_t buffer[8192] = { 0 };
|
||||
ssize_t recieved = tcp_recieve(client_connection, buffer, 8192);
|
||||
ssize_t recieved = tcp_recieve(connection, buffer, 8192);
|
||||
if (recieved < 0) {
|
||||
printf("error: could not recieve\n");
|
||||
break;
|
||||
return;
|
||||
} else if (recieved == 0) {
|
||||
printf("client disconnected\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
HttpRequestHeader header
|
||||
= parse_http_request_header((char*)buffer, strlen((char*)buffer));
|
||||
HttpRequestHeader header = parse_http_request_header((char*)buffer, strlen((char*)buffer));
|
||||
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) {
|
||||
@ -64,28 +43,28 @@ int main(void)
|
||||
"\r\n"
|
||||
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Bad "
|
||||
"request</title></head><body><h1>Fuck you!</h1></body></html>\r\n";
|
||||
ssize_t written = tcp_send(client_connection, send_buffer, sizeof(send_buffer));
|
||||
ssize_t written = tcp_send(connection, send_buffer, sizeof(send_buffer));
|
||||
if (written < 0) {
|
||||
printf("error: could not write\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
} 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;
|
||||
return;
|
||||
}
|
||||
uint8_t send_buffer[] = "HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/html\r\n"
|
||||
"\r\n";
|
||||
ssize_t written = tcp_send(client_connection, send_buffer, sizeof(send_buffer));
|
||||
ssize_t written = tcp_send(connection, send_buffer, sizeof(send_buffer));
|
||||
if (written < 0) {
|
||||
printf("error: could not write\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
char char_read;
|
||||
while ((char_read = (char)fgetc(file)) != EOF) {
|
||||
tcp_send(client_connection, (uint8_t*)&char_read, sizeof(char));
|
||||
tcp_send(connection, (uint8_t*)&char_read, sizeof(char));
|
||||
}
|
||||
} else {
|
||||
char rootpath[] = "../frontend";
|
||||
@ -117,39 +96,37 @@ int main(void)
|
||||
file_flag[1] = 'b';
|
||||
} else {
|
||||
printf("error: unknown file type\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
|
||||
FILE* file = fopen(filepath, file_flag);
|
||||
if (file == NULL) {
|
||||
printf("error: could not open file\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
|
||||
char send_buffer_1[] = "HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: ";
|
||||
char send_buffer_2[] = "\r\n"
|
||||
"\r\n";
|
||||
ssize_t written = tcp_send(
|
||||
client_connection, (uint8_t*)send_buffer_1, strlen(send_buffer_1));
|
||||
ssize_t written = tcp_send(connection, (uint8_t*)send_buffer_1, strlen(send_buffer_1));
|
||||
if (written < 0) {
|
||||
printf("error: could not write\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
written = tcp_send(client_connection, (uint8_t*)mime_type, strlen(mime_type));
|
||||
written = tcp_send(connection, (uint8_t*)mime_type, strlen(mime_type));
|
||||
if (written < 0) {
|
||||
printf("error: could not write\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
written = tcp_send(
|
||||
client_connection, (uint8_t*)send_buffer_2, strlen(send_buffer_2));
|
||||
written = tcp_send(connection, (uint8_t*)send_buffer_2, strlen(send_buffer_2));
|
||||
if (written < 0) {
|
||||
printf("error: could not write\n");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
int char_read;
|
||||
while ((char_read = fgetc(file)) != EOF) {
|
||||
tcp_send(client_connection, (uint8_t*)&char_read, sizeof(char));
|
||||
tcp_send(connection, (uint8_t*)&char_read, sizeof(char));
|
||||
}
|
||||
|
||||
free(filepath);
|
||||
@ -157,10 +134,33 @@ int main(void)
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
break;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
tcp_global_initialize_sockets();
|
||||
|
||||
signal(SIGINT, &interrupt_handler);
|
||||
|
||||
const uint16_t port = 8000;
|
||||
|
||||
printf("starting server...\n");
|
||||
server = tcp_server_create("127.0.0.1", port);
|
||||
if (server == NULL)
|
||||
return 1;
|
||||
printf("listening on port %d\n", port);
|
||||
|
||||
while (true) {
|
||||
printf("waiting for client...\n");
|
||||
TcpConnection* connection = tcp_server_accept(server);
|
||||
if (connection == NULL) {
|
||||
printf("error: could not accept client\n");
|
||||
continue;
|
||||
}
|
||||
printf("client connected\n");
|
||||
handle_client_connection(connection);
|
||||
printf("disconnecting client\n");
|
||||
tcp_connection_destroy(client_connection);
|
||||
tcp_connection_destroy(connection);
|
||||
}
|
||||
tcp_server_destroy(server);
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user