Skip to content

Commit ae39653

Browse files
Alexey Bakhtingnu-andrew
authored andcommitted
8350991: Improve HTTP client header handling
Reviewed-by: mbalao, andrew Backport-of: 3b0f6ebdf8dbaf0caf9a9ec1f201d5938f674021
1 parent 898c007 commit ae39653

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.net.http.HttpClient;
4242
import java.net.http.HttpHeaders;
4343
import java.net.http.HttpRequest;
44+
import java.util.function.BiPredicate;
4445

4546
import jdk.internal.net.http.common.HttpHeadersBuilder;
4647
import jdk.internal.net.http.common.Utils;
@@ -148,7 +149,11 @@ public static HttpRequestImpl newInstanceForRedirection(URI uri,
148149
String method,
149150
HttpRequestImpl other,
150151
boolean mayHaveBody) {
151-
return new HttpRequestImpl(uri, method, other, mayHaveBody);
152+
if (uri.getScheme().equalsIgnoreCase(other.uri.getScheme()) &&
153+
uri.getRawAuthority().equals(other.uri.getRawAuthority())) {
154+
return new HttpRequestImpl(uri, method, other, mayHaveBody, Optional.empty());
155+
}
156+
return new HttpRequestImpl(uri, method, other, mayHaveBody, Optional.of(Utils.ALLOWED_REDIRECT_HEADERS));
152157
}
153158

154159
/** Returns a new instance suitable for authentication. */
@@ -168,9 +173,19 @@ private HttpRequestImpl(URI uri,
168173
String method,
169174
HttpRequestImpl other,
170175
boolean mayHaveBody) {
176+
this(uri, method, other, mayHaveBody, Optional.empty());
177+
}
178+
179+
private HttpRequestImpl(URI uri,
180+
String method,
181+
HttpRequestImpl other,
182+
boolean mayHaveBody,
183+
Optional<BiPredicate<String, String>> redirectHeadersFilter) {
171184
assert method == null || Utils.isValidName(method);
172-
this.method = method == null? "GET" : method;
173-
this.userHeaders = other.userHeaders;
185+
this.method = method == null ? "GET" : method;
186+
HttpHeaders userHeaders = redirectHeadersFilter.isPresent() ?
187+
HttpHeaders.of(other.userHeaders.map(), redirectHeadersFilter.get()) : other.userHeaders;
188+
this.userHeaders = userHeaders;
174189
this.isWebSocket = other.isWebSocket;
175190
this.systemHeadersBuilder = new HttpHeadersBuilder();
176191
if (!userHeaders.firstValue("User-Agent").isPresent()) {

src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ private static Set<String> getDisallowedHeaders() {
152152
public static final BiPredicate<String, String>
153153
ALLOWED_HEADERS = (header, unused) -> !DISALLOWED_HEADERS_SET.contains(header);
154154

155+
private static final Set<String> DISALLOWED_REDIRECT_HEADERS_SET = getDisallowedRedirectHeaders();
156+
157+
private static Set<String> getDisallowedRedirectHeaders() {
158+
Set<String> headers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
159+
headers.addAll(Set.of("Authorization", "Cookie", "Origin", "Referer", "Host"));
160+
161+
return Collections.unmodifiableSet(headers);
162+
}
163+
164+
public static final BiPredicate<String, String>
165+
ALLOWED_REDIRECT_HEADERS = (header, unused) -> !DISALLOWED_REDIRECT_HEADERS_SET.contains(header);
166+
155167
public static final BiPredicate<String, String> VALIDATE_USER_HEADER =
156168
(name, value) -> {
157169
assert name != null : "null header name";

test/jdk/java/net/httpclient/DigestEchoClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ public static void main(String[] args) throws Exception {
264264
}
265265
try {
266266
for (DigestEchoServer.HttpAuthType authType : types) {
267-
// The test server does not support PROXY305 properly
268-
if (authType == DigestEchoServer.HttpAuthType.PROXY305) continue;
267+
// The test server does not support PROXY305 or SERVER307 properly
268+
if (authType == DigestEchoServer.HttpAuthType.PROXY305 ||
269+
authType == DigestEchoServer.HttpAuthType.SERVER307) continue;
269270
EnumSet<DigestEchoServer.HttpAuthSchemeType> basics =
270271
EnumSet.of(DigestEchoServer.HttpAuthSchemeType.BASICSERVER,
271272
DigestEchoServer.HttpAuthSchemeType.BASIC);

0 commit comments

Comments
 (0)