Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
Refactor automatic HTTPS configuration; ability to skip certain names
Matthew Holt committed
Jun 26, 2019 at 10:49 UTC
91b03dccb0094d96652def8a9c70016fd53d1006
2 files changed
+62
-26
modules/caddyhttp/caddyhttp.go
+14
-7
@@ -48,16 +48,23 @@ func (app *App) Provision(ctx caddy.Context) error {
48
repl := caddy.NewReplacer()
49
50
for _, srv := range app.Servers {
51
+ if srv.AutoHTTPS == nil {
52
+ // avoid nil pointer dereferences
53
+ srv.AutoHTTPS = new(AutoHTTPSConfig)
54
+ }
55
+
56
// TODO: Test this function to ensure these replacements are performed
57
for i := range srv.Listen {
58
srv.Listen[i] = repl.ReplaceAll(srv.Listen[i], "")
59
}
60
+
61
if srv.Routes != nil {
62
err := srv.Routes.Provision(ctx)
63
if err != nil {
64
return fmt.Errorf("setting up server routes: %v", err)
65
}
66
}
67
+
68
if srv.Errors != nil {
69
err := srv.Errors.Routes.Provision(ctx)
70
if err != nil {
@@ -187,7 +194,7 @@ func (app *App) automaticHTTPS() error {
194
for srvName, srv := range app.Servers {
195
srv.tlsApp = tlsApp
196
190
- if srv.DisableAutoHTTPS {
197
+ if srv.AutoHTTPS.Disabled {
198
continue
199
}
200
@@ -203,7 +210,7 @@ func (app *App) automaticHTTPS() error {
210
for _, m := range matcherSet {
211
if hm, ok := m.(*MatchHost); ok {
212
for _, d := range *hm {
206
- if certmagic.HostQualifies(d) {
213
+ if certmagic.HostQualifies(d) && !srv.AutoHTTPS.HostSkipped(d) {
214
domainSet[d] = struct{}{}
215
}
216
}
@@ -256,7 +263,7 @@ func (app *App) automaticHTTPS() error {
263
{ALPN: defaultALPN},
264
}
265
259
- if srv.DisableAutoHTTPSRedir {
266
+ if srv.AutoHTTPS.DisableRedir {
267
continue
268
}
269
@@ -324,10 +331,10 @@ func (app *App) automaticHTTPS() error {
331
lnAddrs = append(lnAddrs, addr)
332
}
333
app.Servers["auto_https_redirects"] = &Server{
327
- Listen: lnAddrs,
328
- Routes: redirRoutes,
329
- DisableAutoHTTPS: true,
330
- tlsApp: tlsApp, // required to solve HTTP challenge
334
+ Listen: lnAddrs,
335
+ Routes: redirRoutes,
336
+ AutoHTTPS: &AutoHTTPSConfig{Disabled: true},
337
+ tlsApp: tlsApp, // required to solve HTTP challenge
338
}
339
}
340
modules/caddyhttp/server.go
+48
-19
@@ -14,19 +14,17 @@ import (
14
15
// Server is an HTTP server.
16
type Server struct {
17
- Listen []string `json:"listen,omitempty"`
18
- ReadTimeout caddy.Duration `json:"read_timeout,omitempty"`
19
- ReadHeaderTimeout caddy.Duration `json:"read_header_timeout,omitempty"`
20
- WriteTimeout caddy.Duration `json:"write_timeout,omitempty"`
21
- IdleTimeout caddy.Duration `json:"idle_timeout,omitempty"`
22
- MaxHeaderBytes int `json:"max_header_bytes,omitempty"`
23
- Routes RouteList `json:"routes,omitempty"`
24
- Errors *httpErrorConfig `json:"errors,omitempty"`
25
- // TODO: Having a separate connection policy to act as a default or template would be handy... then override using first matching conn policy...
26
- TLSConnPolicies caddytls.ConnectionPolicies `json:"tls_connection_policies,omitempty"`
27
- DisableAutoHTTPS bool `json:"disable_auto_https,omitempty"`
28
- DisableAutoHTTPSRedir bool `json:"disable_auto_https_redir,omitempty"`
29
- MaxRehandles int `json:"max_rehandles,omitempty"`
17
+ Listen []string `json:"listen,omitempty"`
18
+ ReadTimeout caddy.Duration `json:"read_timeout,omitempty"`
19
+ ReadHeaderTimeout caddy.Duration `json:"read_header_timeout,omitempty"`
20
+ WriteTimeout caddy.Duration `json:"write_timeout,omitempty"`
21
+ IdleTimeout caddy.Duration `json:"idle_timeout,omitempty"`
22
+ MaxHeaderBytes int `json:"max_header_bytes,omitempty"`
23
+ Routes RouteList `json:"routes,omitempty"`
24
+ Errors *HTTPErrorConfig `json:"errors,omitempty"`
25
+ TLSConnPolicies caddytls.ConnectionPolicies `json:"tls_connection_policies,omitempty"`
26
+ AutoHTTPS *AutoHTTPSConfig `json:"automatic_https,omitempty"`
27
+ MaxRehandles int `json:"max_rehandles,omitempty"`
28
29
tlsApp *caddytls.TLS
30
}
@@ -121,13 +119,44 @@ func (s *Server) listenersUseAnyPortOtherThan(otherPort int) bool {
119
return false
120
}
121
124
-type httpErrorConfig struct {
122
+// AutoHTTPSConfig is used to disable automatic HTTPS
123
+// or certain aspects of it for a specific server.
124
+type AutoHTTPSConfig struct {
125
+ // If true, automatic HTTPS will be entirely disabled.
126
+ Disabled bool `json:"disable,omitempty"`
127
+
128
+ // If true, only automatic HTTP->HTTPS redirects will
129
+ // be disabled.
130
+ DisableRedir bool `json:"disable_redirects,omitempty"`
131
+
132
+ // Hosts/domain names listed here will not be included
133
+ // in automatic HTTPS (they will not have certificates
134
+ // loaded nor redirects applied).
135
+ Skip []string `json:"skip,omitempty"`
136
+}
137
+
138
+// HostSkipped returns true if name is supposed to be skipped
139
+// when setting up automatic HTTPS.
140
+func (ahc AutoHTTPSConfig) HostSkipped(name string) bool {
141
+ for _, n := range ahc.Skip {
142
+ if name == n {
143
+ return true
144
+ }
145
+ }
146
+ return false
147
+}
148
+
149
+// HTTPErrorConfig determines how to handle errors
150
+// from the HTTP handlers.
151
+type HTTPErrorConfig struct {
152
Routes RouteList `json:"routes,omitempty"`
126
- // TODO: some way to configure the logging of errors, probably? standardize
127
- // the logging configuration first.
153
}
154
130
-const ServerCtxKey caddy.CtxKey = "server"
155
+// Context keys for HTTP request context values.
156
+const (
157
+ // For referencing the server instance
158
+ ServerCtxKey caddy.CtxKey = "server"
159
132
-// TableCtxKey is the context key for the request's variable table. TODO: implement this
133
-const TableCtxKey caddy.CtxKey = "table"
160
+ // For the request's variable table (TODO: implement this)
161
+ TableCtxKey caddy.CtxKey = "table"
162
+)