x/net/http2: expose streams as net.Conn #26574
Closed
Labels
Milestone
Comments
You already can: Client: Server: e.g. type flushWriter struct {
w io.Writer
}
func (fw flushWriter) Write(p []byte) (n int, err error) {
n, err = fw.w.Write(p)
if f, ok := fw.w.(http.Flusher); ok {
f.Flush()
}
return
}
func echoCapitalHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
http.Error(w, "PUT required.", 400)
return
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
io.Copy(flushWriter{w}, capitalizeReader{r.Body})
} What you describe is basically what https://www.backplane.io/ does (also in Go, but closed source). |
Anything to do here or can we close this? |
Thanks! I'll give it a go and reopen if anything is missing. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I’d like to have a way to expose individual streams as a net.Conn so I can implement a proxy that keeps a persistent HTTP/2 tunnel to an endpoint, receives HTTP/1 connections, and sends them to the remote endpoint through the HTTP/2 tunnel. In this way I can share 1 tcp connection with several streams.
Background
I’m trying to implement something like this:
So E1 would be a proxy server that keeps a connection to E2. Ideally, clients would send a regular CONNECT request to E1 and then the rest of the connection would be relayed through the HTTP/2 tunnel to E2 as HTTP/2 streams.
In regular proxies, I’d open a new TCP connection to E2 per
CONNECT
request and then just copy data betweenreq.Body
and the new TCP connection and vice-versa. In this case I want the HTTP/2 connection to be established already and just use a new stream perCONNECT
request.I tried implementing a custom
ClientConnectionPool
so I can get aClientConn
still in the pool but that only exposesRoundTrip()
which I’m not sure how to use in this case: I have ther.Body
from the client but how do I create a new Request with it so it gets sent viaRoundTrip()
? I saw this #17227 (comment) saying this feature would be kinda useless but I’m not sure I understand how to apply that to my use case, maybe I’m missing something obvious.The text was updated successfully, but these errors were encountered: