-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathhttp_request.h
More file actions
160 lines (134 loc) · 3.03 KB
/
http_request.h
File metadata and controls
160 lines (134 loc) · 3.03 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef TEST_HTTP_HTTP_REQUEST_HEADER
#define TEST_HTTP_HTTP_REQUEST_HEADER
#include <cstdint>
#include <string>
#include <stdio.h>
#include <assert.h>
#include <unordered_map>
enum Method {
Invalid,
Get,
Post,
Head,
Put,
Delete
};
enum Version {
Unknown,
Http10,
Http11
};
class HttpRequest {
public:
HttpRequest() : _method(Invalid), _version(Unknown), _path(), _query(), _receive_time(0), _headers_map() { }
~HttpRequest() {
_headers_map.clear();
}
void SetVersion(Version v) {
_version = v;
}
Version GetVersion() const {
return _version;
}
bool SetMethod(const char* start, const char* end) {
assert(_method == Invalid);
std::string temp_method(start, end);
if (temp_method == "GET") {
_method = Get;
} else if (temp_method == "POST") {
_method = Post;
} else if (temp_method == "HEAD") {
_method = Head;
} else if (temp_method == "PUT") {
_method = Put;
} else if (temp_method == "DELETE") {
_method = Delete;
} else {
_method = Invalid;
}
return _method != Invalid;
}
Method GetMethod() const {
return _method;
}
const char* GetMethodString() const {
const char* result = "UNKNOWN";
switch(_method)
{
case Get:
result = "GET";
break;
case Post:
result = "POST";
break;
case Head:
result = "HEAD";
break;
case Put:
result = "PUT";
break;
case Delete:
result = "DELETE";
break;
default:
break;
}
return result;
}
void SetPath(const char* start, const char* end) {
_path.assign(start, end);
}
const std::string& GetPath() const {
return _path;
}
void SetQuery(const char* start, const char* end) {
_query.assign(start, end);
}
const std::string& GetQuery() const {
return _query;
}
void SetReceiveTime(uint64_t t) {
_receive_time = t;
}
uint64_t GetReceiveTime() const {
return _receive_time;
}
void AddHeader(const char* start, const char* colon, const char* end) {
std::string field(start, colon);
++colon;
while (colon < end && isspace(*colon)) {
++colon;
}
std::string value(colon, end);
while (!value.empty() && isspace(value[value.size()-1])) {
value.resize(value.size()-1);
}
_headers_map[field] = value;
}
std::string GetHeader(const std::string& field) const {
std::string result;
auto it = _headers_map.find(field);
if (it != _headers_map.end()) {
result = it->second;
}
return result;
}
const std::unordered_map<std::string, std::string>& GetHeaders() const {
return _headers_map;
}
void Clear() {
_method = Invalid;
_version = Unknown;
_headers_map.clear();
_path.clear();
_query.clear();
}
private:
Method _method;
Version _version;
std::string _path;
std::string _query;
uint64_t _receive_time;
std::unordered_map<std::string, std::string> _headers_map;
};
#endif