File: URLPatternGlue.cpp

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (248 lines) | stat: -rw-r--r-- 8,311 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/net/URLPatternGlue.h"

mozilla::LazyLogModule gUrlPatternLog("urlpattern");

namespace mozilla::net {

UrlpInput CreateUrlpInput(const nsACString& url) {
  UrlpInput input;
  input.string_or_init_type = UrlpStringOrInitType::String;
  input.str = url;
  return input;
}

UrlpInput CreateUrlpInput(const UrlpInit& init) {
  UrlpInput input;
  input.string_or_init_type = UrlpStringOrInitType::Init;
  input.init = init;
  return input;
}

MaybeString CreateMaybeString(const nsACString& str, bool valid) {
  return MaybeString{.string = nsCString(str), .valid = valid};
}

MaybeString CreateMaybeStringNone() {
  return MaybeString{.string = ""_ns, .valid = false};
}

nsAutoCString UrlpGetProtocol(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_protocol_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetUsername(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_username_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetPassword(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_password_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetHostname(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_hostname_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetPort(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_port_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetPathname(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_pathname_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetSearch(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_search_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

nsAutoCString UrlpGetHash(const UrlpPattern aPattern) {
  UrlpComponent component;
  urlp_get_hash_component(aPattern, &component);
  return nsAutoCString(component.pattern_string);
}

// https://urlpattern.spec.whatwg.org/#create-a-component-match-result
Maybe<UrlpComponentResult> ComponentMatches(UrlpComponent& aComponent,
                                            nsACString& aInput,
                                            bool aIgnoreCase) {
  UrlpComponentResult res;
  if (aComponent.regexp_string == "^$") {  // empty string
    if (aInput != "") {
      return Nothing();
    }
  } else {  // check deeper match
    nsTArray<MaybeString> matches;
    if (!urlp_matcher_matches_component(&aComponent.matcher, &aInput,
                                        aIgnoreCase, &matches)) {
      return Nothing();
    }
    for (size_t i = 0; i < matches.Length(); i++) {
      nsAutoCString key;
      key.Assign(aComponent.group_name_list[i]);
      res.mGroups.InsertOrUpdate(key, matches[i]);
    }
  }
  res.mInput = aInput;
  return Some(res);
}

Maybe<UrlpResult> AllComponentMatches(const UrlpPattern aPattern,
                                      UrlpMatchInput& aMatchInput,
                                      bool aIgnoreCase) {
  UrlpResult res;
  UrlpComponent componentProtocol{};
  urlp_get_protocol_component(aPattern, &componentProtocol);
  res.mProtocol =
      ComponentMatches(componentProtocol, aMatchInput.protocol, aIgnoreCase);
  if (res.mProtocol.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentUsername{};
  urlp_get_username_component(aPattern, &componentUsername);
  res.mUsername =
      ComponentMatches(componentUsername, aMatchInput.username, aIgnoreCase);
  if (res.mUsername.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentPassword{};
  urlp_get_password_component(aPattern, &componentPassword);
  res.mPassword =
      ComponentMatches(componentPassword, aMatchInput.password, aIgnoreCase);
  if (res.mPassword.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentHostname{};
  urlp_get_hostname_component(aPattern, &componentHostname);
  res.mHostname =
      ComponentMatches(componentHostname, aMatchInput.hostname, aIgnoreCase);
  if (res.mHostname.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentPort{};
  urlp_get_port_component(aPattern, &componentPort);
  res.mPort = ComponentMatches(componentPort, aMatchInput.port, aIgnoreCase);
  if (res.mPort.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentPathname{};
  urlp_get_pathname_component(aPattern, &componentPathname);
  res.mPathname =
      ComponentMatches(componentPathname, aMatchInput.pathname, aIgnoreCase);
  if (res.mPathname.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentSearch{};
  urlp_get_search_component(aPattern, &componentSearch);
  res.mSearch =
      ComponentMatches(componentSearch, aMatchInput.search, aIgnoreCase);
  if (res.mSearch.isNothing()) {
    return Nothing();
  }

  UrlpComponent componentHash{};
  urlp_get_hash_component(aPattern, &componentHash);
  res.mHash = ComponentMatches(componentHash, aMatchInput.hash, aIgnoreCase);
  if (res.mHash.isNothing()) {
    return Nothing();
  }

  return Some(res);
}

Maybe<UrlpResult> UrlpPatternExec(UrlpPattern aPattern, const UrlpInput& aInput,
                                  Maybe<nsAutoCString> aMaybeBaseUrl,
                                  bool aIgnoreCase) {
  MOZ_LOG(gUrlPatternLog, LogLevel::Debug, ("UrlpPatternExec()...\n"));
  UrlpMatchInputAndInputs matchInputAndInputs;
  if (aInput.string_or_init_type == UrlpStringOrInitType::Init) {
    MOZ_ASSERT(aMaybeBaseUrl.isNothing());
    if (!urlp_process_match_input_from_init(&aInput.init, nullptr,
                                            &matchInputAndInputs)) {
      return Nothing();
    }
  } else {
    nsAutoCString* baseUrl = nullptr;
    if (aMaybeBaseUrl.isSome()) {
      baseUrl = &aMaybeBaseUrl.ref();
    }
    if (!urlp_process_match_input_from_string(&aInput.str, baseUrl,
                                              &matchInputAndInputs)) {
      return Nothing();
    }
  }

  // shouldn't be any need to convert the urlpatternwrapper to quirks wrapper
  // the all_component_matches signature should be able to receive as a wrapper
  Maybe<UrlpResult> res =
      AllComponentMatches(aPattern, matchInputAndInputs.input, aIgnoreCase);
  if (res.isNothing()) {
    return Nothing();
  }

  if (matchInputAndInputs.inputs.string_or_init_type ==
      UrlpStringOrInitType::Init) {
    res->mInputs.AppendElement(
        CreateUrlpInput(matchInputAndInputs.inputs.init));
  } else {
    res->mInputs.AppendElement(CreateUrlpInput(matchInputAndInputs.inputs.str));
    if (matchInputAndInputs.inputs.base.valid) {
      res->mInputs.AppendElement(
          CreateUrlpInput(matchInputAndInputs.inputs.base.string));
    }
  }

  return res;
}

bool UrlpPatternTest(UrlpPattern aPattern, const UrlpInput& aInput,
                     Maybe<nsAutoCString> aMaybeBaseUrl, bool aIgnoreCase) {
  MOZ_LOG(gUrlPatternLog, LogLevel::Debug, ("UrlpPatternTest()...\n"));
  UrlpMatchInputAndInputs matchInputAndInputs;
  if (aInput.string_or_init_type == UrlpStringOrInitType::Init) {
    MOZ_ASSERT(aMaybeBaseUrl.isNothing());
    if (!urlp_process_match_input_from_init(&aInput.init, nullptr,
                                            &matchInputAndInputs)) {
      return false;
    }
  } else {
    nsAutoCString* baseUrl = nullptr;
    if (aMaybeBaseUrl.isSome()) {
      baseUrl = &aMaybeBaseUrl.ref();
    }
    if (!urlp_process_match_input_from_string(&aInput.str, baseUrl,
                                              &matchInputAndInputs)) {
      return false;
    }
  }

  // shouldn't be any need to convert the urlpatternwrapper to quirks wrapper
  // the all_component_matches signature should be able to receive as a wrapper
  Maybe<UrlpResult> res =
      AllComponentMatches(aPattern, matchInputAndInputs.input, aIgnoreCase);
  return !res.isNothing();
}

}  // namespace mozilla::net