mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
e8382ece65
Signed-off-by: Tibor Vass <tibor@docker.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package grpc // import "github.com/docker/docker/api/server/router/grpc"
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/net/http2"
|
|
)
|
|
|
|
func (gr *grpcRouter) serveGRPC(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
h, ok := w.(http.Hijacker)
|
|
if !ok {
|
|
return errors.New("handler does not support hijack")
|
|
}
|
|
proto := r.Header.Get("Upgrade")
|
|
if proto == "" {
|
|
return errors.New("no upgrade proto in request")
|
|
}
|
|
if proto != "h2c" {
|
|
return errors.Errorf("protocol %s not supported", proto)
|
|
}
|
|
|
|
conn, _, err := h.Hijack()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp := &http.Response{
|
|
StatusCode: http.StatusSwitchingProtocols,
|
|
ProtoMajor: 1,
|
|
ProtoMinor: 1,
|
|
Header: http.Header{},
|
|
}
|
|
resp.Header.Set("Connection", "Upgrade")
|
|
resp.Header.Set("Upgrade", proto)
|
|
|
|
// set raw mode
|
|
conn.Write([]byte{})
|
|
resp.Write(conn)
|
|
|
|
// https://godoc.org/golang.org/x/net/http2#Server.ServeConn
|
|
// TODO: is it a problem that conn has already been written to?
|
|
gr.h2Server.ServeConn(conn, &http2.ServeConnOpts{Handler: gr.grpcServer})
|
|
return nil
|
|
}
|