Skip to content

proposal: x/net/http2: distinguish no body from empty body #53894

@shreyas44

Description

@shreyas44

Issue

A server cannot differentiate between the below two scenarios:

  1. No body was sent - The final header frame of the h2 request contains the END_STREAM and END_HEADERS flag and no data frame was sent after that
  2. An empty body was sent - The final header frame of the h2 request only contains the END_HEADERS flag. Subsequently, an empty data frame with an END_STREAM flag is sent.

When reading the body on http.Request, both immediately return io.EOF and 0 bytes are read.

Proposal

Create http2.NoBody and http2.EmptyBody to represent the two scenarios and set the req.Body to them. http2.NoBody can equal http.NoBody as well.

Use Case

Some servers differentiate between the two kinds of requests. We're running a MITM proxy that needs to read which kind of request a client is making and forward the same to the target server.

Alternatives Considered

Add a method to http.Request (named ReadDataFrame?) that returns whether or not a client sent a data frame.

Personally, I prefer adding http2.NoBody and http2.EmptyBody.

Additional Notes

  • When req.Body is set to nil or http.NoBody, no data frame is sent by http2.Transport. If it's set to another reader that returns 0 bytes (ex: ioutil.NopCloser(strings.NewReader(""))), then an empty data frame is sent. This gives a way to differentiate between the two scenarios when sending requests but not when accepting requests through http.Server or http2.Server

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Hold

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions