/proc/self/cwd/pagespeed/kernel/packs/pack.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/pack.h" |
5 | | |
6 | | #include <algorithm> |
7 | | |
8 | | #include "absl/strings/str_cat.h" |
9 | | #include "pagespeed/kernel/packs/url_norm.h" |
10 | | |
11 | | namespace pagespeed::packs { |
12 | | |
13 | 9.54k | std::string_view ModeName(Mode mode) { |
14 | 9.54k | return mode == Mode::kEnforce ? "enforce" : "report"; |
15 | 9.54k | } |
16 | | |
17 | 0 | std::string_view OnPresentName(OnPresent on_present) { |
18 | 0 | switch (on_present) { |
19 | 0 | case OnPresent::kKeep: |
20 | 0 | return "keep"; |
21 | 0 | case OnPresent::kRepair: |
22 | 0 | return "repair"; |
23 | 0 | case OnPresent::kReplace: |
24 | 0 | return "replace"; |
25 | 0 | } |
26 | 0 | return ""; |
27 | 0 | } |
28 | | |
29 | 0 | std::string_view LinkHeaderModeName(LinkHeaderMode mode) { |
30 | 0 | return mode == LinkHeaderMode::kMatch ? "match" : "off"; |
31 | 0 | } |
32 | | |
33 | 111k | Mode EffectiveMode(Mode global, Mode site, bool rule_enforce) { |
34 | 111k | const Mode rule = rule_enforce ? Mode::kEnforce : Mode::kReport; |
35 | 111k | return std::min({global, site, rule}); |
36 | 111k | } |
37 | | |
38 | 87.3k | absl::Status CheckUrlValue(std::string_view value) { |
39 | 87.3k | if (value.empty()) return absl::InvalidArgumentError("URL is empty"); |
40 | 87.3k | if (value.size() > kMaxUrlBytes) { |
41 | 11 | return absl::InvalidArgumentError( |
42 | 11 | absl::StrCat("URL is longer than ", kMaxUrlBytes, " bytes")); |
43 | 11 | } |
44 | 2.49M | for (char c : value) { |
45 | 2.49M | if (static_cast<unsigned char>(c) <= 0x20 || |
46 | 2.49M | static_cast<unsigned char>(c) == 0x7f) { |
47 | 47 | return absl::InvalidArgumentError( |
48 | 47 | "URL contains whitespace or a control character"); |
49 | 47 | } |
50 | 2.49M | if (c == '"' || c == '<' || c == '>' || c == '\\') { |
51 | 12 | return absl::InvalidArgumentError( |
52 | 12 | "URL contains a backslash or one of the characters \" < >"); |
53 | 12 | } |
54 | 2.49M | } |
55 | 87.3k | if (!NormUrl(value).has_value()) { |
56 | 227 | return absl::InvalidArgumentError( |
57 | 227 | "URL must be absolute (http:// or https://) with a host"); |
58 | 227 | } |
59 | 87.0k | return absl::OkStatus(); |
60 | 87.3k | } |
61 | | |
62 | 84.5k | RE2::Options MakeRegexOptions() { |
63 | 84.5k | RE2::Options options; |
64 | 84.5k | options.set_encoding(RE2::Options::EncodingLatin1); |
65 | 84.5k | options.set_max_mem(kRegexMaxMemBytes); |
66 | 84.5k | options.set_log_errors(false); |
67 | 84.5k | return options; |
68 | 84.5k | } |
69 | | |
70 | 53.3k | bool HasControlChars(std::string_view s) { |
71 | 479k | for (char c : s) { |
72 | 479k | const unsigned char u = static_cast<unsigned char>(c); |
73 | 479k | if (u < 0x20 || u == 0x7f) return true; |
74 | 479k | } |
75 | 53.2k | return false; |
76 | 53.3k | } |
77 | | |
78 | 241k | bool JsonNestingExceeds(std::string_view s, size_t max_depth) { |
79 | 241k | size_t depth = 0; |
80 | 241k | bool in_string = false; |
81 | 241k | bool escaped = false; |
82 | 167M | for (char c : s) { |
83 | 167M | if (in_string) { |
84 | 33.8M | if (escaped) { |
85 | 641k | escaped = false; |
86 | 33.1M | } else if (c == '\\') { |
87 | 641k | escaped = true; |
88 | 32.5M | } else if (c == '"') { |
89 | 1.71M | in_string = false; |
90 | 1.71M | } |
91 | 133M | } else if (c == '"') { |
92 | 1.74M | in_string = true; |
93 | 131M | } else if (c == '[' || c == '{') { |
94 | 743k | if (++depth > max_depth) return true; |
95 | 130M | } else if ((c == ']' || c == '}') && depth > 0) { |
96 | 495k | --depth; |
97 | 495k | } |
98 | 167M | } |
99 | 240k | return false; |
100 | 241k | } |
101 | | |
102 | 47.8k | size_t Utf8Length(std::string_view s) { |
103 | 47.8k | size_t n = 0; |
104 | 425k | for (char c : s) { |
105 | 425k | if ((static_cast<unsigned char>(c) & 0xC0) != 0x80) ++n; |
106 | 425k | } |
107 | 47.8k | return n; |
108 | 47.8k | } |
109 | | |
110 | | } // namespace pagespeed::packs |