Skip to content

Commit 2ee263a

Browse files
authored
Merge pull request moby#5044 from vvoland/http-fallback-insecure-host
util/resolver: Make httpFallback concurrent safe
2 parents 15eec79 + 0285950 commit 2ee263a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

util/resolver/resolver.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"runtime"
1212
"strings"
13+
"sync"
1314
"syscall"
1415
"time"
1516

@@ -213,13 +214,18 @@ func newDefaultTransport() *http.Transport {
213214
}
214215

215216
type httpFallback struct {
216-
super http.RoundTripper
217-
host string
217+
super http.RoundTripper
218+
host string
219+
hostMut sync.Mutex
218220
}
219221

220222
func (f *httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
221-
// only fall back if the same host had previously fell back
222-
if f.host != r.URL.Host {
223+
f.hostMut.Lock()
224+
// Skip the HTTPS call only if the same host had previously fell back
225+
tryHTTPSFirst := f.host != r.URL.Host
226+
f.hostMut.Unlock()
227+
228+
if tryHTTPSFirst {
223229
resp, err := f.super.RoundTrip(r)
224230
if !isTLSError(err) && !isPortError(err, r.URL.Host) {
225231
return resp, err
@@ -232,8 +238,13 @@ func (f *httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
232238
plainHTTPRequest := *r
233239
plainHTTPRequest.URL = &plainHTTPUrl
234240

235-
if f.host != r.URL.Host {
241+
// We tried HTTPS first but it failed.
242+
// Mark the host so we don't try HTTPS for this host next time
243+
// and refresh the request body.
244+
if tryHTTPSFirst {
245+
f.hostMut.Lock()
236246
f.host = r.URL.Host
247+
f.hostMut.Unlock()
237248

238249
// update body on the second attempt
239250
if r.Body != nil && r.GetBody != nil {

0 commit comments

Comments
 (0)