Skip to content

Commit

Permalink
generic proxy: change the status code type to int (envoyproxy#31964)
Browse files Browse the repository at this point in the history
* generic proxy: change the status code type to int

To support minus response code and output.

Signed-off-by: wbpcode <[email protected]>
  • Loading branch information
code authored Jan 24, 2024
1 parent 282ff3a commit bd9ee2e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
32 changes: 32 additions & 0 deletions contrib/generic_proxy/filters/network/source/access_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ class StringValueFormatterProvider : public FormatterProvider {
absl::optional<size_t> max_length_;
};

class GenericStatusCodeFormatterProvider : public FormatterProvider {
public:
GenericStatusCodeFormatterProvider() = default;

// FormatterProvider
absl::optional<std::string> formatWithContext(const FormatterContext& context,
const StreamInfo::StreamInfo&) const override {
if (context.response_ == nullptr) {
return absl::nullopt;
}

const int code = context.response_->status().code();
return std::to_string(code);
}
ProtobufWkt::Value formatValueWithContext(const FormatterContext& context,
const StreamInfo::StreamInfo&) const override {
if (context.response_ == nullptr) {
return ValueUtil::nullValue();
}

const int code = context.response_->status().code();
return ValueUtil::numberValue(code);
}
};

class SimpleCommandParser : public CommandParser {
public:
using ProviderFunc =
Expand Down Expand Up @@ -139,6 +164,13 @@ class SimpleCommandParser : public CommandParser {
return std::string(optional_view.value());
});
}},
// A formatter for the response status code. This supports the case where the response
// code is minus value and will override the common RESPONSE_CODE formatter for generic
// proxy.
{"RESPONSE_CODE",
[](absl::string_view, absl::optional<size_t>) -> FormatterProviderPtr {
return std::make_unique<GenericStatusCodeFormatterProvider>();
}},
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,18 @@ using StatusCode = absl::StatusCode;
struct StreamStatus {
public:
StreamStatus() = default;
StreamStatus(uint32_t code, bool ok) : code_(code), ok_(ok) {}
StreamStatus(int code, bool ok) : code_(code), ok_(ok) {}

// Returns true if the status indicates success. This will be used for tracing, logging
// or stats purposes.
ABSL_MUST_USE_RESULT bool ok() const { return ok_; }

// Returns the status code value. The code will be used for tracing, logging or stats
// purposes. The specific code value is application protocol specific.
ABSL_MUST_USE_RESULT uint32_t code() const { return code_; }
ABSL_MUST_USE_RESULT int code() const { return code_; }

private:
uint32_t code_{};
int code_{};
bool ok_{true};
};

Expand Down
7 changes: 3 additions & 4 deletions contrib/generic_proxy/filters/network/test/fake_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,15 @@ class FakeStreamCodecFactory : public CodecFactory {
}
// Additional 4 bytes for status.
encoding_buffer_.writeBEInt<uint32_t>(body.size() + 4);
encoding_buffer_.writeBEInt<uint32_t>(static_cast<int32_t>(typed_response->status_.code()));
encoding_buffer_.writeBEInt<int>(typed_response->status_.code());
encoding_buffer_.add(body);

callback.onEncodingSuccess(encoding_buffer_, response.frameFlags().endStream());
}

ResponsePtr respond(Status status, absl::string_view, const Request&) override {
auto response = std::make_unique<FakeResponse>();
response->status_ = {static_cast<uint32_t>(status.code()),
status.code() == absl::StatusCode::kOk};
response->status_ = {status.raw_code(), status.ok()};
response->message_ = status.message();
response->protocol_ = "fake_protocol_for_test";
return response;
Expand Down Expand Up @@ -192,7 +191,7 @@ class FakeStreamCodecFactory : public CodecFactory {
}

auto response = std::make_unique<FakeResponse>();
response->status_ = {static_cast<uint32_t>(status_code),
response->status_ = {status_code,
static_cast<absl::StatusCode>(status_code) == absl::StatusCode::kOk};
response->protocol_ = std::string(result[0]);
for (absl::string_view pair_str : absl::StrSplit(result[2], ';', absl::SkipEmpty())) {
Expand Down
3 changes: 1 addition & 2 deletions contrib/generic_proxy/filters/network/test/proxy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,7 @@ TEST_F(FilterTest, ActiveStreamSendLocalReply) {
EXPECT_CALL(*server_codec_, respond(_, _, _))
.WillOnce(Invoke([&](Status status, absl::string_view, const Request&) -> ResponsePtr {
auto response = std::make_unique<FakeStreamCodecFactory::FakeResponse>();
response->status_ = {static_cast<uint32_t>(status.code()),
status.code() == StatusCode::kOk};
response->status_ = {static_cast<int>(status.code()), status.code() == StatusCode::kOk};
response->message_ = status.message();
return response;
}));
Expand Down

0 comments on commit bd9ee2e

Please sign in to comment.