基于 Tinyhttpd 的教学项目,新增了 HTTP/2 协议支持。用 C 语言实现,适合学习 HTTP/2 协议原理和网络编程。
A learning-oriented HTTP server based on Tinyhttpd, extended with HTTP/2 protocol support. Written in C, great for understanding HTTP/2 internals and network programming.
- HTTP/2 binary framing (二进制分帧)
- HPACK header compression with Huffman encoding (HPACK 头部压缩)
- Stream multiplexing (流多路复用)
- Flow control (流量控制)
- HTTP/2 direct connection and h2c upgrade (直连和 h2c 升级)
- Backward compatible with HTTP/1.0 (向后兼容 HTTP/1.0)
- Static file serving and CGI support (静态文件和 CGI)
make httpd./httpd 4000The server listens on the specified port (e.g. 4000).
# Run all tests (unit + property-based)
make test
# Run only property-based tests
make test-pbt
# Manual HTTP/2 test with curl
curl --http2-prior-knowledge http://localhost:4000/index.html
# HTTP/1.0 backward compatibility
curl --http1.0 http://localhost:4000/index.html
# Multiplexed requests
curl --http2-prior-knowledge http://localhost:4000/index.html http://localhost:4000/README| File | Description |
|---|---|
httpd.c |
Main server, protocol detection, HTTP/1.0 handler |
http2_connection.c/h |
HTTP/2 connection lifecycle and frame dispatch |
http2_frame.c/h |
Frame parsing and serialization (RFC 7540) |
hpack.c/h |
HPACK header compression (RFC 7541) |
http2_stream.c/h |
Stream management and state machine |
http2_flow.c/h |
Flow control |
http2_settings.c/h |
SETTINGS frame handling |
http2_types.h |
Shared types, constants, error codes |
test_*.c |
Unit tests and property-based tests |
Client Request
│
▼
Protocol Detector ──→ HTTP/1.0 Handler (original logic)
│
▼
HTTP/2 Connection Handler
├── Frame Parser / Serializer
├── HPACK Encoder / Decoder
├── Stream Manager
├── Flow Controller
└── Settings Handler
The project uses property-based testing to verify correctness properties derived from RFC 7540 and RFC 7541:
| Property | Description |
|---|---|
| 1 | Frame parse/serialize roundtrip (帧解析序列化往返一致性) |
| 2 | HPACK encode/decode roundtrip (HPACK 编解码往返一致性) |
| 3 | Huffman encode/decode roundtrip (Huffman 编解码往返一致性) |
| 4 | Stream ID validation (流 ID 验证规则) |
| 5 | Stream state transitions (流状态转换正确性) |
| 6 | Concurrent stream limits (并发流数量限制) |
| 7 | Flow control window invariants (流量控制窗口不变量) |
| 8 | Flow control blocking behavior (流量控制阻塞行为) |
| 9 | SETTINGS parameter validation (SETTINGS 参数验证) |
| 10 | GOAWAY frame correctness (GOAWAY 帧正确性) |
| 11 | PING response correctness (PING 响应正确性) |
| 12 | Protocol upgrade response format (协议升级响应格式) |
| 13 | Connection preface validation (连接前言验证) |
- GCC with C99 support
- pthreads
- Perl + perl-CGI (for CGI scripts)
Original Tinyhttpd by J. David Blackstone (1999), licensed under GPL. HTTP/2 support added as a learning exercise following RFC 7540 and RFC 7541.