Skip to content

Client sends TransferEncoding: chunked for empty Body #1373

Closed
@brandlk

Description

@brandlk

When receiving a HTTP request without a body, a Request with an empty Body is constructed instead of a Request with None as Body. Since GET requests neither have the ContentLength nor the TransferEncoding header set (this is the correct behavious as far as I understood), the hyper implementation automatically sets TransferEncoding: chunked in case I forward this request unchanged (as for example a proxy would do). This results in some servers responding with "Bad request" (for example "example.org"), which again seems to be the correct behaviour. Some servers are apparently more tolerant and reply nevertheless.

This means by literally doing nothing (e.g. just forwarding an incoming request), hyper turns a valid request into an invalid one, which seems strange.
Of course I could inspect the body and send a request without a body in case it is empty or build a request without a body in case neither ContentLength nor TransferEnconding is set, but this feels a bit like a workaround. And additionally, a Request does not offer any method to remove a body from the request. The method body() which takes the body out of the request, just replaces the original one with an empty one and thus does not help in this case. This means I have to build a new request and copy the headers from the original one before passing it on.

Is this behaviour intended? Or is there a chance to fix it? By skimming through the implementation I figured that Body is just a wrapper around the tokio-proto Body. This struct internally distinguishes between "Empty", "Stream" and "Once" but doesn't expose this functionality. If it would, (e.g, offering a "is_empty()" method), then it would be easy to change the hyper behaviour when encoding a request. But I'm not sure if it is desired to expose this, since conceptually there is no difference between an "officially" empty body and a stream without items. So maybe this is a stupid approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-clientArea: client.C-bugCategory: bug. Something is wrong. This is bad!E-easyEffort: easy. A task that would be a great starting point for a new contributor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions