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

相关推荐
枫叶丹41 小时前
【在Linux世界中追寻伟大的One Piece】进程信号
linux·运维·服务器
zaim11 小时前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
学习使我变快乐1 小时前
C++:const成员
开发语言·c++
GodK7772 小时前
HTTPS 的加密流程
网络协议·http·https
灯火不休ᝰ2 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
一律清风4 小时前
QT-文件创建时间修改器
c++·qt
数云界4 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
风清扬_jd4 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
Death2005 小时前
Qt 6 相比 Qt 5 的主要提升与更新
开发语言·c++·qt·交互·数据可视化
叫我龙翔7 小时前
【Linux】进程间关系与守护进程
linux·运维·服务器·计算机网络