github.com/vmware/govmomi@v0.51.0/simulator/internal/server.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package internal 6 7 import ( 8 "crypto/tls" 9 "crypto/x509" 10 "fmt" 11 "log" 12 "net" 13 "net/http" 14 "strings" 15 "sync" 16 "time" 17 ) 18 19 // A Server is an HTTP server listening on a system-chosen port on the 20 // local loopback interface, for use in end-to-end HTTP tests. 21 type Server struct { 22 URL string // base URL of form http://ipaddr:port with no trailing slash 23 Listener net.Listener 24 25 // TLS is the optional TLS configuration, populated with a new config 26 // after TLS is started. If set on an unstarted server before StartTLS 27 // is called, existing fields are copied into the new config. 28 TLS *tls.Config 29 30 // Config may be changed after calling NewUnstartedServer and 31 // before Start or StartTLS. 32 Config *http.Server 33 34 // certificate is a parsed version of the TLS config certificate, if present. 35 certificate *x509.Certificate 36 37 // wg counts the number of outstanding HTTP requests on this server. 38 // Close blocks until all requests are finished. 39 wg sync.WaitGroup 40 41 mu sync.Mutex // guards closed and conns 42 closed bool 43 conns map[net.Conn]http.ConnState // except terminal states 44 45 // client is configured for use with the server. 46 // Its transport is automatically closed when Close is called. 47 client *http.Client 48 } 49 50 func newLocalListener(serve string) net.Listener { 51 if serve != "" { 52 l, err := net.Listen("tcp", serve) 53 if err != nil { 54 panic(fmt.Sprintf("httptest: failed to listen on %v: %v", serve, err)) 55 } 56 return l 57 } 58 l, err := net.Listen("tcp", "127.0.0.1:0") 59 if err != nil { 60 if l, err = net.Listen("tcp6", "[::1]:0"); err != nil { 61 panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err)) 62 } 63 } 64 return l 65 } 66 67 // NewServer starts and returns a new Server. 68 // The caller should call Close when finished, to shut it down. 69 func NewServer(handler http.Handler) *Server { 70 ts := NewUnstartedServer(handler, "") 71 ts.Start() 72 return ts 73 } 74 75 // NewUnstartedServer returns a new Server but doesn't start it. 76 // 77 // After changing its configuration, the caller should call Start or 78 // StartTLS. 79 // 80 // The caller should call Close when finished, to shut it down. 81 // serve allows the server's listen address to be specified. 82 func NewUnstartedServer(handler http.Handler, serve string) *Server { 83 return &Server{ 84 Listener: newLocalListener(serve), 85 Config: &http.Server{Handler: handler}, 86 } 87 } 88 89 // Start starts a server from NewUnstartedServer. 90 func (s *Server) Start() { 91 if s.URL != "" { 92 panic("Server already started") 93 } 94 if s.client == nil { 95 s.client = &http.Client{Transport: &http.Transport{}} 96 } 97 s.URL = "http://" + s.Listener.Addr().String() 98 s.wrap() 99 s.goServe() 100 } 101 102 // StartTLS starts TLS on a server from NewUnstartedServer. 103 func (s *Server) StartTLS() { 104 if s.URL != "" { 105 panic("Server already started") 106 } 107 if s.client == nil { 108 s.client = &http.Client{Transport: &http.Transport{}} 109 } 110 cert, err := tls.X509KeyPair(LocalhostCert, LocalhostKey) 111 if err != nil { 112 panic(fmt.Sprintf("httptest: NewTLSServer: %v", err)) 113 } 114 115 existingConfig := s.TLS 116 if existingConfig != nil { 117 s.TLS = existingConfig.Clone() 118 } else { 119 s.TLS = new(tls.Config) 120 } 121 if s.TLS.NextProtos == nil { 122 s.TLS.NextProtos = []string{"http/1.1"} 123 } 124 if len(s.TLS.Certificates) == 0 { 125 s.TLS.Certificates = []tls.Certificate{cert} 126 } 127 s.certificate, err = x509.ParseCertificate(s.TLS.Certificates[0].Certificate[0]) 128 if err != nil { 129 panic(fmt.Sprintf("httptest: NewTLSServer: %v", err)) 130 } 131 certpool := x509.NewCertPool() 132 certpool.AddCert(s.certificate) 133 s.client.Transport = &http.Transport{ 134 TLSClientConfig: &tls.Config{ 135 RootCAs: certpool, 136 }, 137 } 138 s.Listener = tls.NewListener(s.Listener, s.TLS) 139 s.URL = "https://" + s.Listener.Addr().String() 140 s.wrap() 141 s.goServe() 142 } 143 144 // NewTLSServer starts and returns a new Server using TLS. 145 // The caller should call Close when finished, to shut it down. 146 func NewTLSServer(handler http.Handler) *Server { 147 ts := NewUnstartedServer(handler, "") 148 ts.StartTLS() 149 return ts 150 } 151 152 type closeIdleTransport interface { 153 CloseIdleConnections() 154 } 155 156 // Close shuts down the server and blocks until all outstanding 157 // requests on this server have completed. 158 func (s *Server) Close() { 159 s.mu.Lock() 160 if !s.closed { 161 s.closed = true 162 s.Listener.Close() 163 s.Config.SetKeepAlivesEnabled(false) 164 for c, st := range s.conns { 165 // Force-close any idle connections (those between 166 // requests) and new connections (those which connected 167 // but never sent a request). StateNew connections are 168 // super rare and have only been seen (in 169 // previously-flaky tests) in the case of 170 // socket-late-binding races from the http Client 171 // dialing this server and then getting an idle 172 // connection before the dial completed. There is thus 173 // a connected connection in StateNew with no 174 // associated Request. We only close StateIdle and 175 // StateNew because they're not doing anything. It's 176 // possible StateNew is about to do something in a few 177 // milliseconds, but a previous CL to check again in a 178 // few milliseconds wasn't liked (early versions of 179 // https://golang.org/cl/15151) so now we just 180 // forcefully close StateNew. The docs for Server.Close say 181 // we wait for "outstanding requests", so we don't close things 182 // in StateActive. 183 if st == http.StateIdle || st == http.StateNew { 184 s.closeConn(c) 185 } 186 } 187 // If this server doesn't shut down in 5 seconds, tell the user why. 188 t := time.AfterFunc(5*time.Second, s.logCloseHangDebugInfo) 189 defer t.Stop() 190 } 191 s.mu.Unlock() 192 193 // Not part of httptest.Server's correctness, but assume most 194 // users of httptest.Server will be using the standard 195 // transport, so help them out and close any idle connections for them. 196 if t, ok := http.DefaultTransport.(closeIdleTransport); ok { 197 t.CloseIdleConnections() 198 } 199 200 // Also close the client idle connections. 201 if s.client != nil { 202 if t, ok := s.client.Transport.(closeIdleTransport); ok { 203 t.CloseIdleConnections() 204 } 205 } 206 207 s.wg.Wait() 208 } 209 210 func (s *Server) logCloseHangDebugInfo() { 211 s.mu.Lock() 212 defer s.mu.Unlock() 213 var buf strings.Builder 214 buf.WriteString("httptest.Server blocked in Close after 5 seconds, waiting for connections:\n") 215 for c, st := range s.conns { 216 fmt.Fprintf(&buf, " %T %p %v in state %v\n", c, c, c.RemoteAddr(), st) 217 } 218 log.Print(buf.String()) 219 } 220 221 // CloseClientConnections closes any open HTTP connections to the test Server. 222 func (s *Server) CloseClientConnections() { 223 s.mu.Lock() 224 nconn := len(s.conns) 225 ch := make(chan struct{}, nconn) 226 for c := range s.conns { 227 go s.closeConnChan(c, ch) 228 } 229 s.mu.Unlock() 230 231 // Wait for outstanding closes to finish. 232 // 233 // Out of paranoia for making a late change in Go 1.6, we 234 // bound how long this can wait, since golang.org/issue/14291 235 // isn't fully understood yet. At least this should only be used 236 // in tests. 237 timer := time.NewTimer(5 * time.Second) 238 defer timer.Stop() 239 for i := 0; i < nconn; i++ { 240 select { 241 case <-ch: 242 case <-timer.C: 243 // Too slow. Give up. 244 return 245 } 246 } 247 } 248 249 // Certificate returns the certificate used by the server, or nil if 250 // the server doesn't use TLS. 251 func (s *Server) Certificate() *x509.Certificate { 252 return s.certificate 253 } 254 255 // Client returns an HTTP client configured for making requests to the server. 256 // It is configured to trust the server's TLS test certificate and will 257 // close its idle connections on Server.Close. 258 func (s *Server) Client() *http.Client { 259 return s.client 260 } 261 262 func (s *Server) goServe() { 263 s.wg.Add(1) 264 go func() { 265 defer s.wg.Done() 266 s.Config.Serve(s.Listener) 267 }() 268 } 269 270 // wrap installs the connection state-tracking hook to know which 271 // connections are idle. 272 func (s *Server) wrap() { 273 oldHook := s.Config.ConnState 274 s.Config.ConnState = func(c net.Conn, cs http.ConnState) { 275 s.mu.Lock() 276 defer s.mu.Unlock() 277 278 switch cs { 279 case http.StateNew: 280 if _, exists := s.conns[c]; exists { 281 panic("invalid state transition") 282 } 283 if s.conns == nil { 284 s.conns = make(map[net.Conn]http.ConnState) 285 } 286 // Add c to the set of tracked conns and increment it to the 287 // waitgroup. 288 s.wg.Add(1) 289 s.conns[c] = cs 290 if s.closed { 291 // Probably just a socket-late-binding dial from 292 // the default transport that lost the race (and 293 // thus this connection is now idle and will 294 // never be used). 295 s.closeConn(c) 296 } 297 case http.StateActive: 298 if oldState, ok := s.conns[c]; ok { 299 if oldState != http.StateNew && oldState != http.StateIdle { 300 panic("invalid state transition") 301 } 302 s.conns[c] = cs 303 } 304 case http.StateIdle: 305 if oldState, ok := s.conns[c]; ok { 306 if oldState != http.StateActive { 307 panic("invalid state transition") 308 } 309 s.conns[c] = cs 310 } 311 if s.closed { 312 s.closeConn(c) 313 } 314 case http.StateHijacked, http.StateClosed: 315 // Remove c from the set of tracked conns and decrement it from the 316 // waitgroup, unless it was previously removed. 317 if _, ok := s.conns[c]; ok { 318 delete(s.conns, c) 319 // Keep Close from returning until the user's ConnState hook 320 // (if any) finishes. 321 defer s.wg.Done() 322 } 323 } 324 if oldHook != nil { 325 oldHook(c, cs) 326 } 327 } 328 } 329 330 // closeConn closes c. 331 // s.mu must be held. 332 func (s *Server) closeConn(c net.Conn) { s.closeConnChan(c, nil) } 333 334 // closeConnChan is like closeConn, but takes an optional channel to receive a value 335 // when the goroutine closing c is done. 336 func (s *Server) closeConnChan(c net.Conn, done chan<- struct{}) { 337 c.Close() 338 if done != nil { 339 done <- struct{}{} 340 } 341 }