mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
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.
This commit is contained in:
parent
d9ece296f0
commit
1678aaa555
Notes:
sideshowbarker
2024-07-19 05:39:39 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1678aaa5550
11 changed files with 20 additions and 10 deletions
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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) =|
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue