Skip to content

Commit 92c2f1b

Browse files
Nibedita JenaRavi Reddy
authored andcommitted
8350991: Improve HTTP client header handling
Reviewed-by: rreddy Backport-of: bddcbf968a54379f22e267fae4ad794af67c6585
1 parent f831e10 commit 92c2f1b

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
@@ -39,6 +39,7 @@
3939
import java.net.http.HttpClient;
4040
import java.net.http.HttpHeaders;
4141
import java.net.http.HttpRequest;
42+
import java.util.function.BiPredicate;
4243

4344
import jdk.internal.net.http.common.Alpns;
4445
import jdk.internal.net.http.common.HttpHeadersBuilder;
@@ -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").isEmpty()) {

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
@@ -187,6 +187,18 @@ private static Set<String> getDisallowedHeaders() {
187187
public static final BiPredicate<String, String>
188188
ALLOWED_HEADERS = (header, unused) -> !DISALLOWED_HEADERS_SET.contains(header);
189189

190+
private static final Set<String> DISALLOWED_REDIRECT_HEADERS_SET = getDisallowedRedirectHeaders();
191+
192+
private static Set<String> getDisallowedRedirectHeaders() {
193+
Set<String> headers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
194+
headers.addAll(Set.of("Authorization", "Cookie", "Origin", "Referer", "Host"));
195+
196+
return Collections.unmodifiableSet(headers);
197+
}
198+
199+
public static final BiPredicate<String, String>
200+
ALLOWED_REDIRECT_HEADERS = (header, _) -> !DISALLOWED_REDIRECT_HEADERS_SET.contains(header);
201+
190202
public static final BiPredicate<String, String> VALIDATE_USER_HEADER =
191203
(name, value) -> {
192204
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
@@ -260,8 +260,9 @@ public static void main(String[] args) throws Exception {
260260
}
261261
try {
262262
for (DigestEchoServer.HttpAuthType authType : types) {
263-
// The test server does not support PROXY305 properly
264-
if (authType == DigestEchoServer.HttpAuthType.PROXY305) continue;
263+
// The test server does not support PROXY305 or SERVER307 properly
264+
if (authType == DigestEchoServer.HttpAuthType.PROXY305 ||
265+
authType == DigestEchoServer.HttpAuthType.SERVER307) continue;
265266
EnumSet<DigestEchoServer.HttpAuthSchemeType> basics =
266267
EnumSet.of(DigestEchoServer.HttpAuthSchemeType.BASICSERVER,
267268
DigestEchoServer.HttpAuthSchemeType.BASIC);

0 commit comments

Comments
 (0)