File: grpc_routes.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (45 lines) | stat: -rw-r--r-- 1,121 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
}