mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
ProtocolServer+LibProtocol: Reject unhandled URLs instead of asserting
StartDownload requests for unhandled protocols (or invalid URLs) will now refuse to load instead of asserting. A failure code is sent back to LibProtocol and Protocol::Client::start_download() returns nullptr. Fixes #1604.
This commit is contained in:
parent
53d0ca2ad8
commit
fc5067afd2
4 changed files with 15 additions and 2 deletions
|
@ -50,6 +50,8 @@ bool Client::is_supported_protocol(const String& protocol)
|
|||
RefPtr<Download> Client::start_download(const String& url)
|
||||
{
|
||||
i32 download_id = send_sync<Messages::ProtocolServer::StartDownload>(url)->download_id();
|
||||
if (download_id < 0)
|
||||
return nullptr;
|
||||
auto download = Download::create_from_id({}, *this, download_id);
|
||||
m_downloads.set(download_id, download);
|
||||
return download;
|
||||
|
|
|
@ -86,6 +86,11 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> succ
|
|||
|
||||
if (url.protocol() == "http") {
|
||||
auto download = protocol_client().start_download(url.to_string());
|
||||
if (!download) {
|
||||
if (error_callback)
|
||||
error_callback("Failed to initiate load");
|
||||
return;
|
||||
}
|
||||
download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto) {
|
||||
--m_pending_loads;
|
||||
if (on_load_counter_change)
|
||||
|
|
|
@ -57,9 +57,11 @@ OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> PSClientConnection
|
|||
OwnPtr<Messages::ProtocolServer::StartDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StartDownload& message)
|
||||
{
|
||||
URL url(message.url());
|
||||
ASSERT(url.is_valid());
|
||||
if (!url.is_valid())
|
||||
return make<Messages::ProtocolServer::StartDownloadResponse>(-1);
|
||||
auto* protocol = Protocol::find_by_name(url.protocol());
|
||||
ASSERT(protocol);
|
||||
if (!protocol)
|
||||
return make<Messages::ProtocolServer::StartDownloadResponse>(-1);
|
||||
auto download = protocol->start_download(*this, url);
|
||||
return make<Messages::ProtocolServer::StartDownloadResponse>(download->id());
|
||||
}
|
||||
|
|
|
@ -49,6 +49,10 @@ int main(int argc, char** argv)
|
|||
auto protocol_client = Protocol::Client::construct();
|
||||
|
||||
auto download = protocol_client->start_download(url.to_string());
|
||||
if (!download) {
|
||||
fprintf(stderr, "Failed to start download for '%s'\n", url_string.characters());
|
||||
return 1;
|
||||
}
|
||||
download->on_progress = [](u32 total_size, u32 downloaded_size) {
|
||||
dbgprintf("download progress: %u / %u\n", downloaded_size, total_size);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue