Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1308)

Unified Diff: mojo/services/network/http_server_impl.cc

Issue 1134883002: Mojo service implementation for HTTP server - part 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/services/network/http_server_impl.cc
diff --git a/mojo/services/network/http_server_impl.cc b/mojo/services/network/http_server_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4b44599d3d1b48bf9f99a5794deb1858b06a1fdc
--- /dev/null
+++ b/mojo/services/network/http_server_impl.cc
@@ -0,0 +1,108 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/services/network/http_server_impl.h"
+
+#include "base/logging.h"
+#include "mojo/services/network/http_connection_impl.h"
+#include "mojo/services/network/net_adapters.h"
+#include "mojo/services/network/net_address_type_converters.h"
+#include "net/base/ip_endpoint.h"
+#include "net/base/net_errors.h"
+#include "net/log/net_log.h"
+#include "net/socket/tcp_server_socket.h"
+
+namespace mojo {
+
+namespace {
+
+const int kBackLog = 10;
+
+} // namespace
+
+// static
+HttpServerImpl* HttpServerImpl::Create(HttpServerDelegatePtr delegate) {
+ return new HttpServerImpl(delegate.Pass());
+}
+
+HttpServerImpl::HttpServerImpl(HttpServerDelegatePtr delegate)
+ : delegate_(delegate.Pass()) {
+ DCHECK(delegate_);
+ delegate_.set_error_handler(this);
+}
+
+HttpServerImpl::~HttpServerImpl() {}
+
+int HttpServerImpl::Start(NetAddressPtr local_address) {
+ DCHECK(local_address);
+
+ scoped_ptr<net::ServerSocket> socket(
+ new net::TCPServerSocket(nullptr, net::NetLog::Source()));
+ int net_result = socket->Listen(local_address.To<net::IPEndPoint>(),
+ kBackLog);
+ if (net_result != net::OK)
+ return net_result;
+
+ server_.reset(new net::HttpServer(socket.Pass(), this));
+
+ return net::OK;
+}
+
+NetAddressPtr HttpServerImpl::GetLocalAddress() const {
+ if (!server_)
+ return nullptr;
+
+ net::IPEndPoint address;
+ int net_result = server_->GetLocalAddress(&address);
+ if (net_result != net::OK)
+ return nullptr;
+
+ return NetAddress::From(address);
+}
+
+void HttpServerImpl::OnConnect(int connection_id) {
+ DCHECK(connections_.find(connection_id) == connections_.end());
+
+ HttpConnectionPtr connection;
+ HttpConnectionDelegatePtr connection_delegate;
+ InterfaceRequest<HttpConnectionDelegate> delegate_request =
+ GetProxy(&connection_delegate);
+ linked_ptr<HttpConnectionImpl> connection_impl(
+ new HttpConnectionImpl(connection_id, this, connection_delegate.Pass(),
+ &connection));
+
+ connections_[connection_id] = connection_impl;
+
+ delegate_->OnConnected(connection.Pass(), delegate_request.Pass());
+}
+
+void HttpServerImpl::OnHttpRequest(int connection_id,
+ const net::HttpServerRequestInfo& info) {
+ DCHECK(connections_.find(connection_id) != connections_.end());
+ connections_[connection_id]->OnReceivedHttpRequest(info);
+}
+
+void HttpServerImpl::OnWebSocketRequest(
+ int connection_id,
+ const net::HttpServerRequestInfo& info) {
+ DCHECK(connections_.find(connection_id) != connections_.end());
+ connections_[connection_id]->OnReceivedWebSocketRequest(info);
+}
+
+void HttpServerImpl::OnWebSocketMessage(int connection_id,
+ const std::string& data) {
+ DCHECK(connections_.find(connection_id) != connections_.end());
+ connections_[connection_id]->OnReceivedWebSocketMessage(data);
+}
+
+void HttpServerImpl::OnClose(int connection_id) {
+ DCHECK(connections_.find(connection_id) != connections_.end());
+ connections_.erase(connection_id);
+}
+
+void HttpServerImpl::OnConnectionError() {
+ delete this;
+}
+
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698