/proc/self/cwd/source/extensions/path/uri_template_lib/uri_template.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include <string> |
4 | | |
5 | | #include "source/extensions/path/uri_template_lib/uri_template_internal.h" |
6 | | |
7 | | #include "absl/status/statusor.h" |
8 | | #include "absl/strings/string_view.h" |
9 | | |
10 | | namespace Envoy { |
11 | | namespace Extensions { |
12 | | namespace UriTemplate { |
13 | | |
14 | | // This library provides functionality to rewrite paths based on templates |
15 | | // (https://www.rfc-editor.org/rfc/rfc6570.html). Paths are matches against "match patterns" which |
16 | | // capture matched tokens. For example: The match pattern "/foo/{bar}" will match "/foo/cat" and |
17 | | // will capture "cat" in the variable "bar". |
18 | | // |
19 | | // A matched path can then be rewritten with a rewrite pattern. |
20 | | // For example: rewrite pattern "/pat/hat/{bar}" would use the captured variable "bar" from above |
21 | | // to rewrite "/foo/cat" into "/pat/hat/cat" |
22 | | |
23 | | enum class RewriteStringKind { Variable, Literal }; |
24 | | |
25 | | struct ParsedSegment { |
26 | 610k | ParsedSegment(absl::string_view value, RewriteStringKind kind) : value_(value), kind_(kind) {} |
27 | | absl::string_view value_; |
28 | | RewriteStringKind kind_; |
29 | | }; |
30 | | |
31 | | // Stores string literals and regex capture indexes for rewriting paths |
32 | | using RewriteSegment = absl::variant<int, std::string>; |
33 | | |
34 | | // Stores all segments in left to right order for a path rewrite |
35 | | using RewriteSegments = std::vector<RewriteSegment>; |
36 | | |
37 | | /** |
38 | | * Returns the safe regex that Envoy understands that is equivalent to the given pattern. |
39 | | */ |
40 | | absl::StatusOr<std::string> convertPathPatternSyntaxToRegex(absl::string_view path_pattern); |
41 | | |
42 | | /** |
43 | | * Parses the specified pattern into a sequence of segments (which are |
44 | | * either literals or variable names). |
45 | | **/ |
46 | | absl::StatusOr<std::vector<ParsedSegment>> parseRewritePattern(absl::string_view path_pattern); |
47 | | |
48 | | /** |
49 | | * Returns the parsed path rewrite pattern and processes variables. |
50 | | */ |
51 | | absl::StatusOr<RewriteSegments> parseRewritePattern(absl::string_view pattern, |
52 | | absl::string_view capture_regex); |
53 | | |
54 | | /** |
55 | | * Returns true if provided rewrite pattern is valid. |
56 | | */ |
57 | | absl::Status isValidRewritePattern(absl::string_view path_template_rewrite); |
58 | | |
59 | | /** |
60 | | * Returns true if path_template_rewrite and capture_regex have valid variables. |
61 | | * Every variable in rewrite MUST be present in match. |
62 | | * For example: |
63 | | * Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid. |
64 | | * Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. |
65 | | */ |
66 | | absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex); |
67 | | |
68 | | /** |
69 | | * Returns true if the match_pattern is valid. |
70 | | * Validation attempts to parse pattern into literals and variables. |
71 | | */ |
72 | | absl::Status isValidMatchPattern(absl::string_view match_pattern); |
73 | | |
74 | | } // namespace UriTemplate |
75 | | } // namespace Extensions |
76 | | } // namespace Envoy |