using http.ResponseWriter makes *http.Request unavailable?

1,108 views
Skip to first unread message

Peter Kleiweg

unread,
Oct 21, 2011, 2:21:42 PM10/21/11
to golan...@googlegroups.com

I am writing a Go application with Google App Engine.

I have a function that handles a POST, which uploads a data file.

This is part of the function:

func upload(w http.ResponseWriter, r *http.Request) {

f, _, e := r.FormFile("data")

fmt.Fprintf(w, "%c", 0xfeff)

// more code

}

This works fine.

But then I change it into this:

func upload(w http.ResponseWriter, r *http.Request) {

fmt.Fprintf(w, "%c", 0xfeff)

f, _, e := r.FormFile("data")

// more code

}

Now I get an error if I run the code:

multipart: NextPart: http: invalid Read on closed request Body

What is going on here?


--
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/

Brad Fitzpatrick

unread,
Oct 21, 2011, 4:09:28 PM10/21/11
to Peter Kleiweg, golan...@googlegroups.com
Yes.  You must consume the request before writing the response.  Once you start writing your response, you can't read more of the request any more.

The App Engine behavior is intentional, in order to match the behavior of Go's stand-alone HTTP server.

There were deadlocks before this behavior was implemented:   a client sending a large request body would block until their request was sent, not reading the response from the server.  But if the server was blocked writing the response, it wouldn't consume more of the request.

I figure we could relax the behavior in the future if anybody can definitively tell me what is correct.  RFC 2616 doesn't seem to say either way, or I can't find it.
Reply all
Reply to author
Forward
0 new messages