39 lines
885 B
C
39 lines
885 B
C
#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);
|
|
}
|