# wltoolkit **Repository Path**: cwl2914/wltoolkit ## Basic Information - **Project Name**: wltoolkit - **Description**: 高性能C++ HTTP工具库,提供轻量级、易用的HttpServer和HttpClient实现。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-28 - **Last Updated**: 2026-03-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # wltoolkit 高性能C++ HTTP工具库,提供轻量级、易用的HttpServer和HttpClient实现。 ## 特性 ### HttpServer - ✅ 高性能异步/多线程架构 - ✅ 支持HTTP/1.1协议 - ✅ 支持GET/POST/PUT/DELETE方法 - ✅ 路由系统(支持参数化路由) - ✅ 请求/响应处理 - ✅ 文件上传支持 - ✅ 跨平台支持(Windows/Linux) - ✅ 连接池管理 ### HttpClient - ✅ 高性能连接池 - ✅ 同步/异步请求支持 - ✅ 批量并发请求 - ✅ 超时控制 - ✅ 自动重定向 - ✅ 连接复用(Keep-Alive) - ✅ 请求统计 ## 依赖 - CMake 3.15+ - C++17或更高版本 - jsoncpp (用于JSON处理,位于 `C:\jsoncpp-1.9.6`) - Google Test (用于测试,可选) ## 编译 ### 编译选项 CMake支持以下编译选项: | 选项 | 默认值 | 说明 | |-------|---------|------| | `BUILD_EXAMPLES` | ON | 是否编译示例程序 | | `BUILD_TESTS` | ON | 是否编译测试程序 | | `BUILD_SHARED_LIBS` | ON | 是否编译动态库(.dll/.so) | | `BUILD_STATIC_LIBS` | ON | 是否编译静态库(.lib/.a) | | `ENABLE_ASAN` | OFF | 是否启用地址检测器(调试用) | ### Linux/macOS ```bash # 克隆项目 git clone https://gitee.com/cwl2914/wltoolkit.git cd wltoolkit # 创建构建目录 mkdir build && cd build # 默认配置(编译所有内容) cmake .. # 或自定义配置 cmake -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON .. # 编译 make -j$(nproc) # 运行测试 make test # 或者直接运行测试 ./test_http ``` ### Windows (Visual Studio) ```cmd # 克隆项目 git clone https://gitee.com/cwl2914/wltoolkit.git cd wltoolkit # 创建构建目录 mkdir build cd build # 默认配置(Release模式) cmake .. -GG "Visual Studio 17 2022" -A x64 # 或自定义配置(不编译示例和测试) cmake .. -G "Visual Studio 17 2022" -A x64 -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF # 编译 cmake --build . --config Release # 运行测试(如果已编译) .\Release\test_http.exe ``` ### Windows (MinGW) ```cmd # 克隆项目 git clone https://gitee.com/cwl2914/wltoolkit.git cd wltoolkit # 创建构建目录 mkdir build && cd build # 配置和编译 cmake .. -G "MinGW Makefiles" mingw32-make # 运行测试 .\test_http.exe ``` ### 仅编译库(不编译示例和测试) ```bash # Linux/macOS cmake -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=ON .. make -j$(nproc) # Windows cmake .. -G "Visual Studio 17 2022" -A x64 -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF cmake --build . --config Release ``` ## 使用示例 ### HttpServer 示例 ```cpp #include #include #include #include "wltoolkit/http_server.h" using namespace wltoolkit; using namespace Json; int main() { // 创建HTTP服务器,监听8080端口 HttpServer server(8080); // 添加GET路由 server.get("/health", [](const HttpRequest& req, HttpResponse& res) { res.set_status(200); res.set_body(R"({"status": "ok"})"); res.set_header("Content-Type", "application/json"); }); // 添加参数化路由 server.get("/users/:id", [](const HttpRequest& req, HttpResponse& res) { int user_id = std::stoi(req.params.at("id")); Value response(objectValue); response["id"] = user_id; response["name"] = "张三"; FastWriter writer; res.set_status(200); res.set_body(writer.write(response)); res.set_header("Content-Type", "application/json"); }); // 添加POST路由 server.post("/users", [](const HttpRequest& req, HttpResponse& res) { // 解析JSON请求体 Value data; Reader reader; reader.parse(req.body, data); // 处理数据 std::string name = data["name"].asString(); res.set_status(201); res.set_body("User created: " + name); }); // 启动服务器(使用4个工作线程) ErrorCode error = server.start(4); if (error == ErrorCode::OK) { std::cout << "Server started on port 8080" << std::endl; // 等待用户输入停止 std::string line; std::getline(std::cin, line); server.stop(); } return 0; } ``` ### HttpClient 示例 ```cpp #include #include #include #include "wltoolkit/http_client.h" using namespace wltoolkit; using namespace Json; int main() { // 创建HTTP客户端 ClientConfig config; config.connection_timeout_ms = 5000; config.request_timeout_ms = 10000; config.keep_alive = true; HttpClient client(config); // 同步GET请求 HttpResponse res; ErrorCode error = client.get("http://localhost:8080/health", res); if (error == ErrorCode::OK) { std::cout << "Status: " << res.status_code() << std::endl; std::cout << "Body: " << res.body() << std::endl; } // 同步POST请求 Value post_data(objectValue); post_data["name"] = "张三"; post_data["email"] = "zhangsan@example.com"; FastWriter writer; error = client.post("http://localhost:8080/users", writer.write(post_data), res); // 异步GET请求 auto future = client.async_get("http://localhost:8080/health"); // 可以在这里做其他工作 AsyncResult result = future.get(); if (result.error == ErrorCode::OK) { std::cout << "Async response: " << result.response.body() << std::endl; } // 批量异步请求 std::vector urls = { "http://localhost:8080/users/1", "http://localhost:8080/users/2", "http://localhost:8080/users/3" }; client.async_batch(urls, [](const std::string& url, const AsyncResult& result) { std::cout << "Response for " << url << ": " << result.response.status_code() << std::endl; }); return 0; } ``` ## 运行示例程序 ### 启动服务器 ```bash # Linux/macOS ./server_example # Windows .\server_example.exe ``` ### 运行客户端 ```bash # Linux/macOS ./client_example # Windows .\client_example.exe ``` ## 运行测试 ```bash # 使用CMake make test # 或者直接运行 ./test_http ``` ## 项目结构 ``` wltoolkit/ ├── CMakeLists.txt # CMake构建配置 ├── README.md # 项目文档 ├── include/ # 头文件 │ └── wltoolkit/ │ ├── http_server.h # HTTP服务器 │ ├── http_client.h # HTTP客户端 │ └── common.h # 公共定义 ├── src/ # 源文件 │ ├── http_server.cpp │ ├── http_client.cpp │ └── common.cpp ├── examples/ # 示例程序 │ ├── server_example.cpp │ └── client_example.cpp └── tests/ # 单元测试 └── test_http.cpp ``` ## API文档 ### HttpServer #### 构造函数 ```cpp HttpServer(uint16_t port = 80); ``` #### 主要方法 ```cpp ErrorCode start(int worker_threads = 4); // 启动服务器 void stop(); // 停止服务器 void get(const std::string& path, RequestHandler handler); void post(const std::string& path, RequestHandler handler); void put(const std::string& path, RequestHandler handler); void del(const std::string& path, RequestHandler handler); ``` ### HttpClient #### 构造函数 ```cpp HttpClient(const ClientConfig& config = ClientConfig()); ``` #### 同步请求 ```cpp ErrorCode get(const std::string& url, HttpResponse& response); ErrorCode post(const std::string& url, const std::string& body, HttpResponse& response); ErrorCode put(const std::string& url, const std::string& body, HttpResponse& response); ErrorCode del(const std::string& url, HttpResponse& response); ``` #### 异步请求 ```cpp std::future async_get(const std::string& url); std::future async_post(const std::string& url, const std::string& body); std::future async_put(const std::string& url, const std::string& body); std::future async_del(const std::string& url); ``` ## 性能优化 - 使用线程池处理并发请求 - 连接复用减少TCP握手开销 - 零拷贝数据传输(尽可能使用) - 内存池减少内存分配 - 异步I/O提高吞吐量 ## 许可证 MIT License ## 贡献 欢迎提交Issue和Pull Request! ## 联系方式 - 项目地址: https://gitee.com/cwl2914/wltoolkit.git - 作者: cwl2914 ## 更新日志 ### v1.0.0 (2026-02-28) - 初始版本发布 - 实现HttpServer和HttpClient核心功能 - 支持HTTP/1.1协议 - 支持同步/异步请求 - 提供完整的使用示例和单元测试