-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(netty): preserve all redirect body types #2316
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,20 +147,7 @@ public boolean exitAfterHandlingRedirect(Channel channel, NettyResponseFuture<?> | |
| } | ||
|
|
||
| if (keepBody) { | ||
| requestBuilder.setCharset(request.getCharset()); | ||
| if (isNonEmpty(request.getFormParams())) { | ||
| requestBuilder.setFormParams(request.getFormParams()); | ||
| } else if (request.getStringData() != null) { | ||
| requestBuilder.setBody(request.getStringData()); | ||
| } else if (request.getByteData() != null) { | ||
| requestBuilder.setBody(request.getByteData()); | ||
| } else if (request.getByteBufferData() != null) { | ||
| requestBuilder.setBody(request.getByteBufferData()); | ||
| } else if (request.getBodyGenerator() != null) { | ||
| requestBuilder.setBody(request.getBodyGenerator()); | ||
| } else if (isNonEmpty(request.getBodyParts())) { | ||
| requestBuilder.setBodyParts(request.getBodyParts()); | ||
| } | ||
| copyBody(requestBuilder, request); | ||
| } | ||
|
|
||
| requestBuilder.setHeaders(propagatedHeaders(request, realm, keepBody, stripAuth)); | ||
|
|
@@ -219,6 +206,35 @@ public boolean exitAfterHandlingRedirect(Channel channel, NettyResponseFuture<?> | |
| return false; | ||
| } | ||
|
|
||
| private static void copyBody(RequestBuilder requestBuilder, Request request) { | ||
| requestBuilder.setCharset(request.getCharset()); | ||
|
|
||
| // Keep this precedence aligned with NettyRequestFactory.body. A Request can retain a File or | ||
| // BodyGenerator alongside another representation, so the redirect must copy the representation | ||
| // that the original request actually sent. | ||
| if (request.getByteData() != null) { | ||
| requestBuilder.setBody(request.getByteData()); | ||
| } else if (request.getCompositeByteData() != null) { | ||
| requestBuilder.setBody(request.getCompositeByteData()); | ||
| } else if (request.getStringData() != null) { | ||
| requestBuilder.setBody(request.getStringData()); | ||
| } else if (request.getByteBufferData() != null) { | ||
| requestBuilder.setBody(request.getByteBufferData()); | ||
| } else if (request.getByteBufData() != null) { | ||
| requestBuilder.setBody(request.getByteBufData()); | ||
| } else if (request.getStreamData() != null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We used to send an empty body on the redirect leg, so this turns a silent bug into an |
||
| requestBuilder.setBody(request.getStreamData()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else if (isNonEmpty(request.getFormParams())) { | ||
| requestBuilder.setFormParams(request.getFormParams()); | ||
| } else if (isNonEmpty(request.getBodyParts())) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So for an |
||
| requestBuilder.setBodyParts(request.getBodyParts()); | ||
| } else if (request.getFile() != null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Today the Same origin is fine. Cross origin I'd like to be a deliberate decision, and same for |
||
| requestBuilder.setBody(request.getFile()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else if (request.getBodyGenerator() != null) { | ||
| requestBuilder.setBody(request.getBodyGenerator()); | ||
| } | ||
| } | ||
|
|
||
| private static HttpHeaders propagatedHeaders(Request request, Realm realm, boolean keepBody, boolean stripAuthorization) { | ||
| HttpHeaders headers = request.getHeaders() | ||
| .remove(HOST) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,25 +16,43 @@ | |
| package org.asynchttpclient; | ||
|
|
||
| import io.github.artsok.RepeatedIfExceptionsTest; | ||
| import io.netty.buffer.ByteBuf; | ||
| import io.netty.buffer.Unpooled; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.asynchttpclient.request.body.multipart.StringPart; | ||
| import org.eclipse.jetty.server.Request; | ||
| import org.eclipse.jetty.server.handler.AbstractHandler; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.FilterInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Arrays; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE; | ||
| import static io.netty.handler.codec.http.HttpHeaderNames.LOCATION; | ||
| import static org.asynchttpclient.Dsl.asyncHttpClient; | ||
| import static org.asynchttpclient.Dsl.config; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class RedirectBodyTest extends AbstractBasicTest { | ||
|
|
||
| private static final byte[] REDIRECT_BODY = "redirect body".getBytes(UTF_8); | ||
| private static final String CONTENT_TYPE_VALUE = "application/octet-stream"; | ||
|
|
||
| private static volatile boolean redirectAlreadyPerformed; | ||
| private static volatile String receivedContentType; | ||
|
|
||
|
|
@@ -50,6 +68,7 @@ public AbstractHandler configureHandler() throws Exception { | |
| @Override | ||
| public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException { | ||
|
|
||
| byte[] body = IOUtils.toByteArray(request.getInputStream()); | ||
| String redirectHeader = httpRequest.getHeader("X-REDIRECT"); | ||
| if (redirectHeader != null && !redirectAlreadyPerformed) { | ||
| redirectAlreadyPerformed = true; | ||
|
|
@@ -60,12 +79,9 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt | |
| } else { | ||
| receivedContentType = request.getContentType(); | ||
| httpResponse.setStatus(200); | ||
| int len = request.getContentLength(); | ||
| httpResponse.setContentLength(len); | ||
| if (len > 0) { | ||
| byte[] buffer = new byte[len]; | ||
| IOUtils.read(request.getInputStream(), buffer); | ||
| httpResponse.getOutputStream().write(buffer); | ||
| httpResponse.setContentLength(body.length); | ||
| if (body.length > 0) { | ||
| httpResponse.getOutputStream().write(body); | ||
| } | ||
| } | ||
| httpResponse.getOutputStream().flush(); | ||
|
|
@@ -121,4 +137,134 @@ public void regular307KeepsBody() throws Exception { | |
| assertEquals(receivedContentType, contentType); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void compositeByteArray307KeepsBody() throws Exception { | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| byte[] first = "redirect ".getBytes(UTF_8); | ||
| byte[] second = "body".getBytes(UTF_8); | ||
|
|
||
| Response response = execute307(c.preparePost(getTargetUrl()).setBody(Arrays.asList(first, second))); | ||
|
|
||
| assertRedirectBody(response); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void byteBuf307KeepsBody() throws Exception { | ||
| ByteBuf body = Unpooled.wrappedBuffer(REDIRECT_BODY); | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| Response response = execute307(c.preparePost(getTargetUrl()).setBody(body)); | ||
|
|
||
| assertRedirectBody(response); | ||
| assertEquals(1, body.refCnt(), "the caller must retain ownership of its ByteBuf"); | ||
| } finally { | ||
| body.release(); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void resettableInputStream307KeepsBody() throws Exception { | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| Response response = execute307(c.preparePost(getTargetUrl()).setBody(new ByteArrayInputStream(REDIRECT_BODY))); | ||
|
|
||
| assertRedirectBody(response); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void nonResettableInputStream307FailsPromptly() throws Exception { | ||
| InputStream body = new FilterInputStream(new ByteArrayInputStream(REDIRECT_BODY)) { | ||
| @Override | ||
| public boolean markSupported() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void reset() throws IOException { | ||
| throw new IOException("reset not supported"); | ||
| } | ||
| }; | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| ExecutionException thrown = assertThrows(ExecutionException.class, | ||
| () -> execute307(c.preparePost(getTargetUrl()).setBody(body))); | ||
|
|
||
| assertInstanceOf(IOException.class, thrown.getCause()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any channel-level failure is an |
||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void file307KeepsBody() throws Exception { | ||
| Path body = Files.createTempFile("ahc-redirect-body-", ".bin"); | ||
| try { | ||
| Files.write(body, REDIRECT_BODY); | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| Response response = execute307(c.preparePost(getTargetUrl()).setBody(body.toFile())); | ||
|
|
||
| assertRedirectBody(response); | ||
| } | ||
| } finally { | ||
| Files.deleteIfExists(body); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void coexistingFileAndByteArray308UsesByteArray() throws Exception { | ||
| Path file = Files.createTempFile("ahc-redirect-precedence-", ".bin"); | ||
| try { | ||
| Files.write(file, "wrong file body".getBytes(UTF_8)); | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| Response response = c.preparePost(getTargetUrl()) | ||
| .setBody(file.toFile()) | ||
| .setBody(REDIRECT_BODY) | ||
| .setHeader(CONTENT_TYPE, CONTENT_TYPE_VALUE) | ||
| .setHeader("X-REDIRECT", "308") | ||
| .execute() | ||
| .get(TIMEOUT, TimeUnit.SECONDS); | ||
|
|
||
| assertRedirectBody(response); | ||
| } | ||
| } finally { | ||
| Files.deleteIfExists(file); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void formParams307KeepBody() throws Exception { | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| Response response = c.preparePost(getTargetUrl()) | ||
| .addFormParam("field", "value") | ||
| .setHeader("X-REDIRECT", "307") | ||
| .execute() | ||
| .get(TIMEOUT, TimeUnit.SECONDS); | ||
|
|
||
| assertEquals("field=value", response.getResponseBody()); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void multipart307KeepsBody() throws Exception { | ||
| try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { | ||
| Response response = c.preparePost(getTargetUrl()) | ||
| .addBodyPart(new StringPart("field", "multipart value")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .setHeader("X-REDIRECT", "307") | ||
| .execute() | ||
| .get(TIMEOUT, TimeUnit.SECONDS); | ||
|
|
||
| assertTrue(response.getResponseBody().contains("multipart value")); | ||
| } | ||
| } | ||
|
|
||
| private static Response execute307(BoundRequestBuilder requestBuilder) throws Exception { | ||
| return requestBuilder | ||
| .setHeader(CONTENT_TYPE, CONTENT_TYPE_VALUE) | ||
| .setHeader("X-REDIRECT", "307") | ||
| .execute() | ||
| .get(TIMEOUT, TimeUnit.SECONDS); | ||
| } | ||
|
|
||
| private static void assertRedirectBody(Response response) { | ||
| assertArrayEquals(REDIRECT_BODY, response.getResponseBodyAsBytes()); | ||
| assertEquals(CONTENT_TYPE_VALUE, receivedContentType); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we build this from
request.toBuilder()and override method, uri, headers and realm after?RequestBuilderBasealready copies all the body fields, plus charset, rangeOffset and readTimeout.The builder above copies
setRequestTimeoutbut notsetReadTimeout, so a per-request read timeout silently falls back to the config default after the first redirect.toBuilder()would fix that too, and this wouldn't drift out of sync withNettyRequestFactory.bodyagain.