Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: ios/chrome/common/x_callback_url.cc

Issue 2485573003: Functions to create and manipulate x-callback-url URLs. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "ios/chrome/common/x_callback_url.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/base/escape.h"
11 #include "net/base/url_util.h"
12
13 namespace {
14
15 const char kXCallbackURLHost[] = "x-callback-url";
16 const char kSuccessURLParameterName[] = "x-success";
17 const char kErrorURLParameterName[] = "x-error";
18 const char kCancelURLParameterName[] = "x-cancel";
19
20 } // namespace
21
22 bool IsXCallbackURL(const GURL& url) {
23 return url.is_valid() && url.host_piece() == kXCallbackURLHost;
24 }
25
26 GURL CreateXCallbackURL(base::StringPiece scheme, base::StringPiece action) {
27 return CreateXCallbackURLWithParameters(scheme, action, GURL(), GURL(),
28 GURL(),
29 std::map<std::string, std::string>());
30 }
31
32 GURL CreateXCallbackURLWithParameters(
33 base::StringPiece scheme,
34 base::StringPiece action,
35 const GURL& success_url,
36 const GURL& error_url,
37 const GURL& cancel_url,
38 const std::map<std::string, std::string>& parameters) {
39 DCHECK(!scheme.empty());
40 GURL url(base::StringPrintf("%s://%s/%s", scheme.data(), kXCallbackURLHost,
rohitrao (ping after 24h) 2016/11/07 17:58:17 Is there any way to do this using GURL primitives,
sdefresne 2016/11/08 07:15:28 I've tried the following: GURL url; GURL::Replace
41 !action.empty() ? action.data() : ""));
42
43 if (success_url.is_valid()) {
44 url = net::AppendQueryParameter(url, kSuccessURLParameterName,
rohitrao (ping after 24h) 2016/11/07 17:58:17 Does AppendQueryParameter handle escaping?
mef 2016/11/07 18:42:10 Yes, Unsafe characters in the name and the value
45 success_url.spec());
46 }
47
48 if (error_url.is_valid()) {
49 url = net::AppendQueryParameter(url, kErrorURLParameterName,
50 error_url.spec());
51 }
52
53 if (cancel_url.is_valid()) {
54 url = net::AppendQueryParameter(url, kCancelURLParameterName,
55 cancel_url.spec());
56 }
57
58 if (!parameters.empty()) {
59 for (const auto& pair : parameters) {
60 url = net::AppendQueryParameter(url, pair.first, pair.second);
61 }
62 }
63
64 return url;
65 }
66
67 std::map<std::string, std::string> ExtractQueryParametersFromXCallbackURL(
68 const GURL& x_callback_url) {
69 DCHECK(IsXCallbackURL(x_callback_url));
70 std::map<std::string, std::string> parameters;
71
72 net::QueryIterator query_iterator(x_callback_url);
73 while (!query_iterator.IsAtEnd()) {
74 parameters.insert(std::make_pair(query_iterator.GetKey(),
75 query_iterator.GetUnescapedValue()));
76 query_iterator.Advance();
77 }
78
79 return parameters;
80 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698