Skip to content

Commit 2494864

Browse files
Alexey BakhtinRealCLanger
authored andcommitted
8350991: Improve HTTP client header handling
Reviewed-by: mbalao, andrew Backport-of: 3b0f6ebdf8dbaf0caf9a9ec1f201d5938f674021
1 parent bab305a commit 2494864

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;
@@ -150,7 +151,11 @@ public static HttpRequestImpl newInstanceForRedirection(URI uri,
150151
String method,
151152
HttpRequestImpl other,
152153
boolean mayHaveBody) {
153-
return new HttpRequestImpl(uri, method, other, mayHaveBody);
154+
if (uri.getScheme().equalsIgnoreCase(other.uri.getScheme()) &&
155+
uri.getRawAuthority().equals(other.uri.getRawAuthority())) {
156+
return new HttpRequestImpl(uri, method, other, mayHaveBody, Optional.empty());
157+
}
158+
return new HttpRequestImpl(uri, method, other, mayHaveBody, Optional.of(Utils.ALLOWED_REDIRECT_HEADERS));
154159
}
155160

156161
/** Returns a new instance suitable for authentication. */
@@ -170,9 +175,19 @@ private HttpRequestImpl(URI uri,
170175
String method,
171176
HttpRequestImpl other,
172177
boolean mayHaveBody) {
178+
this(uri, method, other, mayHaveBody, Optional.empty());
179+
}
180+
181+
private HttpRequestImpl(URI uri,
182+
String method,
183+
HttpRequestImpl other,
184+
boolean mayHaveBody,
185+
Optional<BiPredicate<String, String>> redirectHeadersFilter) {
173186
assert method == null || Utils.isValidName(method);
174-
this.method = method == null? "GET" : method;
175-
this.userHeaders = other.userHeaders;
187+
this.method = method == null ? "GET" : method;
188+
HttpHeaders userHeaders = redirectHeadersFilter.isPresent() ?
189+
HttpHeaders.of(other.userHeaders.map(), redirectHeadersFilter.get()) : other.userHeaders;
190+
this.userHeaders = userHeaders;
176191
this.isWebSocket = other.isWebSocket;
177192
this.systemHeadersBuilder = new HttpHeadersBuilder();
178193
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
@@ -192,6 +192,18 @@ private static Set<String> getDisallowedHeaders() {
192192
public static final BiPredicate<String, String>
193193
ALLOWED_HEADERS = (header, unused) -> !DISALLOWED_HEADERS_SET.contains(header);
194194

195+
private static final Set<String> DISALLOWED_REDIRECT_HEADERS_SET = getDisallowedRedirectHeaders();
196+
197+
private static Set<String> getDisallowedRedirectHeaders() {
198+
Set<String> headers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
199+
headers.addAll(Set.of("Authorization", "Cookie", "Origin", "Referer", "Host"));
200+
201+
return Collections.unmodifiableSet(headers);
202+
}
203+
204+
public static final BiPredicate<String, String>
205+
ALLOWED_REDIRECT_HEADERS = (header, unused) -> !DISALLOWED_REDIRECT_HEADERS_SET.contains(header);
206+
195207
public static final BiPredicate<String, String> VALIDATE_USER_HEADER =
196208
(name, value) -> {
197209
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)