This project is a fully functional HTTP server written in C++. The server can handle basic HTTP methods like GET and POST, serve static files, and provide structured responses to clients. The implementation focuses on modular design and efficient handling of multiple client connections.
The primary components of the project are:
- Logger: Handles logging of server activity and errors.
- Parser: Parses incoming HTTP requests.
- Router: Routes requests to appropriate handlers and serves static files.
- Server: The core server that manages client connections and request handling.
- The server parses incoming HTTP requests and extracts the request method, path, HTTP version, headers, and body.
- Cookies are automatically parsed from the
Cookieheader.
- The server routes requests to appropriate handlers based on the HTTP method and URL path.
- It serves static files (e.g., HTML, CSS, JS, images) from a predefined directory (
Website). - MIME types are determined automatically based on file extensions.
- Comprehensive logging helps track server activity, errors, and client connections.
- Logs can be displayed in the console for real-time monitoring.
- The server can handle multiple clients concurrently using the
select()system call. - It maintains a list of active client connections and processes them efficiently.
- The server returns appropriate HTTP error responses, such as 404 Not Found for missing resources.
- Proper error messages are logged for debugging purposes.
The project consists of several key components, each encapsulated in a class:
-
Logger:
Logs messages to the console or a log file.- File:
HttpLogger/HttpLogger.handHttpLogger/HttpLogger.cpp
- File:
-
HttpRequestandHttpResponse:
Represent HTTP request and response objects, respectively.- Files:
HttpR/HttpRequest.handHttpR/HttpResponse.h
- Files:
-
Parser:
Decodes HTTP requests and encodes HTTP responses.- Files:
HttpParser/HttpParser.handHttpParser/HttpParser.cpp
- Files:
-
Router:
Routes incoming requests to appropriate handlers and serves static files.- Files:
HttpRouter/HttpRouter.handHttpRouter/HttpRouter.cpp
- Files:
-
HttpServer:
The core server that manages sockets, listens for connections, and handles client requests.- Files:
HttpServer/HttpServer.handHttpServer/HttpServer.cpp
- Files:
-
Utils:
Utility functions such as string trimming and MIME type detection.- Files:
Utils/Utils.handUtils/Utils.cpp
- Files:
-
main:
The entry point of the application where the server is initialized and run.- File:
main.cpp
- File:
-
Initialization:
- The server is initialized with a logger and a router.
- The default address is set to
127.0.0.1and the port to8080.
-
Running the Server:
- The server starts listening for incoming connections.
- When a client connects, the server accepts the connection and reads the incoming request.
-
Request Handling:
- The request is parsed into an
HttpRequestobject. - The router processes the request and generates an appropriate
HttpResponse.
- The request is parsed into an
-
Serving Responses:
- The response is encoded and sent back to the client.
- The connection is then closed.
-
Logging:
- All significant actions, such as incoming requests, errors, and client disconnections, are logged for monitoring and debugging.
- C++ Compiler (GCC or Clang)
- CMake (for building the project)
- POSIX-compliant system (Linux/macOS)
-
Clone the Repository:
git clone <repository-url> cd <project-directory>
```bash
mkdir build
cd build
make
- Run the Server
./HttpServer
- Access the Server
Open a web browser and navigate to:
http://127.0.0.1:8080
Future Improvements
Support for additional HTTP methods like PUT and DELETE. HTTPS support for secure communication. Thread-based concurrency for improved performance with high loads. Configuration file support for customizable server settings.
Conclusion
This project serves as a solid foundation for building more advanced HTTP servers. It provides an opportunity to explore networking, HTTP protocol handling, and modular software design in C++. The clean architecture allows for easy extension and maintenance, making it a versatile tool for learning and experimentation.