summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/third_party/uri/src/detail/uri_normalize.cpp
blob: de8c4da610f74dc8dc828c1bbf9d7073aaf0fa34 (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
// 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 "uri_normalize.hpp"
#include "uri_percent_encode.hpp"
#include "algorithm.hpp"
#include "algorithm_split.hpp"
#include <iterator>
#include <vector>

using namespace network::algorithm;
using network::string_view;
using network::uri_comparison_level;
namespace network_detail = network::detail;

std::string network_detail::normalize_path_segments(string_view path) {
  std::string result;

  if (!path.empty()) {
    std::vector<std::string> path_segments;
    split(path_segments, path, '/');

    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 network_detail::normalize_path(string_view path,
                                           uri_comparison_level level) {
  auto result = path.to_string();

  if (uri_comparison_level::syntax_based == level) {
    // case normalization
    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;
}