From 078ecb7734fbbcad8843419a928f1a2b7af9fb67 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Thu, 19 Jul 2018 10:05:21 +0800 Subject: [PATCH] add handler to cowork with net/http --- conn.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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()