-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.cc
45 lines (37 loc) · 1018 Bytes
/
header.cc
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
#include "header.h"
namespace Base {
Header::Header() {}
Header::~Header() {}
Header Header::parse(std::string str) {
auto result = std::remove_if(str.begin(), str.end(),
[](int ch) {return ch == '\x0d';}
);
str.erase(result, str.end());
return parse(std::istringstream(str));
}
Header Header::parse(std::istringstream iss) {
Header tmp;
std::string header;
while (std::getline(iss, header, '\x0a')) {
decltype(header)::size_type pos = header.find(':');
if (pos == decltype(header)::npos) {
continue;
}
std::string key = header.substr(0, pos);
std::string value = header.substr(pos + 2);
tmp.set(key, value);
}
return tmp;
}
std::optional<std::string> Header::get(std::string key) const {
try {
return std::make_optional<std::string>(map_.at(key));
}
catch (std::exception e) {
return std::nullopt;
}
}
void Header::remove(std::string key) {
map_.erase(key);
}
}