• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/containers/cxx20_erase_vector.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "net/base/url_util.h"
16 #include "url/gurl.h"
17 
18 namespace net {
19 
UrlSearchParams(const GURL & url)20 UrlSearchParams::UrlSearchParams(const GURL& url) {
21   for (auto it = QueryIterator(url); !it.IsAtEnd(); it.Advance()) {
22     // Use unescaped keys and values in order to mitigate potentially different
23     // representations for query search params names/values.
24     // E.g. a space character might be encoded as '+' or as "%20". A character
25     // might be encoded as a character or as its percent encoded
26     // representation (e.g. ?%63=2 should be the same as ?c=2). E.g. ぁ would be
27     // percent encoded as %E3%81%81. Unescapes the given `key` and `value`
28     // using URL escaping rules.
29     params_.emplace_back(UnescapePercentEncodedUrl(it.GetKey()),
30                          UnescapePercentEncodedUrl(it.GetValue()));
31   }
32 }
33 
34 UrlSearchParams::~UrlSearchParams() = default;
35 
Sort()36 void UrlSearchParams::Sort() {
37   // Note: since query is ASCII and we've Unescaped the keys already,
38   // the URL equivalence under No-Vary-Search conditions using the normal string
39   // comparison should be enough.
40   std::stable_sort(params_.begin(), params_.end(),
41                    [](const std::pair<std::string, std::string>& a,
42                       const std::pair<std::string, std::string>& b) {
43                      return a.first < b.first;
44                    });
45 }
46 
DeleteAllWithNames(const base::flat_set<std::string> & names)47 void UrlSearchParams::DeleteAllWithNames(
48     const base::flat_set<std::string>& names) {
49   base::EraseIf(params_,
50                 [&](const auto& pair) { return names.contains(pair.first); });
51 }
52 
DeleteAllExceptWithNames(const base::flat_set<std::string> & names)53 void UrlSearchParams::DeleteAllExceptWithNames(
54     const base::flat_set<std::string>& names) {
55   base::EraseIf(params_,
56                 [&](const auto& pair) { return !names.contains(pair.first); });
57 }
58 
59 const std::vector<std::pair<std::string, std::string>>&
params() const60 UrlSearchParams::params() const {
61   return params_;
62 }
63 
64 }  // namespace net
65