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