blob: 60da71420645c866e0c85f26554a26b3f8c6aa79 [file] [log] [blame]
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +01001// Copyright 2015 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 "services/http_server/http_server_factory_impl.h"
6
7#include "base/stl_util.h"
Viet-Trung Luu84765c42015-10-10 01:07:51 -07008#include "mojo/services/http_server/interfaces/http_server.mojom.h"
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +01009#include "services/http_server/http_server_impl.h"
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010010
11namespace http_server {
12
13HttpServerFactoryImpl::HttpServerFactoryImpl(mojo::ApplicationImpl* app) {
14 app_ = app;
15}
16
17HttpServerFactoryImpl::~HttpServerFactoryImpl() {
18 // Free the http servers.
19 STLDeleteContainerPairSecondPointers(port_indicated_servers_.begin(),
20 port_indicated_servers_.end());
21 STLDeleteContainerPointers(port_any_servers_.begin(),
22 port_any_servers_.end());
23}
24
25void HttpServerFactoryImpl::AddBinding(
26 mojo::InterfaceRequest<HttpServerFactory> request) {
27 bindings_.AddBinding(this, request.Pass());
28}
29
30void HttpServerFactoryImpl::DeleteServer(HttpServerImpl* server,
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010031 mojo::NetAddress* requested_address) {
32 ServerKey key = GetServerKey(requested_address);
33
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]);
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010037
38 delete server;
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010039 port_indicated_servers_.erase(key);
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010040 } else {
41 DCHECK(port_any_servers_.count(server));
42
43 delete server;
44 port_any_servers_.erase(server);
45 }
46}
47
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010048HttpServerFactoryImpl::ServerKey HttpServerFactoryImpl::GetServerKey(
49 mojo::NetAddress* local_address) {
50 DCHECK(local_address);
51
John Grossman87fe5142015-10-02 10:11:43 -070052 if (local_address->family == mojo::NetAddressFamily::IPV6) {
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010053 return ServerKey(local_address->ipv6->addr, local_address->ipv6->port);
John Grossman87fe5142015-10-02 10:11:43 -070054 } else if (local_address->family == mojo::NetAddressFamily::IPV4) {
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010055 return ServerKey(local_address->ipv4->addr, local_address->ipv4->port);
56 } else {
57 return ServerKey();
58 }
59}
60
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010061void HttpServerFactoryImpl::CreateHttpServer(
62 mojo::InterfaceRequest<HttpServer> server_request,
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010063 mojo::NetAddressPtr local_address) {
64 if (!local_address) {
65 local_address = mojo::NetAddress::New();
John Grossman87fe5142015-10-02 10:11:43 -070066 local_address->family = mojo::NetAddressFamily::IPV4;
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010067 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());
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010081 }
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010082 port_indicated_servers_[key]->AddBinding(server_request.Pass());
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010083 } else {
Przemyslaw Pietrzkiewicz8d249d22015-02-24 17:33:42 +010084 HttpServerImpl* server =
85 new HttpServerImpl(app_, this, local_address.Pass());
Przemyslaw Pietrzkiewiczf2cf19d2015-02-13 16:45:13 +010086 server->AddBinding(server_request.Pass());
87 port_any_servers_.insert(server);
88 }
89}
90
91} // namespace http_server