1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/url_search_params.h"
6
7 #include <algorithm>
8 #include <string>
9 #include <string_view>
10 #include <utility>
11 #include <vector>
12
13 #include "base/strings/utf_string_conversions.h"
14 #include "net/base/url_util.h"
15 #include "url/gurl.h"
16
17 namespace net {
18
UrlSearchParams(const GURL & url)19 UrlSearchParams::UrlSearchParams(const GURL& url) {
20 for (auto it = QueryIterator(url); !it.IsAtEnd(); it.Advance()) {
21 // Use unescaped keys and values in order to mitigate potentially different
22 // representations for query search params names/values.
23 // E.g. a space character might be encoded as '+' or as "%20". A character
24 // might be encoded as a character or as its percent encoded
25 // representation (e.g. ?%63=2 should be the same as ?c=2). E.g. ぁ would be
26 // percent encoded as %E3%81%81. Unescapes the given `key` and `value`
27 // using URL escaping rules.
28 params_.emplace_back(UnescapePercentEncodedUrl(it.GetKey()),
29 UnescapePercentEncodedUrl(it.GetValue()));
30 }
31 }
32
33 UrlSearchParams::~UrlSearchParams() = default;
34
Sort()35 void UrlSearchParams::Sort() {
36 // Note: since query is ASCII and we've Unescaped the keys already,
37 // the URL equivalence under No-Vary-Search conditions using the normal string
38 // comparison should be enough.
39 std::stable_sort(params_.begin(), params_.end(),
40 [](const std::pair<std::string, std::string>& a,
41 const std::pair<std::string, std::string>& b) {
42 return a.first < b.first;
43 });
44 }
45
DeleteAllWithNames(const base::flat_set<std::string> & names)46 void UrlSearchParams::DeleteAllWithNames(
47 const base::flat_set<std::string>& names) {
48 std::erase_if(params_,
49 [&](const auto& pair) { return names.contains(pair.first); });
50 }
51
DeleteAllExceptWithNames(const base::flat_set<std::string> & names)52 void UrlSearchParams::DeleteAllExceptWithNames(
53 const base::flat_set<std::string>& names) {
54 std::erase_if(params_,
55 [&](const auto& pair) { return !names.contains(pair.first); });
56 }
57
58 const std::vector<std::pair<std::string, std::string>>&
params() const59 UrlSearchParams::params() const {
60 return params_;
61 }
62
63 } // namespace net
64