2019-05-21 00:59:20 +08:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-05-23 04:14:26 +08:00
|
|
|
"net"
|
2019-05-21 00:59:20 +08:00
|
|
|
"net/http"
|
2019-05-21 11:21:33 +08:00
|
|
|
"strconv"
|
2019-05-21 00:59:20 +08:00
|
|
|
|
|
|
|
"bitbucket.org/lightcodelabs/caddy2"
|
|
|
|
"bitbucket.org/lightcodelabs/caddy2/modules/caddytls"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server is an HTTP server.
|
|
|
|
type Server struct {
|
2019-05-23 02:32:36 +08:00
|
|
|
Listen []string `json:"listen,omitempty"`
|
|
|
|
ReadTimeout caddy2.Duration `json:"read_timeout,omitempty"`
|
|
|
|
ReadHeaderTimeout caddy2.Duration `json:"read_header_timeout,omitempty"`
|
2019-05-24 03:16:34 +08:00
|
|
|
WriteTimeout caddy2.Duration `json:"write_timeout,omitempty"`
|
|
|
|
IdleTimeout caddy2.Duration `json:"idle_timeout,omitempty"`
|
|
|
|
MaxHeaderBytes int `json:"max_header_bytes,omitempty"`
|
2019-05-23 02:32:36 +08:00
|
|
|
Routes RouteList `json:"routes,omitempty"`
|
|
|
|
Errors *httpErrorConfig `json:"errors,omitempty"`
|
|
|
|
TLSConnPolicies caddytls.ConnectionPolicies `json:"tls_connection_policies,omitempty"`
|
|
|
|
DisableAutoHTTPS bool `json:"disable_auto_https,omitempty"`
|
|
|
|
DisableAutoHTTPSRedir bool `json:"disable_auto_https_redir,omitempty"`
|
|
|
|
MaxRehandles int `json:"max_rehandles,omitempty"`
|
2019-05-21 00:59:20 +08:00
|
|
|
|
|
|
|
tlsApp *caddytls.TLS
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP is the entry point for all HTTP requests.
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if s.tlsApp.HandleHTTPChallenge(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-21 13:48:43 +08:00
|
|
|
// set up the context for the request
|
|
|
|
repl := caddy2.NewReplacer()
|
2019-05-21 00:59:20 +08:00
|
|
|
ctx := context.WithValue(r.Context(), caddy2.ReplacerCtxKey, repl)
|
|
|
|
ctx = context.WithValue(ctx, TableCtxKey, make(map[string]interface{})) // TODO: Implement this
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
2019-05-21 13:48:43 +08:00
|
|
|
// once the pointer to the request won't change
|
|
|
|
// anymore, finish setting up the replacer
|
|
|
|
addHTTPVarsToReplacer(repl, r, w)
|
|
|
|
|
2019-05-21 00:59:20 +08:00
|
|
|
// build and execute the main handler chain
|
2019-05-21 13:48:43 +08:00
|
|
|
stack, w := s.Routes.BuildCompositeRoute(w, r)
|
2019-05-21 00:59:20 +08:00
|
|
|
err := s.executeCompositeRoute(w, r, stack)
|
|
|
|
if err != nil {
|
2019-05-21 11:21:33 +08:00
|
|
|
// add the raw error value to the request context
|
|
|
|
// so it can be accessed by error handlers
|
2019-05-21 00:59:20 +08:00
|
|
|
c := context.WithValue(r.Context(), ErrorCtxKey, err)
|
|
|
|
r = r.WithContext(c)
|
2019-05-21 11:21:33 +08:00
|
|
|
|
|
|
|
// add error values to the replacer
|
|
|
|
repl.Set("http.error", err.Error())
|
|
|
|
if handlerErr, ok := err.(HandlerError); ok {
|
|
|
|
repl.Set("http.error.status_code", strconv.Itoa(handlerErr.StatusCode))
|
|
|
|
repl.Set("http.error.status_text", http.StatusText(handlerErr.StatusCode))
|
|
|
|
repl.Set("http.error.message", handlerErr.Message)
|
|
|
|
repl.Set("http.error.trace", handlerErr.Trace)
|
|
|
|
repl.Set("http.error.id", handlerErr.ID)
|
|
|
|
}
|
2019-05-21 00:59:20 +08:00
|
|
|
|
2019-05-24 04:42:14 +08:00
|
|
|
if s.Errors != nil && len(s.Errors.Routes) > 0 {
|
2019-05-21 13:48:43 +08:00
|
|
|
errStack, w := s.Errors.Routes.BuildCompositeRoute(w, r)
|
2019-05-21 00:59:20 +08:00
|
|
|
err := s.executeCompositeRoute(w, r, errStack)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: what should we do if the error handler has an error?
|
|
|
|
log.Printf("[ERROR] handling error: %v", err)
|
|
|
|
}
|
2019-05-24 04:42:14 +08:00
|
|
|
} else {
|
|
|
|
// TODO: polish the default error handling
|
|
|
|
log.Printf("[ERROR] Handler: %s", err)
|
|
|
|
if handlerErr, ok := err.(HandlerError); ok {
|
|
|
|
w.WriteHeader(handlerErr.StatusCode)
|
|
|
|
}
|
2019-05-21 00:59:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// executeCompositeRoute executes stack with w and r. This function handles
|
|
|
|
// the special ErrRehandle error value, which reprocesses requests through
|
|
|
|
// the stack again. Any error value returned from this function would be an
|
|
|
|
// actual error that needs to be handled.
|
|
|
|
func (s *Server) executeCompositeRoute(w http.ResponseWriter, r *http.Request, stack Handler) error {
|
|
|
|
var err error
|
|
|
|
for i := -1; i <= s.MaxRehandles; i++ {
|
|
|
|
// we started the counter at -1 because we
|
|
|
|
// always want to run this at least once
|
|
|
|
err = stack.ServeHTTP(w, r)
|
|
|
|
if err != ErrRehandle {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i >= s.MaxRehandles-1 {
|
|
|
|
return fmt.Errorf("too many rehandles")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-23 04:14:26 +08:00
|
|
|
func (s *Server) listenersUseAnyPortOtherThan(otherPort int) bool {
|
|
|
|
for _, lnAddr := range s.Listen {
|
|
|
|
_, addrs, err := parseListenAddr(lnAddr)
|
|
|
|
if err == nil {
|
|
|
|
for _, a := range addrs {
|
|
|
|
_, port, err := net.SplitHostPort(a)
|
|
|
|
if err == nil && port != strconv.Itoa(otherPort) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-21 00:59:20 +08:00
|
|
|
type httpErrorConfig struct {
|
2019-05-23 02:32:36 +08:00
|
|
|
Routes RouteList `json:"routes,omitempty"`
|
2019-05-21 00:59:20 +08:00
|
|
|
// TODO: some way to configure the logging of errors, probably? standardize
|
|
|
|
// the logging configuration first.
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableCtxKey is the context key for the request's variable table.
|
|
|
|
const TableCtxKey caddy2.CtxKey = "table"
|