Skip to content

sandeepraj25/cpp-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

C++ Build License

πŸš€ CppHttpd β€” Advanced HTTP/1.1 Server in C++

A high-performance, multithreaded HTTP/1.1 server built from scratch in modern C++. Designed with real-world backend concepts like routing, middleware, thread pooling, static file serving, and graceful shutdown.


πŸ“Έ Homepage

Homepage


πŸ“Έ API Testing

curl http://localhost:8080/health
curl http://localhost:8080/users
curl -X POST http://localhost:8080/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice","email":"alice@example.com"}'
curl http://localhost:8080/users/1
curl -X DELETE http://localhost:8080/users/1
curl "http://localhost:8080/echo?msg=hello"

API Testing


πŸ“Έ Project Structure

Structure


✨ Features

⚑ Core Features

  • HTTP/1.1 server implementation using low-level socket programming
  • Multithreaded architecture using a fixed-size thread pool
  • Keep-Alive connection support for performance optimization
  • Graceful shutdown handling (SIGINT / SIGTERM)

🧠 Backend Architecture

  • Custom Router with dynamic route support (/users/:id)
  • Middleware pipeline (inspired by Express.js / Gin)
  • Modular and scalable project structure

πŸ”Œ Middleware Support

  • Request logging (thread-safe logger)

  • CORS handling

  • Security headers:

    • X-Frame-Options
    • X-Content-Type-Options
    • X-XSS-Protection
  • Rate limiting (per-IP sliding window)

πŸ“‚ Static File Server

  • Serves files from /static directory
  • Automatic MIME type detection
  • In-memory caching (reduces disk I/O)
  • Directory fallback (index.html)

πŸ”„ REST API Support

  • GET /health β†’ Health check
  • GET /users β†’ Get all users
  • POST /users β†’ Create user
  • GET /users/:id β†’ Get user by ID
  • DELETE /users/:id β†’ Delete user
  • GET /echo?msg=hello β†’ Query param echo

πŸ› οΈ System-Level Concepts Used

  • Socket programming (bind, listen, accept)
  • Multithreading (std::thread, mutex, condition_variable)
  • Atomic operations
  • File system operations (std::filesystem)
  • Signal handling (SIGINT, SIGTERM, SIGPIPE)
  • Memory-safe design (RAII principles)

πŸ“ Project Structure

cpp-http-server/
β”œβ”€β”€ src/
β”‚   └── main.cpp
β”œβ”€β”€ include/
β”‚   β”œβ”€β”€ config.h
β”‚   β”œβ”€β”€ logger.h
β”‚   β”œβ”€β”€ thread_pool.h
β”‚   β”œβ”€β”€ http_types.h
β”‚   β”œβ”€β”€ http_parser.h
β”‚   β”œβ”€β”€ router.h
β”‚   β”œβ”€β”€ middleware.h
β”‚   β”œβ”€β”€ connection_handler.h
β”‚   β”œβ”€β”€ static_server.h
β”‚   └── http_server.h
β”œβ”€β”€ static/
β”‚   └── index.html
β”œβ”€β”€ screenshots/
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ .gitignore

βš™οΈ Build & Run

πŸ”§ Build

make

▢️ Run Server

./server

Server runs on:

http://localhost:8080

πŸ§ͺ API Testing

Health Check

curl http://localhost:8080/health

Create User

curl -X POST http://localhost:8080/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice","email":"alice@example.com"}'

Get Users

curl http://localhost:8080/users

Get User by ID

curl http://localhost:8080/users/1

Delete User

curl -X DELETE http://localhost:8080/users/1

Static Files

curl http://localhost:8080/static

🧠 How It Works

  1. Client sends HTTP request
  2. Server accepts connection and assigns it to thread pool
  3. Request is parsed into structured format
  4. Middleware pipeline processes request
  5. Router matches endpoint
  6. Handler or static server generates response
  7. Response is sent back with keep-alive support

πŸ“Š Why This Project?

Unlike basic projects, this server implements real backend internals:

  • No frameworks used
  • Built completely from scratch
  • Demonstrates understanding of system-level programming
  • Covers real-world backend concepts used in production systems

πŸš€ Future Improvements

  • HTTPS (SSL/TLS support)
  • JSON parser integration
  • Database support (MongoDB / SQLite)
  • File upload handling
  • Load balancing
  • Performance benchmarking

πŸ‘¨β€πŸ’» Author

Sandeep Raj Aspiring Software Engineer | MERN + C++ Backend Developer


⭐ Show Your Support

If you like this project, consider giving it a ⭐ on GitHub! ject, consider giving it a ⭐ on GitHub!

About

Multithreaded HTTP/1.1 server in C++ with routing, middleware, static file serving, and REST APIs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors