Skip to content

🧠 Lightweight multithreaded C++ HTTP server with support for GET, POST, DELETE, cookies, file uploads/downloads, and CGI scripting.

Notifications You must be signed in to change notification settings

SanielDev/http-server-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

C++ HTTP Server

This project is a complete lightweight HTTP server built from scratch in C++. It supports basic HTTP methods and extends into advanced web features through modular and well-structured code. This server was developed as a comprehensive hands-on learning project and is suitable for showcasing on resumes and GitHub.

πŸ”§ Features Implemented

  • βœ… Non-blocking socket server using select() for I/O multiplexing
  • βœ… Custom HTTP request parsing (supports GET, POST, DELETE)
  • βœ… Static file serving from the /public directory
  • βœ… Cookie-based login and logout functionality with expiration tracking
  • βœ… File upload using multipart/form-data via POST requests
  • βœ… Download most recently uploaded file (/download endpoint)
  • βœ… CGI script execution for dynamic content (GET/POST)
  • βœ… Cookie store with in-memory session expiration
  • βœ… Thread Pool for concurrent client request processing
  • βœ… Auto-cleanup of old uploaded files after a timeout

πŸ“ Project Structure

.
β”œβ”€β”€ include/
β”‚   β”œβ”€β”€ http_request.h
β”‚   β”œβ”€β”€ utils.h
β”‚   β”œβ”€β”€ ThreadPool.h
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.cpp
β”‚   β”œβ”€β”€ http_request.cpp
β”‚   β”œβ”€β”€ utils.cpp
β”‚   β”œβ”€β”€ ThreadPool.tpp
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ upload.html
β”‚   β”œβ”€β”€ uploaded_file.txt/pdf  <-- dynamically created
β”œβ”€β”€ cgi-bin/
β”‚   β”œβ”€β”€ env.py
β”‚   β”œβ”€β”€ post_echo.py
β”œβ”€β”€ Makefile
└── README.md

πŸš€ How to Run

make clean
make
./server

πŸ” Testing All Functionality

1. Static File Serving

curl http://localhost:8080/
curl http://localhost:8080/upload.html

2. Echo POST Request

curl -X POST http://localhost:8080/echo -d "name=John&age=30"

3. File Upload (multipart/form-data)

echo "This is a test file." > test_upload.txt
curl -X POST http://localhost:8080/upload -F "file=@test_upload.txt"

4. Download Most Recently Uploaded File

curl -O http://localhost:8080/download

5. Set Cookie and Retrieve It

curl -i "http://localhost:8080/set-cookie?name=John"
curl -b "username=John" http://localhost:8080/get-cookie

6. Login and Access Protected Profile

curl -c cookies.txt "http://localhost:8080/login?user=Sam"
curl -b cookies.txt http://localhost:8080/profile

7. Logout and Revisit Profile

curl -b cookies.txt -c cookies.txt http://localhost:8080/logout
curl -b cookies.txt http://localhost:8080/profile

8. CGI Execution (GET and POST)

curl "http://localhost:8080/cgi-bin/env.py?foo=bar&user=test"
curl -X POST http://localhost:8080/cgi-bin/post_echo.py -d "x=5&y=10"

9. DELETE File

curl -X DELETE http://localhost:8080/uploaded_file.txt

10. Thread Pool Stress Test

seq 10 | xargs -n1 -P10 curl -s http://localhost:8080/upload.html > /dev/null

βœ… Future Improvements

  • Add persistent file tracking instead of single uploaded_file.txt
  • Add full multipart/form-data parser for filenames and types
  • Implement async I/O using epoll or Boost ASIO (Linux)
  • Add HTTPS support

Β© Developed as a full-stack systems project in C++

About

🧠 Lightweight multithreaded C++ HTTP server with support for GET, POST, DELETE, cookies, file uploads/downloads, and CGI scripting.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published