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

Only cap the input if content length header is given #1302

Merged
merged 1 commit into from
Aug 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
23 changes: 17 additions & 6 deletions src/main/java/org/takes/rq/RqLengthAware.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
*/
@EqualsAndHashCode(callSuper = true)
public final class RqLengthAware extends RqWrap {
/**
* Constant for the Content-Length header.
*/
private static final String CONTENT_LENGTH = "Content-Length";

/**
* Ctor.
Expand All @@ -68,14 +72,21 @@ public RqLengthAware(final Request req) {
*/
private static InputStream cap(final Request req) throws IOException {
final Iterator<String> hdr = new RqHeaders.Base(req)
.header("Content-Length").iterator();
InputStream body = req.body();
long length = (long) body.available();
.header(RqLengthAware.CONTENT_LENGTH).iterator();
final InputStream result;
if (hdr.hasNext()) {
length = Long.parseLong(hdr.next());
final String value = hdr.next();
try {
result = new CapInputStream(req.body(), Long.parseLong(value));
} catch (final NumberFormatException ex) {
final String msg = "Invalid %s header: %s";
final String formated = String.format(msg, RqLengthAware.CONTENT_LENGTH, value);
throw new IOException(formated, ex);
}
} else {
result = req.body();
}
body = new CapInputStream(body, length);
return body;
return result;
}

}
28 changes: 28 additions & 0 deletions src/test/java/org/takes/rq/RqLengthAwareTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.takes.rq;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
Expand Down Expand Up @@ -96,6 +98,32 @@ void readsByte() throws IOException {
);
}

@Test
void noContentLength() throws IOException {
final byte[] bytes = "test".getBytes();
final InputStream data = new FilterInputStream(new ByteArrayInputStream(bytes)) {
@Override
public int available() throws IOException {
return 1;
}
};
final InputStream stream = new RqLengthAware(
new RqFake(
Arrays.asList(
"GET /test1",
"Host: b.example.com"
),
data
)
).body();
for (final byte element : bytes) {
MatcherAssert.assertThat(
stream.read(),
Matchers.equalTo(element & 0xFF)
);
}
}

@Test
void readsByteArray() throws IOException {
final String data = "array";
Expand Down
Loading