当前位置: 首页 > news >正文

高效轻量的C++ HTTP服务:cpp-httplib使用指南

文章目录

    • httplib介绍与安装
    • 使用案例

httplib介绍与安装

C++ HTTP 库(cpp-httplib)是一个轻量级的 C++ HTTP 客户端/服务器库,它提供了简单的 API 来创建 HTTP 服务器和客户端,支持同步和异步操作。以下是一些关于cpp-httplib 的主要特点:

1.轻量级:cpp-httplib 的设计目标是简单和轻量,只有一个头文件包含即可,不依赖于任何外部库。

2.跨平台:它支持多种操作系统,包括 Windows、Linux 和 macOS。

3.同步和异步操作:库提供了同步和异步两种操作方式,允许开发者根据需要选择。

4.支持 HTTP/1.1:它实现了 HTTP/1.1 协议,包括持久连接和管道化。

5.Multipart form-data:支持发送和接收 multipart/form-data 类型的请求,这对于文件上传非常有用。

6.SSL/TLS 支持:通过使用 OpenSSL 或 mbedTLS 库,cpp-httplib 支持 HTTPS 和 WSS。

7.简单易用:API 设计简洁,易于学习和使用。

8.性能:尽管是轻量级库,但性能表现良好,适合多种应用场景。

9.社区活跃:cpp-httplib 有一个活跃的社区,不断有新的功能和改进被加入。

安装

git clone https://github.com/yhirose/cpp-httplib.git

接口介绍

请求中包含请求方法,请求的资源路径,请求头部,请求正文和查询字符串。

struct Request
{std::string method;std::string path;Headers headers;std::string body;Params params;
};

响应中包含HTTP协议版本,响应状态码,响应状态码描述,响应头部,响应正文。

struct Response
{std::string version;int status = -1;std::string reason;Headers headers;std::string body;void set_content(const std::string &s,const std::string &content_type);void set_header(const std::string &key,const std::string &val);
};   

服务器句柄,通过GET/POST/PUT/DELETE函数处理对应的HTTP请求,设置请求路径和对应的回调函数即可,通过listen函数进行对应端口的监听。

class Server
{using Handler = std::function<void(const Request&, Response&)>;Server &Get(const std::string &pattern, Handler handler);Server &Post(const std::string &pattern, Handler handler);Server &Put(const std::string &pattern, Handler handler);Server &Delete(const std::string &pattern, Handler handler);bool listen(const std::string &host, int port);
};

客户端句柄同样也是GET/POST/PUT/DELETE发起对应的请求,参数为请求路径,请求正文和请求的类型。

class Client
{explicit Client(const std::string &host, int port);Result Get(const std::string &path, const Headers &headers);Result Post(const std::string &path, const std::string &body,const std::string &content_type);Result Put(const std::string &path, const std::string &body,const std::string &content_type);Result Delete(const std::string &path, const std::string &body,const std::string &content_type);
};

使用案例

main.cc

#include "../common/httplib.h"
#include <iostream>int main()
{httplib::Server server;// using Handler = std::function<void(const Request &, Response &)>;server.Get("/hello",[](const httplib::Request & req, httplib::Response & resp){std::cout << "method: " << req.method << std::endl;std::cout << "path: " << req.path << std::endl;std::string body = "<html><body><h1>Hello World</h1></body></html>";resp.set_content(body,"text/html");resp.status = 200;});server.listen("0.0.0.0",8080);return 0;
}

makefile

main:main.ccg++ -o $@ $^ -std=c++17 -lpthread.PHONY:clean
clean:rm -rf main

makefile```makefile
main:main.ccg++ -o $@ $^ -std=c++17 -lpthread.PHONY:clean
clean:rm -rf main

image-20250214203709570

http://www.dtcms.com/a/311202.html

相关文章:

  • Android 15 限制APK包手动安装但不限制自升级的实现方案
  • 把“多视图融合、深度传感”组合在一起,今天分享3篇3D传感技术干货
  • [硬件电路-120]:模拟电路 - 信号处理电路 - 在信息系统众多不同的场景,“高速”的含义是不尽相同的。
  • Word怎样转换为PDF
  • Qwen3 Embedding:新一代文本表征与排序模型
  • 2411. 按位或最大的最小子数组长度
  • Django开发中医针灸经络图系统实战
  • 【iOS】3GShare仿写
  • 【Linux网络】netstat 的 -anptu 各个参数各自表示什么意思?
  • 2025 年 VSCode 插件离线下载硬核攻略
  • 打破传统养育框架:梁婉昕的 “非矫正式教育” 探索|创客匠人
  • 八股取士--docker
  • 在 AKS 中运行 Azure DevOps 自托管代理-2
  • 贪心算法应用:3D打印支撑结构问题详解
  • CommonJS和ES6 Modules区别
  • 如何安装和使用 Cursor AI 编辑器
  • 深度解读 | 斯坦福:2025 AI 指数报告
  • 【深度学习新浪潮】什么是专业科研智能体?
  • 【OpenGL】LearnOpenGL学习笔记01 - 环境配置、窗口创建
  • RS232转Profinet网关与西门子S7-1200 PLC的智能化工业通信应用
  • 区块链笔记
  • 李宏毅NLP-10-语音分离
  • (1-8-1) Java -XML
  • 关于Web前端安全防御XSS攻防的几点考虑
  • Unity_数据持久化_XML存储相关
  • 第三十九章:AI导演的“魔法时间轴”:文生视频与Video Latent扩散结构
  • [ LeetCode-----盛最多的水]
  • c++ 链表知识汇总
  • FreeRTOS源码分析一:task创建(RISCV架构)
  • 【Pytorch✨】LSTM 入门