net/http: document explicitly that http2 doesn't support Hijacker #15312
When I upgraded to go1.6 (Linux amd64) our applications, which rely heavily on Server-sent events, stopped working because *http2responseWriter does not implement http.Hijacker (#14797) (tried Firefox only).
I'm not using any SSE package, see code below (slightly edited), and it worked fine for 4 years until go1.6.
As I said in the referenced issue, I think there is a problem with the documentation because I relied on a feature that stopped working without warning.
EDIT: I realize I can't expect my code to work across http versions if I hardcode the version myself, but even if the version was correct it doesn't reach past the type assertion. How should I go about rewriting this without hijacking?
The docs on https://golang.org/pkg/net/http/#Hijacker do already say "The Hijacker interface is implemented by ResponseWriters that allow an HTTP handler to take over the connection", which is trying to say that not all ResponseWriters support that. If they did, it would just be part of the ResponseWriter interface.
To do this without hijack, just write to your ResponseWriter and use https://golang.org/pkg/net/http/#Flusher instead. Both http1 and http2 support Flusher (but you should still test for it and conditionally use it). There's no need to use a buffered writer. The http1 and http2 response writers are already buffered. (and any unbuffered one wouldn't need a flush)
changed the title
Server-sent events implemented using http hijacking is broken by http2 without warning
Apr 15, 2016
Thanks, your solution works fine!
About the docs I understand. I'm not saying that it is incorrect, I'm saying that it could be more useful.
I understand that use cases for Hijacker are limited but I'm glad you decided to document it, thanks.