mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:27:35 +00:00
ProtocolServer+LibWeb: Support more detailed HTTP requests
This patch adds the ability for ProtocolServer clients to specify which HTTP method to use, and also to include an optional HTTP request body.
This commit is contained in:
parent
cfafd4d52d
commit
2946a684ef
21 changed files with 78 additions and 26 deletions
|
@ -79,6 +79,10 @@ ByteBuffer HttpRequest::to_raw_request() const
|
|||
builder.append("\r\n");
|
||||
}
|
||||
builder.append("Connection: close\r\n\r\n");
|
||||
if (!m_body.is_empty()) {
|
||||
builder.append((const char*)m_body.data(), m_body.size());
|
||||
builder.append("\r\n");
|
||||
}
|
||||
return builder.to_byte_buffer();
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
|
@ -60,6 +61,9 @@ public:
|
|||
Method method() const { return m_method; }
|
||||
void set_method(Method method) { m_method = method; }
|
||||
|
||||
const ByteBuffer& body() const { return m_body; }
|
||||
void set_body(const ByteBuffer& body) { m_body = body; }
|
||||
|
||||
String method_name() const;
|
||||
ByteBuffer to_raw_request() const;
|
||||
|
||||
|
@ -74,6 +78,7 @@ private:
|
|||
String m_resource;
|
||||
Method m_method { GET };
|
||||
Vector<Header> m_headers;
|
||||
ByteBuffer m_body;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -47,13 +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, const HashMap<String, String>& request_headers)
|
||||
RefPtr<Download> Client::start_download(const String& method, const String& url, const HashMap<String, String>& request_headers, const ByteBuffer& request_body)
|
||||
{
|
||||
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();
|
||||
i32 download_id = send_sync<Messages::ProtocolServer::StartDownload>(method, url, header_dictionary, String::copy(request_body))->download_id();
|
||||
if (download_id < 0)
|
||||
return nullptr;
|
||||
auto download = Download::create_from_id({}, *this, download_id);
|
||||
|
|
|
@ -44,7 +44,7 @@ public:
|
|||
virtual void handshake() override;
|
||||
|
||||
bool is_supported_protocol(const String&);
|
||||
RefPtr<Download> start_download(const String& url, const HashMap<String, String>& request_headers = {});
|
||||
RefPtr<Download> start_download(const String& method, const String& url, const HashMap<String, String>& request_headers = {}, const ByteBuffer& request_body = {});
|
||||
|
||||
bool stop_download(Badge<Download>, Download&);
|
||||
bool set_certificate(Badge<Download>, Download&, String, String);
|
||||
|
|
|
@ -139,17 +139,15 @@ RefPtr<DOM::Document> FrameLoader::create_document_from_mime_type(const ByteBuff
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool FrameLoader::load(const URL& url, Type type)
|
||||
bool FrameLoader::load(const LoadRequest& request, Type type)
|
||||
{
|
||||
dbg() << "FrameLoader::load: " << url;
|
||||
|
||||
if (!url.is_valid()) {
|
||||
load_error_page(url, "Invalid URL");
|
||||
if (!request.is_valid()) {
|
||||
load_error_page(request.url(), "Invalid request");
|
||||
return false;
|
||||
}
|
||||
|
||||
LoadRequest request;
|
||||
request.set_url(url);
|
||||
auto& url = request.url();
|
||||
|
||||
set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
|
||||
|
||||
if (type == Type::Navigation)
|
||||
|
@ -180,6 +178,21 @@ bool FrameLoader::load(const URL& url, Type type)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool FrameLoader::load(const URL& url, Type type)
|
||||
{
|
||||
dbg() << "FrameLoader::load: " << url;
|
||||
|
||||
if (!url.is_valid()) {
|
||||
load_error_page(url, "Invalid URL");
|
||||
return false;
|
||||
}
|
||||
|
||||
LoadRequest request;
|
||||
request.set_url(url);
|
||||
|
||||
return load(request, type);
|
||||
}
|
||||
|
||||
void FrameLoader::load_error_page(const URL& failed_url, const String& error)
|
||||
{
|
||||
auto error_page_url = "file:///res/html/error.html";
|
||||
|
|
|
@ -45,6 +45,7 @@ public:
|
|||
~FrameLoader();
|
||||
|
||||
bool load(const URL&, Type);
|
||||
bool load(const LoadRequest&, Type);
|
||||
|
||||
Frame& frame() { return m_frame; }
|
||||
const Frame& frame() const { return m_frame; }
|
||||
|
|
|
@ -96,7 +96,7 @@ RefPtr<Resource> ResourceLoader::load_resource(Resource::Type type, const LoadRe
|
|||
s_resource_cache.set(request, resource);
|
||||
|
||||
load(
|
||||
request.url(),
|
||||
request,
|
||||
[=](auto& data, auto& headers) {
|
||||
const_cast<Resource&>(*resource).did_load({}, data, headers);
|
||||
},
|
||||
|
@ -107,8 +107,9 @@ RefPtr<Resource> ResourceLoader::load_resource(Resource::Type type, const LoadRe
|
|||
return resource;
|
||||
}
|
||||
|
||||
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback)
|
||||
void ResourceLoader::load(const LoadRequest& request, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback)
|
||||
{
|
||||
auto& url = request.url();
|
||||
if (is_port_blocked(url.port())) {
|
||||
dbg() << "ResourceLoader::load: Error: blocked port " << url.port() << " for URL: " << url;
|
||||
return;
|
||||
|
@ -157,7 +158,12 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const
|
|||
if (url.protocol() == "http" || url.protocol() == "https" || url.protocol() == "gemini") {
|
||||
HashMap<String, String> headers;
|
||||
headers.set("User-Agent", m_user_agent);
|
||||
auto download = protocol_client().start_download(url.to_string(), headers);
|
||||
|
||||
for (auto& it : request.headers()) {
|
||||
headers.set(it.key, it.value);
|
||||
}
|
||||
|
||||
auto download = protocol_client().start_download(request.method(), url.to_string(), headers, request.body());
|
||||
if (!download) {
|
||||
if (error_callback)
|
||||
error_callback("Failed to initiate load");
|
||||
|
@ -192,6 +198,13 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const
|
|||
error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
|
||||
}
|
||||
|
||||
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback)
|
||||
{
|
||||
LoadRequest request;
|
||||
request.set_url(url);
|
||||
load(request, move(success_callback), move(error_callback));
|
||||
}
|
||||
|
||||
bool ResourceLoader::is_port_blocked(int port)
|
||||
{
|
||||
int ports[] { 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42,
|
||||
|
|
|
@ -44,6 +44,7 @@ public:
|
|||
|
||||
RefPtr<Resource> load_resource(Resource::Type, const LoadRequest&);
|
||||
|
||||
void load(const LoadRequest&, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback = nullptr);
|
||||
void load(const URL&, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback = nullptr);
|
||||
void load_sync(const URL&, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback = nullptr);
|
||||
|
||||
|
|
|
@ -57,6 +57,11 @@ void Page::load(const URL& url)
|
|||
main_frame().loader().load(url, FrameLoader::Type::Navigation);
|
||||
}
|
||||
|
||||
void Page::load(const LoadRequest& request)
|
||||
{
|
||||
main_frame().loader().load(request, FrameLoader::Type::Navigation);
|
||||
}
|
||||
|
||||
Gfx::Palette Page::palette() const
|
||||
{
|
||||
return m_client.palette();
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include <AK/Noncopyable.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/URL.h>
|
||||
#include <Kernel/API/KeyCode.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
|
@ -59,6 +60,7 @@ public:
|
|||
void set_focused_frame(Badge<EventHandler>, Frame&);
|
||||
|
||||
void load(const URL&);
|
||||
void load(const LoadRequest&);
|
||||
|
||||
bool handle_mouseup(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
|
||||
bool handle_mousedown(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue