blob: 1c6ca1484b266cc66ff360470726896adf6cbbae (
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
81
82
83
84
85
86
87
|
// 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)
#include <iterator>
#include <vector>
#include <algorithm>
#ifdef NETWORK_URI_EXTERNAL_BOOST
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
namespace boost = boost;
#else // NETWORK_URI_EXTERNAL_BOOST
#include "boost/algorithm/string/split.hpp"
#include "boost/algorithm/string/join.hpp"
#endif // NETWORK_URI_EXTERNAL_BOOST
#include "uri_normalize.hpp"
#include "uri_percent_encode.hpp"
#include "algorithm.hpp"
namespace network {
namespace detail {
std::string normalize_path_segments(string_view path) {
std::string result;
if (!path.empty()) {
std::vector<std::string> path_segments;
boost::split(path_segments, path, [](char ch) {
return ch == '/';
});
bool last_segment_is_slash = path_segments.back().empty();
std::vector<std::string> normalized_segments;
for (const auto &segment : path_segments) {
if (segment.empty() || (segment == ".")) {
continue;
}
else if (segment == "..") {
if (normalized_segments.empty()) {
throw uri_builder_error();
}
normalized_segments.pop_back();
}
else {
normalized_segments.push_back(segment);
}
}
for (const auto &segment : normalized_segments) {
result += "/" + segment;
}
if (last_segment_is_slash) {
result += "/";
}
}
if (result.empty()) {
result = "/";
}
return result;
}
std::string normalize_path(string_view path, uri_comparison_level level) {
auto result = path.to_string();
if (uri_comparison_level::syntax_based == level) {
// case normalization
detail::for_each(result, percent_encoded_to_upper<std::string>());
// % encoding normalization
result.erase(
detail::decode_encoded_unreserved_chars(std::begin(result),
std::end(result)),
std::end(result));
// % path segment normalization
result = normalize_path_segments(result);
}
return result;
}
} // namespace detail
} // namespace network
|