Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions changelogs/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ minor_behavior_changes:

bug_fixes:
# *Changes expected to improve the state of the world and are unlikely to have negative effects*
- area: http
change: |
Fixed a bug where the ``response_headers_to_add`` may be processed multiple times for the local responses from
the router filter.

removed_config_or_runtime:
# *Normally occurs at the end of the* :ref:`deprecation period <deprecated>`
Expand Down
3 changes: 1 addition & 2 deletions source/common/router/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,6 @@ Http::FilterHeadersStatus Filter::decodeHeaders(Http::RequestHeaderMap& headers,
modify_headers_from_upstream_lb_(headers);
}

route_entry_->finalizeResponseHeaders(headers, callbacks_->streamInfo());

if (attempt_count_ == 0 || !route_entry_->includeAttemptCountInResponse()) {
return;
}
Expand Down Expand Up @@ -1758,6 +1756,7 @@ void Filter::onUpstreamHeaders(uint64_t response_code, Http::ResponseHeaderMapPt

// Modify response headers after we have set the final upstream info because we may need to
// modify the headers based on the upstream host.
route_entry_->finalizeResponseHeaders(*headers, callbacks_->streamInfo());
modify_headers_(*headers);

if (end_stream) {
Expand Down
5 changes: 0 additions & 5 deletions test/common/router/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ TEST_F(RouterTest, NoHost) {
EXPECT_CALL(callbacks_, encodeData(_, true));
EXPECT_CALL(callbacks_.stream_info_,
setResponseFlag(StreamInfo::CoreResponseFlag::NoHealthyUpstream));
EXPECT_CALL(callbacks_.route_->route_entry_, finalizeResponseHeaders(_, _));

Http::TestRequestHeaderMapImpl headers;
HttpTestUtility::addDefaultHeaders(headers);
Expand Down Expand Up @@ -846,7 +845,6 @@ TEST_F(RouterTest, MaintenanceMode) {
EXPECT_CALL(callbacks_, encodeData(_, true));
EXPECT_CALL(callbacks_.stream_info_,
setResponseFlag(StreamInfo::CoreResponseFlag::UpstreamOverflow));
EXPECT_CALL(callbacks_.route_->route_entry_, finalizeResponseHeaders(_, _));

Http::TestRequestHeaderMapImpl headers;
HttpTestUtility::addDefaultHeaders(headers);
Expand Down Expand Up @@ -894,7 +892,6 @@ TEST_F(RouterTest, DropOverloadDropped) {
EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&response_headers), false));
EXPECT_CALL(callbacks_, encodeData(_, true));
EXPECT_CALL(callbacks_.stream_info_, setResponseFlag(StreamInfo::CoreResponseFlag::DropOverLoad));
EXPECT_CALL(callbacks_.route_->route_entry_, finalizeResponseHeaders(_, _));

Http::TestRequestHeaderMapImpl headers;
HttpTestUtility::addDefaultHeaders(headers);
Expand Down Expand Up @@ -1226,7 +1223,6 @@ TEST_F(RouterTest, AllDebugConfig) {
Http::TestResponseHeaderMapImpl response_headers{{":status", "204"},
{"x-envoy-not-forwarded", "true"}};
EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&response_headers), true));
EXPECT_CALL(callbacks_.route_->route_entry_, finalizeResponseHeaders(_, _));

Http::TestRequestHeaderMapImpl headers;
HttpTestUtility::addDefaultHeaders(headers);
Expand Down Expand Up @@ -3332,7 +3328,6 @@ TEST_F(RouterTest, RetryNoneHealthy) {
EXPECT_CALL(callbacks_, encodeData(_, true));
EXPECT_CALL(callbacks_.stream_info_,
setResponseFlag(StreamInfo::CoreResponseFlag::NoHealthyUpstream));
EXPECT_CALL(callbacks_.route_->route_entry_, finalizeResponseHeaders(_, _));
router_->retry_state_->callback_();
EXPECT_TRUE(verifyHostUpstreamStats(0, 1));
// Pool failure for the first try, so only 1 upstream request was made.
Expand Down
39 changes: 39 additions & 0 deletions test/integration/header_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,45 @@ TEST_P(HeaderIntegrationTest, TestDynamicHeaders) {
});
}

TEST_P(HeaderIntegrationTest, TestResponseHeadersOnlyBeHandledOnce) {
initializeFilter(HeaderMode::Append, false);
registerTestServerPorts({"http"});
codec_client_ = makeHttpConnection(makeClientConnection(lookupPort("http")));

auto encoder_decoder = codec_client_->startRequest(Http::TestRequestHeaderMapImpl{
{":method", "POST"},
{":path", "/vhost-and-route"},
{":scheme", "http"},
{":authority", "vhost-headers.com"},
});
auto response = std::move(encoder_decoder.second);

ASSERT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));

ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
ASSERT_TRUE(fake_upstream_connection_->close());
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
ASSERT_TRUE(response->waitForEndStream());

if (downstream_protocol_ == Http::CodecType::HTTP1) {
ASSERT_TRUE(codec_client_->waitForDisconnect());
} else {
codec_client_->close();
}

EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());

EXPECT_TRUE(response->complete());

Http::TestResponseHeaderMapImpl response_headers{response->headers()};
EXPECT_EQ(1, response_headers.get(Http::LowerCaseString("x-route-response")).size());
EXPECT_EQ("route", response_headers.get_("x-route-response"));
EXPECT_EQ(1, response_headers.get(Http::LowerCaseString("x-vhost-response")).size());
EXPECT_EQ("vhost", response_headers.get_("x-vhost-response"));
}

// Validates that XFF gets properly parsed.
TEST_P(HeaderIntegrationTest, TestXFFParsing) {
initializeFilter(HeaderMode::Replace, false);
Expand Down