Skip to content

Commit c2e6e9a

Browse files
committed
Update README.md
1 parent bb24cc6 commit c2e6e9a

1 file changed

Lines changed: 130 additions & 90 deletions

File tree

README.md

Lines changed: 130 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,58 @@
1-
# simple_http
1+
# simple_http 🚀
2+
3+
> **Lightweight, Modern, High-Performance C++20 HTTP/1.1 & HTTP/2 Framework.**
24
35
[![gcc](https://github.com/fantasy-peak/simple_http/actions/workflows/gcc.yaml/badge.svg)](https://github.com/fantasy-peak/simple_http/actions/workflows/gcc.yaml)
46
[![clang](https://github.com/fantasy-peak/simple_http/actions/workflows/clang.yaml/badge.svg)](https://github.com/fantasy-peak/simple_http/actions/workflows/clang.yaml)
5-
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6-
7-
`simple_http` is a lightweight, high-performance, asynchronous HTTP/1.1 & HTTP/2 server and client library for C++20, built upon Boost.Asio and nghttp2. It is designed as header-only for easy integration and use.
8-
9-
## ✨ Features
7+
![C++ Standard](https://img.shields.io/badge/C%2B%2B-20%2F23%2F26-blue.svg)
8+
[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
109

11-
- **Header-only**: Easy to integrate into any project by just including headers.
12-
- **Modern C++**: Leverages C++20/23/26 and coroutines for clean, asynchronous logic.
13-
- **Dual Protocol Support**: Supports both HTTP/1.1 and HTTP/2 with automatic negotiation.
14-
- **Server & Client**: Provides a consistent API for both server and client functionalities.
15-
- **Secure Communication**: Supports HTTPS and TLS mutual authentication (mTLS).
16-
- **Highly Configurable**: Easily configure server worker threads, concurrent stream limits, window sizes, and more.
17-
- **Middleware Support**: Offers a `setBefore` interface for middleware-style request pre-processing.
18-
- **IPv4 & IPv6**: Full dual-stack support.
19-
- **UNIX Domain Sockets**: High-performance local IPC with zero network overhead.
10+
`simple_http` is a lightweight, asynchronous HTTP/1.1 & HTTP/2 framework for C++20/23/26. Built upon **Boost.Beast** and **nghttp2**, it leverages modern C++ coroutines to provide a clean, high-performance API for both servers and clients.
2011

21-
## 🚀 Requirements
12+
---
2213

23-
- **C++20/23/26** compatible compiler (e.g., GCC 13+, Clang 20+).
24-
- **xmake**: Used for building examples and dependency management.
25-
- **Boost** (`beast`)
26-
- **nghttp2**
27-
- **OpenSSL**
14+
## 📖 Table of Contents
15+
- [✨ Features](#-features)
16+
- [🚀 Quick Start](#-quick-start)
17+
- [📦 Requirements](#-requirements)
18+
- [🧪 C++20 Modules Support (Experimental)](#-c20-modules-support-experimental)
19+
- [🛠 Configuration Macros](#-configuration-macros)
20+
- [📊 Performance](#-performance)
21+
- [🧪 Testing Guide](#-testing-guide)
22+
- [🤝 Contributing](#-contributing)
2823

29-
> **Note**: When building with `xmake`, it automatically downloads and links all necessary dependencies, so you don't need to install them manually.
24+
---
3025

31-
## 📦 C++20 Modules Support (Experimental)
26+
## ✨ Features
3227

33-
`simple_http` provides experimental support for C++20 Modules via `include/simple_http.cppm`. This allows for faster compilation and better code isolation compared to traditional header inclusions.
28+
- **📦 Header-only**: Simple to integrate; just include and go.
29+
- **🛡️ Modern C++**: Built with C++20/23/26 coroutines for intuitive async logic.
30+
- **🔄 Dual Protocol**: Seamless HTTP/1.1 & HTTP/2 support with ALPN negotiation.
31+
- **🔄 Server & Client**: Symmetrical API design for both roles.
32+
- **🔒 Secure**: Robust HTTPS and mTLS (Mutual TLS) support.
33+
- **🔌 Advanced Transport**: Supports **IPv4/IPv6** and **UNIX Domain Sockets** for high-speed local IPC.
34+
- **🧩 Middleware**: Flexible `setBefore` interceptors for pre-processing.
35+
- **🌊 Full Streaming**: Bi-directional streaming for client and server.
36+
- **🌐 Proxy**: Built-in client-side HTTP proxy support.
3437

35-
> **Note**: Currently, C++20 Modules support is verified on **Clang 20+**. Support for GCC and MSVC is planned.
38+
---
3639

37-
### Usage in C++
38-
To use the module, simply replace your `#include` with an `import` statement:
40+
## 🚀 Quick Start
3941

42+
### Simple Server Snippet
4043
```cpp
4144
import simple_http;
4245

43-
simple_http::HttpServer hs(cfg);
44-
```
45-
46-
### Integration with xmake
47-
The following example shows how to integrate `simple_http` as a C++20 module in your `xmake.lua` project:
48-
49-
```lua
50-
add_requires("simple_http")
51-
52-
set_policy("build.c++.modules", true)
53-
set_policy("build.c++.modules.std", true)
54-
55-
target("server")
56-
set_kind("binary")
57-
58-
add_cxflags("-fuse-ld=mold")
59-
add_cxxflags("-stdlib=libc++")
60-
61-
on_load(function (target)
62-
local pkg = target:pkg("simple_http")
63-
if pkg then
64-
local installdir = pkg:installdir()
65-
local module_file = path.join(installdir, "include", "simple_http.cppm")
66-
target:add("files", module_file)
67-
print("Successfully linked C++20 module from: " .. module_file)
68-
end
69-
end)
70-
71-
add_files("src/main.cpp")
72-
add_packages("simple_http")
46+
hs.setHttpHandler("/hello", [](auto req, auto writer) -> asio::awaitable<void> {
47+
writer->writeHttpResponse(simple_http::makeHttpResponse(http::status::ok, "Hello World!"));
48+
co_return;
49+
});
7350
```
7451

52+
<details>
53+
<summary><b>Click to view Full Server Example</b></summary>
7554

76-
## 🛠 Configuration Macros
77-
78-
The following macros can be defined to enable or customize specific features:
79-
80-
- `SIMPLE_HTTP_EXPERIMENT_WEBSOCKET`: Enables experimental WebSocket support.
81-
- `SIMPLE_HTTP_EXPERIMENT_HTTP2CLIENT`: Enables experimental HTTP/2 client support.
82-
- `SIMPLE_HTTP_USE_BOOST_REGEX`: Uses `boost::regex` instead of `std::regex` for header parsing and routing. This can provide better performance and compatibility in certain environments.
83-
- `SIMPLE_HTTP_BIND_UNIX_SOCKET`: Enables support for binding the server to UNIX Domain Sockets (UDS) for high-performance local IPC.
84-
85-
## Server Example
86-
```
55+
```cpp
8756
import std;
8857
import simple_http;
8958

@@ -101,7 +70,6 @@ asio::awaitable<void> start() {
10170
.ssl_ca = "./test/tls_certificates/ca_cert.pem",
10271
.socket_setup_cb =
10372
[](asio::ip::tcp::socket& socket) {
104-
// Set socket properties
10573
socket.set_option(asio::socket_base::keep_alive(true));
10674
},
10775
.enable_ipv6 = true,
@@ -131,6 +99,13 @@ asio::awaitable<void> start() {
13199
writer->writeStreamEnd();
132100
co_return;
133101
});
102+
hs.setHttpHandler("/world",
103+
[](auto req, auto writer) -> asio::awaitable<void> {
104+
auto response = simple_http::makeHttpResponse(http::status::ok);
105+
response->body() = "ok!";
106+
writer->writeHttpResponse(response);
107+
co_return;
108+
});
134109
co_await hs.start();
135110
}
136111

@@ -143,8 +118,12 @@ int main() {
143118
return 0;
144119
}
145120
```
146-
## Client Example
147-
```
121+
</details>
122+
123+
<details>
124+
<summary><b>Click to view Full Client Example</b></summary>
125+
126+
```cpp
148127
import std;
149128
import simple_http;
150129

@@ -161,7 +140,6 @@ asio::awaitable<void> client(simple_http::IoCtxPool& pool) {
161140
.ssl_context = nullptr,
162141
.tlsext_host_name = "SimpleHttpServer",
163142
};
164-
// only support h2 and h2c not support http1.1
165143
auto client = std::make_shared<simple_http::Http2Client>(cfg, pool.getIoContextPtr());
166144
auto [ret, err] = co_await client->asyncStart(std::chrono::seconds(5), asio::use_awaitable);
167145
if (!ret) {
@@ -211,8 +189,7 @@ int main() {
211189
pool.start();
212190
asio::co_spawn(pool.getIoContext(), client(pool), [](const std::exception_ptr& ep) {
213191
try {
214-
if (ep)
215-
std::rethrow_exception(ep);
192+
if (ep) std::rethrow_exception(ep);
216193
} catch (const std::exception& e) {
217194
SIMPLE_HTTP_ERROR_LOG("{}", e.what());
218195
} catch (...) {
@@ -224,37 +201,100 @@ int main() {
224201
return 0;
225202
}
226203
```
204+
</details>
227205
228-
## 📊 Performance
206+
---
229207
230-
The following benchmark was performed using `h2load`.
208+
## 📦 Requirements
209+
- **C++20/23/26** (GCC 13+, Clang 20+)
210+
- **xmake** Used for building examples and dependency management.
211+
- **Dependencies**: Boost.Beast, nghttp2, OpenSSL
231212
232-
**Machine Configuration:**
233-
- **OS**: Ubuntu 25.10
234-
- **CPU**: 13th Gen Intel(R) Core(TM) i7-13620H (16 vCPUs)
235-
- **Memory**: 41Gi RAM
213+
## 🧪 C++20 Modules Support (Experimental)
214+
`simple_http` provides native C++20 Modules support via `include/simple_http.cppm`.
236215
237-
**Test Configuration:**
238-
- **Request Size**: 1KB (via `b.txt`)
239-
- **Response Size**: 10KB
240-
- **Command**: `h2load -t 4 -n 1000000 -c 1000 -m 40 -H 'Content-Type: application/json' --data=b.txt http://localhost:7788/hello`
216+
> **Status**: Verified on Clang 20+. Support for GCC and MSVC is on the roadmap.
241217
242-
**Results:**
218+
#### xmake Integration
219+
220+
<details>
221+
<summary><b>Click to view detailed xmake.lua configuration</b></summary>
222+
223+
The following example shows how to integrate `simple_http` as a C++20 module in your `xmake.lua` project:
224+
225+
```lua
226+
add_requires("simple_http")
227+
228+
set_policy("build.c++.modules", true)
229+
set_policy("build.c++.modules.std", true)
230+
231+
target("server")
232+
set_kind("binary")
233+
234+
add_cxflags("-fuse-ld=mold")
235+
add_cxxflags("-stdlib=libc++")
236+
237+
on_load(function (target)
238+
local pkg = target:pkg("simple_http")
239+
if pkg then
240+
local installdir = pkg:installdir()
241+
local module_file = path.join(installdir, "include", "simple_http.cppm")
242+
target:add("files", module_file)
243+
print("Successfully linked C++20 module from: " .. module_file)
244+
end
245+
end)
246+
247+
add_files("src/main.cpp")
248+
add_packages("simple_http")
249+
```
250+
</details>
251+
252+
---
253+
254+
## 🛠 Configuration Macros
255+
256+
| Macro | Description |
257+
| :--- | :--- |
258+
| `SIMPLE_HTTP_EXPERIMENT_WEBSOCKET` | Enables experimental WebSocket support. |
259+
| `SIMPLE_HTTP_EXPERIMENT_HTTP2CLIENT` | Enables experimental HTTP/2 client support. |
260+
| `SIMPLE_HTTP_USE_BOOST_REGEX` | Uses `boost::regex` instead of `std::regex` for better performance. |
261+
| `SIMPLE_HTTP_BIND_UNIX_SOCKET` | Enables support for binding to UNIX Domain Sockets (UDS). |
262+
263+
---
264+
265+
## 📊 Performance
266+
267+
Benchmark conducted using `h2load` on **Ubuntu 25.10 | i7-13620H (16 vCPUs) | 41Gi RAM**.
268+
269+
| Metric | Result |
270+
| :--- | :--- |
271+
| **Throughput** | **~99,558 req/s** |
272+
| **Transfer Rate** | **~976.28 MB/s** |
273+
| **Success Rate** | 100% (1,000,000 requests) |
274+
275+
**Detailed Results:**
243276

244277
```text
245278
finished in 10.04s, 99558.82 req/s, 976.28MB/s
246279
requests: 1000000 total, 1000000 started, 1000000 done, 1000000 succeeded, 0 failed, 0 errored, 0 timeout
247280
status codes: 1000000 2xx, 0 3xx, 0 4xx, 0 5xx
248281
traffic: 9.58GB (10282450426) total, 2.89MB (3026000) headers (space savings 95.12%), 9.54GB (10243000000) data
282+
249283
min max mean sd +/- sd
250284
time for request: 5.99ms 1.43s 342.76ms 137.20ms 77.08%
251285
time for connect: 5.65ms 151.01ms 62.93ms 37.19ms 67.60%
252286
time to 1st byte: 79.20ms 1.02s 537.90ms 308.48ms 51.80%
253287
req/s : 100.72 124.03 105.92 4.56 71.10%
254288
```
255289

256-
## Test Cmd
257-
```
290+
> **Benchmark Command**:
291+
> `h2load -t 4 -n 1000000 -c 1000 -m 40 -H 'Content-Type: application/json' --data=b.txt http://localhost:7788/hello`
292+
293+
---
294+
295+
## 🧪 Testing Guide
296+
297+
```bash
258298
curl -N -v --http2-prior-knowledge http://localhost:7788/hello\?key1\=value1\&key2\=value2
259299
curl -N -v --http2-prior-knowledge http://localhost:7788/hello -d "abcd"
260300
curl -N -v --http2 http://localhost:7788/hello -d "abcd"
@@ -267,10 +307,10 @@ need define SIMPLE_HTTP_BIND_UNIX_SOCKET macro
267307
curl --unix-socket /tmp/simple_http.sock https://SimpleHttpServer:7788/hello?123456 --cacert ca_cert.pem --cert client_cert.pem --key client_key.pem -X POST -d "123"
268308
```
269309

270-
## 🤝 Contributing
310+
---
271311

272-
Contributions of any kind are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) to learn how to contribute to the project.
312+
## 🤝 Contributing
313+
Contributions are welcome! Please check [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
273314

274315
## 📄 License
275-
276316
`simple_http` is licensed under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)