OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/p2p/client/httpportallocator.h" | 11 #include "webrtc/p2p/client/httpportallocator.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <map> | 14 #include <map> |
15 | 15 |
16 #include "webrtc/base/asynchttprequest.h" | |
17 #include "webrtc/base/basicdefs.h" | 16 #include "webrtc/base/basicdefs.h" |
18 #include "webrtc/base/common.h" | 17 #include "webrtc/base/common.h" |
19 #include "webrtc/base/helpers.h" | 18 #include "webrtc/base/helpers.h" |
| 19 #include "webrtc/base/httpcommon.h" |
20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/nethelpers.h" | 21 #include "webrtc/base/nethelpers.h" |
22 #include "webrtc/base/signalthread.h" | 22 #include "webrtc/base/signalthread.h" |
23 #include "webrtc/base/stringencode.h" | 23 #include "webrtc/base/stringencode.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Helper routine to remove whitespace from the ends of a string. | 27 // Helper routine to remove whitespace from the ends of a string. |
28 void Trim(std::string& str) { | 28 void Trim(std::string& str) { |
29 size_t first = str.find_first_not_of(" \t\r\n"); | 29 size_t first = str.find_first_not_of(" \t\r\n"); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 ConfigReady(config); | 137 ConfigReady(config); |
138 TryCreateRelaySession(); | 138 TryCreateRelaySession(); |
139 } | 139 } |
140 | 140 |
141 void HttpPortAllocatorSessionBase::TryCreateRelaySession() { | 141 void HttpPortAllocatorSessionBase::TryCreateRelaySession() { |
142 if (allocator()->flags() & PORTALLOCATOR_DISABLE_RELAY) { | 142 if (allocator()->flags() & PORTALLOCATOR_DISABLE_RELAY) { |
143 LOG(LS_VERBOSE) << "HttpPortAllocator: Relay ports disabled, skipping."; | 143 LOG(LS_VERBOSE) << "HttpPortAllocator: Relay ports disabled, skipping."; |
144 return; | 144 return; |
145 } | 145 } |
146 | 146 |
147 if (attempts_ == HttpPortAllocator::kNumRetries) { | 147 if (attempts_ == HttpPortAllocatorBase::kNumRetries) { |
148 LOG(LS_ERROR) << "HttpPortAllocator: maximum number of requests reached; " | 148 LOG(LS_ERROR) << "HttpPortAllocator: maximum number of requests reached; " |
149 << "giving up on relay."; | 149 << "giving up on relay."; |
150 return; | 150 return; |
151 } | 151 } |
152 | 152 |
153 if (relay_hosts_.size() == 0) { | 153 if (relay_hosts_.size() == 0) { |
154 LOG(LS_ERROR) << "HttpPortAllocator: no relay hosts configured."; | 154 LOG(LS_ERROR) << "HttpPortAllocator: no relay hosts configured."; |
155 return; | 155 return; |
156 } | 156 } |
157 | 157 |
158 // Choose the next host to try. | 158 // Choose the next host to try. |
159 std::string host = relay_hosts_[attempts_ % relay_hosts_.size()]; | 159 std::string host = relay_hosts_[attempts_ % relay_hosts_.size()]; |
160 attempts_++; | 160 attempts_++; |
161 LOG(LS_INFO) << "HTTPPortAllocator: sending to relay host " << host; | 161 LOG(LS_INFO) << "HTTPPortAllocator: sending to relay host " << host; |
162 if (relay_token_.empty()) { | 162 if (relay_token_.empty()) { |
163 LOG(LS_WARNING) << "No relay auth token found."; | 163 LOG(LS_WARNING) << "No relay auth token found."; |
164 } | 164 } |
165 | 165 |
166 SendSessionRequest(host, rtc::HTTP_SECURE_PORT); | 166 SendSessionRequest(host, rtc::HTTP_SECURE_PORT); |
167 } | 167 } |
168 | 168 |
169 std::string HttpPortAllocatorSessionBase::GetSessionRequestUrl() { | 169 std::string HttpPortAllocatorSessionBase::GetSessionRequestUrl() { |
170 std::string url = std::string(HttpPortAllocator::kCreateSessionURL); | 170 std::string url = std::string(HttpPortAllocatorBase::kCreateSessionURL); |
171 ASSERT(!username().empty()); | 171 ASSERT(!username().empty()); |
172 ASSERT(!password().empty()); | 172 ASSERT(!password().empty()); |
173 url = url + "?username=" + rtc::s_url_encode(username()) + | 173 url = url + "?username=" + rtc::s_url_encode(username()) + |
174 "&password=" + rtc::s_url_encode(password()); | 174 "&password=" + rtc::s_url_encode(password()); |
175 return url; | 175 return url; |
176 } | 176 } |
177 | 177 |
178 void HttpPortAllocatorSessionBase::ReceiveSessionResponse( | 178 void HttpPortAllocatorSessionBase::ReceiveSessionResponse( |
179 const std::string& response) { | 179 const std::string& response) { |
180 | 180 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 relay_config.ports.push_back(ProtocolAddress(address, PROTO_TCP)); | 213 relay_config.ports.push_back(ProtocolAddress(address, PROTO_TCP)); |
214 } | 214 } |
215 if (!relay_ssltcp_port.empty()) { | 215 if (!relay_ssltcp_port.empty()) { |
216 rtc::SocketAddress address(relay_ip, atoi(relay_ssltcp_port.c_str())); | 216 rtc::SocketAddress address(relay_ip, atoi(relay_ssltcp_port.c_str())); |
217 relay_config.ports.push_back(ProtocolAddress(address, PROTO_SSLTCP)); | 217 relay_config.ports.push_back(ProtocolAddress(address, PROTO_SSLTCP)); |
218 } | 218 } |
219 config->AddRelay(relay_config); | 219 config->AddRelay(relay_config); |
220 ConfigReady(config); | 220 ConfigReady(config); |
221 } | 221 } |
222 | 222 |
223 // HttpPortAllocator | |
224 | |
225 HttpPortAllocator::HttpPortAllocator( | |
226 rtc::NetworkManager* network_manager, | |
227 rtc::PacketSocketFactory* socket_factory, | |
228 const std::string &user_agent) | |
229 : HttpPortAllocatorBase(network_manager, socket_factory, user_agent) { | |
230 } | |
231 | |
232 HttpPortAllocator::HttpPortAllocator( | |
233 rtc::NetworkManager* network_manager, | |
234 const std::string &user_agent) | |
235 : HttpPortAllocatorBase(network_manager, user_agent) { | |
236 } | |
237 HttpPortAllocator::~HttpPortAllocator() {} | |
238 | |
239 PortAllocatorSession* HttpPortAllocator::CreateSessionInternal( | |
240 const std::string& content_name, | |
241 int component, | |
242 const std::string& ice_ufrag, const std::string& ice_pwd) { | |
243 return new HttpPortAllocatorSession(this, content_name, component, | |
244 ice_ufrag, ice_pwd, stun_hosts(), | |
245 relay_hosts(), relay_token(), | |
246 user_agent()); | |
247 } | |
248 | |
249 // HttpPortAllocatorSession | |
250 | |
251 HttpPortAllocatorSession::HttpPortAllocatorSession( | |
252 HttpPortAllocator* allocator, | |
253 const std::string& content_name, | |
254 int component, | |
255 const std::string& ice_ufrag, | |
256 const std::string& ice_pwd, | |
257 const std::vector<rtc::SocketAddress>& stun_hosts, | |
258 const std::vector<std::string>& relay_hosts, | |
259 const std::string& relay, | |
260 const std::string& agent) | |
261 : HttpPortAllocatorSessionBase(allocator, content_name, component, | |
262 ice_ufrag, ice_pwd, stun_hosts, | |
263 relay_hosts, relay, agent) { | |
264 } | |
265 | |
266 HttpPortAllocatorSession::~HttpPortAllocatorSession() { | |
267 for (std::list<rtc::AsyncHttpRequest*>::iterator it = requests_.begin(); | |
268 it != requests_.end(); ++it) { | |
269 (*it)->Destroy(true); | |
270 } | |
271 } | |
272 | |
273 void HttpPortAllocatorSession::SendSessionRequest(const std::string& host, | |
274 int port) { | |
275 // Initiate an HTTP request to create a session through the chosen host. | |
276 rtc::AsyncHttpRequest* request = | |
277 new rtc::AsyncHttpRequest(user_agent()); | |
278 request->SignalWorkDone.connect(this, | |
279 &HttpPortAllocatorSession::OnRequestDone); | |
280 | |
281 request->set_secure(port == rtc::HTTP_SECURE_PORT); | |
282 request->set_proxy(allocator()->proxy()); | |
283 request->response().document.reset(new rtc::MemoryStream); | |
284 request->request().verb = rtc::HV_GET; | |
285 request->request().path = GetSessionRequestUrl(); | |
286 request->request().addHeader("X-Talk-Google-Relay-Auth", relay_token(), true); | |
287 request->request().addHeader("X-Stream-Type", "video_rtp", true); | |
288 request->set_host(host); | |
289 request->set_port(port); | |
290 request->Start(); | |
291 request->Release(); | |
292 | |
293 requests_.push_back(request); | |
294 } | |
295 | |
296 void HttpPortAllocatorSession::OnRequestDone(rtc::SignalThread* data) { | |
297 rtc::AsyncHttpRequest* request = | |
298 static_cast<rtc::AsyncHttpRequest*>(data); | |
299 | |
300 // Remove the request from the list of active requests. | |
301 std::list<rtc::AsyncHttpRequest*>::iterator it = | |
302 std::find(requests_.begin(), requests_.end(), request); | |
303 if (it != requests_.end()) { | |
304 requests_.erase(it); | |
305 } | |
306 | |
307 if (request->response().scode != 200) { | |
308 LOG(LS_WARNING) << "HTTPPortAllocator: request " | |
309 << " received error " << request->response().scode; | |
310 TryCreateRelaySession(); | |
311 return; | |
312 } | |
313 LOG(LS_INFO) << "HTTPPortAllocator: request succeeded"; | |
314 | |
315 rtc::MemoryStream* stream = | |
316 static_cast<rtc::MemoryStream*>(request->response().document.get()); | |
317 stream->Rewind(); | |
318 size_t length; | |
319 stream->GetSize(&length); | |
320 std::string resp = std::string(stream->GetBuffer(), length); | |
321 ReceiveSessionResponse(resp); | |
322 } | |
323 | |
324 } // namespace cricket | 223 } // namespace cricket |
OLD | NEW |