Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion helper/http2/http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package http2

import (
"context"
"crypto/tls"
"fmt"
"log"
Expand Down Expand Up @@ -143,7 +144,13 @@ func (srv *Server) serveConn(conn net.Conn) error {
switch proto {
case http2.NextProtoTLS, "h2c":
defer conn.Close()
opts := http2.ServeConnOpts{Handler: srv.h1.Handler}

ctx := context.Background()
if srv.h1.ConnContext != nil {
ctx = srv.h1.ConnContext(ctx, conn)
}

opts := http2.ServeConnOpts{Context: ctx, BaseConfig: srv.h1}
srv.h2.ServeConn(conn, &opts)
return nil
case "", "http/1.0", "http/1.1":
Expand Down
11 changes: 11 additions & 0 deletions helper/http2/http2_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http2_test

import (
"context"
"errors"
"log"
"net"
Expand Down Expand Up @@ -32,6 +33,10 @@ func ExampleServer() {
}
}

type contextKey string

const connContextKey = contextKey("conn")

func TestServer_h1(t *testing.T) {
addr, server := newTestServer(t)
defer server.Close()
Expand Down Expand Up @@ -94,7 +99,13 @@ func newTestServer(t *testing.T) (addr string, server *http.Server) {

server = &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if v := r.Context().Value(connContextKey); v == nil {
t.Errorf("http.Request.Context missing connContextKey")
}
}),
ConnContext: func(ctx context.Context, conn net.Conn) context.Context {
return context.WithValue(ctx, connContextKey, struct{}{})
},
}

h2Server := h2proxy.NewServer(server, nil)
Expand Down
Loading