Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions src/main/java/org/htmlunit/HttpWebConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
private static final String HACKED_COOKIE_POLICY = "mine";

// have one per thread because this is (re)configured for every call (see configureHttpProcessorBuilder)
// do not use a ThreadLocal because this in only accessed form this class
// do not use a ThreadLocal because this in only accessed form this class, but we still need it synchronized
private final Map<Thread, HttpClientBuilder> httpClientBuilder_ = new WeakHashMap<>();
private final WebClient webClient_;

Expand Down Expand Up @@ -211,7 +211,9 @@
// Calling code may catch the StackOverflowError, but due to the leak, the httpClient_ may
// come out of connections and throw a ConnectionPoolTimeoutException.
// => best solution, discard the HttpClient instance.
httpClientBuilder_.remove(Thread.currentThread());
synchronized (httpClientBuilder_) {

Check warning on line 214 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Use ReentrantLock rather than synchronization Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":18,"endLine":216,"startColumn":17,"startLine":214}}}],"message":{"text":"Use ReentrantLock rather than synchronization"},"ruleId":"AvoidSynchronizedStatement","ruleIndex":4}
httpClientBuilder_.remove(Thread.currentThread());

Check warning on line 215 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 To be compliant to J2EE, a webapp should not use any thread. Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":53,"endLine":215,"startColumn":47,"startLine":215}}}],"message":{"text":"To be compliant to J2EE, a webapp should not use any thread."},"ruleId":"DoNotUseThreads","ruleIndex":13}
}
throw e;
}
}
Expand Down Expand Up @@ -531,24 +533,27 @@
* @return the initialized HTTP client
*/
protected HttpClientBuilder getHttpClientBuilder() {
final Thread currentThread = Thread.currentThread();
HttpClientBuilder builder = httpClientBuilder_.get(currentThread);
if (builder == null) {
builder = createHttpClientBuilder();

// this factory is required later
// to be sure this is done, we do it outside the createHttpClient() call
final RegistryBuilder<CookieSpecProvider> registeryBuilder
= RegistryBuilder.<CookieSpecProvider>create()
.register(HACKED_COOKIE_POLICY, htmlUnitCookieSpecProvider_);
builder.setDefaultCookieSpecRegistry(registeryBuilder.build());

builder.setDefaultCookieStore(new HtmlUnitCookieStore(webClient_.getCookieManager()));
builder.setUserAgent(webClient_.getBrowserVersion().getUserAgent());
httpClientBuilder_.put(currentThread, builder);
synchronized (httpClientBuilder_)

Check warning on line 536 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Use ReentrantLock rather than synchronization Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":10,"endLine":556,"startColumn":9,"startLine":536}}}],"message":{"text":"Use ReentrantLock rather than synchronization"},"ruleId":"AvoidSynchronizedStatement","ruleIndex":4}
{

Check failure on line 537 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 '{' at column 9 should be on the previous line. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java:537:9: error: '{' at column 9 should be on the previous line. (com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck)

Check failure on line 537 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / build (21)

[CodeStyleTest::codeStyle] reported by reviewdog 🐶 Opening curly bracket is alone Raw Output: src/main/java/org/htmlunit/HttpWebConnection.java, line 537: Opening curly bracket is alone
final Thread currentThread = Thread.currentThread();

Check warning on line 538 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 To be compliant to J2EE, a webapp should not use any thread. Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":48,"endLine":538,"startColumn":42,"startLine":538}}}],"message":{"text":"To be compliant to J2EE, a webapp should not use any thread."},"ruleId":"DoNotUseThreads","ruleIndex":13}
HttpClientBuilder builder = httpClientBuilder_.get(currentThread);
if (builder == null) {
builder = createHttpClientBuilder();

Check failure on line 542 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Trailing whitespace found. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java:542:0: error: Trailing whitespace found. (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
// this factory is required later
// to be sure this is done, we do it outside the createHttpClient() call
final RegistryBuilder<CookieSpecProvider> registeryBuilder
= RegistryBuilder.<CookieSpecProvider>create()
.register(HACKED_COOKIE_POLICY, htmlUnitCookieSpecProvider_);
builder.setDefaultCookieSpecRegistry(registeryBuilder.build());

Check failure on line 549 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Trailing whitespace found. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java:549:0: error: Trailing whitespace found. (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
builder.setDefaultCookieStore(new HtmlUnitCookieStore(webClient_.getCookieManager()));
builder.setUserAgent(webClient_.getBrowserVersion().getUserAgent());
httpClientBuilder_.put(currentThread, builder);
}

Check failure on line 554 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Trailing whitespace found. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java:554:0: error: Trailing whitespace found. (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
return builder;
}

return builder;
}

/**
Expand Down Expand Up @@ -1288,7 +1293,9 @@
*/
@Override
public void close() {
httpClientBuilder_.clear();
synchronized (httpClientBuilder_) {

Check warning on line 1296 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Use ReentrantLock rather than synchronization Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":10,"endLine":1298,"startColumn":9,"startLine":1296}}}],"message":{"text":"Use ReentrantLock rather than synchronization"},"ruleId":"AvoidSynchronizedStatement","ruleIndex":4}
httpClientBuilder_.clear();
}
sharedAuthCache_.clear();
httpClientContextByThread_.clear();

Expand Down
Loading