一、HttpRequest模块

二、HttpResponse模块

三、实现思想
(一)功能
- HttpRequest模块
存储HTTP请求信息
接收到一个数据,按照HTTP请求格式进行解析,得到各个关键要素放到Request中 - HttpResponse模块
存储HTTP响应信息
进行业务处理的同时,让使用者向Response中填充响应要素,完毕后,将其组织成HTTP响应格式的数据,发给客户端。
(二)意义
- HttpRequest模块
让HTTP请求的分析更加简单 - HttpResponse模块
让HTTP响应的过程变得简单
四、代码
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 {
bool ret = HasHeader("Content-Length");
if (ret == false) {
return 0;
}
std::string clen = GetHeader("Content-Length");
return std::stol(clen);
}
bool Close() const {
if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive") {
return false;
}
return true;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
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() {
if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive") {
return false;
}
return true;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56