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)
}
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
}