View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  import static java.nio.charset.StandardCharsets.UTF_8;
19  import static org.htmlunit.BrowserVersionFeatures.HTTP_HEADER_CH_UA;
20  import static org.htmlunit.BrowserVersionFeatures.HTTP_HEADER_PRIORITY;
21  
22  import java.io.BufferedInputStream;
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.ObjectInputStream;
27  import java.io.Serializable;
28  import java.lang.ref.WeakReference;
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  import java.net.URLConnection;
32  import java.net.URLDecoder;
33  import java.nio.charset.Charset;
34  import java.nio.file.Files;
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.ConcurrentModificationException;
38  import java.util.Date;
39  import java.util.HashMap;
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.LinkedHashMap;
43  import java.util.LinkedHashSet;
44  import java.util.List;
45  import java.util.Locale;
46  import java.util.Map;
47  import java.util.Objects;
48  import java.util.Optional;
49  import java.util.Set;
50  import java.util.concurrent.ConcurrentLinkedDeque;
51  import java.util.concurrent.Executor;
52  import java.util.concurrent.ExecutorService;
53  import java.util.concurrent.Executors;
54  import java.util.concurrent.ThreadFactory;
55  import java.util.concurrent.ThreadPoolExecutor;
56  
57  import org.apache.commons.logging.Log;
58  import org.apache.commons.logging.LogFactory;
59  import org.apache.http.NoHttpResponseException;
60  import org.apache.http.client.CredentialsProvider;
61  import org.apache.http.cookie.MalformedCookieException;
62  import org.htmlunit.attachment.Attachment;
63  import org.htmlunit.attachment.AttachmentHandler;
64  import org.htmlunit.csp.Policy;
65  import org.htmlunit.csp.url.URI;
66  import org.htmlunit.css.ComputedCssStyleDeclaration;
67  import org.htmlunit.cssparser.parser.CSSErrorHandler;
68  import org.htmlunit.cssparser.parser.javacc.CSS3Parser;
69  import org.htmlunit.html.BaseFrameElement;
70  import org.htmlunit.html.DomElement;
71  import org.htmlunit.html.DomNode;
72  import org.htmlunit.html.FrameWindow;
73  import org.htmlunit.html.FrameWindow.PageDenied;
74  import org.htmlunit.html.HtmlElement;
75  import org.htmlunit.html.HtmlInlineFrame;
76  import org.htmlunit.html.HtmlPage;
77  import org.htmlunit.html.XHtmlPage;
78  import org.htmlunit.html.parser.HTMLParser;
79  import org.htmlunit.html.parser.HTMLParserListener;
80  import org.htmlunit.http.HttpStatus;
81  import org.htmlunit.http.HttpUtils;
82  import org.htmlunit.httpclient.HttpClientConverter;
83  import org.htmlunit.javascript.AbstractJavaScriptEngine;
84  import org.htmlunit.javascript.DefaultJavaScriptErrorListener;
85  import org.htmlunit.javascript.HtmlUnitScriptable;
86  import org.htmlunit.javascript.JavaScriptEngine;
87  import org.htmlunit.javascript.JavaScriptErrorListener;
88  import org.htmlunit.javascript.background.JavaScriptJobManager;
89  import org.htmlunit.javascript.host.BroadcastChannel;
90  import org.htmlunit.javascript.host.Location;
91  import org.htmlunit.javascript.host.Window;
92  import org.htmlunit.javascript.host.dom.Node;
93  import org.htmlunit.javascript.host.event.Event;
94  import org.htmlunit.javascript.host.file.Blob;
95  import org.htmlunit.javascript.host.html.HTMLIFrameElement;
96  import org.htmlunit.protocol.data.DataURLConnection;
97  import org.htmlunit.util.Cookie;
98  import org.htmlunit.util.HeaderUtils;
99  import org.htmlunit.util.MimeType;
100 import org.htmlunit.util.NameValuePair;
101 import org.htmlunit.util.StringUtils;
102 import org.htmlunit.util.UrlUtils;
103 import org.htmlunit.websocket.JettyWebSocketAdapter.JettyWebSocketAdapterFactory;
104 import org.htmlunit.websocket.WebSocketAdapter;
105 import org.htmlunit.websocket.WebSocketAdapterFactory;
106 import org.htmlunit.websocket.WebSocketListener;
107 import org.htmlunit.webstart.WebStartHandler;
108 
109 /**
110  * The main starting point in HtmlUnit: this class simulates a web browser.
111  * <p>
112  * A standard usage of HtmlUnit will start with using the {@link #getPage(String)} method
113  * (or {@link #getPage(URL)}) to load a first {@link Page}
114  * and will continue with further processing on this page depending on its type.
115  * </p>
116  * <b>Example:</b><br>
117  * <br>
118  * <code>
119  * final WebClient webClient = new WebClient();<br>
120  * final {@link HtmlPage} startPage = webClient.getPage("http://htmlunit.sf.net");<br>
121  * assertEquals("HtmlUnit - Welcome to HtmlUnit", startPage.{@link HtmlPage#getTitleText() getTitleText}());
122  * </code>
123  * <p>
124  * Note: a {@link WebClient} instance is <b>not thread safe</b>. It is intended to be used from a single thread.
125  * </p>
126  * @author Mike Bowler
127  * @author Mike J. Bresnahan
128  * @author Dominique Broeglin
129  * @author Noboru Sinohara
130  * @author Chen Jun
131  * @author David K. Taylor
132  * @author Christian Sell
133  * @author Ben Curren
134  * @author Marc Guillemot
135  * @author Chris Erskine
136  * @author Daniel Gredler
137  * @author Sergey Gorelkin
138  * @author Hans Donner
139  * @author Paul King
140  * @author Ahmed Ashour
141  * @author Bruce Chapman
142  * @author Sudhan Moghe
143  * @author Martin Tamme
144  * @author Amit Manjhi
145  * @author Nicolas Belisle
146  * @author Ronald Brill
147  * @author Frank Danek
148  * @author Joerg Werner
149  * @author Anton Demydenko
150  * @author Sergio Moreno
151  * @author Lai Quang Duong
152  * @author René Schwietzke
153  * @author Sven Strickroth
154  */
155 @SuppressWarnings("PMD.TooManyFields")
156 public class WebClient implements Serializable, AutoCloseable {
157 
158     /** Logging support. */
159     private static final Log LOG = LogFactory.getLog(WebClient.class);
160 
161     /** Like the Firefox default value for {@code network.http.redirection-limit}. */
162     private static final int ALLOWED_REDIRECTIONS_SAME_URL = 20;
163     private static final WebResponseData RESPONSE_DATA_NO_HTTP_RESPONSE = new WebResponseData(
164             0, "No HTTP Response", Collections.emptyList());
165 
166     /**
167      * These response headers are not copied from a 304 response to the cached
168      * response headers. This list is based on Chromium http_response_headers.cc
169      */
170     private static final String[] DISCARDING_304_RESPONSE_HEADER_NAMES = {
171         "connection",
172         "proxy-connection",
173         "keep-alive",
174         "www-authenticate",
175         "proxy-authenticate",
176         "proxy-authorization",
177         "te",
178         "trailer",
179         "transfer-encoding",
180         "upgrade",
181         "content-location",
182         "content-md5",
183         "etag",
184         "content-encoding",
185         "content-range",
186         "content-type",
187         "content-length",
188         "x-frame-options",
189         "x-xss-protection",
190     };
191 
192     private static final String[] DISCARDING_304_HEADER_PREFIXES = {
193         "x-content-",
194         "x-webkit-"
195     };
196 
197     private transient WebConnection webConnection_;
198     private CredentialsProvider credentialsProvider_ = new DefaultCredentialsProvider();
199     private CookieManager cookieManager_ = new CookieManager();
200     private WebSocketAdapterFactory webSocketAdapterFactory_;
201     private transient AbstractJavaScriptEngine<?> scriptEngine_;
202     private transient List<LoadJob> loadQueue_;
203     private final Map<String, String> requestHeaders_ = Collections.synchronizedMap(new HashMap<>(89));
204     private IncorrectnessListener incorrectnessListener_ = new IncorrectnessListenerImpl();
205     private WebConsole webConsole_;
206     private transient ExecutorService executor_;
207 
208     private AlertHandler alertHandler_;
209     private ConfirmHandler confirmHandler_;
210     private PromptHandler promptHandler_;
211     private StatusHandler statusHandler_;
212     private AttachmentHandler attachmentHandler_;
213     private ClipboardHandler clipboardHandler_;
214     private PrintHandler printHandler_;
215     private WebStartHandler webStartHandler_;
216     private FrameContentHandler frameContentHandler_;
217 
218     private AjaxController ajaxController_ = new AjaxController();
219 
220     private final BrowserVersion browserVersion_;
221     private PageCreator pageCreator_ = new DefaultPageCreator();
222 
223     // we need a separate one to be sure the one is always informed as first
224     // one. Only then we can make sure our state is consistent when the others
225     // are informed.
226     private CurrentWindowTracker currentWindowTracker_;
227     private final Set<WebWindowListener> webWindowListeners_ = new HashSet<>(5);
228 
229     private final List<TopLevelWindow> topLevelWindows_ =
230             Collections.synchronizedList(new ArrayList<>()); // top-level windows
231     private final List<WebWindow> windows_ = Collections.synchronizedList(new ArrayList<>()); // all windows
232     private transient List<WeakReference<JavaScriptJobManager>> jobManagers_ =
233             Collections.synchronizedList(new ArrayList<>());
234     private WebWindow currentWindow_;
235 
236     private HTMLParserListener htmlParserListener_;
237     private CSSErrorHandler cssErrorHandler_ = new DefaultCssErrorHandler();
238     private OnbeforeunloadHandler onbeforeunloadHandler_;
239     private Cache cache_ = new Cache();
240 
241     // mini pool to save resource when parsing CSS
242     private transient CSS3ParserPool css3ParserPool_ = new CSS3ParserPool();
243 
244     /** target "_blank". */
245     public static final String TARGET_BLANK = "_blank";
246 
247     /** target "_self". */
248     public static final String TARGET_SELF = "_self";
249 
250     /** target "_parent". */
251     private static final String TARGET_PARENT = "_parent";
252     /** target "_top". */
253     private static final String TARGET_TOP = "_top";
254 
255     private ScriptPreProcessor scriptPreProcessor_;
256 
257     private RefreshHandler refreshHandler_ = new NiceRefreshHandler(2);
258     private JavaScriptErrorListener javaScriptErrorListener_ = new DefaultJavaScriptErrorListener();
259 
260     private final WebClientOptions options_ = new WebClientOptions();
261     private final boolean javaScriptEngineEnabled_;
262     private final StorageHolder storageHolder_ = new StorageHolder();
263 
264     private transient Set<BroadcastChannel> broadcastChannel_ = new HashSet<>();
265 
266     /**
267      * Creates a web client instance using the browser version returned by
268      * {@link BrowserVersion#getDefault()}.
269      */
270     public WebClient() {
271         this(BrowserVersion.getDefault());
272     }
273 
274     /**
275      * Creates a web client instance using the specified {@link BrowserVersion}.
276      * @param browserVersion the browser version to simulate
277      */
278     public WebClient(final BrowserVersion browserVersion) {
279         this(browserVersion, null, -1);
280     }
281 
282     /**
283      * Creates an instance that will use the specified {@link BrowserVersion} and proxy server.
284      * @param browserVersion the browser version to simulate
285      * @param proxyHost the server that will act as proxy or null for no proxy
286      * @param proxyPort the port to use on the proxy server
287      */
288     public WebClient(final BrowserVersion browserVersion, final String proxyHost, final int proxyPort) {
289         this(browserVersion, true, proxyHost, proxyPort, null);
290     }
291 
292     /**
293      * Creates an instance that will use the specified {@link BrowserVersion} and proxy server.
294      * @param browserVersion the browser version to simulate
295      * @param proxyHost the server that will act as proxy or null for no proxy
296      * @param proxyPort the port to use on the proxy server
297      * @param proxyScheme the scheme http/https
298      */
299     public WebClient(final BrowserVersion browserVersion,
300             final String proxyHost, final int proxyPort, final String proxyScheme) {
301         this(browserVersion, true, proxyHost, proxyPort, proxyScheme);
302     }
303 
304     /**
305      * Creates an instance that will use the specified {@link BrowserVersion} and proxy server.
306      * @param browserVersion the browser version to simulate
307      * @param javaScriptEngineEnabled set to false if the simulated browser should not support javaScript
308      * @param proxyHost the server that will act as proxy or null for no proxy
309      * @param proxyPort the port to use on the proxy server
310      */
311     public WebClient(final BrowserVersion browserVersion, final boolean javaScriptEngineEnabled,
312             final String proxyHost, final int proxyPort) {
313         this(browserVersion, javaScriptEngineEnabled, proxyHost, proxyPort, null);
314     }
315 
316     /**
317      * Creates an instance that will use the specified {@link BrowserVersion} and proxy server.
318      * @param browserVersion the browser version to simulate
319      * @param javaScriptEngineEnabled set to false if the simulated browser should not support javaScript
320      * @param proxyHost the server that will act as proxy or null for no proxy
321      * @param proxyPort the port to use on the proxy server
322      * @param proxyScheme the scheme http/https
323      */
324     public WebClient(final BrowserVersion browserVersion, final boolean javaScriptEngineEnabled,
325             final String proxyHost, final int proxyPort, final String proxyScheme) {
326         WebAssert.notNull("browserVersion", browserVersion);
327 
328         browserVersion_ = browserVersion;
329         javaScriptEngineEnabled_ = javaScriptEngineEnabled;
330 
331         if (proxyHost == null) {
332             getOptions().setProxyConfig(new ProxyConfig());
333         }
334         else {
335             getOptions().setProxyConfig(new ProxyConfig(proxyHost, proxyPort, proxyScheme));
336         }
337 
338         webConnection_ = new HttpWebConnection(this); // this has to be done after the browser version was set
339         if (javaScriptEngineEnabled_) {
340             scriptEngine_ = new JavaScriptEngine(this);
341         }
342         loadQueue_ = new ArrayList<>();
343 
344         webSocketAdapterFactory_ = new JettyWebSocketAdapterFactory();
345 
346         // The window must be constructed AFTER the script engine.
347         currentWindowTracker_ = new CurrentWindowTracker(this, true);
348         currentWindow_ = new TopLevelWindow("", this);
349     }
350 
351     /**
352      * Our simple impl of a ThreadFactory (decorator) to be able to name
353      * our threads.
354      */
355     private static final class ThreadNamingFactory implements ThreadFactory {
356         private static int ID_ = 1;
357         private final ThreadFactory baseFactory_;
358 
359         ThreadNamingFactory(final ThreadFactory aBaseFactory) {
360             baseFactory_ = aBaseFactory;
361         }
362 
363         @Override
364         public Thread newThread(final Runnable aRunnable) {
365             final Thread thread = baseFactory_.newThread(aRunnable);
366             thread.setName("WebClient Thread " + ID_++);
367             return thread;
368         }
369     }
370 
371     /**
372      * Returns the object that will resolve all URL requests.
373      *
374      * @return the connection that will be used
375      */
376     public WebConnection getWebConnection() {
377         return webConnection_;
378     }
379 
380     /**
381      * Sets the object that will resolve all URL requests.
382      *
383      * @param webConnection the new web connection
384      */
385     public void setWebConnection(final WebConnection webConnection) {
386         WebAssert.notNull("webConnection", webConnection);
387         webConnection_ = webConnection;
388     }
389 
390     /**
391      * Send a request to a server and return a Page that represents the
392      * response from the server. This page will be used to populate the provided window.
393      * <p>
394      * The returned {@link Page} will be created by the {@link PageCreator}
395      * configured by {@link #setPageCreator(PageCreator)}, if any.
396      * <p>
397      * The {@link DefaultPageCreator} will create a {@link Page} depending on the content type of the HTTP response,
398      * basically {@link HtmlPage} for HTML content, {@link org.htmlunit.xml.XmlPage} for XML content,
399      * {@link TextPage} for other text content and {@link UnexpectedPage} for anything else.
400      *
401      * @param webWindow the WebWindow to load the result of the request into
402      * @param webRequest the web request
403      * @param <P> the page type
404      * @return the page returned by the server when the specified request was made in the specified window
405      * @throws IOException if an IO error occurs
406      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
407      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true
408      *
409      * @see WebRequest
410      */
411     public <P extends Page> P getPage(final WebWindow webWindow, final WebRequest webRequest)
412             throws IOException, FailingHttpStatusCodeException {
413         return getPage(webWindow, webRequest, true);
414     }
415 
416     /**
417      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
418      *
419      * Send a request to a server and return a Page that represents the
420      * response from the server. This page will be used to populate the provided window.
421      * <p>
422      * The returned {@link Page} will be created by the {@link PageCreator}
423      * configured by {@link #setPageCreator(PageCreator)}, if any.
424      * <p>
425      * The {@link DefaultPageCreator} will create a {@link Page} depending on the content type of the HTTP response,
426      * basically {@link HtmlPage} for HTML content, {@link org.htmlunit.xml.XmlPage} for XML content,
427      * {@link TextPage} for other text content and {@link UnexpectedPage} for anything else.
428      *
429      * @param webWindow the WebWindow to load the result of the request into
430      * @param webRequest the web request
431      * @param addToHistory true if the page should be part of the history
432      * @param <P> the page type
433      * @return the page returned by the server when the specified request was made in the specified window
434      * @throws IOException if an IO error occurs
435      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
436      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true
437      *
438      * @see WebRequest
439      */
440     @SuppressWarnings("unchecked")
441     <P extends Page> P getPage(final WebWindow webWindow, final WebRequest webRequest,
442             final boolean addToHistory)
443         throws IOException, FailingHttpStatusCodeException {
444 
445         final Page page = webWindow.getEnclosedPage();
446 
447         if (page != null) {
448             final URL prev = page.getUrl();
449             final URL current = webRequest.getUrl();
450             if (UrlUtils.sameFile(current, prev)
451                         && current.getRef() != null
452                         && !Objects.equals(current.getRef(), prev.getRef())) {
453                 // We're just navigating to an anchor within the current page.
454                 page.getWebResponse().getWebRequest().setUrl(current);
455                 if (addToHistory) {
456                     webWindow.getHistory().addPage(page);
457                 }
458 
459                 // clear the cache because the anchors are now matched by
460                 // the target pseudo style
461                 if (page instanceof HtmlPage) {
462                     ((HtmlPage) page).clearComputedStyles();
463                 }
464 
465                 final Window window = webWindow.getScriptableObject();
466                 if (window != null) { // js enabled
467                     window.getLocation().setHash(current.getRef());
468                 }
469                 return (P) page;
470             }
471 
472             if (page.isHtmlPage()) {
473                 final HtmlPage htmlPage = (HtmlPage) page;
474                 if (!htmlPage.isOnbeforeunloadAccepted()) {
475                     LOG.debug("The registered OnbeforeunloadHandler rejected to load a new page.");
476                     return (P) page;
477                 }
478             }
479         }
480 
481         if (LOG.isDebugEnabled()) {
482             LOG.debug("Get page for window named '" + webWindow.getName() + "', using " + webRequest);
483         }
484 
485         WebResponse webResponse;
486         final String protocol = webRequest.getUrl().getProtocol();
487         if ("javascript".equals(protocol)) {
488             webResponse = makeWebResponseForJavaScriptUrl(webWindow, webRequest.getUrl(), webRequest.getCharset());
489             if (webWindow.getEnclosedPage() != null && webWindow.getEnclosedPage().getWebResponse() == webResponse) {
490                 // a javascript:... url with result of type undefined didn't changed the page
491                 return (P) webWindow.getEnclosedPage();
492             }
493         }
494         else {
495             try {
496                 webResponse = loadWebResponse(webRequest);
497             }
498             catch (final NoHttpResponseException e) {
499                 webResponse = new WebResponse(RESPONSE_DATA_NO_HTTP_RESPONSE, webRequest, 0);
500             }
501         }
502 
503         printContentIfNecessary(webResponse);
504         loadWebResponseInto(webResponse, webWindow);
505 
506         // start execution here
507         // note: we have to do this also if the server reports an error!
508         //       e.g. if the server returns a 404 error page that includes javascript
509         if (scriptEngine_ != null) {
510             scriptEngine_.registerWindowAndMaybeStartEventLoop(webWindow);
511         }
512 
513         // check and report problems if needed
514         throwFailingHttpStatusCodeExceptionIfNecessary(webResponse);
515         return (P) webWindow.getEnclosedPage();
516     }
517 
518     /**
519      * Convenient method to build a URL and load it into the current WebWindow as it would be done
520      * by {@link #getPage(WebWindow, WebRequest)}.
521      * @param url the URL of the new content; in contrast to real browsers plain file url's are not supported.
522      *        You have to use the 'file', 'data', 'blob', 'http' or 'https' protocol.
523      * @param <P> the page type
524      * @return the new page
525      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
526      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true.
527      * @throws IOException if an IO problem occurs
528      * @throws MalformedURLException if no URL can be created from the provided string
529      */
530     public <P extends Page> P getPage(final String url) throws IOException, FailingHttpStatusCodeException,
531         MalformedURLException {
532         return getPage(UrlUtils.toUrlUnsafe(url));
533     }
534 
535     /**
536      * Convenient method to load a URL into the current top WebWindow as it would be done
537      * by {@link #getPage(WebWindow, WebRequest)}.
538      * @param url the URL of the new content; in contrast to real browsers plain file url's are not supported.
539      *        You have to use the 'file', 'data', 'blob', 'http' or 'https' protocol.
540      * @param <P> the page type
541      * @return the new page
542      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
543      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true.
544      * @throws IOException if an IO problem occurs
545      */
546     public <P extends Page> P getPage(final URL url) throws IOException, FailingHttpStatusCodeException {
547         final WebRequest request = new WebRequest(url, getBrowserVersion().getHtmlAcceptHeader(),
548                                                           getBrowserVersion().getAcceptEncodingHeader());
549         request.setCharset(UTF_8);
550         return getPage(getCurrentWindow().getTopWindow(), request);
551     }
552 
553     /**
554      * Convenient method to load a web request into the current top WebWindow.
555      * @param request the request parameters
556      * @param <P> the page type
557      * @return the new page
558      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
559      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true.
560      * @throws IOException if an IO problem occurs
561      * @see #getPage(WebWindow,WebRequest)
562      */
563     public <P extends Page> P getPage(final WebRequest request) throws IOException,
564         FailingHttpStatusCodeException {
565         return getPage(getCurrentWindow().getTopWindow(), request);
566     }
567 
568     /**
569      * <p>Creates a page based on the specified response and inserts it into the specified window. All page
570      * initialization and event notification is handled here.</p>
571      *
572      * <p>Note that if the page created is an attachment page, and an {@link AttachmentHandler} has been
573      * registered with this client, the page is <b>not</b> loaded into the specified window; in this case,
574      * the page is loaded into a new window, and attachment handling is delegated to the registered
575      * <code>AttachmentHandler</code>.</p>
576      *
577      * @param webResponse the response that will be used to create the new page
578      * @param webWindow the window that the new page will be placed within
579      * @throws IOException if an IO error occurs
580      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
581      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true
582      * @return the newly created page
583      * @see #setAttachmentHandler(AttachmentHandler)
584      */
585     public Page loadWebResponseInto(final WebResponse webResponse, final WebWindow webWindow)
586         throws IOException, FailingHttpStatusCodeException {
587         return loadWebResponseInto(webResponse, webWindow, null);
588     }
589 
590     /**
591      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
592      *
593      * <p>Creates a page based on the specified response and inserts it into the specified window. All page
594      * initialization and event notification is handled here.</p>
595      *
596      * <p>Note that if the page created is an attachment page, and an {@link AttachmentHandler} has been
597      * registered with this client, the page is <b>not</b> loaded into the specified window; in this case,
598      * the page is loaded into a new window, and attachment handling is delegated to the registered
599      * <code>AttachmentHandler</code>.</p>
600      *
601      * @param webResponse the response that will be used to create the new page
602      * @param webWindow the window that the new page will be placed within
603      * @param forceAttachmentWithFilename if not {@code null}, handle this as an attachment with the specified name
604      *        or if an empty string ("") use the filename provided in the response
605      * @throws IOException if an IO error occurs
606      * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
607      *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true
608      * @return the newly created page
609      * @see #setAttachmentHandler(AttachmentHandler)
610      */
611     public Page loadWebResponseInto(final WebResponse webResponse, final WebWindow webWindow,
612             String forceAttachmentWithFilename)
613             throws IOException, FailingHttpStatusCodeException {
614         WebAssert.notNull("webResponse", webResponse);
615         WebAssert.notNull("webWindow", webWindow);
616 
617         if (webResponse.getStatusCode() == HttpStatus.NO_CONTENT_204) {
618             return webWindow.getEnclosedPage();
619         }
620 
621         if (webStartHandler_ != null && "application/x-java-jnlp-file".equals(webResponse.getContentType())) {
622             webStartHandler_.handleJnlpResponse(webResponse);
623             return webWindow.getEnclosedPage();
624         }
625 
626         if (attachmentHandler_ != null
627                 && (forceAttachmentWithFilename != null || attachmentHandler_.isAttachment(webResponse))) {
628 
629             // check content disposition header for nothing provided
630             if (StringUtils.isEmptyOrNull(forceAttachmentWithFilename)) {
631                 final String disp = webResponse.getResponseHeaderValue(HttpHeader.CONTENT_DISPOSITION);
632                 forceAttachmentWithFilename = Attachment.getSuggestedFilename(disp);
633             }
634 
635             if (attachmentHandler_.handleAttachment(webResponse,
636                         StringUtils.isEmptyOrNull(forceAttachmentWithFilename) ? null : forceAttachmentWithFilename)) {
637                 // the handling is done by the attachment handler;
638                 // do not open a new window
639                 return webWindow.getEnclosedPage();
640             }
641 
642             final WebWindow w = openWindow(null, null, webWindow);
643             final Page page = pageCreator_.createPage(webResponse, w);
644             attachmentHandler_.handleAttachment(page,
645                                 StringUtils.isEmptyOrNull(forceAttachmentWithFilename)
646                                         ? null : forceAttachmentWithFilename);
647             return page;
648         }
649 
650         final Page oldPage = webWindow.getEnclosedPage();
651         if (oldPage != null) {
652             // Remove the old page before create new one.
653             oldPage.cleanUp();
654         }
655 
656         Page newPage = null;
657         FrameWindow.PageDenied pageDenied = PageDenied.NONE;
658         if (windows_.contains(webWindow)) {
659             if (webWindow instanceof FrameWindow) {
660                 final String contentSecurityPolicy =
661                         webResponse.getResponseHeaderValue(HttpHeader.CONTENT_SECURIRY_POLICY);
662                 if (StringUtils.isNotBlank(contentSecurityPolicy)) {
663                     final URL origin = UrlUtils.getUrlWithoutPathRefQuery(
664                             ((FrameWindow) webWindow).getEnclosingPage().getUrl());
665                     final URL source = UrlUtils.getUrlWithoutPathRefQuery(webResponse.getWebRequest().getUrl());
666                     final Policy policy = Policy.parseSerializedCSP(contentSecurityPolicy,
667                                                     Policy.PolicyErrorConsumer.ignored);
668                     if (!policy.allowsFrameAncestor(
669                             Optional.of(URI.parseURI(source.toExternalForm()).orElse(null)),
670                             Optional.of(URI.parseURI(origin.toExternalForm()).orElse(null)))) {
671                         pageDenied = PageDenied.BY_CONTENT_SECURIRY_POLICY;
672 
673                         if (LOG.isWarnEnabled()) {
674                             LOG.warn("Load denied by Content-Security-Policy: '" + contentSecurityPolicy + "' - "
675                                     + webResponse.getWebRequest().getUrl() + "' does not permit framing.");
676                         }
677                     }
678                 }
679 
680                 if (pageDenied == PageDenied.NONE) {
681                     final String xFrameOptions = webResponse.getResponseHeaderValue(HttpHeader.X_FRAME_OPTIONS);
682                     if ("DENY".equalsIgnoreCase(xFrameOptions)) {
683                         pageDenied = PageDenied.BY_X_FRAME_OPTIONS;
684 
685                         if (LOG.isWarnEnabled()) {
686                             LOG.warn("Load denied by X-Frame-Options: DENY; - '"
687                                     + webResponse.getWebRequest().getUrl() + "' does not permit framing.");
688                         }
689                     }
690                 }
691             }
692 
693             if (pageDenied == PageDenied.NONE) {
694                 newPage = pageCreator_.createPage(webResponse, webWindow);
695             }
696             else {
697                 try {
698                     final WebResponse aboutBlank = loadWebResponse(WebRequest.newAboutBlankRequest());
699                     newPage = pageCreator_.createPage(aboutBlank, webWindow);
700                     // TODO - maybe we have to attach to original request/response to the page
701 
702                     ((FrameWindow) webWindow).setPageDenied(pageDenied);
703                 }
704                 catch (final IOException ignored) {
705                     // ignore
706                 }
707             }
708 
709             if (windows_.contains(webWindow)) {
710                 fireWindowContentChanged(new WebWindowEvent(webWindow, WebWindowEvent.CHANGE, oldPage, newPage));
711 
712                 // The page being loaded may already have been replaced by another page via JavaScript code.
713                 if (webWindow.getEnclosedPage() == newPage) {
714                     newPage.initialize();
715                     // hack: onload should be fired the same way for all type of pages
716                     // here is a hack to handle non HTML pages
717                     if (isJavaScriptEnabled()
718                             && webWindow instanceof FrameWindow && !newPage.isHtmlPage()) {
719                         final FrameWindow fw = (FrameWindow) webWindow;
720                         final BaseFrameElement frame = fw.getFrameElement();
721                         if (frame.hasEventHandlers("onload")) {
722                             if (LOG.isDebugEnabled()) {
723                                 LOG.debug("Executing onload handler for " + frame);
724                             }
725                             final Event event = new Event(frame, Event.TYPE_LOAD);
726                             ((Node) frame.getScriptableObject()).executeEventLocally(event);
727                         }
728                     }
729                 }
730             }
731         }
732         return newPage;
733     }
734 
735     /**
736      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
737      *
738      * <p>Logs the response's content if its status code indicates a request failure and
739      * {@link WebClientOptions#isPrintContentOnFailingStatusCode()} returns {@code true}.
740      *
741      * @param webResponse the response whose content may be logged
742      */
743     public void printContentIfNecessary(final WebResponse webResponse) {
744         if (getOptions().isPrintContentOnFailingStatusCode()
745                 && !webResponse.isSuccess() && LOG.isInfoEnabled()) {
746             final String contentType = webResponse.getContentType();
747             LOG.info("statusCode=[" + webResponse.getStatusCode() + "] contentType=[" + contentType + "]");
748             LOG.info(webResponse.getContentAsString());
749         }
750     }
751 
752     /**
753      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
754      *
755      * <p>Throws a {@link FailingHttpStatusCodeException} if the request's status code indicates a request
756      * failure and {@link WebClientOptions#isThrowExceptionOnFailingStatusCode()} returns {@code true}.
757      *
758      * @param webResponse the response which may trigger a {@link FailingHttpStatusCodeException}
759      */
760     public void throwFailingHttpStatusCodeExceptionIfNecessary(final WebResponse webResponse) {
761         if (getOptions().isThrowExceptionOnFailingStatusCode() && !webResponse.isSuccessOrUseProxyOrNotModified()) {
762             throw new FailingHttpStatusCodeException(webResponse);
763         }
764     }
765 
766     /**
767      * Adds a header which will be sent with EVERY request from this client.
768      * This list is empty per default; use this to add specific headers for your
769      * case.
770      * @param name the name of the header to add
771      * @param value the value of the header to add
772      * @see #removeRequestHeader(String)
773      */
774     public void addRequestHeader(final String name, final String value) {
775         if (HttpHeader.COOKIE_LC.equalsIgnoreCase(name)) {
776             throw new IllegalArgumentException("Do not add 'Cookie' header, use .getCookieManager() instead");
777         }
778         requestHeaders_.put(name, value);
779     }
780 
781     /**
782      * Removes a header from being sent with EVERY request from this client.
783      * This list is empty per default; use this method to remove specific headers
784      * your have added using {{@link #addRequestHeader(String, String)} before.<br>
785      * You can't use this to avoid sending standard headers like "Accept-Language"
786      * or "Sec-Fetch-Dest".
787      * @param name the name of the header to remove
788      * @see #addRequestHeader
789      */
790     public void removeRequestHeader(final String name) {
791         requestHeaders_.remove(name);
792     }
793 
794     /**
795      * Sets the credentials provider that will provide authentication information when
796      * trying to access protected information on a web server. This information is
797      * required when the server is using Basic HTTP authentication, NTLM authentication,
798      * or Digest authentication.
799      * @param credentialsProvider the new credentials provider to use to authenticate
800      */
801     public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
802         WebAssert.notNull("credentialsProvider", credentialsProvider);
803         credentialsProvider_ = credentialsProvider;
804     }
805 
806     /**
807      * Returns the credentials provider for this client instance. By default, this
808      * method returns an instance of {@link DefaultCredentialsProvider}.
809      * @return the credentials provider for this client instance
810      */
811     public CredentialsProvider getCredentialsProvider() {
812         return credentialsProvider_;
813     }
814 
815     /**
816      * This method is intended for testing only - use at your own risk.
817      * @return the current JavaScript engine (never {@code null})
818      */
819     public AbstractJavaScriptEngine<?> getJavaScriptEngine() {
820         return scriptEngine_;
821     }
822 
823     /**
824      * This method is intended for testing only - use at your own risk.
825      *
826      * @param engine the new script engine to use
827      */
828     public void setJavaScriptEngine(final AbstractJavaScriptEngine<?> engine) {
829         if (engine == null) {
830             throw new IllegalArgumentException("Can't set JavaScriptEngine to null");
831         }
832         scriptEngine_ = engine;
833     }
834 
835     /**
836      * Returns the cookie manager used by this web client.
837      * @return the cookie manager used by this web client
838      */
839     public CookieManager getCookieManager() {
840         return cookieManager_;
841     }
842 
843     /**
844      * Sets the cookie manager used by this web client.
845      * @param cookieManager the cookie manager used by this web client
846      */
847     public void setCookieManager(final CookieManager cookieManager) {
848         WebAssert.notNull("cookieManager", cookieManager);
849         cookieManager_ = cookieManager;
850     }
851 
852     /**
853      * Sets the alert handler for this webclient.
854      * @param alertHandler the new alerthandler or null if none is specified
855      */
856     public void setAlertHandler(final AlertHandler alertHandler) {
857         alertHandler_ = alertHandler;
858     }
859 
860     /**
861      * Returns the alert handler for this webclient.
862      * @return the alert handler or null if one hasn't been set
863      */
864     public AlertHandler getAlertHandler() {
865         return alertHandler_;
866     }
867 
868     /**
869      * Sets the handler that will be executed when the JavaScript method Window.confirm() is called.
870      * @param handler the new handler or null if no handler is to be used
871      */
872     public void setConfirmHandler(final ConfirmHandler handler) {
873         confirmHandler_ = handler;
874     }
875 
876     /**
877      * Returns the confirm handler.
878      * @return the confirm handler or null if one hasn't been set
879      */
880     public ConfirmHandler getConfirmHandler() {
881         return confirmHandler_;
882     }
883 
884     /**
885      * Sets the handler that will be executed when the JavaScript method Window.prompt() is called.
886      * @param handler the new handler or null if no handler is to be used
887      */
888     public void setPromptHandler(final PromptHandler handler) {
889         promptHandler_ = handler;
890     }
891 
892     /**
893      * Returns the prompt handler.
894      * @return the prompt handler or null if one hasn't been set
895      */
896     public PromptHandler getPromptHandler() {
897         return promptHandler_;
898     }
899 
900     /**
901      * Sets the status handler for this webclient.
902      * @param statusHandler the new status handler or null if none is specified
903      */
904     public void setStatusHandler(final StatusHandler statusHandler) {
905         statusHandler_ = statusHandler;
906     }
907 
908     /**
909      * Returns the status handler for this {@link WebClient}.
910      * @return the status handler or null if one hasn't been set
911      */
912     public StatusHandler getStatusHandler() {
913         return statusHandler_;
914     }
915 
916     /**
917      * Returns the executor for this {@link WebClient}.
918      * @return the executor
919      */
920     public synchronized Executor getExecutor() {
921         if (executor_ == null) {
922             final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
923             threadPoolExecutor.setThreadFactory(new ThreadNamingFactory(threadPoolExecutor.getThreadFactory()));
924             // threadPoolExecutor.prestartAllCoreThreads();
925             executor_ = threadPoolExecutor;
926         }
927 
928         return executor_;
929     }
930 
931     /**
932      * Changes the ExecutorService for this {@link WebClient}.
933      * You have to call this before the first use of the executor, otherwise
934      * an IllegalStateExceptions is thrown.
935      * @param executor the new Executor.
936      */
937     public synchronized void setExecutor(final ExecutorService executor) {
938         if (executor_ != null) {
939             throw new IllegalStateException("Can't change the executor after first use.");
940         }
941 
942         executor_ = executor;
943     }
944 
945     /**
946      * Sets the javascript error listener for this {@link WebClient}.
947      * When setting to null, the {@link DefaultJavaScriptErrorListener} is used.
948      * @param javaScriptErrorListener the new JavaScriptErrorListener or null if none is specified
949      */
950     public void setJavaScriptErrorListener(final JavaScriptErrorListener javaScriptErrorListener) {
951         if (javaScriptErrorListener == null) {
952             javaScriptErrorListener_ = new DefaultJavaScriptErrorListener();
953         }
954         else {
955             javaScriptErrorListener_ = javaScriptErrorListener;
956         }
957     }
958 
959     /**
960      * Returns the javascript error listener for this {@link WebClient}.
961      * @return the javascript error listener or null if one hasn't been set
962      */
963     public JavaScriptErrorListener getJavaScriptErrorListener() {
964         return javaScriptErrorListener_;
965     }
966 
967     /**
968      * Returns the current browser version.
969      * @return the current browser version
970      */
971     public BrowserVersion getBrowserVersion() {
972         return browserVersion_;
973     }
974 
975     /**
976      * Returns the "current" window for this client. This window (or its top window) will be used
977      * when <code>getPage(...)</code> is called without specifying a window.
978      * @return the "current" window for this client
979      */
980     public WebWindow getCurrentWindow() {
981         return currentWindow_;
982     }
983 
984     /**
985      * Sets the "current" window for this client. This is the window that will be used when
986      * <code>getPage(...)</code> is called without specifying a window.
987      * @param window the new "current" window for this client
988      */
989     public void setCurrentWindow(final WebWindow window) {
990         WebAssert.notNull("window", window);
991         if (currentWindow_ == window) {
992             return;
993         }
994         // onBlur event is triggered for focused element of old current window
995         if (currentWindow_ != null && !currentWindow_.isClosed()) {
996             final Page enclosedPage = currentWindow_.getEnclosedPage();
997             if (enclosedPage != null && enclosedPage.isHtmlPage()) {
998                 final DomElement focusedElement = ((HtmlPage) enclosedPage).getFocusedElement();
999                 if (focusedElement != null) {
1000                     focusedElement.fireEvent(Event.TYPE_BLUR);
1001                 }
1002             }
1003         }
1004         currentWindow_ = window;
1005 
1006         // when marking an iframe window as current we have no need to move the focus
1007         final boolean isIFrame = currentWindow_ instanceof FrameWindow
1008                 && ((FrameWindow) currentWindow_).getFrameElement() instanceof HtmlInlineFrame;
1009         if (!isIFrame) {
1010             //1. activeElement becomes focused element for new current window
1011             //2. onFocus event is triggered for focusedElement of new current window
1012             final Page enclosedPage = currentWindow_.getEnclosedPage();
1013             if (enclosedPage != null && enclosedPage.isHtmlPage()) {
1014                 final HtmlPage enclosedHtmlPage = (HtmlPage) enclosedPage;
1015                 final HtmlElement activeElement = enclosedHtmlPage.getActiveElement();
1016                 if (activeElement != null) {
1017                     enclosedHtmlPage.setFocusedElement(activeElement, true);
1018                 }
1019             }
1020         }
1021     }
1022 
1023     /**
1024      * Adds a listener for {@link WebWindowEvent}s. All events from all windows associated with this
1025      * client will be sent to the specified listener.
1026      * @param listener a listener
1027      */
1028     public void addWebWindowListener(final WebWindowListener listener) {
1029         WebAssert.notNull("listener", listener);
1030         webWindowListeners_.add(listener);
1031     }
1032 
1033     /**
1034      * Removes a listener for {@link WebWindowEvent}s.
1035      * @param listener a listener
1036      */
1037     public void removeWebWindowListener(final WebWindowListener listener) {
1038         WebAssert.notNull("listener", listener);
1039         webWindowListeners_.remove(listener);
1040     }
1041 
1042     private void fireWindowContentChanged(final WebWindowEvent event) {
1043         if (currentWindowTracker_ != null) {
1044             currentWindowTracker_.webWindowContentChanged(event);
1045         }
1046         for (final WebWindowListener listener : new ArrayList<>(webWindowListeners_)) {
1047             listener.webWindowContentChanged(event);
1048         }
1049     }
1050 
1051     private void fireWindowOpened(final WebWindowEvent event) {
1052         if (currentWindowTracker_ != null) {
1053             currentWindowTracker_.webWindowOpened(event);
1054         }
1055         for (final WebWindowListener listener : new ArrayList<>(webWindowListeners_)) {
1056             listener.webWindowOpened(event);
1057         }
1058     }
1059 
1060     private void fireWindowClosed(final WebWindowEvent event) {
1061         if (currentWindowTracker_ != null) {
1062             currentWindowTracker_.webWindowClosed(event);
1063         }
1064 
1065         for (final WebWindowListener listener : new ArrayList<>(webWindowListeners_)) {
1066             listener.webWindowClosed(event);
1067         }
1068 
1069         // to open a new top level window if all others are gone
1070         if (currentWindowTracker_ != null) {
1071             currentWindowTracker_.afterWebWindowClosedListenersProcessed(event);
1072         }
1073     }
1074 
1075     /**
1076      * Open a new window with the specified name. If the URL is non-null then attempt to load
1077      * a page from that location and put it in the new window.
1078      *
1079      * @param url the URL to load content from or null if no content is to be loaded
1080      * @param windowName the name of the new window
1081      * @return the new window
1082      */
1083     public WebWindow openWindow(final URL url, final String windowName) {
1084         WebAssert.notNull("windowName", windowName);
1085         return openWindow(url, windowName, getCurrentWindow());
1086     }
1087 
1088     /**
1089      * Open a new window with the specified name. If the URL is non-null then attempt to load
1090      * a page from that location and put it in the new window.
1091      *
1092      * @param url the URL to load content from or null if no content is to be loaded
1093      * @param windowName the name of the new window
1094      * @param opener the web window that is calling openWindow
1095      * @return the new window
1096      */
1097     public WebWindow openWindow(final URL url, final String windowName, final WebWindow opener) {
1098         final WebWindow window = openTargetWindow(opener, windowName, TARGET_BLANK);
1099         if (url == null) {
1100             initializeEmptyWindow(window, window.getEnclosedPage());
1101         }
1102         else {
1103             try {
1104                 final WebRequest request = new WebRequest(url, getBrowserVersion().getHtmlAcceptHeader(),
1105                                                                 getBrowserVersion().getAcceptEncodingHeader());
1106                 request.setCharset(UTF_8);
1107 
1108                 final Page openerPage = opener.getEnclosedPage();
1109                 if (openerPage != null && openerPage.getUrl() != null) {
1110                     request.setRefererHeader(openerPage.getUrl());
1111                 }
1112                 getPage(window, request);
1113             }
1114             catch (final IOException e) {
1115                 LOG.error("Error loading content into window", e);
1116             }
1117         }
1118         return window;
1119     }
1120 
1121     /**
1122      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1123      *
1124      * Open the window with the specified name. The name may be a special
1125      * target name of _self, _parent, _top, or _blank. An empty or null
1126      * name is set to the default. The special target names are relative to
1127      * the opener window.
1128      *
1129      * @param opener the web window that is calling openWindow
1130      * @param windowName the name of the new window
1131      * @param defaultName the default target if no name is given
1132      * @return the new window
1133      */
1134     public WebWindow openTargetWindow(
1135             final WebWindow opener, final String windowName, final String defaultName) {
1136 
1137         WebAssert.notNull("opener", opener);
1138         WebAssert.notNull("defaultName", defaultName);
1139 
1140         String windowToOpen = windowName;
1141         if (windowToOpen == null || windowToOpen.isEmpty()) {
1142             windowToOpen = defaultName;
1143         }
1144 
1145         WebWindow webWindow = resolveWindow(opener, windowToOpen);
1146 
1147         if (webWindow == null) {
1148             if (TARGET_BLANK.equals(windowToOpen)) {
1149                 windowToOpen = "";
1150             }
1151             webWindow = new TopLevelWindow(windowToOpen, this);
1152         }
1153 
1154         if (webWindow instanceof TopLevelWindow && webWindow != opener.getTopWindow()) {
1155             ((TopLevelWindow) webWindow).setOpener(opener);
1156         }
1157 
1158         return webWindow;
1159     }
1160 
1161     private WebWindow resolveWindow(final WebWindow opener, final String name) {
1162         if (name == null || name.isEmpty() || TARGET_SELF.equals(name)) {
1163             return opener;
1164         }
1165 
1166         if (TARGET_PARENT.equals(name)) {
1167             return opener.getParentWindow();
1168         }
1169 
1170         if (TARGET_TOP.equals(name)) {
1171             return opener.getTopWindow();
1172         }
1173 
1174         if (TARGET_BLANK.equals(name)) {
1175             return null;
1176         }
1177 
1178         // first search for frame windows inside our window hierarchy
1179         WebWindow window = opener;
1180         while (true) {
1181             final Page page = window.getEnclosedPage();
1182             if (page != null && page.isHtmlPage()) {
1183                 try {
1184                     final FrameWindow frame = ((HtmlPage) page).getFrameByName(name);
1185                     final HtmlUnitScriptable scriptable = frame.getFrameElement().getScriptableObject();
1186                     if (scriptable instanceof HTMLIFrameElement) {
1187                         ((HTMLIFrameElement) scriptable).onRefresh();
1188                     }
1189                     return frame;
1190                 }
1191                 catch (final ElementNotFoundException expected) {
1192                     // Fall through
1193                 }
1194             }
1195 
1196             if (window == window.getParentWindow()) {
1197                 // TODO: should getParentWindow() return null on top windows?
1198                 break;
1199             }
1200             window = window.getParentWindow();
1201         }
1202 
1203         try {
1204             return getWebWindowByName(name);
1205         }
1206         catch (final WebWindowNotFoundException expected) {
1207             // Fall through - a new window will be created below
1208         }
1209         return null;
1210     }
1211 
1212     /**
1213      * <p><span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span></p>
1214      *
1215      * Opens a new dialog window.
1216      * @param url the URL of the document to load and display
1217      * @param opener the web window that is opening the dialog
1218      * @param dialogArguments the object to make available inside the dialog via <code>window.dialogArguments</code>
1219      * @return the new dialog window
1220      * @throws IOException if there is an IO error
1221      */
1222     public DialogWindow openDialogWindow(final URL url, final WebWindow opener, final Object dialogArguments)
1223         throws IOException {
1224 
1225         WebAssert.notNull("url", url);
1226         WebAssert.notNull("opener", opener);
1227 
1228         final DialogWindow window = new DialogWindow(this, dialogArguments);
1229 
1230         final HtmlPage openerPage = (HtmlPage) opener.getEnclosedPage();
1231         final WebRequest request = new WebRequest(url, getBrowserVersion().getHtmlAcceptHeader(),
1232                                                         getBrowserVersion().getAcceptEncodingHeader());
1233         request.setCharset(UTF_8);
1234 
1235         if (openerPage != null) {
1236             request.setRefererHeader(openerPage.getUrl());
1237         }
1238 
1239         getPage(window, request);
1240 
1241         return window;
1242     }
1243 
1244     /**
1245      * Sets the object that will be used to create pages. Set this if you want
1246      * to customize the type of page that is returned for a given content type.
1247      *
1248      * @param pageCreator the new page creator
1249      */
1250     public void setPageCreator(final PageCreator pageCreator) {
1251         WebAssert.notNull("pageCreator", pageCreator);
1252         pageCreator_ = pageCreator;
1253     }
1254 
1255     /**
1256      * Returns the current page creator.
1257      *
1258      * @return the page creator
1259      */
1260     public PageCreator getPageCreator() {
1261         return pageCreator_;
1262     }
1263 
1264     /**
1265      * Returns the first {@link WebWindow} that matches the specified name.
1266      *
1267      * @param name the name to search for
1268      * @return the {@link WebWindow} with the specified name
1269      * @throws WebWindowNotFoundException if the {@link WebWindow} can't be found
1270      * @see #getWebWindows()
1271      * @see #getTopLevelWindows()
1272      */
1273     public WebWindow getWebWindowByName(final String name) throws WebWindowNotFoundException {
1274         WebAssert.notNull("name", name);
1275 
1276         for (final WebWindow webWindow : windows_) {
1277             if (name.equals(webWindow.getName())) {
1278                 return webWindow;
1279             }
1280         }
1281 
1282         throw new WebWindowNotFoundException(name);
1283     }
1284 
1285     /**
1286      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1287      *
1288      * Initializes a new web window for JavaScript.
1289      * @param webWindow the new WebWindow
1290      * @param page the page that will become the enclosing page
1291      */
1292     public void initialize(final WebWindow webWindow, final Page page) {
1293         WebAssert.notNull("webWindow", webWindow);
1294 
1295         if (isJavaScriptEngineEnabled()) {
1296             scriptEngine_.initialize(webWindow, page);
1297         }
1298     }
1299 
1300     /**
1301      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1302      *
1303      * Initializes a new empty window for JavaScript.
1304      *
1305      * @param webWindow the new WebWindow
1306      * @param page the page that will become the enclosing page
1307      */
1308     public void initializeEmptyWindow(final WebWindow webWindow, final Page page) {
1309         WebAssert.notNull("webWindow", webWindow);
1310 
1311         if (isJavaScriptEngineEnabled()) {
1312             initialize(webWindow, page);
1313             ((Window) webWindow.getScriptableObject()).initialize();
1314         }
1315     }
1316 
1317     /**
1318      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1319      *
1320      * Adds a new window to the list of available windows.
1321      *
1322      * @param webWindow the new WebWindow
1323      */
1324     public void registerWebWindow(final WebWindow webWindow) {
1325         WebAssert.notNull("webWindow", webWindow);
1326         if (windows_.add(webWindow)) {
1327             fireWindowOpened(new WebWindowEvent(webWindow, WebWindowEvent.OPEN, webWindow.getEnclosedPage(), null));
1328         }
1329         // register JobManager here but don't deregister in deregisterWebWindow as it can live longer
1330         jobManagers_.add(new WeakReference<>(webWindow.getJobManager()));
1331     }
1332 
1333     /**
1334      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1335      *
1336      * Removes a window from the list of available windows.
1337      *
1338      * @param webWindow the window to remove
1339      */
1340     public void deregisterWebWindow(final WebWindow webWindow) {
1341         WebAssert.notNull("webWindow", webWindow);
1342         if (windows_.remove(webWindow)) {
1343             fireWindowClosed(new WebWindowEvent(webWindow, WebWindowEvent.CLOSE, webWindow.getEnclosedPage(), null));
1344         }
1345     }
1346 
1347     /**
1348      * Expands a relative URL relative to the specified base. In most situations
1349      * this is the same as <code>new URL(baseUrl, relativeUrl)</code> but
1350      * there are some cases that URL doesn't handle correctly. See
1351      * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a>
1352      * regarding Relative Uniform Resource Locators for more information.
1353      *
1354      * @param baseUrl the base URL
1355      * @param relativeUrl the relative URL
1356      * @return the expansion of the specified base and relative URLs
1357      * @throws MalformedURLException if an error occurred when creating a URL object
1358      */
1359     public static URL expandUrl(final URL baseUrl, final String relativeUrl) throws MalformedURLException {
1360         final String newUrl = UrlUtils.resolveUrl(baseUrl, relativeUrl);
1361         return UrlUtils.toUrlUnsafe(newUrl);
1362     }
1363 
1364     private WebResponse makeWebResponseForDataUrl(final WebRequest webRequest) throws IOException {
1365         final URL url = webRequest.getUrl();
1366 
1367         final DataURLConnection connection = new DataURLConnection(url);
1368 
1369         final List<NameValuePair> responseHeaders = new ArrayList<>();
1370         responseHeaders.add(new NameValuePair(HttpHeader.CONTENT_TYPE_LC,
1371             connection.getMediaType() + ";charset=" + connection.getCharset()));
1372 
1373         if (HttpMethod.HEAD.equals(webRequest.getHttpMethod())) {
1374             final WebResponseData data = new WebResponseData(200, "OK", responseHeaders);
1375             return new WebResponse(data, url, webRequest.getHttpMethod(), 0);
1376         }
1377 
1378         try (InputStream is = connection.getInputStream()) {
1379             final DownloadedContent downloadedContent =
1380                     HttpWebConnection.downloadContent(is,
1381                             getOptions().getMaxInMemory(),
1382                             getOptions().getTempFileDirectory());
1383             final WebResponseData data = new WebResponseData(downloadedContent, 200, "OK", responseHeaders);
1384             return new WebResponse(data, url, webRequest.getHttpMethod(), 0);
1385         }
1386     }
1387 
1388     private static WebResponse makeWebResponseForAboutUrl(final WebRequest webRequest) throws MalformedURLException {
1389         final URL url = webRequest.getUrl();
1390         final String urlString = url.toExternalForm();
1391         if (UrlUtils.ABOUT_BLANK.equalsIgnoreCase(urlString)) {
1392             return new StringWebResponse("", UrlUtils.URL_ABOUT_BLANK);
1393         }
1394 
1395         final String urlWithoutQuery = StringUtils.substringBefore(urlString, "?");
1396         if (!"blank".equalsIgnoreCase(StringUtils.substringAfter(urlWithoutQuery, UrlUtils.ABOUT_SCHEME))) {
1397             throw new MalformedURLException(url + " is not supported, only about:blank is supported at the moment.");
1398         }
1399         return new StringWebResponse("", url);
1400     }
1401 
1402     /**
1403      * Builds a WebResponse for a file URL.
1404      * This first implementation is basic.
1405      * It assumes that the file contains an HTML page encoded with the specified encoding.
1406      * @param webRequest the request
1407      * @return the web response
1408      * @throws IOException if an IO problem occurs
1409      */
1410     private WebResponse makeWebResponseForFileUrl(final WebRequest webRequest) throws IOException {
1411         URL cleanUrl = webRequest.getUrl();
1412         if (cleanUrl.getQuery() != null) {
1413             // Get rid of the query portion before trying to load the file.
1414             cleanUrl = UrlUtils.getUrlWithNewQuery(cleanUrl, null);
1415         }
1416         if (cleanUrl.getRef() != null) {
1417             // Get rid of the ref portion before trying to load the file.
1418             cleanUrl = UrlUtils.getUrlWithNewRef(cleanUrl, null);
1419         }
1420 
1421         final WebResponse fromCache = getCache().getCachedResponse(webRequest);
1422         if (fromCache != null) {
1423             return new WebResponseFromCache(fromCache, webRequest);
1424         }
1425 
1426         String fileUrl = cleanUrl.toExternalForm();
1427         fileUrl = URLDecoder.decode(fileUrl, UTF_8.name());
1428         final File file = new File(fileUrl.substring(5));
1429         if (!file.exists()) {
1430             // construct 404
1431             final List<NameValuePair> compiledHeaders = new ArrayList<>();
1432             compiledHeaders.add(new NameValuePair(HttpHeader.CONTENT_TYPE, MimeType.TEXT_HTML));
1433             final WebResponseData responseData =
1434                 new WebResponseData(
1435                         StringUtils
1436                             .toByteArray("File: " + file.getAbsolutePath(), UTF_8),
1437                     404, "Not Found", compiledHeaders);
1438             return new WebResponse(responseData, webRequest, 0);
1439         }
1440 
1441         final String contentType = guessContentType(file);
1442 
1443         final DownloadedContent content = new DownloadedContent.OnFile(file, false);
1444         final List<NameValuePair> compiledHeaders = new ArrayList<>();
1445         compiledHeaders.add(new NameValuePair(HttpHeader.CONTENT_TYPE, contentType));
1446         compiledHeaders.add(new NameValuePair(HttpHeader.LAST_MODIFIED,
1447                 HttpUtils.formatDate(new Date(file.lastModified()))));
1448         final WebResponseData responseData = new WebResponseData(content, 200, "OK", compiledHeaders);
1449         final WebResponse webResponse = new WebResponse(responseData, webRequest, 0);
1450         getCache().cacheIfPossible(webRequest, webResponse, null);
1451         return webResponse;
1452     }
1453 
1454     private WebResponse makeWebResponseForBlobUrl(final WebRequest webRequest) {
1455         final Window window = getCurrentWindow().getScriptableObject();
1456         final Blob fileOrBlob = window.getDocument().resolveBlobUrl(webRequest.getUrl().toString());
1457         if (fileOrBlob == null) {
1458             throw JavaScriptEngine.typeError("Cannot load data from " + webRequest.getUrl());
1459         }
1460 
1461         final List<NameValuePair> headers = new ArrayList<>();
1462         final String type = fileOrBlob.getType();
1463         if (!StringUtils.isEmptyOrNull(type)) {
1464             headers.add(new NameValuePair(HttpHeader.CONTENT_TYPE, fileOrBlob.getType()));
1465         }
1466         if (fileOrBlob instanceof org.htmlunit.javascript.host.file.File) {
1467             final org.htmlunit.javascript.host.file.File file = (org.htmlunit.javascript.host.file.File) fileOrBlob;
1468             final String fileName = file.getName();
1469             if (!StringUtils.isEmptyOrNull(fileName)) {
1470                 // https://datatracker.ietf.org/doc/html/rfc6266#autoid-10
1471                 headers.add(new NameValuePair(HttpHeader.CONTENT_DISPOSITION, "inline; filename=\"" + fileName + "\""));
1472             }
1473         }
1474 
1475         final DownloadedContent content = new DownloadedContent.InMemory(fileOrBlob.getBytes());
1476         final WebResponseData responseData = new WebResponseData(content, 200, "OK", headers);
1477         return new WebResponse(responseData, webRequest, 0);
1478     }
1479 
1480     /**
1481      * Tries to guess the content type of the file.<br>
1482      * This utility could be located in a helper class but we can compare this functionality
1483      * for instance with the "Helper Applications" settings of Mozilla and therefore see it as a
1484      * property of the "browser".
1485      * @param file the file
1486      * @return "application/octet-stream" if nothing could be guessed
1487      */
1488     public String guessContentType(final File file) {
1489         final String fileName = file.getName();
1490         final String fileNameLC = fileName.toLowerCase(Locale.ROOT);
1491         if (fileNameLC.endsWith(".xhtml")) {
1492             // Java's mime type map returns application/xml in JDK8.
1493             return MimeType.APPLICATION_XHTML;
1494         }
1495 
1496         // Java's mime type map does not know these in JDK8.
1497         if (fileNameLC.endsWith(".js")) {
1498             return MimeType.TEXT_JAVASCRIPT;
1499         }
1500 
1501         if (fileNameLC.endsWith(".css")) {
1502             return MimeType.TEXT_CSS;
1503         }
1504 
1505         String contentType = null;
1506         if (!fileNameLC.endsWith(".php")) {
1507             contentType = URLConnection.guessContentTypeFromName(fileName);
1508         }
1509         if (contentType == null) {
1510             try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
1511                 contentType = URLConnection.guessContentTypeFromStream(inputStream);
1512             }
1513             catch (final IOException ignored) {
1514                 // Ignore silently.
1515             }
1516         }
1517         if (contentType == null) {
1518             contentType = MimeType.APPLICATION_OCTET_STREAM;
1519         }
1520         return contentType;
1521     }
1522 
1523     private WebResponse makeWebResponseForJavaScriptUrl(final WebWindow webWindow, final URL url,
1524         final Charset charset) throws FailingHttpStatusCodeException, IOException {
1525 
1526         HtmlPage page = null;
1527         if (webWindow instanceof FrameWindow) {
1528             final FrameWindow frameWindow = (FrameWindow) webWindow;
1529             page = (HtmlPage) frameWindow.getEnclosedPage();
1530         }
1531         else {
1532             final Page currentPage = webWindow.getEnclosedPage();
1533             if (currentPage instanceof HtmlPage) {
1534                 page = (HtmlPage) currentPage;
1535             }
1536         }
1537 
1538         if (page == null) {
1539             page = getPage(webWindow, WebRequest.newAboutBlankRequest());
1540         }
1541         final ScriptResult r = page.executeJavaScript(url.toExternalForm(), "JavaScript URL", 1);
1542         if (r.getJavaScriptResult() == null || ScriptResult.isUndefined(r)) {
1543             // No new WebResponse to produce.
1544             return webWindow.getEnclosedPage().getWebResponse();
1545         }
1546 
1547         final String contentString = r.getJavaScriptResult().toString();
1548         final StringWebResponse response = new StringWebResponse(contentString, charset, url);
1549         response.setFromJavascript(true);
1550         return response;
1551     }
1552 
1553     /**
1554      * Loads a {@link WebResponse} from the server.
1555      * @param webRequest the request
1556      * @throws IOException if an IO problem occurs
1557      * @return the WebResponse
1558      */
1559     public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException {
1560         final String protocol = webRequest.getUrl().getProtocol();
1561         switch (protocol) {
1562             case UrlUtils.ABOUT:
1563                 return makeWebResponseForAboutUrl(webRequest);
1564 
1565             case "file":
1566                 return makeWebResponseForFileUrl(webRequest);
1567 
1568             case "data":
1569                 return makeWebResponseForDataUrl(webRequest);
1570 
1571             case "blob":
1572                 return makeWebResponseForBlobUrl(webRequest);
1573 
1574             case "http":
1575             case "https":
1576                 return loadWebResponseFromWebConnection(webRequest, ALLOWED_REDIRECTIONS_SAME_URL);
1577 
1578             default:
1579                 throw new IOException("Unsupported protocol '" + protocol + "'");
1580         }
1581     }
1582 
1583     /**
1584      * Loads a {@link WebResponse} from the server through the WebConnection.
1585      * @param webRequest the request
1586      * @param allowedRedirects the number of allowed redirects remaining
1587      * @throws IOException if an IO problem occurs
1588      * @return the resultant {@link WebResponse}
1589      */
1590     private WebResponse loadWebResponseFromWebConnection(final WebRequest webRequest,
1591         final int allowedRedirects) throws IOException {
1592 
1593         URL url = webRequest.getUrl();
1594         final HttpMethod method = webRequest.getHttpMethod();
1595         final List<NameValuePair> parameters = webRequest.getRequestParameters();
1596 
1597         WebAssert.notNull("url", url);
1598         WebAssert.notNull("method", method);
1599         WebAssert.notNull("parameters", parameters);
1600 
1601         url = UrlUtils.encodeUrl(url, webRequest.getCharset());
1602         webRequest.setUrl(url);
1603 
1604         if (LOG.isDebugEnabled()) {
1605             LOG.debug("Load response for " + method + " " + url.toExternalForm());
1606         }
1607 
1608         // If the request settings don't specify a custom proxy, use the default client proxy...
1609         if (webRequest.getProxyHost() == null) {
1610             final ProxyConfig proxyConfig = getOptions().getProxyConfig();
1611             if (proxyConfig.getProxyAutoConfigUrl() != null) {
1612                 if (!UrlUtils.sameFile(new URL(proxyConfig.getProxyAutoConfigUrl()), url)) {
1613                     String content = proxyConfig.getProxyAutoConfigContent();
1614                     if (content == null) {
1615                         content = getPage(proxyConfig.getProxyAutoConfigUrl())
1616                             .getWebResponse().getContentAsString();
1617                         proxyConfig.setProxyAutoConfigContent(content);
1618                     }
1619                     final String allValue = JavaScriptEngine.evaluateProxyAutoConfig(getBrowserVersion(), content, url);
1620                     if (LOG.isDebugEnabled()) {
1621                         LOG.debug("Proxy Auto-Config: value '" + allValue + "' for URL " + url);
1622                     }
1623                     String value = allValue.split(";")[0].trim();
1624                     if (value.startsWith("PROXY")) {
1625                         value = value.substring(6);
1626                         final int colonIndex = value.indexOf(':');
1627                         webRequest.setSocksProxy(false);
1628                         webRequest.setProxyHost(value.substring(0, colonIndex));
1629                         webRequest.setProxyPort(Integer.parseInt(value.substring(colonIndex + 1)));
1630                     }
1631                     else if (value.startsWith("SOCKS")) {
1632                         value = value.substring(6);
1633                         final int colonIndex = value.indexOf(':');
1634                         webRequest.setSocksProxy(true);
1635                         webRequest.setProxyHost(value.substring(0, colonIndex));
1636                         webRequest.setProxyPort(Integer.parseInt(value.substring(colonIndex + 1)));
1637                     }
1638                 }
1639             }
1640             // ...unless the host needs to bypass the configured client proxy!
1641             else if (!proxyConfig.shouldBypassProxy(webRequest.getUrl().getHost())) {
1642                 webRequest.setProxyHost(proxyConfig.getProxyHost());
1643                 webRequest.setProxyPort(proxyConfig.getProxyPort());
1644                 webRequest.setProxyScheme(proxyConfig.getProxyScheme());
1645                 webRequest.setSocksProxy(proxyConfig.isSocksProxy());
1646             }
1647         }
1648 
1649         // Add the headers that are sent with every request.
1650         addDefaultHeaders(webRequest);
1651 
1652         // Retrieve the response, either from the cache or from the server.
1653         final WebResponse fromCache = getCache().getCachedResponse(webRequest);
1654         final WebResponse webResponse = getWebResponseOrUseCached(webRequest, fromCache);
1655 
1656         // Continue according to the HTTP status code.
1657         final int status = webResponse.getStatusCode();
1658         if (status == HttpStatus.USE_PROXY_305) {
1659             getIncorrectnessListener().notify("Ignoring HTTP status code [305] 'Use Proxy'", this);
1660         }
1661         else if (status >= HttpStatus.MOVED_PERMANENTLY_301
1662             && status <= HttpStatus.PERMANENT_REDIRECT_308
1663             && status != HttpStatus.NOT_MODIFIED_304
1664             && getOptions().isRedirectEnabled()) {
1665 
1666             final URL newUrl;
1667             String locationString = null;
1668             try {
1669                 locationString = webResponse.getResponseHeaderValue("Location");
1670                 if (locationString == null) {
1671                     return webResponse;
1672                 }
1673                 locationString = new String(locationString.getBytes(ISO_8859_1), UTF_8);
1674                 newUrl = expandUrl(url, locationString);
1675             }
1676             catch (final MalformedURLException e) {
1677                 getIncorrectnessListener().notify("Got a redirect status code [" + status + " "
1678                     + webResponse.getStatusMessage()
1679                     + "] but the location is not a valid URL [" + locationString
1680                     + "]. Skipping redirection processing.", this);
1681                 return webResponse;
1682             }
1683 
1684             if (LOG.isDebugEnabled()) {
1685                 LOG.debug("Got a redirect status code [" + status + "] new location = [" + locationString + "]");
1686             }
1687 
1688             if (allowedRedirects == 0) {
1689                 throw new FailingHttpStatusCodeException("Too many redirects for "
1690                     + webResponse.getWebRequest().getUrl(), webResponse);
1691             }
1692 
1693             if (status == HttpStatus.MOVED_PERMANENTLY_301
1694                     || status == HttpStatus.FOUND_302
1695                     || status == HttpStatus.SEE_OTHER_303) {
1696                 final WebRequest wrs = new WebRequest(newUrl, HttpMethod.GET);
1697                 wrs.setCharset(webRequest.getCharset());
1698 
1699                 if (HttpMethod.HEAD == webRequest.getHttpMethod()) {
1700                     wrs.setHttpMethod(HttpMethod.HEAD);
1701                 }
1702                 for (final Map.Entry<String, String> entry : webRequest.getAdditionalHeaders().entrySet()) {
1703                     wrs.setAdditionalHeader(entry.getKey(), entry.getValue());
1704                 }
1705                 return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1);
1706             }
1707             else if (status == HttpStatus.TEMPORARY_REDIRECT_307
1708                         || status == HttpStatus.PERMANENT_REDIRECT_308) {
1709                 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
1710                 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
1711                 // reuse method and body
1712                 final WebRequest wrs = new WebRequest(newUrl, webRequest.getHttpMethod());
1713                 wrs.setCharset(webRequest.getCharset());
1714                 if (webRequest.getRequestBody() != null) {
1715                     if (HttpMethod.POST == webRequest.getHttpMethod()
1716                             || HttpMethod.PUT == webRequest.getHttpMethod()
1717                             || HttpMethod.PATCH == webRequest.getHttpMethod()) {
1718                         wrs.setRequestBody(webRequest.getRequestBody());
1719                         wrs.setEncodingType(webRequest.getEncodingType());
1720                     }
1721                 }
1722                 else {
1723                     wrs.setRequestParameters(parameters);
1724                 }
1725 
1726                 for (final Map.Entry<String, String> entry : webRequest.getAdditionalHeaders().entrySet()) {
1727                     wrs.setAdditionalHeader(entry.getKey(), entry.getValue());
1728                 }
1729 
1730                 return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1);
1731             }
1732         }
1733 
1734         if (fromCache == null) {
1735             getCache().cacheIfPossible(webRequest, webResponse, null);
1736         }
1737         return webResponse;
1738     }
1739 
1740     /**
1741      * Returns the cached response provided for the request if usable otherwise makes the
1742      * request and returns the response.
1743      * @param webRequest the request
1744      * @param cached a previous cached response for the request, or {@code null}
1745      */
1746     private WebResponse getWebResponseOrUseCached(
1747             final WebRequest webRequest, final WebResponse cached) throws IOException {
1748         if (cached == null) {
1749             return getWebConnection().getResponse(webRequest);
1750         }
1751 
1752         if (!HeaderUtils.containsNoCache(cached)) {
1753             return new WebResponseFromCache(cached, webRequest);
1754         }
1755 
1756         // implementation based on rfc9111 https://www.rfc-editor.org/rfc/rfc9111#name-validation
1757         if (HeaderUtils.containsETag(cached)) {
1758             webRequest.setAdditionalHeader(HttpHeader.IF_NONE_MATCH, cached.getResponseHeaderValue(HttpHeader.ETAG));
1759         }
1760         if (HeaderUtils.containsLastModified(cached)) {
1761             webRequest.setAdditionalHeader(HttpHeader.IF_MODIFIED_SINCE,
1762                     cached.getResponseHeaderValue(HttpHeader.LAST_MODIFIED));
1763         }
1764 
1765         final WebResponse webResponse = getWebConnection().getResponse(webRequest);
1766 
1767         if (webResponse.getStatusCode() >= HttpStatus.INTERNAL_SERVER_ERROR_500) {
1768             return new WebResponseFromCache(cached, webRequest);
1769         }
1770 
1771         if (webResponse.getStatusCode() == HttpStatus.NOT_MODIFIED_304) {
1772             final Map<String, NameValuePair> header2NameValuePair = new LinkedHashMap<>();
1773             for (final NameValuePair pair : cached.getResponseHeaders()) {
1774                 header2NameValuePair.put(pair.getName(), pair);
1775             }
1776             for (final NameValuePair pair : webResponse.getResponseHeaders()) {
1777                 if (preferHeaderFrom304Response(pair.getName())) {
1778                     header2NameValuePair.put(pair.getName(), pair);
1779                 }
1780             }
1781             // WebResponse headers is unmodifiableList so we cannot update it directly
1782             // instead, create a new WebResponseFromCache with updated headers
1783             // then use it to replace the old cached value
1784             final WebResponse updatedCached =
1785                     new WebResponseFromCache(cached, new ArrayList<>(header2NameValuePair.values()), webRequest);
1786             getCache().cacheIfPossible(webRequest, updatedCached, null);
1787             return updatedCached;
1788         }
1789 
1790         getCache().cacheIfPossible(webRequest, webResponse, null);
1791         return webResponse;
1792     }
1793 
1794     /**
1795      * Returns true if the value of the specified header in a 304 Not Modified response should be
1796      * adopted over any previously cached value.
1797      */
1798     private static boolean preferHeaderFrom304Response(final String name) {
1799         final String lcName = name.toLowerCase(Locale.ROOT);
1800         for (final String header : DISCARDING_304_RESPONSE_HEADER_NAMES) {
1801             if (lcName.equals(header)) {
1802                 return false;
1803             }
1804         }
1805         for (final String prefix : DISCARDING_304_HEADER_PREFIXES) {
1806             if (lcName.startsWith(prefix)) {
1807                 return false;
1808             }
1809         }
1810         return true;
1811     }
1812 
1813     /**
1814      * Adds the headers that are sent with every request to the specified {@link WebRequest} instance.
1815      * @param wrs the <code>WebRequestSettings</code> instance to modify
1816      */
1817     private void addDefaultHeaders(final WebRequest wrs) {
1818         // Add user-specified headers to the web request if not present there yet.
1819         requestHeaders_.forEach((name, value) -> {
1820             if (!wrs.isAdditionalHeader(name)) {
1821                 wrs.setAdditionalHeader(name, value);
1822             }
1823         });
1824 
1825         // Add standard HtmlUnit headers to the web request if still not present there yet.
1826         if (!wrs.isAdditionalHeader(HttpHeader.ACCEPT_LANGUAGE)) {
1827             wrs.setAdditionalHeader(HttpHeader.ACCEPT_LANGUAGE, getBrowserVersion().getAcceptLanguageHeader());
1828         }
1829 
1830         if (!wrs.isAdditionalHeader(HttpHeader.SEC_FETCH_DEST)) {
1831             wrs.setAdditionalHeader(HttpHeader.SEC_FETCH_DEST, "document");
1832         }
1833         if (!wrs.isAdditionalHeader(HttpHeader.SEC_FETCH_MODE)) {
1834             wrs.setAdditionalHeader(HttpHeader.SEC_FETCH_MODE, "navigate");
1835         }
1836         if (!wrs.isAdditionalHeader(HttpHeader.SEC_FETCH_SITE)) {
1837             wrs.setAdditionalHeader(HttpHeader.SEC_FETCH_SITE, "same-origin");
1838         }
1839         if (!wrs.isAdditionalHeader(HttpHeader.SEC_FETCH_USER)) {
1840             wrs.setAdditionalHeader(HttpHeader.SEC_FETCH_USER, "?1");
1841         }
1842         if (getBrowserVersion().hasFeature(HTTP_HEADER_PRIORITY)
1843                 && !wrs.isAdditionalHeader(HttpHeader.PRIORITY)) {
1844             wrs.setAdditionalHeader(HttpHeader.PRIORITY, "u=0, i");
1845         }
1846 
1847         if (getBrowserVersion().hasFeature(HTTP_HEADER_CH_UA)
1848                 && !wrs.isAdditionalHeader(HttpHeader.SEC_CH_UA)) {
1849             wrs.setAdditionalHeader(HttpHeader.SEC_CH_UA, getBrowserVersion().getSecClientHintUserAgentHeader());
1850         }
1851         if (getBrowserVersion().hasFeature(HTTP_HEADER_CH_UA)
1852                 && !wrs.isAdditionalHeader(HttpHeader.SEC_CH_UA_MOBILE)) {
1853             wrs.setAdditionalHeader(HttpHeader.SEC_CH_UA_MOBILE, "?0");
1854         }
1855         if (getBrowserVersion().hasFeature(HTTP_HEADER_CH_UA)
1856                 && !wrs.isAdditionalHeader(HttpHeader.SEC_CH_UA_PLATFORM)) {
1857             wrs.setAdditionalHeader(HttpHeader.SEC_CH_UA_PLATFORM,
1858                     getBrowserVersion().getSecClientHintUserAgentPlatformHeader());
1859         }
1860 
1861         if (!wrs.isAdditionalHeader(HttpHeader.UPGRADE_INSECURE_REQUESTS)) {
1862             wrs.setAdditionalHeader(HttpHeader.UPGRADE_INSECURE_REQUESTS, "1");
1863         }
1864     }
1865 
1866     /**
1867      * Returns an immutable list of open web windows (whether they are top level windows or not).
1868      * This is a snapshot; future changes are not reflected by this list.
1869      * <p>
1870      * The list is ordered by age, the oldest one first.
1871      *
1872      * @return an immutable list of open web windows (whether they are top level windows or not)
1873      * @see #getWebWindowByName(String)
1874      * @see #getTopLevelWindows()
1875      */
1876     public List<WebWindow> getWebWindows() {
1877         return Collections.unmodifiableList(new ArrayList<>(windows_));
1878     }
1879 
1880     /**
1881      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1882      *
1883      * Returns true if the list of WebWindows contains the provided one.
1884      * This method is there to improve the performance of some internal checks because
1885      * calling getWebWindows().contains(.) creates some objects without any need.
1886      *
1887      * @param webWindow the window to check
1888      * @return true or false
1889      */
1890     public boolean containsWebWindow(final WebWindow webWindow) {
1891         return windows_.contains(webWindow);
1892     }
1893 
1894     /**
1895      * Returns an immutable list of open top level windows.
1896      * This is a snapshot; future changes are not reflected by this list.
1897      * <p>
1898      * The list is ordered by age, the oldest one first.
1899      *
1900      * @return an immutable list of open top level windows
1901      * @see #getWebWindowByName(String)
1902      * @see #getWebWindows()
1903      */
1904     public List<TopLevelWindow> getTopLevelWindows() {
1905         return Collections.unmodifiableList(new ArrayList<>(topLevelWindows_));
1906     }
1907 
1908     /**
1909      * Sets the handler to be used whenever a refresh is triggered. Refer
1910      * to the documentation for {@link RefreshHandler} for more details.
1911      * @param handler the new handler
1912      */
1913     public void setRefreshHandler(final RefreshHandler handler) {
1914         if (handler == null) {
1915             refreshHandler_ = new NiceRefreshHandler(2);
1916         }
1917         else {
1918             refreshHandler_ = handler;
1919         }
1920     }
1921 
1922     /**
1923      * Returns the current refresh handler.
1924      * The default refresh handler is a {@link NiceRefreshHandler NiceRefreshHandler(2)}.
1925      * @return the current RefreshHandler
1926      */
1927     public RefreshHandler getRefreshHandler() {
1928         return refreshHandler_;
1929     }
1930 
1931     /**
1932      * Sets the script pre processor for this {@link WebClient}.
1933      * @param scriptPreProcessor the new preprocessor or null if none is specified
1934      */
1935     public void setScriptPreProcessor(final ScriptPreProcessor scriptPreProcessor) {
1936         scriptPreProcessor_ = scriptPreProcessor;
1937     }
1938 
1939     /**
1940      * Returns the script pre processor for this {@link WebClient}.
1941      * @return the pre processor or null of one hasn't been set
1942      */
1943     public ScriptPreProcessor getScriptPreProcessor() {
1944         return scriptPreProcessor_;
1945     }
1946 
1947     /**
1948      * Sets the listener for messages generated by the HTML parser.
1949      * @param listener the new listener, {@code null} if messages should be totally ignored
1950      */
1951     public void setHTMLParserListener(final HTMLParserListener listener) {
1952         htmlParserListener_ = listener;
1953     }
1954 
1955     /**
1956      * Gets the configured listener for messages generated by the HTML parser.
1957      * @return {@code null} if no listener is defined (default value)
1958      */
1959     public HTMLParserListener getHTMLParserListener() {
1960         return htmlParserListener_;
1961     }
1962 
1963     /**
1964      * Returns the CSS error handler used by this web client when CSS problems are encountered.
1965      * @return the CSS error handler used by this web client when CSS problems are encountered
1966      * @see DefaultCssErrorHandler
1967      * @see SilentCssErrorHandler
1968      */
1969     public CSSErrorHandler getCssErrorHandler() {
1970         return cssErrorHandler_;
1971     }
1972 
1973     /**
1974      * Sets the CSS error handler used by this web client when CSS problems are encountered.
1975      * @param cssErrorHandler the CSS error handler used by this web client when CSS problems are encountered
1976      * @see DefaultCssErrorHandler
1977      * @see SilentCssErrorHandler
1978      */
1979     public void setCssErrorHandler(final CSSErrorHandler cssErrorHandler) {
1980         WebAssert.notNull("cssErrorHandler", cssErrorHandler);
1981         cssErrorHandler_ = cssErrorHandler;
1982     }
1983 
1984     /**
1985      * Sets the number of milliseconds that a script is allowed to execute before being terminated.
1986      * A value of 0 or less means no timeout.
1987      *
1988      * @param timeout the timeout value, in milliseconds
1989      */
1990     public void setJavaScriptTimeout(final long timeout) {
1991         scriptEngine_.setJavaScriptTimeout(timeout);
1992     }
1993 
1994     /**
1995      * Returns the number of milliseconds that a script is allowed to execute before being terminated.
1996      * A value of 0 or less means no timeout.
1997      *
1998      * @return the timeout value, in milliseconds
1999      */
2000     public long getJavaScriptTimeout() {
2001         return scriptEngine_.getJavaScriptTimeout();
2002     }
2003 
2004     /**
2005      * Gets the current listener for encountered incorrectness (except HTML parsing messages that
2006      * are handled by the HTML parser listener). Default value is an instance of
2007      * {@link IncorrectnessListenerImpl}.
2008      * @return the current listener (not {@code null})
2009      */
2010     public IncorrectnessListener getIncorrectnessListener() {
2011         return incorrectnessListener_;
2012     }
2013 
2014     /**
2015      * Returns the current HTML incorrectness listener.
2016      * @param listener the new value (not {@code null})
2017      */
2018     public void setIncorrectnessListener(final IncorrectnessListener listener) {
2019         if (listener == null) {
2020             throw new IllegalArgumentException("Null is not a valid IncorrectnessListener");
2021         }
2022         incorrectnessListener_ = listener;
2023     }
2024 
2025     /**
2026      * Returns the WebConsole.
2027      * @return the web console
2028      */
2029     public WebConsole getWebConsole() {
2030         if (webConsole_ == null) {
2031             webConsole_ = new WebConsole();
2032         }
2033         return webConsole_;
2034     }
2035 
2036     /**
2037      * Gets the current AJAX controller.
2038      * @return the controller
2039      */
2040     public AjaxController getAjaxController() {
2041         return ajaxController_;
2042     }
2043 
2044     /**
2045      * Sets the current AJAX controller.
2046      * @param newValue the controller
2047      */
2048     public void setAjaxController(final AjaxController newValue) {
2049         if (newValue == null) {
2050             throw new IllegalArgumentException("Null is not a valid AjaxController");
2051         }
2052         ajaxController_ = newValue;
2053     }
2054 
2055     /**
2056      * Sets the attachment handler.
2057      * @param handler the new attachment handler
2058      */
2059     public void setAttachmentHandler(final AttachmentHandler handler) {
2060         attachmentHandler_ = handler;
2061     }
2062 
2063     /**
2064      * Returns the current attachment handler.
2065      * @return the current attachment handler
2066      */
2067     public AttachmentHandler getAttachmentHandler() {
2068         return attachmentHandler_;
2069     }
2070 
2071     /**
2072      * Sets the WebStart handler.
2073      * @param handler the new WebStart handler
2074      */
2075     public void setWebStartHandler(final WebStartHandler handler) {
2076         webStartHandler_ = handler;
2077     }
2078 
2079     /**
2080      * Returns the current WebStart handler.
2081      * @return the current WebStart handler
2082      */
2083     public WebStartHandler getWebStartHandler() {
2084         return webStartHandler_;
2085     }
2086 
2087     /**
2088      * Returns the current clipboard handler.
2089      * @return the current clipboard handler
2090      */
2091     public ClipboardHandler getClipboardHandler() {
2092         return clipboardHandler_;
2093     }
2094 
2095     /**
2096      * Sets the clipboard handler.
2097      * @param handler the new clipboard handler
2098      */
2099     public void setClipboardHandler(final ClipboardHandler handler) {
2100         clipboardHandler_ = handler;
2101     }
2102 
2103     /**
2104      * Returns the current {@link PrintHandler}.
2105      * @return the current {@link PrintHandler} or null if print
2106      *         requests are ignored
2107      */
2108     public PrintHandler getPrintHandler() {
2109         return printHandler_;
2110     }
2111 
2112     /**
2113      * Sets the {@link PrintHandler} to be used if Windoe.print() is called
2114      * (<a href="https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#printing">Printing Spec</a>).
2115      *
2116      * @param handler the new {@link PrintHandler} or null if you like to
2117      *        ignore print requests (default is null)
2118      */
2119     public void setPrintHandler(final PrintHandler handler) {
2120         printHandler_ = handler;
2121     }
2122 
2123     /**
2124      * Returns the current FrameContent handler.
2125      * @return the current FrameContent handler
2126      */
2127     public FrameContentHandler getFrameContentHandler() {
2128         return frameContentHandler_;
2129     }
2130 
2131     /**
2132      * Sets the FrameContent handler.
2133      * @param handler the new FrameContent handler
2134      */
2135     public void setFrameContentHandler(final FrameContentHandler handler) {
2136         frameContentHandler_ = handler;
2137     }
2138 
2139     /**
2140      * Sets the onbeforeunload handler for this {@link WebClient}.
2141      * @param onbeforeunloadHandler the new onbeforeunloadHandler or null if none is specified
2142      */
2143     public void setOnbeforeunloadHandler(final OnbeforeunloadHandler onbeforeunloadHandler) {
2144         onbeforeunloadHandler_ = onbeforeunloadHandler;
2145     }
2146 
2147     /**
2148      * Returns the onbeforeunload handler for this {@link WebClient}.
2149      * @return the onbeforeunload handler or null if one hasn't been set
2150      */
2151     public OnbeforeunloadHandler getOnbeforeunloadHandler() {
2152         return onbeforeunloadHandler_;
2153     }
2154 
2155     /**
2156      * Gets the cache currently being used.
2157      * @return the cache (may not be null)
2158      */
2159     public Cache getCache() {
2160         return cache_;
2161     }
2162 
2163     /**
2164      * Sets the cache to use.
2165      * @param cache the new cache (must not be {@code null})
2166      */
2167     public void setCache(final Cache cache) {
2168         if (cache == null) {
2169             throw new IllegalArgumentException("cache should not be null!");
2170         }
2171         cache_ = cache;
2172     }
2173 
2174     /**
2175      * Keeps track of the current window. Inspired by WebTest's logic to track the current response.
2176      */
2177     private static final class CurrentWindowTracker implements WebWindowListener, Serializable {
2178         private final WebClient webClient_;
2179         private final boolean ensureOneTopLevelWindow_;
2180 
2181         CurrentWindowTracker(final WebClient webClient, final boolean ensureOneTopLevelWindow) {
2182             webClient_ = webClient;
2183             ensureOneTopLevelWindow_ = ensureOneTopLevelWindow;
2184         }
2185 
2186         /**
2187          * {@inheritDoc}
2188          */
2189         @Override
2190         public void webWindowClosed(final WebWindowEvent event) {
2191             final WebWindow window = event.getWebWindow();
2192             if (window instanceof TopLevelWindow) {
2193                 webClient_.topLevelWindows_.remove(window);
2194                 if (window == webClient_.getCurrentWindow()) {
2195                     if (!webClient_.topLevelWindows_.isEmpty()) {
2196                         // The current window is now the previous top-level window.
2197                         webClient_.setCurrentWindow(
2198                                 webClient_.topLevelWindows_.get(webClient_.topLevelWindows_.size() - 1));
2199                     }
2200                 }
2201             }
2202             else if (window == webClient_.getCurrentWindow()) {
2203                 // The current window is now the last top-level window.
2204                 if (webClient_.topLevelWindows_.isEmpty()) {
2205                     webClient_.setCurrentWindow(null);
2206                 }
2207                 else {
2208                     webClient_.setCurrentWindow(
2209                             webClient_.topLevelWindows_.get(webClient_.topLevelWindows_.size() - 1));
2210                 }
2211             }
2212         }
2213 
2214         /**
2215          * Postprocessing to make sure we have always one top level window open.
2216          */
2217         public void afterWebWindowClosedListenersProcessed(final WebWindowEvent event) {
2218             if (!ensureOneTopLevelWindow_) {
2219                 return;
2220             }
2221 
2222             if (webClient_.topLevelWindows_.isEmpty()) {
2223                 // Must always have at least window, and there are no top-level windows left; must create one.
2224                 final TopLevelWindow newWindow = new TopLevelWindow("", webClient_);
2225                 webClient_.setCurrentWindow(newWindow);
2226             }
2227         }
2228 
2229         /**
2230          * {@inheritDoc}
2231          */
2232         @Override
2233         public void webWindowContentChanged(final WebWindowEvent event) {
2234             final WebWindow window = event.getWebWindow();
2235             boolean use = false;
2236             if (window instanceof DialogWindow) {
2237                 use = true;
2238             }
2239             else if (window instanceof TopLevelWindow) {
2240                 use = event.getOldPage() == null;
2241             }
2242             else if (window instanceof FrameWindow) {
2243                 final FrameWindow fw = (FrameWindow) window;
2244                 final String enclosingPageState = fw.getEnclosingPage().getDocumentElement().getReadyState();
2245                 final URL frameUrl = fw.getEnclosedPage().getUrl();
2246                 if (!DomNode.READY_STATE_COMPLETE.equals(enclosingPageState) || frameUrl == UrlUtils.URL_ABOUT_BLANK) {
2247                     return;
2248                 }
2249 
2250                 // now looks at the visibility of the frame window
2251                 final BaseFrameElement frameElement = fw.getFrameElement();
2252                 if (webClient_.isJavaScriptEnabled() && frameElement.isDisplayed()) {
2253                     final ComputedCssStyleDeclaration style = fw.getComputedStyle(frameElement, null);
2254                     use = style.getCalculatedWidth(false, false) != 0
2255                             && style.getCalculatedHeight(false, false) != 0;
2256                 }
2257             }
2258             if (use) {
2259                 webClient_.setCurrentWindow(window);
2260             }
2261         }
2262 
2263         /**
2264          * {@inheritDoc}
2265          */
2266         @Override
2267         public void webWindowOpened(final WebWindowEvent event) {
2268             final WebWindow window = event.getWebWindow();
2269             if (window instanceof TopLevelWindow) {
2270                 final TopLevelWindow tlw = (TopLevelWindow) window;
2271                 webClient_.topLevelWindows_.add(tlw);
2272             }
2273             // Page is not loaded yet, don't set it now as current window.
2274         }
2275     }
2276 
2277     /**
2278      * Closes all opened windows, stopping all background JavaScript processing.
2279      * The WebClient is not really usable after this - you have to create a new one or
2280      * use WebClient.reset() instead.
2281      * <p>
2282      * {@inheritDoc}
2283      */
2284     @Override
2285     public void close() {
2286         // avoid attaching new windows to the js engine
2287         if (scriptEngine_ != null) {
2288             scriptEngine_.prepareShutdown();
2289         }
2290 
2291         // stop the CurrentWindowTracker from making sure there is still one window available
2292         currentWindowTracker_ = new CurrentWindowTracker(this, false);
2293 
2294         // Hint: a new TopLevelWindow may be opened by some JS script while we are closing the others
2295         // but the prepareShutdown() call will prevent the new window form getting js support
2296         List<WebWindow> windows = new ArrayList<>(windows_);
2297         for (final WebWindow window : windows) {
2298             if (window instanceof TopLevelWindow) {
2299                 final TopLevelWindow topLevelWindow = (TopLevelWindow) window;
2300 
2301                 try {
2302                     topLevelWindow.close(true);
2303                 }
2304                 catch (final Exception e) {
2305                     LOG.error("Exception while closing a TopLevelWindow", e);
2306                 }
2307             }
2308             else if (window instanceof DialogWindow) {
2309                 final DialogWindow dialogWindow = (DialogWindow) window;
2310 
2311                 try {
2312                     dialogWindow.close();
2313                 }
2314                 catch (final Exception e) {
2315                     LOG.error("Exception while closing a DialogWindow", e);
2316                 }
2317             }
2318         }
2319 
2320         // second round, none of the remaining windows should be registered to
2321         // the js engine because of prepareShutdown()
2322         windows = new ArrayList<>(windows_);
2323         for (final WebWindow window : windows) {
2324             if (window instanceof TopLevelWindow) {
2325                 final TopLevelWindow topLevelWindow = (TopLevelWindow) window;
2326 
2327                 try {
2328                     topLevelWindow.close(true);
2329                 }
2330                 catch (final Exception e) {
2331                     LOG.error("Exception while closing a TopLevelWindow", e);
2332                 }
2333             }
2334             else if (window instanceof DialogWindow) {
2335                 final DialogWindow dialogWindow = (DialogWindow) window;
2336 
2337                 try {
2338                     dialogWindow.close();
2339                 }
2340                 catch (final Exception e) {
2341                     LOG.error("Exception while closing a DialogWindow", e);
2342                 }
2343             }
2344         }
2345 
2346         // now both lists have to be empty
2347         if (!topLevelWindows_.isEmpty()) {
2348             LOG.error("Sill " + topLevelWindows_.size() + " top level windows are open. Please report this error!");
2349             topLevelWindows_.clear();
2350         }
2351 
2352         if (!windows_.isEmpty()) {
2353             LOG.error("Sill " + windows_.size() + " windows are open. Please report this error!");
2354             windows_.clear();
2355         }
2356         currentWindow_ = null;
2357 
2358         ThreadDeath toThrow = null;
2359         if (scriptEngine_ != null) {
2360             try {
2361                 scriptEngine_.shutdown();
2362             }
2363             catch (final ThreadDeath ex) {
2364                 // make sure the following cleanup is performed to avoid resource leaks
2365                 toThrow = ex;
2366             }
2367             catch (final Exception e) {
2368                 LOG.error("Exception while shutdown the scriptEngine", e);
2369             }
2370         }
2371         scriptEngine_ = null;
2372 
2373         if (webConnection_ != null) {
2374             try {
2375                 webConnection_.close();
2376             }
2377             catch (final Exception e) {
2378                 LOG.error("Exception while closing the connection", e);
2379             }
2380         }
2381         webConnection_ = null;
2382 
2383         synchronized (this) {
2384             if (executor_ != null) {
2385                 try {
2386                     executor_.shutdownNow();
2387                 }
2388                 catch (final Exception e) {
2389                     LOG.error("Exception while shutdown the executor service", e);
2390                 }
2391             }
2392         }
2393         executor_ = null;
2394 
2395         cache_.clear();
2396         if (toThrow != null) {
2397             throw toThrow;
2398         }
2399     }
2400 
2401     /**
2402      * <p><span style="color:red">Experimental API: May be changed in next release
2403      * and may not yet work perfectly!</span></p>
2404      *
2405      * <p>This shuts down the whole client and restarts with a new empty window.
2406      * Cookies and other states are preserved.
2407      */
2408     public void reset() {
2409         close();
2410 
2411         // this has to be done after the browser version was set
2412         webConnection_ = new HttpWebConnection(this);
2413         if (javaScriptEngineEnabled_) {
2414             scriptEngine_ = new JavaScriptEngine(this);
2415         }
2416 
2417         // The window must be constructed AFTER the script engine.
2418         currentWindowTracker_ = new CurrentWindowTracker(this, true);
2419         currentWindow_ = new TopLevelWindow("", this);
2420     }
2421 
2422     /**
2423      * <p>Blocks until all background JavaScript tasks have finished executing or until the specified
2424      * timeout is reached, whichever occurs first. Background JavaScript tasks include:</p>
2425      * <ul>
2426      *   <li>JavaScript scheduled via <code>window.setTimeout()</code></li>
2427      *   <li>JavaScript scheduled via <code>window.setInterval()</code></li>
2428      *   <li>Asynchronous <code>XMLHttpRequest</code> operations</li>
2429      *   <li>Other asynchronous JavaScript operations across all windows managed by this WebClient</li>
2430      * </ul>
2431      *
2432      * <p><strong>Timeout Behavior:</strong> If background tasks are scheduled to execute after
2433      * <code>(now + timeoutMillis)</code>, this method will wait for the full timeout duration
2434      * and then return the number of remaining jobs. The method guarantees it will never block
2435      * longer than the specified timeout.</p>
2436      *
2437      * <p><strong>Use Case:</strong> Use this method when you don't know the exact timing of when
2438      * background JavaScript will start, but you have a reasonable estimate of how long all
2439      * tasks should take to complete. For scenarios where you know when tasks should start
2440      * executing, consider using {@link #waitForBackgroundJavaScriptStartingBefore(long)} instead.</p>
2441      *
2442      * <p><strong>Thread Safety:</strong> This method is thread-safe and handles concurrent
2443      * modifications to the internal job manager list gracefully.</p>
2444      *
2445      * <p><strong>Example Usage:</strong></p>
2446      * <pre><code>
2447      * // Wait up to 5 seconds for all background JavaScript to complete
2448      * int remainingJobs = webClient.waitForBackgroundJavaScript(5000);
2449      * if (remainingJobs == 0) {
2450      *     log("All background JavaScript completed");
2451      * } else {
2452      *     log("Timeout reached, " + remainingJobs + " jobs still pending");
2453      * }
2454      * </code></pre>
2455      *
2456      * @param timeoutMillis the maximum amount of time to wait in milliseconds; must be positive
2457      * @return the number of background JavaScript jobs still executing or waiting to be executed
2458      *         when this method returns; returns <code>0</code> if all jobs completed successfully
2459      *         within the timeout period
2460      * @throws IllegalArgumentException if timeoutMillis is negative
2461      * @see #waitForBackgroundJavaScriptStartingBefore(long)
2462      * @see #waitForBackgroundJavaScriptStartingBefore(long, long)
2463      */
2464     public int waitForBackgroundJavaScript(final long timeoutMillis) {
2465         int count = 0;
2466         final long endTime = System.currentTimeMillis() + timeoutMillis;
2467         for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext();) {
2468             final JavaScriptJobManager jobManager;
2469             final WeakReference<JavaScriptJobManager> reference;
2470             try {
2471                 reference = i.next();
2472                 jobManager = reference.get();
2473                 if (jobManager == null) {
2474                     i.remove();
2475                     continue;
2476                 }
2477             }
2478             catch (final ConcurrentModificationException e) {
2479                 i = jobManagers_.iterator();
2480                 count = 0;
2481                 continue;
2482             }
2483 
2484             final long newTimeout = endTime - System.currentTimeMillis();
2485             count += jobManager.waitForJobs(newTimeout);
2486         }
2487         if (count != getAggregateJobCount()) {
2488             final long newTimeout = endTime - System.currentTimeMillis();
2489             return waitForBackgroundJavaScript(newTimeout);
2490         }
2491         return count;
2492     }
2493 
2494     /**
2495      * <p>Blocks until all background JavaScript tasks scheduled to start executing before
2496      * <code>(now + delayMillis)</code> have finished executing. Background JavaScript tasks include:</p>
2497      * <ul>
2498      *   <li>JavaScript scheduled via <code>window.setTimeout()</code></li>
2499      *   <li>JavaScript scheduled via <code>window.setInterval()</code></li>
2500      *   <li>Asynchronous <code>XMLHttpRequest</code> operations</li>
2501      *   <li>Other asynchronous JavaScript operations across all windows managed by this WebClient</li>
2502      * </ul>
2503      *
2504      * <p><strong>Method Behavior:</strong></p>
2505      * <ul>
2506      *   <li>If no background JavaScript tasks are currently executing and none are scheduled
2507      *       to start within <code>delayMillis</code>, this method returns immediately</li>
2508      *   <li>Tasks scheduled to execute after <code>(now + delayMillis)</code> are ignored
2509      *       and do not affect the waiting behavior</li>
2510      *   <li>The method waits for tasks to complete execution, not just to start</li>
2511      *   <li>This method waits indefinitely for qualifying tasks to complete (no timeout)</li>
2512      * </ul>
2513      *
2514      * <p><strong>Use Case:</strong> This method is ideal when you know approximately when
2515      * background JavaScript should start executing but are uncertain about execution duration.
2516      * Use this when you don't need a timeout and want to ensure all relevant tasks complete.
2517      * For scenarios where you need to wait for all background tasks regardless of timing,
2518      * use {@link #waitForBackgroundJavaScript(long)} instead. For timeout control, use
2519      * {@link #waitForBackgroundJavaScriptStartingBefore(long, long)} instead.</p>
2520      *
2521      * <p><strong>Thread Safety:</strong> This method is thread-safe and handles concurrent
2522      * modifications to the internal job manager list gracefully.</p>
2523      *
2524      * <p><strong>Example Usage:</strong></p>
2525      * <pre><code>
2526      * // Wait indefinitely for JavaScript tasks starting within 1 second
2527      * int remainingJobs = webClient.waitForBackgroundJavaScriptStartingBefore(1000);
2528      * if (remainingJobs == 0) {
2529      *     log("All relevant background JavaScript completed");
2530      * } else {
2531      *     log("Some tasks may still be pending: " + remainingJobs + " jobs");
2532      * }
2533      *
2534      * // Common pattern: wait for tasks that should start soon
2535      * // (useful after triggering an action that schedules JavaScript)
2536      * webClient.waitForBackgroundJavaScriptStartingBefore(500);
2537      * </code></pre>
2538      *
2539      * @param delayMillis the delay which determines the background tasks to wait for (in milliseconds);
2540      *                   must be non-negative
2541      * @return the number of background JavaScript jobs still executing or waiting to be executed
2542      *         when this method returns; returns <code>0</code> if all qualifying jobs completed
2543      *         successfully
2544      * @see #waitForBackgroundJavaScript(long)
2545      * @see #waitForBackgroundJavaScriptStartingBefore(long, long)
2546      */
2547     public int waitForBackgroundJavaScriptStartingBefore(final long delayMillis) {
2548         return waitForBackgroundJavaScriptStartingBefore(delayMillis, -1);
2549     }
2550 
2551     /**
2552      * <p>Blocks until all background JavaScript tasks scheduled to start executing before
2553      * <code>(now + delayMillis)</code> have finished executing, or until the specified timeout
2554      * is reached, whichever occurs first. Background JavaScript tasks include:</p>
2555      * <ul>
2556      *   <li>JavaScript scheduled via <code>window.setTimeout()</code></li>
2557      *   <li>JavaScript scheduled via <code>window.setInterval()</code></li>
2558      *   <li>Asynchronous <code>XMLHttpRequest</code> operations</li>
2559      *   <li>Other asynchronous JavaScript operations across all windows managed by this WebClient</li>
2560      * </ul>
2561      *
2562      * <p><strong>Method Behavior:</strong></p>
2563      * <ul>
2564      *   <li>If no background JavaScript tasks are currently executing and none are scheduled
2565      *       to start within <code>delayMillis</code>, this method returns immediately</li>
2566      *   <li>Tasks scheduled to execute after <code>(now + delayMillis)</code> are ignored
2567      *       and do not affect the waiting behavior</li>
2568      *   <li>The method waits for tasks to complete execution, not just to start</li>
2569      * </ul>
2570      *
2571      * <p><strong>Timeout Behavior:</strong></p>
2572      * <ul>
2573      *   <li>If <code>timeoutMillis</code> is negative or less than <code>delayMillis</code>,
2574      *       the timeout is ignored and the method waits indefinitely</li>
2575      *   <li>When a valid timeout is specified, the method will never block longer than
2576      *       <code>timeoutMillis</code> milliseconds</li>
2577      *   <li>The timeout applies to the total waiting time, not per task</li>
2578      * </ul>
2579      *
2580      * <p><strong>Use Case:</strong> This method is ideal when you know approximately when
2581      * background JavaScript should start executing but are uncertain about execution duration.
2582      * For scenarios where you need to wait for all background tasks regardless of timing,
2583      * use {@link #waitForBackgroundJavaScript(long)} instead.</p>
2584      *
2585      * <p><strong>Thread Safety:</strong> This method is thread-safe and handles concurrent
2586      * modifications to the internal job manager list gracefully.</p>
2587      *
2588      * <p><strong>Example Usage:</strong></p>
2589      * <pre><code>
2590      * // Wait for JavaScript tasks starting within 1 second, with 10 second max timeout
2591      * int remainingJobs = webClient.waitForBackgroundJavaScriptStartingBefore(1000, 10000);
2592      * if (remainingJobs == 0) {
2593      *     log("All relevant background JavaScript completed");
2594      * } else {
2595      *     log("Timeout reached or tasks still pending: " + remainingJobs + " jobs");
2596      * }
2597      *
2598      * // Wait indefinitely for tasks starting within 500ms (timeout ignored)
2599      * webClient.waitForBackgroundJavaScriptStartingBefore(500, 100); // timeout &lt; delay
2600      * </code></pre>
2601      *
2602      * @param delayMillis the delay which determines the background tasks to wait for (in milliseconds);
2603      *                   must be non-negative
2604      * @param timeoutMillis the maximum amount of time to wait (in milliseconds); if negative or
2605      *                     less than <code>delayMillis</code>, the timeout is ignored and the method
2606      *                     waits indefinitely for qualifying tasks to complete
2607      * @return the number of background JavaScript jobs still executing or waiting to be executed
2608      *         when this method returns; returns <code>0</code> if all qualifying jobs completed
2609      *         successfully within the specified constraints
2610      * @see #waitForBackgroundJavaScript(long)
2611      * @see #waitForBackgroundJavaScriptStartingBefore(long)
2612      */
2613     public int waitForBackgroundJavaScriptStartingBefore(final long delayMillis, final long timeoutMillis) {
2614         int count = 0;
2615         long now = System.currentTimeMillis();
2616         final long endTime = now + delayMillis;
2617         long endTimeout = now + timeoutMillis;
2618         if (timeoutMillis < 0 || timeoutMillis < delayMillis) {
2619             endTimeout = -1;
2620         }
2621 
2622         for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext();) {
2623             final JavaScriptJobManager jobManager;
2624             final WeakReference<JavaScriptJobManager> reference;
2625             try {
2626                 reference = i.next();
2627                 jobManager = reference.get();
2628                 if (jobManager == null) {
2629                     i.remove();
2630                     continue;
2631                 }
2632             }
2633             catch (final ConcurrentModificationException e) {
2634                 i = jobManagers_.iterator();
2635                 count = 0;
2636                 continue;
2637             }
2638             now = System.currentTimeMillis();
2639             final long newDelay = endTime - now;
2640             final long newTimeout = (endTimeout == -1) ? -1 : endTimeout - now;
2641             count += jobManager.waitForJobsStartingBefore(newDelay, newTimeout);
2642         }
2643         if (count != getAggregateJobCount()) {
2644             now = System.currentTimeMillis();
2645             final long newDelay = endTime - now;
2646             final long newTimeout = (endTimeout == -1) ? -1 : endTimeout - now;
2647             return waitForBackgroundJavaScriptStartingBefore(newDelay, newTimeout);
2648         }
2649         return count;
2650     }
2651 
2652     /**
2653      * Returns the aggregate background JavaScript job count across all windows.
2654      * @return the aggregate background JavaScript job count across all windows
2655      */
2656     private int getAggregateJobCount() {
2657         int count = 0;
2658         for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext();) {
2659             final JavaScriptJobManager jobManager;
2660             final WeakReference<JavaScriptJobManager> reference;
2661             try {
2662                 reference = i.next();
2663                 jobManager = reference.get();
2664                 if (jobManager == null) {
2665                     i.remove();
2666                     continue;
2667                 }
2668             }
2669             catch (final ConcurrentModificationException e) {
2670                 i = jobManagers_.iterator();
2671                 count = 0;
2672                 continue;
2673             }
2674             final int jobCount = jobManager.getJobCount();
2675             count += jobCount;
2676         }
2677         return count;
2678     }
2679 
2680     /**
2681      * When we deserialize, re-initializie transient fields.
2682      * @param in the object input stream
2683      * @throws IOException if an error occurs
2684      * @throws ClassNotFoundException if an error occurs
2685      */
2686     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
2687         in.defaultReadObject();
2688 
2689         webConnection_ = new HttpWebConnection(this);
2690         scriptEngine_ = new JavaScriptEngine(this);
2691         jobManagers_ = Collections.synchronizedList(new ArrayList<>());
2692         loadQueue_ = new ArrayList<>();
2693         css3ParserPool_ = new CSS3ParserPool();
2694         broadcastChannel_ = new HashSet<>();
2695     }
2696 
2697     private static class LoadJob {
2698         private final WebWindow requestingWindow_;
2699         private final String target_;
2700         private final WebResponse response_;
2701         private final WeakReference<Page> originalPage_;
2702         private final WebRequest request_;
2703         private final String forceAttachmentWithFilename_;
2704 
2705         // we can't us the WebRequest from the WebResponse because
2706         // we need the original request e.g. after a redirect
2707         LoadJob(final WebRequest request, final WebResponse response,
2708                 final WebWindow requestingWindow, final String target, final String forceAttachmentWithFilename) {
2709             request_ = request;
2710             requestingWindow_ = requestingWindow;
2711             target_ = target;
2712             response_ = response;
2713             originalPage_ = new WeakReference<>(requestingWindow.getEnclosedPage());
2714             forceAttachmentWithFilename_ = forceAttachmentWithFilename;
2715         }
2716 
2717         public boolean isOutdated() {
2718             if (target_ != null && !target_.isEmpty()) {
2719                 return false;
2720             }
2721 
2722             if (requestingWindow_.isClosed()) {
2723                 return true;
2724             }
2725 
2726             if (requestingWindow_.getEnclosedPage() != originalPage_.get()) {
2727                 return true;
2728             }
2729 
2730             return false;
2731         }
2732     }
2733 
2734     /**
2735      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
2736      *
2737      * Perform the downloads and stores it for loading later into a window.
2738      * In the future downloads should be performed in parallel in separated threads.
2739      * TODO: refactor it before next release.
2740      * @param requestingWindow the window from which the request comes
2741      * @param target the name of the target window
2742      * @param request the request to perform
2743      * @param checkHash if true check for hashChenage
2744      * @param forceAttachmentWithFilename if not {@code null} the AttachmentHandler isAttachment() method is not called,
2745      *        the response has to be handled as attachment in any case
2746      * @param description information about the origin of the request. Useful for debugging.
2747      */
2748     public void download(final WebWindow requestingWindow, final String target,
2749         final WebRequest request, final boolean checkHash,
2750         final String forceAttachmentWithFilename, final String description) {
2751 
2752         final WebWindow targetWindow = resolveWindow(requestingWindow, target);
2753         final URL url = request.getUrl();
2754 
2755         if (targetWindow != null && HttpMethod.POST != request.getHttpMethod()) {
2756             final Page page = targetWindow.getEnclosedPage();
2757             if (page != null) {
2758                 if (page.isHtmlPage() && !((HtmlPage) page).isOnbeforeunloadAccepted()) {
2759                     return;
2760                 }
2761 
2762                 if (checkHash) {
2763                     final URL current = page.getUrl();
2764                     final boolean justHashJump =
2765                             HttpMethod.GET == request.getHttpMethod()
2766                             && UrlUtils.sameFile(url, current)
2767                             && null != url.getRef();
2768 
2769                     if (justHashJump) {
2770                         processOnlyHashChange(targetWindow, url);
2771                         return;
2772                     }
2773                 }
2774             }
2775         }
2776 
2777         synchronized (loadQueue_) {
2778             // verify if this load job doesn't already exist
2779             for (final LoadJob otherLoadJob : loadQueue_) {
2780                 if (otherLoadJob.response_ == null) {
2781                     continue;
2782                 }
2783                 final WebRequest otherRequest = otherLoadJob.request_;
2784                 final URL otherUrl = otherRequest.getUrl();
2785 
2786                 if (url.getPath().equals(otherUrl.getPath()) // fail fast
2787                     && url.toString().equals(otherUrl.toString())
2788                     && request.getRequestParameters().equals(otherRequest.getRequestParameters())
2789                     && Objects.equals(request.getRequestBody(), otherRequest.getRequestBody())) {
2790                     return; // skip it;
2791                 }
2792             }
2793         }
2794 
2795         final LoadJob loadJob;
2796         try {
2797             WebResponse response;
2798             try {
2799                 response = loadWebResponse(request);
2800             }
2801             catch (final NoHttpResponseException e) {
2802                 LOG.error("NoHttpResponseException while downloading; generating a NoHttpResponse", e);
2803                 response = new WebResponse(RESPONSE_DATA_NO_HTTP_RESPONSE, request, 0);
2804             }
2805             loadJob = new LoadJob(request, response, requestingWindow, target, forceAttachmentWithFilename);
2806         }
2807         catch (final IOException e) {
2808             throw new RuntimeException(e);
2809         }
2810 
2811         synchronized (loadQueue_) {
2812             loadQueue_.add(loadJob);
2813         }
2814     }
2815 
2816     /**
2817      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
2818      *
2819      * Loads downloaded responses into the corresponding windows.
2820      * TODO: refactor it before next release.
2821      * @throws IOException in case of exception
2822      * @throws FailingHttpStatusCodeException in case of exception
2823      */
2824     public void loadDownloadedResponses() throws FailingHttpStatusCodeException, IOException {
2825         final List<LoadJob> queue;
2826 
2827         // synchronize access to the loadQueue_,
2828         // to be sure no job is ignored
2829         synchronized (loadQueue_) {
2830             if (loadQueue_.isEmpty()) {
2831                 return;
2832             }
2833             queue = new ArrayList<>(loadQueue_);
2834             loadQueue_.clear();
2835         }
2836 
2837         final HashSet<WebWindow> updatedWindows = new HashSet<>();
2838         for (int i = queue.size() - 1; i >= 0; --i) {
2839             final LoadJob loadJob = queue.get(i);
2840             if (loadJob.isOutdated()) {
2841                 if (LOG.isInfoEnabled()) {
2842                     LOG.info("No usage of download: " + loadJob);
2843                 }
2844                 continue;
2845             }
2846 
2847             final WebWindow window = resolveWindow(loadJob.requestingWindow_, loadJob.target_);
2848             if (updatedWindows.contains(window)) {
2849                 if (LOG.isInfoEnabled()) {
2850                     LOG.info("No usage of download: " + loadJob);
2851                 }
2852                 continue;
2853             }
2854 
2855             final WebWindow win = openTargetWindow(loadJob.requestingWindow_, loadJob.target_, TARGET_SELF);
2856             final Page pageBeforeLoad = win.getEnclosedPage();
2857             loadWebResponseInto(loadJob.response_, win, loadJob.forceAttachmentWithFilename_);
2858 
2859             // start execution here.
2860             if (scriptEngine_ != null) {
2861                 scriptEngine_.registerWindowAndMaybeStartEventLoop(win);
2862             }
2863 
2864             if (pageBeforeLoad != win.getEnclosedPage()) {
2865                 updatedWindows.add(win);
2866             }
2867 
2868             // check and report problems if needed
2869             throwFailingHttpStatusCodeExceptionIfNecessary(loadJob.response_);
2870         }
2871     }
2872 
2873     private static void processOnlyHashChange(final WebWindow window, final URL urlWithOnlyHashChange) {
2874         final Page page = window.getEnclosedPage();
2875         final String oldURL = page.getUrl().toExternalForm();
2876 
2877         // update request url
2878         final WebRequest req = page.getWebResponse().getWebRequest();
2879         req.setUrl(urlWithOnlyHashChange);
2880 
2881         // update location.hash
2882         final Window jsWindow = window.getScriptableObject();
2883         if (null != jsWindow) {
2884             final Location location = jsWindow.getLocation();
2885             location.setHash(oldURL, urlWithOnlyHashChange.getRef());
2886         }
2887 
2888         // add to history
2889         window.getHistory().addPage(page);
2890     }
2891 
2892     /**
2893      * Returns the options object of this WebClient.
2894      * @return the options object
2895      */
2896     public WebClientOptions getOptions() {
2897         return options_;
2898     }
2899 
2900     /**
2901      * Gets the holder for the different storages.
2902      * <p><span style="color:red">Experimental API: May be changed in next release!</span></p>
2903      * @return the holder
2904      */
2905     public StorageHolder getStorageHolder() {
2906         return storageHolder_;
2907     }
2908 
2909     /**
2910      * Returns the currently configured cookies applicable to the specified URL, in an unmodifiable set.
2911      * If disabled, this returns an empty set.
2912      * @param url the URL on which to filter the returned cookies
2913      * @return the currently configured cookies applicable to the specified URL, in an unmodifiable set
2914      */
2915     public synchronized Set<Cookie> getCookies(final URL url) {
2916         final CookieManager cookieManager = getCookieManager();
2917 
2918         if (!cookieManager.isCookiesEnabled()) {
2919             return Collections.emptySet();
2920         }
2921 
2922         final URL normalizedUrl = HttpClientConverter.replaceForCookieIfNecessary(url);
2923 
2924         final String host = normalizedUrl.getHost();
2925         // URLs like "about:blank" don't have cookies and we need to catch these
2926         // cases here before HttpClient complains
2927         if (host.isEmpty()) {
2928             return Collections.emptySet();
2929         }
2930 
2931         // discard expired cookies
2932         cookieManager.clearExpired(new Date());
2933 
2934         final Set<Cookie> matchingCookies = new LinkedHashSet<>();
2935         HttpClientConverter.addMatching(cookieManager.getCookies(), normalizedUrl,
2936                 getBrowserVersion(), matchingCookies);
2937         return Collections.unmodifiableSet(matchingCookies);
2938     }
2939 
2940     /**
2941      * Parses the given cookie and adds this to our cookie store.
2942      * @param cookieString the string to parse
2943      * @param pageUrl the url of the page that likes to set the cookie
2944      * @param origin the requester
2945      */
2946     public void addCookie(final String cookieString, final URL pageUrl, final Object origin) {
2947         final CookieManager cookieManager = getCookieManager();
2948         if (!cookieManager.isCookiesEnabled()) {
2949             if (LOG.isDebugEnabled()) {
2950                 LOG.debug("Skipped adding cookie: '" + cookieString
2951                         + "' because cookies are not enabled for the CookieManager.");
2952             }
2953             return;
2954         }
2955 
2956         try {
2957             final List<Cookie> cookies = HttpClientConverter.parseCookie(cookieString, pageUrl, getBrowserVersion());
2958 
2959             for (final Cookie cookie : cookies) {
2960                 cookieManager.addCookie(cookie);
2961 
2962                 if (LOG.isDebugEnabled()) {
2963                     LOG.debug("Added cookie: '" + cookieString + "'");
2964                 }
2965             }
2966         }
2967         catch (final MalformedCookieException e) {
2968             if (LOG.isDebugEnabled()) {
2969                 LOG.warn("Adding cookie '" + cookieString + "' failed.", e);
2970             }
2971             getIncorrectnessListener().notify("Adding cookie '" + cookieString
2972                         + "' failed; reason: '" + e.getMessage() + "'.", origin);
2973         }
2974     }
2975 
2976     /**
2977      * Returns true if the javaScript support is enabled.
2978      * To disable the javascript support (eg. temporary)
2979      * you have to use the {@link WebClientOptions#setJavaScriptEnabled(boolean)} setter.
2980      * @see #isJavaScriptEngineEnabled()
2981      * @see WebClientOptions#isJavaScriptEnabled()
2982      * @return true if the javaScript engine and the javaScript support is enabled.
2983      */
2984     public boolean isJavaScriptEnabled() {
2985         return javaScriptEngineEnabled_ && getOptions().isJavaScriptEnabled();
2986     }
2987 
2988     /**
2989      * Returns true if the javaScript engine is enabled.
2990      * To disable the javascript engine you have to use the
2991      * {@link WebClient#WebClient(BrowserVersion, boolean, String, int)} constructor.
2992      * @return true if the javaScript engine is enabled.
2993      */
2994     public boolean isJavaScriptEngineEnabled() {
2995         return javaScriptEngineEnabled_;
2996     }
2997 
2998     /**
2999      * Parses the given XHtml code string and loads the resulting XHtmlPage into
3000      * the current window.
3001      *
3002      * @param htmlCode the html code as string
3003      * @return the HtmlPage
3004      * @throws IOException in case of error
3005      */
3006     public HtmlPage loadHtmlCodeIntoCurrentWindow(final String htmlCode) throws IOException {
3007         final HTMLParser htmlParser = getPageCreator().getHtmlParser();
3008         final WebWindow webWindow = getCurrentWindow();
3009 
3010         final StringWebResponse webResponse =
3011                 new StringWebResponse(htmlCode, new URL("https://www.htmlunit.org/dummy.html"));
3012         final HtmlPage page = new HtmlPage(webResponse, webWindow);
3013         webWindow.setEnclosedPage(page);
3014 
3015         htmlParser.parse(this, webResponse, page, false, false);
3016         return page;
3017     }
3018 
3019     /**
3020      * Parses the given XHtml code string and loads the resulting XHtmlPage into
3021      * the current window.
3022      *
3023      * @param xhtmlCode the xhtml code as string
3024      * @return the XHtmlPage
3025      * @throws IOException in case of error
3026      */
3027     public XHtmlPage loadXHtmlCodeIntoCurrentWindow(final String xhtmlCode) throws IOException {
3028         final HTMLParser htmlParser = getPageCreator().getHtmlParser();
3029         final WebWindow webWindow = getCurrentWindow();
3030 
3031         final StringWebResponse webResponse =
3032                 new StringWebResponse(xhtmlCode, new URL("https://www.htmlunit.org/dummy.html"));
3033         final XHtmlPage page = new XHtmlPage(webResponse, webWindow);
3034         webWindow.setEnclosedPage(page);
3035 
3036         htmlParser.parse(this, webResponse, page, true, false);
3037         return page;
3038     }
3039 
3040     /**
3041      * Creates a new {@link WebSocketAdapter}.
3042      *
3043      * @param webSocketListener the {@link WebSocketListener}
3044      * @return a new {@link WebSocketAdapter}
3045      */
3046     public WebSocketAdapter buildWebSocketAdapter(final WebSocketListener webSocketListener) {
3047         return webSocketAdapterFactory_.buildWebSocketAdapter(this, webSocketListener);
3048     }
3049 
3050     /**
3051      * Defines a new factory method to create a new WebSocketAdapter.
3052      *
3053      * @param factory a {@link WebSocketAdapterFactory}
3054      */
3055     public void setWebSocketAdapter(final WebSocketAdapterFactory factory) {
3056         webSocketAdapterFactory_ = factory;
3057     }
3058 
3059     /**
3060      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
3061      *
3062      * @return a CSS3Parser that will return to an internal pool for reuse if closed using the
3063      *         try-with-resource concept
3064      */
3065     public PooledCSS3Parser getCSS3Parser() {
3066         return this.css3ParserPool_.get();
3067     }
3068 
3069     /**
3070      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
3071      *
3072      * @return the set of known {@link BroadcastChannel}s
3073      */
3074     public Set<BroadcastChannel> getBroadcastChannels() {
3075         return broadcastChannel_;
3076     }
3077 
3078     /**
3079      * Our pool of CSS3Parsers. If you need a parser, get it from here and use the AutoCloseable
3080      * functionality with a try-with-resource block. If you don't want to do that at all, continue
3081      * to build CSS3Parsers the old fashioned way.
3082      * <p>
3083      * Fetching a parser is thread safe. This API is built to minimize synchronization overhead,
3084      * hence it is possible to miss a returned parser from another thread under heavy pressure,
3085      * but because that is unlikely, we keep it simple and efficient. Caches are not supposed
3086      * to give cutting-edge guarantees.
3087      * <p>
3088      * This concept avoids a resource leak when someone does not close the fetched
3089      * parser because the pool does not know anything about the parser unless
3090      * it returns. We are not running a checkout-checkin concept.
3091      * <p>
3092      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
3093      */
3094     static class CSS3ParserPool {
3095         /*
3096          * Our pool. We only hold data when it is available. In addition, synchronization against
3097          * this deque is cheap.
3098          */
3099         private final ConcurrentLinkedDeque<PooledCSS3Parser> parsers_ = new ConcurrentLinkedDeque<>();
3100 
3101         /**
3102          * Fetch a new or recycled CSS3parser. Make sure you use the try-with-resource concept
3103          * to automatically return it after use because a parser creation is expensive.
3104          * We won't get a leak, if you don't do so, but that will remove the advantage.
3105          *
3106          * @return a parser
3107          */
3108         public PooledCSS3Parser get() {
3109             // see if we have one, LIFO
3110             final PooledCSS3Parser parser = parsers_.pollLast();
3111 
3112             // if we don't have one, get us one
3113             return parser != null ? parser.markInUse(this) : new PooledCSS3Parser(this);
3114         }
3115 
3116         /**
3117          * Return a parser. Normally you don't have to use that method explicitly.
3118          * Prefer to user the AutoCloseable interface of the PooledParser by
3119          * using a try-with-resource statement.
3120          *
3121          * @param parser the parser to recycle
3122          */
3123         protected void recycle(final PooledCSS3Parser parser) {
3124             parsers_.addLast(parser);
3125         }
3126     }
3127 
3128     /**
3129      * This is a poolable CSS3Parser which can be reused automatically when closed.
3130      * A regular CSS3Parser is not thread-safe, hence also our pooled parser
3131      * is not thread-safe.
3132      * <p>
3133      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
3134      */
3135     public static class PooledCSS3Parser extends CSS3Parser implements AutoCloseable {
3136         /**
3137          * The pool we want to return us to. Because multiple threads can use this, we
3138          * have to ensure that we see the action here.
3139          */
3140         private CSS3ParserPool pool_;
3141 
3142         /**
3143          * Create a new poolable parser.
3144          *
3145          * @param pool the pool the parser should return to when it is closed
3146          */
3147         protected PooledCSS3Parser(final CSS3ParserPool pool) {
3148             super();
3149             this.pool_ = pool;
3150         }
3151 
3152         /**
3153          * Resets the parser's pool state so it can be safely returned again.
3154          *
3155          * @param pool the pool the parser should return to when it is closed
3156          * @return this parser for fluid programming
3157          */
3158         protected PooledCSS3Parser markInUse(final CSS3ParserPool pool) {
3159             // ensure we detect programming mistakes
3160             if (this.pool_ == null) {
3161                 this.pool_ = pool;
3162             }
3163             else {
3164                 throw new IllegalStateException("This PooledParser was not returned to the pool properly");
3165             }
3166 
3167             return this;
3168         }
3169 
3170         /**
3171          * Implements the AutoClosable interface. The return method ensures that
3172          * we are notified when we incorrectly close it twice which indicates a
3173          * programming flow defect.
3174          *
3175          * @throws IllegalStateException in case the parser is closed several times
3176          */
3177         @Override
3178         public void close() {
3179             if (this.pool_ != null) {
3180                 final CSS3ParserPool oldPool = this.pool_;
3181                 // set null first and recycle later to avoid exposing a broken state
3182                 // volatile guarantees visibility
3183                 this.pool_ = null;
3184 
3185                 // return
3186                 oldPool.recycle(this);
3187             }
3188             else {
3189                 throw new IllegalStateException("This PooledParser was returned already");
3190             }
3191         }
3192     }
3193 }