diff --git a/Userspace/apps/test/web_test/Makefile b/Userspace/apps/test/web_test/Makefile new file mode 100644 index 00000000..13dee6ab --- /dev/null +++ b/Userspace/apps/test/web_test/Makefile @@ -0,0 +1,36 @@ +default: + $(error Do not run this Makefile directly!) + +S_SOURCES = $(shell find ./ -type f -name '*.S') +C_SOURCES = $(shell find ./ -type f -name '*.c') +CXX_SOURCES = $(shell find ./ -type f -name '*.cpp') + +OBJ = $(S_SOURCES:.S=.o) $(C_SOURCES:.c=.o) $(CXX_SOURCES:.cpp=.o) + +FILENAME = $(notdir $(shell pwd)) +WARNCFLAG = -Wall -Wextra + +build: $(FILENAME).elf + cp $(FILENAME).elf $(WORKSPACE_DIR)/out/bin/$(FILENAME) + +# Use static linking +LDFLAGS += -static -fno-pic -fno-pie -Wl,-static + +$(FILENAME).elf: $(OBJ) + $(info Linking $@) + $(CC) $(LDFLAGS) $(SYSROOT) $(OBJ) -o $@ + +%.o: %.c $(HEADERS) + $(info Compiling $<) + $(CC) $(CFLAGS) $(WARNCFLAG) -std=c17 -c $< -o $@ + +%.o: %.cpp $(HEADERS) + $(info Compiling $<) + $(CXX) $(CFLAGS) $(WARNCFLAG) -std=c++20 -fexceptions -c $< -o $@ -fno-rtti + +%.o: %.S + $(info Compiling $<) + $(AS) -o $@ $< + +clean: + rm -f $(OBJ) $(FILENAME).elf diff --git a/Userspace/apps/test/web_test/web.c b/Userspace/apps/test/web_test/web.c new file mode 100644 index 00000000..cab7416c --- /dev/null +++ b/Userspace/apps/test/web_test/web.c @@ -0,0 +1,86 @@ +/* + This file is part of Fennix Userspace. + + Fennix Userspace is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Userspace is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Userspace. If not, see . +*/ + +#include +#include +#include +#include +#include + +#define PORT 8080 +#define BUFFER_SIZE 1024 + +void HandleClient(int clientSocket) +{ + char buffer[BUFFER_SIZE]; + read(clientSocket, buffer, BUFFER_SIZE - 1); + + const char *response = + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 12\r\n" + "Connection: close\r\n" + "\r\n" + "Hello World!"; + + write(clientSocket, response, strlen(response)); + close(clientSocket); +} + +int main() +{ + int serverSocket = socket(AF_INET, SOCK_STREAM, 0); + if (serverSocket == -1) + { + perror("Socket creation failed"); + return 1; + } + + struct sockaddr_in serverAddr; + serverAddr.sin_family = AF_INET; + serverAddr.sin_addr.s_addr = INADDR_ANY; + serverAddr.sin_port = htons(PORT); + + if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) + { + perror("Bind failed"); + return 1; + } + + if (listen(serverSocket, 5) < 0) + { + perror("Listen failed"); + return 1; + } + + printf("Server running on port %d\n", PORT); + while (1) + { + struct sockaddr_in clientAddr; + socklen_t addrLen = sizeof(clientAddr); + int clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &addrLen); + if (clientSocket < 0) + { + perror("Accept failed"); + continue; + } + HandleClient(clientSocket); + } + + close(serverSocket); + return 0; +}