@marcel91200 / caddy Mirror · read-only / commits / fae06426
Fork 0

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

httpcaddyfile: Add `auto_https` global option (#3284)

Francis Lavoie committed May 19, 2020 at 18:59 UTC fae064262d9c40393d634660b94d36ce6703aa1c
4 files changed +103 -1
caddyconfig/httpcaddyfile/httptype.go
+16 -1
@@ -343,12 +343,27 @@ func (st *ServerType) serversFromPairings(
343 if hsp, ok := options["https_port"].(int); ok {
344 httpsPort = strconv.Itoa(hsp)
345 }
346 + autoHTTPS := "on"
347 + if ah, ok := options["auto_https"].(string); ok {
348 + autoHTTPS = ah
349 + }
350
351 for i, p := range pairings {
352 srv := &caddyhttp.Server{
353 Listen: p.addresses,
354 }
355
356 + // handle the auto_https global option
357 + if autoHTTPS != "on" {
358 + srv.AutoHTTPS = new(caddyhttp.AutoHTTPSConfig)
359 + if autoHTTPS == "off" {
360 + srv.AutoHTTPS.Disabled = true
361 + }
362 + if autoHTTPS == "disable_redirects" {
363 + srv.AutoHTTPS.DisableRedir = true
364 + }
365 + }
366 +
367 // sort server blocks by their keys; this is important because
368 // only the first matching site should be evaluated, and we should
369 // attempt to match most specific site first (host and path), in
@@ -382,7 +397,7 @@ func (st *ServerType) serversFromPairings(
397 })
398
399 var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
385 - autoHTTPSWillAddConnPolicy := true
400 + autoHTTPSWillAddConnPolicy := autoHTTPS != "off"
401
402 // create a subroute for each site in the server block
403 for _, sblock := range p.serverBlocks {
caddyconfig/httpcaddyfile/options.go
+16
@@ -38,6 +38,7 @@ func init() {
38 RegisterGlobalOption("on_demand_tls", parseOptOnDemand)
39 RegisterGlobalOption("local_certs", parseOptTrue)
40 RegisterGlobalOption("key_type", parseOptSingleString)
41 + RegisterGlobalOption("auto_https", parseOptAutoHTTPS)
42 }
43
44 func parseOptTrue(d *caddyfile.Dispenser) (interface{}, error) {
@@ -264,3 +265,18 @@ func parseOptOnDemand(d *caddyfile.Dispenser) (interface{}, error) {
265 }
266 return ond, nil
267 }
268 +
269 +func parseOptAutoHTTPS(d *caddyfile.Dispenser) (interface{}, error) {
270 + d.Next() // consume parameter name
271 + if !d.Next() {
272 + return "", d.ArgErr()
273 + }
274 + val := d.Val()
275 + if d.Next() {
276 + return "", d.ArgErr()
277 + }
278 + if val != "off" && val != "disable_redirects" {
279 + return "", d.Errf("auto_https must be either 'off' or 'disable_redirects'")
280 + }
281 + return val, nil
282 +}
caddytest/integration/caddyfile_adapt/auto_https_disable_redirects.txt new
+34
@@ -0,0 +1,34 @@
1 +{
2 + auto_https disable_redirects
3 +}
4 +
5 +localhost
6 +----------
7 +{
8 + "apps": {
9 + "http": {
10 + "servers": {
11 + "srv0": {
12 + "listen": [
13 + ":443"
14 + ],
15 + "routes": [
16 + {
17 + "match": [
18 + {
19 + "host": [
20 + "localhost"
21 + ]
22 + }
23 + ],
24 + "terminal": true
25 + }
26 + ],
27 + "automatic_https": {
28 + "disable_redirects": true
29 + }
30 + }
31 + }
32 + }
33 + }
34 +}
\ No newline at end of file
caddytest/integration/caddyfile_adapt/auto_https_off.txt new
+37
@@ -0,0 +1,37 @@
1 +{
2 + auto_https off
3 +}
4 +
5 +localhost
6 +----------
7 +{
8 + "apps": {
9 + "http": {
10 + "servers": {
11 + "srv0": {
12 + "listen": [
13 + ":443"
14 + ],
15 + "routes": [
16 + {
17 + "match": [
18 + {
19 + "host": [
20 + "localhost"
21 + ]
22 + }
23 + ],
24 + "terminal": true
25 + }
26 + ],
27 + "tls_connection_policies": [
28 + {}
29 + ],
30 + "automatic_https": {
31 + "disable": true
32 + }
33 + }
34 + }
35 + }
36 + }
37 +}
\ No newline at end of file