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 HttpServerImpl* HttpServerImpl::Create(HttpServerDelegatePtr delegate) { |
| 26 return new HttpServerImpl(delegate.Pass()); |
| 27 } |
| 28 |
| 29 HttpServerImpl::HttpServerImpl(HttpServerDelegatePtr delegate) |
| 30 : delegate_(delegate.Pass()) { |
| 31 DCHECK(delegate_); |
| 32 delegate_.set_error_handler(this); |
| 33 } |
| 34 |
| 35 HttpServerImpl::~HttpServerImpl() {} |
| 36 |
| 37 int HttpServerImpl::Start(NetAddressPtr local_address) { |
| 38 DCHECK(local_address); |
| 39 |
| 40 scoped_ptr<net::ServerSocket> socket( |
| 41 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
| 42 int net_result = socket->Listen(local_address.To<net::IPEndPoint>(), |
| 43 kBackLog); |
| 44 if (net_result != net::OK) |
| 45 return net_result; |
| 46 |
| 47 server_.reset(new net::HttpServer(socket.Pass(), this)); |
| 48 |
| 49 return net::OK; |
| 50 } |
| 51 |
| 52 NetAddressPtr HttpServerImpl::GetLocalAddress() const { |
| 53 if (!server_) |
| 54 return nullptr; |
| 55 |
| 56 net::IPEndPoint address; |
| 57 int net_result = server_->GetLocalAddress(&address); |
| 58 if (net_result != net::OK) |
| 59 return nullptr; |
| 60 |
| 61 return NetAddress::From(address); |
| 62 } |
| 63 |
| 64 void HttpServerImpl::OnConnect(int connection_id) { |
| 65 DCHECK(connections_.find(connection_id) == connections_.end()); |
| 66 |
| 67 HttpConnectionPtr connection; |
| 68 HttpConnectionDelegatePtr connection_delegate; |
| 69 InterfaceRequest<HttpConnectionDelegate> delegate_request = |
| 70 GetProxy(&connection_delegate); |
| 71 linked_ptr<HttpConnectionImpl> connection_impl( |
| 72 new HttpConnectionImpl(connection_id, this, connection_delegate.Pass(), |
| 73 &connection)); |
| 74 |
| 75 connections_[connection_id] = connection_impl; |
| 76 |
| 77 delegate_->OnConnected(connection.Pass(), delegate_request.Pass()); |
| 78 } |
| 79 |
| 80 void HttpServerImpl::OnHttpRequest(int connection_id, |
| 81 const net::HttpServerRequestInfo& info) { |
| 82 DCHECK(connections_.find(connection_id) != connections_.end()); |
| 83 connections_[connection_id]->OnReceivedHttpRequest(info); |
| 84 } |
| 85 |
| 86 void HttpServerImpl::OnWebSocketRequest( |
| 87 int connection_id, |
| 88 const net::HttpServerRequestInfo& info) { |
| 89 DCHECK(connections_.find(connection_id) != connections_.end()); |
| 90 connections_[connection_id]->OnReceivedWebSocketRequest(info); |
| 91 } |
| 92 |
| 93 void HttpServerImpl::OnWebSocketMessage(int connection_id, |
| 94 const std::string& data) { |
| 95 DCHECK(connections_.find(connection_id) != connections_.end()); |
| 96 connections_[connection_id]->OnReceivedWebSocketMessage(data); |
| 97 } |
| 98 |
| 99 void HttpServerImpl::OnClose(int connection_id) { |
| 100 DCHECK(connections_.find(connection_id) != connections_.end()); |
| 101 connections_.erase(connection_id); |
| 102 } |
| 103 |
| 104 void HttpServerImpl::OnConnectionError() { |
| 105 delete this; |
| 106 } |
| 107 |
| 108 } // namespace mojo |
OLD | NEW |