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博客

相关推荐
群联云防护小杜5 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
van叶~20 分钟前
Linux探秘坊-------15.线程概念与控制
linux·运维·服务器
en-route25 分钟前
Http请求中的特殊字符
spring·http
2301_780789664 小时前
UDP和TCP的主要区别是什么
服务器·网络协议·web安全·网络安全·udp
古月-一个C++方向的小白5 小时前
C++11之lambda表达式与包装器
开发语言·c++
一个龙的传说7 小时前
linux 常用命令
linux·服务器·zookeeper
tanyongxi667 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
斯是 陋室9 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷10 小时前
C++:list
开发语言·c++
HHRL-yx10 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip