/proc/self/cwd/pagespeed/kernel/packs/planner.cc
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // Copyright (c) 2024-2026 We-Amp B.V. |
3 | | |
4 | | #include "pagespeed/kernel/packs/planner.h" |
5 | | |
6 | | #include <cstddef> |
7 | | #include <optional> |
8 | | #include <string> |
9 | | #include <string_view> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | #include "absl/strings/ascii.h" |
14 | | #include "absl/strings/str_cat.h" |
15 | | #include "pagespeed/kernel/packs/kind_registry.h" |
16 | | #include "pagespeed/kernel/packs/url_norm.h" |
17 | | |
18 | | namespace pagespeed::packs { |
19 | | |
20 | 29.0k | std::optional<PageUrl> ParsePageUrl(std::string_view url) { |
21 | 29.0k | PageUrl out; |
22 | 29.0k | const size_t scheme_end = url.find("://"); |
23 | 29.0k | if (scheme_end == std::string_view::npos) return std::nullopt; |
24 | 29.0k | out.scheme = absl::AsciiStrToLower(url.substr(0, scheme_end)); |
25 | 29.0k | if (out.scheme != "http" && out.scheme != "https") return std::nullopt; |
26 | 29.0k | url.remove_prefix(scheme_end + 3); |
27 | 29.0k | const size_t frag = url.find('#'); |
28 | 29.0k | if (frag != std::string_view::npos) url = url.substr(0, frag); |
29 | 29.0k | const size_t auth_end = url.find_first_of("/?"); |
30 | 29.0k | const std::string_view authority = url.substr(0, auth_end); |
31 | 29.0k | std::string_view rest = auth_end == std::string_view::npos |
32 | 29.0k | ? std::string_view() |
33 | 29.0k | : url.substr(auth_end); |
34 | 29.0k | if (authority.find('@') != std::string_view::npos) return std::nullopt; |
35 | 29.0k | out.host = NormalizeHost(authority); |
36 | 29.0k | if (out.host.empty()) return std::nullopt; |
37 | 29.0k | const size_t q = rest.find('?'); |
38 | 29.0k | std::string_view path = rest.substr(0, q); |
39 | 29.0k | if (q != std::string_view::npos) out.query = std::string(rest.substr(q + 1)); |
40 | 29.0k | out.path = path.empty() ? "/" : std::string(path); |
41 | 29.0k | return out; |
42 | 29.0k | } |
43 | | |
44 | 69.9k | std::string PageUrlString(const PageUrl& url) { |
45 | 69.9k | return absl::StrCat(url.scheme, "://", url.host, url.path, |
46 | 69.9k | url.query.empty() ? "" : "?", url.query); |
47 | 69.9k | } |
48 | | |
49 | 69.9k | std::string BaseUrl(const PageUrl& url, const PageFacts& facts) { |
50 | 69.9k | std::string page_url = PageUrlString(url); |
51 | 69.9k | if (!facts.base_href.empty()) { |
52 | 5.20k | std::string b = ResolveUrl(page_url, facts.base_href); |
53 | 5.20k | if (!b.empty() && NormUrl(b).has_value()) return b; |
54 | 5.20k | } |
55 | 65.4k | return page_url; |
56 | 69.9k | } |
57 | | |
58 | | PagePlan BuildPlan(const Pack& pack, const PageContext& ctx, |
59 | | const Selection& sel, const PageFacts& facts, |
60 | | DecisionPoint point, size_t max_added_bytes, |
61 | 62.6k | size_t extra_bytes_per_change) { |
62 | 62.6k | PagePlan plan; |
63 | 62.6k | if (sel.site == nullptr) { |
64 | 0 | plan.skip = SkipReason::kHostNotListed; |
65 | 0 | return plan; |
66 | 0 | } |
67 | | |
68 | 62.6k | bool any = false; |
69 | 313k | for (const RuleKind* kind : AllKinds()) { |
70 | 313k | if (kind->decision_point() == point && |
71 | 165k | sel.by_kind[KindIndex(kind)].has_value()) { |
72 | 160k | any = true; |
73 | 160k | } |
74 | 313k | } |
75 | 62.6k | if (!any) return plan; |
76 | 61.6k | if (facts.malformed_head) { |
77 | 368 | plan.skip = SkipReason::kMalformedHead; |
78 | 368 | return plan; |
79 | 368 | } |
80 | 61.2k | if (facts.head == nullptr) { |
81 | 19.6k | plan.skip = SkipReason::kNoHead; |
82 | 19.6k | return plan; |
83 | 19.6k | } |
84 | | |
85 | 41.5k | const std::string base = BaseUrl(ctx.url, facts); |
86 | | |
87 | | // The head values the page ends with, for the kinds that quote them: |
88 | | // `planned` as if every head rule had been applied, `actual` with only the |
89 | | // rules that really are enforcing and were applied. A rule that only |
90 | | // reports answers "what would the whole pack do"; an enforcing one must not |
91 | | // quote a value that is not in the document. |
92 | 41.5k | struct HeadValues { |
93 | 41.5k | std::string canonical, title, description; |
94 | 41.5k | }; |
95 | 41.5k | HeadValues planned, actual; |
96 | 41.5k | if (point == DecisionPoint::kDocumentEnd) { |
97 | 88.5k | for (const RuleKind* kind : AllKinds()) { |
98 | 88.5k | const HeadSlot slot = kind->head_slot(); |
99 | 88.5k | if (slot == HeadSlot::kNone) continue; |
100 | 53.1k | const RulePlan* head_rule = nullptr; |
101 | 53.1k | if (ctx.head_plan != nullptr) { |
102 | 212k | for (const RulePlan& r : ctx.head_plan->rules) { |
103 | 212k | if (r.decision.kind == kind) head_rule = &r; |
104 | 212k | } |
105 | 53.1k | } |
106 | 53.1k | const bool applied = head_rule != nullptr && ctx.head_applied && |
107 | 39.7k | head_rule->decision.mode == Mode::kEnforce; |
108 | 53.1k | const std::string pv = kind->FinalValue(head_rule, facts.For(kind), base); |
109 | 53.1k | const std::string av = |
110 | 53.1k | applied ? pv : kind->FinalValue(nullptr, facts.For(kind), base); |
111 | 53.1k | HeadValues* both[2] = {&planned, &actual}; |
112 | 53.1k | const std::string* values[2] = {&pv, &av}; |
113 | 159k | for (int i = 0; i < 2; ++i) { |
114 | 106k | switch (slot) { |
115 | 35.4k | case HeadSlot::kCanonical: |
116 | 35.4k | both[i]->canonical = *values[i]; |
117 | 35.4k | break; |
118 | 35.4k | case HeadSlot::kTitle: |
119 | 35.4k | both[i]->title = *values[i]; |
120 | 35.4k | break; |
121 | 35.4k | case HeadSlot::kDescription: |
122 | 35.4k | both[i]->description = *values[i]; |
123 | 35.4k | break; |
124 | 0 | case HeadSlot::kNone: |
125 | 0 | break; |
126 | 106k | } |
127 | 106k | } |
128 | 53.1k | } |
129 | 17.7k | } |
130 | | |
131 | 207k | for (const RuleKind* kind : AllKinds()) { |
132 | 207k | if (kind->decision_point() != point) continue; |
133 | 113k | const std::optional<RuleHit>& hit = sel.by_kind[KindIndex(kind)]; |
134 | 113k | if (!hit.has_value()) continue; |
135 | 111k | const Rule& rule = *hit->rule; |
136 | | |
137 | 111k | RulePlan rp; |
138 | 111k | rp.rule = &rule; |
139 | 111k | rp.decision.rule_id = rule.id; |
140 | 111k | rp.decision.kind = kind; |
141 | 111k | rp.decision.mode = |
142 | 111k | EffectiveMode(ctx.global_mode, sel.site->mode, rule.enforce); |
143 | | |
144 | 111k | PlanInput in; |
145 | 111k | in.pack = &pack; |
146 | 111k | in.rule = &rule; |
147 | 111k | in.url = ctx.url; |
148 | 111k | in.captures = hit->captures; |
149 | 111k | in.facts = &facts.For(kind); |
150 | 111k | in.base = base; |
151 | 111k | if (rule.value.source == ValueSource::kTable) { |
152 | 9.88k | if (rule.value.table == kHreflangTableName) { |
153 | 0 | in.cluster = FindCluster(pack, PageUrlString(ctx.url)); |
154 | 9.88k | } else { |
155 | 9.88k | auto table = pack.tables.find(rule.value.table); |
156 | 9.88k | if (table != pack.tables.end()) { |
157 | 9.88k | in.table_value = |
158 | 9.88k | LookupTableValue(table->second, ctx.url.path, ctx.url.query); |
159 | 9.88k | } |
160 | 9.88k | } |
161 | 9.88k | } |
162 | 111k | if (point == DecisionPoint::kDocumentEnd) { |
163 | 17.7k | const HeadValues& fin = |
164 | 17.7k | rp.decision.mode == Mode::kEnforce ? actual : planned; |
165 | 17.7k | in.final_canonical = fin.canonical; |
166 | 17.7k | in.final_title = fin.title; |
167 | 17.7k | in.final_description = fin.description; |
168 | 17.7k | } |
169 | 111k | kind->Plan(in, &rp); |
170 | 111k | if (!rp.planned) continue; |
171 | | |
172 | 111k | size_t removals = 0; |
173 | 707k | for (const PlanOp& op : rp.ops) removals += op.type == OpType::kRemove; |
174 | 111k | CapRemoved(&rp, removals); |
175 | 111k | plan.added_bytes += AddedBytes(rp, extra_bytes_per_change); |
176 | 111k | plan.rules.push_back(std::move(rp)); |
177 | 111k | } |
178 | | |
179 | 41.5k | if (plan.added_bytes > max_added_bytes) { |
180 | 0 | plan.skip = SkipReason::kSizeLimit; |
181 | 0 | plan.rules.clear(); |
182 | 0 | } |
183 | 41.5k | return plan; |
184 | 41.5k | } |
185 | | |
186 | | PagePlan BuildPlan(const Pack& pack, const PageContext& ctx, |
187 | | const PageFacts& facts, DecisionPoint point, |
188 | 0 | size_t max_added_bytes, size_t extra_bytes_per_change) { |
189 | 0 | const Selection sel = SelectRules(pack, ctx.url.host, ctx.url.path); |
190 | 0 | return BuildPlan(pack, ctx, sel, facts, point, max_added_bytes, |
191 | 0 | extra_bytes_per_change); |
192 | 0 | } |
193 | | |
194 | | } // namespace pagespeed::packs |