-
Notifications
You must be signed in to change notification settings - Fork 1.6k
#857 Support multipart files using InputStream from source file #1593
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
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
049ee8d
857 Support multipart files using InputStream from source file
puresam 1a6c522
Added explicit constructor for -1 contentLength
puresam 806670f
Add unknown-content-length coverage for large file uploads using Inpu…
puresam 7ab6f45
Remove zero-copy implementation for InputStreamPart
puresam 0a1e490
Update README.md to document lack of support for zero-copy with Input…
puresam db29e79
Revert "Update README.md to document lack of support for zero-copy wi…
puresam 7f5c687
Revert "Remove zero-copy implementation for InputStreamPart"
puresam ec1f32f
Use BodyChunkedInput in contentLength < 0 in NettyBodyBody
puresam 28b3cf8
Add license header to new files
puresam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
client/src/main/java/org/asynchttpclient/request/body/multipart/InputStreamPart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.asynchttpclient.request.body.multipart; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.Charset; | ||
|
||
import static org.asynchttpclient.util.Assertions.assertNotNull; | ||
|
||
public class InputStreamPart extends FileLikePart { | ||
|
||
private final InputStream inputStream; | ||
private final long contentLength; | ||
|
||
public InputStreamPart(String name, InputStream inputStream, String fileName) { | ||
this(name, inputStream, fileName, -1); | ||
} | ||
|
||
public InputStreamPart(String name, InputStream inputStream, String fileName, long contentLength) { | ||
this(name, inputStream, fileName, contentLength, null); | ||
} | ||
|
||
public InputStreamPart(String name, InputStream inputStream, String fileName, long contentLength, String contentType) { | ||
this(name, inputStream, fileName, contentLength, contentType, null); | ||
} | ||
|
||
public InputStreamPart(String name, InputStream inputStream, String fileName, long contentLength, String contentType, Charset charset) { | ||
this(name, inputStream, fileName, contentLength, contentType, charset, null); | ||
} | ||
|
||
public InputStreamPart(String name, InputStream inputStream, String fileName, long contentLength, String contentType, Charset charset, | ||
String contentId) { | ||
this(name, inputStream, fileName, contentLength, contentType, charset, contentId, null); | ||
} | ||
|
||
public InputStreamPart(String name, InputStream inputStream, String fileName, long contentLength, String contentType, Charset charset, | ||
String contentId, String transferEncoding) { | ||
super(name, | ||
contentType, | ||
charset, | ||
fileName, | ||
contentId, | ||
transferEncoding); | ||
this.inputStream = assertNotNull(inputStream, "inputStream"); | ||
this.contentLength = contentLength; | ||
} | ||
|
||
public InputStream getInputStream() { | ||
return inputStream; | ||
} | ||
|
||
public long getContentLength() { | ||
return contentLength; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...c/main/java/org/asynchttpclient/request/body/multipart/part/InputStreamMultipartPart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.asynchttpclient.request.body.multipart.part; | ||
slandelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import io.netty.buffer.ByteBuf; | ||
import org.asynchttpclient.request.body.multipart.InputStreamPart; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
import static org.asynchttpclient.util.MiscUtils.closeSilently; | ||
|
||
public class InputStreamMultipartPart extends FileLikeMultipartPart<InputStreamPart> { | ||
|
||
private long position = 0L; | ||
|
||
public InputStreamMultipartPart(InputStreamPart part, byte[] boundary) { | ||
super(part, boundary); | ||
} | ||
|
||
@Override | ||
protected long getContentLength() { | ||
return part.getContentLength(); | ||
} | ||
|
||
@Override | ||
protected long transferContentTo(ByteBuf target) throws IOException { | ||
InputStream inputStream = part.getInputStream(); | ||
int transferred = target.writeBytes(inputStream, target.writableBytes()); | ||
if (transferred > 0) { | ||
position += transferred; | ||
} | ||
if (position == getContentLength() || transferred < 0) { | ||
state = MultipartState.POST_CONTENT; | ||
inputStream.close(); | ||
} | ||
return transferred; | ||
} | ||
|
||
@Override | ||
protected long transferContentTo(WritableByteChannel target) throws IOException { | ||
throw new UnsupportedOperationException("InputStreamPart does not support zero-copy transfers"); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
super.close(); | ||
closeSilently(part.getInputStream()); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
client/src/test/java/org/asynchttpclient/request/body/InputStreamPartLargeFileTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
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. Please fix header: Sonatype is no longer involved in this project and new code should be donated to AHC. 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. Fixed |
||
package org.asynchttpclient.request.body; | ||
|
||
import org.asynchttpclient.AbstractBasicTest; | ||
import org.asynchttpclient.AsyncHttpClient; | ||
import org.asynchttpclient.Response; | ||
import org.asynchttpclient.request.body.multipart.InputStreamPart; | ||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.handler.AbstractHandler; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.*; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.asynchttpclient.Dsl.asyncHttpClient; | ||
import static org.asynchttpclient.Dsl.config; | ||
import static org.asynchttpclient.test.TestUtils.LARGE_IMAGE_FILE; | ||
import static org.asynchttpclient.test.TestUtils.createTempFile; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class InputStreamPartLargeFileTest extends AbstractBasicTest { | ||
|
||
@Override | ||
public AbstractHandler configureHandler() throws Exception { | ||
return new AbstractHandler() { | ||
|
||
public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
|
||
ServletInputStream in = req.getInputStream(); | ||
byte[] b = new byte[8192]; | ||
|
||
int count; | ||
int total = 0; | ||
while ((count = in.read(b)) != -1) { | ||
b = new byte[8192]; | ||
total += count; | ||
} | ||
resp.setStatus(200); | ||
resp.addHeader("X-TRANSFERRED", String.valueOf(total)); | ||
resp.getOutputStream().flush(); | ||
resp.getOutputStream().close(); | ||
|
||
baseRequest.setHandled(true); | ||
} | ||
}; | ||
} | ||
|
||
@Test(expectedExceptions = ExecutionException.class) | ||
public void testPutImageFileThrowsExecutionException() throws Exception { | ||
// Should throw ExecutionException when zero-copy is enabled | ||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); | ||
client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), LARGE_IMAGE_FILE.length(), "application/octet-stream", UTF_8)).execute().get(); | ||
} | ||
} | ||
|
||
@Test(expectedExceptions = ExecutionException.class) | ||
public void testPutImageFileUnknownSizeThrowsExecutionException() throws Exception { | ||
// Should throw ExecutionException when zero-copy is enabled | ||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); | ||
client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPutImageFile() throws Exception { | ||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000).setDisableZeroCopy(true))) { | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); | ||
client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), LARGE_IMAGE_FILE.length(), "application/octet-stream", UTF_8)).execute().get(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPutImageFileUnknownSize() throws Exception { | ||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000).setDisableZeroCopy(true))) { | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); | ||
Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); | ||
assertEquals(response.getStatusCode(), 200); | ||
} | ||
} | ||
|
||
@Test(expectedExceptions = ExecutionException.class) | ||
public void testPutLargeTextFileThrowsExecutionException() throws Exception { | ||
File file = createTempFile(1024 * 1024); | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); | ||
|
||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { | ||
Response response = client.preparePut(getTargetUrl()) | ||
.addBodyPart(new InputStreamPart("test", inputStream, file.getName(), file.length(), "application/octet-stream", UTF_8)).execute().get(); | ||
assertEquals(response.getStatusCode(), 200); | ||
} | ||
} | ||
|
||
@Test(expectedExceptions = ExecutionException.class) | ||
public void testPutLargeTextFileUnknownSizeThrowsExecutionException() throws Exception { | ||
File file = createTempFile(1024 * 1024); | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); | ||
|
||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { | ||
Response response = client.preparePut(getTargetUrl()) | ||
.addBodyPart(new InputStreamPart("test", inputStream, file.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); | ||
assertEquals(response.getStatusCode(), 200); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPutLargeTextFile() throws Exception { | ||
File file = createTempFile(1024 * 1024); | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); | ||
|
||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000).setDisableZeroCopy(true))) { | ||
Response response = client.preparePut(getTargetUrl()) | ||
.addBodyPart(new InputStreamPart("test", inputStream, file.getName(), file.length(), "application/octet-stream", UTF_8)).execute().get(); | ||
assertEquals(response.getStatusCode(), 200); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPutLargeTextFileUnknownSize() throws Exception { | ||
File file = createTempFile(1024 * 1024); | ||
InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); | ||
|
||
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000).setDisableZeroCopy(true))) { | ||
Response response = client.preparePut(getTargetUrl()) | ||
.addBodyPart(new InputStreamPart("test", inputStream, file.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); | ||
assertEquals(response.getStatusCode(), 200); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.