Skip to content

Latest commit

 

History

History
297 lines (230 loc) · 11 KB

File metadata and controls

297 lines (230 loc) · 11 KB

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 加密通信

许可证

本项目仅供教育学习目的使用。