Skip to content

Commit

Permalink
Change the precedence between :authority and Host headers (open-t…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored and Abhishekkr3003 committed Nov 7, 2023
1 parent 4f0cf16 commit 82d1d48
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public void extract(AddressPortSink sink, REQUEST request) {
}
}

// try Host
for (String host : getter.getHttpRequestHeader(request, "host")) {
// try :authority (HTTP 2.0 pseudo-header)
for (String host : getter.getHttpRequestHeader(request, ":authority")) {
if (extractHost(sink, host, 0, host.length())) {
return;
}
}

// try :authority (HTTP 2.0 pseudo-header)
for (String host : getter.getHttpRequestHeader(request, ":authority")) {
// try Host
for (String host : getter.getHttpRequestHeader(request, "host")) {
if (extractHost(sink, host, 0, host.length())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ void shouldParseForwardedHost(

@ParameterizedTest
@ArgumentsSource(HostArgs.class)
void shouldParseHost(
void shouldParsePseudoAuthority(
List<String> headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) {
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "forwarded");
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "x-forwarded-host");
doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, "host");
doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, ":authority");

AddressAndPort sink = new AddressAndPort();
underTest.extract(sink, REQUEST);
Expand All @@ -111,12 +111,12 @@ void shouldParseHost(

@ParameterizedTest
@ArgumentsSource(HostArgs.class)
void shouldParsePseudoAuthority(
void shouldParseHost(
List<String> headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) {
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "forwarded");
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "x-forwarded-host");
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "host");
doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, ":authority");
doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, ":authority");
doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, "host");

AddressAndPort sink = new AddressAndPort();
underTest.extract(sink, REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ void shouldExtractServerAddressAndPortFromForwardedHostHeader() {
}

@Test
void shouldExtractServerAddressAndPortFromHostHeader() {
void shouldExtractServerAddressAndPortFromAuthorityPseudoHeader() {
Map<String, String> request = new HashMap<>();
request.put("header.host", "github.com:123");
request.put("header.:authority", "opentelemetry.io:42");
request.put("header.host", "github.com:123");

Map<String, String> response = new HashMap<>();
response.put("statusCode", "200");
Expand All @@ -470,8 +470,8 @@ void shouldExtractServerAddressAndPortFromHostHeader() {

assertThat(startAttributes.build())
.containsOnly(
entry(SemanticAttributes.SERVER_ADDRESS, "github.com"),
entry(SemanticAttributes.SERVER_PORT, 123L));
entry(SemanticAttributes.SERVER_ADDRESS, "opentelemetry.io"),
entry(SemanticAttributes.SERVER_PORT, 42L));

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, response, null);
Expand All @@ -480,9 +480,9 @@ void shouldExtractServerAddressAndPortFromHostHeader() {
}

@Test
void shouldExtractServerAddressAndPortFromAuthorityPseudoHeader() {
void shouldExtractServerAddressAndPortFromHostHeader() {
Map<String, String> request = new HashMap<>();
request.put("header.:authority", "opentelemetry.io:42");
request.put("header.host", "github.com:123");

Map<String, String> response = new HashMap<>();
response.put("statusCode", "200");
Expand All @@ -495,8 +495,8 @@ void shouldExtractServerAddressAndPortFromAuthorityPseudoHeader() {

assertThat(startAttributes.build())
.containsOnly(
entry(SemanticAttributes.SERVER_ADDRESS, "opentelemetry.io"),
entry(SemanticAttributes.SERVER_PORT, 42L));
entry(SemanticAttributes.SERVER_ADDRESS, "github.com"),
entry(SemanticAttributes.SERVER_PORT, 123L));

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, response, null);
Expand Down

0 comments on commit 82d1d48

Please sign in to comment.