This commit is contained in:
alexvoste
2026-05-07 01:22:24 +03:00
parent 07e8fdd4b9
commit f3fc474eca
5 changed files with 120 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
#include "server.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
void handle_client(int client_socket, const char *uri) {
char buffer[1024];
char path[256] = "./public";
if (strcmp(uri, "/") == 0) {
strcat(path, "/index.html");
} else {
strcat(path, uri);
}
FILE *f = fopen(path, "rb");
if (f == NULL) {
char *response = "HTTP/1.1 404 Not Found\r\OK\r\nContent-Length: 0\r\n\r\n";
send(client_socket, response, strlen(response), 0);
return;
}
struct stat st;
stat(path, &st);
long file_size = st.st_size;
recv(client_socket, buffer, 1024, 0);
char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello "
"from server 8080 localhost!";
send(client_socket, response, strlen(response), 0);
// Close the socket
close(client_socket);
}