2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-03-26 13:00:54 -05:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
2019-08-09 13:05:47 -05:00
|
|
|
"bytes"
|
2019-03-26 20:42:52 -05:00
|
|
|
"context"
|
2019-04-25 14:54:48 -05:00
|
|
|
"crypto/tls"
|
2019-08-09 13:05:47 -05:00
|
|
|
"encoding/json"
|
2019-03-26 16:45:51 -05:00
|
|
|
"fmt"
|
2019-08-09 13:05:47 -05:00
|
|
|
"io"
|
2019-05-20 16:46:34 -05:00
|
|
|
weakrand "math/rand"
|
2019-03-26 16:45:51 -05:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2019-03-26 13:00:54 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-09-10 09:03:37 -05:00
|
|
|
"github.com/lucas-clemente/quic-go/http3"
|
2019-10-28 15:39:37 -05:00
|
|
|
"go.uber.org/zap"
|
2019-03-26 13:00:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-05-20 16:46:34 -05:00
|
|
|
weakrand.Seed(time.Now().UnixNano())
|
2019-04-25 14:54:48 -05:00
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
err := caddy.RegisterModule(App{})
|
2019-03-26 13:00:54 -05:00
|
|
|
if err != nil {
|
2019-10-28 15:39:37 -05:00
|
|
|
caddy.Log().Fatal(err.Error())
|
2019-03-26 13:00:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:16:34 -05:00
|
|
|
// App is a robust, production-ready HTTP server.
|
|
|
|
//
|
|
|
|
// HTTPS is enabled by default if host matchers with qualifying names are used
|
|
|
|
// in any of routes; certificates are automatically provisioned and renewed.
|
|
|
|
// Additionally, automatic HTTPS will also enable HTTPS for servers that listen
|
|
|
|
// only on the HTTPS port but which do not have any TLS connection policies
|
|
|
|
// defined by adding a good, default TLS connection policy.
|
|
|
|
//
|
2020-01-13 18:16:20 -05:00
|
|
|
// In HTTP routes, additional placeholders are available (replace any `*`):
|
2019-12-29 15:16:34 -05:00
|
|
|
//
|
|
|
|
// Placeholder | Description
|
|
|
|
// ------------|---------------
|
|
|
|
// `{http.request.cookie.*}` | HTTP request cookie
|
|
|
|
// `{http.request.header.*}` | Specific request header field
|
|
|
|
// `{http.request.host.labels.*}` | Request host labels (0-based from right); e.g. for foo.example.com: 0=com, 1=example, 2=foo
|
|
|
|
// `{http.request.host}` | The host part of the request's Host header
|
|
|
|
// `{http.request.hostport}` | The host and port from the request's Host header
|
|
|
|
// `{http.request.method}` | The request method
|
2020-02-04 17:49:38 -05:00
|
|
|
// `{http.request.orig_method}` | The request's original method
|
|
|
|
// `{http.request.orig_uri.path.dir}` | The request's original directory
|
|
|
|
// `{http.request.orig_uri.path.file}` | The request's original filename
|
|
|
|
// `{http.request.orig_uri.path}` | The request's original path
|
|
|
|
// `{http.request.orig_uri.query}` | The request's original query string (without `?`)
|
|
|
|
// `{http.request.orig_uri}` | The request's original URI
|
2019-12-29 15:16:34 -05:00
|
|
|
// `{http.request.port}` | The port part of the request's Host header
|
|
|
|
// `{http.request.proto}` | The protocol of the request
|
|
|
|
// `{http.request.remote.host}` | The host part of the remote client's address
|
|
|
|
// `{http.request.remote.port}` | The port part of the remote client's address
|
|
|
|
// `{http.request.remote}` | The address of the remote client
|
|
|
|
// `{http.request.scheme}` | The request scheme
|
|
|
|
// `{http.request.uri.path.*}` | Parts of the path, split by `/` (0-based from left)
|
|
|
|
// `{http.request.uri.path.dir}` | The directory, excluding leaf filename
|
|
|
|
// `{http.request.uri.path.file}` | The filename of the path, excluding directory
|
|
|
|
// `{http.request.uri.path}` | The path component of the request URI
|
|
|
|
// `{http.request.uri.query.*}` | Individual query string value
|
|
|
|
// `{http.request.uri.query}` | The query string (without `?`)
|
|
|
|
// `{http.request.uri}` | The full request URI
|
|
|
|
// `{http.response.header.*}` | Specific response header field
|
|
|
|
// `{http.vars.*}` | Custom variables in the HTTP handler chain
|
2019-05-14 15:14:05 -05:00
|
|
|
type App struct {
|
2019-12-10 15:36:46 -05:00
|
|
|
// HTTPPort specifies the port to use for HTTP (as opposed to HTTPS),
|
|
|
|
// which is used when setting up HTTP->HTTPS redirects or ACME HTTP
|
|
|
|
// challenge solvers. Default: 80.
|
|
|
|
HTTPPort int `json:"http_port,omitempty"`
|
|
|
|
|
|
|
|
// HTTPSPort specifies the port to use for HTTPS, which is used when
|
|
|
|
// solving the ACME TLS-ALPN challenges, or whenever HTTPS is needed
|
|
|
|
// but no specific port number is given. Default: 443.
|
|
|
|
HTTPSPort int `json:"https_port,omitempty"`
|
|
|
|
|
|
|
|
// GracePeriod is how long to wait for active connections when shutting
|
|
|
|
// down the server. Once the grace period is over, connections will
|
|
|
|
// be forcefully closed.
|
|
|
|
GracePeriod caddy.Duration `json:"grace_period,omitempty"`
|
|
|
|
|
|
|
|
// Servers is the list of servers, keyed by arbitrary names chosen
|
|
|
|
// at your discretion for your own convenience; the keys do not
|
|
|
|
// affect functionality.
|
|
|
|
Servers map[string]*Server `json:"servers,omitempty"`
|
2019-03-26 20:42:52 -05:00
|
|
|
|
2019-09-10 09:03:37 -05:00
|
|
|
servers []*http.Server
|
|
|
|
h3servers []*http3.Server
|
|
|
|
h3listeners []net.PacketConn
|
2019-05-16 17:05:38 -05:00
|
|
|
|
2019-10-28 15:39:37 -05:00
|
|
|
ctx caddy.Context
|
|
|
|
logger *zap.Logger
|
2019-03-26 13:00:54 -05:00
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (App) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http",
|
|
|
|
New: func() caddy.Module { return new(App) },
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:14:05 -05:00
|
|
|
// Provision sets up the app.
|
2019-06-14 12:58:28 -05:00
|
|
|
func (app *App) Provision(ctx caddy.Context) error {
|
2019-05-16 17:05:38 -05:00
|
|
|
app.ctx = ctx
|
2019-10-28 15:39:37 -05:00
|
|
|
app.logger = ctx.Logger(app)
|
2019-05-16 17:05:38 -05:00
|
|
|
|
2019-06-14 12:58:28 -05:00
|
|
|
repl := caddy.NewReplacer()
|
2019-05-20 11:59:20 -05:00
|
|
|
|
2020-01-13 18:16:20 -05:00
|
|
|
// this provisions the matchers for each route,
|
|
|
|
// and prepares auto HTTP->HTTP redirects, and
|
|
|
|
// is required before we provision each server
|
|
|
|
err := app.automaticHTTPSPhase1(ctx, repl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:39:37 -05:00
|
|
|
for srvName, srv := range app.Servers {
|
|
|
|
srv.logger = app.logger.Named("log")
|
|
|
|
srv.errorLogger = app.logger.Named("log.error")
|
|
|
|
|
2019-11-15 19:01:07 -05:00
|
|
|
// only enable access logs if configured
|
|
|
|
if srv.Logs != nil {
|
|
|
|
srv.accessLogger = app.logger.Named("log.access")
|
|
|
|
}
|
|
|
|
|
2019-09-18 00:13:21 -05:00
|
|
|
// if not explicitly configured by the user, disallow TLS
|
|
|
|
// client auth bypass (domain fronting) which could
|
|
|
|
// otherwise be exploited by sending an unprotected SNI
|
|
|
|
// value during a TLS handshake, then putting a protected
|
|
|
|
// domain in the Host header after establishing connection;
|
|
|
|
// this is a safe default, but we allow users to override
|
|
|
|
// it for example in the case of running a proxy where
|
|
|
|
// domain fronting is desired and access is not restricted
|
|
|
|
// based on hostname
|
|
|
|
if srv.StrictSNIHost == nil && srv.hasTLSClientAuth() {
|
2020-01-13 18:16:20 -05:00
|
|
|
app.logger.Info("enabling strict SNI-Host matching because TLS client auth is configured",
|
|
|
|
zap.String("server_name", srvName),
|
|
|
|
)
|
2019-09-18 00:13:21 -05:00
|
|
|
trueBool := true
|
|
|
|
srv.StrictSNIHost = &trueBool
|
2019-09-03 10:35:36 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:59:20 -05:00
|
|
|
for i := range srv.Listen {
|
2019-10-28 15:39:37 -05:00
|
|
|
lnOut, err := repl.ReplaceOrErr(srv.Listen[i], true, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("server %s, listener %d: %v",
|
|
|
|
srvName, i, err)
|
|
|
|
}
|
|
|
|
srv.Listen[i] = lnOut
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
2019-06-26 11:49:32 -05:00
|
|
|
|
2020-01-13 18:16:20 -05:00
|
|
|
// pre-compile the primary handler chain, and be sure to wrap it in our
|
|
|
|
// route handler so that important security checks are done, etc.
|
2020-01-11 02:33:47 -05:00
|
|
|
primaryRoute := emptyHandler
|
2019-05-22 14:13:39 -05:00
|
|
|
if srv.Routes != nil {
|
2020-01-13 18:16:20 -05:00
|
|
|
err := srv.Routes.ProvisionHandlers(ctx)
|
2019-05-22 14:13:39 -05:00
|
|
|
if err != nil {
|
2020-01-13 18:16:20 -05:00
|
|
|
return fmt.Errorf("server %s: setting up route handlers: %v", srvName, err)
|
2019-05-22 14:13:39 -05:00
|
|
|
}
|
2020-01-12 15:39:32 -05:00
|
|
|
primaryRoute = srv.Routes.Compile(emptyHandler)
|
2019-04-11 21:42:55 -05:00
|
|
|
}
|
2020-01-11 02:33:47 -05:00
|
|
|
srv.primaryHandlerChain = srv.wrapPrimaryRoute(primaryRoute)
|
2019-06-26 11:49:32 -05:00
|
|
|
|
2020-01-13 18:16:20 -05:00
|
|
|
// pre-compile the error handler chain
|
2019-05-22 14:13:39 -05:00
|
|
|
if srv.Errors != nil {
|
|
|
|
err := srv.Errors.Routes.Provision(ctx)
|
|
|
|
if err != nil {
|
2019-10-28 15:39:37 -05:00
|
|
|
return fmt.Errorf("server %s: setting up server error handling routes: %v", srvName, err)
|
2019-05-22 14:13:39 -05:00
|
|
|
}
|
2020-02-20 17:00:30 -05:00
|
|
|
|
|
|
|
srv.errorHandlerChain = srv.Errors.Routes.Compile(errorEmptyHandler)
|
2019-07-11 23:02:47 -05:00
|
|
|
}
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
2019-03-31 21:41:29 -05:00
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:14:05 -05:00
|
|
|
// Validate ensures the app's configuration is valid.
|
2019-05-16 17:05:38 -05:00
|
|
|
func (app *App) Validate() error {
|
2019-05-07 10:56:13 -05:00
|
|
|
// each server must use distinct listener addresses
|
|
|
|
lnAddrs := make(map[string]string)
|
2019-05-16 17:05:38 -05:00
|
|
|
for srvName, srv := range app.Servers {
|
2019-05-07 10:56:13 -05:00
|
|
|
for _, addr := range srv.Listen {
|
2019-11-11 17:33:38 -05:00
|
|
|
listenAddr, err := caddy.ParseNetworkAddress(addr)
|
2019-05-07 10:56:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid listener address '%s': %v", addr, err)
|
|
|
|
}
|
2019-11-11 17:33:38 -05:00
|
|
|
// check that every address in the port range is unique to this server;
|
|
|
|
// we do not use <= here because PortRangeSize() adds 1 to EndPort for us
|
|
|
|
for i := uint(0); i < listenAddr.PortRangeSize(); i++ {
|
|
|
|
addr := caddy.JoinNetworkAddress(listenAddr.Network, listenAddr.Host, strconv.Itoa(int(listenAddr.StartPort+i)))
|
|
|
|
if sn, ok := lnAddrs[addr]; ok {
|
|
|
|
return fmt.Errorf("server %s: listener address repeated: %s (already claimed by server '%s')", srvName, addr, sn)
|
2019-05-07 10:56:13 -05:00
|
|
|
}
|
2019-11-11 17:33:38 -05:00
|
|
|
lnAddrs[addr] = srvName
|
2019-05-07 10:56:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 18:16:20 -05:00
|
|
|
// Start runs the app. It finishes automatic HTTPS if enabled,
|
|
|
|
// including management of certificates.
|
2019-05-16 17:05:38 -05:00
|
|
|
func (app *App) Start() error {
|
2020-02-05 19:34:28 -05:00
|
|
|
// give each server a pointer to the TLS app;
|
|
|
|
// this is required before they are started so
|
|
|
|
// they can solve ACME challenges
|
2020-01-13 18:16:20 -05:00
|
|
|
err := app.automaticHTTPSPhase2()
|
2019-04-25 14:54:48 -05:00
|
|
|
if err != nil {
|
2020-02-05 19:34:28 -05:00
|
|
|
return fmt.Errorf("enabling automatic HTTPS, phase 2: %v", err)
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
2019-05-16 17:05:38 -05:00
|
|
|
for srvName, srv := range app.Servers {
|
2019-03-26 20:42:52 -05:00
|
|
|
s := &http.Server{
|
2019-03-26 16:45:51 -05:00
|
|
|
ReadTimeout: time.Duration(srv.ReadTimeout),
|
|
|
|
ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout),
|
2019-05-23 14:16:34 -05:00
|
|
|
WriteTimeout: time.Duration(srv.WriteTimeout),
|
|
|
|
IdleTimeout: time.Duration(srv.IdleTimeout),
|
|
|
|
MaxHeaderBytes: srv.MaxHeaderBytes,
|
2019-03-31 21:41:29 -05:00
|
|
|
Handler: srv,
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, lnAddr := range srv.Listen {
|
2019-11-11 17:33:38 -05:00
|
|
|
listenAddr, err := caddy.ParseNetworkAddress(lnAddr)
|
2019-03-26 16:45:51 -05:00
|
|
|
if err != nil {
|
2019-04-25 14:54:48 -05:00
|
|
|
return fmt.Errorf("%s: parsing listen address '%s': %v", srvName, lnAddr, err)
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
2020-01-13 18:16:20 -05:00
|
|
|
for portOffset := uint(0); portOffset < listenAddr.PortRangeSize(); portOffset++ {
|
|
|
|
hostport := listenAddr.JoinHostPort(portOffset)
|
2019-11-11 17:33:38 -05:00
|
|
|
ln, err := caddy.Listen(listenAddr.Network, hostport)
|
2019-03-26 16:45:51 -05:00
|
|
|
if err != nil {
|
2019-11-11 17:33:38 -05:00
|
|
|
return fmt.Errorf("%s: listening on %s: %v", listenAddr.Network, hostport, err)
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
2019-04-25 14:54:48 -05:00
|
|
|
|
2019-09-03 10:35:36 -05:00
|
|
|
// enable HTTP/2 by default
|
2019-04-25 14:54:48 -05:00
|
|
|
for _, pol := range srv.TLSConnPolicies {
|
|
|
|
if len(pol.ALPN) == 0 {
|
|
|
|
pol.ALPN = append(pol.ALPN, defaultALPN...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 18:16:20 -05:00
|
|
|
// enable TLS if there is a policy and if this is not the HTTP port
|
|
|
|
if len(srv.TLSConnPolicies) > 0 &&
|
|
|
|
int(listenAddr.StartPort+portOffset) != app.httpPort() {
|
|
|
|
// create TLS listener
|
2019-05-16 17:05:38 -05:00
|
|
|
tlsCfg, err := srv.TLSConnPolicies.TLSConfig(app.ctx)
|
2019-04-25 14:54:48 -05:00
|
|
|
if err != nil {
|
2019-11-11 17:33:38 -05:00
|
|
|
return fmt.Errorf("%s/%s: making TLS configuration: %v", listenAddr.Network, hostport, err)
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
ln = tls.NewListener(ln, tlsCfg)
|
2019-09-10 09:03:37 -05:00
|
|
|
|
|
|
|
/////////
|
|
|
|
// TODO: HTTP/3 support is experimental for now
|
|
|
|
if srv.ExperimentalHTTP3 {
|
2019-10-28 15:39:37 -05:00
|
|
|
app.logger.Info("enabling experimental HTTP/3 listener",
|
2019-11-11 17:33:38 -05:00
|
|
|
zap.String("addr", hostport),
|
2019-10-28 15:39:37 -05:00
|
|
|
)
|
2019-11-11 17:33:38 -05:00
|
|
|
h3ln, err := caddy.ListenPacket("udp", hostport)
|
2019-09-10 09:03:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting HTTP/3 UDP listener: %v", err)
|
|
|
|
}
|
|
|
|
h3srv := &http3.Server{
|
|
|
|
Server: &http.Server{
|
2019-11-11 17:33:38 -05:00
|
|
|
Addr: hostport,
|
2019-09-10 09:03:37 -05:00
|
|
|
Handler: srv,
|
|
|
|
TLSConfig: tlsCfg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
go h3srv.Serve(h3ln)
|
|
|
|
app.h3servers = append(app.h3servers, h3srv)
|
|
|
|
app.h3listeners = append(app.h3listeners, h3ln)
|
2019-09-11 19:49:21 -05:00
|
|
|
srv.h3server = h3srv
|
2019-09-10 09:03:37 -05:00
|
|
|
}
|
|
|
|
/////////
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
go s.Serve(ln)
|
2019-05-16 17:05:38 -05:00
|
|
|
app.servers = append(app.servers, s)
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:34:28 -05:00
|
|
|
// finish automatic HTTPS by finally beginning
|
|
|
|
// certificate management
|
|
|
|
err = app.automaticHTTPSPhase3()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("finalizing automatic HTTPS: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-07 10:56:13 -05:00
|
|
|
// Stop gracefully shuts down the HTTP server.
|
2019-05-16 17:05:38 -05:00
|
|
|
func (app *App) Stop() error {
|
2019-05-07 10:56:13 -05:00
|
|
|
ctx := context.Background()
|
2019-05-16 17:05:38 -05:00
|
|
|
if app.GracePeriod > 0 {
|
2019-05-07 10:56:13 -05:00
|
|
|
var cancel context.CancelFunc
|
2019-05-16 17:05:38 -05:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(app.GracePeriod))
|
2019-05-07 10:56:13 -05:00
|
|
|
defer cancel()
|
|
|
|
}
|
2019-05-16 17:05:38 -05:00
|
|
|
for _, s := range app.servers {
|
2019-05-07 10:56:13 -05:00
|
|
|
err := s.Shutdown(ctx)
|
2019-03-26 20:42:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 12:39:34 -05:00
|
|
|
|
|
|
|
// close the http3 servers; it's unclear whether the bug reported in
|
|
|
|
// https://github.com/caddyserver/caddy/pull/2727#issuecomment-526856566
|
|
|
|
// was ever truly fixed, since it seemed racey/nondeterministic; but
|
|
|
|
// recent tests in 2020 were unable to replicate the issue again after
|
|
|
|
// repeated attempts (the bug manifested after a config reload; i.e.
|
|
|
|
// reusing a http3 server or listener was problematic), but it seems
|
|
|
|
// to be working fine now
|
|
|
|
for _, s := range app.h3servers {
|
|
|
|
// TODO: CloseGracefully, once implemented upstream
|
|
|
|
// (see https://github.com/lucas-clemente/quic-go/issues/2103)
|
|
|
|
err := s.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// closing an http3.Server does not close their underlying listeners
|
|
|
|
// since apparently the listener can be used both by servers and
|
|
|
|
// clients at the same time; so we need to manually call Close()
|
|
|
|
// on the underlying h3 listeners (see lucas-clemente/quic-go#2103)
|
2019-09-10 09:03:37 -05:00
|
|
|
for _, pc := range app.h3listeners {
|
|
|
|
err := pc.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 20:42:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-18 19:01:32 -05:00
|
|
|
func (app *App) httpPort() int {
|
|
|
|
if app.HTTPPort == 0 {
|
|
|
|
return DefaultHTTPPort
|
|
|
|
}
|
|
|
|
return app.HTTPPort
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) httpsPort() int {
|
|
|
|
if app.HTTPSPort == 0 {
|
|
|
|
return DefaultHTTPSPort
|
2019-05-07 10:56:13 -05:00
|
|
|
}
|
2019-09-18 19:01:32 -05:00
|
|
|
return app.HTTPSPort
|
2019-05-07 10:56:13 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
var defaultALPN = []string{"h2", "http/1.1"}
|
|
|
|
|
2019-05-20 11:59:20 -05:00
|
|
|
// RequestMatcher is a type that can match to a request.
|
2019-07-09 13:58:39 -05:00
|
|
|
// A route matcher MUST NOT modify the request, with the
|
|
|
|
// only exception being its context.
|
2019-05-20 11:59:20 -05:00
|
|
|
type RequestMatcher interface {
|
2019-03-31 21:41:29 -05:00
|
|
|
Match(*http.Request) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler is like http.Handler except ServeHTTP may return an error.
|
|
|
|
//
|
|
|
|
// If any handler encounters an error, it should be returned for proper
|
|
|
|
// handling. Return values should be propagated down the middleware chain
|
2019-07-09 13:58:39 -05:00
|
|
|
// by returning it unchanged. Returned errors should not be re-wrapped
|
|
|
|
// if they are already HandlerError values.
|
2019-03-31 21:41:29 -05:00
|
|
|
type Handler interface {
|
|
|
|
ServeHTTP(http.ResponseWriter, *http.Request) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandlerFunc is a convenience type like http.HandlerFunc.
|
|
|
|
type HandlerFunc func(http.ResponseWriter, *http.Request) error
|
|
|
|
|
|
|
|
// ServeHTTP implements the Handler interface.
|
|
|
|
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
return f(w, r)
|
|
|
|
}
|
|
|
|
|
2019-07-09 13:58:39 -05:00
|
|
|
// Middleware chains one Handler to the next by being passed
|
|
|
|
// the next Handler in the chain.
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-09 12:00:13 -05:00
|
|
|
type Middleware func(Handler) Handler
|
2019-07-09 13:58:39 -05:00
|
|
|
|
|
|
|
// MiddlewareHandler is like Handler except it takes as a third
|
|
|
|
// argument the next handler in the chain. The next handler will
|
|
|
|
// never be nil, but may be a no-op handler if this is the last
|
|
|
|
// handler in the chain. Handlers which act as middleware should
|
|
|
|
// call the next handler's ServeHTTP method so as to propagate
|
|
|
|
// the request down the chain properly. Handlers which act as
|
|
|
|
// responders (content origins) need not invoke the next handler,
|
|
|
|
// since the last handler in the chain should be the first to
|
|
|
|
// write the response.
|
|
|
|
type MiddlewareHandler interface {
|
|
|
|
ServeHTTP(http.ResponseWriter, *http.Request, Handler) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// emptyHandler is used as a no-op handler.
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-09 12:00:13 -05:00
|
|
|
var emptyHandler Handler = HandlerFunc(func(http.ResponseWriter, *http.Request) error { return nil })
|
2019-04-11 21:42:55 -05:00
|
|
|
|
2020-02-20 17:00:30 -05:00
|
|
|
// An implicit suffix middleware that, if reached, sets the StatusCode to the
|
|
|
|
// error stored in the ErrorCtxKey. This is to prevent situations where the
|
|
|
|
// Error chain does not actually handle the error (for instance, it matches only
|
|
|
|
// on some errors). See #3053
|
|
|
|
var errorEmptyHandler Handler = HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
httpError := r.Context().Value(ErrorCtxKey)
|
|
|
|
if handlerError, ok := httpError.(HandlerError); ok {
|
|
|
|
w.WriteHeader(handlerError.StatusCode)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-08-09 13:05:47 -05:00
|
|
|
// WeakString is a type that unmarshals any JSON value
|
|
|
|
// as a string literal, with the following exceptions:
|
2019-12-10 15:36:46 -05:00
|
|
|
//
|
|
|
|
// 1. actual string values are decoded as strings; and
|
|
|
|
// 2. null is decoded as empty string;
|
|
|
|
//
|
2019-08-09 13:05:47 -05:00
|
|
|
// and provides methods for getting the value as various
|
|
|
|
// primitive types. However, using this type removes any
|
|
|
|
// type safety as far as deserializing JSON is concerned.
|
|
|
|
type WeakString string
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler according to
|
|
|
|
// this type's documentation.
|
|
|
|
func (ws *WeakString) UnmarshalJSON(b []byte) error {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(b, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*ws = WeakString(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if bytes.Equal(b, []byte("null")) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*ws = WeakString(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals was a boolean if true or false,
|
|
|
|
// a number if an integer, or a string otherwise.
|
|
|
|
func (ws WeakString) MarshalJSON() ([]byte, error) {
|
|
|
|
if ws == "true" {
|
|
|
|
return []byte("true"), nil
|
|
|
|
}
|
|
|
|
if ws == "false" {
|
|
|
|
return []byte("false"), nil
|
|
|
|
}
|
|
|
|
if num, err := strconv.Atoi(string(ws)); err == nil {
|
|
|
|
return json.Marshal(num)
|
|
|
|
}
|
|
|
|
return json.Marshal(string(ws))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int returns ws as an integer. If ws is not an
|
|
|
|
// integer, 0 is returned.
|
|
|
|
func (ws WeakString) Int() int {
|
|
|
|
num, _ := strconv.Atoi(string(ws))
|
|
|
|
return num
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float64 returns ws as a float64. If ws is not a
|
|
|
|
// float value, the zero value is returned.
|
|
|
|
func (ws WeakString) Float64() float64 {
|
|
|
|
num, _ := strconv.ParseFloat(string(ws), 64)
|
|
|
|
return num
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bool returns ws as a boolean. If ws is not a
|
|
|
|
// boolean, false is returned.
|
|
|
|
func (ws WeakString) Bool() bool {
|
|
|
|
return string(ws) == "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns ws as a string.
|
|
|
|
func (ws WeakString) String() string {
|
|
|
|
return string(ws)
|
|
|
|
}
|
|
|
|
|
2019-10-15 15:07:10 -05:00
|
|
|
// CopyHeader copies HTTP headers by completely
|
|
|
|
// replacing dest with src. (This allows deletions
|
|
|
|
// to be propagated, assuming src started as a
|
|
|
|
// consistent copy of dest.)
|
|
|
|
func CopyHeader(dest, src http.Header) {
|
|
|
|
for field := range dest {
|
|
|
|
delete(dest, field)
|
|
|
|
}
|
|
|
|
for field, val := range src {
|
|
|
|
dest[field] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// StatusCodeMatches returns true if a real HTTP status code matches
|
|
|
|
// the configured status code, which may be either a real HTTP status
|
|
|
|
// code or an integer representing a class of codes (e.g. 4 for all
|
|
|
|
// 4xx statuses).
|
|
|
|
func StatusCodeMatches(actual, configured int) bool {
|
|
|
|
if actual == configured {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-13 18:16:20 -05:00
|
|
|
if configured < 100 &&
|
|
|
|
actual >= configured*100 &&
|
|
|
|
actual < (configured+1)*100 {
|
2019-09-02 23:01:02 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-07 10:56:13 -05:00
|
|
|
const (
|
|
|
|
// DefaultHTTPPort is the default port for HTTP.
|
|
|
|
DefaultHTTPPort = 80
|
|
|
|
|
|
|
|
// DefaultHTTPSPort is the default port for HTTPS.
|
|
|
|
DefaultHTTPSPort = 443
|
|
|
|
)
|
|
|
|
|
2019-07-07 15:12:22 -05:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.App = (*App)(nil)
|
|
|
|
_ caddy.Provisioner = (*App)(nil)
|
|
|
|
_ caddy.Validator = (*App)(nil)
|
|
|
|
)
|