mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:37:34 +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
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue