HttpRequest请求模块设计与实现(http模块二)

目录

类功能

类定义

类实现

编译测试


类功能

类定义

cpp 复制代码
// HttpRequest请求模块
class HttpRequest
{
public:
    std::string _method;                                   // 请求方法
    std::string _path;                                     // 资源路径
    std::string _version;                                  // 协议版本
    std::string _body;                                     // 请求正文
    std::smatch _matches;                                  // 资源路径的正则提取数据
    std::unordered_map<std::string, std::string> _headers; // 头部字段
    std::unordered_map<std::string, std::string> _params;  // 查询字符串
public:
    // 重置请求 -- 每一次请求的到来都要重置上一次请求的数据
    void ReSet();
    // 插入头部字段
    void SetHeader(const std::string &key, const std::string &val);
    // 判断是否存在指定头部字段
    bool HasHeader(const std::string &key);
    // 获取指定头部字段的值
    std::string GetHeader(std::string &key);
    // 插入查询字符串
    void SetParam(const std::string &key, const std::string &val);
    // 判断是否有某个指定的查询字符串
    bool HasParam(const std::string &key);
    // 获取指定的查询字符串
    std::string GetParam(std::string &key);
    // 获取正文长度
    size_t ContentLength();
    // 判断是否是短链接
    bool Close();
};

类实现

cpp 复制代码
// HttpRequest请求模块
class HttpRequest
{
public:
    std::string _method;                                   // 请求方法
    std::string _path;                                     // 资源路径
    std::string _version;                                  // 协议版本
    std::string _body;                                     // 请求正文
    std::smatch _matches;                                  // 资源路径的正则提取数据
    std::unordered_map<std::string, std::string> _headers; // 头部字段
    std::unordered_map<std::string, std::string> _params;  // 查询字符串
public:
    // 重置请求 -- 每一次请求的到来都要重置上一次请求的数据
    void ReSet()
    {
        _method.clear();
        _path.clear();
        _version.clear();
        _body.clear();
        std::smatch match; // smatch 内并没有clear的接口,所以使用交换进行清理
        _matches.swap(match);
        _headers.clear();
        _params.clear();
    }
    // 插入头部字段
    void SetHeader(const std::string &key, const std::string &val)
    {
        _headers.insert(std::make_pair(key, val));
    }
    // 判断是否存在指定头部字段
    bool HasHeader(const std::string &key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
            return false;
        return true;
    }
    // 获取指定头部字段的值
    std::string GetHeader(const std::string &key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
            return "";
        return it->second;
    }
    // 插入查询字符串
    void SetParam(const std::string &key, const std::string &val)
    {
        _params.insert(std::make_pair(key, val));
    }
    // 判断是否有某个指定的查询字符串
    bool HasParam(const std::string &key)
    {
        auto it = _params.find(key);
        if (it == _params.end())
            return false;
        return true;
    }
    // 获取指定的查询字符串
    std::string GetParam(const std::string &key)
    {
        auto it = _params.find(key);
        if (it == _params.end())
            return "";
        return it->second;
    }
    // 获取正文长度
    size_t ContentLength()
    {   
        // 下面的字符串肯定是已经被解析放置在_headers内了
        // Content-Length: 1234\r\n
        bool ret = HasHeader("Content-Length");
        if (ret == false)
            return 0;
        std::string clen = GetHeader("Content-Length");
        return std::stol(clen); // 字符转长整型
    }
    // 判断是否是短链接
    bool Close()
    {
        // 没有Connection字段,或者有Connection字段但是值是close,则是短链接,否则就是长链接
        // keep-alive是长链接的意思
        if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive")
            return true;
        return false;
    }
};

编译测试

因为和下一个模块的联动性较强所以,关于编译测试的部分就放在下一篇文章中去了

HttpResponse响应模块设计与实现(http模块三)-CSDN博客

相关推荐
Abdullah al-Sa14 分钟前
Docker教程(喂饭级!)
c++·人工智能·docker·容器
进击的_鹏17 分钟前
【C++】list 链表的使用+模拟实现
开发语言·c++·链表
web2u28 分钟前
Docker入门及基本概念
java·运维·服务器·spring·docker·容器
C语言扫地僧1 小时前
RPC 框架项目剖析
c++·网络协议·学习·rpc
水月梦镜花2 小时前
数据结构:基数排序(c++实现)
开发语言·数据结构·c++
YGGP2 小时前
基于 go-wrk 在 Windows 环境下对 Go Web 应用进行 HTTP 压力测试
http·golang·压力测试
Anna_Tong2 小时前
阿里云如何协助解决操作系统兼容性问题
linux·服务器·ubuntu·阿里云·centos·云计算·系统迁移
HaoHao_0102 小时前
如何将MySQL数据库迁移至阿里云
服务器·数据库·阿里云·云计算·云服务器·迁移
不良人天码星2 小时前
Linux的基础指令和环境部署,项目部署实战(下)
linux·运维·服务器
火一线3 小时前
【ASP .NET Core】ASP .NET Core介绍
服务器·游戏·.netcore