Skip to content

Skyl1te/HttpServer-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 C++ HTTP Server (WinSock, Multithreaded)

A production-style HTTP/1.1 server written from scratch in C++ using the WinSock2 API.

This project demonstrates low-level networking, HTTP protocol handling, multithreading, configuration management, and structured logging — without using any external frameworks.


🔥 Features

  • TCP server using WinSock2

  • 🌐 HTTP/1.1 request parsing (GET)

  • 📁 Static file serving (HTML, CSS, JS, images)

  • 🧵 Multithreaded architecture (one thread per client)

  • ⚙️ INI-based configuration system

  • 📝 Thread-safe logging (console + file)

  • 🛡️ Security features

    • Path traversal protection (..)
    • Request size limiting (413)
    • Connection limits (503 when overloaded)
  • ⏱️ Socket timeouts (prevents hanging connections)

  • 🔌 Graceful shutdown (Ctrl+C)

  • 📦 MIME type detection


🧠 Architecture

The server is built with a modular design:

  • Networking layer → WinSock (socket, bind, listen, accept)

  • HTTP layer → request parsing, response building

  • Core system

    • Config loader (INI parser)
    • Logger (thread-safe)
    • File system handler
  • Concurrency model

    • One thread per connection
    • Atomic counters for active clients

📁 Project Structure

HttpServer-cpp/
│
├── main.cpp          # Core server implementation
├── config.ini        # Server configuration
├── static/           # Static files (HTML, CSS, JS)
├── logs/             # Log files (auto-created)
├── CMakeLists.txt
└── README.md

⚙️ Configuration

The server is fully configurable via config.ini.

Example:

[server]
host = 127.0.0.1
port = 8080
www_root = static

max_connections = 20
max_active_clients = 100

buffer_size = 37020
max_request_size = 8192

recv_timeout_ms = 5000
send_timeout_ms = 5000

[logging]
log_to_file = true
log_level = INFO
log_dir = logs

▶️ How to Run

1. Build (CLion / CMake)

Make sure WinSock is linked:

target_link_libraries(HttpServer_cpp ws2_32)

Then build:

cmake --build .

2. Run

./HttpServer_cpp.exe

3. Open in browser

http://127.0.0.1:8080

🌐 Example Request

GET /index.html HTTP/1.1
Host: 127.0.0.1:8080

📤 Example Response

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 123

<html>...</html>

🛡️ Error Handling

Status Description
200 OK
403 Forbidden (path traversal blocked)
404 File not found
413 Request too large
503 Server overloaded

📊 Logging

Logs include:

  • connection events
  • requests
  • errors
  • performance data

Example:

[2026-04-29 14:32:10] [INFO ] Connected: 127.0.0.1
[2026-04-29 14:32:10] [INFO ] Requested: /index.html
[2026-04-29 14:32:10] [INFO ] Serving file (512 bytes)

🔐 Security Features

  • Prevents directory traversal attacks (../)
  • Limits request size
  • Connection throttling
  • Socket timeouts (anti-slowloris)

🚧 Limitations

  • Windows-only (WinSock)
  • No HTTPS (TLS)
  • Thread-per-connection model (not scalable for very high load)

🔮 Future Improvements

  • Thread pool instead of detached threads
  • HTTP Keep-Alive support
  • POST / JSON API handling
  • Async I/O (IOCP)
  • Linux support (POSIX sockets)
  • Reverse proxy mode

💡 What I Learned

  • Low-level networking (TCP, sockets)
  • HTTP protocol internals
  • Multithreading & synchronization
  • File I/O and MIME handling
  • System-level programming in C++
  • Building production-style architecture without frameworks

📌 Author


⭐ Why This Project Matters

This project shows the ability to:

  • build backend systems from scratch
  • understand how the web actually works under the hood
  • work with low-level APIs and system programming

⭐ If you like this project, consider starring it!

About

This project demonstrates low-level networking, HTTP protocol handling, multithreading, configuration management, and structured logging — without using any external frameworks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages