Skip to content

http client: chunked request body writes the chunk size on FIN and the trailing CRLF on INIT #2650

Description

@jfallows

Describe the bug

In HttpClientFactory.doEncodeHttp1Body, a chunked HTTP/1.1 request body is framed per data frame, with the chunk size line written when the FIN flag is set and the trailing CRLF written when the INIT flag is set:

if (exchange.requestChunked && flags != NONE)
{
    int chunkLimit = 0;

    if (hasFin(flags))
    {
        // chunk size and CRLF
    }

    // payload

    if (hasInit(flags))
    {
        // trailing CRLF
    }
}

For a message carried in a single frame (both INIT and FIN set) the result is a correct chunk: size line, payload, CRLF. For a message that spans several frames the first frame (INIT only) is written as payload followed by CRLF with no size line, and the last frame (FIN only) is written as a size line and payload with no trailing CRLF. The size line also describes only the last fragment rather than the whole message.

To Reproduce

Found by reading the code, not reproduced. A reproduction would be a chunked request whose body message is delivered to the client binding in more than one data frame, then inspecting the bytes written to the network.

Expected behavior

Each chunk is framed once: the size line before the first fragment's payload and the trailing CRLF after the last fragment's payload, or equivalently one chunk per frame with its own size.

Additional context

Noticed while replacing the per-module flag constants with a shared helper. The literal tests (flags & 0x01 before the size line, flags & 0x02 before the trailing CRLF) are unchanged in behavior by that change, which is why this is filed separately.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions