1.15.C++项目:仿muduo库实现并发服务器之HttpRequest和HttpResponse模块的设计

文章目录

一、HttpRequest模块

二、HttpResponse模块

三、实现思想

(一)功能

  • HttpRequest模块
    存储HTTP请求信息
    接收到一个数据,按照HTTP请求格式进行解析,得到各个关键要素放到Request中
  • HttpResponse模块
    存储HTTP响应信息
    进行业务处理的同时,让使用者向Response中填充响应要素,完毕后,将其组织成HTTP响应格式的数据,发给客户端。

(二)意义

  • HttpRequest模块
    让HTTP请求的分析更加简单
  • HttpResponse模块
    让HTTP响应的过程变得简单

四、代码

  • HttpRequest模块
cpp 复制代码
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:
        HttpRequest():_version("HTTP/1.1") {}
        void ReSet() {
            _method.clear();
            _path.clear();
            _version = "HTTP/1.1";
            _body.clear();
            std::smatch match;
            _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) const {
            auto it = _headers.find(key);
            if (it == _headers.end()) {
                return false;
            }
            return true;
        }
        //获取指定头部字段的值
        std::string GetHeader(const std::string &key) const {
            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) const {
            auto it = _params.find(key);
            if (it == _params.end()) {
                return false;
            }
            return true;
        }
        //获取指定的查询字符串
        std::string GetParam(const std::string &key) const {
            auto it = _params.find(key);
            if (it == _params.end()) {
                return "";
            }
            return it->second;
        }
        //获取正文长度
        size_t ContentLength() const {
            // 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() const {
            // 没有Connection字段,或者有Connection但是值是close,则都是短链接,否则就是长连接
            if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive") {
                return false;
            }
            return true;
        }
};
  • HttpResponse模块
cpp 复制代码
class HttpResponse {
    public:
        int _statu;
        bool _redirect_flag;
        std::string _body;
        std::string _redirect_url;
        std::unordered_map<std::string, std::string> _headers;
    public:
        HttpResponse():_redirect_flag(false), _statu(200) {}
        HttpResponse(int statu):_redirect_flag(false), _statu(statu) {} 
        void ReSet() {
            _statu = 200;
            _redirect_flag = false;
            _body.clear();
            _redirect_url.clear();
            _headers.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 SetContent(const std::string &body,  const std::string &type = "text/html") {
            _body = body;
            SetHeader("Content-Type", type);
        }
        void SetRedirect(const std::string &url, int statu = 302) {
            _statu = statu;
            _redirect_flag = true;
            _redirect_url = url;
        }
        //判断是否是短链接
        bool Close() {
            // 没有Connection字段,或者有Connection但是值是close,则都是短链接,否则就是长连接
            if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive") {
                return false;
            }
            return true;
        }
};
相关推荐
郝学胜-神的一滴几秒前
QStyleOption:Qt样式系统之魂
开发语言·c++·qt·程序人生
木卫二号Coding1 分钟前
第七十二篇-V100-32G+WebUI+Flux.1-Schnell+Lora+文生图
开发语言·人工智能·python
恒者走天下1 分钟前
操作系统知识资料及对应简历书写分享
c++
墨笔之风2 分钟前
基于python 实现的小游戏
开发语言·python·pygame
脏脏a2 分钟前
【Linux】进程等待:wait/waitpid 与僵尸进程治理
linux·运维·服务器·wait·进程等待·waitpid
墨风如雪2 分钟前
Netcup VPS 过户全攻略:手把手教你如何安全地将服务器 Push 给新用户
服务器
予枫的编程笔记2 分钟前
Elasticsearch 全面解析:从原理到实战的分布式搜索引擎指南
java·开发语言·分布式·后端·elasticsearch·搜索引擎·全文检索
研☆香2 分钟前
html界面面包屑功能的介绍与制作
服务器·前端·html
REDcker2 分钟前
iperf3 服务器测速服务搭建指南
运维·服务器·后端·后端开发·iperf·iperf3·测速
枫叶丹43 分钟前
【Qt开发】Qt系统(四)-> Qt文件
c语言·开发语言·c++·qt