Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions source/server/http_filter_config_base.cc
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,

Copy link
Copy Markdown
Contributor

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.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be const? Or is sendLocalReply a non-const function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
80 changes: 80 additions & 0 deletions source/server/http_filter_config_base.h
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 =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be a const reference?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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