Skip to content

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

 
 
 

Contributors