[C++][第三方库][httplib]详细讲解

目录


1.介绍

  • C++ HTTP库(cpp-httplib)是一个轻量级的C++ HTTP客户端/服务器库,它提供了简单的API来创建HTTP服务器和客户端,支持同步和异步操作
  • 特点
    • 轻量级cpp-httplib的设计目标是简单和轻量,只有一个头文件包含即可,不依赖于任何外部库
    • 跨平台:它支持多种操作系统,包括Windows、Linux、MacOS
    • 同步和异步操作:提供了同步和异步两种操作方式,允许开发者根据需要选择
    • 支持HTTP/1.1:实现了HTTP/1.1协议,包括持久连接和管道化
    • Multipart from-data :支持发送和接收multipart/form-data类型的请求,这对于文件上传非常有用
    • SSL/TLS :通过使用OpenSSLmbedTLS库,cpp-httplib支持HTTPS和WSS
    • 简单易用:API设计简洁,易于学习和使用
    • 性能:尽管是轻量级库,但性能表现良好,适合多种应用场景
    • 社区活跃cpp-httplib有一个活跃的社区,不断有新的功能和改进被加入

2.安装

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

3.类与接口

  • httplib回调处理流程 :在一个哈希表内,维护某个方法内对应请求资源的对应处理方法

    cpp 复制代码
    namespace httplib 
    { 
        struct Request 
        { 
            std::string method; 
            std::string path; 
            Headers headers; 
            std::string body; 
            Params params; 
        };
    
        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); 
        };
        
        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); 
        };
        
        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); 
        };
    } 

4.使用

  • 服务器搭建流程

    • 实例化服务器对象Server
    • 注册回调函数,告诉服务器收到哪个请求,应该使用哪个回调函数进行处理
    • 启动服务器
    cpp 复制代码
    #include "httplib.h"
    
    int main()
    {
        // 1.实例化服务器对象
        httplib::Server svr;
    
        // 2.注册回调函数  void(const httplib::Request&, const httplib::Response&)
        svr.Get("/SnowK", [](const httplib::Request& req, httplib::Response& resp)
        {
            std::cout << req.method << std::endl;
            std::cout << req.path << std::endl;
            for(auto& iter : req.headers)
            {
                std::cout << iter.first << ": " << iter.second << std::endl;
            }
    
            std::string body("<html><body><h1>Hello SnowK<h1><body><html>");
            resp.set_content(body, "text/html");
            resp.status = 200;
        });
    
        // 3.启动服务器
        svr.listen("0.0.0.0", 9200);
    
        return 0;
    }
相关推荐
命运之光9 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
不会就选b9 小时前
Linux之进程间通信(二)---模拟进程池
linux·运维·服务器
命运之光9 小时前
【C语言完整代码】图书管理系统:图书馆场景下的增删改查实战
c语言·开发语言
猿长大人9 小时前
C# | Serilog 新手入门
开发语言·c#·.net·log
NeilYuen10 小时前
用UDP实现向DNS服务器请求IP地址
服务器·tcp/ip·udp
hansang_IR10 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘10 小时前
day7.c++继承
c++
在世修行11 小时前
从零打造 C# 工业视觉检测系统(七):串口通信 SerialPort 封装与开发
开发语言·c#·modbus rtu·rs232/rs485
沫璃染墨11 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌11 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习