From d232f3fc41b5aefd99515b6d4d0140a9d04cde66 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 11 Jul 2022 21:50:45 +0100 Subject: [PATCH] LibWeb: Use newly added 'Methods' Fetch infrastructure in XMLHttpRequest --- .../Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 7eca4a9467..d216097af7 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling - * Copyright (c) 2021, Linus Groh + * Copyright (c) 2021-2022, Linus Groh * Copyright (c) 2022, Luke Wilde * Copyright (c) 2022, Ali Mohammad Pur * Copyright (c) 2022, Kenneth Myhra @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -379,20 +380,6 @@ static bool is_forbidden_header_name(String const& header_name) return lowercase_header_name.is_one_of("accept-charset", "accept-encoding", "access-control-request-headers", "access-control-request-method", "connection", "content-length", "cookie", "cookie2", "date", "dnt", "expect", "host", "keep-alive", "origin", "referer", "te", "trailer", "transfer-encoding", "upgrade", "via"); } -// https://fetch.spec.whatwg.org/#forbidden-method -static bool is_forbidden_method(String const& method) -{ - auto lowercase_method = method.to_lowercase(); - return lowercase_method.is_one_of("connect", "trace", "track"); -} - -// https://fetch.spec.whatwg.org/#concept-method -static bool is_method(String const& method) -{ - Regex regex { R"~~~(^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$)~~~" }; - return regex.has_match(method); -} - // https://fetch.spec.whatwg.org/#header-name static bool is_header_name(String const& header_name) { @@ -400,15 +387,6 @@ static bool is_header_name(String const& header_name) return regex.has_match(header_name); } -// https://fetch.spec.whatwg.org/#concept-method-normalize -static String normalize_method(String const& method) -{ - auto lowercase_method = method.to_lowercase(); - if (lowercase_method.is_one_of("delete", "get", "head", "options", "post", "put")) - return method.to_uppercase(); - return method; -} - // https://fetch.spec.whatwg.org/#concept-header-value-normalize static String normalize_header_value(String const& header_value) { @@ -480,14 +458,16 @@ DOM::ExceptionOr XMLHttpRequest::set_request_header(String const& name, St } // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open -DOM::ExceptionOr XMLHttpRequest::open(String const& method, String const& url) +DOM::ExceptionOr XMLHttpRequest::open(String const& method_string, String const& url) { // 8. If the async argument is omitted, set async to true, and set username and password to null. - return open(method, url, true, {}, {}); + return open(method_string, url, true, {}, {}); } -DOM::ExceptionOr XMLHttpRequest::open(String const& method, String const& url, bool async, String const& username, String const& password) +DOM::ExceptionOr XMLHttpRequest::open(String const& method_string, String const& url, bool async, String const& username, String const& password) { + auto method = method_string.to_byte_buffer(); + // 1. Let settingsObject be this’s relevant settings object. auto& settings_object = m_window->associated_document().relevant_settings_object(); @@ -496,15 +476,15 @@ DOM::ExceptionOr XMLHttpRequest::open(String const& method, String const& return DOM::InvalidStateError::create("Invalid state: Responsible document is not fully active."); // 3. If method is not a method, then throw a "SyntaxError" DOMException. - if (!is_method(method)) + if (!Fetch::is_method(method)) return DOM::SyntaxError::create("An invalid or illegal string was specified."); // 4. If method is a forbidden method, then throw a "SecurityError" DOMException. - if (is_forbidden_method(method)) + if (Fetch::is_forbidden_method(method)) return DOM::SecurityError::create("Forbidden method, must not be 'CONNECT', 'TRACE', or 'TRACK'"); // 5. Normalize method. - auto normalized_method = normalize_method(method); + method = MUST(Fetch::normalize_method(method)); // 6. Let parsedURL be the result of parsing url with settingsObject’s API base URL and settingsObject’s API URL character encoding. auto parsed_url = settings_object.api_base_url().complete_url(url); @@ -537,7 +517,7 @@ DOM::ExceptionOr XMLHttpRequest::open(String const& method, String const& // Unset this’s upload listener flag. m_upload_listener = false; // Set this’s request method to method. - m_method = normalized_method; + m_method = move(method); // Set this’s request URL to parsedURL. m_url = parsed_url; // Set this’s synchronous flag if async is false; otherwise unset this’s synchronous flag.