From 63d98bbd76da598a204c5b239667b5c04f864e02 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 21 May 2020 12:58:57 +0200 Subject: LibWeb: Send User-Agent in HTTP requests Coming soon to a Browser Market Share graph near you! :^) --- Libraries/LibWeb/Bindings/NavigatorObject.cpp | 10 +++++++++- Libraries/LibWeb/Bindings/NavigatorObject.h | 2 ++ Libraries/LibWeb/ResourceLoader.cpp | 5 ++++- Libraries/LibWeb/ResourceLoader.h | 5 ++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Libraries/LibWeb/Bindings/NavigatorObject.cpp b/Libraries/LibWeb/Bindings/NavigatorObject.cpp index 5100c5541c..cb91b5f35c 100644 --- a/Libraries/LibWeb/Bindings/NavigatorObject.cpp +++ b/Libraries/LibWeb/Bindings/NavigatorObject.cpp @@ -28,6 +28,7 @@ #include #include #include +#include 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()); } + +} + } diff --git a/Libraries/LibWeb/Bindings/NavigatorObject.h b/Libraries/LibWeb/Bindings/NavigatorObject.h index 3052c2fc8e..5f12f4b8a6 100644 --- a/Libraries/LibWeb/Bindings/NavigatorObject.h +++ b/Libraries/LibWeb/Bindings/NavigatorObject.h @@ -39,6 +39,8 @@ public: private: virtual const char* class_name() const override { return "NavigatorObject"; } + + static JS::Value user_agent_getter(JS::Interpreter&); }; } diff --git a/Libraries/LibWeb/ResourceLoader.cpp b/Libraries/LibWeb/ResourceLoader.cpp index ce047b7e18..9ef4413da2 100644 --- a/Libraries/LibWeb/ResourceLoader.cpp +++ b/Libraries/LibWeb/ResourceLoader.cpp @@ -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 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"); diff --git a/Libraries/LibWeb/ResourceLoader.h b/Libraries/LibWeb/ResourceLoader.h index 7bca500a60..b87ce20803 100644 --- a/Libraries/LibWeb/ResourceLoader.h +++ b/Libraries/LibWeb/ResourceLoader.h @@ -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 m_protocol_client; - bool is_port_blocked(int port); + String m_user_agent; }; } -- cgit v1.2.3