Fixed possible race condition

This commit is contained in:
Andreas Schneider 2018-04-03 11:55:45 +02:00
parent 0c1c63e20e
commit 69aa8d81f6
1 changed files with 23 additions and 25 deletions

View File

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