OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/http_server/http_server_factory_impl.h" | 5 #include "services/http_server/http_server_factory_impl.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "services/http_server/http_server_impl.h" | 8 #include "services/http_server/http_server_impl.h" |
9 #include "services/http_server/public/http_server.mojom.h" | 9 #include "services/http_server/public/http_server.mojom.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 STLDeleteContainerPointers(port_any_servers_.begin(), | 21 STLDeleteContainerPointers(port_any_servers_.begin(), |
22 port_any_servers_.end()); | 22 port_any_servers_.end()); |
23 } | 23 } |
24 | 24 |
25 void HttpServerFactoryImpl::AddBinding( | 25 void HttpServerFactoryImpl::AddBinding( |
26 mojo::InterfaceRequest<HttpServerFactory> request) { | 26 mojo::InterfaceRequest<HttpServerFactory> request) { |
27 bindings_.AddBinding(this, request.Pass()); | 27 bindings_.AddBinding(this, request.Pass()); |
28 } | 28 } |
29 | 29 |
30 void HttpServerFactoryImpl::DeleteServer(HttpServerImpl* server, | 30 void HttpServerFactoryImpl::DeleteServer(HttpServerImpl* server, |
31 uint16_t requested_port) { | 31 mojo::NetAddress* requested_address) { |
32 if (requested_port) { | 32 ServerKey key = GetServerKey(requested_address); |
33 DCHECK(port_indicated_servers_.count(requested_port)); | 33 |
34 DCHECK_EQ(server, port_indicated_servers_[requested_port]); | 34 if (key.second) { // If the port is non-zero. |
| 35 DCHECK(port_indicated_servers_.count(key)); |
| 36 DCHECK_EQ(server, port_indicated_servers_[key]); |
35 | 37 |
36 delete server; | 38 delete server; |
37 port_indicated_servers_.erase(requested_port); | 39 port_indicated_servers_.erase(key); |
38 } else { | 40 } else { |
39 DCHECK(port_any_servers_.count(server)); | 41 DCHECK(port_any_servers_.count(server)); |
40 | 42 |
41 delete server; | 43 delete server; |
42 port_any_servers_.erase(server); | 44 port_any_servers_.erase(server); |
43 } | 45 } |
44 } | 46 } |
45 | 47 |
| 48 HttpServerFactoryImpl::ServerKey HttpServerFactoryImpl::GetServerKey( |
| 49 mojo::NetAddress* local_address) { |
| 50 DCHECK(local_address); |
| 51 |
| 52 if (local_address->family == mojo::NET_ADDRESS_FAMILY_IPV6) { |
| 53 return ServerKey(local_address->ipv6->addr, local_address->ipv6->port); |
| 54 } else if (local_address->family == mojo::NET_ADDRESS_FAMILY_IPV4) { |
| 55 return ServerKey(local_address->ipv4->addr, local_address->ipv4->port); |
| 56 } else { |
| 57 return ServerKey(); |
| 58 } |
| 59 } |
| 60 |
46 void HttpServerFactoryImpl::CreateHttpServer( | 61 void HttpServerFactoryImpl::CreateHttpServer( |
47 mojo::InterfaceRequest<HttpServer> server_request, | 62 mojo::InterfaceRequest<HttpServer> server_request, |
48 uint16_t port) { | 63 mojo::NetAddressPtr local_address) { |
49 if (port) { | 64 if (!local_address) { |
50 if (!port_indicated_servers_.count(port)) { | 65 local_address = mojo::NetAddress::New(); |
51 port_indicated_servers_[port] = new HttpServerImpl(app_, this, port); | 66 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4; |
| 67 local_address->ipv4 = mojo::NetAddressIPv4::New(); |
| 68 local_address->ipv4->addr.resize(4); |
| 69 local_address->ipv4->addr[0] = 0; |
| 70 local_address->ipv4->addr[1] = 0; |
| 71 local_address->ipv4->addr[2] = 0; |
| 72 local_address->ipv4->addr[3] = 0; |
| 73 local_address->ipv4->port = 0; |
| 74 } |
| 75 ServerKey key = GetServerKey(local_address.get()); |
| 76 |
| 77 if (key.second) { // If the port is non-zero. |
| 78 if (!port_indicated_servers_.count(key)) { |
| 79 port_indicated_servers_[key] = |
| 80 new HttpServerImpl(app_, this, local_address.Pass()); |
52 } | 81 } |
53 port_indicated_servers_[port]->AddBinding(server_request.Pass()); | 82 port_indicated_servers_[key]->AddBinding(server_request.Pass()); |
54 } else { | 83 } else { |
55 HttpServerImpl* server = new HttpServerImpl(app_, this, port); | 84 HttpServerImpl* server = |
| 85 new HttpServerImpl(app_, this, local_address.Pass()); |
56 server->AddBinding(server_request.Pass()); | 86 server->AddBinding(server_request.Pass()); |
57 port_any_servers_.insert(server); | 87 port_any_servers_.insert(server); |
58 } | 88 } |
59 } | 89 } |
60 | 90 |
61 } // namespace http_server | 91 } // namespace http_server |
OLD | NEW |