高效轻量的C++ HTTP服务:cpp-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 有一个活跃的社区,不断有新的功能和改进被加入。

安装

bash 复制代码
git clone https://github.com/yhirose/cpp-httplib.git

接口介绍

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

cpp 复制代码
struct Request
{
    std::string method;
    std::string path;
    Headers headers;
    std::string body;
    Params params;
};

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

cpp 复制代码
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函数进行对应端口的监听。

cpp 复制代码
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发起对应的请求,参数为请求路径,请求正文和请求的类型。

cpp 复制代码
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

cpp 复制代码
#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

makefile 复制代码
main:main.cc
	g++ -o $@ $^ -std=c++17 -lpthread

.PHONY:clean
clean:
	rm -rf main
复制代码
makefile

```makefile
main:main.cc
	g++ -o $@ $^ -std=c++17 -lpthread

.PHONY:clean
clean:
	rm -rf main
相关推荐
LgZhu(Yanker)1 天前
R/3 销售与分销
大数据·网络·数据库·sap·erp·销售
Python私教1 天前
C 语言运算符全景:从入门到进阶
c语言·开发语言·网络
Mr. zhihao1 天前
理解 HTTPS 和 Burp Suite 证书信任机制
网络协议·http·https
星释1 天前
IIS申请免费证书并配置网页重定向至HTTPS
网络协议·http·https
运维李哥不背锅1 天前
Ansible 模块详解:高效管理你的 IT 基础设施
服务器·网络·ansible
せいしゅん青春之我1 天前
【JavaEE初阶】网络原理——TCP报文结构、确认应答机制
网络·笔记·网络协议·tcp/ip·java-ee
☆璇1 天前
【Linux】数据链路层
linux·服务器·网络
电鱼智能的电小鱼1 天前
基于电鱼 ARM 工控机的AI视频智能分析方案:让传统监控变得更聪明
网络·arm开发·人工智能·嵌入式硬件·算法·音视频
BAGAE1 天前
MQTT 与 HTTP 协议对比
java·linux·http·https·硬件工程