2015-01-30 01:52:18 -05: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-13 14:43:45 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-04-15 15:11:32 -05:00
|
|
|
"crypto/tls"
|
2015-03-29 23:01:42 -05:00
|
|
|
"fmt"
|
2015-01-13 14:43:45 -05:00
|
|
|
"log"
|
2015-04-15 15:11:32 -05:00
|
|
|
"net"
|
2015-01-13 14:43:45 -05:00
|
|
|
"net/http"
|
2015-03-26 10:52:03 -05:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-03-16 12:44:54 -05:00
|
|
|
"github.com/bradfitz/http2"
|
2015-01-13 14:43:45 -05:00
|
|
|
"github.com/mholt/caddy/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server represents an instance of a server, which serves
|
|
|
|
// static content at a particular address (host and port).
|
|
|
|
type Server struct {
|
2015-04-21 17:00:16 -05: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-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:11:32 -05: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.
|
|
|
|
func New(addr string, configs []config.Config, tls bool) (*Server, error) {
|
|
|
|
s := &Server{
|
|
|
|
address: addr,
|
|
|
|
tls: tls,
|
|
|
|
vhosts: make(map[string]virtualHost),
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
for _, conf := range configs {
|
|
|
|
if _, exists := s.vhosts[conf.Host]; exists {
|
|
|
|
return nil, fmt.Errorf("Cannot serve %s - host already defined for address %s", conf.Address(), s.address)
|
|
|
|
}
|
2015-01-21 16:10:52 -05:00
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
vh := virtualHost{config: conf}
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
// Build middleware stack
|
|
|
|
err := vh.buildStack()
|
2015-03-28 17:37:37 -05:00
|
|
|
if err != nil {
|
2015-04-15 15:11:32 -05:00
|
|
|
return nil, err
|
2015-03-28 17:37:37 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
s.vhosts[conf.Host] = vh
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
return s, nil
|
|
|
|
}
|
2015-01-21 16:10:52 -05:00
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
// Serve starts the server. It blocks until the server quits.
|
|
|
|
func (s *Server) Serve() error {
|
2015-03-16 12:44:54 -05:00
|
|
|
server := &http.Server{
|
2015-04-15 15:11:32 -05:00
|
|
|
Addr: s.address,
|
2015-03-16 12:44:54 -05:00
|
|
|
Handler: s,
|
|
|
|
}
|
|
|
|
|
2015-04-09 11:08:22 -05: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-16 12:44:54 -05:00
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
for _, vh := range s.vhosts {
|
2015-04-24 21:08:14 -05:00
|
|
|
// Execute startup functions now
|
2015-04-15 15:11:32 -05:00
|
|
|
for _, start := range vh.config.Startup {
|
|
|
|
err := start()
|
2015-03-26 10:52:03 -05:00
|
|
|
if err != nil {
|
2015-04-15 15:11:32 -05:00
|
|
|
return err
|
2015-03-26 10:52:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:08:14 -05:00
|
|
|
// Execute shutdown commands on exit
|
2015-04-15 15:11:32 -05:00
|
|
|
if len(vh.config.Shutdown) > 0 {
|
|
|
|
go func() {
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt, os.Kill) // TODO: syscall.SIGQUIT? (Ctrl+\, Unix-only)
|
|
|
|
<-interrupt
|
|
|
|
for _, shutdownFunc := range vh.config.Shutdown {
|
|
|
|
err := shutdownFunc()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.tls {
|
|
|
|
var tlsConfigs []config.TLSConfig
|
|
|
|
for _, vh := range s.vhosts {
|
|
|
|
tlsConfigs = append(tlsConfigs, vh.config.TLS)
|
|
|
|
}
|
|
|
|
return ListenAndServeTLSWithSNI(server, tlsConfigs)
|
2015-01-13 14:43:45 -05:00
|
|
|
} else {
|
2015-03-16 12:44:54 -05:00
|
|
|
return server.ListenAndServe()
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 15:11:32 -05: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.
|
|
|
|
func ListenAndServeTLSWithSNI(srv *http.Server, tlsConfigs []config.TLSConfig) error {
|
|
|
|
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()
|
|
|
|
|
|
|
|
conn, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsListener := tls.NewListener(conn, config)
|
|
|
|
return srv.Serve(tlsListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-13 14:43:45 -05:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-03-26 23:52:27 -05:00
|
|
|
defer func() {
|
2015-03-28 17:37:37 -05:00
|
|
|
// In case the user doesn't enable error middleware, we still
|
|
|
|
// need to make sure that we stay alive up here
|
2015-03-26 23:52:27 -05:00
|
|
|
if rec := recover(); rec != nil {
|
2015-03-28 17:37:37 -05:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError),
|
|
|
|
http.StatusInternalServerError)
|
2015-03-26 23:52:27 -05:00
|
|
|
}
|
|
|
|
}()
|
2015-03-29 23:01:42 -05:00
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
host, _, err := net.SplitHostPort(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
host = r.Host // oh well
|
2015-03-29 23:01:42 -05:00
|
|
|
}
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-04-28 16:31:42 -05: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-04-15 15:11:32 -05:00
|
|
|
if vh, ok := s.vhosts[host]; ok {
|
2015-04-27 00:09:26 -05:00
|
|
|
w.Header().Set("Server", "Caddy")
|
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
status, _ := vh.stack.ServeHTTP(w, r)
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-04-15 15:11:32 -05:00
|
|
|
// Fallback error response in case error handling wasn't chained in
|
|
|
|
if status >= 400 {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
fmt.Fprintf(w, "%d %s", status, http.StatusText(status))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "No such host at %s", s.address)
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
}
|