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

reverse_proxy: Make HTTP versions configurable, don't set NextProtos

Matthew Holt committed Nov 5, 2019 at 16:27 UTC 97d918df3e89a3cc4dd5e5c06967bb85870c5ce3
2 files changed +31 -11
modules/caddyhttp/reverseproxy/httptransport.go
+25 -11
@@ -51,6 +51,7 @@ type HTTPTransport struct {
51 MaxResponseHeaderSize int64 `json:"max_response_header_size,omitempty"`
52 WriteBufferSize int `json:"write_buffer_size,omitempty"`
53 ReadBufferSize int `json:"read_buffer_size,omitempty"`
54 + Versions []string `json:"versions,omitempty"`
55
56 RoundTripper http.RoundTripper `json:"-"`
57 }
@@ -66,6 +67,9 @@ func (HTTPTransport) CaddyModule() caddy.ModuleInfo {
67 // Provision sets up h.RoundTripper with a http.Transport
68 // that is ready to use.
69 func (h *HTTPTransport) Provision(_ caddy.Context) error {
70 + if len(h.Versions) == 0 {
71 + h.Versions = []string{"1.1", "2"}
72 + }
73 dialer := &net.Dialer{
74 Timeout: time.Duration(h.DialTimeout),
75 FallbackDelay: time.Duration(h.FallbackDelay),
@@ -121,8 +125,10 @@ func (h *HTTPTransport) Provision(_ caddy.Context) error {
125 rt.DisableCompression = !*h.Compression
126 }
127
124 - if err := http2.ConfigureTransport(rt); err != nil {
125 - return err
128 + if sliceContains(h.Versions, "2") {
129 + if err := http2.ConfigureTransport(rt); err != nil {
130 + return nil, err
131 + }
132 }
133
134 h.RoundTripper = rt
@@ -199,11 +205,18 @@ func (t TLSConfig) MakeTLSClientConfig() (*tls.Config, error) {
205 return nil, nil
206 }
207
202 - cfg.NextProtos = []string{"h2", "http/1.1"} // TODO: ensure that this actually enables HTTP/2
203 -
208 return cfg, nil
209 }
210
211 +// KeepAlive holds configuration pertaining to HTTP Keep-Alive.
212 +type KeepAlive struct {
213 + Enabled *bool `json:"enabled,omitempty"`
214 + ProbeInterval caddy.Duration `json:"probe_interval,omitempty"`
215 + MaxIdleConns int `json:"max_idle_conns,omitempty"`
216 + MaxIdleConnsPerHost int `json:"max_idle_conns_per_host,omitempty"`
217 + IdleConnTimeout caddy.Duration `json:"idle_timeout,omitempty"` // how long should connections be kept alive when idle
218 +}
219 +
220 // decodeBase64DERCert base64-decodes, then DER-decodes, certStr.
221 func decodeBase64DERCert(certStr string) (*x509.Certificate, error) {
222 // decode base64
@@ -216,13 +229,14 @@ func decodeBase64DERCert(certStr string) (*x509.Certificate, error) {
229 return x509.ParseCertificate(derBytes)
230 }
231
219 -// KeepAlive holds configuration pertaining to HTTP Keep-Alive.
220 -type KeepAlive struct {
221 - Enabled *bool `json:"enabled,omitempty"`
222 - ProbeInterval caddy.Duration `json:"probe_interval,omitempty"`
223 - MaxIdleConns int `json:"max_idle_conns,omitempty"`
224 - MaxIdleConnsPerHost int `json:"max_idle_conns_per_host,omitempty"`
225 - IdleConnTimeout caddy.Duration `json:"idle_timeout,omitempty"` // how long should connections be kept alive when idle
232 +// sliceContains returns true if needle is in haystack.
233 +func sliceContains(haystack []string, needle string) bool {
234 + for _, s := range haystack {
235 + if s == needle {
236 + return true
237 + }
238 + }
239 + return false
240 }
241
242 // Interface guards
modules/caddyhttp/reverseproxy/reverseproxy.go
+6
@@ -394,6 +394,12 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, di Dia
394 return err
395 }
396
397 + h.logger.Debug("upstream roundtrip",
398 + zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: req}),
399 + zap.Object("headers", caddyhttp.LoggableHTTPHeader(res.Header)),
400 + zap.Int("status", res.StatusCode),
401 + )
402 +
403 // update circuit breaker on current conditions
404 if di.Upstream.cb != nil {
405 di.Upstream.cb.RecordMetric(res.StatusCode, latency)