net: golang.org/x/net/http2/h2c Index | Examples | Files

package h2c

import "golang.org/x/net/http2/h2c"

Package h2c implements the unencrypted "h2c" form of HTTP/2.

The h2c protocol is the non-TLS version of HTTP/2 which is not available from net/http or golang.org/x/net/http2.

Index

Examples

Package Files

h2c.go

func NewHandler Uses

func NewHandler(h http.Handler, s *http2.Server) http.Handler

NewHandler returns an http.Handler that wraps h, intercepting any h2c traffic. If a request is an h2c connection, it's hijacked and redirected to s.ServeConn. Otherwise the returned Handler just forwards requests to h. This works because h2c is designed to be parseable as valid HTTP/1, but ignored by any HTTP server that does not handle h2c. Therefore we leverage the HTTP/1 compatible parts of the Go http library to parse and recognize h2c requests. Once a request is recognized as h2c, we hijack the connection and convert it to an HTTP/2 connection which is understandable to s.ServeConn. (s.ServeConn understands HTTP/2 except for the h2c part of it.)

Code:

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello world")
})
h2s := &http2.Server{
    // ...
}
h1s := &http.Server{
    Addr:    ":8080",
    Handler: NewHandler(handler, h2s),
}
log.Fatal(h1s.ListenAndServe())

Package h2c imports 16 packages (graph) and is imported by 35 packages. Updated 2019-10-22. Refresh now. Tools for package owners.