From 1c51408dd925e33f353b35a1eb1c88ef881c4c4f Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Thu, 6 Jun 2024 20:13:46 +0700 Subject: [PATCH] Added File Reading functionality --- .gitignore | 3 +++ Makefile | 33 ++++++++++++++++++--------------- htdocs/index.html | 1 + src/file.c | 13 ++++++++----- src/server.c | 44 +++++++++++++++++++++++++++----------------- 5 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 htdocs/index.html diff --git a/.gitignore b/.gitignore index cd531cf..3335eb0 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,6 @@ Module.symvers Mkfile.old dkms.conf +# Custom +build/ +obj/ diff --git a/Makefile b/Makefile index b1c0f2a..5f0a63c 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,24 @@ -CC = /usr/bin/clang -CFLAGS = -g -Wall +CC = clang +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 -.OBJPATH: ${.CURDIR}/obj +all: $(BIN) -all: server +$(BIN): $(OBJs) + $(CC) $(CFLAGS) $(CPPFLAGS) $(OBJs) -o $@ $(LDLIBS) -server: server.o file.o - $(CC) $(CFLAGS) -o server server.o file.o - -server.o: server.c server.h - $(CC) $(CFLAGS) -c server.c - -file.o: file.c file.h - $(CC) $(CFLAGS) -c +$(OBJs): $(SRCs) + $(MKDIR) $(dir $@) + $(CC) $(CFLAGS) $(CPPFLAGS) -c $(subst $(OBJ), $(SRC), $(@:.o=.c)) -o $@ clean: - $(RM) $(TARGET) + $(RM) -R $(BIN) + $(RM) -R $(OBJ) diff --git a/htdocs/index.html b/htdocs/index.html new file mode 100644 index 0000000..acbe86c --- /dev/null +++ b/htdocs/index.html @@ -0,0 +1 @@ +abcd diff --git a/src/file.c b/src/file.c index fd1d1cf..f7fd66a 100644 --- a/src/file.c +++ b/src/file.c @@ -8,15 +8,18 @@ void get_file_path(char *uri, char *file_path) { if(question) { *question = '\0'; } - + + char index_file[] = "index.html"; 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, (int)sizeof(uri)); - + strncat(file_path, uri, 30); + printf("%s\n", file_path); + const char *dot = strrchr(file_path, '.'); if(!dot || dot == file_path) { strncat(file_path, ".html", 6); diff --git a/src/server.c b/src/server.c index 5cb2e0f..5e1972c 100644 --- a/src/server.c +++ b/src/server.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -28,10 +29,10 @@ int send_response(int fd, const char *header, char *content_type, void *body, in "Content-Type: %s\n" "\n", header, SERVER_VERSION, date_time, content_length, content_type); - + memcpy(response + header_length, body, content_length); int write_res = write(fd, response, header_length + content_length); - + if (write_res < 0) { perror("webserver (write)"); return write_res; @@ -45,6 +46,8 @@ int main() { // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); + int option = 1; + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); if (sockfd == -1) { perror("webserver (socket)"); 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); 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 { char file_path[4096]; get_file_path(uri, file_path); + printf("Getting file\n"); FILE *file = fopen(file_path, "r"); 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);