|
9 | 9 | package http2 |
10 | 10 |
|
11 | 11 | import ( |
| 12 | + "context" |
12 | 13 | "errors" |
13 | 14 | "net" |
14 | 15 | "net/http" |
| 16 | + "sync" |
| 17 | + "time" |
15 | 18 | ) |
16 | 19 |
|
17 | 20 | type serverInternalState struct { |
| 21 | + s1 *http.Server |
| 22 | + initOnce sync.Once |
| 23 | + serveConnFunc func(context.Context, net.Conn, http.Handler, bool, *http.Request, []byte) |
18 | 24 | } |
19 | 25 |
|
20 | 26 | func configureServer(s *http.Server, conf *Server) error { |
21 | | - return errors.New("TODO") |
| 27 | + if s == nil { |
| 28 | + panic("nil *http.Server") |
| 29 | + } |
| 30 | + if conf == nil { |
| 31 | + conf = new(Server) |
| 32 | + } |
| 33 | + if conf.state != nil { |
| 34 | + // This isn't a panic in the pre-wrapping implementation, |
| 35 | + // but calling ConfigureServer twice with the same http2.Server |
| 36 | + // overwrites internal state on the server. |
| 37 | + // Make the error explicit and early here. |
| 38 | + panic("ConfigureServer may be called only once per Server") |
| 39 | + } |
| 40 | + if h1, h2 := s, conf; h2.IdleTimeout == 0 { |
| 41 | + if h1.IdleTimeout != 0 { |
| 42 | + h2.IdleTimeout = h1.IdleTimeout |
| 43 | + } else { |
| 44 | + h2.IdleTimeout = h1.ReadTimeout |
| 45 | + } |
| 46 | + } |
| 47 | + conf.state = &serverInternalState{ |
| 48 | + s1: s, |
| 49 | + } |
| 50 | + sconfig := &serverConfig{s: conf} |
| 51 | + if err := s.Serve(sconfig); err != nil || sconfig.serveConnFunc == nil { |
| 52 | + panic("http2: net/http does not support this version of x/net/http2") |
| 53 | + } |
| 54 | + conf.state.serveConnFunc = sconfig.serveConnFunc |
| 55 | + return nil |
22 | 56 | } |
23 | 57 |
|
24 | | -func (s *Server) serveConn(c net.Conn, opts *ServeConnOpts, newf func(*ServerConn)) { |
25 | | - c.Close() // TODO |
| 58 | +type serverConfig struct { |
| 59 | + s *Server |
| 60 | + serveConnFunc func(context.Context, net.Conn, http.Handler, bool, *http.Request, []byte) |
| 61 | +} |
| 62 | + |
| 63 | +func (*serverConfig) Accept() (net.Conn, error) { |
| 64 | + return nil, errors.New("unexpected call to Accept") |
| 65 | +} |
| 66 | +func (*serverConfig) Close() error { |
| 67 | + return nil |
| 68 | +} |
| 69 | +func (*serverConfig) Addr() net.Addr { |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (s *serverConfig) ServeConnFunc(f func(context.Context, net.Conn, http.Handler, bool, *http.Request, []byte)) { |
| 74 | + s.serveConnFunc = f |
| 75 | +} |
| 76 | + |
| 77 | +func (s *serverConfig) HTTP2Config() http.HTTP2Config { |
| 78 | + return http.HTTP2Config{ |
| 79 | + MaxConcurrentStreams: int(s.s.MaxConcurrentStreams), |
| 80 | + MaxDecoderHeaderTableSize: int(s.s.MaxDecoderHeaderTableSize), |
| 81 | + MaxEncoderHeaderTableSize: int(s.s.MaxEncoderHeaderTableSize), |
| 82 | + MaxReadFrameSize: int(s.s.MaxReadFrameSize), |
| 83 | + PermitProhibitedCipherSuites: s.s.PermitProhibitedCipherSuites, |
| 84 | + MaxReceiveBufferPerConnection: int(s.s.MaxUploadBufferPerConnection), |
| 85 | + MaxReceiveBufferPerStream: int(s.s.MaxUploadBufferPerStream), |
| 86 | + SendPingTimeout: s.s.ReadIdleTimeout, |
| 87 | + PingTimeout: s.s.PingTimeout, |
| 88 | + WriteByteTimeout: s.s.WriteByteTimeout, |
| 89 | + CountError: s.s.CountError, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func (s *serverConfig) IdleTimeout() time.Duration { |
| 94 | + return s.s.IdleTimeout |
| 95 | +} |
| 96 | + |
| 97 | +type serverConn struct{} |
| 98 | + |
| 99 | +func (s *Server) serveConn(c net.Conn, opts *ServeConnOpts, _ func(*serverConn)) { |
| 100 | + var serveConnFunc func(context.Context, net.Conn, http.Handler, bool, *http.Request, []byte) |
| 101 | + switch { |
| 102 | + case opts.BaseConfig != nil: |
| 103 | + // The user has provided us with an http.Server to take configuration from. |
| 104 | + // |
| 105 | + // We can't send our request to opts.BaseConfig, because an http.Server can |
| 106 | + // only be associated with a single http2.Server and the user might |
| 107 | + // use this one with several http.Servers. |
| 108 | + // |
| 109 | + // We can't send our request to s.state.s1, because it doesn't contain |
| 110 | + // the right configuration. |
| 111 | + // |
| 112 | + // So create a one-off copy of opts.BaseConfig and use it. |
| 113 | + h1 := &http.Server{ |
| 114 | + TLSConfig: opts.BaseConfig.TLSConfig, |
| 115 | + ReadTimeout: opts.BaseConfig.ReadTimeout, |
| 116 | + ReadHeaderTimeout: opts.BaseConfig.ReadHeaderTimeout, |
| 117 | + WriteTimeout: opts.BaseConfig.WriteTimeout, |
| 118 | + IdleTimeout: opts.BaseConfig.IdleTimeout, |
| 119 | + MaxHeaderBytes: opts.BaseConfig.MaxHeaderBytes, |
| 120 | + ConnState: opts.BaseConfig.ConnState, |
| 121 | + ErrorLog: opts.BaseConfig.ErrorLog, |
| 122 | + BaseContext: opts.BaseConfig.BaseContext, |
| 123 | + ConnContext: opts.BaseConfig.ConnContext, |
| 124 | + HTTP2: opts.BaseConfig.HTTP2, |
| 125 | + } |
| 126 | + sconfig := &serverConfig{s: s} |
| 127 | + if err := h1.Serve(sconfig); err != nil || sconfig.serveConnFunc == nil { |
| 128 | + panic("http2: net/http does not support this version of x/net/http2") |
| 129 | + } |
| 130 | + serveConnFunc = sconfig.serveConnFunc |
| 131 | + case s.state != nil: |
| 132 | + serveConnFunc = s.state.serveConnFunc |
| 133 | + default: |
| 134 | + // Strange-but-true: Server has no concurrency-safe way to initialize |
| 135 | + // its internal state, so historically ServeConn just doesn't use any |
| 136 | + // persistent state if you don't call ConfigureServer first. |
| 137 | + // |
| 138 | + // If ConfigureServer hasn't been called, create a one-off http.Server |
| 139 | + // for the connection, since we don't have any way to keep one around for reuse. |
| 140 | + h1 := &http.Server{} |
| 141 | + sconfig := &serverConfig{s: s} |
| 142 | + if err := h1.Serve(sconfig); err != nil || sconfig.serveConnFunc == nil { |
| 143 | + panic("http2: net/http does not support this version of x/net/http2") |
| 144 | + } |
| 145 | + serveConnFunc = sconfig.serveConnFunc |
| 146 | + } |
| 147 | + |
| 148 | + ctx, cancel := serverConnBaseContext(c, opts) |
| 149 | + defer cancel() |
| 150 | + serveConnFunc(ctx, c, opts.handler(), opts.SawClientPreface, opts.UpgradeRequest, opts.Settings) |
| 151 | + |
26 | 152 | } |
27 | 153 |
|
28 | 154 | // FrameWriteRequest is a request to write a frame. |
|
0 commit comments