Skip to content

Commit

Permalink
Support empty part in DefaultPartHttpMessageReader
Browse files Browse the repository at this point in the history
This commit fixes a bug in DefaultPartHttpMessageReader's
MultipartParser, due to which the last token in a part window was not
properly indicated.

Closes gh-30953
  • Loading branch information
poutsma committed Dec 6, 2023
1 parent 2e3d133 commit ef4ffa0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ else if (len < 0) {
while ((prev = this.queue.pollLast()) != null) {
int prevByteCount = prev.readableByteCount();
int prevLen = prevByteCount + len;
if (prevLen > 0) {
if (prevLen >= 0) {
// slice body part of previous buffer, and flush it
DataBuffer body = prev.split(prevLen + prev.readPosition());
DataBufferUtils.release(prev);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ void exceedHeaderLimit() throws InterruptedException {
latch.await();
}

@ParameterizedDefaultPartHttpMessageReaderTest
void emptyLastPart(DefaultPartHttpMessageReader reader) throws InterruptedException {
MockServerHttpRequest request = createRequest(
new ClassPathResource("empty-part.multipart", getClass()), "LiG0chJ0k7YtLt-FzTklYFgz50i88xJCW5jD");

Flux<Part> result = reader.read(forClass(Part.class), request, emptyMap());

CountDownLatch latch = new CountDownLatch(2);
StepVerifier.create(result)
.consumeNextWith(part -> testPart(part, null, "", latch))
.consumeNextWith(part -> testPart(part, null, "", latch))
.verifyComplete();

latch.await();
}


private void testBrowser(DefaultPartHttpMessageReader reader, Resource resource, String boundary)
throws InterruptedException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--LiG0chJ0k7YtLt-FzTklYFgz50i88xJCW5jD
Content-Disposition: form-data; name="files"; filename="file17312898095703516893.tmp"
Content-Type: application/octet-stream
Content-Length: 0


--LiG0chJ0k7YtLt-FzTklYFgz50i88xJCW5jD
Content-Disposition: form-data; name="files"; filename="file14790463448453253614.tmp"
Content-Type: application/octet-stream
Content-Length: 0


--LiG0chJ0k7YtLt-FzTklYFgz50i88xJCW5jD--
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,19 @@ public Mono<ServerResponse> partData(ServerRequest request) {
assertThat(data).hasSize(2);

List<PartEvent> fileData = data.get(0);
assertThat(fileData).hasSize(1);
assertThat(fileData).hasSize(2);
assertThat(fileData).element(0).isInstanceOf(FilePartEvent.class);
FilePartEvent filePartEvent = (FilePartEvent) fileData.get(0);
assertThat(filePartEvent.name()).isEqualTo("fooPart");
assertThat(filePartEvent.filename()).isEqualTo("foo.txt");
DataBufferUtils.release(filePartEvent.content());

assertThat(fileData).element(1).isInstanceOf(FilePartEvent.class);
filePartEvent = (FilePartEvent) fileData.get(1);
assertThat(filePartEvent.name()).isEqualTo("fooPart");
assertThat(filePartEvent.filename()).isEqualTo("foo.txt");
DataBufferUtils.release(filePartEvent.content());

List<PartEvent> fieldData = data.get(1);
assertThat(fieldData).hasSize(1);
assertThat(fieldData).element(0).isInstanceOf(FormPartEvent.class);
Expand Down

0 comments on commit ef4ffa0

Please sign in to comment.