summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-21 12:58:57 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-21 12:58:57 +0200
commit63d98bbd76da598a204c5b239667b5c04f864e02 (patch)
treeb29456722ba842412e0d27d84368ca5e34ff6c33
parent897998017af64a641ddbd82e5d12d08f6bcc2aea (diff)
downloadserenity-63d98bbd76da598a204c5b239667b5c04f864e02.zip
LibWeb: Send User-Agent in HTTP requests
Coming soon to a Browser Market Share graph near you! :^)
-rw-r--r--Libraries/LibWeb/Bindings/NavigatorObject.cpp10
-rw-r--r--Libraries/LibWeb/Bindings/NavigatorObject.h2
-rw-r--r--Libraries/LibWeb/ResourceLoader.cpp5
-rw-r--r--Libraries/LibWeb/ResourceLoader.h5
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 <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());
}
+
+}
+
}
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<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");
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<Protocol::Client> m_protocol_client;
- bool is_port_blocked(int port);
+ String m_user_agent;
};
}