Skip to content

ZhangShurong/tinyhttps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

tinyhttps

English | ไธญๆ–‡


English

Overview

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.

Features

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

Architecture

                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚   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
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Project Structure

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

Prerequisites

  • 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.)

Building

# 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/tinyhttps

Running

cd build
./tinyhttps

Expected output:

https running on port 4430
http running on port 8000
http running on port 10086

Ports

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 standard 443) is used to avoid requiring root privileges. For production use on port 443, run with sudo or configure capability.

Testing

# 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

Dependencies

  • pthread โ€” Multi-threading for concurrent HTTP/HTTPS/Proxy servers
  • OpenSSL (libssl, libcrypto) โ€” SSL/TLS encryption for HTTPS

License

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/tinyhttps

่ฟ่กŒๆ–นๅผ

cd 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 ๅŠ ๅฏ†้€šไฟก

่ฎธๅฏ่ฏ

ๆœฌ้กน็›ฎไป…ไพ›ๆ•™่‚ฒๅญฆไน ็›ฎ็š„ไฝฟ็”จใ€‚

About

A simple https server using openssl and tinyhttpd.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors