Skip to content

ThatDutchBoio/cpp-webserver-test

Repository files navigation

Project Documentation

TBD

  • Support for other request types
  • Support for sending/requesting image files
  • Implement more request headers (Security headers, etc.)

Table of Contents

Introduction

This project is a simple C++ web server that handles HTTP requests and responses. It supports basic routing and can serve static HTML files.

Project Structure

.gitignore
.vscode/
    settings.json
build/
    server
build.sh
buildTrace.sh
docs/
    docs.md
include/
    server/
        constants.h
        file_parser.h
        ipv4.h
        listener.h
        request.h
        response.h
        router.h
        server.h
        static.h
        string_utils.h
main.cpp
preprocessed_output.cpp
public/
    index.html
    script.js
    style.css
test/
    index.test
README.md
src/
    api/
        testing.cpp
    server/
        file_parser.cpp
        ipv4.cpp
        listener.cpp
        request.cpp
        response.cpp
        router.cpp
        server_lib.cpp
        server.cpp

Classes and Functions

Server

Defined in include/server/server.h

Public Methods:

Private Members:

  • int server_sock: Server socket ID.
  • bool running: Server running state.
  • sockaddr_in serverAddr: Server address.
  • std::vector<ListenerHelper::Listener> listeners: List of registered listeners.

Listener

Defined in include/server/listener.h

Members:

  • bool is_static: Indicates if the listener is for static content.
  • ListenerHelper::callback_function Callback: Function pointer for the callback.
  • ListenerHelper::callback_function_static CallbackStatic: Function pointer for static content callback.
  • std::string Path: Path for the listener.
  • Enums::HTTP_METHOD Method: HTTP method for the listener.
  • serve_static::StaticFile file: Static file associated with the listener.

Request

Defined in include/server/request.h

Members:

  • int RequestMethod: HTTP request method.
  • std::string Path: Request path.
  • sockaddr_in Source_Address: Source address of the request.
  • std::string body: Request body.

Namespaces:

  • RequestHelper: Contains helper functions for parsing requests.

Functions:

Response

Defined in include/server/response.h

Public Methods:

Private Members:

  • std::string GetContentLength(): Returns the content length as a string.
  • std::string GetContentType(): Returns the content type as a string.
  • std::string HTTP_Version: HTTP version.
  • int Content_Length: Content length.
  • std::string HTTP_Status: HTTP status.
  • int SocketId: Server socket ID.
  • std::string body: Response body.
  • std::string Content_type: Content type.
  • int ClientSock: Client socket ID.

Router

Defined in include/server/router.h

Public Methods:

FileParser

Defined in include/server/file_parser.h

Public Methods:

StringUtils

Defined in include/server/string_utils.h

Public Methods:

Constants

Defined in include/server/constants.h

Members:

  • char HTTP_GET[3]: HTTP GET method.
  • char HTTP_POST[4]: HTTP POST method.

Enums:

Usage

  1. Define routes using the Server::Get method.
  2. Start the server using the Server::Start method.
  3. Handle client requests and send responses using the Request and Response classes.

Building and Running

To build and run the project, follow these steps:

  1. Open a terminal and navigate to the project directory.
  2. Run the build script:
    ./build.sh
    
  3. Run the compiled executable:
    ./build/server
    

The server will start and listen for incoming connections on the specified port.


This documentation provides an overview of the project's structure, classes, and functions. For more detailed information, refer to the source code files.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages