Skip to content

Commit bf41b95

Browse files
committed
cleanup, testing.
1 parent 17715d4 commit bf41b95

File tree

5 files changed

+26
-42
lines changed

5 files changed

+26
-42
lines changed

src/main/java/io/fusionauth/http/server/io/HTTPInputStream.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,6 @@ private void initialize() throws IOException {
168168
Long contentLength = request.getContentLength();
169169
boolean hasBody = (contentLength != null && contentLength > 0) || request.isChunked();
170170

171-
// Request order of operations:
172-
// - "hello world" -> compress -> chunked.
173-
174-
// Current:
175-
// Chunked:
176-
// HTTPInputStream > Chunked > Pushback > Throughput > Socket
177-
// Fixed:
178-
// HTTPInputStream > Pushback > Throughput > Socket
179-
180-
// New:
181-
// Chunked
182-
// HTTPInputStream > Chunked > Pushback > Decompress > Throughput > Socket
183-
// Fixed
184-
// HTTPInputStream > Pushback > Decompress > Throughput > Socket
185-
186171
// Note that isChunked() should take precedence over the fact that we have a Content-Length.
187172
// - The client should not send both, but in the case they are both present we ignore Content-Length
188173
if (!hasBody) {
@@ -210,14 +195,16 @@ private void initialize() throws IOException {
210195
throw new ContentTooLargeException(maximumContentLength, detailedMessage);
211196
}
212197

213-
// The way it is currently coded
214-
// HTTPInputStream (this) > Decompress > Fixed > Pushback > Throughput > Socket
198+
// Current state
199+
// Chunked HTTPInputStream (this) > Chunked > Pushback > Throughput > Socket
200+
// Fixed length HTTPInputStream (this) > Fixed > Pushback > Throughput > Socket
215201

216-
// HTTPInputStream (this) > Chunked > Pushback > Throughput > Socket
217-
// > HTTPInputStream (this) > Decompress > Chunked > Pushback > Throughput > Socket
202+
// When decompressing, the result will be something like this:
203+
// Chunked HTTPInputStream (this) > Decompress > Chunked > Pushback > Throughput > Socket
204+
// Fixed length HTTPInputStream (this) > Decompress> Fixed > Pushback > Throughput > Socket
205+
//
206+
// You may have one or more Decompress InputStreams in the above diagram.
218207

219-
// TODO : Note I could leave this alone, but when we parse the header we can lower case these values and then remove the equalsIgnoreCase here?
220-
// Seems like ideally we would normalize them to lowercase earlier.
221208
if (hasBody) {
222209
// The request may contain more than one value, apply in reverse order.
223210
// - These are both using the default 512 buffer size.

src/test/java/io/fusionauth/http/BaseSocketTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void assertResponse(String request, String chunkedExtension, String resp
6767

6868
if (request.contains("Transfer-Encoding: chunked")) {
6969
// Chunk in 100 byte increments. Using a smaller chunk size to ensure we don't end up with a single chunk.
70-
body = chunkItUp(body, 100, chunkedExtension);
70+
body = new String(chunkEncoded(body.getBytes(StandardCharsets.UTF_8), 100, chunkedExtension));
7171
}
7272

7373
request = request.replace("{body}", body);

src/test/java/io/fusionauth/http/BaseTest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@
4949
import java.time.Instant;
5050
import java.time.ZonedDateTime;
5151
import java.time.format.DateTimeFormatter;
52-
import java.util.ArrayList;
5352
import java.util.Arrays;
5453
import java.util.Date;
55-
import java.util.List;
5654
import java.util.Map;
5755
import java.util.UUID;
5856
import java.util.stream.Collectors;
@@ -436,25 +434,29 @@ protected void assertHTTPResponseEquals(Socket socket, String expectedResponse)
436434
}
437435
}
438436

439-
protected String chunkItUp(String body, int chunkSize, String chunkedExtension) {
440-
List<String> result = new ArrayList<>();
441-
for (var i = 0; i < body.length(); i += chunkSize) {
442-
var endIndex = Math.min(i + chunkSize, body.length());
443-
var chunk = body.substring(i, endIndex);
444-
var chunkLength = chunk.getBytes(StandardCharsets.UTF_8).length;
437+
protected byte[] chunkEncoded(byte[] bytes, int chunkSize, String chunkedExtension) throws IOException {
438+
ByteArrayOutputStream out = new ByteArrayOutputStream();
439+
for (var i = 0; i < bytes.length; i += chunkSize) {
440+
var endIndex = Math.min(i + chunkSize, bytes.length);
441+
442+
var chunk = Arrays.copyOfRange(bytes, i, endIndex);
443+
var chunkLength = chunk.length;
445444

446445
String hex = Integer.toHexString(chunkLength);
447-
result.add(hex);
446+
out.write(hex.getBytes(StandardCharsets.UTF_8));
448447

449448
if (chunkedExtension != null) {
450-
result.add(chunkedExtension);
449+
out.write(chunkedExtension.getBytes(StandardCharsets.UTF_8));
451450
}
452451

453-
result.add(("\r\n" + chunk + "\r\n"));
452+
out.write("\r\n".getBytes(StandardCharsets.UTF_8));
453+
out.write(chunk);
454+
out.write("\r\n".getBytes(StandardCharsets.UTF_8));
454455
}
455456

456-
result.add(("0\r\n\r\n"));
457-
return String.join("", result);
457+
458+
out.write(("0\r\n\r\n".getBytes(StandardCharsets.UTF_8)));
459+
return out.toByteArray();
458460
}
459461

460462
protected byte[] deflate(byte[] bytes) throws Exception {

src/test/java/io/fusionauth/http/FormDataTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public Builder expectResponse(String response) throws Exception {
286286

287287
// Convert body to chunked
288288
// - Using a small chunk to ensure we end up with more than one chunk.
289-
body = chunkItUp(body, 100, null);
289+
body = new String(chunkEncoded(body.getBytes(StandardCharsets.UTF_8), 100, null));
290290
} else {
291291
var contentLength = body.getBytes(StandardCharsets.UTF_8).length;
292292
if (contentLength > 0) {

src/test/java/io/fusionauth/http/io/HTTPInputStreamTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ public void read_chunked_withPushback(String contentEncoding) throws Exception {
6767
}
6868

6969
// Chunk the content, add part of the next request
70-
var temp1 = payload;
71-
var temp2 = new String(payload, StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8);
72-
assertEquals(temp1, temp2);
73-
74-
String chunked = chunkItUp(new String(payload, StandardCharsets.UTF_8), 38, null);
75-
payload = chunked.getBytes(StandardCharsets.UTF_8);
70+
payload = chunkEncoded(payload, 38, null);
7671

7772
ByteArrayOutputStream out = new ByteArrayOutputStream();
7873
out.write(payload);

0 commit comments

Comments
 (0)