Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 16:45 UTC
reverseproxy: Fix panic when TLS is not configured (#4848)
* reverseproxy: Fix panic when TLS is not configured * Refactor and simplify setScheme Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
Francis Lavoie committed
Jun 22, 2022 at 15:01 UTC
25f10511e7ef80c10493519499c479f6ffa49a0f
1 file changed
+23
-16
modules/caddyhttp/reverseproxy/httptransport.go
+23
-16
@@ -281,7 +281,7 @@ func (h *HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
281
repl := req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
282
transport := h.replaceTLSServername(repl)
283
284
- transport.SetScheme(req)
284
+ transport.setScheme(req)
285
286
// if H2C ("HTTP/2 over cleartext") is enabled and the upstream request is
287
// HTTP without TLS, use the alternate H2C-capable transport instead
@@ -292,27 +292,34 @@ func (h *HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
292
return transport.Transport.RoundTrip(req)
293
}
294
295
-// SetScheme ensures that the outbound request req
295
+// setScheme ensures that the outbound request req
296
// has the scheme set in its URL; the underlying
297
// http.Transport requires a scheme to be set.
298
-func (h *HTTPTransport) SetScheme(req *http.Request) {
299
- skipTLSport := false
300
- if h.TLS.ExceptPorts != nil {
301
- port := req.URL.Port()
302
- for i := range h.TLS.ExceptPorts {
303
- if h.TLS.ExceptPorts[i] == port {
304
- skipTLSport = true
305
- break
306
- }
307
- }
298
+func (h *HTTPTransport) setScheme(req *http.Request) {
299
+ if req.URL.Scheme != "" {
300
+ return
301
}
309
-
310
- if req.URL.Scheme == "" {
302
+ if h.shouldUseTLS(req) {
303
+ req.URL.Scheme = "https"
304
+ } else {
305
req.URL.Scheme = "http"
312
- if h.TLS != nil && !skipTLSport {
313
- req.URL.Scheme = "https"
306
+ }
307
+}
308
+
309
+// shouldUseTLS returns true if TLS should be used for req.
310
+func (h *HTTPTransport) shouldUseTLS(req *http.Request) bool {
311
+ if h.TLS == nil {
312
+ return false
313
+ }
314
+
315
+ port := req.URL.Port()
316
+ for i := range h.TLS.ExceptPorts {
317
+ if h.TLS.ExceptPorts[i] == port {
318
+ return false
319
}
320
}
321
+
322
+ return true
323
}
324
325
// TLSEnabled returns true if TLS is enabled.