-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrequest_headers.cpp
More file actions
173 lines (136 loc) · 5.94 KB
/
Copy pathrequest_headers.cpp
File metadata and controls
173 lines (136 loc) · 5.94 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
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "request_headers.h"
#include <limits>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <cctype>
const char rs::httpserver::RequestHeaders::endOfLine_[4] = { '\r', '\n', '\r', '\n' };
const std::string rs::httpserver::RequestHeaders::emptyValue_;
const rs::httpserver::RequestHeaders::range_index rs::httpserver::RequestHeaders::RANGE_END =
std::numeric_limits<rs::httpserver::RequestHeaders::range_index>::max();
rs::httpserver::request_headers_ptr rs::httpserver::RequestHeaders::Create(HeaderBuffer& buffer) {
auto headersEnd = std::search(buffer.cbegin(), buffer.cend(), endOfLine_, endOfLine_ + sizeof(endOfLine_));
if (headersEnd != buffer.cend()) {
auto headers = boost::make_shared<RequestHeaders>();
auto headersLength = headersEnd - buffer.cbegin() + sizeof(endOfLine_);
headers->GetHeaders(buffer, headersLength);
buffer.setPosition(headersLength);
return headers;
} else {
return nullptr;
}
}
void rs::httpserver::RequestHeaders::GetHeaders(const HeaderBuffer& buffer, int headersLength) {
auto headersStart = buffer.cbegin();
auto headersEnd = headersStart + headersLength;
auto methodStart = headersStart;
auto methodEnd = std::find(headersStart, headersEnd, ' ');
if (methodEnd == headersEnd) {
throw HeaderMalformedException("Missing method");
}
method_.assign(methodStart, methodEnd);
while (*++methodEnd == ' ');
auto uriStart = methodEnd;
auto uriEnd = std::find(uriStart, headersEnd, ' ');
if (methodEnd == headersEnd) {
throw HeaderMalformedException("Missing URI");
}
rawUri_.assign(uriStart, uriEnd);
while (*++uriEnd == ' ');
auto versionStart = uriEnd;
auto versionEnd = std::find(versionStart, headersEnd, '\r');
if (versionEnd == headersEnd || versionEnd == versionStart) {
throw HeaderMalformedException("Missing version");
}
auto lineStart = versionEnd + 2;
while (*(versionEnd - 1) == ' ') { --versionEnd; }
version_.assign(versionStart, versionEnd);
if (version_.find("HTTP/") != 0 || version_.length() != 8 || !std::isdigit(version_[5]) || version_[6] != '.' || !std::isdigit(version_[7])) {
throw HeaderMalformedException("Malformed version");
}
while (lineStart < headersEnd && *lineStart != '\r') {
auto nameStart = lineStart;
auto nameEnd = std::find(nameStart, headersEnd, ':');
if (nameEnd == headersEnd) {
throw HeaderMalformedException("Bad header key");
}
auto valueStart = nameEnd + 1;
while (*valueStart == ' ') {
++valueStart;
}
auto valueEnd = std::find(valueStart, headersEnd, '\r');
if (valueEnd == headersEnd) {
throw HeaderMalformedException("Bad header value");
}
lineStart = valueEnd + 2;
while (*(nameEnd - 1) == ' ') {
--nameEnd;
}
while (*(valueEnd - 1) == ' ') {
--valueEnd;
}
std::string name(nameStart, nameEnd);
std::string value(valueStart, valueEnd);
headers_.emplace(std::move(name), std::move(value));
}
auto queryStringStart = std::find(rawUri_.cbegin(), rawUri_.cend(), '?');
if (queryStringStart != rawUri_.cend()) {
uri_ = std::string(rawUri_.cbegin(), queryStringStart);
queryString_ = UriDecode(std::string(queryStringStart + 1, rawUri_.cend()));
} else {
uri_ = rawUri_;
queryString_ = emptyValue_;
}
}
std::string rs::httpserver::RequestHeaders::UriDecode(const std::string& data) {
if (data.find('%') != std::string::npos) {
auto size = data.size();
std::string decodedData;
decodedData.reserve(size);
for (decltype(size) i = 0; i < size; ++i) {
auto ch = data[i];
if (ch == '%' && (i + 2) < size && std::isxdigit(data[i + 1]) && std::isxdigit(data[i + 2])) {
unsigned code = GetDigitValue(data[++i]) << 4;
code |= GetDigitValue(data[++i]);
decodedData.push_back((char)code);
} else {
decodedData.push_back(ch);
}
}
return decodedData;
} else {
return data;
}
}
unsigned rs::httpserver::RequestHeaders::GetDigitValue(char ch) {
if (ch >= 0x30 && ch <= 0x39) {
return ch - 0x30;
} else {
return (ch | 0x20) - 0x61 + 10;
}
}
rs::httpserver::RequestHeaders::byte_range_collection rs::httpserver::RequestHeaders::getByteRanges() {
byte_range_collection ranges;
auto range = getRange();
if (range.size() > 0 && boost::algorithm::istarts_with(range, "bytes=")) {
range = std::string{range.cbegin() + 6, range.cend()};
std::vector<std::string> splitRanges;
boost::split(splitRanges, range, boost::is_any_of(","), boost::token_compress_off);
try {
for (auto splitRange : splitRanges) {
auto splitIndex = splitRange.find('-');
if (splitIndex == 0 || splitIndex == std::string::npos) {
auto start = boost::lexical_cast<range_index>(splitRange);
ranges.emplace_back(start, RANGE_END);
} else {
auto str = splitRange.c_str();
auto start = boost::lexical_cast<byte_range_collection::value_type::first_type>(str, splitIndex);
auto end = splitIndex < (splitRange.size() - 1) ? boost::lexical_cast<range_index>(str + splitIndex + 1) : RANGE_END;
ranges.emplace_back(start, end);
}
}
} catch (const boost::bad_lexical_cast&) {
ranges.clear();
}
}
return ranges;
}