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

相关推荐
Lo-Y-eH41 分钟前
HTTP 四种常见方法
网络·网络协议·http
CodeWithMe1 小时前
【读书笔记】《C++ Software Design》第三章 The Purpose of Design Patterns
c++·设计模式
Kevin Wang7271 小时前
Ubuntu服务器安装Miniconda
linux·服务器·ubuntu
Thymme2 小时前
C++获取时间和格式化时间
c++
真的想上岸啊2 小时前
学习C++、QT---25(QT中实现QCombobox库的介绍和用QCombobox设置编码和使用编码的讲解)
c++·qt·学习
药9552 小时前
网络编程(TCP连接)
运维·服务器
刃神太酷啦2 小时前
C++ 多态详解:从概念到实现原理----《Hello C++ Wrold!》(14)--(C/C++)
java·c语言·c++·qt·算法·leetcode·面试
山河木马2 小时前
前端学C++可太简单了:引用
前端·javascript·c++
wjm0410063 小时前
C++后端面试八股文
java·c++·面试
电信2301杨臣3 小时前
Imx6ull用网线与电脑连接
运维·服务器·网络