-
Notifications
You must be signed in to change notification settings - Fork 2
/
url-query_string.cpp
155 lines (125 loc) · 4.01 KB
/
url-query_string.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
Copyright 1999-2020 Red Anchor Trading Co. Ltd.
Distributed under the Boost Software License, Version 1.0.
See <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "fost-inet.hpp"
#include <fost/url.hpp>
#include <fost/string>
#include <fost/exception/not_null.hpp>
using namespace fostlib;
namespace {
template<typename C>
C digit(unsigned char dig) {
if (dig < 0x0a) return dig + '0';
if (dig < 0x10) return dig + 'A' - 0x0a;
throw fostlib::exceptions::out_of_range<int>{
"Number to convert to hex digit is too big", 0, 0x10, dig};
}
template<typename S>
S hex(unsigned char ch) {
std::string num;
num += '%';
num += digit<typename std::string::value_type>((ch & 0xf0) >> 4);
num += digit<typename std::string::value_type>(ch & 0x0f);
return S{std::move(num)};
}
/**
* Including the `=` sign here is a bit dodgy. It's OK to have it un-
* encoded in the value part, but clearly if it's not encoded in key
* then clearly things will go wrong. But if you put a `=` sign in
* the query string key names you're pretty much asking for a
* ton of trouble anyway, so....
*/
const fostlib::utf8_string g_query_string_allowed(
".,:/\\_-*~="
"0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
/*
fostlib::url::query_string
*/
fostlib::url::query_string::query_string() {}
fostlib::url::query_string::query_string(const ascii_printable_string &q)
: m_string(q) {}
void fostlib::url::query_string::append(
const string &name, const nullable<string> &value) {
m_string = null;
m_query[name].push_back(value);
}
void fostlib::url::query_string::remove(const string &name) {
const auto p(m_query.find(name));
if (p != m_query.end()) {
m_string = null;
m_query.erase(p);
}
}
std::size_t fostlib::url::query_string::has_key(const string &key) const {
const auto p(m_query.find(key));
if (p == m_query.end()) {
return 0;
} else {
return p->second.size();
}
}
nullable<string> fostlib::url::query_string::operator[](const string &k) const {
const auto p(m_query.find(k));
if (p == m_query.end() || p->second.size() == 0u) {
return null;
} else {
return *p->second.begin();
}
}
namespace {
const std::vector<nullable<string>> c_empty_list;
}
const std::vector<nullable<string>> &
fostlib::url::query_string::at(const string &key) const {
const auto p(m_query.find(key));
if (p == m_query.end()) {
return c_empty_list;
} else {
return p->second;
}
}
namespace {
ascii_printable_string query_string_encode(const string &s) {
std::string r;
auto const i(coerce<utf8_string>(s));
for (auto const c : i.memory()) {
if (g_query_string_allowed.underlying().find(c)
== std::string::npos) {
r += hex<std::string>(c);
} else {
r += c;
}
}
return ascii_printable_string{std::move(r)};
}
nullable<ascii_printable_string>
query_string_encode(const nullable<string> &s) {
if (not s) {
return null;
} else {
return query_string_encode(s.value());
}
}
}
const nullable<ascii_printable_string> &
fostlib::url::query_string::as_string() const {
if (not m_string) {
nullable<ascii_printable_string> r;
for (auto it(m_query.begin()); it != m_query.end(); ++it) {
for (auto v(it->second.begin()); v != it->second.end(); ++v) {
r =
concat(r, ascii_printable_string("&"),
concat(query_string_encode(it->first)
+ ascii_printable_string("="),
query_string_encode(*v)));
}
}
m_string = r;
}
return m_string;
}