A simple HTTP server for learning purpose. It supports basic GET and POST methods, static file serving, thread pooling, LRU caching, and logging.
-
GET Method
- Basic GET: Get server files.
- Echo Endpoint: Echoes the request details.
- Static File Serving: Serve static files from the server directory.
-
POST Method
- Echo Endpoint: Echoes the request details.
- Upload Endpoint: Uploads content to a default text file.
-
Logging
- Uses spdlog for logging.
-
LRU Caching
- When a file is requested, check the cache first. If it exist, serve the file from cache, if not, load from disk and put it into cache.
- Caches entries will expire if they are more than 1 minute old.
-
Thread Pooling
- Use STL thread for managing threads.
mkdir build
cd build
cmake ..
make -j
./http-server
Default (home.html)
curl http://localhost
- The default URL of GET method is “home.html”
GET home.html
curl http://localhost:8080/home.html
- Get server files (example: home.html)
Echo
curl http://localhost:8080/echo
- The endpoint of echo in GET method
btw, I use Neovim and tmux. You can check out my configuration here.
Running a Concurrency Test
oha http://localhost:8080
- Perform a concurrency test using oha
Echo
curl -X POST http://localhost:8080/echo
- The endpoint of echo in POST method
Upload data to the server.
curl -D - -X POST -H "Content-Type: text/plain" -d "Hello!" http://localhost:8080/upload
- create "uploads/" directory (default).
- write contents "Hello!" to "uploads/uploaded_file.txt"
-
include/cache.h
,src/cache.cpp
- LRU cache implementation.
-
include/file.h
,src/file.cpp
- file-related utilities.
-
include/log.h
,src/log.cpp
- logging by spdlog
-
include/net.h
,src/net.cpp
- Low-level networking code.
-
include/request.h
,src/request.cpp
- HTTP request handling.
-
include/response.h
,src/response.cpp
- HTTP response building.
-
include/server.h
,src/server.cpp
- HTTP server implementation.
-
include/thread_pool.hpp
- thread pool implementation (using .hpp for template code).
- The cat image of status code is from https://http.cat/.
- The feature inspirations are mostly from bloominstituteoftechnology/C-Web-Server.
- The coding challenge from CodeCrafters: Build your own HTTP server guided the development process.