| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_impl.h" | 5 #include "services/http_server/http_server_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/bindings/error_handler.h" | 10 #include "mojo/public/cpp/bindings/error_handler.h" |
| 11 #include "mojo/public/cpp/system/data_pipe.h" | 11 #include "mojo/public/cpp/system/data_pipe.h" |
| 12 #include "services/http_server/connection.h" | 12 #include "services/http_server/connection.h" |
| 13 #include "services/http_server/http_server_factory_impl.h" | 13 #include "services/http_server/http_server_factory_impl.h" |
| 14 #include "services/http_server/public/http_server_util.h" | 14 #include "services/http_server/public/http_server_util.h" |
| 15 | 15 |
| 16 namespace http_server { | 16 namespace http_server { |
| 17 | 17 |
| 18 HttpServerImpl::HttpServerImpl(mojo::ApplicationImpl* app, | 18 HttpServerImpl::HttpServerImpl(mojo::ApplicationImpl* app, |
| 19 HttpServerFactoryImpl* factory, | 19 HttpServerFactoryImpl* factory, |
| 20 uint16_t port) | 20 mojo::NetAddressPtr requested_local_address) |
| 21 : factory_(factory), | 21 : factory_(factory), |
| 22 requested_port_(port), | 22 requested_local_address_(requested_local_address.Pass()), |
| 23 assigned_port_(0), | 23 assigned_port_(0), |
| 24 weak_ptr_factory_(this) { | 24 weak_ptr_factory_(this) { |
| 25 app->ConnectToService("mojo:network_service", &network_service_); | 25 app->ConnectToService("mojo:network_service", &network_service_); |
| 26 Start(); | 26 Start(); |
| 27 bindings_.set_error_handler(this); | 27 bindings_.set_error_handler(this); |
| 28 } | 28 } |
| 29 | 29 |
| 30 HttpServerImpl::~HttpServerImpl() { | 30 HttpServerImpl::~HttpServerImpl() { |
| 31 } | 31 } |
| 32 | 32 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 56 } | 56 } |
| 57 | 57 |
| 58 void HttpServerImpl::OnConnectionError() { | 58 void HttpServerImpl::OnConnectionError() { |
| 59 handlers_.erase( | 59 handlers_.erase( |
| 60 std::remove_if(handlers_.begin(), handlers_.end(), [](Handler* h) { | 60 std::remove_if(handlers_.begin(), handlers_.end(), [](Handler* h) { |
| 61 return h->http_handler.encountered_error(); | 61 return h->http_handler.encountered_error(); |
| 62 }), handlers_.end()); | 62 }), handlers_.end()); |
| 63 | 63 |
| 64 if (handlers_.empty()) { | 64 if (handlers_.empty()) { |
| 65 // The call deregisters the server from the factory and deletes |this|. | 65 // The call deregisters the server from the factory and deletes |this|. |
| 66 factory_->DeleteServer(this, requested_port_); | 66 factory_->DeleteServer(this, requested_local_address_.Clone()); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 void HttpServerImpl::OnSocketBound(mojo::NetworkErrorPtr err, | 70 void HttpServerImpl::OnSocketBound(mojo::NetworkErrorPtr err, |
| 71 mojo::NetAddressPtr bound_address) { | 71 mojo::NetAddressPtr bound_address) { |
| 72 if (err->code != 0) { | 72 if (err->code != 0) { |
| 73 LOG(ERROR) << "Failed to bind the socket, err = " << err->code; | 73 LOG(ERROR) << "Failed to bind the socket, err = " << err->code; |
| 74 return; | 74 return; |
| 75 } | 75 } |
| 76 | 76 |
| 77 assigned_port_ = bound_address->ipv4->port; | 77 assigned_port_ = bound_address->ipv4->port; |
| 78 DCHECK(requested_port_ == 0u || requested_port_ == assigned_port_); | |
| 79 | 78 |
| 80 for (GetPortCallback& port_callback : pending_get_port_callbacks_) { | 79 for (GetPortCallback& port_callback : pending_get_port_callbacks_) { |
| 81 port_callback.Run(assigned_port_); | 80 port_callback.Run(assigned_port_); |
| 82 } | 81 } |
| 83 pending_get_port_callbacks_.clear(); | 82 pending_get_port_callbacks_.clear(); |
| 84 } | 83 } |
| 85 | 84 |
| 86 void HttpServerImpl::OnSocketListening(mojo::NetworkErrorPtr err) { | 85 void HttpServerImpl::OnSocketListening(mojo::NetworkErrorPtr err) { |
| 87 if (err->code != 0) { | 86 if (err->code != 0) { |
| 88 LOG(ERROR) << "Failed to listen on the socket, err = " << err->code; | 87 LOG(ERROR) << "Failed to listen on the socket, err = " << err->code; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 MOJO_ALLOW_UNUSED_LOCAL(result); | 121 MOJO_ALLOW_UNUSED_LOCAL(result); |
| 123 | 122 |
| 124 server_socket_->Accept(send_consumer_handle.Pass(), | 123 server_socket_->Accept(send_consumer_handle.Pass(), |
| 125 receive_producer_handle.Pass(), | 124 receive_producer_handle.Pass(), |
| 126 GetProxy(&pending_connected_socket_), | 125 GetProxy(&pending_connected_socket_), |
| 127 base::Bind(&HttpServerImpl::OnConnectionAccepted, | 126 base::Bind(&HttpServerImpl::OnConnectionAccepted, |
| 128 base::Unretained(this))); | 127 base::Unretained(this))); |
| 129 } | 128 } |
| 130 | 129 |
| 131 void HttpServerImpl::Start() { | 130 void HttpServerImpl::Start() { |
| 132 mojo::NetAddressPtr net_address(mojo::NetAddress::New()); | |
| 133 net_address->family = mojo::NET_ADDRESS_FAMILY_IPV4; | |
| 134 net_address->ipv4 = mojo::NetAddressIPv4::New(); | |
| 135 net_address->ipv4->addr.resize(4); | |
| 136 net_address->ipv4->addr[0] = 0; | |
| 137 net_address->ipv4->addr[1] = 0; | |
| 138 net_address->ipv4->addr[2] = 0; | |
| 139 net_address->ipv4->addr[3] = 0; | |
| 140 net_address->ipv4->port = requested_port_; | |
| 141 | |
| 142 // Note that we can start using the proxies right away even thought the | 131 // Note that we can start using the proxies right away even thought the |
| 143 // callbacks have not been called yet. If a previous step fails, they'll | 132 // callbacks have not been called yet. If a previous step fails, they'll |
| 144 // all fail. | 133 // all fail. |
| 145 network_service_->CreateTCPBoundSocket( | 134 network_service_->CreateTCPBoundSocket( |
| 146 net_address.Pass(), GetProxy(&bound_socket_), | 135 requested_local_address_.Clone(), GetProxy(&bound_socket_), |
| 147 base::Bind(&HttpServerImpl::OnSocketBound, base::Unretained(this))); | 136 base::Bind(&HttpServerImpl::OnSocketBound, base::Unretained(this))); |
| 148 bound_socket_->StartListening( | 137 bound_socket_->StartListening( |
| 149 GetProxy(&server_socket_), | 138 GetProxy(&server_socket_), |
| 150 base::Bind(&HttpServerImpl::OnSocketListening, base::Unretained(this))); | 139 base::Bind(&HttpServerImpl::OnSocketListening, base::Unretained(this))); |
| 151 WaitForNextConnection(); | 140 WaitForNextConnection(); |
| 152 } | 141 } |
| 153 | 142 |
| 154 void HttpServerImpl::HandleRequest(Connection* connection, | 143 void HttpServerImpl::HandleRequest(Connection* connection, |
| 155 HttpRequestPtr request) { | 144 HttpRequestPtr request) { |
| 156 for (auto& handler : handlers_) { | 145 for (auto& handler : handlers_) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 172 | 161 |
| 173 HttpServerImpl::Handler::Handler(const std::string& pattern, | 162 HttpServerImpl::Handler::Handler(const std::string& pattern, |
| 174 HttpHandlerPtr http_handler) | 163 HttpHandlerPtr http_handler) |
| 175 : pattern(new RE2(pattern.c_str())), http_handler(http_handler.Pass()) { | 164 : pattern(new RE2(pattern.c_str())), http_handler(http_handler.Pass()) { |
| 176 } | 165 } |
| 177 | 166 |
| 178 HttpServerImpl::Handler::~Handler() { | 167 HttpServerImpl::Handler::~Handler() { |
| 179 } | 168 } |
| 180 | 169 |
| 181 } // namespace http_server | 170 } // namespace http_server |
| OLD | NEW |