diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-13 22:19:34 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-13 22:20:37 +0200 |
commit | 1678aaa5550ebf8d89b83a762fa03c2613b30916 (patch) | |
tree | 79c980f459d8d379987431c209251f073f34ea4c | |
parent | d9ece296f0a7a0e5c293c9f87303da4c57b5d4c3 (diff) | |
download | serenity-1678aaa5550ebf8d89b83a762fa03c2613b30916.zip |
ProtocolServer+LibProtocol: Propagate HTTP status codes to clients
Clients now receive HTTP status codes like 200, 404, etc.
Note that a 404 with content is still considered a "successful"
download from ProtocolServer's perspective. It's up to the client
to interpret the status code.
I'm not sure if this is the best API, but it'll work for now.
-rw-r--r-- | Applications/Browser/DownloadWidget.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibProtocol/Client.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibProtocol/Download.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibProtocol/Download.h | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/Loader/ResourceLoader.cpp | 7 | ||||
-rw-r--r-- | Services/ProtocolServer/ClientConnection.cpp | 2 | ||||
-rw-r--r-- | Services/ProtocolServer/Download.h | 3 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpDownload.cpp | 1 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpsDownload.cpp | 1 | ||||
-rw-r--r-- | Services/ProtocolServer/ProtocolClient.ipc | 2 | ||||
-rw-r--r-- | Userland/pro.cpp | 2 |
11 files changed, 20 insertions, 10 deletions
diff --git a/Applications/Browser/DownloadWidget.cpp b/Applications/Browser/DownloadWidget.cpp index b03cd332bb..69d53f9c7d 100644 --- a/Applications/Browser/DownloadWidget.cpp +++ b/Applications/Browser/DownloadWidget.cpp @@ -60,7 +60,7 @@ DownloadWidget::DownloadWidget(const URL& url) m_download->on_progress = [this](Optional<u32> total_size, u32 downloaded_size) { did_progress(total_size.value(), downloaded_size); }; - m_download->on_finish = [this](bool success, auto& payload, auto payload_storage, auto& response_headers) { + m_download->on_finish = [this](bool success, auto& payload, auto payload_storage, auto& response_headers, auto) { did_finish(success, payload, payload_storage, response_headers); }; diff --git a/Libraries/LibProtocol/Client.cpp b/Libraries/LibProtocol/Client.cpp index 303910dce2..f5e22979e4 100644 --- a/Libraries/LibProtocol/Client.cpp +++ b/Libraries/LibProtocol/Client.cpp @@ -72,7 +72,7 @@ void Client::handle(const Messages::ProtocolClient::DownloadFinished& message) { RefPtr<Download> download; if ((download = m_downloads.get(message.download_id()).value_or(nullptr))) { - download->did_finish({}, message.success(), message.total_size(), message.shbuf_id(), message.response_headers()); + download->did_finish({}, message.success(), message.status_code(), message.total_size(), message.shbuf_id(), message.response_headers()); } send_sync<Messages::ProtocolServer::DisownSharedBuffer>(message.shbuf_id()); m_downloads.remove(message.download_id()); diff --git a/Libraries/LibProtocol/Download.cpp b/Libraries/LibProtocol/Download.cpp index 616321df92..0a2a29ed68 100644 --- a/Libraries/LibProtocol/Download.cpp +++ b/Libraries/LibProtocol/Download.cpp @@ -41,7 +41,7 @@ bool Download::stop() return m_client->stop_download({}, *this); } -void Download::did_finish(Badge<Client>, bool success, u32 total_size, i32 shbuf_id, const IPC::Dictionary& response_headers) +void Download::did_finish(Badge<Client>, bool success, Optional<u32> status_code, u32 total_size, i32 shbuf_id, const IPC::Dictionary& response_headers) { if (!on_finish) return; @@ -59,7 +59,7 @@ void Download::did_finish(Badge<Client>, bool success, u32 total_size, i32 shbuf caseless_response_headers.set(name, value); }); - on_finish(success, payload, move(shared_buffer), caseless_response_headers); + on_finish(success, payload, move(shared_buffer), caseless_response_headers, status_code); } void Download::did_progress(Badge<Client>, Optional<u32> total_size, u32 downloaded_size) diff --git a/Libraries/LibProtocol/Download.h b/Libraries/LibProtocol/Download.h index 4934e54fb9..12f7ce9052 100644 --- a/Libraries/LibProtocol/Download.h +++ b/Libraries/LibProtocol/Download.h @@ -48,10 +48,10 @@ public: int id() const { return m_download_id; } bool stop(); - Function<void(bool success, const ByteBuffer& payload, RefPtr<SharedBuffer> payload_storage, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> on_finish; + Function<void(bool success, const ByteBuffer& payload, RefPtr<SharedBuffer> payload_storage, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> status_code)> on_finish; Function<void(Optional<u32> total_size, u32 downloaded_size)> on_progress; - void did_finish(Badge<Client>, bool success, u32 total_size, i32 shbuf_id, const IPC::Dictionary& response_headers); + void did_finish(Badge<Client>, bool success, Optional<u32> status_code, u32 total_size, i32 shbuf_id, const IPC::Dictionary& response_headers); void did_progress(Badge<Client>, Optional<u32> total_size, u32 downloaded_size); private: diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp index f8c4d5a3ee..20c12e452b 100644 --- a/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -163,7 +163,7 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const 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, auto& response_headers) { + download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto, auto& response_headers, auto status_code) { --m_pending_loads; if (on_load_counter_change) on_load_counter_change(); @@ -172,6 +172,11 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const error_callback("HTTP load failed"); return; } + if (status_code.has_value() && status_code.value() == 404) { + if (error_callback) + error_callback("HTTP not found (four-oh-four!)"); + return; + } success_callback(ByteBuffer::copy(payload.data(), payload.size()), response_headers); }; ++m_pending_loads; diff --git a/Services/ProtocolServer/ClientConnection.cpp b/Services/ProtocolServer/ClientConnection.cpp index b9f78e2520..21c23d3ec8 100644 --- a/Services/ProtocolServer/ClientConnection.cpp +++ b/Services/ProtocolServer/ClientConnection.cpp @@ -99,7 +99,7 @@ void ClientConnection::did_finish_download(Badge<Download>, Download& download, IPC::Dictionary response_headers; for (auto& it : download.response_headers()) response_headers.add(it.key, it.value); - post_message(Messages::ProtocolClient::DownloadFinished(download.id(), success, download.total_size().value(), buffer ? buffer->shbuf_id() : -1, response_headers)); + post_message(Messages::ProtocolClient::DownloadFinished(download.id(), success, download.status_code(), download.total_size().value(), buffer ? buffer->shbuf_id() : -1, response_headers)); m_downloads.remove(download.id()); } diff --git a/Services/ProtocolServer/Download.h b/Services/ProtocolServer/Download.h index 9bc3c7a5f7..2cc0487a6d 100644 --- a/Services/ProtocolServer/Download.h +++ b/Services/ProtocolServer/Download.h @@ -42,6 +42,7 @@ public: i32 id() const { return m_id; } URL url() const { return m_url; } + Optional<u32> status_code() const { return m_status_code; } Optional<u32> total_size() const { return m_total_size; } size_t downloaded_size() const { return m_downloaded_size; } const ByteBuffer& payload() const { return m_payload; } @@ -54,6 +55,7 @@ protected: void did_finish(bool success); void did_progress(Optional<u32> total_size, u32 downloaded_size); + void set_status_code(u32 status_code) { m_status_code = status_code; } void set_payload(const ByteBuffer&); void set_response_headers(const HashMap<String, String, CaseInsensitiveStringTraits>&); @@ -61,6 +63,7 @@ private: ClientConnection& m_client; i32 m_id { 0 }; URL m_url; + Optional<u32> m_status_code; Optional<u32> m_total_size {}; size_t m_downloaded_size { 0 }; ByteBuffer m_payload; diff --git a/Services/ProtocolServer/HttpDownload.cpp b/Services/ProtocolServer/HttpDownload.cpp index fd0a9862cf..bfa22351d3 100644 --- a/Services/ProtocolServer/HttpDownload.cpp +++ b/Services/ProtocolServer/HttpDownload.cpp @@ -36,6 +36,7 @@ HttpDownload::HttpDownload(ClientConnection& client, NonnullRefPtr<HTTP::HttpJob { m_job->on_finish = [this](bool success) { if (auto* response = m_job->response()) { + set_status_code(response->code()); set_payload(response->payload()); set_response_headers(response->headers()); } diff --git a/Services/ProtocolServer/HttpsDownload.cpp b/Services/ProtocolServer/HttpsDownload.cpp index 6a578b80ab..899c204019 100644 --- a/Services/ProtocolServer/HttpsDownload.cpp +++ b/Services/ProtocolServer/HttpsDownload.cpp @@ -36,6 +36,7 @@ HttpsDownload::HttpsDownload(ClientConnection& client, NonnullRefPtr<HTTP::Https { m_job->on_finish = [this](bool success) { if (auto* response = m_job->response()) { + set_status_code(response->code()); set_payload(response->payload()); set_response_headers(response->headers()); } diff --git a/Services/ProtocolServer/ProtocolClient.ipc b/Services/ProtocolServer/ProtocolClient.ipc index 575b3ddd2b..e4f7ab7a5b 100644 --- a/Services/ProtocolServer/ProtocolClient.ipc +++ b/Services/ProtocolServer/ProtocolClient.ipc @@ -2,5 +2,5 @@ endpoint ProtocolClient = 13 { // Download notifications DownloadProgress(i32 download_id, Optional<u32> total_size, u32 downloaded_size) =| - DownloadFinished(i32 download_id, bool success, u32 total_size, i32 shbuf_id, IPC::Dictionary response_headers) =| + DownloadFinished(i32 download_id, bool success, Optional<u32> status_code, u32 total_size, i32 shbuf_id, IPC::Dictionary response_headers) =| } diff --git a/Userland/pro.cpp b/Userland/pro.cpp index 3ab6590cb8..fcad52f95a 100644 --- a/Userland/pro.cpp +++ b/Userland/pro.cpp @@ -78,7 +78,7 @@ int main(int argc, char** argv) previous_downloaded_size = downloaded_size; prev_time = current_time; }; - download->on_finish = [&](bool success, auto& payload, auto, auto&) { + download->on_finish = [&](bool success, auto& payload, auto, auto&, auto) { fprintf(stderr, "\033]9;-1;\033\\"); fprintf(stderr, "\n"); if (success) |