OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/url_util.h" | 5 #include "net/base/url_util.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
7 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
8 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
9 #include "url/gurl.h" | 12 #include "url/gurl.h" |
10 | 13 |
11 namespace net { | 14 namespace net { |
12 | 15 |
13 GURL AppendQueryParameter(const GURL& url, | 16 GURL AppendQueryParameter(const GURL& url, |
14 const std::string& name, | 17 const std::string& name, |
15 const std::string& value) { | 18 const std::string& value) { |
16 std::string query(url.query()); | 19 std::string query(url.query()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 if (!output.empty()) | 64 if (!output.empty()) |
62 output += "&"; | 65 output += "&"; |
63 | 66 |
64 output += (param_name + "=" + param_value); | 67 output += (param_name + "=" + param_value); |
65 } | 68 } |
66 GURL::Replacements replacements; | 69 GURL::Replacements replacements; |
67 replacements.SetQueryStr(output); | 70 replacements.SetQueryStr(output); |
68 return url.ReplaceComponents(replacements); | 71 return url.ReplaceComponents(replacements); |
69 } | 72 } |
70 | 73 |
| 74 QueryIterator::QueryIterator(const GURL& url) |
| 75 : url_(url), |
| 76 at_end_(!url.is_valid()) { |
| 77 if (!at_end_) { |
| 78 query_ = url.parsed_for_possibly_invalid_spec().query; |
| 79 Advance(); |
| 80 } |
| 81 } |
| 82 |
| 83 QueryIterator::~QueryIterator() { |
| 84 } |
| 85 |
| 86 std::string QueryIterator::GetKey() const { |
| 87 DCHECK(!at_end_); |
| 88 if (key_.is_nonempty()) |
| 89 return url_.spec().substr(key_.begin, key_.len); |
| 90 return std::string(); |
| 91 } |
| 92 |
| 93 std::string QueryIterator::GetValue() const { |
| 94 DCHECK(!at_end_); |
| 95 if (value_.is_nonempty()) |
| 96 return url_.spec().substr(value_.begin, value_.len); |
| 97 return std::string(); |
| 98 } |
| 99 |
| 100 const std::string& QueryIterator::GetUnescapedValue() { |
| 101 DCHECK(!at_end_); |
| 102 if (value_.is_nonempty() && unescaped_value_.empty()) { |
| 103 unescaped_value_ = UnescapeURLComponent( |
| 104 GetValue(), |
| 105 UnescapeRule::SPACES | |
| 106 UnescapeRule::URL_SPECIAL_CHARS | |
| 107 UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
| 108 } |
| 109 return unescaped_value_; |
| 110 } |
| 111 |
| 112 bool QueryIterator::IsAtEnd() const { |
| 113 return at_end_; |
| 114 } |
| 115 |
| 116 void QueryIterator::Advance() { |
| 117 DCHECK (!at_end_); |
| 118 key_.reset(); |
| 119 value_.reset(); |
| 120 unescaped_value_.clear(); |
| 121 at_end_ = !url_parse::ExtractQueryKeyValue(url_.spec().c_str(), |
| 122 &query_, |
| 123 &key_, |
| 124 &value_); |
| 125 } |
| 126 |
71 bool GetValueForKeyInQuery(const GURL& url, | 127 bool GetValueForKeyInQuery(const GURL& url, |
72 const std::string& search_key, | 128 const std::string& search_key, |
73 std::string* out_value) { | 129 std::string* out_value) { |
74 if (!url.is_valid()) | 130 for (QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { |
75 return false; | 131 if (it.GetKey() == search_key) { |
76 | 132 *out_value = it.GetUnescapedValue(); |
77 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | 133 return true; |
78 url_parse::Component key, value; | |
79 while (url_parse::ExtractQueryKeyValue( | |
80 url.spec().c_str(), &query, &key, &value)) { | |
81 if (key.is_nonempty()) { | |
82 std::string key_string = url.spec().substr(key.begin, key.len); | |
83 if (key_string == search_key) { | |
84 if (value.is_nonempty()) { | |
85 *out_value = UnescapeURLComponent( | |
86 url.spec().substr(value.begin, value.len), | |
87 UnescapeRule::SPACES | | |
88 UnescapeRule::URL_SPECIAL_CHARS | | |
89 UnescapeRule::REPLACE_PLUS_WITH_SPACE); | |
90 } else { | |
91 *out_value = ""; | |
92 } | |
93 return true; | |
94 } | |
95 } | 134 } |
96 } | 135 } |
97 return false; | 136 return false; |
98 } | 137 } |
99 | 138 |
100 } // namespace net | 139 } // namespace net |
OLD | NEW |