commit c84df3426b2f67fba1107a659b60b8ec3b26a7ab Author: Reinhard Fürst Date: Wed Feb 25 17:10:36 2026 +0100 Ertser Commit diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..379423c --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,33 @@ +{ + "version": 4, + "configurations": [ + { + "name": "Cygwin", + "compilerPath": "C:/cygwin64/bin/g++.exe", + "intelliSenseMode": "linux-gcc-x64", + "cppStandard": "c++17", + "cStandard": "c17", + "includePath": [ + "${workspaceFolder}", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include/c++", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include/c++/x86_64-pc-cygwin", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include/c++/backward", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include", + "C:/cygwin64/usr/include", + "C:/cygwin64/usr/include/w32api" + ], + "browse": { + "path": [ + "${workspaceFolder}", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include/c++", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include/c++/x86_64-pc-cygwin", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include/c++/backward", + "C:/cygwin64/usr/lib/gcc/x86_64-pc-cygwin/13/include", + "C:/cygwin64/usr/include", + "C:/cygwin64/usr/include/w32api" + ], + "limitSymbolsToIncludedHeaders": true + } + } + ] +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..d6663db --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "ms-vscode.cpptools" + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5fb3c8d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,49 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Starten", + "type": "cppdbg", + "request": "launch", + "program": "/cygdrive/c/Users/rexfu/Projekte/WebServer/webserver.exe", + "args": [], + "stopAtEntry": false, + "cwd": "/cygdrive/c/Users/rexfu/Projekte/WebServer", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", + "pipeTransport": { + "pipeProgram": "C:/cygwin64/bin/bash.exe", + "pipeArgs": [ + "-lc" + ], + "debuggerPath": "/usr/bin/gdb" + }, + "sourceFileMap": { + "/cygdrive/c/Users/rexfu/Projekte/WebServer": "${workspaceFolder}" + }, + "logging": { + "engineLogging": false, + "trace": false, + "traceResponse": false + }, + "preLaunchTask": "C++ Build", + "setupCommands": [ + { + "description": "Automatische Strukturierung und Einrückung für gdb aktivieren", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Disassemblierungsvariante auf Intel festlegen", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..62c37e4 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,26 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "C++ Build", + "type": "shell", + "command": "C:/cygwin64/bin/bash.exe", + "args": [ + "-lc", + "cd \"$(cygpath -u \"$WORKSPACE_FOLDER\")\"; g++ -std=c++17 -g -o webserver.exe webserver.cpp" + ], + "options": { + "cwd": "${workspaceFolder}", + "env": { + "WORKSPACE_FOLDER": "${workspaceFolder}" + } + }, + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": ["$gcc"] + } + ] +} + diff --git a/webserver b/webserver new file mode 100644 index 0000000..0d8b9bd Binary files /dev/null and b/webserver differ diff --git a/webserver.cpp b/webserver.cpp new file mode 100644 index 0000000..4315467 --- /dev/null +++ b/webserver.cpp @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include +#include + +const int PORT = 8089; + +std::string getHtmlPage(const std::string& path) { + if (path == "/" || path == "/index") { + return R"( + + + + + C++ Webserver + + + +
+
C++ HTTP Server
+

🌐 Hallo, Welt!

+

+ Dieser Webserver wurde komplett in reinem C++ geschrieben — + ohne externe Bibliotheken. Er nutzt direkt die + POSIX Socket API des Betriebssystems. +

+ +
+ +)"; + } else if (path == "/info") { + return R"( + + + + Server Info + + + +

🔧 Server-Informationen

+ + + + + + + +
EigenschaftWert
SpracheC++ (ISO C++17)
BibliothekenNur POSIX Standard (sys/socket.h)
Port8080
ProtokollHTTP/1.0
PlattformLinux
+

← Zurück

+ +)"; + } else if (path == "/zeit") { + time_t now = time(nullptr); + std::string zeit = std::string(ctime(&now)); + return "Zeit" + "" + "

🕐 Aktuelle Serverzeit

" + zeit + "

" + "

← Zurück

"; + } + + return R"( +404 + +

404

Seite nicht gefunden.

Startseite

+)"; +} + +std::string buildResponse(const std::string& path) { + std::string body = getHtmlPage(path); + std::string status = (path == "/" || path == "/info" || path == "/zeit") + ? "200 OK" : "404 Not Found"; + + std::ostringstream response; + response << "HTTP/1.0 " << status << "\r\n" + << "Content-Type: text/html; charset=UTF-8\r\n" + << "Content-Length: " << body.size() << "\r\n" + << "Connection: close\r\n" + << "\r\n" + << body; + return response.str(); +} + +int main() { + int server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd < 0) { std::cerr << "Socket-Fehler\n"; return 1; } + + int opt = 1; + setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(PORT); + + if (bind(server_fd, (sockaddr*)&address, sizeof(address)) < 0) { + std::cerr << "Bind-Fehler\n"; return 1; + } + if (listen(server_fd, 10) < 0) { + std::cerr << "Listen-Fehler\n"; return 1; + } + + std::cout << "Server laeuft auf http://localhost:" << PORT << "\n"; + std::cout << "Seiten: / /info /zeit\n"; + std::cout << "Beenden mit Ctrl+C\n\n"; + + while (true) { + int client_fd = accept(server_fd, nullptr, nullptr); + if (client_fd < 0) continue; + + char buffer[4096] = {}; + + read(client_fd, buffer, sizeof(buffer) - 1); + + // HTTP-Anfrage parsen: "GET /path HTTP/1.x" + std::string request(buffer); + std::string path = "/"; + size_t start = request.find(' '); + if (start != std::string::npos) { + size_t end = request.find(' ', start + 1); + if (end != std::string::npos) + path = request.substr(start + 1, end - start - 1); + } + + std::cout << "Anfrage: " << path << "\n"; + + std::string response = buildResponse(path); + send(client_fd, response.c_str(), response.size(), 0); + close(client_fd); + } + + close(server_fd); + return 0; +} diff --git a/webserver.exe b/webserver.exe new file mode 100644 index 0000000..49348ad Binary files /dev/null and b/webserver.exe differ