@marcel91200 / caddy Mirror · read-only / commits / 4a641f6c
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

reverseproxy: Add Caddyfile scheme shorthand for h2c (#3629)

* reverseproxy: Add Caddyfile scheme shorthand for h2c * reverseproxy: Use parentheses for condition Co-authored-by: Matt Holt <mholt@users.noreply.github.com> Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Francis Lavoie committed Nov 23, 2020 at 14:18 UTC 4a641f6c6fb441a8301eeb8cab0c0b61de70ff7f
2 files changed +48 -3
caddytest/integration/caddyfile_adapt/reverse_proxy_h2c_shorthand.txt new
+38
@@ -0,0 +1,38 @@
1 +:8884
2 +
3 +reverse_proxy h2c://localhost:8080
4 +----------
5 +{
6 + "apps": {
7 + "http": {
8 + "servers": {
9 + "srv0": {
10 + "listen": [
11 + ":8884"
12 + ],
13 + "routes": [
14 + {
15 + "handle": [
16 + {
17 + "handler": "reverse_proxy",
18 + "transport": {
19 + "protocol": "http",
20 + "versions": [
21 + "h2c",
22 + "2"
23 + ]
24 + },
25 + "upstreams": [
26 + {
27 + "dial": "localhost:8080"
28 + }
29 + ]
30 + }
31 + ]
32 + }
33 + ]
34 + }
35 + }
36 + }
37 + }
38 +}
\ No newline at end of file
modules/caddyhttp/reverseproxy/caddyfile.go
+10 -3
@@ -131,12 +131,15 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
131 if toURL.Scheme == "https" && urlPort == "80" {
132 return "", d.Err("upstream address has conflicting scheme (https://) and port (:80, the HTTP port)")
133 }
134 + if toURL.Scheme == "h2c" && urlPort == "443" {
135 + return "", d.Err("upstream address has conflicting scheme (h2c://) and port (:443, the HTTPS port)")
136 + }
137
138 // if port is missing, attempt to infer from scheme
139 if toURL.Port() == "" {
140 var toPort string
141 switch toURL.Scheme {
139 - case "", "http":
142 + case "", "http", "h2c":
143 toPort = "80"
144 case "https":
145 toPort = "443"
@@ -565,8 +568,9 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
568 }
569
570 // if the scheme inferred from the backends' addresses is
568 - // HTTPS, we will need a non-nil transport to enable TLS
569 - if commonScheme == "https" && transport == nil {
571 + // HTTPS, we will need a non-nil transport to enable TLS,
572 + // or if H2C, to set the transport versions.
573 + if (commonScheme == "https" || commonScheme == "h2c") && transport == nil {
574 transport = new(HTTPTransport)
575 transportModuleName = "http"
576 }
@@ -583,6 +587,9 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
587 if commonScheme == "http" && te.TLSEnabled() {
588 return d.Errf("upstream address scheme is HTTP but transport is configured for HTTP+TLS (HTTPS)")
589 }
590 + if te, ok := transport.(*HTTPTransport); ok && commonScheme == "h2c" {
591 + te.Versions = []string{"h2c", "2"}
592 + }
593 } else if commonScheme == "https" {
594 return d.Errf("upstreams are configured for HTTPS but transport module does not support TLS: %T", transport)
595 }