// like encodeHeader, but don't add implicit psuedo headers. func encodeHeaderNoImplicit(t *testing.T, headers ...string) []byte { var buf bytes.Buffer enc := hpack.NewEncoder(&buf) for len(headers) > 0 { k, v := headers[0], headers[1] headers = headers[2:] if err := enc.WriteField(hpack.HeaderField{Name: k, Value: v}); err != nil { t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err) } } return buf.Bytes() }
// newHTTP2Server constructs a ServerTransport based on HTTP2. ConnectionError is // returned if something goes wrong. func newHTTP2Server(conn net.Conn, maxStreams uint32) (_ ServerTransport, err error) { framer := newFramer(conn) // Send initial settings as connection preface to client. // TODO(zhaoq): Have a better way to signal "no limit" because 0 is // permitted in the HTTP2 spec. var settings []http2.Setting // TODO(zhaoq): Have a better way to signal "no limit" because 0 is // permitted in the HTTP2 spec. if maxStreams == 0 { maxStreams = math.MaxUint32 } else { settings = append(settings, http2.Setting{http2.SettingMaxConcurrentStreams, maxStreams}) } if initialWindowSize != defaultWindowSize { settings = append(settings, http2.Setting{http2.SettingInitialWindowSize, uint32(initialWindowSize)}) } if err := framer.writeSettings(true, settings...); err != nil { return nil, ConnectionErrorf("transport: %v", err) } // Adjust the connection flow control window if needed. if delta := uint32(initialConnWindowSize - defaultWindowSize); delta > 0 { if err := framer.writeWindowUpdate(true, 0, delta); err != nil { return nil, ConnectionErrorf("transport: %v", err) } } var buf bytes.Buffer t := &http2Server{ conn: conn, framer: framer, hBuf: &buf, hEnc: hpack.NewEncoder(&buf), maxStreams: maxStreams, controlBuf: newRecvBuffer(), fc: &inFlow{limit: initialConnWindowSize}, sendQuotaPool: newQuotaPool(defaultWindowSize), state: reachable, writableChan: make(chan int, 1), shutdownChan: make(chan struct{}), activeStreams: make(map[uint32]*Stream), streamSendQuota: defaultWindowSize, } go t.controller() t.writableChan <- 0 return t, nil }
// newHTTP2Client constructs a connected ClientTransport to addr based on HTTP2 // and starts to receive messages on it. Non-nil error returns if construction // fails. func newHTTP2Client(addr string, opts *ConnectOptions) (_ ClientTransport, err error) { if opts.Dialer == nil { // Set the default Dialer. opts.Dialer = func(addr string, timeout time.Duration) (net.Conn, error) { return net.DialTimeout("tcp", addr, timeout) } } scheme := "http" startT := time.Now() timeout := opts.Timeout conn, connErr := opts.Dialer(addr, timeout) if connErr != nil { return nil, ConnectionErrorf("transport: %v", connErr) } for _, c := range opts.AuthOptions { if ccreds, ok := c.(credentials.TransportAuthenticator); ok { scheme = "https" // TODO(zhaoq): Now the first TransportAuthenticator is used if there are // multiple ones provided. Revisit this if it is not appropriate. Probably // place the ClientTransport construction into a separate function to make // things clear. if timeout > 0 { timeout -= time.Since(startT) } conn, connErr = ccreds.ClientHandshake(addr, conn, timeout) break } } if connErr != nil { return nil, ConnectionErrorf("transport: %v", connErr) } defer func() { if err != nil { conn.Close() } }() // Send connection preface to server. n, err := conn.Write(clientPreface) if err != nil { return nil, ConnectionErrorf("transport: %v", err) } if n != len(clientPreface) { return nil, ConnectionErrorf("transport: preface mismatch, wrote %d bytes; want %d", n, len(clientPreface)) } framer := newFramer(conn) if initialWindowSize != defaultWindowSize { err = framer.writeSettings(true, http2.Setting{http2.SettingInitialWindowSize, uint32(initialWindowSize)}) } else { err = framer.writeSettings(true) } if err != nil { return nil, ConnectionErrorf("transport: %v", err) } // Adjust the connection flow control window if needed. if delta := uint32(initialConnWindowSize - defaultWindowSize); delta > 0 { if err := framer.writeWindowUpdate(true, 0, delta); err != nil { return nil, ConnectionErrorf("transport: %v", err) } } var buf bytes.Buffer t := &http2Client{ target: addr, conn: conn, // The client initiated stream id is odd starting from 1. nextID: 1, writableChan: make(chan int, 1), shutdownChan: make(chan struct{}), errorChan: make(chan struct{}), framer: framer, hBuf: &buf, hEnc: hpack.NewEncoder(&buf), controlBuf: newRecvBuffer(), fc: &inFlow{limit: initialConnWindowSize}, sendQuotaPool: newQuotaPool(defaultWindowSize), scheme: scheme, state: reachable, activeStreams: make(map[uint32]*Stream), maxStreams: math.MaxUint32, authCreds: opts.AuthOptions, streamSendQuota: defaultWindowSize, } go t.controller() t.writableChan <- 0 // Start the reader goroutine for incoming message. The threading model // on receiving is that each transport has a dedicated goroutine which // reads HTTP2 frame from network. Then it dispatches the frame to the // corresponding stream entity. go t.reader() return t, nil }