-
Notifications
You must be signed in to change notification settings - Fork 94
Add FilterConfigurationBase #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0a3fc86
c3ef6c0
c003ec9
562a1d9
b894e96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #include "server/http_filter_config_base.h" | ||
|
|
||
| namespace Nighthawk { | ||
| namespace Server { | ||
|
|
||
| FilterConfigurationBase::FilterConfigurationBase(nighthawk::server::ResponseOptions proto_config, | ||
| absl::string_view filter_name) | ||
| : filter_name_(filter_name), | ||
| server_config_(std::make_shared<nighthawk::server::ResponseOptions>(std::move(proto_config))), | ||
| effective_config_(server_config_) {} | ||
|
|
||
| void FilterConfigurationBase::computeEffectiveConfiguration( | ||
| const Envoy::Http::RequestHeaderMap& headers) { | ||
| const auto* request_config_header = headers.get(TestServer::HeaderNames::get().TestServerConfig); | ||
| if (request_config_header) { | ||
| nighthawk::server::ResponseOptions response_options = *server_config_; | ||
| std::string error_message; | ||
| if (Configuration::mergeJsonConfig(request_config_header->value().getStringView(), | ||
| response_options, error_message)) { | ||
| effective_config_ = | ||
| std::make_shared<const nighthawk::server::ResponseOptions>(std::move(response_options)); | ||
| } else { | ||
| effective_config_ = absl::InvalidArgumentError(error_message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool FilterConfigurationBase::maybeSendErrorReply( | ||
| Envoy::Http::StreamDecoderFilterCallbacks& decoder_callbacks) const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be const? Or is sendLocalReply a non-const function?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sendLocalReply is a non-const function, so we can't const this. |
||
| if (!effective_config_.ok()) { | ||
| decoder_callbacks.sendLocalReply(static_cast<Envoy::Http::Code>(500), | ||
| fmt::format("{} didn't understand the request: {}", | ||
| filter_name_, | ||
| effective_config_.status().message()), | ||
| nullptr, absl::nullopt, ""); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } // namespace Server | ||
| } // namespace Nighthawk | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "envoy/server/filter_config.h" | ||
|
|
||
| #include "external/envoy/source/common/common/statusor.h" | ||
|
|
||
| #include "api/server/response_options.pb.h" | ||
|
|
||
| #include "server/configuration.h" | ||
| #include "server/well_known_headers.h" | ||
|
|
||
| #include "absl/status/status.h" | ||
|
|
||
| namespace Nighthawk { | ||
| namespace Server { | ||
|
|
||
| /** | ||
| * Shorthand and canonical representation of the effective filter configuration. Either a status | ||
| * or a shared pointer to the effective configuration. We use a shared pointer to avoid copying | ||
| * in the static configuration flow. | ||
| */ | ||
| using EffectiveFilterConfiguration = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think including StatusOr as part of this is a little confusing. Can we make EffectiveFilterConfiguration just the shared pointer, and then return StatusOr?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, let me know if you feel this was properly addressed. |
||
| absl::StatusOr<std::shared_ptr<const nighthawk::server::ResponseOptions>>; | ||
|
|
||
| /** | ||
| * Provides functionality for parsing and merging request-header based configuration, as well as | ||
| * generating a common error response accross all extensions. | ||
| */ | ||
| class FilterConfigurationBase { | ||
| public: | ||
| /** | ||
| * @brief Construct a new Filter Configuration Base object | ||
| * | ||
| * @param proto_config the static disk-based response options configuration | ||
| * @param filter_name name of the extension that is consuming this. Used during error response | ||
| * generation. | ||
| */ | ||
| FilterConfigurationBase(nighthawk::server::ResponseOptions static_proto_config, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be a const reference?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| absl::string_view filter_name); | ||
|
|
||
| /** | ||
| * Copmute the effective configuration, based on considering the static configuration as well as | ||
| * any configuration provided via request headers. | ||
| * | ||
| * @param request_headers Full set of request headers to be inspected for configuration. | ||
| */ | ||
| void computeEffectiveConfiguration(const Envoy::Http::RequestHeaderMap& request_headers); | ||
|
|
||
| /** | ||
| * Send an error reply based on status of the effective configuration. For example, when dynamic | ||
| * configuration delivered via request headers could not be parsed or was out of spec. | ||
| * | ||
| * @param decoder_callbacks Decoder used to generate the reply. | ||
| * @return true iff an error reply was generated. | ||
| */ | ||
| bool maybeSendErrorReply(Envoy::Http::StreamDecoderFilterCallbacks& decoder_callbacks) const; | ||
|
|
||
| /** | ||
| * @brief Get the effective configuration. Depending on state ,this could be one of static | ||
| * configuration, dynamic configuration, or an error status. | ||
| * | ||
| * @return const EffectiveFilterConfiguration The effective configuration, or an error status. | ||
| */ | ||
| const EffectiveFilterConfiguration getEffectiveConfiguration() const { return effective_config_; } | ||
|
|
||
| /** | ||
| * @return absl::string_view Name of the filter that constructed this instance. | ||
| */ | ||
| absl::string_view filter_name() const { return filter_name_; } | ||
|
|
||
| private: | ||
| const std::string filter_name_; | ||
| const std::shared_ptr<nighthawk::server::ResponseOptions> server_config_; | ||
| EffectiveFilterConfiguration effective_config_; | ||
| }; | ||
|
|
||
| } // namespace Server | ||
| } // namespace Nighthawk | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: proto_config has a different name in the header file.