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