/* * Copyright (c) 2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace WebView { Optional sanitize_url(StringView url, Optional search_engine, AppendTLD append_tld) { if (FileSystem::exists(url)) { auto path = FileSystem::real_path(url); if (path.is_error()) return {}; return URL::create_with_file_scheme(path.value().to_deprecated_string()); } auto format_search_engine = [&]() -> Optional { if (!search_engine.has_value()) return {}; return MUST(String::formatted(*search_engine, URL::percent_decode(url))); }; String url_buffer; if (append_tld == AppendTLD::Yes) { // FIXME: Expand the list of top level domains. if (!url.ends_with(".com"sv) && !url.ends_with(".net"sv) && !url.ends_with(".org"sv)) { url_buffer = MUST(String::formatted("{}.com", url)); url = url_buffer; } } auto result = PublicSuffix::absolute_url(url); if (result.is_error()) return format_search_engine(); return result.release_value(); } }