View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.javascript.host.xml;
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.XHR_HANDLE_SYNC_NETWORK_ERRORS;
20  import static org.htmlunit.BrowserVersionFeatures.XHR_LOAD_ALWAYS_AFTER_DONE;
21  import static org.htmlunit.BrowserVersionFeatures.XHR_PREFLIGHT_CORS;
22  import static org.htmlunit.BrowserVersionFeatures.XHR_RESPONSE_TEXT_EMPTY_UNSENT;
23  import static org.htmlunit.BrowserVersionFeatures.XHR_SEND_NETWORK_ERROR_IF_ABORTED;
24  import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
25  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
26  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
27  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.StringWriter;
32  import java.net.MalformedURLException;
33  import java.net.SocketTimeoutException;
34  import java.net.URL;
35  import java.nio.charset.Charset;
36  import java.util.Arrays;
37  import java.util.Collections;
38  import java.util.HashSet;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Map.Entry;
42  import java.util.TreeMap;
43  
44  import javax.xml.transform.OutputKeys;
45  import javax.xml.transform.Transformer;
46  import javax.xml.transform.TransformerFactory;
47  import javax.xml.transform.dom.DOMSource;
48  import javax.xml.transform.stream.StreamResult;
49  
50  import org.apache.commons.io.IOUtils;
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  import org.htmlunit.AjaxController;
54  import org.htmlunit.BrowserVersion;
55  import org.htmlunit.FormEncodingType;
56  import org.htmlunit.HttpHeader;
57  import org.htmlunit.HttpMethod;
58  import org.htmlunit.SgmlPage;
59  import org.htmlunit.WebClient;
60  import org.htmlunit.WebRequest;
61  import org.htmlunit.WebRequest.FetchMode;
62  import org.htmlunit.WebRequest.HttpHint;
63  import org.htmlunit.WebResponse;
64  import org.htmlunit.WebWindow;
65  import org.htmlunit.corejs.javascript.Context;
66  import org.htmlunit.corejs.javascript.ContextAction;
67  import org.htmlunit.corejs.javascript.Function;
68  import org.htmlunit.corejs.javascript.ScriptableObject;
69  import org.htmlunit.corejs.javascript.json.JsonParser;
70  import org.htmlunit.corejs.javascript.json.JsonParser.ParseException;
71  import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
72  import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBufferView;
73  import org.htmlunit.html.HtmlPage;
74  import org.htmlunit.html.csp.ContentSecurityPolicy;
75  import org.htmlunit.httpclient.HtmlUnitUsernamePasswordCredentials;
76  import org.htmlunit.javascript.HtmlUnitContextFactory;
77  import org.htmlunit.javascript.JavaScriptEngine;
78  import org.htmlunit.javascript.background.BackgroundJavaScriptFactory;
79  import org.htmlunit.javascript.background.JavaScriptJob;
80  import org.htmlunit.javascript.configuration.JsxClass;
81  import org.htmlunit.javascript.configuration.JsxConstant;
82  import org.htmlunit.javascript.configuration.JsxConstructor;
83  import org.htmlunit.javascript.configuration.JsxFunction;
84  import org.htmlunit.javascript.configuration.JsxGetter;
85  import org.htmlunit.javascript.configuration.JsxSetter;
86  import org.htmlunit.javascript.host.Element;
87  import org.htmlunit.javascript.host.URLSearchParams;
88  import org.htmlunit.javascript.host.Window;
89  import org.htmlunit.javascript.host.dom.DOMException;
90  import org.htmlunit.javascript.host.dom.DOMParser;
91  import org.htmlunit.javascript.host.dom.Document;
92  import org.htmlunit.javascript.host.event.Event;
93  import org.htmlunit.javascript.host.event.ProgressEvent;
94  import org.htmlunit.javascript.host.file.Blob;
95  import org.htmlunit.javascript.host.html.HTMLDocument;
96  import org.htmlunit.util.EncodingSniffer;
97  import org.htmlunit.util.MimeType;
98  import org.htmlunit.util.NameValuePair;
99  import org.htmlunit.util.StringUtils;
100 import org.htmlunit.util.UrlUtils;
101 import org.htmlunit.util.WebResponseWrapper;
102 import org.htmlunit.util.XUserDefinedCharset;
103 import org.htmlunit.xml.XmlPage;
104 import org.w3c.dom.DocumentType;
105 
106 /**
107  * JavaScript host object for {@code XMLHttpRequest}.
108  *
109  * @author Daniel Gredler
110  * @author Marc Guillemot
111  * @author Ahmed Ashour
112  * @author Stuart Begg
113  * @author Ronald Brill
114  * @author Sebastian Cato
115  * @author Frank Danek
116  * @author Jake Cobb
117  * @author Thorsten Wendelmuth
118  * @author Lai Quang Duong
119  * @author Sven Strickroth
120  * @author Ashutosh Panda
121  * @author Vidya m
122  *
123  * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest">MDN Documentation</a>
124  */
125 @JsxClass
126 public class XMLHttpRequest extends XMLHttpRequestEventTarget {
127 
128     private static final Log LOG = LogFactory.getLog(XMLHttpRequest.class);
129 
130     /** The object has been created, but not initialized (the {@code open()} method has not been called). */
131     @JsxConstant
132     public static final int UNSENT = 0;
133 
134     /** The object has been created, but the {@code send()} method has not been called. */
135     @JsxConstant
136     public static final int OPENED = 1;
137 
138     /** The {@code send()} method has been called, but the status and headers are not yet available. */
139     @JsxConstant
140     public static final int HEADERS_RECEIVED = 2;
141 
142     /** Some data has been received. */
143     @JsxConstant
144     public static final int LOADING = 3;
145 
146     /** All the data has been received; the complete data is available in responseBody and responseText. */
147     @JsxConstant
148     public static final int DONE = 4;
149 
150     private static final String RESPONSE_TYPE_DEFAULT = "";
151     private static final String RESPONSE_TYPE_ARRAYBUFFER = "arraybuffer";
152     private static final String RESPONSE_TYPE_BLOB = "blob";
153     private static final String RESPONSE_TYPE_DOCUMENT = "document";
154     private static final String RESPONSE_TYPE_JSON = "json";
155     private static final String RESPONSE_TYPE_TEXT = "text";
156 
157     private static final String ALLOW_ORIGIN_ALL = "*";
158 
159     private static final int MAX_REDIRECTS = 20;
160 
161     private static final HashSet<String> PROHIBITED_HEADERS_ = new HashSet<>(Arrays.asList(
162         "accept-charset", HttpHeader.ACCEPT_ENCODING_LC,
163         HttpHeader.CONNECTION_LC, HttpHeader.CONTENT_LENGTH_LC, HttpHeader.COOKIE_LC, "cookie2",
164         "content-transfer-encoding", "date", "expect",
165         HttpHeader.HOST_LC, "keep-alive", HttpHeader.REFERER_LC, "te", "trailer", "transfer-encoding",
166         "upgrade", HttpHeader.USER_AGENT_LC, "via"));
167 
168     private int state_;
169     private WebRequest webRequest_;
170     private boolean async_;
171     private int jobID_;
172     private WebResponse webResponse_;
173     private String overriddenMimeType_;
174     private boolean withCredentials_;
175     private boolean isSameOrigin_;
176     private int timeout_;
177     private boolean aborted_;
178     private String responseType_;
179 
180     private Document responseXML_;
181     private XMLHttpRequestUpload upload_;
182 
183     /**
184      * Creates a new instance.
185      */
186     public XMLHttpRequest() {
187         state_ = UNSENT;
188         responseType_ = RESPONSE_TYPE_DEFAULT;
189     }
190 
191     /**
192      * Creates an instance of this object.
193      */
194     @Override
195     @JsxConstructor
196     public void jsConstructor() {
197         // don't call super here
198     }
199 
200     /**
201      * Sets the state as specified and invokes the state change handler if one has been set.
202      *
203      * @param state the new state
204      */
205     private void setState(final int state) {
206         if (state == UNSENT
207                 || state == OPENED
208                 || state == HEADERS_RECEIVED
209                 || state == LOADING
210                 || state == DONE) {
211             state_ = state;
212             if (LOG.isDebugEnabled()) {
213                 LOG.debug("State changed to : " + state);
214             }
215             return;
216         }
217 
218         LOG.error("Received an unknown state " + state
219                         + ", the state is not implemented, please check setState() implementation.");
220     }
221 
222     private void fireJavascriptEvent(final String eventName) {
223         if (aborted_) {
224             if (LOG.isDebugEnabled()) {
225                 LOG.debug("Firing javascript XHR event: " + eventName + " for an already aborted request - ignored.");
226             }
227 
228             return;
229         }
230         fireJavascriptEventIgnoreAbort(eventName);
231     }
232 
233     private void fireJavascriptEventIgnoreAbort(final String eventName) {
234         if (LOG.isDebugEnabled()) {
235             LOG.debug("Firing javascript XHR event: " + eventName);
236         }
237 
238         final boolean isReadyStateChange = Event.TYPE_READY_STATE_CHANGE.equalsIgnoreCase(eventName);
239         final Event event;
240         if (isReadyStateChange) {
241             event = new Event(this, Event.TYPE_READY_STATE_CHANGE);
242         }
243         else {
244             final ProgressEvent progressEvent = new ProgressEvent(this, eventName);
245 
246             if (webResponse_ != null) {
247                 final long contentLength = webResponse_.getContentLength();
248                 progressEvent.setLoaded(contentLength);
249             }
250             event = progressEvent;
251         }
252 
253         executeEventLocally(event);
254     }
255 
256     /**
257      * Returns the current state of the HTTP request. The possible values are:
258      * <ul>
259      *   <li>0 = unsent</li>
260      *   <li>1 = opened</li>
261      *   <li>2 = headers_received</li>
262      *   <li>3 = loading</li>
263      *   <li>4 = done</li>
264      * </ul>
265      *
266      * @return the current state of the HTTP request
267      */
268     @JsxGetter
269     public int getReadyState() {
270         return state_;
271     }
272 
273     /**
274      * Returns the {@code responseType} property.
275      *
276      * @return the {@code responseType} property
277      */
278     @JsxGetter
279     public String getResponseType() {
280         return responseType_;
281     }
282 
283     /**
284      * Sets the {@code responseType} property.
285      *
286      * @param responseType the {@code responseType} property
287      */
288     @JsxSetter
289     public void setResponseType(final String responseType) {
290         if (state_ == LOADING || state_ == DONE) {
291             throw JavaScriptEngine.reportRuntimeError("InvalidStateError");
292         }
293 
294         if (RESPONSE_TYPE_DEFAULT.equals(responseType)
295                 || RESPONSE_TYPE_ARRAYBUFFER.equals(responseType)
296                 || RESPONSE_TYPE_BLOB.equals(responseType)
297                 || RESPONSE_TYPE_DOCUMENT.equals(responseType)
298                 || RESPONSE_TYPE_JSON.equals(responseType)
299                 || RESPONSE_TYPE_TEXT.equals(responseType)) {
300 
301             if (state_ == OPENED && !async_) {
302                 throw JavaScriptEngine.asJavaScriptException(
303                         getWindow(),
304                         "synchronous XMLHttpRequests do not support responseType",
305                         DOMException.INVALID_ACCESS_ERR);
306             }
307 
308             responseType_ = responseType;
309         }
310     }
311 
312     /**
313      * Returns the response body as an {@code ArrayBuffer}, {@code Blob}, {@code Document},
314      * JavaScript object, or {@code DOMString}, depending on the value of the {@code responseType} property.
315      *
316      * @return the response body
317      */
318     @JsxGetter
319     public Object getResponse() {
320         if (RESPONSE_TYPE_DEFAULT.equals(responseType_) || RESPONSE_TYPE_TEXT.equals(responseType_)) {
321             if (webResponse_ != null) {
322                 final Charset encoding = webResponse_.getContentCharset();
323                 final String content = webResponse_.getContentAsString(encoding);
324                 if (content == null) {
325                     return "";
326                 }
327                 return content;
328             }
329         }
330 
331         if (state_ != DONE) {
332             return null;
333         }
334 
335         if (webResponse_ instanceof NetworkErrorWebResponse response) {
336             if (LOG.isDebugEnabled()) {
337                 LOG.debug("XMLHttpRequest.responseXML returns because of a network error ("
338                         + response.getError() + ")");
339             }
340             return null;
341         }
342 
343         if (RESPONSE_TYPE_ARRAYBUFFER.equals(responseType_)) {
344             long contentLength = webResponse_.getContentLength();
345             NativeArrayBuffer nativeArrayBuffer = new NativeArrayBuffer(JavaScriptEngine.toArrayLength(contentLength));
346 
347             try {
348                 final int bufferLength = Math.min(1024, (int) contentLength);
349                 final byte[] buffer = new byte[bufferLength];
350                 int offset = 0;
351                 try (InputStream inputStream = webResponse_.getContentAsStream()) {
352                     int readLen;
353                     while ((readLen = inputStream.read(buffer, 0, bufferLength)) != -1) {
354                         final long newLength = offset + readLen;
355                         // gzip content and the unzipped content is larger
356                         if (newLength > contentLength) {
357                             final NativeArrayBuffer expanded =
358                                     new NativeArrayBuffer(JavaScriptEngine.toArrayLength(newLength));
359                             expanded.getByteBuffer().put(0, nativeArrayBuffer.getByteBuffer(), 0, (int) contentLength);
360                             contentLength = newLength;
361                             nativeArrayBuffer = expanded;
362                         }
363 
364                         // Copy the newly read chunk into the buffer at the specified offset
365                         nativeArrayBuffer.getByteBuffer().put(offset, buffer, 0, readLen);
366                         offset = (int) newLength;
367                     }
368                 }
369 
370                 // for small responses the gzipped content might be larger than the original
371                 if (offset < contentLength) {
372                     final NativeArrayBuffer shrinked = new NativeArrayBuffer(offset);
373                     shrinked.getByteBuffer().put(0, nativeArrayBuffer.getByteBuffer(), 0, offset);
374                     nativeArrayBuffer = shrinked;
375                 }
376 
377                 nativeArrayBuffer.setParentScope(getParentScope());
378                 nativeArrayBuffer.setPrototype(
379                         ScriptableObject.getClassPrototype(getParentScope(), nativeArrayBuffer.getClassName()));
380 
381                 return nativeArrayBuffer;
382             }
383             catch (final IOException e) {
384                 webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
385                 return null;
386             }
387         }
388         else if (RESPONSE_TYPE_BLOB.equals(responseType_)) {
389             try {
390                 if (webResponse_ != null) {
391                     try (InputStream inputStream = webResponse_.getContentAsStream()) {
392                         final Blob blob = new Blob(IOUtils.toByteArray(inputStream), webResponse_.getContentType());
393                         blob.setParentScope(getParentScope());
394                         blob.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), blob.getClassName()));
395 
396                         return blob;
397                     }
398                 }
399             }
400             catch (final IOException e) {
401                 webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
402                 return null;
403             }
404         }
405         else if (RESPONSE_TYPE_DOCUMENT.equals(responseType_)) {
406             if (responseXML_ != null) {
407                 return responseXML_;
408             }
409 
410             if (webResponse_ != null) {
411                 String contentType = webResponse_.getContentType();
412                 if (org.htmlunit.util.StringUtils.isEmptyOrNull(contentType)) {
413                     contentType = MimeType.TEXT_XML;
414                 }
415                 return buildResponseXML(contentType);
416             }
417         }
418         else if (RESPONSE_TYPE_JSON.equals(responseType_)) {
419             if (webResponse_ != null) {
420                 final Charset encoding = webResponse_.getContentCharset();
421                 final String content = webResponse_.getContentAsString(encoding);
422                 if (content == null) {
423                     return null;
424                 }
425 
426                 try {
427                     return new JsonParser(Context.getCurrentContext(), getParentScope()).parseValue(content);
428                 }
429                 catch (final ParseException e) {
430                     if (LOG.isDebugEnabled()) {
431                         LOG.debug("XMLHttpRequest json parsing faild (" + e.getMessage() + ")");
432                     }
433                     return null;
434                 }
435             }
436         }
437 
438         return "";
439     }
440 
441     private Document buildResponseXML(final String contentType) {
442         try {
443             if (MimeType.TEXT_XML.equals(contentType)
444                     || MimeType.APPLICATION_XML.equals(contentType)
445                     || MimeType.APPLICATION_XHTML.equals(contentType)
446                     || "image/svg+xml".equals(contentType)) {
447                 final XMLDocument document = new XMLDocument();
448                 document.setParentScope(getParentScope());
449                 document.setPrototype(getPrototype(XMLDocument.class));
450                 final XmlPage page = new XmlPage(webResponse_, getWindow().getWebWindow(), false);
451                 if (!page.hasChildNodes()) {
452                     return null;
453                 }
454                 document.setDomNode(page);
455                 responseXML_ = document;
456                 return responseXML_;
457             }
458 
459             if (MimeType.TEXT_HTML.equals(contentType)) {
460                 responseXML_ = DOMParser.parseHtmlDocument(this, webResponse_, getWindow().getWebWindow());
461                 return responseXML_;
462             }
463             return null;
464         }
465         catch (final IOException e) {
466             webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
467             return null;
468         }
469     }
470 
471     /**
472      * Returns the response body as a string.
473      *
474      * @return a string version of the data retrieved from the server
475      */
476     @JsxGetter
477     public String getResponseText() {
478         if ((state_ == UNSENT || state_ == OPENED) && getBrowserVersion().hasFeature(XHR_RESPONSE_TEXT_EMPTY_UNSENT)) {
479             return "";
480         }
481 
482         if (!RESPONSE_TYPE_DEFAULT.equals(responseType_) && !RESPONSE_TYPE_TEXT.equals(responseType_)) {
483             throw JavaScriptEngine.asJavaScriptException(
484                     getWindow(),
485                     "InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': "
486                             + "The value is only accessible if the object's 'responseType' is '' or 'text' "
487                             + "(was '" + getResponseType() + "').",
488                     DOMException.INVALID_STATE_ERR);
489         }
490 
491         if (state_ == UNSENT || state_ == OPENED) {
492             return "";
493         }
494 
495         if (webResponse_ instanceof NetworkErrorWebResponse resp) {
496             if (LOG.isDebugEnabled()) {
497                 LOG.debug("XMLHttpRequest.responseXML returns because of a network error ("
498                         + resp.getError() + ")");
499             }
500             if (resp.getError() instanceof NoPermittedHeaderException
501                     || resp.getError() instanceof RedirectException) {
502                 return "";
503             }
504             return null;
505         }
506 
507         if (webResponse_ != null) {
508             final Charset encoding = webResponse_.getContentCharset();
509             final String content = webResponse_.getContentAsString(encoding);
510             if (content == null) {
511                 return "";
512             }
513             return content;
514         }
515 
516         LOG.debug("XMLHttpRequest.responseText was retrieved before the response was available.");
517         return "";
518     }
519 
520     /**
521      * Returns a DOM-compatible document object version of the data retrieved from the server.
522      *
523      * @return a DOM-compatible document object, or {@code null}
524      */
525     @JsxGetter
526     public Object getResponseXML() {
527         if (responseXML_ != null) {
528             return responseXML_;
529         }
530 
531         if (!RESPONSE_TYPE_DEFAULT.equals(responseType_) && !RESPONSE_TYPE_DOCUMENT.equals(responseType_)) {
532             throw JavaScriptEngine.asJavaScriptException(
533                     getWindow(),
534                     "InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': "
535                             + "The value is only accessible if the object's 'responseType' is '' or 'document' "
536                             + "(was '" + getResponseType() + "').",
537                     DOMException.INVALID_STATE_ERR);
538         }
539 
540         if (webResponse_ == null) {
541             if (LOG.isDebugEnabled()) {
542                 LOG.debug("XMLHttpRequest.responseXML returns null because there "
543                         + "is no web response so far (has send() been called?)");
544             }
545             return null;
546         }
547 
548         if (webResponse_ instanceof NetworkErrorWebResponse response) {
549             if (LOG.isDebugEnabled()) {
550                 LOG.debug("XMLHttpRequest.responseXML returns because of a network error ("
551                         + response.getError() + ")");
552             }
553             return null;
554         }
555 
556         String contentType = webResponse_.getContentType();
557         if (org.htmlunit.util.StringUtils.isEmptyOrNull(contentType)) {
558             contentType = MimeType.TEXT_XML;
559         }
560 
561         if (MimeType.TEXT_HTML.equalsIgnoreCase(contentType)) {
562             if (!async_ || !RESPONSE_TYPE_DOCUMENT.equals(responseType_)) {
563                 return null;
564             }
565         }
566 
567         return buildResponseXML(contentType);
568     }
569 
570     /**
571      * Returns the numeric HTTP status code returned by the server (e.g. 404 for "Not Found"
572      * or 200 for "OK").
573      *
574      * @return the numeric HTTP status code
575      */
576     @JsxGetter
577     public int getStatus() {
578         if (state_ == UNSENT || state_ == OPENED) {
579             return 0;
580         }
581         if (webResponse_ != null) {
582             return webResponse_.getStatusCode();
583         }
584 
585         if (LOG.isErrorEnabled()) {
586             LOG.error("XMLHttpRequest.status was retrieved without a response available (readyState: "
587                 + state_ + ").");
588         }
589         return 0;
590     }
591 
592     /**
593      * Returns the serialized url of the response (the final url after all redirects, without the fragment).
594      *
595      * @return the url of the response or an empty string if there is no response
596      */
597     @JsxGetter
598     public String getResponseURL() {
599         if (state_ == UNSENT || state_ == OPENED
600                 || webResponse_ == null
601                 || webResponse_ instanceof NetworkErrorWebResponse) {
602             return "";
603         }
604 
605         final String url = webResponse_.getWebRequest().getUrl().toExternalForm();
606         final int fragment = url.indexOf('#');
607         return fragment < 0 ? url : url.substring(0, fragment);
608     }
609 
610     /**
611      * Returns the HTTP status message accompanying the status code (e.g. "Not Found" or "OK").
612      *
613      * @return the HTTP status message
614      */
615     @JsxGetter
616     public String getStatusText() {
617         if (state_ == UNSENT || state_ == OPENED) {
618             return "";
619         }
620         if (webResponse_ != null) {
621             return webResponse_.getStatusMessage();
622         }
623 
624         if (LOG.isErrorEnabled()) {
625             LOG.error("XMLHttpRequest.statusText was retrieved without a response available (readyState: "
626                 + state_ + ").");
627         }
628         return "";
629     }
630 
631     /**
632      * Cancels the current HTTP request.
633      */
634     @JsxFunction
635     public void abort() {
636         getWindow().getWebWindow().getJobManager().stopJob(jobID_);
637 
638         if (state_ == OPENED
639                 || state_ == HEADERS_RECEIVED
640                 || state_ == LOADING) {
641             setState(DONE);
642             webResponse_ = new NetworkErrorWebResponse(webRequest_, null);
643             fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
644             fireJavascriptEvent(Event.TYPE_ABORT);
645             fireJavascriptEvent(Event.TYPE_LOAD_END);
646         }
647 
648         setState(UNSENT);
649         webResponse_ = new NetworkErrorWebResponse(webRequest_, null);
650         aborted_ = true;
651     }
652 
653     /**
654      * Returns all the HTTP response headers as a string.
655      *
656      * @return all response header names and values as a string
657      */
658     @JsxFunction
659     public String getAllResponseHeaders() {
660         if (state_ == UNSENT || state_ == OPENED) {
661             return "";
662         }
663         if (webResponse_ != null) {
664             final StringBuilder builder = new StringBuilder();
665             for (final NameValuePair header : webResponse_.getResponseHeaders()) {
666                 builder
667                     .append(header.getName())
668                     .append(": ")
669                     .append(header.getValue())
670                     .append("\r\n");
671             }
672             return builder.toString();
673         }
674 
675         if (LOG.isErrorEnabled()) {
676             LOG.error("XMLHttpRequest.getAllResponseHeaders() was called without a response available (readyState: "
677                 + state_ + ").");
678         }
679         return "";
680     }
681 
682     /**
683      * Returns the value of the specified HTTP response header.
684      *
685      * @param headerName the case-insensitive name of the header to retrieve
686      * @return the value of the specified HTTP header, or {@code null} if not found
687      */
688     @JsxFunction
689     public String getResponseHeader(final String headerName) {
690         if (state_ == UNSENT || state_ == OPENED) {
691             return null;
692         }
693         if (webResponse_ instanceof NetworkErrorWebResponse) {
694             return null;
695         }
696         if (webResponse_ != null) {
697             return webResponse_.getResponseHeaderValue(headerName);
698         }
699 
700         if (LOG.isErrorEnabled()) {
701             LOG.error("XMLHttpRequest.getAllResponseHeaders(..) was called without a response available (readyState: "
702                 + state_ + ").");
703         }
704         return null;
705     }
706 
707     /**
708      * Initializes the request by specifying the destination URL, method, and other optional attributes.
709      *
710      * @param method the HTTP method to use (e.g. GET, POST)
711      * @param urlParam the URL to send the request to
712      * @param asyncParam whether to send the request asynchronously; defaults to {@code true}
713      * @param user the username to use for authentication, if required
714      * @param password the password to use for authentication, if required
715      */
716     @JsxFunction
717     public void open(final String method, final Object urlParam, final Object asyncParam,
718         final Object user, final Object password) {
719 
720         // async defaults to true if not specified
721         boolean async = true;
722         if (!JavaScriptEngine.isUndefined(asyncParam)) {
723             async = JavaScriptEngine.toBoolean(asyncParam);
724         }
725 
726         final String url = JavaScriptEngine.toString(urlParam);
727 
728         // (URL + Method + User + Password) become a WebRequest instance.
729         final HtmlPage containingPage = (HtmlPage) getWindow().getWebWindow().getEnclosedPage();
730 
731         try {
732             final URL pageUrl = containingPage.getUrl();
733             // about:blank documents have the origin of their creator
734             final URL originUrl = containingPage.getOriginUrl();
735             // upgrade-insecure-requests of the Content-Security-Policy
736             final URL fullUrl = ContentSecurityPolicy.upgradeInsecureRequest(containingPage,
737                                     containingPage.getFullyQualifiedUrl(url));
738             final WebRequest request = new WebRequest(fullUrl, getBrowserVersion().getXmlHttpRequestAcceptHeader(),
739                                                                 getBrowserVersion().getAcceptEncodingHeader());
740             request.setCharset(UTF_8);
741             // https://xhr.spec.whatwg.org/#response-body
742             request.setDefaultResponseContentCharset(UTF_8);
743             request.setRefererHeader(pageUrl);
744 
745             request.setFetchDestination(WebRequest.FetchDestination.EMPTY);
746             request.setFetchModeOverride(FetchMode.CORS);
747             request.setRequestingUrl(originUrl);
748 
749             try {
750                 HttpMethod.validateHttpMethodName(method);
751             }
752             catch (final IllegalArgumentException e) {
753                 throw JavaScriptEngine.asJavaScriptException(
754                         getWindow(),
755                         e.getMessage(),
756                         DOMException.SYNTAX_ERR);
757             }
758 
759             final String methodUC = method.toUpperCase(Locale.ROOT);
760             if ("TRACE".equals(methodUC)) {
761                 throw JavaScriptEngine.asJavaScriptException(
762                         getWindow(),
763                         "HTTP Method '" + method + "' not allowed.",
764                         DOMException.SECURITY_ERR);
765             }
766 
767             try {
768                 request.setHttpMethod(HttpMethod.valueOf(methodUC));
769             }
770             catch (final IllegalArgumentException e) {
771                 if (LOG.isInfoEnabled()) {
772                     LOG.info("Incorrect HTTP Method '" + method + "'");
773                 }
774                 return;
775             }
776 
777             if ("data".equals(fullUrl.getProtocol())) {
778                 isSameOrigin_ = true;
779             }
780             else if ("blob".equals(fullUrl.getProtocol())) {
781                 boolean sameOrigin = false;
782                 try {
783                     final URL blobOrigin = UrlUtils.toUrlUnsafe(fullUrl.toExternalForm().substring("blob:".length()));
784                     sameOrigin = UrlUtils.isSameOrigin(originUrl, blobOrigin);
785                 }
786                 catch (final MalformedURLException ignored) {
787                     // keep sameOrigin = false
788                 }
789                 isSameOrigin_ = sameOrigin;
790             }
791             else {
792                 isSameOrigin_ = UrlUtils.isSameOrigin(originUrl, fullUrl);
793                 final boolean alwaysAddOrigin = HttpMethod.GET != request.getHttpMethod()
794                                                 && HttpMethod.HEAD != request.getHttpMethod();
795                 if (alwaysAddOrigin || !isSameOrigin_) {
796                     final StringBuilder origin = new StringBuilder().append(originUrl.getProtocol()).append("://")
797                             .append(originUrl.getHost());
798                     if (originUrl.getPort() != -1) {
799                         origin.append(':').append(originUrl.getPort());
800                     }
801                     request.setAdditionalHeader(HttpHeader.ORIGIN, origin.toString());
802                 }
803 
804                 // password is ignored if no user defined
805                 if (user != null && !JavaScriptEngine.isUndefined(user)) {
806                     final String userCred = user.toString();
807 
808                     String passwordCred = "";
809                     if (password != null && !JavaScriptEngine.isUndefined(password)) {
810                         passwordCred = password.toString();
811                     }
812 
813                     request.setCredentials(
814                                 new HtmlUnitUsernamePasswordCredentials(userCred, passwordCred.toCharArray()));
815                 }
816             }
817             webRequest_ = request;
818         }
819         catch (final MalformedURLException e) {
820             if (LOG.isErrorEnabled()) {
821                 LOG.error("Unable to initialize XMLHttpRequest using malformed URL '" + url + "'.");
822             }
823             return;
824         }
825 
826         // Async stays a boolean.
827         async_ = async;
828 
829         // Change the state!
830         setState(OPENED);
831         fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
832     }
833 
834     /**
835      * Sends the request to the server with the specified content as the request body.
836      *
837      * @param content the body of the message being sent with the request
838      */
839     @JsxFunction
840     public void send(final Object content) {
841         responseXML_ = null;
842 
843         if (webRequest_ == null) {
844             return;
845         }
846         if (!async_ && timeout_ > 0) {
847             throw JavaScriptEngine.throwAsScriptRuntimeEx(
848                     new RuntimeException("Synchronous requests must not set a timeout."));
849         }
850 
851         prepareRequestContent(content);
852         if (timeout_ > 0) {
853             webRequest_.setTimeout(timeout_);
854         }
855 
856         final Window w = getWindow();
857         final WebWindow ww = w.getWebWindow();
858         final WebClient client = ww.getWebClient();
859         final AjaxController ajaxController = client.getAjaxController();
860         final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
861         final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
862         if (synchron) {
863             doSend();
864         }
865         else {
866             // Create and start a thread in which to execute the request.
867             final HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory();
868             final ContextAction<Object> action = new ContextAction<>() {
869                 @Override
870                 public Object run(final Context cx) {
871                     doSend();
872                     return null;
873                 }
874 
875                 @Override
876                 public String toString() {
877                     return "XMLHttpRequest " + webRequest_.getHttpMethod() + " '" + webRequest_.getUrl() + "'";
878                 }
879             };
880             final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().
881                     createJavascriptXMLHttpRequestJob(cf, action);
882             LOG.debug("Starting XMLHttpRequest thread for asynchronous request");
883             jobID_ = ww.getJobManager().addJob(job, page);
884 
885             fireJavascriptEvent(Event.TYPE_LOAD_START);
886         }
887     }
888 
889     /**
890      * Prepares the {@link WebRequest} that will be sent.
891      *
892      * @param content the content to send
893      */
894     private void prepareRequestContent(final Object content) {
895         if (content != null
896             && (HttpMethod.POST == webRequest_.getHttpMethod()
897                     || HttpMethod.PUT == webRequest_.getHttpMethod()
898                     || HttpMethod.PATCH == webRequest_.getHttpMethod()
899                     || HttpMethod.DELETE == webRequest_.getHttpMethod()
900                     || HttpMethod.OPTIONS == webRequest_.getHttpMethod())
901             && !JavaScriptEngine.isUndefined(content)) {
902 
903             final boolean setEncodingType = webRequest_.getAdditionalHeader(HttpHeader.CONTENT_TYPE) == null;
904 
905             if (content instanceof HTMLDocument document) {
906                 String body = new XMLSerializer().serializeToString(document);
907                 if (LOG.isDebugEnabled()) {
908                     LOG.debug("Setting request body to: " + body);
909                 }
910 
911                 final Element docElement = ((Document) content).getDocumentElement();
912                 final SgmlPage page = docElement.getDomNodeOrDie().getPage();
913                 final DocumentType doctype = page.getDoctype();
914                 if (doctype != null && !StringUtils.isEmptyOrNull(doctype.getName())) {
915                     body = "<!DOCTYPE " + doctype.getName() + ">" + body;
916                 }
917 
918                 webRequest_.setRequestBody(body);
919                 if (setEncodingType) {
920                     webRequest_.setAdditionalHeader(HttpHeader.CONTENT_TYPE, "text/html;charset=UTF-8");
921                 }
922             }
923             else if (content instanceof XMLDocument xmlDocument) {
924                 // this output differs from real browsers but it seems to be a good starting point
925                 try (StringWriter writer = new StringWriter()) {
926 
927                     final Transformer transformer = TransformerFactory.newInstance().newTransformer();
928                     transformer.setOutputProperty(OutputKeys.METHOD, "xml");
929                     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
930                     transformer.setOutputProperty(OutputKeys.INDENT, "no");
931                     transformer.transform(
932                             new DOMSource(xmlDocument.getDomNodeOrDie().getFirstChild()), new StreamResult(writer));
933 
934                     final String body = writer.toString();
935                     if (LOG.isDebugEnabled()) {
936                         LOG.debug("Setting request body to: " + body);
937                     }
938                     webRequest_.setRequestBody(body);
939                     if (setEncodingType) {
940                         webRequest_.setAdditionalHeader(HttpHeader.CONTENT_TYPE,
941                                         MimeType.APPLICATION_XML + ";charset=UTF-8");
942                     }
943                 }
944                 catch (final Exception e) {
945                     throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
946                 }
947             }
948             else if (content instanceof FormData data) {
949                 data.fillRequest(webRequest_);
950             }
951             else if (content instanceof NativeArrayBufferView view) {
952                 webRequest_.setRequestBody(
953                         UTF_8.decode(
954                             view.getBuffer()
955                                 .getByteBuffer()
956                                 .position(view.getByteOffset())
957                                 .limit(view.getByteOffset() + view.getByteLength())
958                         ).toString());
959                 if (setEncodingType) {
960                     webRequest_.setEncodingType(null);
961                 }
962             }
963             else if (content instanceof URLSearchParams params) {
964                 params.fillRequest(webRequest_);
965                 webRequest_.addHint(HttpHint.IncludeCharsetInContentTypeHeader);
966             }
967             else if (content instanceof Blob blob) {
968                 blob.fillRequest(webRequest_);
969             }
970             else {
971                 final String body = JavaScriptEngine.toString(content);
972                 if (!body.isEmpty()) {
973                     if (LOG.isDebugEnabled()) {
974                         LOG.debug("Setting request body to: " + body);
975                     }
976                     webRequest_.setRequestBody(body);
977                     webRequest_.setCharset(UTF_8);
978                     if (setEncodingType) {
979                         webRequest_.setEncodingType(FormEncodingType.TEXT_PLAIN);
980                     }
981                 }
982             }
983         }
984     }
985 
986     /**
987      * Performs the actual send operation.
988      */
989     void doSend() {
990         final Window window = getWindow();
991         final WebWindow webWindow = window.getWebWindow();
992         final WebClient wc = webWindow.getWebClient();
993 
994         // accessing to local resource is forbidden for security reason
995         if (!wc.getOptions().isFileProtocolForXMLHttpRequestsAllowed()
996                 && "file".equals(webRequest_.getUrl().getProtocol())) {
997 
998             if (LOG.isDebugEnabled()) {
999                 LOG.debug("Not allowed to load local resource: " + webRequest_.getUrl());
1000             }
1001 
1002             if (async_) {
1003                 setState(DONE);
1004                 fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1005                 fireJavascriptEvent(Event.TYPE_ERROR);
1006                 fireJavascriptEvent(Event.TYPE_LOAD_END);
1007                 return;
1008             }
1009 
1010             throw JavaScriptEngine.asJavaScriptException(
1011                     window,
1012                     "Not allowed to load local resource: " + webRequest_.getUrl(),
1013                     DOMException.NETWORK_ERR);
1014         }
1015 
1016         final BrowserVersion browserVersion = getBrowserVersion();
1017         try {
1018             ensureConnectionAllowed(webWindow, webRequest_.getUrl());
1019 
1020             if (!isSameOrigin_ && isPreflight(webRequest_)) {
1021                 final WebResponse preflightResponse = sendPreflight(wc, webRequest_);
1022                 if (!preflightResponse.isSuccessOrUseProxyOrNotModified()
1023                         || !isPreflightAuthorized(webRequest_, preflightResponse)) {
1024                     setState(DONE);
1025                     if (async_ || browserVersion.hasFeature(XHR_HANDLE_SYNC_NETWORK_ERRORS)) {
1026                         fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1027                         fireJavascriptEvent(Event.TYPE_ERROR);
1028                         fireJavascriptEvent(Event.TYPE_LOAD_END);
1029                     }
1030 
1031                     if (LOG.isDebugEnabled()) {
1032                         LOG.debug("No permitted request for URL " + webRequest_.getUrl());
1033                     }
1034                     throw JavaScriptEngine.asJavaScriptException(
1035                             window,
1036                             "No permitted \"Access-Control-Allow-Origin\" header.",
1037                             DOMException.NETWORK_ERR);
1038                 }
1039             }
1040 
1041             // follows the redirects like the fetch standard describes it
1042             // and checks the CORS headers of all responses of a cross origin request
1043             webResponse_ = loadWebResponseFollowingRedirects(wc);
1044             LOG.debug("Web response loaded successfully.");
1045 
1046             if (overriddenMimeType_ != null) {
1047                 final int index = overriddenMimeType_.toLowerCase(Locale.ROOT).indexOf("charset=");
1048                 String charsetName = "";
1049                 if (index != -1) {
1050                     charsetName = overriddenMimeType_.substring(index + "charset=".length());
1051                 }
1052 
1053                 final String charsetNameFinal = charsetName;
1054                 final Charset charset;
1055                 if (XUserDefinedCharset.NAME.equalsIgnoreCase(charsetName)) {
1056                     charset = XUserDefinedCharset.INSTANCE;
1057                 }
1058                 else {
1059                     charset = EncodingSniffer.toCharset(charsetName);
1060                 }
1061                 webResponse_ = new WebResponseWrapper(webResponse_) {
1062                     @Override
1063                     public String getContentType() {
1064                         return overriddenMimeType_;
1065                     }
1066 
1067                     @Override
1068                     public Charset getContentCharset() {
1069                         if (charsetNameFinal.isEmpty() || charset == null) {
1070                             return super.getContentCharset();
1071                         }
1072                         return charset;
1073                     }
1074                 };
1075             }
1076 
1077             setState(HEADERS_RECEIVED);
1078             if (async_) {
1079                 fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1080 
1081                 setState(LOADING);
1082                 fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1083                 fireJavascriptEvent(Event.TYPE_PROGRESS);
1084             }
1085 
1086             setState(DONE);
1087             fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1088 
1089             if (!async_ && aborted_
1090                     && browserVersion.hasFeature(XHR_SEND_NETWORK_ERROR_IF_ABORTED)) {
1091                 throw JavaScriptEngine.constructError("Error",
1092                         "Failed to execute 'send' on 'XMLHttpRequest': Failed to load '" + webRequest_.getUrl() + "'");
1093             }
1094 
1095             if (browserVersion.hasFeature(XHR_LOAD_ALWAYS_AFTER_DONE)) {
1096                 fireJavascriptEventIgnoreAbort(Event.TYPE_LOAD);
1097                 fireJavascriptEventIgnoreAbort(Event.TYPE_LOAD_END);
1098             }
1099             else {
1100                 fireJavascriptEvent(Event.TYPE_LOAD);
1101                 fireJavascriptEvent(Event.TYPE_LOAD_END);
1102             }
1103         }
1104         catch (final IOException e) {
1105             LOG.debug("IOException: returning a network error response.", e);
1106 
1107             webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
1108             if (async_) {
1109                 setState(DONE);
1110                 fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1111                 if (e instanceof SocketTimeoutException) {
1112                     fireJavascriptEvent(Event.TYPE_TIMEOUT);
1113                 }
1114                 else {
1115                     fireJavascriptEvent(Event.TYPE_ERROR);
1116                 }
1117                 fireJavascriptEvent(Event.TYPE_LOAD_END);
1118             }
1119             else {
1120                 setState(DONE);
1121                 if (browserVersion.hasFeature(XHR_HANDLE_SYNC_NETWORK_ERRORS)) {
1122                     fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
1123                     if (e instanceof SocketTimeoutException) {
1124                         fireJavascriptEvent(Event.TYPE_TIMEOUT);
1125                     }
1126                     else {
1127                         fireJavascriptEvent(Event.TYPE_ERROR);
1128                     }
1129                     fireJavascriptEvent(Event.TYPE_LOAD_END);
1130                 }
1131 
1132                 throw JavaScriptEngine.asJavaScriptException(window,
1133                         e.getMessage(), DOMException.NETWORK_ERR);
1134             }
1135         }
1136     }
1137 
1138     /**
1139      * Checks the connect-src directive of the Content-Security-Policy of the page.
1140      *
1141      * @param webWindow the window
1142      * @param url the url to connect to (the request url or a redirect target)
1143      * @throws IOException if the connection is not allowed
1144      */
1145     private void ensureConnectionAllowed(final WebWindow webWindow, final URL url) throws IOException {
1146         if (webWindow != null
1147                 && webWindow.getEnclosedPage() instanceof HtmlPage page
1148                 && !ContentSecurityPolicy.allowsConnection(page, url, webRequest_.getUrl())) {
1149             throw new IOException("Refused to connect to '" + url
1150                     + "' because it violates the Content Security Policy of the page.");
1151         }
1152     }
1153 
1154     /**
1155      * Loads the response of the request and follows the redirects the way the
1156      * <a href="https://fetch.spec.whatwg.org/#http-redirect-fetch">fetch standard</a> describes it for a
1157      * request in cors mode:.
1158      * <ul>
1159      *   <li>only http and https locations are followed, not more than {@value #MAX_REDIRECTS} times</li>
1160      *   <li>as soon as a url of another origin is involved, all responses (also the ones that redirect)
1161      *       have to pass the CORS check and cookies are only sent if credentials are used</li>
1162      *   <li>the Origin header is the one of the page; after a redirect from one foreign origin to another
1163      *       one it is 'null'</li>
1164      *   <li>a redirect to a foreign url with credentials is a network error</li>
1165      *   <li>301/302 (for POST) and 303 switch to GET and drop the body; 307/308 keep method and body</li>
1166      *   <li>the Authorization header and the credentials of the request are dropped if the redirect
1167      *       leaves the origin</li>
1168      * </ul>
1169      *
1170      * @param wc the web client
1171      * @return the final response
1172      * @throws IOException in case of a network error
1173      */
1174     private WebResponse loadWebResponseFollowingRedirects(final WebClient wc) throws IOException {
1175         final URL pageUrl = webRequest_.getRequestingUrl();
1176         final String pageOrigin = serializeOrigin(pageUrl);
1177 
1178         WebRequest request = webRequest_;
1179         // the response tainting 'cors' of the fetch standard: as soon as an url of another origin is involved
1180         boolean corsTainting = !isSameOrigin_;
1181         // the origin of the request is 'null' after a redirect from one foreign origin to another one
1182         boolean taintedOrigin = false;
1183 
1184         for (int redirects = 0;; redirects++) {
1185             // the redirects are handled here, the web client has to return the response as it is
1186             request.addHint(HttpHint.DoNotFollowRedirects);
1187             if (corsTainting && !isWithCredentials()) {
1188                 // Cookies should not be sent for cross-origin requests when withCredentials is false
1189                 request.addHint(HttpHint.BlockCookies);
1190             }
1191 
1192             final WebResponse response = wc.loadWebResponse(request);
1193             final String originHeader = request.getAdditionalHeaders().get(HttpHeader.ORIGIN);
1194             final URL currentUrl = request.getUrl();
1195 
1196             URL nextUrl = wc.getOptions().isRedirectEnabled() ? getRedirectUrl(response, currentUrl) : null;
1197             // upgrade-insecure-requests of the Content-Security-Policy
1198             if (nextUrl != null && getWindow().getWebWindow().getEnclosedPage() instanceof HtmlPage page) {
1199                 nextUrl = ContentSecurityPolicy.upgradeInsecureRequest(page, nextUrl);
1200             }
1201 
1202             // each response of a cors request has to pass the check, also the one that redirects
1203             if (corsTainting && !isCorsAllowed(response, originHeader)) {
1204                 if (LOG.isDebugEnabled()) {
1205                     LOG.debug("No permitted \"Access-Control-Allow-Origin\" header for URL " + currentUrl);
1206                 }
1207                 throw new NoPermittedHeaderException("No permitted \"Access-Control-Allow-Origin\" header.");
1208             }
1209 
1210             if (nextUrl == null) {
1211                 return response;
1212             }
1213 
1214             if (redirects >= MAX_REDIRECTS) {
1215                 throw new RedirectException("Too many redirects for " + webRequest_.getUrl());
1216             }
1217 
1218             ensureConnectionAllowed(getWindow().getWebWindow(), nextUrl);
1219 
1220             final boolean crossOriginRedirect = !UrlUtils.isSameOrigin(currentUrl, nextUrl);
1221             final boolean foreignTarget = !UrlUtils.isSameOrigin(pageUrl, nextUrl);
1222             if (nextUrl.getUserInfo() != null && (corsTainting || foreignTarget)) {
1223                 throw new RedirectException("Redirect to a URL with credentials is not allowed: " + currentUrl);
1224             }
1225 
1226             if (crossOriginRedirect && !UrlUtils.isSameOrigin(pageUrl, currentUrl)) {
1227                 taintedOrigin = true;
1228             }
1229             corsTainting = corsTainting || foreignTarget;
1230 
1231             request = createRedirectRequest(request, response.getStatusCode(), nextUrl, crossOriginRedirect);
1232             setOriginHeader(request, corsTainting, taintedOrigin, pageOrigin);
1233 
1234             if (corsTainting && isPreflight(request)) {
1235                 final WebResponse preflightResponse = sendPreflight(wc, request);
1236                 if (!preflightResponse.isSuccessOrUseProxyOrNotModified()
1237                         || !isPreflightAuthorized(request, preflightResponse)) {
1238                     throw new NoPermittedHeaderException("No permitted \"Access-Control-Allow-Origin\" header.");
1239                 }
1240             }
1241         }
1242     }
1243 
1244     /**
1245      * Returns the url a response redirects to.
1246      *
1247      * @param response the response
1248      * @param currentUrl the url the response was loaded from
1249      * @return the url or {@code null} if the response is not a redirect (or has no location)
1250      * @throws IOException if the location is invalid or not a http(s) url
1251      */
1252     private static URL getRedirectUrl(final WebResponse response, final URL currentUrl) throws IOException {
1253         final int status = response.getStatusCode();
1254         if (status != 301 && status != 302 && status != 303 && status != 307 && status != 308) {
1255             return null;
1256         }
1257 
1258         String location = response.getResponseHeaderValue("Location");
1259         if (location == null) {
1260             return null;
1261         }
1262         location = new String(location.getBytes(ISO_8859_1), UTF_8);
1263 
1264         final URL url;
1265         try {
1266             url = WebClient.expandUrl(currentUrl, location);
1267         }
1268         catch (final MalformedURLException e) {
1269             throw new RedirectException("Invalid redirect location '" + location + "'", e);
1270         }
1271 
1272         final String protocol = url.getProtocol();
1273         if (!"http".equals(protocol) && !"https".equals(protocol)) {
1274             throw new RedirectException("Redirect to the unsupported scheme '" + protocol + "' is not allowed.");
1275         }
1276         return url;
1277     }
1278 
1279     /**
1280      * Creates the request for the target of a redirect.
1281      */
1282     private static WebRequest createRedirectRequest(final WebRequest request, final int status,
1283             final URL url, final boolean crossOriginRedirect) {
1284         final HttpMethod method = request.getHttpMethod();
1285         final boolean toGet = ((status == 301 || status == 302) && method == HttpMethod.POST)
1286                 || (status == 303 && method != HttpMethod.GET && method != HttpMethod.HEAD);
1287 
1288         final WebRequest next = new WebRequest(url, toGet ? HttpMethod.GET : method);
1289         next.setCharset(request.getCharset());
1290         next.setDefaultResponseContentCharset(request.getDefaultResponseContentCharset());
1291         next.setFetchDestination(request.getFetchDestination());
1292         next.setFetchModeOverride(request.getFetchModeOverride());
1293         next.setRequestingUrl(request.getRequestingUrl());
1294         next.setTimeout(request.getTimeout());
1295         if (request.hasHint(HttpHint.IncludeCharsetInContentTypeHeader)) {
1296             next.addHint(HttpHint.IncludeCharsetInContentTypeHeader);
1297         }
1298 
1299         for (final Entry<String, String> header : request.getAdditionalHeaders().entrySet()) {
1300             next.setAdditionalHeader(header.getKey(), header.getValue());
1301         }
1302 
1303         if (toGet) {
1304             // the body is gone, and the headers that describe it
1305             next.removeAdditionalHeader(HttpHeader.CONTENT_TYPE);
1306             next.removeAdditionalHeader("Content-Length");
1307             next.removeAdditionalHeader("Content-Encoding");
1308             next.removeAdditionalHeader("Content-Language");
1309             next.removeAdditionalHeader("Content-Location");
1310         }
1311         else if (request.getRequestBody() != null) {
1312             next.setRequestBody(request.getRequestBody());
1313             next.setEncodingType(request.getEncodingType());
1314         }
1315         else if (!request.getRequestParameters().isEmpty()) {
1316             next.setRequestParameters(request.getRequestParameters());
1317         }
1318 
1319         if (crossOriginRedirect) {
1320             // credentials are not forwarded to another origin
1321             next.removeAdditionalHeader("Authorization");
1322         }
1323         else {
1324             next.setCredentials(request.getCredentials());
1325         }
1326         return next;
1327     }
1328 
1329     /**
1330      * The Origin header is sent for requests of another origin and for all methods but GET and HEAD.
1331      */
1332     private static void setOriginHeader(final WebRequest request, final boolean corsTainting,
1333             final boolean taintedOrigin, final String pageOrigin) {
1334         final HttpMethod method = request.getHttpMethod();
1335         if (corsTainting || (method != HttpMethod.GET && method != HttpMethod.HEAD)) {
1336             request.setAdditionalHeader(HttpHeader.ORIGIN, taintedOrigin ? "null" : pageOrigin);
1337         }
1338         else {
1339             request.removeAdditionalHeader(HttpHeader.ORIGIN);
1340         }
1341     }
1342 
1343     private static String serializeOrigin(final URL url) {
1344         final StringBuilder origin = new StringBuilder().append(url.getProtocol()).append("://")
1345                 .append(url.getHost());
1346         if (url.getPort() != -1) {
1347             origin.append(':').append(url.getPort());
1348         }
1349         return origin.toString();
1350     }
1351 
1352     /**
1353      * The CORS check of the fetch standard.
1354      */
1355     private boolean isCorsAllowed(final WebResponse response, final String origin) {
1356         String value = response.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_ORIGIN);
1357         final boolean allowed = origin != null && origin.equals(value);
1358         if (isWithCredentials()) {
1359             // second step: check the allow-credentials header for true
1360             value = response.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_CREDENTIALS);
1361             return allowed && Boolean.parseBoolean(value);
1362         }
1363         return allowed || ALLOW_ORIGIN_ALL.equals(value);
1364     }
1365 
1366     /**
1367      * Sends the preflight request for the given request.
1368      */
1369     private WebResponse sendPreflight(final WebClient wc, final WebRequest request) throws IOException {
1370         final WebRequest preflightRequest = new WebRequest(request.getUrl(), HttpMethod.OPTIONS);
1371         preflightRequest.setEncodingType(null);
1372 
1373         // preflight request shouldn't have cookies
1374         preflightRequest.addHint(HttpHint.BlockCookies);
1375 
1376         // header origin
1377         final String originHeaderValue = request.getAdditionalHeaders().get(HttpHeader.ORIGIN);
1378         preflightRequest.setAdditionalHeader(HttpHeader.ORIGIN, originHeaderValue);
1379 
1380         // header request-method
1381         preflightRequest.setAdditionalHeader(
1382                 HttpHeader.ACCESS_CONTROL_REQUEST_METHOD,
1383                 request.getHttpMethod().name());
1384 
1385         // header request-headers
1386         final StringBuilder builder = new StringBuilder();
1387         for (final Entry<String, String> header
1388                 : new TreeMap<>(request.getAdditionalHeaders()).entrySet()) {
1389             final String name = org.htmlunit.util.StringUtils
1390                                     .toRootLowerCase(header.getKey());
1391             if (isPreflightHeader(name, header.getValue())) {
1392                 if (builder.length() != 0) {
1393                     builder.append(',');
1394                 }
1395                 builder.append(name);
1396             }
1397         }
1398         preflightRequest.setAdditionalHeader(HttpHeader.ACCESS_CONTROL_REQUEST_HEADERS, builder.toString());
1399         if (timeout_ > 0) {
1400             preflightRequest.setTimeout(timeout_);
1401         }
1402 
1403         if (getBrowserVersion().hasFeature(XHR_PREFLIGHT_CORS)) {
1404             // preflightRequest.setFetchModeOverride(FetchMode.CORS);
1405             preflightRequest.setAdditionalHeader(HttpHeader.SEC_FETCH_MODE, "cors");
1406         }
1407 
1408         final HtmlPage containingPage = (HtmlPage) getWindow().getWebWindow().getEnclosedPage();
1409         preflightRequest.setRefererHeader(containingPage.getUrl());
1410 
1411         // do the preflight request
1412         return wc.loadWebResponse(preflightRequest);
1413     }
1414 
1415     private static boolean isPreflight(final WebRequest request) {
1416         final HttpMethod method = request.getHttpMethod();
1417         if (method != HttpMethod.GET && method != HttpMethod.HEAD && method != HttpMethod.POST) {
1418             return true;
1419         }
1420         for (final Entry<String, String> header : request.getAdditionalHeaders().entrySet()) {
1421             if (isPreflightHeader(header.getKey().toLowerCase(Locale.ROOT), header.getValue())) {
1422                 return true;
1423             }
1424         }
1425         return false;
1426     }
1427 
1428     private boolean isPreflightAuthorized(final WebRequest request, final WebResponse preflightResponse) {
1429         // the preflight response has to pass the CORS check too; for a request with credentials
1430         // this requires the exact origin and Access-Control-Allow-Credentials
1431         if (!isCorsAllowed(preflightResponse, request.getAdditionalHeaders().get(HttpHeader.ORIGIN))) {
1432             return false;
1433         }
1434 
1435         // the wildcard is not supported for requests with credentials
1436         final boolean wildcard = !isWithCredentials();
1437 
1438         final HttpMethod method = request.getHttpMethod();
1439         if (method != HttpMethod.GET && method != HttpMethod.HEAD && method != HttpMethod.POST
1440                 && !isPreflightMethodAllowed(method.name(), preflightResponse, wildcard)) {
1441             return false;
1442         }
1443 
1444         // there is no test case for this because the servlet API has no support
1445         // for adding the same header twice
1446         final HashSet<String> accessControlValues = new HashSet<>();
1447         for (final NameValuePair pair : preflightResponse.getResponseHeaders()) {
1448             if (HttpHeader.ACCESS_CONTROL_ALLOW_HEADERS.equalsIgnoreCase(pair.getName())) {
1449                 String value = pair.getValue();
1450                 if (value != null) {
1451                     if (wildcard && ALLOW_ORIGIN_ALL.equals(value)) {
1452                         // all headers are allowed
1453                         return true;
1454                     }
1455                     value = org.htmlunit.util.StringUtils.toRootLowerCase(value);
1456                     final String[] values = org.htmlunit.util.StringUtils.splitAtComma(value);
1457                     for (String part : values) {
1458                         part = part.trim();
1459                         if (!org.htmlunit.util.StringUtils.isEmptyOrNull(part)) {
1460                             accessControlValues.add(part);
1461                         }
1462                     }
1463                 }
1464             }
1465         }
1466 
1467         for (final Entry<String, String> header : request.getAdditionalHeaders().entrySet()) {
1468             final String key = org.htmlunit.util.StringUtils.toRootLowerCase(header.getKey());
1469             if (isPreflightHeader(key, header.getValue())
1470                     && !accessControlValues.contains(key)) {
1471                 return false;
1472             }
1473         }
1474         return true;
1475     }
1476 
1477     /**
1478      * Checks whether the given method is listed in the Access-Control-Allow-Methods header
1479      * of the preflight response (the method names are case-sensitive).
1480      *
1481      * @param method the method name
1482      * @param preflightResponse the preflight response
1483      * @param wildcard whether the wildcard '*' allows all methods
1484      * @return {@code true} if the method is allowed
1485      */
1486     private static boolean isPreflightMethodAllowed(final String method, final WebResponse preflightResponse,
1487             final boolean wildcard) {
1488         for (final NameValuePair pair : preflightResponse.getResponseHeaders()) {
1489             if (HttpHeader.ACCESS_CONTROL_ALLOW_METHODS.equalsIgnoreCase(pair.getName())
1490                     && pair.getValue() != null) {
1491                 for (final String part : StringUtils.splitAtComma(pair.getValue())) {
1492                     final String allowed = part.trim();
1493                     if (method.equals(allowed) || (wildcard && ALLOW_ORIGIN_ALL.equals(allowed))) {
1494                         return true;
1495                     }
1496                 }
1497             }
1498         }
1499         return false;
1500     }
1501 
1502     /**
1503      * Checks whether the given header name requires a preflight request.
1504      *
1505      * @param name the header name (MUST be lower-case for performance reasons)
1506      * @param value the header value
1507      * @return {@code true} if this header requires a preflight
1508      */
1509     private static boolean isPreflightHeader(final String name, final String value) {
1510         if (HttpHeader.CONTENT_TYPE_LC.equals(name)) {
1511             final String lcValue = value.toLowerCase(Locale.ROOT);
1512             return !lcValue.startsWith(FormEncodingType.URL_ENCODED.getName())
1513                     && !lcValue.startsWith(FormEncodingType.MULTIPART.getName())
1514                     && !lcValue.startsWith(FormEncodingType.TEXT_PLAIN.getName());
1515         }
1516         return !(HttpHeader.ACCEPT_LC.equals(name)
1517                 || HttpHeader.ACCEPT_LANGUAGE_LC.equals(name)
1518                 || HttpHeader.CONTENT_LANGUAGE_LC.equals(name)
1519                 || HttpHeader.REFERER_LC.equals(name)
1520                 || "accept-encoding".equals(name)
1521                 || HttpHeader.ORIGIN_LC.equals(name));
1522     }
1523 
1524     /**
1525      * Sets the specified header to the specified value. The {@code open()} method must be
1526      * called before this method, or an error will occur.
1527      *
1528      * @param name the name of the header to set
1529      * @param value the value of the header to set
1530      */
1531     @JsxFunction
1532     public void setRequestHeader(final String name, final String value) {
1533         if (!isAuthorizedHeader(name)) {
1534             if (LOG.isWarnEnabled()) {
1535                 LOG.warn("Ignoring XMLHttpRequest.setRequestHeader for " + name
1536                     + ": it is a restricted header");
1537             }
1538             return;
1539         }
1540 
1541         if (webRequest_ != null) {
1542             webRequest_.setAdditionalHeader(name, value);
1543         }
1544         else {
1545             throw JavaScriptEngine.asJavaScriptException(
1546                     getWindow(),
1547                     "The open() method must be called before setRequestHeader().",
1548                     DOMException.INVALID_STATE_ERR);
1549         }
1550     }
1551 
1552     /**
1553      * Checks whether the specified request header may be set from JavaScript.
1554      *
1555      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader">MDN Documentation</a>
1556      * @param name the header name
1557      * @return {@code true} if the header can be set from JavaScript
1558      */
1559     static boolean isAuthorizedHeader(final String name) {
1560         final String nameLowerCase = org.htmlunit.util.StringUtils.toRootLowerCase(name);
1561         if (PROHIBITED_HEADERS_.contains(nameLowerCase)) {
1562             return false;
1563         }
1564         return !nameLowerCase.startsWith("proxy-") && !nameLowerCase.startsWith("sec-");
1565     }
1566 
1567     /**
1568      * Sets the attribution reporting options of the request
1569      * (<a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setAttributionReporting">MDN</a>).
1570      * <p>
1571      * The attribution reporting itself is not supported, so this only validates the argument.
1572      * </p>
1573      *
1574      * @param options the options ({@code eventSourceEligible} and {@code triggerEligible})
1575      */
1576     @JsxFunction({CHROME, EDGE})
1577     public void setAttributionReporting(final Object options) {
1578         if (!(options instanceof ScriptableObject)) {
1579             throw JavaScriptEngine.typeError("Failed to execute 'setAttributionReporting' on 'XMLHttpRequest': "
1580                     + "parameter 1 is not of type 'AttributionReportingRequestOptions'.");
1581         }
1582     }
1583 
1584     /**
1585      * Sets the private state token options of the request
1586      * (<a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setPrivateToken">MDN</a>).
1587      * <p>
1588      * Private state tokens are not supported, so this only validates the argument.
1589      * </p>
1590      *
1591      * @param options the options ({@code version}, {@code operation}, ...)
1592      */
1593     @JsxFunction({CHROME, EDGE})
1594     public void setPrivateToken(final Object options) {
1595         if (!(options instanceof ScriptableObject)) {
1596             throw JavaScriptEngine.typeError("Failed to execute 'setPrivateToken' on 'XMLHttpRequest': "
1597                     + "parameter 1 is not of type 'PrivateToken'.");
1598         }
1599     }
1600 
1601     /**
1602      * Overrides the MIME type returned by the server. This must be called before {@code send()}.
1603      * This may be used, for example, to force a stream to be treated and parsed as {@code text/xml},
1604      * even if the server does not report it as such.
1605      *
1606      * @param mimeType the MIME type to use instead of the one returned by the server
1607      */
1608     @JsxFunction
1609     public void overrideMimeType(final String mimeType) {
1610         if (state_ != UNSENT && state_ != OPENED) {
1611             throw JavaScriptEngine.asJavaScriptException(
1612                     getWindow(),
1613                     "Property 'overrideMimeType' not writable after sent.",
1614                     DOMException.INVALID_STATE_ERR);
1615         }
1616         overriddenMimeType_ = mimeType;
1617     }
1618 
1619     /**
1620      * Returns the {@code withCredentials} property.
1621      *
1622      * @return the {@code withCredentials} property
1623      */
1624     @JsxGetter
1625     public boolean isWithCredentials() {
1626         return withCredentials_;
1627     }
1628 
1629     /**
1630      * Sets the {@code withCredentials} property.
1631      *
1632      * @param withCredentials the {@code withCredentials} property
1633      */
1634     @JsxSetter
1635     public void setWithCredentials(final boolean withCredentials) {
1636         withCredentials_ = withCredentials;
1637     }
1638 
1639     /**
1640      * Returns the {@code mozAnon} property; a normal web page can't create an anonymous request,
1641      * therefore this is always {@code false}.
1642      *
1643      * @return {@code false}
1644      */
1645     @JsxGetter({FF, FF_ESR})
1646     public boolean isMozAnon() {
1647         return false;
1648     }
1649 
1650     /**
1651      * Returns the {@code mozSystem} property; only privileged code can create a system request,
1652      * therefore this is always {@code false}.
1653      *
1654      * @return {@code false}
1655      */
1656     @JsxGetter({FF, FF_ESR})
1657     public boolean isMozSystem() {
1658         return false;
1659     }
1660 
1661     /**
1662      * Returns the {@code upload} property.
1663      *
1664      * @return the {@code upload} property
1665      */
1666     @JsxGetter
1667     public XMLHttpRequestUpload getUpload() {
1668         if (upload_ != null) {
1669             return upload_;
1670         }
1671 
1672         final XMLHttpRequestUpload upload = new XMLHttpRequestUpload();
1673         upload.setParentScope(getParentScope());
1674         upload.setPrototype(getPrototype(upload.getClass()));
1675 
1676         upload_ = upload;
1677         return upload_;
1678     }
1679 
1680     /**
1681      * {@inheritDoc}
1682      */
1683     @JsxGetter
1684     @Override
1685     public Function getOnreadystatechange() {
1686         return super.getOnreadystatechange();
1687     }
1688 
1689     /**
1690      * {@inheritDoc}
1691      */
1692     @JsxSetter
1693     @Override
1694     public void setOnreadystatechange(final Function readyStateChangeHandler) {
1695         super.setOnreadystatechange(readyStateChangeHandler);
1696     }
1697 
1698     /**
1699      * Returns the number of milliseconds a request can take before automatically being terminated.
1700      * A value of {@code 0} means there is no timeout.
1701      *
1702      * @return the timeout in milliseconds
1703      */
1704     @JsxGetter
1705     public int getTimeout() {
1706         return timeout_;
1707     }
1708 
1709     /**
1710      * Sets the number of milliseconds a request can take before automatically being terminated.
1711      *
1712      * @param timeout the timeout in milliseconds
1713      */
1714     @JsxSetter
1715     public void setTimeout(final int timeout) {
1716         timeout_ = timeout;
1717     }
1718 
1719     private static final class NetworkErrorWebResponse extends WebResponse {
1720         private final WebRequest request_;
1721         private final IOException error_;
1722 
1723         NetworkErrorWebResponse(final WebRequest webRequest, final IOException error) {
1724             super(null, null, 0);
1725             request_ = webRequest;
1726             error_ = error;
1727         }
1728 
1729         @Override
1730         public int getStatusCode() {
1731             return 0;
1732         }
1733 
1734         @Override
1735         public String getStatusMessage() {
1736             return "";
1737         }
1738 
1739         @Override
1740         public String getContentType() {
1741             return "";
1742         }
1743 
1744         @Override
1745         public String getContentAsString() {
1746             return "";
1747         }
1748 
1749         @Override
1750         public InputStream getContentAsStream() {
1751             return null;
1752         }
1753 
1754         @Override
1755         public List<NameValuePair> getResponseHeaders() {
1756             return Collections.emptyList();
1757         }
1758 
1759         @Override
1760         public String getResponseHeaderValue(final String headerName) {
1761             return "";
1762         }
1763 
1764         @Override
1765         public long getLoadTime() {
1766             return 0;
1767         }
1768 
1769         @Override
1770         public Charset getContentCharset() {
1771             return null;
1772         }
1773 
1774         @Override
1775         public WebRequest getWebRequest() {
1776             return request_;
1777         }
1778 
1779         /**
1780          * Returns the I/O error that caused this network error response.
1781          *
1782          * @return the error
1783          */
1784         public IOException getError() {
1785             return error_;
1786         }
1787     }
1788 
1789     private static final class NoPermittedHeaderException extends IOException {
1790         NoPermittedHeaderException(final String msg) {
1791             super(msg);
1792         }
1793     }
1794 
1795     /**
1796      * A redirect that is not allowed (or not possible); this is a network error, but
1797      * like for the CORS errors the browsers do not hide the (empty) responseText.
1798      */
1799     private static final class RedirectException extends IOException {
1800         RedirectException(final String msg) {
1801             super(msg);
1802         }
1803 
1804         RedirectException(final String msg, final Throwable cause) {
1805             super(msg, cause);
1806         }
1807     }
1808 }