Skip to content

Commit

Permalink
stream_info: add setResponseCode and update local_reply to take a nor…
Browse files Browse the repository at this point in the history
…mal StreamInfo (#14402)

Signed-off-by: John Esmet <john.esmet@gmail.com>
  • Loading branch information
esmet committed Dec 16, 2020
1 parent 93ee668 commit ffbf952
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions include/envoy/stream_info/stream_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ class StreamInfo {
*/
virtual void setResponseFlag(ResponseFlag response_flag) PURE;

/**
* @param code the HTTP response code to set for this request.
*/
virtual void setResponseCode(uint32_t code) PURE;

/**
* @param rc_details the response code details string to set for this request.
* See ResponseCodeDetailValues above for well-known constants.
Expand Down
8 changes: 4 additions & 4 deletions source/common/local_reply/local_reply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ResponseMapper {
bool matchAndRewrite(const Http::RequestHeaderMap& request_headers,
Http::ResponseHeaderMap& response_headers,
const Http::ResponseTrailerMap& response_trailers,
StreamInfo::StreamInfoImpl& stream_info, Http::Code& code, std::string& body,
StreamInfo::StreamInfo& stream_info, Http::Code& code, std::string& body,
BodyFormatter*& final_formatter) const {
// If not matched, just bail out.
if (!filter_->evaluate(stream_info, request_headers, response_headers, response_trailers)) {
Expand All @@ -93,7 +93,7 @@ class ResponseMapper {
if (status_code_.has_value() && code != status_code_.value()) {
code = status_code_.value();
response_headers.setStatus(std::to_string(enumToInt(code)));
stream_info.response_code_ = static_cast<uint32_t>(code);
stream_info.setResponseCode(static_cast<uint32_t>(code));
}

if (body_formatter_) {
Expand Down Expand Up @@ -129,14 +129,14 @@ class LocalReplyImpl : public LocalReply {
}

void rewrite(const Http::RequestHeaderMap* request_headers,
Http::ResponseHeaderMap& response_headers, StreamInfo::StreamInfoImpl& stream_info,
Http::ResponseHeaderMap& response_headers, StreamInfo::StreamInfo& stream_info,
Http::Code& code, std::string& body,
absl::string_view& content_type) const override {
// Set response code to stream_info and response_headers due to:
// 1) StatusCode filter is using response_code from stream_info,
// 2) %RESP(:status)% is from Status() in response_headers.
response_headers.setStatus(std::to_string(enumToInt(code)));
stream_info.response_code_ = static_cast<uint32_t>(code);
stream_info.setResponseCode(static_cast<uint32_t>(code));

if (request_headers == nullptr) {
request_headers = Http::StaticEmptyHeaders::get().request_headers.get();
Expand Down
2 changes: 1 addition & 1 deletion source/common/local_reply/local_reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LocalReply {
*/
virtual void rewrite(const Http::RequestHeaderMap* request_headers,
Http::ResponseHeaderMap& response_headers,
StreamInfo::StreamInfoImpl& stream_info, Http::Code& code, std::string& body,
StreamInfo::StreamInfo& stream_info, Http::Code& code, std::string& body,
absl::string_view& content_type) const PURE;
};

Expand Down
2 changes: 2 additions & 0 deletions source/common/stream_info/stream_info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ struct StreamInfoImpl : public StreamInfo {
return response_code_details_;
}

void setResponseCode(uint32_t code) override { response_code_ = code; }

void setResponseCodeDetails(absl::string_view rc_details) override {
response_code_details_.emplace(absl::StrReplaceAll(rc_details, emptySpaceReplacement()));
}
Expand Down
1 change: 1 addition & 0 deletions test/common/stream_info/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TestStreamInfo : public StreamInfo::StreamInfo {
const absl::optional<std::string>& responseCodeDetails() const override {
return response_code_details_;
}
void setResponseCode(uint32_t code) override { response_code_ = code; }
void setResponseCodeDetails(absl::string_view rc_details) override {
response_code_details_.emplace(rc_details);
}
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/local_reply/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class MockLocalReply : public LocalReply {

MOCK_METHOD(void, rewrite,
(const Http::RequestHeaderMap* request_headers,
Http::ResponseHeaderMap& response_headers, StreamInfo::StreamInfoImpl& stream_info,
Http::ResponseHeaderMap& response_headers, StreamInfo::StreamInfo& stream_info,
Http::Code& code, std::string& body, absl::string_view& content_type),
(const));
};
} // namespace LocalReply
} // namespace Envoy
} // namespace Envoy
3 changes: 3 additions & 0 deletions test/mocks/stream_info/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ MockStreamInfo::MockStreamInfo()
ON_CALL(*this, setResponseFlag(_)).WillByDefault(Invoke([this](ResponseFlag response_flag) {
response_flags_ |= response_flag;
}));
ON_CALL(*this, setResponseCode(_)).WillByDefault(Invoke([this](uint32_t code) {
response_code_ = code;
}));
ON_CALL(*this, setResponseCodeDetails(_)).WillByDefault(Invoke([this](absl::string_view details) {
response_code_details_ = std::string(details);
}));
Expand Down
1 change: 1 addition & 0 deletions test/mocks/stream_info/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MockStreamInfo : public StreamInfo {

// StreamInfo::StreamInfo
MOCK_METHOD(void, setResponseFlag, (ResponseFlag response_flag));
MOCK_METHOD(void, setResponseCode, (uint32_t));
MOCK_METHOD(void, setResponseCodeDetails, (absl::string_view));
MOCK_METHOD(void, setConnectionTerminationDetails, (absl::string_view));
MOCK_METHOD(bool, intersectResponseFlags, (uint64_t), (const));
Expand Down

0 comments on commit ffbf952

Please sign in to comment.