فهرست منبع

LibURL: Make percent_encode return a String

This simplifies a bunch of places which were needing to error check and
convert from a ByteString to String.
Shannon Booth 1 سال پیش
والد
کامیت
84a7fead0e

+ 1 - 1
Userland/Libraries/LibURL/Parser.cpp

@@ -75,7 +75,7 @@ static Optional<Host> parse_opaque_host(StringView input)
     //       currently report validation errors, they are only useful for debugging efforts in the URL parsing code.
 
     // 4. Return the result of running UTF-8 percent-encode on input using the C0 control percent-encode set.
-    return String::from_byte_string(percent_encode(input, PercentEncodeSet::C0Control)).release_value_but_fixme_should_propagate_errors();
+    return percent_encode(input, PercentEncodeSet::C0Control);
 }
 
 struct ParsedIPv4Number {

+ 8 - 10
Userland/Libraries/LibURL/URL.cpp

@@ -59,21 +59,19 @@ void URL::set_scheme(String scheme)
 }
 
 // https://url.spec.whatwg.org/#set-the-username
-ErrorOr<void> URL::set_username(StringView username)
+void URL::set_username(StringView username)
 {
     // To set the username given a url and username, set url’s username to the result of running UTF-8 percent-encode on username using the userinfo percent-encode set.
-    m_data->username = TRY(String::from_byte_string(percent_encode(username, PercentEncodeSet::Userinfo)));
+    m_data->username = percent_encode(username, PercentEncodeSet::Userinfo);
     m_data->valid = compute_validity();
-    return {};
 }
 
 // https://url.spec.whatwg.org/#set-the-password
-ErrorOr<void> URL::set_password(StringView password)
+void URL::set_password(StringView password)
 {
     // To set the password given a url and password, set url’s password to the result of running UTF-8 percent-encode on password using the userinfo percent-encode set.
-    m_data->password = TRY(String::from_byte_string(percent_encode(password, PercentEncodeSet::Userinfo)));
+    m_data->password = percent_encode(password, PercentEncodeSet::Userinfo);
     m_data->valid = compute_validity();
-    return {};
 }
 
 void URL::set_host(Host host)
@@ -103,13 +101,13 @@ void URL::set_paths(Vector<ByteString> const& paths)
     m_data->paths.clear_with_capacity();
     m_data->paths.ensure_capacity(paths.size());
     for (auto const& segment : paths)
-        m_data->paths.unchecked_append(String::from_byte_string(percent_encode(segment, PercentEncodeSet::Path)).release_value_but_fixme_should_propagate_errors());
+        m_data->paths.unchecked_append(percent_encode(segment, PercentEncodeSet::Path));
     m_data->valid = compute_validity();
 }
 
 void URL::append_path(StringView path)
 {
-    m_data->paths.append(String::from_byte_string(percent_encode(path, PercentEncodeSet::Path)).release_value_but_fixme_should_propagate_errors());
+    m_data->paths.append(percent_encode(path, PercentEncodeSet::Path));
 }
 
 // https://url.spec.whatwg.org/#cannot-have-a-username-password-port
@@ -444,7 +442,7 @@ void append_percent_encoded_if_necessary(StringBuilder& builder, u32 code_point,
         builder.append_code_point(code_point);
 }
 
-ByteString percent_encode(StringView input, PercentEncodeSet set, SpaceAsPlus space_as_plus)
+String percent_encode(StringView input, PercentEncodeSet set, SpaceAsPlus space_as_plus)
 {
     StringBuilder builder;
     for (auto code_point : Utf8View(input)) {
@@ -453,7 +451,7 @@ ByteString percent_encode(StringView input, PercentEncodeSet set, SpaceAsPlus sp
         else
             append_percent_encoded_if_necessary(builder, code_point, set);
     }
-    return builder.to_byte_string();
+    return MUST(builder.to_string());
 }
 
 ByteString percent_decode(StringView input)

+ 3 - 3
Userland/Libraries/LibURL/URL.h

@@ -70,7 +70,7 @@ enum class SpaceAsPlus {
     No,
     Yes,
 };
-ByteString percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
+String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
 ByteString percent_decode(StringView input);
 
 template<typename T>
@@ -140,8 +140,8 @@ public:
     bool is_special() const { return is_special_scheme(m_data->scheme); }
 
     void set_scheme(String);
-    ErrorOr<void> set_username(StringView);
-    ErrorOr<void> set_password(StringView);
+    void set_username(StringView);
+    void set_password(StringView);
     void set_host(Host);
     void set_port(Optional<u16>);
     void set_paths(Vector<ByteString> const&);

+ 2 - 2
Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp

@@ -260,7 +260,7 @@ void DOMURL::set_username(String const& username)
         return;
 
     // 2. Set the username given this’s URL and the given value.
-    MUST(m_url.set_username(username));
+    m_url.set_username(username);
 }
 
 // https://url.spec.whatwg.org/#dom-url-password
@@ -278,7 +278,7 @@ void DOMURL::set_password(String const& password)
         return;
 
     // 2. Set the password given this’s URL and the given value.
-    MUST(m_url.set_password(password));
+    m_url.set_password(password);
 }
 
 // https://url.spec.whatwg.org/#dom-url-host

+ 2 - 2
Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp

@@ -2024,10 +2024,10 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fet
                 auto password = ByteString::empty();
 
                 // 3. Set the username given request’s current URL and username.
-                MUST(request->current_url().set_username(username));
+                request->current_url().set_username(username);
 
                 // 4. Set the password given request’s current URL and password.
-                MUST(request->current_url().set_password(password));
+                request->current_url().set_password(password);
             }
 
             // 4. Set response to the result of running HTTP-network-or-cache fetch given fetchParams and true.

+ 1 - 2
Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp

@@ -845,8 +845,7 @@ ErrorOr<void> HTMLFormElement::mail_as_body(URL::URL parsed_action, Vector<XHR::
         // 2. Set body to the result of running UTF-8 percent-encode on body using the default encode set. [URL]
         // NOTE: body is already UTF-8 encoded due to using AK::String, so we only have to do the percent encoding.
         // NOTE: "default encode set" links to "path percent-encode-set": https://url.spec.whatwg.org/#default-encode-set
-        auto percent_encoded_body = URL::percent_encode(body, URL::PercentEncodeSet::Path);
-        body = TRY(String::from_utf8(percent_encoded_body.view()));
+        body = URL::percent_encode(body, URL::PercentEncodeSet::Path);
         break;
     }
     default:

+ 2 - 2
Userland/Libraries/LibWeb/HTML/HTMLHyperlinkElementUtils.cpp

@@ -114,7 +114,7 @@ void HTMLHyperlinkElementUtils::set_username(StringView username)
         return;
 
     // 4. Set the username given this’s URL and the given value.
-    MUST(url->set_username(username));
+    url->set_username(username);
 
     // 5. Update href.
     update_href();
@@ -151,7 +151,7 @@ void HTMLHyperlinkElementUtils::set_password(StringView password)
         return;
 
     // 4. Set the password, given url and the given value.
-    MUST(url->set_password(password));
+    url->set_password(password);
 
     // 5. Update href.
     update_href();

+ 2 - 2
Userland/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp

@@ -210,10 +210,10 @@ Optional<URL::URL> strip_url_for_use_as_referrer(Optional<URL::URL> url, OriginO
         return {};
 
     // 3. Set url’s username to the empty string.
-    MUST(url->set_username(""sv));
+    url->set_username(""sv);
 
     // 4. Set url’s password to the empty string.
-    MUST(url->set_password(""sv));
+    url->set_password(""sv);
 
     // 5. Set url’s fragment to null.
     url->set_fragment({});

+ 2 - 2
Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp

@@ -495,10 +495,10 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method_string, Stri
     if (!parsed_url.host().has<Empty>()) {
         // 1. If the username argument is not null, set the username given parsedURL and username.
         if (username.has_value())
-            MUST(parsed_url.set_username(username.value()));
+            parsed_url.set_username(username.value());
         // 2. If the password argument is not null, set the password given parsedURL and password.
         if (password.has_value())
-            MUST(parsed_url.set_password(password.value()));
+            parsed_url.set_password(password.value());
     }
 
     // 9. If async is false, the current global object is a Window object, and either this’s timeout is