Added File Reading functionality

This commit is contained in:
Win 2024-06-06 20:13:46 +07:00
parent 800ba738dc
commit 1c51408dd9
5 changed files with 57 additions and 37 deletions

3
.gitignore vendored
View File

@ -52,3 +52,6 @@ Module.symvers
Mkfile.old Mkfile.old
dkms.conf dkms.conf
# Custom
build/
obj/

View File

@ -1,21 +1,24 @@
CC = /usr/bin/clang CC = clang
CFLAGS = -g -Wall CPPFLAGS = -Iinclude -Isrc
CFLAGS = -Wall -pthread
LDLIBS = -lm -lpthread
TARGET = server SRC = src
OBJ = obj
BIN = winnie-http
MKDIR = mkdir -p
SRCs := $(shell find $(SRC) -name "*.c")
OBJs := $(subst $(SRC), $(OBJ), $(SRCs:.c=.o))
.PATH: ${.CURDIR}/src all: $(BIN)
.OBJPATH: ${.CURDIR}/obj
all: server $(BIN): $(OBJs)
$(CC) $(CFLAGS) $(CPPFLAGS) $(OBJs) -o $@ $(LDLIBS)
server: server.o file.o $(OBJs): $(SRCs)
$(CC) $(CFLAGS) -o server server.o file.o $(MKDIR) $(dir $@)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $(subst $(OBJ), $(SRC), $(@:.o=.c)) -o $@
server.o: server.c server.h
$(CC) $(CFLAGS) -c server.c
file.o: file.c file.h
$(CC) $(CFLAGS) -c
clean: clean:
$(RM) $(TARGET) $(RM) -R $(BIN)
$(RM) -R $(OBJ)

1
htdocs/index.html Normal file
View File

@ -0,0 +1 @@
abcd

View File

@ -9,13 +9,16 @@ void get_file_path(char *uri, char *file_path) {
*question = '\0'; *question = '\0';
} }
char index_file[] = "index.html";
if(uri[strlen(uri) - 1] == '/') { if(uri[strlen(uri) - 1] == '/') {
strncat(uri, "index.html", 11); strncat(uri, index_file, 20);
} }
printf("%s\n", uri);
strncpy(file_path, file_dir, (int)sizeof(file_dir)); strncpy(file_path, file_dir, 20);
printf("%s\n", file_path);
strncat(file_path, uri, 30);
printf("%s\n", file_path); printf("%s\n", file_path);
strncat(file_path, uri, (int)sizeof(uri));
const char *dot = strrchr(file_path, '.'); const char *dot = strrchr(file_path, '.');
if(!dot || dot == file_path) { if(!dot || dot == file_path) {

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -45,6 +46,8 @@ int main() {
// create a socket // create a socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0); int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int option = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
if (sockfd == -1) { if (sockfd == -1) {
perror("webserver (socket)"); perror("webserver (socket)");
return 1; return 1;
@ -106,28 +109,35 @@ int main() {
printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), method, version, uri); printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), method, version, uri);
if (strncmp("GET", method, 7) != 0) { if (strncmp("GET", method, 7) != 0) {
send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0); send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/plain", NULL, 0);
} else { } else {
char file_path[4096]; char file_path[4096];
get_file_path(uri, file_path); get_file_path(uri, file_path);
printf("Getting file\n");
FILE *file = fopen(file_path, "r"); FILE *file = fopen(file_path, "r");
if(!file) { if(!file) {
send_response(client_sock_fd, NOT_FOUND_HEAD, "text/html", NULL, 0); printf("File does not exist\n");
char not_found[] = "404 page not found";
send_response(client_sock_fd, NOT_FOUND_HEAD, "text/plain", not_found, (int)sizeof(not_found));
} else {
fseek(file, 0, SEEK_END);
unsigned long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
printf("Size of File: %ld\n", file_size);
char mime_type[15];
get_mime_type(uri, mime_type);
char *file_buffer = (char *) malloc(file_size);
fread(file_buffer, file_size, 1, file);
send_response(client_sock_fd, OK_HEAD, mime_type, file_buffer, file_size);
free(file_buffer);
fclose(file);
} }
fseek(file, 0, SEEK_END);
unsigned long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
printf("Size of File: %ld\n", file_size);
char mime_type[15];
get_mime_type(uri, mime_type);
send_response(client_sock_fd, OK_HEAD, mime_type, file, file_size);
fclose(file);
} }
close(client_sock_fd); close(client_sock_fd);