OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/spy/websocket_server.h" | 5 #include "mojo/spy/websocket_server.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
11 | |
12 #include "mojo/public/cpp/bindings/message.h" | 12 #include "mojo/public/cpp/bindings/message.h" |
13 | |
14 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
15 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
16 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
17 #include "net/server/http_server_response_info.h" | 16 #include "net/server/http_server_response_info.h" |
18 #include "net/socket/tcp_listen_socket.h" | 17 #include "net/socket/tcp_listen_socket.h" |
| 18 #include "url/gurl.h" |
19 | 19 |
20 namespace mojo { | 20 namespace mojo { |
21 | 21 |
22 const int kNotConnected = -1; | 22 const int kNotConnected = -1; |
23 | 23 |
| 24 #define MOJO_DEBUGGER_MESSAGE_FORMAT "\"url: %s\n," \ |
| 25 "time: %02d:%02d:%02d\n,"\ |
| 26 "bytes: %u\n,"\ |
| 27 "fields: %u\n,"\ |
| 28 "name: %u\n,"\ |
| 29 "requestId: %llu\n,"\ |
| 30 "type: %s\n,"\ |
| 31 "end:\n\"" |
| 32 |
24 WebSocketServer::WebSocketServer(int port, | 33 WebSocketServer::WebSocketServer(int port, |
25 mojo::ScopedMessagePipeHandle server_pipe) | 34 mojo::ScopedMessagePipeHandle server_pipe) |
26 : port_(port), | 35 : port_(port), |
27 connection_id_(kNotConnected), | 36 connection_id_(kNotConnected), |
28 spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) { | 37 spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) { |
29 spy_server_.set_client(this); | 38 spy_server_.set_client(this); |
30 } | 39 } |
31 | 40 |
32 WebSocketServer::~WebSocketServer() { | 41 WebSocketServer::~WebSocketServer() { |
33 } | 42 } |
34 | 43 |
35 bool WebSocketServer::Start() { | 44 bool WebSocketServer::Start() { |
36 net::TCPListenSocketFactory factory("0.0.0.0", port_); | 45 net::TCPListenSocketFactory factory("0.0.0.0", port_); |
37 web_server_ = new net::HttpServer(factory, this); | 46 web_server_ = new net::HttpServer(factory, this); |
38 net::IPEndPoint address; | 47 net::IPEndPoint address; |
39 int error = web_server_->GetLocalAddress(&address); | 48 int error = web_server_->GetLocalAddress(&address); |
40 port_ = address.port(); | 49 port_ = address.port(); |
41 return (error == net::OK); | 50 return (error == net::OK); |
42 } | 51 } |
43 | 52 |
| 53 void WebSocketServer::LogMessageInfo( |
| 54 const mojo::MojoRequestHeader& message_header, |
| 55 const GURL& url, |
| 56 const base::Time& message_time) { |
| 57 base::Time::Exploded exploded; |
| 58 message_time.LocalExplode(&exploded); |
| 59 |
| 60 std::string output_message = base::StringPrintf( |
| 61 MOJO_DEBUGGER_MESSAGE_FORMAT, |
| 62 url.spec().c_str(), |
| 63 exploded.hour, |
| 64 exploded.minute, |
| 65 exploded.second, |
| 66 static_cast<unsigned>(message_header.num_bytes), |
| 67 static_cast<unsigned>(message_header.num_fields), |
| 68 static_cast<unsigned>(message_header.name), |
| 69 static_cast<unsigned long long>( |
| 70 message_header.num_fields == 3 ? message_header.request_id |
| 71 : 0), |
| 72 message_header.flags != 0 ? |
| 73 (message_header.flags & mojo::kMessageExpectsResponse ? |
| 74 "Expects response" : "Response message") |
| 75 : "Not a request or response message"); |
| 76 if (Connected()) { |
| 77 web_server_->SendOverWebSocket(connection_id_, output_message.c_str()); |
| 78 } else { |
| 79 DVLOG(1) << output_message; |
| 80 } |
| 81 } |
| 82 |
44 void WebSocketServer::OnHttpRequest( | 83 void WebSocketServer::OnHttpRequest( |
45 int connection_id, | 84 int connection_id, |
46 const net::HttpServerRequestInfo& info) { | 85 const net::HttpServerRequestInfo& info) { |
47 web_server_->Send500(connection_id, "websockets protocol only"); | 86 web_server_->Send500(connection_id, "websockets protocol only"); |
48 } | 87 } |
49 | 88 |
50 void WebSocketServer::OnWebSocketRequest( | 89 void WebSocketServer::OnWebSocketRequest( |
51 int connection_id, | 90 int connection_id, |
52 const net::HttpServerRequestInfo& info) { | 91 const net::HttpServerRequestInfo& info) { |
53 if (connection_id_ != kNotConnected) { | 92 if (connection_id_ != kNotConnected) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 web_server_->SendOverWebSocket(connection_id_, cc); | 145 web_server_->SendOverWebSocket(connection_id_, cc); |
107 } | 146 } |
108 | 147 |
109 void WebSocketServer::OnMessage(spy_api::MessagePtr message) { | 148 void WebSocketServer::OnMessage(spy_api::MessagePtr message) { |
110 } | 149 } |
111 | 150 |
112 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) { | 151 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) { |
113 web_server_->SendOverWebSocket(connection_id_, "\"ok start\""); | 152 web_server_->SendOverWebSocket(connection_id_, "\"ok start\""); |
114 } | 153 } |
115 | 154 |
| 155 bool WebSocketServer::Connected() const { |
| 156 return connection_id_ != kNotConnected; |
| 157 } |
| 158 |
116 } // namespace mojo | 159 } // namespace mojo |
| 160 |
OLD | NEW |