Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : 5 : #include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.h" 6 : #include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.validate.h" 7 : #include "envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.pb.h" 8 : #include "envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.pb.validate.h" 9 : #include "envoy/router/path_matcher.h" 10 : #include "envoy/router/path_rewriter.h" 11 : 12 : #include "source/common/protobuf/message_validator_impl.h" 13 : #include "source/common/protobuf/utility.h" 14 : #include "source/extensions/path/uri_template_lib/uri_template.h" 15 : 16 : #include "absl/status/statusor.h" 17 : #include "absl/strings/string_view.h" 18 : 19 : namespace Envoy { 20 : namespace Extensions { 21 : namespace UriTemplate { 22 : namespace Rewrite { 23 : 24 : const absl::string_view NAME = "envoy.path.rewrite.uri_template.uri_template_rewriter"; 25 : 26 : /** 27 : * UriTemplateRewriter allows rewriting paths based on match pattern variables provided 28 : * in UriTemplateMatcher. 29 : * 30 : * Example: 31 : * UriTemplateMatcher = /foo/bar/{var} 32 : * UriTemplateRewriter = /foo/{var} 33 : * Will replace segment of path with value of {var} 34 : * e.g. /foo/bar/cat -> /foo/cat 35 : */ 36 : class UriTemplateRewriter : public Router::PathRewriter { 37 : public: 38 : explicit UriTemplateRewriter( 39 : const envoy::extensions::path::rewrite::uri_template::v3::UriTemplateRewriteConfig& 40 : rewrite_config) 41 44 : : rewrite_pattern_(rewrite_config.path_template_rewrite()) {} 42 : 43 : // Router::PathRewriter 44 0 : absl::string_view uriTemplate() const override { return rewrite_pattern_; } 45 : 46 : /** 47 : * Concatenates literals and extracts variable values to form the final rewritten path. 48 : * For example: 49 : * rewrite_pattern: [capture_index=2, literal="cat"] 50 : * path: "/bar/var" 51 : * capture_regex: "(1)/(2)" 52 : * Rewrite would result in rewrite of "/var/cat". 53 : */ 54 : absl::StatusOr<std::string> rewritePath(absl::string_view pattern, 55 : absl::string_view matched_path) const override; 56 : absl::Status isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher) const override; 57 0 : absl::string_view name() const override { return NAME; } 58 : 59 : private: 60 : std::string rewrite_pattern_; 61 : }; 62 : 63 : } // namespace Rewrite 64 : } // namespace UriTemplate 65 : } // namespace Extensions 66 : } // namespace Envoy