Skip to content

Use original method in redirects for HEAD / OPTIONS requests #1736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static org.asynchttpclient.util.HttpConstants.Methods.GET;
import static org.asynchttpclient.util.HttpConstants.Methods.HEAD;
import static org.asynchttpclient.util.HttpConstants.Methods.OPTIONS;
import static org.asynchttpclient.util.HttpConstants.ResponseStatusCodes.*;
import static org.asynchttpclient.util.HttpUtils.followRedirect;
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
Expand Down Expand Up @@ -87,7 +89,7 @@ public boolean exitAfterHandlingRedirect(Channel channel,

String originalMethod = request.getMethod();
boolean switchToGet = !originalMethod.equals(GET)
&& (statusCode == MOVED_PERMANENTLY_301 || statusCode == SEE_OTHER_303 || (statusCode == FOUND_302 && !config.isStrict302Handling()));
&& !originalMethod.equals(OPTIONS) && !originalMethod.equals(HEAD) && (statusCode == MOVED_PERMANENTLY_301 || statusCode == SEE_OTHER_303 || (statusCode == FOUND_302 && !config.isStrict302Handling()));
boolean keepBody = statusCode == TEMPORARY_REDIRECT_307 || statusCode == PERMANENT_REDIRECT_308 || (statusCode == FOUND_302 && config.isStrict302Handling());

final RequestBuilder requestBuilder = new RequestBuilder(switchToGet ? GET : originalMethod)
Expand Down Expand Up @@ -126,7 +128,6 @@ else if (isNonEmpty(request.getBodyParts())) {
HttpHeaders responseHeaders = response.headers();
String location = responseHeaders.get(LOCATION);
Uri newUri = Uri.create(future.getUri(), location);

LOGGER.debug("Redirecting to {}", newUri);

CookieStore cookieStore = config.getCookieStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ public Integer onCompleted() {
}));
}

// This test is flaky - see https://github.com/AsyncHttpClient/async-http-client/issues/1728#issuecomment-699962325
// For now, just run again if fails
@Test(groups = "online")
public void asyncOptionsTest() throws Throwable {

Expand Down
14 changes: 11 additions & 3 deletions client/src/test/java/org/asynchttpclient/Head302Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,17 @@ public Response onCompleted(Response response) throws Exception {
private static class Head302handler extends AbstractHandler {
public void handle(String s, org.eclipse.jetty.server.Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if ("HEAD".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_FOUND); // 302
response.setHeader("Location", request.getPathInfo() + "_moved");
} else if ("GET".equalsIgnoreCase(request.getMethod())) {
// See https://github.com/AsyncHttpClient/async-http-client/issues/1728#issuecomment-700007980
// When setFollowRedirect == TRUE, a follow-up request to a HEAD request will also be a HEAD.
// This will cause an infinite loop, which will error out once the maximum amount of redirects is hit (default 5).
// Instead, we (arbitrarily) choose to allow for 3 redirects and then return a 200.
if(request.getRequestURI().endsWith("_moved_moved_moved")) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.setStatus(HttpServletResponse.SC_FOUND); // 302
response.setHeader("Location", request.getPathInfo() + "_moved");
}
} else if ("GET".equalsIgnoreCase(request.getMethod()) ) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
Expand Down