srctree

Robin Linden parent 77ca9453 f9d6b696
engine: Follow redirects when loading stylesheets

inlinesplit
engine/engine.cpp added: 30, removed: 3, total 27
@@ -78,7 +78,10 @@ void Engine::on_navigation_success() {
auto stylesheet_url = uri::Uri::parse(href, uri_);
 
spdlog::info("Downloading stylesheet from {}", stylesheet_url.uri);
auto style_data = protocol_handler_->handle(stylesheet_url);
auto res = load(stylesheet_url);
auto &style_data = res.response;
stylesheet_url = std::move(res.uri_after_redirects);
 
if (style_data.err != protocol::Error::Ok) {
spdlog::warn("Error {} downloading {}", static_cast<int>(style_data.err), stylesheet_url.uri);
return {};
 
engine/engine_test.cpp added: 30, removed: 3, total 27
@@ -394,5 +394,29 @@ int main() {
expect_eq(e.navigate(uri::Uri::parse("hax://example.com")), protocol::Error::InvalidResponse);
});
 
etest::test("redirect, style", [] {
std::map<std::string, Response> responses;
responses["hax://example.com"s] = Response{
.err = Error::Ok,
.status_line = {.status_code = 200},
.body{"<html><head>"
"<link rel=stylesheet href=hello.css />"
"</head></html>"},
};
responses["hax://example.com/hello.css"s] = Response{
.err = Error::Ok,
.status_line = {.status_code = 301},
.headers = {{"Location", "hax://example.com/redirected.css"}},
};
responses["hax://example.com/redirected.css"s] = Response{
.err = Error::Ok,
.status_line = {.status_code = 200},
.body{"p { color: green; }"},
};
engine::Engine e{std::make_unique<FakeProtocolHandler>(std::move(responses))};
expect_eq(e.navigate(uri::Uri::parse("hax://example.com")), protocol::Error::Ok);
expect(contains(e.stylesheet().rules, {.selectors{"p"}, .declarations{{css::PropertyId::Color, "green"}}}));
});
 
return etest::run_all_tests();
}