请求正文过大,导致Go中的连接重置

I have a simple multipart form which uploads to a Go app. I wanted to set a restriction on the upload size, so I did the following:

func myHandler(rw http.ResponseWriter, request *http.Request){  
    request.Body = http.MaxBytesReader(rw, request.Body, 1024)
    err := request.ParseMultipartForm(1024)
    if err != nil{
    // Some response.
    } 
}  

Whenever an upload exceeds the maximum size, I get a connection reset like the following: enter image description here

and yet the code continues executing. I can't seem to provide any feedback to the user. Instead of severing the connection I'd prefer to say "You've exceeded the size limit". Is this possible?

This code works as intended. Description of http.MaxBytesReader

MaxBytesReader is similar to io.LimitReader but is intended for limiting the size of incoming request bodies. In contrast to io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a non-EOF error for a Read beyond the limit, and closes the underlying reader when its Close method is called.

MaxBytesReader prevents clients from accidentally or maliciously sending a large request and wasting server resources.

You could use io.LimitReader to read just N bytes and then do the handling of the HTTP request on your own.

The only way to force a client to stop sending data is to forcefully close the connection, which is what you're doing with http.MaxBytesReader.

You could use a io.LimitReader wrapped in a ioutil.NopCloser, and notify the client of the error state. You could then check for more data, and try and drain the connection up to another limit to keep it open. However, clients that aren't responding correctly to MaxBytesReader may not work in this case either.

The graceful way to handle something like this is using Expect: 100-continue, but that only really applies to clients other than web browsers.