Repository: caddyserver/caddy (language: go)

Issue:
reverse_proxy with h2c http transport not working when client requests using http1.1
Caddy version: v2.5.1

I'm trying to send incoming requests to a h2c upstream server. This doesn't work if the client is using HTTP1.1 only. Caddy sends a HTTP1.1 request to the upstream server while it should use h2c. Did i miss something about how this is supposed to work or is the config not correct?

**Caddyfile**
```
localhost {
    reverse_proxy {
        transport http {
            versions h2c
        }
        to 127.0.0.1:8988
        # doesn't work either: reverse_proxy h2c://127.0.0.1:8988
    }
}
```

```bash
curl -v --http1.1 https://localhost/some/url
```

**Logs**
```
2022/05/09 13:39:55.692	DEBUG	tls.handshake	choosing certificate	{"identifier": "localhost", "num_choices": 1}
2022/05/09 13:39:55.692	DEBUG	tls.handshake	default certificate selection results	{"identifier": "localhost", "subjects": ["localhost"], "managed": true, "issuer_key": "local", "hash": "3b1a3eb05354d36a48042d511a80e9106bc7a9415660914f9c9476f85d8889e5"}
2022/05/09 13:39:55.692	DEBUG	tls.handshake	matched certificate in cache	{"subjects": ["localhost"], "managed": true, "expiration": "2022/05/10 01:07:01.000", "hash": "3b1a3eb05354d36a48042d511a80e9106bc7a9415660914f9c9476f85d8889e5"}
2022/05/09 13:39:55.693	DEBUG	http.handlers.reverse_proxy	selected upstream	{"dial": "127.0.0.1:8988", "total_upstreams": 1}
2022/05/09 13:39:55.694	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "127.0.0.1:8988", "duration": 0.001156052, "request": {"remote_ip": "127.0.0.1", "remote_port": "53180", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/some/url", "headers": {"User-Agent": ["curl/7.68.0"], "Accept": ["*/*"], "X-Forwarded-For": ["127.0.0.1"], "X-Forwarded-Proto": ["https"], "X-Forwarded-Host": ["localhost"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "http/1.1", "server_name": "localhost"}}, "error": "net/http: HTTP/1.x transport connection broken: malformed HTTP response \"\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\b\\a\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\""}
2022/05/09 13:39:55.694	ERROR	http.log.error.log0	net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\b\a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"	{"request": {"remote_ip": "127.0.0.1", "remote_port": "53180", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/some/url", "headers": {"User-Agent": ["curl/7.68.0"], "Accept": ["*/*"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "http/1.1", "server_name": "localhost"}}, "duration": 0.001299073, "status": 502, "err_id": "5f3h3tzr5", "err_trace": "reverseproxy.statusError (reverseproxy.go:1196)"}
2022/05/09 13:39:55.694	ERROR	http.log.access.log0	handled request	{"request": {"remote_ip": "127.0.0.1", "remote_port": "53180", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/some/url", "headers": {"Accept": ["*/*"], "User-Agent": ["curl/7.68.0"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "http/1.1", "server_name": "localhost"}}, "user_id": "", "duration": 0.001299073, "size": 0, "status": 502, "resp_headers": {"Server": ["Caddy"]}}

```


Discussion / hints:
I think the incoming/original request protocol _also_ has to be HTTP/2. I'm not sure we've had a request yet for it to support HTTP/1.1 downstream and upconvert to 2 going upstream. Can you try with an incoming HTTP/2 request and make sure it works? If so, I can prepare a branch I would like you to try that could solve it.
When requesting with http2 it works flawlessly. It would be extremely awesome if the conversion from http1.1 to http2 for upstream would work. Thanks so much!

Fix patch:
diff --git a/modules/caddyhttp/reverseproxy/httptransport.go b/modules/caddyhttp/reverseproxy/httptransport.go
index f7472bea2d6..264bd2aa8cc 100644
--- a/modules/caddyhttp/reverseproxy/httptransport.go
+++ b/modules/caddyhttp/reverseproxy/httptransport.go
@@ -247,8 +247,8 @@ func (h *HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
 	h.SetScheme(req)
 
 	// if H2C ("HTTP/2 over cleartext") is enabled and the upstream request is
-	// HTTP/2 without TLS, use the alternate H2C-capable transport instead
-	if req.ProtoMajor == 2 && req.URL.Scheme == "http" && h.h2cTransport != nil {
+	// HTTP without TLS, use the alternate H2C-capable transport instead
+	if req.URL.Scheme == "http" && h.h2cTransport != nil {
 		return h.h2cTransport.RoundTrip(req)
 	}
 


Test patch: