Skip to content

Commit

Permalink
[apache#4461]For netty changed add maxFormBufferedBytes & maxFormFiel…
Browse files Browse the repository at this point in the history
…ds (apache#4464)
  • Loading branch information
liubao68 authored Aug 9, 2024
1 parent eb1bb1f commit f3dc998
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void testRestTransport() throws Exception {
testFormRequestFail();
// testFormRequestFail会关闭连接,防止下个测试用例失败,睡眠2s
Thread.sleep(2000);
testFormRequestBufferSize();
testFormRequestSuccess();
}

Expand Down Expand Up @@ -86,4 +87,28 @@ private void testFormRequestFail() throws Exception {
TestMgr.check(e.getMessage().contains("Internal Server Error"), true);
}
}

private void testFormRequestBufferSize() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
StringBuilder stringBuffer = new StringBuilder();
for (int i = 0; i < 1020; i++) {
stringBuffer.append("a");
}
formData.add("F0123456789001234567890012345678900123456789001234567890"
+ "0123456789001234567890012345678900123456789001234567890", String.valueOf(stringBuffer)
);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
try {
// we can not test a situation for form exceed max buffer size, because the netty buffer is very
// big and the trunc can always be decoded and cached buffer size is always 0.
ResponseEntity<String> responseEntity =
restTemplate.postForEntity("cse://jaxrs/form/formLongName", requestEntity, String.class);
TestMgr.check(responseEntity.getBody(), "formRequest success : 1020");
} catch (Throwable e) {
LOGGER.error("testFormRequestBufferSize-->", e);
TestMgr.failed("", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ public String formRequestSuccess(@FormParam("formData") String formData) throws
return "formRequest success : " + formData.length();
}

@Path("/formLongName")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String formLongName(@FormParam("F0123456789001234567890012345678900123456789001234567890"
+ "0123456789001234567890012345678900123456789001234567890") String formData) throws Exception {
return "formRequest success : " + formData.length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ servicecomb:
rest:
address: 0.0.0.0:8080
server:
maxFormAttributeSize: 1024
maxFormAttributeSize: 1024 # for testing, and bigger than netty buffer allocator
maxFormBufferedBytes: 100
highway:
address: 0.0.0.0:7070
handler:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,15 @@ public BHandler(RoutingContext context, long contentLength) {

context.request().exceptionHandler(t -> {
context.cancelAndCleanupFileUploads();
int sc = 200;
if (t instanceof DecoderException) {
// bad request
context.fail(400, t.getCause());
} else {
context.fail(t);
sc = 400;
if (t.getCause() != null) {
t = t.getCause();
}
}
context.fail(sc, t);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ private HttpServerOptions createDefaultHttpServerOptions() {
serverOptions.setCompressionSupported(TransportConfig.getCompressed());
serverOptions.setMaxHeaderSize(TransportConfig.getMaxHeaderSize());
serverOptions.setMaxFormAttributeSize(TransportConfig.getMaxFormAttributeSize());
serverOptions.setMaxFormFields(TransportConfig.getMaxFormFields());
serverOptions.setMaxFormBufferedBytes(TransportConfig.getMaxFormBufferedBytes());
serverOptions.setCompressionLevel(TransportConfig.getCompressionLevel());
serverOptions.setMaxChunkSize(TransportConfig.getMaxChunkSize());
serverOptions.setDecompressionSupported(TransportConfig.getDecompressionSupported());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public static int getMaxFormAttributeSize() {
HttpServerOptions.DEFAULT_MAX_FORM_ATTRIBUTE_SIZE).get();
}

public static int getMaxFormFields() {
return DynamicPropertyFactory.getInstance()
.getIntProperty("servicecomb.rest.server.maxFormFields",
HttpServerOptions.DEFAULT_MAX_FORM_FIELDS).get();
}

public static int getMaxFormBufferedBytes() {
return DynamicPropertyFactory.getInstance()
.getIntProperty("servicecomb.rest.server.maxFormBufferedBytes",
HttpServerOptions.DEFAULT_MAX_FORM_BUFFERED_SIZE).get();
}

public static int getCompressionLevel() {
return DynamicPropertyFactory.getInstance()
.getIntProperty("servicecomb.rest.server.compressionLevel",
Expand Down

0 comments on commit f3dc998

Please sign in to comment.