Skip to content
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

Issue 1303 solved. #1330

Merged
merged 4 commits into from
Oct 2, 2024
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
12 changes: 9 additions & 3 deletions src/main/java/org/takes/rq/ChunkedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@

/**
* Input stream from chunked coded http request body.
*
* @since 0.31.2
* @link <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1">Chunked Transfer Coding</a>
* @since 0.31.2
*/
final class ChunkedInputStream extends InputStream {

Expand Down Expand Up @@ -70,6 +69,7 @@ final class ChunkedInputStream extends InputStream {

/**
* Ctor.
*
* @param stream The raw input stream
*/
ChunkedInputStream(final InputStream stream) {
Expand Down Expand Up @@ -109,7 +109,13 @@ public int read(final byte[] buf, final int off, final int len)
if (shift == len) {
result = len;
} else {
result = shift + this.read(buf, off + shift, len - shift);
result = shift + Math.max(
this.read(
buf,
off + shift,
len - shift
), 0
);
}
}
return result;
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/takes/rq/ChunkedInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,33 @@ void ignoresParameterAfterSemiColon() throws IOException {
MatcherAssert.assertThat(stream.available(), Matchers.equalTo(0));
stream.close();
}

@Test
void readsWithLenGreaterThanTotalSize() throws IOException {
final String data = "Hello, World!";
final String length = Integer.toHexString(data.length());
final InputStream stream = new ChunkedInputStream(
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
length,
data,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
);
final byte[] buf = new byte[data.length() + 10];
MatcherAssert.assertThat(
stream.read(buf),
Matchers.equalTo(data.length())
);
MatcherAssert.assertThat(
buf,
Matchers.equalTo((data + new String(new byte[10])).getBytes())
);
MatcherAssert.assertThat(stream.available(), Matchers.equalTo(0));
stream.close();
}
}
Loading