Skip to content

Commit

Permalink
redacted code
Browse files Browse the repository at this point in the history
  • Loading branch information
UFA-MOROZOV committed Oct 2, 2024
1 parent 8d8bc99 commit fd7240a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 60 deletions.
23 changes: 16 additions & 7 deletions src/main/java/org/takes/rq/ChunkedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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 +70,7 @@ final class ChunkedInputStream extends InputStream {

/**
* Ctor.
*
* @param stream The raw input stream
*/
ChunkedInputStream(final InputStream stream) {
Expand Down Expand Up @@ -109,12 +110,13 @@ public int read(final byte[] buf, final int off, final int len)
if (shift == len) {
result = len;
} else {
int nextRead = this.read(buf, off + shift, len - shift);
if (nextRead < 1) {
result = shift;
} else {
result = shift + nextRead;
}
result = shift + Math.max(
this.read(
buf,
off + shift,
len - shift
), 0
);
}
}
return result;
Expand All @@ -127,6 +129,7 @@ public int read(final byte[] buf) throws IOException {

/**
* Read the CRLF terminator.
*
* @throws IOException If an IO error occurs.
*/
private void readCrlf() throws IOException {
Expand All @@ -147,6 +150,7 @@ private void readCrlf() throws IOException {

/**
* Read the next chunk.
*
* @throws IOException If an IO error occurs.
*/
private void nextChunk() throws IOException {
Expand All @@ -165,6 +169,7 @@ private void nextChunk() throws IOException {
* Expects the stream to start with a chunksize in hex with optional
* comments after a semicolon. The line must end with a CRLF: "a3; some
* comment\r\n" Positions the stream at the start of the next line.
*
* @param stream The new input stream.
* @return The chunk size as integer
* @throws IOException when the chunk size could not be parsed
Expand Down Expand Up @@ -225,6 +230,7 @@ private enum State {

/**
* Extract line with chunk size from stream.
*
* @param stream Input stream.
* @return Line with chunk size.
* @throws IOException If fails.
Expand All @@ -241,6 +247,7 @@ private static ByteArrayOutputStream sizeLine(final InputStream stream)

/**
* Get next state for FSM.
*
* @param stream Input stream.
* @param state Current state.
* @param line Current chunk size line.
Expand Down Expand Up @@ -282,6 +289,7 @@ private static State next(final InputStream stream, final State state,

/**
* Maintain next symbol for current state = State.NORMAL.
*
* @param state Current state.
* @param line Current chunk size line.
* @param next Next symbol.
Expand All @@ -307,6 +315,7 @@ private static State nextNormal(final State state,

/**
* Maintain next symbol for current state = State.QUOTED_STRING.
*
* @param stream Input stream.
* @param state Current state.
* @param line Current chunk size line.
Expand Down
112 changes: 59 additions & 53 deletions src/test/java/org/takes/rq/ChunkedInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ void readsOneChunk() throws IOException {
final String data = "1234567890abcdef";
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
)
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
length,
data,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
);
final byte[] buf = new byte[data.length()];
MatcherAssert.assertThat(
stream.read(buf),
Matchers.equalTo(data.length())
stream.read(buf),
Matchers.equalTo(data.length())
);
MatcherAssert.assertThat(buf, Matchers.equalTo(data.getBytes()));
MatcherAssert.assertThat(stream.available(), Matchers.equalTo(0));
Expand All @@ -83,25 +83,25 @@ void readsManyChunks() throws IOException {
final String data = first + second + third;
final Integer length = data.length();
final InputStream stream = new ChunkedInputStream(
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
Integer.toHexString(first.length()),
first,
Integer.toHexString(second.length()),
second,
Integer.toHexString(third.length()),
third,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
Integer.toHexString(first.length()),
first,
Integer.toHexString(second.length()),
second,
Integer.toHexString(third.length()),
third,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
);
final byte[] buf = new byte[length];
MatcherAssert.assertThat(
stream.read(buf),
Matchers.equalTo(length)
stream.read(buf),
Matchers.equalTo(length)
);
MatcherAssert.assertThat(buf, Matchers.equalTo(data.getBytes()));
MatcherAssert.assertThat(stream.available(), Matchers.equalTo(0));
Expand All @@ -114,46 +114,52 @@ void ignoresParameterAfterSemiColon() throws IOException {
final String ignored = ";ignored-stuff";
final String length = Integer.toHexString(data.length());
final InputStream stream = new ChunkedInputStream(
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
length + ignored,
data,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
length + ignored,
data,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
);
final byte[] buf = new byte[data.length()];
MatcherAssert.assertThat(
stream.read(buf),
Matchers.equalTo(data.length())
stream.read(buf),
Matchers.equalTo(data.length())
);
MatcherAssert.assertThat(buf, Matchers.equalTo(data.getBytes()));
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
)
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
length,
data,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
);
final byte[] buf = new byte[data.length() + 10];
final int bytesRead = stream.read(buf);
MatcherAssert.assertThat(bytesRead, Matchers.equalTo(data.length()));
MatcherAssert.assertThat(buf, Matchers.equalTo((data + new String(new byte[10])).getBytes()));
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();
}
Expand Down

0 comments on commit fd7240a

Please sign in to comment.