mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
ProtocolServer: Support request headers
You can now pass a dictionary of request headers when starting a new download in ProtocolServer. The HTTP and HTTPS protocol will include the headers in their requests.
This commit is contained in:
parent
25cfdf3f67
commit
897998017a
Notes:
sideshowbarker
2024-07-19 06:17:37 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/897998017af
13 changed files with 35 additions and 13 deletions
|
@ -71,7 +71,14 @@ ByteBuffer HttpRequest::to_raw_request() const
|
|||
}
|
||||
builder.append(" HTTP/1.1\r\nHost: ");
|
||||
builder.append(m_url.host());
|
||||
builder.append("\r\nConnection: close\r\n\r\n");
|
||||
builder.append("\r\n");
|
||||
for (auto& header : m_headers) {
|
||||
builder.append(header.name);
|
||||
builder.append(": ");
|
||||
builder.append(header.value);
|
||||
builder.append("\r\n");
|
||||
}
|
||||
builder.append("Connection: close\r\n\r\n");
|
||||
return builder.to_byte_buffer();
|
||||
}
|
||||
|
||||
|
@ -181,4 +188,10 @@ Optional<HttpRequest> HttpRequest::from_raw_request(const ByteBuffer& raw_reques
|
|||
return request;
|
||||
}
|
||||
|
||||
void HttpRequest::set_headers(const HashMap<String,String>& headers)
|
||||
{
|
||||
for (auto& it : headers)
|
||||
m_headers.append({ it.key, it.value });
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -65,6 +65,8 @@ public:
|
|||
|
||||
RefPtr<Core::NetworkJob> schedule();
|
||||
|
||||
void set_headers(const HashMap<String, String>&);
|
||||
|
||||
static Optional<HttpRequest> from_raw_request(const ByteBuffer&);
|
||||
|
||||
private:
|
||||
|
|
|
@ -47,9 +47,13 @@ bool Client::is_supported_protocol(const String& protocol)
|
|||
return send_sync<Messages::ProtocolServer::IsSupportedProtocol>(protocol)->supported();
|
||||
}
|
||||
|
||||
RefPtr<Download> Client::start_download(const String& url)
|
||||
RefPtr<Download> Client::start_download(const String& url, const HashMap<String, String>& request_headers)
|
||||
{
|
||||
i32 download_id = send_sync<Messages::ProtocolServer::StartDownload>(url)->download_id();
|
||||
IPC::Dictionary header_dictionary;
|
||||
for (auto& it : request_headers)
|
||||
header_dictionary.add(it.key, it.value);
|
||||
|
||||
i32 download_id = send_sync<Messages::ProtocolServer::StartDownload>(url, header_dictionary)->download_id();
|
||||
if (download_id < 0)
|
||||
return nullptr;
|
||||
auto download = Download::create_from_id({}, *this, download_id);
|
||||
|
|
|
@ -44,7 +44,8 @@ public:
|
|||
virtual void handshake() override;
|
||||
|
||||
bool is_supported_protocol(const String&);
|
||||
RefPtr<Download> start_download(const String& url);
|
||||
RefPtr<Download> start_download(const String& url, const HashMap<String, String>& request_headers = {});
|
||||
|
||||
|
||||
bool stop_download(Badge<Download>, Download&);
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ OwnPtr<Messages::ProtocolServer::StartDownloadResponse> ClientConnection::handle
|
|||
auto* protocol = Protocol::find_by_name(url.protocol());
|
||||
if (!protocol)
|
||||
return make<Messages::ProtocolServer::StartDownloadResponse>(-1);
|
||||
auto download = protocol->start_download(*this, url);
|
||||
auto download = protocol->start_download(*this, url, message.request_headers().entries());
|
||||
if (!download)
|
||||
return make<Messages::ProtocolServer::StartDownloadResponse>(-1);
|
||||
auto id = download->id();
|
||||
|
|
|
@ -40,7 +40,7 @@ GeminiProtocol::~GeminiProtocol()
|
|||
{
|
||||
}
|
||||
|
||||
OwnPtr<Download> GeminiProtocol::start_download(ClientConnection& client, const URL& url)
|
||||
OwnPtr<Download> GeminiProtocol::start_download(ClientConnection& client, const URL& url, const HashMap<String, String>&)
|
||||
{
|
||||
Gemini::GeminiRequest request;
|
||||
request.set_url(url);
|
||||
|
|
|
@ -35,7 +35,7 @@ public:
|
|||
GeminiProtocol();
|
||||
virtual ~GeminiProtocol() override;
|
||||
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) override;
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&, const HashMap<String, String>&) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -40,11 +40,12 @@ HttpProtocol::~HttpProtocol()
|
|||
{
|
||||
}
|
||||
|
||||
OwnPtr<Download> HttpProtocol::start_download(ClientConnection& client, const URL& url)
|
||||
OwnPtr<Download> HttpProtocol::start_download(ClientConnection& client, const URL& url, const HashMap<String, String>& headers)
|
||||
{
|
||||
HTTP::HttpRequest request;
|
||||
request.set_method(HTTP::HttpRequest::Method::GET);
|
||||
request.set_url(url);
|
||||
request.set_headers(headers);
|
||||
auto job = request.schedule();
|
||||
if (!job)
|
||||
return nullptr;
|
||||
|
|
|
@ -35,7 +35,7 @@ public:
|
|||
HttpProtocol();
|
||||
virtual ~HttpProtocol() override;
|
||||
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) override;
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&, const HashMap<String, String>& headers) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -40,11 +40,12 @@ HttpsProtocol::~HttpsProtocol()
|
|||
{
|
||||
}
|
||||
|
||||
OwnPtr<Download> HttpsProtocol::start_download(ClientConnection& client, const URL& url)
|
||||
OwnPtr<Download> HttpsProtocol::start_download(ClientConnection& client, const URL& url, const HashMap<String, String>& headers)
|
||||
{
|
||||
HTTP::HttpRequest request;
|
||||
request.set_method(HTTP::HttpRequest::Method::GET);
|
||||
request.set_url(url);
|
||||
request.set_headers(headers);
|
||||
auto job = HTTP::HttpsJob::construct(request);
|
||||
auto download = HttpsDownload::create_with_job({}, client, (HTTP::HttpsJob&)*job);
|
||||
job->start();
|
||||
|
|
|
@ -35,7 +35,7 @@ public:
|
|||
HttpsProtocol();
|
||||
virtual ~HttpsProtocol() override;
|
||||
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) override;
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&, const HashMap<String, String>& headers) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ public:
|
|||
virtual ~Protocol();
|
||||
|
||||
const String& name() const { return m_name; }
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) = 0;
|
||||
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&, const HashMap<String, String>& headers) = 0;
|
||||
|
||||
static Protocol* find_by_name(const String&);
|
||||
|
||||
|
|
|
@ -10,6 +10,6 @@ endpoint ProtocolServer = 9
|
|||
IsSupportedProtocol(String protocol) => (bool supported)
|
||||
|
||||
// Download API
|
||||
StartDownload(String url) => (i32 download_id)
|
||||
StartDownload(String url, IPC::Dictionary request_headers) => (i32 download_id)
|
||||
StopDownload(i32 download_id) => (bool success)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue