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
3 // found in the LICENSE file.
5 #include "net/base/url_util.h"
7 #include "base/strings/string_piece.h"
8 #include "net/base/escape.h"
13 GURL
AppendQueryParameter(const GURL
& url
,
14 const std::string
& name
,
15 const std::string
& value
) {
16 std::string
query(url
.query());
21 query
+= (EscapeQueryParamValue(name
, true) + "=" +
22 EscapeQueryParamValue(value
, true));
23 GURL::Replacements replacements
;
24 replacements
.SetQueryStr(query
);
25 return url
.ReplaceComponents(replacements
);
28 GURL
AppendOrReplaceQueryParameter(const GURL
& url
,
29 const std::string
& name
,
30 const std::string
& value
) {
31 bool replaced
= false;
32 std::string param_name
= EscapeQueryParamValue(name
, true);
33 std::string param_value
= EscapeQueryParamValue(value
, true);
35 const std::string input
= url
.query();
36 url_parse::Component
cursor(0, input
.size());
38 url_parse::Component key_range
, value_range
;
39 while (url_parse::ExtractQueryKeyValue(
40 input
.data(), &cursor
, &key_range
, &value_range
)) {
41 const base::StringPiece
key(
42 input
.data() + key_range
.begin
, key_range
.len
);
43 const base::StringPiece
value(
44 input
.data() + value_range
.begin
, value_range
.len
);
45 std::string key_value_pair
;
46 // Check |replaced| as only the first pair should be replaced.
47 if (!replaced
&& key
== param_name
) {
49 key_value_pair
= (param_name
+ "=" + param_value
);
51 key_value_pair
.assign(input
.data(),
53 value_range
.end() - key_range
.begin
);
58 output
+= key_value_pair
;
64 output
+= (param_name
+ "=" + param_value
);
66 GURL::Replacements replacements
;
67 replacements
.SetQueryStr(output
);
68 return url
.ReplaceComponents(replacements
);
71 bool GetValueForKeyInQuery(const GURL
& url
,
72 const std::string
& search_key
,
73 std::string
* out_value
) {
77 url_parse::Component query
= url
.parsed_for_possibly_invalid_spec().query
;
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
);