Skip to content
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

http: use OptRef helper to reduce some boilerplate #14361

Merged
merged 8 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
38 changes: 38 additions & 0 deletions include/envoy/common/optref.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,44 @@ template <class T> struct OptRef : public absl::optional<std::reference_wrapper<
const T& ref = **this;
return &ref;
}

/**
* Helper to convert a OptRef into a pointer. If the optional is not set, returns a nullptr.
*/
T* ptr() {
if (this->has_value()) {
T& ref = **this;
return &ref;
}

return nullptr;
}

/**
* Helper to convert a OptRef into a pointer. If the optional is not set, returns a nullptr.
*/
const T* ptr() const {
if (this->has_value()) {
const T& ref = **this;
return &ref;
}

return nullptr;
}

T& ref() { return **this; }

const T& ref() const { return **this; }
};

template <class T> OptRef<T> makeOptRef(T& ref) { return {ref}; }
Copy link
Contributor

Choose a reason for hiding this comment

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

doxygen doc for makeOptRef and makeOptRefFromPtr?


template <class T> OptRef<T> makeOptRefFromPtr(T* ptr) {
if (ptr == nullptr) {
return {};
}

return {*ptr};
}

} // namespace Envoy
9 changes: 5 additions & 4 deletions include/envoy/http/header_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <vector>

#include "envoy/common/optref.h"
#include "envoy/common/pure.h"

#include "common/common/assert.h"
Expand Down Expand Up @@ -748,14 +749,14 @@ class RequestHeaderMap
INLINE_REQ_HEADERS(DEFINE_INLINE_HEADER)
};
using RequestHeaderMapPtr = std::unique_ptr<RequestHeaderMap>;
using RequestHeaderMapOptRef = absl::optional<std::reference_wrapper<RequestHeaderMap>>;
using RequestHeaderMapOptRef = OptRef<RequestHeaderMap>;

// Request trailers.
class RequestTrailerMap
: public HeaderMap,
public CustomInlineHeaderBase<CustomInlineHeaderRegistry::Type::RequestTrailers> {};
using RequestTrailerMapPtr = std::unique_ptr<RequestTrailerMap>;
using RequestTrailerMapOptRef = absl::optional<std::reference_wrapper<RequestTrailerMap>>;
using RequestTrailerMapOptRef = OptRef<RequestTrailerMap>;

// Base class for both response headers and trailers.
class ResponseHeaderOrTrailerMap {
Expand All @@ -774,15 +775,15 @@ class ResponseHeaderMap
INLINE_RESP_HEADERS(DEFINE_INLINE_HEADER)
};
using ResponseHeaderMapPtr = std::unique_ptr<ResponseHeaderMap>;
using ResponseHeaderMapOptRef = absl::optional<std::reference_wrapper<ResponseHeaderMap>>;
using ResponseHeaderMapOptRef = OptRef<ResponseHeaderMap>;

// Response trailers.
class ResponseTrailerMap
: public ResponseHeaderOrTrailerMap,
public HeaderMap,
public CustomInlineHeaderBase<CustomInlineHeaderRegistry::Type::ResponseTrailers> {};
using ResponseTrailerMapPtr = std::unique_ptr<ResponseTrailerMap>;
using ResponseTrailerMapOptRef = absl::optional<std::reference_wrapper<ResponseTrailerMap>>;
using ResponseTrailerMapOptRef = OptRef<ResponseTrailerMap>;

/**
* Convenient container type for storing Http::LowerCaseString and std::string key/value pairs.
Expand Down
2 changes: 1 addition & 1 deletion source/common/common/dump_state_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Envoy {
#define DUMP_DETAILS(member) \
do { \
os << spaces << #member ": "; \
if ((member) != nullptr) { \
if (member) { \
os << "\n"; \
(member)->dumpState(os, indent_level + 1); \
} else { \
Expand Down
13 changes: 6 additions & 7 deletions source/common/http/conn_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include "envoy/access_log/access_log.h"
#include "envoy/common/optref.h"
#include "envoy/common/random_generator.h"
#include "envoy/common/scope_tracker.h"
#include "envoy/common/time.h"
Expand Down Expand Up @@ -229,22 +230,20 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>,
}
void chargeStats(const ResponseHeaderMap& headers) override;

// TODO(snowp): Create shared OptRef/OptConstRef helpers
Http::RequestHeaderMapOptRef requestHeaders() override {
return request_headers_ ? absl::make_optional(std::ref(*request_headers_)) : absl::nullopt;
return makeOptRefFromPtr(request_headers_.get());
}
Http::RequestTrailerMapOptRef requestTrailers() override {
return request_trailers_ ? absl::make_optional(std::ref(*request_trailers_)) : absl::nullopt;
return makeOptRefFromPtr(request_trailers_.get());
}
Http::ResponseHeaderMapOptRef continueHeaders() override {
return continue_headers_ ? absl::make_optional(std::ref(*continue_headers_)) : absl::nullopt;
return makeOptRefFromPtr(continue_headers_.get());
}
Http::ResponseHeaderMapOptRef responseHeaders() override {
return response_headers_ ? absl::make_optional(std::ref(*response_headers_)) : absl::nullopt;
return makeOptRefFromPtr(response_headers_.get());
}
Http::ResponseTrailerMapOptRef responseTrailers() override {
return response_trailers_ ? absl::make_optional(std::ref(*response_trailers_))
: absl::nullopt;
return makeOptRefFromPtr(response_trailers_.get());
}

void endStream() override {
Expand Down
18 changes: 7 additions & 11 deletions source/common/http/filter_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -804,16 +804,14 @@ void FilterManager::sendLocalReplyViaFilterChain(
absl::string_view& content_type) -> void {
// TODO(snowp): This &get() business isn't nice, rework LocalReply and others to accept
// opt refs.
local_reply_.rewrite(filter_manager_callbacks_.requestHeaders().has_value()
? &filter_manager_callbacks_.requestHeaders()->get()
: nullptr,
response_headers, stream_info_, code, body, content_type);
local_reply_.rewrite(filter_manager_callbacks_.requestHeaders().ptr(), response_headers,
stream_info_, code, body, content_type);
},
[this, modify_headers](ResponseHeaderMapPtr&& headers, bool end_stream) -> void {
filter_manager_callbacks_.setResponseHeaders(std::move(headers));
// TODO: Start encoding from the last decoder filter that saw the
// request instead.
encodeHeaders(nullptr, filter_manager_callbacks_.responseHeaders()->get(), end_stream);
encodeHeaders(nullptr, filter_manager_callbacks_.responseHeaders().ref(), end_stream);
},
[this](Buffer::Instance& data, bool end_stream) -> void {
// TODO: Start encoding from the last decoder filter that saw the
Expand Down Expand Up @@ -845,10 +843,8 @@ void FilterManager::sendDirectLocalReply(
},
[&](ResponseHeaderMap& response_headers, Code& code, std::string& body,
absl::string_view& content_type) -> void {
local_reply_.rewrite(filter_manager_callbacks_.requestHeaders().has_value()
? &filter_manager_callbacks_.requestHeaders()->get()
: nullptr,
response_headers, stream_info_, code, body, content_type);
local_reply_.rewrite(filter_manager_callbacks_.requestHeaders().ptr(), response_headers,
stream_info_, code, body, content_type);
},
[&](ResponseHeaderMapPtr&& response_headers, bool end_stream) -> void {
// Move the response headers into the FilterManager to make sure they're visible to
Expand Down Expand Up @@ -1193,11 +1189,11 @@ bool FilterManager::createFilterChain() {
bool upgrade_rejected = false;
const HeaderEntry* upgrade = nullptr;
if (filter_manager_callbacks_.requestHeaders()) {
upgrade = filter_manager_callbacks_.requestHeaders()->get().Upgrade();
upgrade = filter_manager_callbacks_.requestHeaders()->Upgrade();

// Treat CONNECT requests as a special upgrade case.
if (!upgrade && HeaderUtility::isConnect(*filter_manager_callbacks_.requestHeaders())) {
upgrade = filter_manager_callbacks_.requestHeaders()->get().Method();
upgrade = filter_manager_callbacks_.requestHeaders()->Method();
}
}

Expand Down
18 changes: 9 additions & 9 deletions source/common/http/filter_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,10 @@ class FilterManager : public ScopeTrackedObject,
const char* spaces = spacesForLevel(indent_level);
os << spaces << "FilterManager " << this << DUMP_MEMBER(state_.has_continue_headers_) << "\n";

DUMP_OPT_REF_DETAILS(filter_manager_callbacks_.requestHeaders());
DUMP_OPT_REF_DETAILS(filter_manager_callbacks_.requestTrailers());
DUMP_OPT_REF_DETAILS(filter_manager_callbacks_.responseHeaders());
DUMP_OPT_REF_DETAILS(filter_manager_callbacks_.responseTrailers());
DUMP_DETAILS(filter_manager_callbacks_.requestHeaders());
DUMP_DETAILS(filter_manager_callbacks_.requestTrailers());
DUMP_DETAILS(filter_manager_callbacks_.responseHeaders());
DUMP_DETAILS(filter_manager_callbacks_.responseTrailers());
DUMP_DETAILS(&stream_info_);
}

Expand All @@ -531,15 +531,15 @@ class FilterManager : public ScopeTrackedObject,
void log() {
RequestHeaderMap* request_headers = nullptr;
if (filter_manager_callbacks_.requestHeaders()) {
request_headers = &filter_manager_callbacks_.requestHeaders()->get();
request_headers = filter_manager_callbacks_.requestHeaders().ptr();
}
ResponseHeaderMap* response_headers = nullptr;
if (filter_manager_callbacks_.responseHeaders()) {
response_headers = &filter_manager_callbacks_.responseHeaders()->get();
response_headers = filter_manager_callbacks_.responseHeaders().ptr();
}
ResponseTrailerMap* response_trailers = nullptr;
if (filter_manager_callbacks_.responseTrailers()) {
response_trailers = &filter_manager_callbacks_.responseTrailers()->get();
response_trailers = filter_manager_callbacks_.responseTrailers().ptr();
}

for (const auto& log_handler : access_log_handlers_) {
Expand Down Expand Up @@ -660,11 +660,11 @@ class FilterManager : public ScopeTrackedObject,

void requestHeadersInitialized() {
if (Http::Headers::get().MethodValues.Head ==
filter_manager_callbacks_.requestHeaders()->get().getMethodValue()) {
filter_manager_callbacks_.requestHeaders()->getMethodValue()) {
state_.is_head_request_ = true;
}
state_.is_grpc_request_ =
Grpc::Common::isGrpcRequestHeaders(filter_manager_callbacks_.requestHeaders()->get());
Grpc::Common::isGrpcRequestHeaders(filter_manager_callbacks_.requestHeaders().ref());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/common/http/conn_manager_impl_test_2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2356,7 +2356,7 @@ TEST_F(HttpConnectionManagerImplTest, TestSessionTrace) {
object->dumpState(out);
std::string state = out.str();
EXPECT_THAT(state,
testing::HasSubstr("filter_manager_callbacks_.requestHeaders(): empty"));
testing::HasSubstr("filter_manager_callbacks_.requestHeaders(): null"));
EXPECT_THAT(state, testing::HasSubstr("protocol_: 1"));
return nullptr;
}))
Expand Down
4 changes: 2 additions & 2 deletions test/common/http/filter_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST_F(FilterManagerTest, SendLocalReplyDuringDecodingGrpcClassiciation) {
{"content-type", "application/grpc"}}};

ON_CALL(filter_manager_callbacks_, requestHeaders())
.WillByDefault(Return(absl::make_optional(std::ref(*grpc_headers))));
.WillByDefault(Return(makeOptRef(*grpc_headers)));

EXPECT_CALL(filter_factory_, createFilterChain(_))
.WillRepeatedly(Invoke([&](FilterChainFactoryCallbacks& callbacks) -> void {
Expand Down Expand Up @@ -123,7 +123,7 @@ TEST_F(FilterManagerTest, SendLocalReplyDuringEncodingGrpcClassiciation) {
{"content-type", "application/grpc"}}};

ON_CALL(filter_manager_callbacks_, requestHeaders())
.WillByDefault(Return(absl::make_optional(std::ref(*grpc_headers))));
.WillByDefault(Return(makeOptRef(*grpc_headers)));
filter_manager_->createFilterChain();

filter_manager_->requestHeadersInitialized();
Expand Down
6 changes: 2 additions & 4 deletions test/mocks/http/mocks.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "mocks.h"

#include "envoy/buffer/buffer.h"
#include "envoy/common/optref.h"
#include "envoy/event/dispatcher.h"
#include "envoy/http/header_map.h"

Expand All @@ -23,10 +24,7 @@ MockServerConnectionCallbacks::~MockServerConnectionCallbacks() = default;

MockFilterManagerCallbacks::MockFilterManagerCallbacks() {
ON_CALL(*this, responseHeaders()).WillByDefault(Invoke([this]() -> ResponseHeaderMapOptRef {
if (response_headers_) {
return absl::make_optional(std::ref(*response_headers_));
}
return absl::nullopt;
return makeOptRefFromPtr(response_headers_.get());
}));
}
MockFilterManagerCallbacks::~MockFilterManagerCallbacks() = default;
Expand Down