diff --git a/src/calanonsync/ews.go b/src/calanonsync/ews.go index d2f8820..e44ce43 100644 --- a/src/calanonsync/ews.go +++ b/src/calanonsync/ews.go @@ -70,40 +70,38 @@ func (er EWSRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { r.SetBasicAuth(er.username, er.password) } + er.initMutex.Lock() resp, err := er.delegate.RoundTrip(r) - if err == nil && resp.StatusCode == http.StatusUnauthorized { - er.initMutex.Lock() - defer er.initMutex.Unlock() - - if er.authType == authTypeUnknown { - // This is a good time to find out what the server prefers. - authHeaders := resp.Header["Www-Authenticate"] - if authHeaders != nil { - for _, h := range authHeaders { - if strings.HasPrefix(h, "BASIC") { - er.authType = authTypeBasic - } else if strings.HasPrefix(h, "NTLM") { - er.authType = authTypeNTLM - break // NTLM is the best we could do - } + if err == nil && resp.StatusCode == http.StatusUnauthorized && er.authType == authTypeUnknown { + // This is a good time to find out what the server prefers. + authHeaders := resp.Header["Www-Authenticate"] + if authHeaders != nil { + for _, h := range authHeaders { + if strings.HasPrefix(h, "BASIC") { + er.authType = authTypeBasic + } else if strings.HasPrefix(h, "NTLM") { + er.authType = authTypeNTLM + break // NTLM is the best we could do } } + } - // So, do we know more than before? If so, try again. - if er.authType > authTypeUnknown { - if er.authType == authTypeNTLM { - // We need to replace the delegator. - er.delegate = &httpntlm.NtlmTransport{ - Domain: "", - User: er.username, - Password: er.password, - } + // So, do we know more than before? If so, try again. + if er.authType > authTypeUnknown { + if er.authType == authTypeNTLM { + // We need to replace the delegator. + er.delegate = &httpntlm.NtlmTransport{ + Domain: "", + User: er.username, + Password: er.password, } - return er.RoundTrip(r) } + er.initMutex.Unlock() + return er.RoundTrip(r) } } + er.initMutex.Unlock() return resp, err }