diff --git a/conn.go b/conn.go index 0dd5090..c2e8726 100644 --- a/conn.go +++ b/conn.go @@ -7,6 +7,7 @@ package nghttp2 import "C" import ( "bytes" + "crypto/tls" "errors" "fmt" "io" @@ -80,6 +81,27 @@ func Client(c net.Conn) (*Conn, error) { return conn, nil } +// HTTP2Handler is the http2 server handler that can co-work with standard net/http. +// +// usage example: +// l, err := net.Listen("tcp", ":1222") +// srv := &http.Server{ +// TLSConfig: &tls.Config{ +// NextProtos:[]string{"h2", "http/1.1"}, +// } +// TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){ +// "h2": nghttp2.Http2Handler +// } +// } +// srv.ServeTLS(l, "server.crt", "server.key") +func HTTP2Handler(srv *http.Server, conn *tls.Conn, handler http.Handler) { + h2conn, err := Server(conn, handler) + if err != nil { + panic(err.Error()) + } + h2conn.Run() +} + // Error return conn error func (c *Conn) Error() error { c.lock.Lock()