2016-07-24 15:30:57 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-07-24 15:30:57 -04:00
|
|
|
|
|
|
|
import (
|
2016-11-05 15:57:31 -04:00
|
|
|
"bufio"
|
2016-07-24 15:30:57 -04:00
|
|
|
"crypto/tls"
|
2016-08-12 00:33:55 -04:00
|
|
|
"errors"
|
2016-07-24 15:30:57 -04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-08-12 00:33:55 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-07-24 15:30:57 -04:00
|
|
|
)
|
|
|
|
|
2016-11-10 10:44:41 -05:00
|
|
|
// The value chosen below is longest word chosen
|
2016-11-05 15:57:31 -04:00
|
|
|
// from all the http verbs comprising of
|
|
|
|
// "PRI", "OPTIONS", "GET", "HEAD", "POST",
|
|
|
|
// "PUT", "DELETE", "TRACE", "CONNECT".
|
|
|
|
const (
|
|
|
|
maxHTTPVerbLen = 7
|
|
|
|
)
|
|
|
|
|
2016-07-24 15:30:57 -04:00
|
|
|
var defaultHTTP2Methods = []string{
|
|
|
|
"PRI",
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultHTTP1Methods = []string{
|
|
|
|
"OPTIONS",
|
|
|
|
"GET",
|
|
|
|
"HEAD",
|
|
|
|
"POST",
|
|
|
|
"PUT",
|
|
|
|
"DELETE",
|
|
|
|
"TRACE",
|
|
|
|
"CONNECT",
|
|
|
|
}
|
|
|
|
|
2016-11-05 15:57:31 -04:00
|
|
|
// ConnMux - Peeks into the incoming connection for relevant
|
|
|
|
// protocol without advancing the underlying net.Conn (io.Reader).
|
|
|
|
// ConnMux - allows us to multiplex between TLS and Regular HTTP
|
|
|
|
// connections on the same listeners.
|
2016-09-02 22:07:42 -04:00
|
|
|
type ConnMux struct {
|
2016-07-24 15:30:57 -04:00
|
|
|
net.Conn
|
2016-11-05 15:57:31 -04:00
|
|
|
bufrw *bufio.ReadWriter
|
2016-09-02 22:07:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnMux - creates a new ConnMux instance
|
|
|
|
func NewConnMux(c net.Conn) *ConnMux {
|
2016-11-05 15:57:31 -04:00
|
|
|
br := bufio.NewReader(c)
|
|
|
|
bw := bufio.NewWriter(c)
|
|
|
|
return &ConnMux{
|
|
|
|
Conn: c,
|
|
|
|
bufrw: bufio.NewReadWriter(br, bw),
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeekProtocol - reads the first bytes, then checks if it is similar
|
|
|
|
// to one of the default http methods
|
2016-09-02 22:07:42 -04:00
|
|
|
func (c *ConnMux) PeekProtocol() string {
|
2016-11-05 15:57:31 -04:00
|
|
|
buf, err := c.bufrw.Peek(maxHTTPVerbLen)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to peek into the protocol")
|
|
|
|
return "http"
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
|
|
|
for _, m := range defaultHTTP1Methods {
|
2016-11-05 15:57:31 -04:00
|
|
|
if strings.HasPrefix(string(buf), m) {
|
2016-07-24 15:30:57 -04:00
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, m := range defaultHTTP2Methods {
|
2016-11-05 15:57:31 -04:00
|
|
|
if strings.HasPrefix(string(buf), m) {
|
2016-07-24 15:30:57 -04:00
|
|
|
return "http2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "tls"
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:07:42 -04:00
|
|
|
// Read - streams the ConnMux buffer when reset flag is activated, otherwise
|
2016-07-24 15:30:57 -04:00
|
|
|
// streams from the incoming network connection
|
2016-09-02 22:07:42 -04:00
|
|
|
func (c *ConnMux) Read(b []byte) (int, error) {
|
2016-11-05 15:57:31 -04:00
|
|
|
return c.bufrw.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the connection.
|
|
|
|
func (c *ConnMux) Close() (err error) {
|
|
|
|
if err = c.bufrw.Flush(); err != nil {
|
|
|
|
return err
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
2016-11-05 15:57:31 -04:00
|
|
|
return c.Conn.Close()
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:43:28 -04:00
|
|
|
// ListenerMux wraps the standard net.Listener to inspect
|
2016-07-24 15:30:57 -04:00
|
|
|
// the communication protocol upon network connection
|
2016-11-05 12:43:28 -04:00
|
|
|
// ListenerMux also wraps net.Listener to ensure that once
|
|
|
|
// Listener.Close returns, the underlying socket has been closed.
|
|
|
|
//
|
|
|
|
// - https://github.com/golang/go/issues/10527
|
|
|
|
//
|
|
|
|
// The default Listener returns from Close before the underlying
|
|
|
|
// socket has been closed if another goroutine has an active
|
|
|
|
// reference (e.g. is in Accept).
|
|
|
|
//
|
|
|
|
// The following sequence of events can happen:
|
|
|
|
//
|
|
|
|
// Goroutine 1 is running Accept, and is blocked, waiting for epoll
|
|
|
|
//
|
|
|
|
// Goroutine 2 calls Close. It sees an extra reference, and so cannot
|
|
|
|
// destroy the socket, but instead decrements a reference, marks the
|
|
|
|
// connection as closed and unblocks epoll.
|
|
|
|
//
|
|
|
|
// Goroutine 2 returns to the caller, makes a new connection.
|
|
|
|
// The new connection is sent to the socket (since it hasn't been destroyed)
|
|
|
|
//
|
|
|
|
// Goroutine 1 returns from epoll, and accepts the new connection.
|
|
|
|
//
|
|
|
|
// To avoid accepting connections after Close, we block Goroutine 2
|
|
|
|
// from returning from Close till Accept returns an error to the user.
|
2016-09-02 22:07:42 -04:00
|
|
|
type ListenerMux struct {
|
2016-07-24 15:30:57 -04:00
|
|
|
net.Listener
|
|
|
|
config *tls.Config
|
2016-11-06 14:41:01 -05:00
|
|
|
// acceptResCh is a channel for transporting wrapped net.Conn (regular or tls)
|
|
|
|
// after peeking the content of the latter
|
|
|
|
acceptResCh chan ListenerMuxAcceptRes
|
2016-11-05 12:43:28 -04:00
|
|
|
// Cond is used to signal Close when there are no references to the listener.
|
|
|
|
cond *sync.Cond
|
|
|
|
refs int
|
|
|
|
}
|
|
|
|
|
2016-11-06 14:41:01 -05:00
|
|
|
// ListenerMuxAcceptRes contains then final net.Conn data (wrapper by tls or not) to be sent to the http handler
|
|
|
|
type ListenerMuxAcceptRes struct {
|
|
|
|
conn net.Conn
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// newListenerMux listens and wraps accepted connections with tls after protocol peeking
|
|
|
|
func newListenerMux(listener net.Listener, config *tls.Config) *ListenerMux {
|
|
|
|
l := ListenerMux{
|
|
|
|
Listener: listener,
|
|
|
|
config: config,
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
acceptResCh: make(chan ListenerMuxAcceptRes),
|
|
|
|
}
|
|
|
|
// Start listening, wrap connections with tls when needed
|
|
|
|
go func() {
|
|
|
|
// Loop for accepting new connections
|
|
|
|
for {
|
|
|
|
conn, err := l.Listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
l.acceptResCh <- ListenerMuxAcceptRes{err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Wrap the connection with ConnMux to be able to peek the data in the incoming connection
|
|
|
|
// and decide if we need to wrap the connection itself with a TLS or not
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
connMux := NewConnMux(conn)
|
|
|
|
if connMux.PeekProtocol() == "tls" {
|
|
|
|
l.acceptResCh <- ListenerMuxAcceptRes{conn: tls.Server(connMux, l.config)}
|
|
|
|
} else {
|
|
|
|
l.acceptResCh <- ListenerMuxAcceptRes{conn: connMux}
|
|
|
|
}
|
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
2016-11-05 12:43:28 -04:00
|
|
|
// IsClosed - Returns if the underlying listener is closed fully.
|
|
|
|
func (l *ListenerMux) IsClosed() bool {
|
|
|
|
l.cond.L.Lock()
|
|
|
|
defer l.cond.L.Unlock()
|
|
|
|
return l.refs == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *ListenerMux) incRef() {
|
|
|
|
l.cond.L.Lock()
|
|
|
|
l.refs++
|
|
|
|
l.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *ListenerMux) decRef() {
|
|
|
|
l.cond.L.Lock()
|
|
|
|
l.refs--
|
|
|
|
newRefs := l.refs
|
|
|
|
l.cond.L.Unlock()
|
|
|
|
if newRefs == 0 {
|
|
|
|
l.cond.Broadcast()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the listener.
|
|
|
|
// Any blocked Accept operations will be unblocked and return errors.
|
|
|
|
func (l *ListenerMux) Close() error {
|
|
|
|
if l == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.Listener.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.cond.L.Lock()
|
|
|
|
for l.refs > 0 {
|
|
|
|
l.cond.Wait()
|
|
|
|
}
|
|
|
|
l.cond.L.Unlock()
|
|
|
|
return nil
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accept - peek the protocol to decide if we should wrap the
|
|
|
|
// network stream with the TLS server
|
2016-09-02 22:07:42 -04:00
|
|
|
func (l *ListenerMux) Accept() (net.Conn, error) {
|
2016-11-05 12:43:28 -04:00
|
|
|
l.incRef()
|
|
|
|
defer l.decRef()
|
|
|
|
|
2016-11-06 14:41:01 -05:00
|
|
|
res := <-l.acceptResCh
|
|
|
|
return res.conn, res.err
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 22:07:42 -04:00
|
|
|
// ServerMux - the main mux server
|
|
|
|
type ServerMux struct {
|
2016-11-04 15:13:22 -04:00
|
|
|
*http.Server
|
2016-11-01 18:38:28 -04:00
|
|
|
listeners []*ListenerMux
|
2016-08-12 00:33:55 -04:00
|
|
|
WaitGroup *sync.WaitGroup
|
|
|
|
GracefulTimeout time.Duration
|
2016-08-17 03:18:23 -04:00
|
|
|
mu sync.Mutex // guards closed, conns, and listener
|
2016-08-12 00:33:55 -04:00
|
|
|
closed bool
|
|
|
|
conns map[net.Conn]http.ConnState // except terminal states
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:07:42 -04:00
|
|
|
// NewServerMux constructor to create a ServerMux
|
|
|
|
func NewServerMux(addr string, handler http.Handler) *ServerMux {
|
|
|
|
m := &ServerMux{
|
2016-11-04 15:13:22 -04:00
|
|
|
Server: &http.Server{
|
2016-08-12 00:33:55 -04:00
|
|
|
Addr: addr,
|
2016-08-30 15:52:19 -04:00
|
|
|
// Do not add any timeouts Golang net.Conn
|
|
|
|
// closes connections right after 10mins even
|
|
|
|
// if they are not idle.
|
2016-08-12 00:33:55 -04:00
|
|
|
Handler: handler,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
},
|
2016-10-10 02:03:10 -04:00
|
|
|
WaitGroup: &sync.WaitGroup{},
|
|
|
|
// Wait for 5 seconds for new incoming connnections, otherwise
|
|
|
|
// forcibly close them during graceful stop or restart.
|
2016-08-12 00:33:55 -04:00
|
|
|
GracefulTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track connection state
|
|
|
|
m.connState()
|
|
|
|
|
|
|
|
// Returns configured HTTP server.
|
|
|
|
return m
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
2016-11-01 18:38:28 -04:00
|
|
|
// Initialize listeners on all ports.
|
|
|
|
func initListeners(serverAddr string, tls *tls.Config) ([]*ListenerMux, error) {
|
|
|
|
host, port, err := net.SplitHostPort(serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var listeners []*ListenerMux
|
|
|
|
if host == "" {
|
|
|
|
var listener net.Listener
|
|
|
|
listener, err = net.Listen("tcp", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-06 14:41:01 -05:00
|
|
|
listeners = append(listeners, newListenerMux(listener, tls))
|
2016-11-01 18:38:28 -04:00
|
|
|
return listeners, nil
|
|
|
|
}
|
|
|
|
var addrs []string
|
|
|
|
if net.ParseIP(host) != nil {
|
|
|
|
addrs = append(addrs, host)
|
|
|
|
} else {
|
|
|
|
addrs, err = net.LookupHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil, errUnexpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
var listener net.Listener
|
|
|
|
listener, err = net.Listen("tcp", net.JoinHostPort(addr, port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-06 14:41:01 -05:00
|
|
|
listeners = append(listeners, newListenerMux(listener, tls))
|
2016-11-01 18:38:28 -04:00
|
|
|
}
|
|
|
|
return listeners, nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:32:13 -04:00
|
|
|
// ListenAndServe - serve HTTP requests with protocol multiplexing support
|
|
|
|
// TLS is actived when certFile and keyFile parameters are not empty.
|
|
|
|
func (m *ServerMux) ListenAndServe(certFile, keyFile string) (err error) {
|
|
|
|
|
|
|
|
tlsEnabled := certFile != "" && keyFile != ""
|
|
|
|
|
2016-09-02 22:07:42 -04:00
|
|
|
config := &tls.Config{} // Always instantiate.
|
2016-11-05 23:32:13 -04:00
|
|
|
|
|
|
|
if tlsEnabled {
|
|
|
|
// Configure TLS in the server
|
|
|
|
if config.NextProtos == nil {
|
|
|
|
config.NextProtos = []string{"http/1.1", "h2"}
|
|
|
|
}
|
|
|
|
config.Certificates = make([]tls.Certificate, 1)
|
|
|
|
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
2016-08-12 00:33:55 -04:00
|
|
|
|
2016-10-10 02:03:10 -04:00
|
|
|
go m.handleServiceSignals()
|
|
|
|
|
2016-11-01 18:38:28 -04:00
|
|
|
listeners, err := initListeners(m.Server.Addr, config)
|
2016-10-10 02:03:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-17 03:18:23 -04:00
|
|
|
m.mu.Lock()
|
2016-11-01 18:38:28 -04:00
|
|
|
m.listeners = listeners
|
2016-08-17 03:18:23 -04:00
|
|
|
m.mu.Unlock()
|
2016-08-12 00:33:55 -04:00
|
|
|
|
2016-11-05 23:32:13 -04:00
|
|
|
// All http requests start to be processed by httpHandler
|
|
|
|
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if tlsEnabled && r.TLS == nil {
|
|
|
|
// TLS is enabled but Request is not TLS configured
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Opaque: r.URL.Opaque,
|
|
|
|
User: r.URL.User,
|
|
|
|
Host: r.Host,
|
|
|
|
Path: r.URL.Path,
|
|
|
|
RawQuery: r.URL.RawQuery,
|
|
|
|
Fragment: r.URL.Fragment,
|
2016-11-05 12:43:28 -04:00
|
|
|
}
|
2016-11-05 23:32:13 -04:00
|
|
|
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
|
|
|
|
} else {
|
|
|
|
// Execute registered handlers
|
|
|
|
m.Server.Handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
2016-08-12 00:33:55 -04:00
|
|
|
|
2016-11-01 18:38:28 -04:00
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
for _, listener := range listeners {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(listener *ListenerMux) {
|
|
|
|
defer wg.Done()
|
2016-11-05 23:32:13 -04:00
|
|
|
serr := http.Serve(listener, httpHandler)
|
2016-11-05 12:43:28 -04:00
|
|
|
// Do not print the error if the listener is closed.
|
|
|
|
if !listener.IsClosed() {
|
|
|
|
errorIf(serr, "Unable to serve incoming requests.")
|
|
|
|
}
|
2016-11-01 18:38:28 -04:00
|
|
|
}(listener)
|
2016-10-10 02:03:10 -04:00
|
|
|
}
|
2016-11-05 23:32:13 -04:00
|
|
|
// Wait for all http.Serve's to return.
|
2016-11-01 18:38:28 -04:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
2016-07-24 15:30:57 -04:00
|
|
|
}
|
2016-08-12 00:33:55 -04:00
|
|
|
|
|
|
|
// Close initiates the graceful shutdown
|
2016-09-02 22:07:42 -04:00
|
|
|
func (m *ServerMux) Close() error {
|
2016-08-17 03:18:23 -04:00
|
|
|
m.mu.Lock()
|
2016-08-12 00:33:55 -04:00
|
|
|
if m.closed {
|
2016-11-01 18:38:28 -04:00
|
|
|
m.mu.Unlock()
|
2016-08-12 00:33:55 -04:00
|
|
|
return errors.New("Server has been closed")
|
|
|
|
}
|
2016-10-10 02:03:10 -04:00
|
|
|
// Closed completely.
|
2016-08-12 00:33:55 -04:00
|
|
|
m.closed = true
|
2016-08-17 03:18:23 -04:00
|
|
|
|
2016-11-01 18:38:28 -04:00
|
|
|
// Close the listeners.
|
|
|
|
for _, listener := range m.listeners {
|
|
|
|
if err := listener.Close(); err != nil {
|
|
|
|
m.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2016-08-12 00:33:55 -04:00
|
|
|
}
|
|
|
|
|
2016-08-17 03:18:23 -04:00
|
|
|
m.SetKeepAlivesEnabled(false)
|
2016-10-10 02:03:10 -04:00
|
|
|
// Force close any idle and new connections. Waiting for other connections
|
|
|
|
// to close on their own (within the timeout period)
|
2016-08-17 03:18:23 -04:00
|
|
|
for c, st := range m.conns {
|
|
|
|
if st == http.StateIdle || st == http.StateNew {
|
|
|
|
c.Close()
|
2016-08-12 00:33:55 -04:00
|
|
|
}
|
2016-08-17 03:18:23 -04:00
|
|
|
}
|
2016-08-12 00:33:55 -04:00
|
|
|
|
2016-08-17 03:18:23 -04:00
|
|
|
// If the GracefulTimeout happens then forcefully close all connections
|
|
|
|
t := time.AfterFunc(m.GracefulTimeout, func() {
|
|
|
|
for c := range m.conns {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
})
|
2016-10-10 02:03:10 -04:00
|
|
|
|
|
|
|
// Wait for graceful timeout of connections.
|
2016-08-17 03:18:23 -04:00
|
|
|
defer t.Stop()
|
2016-08-12 00:33:55 -04:00
|
|
|
|
2016-08-17 03:18:23 -04:00
|
|
|
m.mu.Unlock()
|
|
|
|
|
|
|
|
// Block until all connections are closed
|
|
|
|
m.WaitGroup.Wait()
|
2016-10-10 02:03:10 -04:00
|
|
|
|
2016-08-17 03:18:23 -04:00
|
|
|
return nil
|
2016-08-12 00:33:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// connState setups the ConnState tracking hook to know which connections are idle
|
2016-09-02 22:07:42 -04:00
|
|
|
func (m *ServerMux) connState() {
|
2016-08-12 00:33:55 -04:00
|
|
|
// Set our ConnState to track idle connections
|
|
|
|
m.Server.ConnState = func(c net.Conn, cs http.ConnState) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
switch cs {
|
|
|
|
case http.StateNew:
|
|
|
|
// New connections increment the WaitGroup and are added the the conns dictionary
|
|
|
|
m.WaitGroup.Add(1)
|
|
|
|
if m.conns == nil {
|
|
|
|
m.conns = make(map[net.Conn]http.ConnState)
|
|
|
|
}
|
|
|
|
m.conns[c] = cs
|
|
|
|
case http.StateActive:
|
|
|
|
// Only update status to StateActive if it's in the conns dictionary
|
|
|
|
if _, ok := m.conns[c]; ok {
|
|
|
|
m.conns[c] = cs
|
|
|
|
}
|
|
|
|
case http.StateIdle:
|
|
|
|
// Only update status to StateIdle if it's in the conns dictionary
|
|
|
|
if _, ok := m.conns[c]; ok {
|
|
|
|
m.conns[c] = cs
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already closed then we need to close this connection.
|
|
|
|
// We don't allow connections to become idle after server is closed
|
|
|
|
if m.closed {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
case http.StateHijacked, http.StateClosed:
|
|
|
|
// If the connection is hijacked or closed we forget it
|
|
|
|
m.forgetConn(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forgetConn removes c from conns and decrements WaitGroup
|
2016-09-02 22:07:42 -04:00
|
|
|
func (m *ServerMux) forgetConn(c net.Conn) {
|
2016-08-12 00:33:55 -04:00
|
|
|
if _, ok := m.conns[c]; ok {
|
|
|
|
delete(m.conns, c)
|
|
|
|
m.WaitGroup.Done()
|
|
|
|
}
|
|
|
|
}
|