2015-01-30 14:52:18 +08:00
|
|
|
// Package server implements a configurable, general-purpose web server.
|
|
|
|
// It relies on configurations obtained from the adjacent config package
|
|
|
|
// and can execute middleware as defined by the adjacent middleware package.
|
2015-01-14 03:43:45 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-04-16 04:11:32 +08:00
|
|
|
"crypto/tls"
|
2015-06-02 13:22:11 +08:00
|
|
|
"crypto/x509"
|
2015-03-30 12:01:42 +08:00
|
|
|
"fmt"
|
2015-06-02 13:22:11 +08:00
|
|
|
"io/ioutil"
|
2015-01-14 03:43:45 +08:00
|
|
|
"log"
|
2015-04-16 04:11:32 +08:00
|
|
|
"net"
|
2015-01-14 03:43:45 +08:00
|
|
|
"net/http"
|
2015-03-26 23:52:03 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-03-17 01:44:54 +08:00
|
|
|
"github.com/bradfitz/http2"
|
2015-01-14 03:43:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server represents an instance of a server, which serves
|
|
|
|
// static content at a particular address (host and port).
|
|
|
|
type Server struct {
|
2015-04-22 06:00:16 +08:00
|
|
|
HTTP2 bool // temporary while http2 is not in std lib (TODO: remove flag when part of std lib)
|
|
|
|
address string // the actual address for net.Listen to listen on
|
|
|
|
tls bool // whether this server is serving all HTTPS hosts or not
|
|
|
|
vhosts map[string]virtualHost // virtual hosts keyed by their address
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// New creates a new Server which will bind to addr and serve
|
|
|
|
// the sites/hosts configured in configs. This function does
|
|
|
|
// not start serving.
|
2015-07-12 02:00:11 +08:00
|
|
|
func New(addr string, configs []Config) (*Server, error) {
|
|
|
|
var tls bool
|
|
|
|
if len(configs) > 0 {
|
|
|
|
tls = configs[0].TLS.Enabled
|
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
s := &Server{
|
|
|
|
address: addr,
|
|
|
|
tls: tls,
|
|
|
|
vhosts: make(map[string]virtualHost),
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
for _, conf := range configs {
|
|
|
|
if _, exists := s.vhosts[conf.Host]; exists {
|
2015-06-08 08:49:17 +08:00
|
|
|
return nil, fmt.Errorf("cannot serve %s - host already defined for address %s", conf.Address(), s.address)
|
2015-04-16 04:11:32 +08:00
|
|
|
}
|
2015-01-22 05:10:52 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
vh := virtualHost{config: conf}
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// Build middleware stack
|
|
|
|
err := vh.buildStack()
|
2015-03-29 06:37:37 +08:00
|
|
|
if err != nil {
|
2015-04-16 04:11:32 +08:00
|
|
|
return nil, err
|
2015-03-29 06:37:37 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
s.vhosts[conf.Host] = vh
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
return s, nil
|
|
|
|
}
|
2015-01-22 05:10:52 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// Serve starts the server. It blocks until the server quits.
|
|
|
|
func (s *Server) Serve() error {
|
2015-03-17 01:44:54 +08:00
|
|
|
server := &http.Server{
|
2015-04-16 04:11:32 +08:00
|
|
|
Addr: s.address,
|
2015-03-17 01:44:54 +08:00
|
|
|
Handler: s,
|
|
|
|
}
|
|
|
|
|
2015-04-10 00:08:22 +08:00
|
|
|
if s.HTTP2 {
|
|
|
|
// TODO: This call may not be necessary after HTTP/2 is merged into std lib
|
|
|
|
http2.ConfigureServer(server, nil)
|
|
|
|
}
|
2015-03-17 01:44:54 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
for _, vh := range s.vhosts {
|
2015-04-25 10:08:14 +08:00
|
|
|
// Execute startup functions now
|
2015-04-16 04:11:32 +08:00
|
|
|
for _, start := range vh.config.Startup {
|
|
|
|
err := start()
|
2015-03-26 23:52:03 +08:00
|
|
|
if err != nil {
|
2015-04-16 04:11:32 +08:00
|
|
|
return err
|
2015-03-26 23:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 10:08:14 +08:00
|
|
|
// Execute shutdown commands on exit
|
2015-04-16 04:11:32 +08:00
|
|
|
if len(vh.config.Shutdown) > 0 {
|
2015-08-02 03:08:31 +08:00
|
|
|
go func(vh virtualHost) {
|
|
|
|
// Wait for signal
|
2015-04-16 04:11:32 +08:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
2015-08-02 03:08:31 +08:00
|
|
|
signal.Notify(interrupt, os.Interrupt, os.Kill)
|
2015-04-16 04:11:32 +08:00
|
|
|
<-interrupt
|
2015-08-02 03:08:31 +08:00
|
|
|
|
|
|
|
// Run callbacks
|
|
|
|
exitCode := 0
|
2015-04-16 04:11:32 +08:00
|
|
|
for _, shutdownFunc := range vh.config.Shutdown {
|
|
|
|
err := shutdownFunc()
|
|
|
|
if err != nil {
|
2015-08-02 03:08:31 +08:00
|
|
|
exitCode = 1
|
|
|
|
log.Println(err)
|
2015-04-16 04:11:32 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-02 03:08:31 +08:00
|
|
|
os.Exit(exitCode) // BUG: Other shutdown goroutines might be running; use sync.WaitGroup
|
|
|
|
}(vh)
|
2015-04-16 04:11:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.tls {
|
2015-05-04 20:53:54 +08:00
|
|
|
var tlsConfigs []TLSConfig
|
2015-04-16 04:11:32 +08:00
|
|
|
for _, vh := range s.vhosts {
|
|
|
|
tlsConfigs = append(tlsConfigs, vh.config.TLS)
|
|
|
|
}
|
|
|
|
return ListenAndServeTLSWithSNI(server, tlsConfigs)
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
2015-05-25 10:52:34 +08:00
|
|
|
return server.ListenAndServe()
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// ListenAndServeTLSWithSNI serves TLS with Server Name Indication (SNI) support, which allows
|
|
|
|
// multiple sites (different hostnames) to be served from the same address. This method is
|
|
|
|
// adapted directly from the std lib's net/http ListenAndServeTLS function, which was
|
|
|
|
// written by the Go Authors. It has been modified to support multiple certificate/key pairs.
|
2015-05-04 20:53:54 +08:00
|
|
|
func ListenAndServeTLSWithSNI(srv *http.Server, tlsConfigs []TLSConfig) error {
|
2015-04-16 04:11:32 +08:00
|
|
|
addr := srv.Addr
|
|
|
|
if addr == "" {
|
|
|
|
addr = ":https"
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(tls.Config)
|
|
|
|
if srv.TLSConfig != nil {
|
|
|
|
*config = *srv.TLSConfig
|
|
|
|
}
|
|
|
|
if config.NextProtos == nil {
|
|
|
|
config.NextProtos = []string{"http/1.1"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we diverge from the stdlib a bit by loading multiple certs/key pairs
|
|
|
|
// then we map the server names to their certs
|
|
|
|
var err error
|
|
|
|
config.Certificates = make([]tls.Certificate, len(tlsConfigs))
|
|
|
|
for i, tlsConfig := range tlsConfigs {
|
|
|
|
config.Certificates[i], err = tls.LoadX509KeyPair(tlsConfig.Certificate, tlsConfig.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.BuildNameToCertificate()
|
|
|
|
|
2015-05-21 14:06:53 +08:00
|
|
|
// Customize our TLS configuration
|
2015-05-19 03:38:21 +08:00
|
|
|
config.MinVersion = tlsConfigs[0].ProtocolMinVersion
|
|
|
|
config.MaxVersion = tlsConfigs[0].ProtocolMaxVersion
|
|
|
|
config.CipherSuites = tlsConfigs[0].Ciphers
|
2015-05-22 00:37:39 +08:00
|
|
|
config.PreferServerCipherSuites = tlsConfigs[0].PreferServerCipherSuites
|
2015-05-05 11:25:29 +08:00
|
|
|
|
2015-06-02 13:22:11 +08:00
|
|
|
// TLS client authentication, if user enabled it
|
|
|
|
err = setupClientAuth(tlsConfigs, config)
|
2015-04-16 04:11:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-02 13:22:11 +08:00
|
|
|
// Create listener and we're on our way
|
|
|
|
conn, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 04:11:32 +08:00
|
|
|
tlsListener := tls.NewListener(conn, config)
|
2015-06-02 13:22:11 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
return srv.Serve(tlsListener)
|
|
|
|
}
|
|
|
|
|
2015-06-02 13:22:11 +08:00
|
|
|
// setupClientAuth sets up TLS client authentication only if
|
|
|
|
// any of the TLS configs specified at least one cert file.
|
|
|
|
func setupClientAuth(tlsConfigs []TLSConfig, config *tls.Config) error {
|
|
|
|
var clientAuth bool
|
|
|
|
for _, cfg := range tlsConfigs {
|
|
|
|
if len(cfg.ClientCerts) > 0 {
|
|
|
|
clientAuth = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if clientAuth {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, cfg := range tlsConfigs {
|
|
|
|
for _, caFile := range cfg.ClientCerts {
|
|
|
|
caCrt, err := ioutil.ReadFile(caFile) // Anyone that gets a cert from Matt Holt can connect
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !pool.AppendCertsFromPEM(caCrt) {
|
2015-06-08 08:49:17 +08:00
|
|
|
return fmt.Errorf("error loading client certificate '%s': no certificates were successfully parsed", caFile)
|
2015-06-02 13:22:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.ClientCAs = pool
|
|
|
|
config.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// ServeHTTP is the entry point for every request to the address that s
|
|
|
|
// is bound to. It acts as a multiplexer for the requests hostname as
|
|
|
|
// defined in the Host header so that the correct virtualhost
|
|
|
|
// (configuration and middleware stack) will handle the request.
|
2015-01-14 03:43:45 +08:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-03-27 12:52:27 +08:00
|
|
|
defer func() {
|
2015-03-29 06:37:37 +08:00
|
|
|
// In case the user doesn't enable error middleware, we still
|
|
|
|
// need to make sure that we stay alive up here
|
2015-03-27 12:52:27 +08:00
|
|
|
if rec := recover(); rec != nil {
|
2015-03-29 06:37:37 +08:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError),
|
|
|
|
http.StatusInternalServerError)
|
2015-03-27 12:52:27 +08:00
|
|
|
}
|
|
|
|
}()
|
2015-03-30 12:01:42 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
host, _, err := net.SplitHostPort(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
host = r.Host // oh well
|
2015-03-30 12:01:42 +08:00
|
|
|
}
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-04-29 05:31:42 +08:00
|
|
|
// Try the host as given, or try falling back to 0.0.0.0 (wildcard)
|
|
|
|
if _, ok := s.vhosts[host]; !ok {
|
|
|
|
if _, ok2 := s.vhosts["0.0.0.0"]; ok2 {
|
|
|
|
host = "0.0.0.0"
|
2015-06-10 13:07:32 +08:00
|
|
|
} else if _, ok2 := s.vhosts[""]; ok2 {
|
|
|
|
host = ""
|
2015-04-29 05:31:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
if vh, ok := s.vhosts[host]; ok {
|
2015-04-27 13:09:26 +08:00
|
|
|
w.Header().Set("Server", "Caddy")
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
status, _ := vh.stack.ServeHTTP(w, r)
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// Fallback error response in case error handling wasn't chained in
|
|
|
|
if status >= 400 {
|
2015-06-16 00:17:09 +08:00
|
|
|
DefaultErrorFunc(w, r, status)
|
2015-04-16 04:11:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "No such host at %s", s.address)
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-16 00:17:09 +08:00
|
|
|
|
|
|
|
func DefaultErrorFunc(w http.ResponseWriter, r *http.Request, status int) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
fmt.Fprintf(w, "%d %s", status, http.StatusText(status))
|
|
|
|
}
|