From 0aeff9c0c45c086f78af099a7e07a06431347589 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sun, 19 Apr 2020 11:55:59 +0300 Subject: [PATCH] AK: Add URL::create_with_url_or_path() This is an utility to create a URL from a given string, which may be either a URL such as http://example.com (which will be used as-is), or a file path such as /etc/fstab (which will be transformed into file:///etc/fstab). --- AK/URL.cpp | 10 ++++++++++ AK/URL.h | 1 + 2 files changed, 11 insertions(+) diff --git a/AK/URL.cpp b/AK/URL.cpp index d029282b95..8c77031b88 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -297,4 +297,14 @@ URL URL::create_with_file_protocol(const String& path) return url; } +URL URL::create_with_url_or_path(const String& url_or_path) +{ + URL url = url_or_path; + if (url.is_valid()) + return url; + + String path = canonicalized_path(url_or_path); + return URL::create_with_file_protocol(path); +} + } diff --git a/AK/URL.h b/AK/URL.h index 4f827cbd03..be8bc05c93 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -65,6 +65,7 @@ public: URL complete_url(const String&) const; + static URL create_with_url_or_path(const String& url_or_path); static URL create_with_file_protocol(const String& path); private: