| 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> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 bool replaced = false; | 34 bool replaced = false; |
| 35 std::string param_name = EscapeQueryParamValue(name, true); | 35 std::string param_name = EscapeQueryParamValue(name, true); |
| 36 std::string param_value = EscapeQueryParamValue(value, true); | 36 std::string param_value = EscapeQueryParamValue(value, true); |
| 37 | 37 |
| 38 const std::string input = url.query(); | 38 const std::string input = url.query(); |
| 39 url::Component cursor(0, input.size()); | 39 url::Component cursor(0, input.size()); |
| 40 std::string output; | 40 std::string output; |
| 41 url::Component key_range, value_range; | 41 url::Component key_range, value_range; |
| 42 while (url::ExtractQueryKeyValue(input.data(), &cursor, &key_range, | 42 while (url::ExtractQueryKeyValue(input.data(), &cursor, &key_range, |
| 43 &value_range)) { | 43 &value_range)) { |
| 44 const base::StringPiece key( | 44 const base::StringPiece key(input.data() + key_range.begin, key_range.len); |
| 45 input.data() + key_range.begin, key_range.len); | 45 const base::StringPiece value(input.data() + value_range.begin, |
| 46 const base::StringPiece value( | 46 value_range.len); |
| 47 input.data() + value_range.begin, value_range.len); | |
| 48 std::string key_value_pair; | 47 std::string key_value_pair; |
| 49 // Check |replaced| as only the first pair should be replaced. | 48 // Check |replaced| as only the first pair should be replaced. |
| 50 if (!replaced && key == param_name) { | 49 if (!replaced && key == param_name) { |
| 51 replaced = true; | 50 replaced = true; |
| 52 key_value_pair = (param_name + "=" + param_value); | 51 key_value_pair = (param_name + "=" + param_value); |
| 53 } else { | 52 } else { |
| 54 key_value_pair.assign(input.data(), | 53 key_value_pair.assign(input.data(), key_range.begin, |
| 55 key_range.begin, | |
| 56 value_range.end() - key_range.begin); | 54 value_range.end() - key_range.begin); |
| 57 } | 55 } |
| 58 if (!output.empty()) | 56 if (!output.empty()) |
| 59 output += "&"; | 57 output += "&"; |
| 60 | 58 |
| 61 output += key_value_pair; | 59 output += key_value_pair; |
| 62 } | 60 } |
| 63 if (!replaced) { | 61 if (!replaced) { |
| 64 if (!output.empty()) | 62 if (!output.empty()) |
| 65 output += "&"; | 63 output += "&"; |
| 66 | 64 |
| 67 output += (param_name + "=" + param_value); | 65 output += (param_name + "=" + param_value); |
| 68 } | 66 } |
| 69 GURL::Replacements replacements; | 67 GURL::Replacements replacements; |
| 70 replacements.SetQueryStr(output); | 68 replacements.SetQueryStr(output); |
| 71 return url.ReplaceComponents(replacements); | 69 return url.ReplaceComponents(replacements); |
| 72 } | 70 } |
| 73 | 71 |
| 74 QueryIterator::QueryIterator(const GURL& url) | 72 QueryIterator::QueryIterator(const GURL& url) |
| 75 : url_(url), | 73 : url_(url), at_end_(!url.is_valid()) { |
| 76 at_end_(!url.is_valid()) { | |
| 77 if (!at_end_) { | 74 if (!at_end_) { |
| 78 query_ = url.parsed_for_possibly_invalid_spec().query; | 75 query_ = url.parsed_for_possibly_invalid_spec().query; |
| 79 Advance(); | 76 Advance(); |
| 80 } | 77 } |
| 81 } | 78 } |
| 82 | 79 |
| 83 QueryIterator::~QueryIterator() { | 80 QueryIterator::~QueryIterator() { |
| 84 } | 81 } |
| 85 | 82 |
| 86 std::string QueryIterator::GetKey() const { | 83 std::string QueryIterator::GetKey() const { |
| 87 DCHECK(!at_end_); | 84 DCHECK(!at_end_); |
| 88 if (key_.is_nonempty()) | 85 if (key_.is_nonempty()) |
| 89 return url_.spec().substr(key_.begin, key_.len); | 86 return url_.spec().substr(key_.begin, key_.len); |
| 90 return std::string(); | 87 return std::string(); |
| 91 } | 88 } |
| 92 | 89 |
| 93 std::string QueryIterator::GetValue() const { | 90 std::string QueryIterator::GetValue() const { |
| 94 DCHECK(!at_end_); | 91 DCHECK(!at_end_); |
| 95 if (value_.is_nonempty()) | 92 if (value_.is_nonempty()) |
| 96 return url_.spec().substr(value_.begin, value_.len); | 93 return url_.spec().substr(value_.begin, value_.len); |
| 97 return std::string(); | 94 return std::string(); |
| 98 } | 95 } |
| 99 | 96 |
| 100 const std::string& QueryIterator::GetUnescapedValue() { | 97 const std::string& QueryIterator::GetUnescapedValue() { |
| 101 DCHECK(!at_end_); | 98 DCHECK(!at_end_); |
| 102 if (value_.is_nonempty() && unescaped_value_.empty()) { | 99 if (value_.is_nonempty() && unescaped_value_.empty()) { |
| 103 unescaped_value_ = UnescapeURLComponent( | 100 unescaped_value_ = UnescapeURLComponent( |
| 104 GetValue(), | 101 GetValue(), UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS | |
| 105 UnescapeRule::SPACES | | 102 UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
| 106 UnescapeRule::URL_SPECIAL_CHARS | | |
| 107 UnescapeRule::REPLACE_PLUS_WITH_SPACE); | |
| 108 } | 103 } |
| 109 return unescaped_value_; | 104 return unescaped_value_; |
| 110 } | 105 } |
| 111 | 106 |
| 112 bool QueryIterator::IsAtEnd() const { | 107 bool QueryIterator::IsAtEnd() const { |
| 113 return at_end_; | 108 return at_end_; |
| 114 } | 109 } |
| 115 | 110 |
| 116 void QueryIterator::Advance() { | 111 void QueryIterator::Advance() { |
| 117 DCHECK (!at_end_); | 112 DCHECK(!at_end_); |
| 118 key_.reset(); | 113 key_.reset(); |
| 119 value_.reset(); | 114 value_.reset(); |
| 120 unescaped_value_.clear(); | 115 unescaped_value_.clear(); |
| 121 at_end_ = | 116 at_end_ = |
| 122 !url::ExtractQueryKeyValue(url_.spec().c_str(), &query_, &key_, &value_); | 117 !url::ExtractQueryKeyValue(url_.spec().c_str(), &query_, &key_, &value_); |
| 123 } | 118 } |
| 124 | 119 |
| 125 bool GetValueForKeyInQuery(const GURL& url, | 120 bool GetValueForKeyInQuery(const GURL& url, |
| 126 const std::string& search_key, | 121 const std::string& search_key, |
| 127 std::string* out_value) { | 122 std::string* out_value) { |
| 128 for (QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { | 123 for (QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { |
| 129 if (it.GetKey() == search_key) { | 124 if (it.GetKey() == search_key) { |
| 130 *out_value = it.GetUnescapedValue(); | 125 *out_value = it.GetUnescapedValue(); |
| 131 return true; | 126 return true; |
| 132 } | 127 } |
| 133 } | 128 } |
| 134 return false; | 129 return false; |
| 135 } | 130 } |
| 136 | 131 |
| 137 } // namespace net | 132 } // namespace net |
| OLD | NEW |