#include "third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/auto_reset.h"
#include "base/containers/contains.h"
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/numerics/safe_conversions.h"
#include "base/time/time.h"
#include "net/base/mime_util.h"
#include "services/network/public/cpp/header_util.h"
#include "services/network/public/mojom/trust_tokens.mojom-shared.h"
#include "third_party/blink/public/common/blob/blob_utils.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom-blink.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_private_token.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_arraybuffer_arraybufferview_blob_document_formdata_urlsearchparams_usvstring.h"
#include "third_party/blink/renderer/core/dom/document_init.h"
#include "third_party/blink/renderer/core/dom/document_parser.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/dom/xml_document.h"
#include "third_party/blink/renderer/core/editing/serializers/serialization.h"
#include "third_party/blink/renderer/core/event_target_names.h"
#include "third_party/blink/renderer/core/events/progress_event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/fetch/attribution_reporting_to_mojom.h"
#include "third_party/blink/renderer/core/fetch/trust_token_to_mojom.h"
#include "third_party/blink/renderer/core/fileapi/blob.h"
#include "third_party/blink/renderer/core/fileapi/file.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_client.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_loader.h"
#include "third_party/blink/renderer/core/fileapi/public_url_manager.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/deprecation/deprecation.h"
#include "third_party/blink/renderer/core/frame/frame.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/page_dismissal_scope.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/html/forms/form_data.h"
#include "third_party/blink/renderer/core/html/html_document.h"
#include "third_party/blink/renderer/core/html/parser/text_resource_decoder.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/core/loader/threadable_loader.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
#include "third_party/blink/renderer/core/url/url_search_params.h"
#include "third_party/blink/renderer/core/xmlhttprequest/xml_http_request_upload.h"
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/blob/blob_data.h"
#include "third_party/blink/renderer/platform/file_metadata.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/loader/cors/cors.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_type_names.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_utils.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_error.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/loader/fetch/text_resource_decoder_options.h"
#include "third_party/blink/renderer/platform/network/http_names.h"
#include "third_party/blink/renderer/platform/network/http_parsers.h"
#include "third_party/blink/renderer/platform/network/mime/mime_type_registry.h"
#include "third_party/blink/renderer/platform/network/parsed_content_type.h"
#include "third_party/blink/renderer/platform/scheduler/public/task_attribution_tracker.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/weborigin/security_policy.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
namespace {
void FindCharsetInMediaType(const String& media_type,
unsigned& charset_pos,
unsigned& charset_len) { … }
String ExtractCharsetFromMediaType(const String& media_type) { … }
void ReplaceCharsetInMediaType(String& media_type,
const String& charset_value) { … }
void LogConsoleError(ExecutionContext* context, const String& message) { … }
bool ValidateOpenArguments(const AtomicString& method,
const KURL& url,
ExceptionState& exception_state) { … }
}
class XMLHttpRequest::BlobLoader final
: public GarbageCollected<XMLHttpRequest::BlobLoader>,
public FileReaderClient { … };
XMLHttpRequest* XMLHttpRequest::Create(ScriptState* script_state) { … }
XMLHttpRequest* XMLHttpRequest::Create(ExecutionContext* context) { … }
XMLHttpRequest::XMLHttpRequest(ExecutionContext* context,
const DOMWrapperWorld* world)
: … { … }
XMLHttpRequest::~XMLHttpRequest() { … }
XMLHttpRequest::State XMLHttpRequest::readyState() const { … }
String XMLHttpRequest::responseText(ExceptionState& exception_state) { … }
void XMLHttpRequest::InitResponseDocument() { … }
Document* XMLHttpRequest::responseXML(ExceptionState& exception_state) { … }
v8::Local<v8::Value> XMLHttpRequest::ResponseJSON(
v8::Isolate* isolate,
ExceptionState& exception_state) { … }
Blob* XMLHttpRequest::ResponseBlob() { … }
DOMArrayBuffer* XMLHttpRequest::ResponseArrayBuffer() { … }
ScriptValue XMLHttpRequest::response(ScriptState* script_state,
ExceptionState& exception_state) { … }
void XMLHttpRequest::setTimeout(unsigned timeout,
ExceptionState& exception_state) { … }
void XMLHttpRequest::setResponseType(const String& response_type,
ExceptionState& exception_state) { … }
String XMLHttpRequest::responseType() { … }
String XMLHttpRequest::responseURL() { … }
XMLHttpRequestUpload* XMLHttpRequest::upload() { … }
void XMLHttpRequest::TrackProgress(uint64_t length) { … }
void XMLHttpRequest::ChangeState(State new_state) { … }
void XMLHttpRequest::DispatchReadyStateChangeEvent() { … }
void XMLHttpRequest::setWithCredentials(bool value,
ExceptionState& exception_state) { … }
void XMLHttpRequest::open(const AtomicString& method,
const String& url_string,
ExceptionState& exception_state) { … }
void XMLHttpRequest::open(const AtomicString& method,
const String& url_string,
bool async,
const String& username,
const String& password,
ExceptionState& exception_state) { … }
void XMLHttpRequest::open(const AtomicString& method,
const KURL& url,
bool async,
ExceptionState& exception_state) { … }
bool XMLHttpRequest::InitSend(ExceptionState& exception_state) { … }
void XMLHttpRequest::send(const V8UnionDocumentOrXMLHttpRequestBodyInit* body,
ExceptionState& exception_state) { … }
bool XMLHttpRequest::AreMethodAndURLValidForSend() { … }
void XMLHttpRequest::send(Document* document, ExceptionState& exception_state) { … }
void XMLHttpRequest::send(const String& body, ExceptionState& exception_state) { … }
void XMLHttpRequest::send(Blob* body, ExceptionState& exception_state) { … }
void XMLHttpRequest::send(FormData* body, ExceptionState& exception_state) { … }
void XMLHttpRequest::send(URLSearchParams* body,
ExceptionState& exception_state) { … }
void XMLHttpRequest::send(DOMArrayBuffer* body,
ExceptionState& exception_state) { … }
void XMLHttpRequest::send(DOMArrayBufferView* body,
ExceptionState& exception_state) { … }
void XMLHttpRequest::SendBytesData(const void* data,
size_t length,
ExceptionState& exception_state) { … }
void XMLHttpRequest::SendForInspectorXHRReplay(
scoped_refptr<EncodedFormData> form_data,
ExceptionState& exception_state) { … }
void XMLHttpRequest::ThrowForLoadFailureIfNeeded(
ExceptionState& exception_state,
const String& reason) { … }
void XMLHttpRequest::CreateRequest(scoped_refptr<EncodedFormData> http_body,
ExceptionState& exception_state) { … }
void XMLHttpRequest::abort() { … }
void XMLHttpRequest::Dispose() { … }
void XMLHttpRequest::ClearVariablesForLoading() { … }
void XMLHttpRequest::InternalAbort() { … }
void XMLHttpRequest::ClearResponse() { … }
void XMLHttpRequest::ClearRequest() { … }
void XMLHttpRequest::DispatchProgressEvent(const AtomicString& type,
int64_t received_length,
int64_t expected_length) { … }
void XMLHttpRequest::DispatchProgressEventFromSnapshot(
const AtomicString& type) { … }
void XMLHttpRequest::HandleNetworkError() { … }
void XMLHttpRequest::HandleDidCancel() { … }
void XMLHttpRequest::HandleRequestError(DOMExceptionCode exception_code,
const AtomicString& type) { … }
void XMLHttpRequest::overrideMimeType(const AtomicString& mime_type,
ExceptionState& exception_state) { … }
void XMLHttpRequest::setRequestHeader(const AtomicString& name,
const AtomicString& value,
ExceptionState& exception_state) { … }
void XMLHttpRequest::SetRequestHeaderInternal(const AtomicString& name,
const AtomicString& value) { … }
void XMLHttpRequest::setPrivateToken(const PrivateToken* trust_token,
ExceptionState& exception_state) { … }
void XMLHttpRequest::setAttributionReporting(
const AttributionReportingRequestOptions* options,
ExceptionState& exception_state) { … }
bool XMLHttpRequest::HasContentTypeRequestHeader() const { … }
String XMLHttpRequest::getAllResponseHeaders() const { … }
const AtomicString& XMLHttpRequest::getResponseHeader(
const AtomicString& name) const { … }
AtomicString XMLHttpRequest::FinalResponseMIMETypeInternal() const { … }
AtomicString XMLHttpRequest::GetResponseMIMEType() const { … }
WTF::TextEncoding XMLHttpRequest::FinalResponseCharset() const { … }
void XMLHttpRequest::UpdateContentTypeAndCharset(
const AtomicString& default_content_type,
const String& charset) { … }
bool XMLHttpRequest::ResponseIsXML() const { … }
bool XMLHttpRequest::ResponseIsHTML() const { … }
int XMLHttpRequest::status() const { … }
String XMLHttpRequest::statusText() const { … }
void XMLHttpRequest::DidFail(uint64_t, const ResourceError& error) { … }
void XMLHttpRequest::DidFailRedirectCheck(uint64_t) { … }
void XMLHttpRequest::DidFinishLoading(uint64_t identifier) { … }
void XMLHttpRequest::DidFinishLoadingInternal() { … }
void XMLHttpRequest::DidFinishLoadingFromBlob() { … }
void XMLHttpRequest::DidFailLoadingFromBlob() { … }
void XMLHttpRequest::NotifyParserStopped() { … }
void XMLHttpRequest::EndLoading() { … }
void XMLHttpRequest::DidSendData(uint64_t bytes_sent,
uint64_t total_bytes_to_be_sent) { … }
void XMLHttpRequest::DidReceiveResponse(uint64_t identifier,
const ResourceResponse& response) { … }
void XMLHttpRequest::ParseDocumentChunk(base::span<const uint8_t> data) { … }
std::unique_ptr<TextResourceDecoder> XMLHttpRequest::CreateDecoder() const { … }
void XMLHttpRequest::DidReceiveData(base::span<const char> data) { … }
void XMLHttpRequest::DidDownloadData(uint64_t data_length) { … }
void XMLHttpRequest::DidDownloadToBlob(scoped_refptr<BlobDataHandle> blob) { … }
void XMLHttpRequest::HandleDidTimeout() { … }
void XMLHttpRequest::ContextDestroyed() { … }
bool XMLHttpRequest::HasPendingActivity() const { … }
const AtomicString& XMLHttpRequest::InterfaceName() const { … }
ExecutionContext* XMLHttpRequest::GetExecutionContext() const { … }
void XMLHttpRequest::ReportMemoryUsageToV8() { … }
void XMLHttpRequest::Trace(Visitor* visitor) const { … }
bool XMLHttpRequest::HasRequestHeaderForTesting(AtomicString name) const { … }
std::optional<scheduler::TaskAttributionTracker::TaskScope>
XMLHttpRequest::MaybeCreateTaskAttributionScope() { … }
std::ostream& operator<<(std::ostream& ostream, const XMLHttpRequest* xhr) { … }
}