Skip to content

Commit 3f3c003

Browse files
committed
Working
1 parent ac88e5a commit 3f3c003

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,7 @@ private void assertResponse(String request, String chunkedExtension, String resp
6666
var body = bodyString.repeat(((requestBufferSize / bodyString.length())) * 2);
6767

6868
if (request.contains("Transfer-Encoding: chunked")) {
69-
//noinspection ExtractMethodRecommender
70-
var result = "";
71-
// Chunk in 100 byte increments. Using a smaller chunk size to ensure we don't end up with a single chunk.
72-
int chunkSize = 100;
73-
for (var i = 0; i < body.length(); i += chunkSize) {
74-
var endIndex = Math.min(i + chunkSize, body.length());
75-
var chunk = body.substring(i, endIndex);
76-
var chunkLength = chunk.getBytes(StandardCharsets.UTF_8).length;
77-
String hex = Integer.toHexString(chunkLength);
78-
//noinspection StringConcatenationInLoop
79-
result += hex;
80-
81-
if (chunkedExtension != null) {
82-
result += chunkedExtension;
83-
}
84-
85-
result += ("\r\n" + chunk + "\r\n");
86-
}
87-
body = result + "0\r\n\r\n";
69+
body = chunkItUp(body, chunkedExtension);
8870
}
8971

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

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
import java.time.Instant;
4848
import java.time.ZonedDateTime;
4949
import java.time.format.DateTimeFormatter;
50+
import java.util.ArrayList;
5051
import java.util.Arrays;
5152
import java.util.Date;
53+
import java.util.List;
5254
import java.util.Map;
5355
import java.util.UUID;
5456
import java.util.stream.Collectors;
@@ -328,6 +330,19 @@ public Object[][] schemes() {
328330
};
329331
}
330332

333+
/**
334+
* @return The possible schemes - {@code http} and {@code https} and chunked.
335+
*/
336+
@DataProvider
337+
public Object[][] schemesAndChunked() {
338+
return new Object[][]{
339+
{"http", true},
340+
{"http", false},
341+
{"https", true},
342+
{"https", false}
343+
};
344+
}
345+
331346
/**
332347
* @return The possible response buffer lengths and schemes.
333348
*/
@@ -415,6 +430,29 @@ protected void assertHTTPResponseEquals(Socket socket, String expectedResponse)
415430
}
416431
}
417432

433+
protected String chunkItUp(String body, String chunkedExtension) {
434+
List<String> result = new ArrayList<>();
435+
// Chunk in 100 byte increments. Using a smaller chunk size to ensure we don't end up with a single chunk.
436+
int chunkSize = 100;
437+
for (var i = 0; i < body.length(); i += chunkSize) {
438+
var endIndex = Math.min(i + chunkSize, body.length());
439+
var chunk = body.substring(i, endIndex);
440+
var chunkLength = chunk.getBytes(StandardCharsets.UTF_8).length;
441+
442+
String hex = Integer.toHexString(chunkLength);
443+
result.add(hex);
444+
445+
if (chunkedExtension != null) {
446+
result.add(chunkedExtension);
447+
}
448+
449+
result.add(("\r\n" + chunk + "\r\n"));
450+
}
451+
452+
result.add(("0\r\n\r\n"));
453+
return String.join("", result);
454+
}
455+
418456
protected void printf(String format, Object... args) {
419457
if (verbose) {
420458
System.out.printf(SystemOutPrefix + format, args);

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@ public class FormDataTest extends BaseTest {
4848
"""
4949
.replaceAll("\\s", "");
5050

51-
@Test(dataProvider = "schemes")
52-
public void post_server_configuration_max_form_data(String scheme) throws Exception {
53-
// Fixed length. n, n is < max length, Ok.
54-
// Fixed length. n, n > too big
55-
// Not tested yet: Chunked, too big.
56-
51+
@Test(dataProvider = "schemesAndChunked")
52+
public void post_server_configuration_max_form_data(String scheme, boolean chunked) throws Exception {
5753
// File too big even though the overall request size is ok.
5854
withScheme(scheme)
55+
.withChunked(chunked)
5956
.withBodyParameterCount(4096)
6057
.withBodyParameterSize(32)
6158
// Body is [180,223]
@@ -75,7 +72,8 @@ public void post_server_configuration_max_form_data(String scheme) throws Except
7572

7673
// Exceeded
7774
withScheme(scheme)
78-
.withBodyParameterCount(42 * 1024) // 131,072, 131,072
75+
.withChunked(chunked)
76+
.withBodyParameterCount(42 * 1024) // 43,008
7977
.withBodyParameterSize(128)
8078
// 4k * 33 > 128k
8179
.withConfiguration(config -> config.withMaxFormDataSize(128 * 1024))
@@ -89,6 +87,7 @@ public void post_server_configuration_max_form_data(String scheme) throws Except
8987

9088
// Large, but max size has been disabled
9189
withScheme(scheme)
90+
.withChunked(chunked)
9291
.withBodyParameterCount(42 * 1024) // 131,072, 131,072
9392
.withBodyParameterSize(128)
9493
// Disable the limit
@@ -155,7 +154,6 @@ public void post_server_configuration_max_request_header_size(String scheme) thr
155154
\r
156155
{"version":"42"}""")
157156
.expectNoExceptionOnWrite();
158-
159157
}
160158

161159
private Builder withScheme(String scheme) {
@@ -168,6 +166,8 @@ private class Builder {
168166

169167
private int bodyParameterSize = 42;
170168

169+
private boolean chunked;
170+
171171
private Consumer<HTTPServerConfiguration> configuration;
172172

173173
private int headerCount;
@@ -257,11 +257,20 @@ public Builder expectResponse(String response) throws Exception {
257257
body += String.join("&", bodyParameters);
258258
}
259259

260-
var contentLength = body.getBytes(StandardCharsets.UTF_8).length;
261-
if (contentLength > 0) {
260+
if (chunked) {
262261
request += """
263-
Content-Length: {contentLength}\r
264-
""".replace("{contentLength}", contentLength + "");
262+
Transfer-Encoding: chunked\r
263+
""";
264+
265+
// Convert body to chunked
266+
body = chunkItUp(body, null);
267+
} else {
268+
var contentLength = body.getBytes(StandardCharsets.UTF_8).length;
269+
if (contentLength > 0) {
270+
request += """
271+
Content-Length: {contentLength}\r
272+
""".replace("{contentLength}", contentLength + "");
273+
}
265274
}
266275

267276
List<String> headers = new ArrayList<>();
@@ -273,9 +282,7 @@ public Builder expectResponse(String response) throws Exception {
273282
request += String.join("\r\n", headers) + "\r\n";
274283
}
275284

276-
request += """
277-
\r
278-
{body}""".replace("{body}", body);
285+
request = request + "\r\n" + body;
279286

280287
var os = socket.getOutputStream();
281288
// Do our best to write, but ignore exceptions.
@@ -301,6 +308,11 @@ public Builder withBodyParameterSize(int bodyParameterSize) {
301308
return this;
302309
}
303310

311+
public Builder withChunked(boolean chunked) {
312+
this.chunked = chunked;
313+
return this;
314+
}
315+
304316
public Builder withConfiguration(Consumer<HTTPServerConfiguration> configuration) throws Exception {
305317
this.configuration = configuration;
306318
return this;

0 commit comments

Comments
 (0)