github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/net/http/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 "bytes" 11 "crypto/tls" 12 "flag" 13 "fmt" 14 "log" 15 "net" 16 "net/http" 17 "os" 18 "runtime" 19 "sync" 20 "time" 21 ) 22 23 // A Server is an HTTP server listening on a system-chosen port on the 24 // local loopback interface, for use in end-to-end HTTP tests. 25 type Server struct { 26 URL string // base URL of form http://ipaddr:port with no trailing slash 27 Listener net.Listener 28 29 // TLS is the optional TLS configuration, populated with a new config 30 // after TLS is started. If set on an unstarted server before StartTLS 31 // is called, existing fields are copied into the new config. 32 TLS *tls.Config 33 34 // Config may be changed after calling NewUnstartedServer and 35 // before Start or StartTLS. 36 Config *http.Server 37 38 // wg counts the number of outstanding HTTP requests on this server. 39 // Close blocks until all requests are finished. 40 wg sync.WaitGroup 41 42 mu sync.Mutex // guards closed and conns 43 closed bool 44 conns map[net.Conn]http.ConnState // except terminal states 45 } 46 47 func newLocalListener() net.Listener { 48 if *serve != "" { 49 l, err := net.Listen("tcp", *serve) 50 if err != nil { 51 panic(fmt.Sprintf("httptest: failed to listen on %v: %v", *serve, err)) 52 } 53 return l 54 } 55 l, err := net.Listen("tcp", "127.0.0.1:0") 56 if err != nil { 57 if l, err = net.Listen("tcp6", "[::1]:0"); err != nil { 58 panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err)) 59 } 60 } 61 return l 62 } 63 64 // When debugging a particular http server-based test, 65 // this flag lets you run 66 // go test -run=BrokenTest -httptest.serve=127.0.0.1:8000 67 // to start the broken server so you can interact with it manually. 68 var serve = flag.String("httptest.serve", "", "if non-empty, httptest.NewServer serves on this address and blocks") 69 70 // NewServer starts and returns a new Server. 71 // The caller should call Close when finished, to shut it down. 72 func NewServer(handler http.Handler) *Server { 73 ts := NewUnstartedServer(handler) 74 ts.Start() 75 return ts 76 } 77 78 // NewUnstartedServer returns a new Server but doesn't start it. 79 // 80 // After changing its configuration, the caller should call Start or 81 // StartTLS. 82 // 83 // The caller should call Close when finished, to shut it down. 84 func NewUnstartedServer(handler http.Handler) *Server { 85 return &Server{ 86 Listener: newLocalListener(), 87 Config: &http.Server{Handler: handler}, 88 } 89 } 90 91 // Start starts a server from NewUnstartedServer. 92 func (s *Server) Start() { 93 if s.URL != "" { 94 panic("Server already started") 95 } 96 s.URL = "http://" + s.Listener.Addr().String() 97 s.wrap() 98 s.goServe() 99 if *serve != "" { 100 fmt.Fprintln(os.Stderr, "httptest: serving on", s.URL) 101 select {} 102 } 103 } 104 105 // StartTLS starts TLS on a server from NewUnstartedServer. 106 func (s *Server) StartTLS() { 107 if s.URL != "" { 108 panic("Server already started") 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 s.TLS = new(tls.Config) 117 if existingConfig != nil { 118 *s.TLS = *existingConfig 119 } 120 if s.TLS.NextProtos == nil { 121 s.TLS.NextProtos = []string{"http/1.1"} 122 } 123 if len(s.TLS.Certificates) == 0 { 124 s.TLS.Certificates = []tls.Certificate{cert} 125 } 126 s.Listener = tls.NewListener(s.Listener, s.TLS) 127 s.URL = "https://" + s.Listener.Addr().String() 128 s.wrap() 129 s.goServe() 130 } 131 132 // NewTLSServer starts and returns a new Server using TLS. 133 // The caller should call Close when finished, to shut it down. 134 func NewTLSServer(handler http.Handler) *Server { 135 ts := NewUnstartedServer(handler) 136 ts.StartTLS() 137 return ts 138 } 139 140 type closeIdleTransport interface { 141 CloseIdleConnections() 142 } 143 144 // Close shuts down the server and blocks until all outstanding 145 // requests on this server have completed. 146 func (s *Server) Close() { 147 s.mu.Lock() 148 if !s.closed { 149 s.closed = true 150 s.Listener.Close() 151 s.Config.SetKeepAlivesEnabled(false) 152 for c, st := range s.conns { 153 // Force-close any idle connections (those between 154 // requests) and new connections (those which connected 155 // but never sent a request). StateNew connections are 156 // super rare and have only been seen (in 157 // previously-flaky tests) in the case of 158 // socket-late-binding races from the http Client 159 // dialing this server and then getting an idle 160 // connection before the dial completed. There is thus 161 // a connected connection in StateNew with no 162 // associated Request. We only close StateIdle and 163 // StateNew because they're not doing anything. It's 164 // possible StateNew is about to do something in a few 165 // milliseconds, but a previous CL to check again in a 166 // few milliseconds wasn't liked (early versions of 167 // https://golang.org/cl/15151) so now we just 168 // forcefully close StateNew. The docs for Server.Close say 169 // we wait for "oustanding requests", so we don't close things 170 // in StateActive. 171 if st == http.StateIdle || st == http.StateNew { 172 s.closeConn(c) 173 } 174 } 175 // If this server doesn't shut down in 5 seconds, tell the user why. 176 t := time.AfterFunc(5*time.Second, s.logCloseHangDebugInfo) 177 defer t.Stop() 178 } 179 s.mu.Unlock() 180 181 // Not part of httptest.Server's correctness, but assume most 182 // users of httptest.Server will be using the standard 183 // transport, so help them out and close any idle connections for them. 184 if t, ok := http.DefaultTransport.(closeIdleTransport); ok { 185 t.CloseIdleConnections() 186 } 187 188 s.wg.Wait() 189 } 190 191 func (s *Server) logCloseHangDebugInfo() { 192 s.mu.Lock() 193 defer s.mu.Unlock() 194 var buf bytes.Buffer 195 buf.WriteString("httptest.Server blocked in Close after 5 seconds, waiting for connections:\n") 196 for c, st := range s.conns { 197 fmt.Fprintf(&buf, " %T %p %v in state %v\n", c, c, c.RemoteAddr(), st) 198 } 199 log.Print(buf.String()) 200 } 201 202 // CloseClientConnections closes any open HTTP connections to the test Server. 203 func (s *Server) CloseClientConnections() { 204 s.mu.Lock() 205 defer s.mu.Unlock() 206 for c := range s.conns { 207 s.closeConn(c) 208 } 209 } 210 211 func (s *Server) goServe() { 212 s.wg.Add(1) 213 go func() { 214 defer s.wg.Done() 215 s.Config.Serve(s.Listener) 216 }() 217 } 218 219 // wrap installs the connection state-tracking hook to know which 220 // connections are idle. 221 func (s *Server) wrap() { 222 oldHook := s.Config.ConnState 223 s.Config.ConnState = func(c net.Conn, cs http.ConnState) { 224 s.mu.Lock() 225 defer s.mu.Unlock() 226 switch cs { 227 case http.StateNew: 228 s.wg.Add(1) 229 if _, exists := s.conns[c]; exists { 230 panic("invalid state transition") 231 } 232 if s.conns == nil { 233 s.conns = make(map[net.Conn]http.ConnState) 234 } 235 s.conns[c] = cs 236 if s.closed { 237 // Probably just a socket-late-binding dial from 238 // the default transport that lost the race (and 239 // thus this connection is now idle and will 240 // never be used). 241 s.closeConn(c) 242 } 243 case http.StateActive: 244 if oldState, ok := s.conns[c]; ok { 245 if oldState != http.StateNew && oldState != http.StateIdle { 246 panic("invalid state transition") 247 } 248 s.conns[c] = cs 249 } 250 case http.StateIdle: 251 if oldState, ok := s.conns[c]; ok { 252 if oldState != http.StateActive { 253 panic("invalid state transition") 254 } 255 s.conns[c] = cs 256 } 257 if s.closed { 258 s.closeConn(c) 259 } 260 case http.StateHijacked, http.StateClosed: 261 s.forgetConn(c) 262 } 263 if oldHook != nil { 264 oldHook(c, cs) 265 } 266 } 267 } 268 269 // closeConn closes c. Except on plan9, which is special. See comment below. 270 // s.mu must be held. 271 func (s *Server) closeConn(c net.Conn) { 272 if runtime.GOOS == "plan9" { 273 // Go's Plan 9 net package isn't great at unblocking reads when 274 // their underlying TCP connections are closed. Don't trust 275 // that that the ConnState state machine will get to 276 // StateClosed. Instead, just go there directly. Plan 9 may leak 277 // resources if the syscall doesn't end up returning. Oh well. 278 s.forgetConn(c) 279 } 280 go c.Close() 281 } 282 283 // forgetConn removes c from the set of tracked conns and decrements it from the 284 // waitgroup, unless it was previously removed. 285 // s.mu must be held. 286 func (s *Server) forgetConn(c net.Conn) { 287 if _, ok := s.conns[c]; ok { 288 delete(s.conns, c) 289 s.wg.Done() 290 } 291 } 292 293 // localhostCert is a PEM-encoded TLS cert with SAN IPs 294 // "127.0.0.1" and "[::1]", expiring at Jan 29 16:00:00 2084 GMT. 295 // generated from src/crypto/tls: 296 // go run generate_cert.go --rsa-bits 1024 --host 127.0.0.1,::1,example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h 297 var localhostCert = []byte(`-----BEGIN CERTIFICATE----- 298 MIICEzCCAXygAwIBAgIQMIMChMLGrR+QvmQvpwAU6zANBgkqhkiG9w0BAQsFADAS 299 MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw 300 MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB 301 iQKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9SjY1bIw4 302 iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZBl2+XsDul 303 rKBxKKtD1rGxlG4LjncdabFn9gvLZad2bSysqz/qTAUStTvqJQIDAQABo2gwZjAO 304 BgNVHQ8BAf8EBAMCAqQwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUw 305 AwEB/zAuBgNVHREEJzAlggtleGFtcGxlLmNvbYcEfwAAAYcQAAAAAAAAAAAAAAAA 306 AAAAATANBgkqhkiG9w0BAQsFAAOBgQCEcetwO59EWk7WiJsG4x8SY+UIAA+flUI9 307 tyC4lNhbcF2Idq9greZwbYCqTTTr2XiRNSMLCOjKyI7ukPoPjo16ocHj+P3vZGfs 308 h1fIw3cSS2OolhloGw/XM6RWPWtPAlGykKLciQrBru5NAPvCMsb/I1DAceTiotQM 309 fblo6RBxUQ== 310 -----END CERTIFICATE-----`) 311 312 // localhostKey is the private key for localhostCert. 313 var localhostKey = []byte(`-----BEGIN RSA PRIVATE KEY----- 314 MIICXgIBAAKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9 315 SjY1bIw4iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZB 316 l2+XsDulrKBxKKtD1rGxlG4LjncdabFn9gvLZad2bSysqz/qTAUStTvqJQIDAQAB 317 AoGAGRzwwir7XvBOAy5tM/uV6e+Zf6anZzus1s1Y1ClbjbE6HXbnWWF/wbZGOpet 318 3Zm4vD6MXc7jpTLryzTQIvVdfQbRc6+MUVeLKwZatTXtdZrhu+Jk7hx0nTPy8Jcb 319 uJqFk541aEw+mMogY/xEcfbWd6IOkp+4xqjlFLBEDytgbIECQQDvH/E6nk+hgN4H 320 qzzVtxxr397vWrjrIgPbJpQvBsafG7b0dA4AFjwVbFLmQcj2PprIMmPcQrooz8vp 321 jy4SHEg1AkEA/v13/5M47K9vCxmb8QeD/asydfsgS5TeuNi8DoUBEmiSJwma7FXY 322 fFUtxuvL7XvjwjN5B30pNEbc6Iuyt7y4MQJBAIt21su4b3sjXNueLKH85Q+phy2U 323 fQtuUE9txblTu14q3N7gHRZB4ZMhFYyDy8CKrN2cPg/Fvyt0Xlp/DoCzjA0CQQDU 324 y2ptGsuSmgUtWj3NM9xuwYPm+Z/F84K6+ARYiZ6PYj013sovGKUFfYAqVXVlxtIX 325 qyUBnu3X9ps8ZfjLZO7BAkEAlT4R5Yl6cGhaJQYZHOde3JEMhNRcVFMO8dJDaFeo 326 f9Oeos0UUothgiDktdQHxdNEwLjQf7lJJBzV+5OtwswCWA== 327 -----END RSA PRIVATE KEY-----`)