tinyhttps is a lightweight, multi-threaded HTTP/HTTPS web server written in C++11. It is built on top of the classic tinyhttpd project with OpenSSL integration for SSL/TLS support, plus an intelligent protocol-detecting proxy that automatically routes clients to HTTP or HTTPS backends.
| Feature | Description |
|---|---|
| Dual Protocol Support | Serves both HTTP (plain) and HTTPS (SSL/TLS encrypted) simultaneously |
| Protocol Auto-Detection Proxy | Listens on a single port and detects TLS vs. HTTP traffic, forwarding to the correct backend |
| Static File Serving | Serves static files from the htdocs/ directory with automatic index.html resolution |
| CGI Script Execution | Supports GET/POST-based CGI scripts via fork()/exec() |
| Graceful Socket Close | Implements buffered socket shutdown to ensure all pending data is transmitted |
| Object-Oriented Design | Abstract socket interface (ZlSocket) with polymorphic HTTP (ZlHttpSocket) and HTTPS (ZlHttpsSocket) implementations |
โโโโโโโโโโโโโโโ
โ main.cpp โ Entry point, thread management
โโโโโโโโฌโโโโโโโ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โผ โผ โผ
โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ
โ HTTP :8000 โ โHTTPS:4430 โ โProxy:10086 โ
โ httpServer โ โhttpsServer โ โproxyServer โ
โโโโโโโฌโโโโโโโ โโโโโโโฌโโโโโโโ โโโโโโโฌโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ
โZlHttpSocketโ โZlHttpsSock.โ โTLS Probe โ โ
โ (plain TCP)โ โ(OpenSSL) โ โHTTP/HTTPS โ
โโโโโโโฌโโโโโโโ โโโโโโโฌโโโโโโโ โโโโโโโโโโโโโโ
โ โ
โโโโโโโโโฌโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโ
โ httpd.cpp โ Request parsing, CGI, file serving
โโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโ
โ htdocs/ โ Static files & CGI scripts
โโโโโโโโโโโโโโโโ
tinyhttps/
โโโ CMakeLists.txt # Build configuration (CMake 3.10+)
โโโ main.cpp # Main entry: server startup, proxy logic, threading
โโโ httpd.h / httpd.cpp # HTTP request handler: parse, route, serve files, execute CGI
โโโ ZlSocket.h # Abstract socket interface (base class)
โโโ ZlHttpSocket.h/cpp # Plain HTTP socket implementation
โโโ ZlHttpsSocket.h/cpp # HTTPS socket implementation (OpenSSL wrapper)
โโโ ssl/
โ โโโ my.cert # Self-signed SSL certificate (for testing)
โ โโโ my.key # Private key
โโโ htdocs/ # Web root directory
โ โโโ index.html # Default homepage
โ โโโ test.html # Test page
โ โโโ check.cgi # Example CGI script (Perl)
โ โโโ color.cgi # Color demo CGI script (Perl)
โโโ README.md # This file
- C++ Compiler: GCC / Clang with C++11 support
- CMake: Version 3.10 or higher
- OpenSSL: Development headers and libraries (libssl, libcrypto)
- OS: Linux (uses POSIX sockets,
fork(), etc.)
# Clone the repository
git clone https://github.com/ZhangShurong/tinyhttps.git
cd tinyhttps
# Configure build
mkdir build && cd build
cmake ..
# Compile
make
# The executable will be at: build/tinyhttpscd build
./tinyhttpsExpected output:
https running on port 4430
http running on port 8000
http running on port 10086
| Port | Protocol | Description |
|---|---|---|
8000 |
HTTP | Plain HTTP server |
4430 |
HTTPS | SSL/TLS encrypted server |
10086 |
Proxy | Auto-detecting TLS/HTTP proxy |
Note: Port
4430(instead of standard443) is used to avoid requiring root privileges. For production use on port 443, run withsudoor configure capability.
# Static file via HTTP
curl http://localhost:8000/test.html
# Homepage via HTTP
curl http://localhost:8000/index.html
# Static file via HTTPS (self-signed cert, use -k)
curl -sk https://localhost:4430/test.html
# 404 error test
curl http://localhost:8000/nonexistent
# Unsupported method (should return 501)
curl -X DELETE http://localhost:8000/test.html- pthread โ Multi-threading for concurrent HTTP/HTTPS/Proxy servers
- OpenSSL (libssl, libcrypto) โ SSL/TLS encryption for HTTPS
This project is provided for educational and learning purposes.
tinyhttps ๆฏไธไธชๅบไบ C++11 ็ผๅ็่ฝป้็บงๅค็บฟ็จ HTTP/HTTPS Web ๆๅกๅจใๅฎๅบไบ็ปๅ ธ็ tinyhttpd ้กน็ฎๆๅปบ๏ผ้ๆไบ OpenSSL ไปฅๆฏๆ SSL/TLS ๅ ๅฏ้ไฟก๏ผๅนถ้ขๅคๅฎ็ฐไบๆบ่ฝๅ่ฎฎๆฃๆตไปฃ็ๅ่ฝ๏ผๅฏ่ชๅจๅฐๅฎขๆท็ซฏ่ฏทๆฑ่ทฏ็ฑ่ณ HTTP ๆ HTTPS ๅ็ซฏใ
| ๅ่ฝ | ๆ่ฟฐ |
|---|---|
| ๅๅ่ฎฎๆฏๆ | ๅๆถๆไพ HTTP๏ผๆๆ๏ผๅ HTTPS๏ผSSL/TLS ๅ ๅฏ๏ผๆๅก |
| ๅ่ฎฎ่ชๅจๆฃๆตไปฃ็ | ๅจๅไธ็ซฏๅฃ็ๅฌ๏ผ่ชๅจ่ฏๅซ TLS ไธ HTTP ๆต้ๅนถ่ฝฌๅ่ณๅฏนๅบๅ็ซฏ |
| ้ๆๆไปถๆๅก | ไป htdocs/ ็ฎๅฝๆไพ้ๆๆไปถๆๅก๏ผๆฏๆ่ชๅจ่งฃๆ index.html |
| CGI ่ๆฌๆง่ก | ้่ฟ fork()/exec() ๆฏๆ GET/POST ๆนๅผ็ CGI ่ๆฌ |
| ไผ้ ๅ ณ้ญ่ฟๆฅ | ๅฎ็ฐ็ผๅฒๅผ socket ๅ ณ้ญ๏ผ็กฎไฟๆๆๅพ ๅ้ๆฐๆฎไผ ่พๅฎๆ |
| ้ขๅๅฏน่ฑก่ฎพ่ฎก | ๆฝ่ฑก socket ๆฅๅฃ๏ผZlSocket๏ผ๏ผๅคๆๅฎ็ฐ HTTP๏ผZlHttpSocket๏ผๅ HTTPS๏ผZlHttpsSocket๏ผ |
โโโโโโโโโโโโโโโ
โ main.cpp โ ๅ
ฅๅฃๆไปถใ็บฟ็จ็ฎก็
โโโโโโโโฌโโโโโโโ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โผ โผ โผ
โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ
โ HTTP :8000 โ โHTTPS:4430 โ โProxy:10086 โ
โ httpServer โ โhttpsServer โ โproxyServer โ
โโโโโโโฌโโโโโโโ โโโโโโโฌโโโโโโโ โโโโโโโฌโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ
โZlHttpSocketโ โZlHttpsSock.โ โTLS ๆขๆต โ โ
โ (็บฏ TCP) โ โ(OpenSSL) โ โHTTP/HTTPS โ
โโโโโโโฌโโโโโโโ โโโโโโโฌโโโโโโโ โโโโโโโโโโโโโโ
โ โ
โโโโโโโโโฌโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโ
โ httpd.cpp โ ่ฏทๆฑ่งฃๆใ่ทฏ็ฑใๆไปถๆๅกใCGI ๆง่ก
โโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโ
โ htdocs/ โ ้ๆๆไปถไธ CGI ่ๆฌ็ฎๅฝ
โโโโโโโโโโโโโโโโ
tinyhttps/
โโโ CMakeLists.txt # ๆๅปบ้
็ฝฎ๏ผCMake 3.10+๏ผ
โโโ main.cpp # ไธป็จๅบ๏ผๆๅกๅฏๅจใไปฃ็้ป่พใ็บฟ็จ็ฎก็
โโโ httpd.h / httpd.cpp # HTTP ่ฏทๆฑๅค็ๅจ๏ผ่งฃๆใ่ทฏ็ฑใๆไปถๆๅกใCGI ๆง่ก
โโโ ZlSocket.h # ๆฝ่ฑก socket ๆฅๅฃ๏ผๅบ็ฑป๏ผ
โโโ ZlHttpSocket.h/cpp # ็บฏ HTTP socket ๅฎ็ฐ
โโโ ZlHttpsSocket.h/cpp # HTTPS socket ๅฎ็ฐ๏ผOpenSSL ๅฐ่ฃ
๏ผ
โโโ ssl/
โ โโโ my.cert # ่ช็ญพๅ SSL ่ฏไนฆ๏ผ็จไบๆต่ฏ๏ผ
โ โโโ my.key # ็ง้ฅๆไปถ
โโโ htdocs/ # Web ๆ น็ฎๅฝ
โ โโโ index.html # ้ป่ฎค้ฆ้กต
โ โโโ test.html # ๆต่ฏ้กต้ข
โ โโโ check.cgi # ็คบไพ CGI ่ๆฌ๏ผPerl๏ผ
โ โโโ color.cgi # ้ข่ฒๆผ็คบ CGI ่ๆฌ๏ผPerl๏ผ
โโโ README.md # ๆฌๆไปถ
- C++ ็ผ่ฏๅจ๏ผGCC ๆ Clang๏ผ้ๆฏๆ C++11 ๆ ๅ
- CMake๏ผ็ๆฌ 3.10 ๅไปฅไธ
- OpenSSL๏ผๅผๅๅคดๆไปถๅๅบ๏ผlibssl, libcrypto๏ผ
- ๆไฝ็ณป็ป๏ผLinux๏ผไฝฟ็จ POSIX socketใ
fork()็ญ็ณป็ป่ฐ็จ๏ผ
# ๅ
้ไปๅบ
git clone https://github.com/ZhangShurong/tinyhttps.git
cd tinyhttps
# ้
็ฝฎๆๅปบ
mkdir build && cd build
cmake ..
# ็ผ่ฏ
make
# ๅฏๆง่กๆไปถไฝไบ๏ผbuild/tinyhttpscd build
./tinyhttps้ขๆ่พๅบ๏ผ
https running on port 4430
http running on port 8000
http running on port 10086
| ็ซฏๅฃ | ๅ่ฎฎ | ่ฏดๆ |
|---|---|---|
8000 |
HTTP | ๆๆ HTTP ๆๅกๅจ |
4430 |
HTTPS | SSL/TLS ๅ ๅฏๆๅกๅจ |
10086 |
ไปฃ็ | ่ชๅจๆฃๆต TLS/HTTP ็ๆบ่ฝไปฃ็ |
ๆณจๆ๏ผ ไฝฟ็จ
4430่้ๆ ๅ443็ซฏๅฃไปฅ้ฟๅ ้่ฆ root ๆ้ใ็ไบง็ฏๅขๅฆ้ไฝฟ็จ 443 ็ซฏๅฃ๏ผ่ฏทไฝฟ็จsudo่ฟ่กๆ้ ็ฝฎ capabilitiesใ
# ้่ฟ HTTP ่ฎฟ้ฎ้ๆๆไปถ
curl http://localhost:8000/test.html
# ้่ฟ HTTP ่ฎฟ้ฎ้ฆ้กต
curl http://localhost:8000/index.html
# ้่ฟ HTTPS ่ฎฟ้ฎ้ๆๆไปถ๏ผ่ช็ญพๅ่ฏไนฆ๏ผๅ -k ่ทณ่ฟ้ช่ฏ๏ผ
curl -sk https://localhost:4430/test.html
# ๆต่ฏ 404 ้่ฏฏ
curl http://localhost:8000/nonexistent
# ๆต่ฏไธๆฏๆ็่ฏทๆฑๆนๆณ๏ผๅบ่ฟๅ 501๏ผ
curl -X DELETE http://localhost:8000/test.html- pthread โ ็จไบ HTTP/HTTPS/Proxy ๆๅกๅจ็ๅค็บฟ็จๅนถๅๅค็
- OpenSSL๏ผlibssl, libcrypto๏ผโ ็จไบ HTTPS ็ SSL/TLS ๅ ๅฏ้ไฟก
ๆฌ้กน็ฎไป ไพๆ่ฒๅญฆไน ็ฎ็ไฝฟ็จใ