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

相关推荐
Two_brushes.4 小时前
【Linux】线程机制深度实践:创建、等待、互斥与同步
linux·运维·服务器·多线程
m0_535064606 小时前
C++模版编程:类模版与继承
java·jvm·c++
Tanecious.8 小时前
C++--红黑树封装实现set和map
网络·c++
yqcoder9 小时前
7. TCP 和 UDP 的区别
网络·网络协议·http
wanhengidc9 小时前
UDP服务器的优缺点都包含哪些?
服务器·网络协议·udp
愚润求学10 小时前
【动态规划】01背包问题
c++·算法·leetcode·动态规划
小猫会后空翻10 小时前
HTTPS与DNS
网络协议·http·https
诗人不说梦^11 小时前
[BUUCTF 2018]Online Tool
linux·运维·服务器
源代码•宸11 小时前
C++高频知识点(十三)
开发语言·c++·经验分享·面经
wa的一声哭了11 小时前
python基础知识pip配置pip.conf文件
java·服务器·开发语言·python·pip·risc-v·os