blob: 0f39a4723d7444abf5e1c692b8d5e1faed7fb212 (
plain)
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
|
// Copyright 2013-2016 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef NETWORK_DETAIL_URI_PERCENT_ENCODE_INC
#define NETWORK_DETAIL_URI_PERCENT_ENCODE_INC
#include <string>
#include <vector>
#include <locale>
#include <network/optional.hpp>
#include <network/uri/detail/decode.hpp>
namespace network {
namespace detail {
inline optional<char> percent_encode(std::string::const_iterator it) {
try {
char output = '\0';
detail::decode_char(it, &output);
return output;
} catch (percent_decoding_error &) {
return optional<char>();
}
}
template <class String>
struct percent_encoded_to_upper {
percent_encoded_to_upper() : count(0) {}
void operator()(typename String::value_type &c) {
if (c == '%') {
count = 2;
} else if (count > 0) {
c = std::toupper(c, std::locale());
--count;
}
}
unsigned count;
};
template <class Iter>
Iter decode_encoded_unreserved_chars(Iter first, Iter last) {
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
// clang-format off
const auto is_unreserved = [](char c) {
return std::isalnum(c, std::locale())
|| '-' == c
|| '.' == c
|| '_' == c
|| '~' == c;
};
// clang-format on
auto it = first, it2 = first;
while (it != last) {
if (*it == '%') {
const auto sfirst = it;
const auto opt_char = percent_encode(sfirst);
if (opt_char && is_unreserved(*opt_char)) {
*it2 = *opt_char;
++it;
++it;
} else {
*it2 = *it;
}
} else {
*it2 = *it;
}
++it;
++it2;
}
return it2;
}
} // namespace detail
} // namespace network
#endif // NETWORK_DETAIL_URI_PERCENT_ENCODE_INC
|