Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/router/config_utility.cc
Line
Count
Source (jump to first uncovered line)
1
#include "source/common/router/config_utility.h"
2
3
#include <string>
4
#include <vector>
5
6
#include "envoy/config/core/v3/base.pb.h"
7
#include "envoy/config/route/v3/route_components.pb.h"
8
#include "envoy/type/matcher/v3/string.pb.h"
9
10
#include "source/common/common/assert.h"
11
#include "source/common/common/regex.h"
12
#include "source/common/config/datasource.h"
13
14
namespace Envoy {
15
namespace Router {
16
namespace {
17
18
absl::optional<Matchers::StringMatcherImpl<envoy::type::matcher::v3::StringMatcher>>
19
maybeCreateStringMatcher(const envoy::config::route::v3::QueryParameterMatcher& config,
20
34.3k
                         Server::Configuration::CommonFactoryContext& context) {
21
34.3k
  switch (config.query_parameter_match_specifier_case()) {
22
3.51k
  case envoy::config::route::v3::QueryParameterMatcher::QueryParameterMatchSpecifierCase::
23
3.51k
      kStringMatch:
24
3.51k
    return Matchers::StringMatcherImpl(config.string_match(), context);
25
5.13k
  case envoy::config::route::v3::QueryParameterMatcher::QueryParameterMatchSpecifierCase::
26
5.13k
      kPresentMatch:
27
5.13k
    return absl::nullopt;
28
25.6k
  case envoy::config::route::v3::QueryParameterMatcher::QueryParameterMatchSpecifierCase::
29
25.6k
      QUERY_PARAMETER_MATCH_SPECIFIER_NOT_SET:
30
25.6k
    return absl::nullopt;
31
34.3k
  }
32
33
0
  return absl::nullopt;
34
34.3k
}
35
36
} // namespace
37
38
ConfigUtility::QueryParameterMatcher::QueryParameterMatcher(
39
    const envoy::config::route::v3::QueryParameterMatcher& config,
40
    Server::Configuration::CommonFactoryContext& context)
41
34.3k
    : name_(config.name()), matcher_(maybeCreateStringMatcher(config, context)) {}
42
43
bool ConfigUtility::QueryParameterMatcher::matches(
44
6.42k
    const Http::Utility::QueryParamsMulti& request_query_params) const {
45
  // This preserves the legacy behavior of ignoring all but the first value for a given key
46
6.42k
  auto data = request_query_params.getFirstValue(name_);
47
6.42k
  if (!data.has_value()) {
48
4.21k
    return false;
49
4.21k
  }
50
51
2.21k
  if (!matcher_.has_value()) {
52
    // Present check
53
1.98k
    return true;
54
1.98k
  }
55
56
229
  return matcher_.value().match(data.value());
57
2.21k
}
58
59
Upstream::ResourcePriority
60
117k
ConfigUtility::parsePriority(const envoy::config::core::v3::RoutingPriority& priority) {
61
117k
  switch (priority) {
62
0
    PANIC_ON_PROTO_ENUM_SENTINEL_VALUES;
63
109k
  case envoy::config::core::v3::DEFAULT:
64
109k
    return Upstream::ResourcePriority::Default;
65
8.63k
  case envoy::config::core::v3::HIGH:
66
8.63k
    return Upstream::ResourcePriority::High;
67
117k
  }
68
0
  PANIC_DUE_TO_CORRUPT_ENUM;
69
0
}
70
71
bool ConfigUtility::matchQueryParams(
72
    const Http::Utility::QueryParamsMulti& query_params,
73
5.29k
    const std::vector<QueryParameterMatcherPtr>& config_query_params) {
74
6.42k
  for (const auto& config_query_param : config_query_params) {
75
6.42k
    if (!config_query_param->matches(query_params)) {
76
4.31k
      return false;
77
4.31k
    }
78
6.42k
  }
79
80
986
  return true;
81
5.29k
}
82
83
Http::Code ConfigUtility::parseRedirectResponseCode(
84
59.7k
    const envoy::config::route::v3::RedirectAction::RedirectResponseCode& code) {
85
59.7k
  switch (code) {
86
0
    PANIC_ON_PROTO_ENUM_SENTINEL_VALUES;
87
51.4k
  case envoy::config::route::v3::RedirectAction::MOVED_PERMANENTLY:
88
51.4k
    return Http::Code::MovedPermanently;
89
2.61k
  case envoy::config::route::v3::RedirectAction::FOUND:
90
2.61k
    return Http::Code::Found;
91
1.48k
  case envoy::config::route::v3::RedirectAction::SEE_OTHER:
92
1.48k
    return Http::Code::SeeOther;
93
1.72k
  case envoy::config::route::v3::RedirectAction::TEMPORARY_REDIRECT:
94
1.72k
    return Http::Code::TemporaryRedirect;
95
2.52k
  case envoy::config::route::v3::RedirectAction::PERMANENT_REDIRECT:
96
2.52k
    return Http::Code::PermanentRedirect;
97
59.7k
  }
98
0
  PANIC_DUE_TO_CORRUPT_ENUM;
99
0
}
100
101
absl::optional<Http::Code>
102
117k
ConfigUtility::parseDirectResponseCode(const envoy::config::route::v3::Route& route) {
103
117k
  if (route.has_redirect()) {
104
59.7k
    return parseRedirectResponseCode(route.redirect().response_code());
105
59.7k
  } else if (route.has_direct_response()) {
106
8.68k
    return static_cast<Http::Code>(route.direct_response().status());
107
8.68k
  }
108
49.3k
  return {};
109
117k
}
110
111
Http::Code ConfigUtility::parseClusterNotFoundResponseCode(
112
117k
    const envoy::config::route::v3::RouteAction::ClusterNotFoundResponseCode& code) {
113
117k
  switch (code) {
114
0
    PANIC_ON_PROTO_ENUM_SENTINEL_VALUES;
115
112k
  case envoy::config::route::v3::RouteAction::SERVICE_UNAVAILABLE:
116
112k
    return Http::Code::ServiceUnavailable;
117
4.11k
  case envoy::config::route::v3::RouteAction::NOT_FOUND:
118
4.11k
    return Http::Code::NotFound;
119
1.64k
  case envoy::config::route::v3::RouteAction::INTERNAL_SERVER_ERROR:
120
1.64k
    return Http::Code::InternalServerError;
121
117k
  }
122
0
  PANIC_DUE_TO_CORRUPT_ENUM;
123
0
}
124
125
} // namespace Router
126
} // namespace Envoy