Skip to content
Permalink
Browse files
HTTPCLIENT-2148: fluent Executor volatile access thread safety (#301)
  • Loading branch information
carterkozak committed Apr 11, 2021
1 parent 58a17cc commit 656d0dd4f3b5ceefc649cb2bd0d952db65dd014c
Showing 1 changed file with 23 additions and 15 deletions.
@@ -100,10 +100,12 @@ public Executor use(final CredentialsStore credentialsStore) {
}

public Executor auth(final AuthScope authScope, final Credentials credentials) {
if (this.credentialsStore == null) {
this.credentialsStore = new BasicCredentialsProvider();
CredentialsStore credentialsStoreSnapshot = credentialsStore;
if (credentialsStoreSnapshot == null) {
credentialsStoreSnapshot = new BasicCredentialsProvider();
this.credentialsStore = credentialsStoreSnapshot;
}
this.credentialsStore.setCredentials(authScope, credentials);
credentialsStoreSnapshot.setCredentials(authScope, credentials);
return this;
}

@@ -125,8 +127,9 @@ public Executor auth(final String host, final Credentials credentials) {
}

public Executor authPreemptive(final HttpHost host) {
if (this.credentialsStore != null) {
final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(host), null);
final CredentialsStore credentialsStoreSnapshot = credentialsStore;
if (credentialsStoreSnapshot != null) {
final Credentials credentials = credentialsStoreSnapshot.getCredentials(new AuthScope(host), null);
if (credentials != null) {
final BasicScheme basicScheme = new BasicScheme();
basicScheme.initPreemptive(credentials);
@@ -150,8 +153,9 @@ public Executor authPreemptive(final String host) {
}

public Executor authPreemptiveProxy(final HttpHost proxy) {
if (this.credentialsStore != null) {
final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(proxy), null);
final CredentialsStore credentialsStoreSnapshot = credentialsStore;
if (credentialsStoreSnapshot != null) {
final Credentials credentials = credentialsStoreSnapshot.getCredentials(new AuthScope(proxy), null);
if (credentials != null) {
final BasicScheme basicScheme = new BasicScheme();
basicScheme.initPreemptive(credentials);
@@ -186,8 +190,9 @@ public Executor auth(final HttpHost host,
}

public Executor clearAuth() {
if (this.credentialsStore != null) {
this.credentialsStore.clear();
final CredentialsStore credentialsStoreSnapshot = credentialsStore;
if (credentialsStoreSnapshot != null) {
credentialsStoreSnapshot.clear();
}
return this;
}
@@ -201,8 +206,9 @@ public Executor use(final CookieStore cookieStore) {
}

public Executor clearCookies() {
if (this.cookieStore != null) {
this.cookieStore.clear();
final CookieStore cookieStoreSnapshot = cookieStore;
if (cookieStoreSnapshot != null) {
cookieStoreSnapshot.clear();
}
return this;
}
@@ -218,14 +224,16 @@ public Executor clearCookies() {
public Response execute(
final Request request) throws IOException {
final HttpClientContext localContext = HttpClientContext.create();
if (this.credentialsStore != null) {
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsStore);
final CredentialsStore credentialsStoreSnapshot = credentialsStore;
if (credentialsStoreSnapshot != null) {
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, credentialsStoreSnapshot);
}
if (this.authCache != null) {
localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
}
if (this.cookieStore != null) {
localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final CookieStore cookieStoreSnapshot = cookieStore;
if (cookieStoreSnapshot != null) {
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStoreSnapshot);
}
return new Response(request.internalExecute(this.httpclient, localContext));
}

0 comments on commit 656d0dd

Please sign in to comment.