/proc/self/cwd/pagespeed/kernel/packs/decision.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/decision.h" |
5 | | |
6 | | #include <cstdint> |
7 | | #include <string> |
8 | | #include <string_view> |
9 | | |
10 | | #include "absl/strings/str_cat.h" |
11 | | #include "pagespeed/kernel/packs/rule_kind.h" |
12 | | #include "pagespeed/kernel/packs/template.h" |
13 | | |
14 | | namespace pagespeed::packs { |
15 | | |
16 | 9.54k | std::string_view ActionName(Action action) { |
17 | 9.54k | switch (action) { |
18 | 0 | case Action::kNone: |
19 | 0 | return "none"; |
20 | 7.54k | case Action::kInsert: |
21 | 7.54k | return "insert"; |
22 | 63 | case Action::kFill: |
23 | 63 | return "fill"; |
24 | 782 | case Action::kReplace: |
25 | 782 | return "replace"; |
26 | 1.12k | case Action::kRemove: |
27 | 1.12k | return "remove"; |
28 | 27 | case Action::kDedupe: |
29 | 27 | return "dedupe"; |
30 | 9.54k | } |
31 | 0 | return "none"; |
32 | 9.54k | } |
33 | | |
34 | 265k | std::string_view ReasonName(Reason reason) { |
35 | 265k | switch (reason) { |
36 | 0 | case Reason::kAbsent: |
37 | 0 | return "absent"; |
38 | 0 | case Reason::kPresent: |
39 | 0 | return "present"; |
40 | 44.8k | case Reason::kDuplicate: |
41 | 44.8k | return "duplicate"; |
42 | 185k | case Reason::kUnusable: |
43 | 185k | return "unusable"; |
44 | 8.05k | case Reason::kOutOfHead: |
45 | 8.05k | return "out_of_head"; |
46 | 25.2k | case Reason::kEmpty: |
47 | 25.2k | return "empty"; |
48 | 2.65k | case Reason::kConflict: |
49 | 2.65k | return "conflict"; |
50 | 0 | case Reason::kEqual: |
51 | 0 | return "equal"; |
52 | 0 | case Reason::kNoValue: |
53 | 0 | return "no_value"; |
54 | 0 | case Reason::kInvalidValue: |
55 | 0 | return "invalid_value"; |
56 | 0 | case Reason::kClusterWithoutSelf: |
57 | 0 | return "cluster_without_self"; |
58 | 0 | case Reason::kShadowed: |
59 | 0 | return "shadowed"; |
60 | 0 | case Reason::kNotRewritable: |
61 | 0 | return "not_rewritable"; |
62 | 0 | case Reason::kLateConflict: |
63 | 0 | return "late_conflict"; |
64 | 0 | case Reason::kHeaderConflict: |
65 | 0 | return "header_conflict"; |
66 | 0 | case Reason::kHeadersFlushed: |
67 | 0 | return "headers_flushed"; |
68 | 0 | case Reason::kClaimed: |
69 | 0 | return "claimed"; |
70 | 265k | } |
71 | 0 | return "present"; |
72 | 265k | } |
73 | | |
74 | 17.3k | std::string HashValue(std::string_view value) { |
75 | 17.3k | uint64_t h = 14695981039346656037ULL; |
76 | 83.7M | for (unsigned char c : value) { |
77 | 83.7M | h ^= c; |
78 | 83.7M | h *= 1099511628211ULL; |
79 | 83.7M | } |
80 | 17.3k | static constexpr char kHex[] = "0123456789abcdef"; |
81 | 17.3k | std::string out = "fnv1a64:"; |
82 | 294k | for (int shift = 60; shift >= 0; shift -= 4) { |
83 | 277k | out.push_back(kHex[(h >> shift) & 0xF]); |
84 | 277k | } |
85 | 17.3k | return out; |
86 | 17.3k | } |
87 | | |
88 | 96.7k | std::string TruncateUtf8(std::string_view value, size_t max_bytes) { |
89 | 96.7k | if (value.size() <= max_bytes) return std::string(value); |
90 | 0 | size_t n = max_bytes; |
91 | 0 | while (n > 0 && (static_cast<unsigned char>(value[n]) & 0xC0) == 0x80) --n; |
92 | 0 | return std::string(value.substr(0, n)); |
93 | 96.7k | } |
94 | | |
95 | | namespace { |
96 | 0 | std::string Quote(std::string_view s) { |
97 | 0 | return absl::StrCat("\"", EscapeJsonString(s), "\""); |
98 | 0 | } |
99 | | } // namespace |
100 | | |
101 | | std::string DecisionToJsonLine(std::string_view host, std::string_view path, |
102 | 0 | const PackDecision& d) { |
103 | 0 | constexpr size_t kMaxPathBytes = 300; |
104 | | // Never splits a UTF-8 sequence, so the line stays valid JSON text. |
105 | 0 | const std::string cut_path = TruncateUtf8(path, kMaxPathBytes); |
106 | 0 | return absl::StrCat( |
107 | 0 | "{\"host\":", Quote(host), ",\"path\":", Quote(cut_path), |
108 | 0 | ",\"rule\":", Quote(d.rule_id), ",\"kind\":", |
109 | 0 | Quote(d.kind == nullptr ? std::string_view() : d.kind->name()), |
110 | 0 | ",\"mode\":", Quote(ModeName(d.mode)), |
111 | 0 | ",\"action\":", Quote(ActionName(d.action)), |
112 | 0 | ",\"reason\":", Quote(ReasonName(d.reason)), |
113 | 0 | ",\"defect\":", Quote(d.defect), ",\"before\":", Quote(d.before_hash), |
114 | 0 | ",\"after\":", Quote(d.after), |
115 | 0 | d.removed.empty() ? std::string() |
116 | 0 | : absl::StrCat(",\"removed\":", Quote(d.removed)), |
117 | 0 | d.header ? ",\"header\":true" : "", |
118 | 0 | d.claimed_by.empty() |
119 | 0 | ? std::string() |
120 | 0 | : absl::StrCat(",\"claimed_by\":", Quote(d.claimed_by)), |
121 | 0 | "}"); |
122 | 0 | } |
123 | | |
124 | | } // namespace pagespeed::packs |