mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-28 15:34:31 +00:00
userspace/test: add web server test program
Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
parent
3e656854bc
commit
ade1c77361
36
Userspace/apps/test/web_test/Makefile
Normal file
36
Userspace/apps/test/web_test/Makefile
Normal file
@ -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
|
86
Userspace/apps/test/web_test/web.c
Normal file
86
Userspace/apps/test/web_test/web.c
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user