forked from spring-cloud/spring-cloud-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As defined by https://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-7.1.3 fixes spring-cloudgh-143
1 parent
e44165f
commit e117906
Showing
10 changed files
with
238 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...ateway-core/src/main/java/org/springframework/cloud/gateway/filter/HttpHeadersFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.springframework.cloud.gateway.filter; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
|
||
import java.util.List; | ||
|
||
@FunctionalInterface | ||
public interface HttpHeadersFilter { | ||
|
||
HttpHeaders filter(HttpHeaders original); | ||
|
||
static HttpHeaders filter(List<HttpHeadersFilter> filters, HttpHeaders original) { | ||
HttpHeaders filtered = original; | ||
if (filters != null) { | ||
for (HttpHeadersFilter filter: filters) { | ||
filtered = filter.filter(filtered); | ||
} | ||
} | ||
return filtered; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...e/src/main/java/org/springframework/cloud/gateway/filter/RemoveHopByHopHeadersFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.springframework.cloud.gateway.filter; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.springframework.core.Ordered; | ||
import org.springframework.http.HttpHeaders; | ||
|
||
public class RemoveHopByHopHeadersFilter implements HttpHeadersFilter, Ordered { | ||
|
||
public static final Set<String> HEADERS_REMOVED_ON_REQUEST = | ||
new HashSet<>(Arrays.asList( | ||
"connection", | ||
"keep-alive", | ||
"transfer-encoding", | ||
"te", | ||
"trailer", | ||
"proxy-authorization", | ||
"proxy-authenticate", | ||
"x-application-context", | ||
"upgrade" | ||
// these two are not listed in https://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-7.1.3 | ||
//"proxy-connection", | ||
// "content-length", | ||
)); | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.LOWEST_PRECEDENCE; | ||
} | ||
|
||
@Override | ||
public HttpHeaders filter(HttpHeaders original) { | ||
HttpHeaders filtered = new HttpHeaders(); | ||
List<String> connection = original.getConnection(); | ||
Set<String> toFilter = new HashSet<>(connection); | ||
toFilter.addAll(HEADERS_REMOVED_ON_REQUEST); | ||
|
||
original.entrySet().stream() | ||
.filter(entry -> !toFilter.contains(entry.getKey().toLowerCase())) | ||
.forEach(entry -> filtered.addAll(entry.getKey(), entry.getValue())); | ||
|
||
return filtered; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
.../test/java/org/springframework/cloud/gateway/filter/RemoveHopByHopHeadersFilterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2013-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.springframework.cloud.gateway.filter; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.cloud.gateway.filter.RemoveHopByHopHeadersFilter.HEADERS_REMOVED_ON_REQUEST; | ||
|
||
/** | ||
* @author Spencer Gibb | ||
*/ | ||
public class RemoveHopByHopHeadersFilterTests { | ||
|
||
@Test | ||
public void happyPath() { | ||
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest | ||
.get("http://localhost/get"); | ||
|
||
HEADERS_REMOVED_ON_REQUEST.forEach(header -> builder.header(header, header+"1")); | ||
|
||
testFilter(builder.build()); | ||
} | ||
|
||
@Test | ||
public void caseInsensitive() { | ||
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest | ||
.get("http://localhost/get"); | ||
|
||
HEADERS_REMOVED_ON_REQUEST.forEach(header -> builder.header(header.toLowerCase(), header+"1")); | ||
|
||
testFilter(builder.build()); | ||
} | ||
|
||
@Test | ||
public void removesHeadersListedInConnectionHeader() { | ||
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest | ||
.get("http://localhost/get"); | ||
|
||
builder.header(HttpHeaders.CONNECTION, "upgrade", "keep-alive"); | ||
builder.header(HttpHeaders.UPGRADE, "WebSocket"); | ||
builder.header("Keep-Alive", "timeout:5"); | ||
|
||
testFilter(builder.build(), "upgrade", "keep-alive"); | ||
} | ||
|
||
private void testFilter(MockServerHttpRequest request, String... additionalHeaders) { | ||
RemoveHopByHopHeadersFilter filter = new RemoveHopByHopHeadersFilter(); | ||
HttpHeaders headers = filter.filter(request.getHeaders()); | ||
|
||
Set<String> toRemove = new HashSet<>(HEADERS_REMOVED_ON_REQUEST); | ||
toRemove.addAll(Arrays.asList(additionalHeaders)); | ||
assertThat(headers).doesNotContainKeys(toRemove.toArray(new String[0])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters