-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_request.h
More file actions
81 lines (66 loc) · 2.24 KB
/
http_request.h
File metadata and controls
81 lines (66 loc) · 2.24 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
76
77
78
79
80
81
#pragma once
#include <netpoll/util/string_view.h>
#include <string>
#include <unordered_map>
#include "types.h"
namespace http {
class Request
{
public:
static Method MethodMapping(netpoll::StringView const& method)
{
#define METHOD_CODE(m) \
if (method == #m) return Method::k##m;
METHOD_CODE(GET)
METHOD_CODE(POST)
METHOD_CODE(HEAD)
METHOD_CODE(PUT)
METHOD_CODE(DELETE)
#undef METHOD_CODE
return Method::kInvalid;
}
Request() : method_(Method::kInvalid), version_(Version::kUnknown) {}
void addHeader(netpoll::StringView const& key,
netpoll::StringView const& value)
{
// Remove Spaces at both ends
int i = 0;
while (i < value.size() && value[i] == ' ') ++i;
if (i == value.size())
{
headers_[{key.data(), key.size()}] = "";
return;
}
int k = static_cast<int>(value.size()) - 1;
while (k >= 0 && value[k] == ' ') --k;
if (k == -1)
{
headers_[{key.data(), key.size()}] = "";
return;
}
headers_[{key.data(), key.size()}] = {value.data() + i,
value.data() + k + 1};
}
auto headers() -> std::unordered_map<std::string, std::string>&
{
return headers_;
}
auto method() -> Method& { return method_; }
auto method() const -> const Method& { return method_; }
auto version() -> Version& { return version_; }
auto version() const -> const Version& { return version_; }
auto body() -> std::string& { return body_; }
auto body() const -> const std::string& { return body_; }
auto path() -> std::string& { return path_; }
auto path() const -> const std::string& { return path_; }
auto query() -> std::string& { return query_; }
auto query() const -> const std::string& { return query_; }
private:
Method method_;
Version version_;
std::string path_;
std::string query_;
std::string body_;
std::unordered_map<std::string, std::string> headers_;
};
} // namespace http