1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:47:46 +00:00

AK+Everywhere: Change URL::path() to serialize_path()

This now defaults to serializing the path with percent decoded segments
(which is what all callers expect), but has an option not to. This fixes
`file://` URLs with spaces in their paths.

The name has been changed to serialize_path() path to make it more clear
that this method will generate a new string each call (except for the
cannot_be_a_base_url() case). A few callers have then been updated to
avoid repeatedly calling this function.
This commit is contained in:
MacDue 2023-04-14 20:12:03 +01:00 committed by Andreas Kling
parent 5acd40c525
commit 35612c6a7f
42 changed files with 131 additions and 123 deletions

View file

@ -146,7 +146,7 @@ void GLContextWidget::drop_event(GUI::DropEvent& event)
if (url.scheme() != "file")
continue;
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), url.path());
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), url.serialize_path());
if (response.is_error())
return;
load_file(response.value().filename(), response.value().release_stream());

View file

@ -253,7 +253,7 @@ DeprecatedString CookieJar::default_path(const URL& url)
// https://tools.ietf.org/html/rfc6265#section-5.1.4
// 1. Let uri-path be the path portion of the request-uri if such a portion exists (and empty otherwise).
DeprecatedString uri_path = url.path();
DeprecatedString uri_path = url.serialize_path();
// 2. If the uri-path is empty or if the first character of the uri-path is not a %x2F ("/") character, output %x2F ("/") and skip the remaining steps.
if (uri_path.is_empty() || (uri_path[0] != '/'))
@ -376,7 +376,7 @@ Vector<Web::Cookie::Cookie> CookieJar::get_matching_cookies(const URL& url, Depr
return;
// The request-uri's path path-matches the cookie's path.
if (!path_matches(url.path(), cookie.path))
if (!path_matches(url.serialize_path(), cookie.path))
return;
// If the cookie's secure-only-flag is true, then the request-uri's scheme must denote a "secure" protocol.

View file

@ -129,13 +129,14 @@ ErrorOr<bool> handle_drop(GUI::DropEvent const& event, DeprecatedString const& d
Vector<DeprecatedString> paths_to_copy;
for (auto& url_to_copy : urls) {
if (!url_to_copy.is_valid() || url_to_copy.path() == target)
auto file_path = url_to_copy.serialize_path();
if (!url_to_copy.is_valid() || file_path == target)
continue;
auto new_path = DeprecatedString::formatted("{}/{}", target, LexicalPath::basename(url_to_copy.path()));
if (url_to_copy.path() == new_path)
auto new_path = DeprecatedString::formatted("{}/{}", target, LexicalPath::basename(file_path));
if (file_path == new_path)
continue;
paths_to_copy.append(url_to_copy.path());
paths_to_copy.append(file_path);
has_accepted_drop = true;
}

View file

@ -202,7 +202,7 @@ void do_paste(DeprecatedString const& target_directory, GUI::Window* window)
dbgln("Cannot paste URI {}", uri_as_string);
continue;
}
source_paths.append(url.path());
source_paths.append(url.serialize_path());
}
if (!source_paths.is_empty()) {

View file

@ -975,8 +975,9 @@ void MainWidget::drop_event(GUI::DropEvent& event)
if (!request_close())
return;
if (auto result = open_file(urls.first().path()); result.is_error())
show_error(result.release_error(), "Opening"sv, LexicalPath { urls.first().path() }.basename());
auto file_path = urls.first().serialize_path();
if (auto result = open_file(file_path); result.is_error())
show_error(result.release_error(), "Opening"sv, LexicalPath { file_path }.basename());
}
}

View file

@ -98,7 +98,7 @@ MainWidget::MainWidget()
m_web_view = find_descendant_of_type_named<WebView::OutOfProcessWebView>("web_view");
m_web_view->on_link_click = [this](auto& url, auto&, unsigned) {
if (url.scheme() == "file") {
auto path = LexicalPath { url.path() };
auto path = LexicalPath { url.serialize_path() };
if (!path.is_child_of(Manual::manual_base_path)) {
open_external(url);
return;
@ -246,7 +246,7 @@ void MainWidget::open_url(URL const& url)
m_web_view->load(url);
m_web_view->scroll_to_top();
auto browse_view_index = m_manual_model->index_from_path(url.path());
auto browse_view_index = m_manual_model->index_from_path(url.serialize_path());
if (browse_view_index.has_value()) {
if (browse_view_index.value() != m_browse_view->selection_start_index()) {
m_browse_view->expand_all_parents_of(browse_view_index.value());

View file

@ -604,7 +604,7 @@ void HexEditorWidget::drop_event(GUI::DropEvent& event)
return;
// TODO: A drop event should be considered user consent for opening a file
auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().path(), Core::File::OpenMode::Read);
auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().serialize_path(), Core::File::OpenMode::Read);
if (response.is_error())
return;
open_file(response.value().filename(), response.value().release_stream());

View file

@ -100,7 +100,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->move_to_front();
auto path = urls.first().path();
auto path = urls.first().serialize_path();
auto result = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path);
if (result.is_error())
return;
@ -109,7 +109,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
widget->open_file(value.filename(), value.stream());
for (size_t i = 1; i < urls.size(); ++i) {
Desktop::Launcher::open(URL::create_with_file_scheme(urls[i].path().characters()), "/bin/ImageViewer");
Desktop::Launcher::open(URL::create_with_file_scheme(urls[i].serialize_path().characters()), "/bin/ImageViewer");
}
};
widget->on_doubleclick = [&] {

View file

@ -1401,7 +1401,7 @@ void MainWidget::drop_event(GUI::DropEvent& event)
if (url.scheme() != "file")
continue;
auto response = FileSystemAccessClient::Client::the().request_file(window(), url.path(), Core::File::OpenMode::Read);
auto response = FileSystemAccessClient::Client::the().request_file(window(), url.serialize_path(), Core::File::OpenMode::Read);
if (response.is_error())
return;
open_image(response.release_value());

View file

@ -214,6 +214,6 @@ void PresenterWidget::drop_event(GUI::DropEvent& event)
return;
window()->move_to_front();
set_file(urls.first().path());
set_file(urls.first().serialize_path());
}
}

View file

@ -141,9 +141,10 @@ bool RunWindow::run_via_launch(DeprecatedString const& run_input)
auto url = URL::create_with_url_or_path(run_input);
if (url.scheme() == "file") {
auto real_path_or_error = FileSystem::real_path(url.path());
auto file_path = url.serialize_path();
auto real_path_or_error = FileSystem::real_path(file_path);
if (real_path_or_error.is_error()) {
warnln("Failed to launch '{}': {}", url.path(), real_path_or_error.error());
warnln("Failed to launch '{}': {}", file_path, real_path_or_error.error());
return false;
}
url = URL::create_with_url_or_path(real_path_or_error.release_value().to_deprecated_string());

View file

@ -161,7 +161,7 @@ void SoundPlayerWidgetAdvancedView::drop_event(GUI::DropEvent& event)
return;
window()->move_to_front();
// FIXME: Add all paths from drop event to the playlist
play_file_path(urls.first().path());
play_file_path(urls.first().serialize_path());
}
}

View file

@ -84,24 +84,25 @@ HelpWindow::HelpWindow(GUI::Window* parent)
m_webview->on_link_click = [this](auto& url, auto&, auto&&) {
VERIFY(url.scheme() == "spreadsheet");
if (url.host() == "example") {
auto entry = LexicalPath::basename(url.path());
auto example_path = url.serialize_path();
auto entry = LexicalPath::basename(example_path);
auto doc_option = m_docs.get_object(entry);
if (!doc_option.has_value()) {
GUI::MessageBox::show_error(this, DeprecatedString::formatted("No documentation entry found for '{}'", url.path()));
GUI::MessageBox::show_error(this, DeprecatedString::formatted("No documentation entry found for '{}'", example_path));
return;
}
auto& doc = doc_option.value();
const auto& name = url.fragment();
auto name = url.fragment();
auto maybe_example_data = doc.get_object("example_data"sv);
if (!maybe_example_data.has_value()) {
GUI::MessageBox::show_error(this, DeprecatedString::formatted("No example data found for '{}'", url.path()));
GUI::MessageBox::show_error(this, DeprecatedString::formatted("No example data found for '{}'", example_path));
return;
}
auto& example_data = maybe_example_data.value();
if (!example_data.has_object(name)) {
GUI::MessageBox::show_error(this, DeprecatedString::formatted("Example '{}' not found for '{}'", name, url.path()));
GUI::MessageBox::show_error(this, DeprecatedString::formatted("Example '{}' not found for '{}'", name, example_path));
return;
}
auto& value = example_data.get_object(name).value();
@ -115,14 +116,14 @@ HelpWindow::HelpWindow(GUI::Window* parent)
auto widget = window->set_main_widget<SpreadsheetWidget>(window, Vector<NonnullRefPtr<Sheet>> {}, false).release_value_but_fixme_should_propagate_errors();
auto sheet = Sheet::from_json(value, widget->workbook());
if (!sheet) {
GUI::MessageBox::show_error(this, DeprecatedString::formatted("Corrupted example '{}' in '{}'", name, url.path()));
GUI::MessageBox::show_error(this, DeprecatedString::formatted("Corrupted example '{}' in '{}'", name, example_path));
return;
}
widget->add_sheet(sheet.release_nonnull());
window->show();
} else if (url.host() == "doc") {
auto entry = LexicalPath::basename(url.path());
auto entry = LexicalPath::basename(url.serialize_path());
m_webview->load(URL::create_with_data("text/html", render(entry)));
} else {
dbgln("Invalid spreadsheet action domain '{}'", url.host());

View file

@ -270,7 +270,7 @@ Optional<Position> Sheet::position_from_url(const URL& url) const
}
// FIXME: Figure out a way to do this cross-process.
VERIFY(url.path() == DeprecatedString::formatted("/{}", getpid()));
VERIFY(url.serialize_path() == DeprecatedString::formatted("/{}", getpid()));
return parse_cell_name(url.fragment());
}

View file

@ -845,7 +845,7 @@ void MainWidget::drop_event(GUI::DropEvent& event)
if (!request_close())
return;
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().path());
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().serialize_path());
if (response.is_error())
return;
if (auto result = read_file(response.value().filename(), response.value().stream()); result.is_error())

View file

@ -671,7 +671,7 @@ void MainWidget::drop_event(GUI::DropEvent& event)
if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
return;
auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().path(), Core::File::OpenMode::Read);
auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().serialize_path(), Core::File::OpenMode::Read);
if (response.is_error())
return;

View file

@ -279,7 +279,7 @@ void VideoPlayerWidget::drop_event(GUI::DropEvent& event)
GUI::MessageBox::show_error(window(), "VideoPlayer can only view one clip at a time!"sv);
return;
}
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().path());
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().serialize_path());
if (response.is_error())
return;
open_file(response.value().filename());