forked from envoyproxy/nighthawk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server extension: start factoring out common code / functionality.
A first step to clean this up. Functional no-op. Part of envoyproxy#498 Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
- Loading branch information
Showing
15 changed files
with
213 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = | ||
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, | ||
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 |
Oops, something went wrong.