LibWeb: Send User-Agent in HTTP requests

Coming soon to a Browser Market Share graph near you! :^)
This commit is contained in:
Andreas Kling 2020-05-21 12:58:57 +02:00
parent 897998017a
commit 63d98bbd76
Notes: sideshowbarker 2024-07-19 06:17:35 +09:00
4 changed files with 19 additions and 3 deletions

View file

@ -28,6 +28,7 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Bindings/NavigatorObject.h>
#include <LibWeb/ResourceLoader.h>
namespace Web {
namespace Bindings {
@ -40,12 +41,19 @@ NavigatorObject::NavigatorObject()
put("appVersion", js_string(heap(), "4.0"));
put("platform", js_string(heap(), "SerenityOS"));
put("product", js_string(heap(), "Gecko"));
put("userAgent", js_string(heap(), "Mozilla/4.0 (SerenityOS; x86) LibWeb+LibJS (Not KHTML, nor Gecko) LibWeb"));
put_native_property("userAgent", user_agent_getter, nullptr);
}
NavigatorObject::~NavigatorObject()
{
}
JS::Value NavigatorObject::user_agent_getter(JS::Interpreter& interpreter)
{
return JS::js_string(interpreter, ResourceLoader::the().user_agent());
}
}
}

View file

@ -39,6 +39,8 @@ public:
private:
virtual const char* class_name() const override { return "NavigatorObject"; }
static JS::Value user_agent_getter(JS::Interpreter&);
};
}

View file

@ -44,6 +44,7 @@ ResourceLoader& ResourceLoader::the()
ResourceLoader::ResourceLoader()
: m_protocol_client(Protocol::Client::construct())
, m_user_agent("Mozilla/4.0 (SerenityOS; x86) LibWeb+LibJS (Not KHTML, nor Gecko) LibWeb")
{
}
@ -114,7 +115,9 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const
}
if (url.protocol() == "http" || url.protocol() == "https" || url.protocol() == "gemini") {
auto download = protocol_client().start_download(url.to_string());
HashMap<String, String> headers;
headers.set("User-Agent", m_user_agent);
auto download = protocol_client().start_download(url.to_string(), headers);
if (!download) {
if (error_callback)
error_callback("Failed to initiate load");

View file

@ -50,13 +50,16 @@ public:
Protocol::Client& protocol_client() { return *m_protocol_client; }
const String& user_agent() const { return m_user_agent; }
private:
ResourceLoader();
static bool is_port_blocked(int port);
int m_pending_loads { 0 };
RefPtr<Protocol::Client> m_protocol_client;
bool is_port_blocked(int port);
String m_user_agent;
};
}