2020-03-16 06:28:42 +03: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.
|
|
|
|
|
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2024-09-30 19:55:03 +03:00
|
|
|
"maps"
|
2022-09-28 22:35:51 +03:00
|
|
|
"net"
|
2020-03-16 06:28:42 +03:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-08-03 20:04:51 +03:00
|
|
|
"sync"
|
2020-03-16 06:28:42 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2020-05-05 21:33:21 +03:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2023-08-14 18:41:15 +03:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyevents"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
2020-03-16 06:28:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-04-13 18:48:54 +03:00
|
|
|
caddy.RegisterModule(App{})
|
2020-03-16 06:28:42 +03: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.
|
|
|
|
//
|
|
|
|
// In HTTP routes, additional placeholders are available (replace any `*`):
|
|
|
|
//
|
|
|
|
// Placeholder | Description
|
|
|
|
// ------------|---------------
|
2020-07-17 04:25:37 +03:00
|
|
|
// `{http.request.body}` | The request body (⚠️ inefficient; use only for debugging)
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.request.cookie.*}` | HTTP request cookie
|
2021-02-22 21:57:21 +03:00
|
|
|
// `{http.request.duration}` | Time up to now spent handling the request (after decoding headers from client)
|
2022-04-11 22:04:05 +03:00
|
|
|
// `{http.request.duration_ms}` | Same as 'duration', but in milliseconds.
|
2021-12-15 10:17:53 +03:00
|
|
|
// `{http.request.uuid}` | The request unique identifier
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.request.header.*}` | Specific request header field
|
|
|
|
// `{http.request.host}` | The host part of the request's Host header
|
2023-02-08 20:49:17 +03:00
|
|
|
// `{http.request.host.labels.*}` | Request host labels (0-based from right); e.g. for foo.example.com: 0=com, 1=example, 2=foo
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.request.hostport}` | The host and port from the request's Host header
|
|
|
|
// `{http.request.method}` | The request method
|
|
|
|
// `{http.request.orig_method}` | The request's original method
|
2023-02-08 20:49:17 +03:00
|
|
|
// `{http.request.orig_uri}` | The request's original URI
|
|
|
|
// `{http.request.orig_uri.path}` | The request's original path
|
|
|
|
// `{http.request.orig_uri.path.*}` | Parts of the original path, split by `/` (0-based from left)
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{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.query}` | The request's original query string (without `?`)
|
|
|
|
// `{http.request.port}` | The port part of the request's Host header
|
|
|
|
// `{http.request.proto}` | The protocol of the request
|
caddyhttp: add http.request.local{,.host,.port} placeholder (#6182)
* caddyhttp: add `http.request.local{,.host,.port}` placeholder
This is the counterpart of `http.request.remote{,.host,.port}`.
`http.request.remote` operates on the remote client's address, while
`http.request.local` operates on the address the connection arrived on.
Take the following example:
- Caddy serving on `203.0.113.1:80`
- Client on `203.0.113.2`
`http.request.remote.host` would return `203.0.113.2` (client IP)
`http.request.local.host` would return `203.0.113.1` (server IP)
`http.request.local.port` would return `80` (server port)
I find this helpful for debugging setups with multiple servers and/or
multiple network paths (multiple IPs, AnyIP, Anycast).
Co-authored-by: networkException <git@nwex.de>
* caddyhttp: add unit test for `http.request.local{,.host,.port}`
* caddyhttp: add integration test for `http.request.local.port`
* caddyhttp: fix `http.request.local.host` placeholder handling with unix sockets
The implementation matches the one of `http.request.remote.host` now and
returns the unix socket path (just like `http.request.local` already did)
instead of an empty string.
---------
Co-authored-by: networkException <git@nwex.de>
2024-03-28 00:36:53 +03:00
|
|
|
// `{http.request.local.host}` | The host (IP) part of the local address the connection arrived on
|
|
|
|
// `{http.request.local.port}` | The port part of the local address the connection arrived on
|
|
|
|
// `{http.request.local}` | The local address the connection arrived on
|
2022-10-12 18:27:08 +03:00
|
|
|
// `{http.request.remote.host}` | The host (IP) part of the remote client's address
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.request.remote.port}` | The port part of the remote client's address
|
|
|
|
// `{http.request.remote}` | The address of the remote client
|
2023-10-23 00:47:16 +03:00
|
|
|
// `{http.request.scheme}` | The request scheme, typically `http` or `https`
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.request.tls.version}` | The TLS version name
|
|
|
|
// `{http.request.tls.cipher_suite}` | The TLS cipher suite
|
|
|
|
// `{http.request.tls.resumed}` | The TLS connection resumed a previous connection
|
|
|
|
// `{http.request.tls.proto}` | The negotiated next protocol
|
|
|
|
// `{http.request.tls.proto_mutual}` | The negotiated next protocol was advertised by the server
|
|
|
|
// `{http.request.tls.server_name}` | The server name requested by the client, if any
|
|
|
|
// `{http.request.tls.client.fingerprint}` | The SHA256 checksum of the client certificate
|
2020-06-12 01:19:07 +03:00
|
|
|
// `{http.request.tls.client.public_key}` | The public key of the client certificate.
|
|
|
|
// `{http.request.tls.client.public_key_sha256}` | The SHA256 checksum of the client's public key.
|
2020-11-04 21:37:41 +03:00
|
|
|
// `{http.request.tls.client.certificate_pem}` | The PEM-encoded value of the certificate.
|
2021-10-02 01:27:29 +03:00
|
|
|
// `{http.request.tls.client.certificate_der_base64}` | The base64-encoded value of the certificate.
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.request.tls.client.issuer}` | The issuer DN of the client certificate
|
|
|
|
// `{http.request.tls.client.serial}` | The serial number of the client certificate
|
|
|
|
// `{http.request.tls.client.subject}` | The subject DN of the client certificate
|
2020-06-12 01:19:07 +03:00
|
|
|
// `{http.request.tls.client.san.dns_names.*}` | SAN DNS names(index optional)
|
|
|
|
// `{http.request.tls.client.san.emails.*}` | SAN email addresses (index optional)
|
|
|
|
// `{http.request.tls.client.san.ips.*}` | SAN IP addresses (index optional)
|
|
|
|
// `{http.request.tls.client.san.uris.*}` | SAN URIs (index optional)
|
2023-02-08 20:49:17 +03:00
|
|
|
// `{http.request.uri}` | The full request URI
|
|
|
|
// `{http.request.uri.path}` | The path component of the request URI
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{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.query}` | The query string (without `?`)
|
2023-02-08 20:49:17 +03:00
|
|
|
// `{http.request.uri.query.*}` | Individual query string value
|
2020-03-16 06:28:42 +03:00
|
|
|
// `{http.response.header.*}` | Specific response header field
|
|
|
|
// `{http.vars.*}` | Custom variables in the HTTP handler chain
|
2022-08-03 20:04:51 +03:00
|
|
|
// `{http.shutting_down}` | True if the HTTP app is shutting down
|
|
|
|
// `{http.time_until_shutdown}` | Time until HTTP server shutdown, if scheduled
|
2020-03-16 06:28:42 +03:00
|
|
|
type App struct {
|
|
|
|
// 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
|
2022-08-03 20:04:51 +03:00
|
|
|
// down the servers. During the grace period, no new connections are
|
|
|
|
// accepted, idle connections are closed, and active connections will
|
|
|
|
// be given the full length of time to become idle and close.
|
|
|
|
// Once the grace period is over, connections will be forcefully closed.
|
|
|
|
// If zero, the grace period is eternal. Default: 0.
|
2020-03-16 06:28:42 +03:00
|
|
|
GracePeriod caddy.Duration `json:"grace_period,omitempty"`
|
|
|
|
|
2022-08-03 20:04:51 +03:00
|
|
|
// ShutdownDelay is how long to wait before initiating the grace
|
|
|
|
// period. When this app is stopping (e.g. during a config reload or
|
|
|
|
// process exit), all servers will be shut down. Normally this immediately
|
|
|
|
// initiates the grace period. However, if this delay is configured, servers
|
|
|
|
// will not be shut down until the delay is over. During this time, servers
|
|
|
|
// continue to function normally and allow new connections. At the end, the
|
|
|
|
// grace period will begin. This can be useful to allow downstream load
|
|
|
|
// balancers time to move this instance out of the rotation without hiccups.
|
|
|
|
//
|
|
|
|
// When shutdown has been scheduled, placeholders {http.shutting_down} (bool)
|
|
|
|
// and {http.time_until_shutdown} (duration) may be useful for health checks.
|
|
|
|
ShutdownDelay caddy.Duration `json:"shutdown_delay,omitempty"`
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
// 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"`
|
|
|
|
|
|
|
|
ctx caddy.Context
|
|
|
|
logger *zap.Logger
|
|
|
|
tlsApp *caddytls.TLS
|
|
|
|
|
|
|
|
// used temporarily between phases 1 and 2 of auto HTTPS
|
|
|
|
allCertDomains []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (App) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
ID: "http",
|
|
|
|
New: func() caddy.Module { return new(App) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision sets up the app.
|
|
|
|
func (app *App) Provision(ctx caddy.Context) error {
|
|
|
|
// store some references
|
|
|
|
tlsAppIface, err := ctx.App("tls")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting tls app: %v", err)
|
|
|
|
}
|
|
|
|
app.tlsApp = tlsAppIface.(*caddytls.TLS)
|
|
|
|
app.ctx = ctx
|
2022-09-17 01:55:30 +03:00
|
|
|
app.logger = ctx.Logger()
|
2020-03-16 06:28:42 +03:00
|
|
|
|
2022-09-01 00:01:30 +03:00
|
|
|
eventsAppIface, err := ctx.App("events")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting events app: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
|
|
|
|
// this provisions the matchers for each route,
|
|
|
|
// and prepares auto HTTP->HTTPS redirects, and
|
|
|
|
// is required before we provision each server
|
|
|
|
err = app.automaticHTTPSPhase1(ctx, repl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare each server
|
2022-09-16 23:48:55 +03:00
|
|
|
oldContext := ctx.Context
|
2020-03-16 06:28:42 +03:00
|
|
|
for srvName, srv := range app.Servers {
|
2022-09-16 23:48:55 +03:00
|
|
|
ctx.Context = context.WithValue(oldContext, ServerCtxKey, srv)
|
2020-09-18 06:46:24 +03:00
|
|
|
srv.name = srvName
|
2020-03-16 06:28:42 +03:00
|
|
|
srv.tlsApp = app.tlsApp
|
2022-09-01 00:01:30 +03:00
|
|
|
srv.events = eventsAppIface.(*caddyevents.App)
|
|
|
|
srv.ctx = ctx
|
2020-03-16 06:28:42 +03:00
|
|
|
srv.logger = app.logger.Named("log")
|
|
|
|
srv.errorLogger = app.logger.Named("log.error")
|
2022-08-03 20:04:51 +03:00
|
|
|
srv.shutdownAtMu = new(sync.RWMutex)
|
2020-03-16 06:28:42 +03:00
|
|
|
|
|
|
|
// only enable access logs if configured
|
|
|
|
if srv.Logs != nil {
|
|
|
|
srv.accessLogger = app.logger.Named("log.access")
|
2024-05-18 23:48:42 +03:00
|
|
|
if srv.Logs.Trace {
|
|
|
|
srv.traceLogger = app.logger.Named("log.trace")
|
|
|
|
}
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
// if no protocols configured explicitly, enable all except h2c
|
|
|
|
if len(srv.Protocols) == 0 {
|
|
|
|
srv.Protocols = []string{"h1", "h2", "h3"}
|
|
|
|
}
|
|
|
|
|
|
|
|
srvProtocolsUnique := map[string]struct{}{}
|
|
|
|
for _, srvProtocol := range srv.Protocols {
|
|
|
|
srvProtocolsUnique[srvProtocol] = struct{}{}
|
|
|
|
}
|
|
|
|
_, h1ok := srvProtocolsUnique["h1"]
|
|
|
|
_, h2ok := srvProtocolsUnique["h2"]
|
|
|
|
_, h2cok := srvProtocolsUnique["h2c"]
|
|
|
|
|
2022-08-15 21:01:58 +03:00
|
|
|
// the Go standard library does not let us serve only HTTP/2 using
|
|
|
|
// http.Server; we would probably need to write our own server
|
2024-09-30 19:55:03 +03:00
|
|
|
if !h1ok && (h2ok || h2cok) {
|
2022-08-15 21:01:58 +03:00
|
|
|
return fmt.Errorf("server %s: cannot enable HTTP/2 or H2C without enabling HTTP/1.1; add h1 to protocols or remove h2/h2c", srvName)
|
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
if srv.ListenProtocols != nil {
|
|
|
|
if len(srv.ListenProtocols) != len(srv.Listen) {
|
|
|
|
return fmt.Errorf("server %s: listener protocols count does not match address count: %d != %d",
|
|
|
|
srvName, len(srv.ListenProtocols), len(srv.Listen))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, lnProtocols := range srv.ListenProtocols {
|
|
|
|
if lnProtocols != nil {
|
|
|
|
// populate empty listen protocols with server protocols
|
|
|
|
lnProtocolsDefault := false
|
|
|
|
var lnProtocolsInclude []string
|
|
|
|
srvProtocolsInclude := maps.Clone(srvProtocolsUnique)
|
|
|
|
|
|
|
|
// keep existing listener protocols unless they are empty
|
|
|
|
for _, lnProtocol := range lnProtocols {
|
|
|
|
if lnProtocol == "" {
|
|
|
|
lnProtocolsDefault = true
|
|
|
|
} else {
|
|
|
|
lnProtocolsInclude = append(lnProtocolsInclude, lnProtocol)
|
|
|
|
delete(srvProtocolsInclude, lnProtocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append server protocols to listener protocols if any listener protocols were empty
|
|
|
|
if lnProtocolsDefault {
|
|
|
|
for _, srvProtocol := range srv.Protocols {
|
|
|
|
if _, ok := srvProtocolsInclude[srvProtocol]; ok {
|
|
|
|
lnProtocolsInclude = append(lnProtocolsInclude, srvProtocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lnProtocolsIncludeUnique := map[string]struct{}{}
|
|
|
|
for _, lnProtocol := range lnProtocolsInclude {
|
|
|
|
lnProtocolsIncludeUnique[lnProtocol] = struct{}{}
|
|
|
|
}
|
|
|
|
_, h1ok := lnProtocolsIncludeUnique["h1"]
|
|
|
|
_, h2ok := lnProtocolsIncludeUnique["h2"]
|
|
|
|
_, h2cok := lnProtocolsIncludeUnique["h2c"]
|
|
|
|
|
|
|
|
// check if any listener protocols contain h2 or h2c without h1
|
|
|
|
if !h1ok && (h2ok || h2cok) {
|
|
|
|
return fmt.Errorf("server %s, listener %d: cannot enable HTTP/2 or H2C without enabling HTTP/1.1; add h1 to protocols or remove h2/h2c", srvName, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.ListenProtocols[i] = lnProtocolsInclude
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 21:01:58 +03:00
|
|
|
}
|
|
|
|
|
2020-03-16 06:28:42 +03: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() {
|
2021-02-16 23:31:53 +03:00
|
|
|
app.logger.Warn("enabling strict SNI-Host enforcement because TLS client auth is configured",
|
2022-08-15 21:01:58 +03:00
|
|
|
zap.String("server_id", srvName))
|
2020-03-16 06:28:42 +03:00
|
|
|
trueBool := true
|
|
|
|
srv.StrictSNIHost = &trueBool
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:44:11 +03:00
|
|
|
// set up the trusted proxies source
|
|
|
|
for srv.TrustedProxiesRaw != nil {
|
|
|
|
val, err := ctx.LoadModule(srv, "TrustedProxiesRaw")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading trusted proxies modules: %v", err)
|
2023-01-10 08:08:23 +03:00
|
|
|
}
|
2023-02-06 22:44:11 +03:00
|
|
|
srv.trustedProxies = val.(IPRangeSource)
|
2023-01-10 08:08:23 +03:00
|
|
|
}
|
|
|
|
|
2023-03-27 23:22:59 +03:00
|
|
|
// set the default client IP header to read from
|
|
|
|
if srv.ClientIPHeaders == nil {
|
|
|
|
srv.ClientIPHeaders = []string{"X-Forwarded-For"}
|
|
|
|
}
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
// process each listener address
|
|
|
|
for i := range srv.Listen {
|
|
|
|
lnOut, err := repl.ReplaceOrErr(srv.Listen[i], true, true)
|
|
|
|
if err != nil {
|
2022-08-15 21:01:58 +03:00
|
|
|
return fmt.Errorf("server %s, listener %d: %v", srvName, i, err)
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
srv.Listen[i] = lnOut
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up each listener modifier
|
|
|
|
if srv.ListenerWrappersRaw != nil {
|
|
|
|
vals, err := ctx.LoadModule(srv, "ListenerWrappersRaw")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading listener wrapper modules: %v", err)
|
|
|
|
}
|
|
|
|
var hasTLSPlaceholder bool
|
2022-08-02 23:39:09 +03:00
|
|
|
for i, val := range vals.([]any) {
|
2020-03-16 06:28:42 +03:00
|
|
|
if _, ok := val.(*tlsPlaceholderWrapper); ok {
|
|
|
|
if i == 0 {
|
|
|
|
// putting the tls placeholder wrapper first is nonsensical because
|
|
|
|
// that is the default, implicit setting: without it, all wrappers
|
|
|
|
// will go after the TLS listener anyway
|
|
|
|
return fmt.Errorf("it is unnecessary to specify the TLS listener wrapper in the first position because that is the default")
|
|
|
|
}
|
|
|
|
if hasTLSPlaceholder {
|
|
|
|
return fmt.Errorf("TLS listener wrapper can only be specified once")
|
|
|
|
}
|
|
|
|
hasTLSPlaceholder = true
|
|
|
|
}
|
|
|
|
srv.listenerWrappers = append(srv.listenerWrappers, val.(caddy.ListenerWrapper))
|
|
|
|
}
|
|
|
|
// if any wrappers were configured but the TLS placeholder wrapper is
|
|
|
|
// absent, prepend it so all defined wrappers come after the TLS
|
|
|
|
// handshake; this simplifies logic when starting the server, since we
|
|
|
|
// can simply assume the TLS placeholder will always be there
|
|
|
|
if !hasTLSPlaceholder && len(srv.listenerWrappers) > 0 {
|
|
|
|
srv.listenerWrappers = append([]caddy.ListenerWrapper{new(tlsPlaceholderWrapper)}, srv.listenerWrappers...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pre-compile the primary handler chain, and be sure to wrap it in our
|
|
|
|
// route handler so that important security checks are done, etc.
|
|
|
|
primaryRoute := emptyHandler
|
|
|
|
if srv.Routes != nil {
|
2024-10-02 17:23:26 +03:00
|
|
|
if srv.Metrics != nil {
|
|
|
|
srv.Metrics.init = sync.Once{}
|
|
|
|
srv.Metrics.httpMetrics = &httpMetrics{}
|
|
|
|
}
|
2022-09-16 22:32:49 +03:00
|
|
|
err := srv.Routes.ProvisionHandlers(ctx, srv.Metrics)
|
2020-03-16 06:28:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("server %s: setting up route handlers: %v", srvName, err)
|
|
|
|
}
|
|
|
|
primaryRoute = srv.Routes.Compile(emptyHandler)
|
|
|
|
}
|
|
|
|
srv.primaryHandlerChain = srv.wrapPrimaryRoute(primaryRoute)
|
|
|
|
|
|
|
|
// pre-compile the error handler chain
|
|
|
|
if srv.Errors != nil {
|
|
|
|
err := srv.Errors.Routes.Provision(ctx)
|
|
|
|
if err != nil {
|
2023-05-16 18:27:52 +03:00
|
|
|
return fmt.Errorf("server %s: setting up error handling routes: %v", srvName, err)
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
srv.errorHandlerChain = srv.Errors.Routes.Compile(errorEmptyHandler)
|
|
|
|
}
|
|
|
|
|
2023-05-16 18:27:52 +03:00
|
|
|
// provision the named routes (they get compiled at runtime)
|
|
|
|
for name, route := range srv.NamedRoutes {
|
|
|
|
err := route.Provision(ctx, srv.Metrics)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("server %s: setting up named route '%s' handlers: %v", name, srvName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
// prepare the TLS connection policies
|
|
|
|
err = srv.TLSConnPolicies.Provision(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("server %s: setting up TLS connection policies: %v", srvName, err)
|
|
|
|
}
|
2020-11-18 20:57:54 +03:00
|
|
|
|
|
|
|
// if there is no idle timeout, set a sane default; users have complained
|
|
|
|
// before that aggressive CDNs leave connections open until the server
|
|
|
|
// closes them, so if we don't close them it leads to resource exhaustion
|
|
|
|
if srv.IdleTimeout == 0 {
|
|
|
|
srv.IdleTimeout = defaultIdleTimeout
|
|
|
|
}
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
2022-09-16 23:48:55 +03:00
|
|
|
ctx.Context = oldContext
|
2020-03-16 06:28:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate ensures the app's configuration is valid.
|
|
|
|
func (app *App) Validate() error {
|
|
|
|
lnAddrs := make(map[string]string)
|
2024-04-22 15:33:07 +03:00
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
for srvName, srv := range app.Servers {
|
2024-04-22 15:33:07 +03:00
|
|
|
// each server must use distinct listener addresses
|
2020-03-16 06:28:42 +03:00
|
|
|
for _, addr := range srv.Listen {
|
|
|
|
listenAddr, err := caddy.ParseNetworkAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid listener address '%s': %v", addr, err)
|
|
|
|
}
|
|
|
|
// 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++ {
|
2024-09-30 19:55:03 +03:00
|
|
|
addr := caddy.JoinNetworkAddress(listenAddr.Network, listenAddr.Host, strconv.FormatUint(uint64(listenAddr.StartPort+i), 10))
|
2020-03-16 06:28:42 +03:00
|
|
|
if sn, ok := lnAddrs[addr]; ok {
|
|
|
|
return fmt.Errorf("server %s: listener address repeated: %s (already claimed by server '%s')", srvName, addr, sn)
|
|
|
|
}
|
|
|
|
lnAddrs[addr] = srvName
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 15:33:07 +03:00
|
|
|
|
|
|
|
// logger names must not have ports
|
|
|
|
if srv.Logs != nil {
|
|
|
|
for host := range srv.Logs.LoggerNames {
|
|
|
|
if _, _, err := net.SplitHostPort(host); err == nil {
|
|
|
|
return fmt.Errorf("server %s: logger name must not have a port: %s", srvName, host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start runs the app. It finishes automatic HTTPS if enabled,
|
|
|
|
// including management of certificates.
|
|
|
|
func (app *App) Start() error {
|
2020-09-08 19:44:58 +03:00
|
|
|
// get a logger compatible with http.Server
|
|
|
|
serverLogger, err := zap.NewStdLogAt(app.logger.Named("stdlib"), zap.DebugLevel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set up server logger: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
for srvName, srv := range app.Servers {
|
2022-08-03 20:04:51 +03:00
|
|
|
srv.server = &http.Server{
|
2020-03-16 06:28:42 +03:00
|
|
|
ReadTimeout: time.Duration(srv.ReadTimeout),
|
|
|
|
ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout),
|
|
|
|
WriteTimeout: time.Duration(srv.WriteTimeout),
|
|
|
|
IdleTimeout: time.Duration(srv.IdleTimeout),
|
|
|
|
MaxHeaderBytes: srv.MaxHeaderBytes,
|
|
|
|
Handler: srv,
|
2020-09-08 19:44:58 +03:00
|
|
|
ErrorLog: serverLogger,
|
2023-04-10 20:05:02 +03:00
|
|
|
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
|
|
|
|
return context.WithValue(ctx, ConnCtxKey, c)
|
|
|
|
},
|
|
|
|
}
|
2023-11-24 04:54:27 +03:00
|
|
|
h2server := new(http2.Server)
|
2022-08-15 21:01:58 +03:00
|
|
|
|
|
|
|
// disable HTTP/2, which we enabled by default during provisioning
|
|
|
|
if !srv.protocol("h2") {
|
|
|
|
srv.server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
|
|
|
for _, cp := range srv.TLSConnPolicies {
|
|
|
|
// the TLSConfig was already provisioned, so... manually remove it
|
|
|
|
for i, np := range cp.TLSConfig.NextProtos {
|
|
|
|
if np == "h2" {
|
|
|
|
cp.TLSConfig.NextProtos = append(cp.TLSConfig.NextProtos[:i], cp.TLSConfig.NextProtos[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove it from the parent connection policy too, just to keep things tidy
|
|
|
|
for i, alpn := range cp.ALPN {
|
|
|
|
if alpn == "h2" {
|
|
|
|
cp.ALPN = append(cp.ALPN[:i], cp.ALPN[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 20:05:02 +03:00
|
|
|
} else {
|
|
|
|
//nolint:errcheck
|
|
|
|
http2.ConfigureServer(srv.server, h2server)
|
2022-08-15 21:01:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// this TLS config is used by the std lib to choose the actual TLS config for connections
|
|
|
|
// by looking through the connection policies to find the first one that matches
|
2022-08-03 20:04:51 +03:00
|
|
|
tlsCfg := srv.TLSConnPolicies.TLSConfig(app.ctx)
|
2022-09-16 23:48:55 +03:00
|
|
|
srv.configureServer(srv.server)
|
2020-03-16 06:28:42 +03:00
|
|
|
|
2022-08-15 21:01:58 +03:00
|
|
|
// enable H2C if configured
|
|
|
|
if srv.protocol("h2c") {
|
2022-08-03 20:04:51 +03:00
|
|
|
srv.server.Handler = h2c.NewHandler(srv, h2server)
|
2020-05-05 21:33:21 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
for lnIndex, lnAddr := range srv.Listen {
|
2020-03-16 06:28:42 +03:00
|
|
|
listenAddr, err := caddy.ParseNetworkAddress(lnAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: parsing listen address '%s': %v", srvName, lnAddr, err)
|
|
|
|
}
|
2024-09-30 19:55:03 +03:00
|
|
|
|
2022-08-03 20:04:51 +03:00
|
|
|
srv.addresses = append(srv.addresses, listenAddr)
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
protocols := srv.Protocols
|
|
|
|
if srv.ListenProtocols != nil && srv.ListenProtocols[lnIndex] != nil {
|
|
|
|
protocols = srv.ListenProtocols[lnIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
protocolsUnique := map[string]struct{}{}
|
|
|
|
for _, protocol := range protocols {
|
|
|
|
protocolsUnique[protocol] = struct{}{}
|
|
|
|
}
|
|
|
|
_, h1ok := protocolsUnique["h1"]
|
|
|
|
_, h2ok := protocolsUnique["h2"]
|
|
|
|
_, h2cok := protocolsUnique["h2c"]
|
|
|
|
_, h3ok := protocolsUnique["h3"]
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
for portOffset := uint(0); portOffset < listenAddr.PortRangeSize(); portOffset++ {
|
|
|
|
hostport := listenAddr.JoinHostPort(portOffset)
|
|
|
|
|
|
|
|
// enable TLS if there is a policy and if this is not the HTTP port
|
|
|
|
useTLS := len(srv.TLSConnPolicies) > 0 && int(listenAddr.StartPort+portOffset) != app.httpPort()
|
2024-09-30 19:55:03 +03:00
|
|
|
|
|
|
|
// enable HTTP/3 if configured
|
|
|
|
if h3ok && useTLS {
|
|
|
|
app.logger.Info("enabling HTTP/3 listener", zap.String("addr", hostport))
|
|
|
|
if err := srv.serveHTTP3(listenAddr.At(portOffset), tlsCfg); err != nil {
|
|
|
|
return err
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
if h3ok && !useTLS {
|
|
|
|
// Can only serve h3 with TLS enabled
|
|
|
|
app.logger.Warn("HTTP/3 skipped because it requires TLS",
|
|
|
|
zap.String("network", listenAddr.Network),
|
|
|
|
zap.String("addr", hostport))
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
if h1ok || h2ok && useTLS || h2cok {
|
|
|
|
// create the listener for this socket
|
|
|
|
lnAny, err := listenAddr.Listen(app.ctx, portOffset, net.ListenConfig{KeepAlive: time.Duration(srv.KeepAliveInterval)})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("listening on %s: %v", listenAddr.At(portOffset), err)
|
|
|
|
}
|
|
|
|
ln, ok := lnAny.(net.Listener)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("network '%s' cannot handle HTTP/1 or HTTP/2 connections", listenAddr.Network)
|
2023-04-10 20:05:02 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
// wrap listener before TLS (up to the TLS placeholder wrapper)
|
|
|
|
var lnWrapperIdx int
|
|
|
|
for i, lnWrapper := range srv.listenerWrappers {
|
|
|
|
if _, ok := lnWrapper.(*tlsPlaceholderWrapper); ok {
|
|
|
|
lnWrapperIdx = i + 1 // mark the next wrapper's spot
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ln = lnWrapper.WrapListener(ln)
|
|
|
|
}
|
|
|
|
|
2024-10-01 08:47:21 +03:00
|
|
|
if useTLS {
|
|
|
|
// create TLS listener - this enables and terminates TLS
|
|
|
|
ln = tls.NewListener(ln, tlsCfg)
|
|
|
|
}
|
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
// finish wrapping listener where we left off before TLS
|
|
|
|
for i := lnWrapperIdx; i < len(srv.listenerWrappers); i++ {
|
|
|
|
ln = srv.listenerWrappers[i].WrapListener(ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle http2 if use tls listener wrapper
|
|
|
|
if h2ok {
|
|
|
|
http2lnWrapper := &http2Listener{
|
|
|
|
Listener: ln,
|
|
|
|
server: srv.server,
|
|
|
|
h2server: h2server,
|
|
|
|
}
|
|
|
|
srv.h2listeners = append(srv.h2listeners, http2lnWrapper)
|
|
|
|
ln = http2lnWrapper
|
|
|
|
}
|
|
|
|
|
|
|
|
// if binding to port 0, the OS chooses a port for us;
|
|
|
|
// but the user won't know the port unless we print it
|
|
|
|
if !listenAddr.IsUnixNetwork() && !listenAddr.IsFdNetwork() && listenAddr.StartPort == 0 && listenAddr.EndPort == 0 {
|
|
|
|
app.logger.Info("port 0 listener",
|
|
|
|
zap.String("input_address", lnAddr),
|
|
|
|
zap.String("actual_address", ln.Addr().String()))
|
|
|
|
}
|
2020-04-01 21:23:07 +03:00
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
app.logger.Debug("starting server loop",
|
|
|
|
zap.String("address", ln.Addr().String()),
|
|
|
|
zap.Bool("tls", useTLS),
|
|
|
|
zap.Bool("http3", srv.h3server != nil))
|
2020-03-16 06:28:42 +03:00
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
srv.listeners = append(srv.listeners, ln)
|
|
|
|
|
|
|
|
// enable HTTP/1 if configured
|
|
|
|
if h1ok {
|
|
|
|
//nolint:errcheck
|
|
|
|
go srv.server.Serve(ln)
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 20:04:51 +03:00
|
|
|
|
2024-09-30 19:55:03 +03:00
|
|
|
if h2ok && !useTLS {
|
|
|
|
// Can only serve h2 with TLS enabled
|
|
|
|
app.logger.Warn("HTTP/2 skipped because it requires TLS",
|
|
|
|
zap.String("network", listenAddr.Network),
|
|
|
|
zap.String("addr", hostport))
|
2022-08-15 21:01:58 +03:00
|
|
|
}
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
}
|
2022-08-15 21:01:58 +03:00
|
|
|
|
|
|
|
srv.logger.Info("server running",
|
|
|
|
zap.String("name", srvName),
|
|
|
|
zap.Strings("protocols", srv.Protocols))
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// finish automatic HTTPS by finally beginning
|
|
|
|
// certificate management
|
2020-09-08 19:44:58 +03:00
|
|
|
err = app.automaticHTTPSPhase2()
|
2020-03-16 06:28:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("finalizing automatic HTTPS: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefully shuts down the HTTP server.
|
|
|
|
func (app *App) Stop() error {
|
|
|
|
ctx := context.Background()
|
2022-08-03 20:04:51 +03:00
|
|
|
|
|
|
|
// see if any listeners in our config will be closing or if they are continuing
|
2024-04-16 07:10:11 +03:00
|
|
|
// through a reload; because if any are closing, we will enforce shutdown delay
|
2022-08-03 20:04:51 +03:00
|
|
|
var delay bool
|
|
|
|
scheduledTime := time.Now().Add(time.Duration(app.ShutdownDelay))
|
|
|
|
if app.ShutdownDelay > 0 {
|
|
|
|
for _, server := range app.Servers {
|
|
|
|
for _, na := range server.addresses {
|
|
|
|
for _, addr := range na.Expand() {
|
|
|
|
if caddy.ListenerUsage(addr.Network, addr.JoinHostPort(0)) < 2 {
|
|
|
|
app.logger.Debug("listener closing and shutdown delay is configured", zap.String("address", addr.String()))
|
|
|
|
server.shutdownAtMu.Lock()
|
|
|
|
server.shutdownAt = scheduledTime
|
|
|
|
server.shutdownAtMu.Unlock()
|
|
|
|
delay = true
|
|
|
|
} else {
|
|
|
|
app.logger.Debug("shutdown delay configured but listener will remain open", zap.String("address", addr.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// honor scheduled/delayed shutdown time
|
|
|
|
if delay {
|
2023-03-03 07:00:18 +03:00
|
|
|
app.logger.Info("shutdown scheduled",
|
2022-08-03 20:04:51 +03:00
|
|
|
zap.Duration("delay_duration", time.Duration(app.ShutdownDelay)),
|
|
|
|
zap.Time("time", scheduledTime))
|
|
|
|
time.Sleep(time.Duration(app.ShutdownDelay))
|
|
|
|
}
|
|
|
|
|
|
|
|
// enforce grace period if configured
|
2020-03-16 06:28:42 +03:00
|
|
|
if app.GracePeriod > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(app.GracePeriod))
|
|
|
|
defer cancel()
|
2023-03-03 07:00:18 +03:00
|
|
|
app.logger.Info("servers shutting down; grace period initiated", zap.Duration("duration", time.Duration(app.GracePeriod)))
|
2022-08-03 20:04:51 +03:00
|
|
|
} else {
|
2023-03-03 07:00:18 +03:00
|
|
|
app.logger.Info("servers shutting down with eternal grace period")
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
2022-08-03 20:04:51 +03:00
|
|
|
|
2022-09-20 06:54:47 +03:00
|
|
|
// goroutines aren't guaranteed to be scheduled right away,
|
|
|
|
// so we'll use one WaitGroup to wait for all the goroutines
|
|
|
|
// to start their server shutdowns, and another to wait for
|
|
|
|
// them to finish; we'll always block for them to start so
|
|
|
|
// that when we return the caller can be confident* that the
|
|
|
|
// old servers are no longer accepting new connections
|
|
|
|
// (* the scheduler might still pause them right before
|
|
|
|
// calling Shutdown(), but it's unlikely)
|
|
|
|
var startedShutdown, finishedShutdown sync.WaitGroup
|
|
|
|
|
|
|
|
// these will run in goroutines
|
|
|
|
stopServer := func(server *Server) {
|
|
|
|
defer finishedShutdown.Done()
|
|
|
|
startedShutdown.Done()
|
|
|
|
|
2022-08-03 20:04:51 +03:00
|
|
|
if err := server.server.Shutdown(ctx); err != nil {
|
2022-07-25 19:39:59 +03:00
|
|
|
app.logger.Error("server shutdown",
|
|
|
|
zap.Error(err),
|
2022-08-03 20:04:51 +03:00
|
|
|
zap.Strings("addresses", server.Listen))
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
2022-09-20 06:54:47 +03:00
|
|
|
}
|
|
|
|
stopH3Server := func(server *Server) {
|
|
|
|
defer finishedShutdown.Done()
|
|
|
|
startedShutdown.Done()
|
2020-03-16 06:28:42 +03:00
|
|
|
|
2022-09-20 06:54:47 +03:00
|
|
|
if server.h3server == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-09-28 22:35:51 +03:00
|
|
|
|
2023-05-19 19:00:00 +03:00
|
|
|
// First close h3server then close listeners unlike stdlib for several reasons:
|
|
|
|
// 1, udp has only a single socket, once closed, no more data can be read and
|
|
|
|
// written. In contrast, closing tcp listeners won't affect established connections.
|
|
|
|
// This have something to do with graceful shutdown when upstream implements it.
|
|
|
|
// 2, h3server will only close listeners it's registered (quic listeners). Closing
|
|
|
|
// listener first and these listeners maybe unregistered thus won't be closed. caddy
|
|
|
|
// distinguishes quic-listener and underlying datagram sockets.
|
|
|
|
|
|
|
|
// TODO: CloseGracefully, once implemented upstream (see https://github.com/quic-go/quic-go/issues/2103)
|
|
|
|
if err := server.h3server.Close(); err != nil {
|
|
|
|
app.logger.Error("HTTP/3 server shutdown",
|
|
|
|
zap.Error(err),
|
|
|
|
zap.Strings("addresses", server.Listen))
|
|
|
|
}
|
2020-03-16 06:28:42 +03:00
|
|
|
}
|
2023-04-10 20:05:02 +03:00
|
|
|
stopH2Listener := func(server *Server) {
|
|
|
|
defer finishedShutdown.Done()
|
|
|
|
startedShutdown.Done()
|
|
|
|
|
|
|
|
for i, s := range server.h2listeners {
|
|
|
|
if err := s.Shutdown(ctx); err != nil {
|
|
|
|
app.logger.Error("http2 listener shutdown",
|
|
|
|
zap.Error(err),
|
|
|
|
zap.Int("index", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 20:04:51 +03:00
|
|
|
|
2022-09-20 06:54:47 +03:00
|
|
|
for _, server := range app.Servers {
|
2023-04-10 20:05:02 +03:00
|
|
|
startedShutdown.Add(3)
|
|
|
|
finishedShutdown.Add(3)
|
2022-09-20 06:54:47 +03:00
|
|
|
go stopServer(server)
|
|
|
|
go stopH3Server(server)
|
2023-04-10 20:05:02 +03:00
|
|
|
go stopH2Listener(server)
|
2022-09-20 06:54:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// block until all the goroutines have been run by the scheduler;
|
|
|
|
// this means that they have likely called Shutdown() by now
|
|
|
|
startedShutdown.Wait()
|
|
|
|
|
|
|
|
// if the process is exiting, we need to block here and wait
|
|
|
|
// for the grace periods to complete, otherwise the process will
|
|
|
|
// terminate before the servers are finished shutting down; but
|
|
|
|
// we don't really need to wait for the grace period to finish
|
|
|
|
// if the process isn't exiting (but note that frequent config
|
|
|
|
// reloads with long grace periods for a sustained length of time
|
|
|
|
// may deplete resources)
|
|
|
|
if caddy.Exiting() {
|
|
|
|
finishedShutdown.Wait()
|
|
|
|
}
|
|
|
|
|
2024-02-15 07:21:23 +03:00
|
|
|
// run stop callbacks now that the server shutdowns are complete
|
|
|
|
for name, s := range app.Servers {
|
|
|
|
for _, stopHook := range s.onStopFuncs {
|
|
|
|
if err := stopHook(ctx); err != nil {
|
|
|
|
app.logger.Error("server stop hook", zap.String("server", name), zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return app.HTTPSPort
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:57:54 +03:00
|
|
|
// defaultIdleTimeout is the default HTTP server timeout
|
|
|
|
// for closing idle connections; useful to avoid resource
|
|
|
|
// exhaustion behind hungry CDNs, for example (we've had
|
|
|
|
// several complaints without this).
|
|
|
|
const defaultIdleTimeout = caddy.Duration(5 * time.Minute)
|
|
|
|
|
2020-03-16 06:28:42 +03:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.App = (*App)(nil)
|
|
|
|
_ caddy.Provisioner = (*App)(nil)
|
|
|
|
_ caddy.Validator = (*App)(nil)
|
|
|
|
)
|