-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathquery_string.cpp
More file actions
48 lines (38 loc) · 1.44 KB
/
Copy pathquery_string.cpp
File metadata and controls
48 lines (38 loc) · 1.44 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
#include "query_string.h"
#include <algorithm>
void rs::httpserver::QueryString::Parse() const {
if (lookup_.size() == 0 && data_.size() > 0) {
auto start = data_.cbegin();
auto end = data_.cend();
for (auto pairStart = start, pairEnd = std::find(pairStart, end, '&'); pairStart < end; pairEnd = std::find(pairStart, end, '&')) {
auto keyEnd = std::find(pairStart, pairEnd, '=');
if (keyEnd != pairEnd) {
std::string key{pairStart, keyEnd};
std::string value{keyEnd + 1, pairEnd};
lookup_.emplace(std::move(key), std::move(value));
} else {
std::string key{pairStart, pairEnd};
lookup_.emplace(std::move(key), emptyValue_);
}
pairStart = *pairEnd ? (pairEnd + 1) : pairEnd;
}
}
}
bool rs::httpserver::QueryString::IsKey(const std::string& key) const {
Parse();
auto pair = lookup_.find(key);
return pair != lookup_.cend();
}
const std::string& rs::httpserver::QueryString::getValue(const std::string& key) const {
Parse();
auto pair = lookup_.find(key);
return pair != lookup_.cend() ? pair->second : emptyValue_;
}
std::vector<std::string> rs::httpserver::QueryString::getKeys() const {
Parse();
std::vector<std::string> keys;
for (auto i : lookup_) {
keys.emplace_back(i.first);
}
return keys;
}