| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/services/network/http_server_impl.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "mojo/services/network/http_connection_impl.h" | |
| 11 #include "mojo/services/network/net_adapters.h" | |
| 12 #include "mojo/services/network/net_address_type_converters.h" | |
| 13 #include "net/base/ip_endpoint.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/log/net_log.h" | |
| 16 #include "net/socket/tcp_server_socket.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int kBackLog = 10; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 // static | |
| 27 void HttpServerImpl::Create( | |
| 28 NetAddressPtr local_address, | |
| 29 HttpServerDelegatePtr delegate, | |
| 30 scoped_ptr<mojo::MessageLoopRef> app_refcount, | |
| 31 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { | |
| 32 HttpServerImpl* http_server = | |
| 33 new HttpServerImpl(std::move(delegate), std::move(app_refcount)); | |
| 34 | |
| 35 int net_error = http_server->Start(std::move(local_address)); | |
| 36 if (net_error != net::OK) { | |
| 37 callback.Run(MakeNetworkError(net_error), nullptr); | |
| 38 delete http_server; | |
| 39 return; | |
| 40 } | |
| 41 callback.Run(MakeNetworkError(net::OK), http_server->GetLocalAddress()); | |
| 42 } | |
| 43 | |
| 44 HttpServerImpl::HttpServerImpl(HttpServerDelegatePtr delegate, | |
| 45 scoped_ptr<mojo::MessageLoopRef> app_refcount) | |
| 46 : delegate_(std::move(delegate)), app_refcount_(std::move(app_refcount)) { | |
| 47 DCHECK(delegate_); | |
| 48 delegate_.set_connection_error_handler([this]() { delete this; }); | |
| 49 } | |
| 50 | |
| 51 HttpServerImpl::~HttpServerImpl() {} | |
| 52 | |
| 53 int HttpServerImpl::Start(NetAddressPtr local_address) { | |
| 54 DCHECK(local_address); | |
| 55 | |
| 56 scoped_ptr<net::ServerSocket> socket( | |
| 57 new net::TCPServerSocket(nullptr, net::NetLog::Source())); | |
| 58 int net_result = socket->Listen(local_address.To<net::IPEndPoint>(), | |
| 59 kBackLog); | |
| 60 if (net_result != net::OK) | |
| 61 return net_result; | |
| 62 | |
| 63 server_.reset(new net::HttpServer(std::move(socket), this)); | |
| 64 | |
| 65 return net::OK; | |
| 66 } | |
| 67 | |
| 68 NetAddressPtr HttpServerImpl::GetLocalAddress() const { | |
| 69 if (!server_) | |
| 70 return nullptr; | |
| 71 | |
| 72 net::IPEndPoint address; | |
| 73 int net_result = server_->GetLocalAddress(&address); | |
| 74 if (net_result != net::OK) | |
| 75 return nullptr; | |
| 76 | |
| 77 return NetAddress::From(address); | |
| 78 } | |
| 79 | |
| 80 void HttpServerImpl::OnConnect(int connection_id) { | |
| 81 DCHECK(connections_.find(connection_id) == connections_.end()); | |
| 82 | |
| 83 HttpConnectionPtr connection; | |
| 84 HttpConnectionDelegatePtr connection_delegate; | |
| 85 InterfaceRequest<HttpConnectionDelegate> delegate_request = | |
| 86 GetProxy(&connection_delegate); | |
| 87 linked_ptr<HttpConnectionImpl> connection_impl(new HttpConnectionImpl( | |
| 88 connection_id, this, std::move(connection_delegate), &connection)); | |
| 89 | |
| 90 connections_[connection_id] = connection_impl; | |
| 91 | |
| 92 delegate_->OnConnected(std::move(connection), std::move(delegate_request)); | |
| 93 } | |
| 94 | |
| 95 void HttpServerImpl::OnHttpRequest(int connection_id, | |
| 96 const net::HttpServerRequestInfo& info) { | |
| 97 DCHECK(connections_.find(connection_id) != connections_.end()); | |
| 98 connections_[connection_id]->OnReceivedHttpRequest(info); | |
| 99 } | |
| 100 | |
| 101 void HttpServerImpl::OnWebSocketRequest( | |
| 102 int connection_id, | |
| 103 const net::HttpServerRequestInfo& info) { | |
| 104 DCHECK(connections_.find(connection_id) != connections_.end()); | |
| 105 connections_[connection_id]->OnReceivedWebSocketRequest(info); | |
| 106 } | |
| 107 | |
| 108 void HttpServerImpl::OnWebSocketMessage(int connection_id, | |
| 109 const std::string& data) { | |
| 110 DCHECK(connections_.find(connection_id) != connections_.end()); | |
| 111 connections_[connection_id]->OnReceivedWebSocketMessage(data); | |
| 112 } | |
| 113 | |
| 114 void HttpServerImpl::OnClose(int connection_id) { | |
| 115 DCHECK(connections_.find(connection_id) != connections_.end()); | |
| 116 connections_.erase(connection_id); | |
| 117 } | |
| 118 | |
| 119 } // namespace mojo | |
| OLD | NEW |