| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/services/network/http_connection_impl.h" | 5 #include "mojo/services/network/http_connection_impl.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "mojo/common/handle_watcher.h" | 14 #include "mojo/message_pump/handle_watcher.h" |
| 15 #include "mojo/services/network/http_server_impl.h" | 15 #include "mojo/services/network/http_server_impl.h" |
| 16 #include "mojo/services/network/interfaces/web_socket.mojom.h" | 16 #include "mojo/services/network/interfaces/web_socket.mojom.h" |
| 17 #include "mojo/services/network/net_adapters.h" | 17 #include "mojo/services/network/net_adapters.h" |
| 18 #include "mojo/services/network/web_socket_read_queue.h" | 18 #include "mojo/services/network/web_socket_read_queue.h" |
| 19 #include "mojo/services/network/web_socket_write_queue.h" | 19 #include "mojo/services/network/web_socket_write_queue.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/http/http_request_headers.h" | 21 #include "net/http/http_request_headers.h" |
| 22 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
| 23 #include "net/server/http_server.h" | 23 #include "net/server/http_server.h" |
| 24 #include "net/server/http_server_request_info.h" | 24 #include "net/server/http_server_request_info.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader); | 85 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader); |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 class HttpConnectionImpl::WebSocketImpl : public WebSocket { | 88 class HttpConnectionImpl::WebSocketImpl : public WebSocket { |
| 89 public: | 89 public: |
| 90 // |connection| must outlive this object. | 90 // |connection| must outlive this object. |
| 91 WebSocketImpl(HttpConnectionImpl* connection, | 91 WebSocketImpl(HttpConnectionImpl* connection, |
| 92 InterfaceRequest<WebSocket> request, | 92 InterfaceRequest<WebSocket> request, |
| 93 ScopedDataPipeConsumerHandle send_stream, | 93 ScopedDataPipeConsumerHandle send_stream, |
| 94 WebSocketClientPtr client) | 94 InterfaceHandle<WebSocketClient> client) |
| 95 : connection_(connection), | 95 : connection_(connection), |
| 96 binding_(this, request.Pass()), | 96 binding_(this, request.Pass()), |
| 97 client_(client.Pass()), | 97 client_(WebSocketClientPtr::Create(client.Pass())), |
| 98 send_stream_(send_stream.Pass()), | 98 send_stream_(send_stream.Pass()), |
| 99 read_send_stream_(new WebSocketReadQueue(send_stream_.get())), | 99 read_send_stream_(new WebSocketReadQueue(send_stream_.get())), |
| 100 pending_send_count_(0) { | 100 pending_send_count_(0) { |
| 101 DCHECK(binding_.is_bound()); | 101 DCHECK(binding_.is_bound()); |
| 102 DCHECK(client_); | 102 DCHECK(client_); |
| 103 DCHECK(send_stream_.is_valid()); | 103 DCHECK(send_stream_.is_valid()); |
| 104 | 104 |
| 105 binding_.set_connection_error_handler([this]() { Close(); }); | 105 binding_.set_connection_error_handler([this]() { Close(); }); |
| 106 client_.set_connection_error_handler([this]() { Close(); }); | 106 client_.set_connection_error_handler([this]() { Close(); }); |
| 107 | 107 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 138 base::Bind(&WebSocketImpl::OnFinishedWritingReceiveStream, | 138 base::Bind(&WebSocketImpl::OnFinishedWritingReceiveStream, |
| 139 base::Unretained(this), size)); | 139 base::Unretained(this), size)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 private: | 142 private: |
| 143 // WebSocket implementation. | 143 // WebSocket implementation. |
| 144 void Connect(const String& url, | 144 void Connect(const String& url, |
| 145 Array<String> protocols, | 145 Array<String> protocols, |
| 146 const String& origin, | 146 const String& origin, |
| 147 ScopedDataPipeConsumerHandle send_stream, | 147 ScopedDataPipeConsumerHandle send_stream, |
| 148 WebSocketClientPtr client) override { | 148 InterfaceHandle<WebSocketClient> client) override { |
| 149 NOTREACHED(); | 149 NOTREACHED(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void Send(bool fin, MessageType type, uint32_t num_bytes) override { | 152 void Send(bool fin, MessageType type, uint32_t num_bytes) override { |
| 153 if (!fin || type != MessageType::TEXT) { | 153 if (!fin || type != MessageType::TEXT) { |
| 154 NOTIMPLEMENTED(); | 154 NOTIMPLEMENTED(); |
| 155 Close(); | 155 Close(); |
| 156 } | 156 } |
| 157 | 157 |
| 158 // TODO(yzshen): It shouldn't be an issue to pass an empty message. However, | 158 // TODO(yzshen): It shouldn't be an issue to pass an empty message. However, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 request->body = data_pipe.consumer_handle.Pass(); | 243 request->body = data_pipe.consumer_handle.Pass(); |
| 244 MojoResult result = | 244 MojoResult result = |
| 245 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), | 245 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), |
| 246 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | 246 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| 247 CHECK_EQ(MOJO_RESULT_OK, result); | 247 CHECK_EQ(MOJO_RESULT_OK, result); |
| 248 } | 248 } |
| 249 return request.Pass(); | 249 return request.Pass(); |
| 250 } | 250 } |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 HttpConnectionImpl::HttpConnectionImpl(int connection_id, | 253 HttpConnectionImpl::HttpConnectionImpl( |
| 254 HttpServerImpl* server, | 254 int connection_id, |
| 255 HttpConnectionDelegatePtr delegate, | 255 HttpServerImpl* server, |
| 256 HttpConnectionPtr* connection) | 256 HttpConnectionDelegatePtr delegate, |
| 257 InterfaceHandle<HttpConnection>* connection) |
| 257 : connection_id_(connection_id), | 258 : connection_id_(connection_id), |
| 258 server_(server), | 259 server_(server), |
| 259 delegate_(delegate.Pass()), | 260 delegate_(delegate.Pass()), |
| 260 binding_(this, connection) { | 261 binding_(this, connection) { |
| 261 DCHECK(delegate_); | 262 DCHECK(delegate_); |
| 262 binding_.set_connection_error_handler([this]() { Close(); }); | 263 binding_.set_connection_error_handler([this]() { Close(); }); |
| 263 delegate_.set_connection_error_handler([this]() { Close(); }); | 264 delegate_.set_connection_error_handler([this]() { Close(); }); |
| 264 } | 265 } |
| 265 | 266 |
| 266 HttpConnectionImpl::~HttpConnectionImpl() { | 267 HttpConnectionImpl::~HttpConnectionImpl() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 290 | 291 |
| 291 void HttpConnectionImpl::OnReceivedWebSocketRequest( | 292 void HttpConnectionImpl::OnReceivedWebSocketRequest( |
| 292 const net::HttpServerRequestInfo& info) { | 293 const net::HttpServerRequestInfo& info) { |
| 293 if (IsClosing()) | 294 if (IsClosing()) |
| 294 return; | 295 return; |
| 295 | 296 |
| 296 delegate_->OnReceivedWebSocketRequest( | 297 delegate_->OnReceivedWebSocketRequest( |
| 297 HttpRequest::From(info), | 298 HttpRequest::From(info), |
| 298 [this, info](InterfaceRequest<WebSocket> web_socket, | 299 [this, info](InterfaceRequest<WebSocket> web_socket, |
| 299 ScopedDataPipeConsumerHandle send_stream, | 300 ScopedDataPipeConsumerHandle send_stream, |
| 300 WebSocketClientPtr web_socket_client) { | 301 InterfaceHandle<WebSocketClient> web_socket_client) { |
| 301 if (!web_socket.is_pending() || !send_stream.is_valid() || | 302 if (!web_socket.is_pending() || !send_stream.is_valid() || |
| 302 !web_socket_client) { | 303 !web_socket_client) { |
| 303 Close(); | 304 Close(); |
| 304 return; | 305 return; |
| 305 } | 306 } |
| 306 | 307 |
| 307 web_socket_.reset(new WebSocketImpl(this, web_socket.Pass(), | 308 web_socket_.reset(new WebSocketImpl(this, web_socket.Pass(), |
| 308 send_stream.Pass(), | 309 send_stream.Pass(), |
| 309 web_socket_client.Pass())); | 310 web_socket_client.Pass())); |
| 310 server_->server()->AcceptWebSocket(connection_id_, info); | 311 server_->server()->AcceptWebSocket(connection_id_, info); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 // The close operation is initiated by this object. | 412 // The close operation is initiated by this object. |
| 412 NotifyOwnerCloseIfAllDone(); | 413 NotifyOwnerCloseIfAllDone(); |
| 413 } else { | 414 } else { |
| 414 // The close operation is initiated by |web_socket_|; start closing this | 415 // The close operation is initiated by |web_socket_|; start closing this |
| 415 // object. | 416 // object. |
| 416 Close(); | 417 Close(); |
| 417 } | 418 } |
| 418 } | 419 } |
| 419 | 420 |
| 420 } // namespace mojo | 421 } // namespace mojo |
| OLD | NEW |