- Support for other request types
- Support for sending/requesting image files
- Implement more request headers (Security headers, etc.)
This project is a simple C++ web server that handles HTTP requests and responses. It supports basic routing and can serve static HTML files.
.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
Defined in include/server/server.h
Public Methods:
Server(sockaddr_in serverAddr)
: Constructor to initialize the server with a given address.~Server()
: Destructor to clean up resources.void Start()
: Starts the server and begins listening for connections.void Stop()
: Stops the server and closes all connections.void HandleSignal(int signal)
: Handles signals for graceful shutdown.void HandleClient(int client_sock)
: Handles client connections.void Get(const std::string& Path, callback_function func)
: Registers a GET route with a callback function.void ProcessRequest(sockaddr_in Source, const std::string& buffer, int client_sock)
: Processes incoming requests.int GetSocketId()
: Returns the server socket ID.void Use(std::string Path, Router::Router& R)
: Registers the routes from the given Router to the Server with the specified base path.
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.
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.
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:
RequestHelper::requestInfo ParseRequest(const std::string& buffer)
: Parses the request buffer into a structured format.RequestHelper::requestInfo ParseRequestType(const std::string& buffer)
: Parses the request type from the buffer.
Defined in include/server/response.h
Public Methods:
Response(int SocketId, int ClientSock)
: Constructor to initialize the response.void SetContentType(Enums::CONTENT_TYPE content_type)
: Sets the content type of the response.void SetStatus(const std::string& status)
: Sets the HTTP status of the response.void SetBody(const std::string& nBody)
: Sets the body of the response.void Send()
: Sends the response to the client.
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.
Defined in include/server/router.h
Public Methods:
Router()
: Constructor for the router.void Get(const std::string& Path, ListenerHelper::callback_function func)
: Registers a GET route with the specified path and callback function.void GetStatic(const std::string& Path, ListenerHelper::callback_function_static func, serve_static::StaticFile file)
: Registers a static file handler for HTTP GET requests.
Defined in include/server/file_parser.h
Public Methods:
static std::string GetHTMLPage(const std::string& path)
: Reads an HTML file from the given path and returns its contents as a string.static std::string GetFileContents(const std::string& path)
: Reads the contents of a file and returns it as a string.static bool IsFileOfType(std::string filename, std::string extension)
: Checks if the given filename has the specified extension.
Defined in include/server/string_utils.h
Public Methods:
static bool Contains(const std::string& str, const std::string& substr)
: Checks if a string contains a substring.
Defined in include/server/constants.h
Members:
char HTTP_GET[3]
: HTTP GET method.char HTTP_POST[4]
: HTTP POST method.
Enums:
typedef char HTTP_STATUS[14]
: HTTP status type.typedef std::string CONTENT_TYPE
: Content type.int HTTP_G
: HTTP GET enum.int HTTP_P
: HTTP POST enum.int HTTP_PU
: HTTP PUT enum.HTTP_STATUS HTTP_OK
: HTTP 200 OK status.CONTENT_TYPE TEXT_PLAIN
: Plain text content type.CONTENT_TYPE TEXT_HTML
: HTML content type.CONTENT_TYPE TEXT_JSON
: JSON content type.CONTENT_TYPE TEXT_CSS
: CSS content type.HTTP_STATUS HTTP_NOTFOUND
: HTTP 404 Not Found status.typedef int HTTP_METHOD
: HTTP method type.
- Define routes using the
Server::Get
method. - Start the server using the
Server::Start
method. - Handle client requests and send responses using the
Request
andResponse
classes.
To build and run the project, follow these steps:
- Open a terminal and navigate to the project directory.
- Run the build script:
./build.sh
- 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.