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

Side by Side Diff: remoting/protocol/http_ice_config_request.cc

Issue 1707223002: Implement HttpIceConfigRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months 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 "remoting/protocol/http_ice_config_request.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/json/json_reader.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "net/base/url_util.h"
14 #include "remoting/protocol/ice_config.h"
15
16 namespace remoting {
17 namespace protocol {
18
19 namespace {
20
21 // Ensure ICE config is correct at least one hour after session starts.
22 const int kMinimumConfigLifetimeSeconds = 3600;
23
24 // See draft-petithuguenin-behave-turn-uris-01.
25 const int kDefaultStunTurnPort = 3478;
26 const int kDefaultTurnsPort = 5349;
27
28 bool ParseLifetime(const std::string& string, base::TimeDelta* result) {
29 double seconds = 0;
30 if (!base::EndsWith(string, "s", base::CompareCase::INSENSITIVE_ASCII) ||
31 !base::StringToDouble(string.substr(0, string.size() - 1), &seconds)) {
32 return false;
33 }
34 *result = base::TimeDelta::FromSecondsD(seconds);
35 return true;
36 }
37
38 // Parses url in form of <stun|turn|turns>:<host>[:<port>][?transport=<udp|tcp>]
39 // and adds an entry to the |config|.
40 bool AddServerToConfig(std::string url,
41 const std::string& username,
42 const std::string& password,
43 IceConfig* config) {
44 cricket::ProtocolType turn_transport_type = cricket::PROTO_LAST;
45
46 const char kTcpTransportSuffix[] = "?transport=tcp";
47 const char kUdpTransportSuffix[] = "?transport=udp";
48 if (base::EndsWith(url, kTcpTransportSuffix,
49 base::CompareCase::INSENSITIVE_ASCII)) {
50 turn_transport_type = cricket::PROTO_TCP;
51 url.resize(url.size() - strlen(kTcpTransportSuffix));
52 } else if (base::EndsWith(url, kUdpTransportSuffix,
53 base::CompareCase::INSENSITIVE_ASCII)) {
54 turn_transport_type = cricket::PROTO_UDP;
55 url.resize(url.size() - strlen(kUdpTransportSuffix));
56 }
57
58 size_t colon_pos = url.find(':');
59 if (colon_pos == std::string::npos)
60 return false;
61
62 std::string protocol = url.substr(0, colon_pos);
63
64 std::string host;
65 int port;
66 if (!net::ParseHostAndPort(url.substr(colon_pos + 1), &host, &port))
67 return false;
68
69 if (protocol == "stun") {
70 if (port == -1)
71 port = kDefaultStunTurnPort;
72 config->stun_servers.push_back(rtc::SocketAddress(host, port));
73 } else if (protocol == "turn") {
74 if (port == -1)
75 port = kDefaultStunTurnPort;
76 if (turn_transport_type == cricket::PROTO_LAST)
77 turn_transport_type = cricket::PROTO_UDP;
78 config->turn_servers.push_back(cricket::RelayServerConfig(
79 host, port, username, password, turn_transport_type, false));
80 } else if (protocol == "turns") {
81 if (port == -1)
82 port = kDefaultTurnsPort;
83 if (turn_transport_type == cricket::PROTO_LAST)
84 turn_transport_type = cricket::PROTO_TCP;
85 config->turn_servers.push_back(cricket::RelayServerConfig(
86 host, port, username, password, turn_transport_type, true));
87 } else {
88 return false;
89 }
90
91 return true;
92 }
93
94 } // namespace
95
96 HttpIceConfigRequest::HttpIceConfigRequest(
97 UrlRequestFactory* url_request_factory,
98 const std::string& url)
99 : url_(url) {
100 url_request_ =
101 url_request_factory->CreateUrlRequest(UrlRequest::Type::POST, url_);
102 url_request_->SetPostData("application/json", "");
103 }
104
105 HttpIceConfigRequest::~HttpIceConfigRequest() {}
106
107 void HttpIceConfigRequest::Send(const OnIceConfigCallback& callback) {
108 DCHECK(on_ice_config_callback_.is_null());
109 DCHECK(!callback.is_null());
110
111 on_ice_config_callback_ = callback;
112 url_request_->Start(
113 base::Bind(&HttpIceConfigRequest::OnResponse, base::Unretained(this)));
114 }
115
116 void HttpIceConfigRequest::OnResponse(const UrlRequest::Result& result) {
117 DCHECK(!on_ice_config_callback_.is_null());
118
119 if (!result.success) {
120 LOG(ERROR) << "Failed to fetch " << url_;
121 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig());
122 return;
123 }
124
125 if (result.status != 200) {
126 LOG(ERROR) << "Received status code " << result.status << " from " << url_
127 << ": " << result.response_body;
128 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig());
129 return;
130 }
131
132 scoped_ptr<base::Value> json = base::JSONReader::Read(result.response_body);
133 base::DictionaryValue* dictionary = nullptr;
134 base::ListValue* ice_servers_list = nullptr;
135 if (!json || !json->GetAsDictionary(&dictionary) ||
136 !dictionary->GetList("iceServers", &ice_servers_list)) {
137 LOG(ERROR) << "Received invalid response from " << url_ << ": "
138 << result.response_body;
139 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig());
140 return;
141 }
142
143 IceConfig ice_config;
144
145 // Parse lifetimeDuration field.
146 std::string lifetime_str;
147 base::TimeDelta lifetime;
148 if (!dictionary->GetString("lifetimeDuration", &lifetime_str) ||
149 !ParseLifetime(lifetime_str, &lifetime)) {
150 LOG(ERROR) << "Received invalid lifetimeDuration value: " << lifetime_str;
151
152 // If the |lifetimeDuration| field is missing or cannot be parsed then mark
153 // the config as expired so it will refreshed for the next session.
154 ice_config.expiration_time = base::Time::Now();
155 } else {
156 ice_config.expiration_time =
157 base::Time::Now() + lifetime -
158 base::TimeDelta::FromSeconds(kMinimumConfigLifetimeSeconds);
159 }
160
161 // Parse iceServers list and store them in |ice_config|.
162 bool errors_found = false;
163 for (base::Value* server : *ice_servers_list) {
164 base::DictionaryValue* server_dict;
165 if (!server->GetAsDictionary(&server_dict)) {
166 errors_found = true;
167 continue;
168 }
169
170 base::ListValue* urls_list = nullptr;
171 if (!server_dict->GetList("urls", &urls_list)) {
172 errors_found = true;
173 continue;
174 }
175
176 std::string username;
177 server_dict->GetString("username", &username);
178
179 std::string password;
180 server_dict->GetString("credential", &password);
181
182 for (base::Value* url : *urls_list) {
183 std::string url_str;
184 if (!url->GetAsString(&url_str)) {
185 errors_found = true;
186 continue;
187 }
188 if (!AddServerToConfig(url_str, username, password, &ice_config)) {
189 LOG(ERROR) << "Invalid ICE server URL: " << url_str;
190 }
191 }
192 }
193
194 if (errors_found) {
195 LOG(ERROR) << "Received ICE config from the server that contained errors: "
196 << result.response_body;
197 }
198
199 // If there are no STUN or no TURN servers then mark the config as expired so
200 // it will refreshed for the next session.
201 if (errors_found || ice_config.stun_servers.empty() ||
202 ice_config.turn_servers.empty()) {
203 ice_config.expiration_time = base::Time::Now();
204 }
205
206 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config);
207 }
208
209 } // namespace protocol
210 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/http_ice_config_request.h ('k') | remoting/protocol/http_ice_config_request_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698