Skip to content

Commit

Permalink
engine: Follow redirects when loading stylesheets
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Nov 14, 2023
1 parent 77ca945 commit f9d6b69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down
24 changes: 24 additions & 0 deletions engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit f9d6b69

Please sign in to comment.