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