|
|
@ -281,18 +281,24 @@ func initListeners(serverAddr string, tls *tls.Config) ([]*ListenerMux, error) { |
|
|
|
return listeners, nil |
|
|
|
return listeners, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ListenAndServeTLS - similar to the http.Server version. However, it has the
|
|
|
|
// ListenAndServe - serve HTTP requests with protocol multiplexing support
|
|
|
|
// ability to redirect http requests to the correct HTTPS url if the client
|
|
|
|
// TLS is actived when certFile and keyFile parameters are not empty.
|
|
|
|
// mistakenly initiates a http connection over the https port
|
|
|
|
func (m *ServerMux) ListenAndServe(certFile, keyFile string) (err error) { |
|
|
|
func (m *ServerMux) ListenAndServeTLS(certFile, keyFile string) (err error) { |
|
|
|
|
|
|
|
|
|
|
|
tlsEnabled := certFile != "" && keyFile != "" |
|
|
|
|
|
|
|
|
|
|
|
config := &tls.Config{} // Always instantiate.
|
|
|
|
config := &tls.Config{} // Always instantiate.
|
|
|
|
if config.NextProtos == nil { |
|
|
|
|
|
|
|
config.NextProtos = []string{"http/1.1", "h2"} |
|
|
|
if tlsEnabled { |
|
|
|
} |
|
|
|
// Configure TLS in the server
|
|
|
|
config.Certificates = make([]tls.Certificate, 1) |
|
|
|
if config.NextProtos == nil { |
|
|
|
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) |
|
|
|
config.NextProtos = []string{"http/1.1", "h2"} |
|
|
|
if err != nil { |
|
|
|
} |
|
|
|
return err |
|
|
|
config.Certificates = make([]tls.Certificate, 1) |
|
|
|
|
|
|
|
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
go m.handleServiceSignals() |
|
|
|
go m.handleServiceSignals() |
|
|
@ -306,68 +312,39 @@ func (m *ServerMux) ListenAndServeTLS(certFile, keyFile string) (err error) { |
|
|
|
m.listeners = listeners |
|
|
|
m.listeners = listeners |
|
|
|
m.mu.Unlock() |
|
|
|
m.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
var wg = &sync.WaitGroup{} |
|
|
|
// All http requests start to be processed by httpHandler
|
|
|
|
for _, listener := range listeners { |
|
|
|
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
wg.Add(1) |
|
|
|
if tlsEnabled && r.TLS == nil { |
|
|
|
go func(listener *ListenerMux) { |
|
|
|
// TLS is enabled but Request is not TLS configured
|
|
|
|
defer wg.Done() |
|
|
|
u := url.URL{ |
|
|
|
serr := http.Serve(listener, |
|
|
|
Scheme: "https", |
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
Opaque: r.URL.Opaque, |
|
|
|
// We reach here when ListenerMux.ConnMux is not wrapped with tls.Server
|
|
|
|
User: r.URL.User, |
|
|
|
if r.TLS == nil { |
|
|
|
Host: r.Host, |
|
|
|
u := url.URL{ |
|
|
|
Path: r.URL.Path, |
|
|
|
Scheme: "https", |
|
|
|
RawQuery: r.URL.RawQuery, |
|
|
|
Opaque: r.URL.Opaque, |
|
|
|
Fragment: r.URL.Fragment, |
|
|
|
User: r.URL.User, |
|
|
|
|
|
|
|
Host: r.Host, |
|
|
|
|
|
|
|
Path: r.URL.Path, |
|
|
|
|
|
|
|
RawQuery: r.URL.RawQuery, |
|
|
|
|
|
|
|
Fragment: r.URL.Fragment, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
http.Redirect(w, r, u.String(), http.StatusMovedPermanently) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Execute registered handlers
|
|
|
|
|
|
|
|
m.Server.Handler.ServeHTTP(w, r) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
// Do not print the error if the listener is closed.
|
|
|
|
|
|
|
|
if !listener.IsClosed() { |
|
|
|
|
|
|
|
errorIf(serr, "Unable to serve incoming requests.") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}(listener) |
|
|
|
http.Redirect(w, r, u.String(), http.StatusMovedPermanently) |
|
|
|
} |
|
|
|
} else { |
|
|
|
// Waits for all http.Serve's to return.
|
|
|
|
// Execute registered handlers
|
|
|
|
wg.Wait() |
|
|
|
m.Server.Handler.ServeHTTP(w, r) |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// ListenAndServe - Same as the http.Server version
|
|
|
|
|
|
|
|
func (m *ServerMux) ListenAndServe() error { |
|
|
|
|
|
|
|
go m.handleServiceSignals() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
listeners, err := initListeners(m.Server.Addr, &tls.Config{}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m.mu.Lock() |
|
|
|
|
|
|
|
m.listeners = listeners |
|
|
|
|
|
|
|
m.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var wg = &sync.WaitGroup{} |
|
|
|
var wg = &sync.WaitGroup{} |
|
|
|
for _, listener := range listeners { |
|
|
|
for _, listener := range listeners { |
|
|
|
wg.Add(1) |
|
|
|
wg.Add(1) |
|
|
|
go func(listener *ListenerMux) { |
|
|
|
go func(listener *ListenerMux) { |
|
|
|
defer wg.Done() |
|
|
|
defer wg.Done() |
|
|
|
serr := m.Server.Serve(listener) |
|
|
|
serr := http.Serve(listener, httpHandler) |
|
|
|
// Do not print the error if the listener is closed.
|
|
|
|
// Do not print the error if the listener is closed.
|
|
|
|
if !listener.IsClosed() { |
|
|
|
if !listener.IsClosed() { |
|
|
|
errorIf(serr, "Unable to serve incoming requests.") |
|
|
|
errorIf(serr, "Unable to serve incoming requests.") |
|
|
|
} |
|
|
|
} |
|
|
|
}(listener) |
|
|
|
}(listener) |
|
|
|
} |
|
|
|
} |
|
|
|
// Wait for all the http.Serve to finish.
|
|
|
|
// Wait for all http.Serve's to return.
|
|
|
|
wg.Wait() |
|
|
|
wg.Wait() |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|