Added File Reading functionality
This commit is contained in:
parent
800ba738dc
commit
1c51408dd9
|
@ -52,3 +52,6 @@ Module.symvers
|
|||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
# Custom
|
||||
build/
|
||||
obj/
|
||||
|
|
33
Makefile
33
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)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
abcd
|
|
@ -9,13 +9,16 @@ void get_file_path(char *uri, char *file_path) {
|
|||
*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, 30);
|
||||
printf("%s\n", file_path);
|
||||
strncat(file_path, uri, (int)sizeof(uri));
|
||||
|
||||
const char *dot = strrchr(file_path, '.');
|
||||
if(!dot || dot == file_path) {
|
||||
|
|
40
src/server.c
40
src/server.c
|
@ -1,4 +1,5 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue