Skip to content

[1.9.x] Sometimes multipart body missed last 1 byte. #1271

Closed
@carryel

Description

@carryel

MultipartBody#read reads multipart body and writes packets in a given ByteBuffer.

For examples,

If ByteBuffer's size is 8192 and remaining multipart body's size is 8193, last 1 byte(\n) will be missed.

In MultipartBody.java,

    private boolean currentBytesFullyRead() {
        return currentBytes == null || currentBytesPosition >= currentBytes.length - 1;
    }

Maybe currentBytes should be written in ByteBuffer and currentBytesPosition means the positon of current bytes after writing.

And it seems that currentBytesFullyRead() means whether currentBytes are written in ByteBuffer totally or not.

Actually "currentBytesPosition == currentBytes.length - 1" means the last byte of currentBytes should be written in next ByteBuffer later.

But currentBytesFullyRead returns true, so currentBytes, currentBytesPosition and transferDone will be reset(missing one byte, '\n').

Please see the next source code:

In MultipartBody.java,

public long read(ByteBuffer buffer) throws IOException {
    ...
            if (doneWritingParts) {
                if (currentBytesPosition == -1) {
                    initializeCurrentBytes(MultipartUtils.getMessageEnd(boundary));
                }

                if (currentBytesPosition > -1) {
                    overallLength += writeCurrentBytes(buffer, maxLength - overallLength);

                    if (currentBytesFullyRead()) {
                        // reset and missed 1 bytes.
                        currentBytes = null;
                        currentBytesPosition = -1;
                        transfertDone = true;
                    }
                }
            }
    ...

So I think MultipartBody#currentBytesFullyRead will be fixed like below:

    private boolean currentBytesFullyRead() {
        return currentBytes == null || currentBytesPosition == -1;
    }

Sometimes I met one missing packet at the upload server when I used AHC with multipart.

When I test it again with the proposed patch, I can find the problem is resolved.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions