Skip to content

Commit 922aaf3

Browse files
authored
[java] JdkHttpClient - Downgrade the method for a 303 redirect (#12070)
Downgrade the method for a 303 redirect
1 parent 94303e0 commit 922aaf3

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.openqa.selenium.remote.http.CloseMessage;
5656
import org.openqa.selenium.remote.http.HttpClient;
5757
import org.openqa.selenium.remote.http.HttpClientName;
58+
import org.openqa.selenium.remote.http.HttpMethod;
5859
import org.openqa.selenium.remote.http.HttpRequest;
5960
import org.openqa.selenium.remote.http.HttpResponse;
6061
import org.openqa.selenium.remote.http.Message;
@@ -357,14 +358,15 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
357358

358359
BodyHandler<byte[]> byteHandler = BodyHandlers.ofByteArray();
359360
try {
361+
HttpMethod method = req.getMethod();
360362
URI rawUri = messages.getRawUri(req);
361363

362364
// We need a custom handling of redirects to:
363365
// - increase the maximum number of retries to 100
364366
// - avoid a downgrade of POST requests, see the javadoc of j.n.h.HttpClient.Redirect
365367
// - not run into https://bugs.openjdk.org/browse/JDK-8304701
366368
for (int i = 0; i < 100; i++) {
367-
java.net.http.HttpRequest request = messages.createRequest(req, rawUri);
369+
java.net.http.HttpRequest request = messages.createRequest(req, method, rawUri);
368370
java.net.http.HttpResponse<byte[]> response;
369371

370372
// use sendAsync to not run into https://bugs.openjdk.org/browse/JDK-8258397
@@ -393,9 +395,11 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
393395
}
394396

395397
switch (response.statusCode()) {
398+
case 303:
399+
method = HttpMethod.GET;
400+
// fall-through
396401
case 301:
397402
case 302:
398-
case 303:
399403
case 307:
400404
case 308:
401405
URI location =

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpMessages.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.openqa.selenium.remote.http.AddSeleniumUserAgent;
3232
import org.openqa.selenium.remote.http.ClientConfig;
3333
import org.openqa.selenium.remote.http.Contents;
34+
import org.openqa.selenium.remote.http.HttpMethod;
3435
import org.openqa.selenium.remote.http.HttpRequest;
3536
import org.openqa.selenium.remote.http.HttpResponse;
3637

@@ -44,7 +45,7 @@ public JdkHttpMessages(ClientConfig config) {
4445
this.config = Objects.requireNonNull(config, "Client config");
4546
}
4647

47-
public java.net.http.HttpRequest createRequest(HttpRequest req, URI rawUri) {
48+
public java.net.http.HttpRequest createRequest(HttpRequest req, HttpMethod method, URI rawUri) {
4849
String rawUrl = rawUri.toString();
4950

5051
// Add query string if necessary
@@ -69,7 +70,7 @@ public java.net.http.HttpRequest createRequest(HttpRequest req, URI rawUri) {
6970
java.net.http.HttpRequest.Builder builder =
7071
java.net.http.HttpRequest.newBuilder().uri(URI.create(rawUrl));
7172

72-
switch (req.getMethod()) {
73+
switch (method) {
7374
case DELETE:
7475
builder = builder.DELETE();
7576
break;

0 commit comments

Comments
 (0)