From 0162d6404d1ce1ec22df13a9889fb6fcc2f5bca6 Mon Sep 17 00:00:00 2001 From: cflip Date: Thu, 15 Sep 2022 10:00:22 -0600 Subject: Start serving files from the filesystem --- Makefile | 2 +- index.html | 10 ++++++++++ src/main.cpp | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 index.html diff --git a/Makefile b/Makefile index f9a6636..d248427 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-pedantic -Wall --std=c++11 +CFLAGS=-pedantic -Wall --std=c++17 SRC=src/main.cpp \ src/ClientConnection.cpp \ diff --git a/index.html b/index.html new file mode 100644 index 0000000..855971c --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + Welcome to cfws! + + +

Welcome to cfws!

+

This page is being served from the filesystem!

+ + diff --git a/src/main.cpp b/src/main.cpp index bb475bd..8078ad7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "ClientConnection.h" #include "HttpRequest.h" @@ -7,8 +9,9 @@ int main(int argc, char** argv) { - ServerConnection server(8080); + namespace fs = std::filesystem; + ServerConnection server(8080); std::cout << "cfws v0.1.0]\n"; while (true) { @@ -16,22 +19,36 @@ int main(int argc, char** argv) ClientConnection client = server.accept_client_connection(); HttpRequest request = client.read_request(); + // Remove leading slash from the path if it exists + std::string relative_request_path = request.uri(); + while (*relative_request_path.begin() == '/') + relative_request_path.erase(0, 1); + + fs::path request_path = fs::current_path() / relative_request_path; + + // Look for an index.html if the requested path is a directory + if (fs::is_directory(request_path)) { + request_path /= "index.html"; + } + HttpResponse response; response.add_header("Server", "cfws"); - if (request.uri() == "/") { - response.set_status_code(HttpStatusCode::OK); - response.add_header("Content-Type", "text/html"); - response.set_content("

Welcome to the web server!

Click here!"); - } else if (request.uri() == "/language") { - if (request.header("Accept-Language").find("zh") == std::string::npos) { - response.set_status_code(HttpStatusCode::Forbidden); - response.set_content("Please set your language to Chinese to access this page."); - } else { - response.set_status_code(HttpStatusCode::OK); - response.set_content("Welcome to the page."); + + if (fs::exists(request_path)) { + std::string file_path = request_path.string(); + std::ifstream input_file(file_path); + if (!input_file.is_open()) { + std::cerr << "Failed to open file " << file_path << std::endl; } + + std::string file_contents((std::istreambuf_iterator(input_file)), std::istreambuf_iterator()); + + response.set_status_code(HttpStatusCode::OK); + response.add_header("Content-Type", "text/plain"); + response.set_content(file_contents); } else { response.set_status_code(HttpStatusCode::NotFound); + response.add_header("Content-Type", "text/plain"); response.set_content("Page not found!"); } -- cgit v1.2.3