HTTPCLIENT-2148: fluent Executor volatile access thread safety (#301)

This commit is contained in:
Carter Kozak 2021-04-11 10:29:55 -04:00 committed by GitHub
parent 58a17cc549
commit 656d0dd4f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 15 deletions

View File

@ -100,10 +100,12 @@ public class Executor {
} }
public Executor auth(final AuthScope authScope, final Credentials credentials) { public Executor auth(final AuthScope authScope, final Credentials credentials) {
if (this.credentialsStore == null) { CredentialsStore credentialsStoreSnapshot = credentialsStore;
this.credentialsStore = new BasicCredentialsProvider(); if (credentialsStoreSnapshot == null) {
credentialsStoreSnapshot = new BasicCredentialsProvider();
this.credentialsStore = credentialsStoreSnapshot;
} }
this.credentialsStore.setCredentials(authScope, credentials); credentialsStoreSnapshot.setCredentials(authScope, credentials);
return this; return this;
} }
@ -125,8 +127,9 @@ public class Executor {
} }
public Executor authPreemptive(final HttpHost host) { public Executor authPreemptive(final HttpHost host) {
if (this.credentialsStore != null) { final CredentialsStore credentialsStoreSnapshot = credentialsStore;
final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(host), null); if (credentialsStoreSnapshot != null) {
final Credentials credentials = credentialsStoreSnapshot.getCredentials(new AuthScope(host), null);
if (credentials != null) { if (credentials != null) {
final BasicScheme basicScheme = new BasicScheme(); final BasicScheme basicScheme = new BasicScheme();
basicScheme.initPreemptive(credentials); basicScheme.initPreemptive(credentials);
@ -150,8 +153,9 @@ public class Executor {
} }
public Executor authPreemptiveProxy(final HttpHost proxy) { public Executor authPreemptiveProxy(final HttpHost proxy) {
if (this.credentialsStore != null) { final CredentialsStore credentialsStoreSnapshot = credentialsStore;
final Credentials credentials = this.credentialsStore.getCredentials(new AuthScope(proxy), null); if (credentialsStoreSnapshot != null) {
final Credentials credentials = credentialsStoreSnapshot.getCredentials(new AuthScope(proxy), null);
if (credentials != null) { if (credentials != null) {
final BasicScheme basicScheme = new BasicScheme(); final BasicScheme basicScheme = new BasicScheme();
basicScheme.initPreemptive(credentials); basicScheme.initPreemptive(credentials);
@ -186,8 +190,9 @@ public class Executor {
} }
public Executor clearAuth() { public Executor clearAuth() {
if (this.credentialsStore != null) { final CredentialsStore credentialsStoreSnapshot = credentialsStore;
this.credentialsStore.clear(); if (credentialsStoreSnapshot != null) {
credentialsStoreSnapshot.clear();
} }
return this; return this;
} }
@ -201,8 +206,9 @@ public class Executor {
} }
public Executor clearCookies() { public Executor clearCookies() {
if (this.cookieStore != null) { final CookieStore cookieStoreSnapshot = cookieStore;
this.cookieStore.clear(); if (cookieStoreSnapshot != null) {
cookieStoreSnapshot.clear();
} }
return this; return this;
} }
@ -218,14 +224,16 @@ public class Executor {
public Response execute( public Response execute(
final Request request) throws IOException { final Request request) throws IOException {
final HttpClientContext localContext = HttpClientContext.create(); final HttpClientContext localContext = HttpClientContext.create();
if (this.credentialsStore != null) { final CredentialsStore credentialsStoreSnapshot = credentialsStore;
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsStore); if (credentialsStoreSnapshot != null) {
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, credentialsStoreSnapshot);
} }
if (this.authCache != null) { if (this.authCache != null) {
localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache); localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
} }
if (this.cookieStore != null) { final CookieStore cookieStoreSnapshot = cookieStore;
localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); if (cookieStoreSnapshot != null) {
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStoreSnapshot);
} }
return new Response(request.internalExecute(this.httpclient, localContext)); return new Response(request.internalExecute(this.httpclient, localContext));
} }