httplib库的安装以及使用

Posted 努力学习的少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httplib库的安装以及使用相关的知识,希望对你有一定的参考价值。

目录

安装httplib库

认识httplib库

httplib请求类

 httplib响应类

 httplib中的Server类       

httplib的client类

httplib库搭建简单的服务器

httplib库搭建简单的客户端


安装httplib库

1.进入github,搜索httplib库

 2.下载库

3.下载完毕将库的压缩包传输到linux系统下

 4.解压库

unzip cpp-httplib-master.zip

认识httplib库

  • httplib 库,一个 C++11 单文件头的跨平台 HTTP/HTTPS 库。
  • httplib 库实际上是用于搭建一个简单的 http 服务器或者客户端的库,这种第三方网络库,可以让我们免去搭建服 务器或客户端的时间,把更多的精力投入到具体的业务处理中,提高开发效率

httplib请求类

  • httplib请求类是它包含了http请求中的请求行的请求方法,url,协议版本以及请求报头中的各个响应报头值
namespace httplib
    //文件信息结构体
    struct MultipartFormData 
        std::string name;        //字段名称
        std::string content;     //文件内容
        std::string filename;    //文件名称
        std::string content_type;//文件类型
   ;
  using MultipartFormDataItems = std::vector<MultipartFormData>;

    struct Request 
        std::string method;  //请求方法
        std::string path;    //资源路径
        Headers headers;     //请求报头
        std::string body;    //请求正文
        // for server

        std::string version; //协议版本
        Params params;       //查询字符串
        MultipartFormDataMap files;//保存的是客户端上传的文件信息
        Ranges ranges;       //

        //判断请求报头中有没有 某个字段
        bool has_header(const char *key) const;
        //获取请求报头中对应的字段值
        std::string get_header_value(const char *key, size_t id = 0) const;
        //将key-val的字段值设定在http请求中
        void set_header(const char *key, const char *val);
        //判定是否有对应的文件
        bool has_file(const char *key) const;
        //获取对应的文件信息
        MultipartFormData get_file_value(const char *key) const;
   ;

 httplib响应类

httplib类是将响应行,响应报头,响应正文设定到Response类的对象中,然后将Response对象组织成http响应的形式发送给对方。

    struct Response 
        std::string version;   //协议版本号,默认时http1.1
        int status = -1;       //响应状态码,
        std::string reason;    
        Headers headers;       //响应报头
        std::string body;      //响应正文
        std::string location; // 重定向位置

//以key-val将相应的字段设定到响应报头中
 void set_header(const char *key, const char *val);
        void set_content(const std::string &s, const char *content_type);
   ;

 httplib中的Server类       


class Server 
        //Handler一个函数指针名称,它的参数是Request,和Response
        using Handler = std::function<void(const Request &, Response &)>;
        //Handlers是一个映射表,它映射对应的请求资源和处理函数映射在一起
        using Handlers = std::vector<std::pair<std::regex, Handler>>;
        //将Get方法的请求资源与处理函数加载到Handlers表中
        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 &Patch(const std::string &pattern, Handler handler);  
        Server &Delete(const std::string &pattern, Handler handler);
        Server &Options(const std::string &pattern, Handler handler);
        //线程池
        std::function<TaskQueue *(void)> new_task_queue;

        //搭建并启动http
        bool listen(const char *host, int port, int socket_flags = 0);
 ;

Handler表结构:

 如上面的Handlers表中:

  • 如果http请求的请求方法是GET方法,且请求资源是/hello,那么服务器则会调用Hello函数,构建相对应的http响应。
  • 如果http请求的请求方法是POST方法,且请求资源是/Post,那么服务器则会调用Post函数. 构建相对应的http响应。
  • 如果http请求中的请求方法和请求资源中 只要有一个不存在Handlers表中或者不符合在Handlers表中,则http服务器会返回一个404的http响应。
  • Get("/hello",Hello):将请求方为GET,请求资源/hello,与函数Hello 注册 在Handlers表中
  • Post("/post",Post):将请求方法POST,请求资源/post,与函数Post 注册 在Handlers表中

线程池的工作

  • 当服务器收到一个http请求的时候,就会将该http请求放入到线程池中,线程池中的线程就会调用相对应的函数解析http请求形成Request类。
  • 在Handler映射表中,如果有相对应请求方法和请求资源有相对应的映射函数,则会调用相对应的映射函数,并构建好http响应.
  • 当处理函数调用完后,则将构建好的http响应发送给客户端。

httplib的client类

   class Client 
        //构造一个客户端对象,传入服务器Ip地址和端口
        Client(const std::string &host, int port);
        //向服务器发送GET请求
        Result Get(const char *path, const Headers &headers);
        //向服务器发送Post请求
        //path是路径,body是request请求路径
        //content_length是正文大小
        //content_type是正文的类型
        Result Post(const char *path, const char *body, size_t content_length,
              const char *content_type);
        
        //以Post方法上传文件
        Result Post(const char *path, const MultipartFormDataItems &items);
   

httplib库搭建简单的服务器

搭建服务器的步骤:

  • 利用httplib库定义server对象
  • 在Handler表结构中注册 请求资源的处理函数
  • 用listen启动服务器
   #include<iostream>    
  #include"httplib.h"    
  using namespace std;    
      
W>void Hello(const httplib::Request& req,httplib::Response& res)    
    res.set_content("Hello world","text/plain");    
    res.status=200;    
      
      
      
  void File(httplib::Request req,httplib::Response res)
    //获取字段为file的文件
    const auto& file=req.get_file_value("file");    
  
    std::cout<<file.filename<<endl;        
    std::cout<<file.content<<endl;    
      
      
  int main()    
    httplib::Server server;//定义sercer对象    
    //注册处理函数    
    server.Get("/hi",Hello);    
    server.Post("/file",File);    
    server.listen("0.0.0.0",8081);//启动服务器    
    return 0;    
      

 

注意:

  •  注册处理函数中的Request参数需要定义为const
  • 注册处理函数中Request和Response参数都需要是引用,否则浏览器会接受不到正文内容.

httplib库搭建简单的客户端

#include"httplib.h"    
#define IP "119.23.41.13"    
#define PORT 8081    
using namespace std;    
    
int main()    
  //建立客户端对象    
  httplib::Client client(IP,PORT); 
  //单个文件信息组织   
  httplib::MultipartFormData file;    
  file.name="file";        
  file.content="hello world";    
  file.filename="Hello.txt";    
  file.content_type="text/plain";    
  
  //MultipartFormDataItems对象可以存储多个文件信息
  httplib::MultipartFormDataItems item;    
  item.push_back(file);    
  //请求服务器上/file资源,发送item文件集合给服务器
  auto result=client.Post("/file",item);     
  return 0;                                                                                                                                 
    

 

 

 

以上是关于httplib库的安装以及使用的主要内容,如果未能解决你的问题,请参考以下文章

项目Httplib库的简单学习和使用

项目Httplib库的简单学习和使用

项目Httplib库的简单学习和使用

Python网页分析httplib库的重定向处理

开源代码:Http请求封装类库HttpLib介绍使用说明

cmake: MinGW编译时为httplib.h增加连接库支持