-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRequest.cpp
134 lines (113 loc) · 3.28 KB
/
Request.cpp
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
#include "Request.hpp"
#include <sys/_types/_size_t.h>
#include <exception>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "parsing/tokengen.hpp"
#include "tools.hpp"
HttpRequest::HttpRequest(const std::string& request)
: _raw(request),
_method(),
_location(),
_version(),
_headers(),
_body(),
_error(false) {
if (request.empty() || request[0] == '\0') {
_error = true;
return;
}
std::vector<std::string> header_and_body =
tools::split_(request, "\r\n\r\n");
if (header_and_body.size() == 0) {
_error = true;
return;
}
std::string& headers = header_and_body[0];
std::vector<std::string> splited = split(headers, "\r\n");
if (splited.size() == 0) {
_error = true;
return;
}
std::vector<std::string> first_line = split(splited[0], " ");
if (first_line.size() != 3) {
_error = true;
return;
}
_method = first_line[0];
_location = first_line[1];
_version = first_line[2];
for (size_t i = 1; i < splited.size(); ++i) {
std::vector<std::string> tmp = split(splited[i], ":");
std::string key = toUppercase(trim(tmp[0], "\n\r "));
std::string value;
if (tmp.size() > 2) {
for (size_t j = 1; j < tmp.size(); ++j)
value += tmp[j] + ":";
value = trim(value, "\n\r :");
} else {
value = trim(tmp[1], "\n\r ");
}
_headers.insert(make_pair(key, value));
}
if (getHeaderValue("Host").empty() ||
getHeaderValue("User-Agent").empty()) {
_error = true;
return;
}
if (getMethod() == "POST") {
if (getHeaderValue("Content-Type").empty() ||
getHeaderValue("Content-Length").empty()) {
_error = true;
return;
}
try {
std::stoull(getHeaderValue("Content-Length"));
} catch (std::exception& e) {
_error = true;
return;
}
}
for (size_t i = 1; i < header_and_body.size(); i++)
_body += header_and_body[i];
}
void HttpRequest::dump() const {
std::cout << _method << " " << _location << " " << _version << "\n";
for (const_multi_iter iter = _headers.begin(); iter != _headers.end();
iter++) {
std::cout << "[" << iter->first << "]"
<< " : [" << iter->second << "]" << '\n';
}
}
bool HttpRequest::error() const {
return _error;
}
const std::string& HttpRequest::getRawData() const {
return _raw;
}
const std::string& HttpRequest::getBody() const {
return _body;
}
std::string HttpRequest::getHeaderValue(const std::string& key) const {
const_multi_iter iter = _headers.find(toUppercase(key));
if (iter == _headers.end())
return "";
return std::string(iter->second);
}
std::pair<const_multi_iter, const_multi_iter> HttpRequest::getHeaderValues(
const std::string& key) const {
return _headers.equal_range(tools::toUppercase(key));
}
const std::string& HttpRequest::getVersion() const {
return _version;
}
const std::string& HttpRequest::getLocation() const {
return _location;
}
const std::string& HttpRequest::getMethod() const {
return _method;
}