1#ifndef SRC_INSPECTOR_SOCKET_SERVER_H_
2#define SRC_INSPECTOR_SOCKET_SERVER_H_
3
4#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6#include "inspector_agent.h"
7#include "inspector_socket.h"
8#include "uv.h"
9
10#include <map>
11#include <string>
12#include <vector>
13
14#if !HAVE_INSPECTOR
15#error("This header can only be used when inspector is enabled")
16#endif
17
18namespace node {
19namespace inspector {
20
21std::string FormatWsAddress(const std::string& host, int port,
22                            const std::string& target_id,
23                            bool include_protocol);
24
25class InspectorSocketServer;
26class SocketSession;
27class ServerSocket;
28
29class SocketServerDelegate {
30 public:
31  virtual void AssignServer(InspectorSocketServer* server) = 0;
32  virtual void StartSession(int session_id, const std::string& target_id) = 0;
33  virtual void EndSession(int session_id) = 0;
34  virtual void MessageReceived(int session_id, const std::string& message) = 0;
35  virtual std::vector<std::string> GetTargetIds() = 0;
36  virtual std::string GetTargetTitle(const std::string& id) = 0;
37  virtual std::string GetTargetUrl(const std::string& id) = 0;
38  virtual ~SocketServerDelegate() = default;
39};
40
41// HTTP Server, writes messages requested as TransportActions, and responds
42// to HTTP requests and WS upgrades.
43
44class InspectorSocketServer {
45 public:
46  InspectorSocketServer(std::unique_ptr<SocketServerDelegate> delegate,
47                        uv_loop_t* loop,
48                        const std::string& host,
49                        int port,
50                        const InspectPublishUid& inspect_publish_uid,
51                        FILE* out = stderr,
52                        int pid = -1);
53  ~InspectorSocketServer();
54
55  // Start listening on host/port
56  bool Start();
57
58  // Called by the TransportAction sent with InspectorIo::Write():
59  //   kKill and kStop
60  void Stop();
61  //   kSendMessage
62  void Send(int session_id, const std::string& message);
63  //   kKill
64  void TerminateConnections();
65  int Port() const;
66
67  // Session connection lifecycle
68  void Accept(int server_port, uv_stream_t* server_socket);
69  bool HandleGetRequest(int session_id, const std::string& host,
70                        const std::string& path);
71  void SessionStarted(int session_id, const std::string& target_id,
72                      const std::string& ws_id);
73  void SessionTerminated(int session_id);
74  void MessageReceived(int session_id, const std::string& message) {
75    delegate_->MessageReceived(session_id, message);
76  }
77  SocketSession* Session(int session_id);
78  bool done() const {
79    return server_sockets_.empty() && connected_sessions_.empty();
80  }
81
82  static void CloseServerSocket(ServerSocket*);
83  using ServerSocketPtr = DeleteFnPtr<ServerSocket, CloseServerSocket>;
84
85 private:
86  void SendListResponse(InspectorSocket* socket, const std::string& host,
87                        SocketSession* session);
88  std::string GetFrontendURL(bool is_compat,
89                             const std::string &formatted_address);
90  bool TargetExists(const std::string& id);
91
92  enum class ServerState {kNew, kRunning, kStopped};
93  uv_loop_t* loop_;
94  std::unique_ptr<SocketServerDelegate> delegate_;
95  const std::string host_;
96  int port_;
97  InspectPublishUid inspect_publish_uid_;
98  std::vector<ServerSocketPtr> server_sockets_;
99  std::map<int, std::pair<std::string, std::unique_ptr<SocketSession>>>
100      connected_sessions_;
101  int next_session_id_;
102  FILE* out_;
103  ServerState state_;
104  int pid_;
105};
106
107}  // namespace inspector
108}  // namespace node
109
110#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
111
112#endif  // SRC_INSPECTOR_SOCKET_SERVER_H_
113