How to send Response in Expect:100-continue Request

I want to return “too large body” errorresponse whenever a request’s body is larger than setting wirh echo.
I have created the original body size checking middleware and it works generally well.
The middleware performs “Content-Length” check and “io.CopyN” check.

However, when a request has an “Expect:100-continue” header and “Content-Length” header disguised as a small value, the middleware does not work well and the connection is broken before the response is received by the client.
That is, if a request with an Expect header passes the length check and fails the read check, the connection will be broken.

I assume this is because the server does not wait for the client to disconnect the connection after sending the response with “Connection: close” header.
It is possible to wait for the connection to be disconnected by hijacking the response. However, I want to keep the code to a minimum.

What should we do? Also, is the cause I am assuming correct?