-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRequest.cpp
More file actions
75 lines (66 loc) · 1.64 KB
/
Request.cpp
File metadata and controls
75 lines (66 loc) · 1.64 KB
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
//
// Created by Alone on 2022-7-21.
//
#include <sstream>
#include "Request.h"
#include "Response.h"
using namespace http;
METHOD Request::get_method_from_text(string_view text)
{
if (text == "GET")
return GET;
if (text == "POST")
return POST;
return static_cast<METHOD>(0);
}
void Request::init_special_fields()
{
auto iter = m_head.find("Content-Length");
if (iter != m_head.end())
{
m_contentLen = stoi(iter->second);
}
iter = m_head.find("Host");
if (iter != m_head.end())
{
m_host = iter->second;
}
iter = m_head.find("Connection");
if (iter != m_head.end())
{
m_keep_alive = iter->second == "keep-alive";
}
iter = m_head.find("Content-type");
if (iter != m_head.end())
{
m_contentType = Response::get_type_from_text(iter->second);
}
//初始化url数据
m_urlData = Url::FromData(m_url);
//初始化post数据
m_postForm = PostForm::FromData(m_body, (ACCEPT_CONTENT_TYPE) m_contentType);
}
string Request::to_string()
{
auto &req = *this;
std::stringstream ss;
int method = req.method();
if (method != GET && method != POST)
throw std::logic_error("missing method parameter");
ss << (method == GET ? "GET" : "POST") << ' ';
ss << req.url() << ' ';
ss << "HTTP/1.1"
<< "\r\n";
//特殊head字段填充
if (req.content_length() != 0)
{
req.head()["Content-Length"] = std::to_string(req.content_length());
}
for (auto &&[k, v]: req.head())
{
ss << k << ':' << v << "\r\n";
}
ss << "\r\n";
ss << req.body();
return ss.str();
}