Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Ostroukhov <eostroukhov@chromium.org>2017-11-11 03:01:00 +0300
committerEugene Ostroukhov <eostroukhov@google.com>2017-12-12 02:53:21 +0300
commit73ad3f9bea3993b486621aaf9e61484dc37741d4 (patch)
tree259b21142e6c4ceedde66deb1eb37a110e317d9e /src/inspector_socket.h
parente51fb90a6db53588ab2b884e4309d4eea9e37bbd (diff)
inspector: Fix crash for WS connection
Attaching WS session will now include a roundtrip onto the main thread to make sure there is no other session (e.g. JS bindings) This change also required refactoring WS socket implementation to better support scenarios like this. Fixes: https://github.com/nodejs/node/issues/16852 PR-URL: https://github.com/nodejs/node/pull/17085 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Diffstat (limited to 'src/inspector_socket.h')
-rw-r--r--src/inspector_socket.h96
1 files changed, 24 insertions, 72 deletions
diff --git a/src/inspector_socket.h b/src/inspector_socket.h
index f93150d6f9a..1a3411435ee 100644
--- a/src/inspector_socket.h
+++ b/src/inspector_socket.h
@@ -1,7 +1,6 @@
#ifndef SRC_INSPECTOR_SOCKET_H_
#define SRC_INSPECTOR_SOCKET_H_
-#include "http_parser.h"
#include "util-inl.h"
#include "uv.h"
@@ -11,88 +10,41 @@
namespace node {
namespace inspector {
-enum inspector_handshake_event {
- kInspectorHandshakeUpgrading,
- kInspectorHandshakeUpgraded,
- kInspectorHandshakeHttpGet,
- kInspectorHandshakeFailed
-};
-
-class InspectorSocket;
-
-typedef void (*inspector_cb)(InspectorSocket*, int);
-// Notifies as handshake is progressing. Returning false as a response to
-// kInspectorHandshakeUpgrading or kInspectorHandshakeHttpGet event will abort
-// the connection. inspector_write can be used from the callback.
-typedef bool (*handshake_cb)(InspectorSocket*,
- enum inspector_handshake_event state,
- const std::string& path);
-
-struct http_parsing_state_s {
- http_parser parser;
- http_parser_settings parser_settings;
- handshake_cb callback;
- bool done;
- bool parsing_value;
- std::string ws_key;
- std::string path;
- std::string current_header;
-};
-
-struct ws_state_s {
- uv_alloc_cb alloc_cb;
- uv_read_cb read_cb;
- inspector_cb close_cb;
- bool close_sent;
- bool received_close;
-};
+class ProtocolHandler;
// HTTP Wrapper around a uv_tcp_t
class InspectorSocket {
public:
- InspectorSocket() : data(nullptr), http_parsing_state(nullptr),
- ws_state(nullptr), buffer(0), ws_mode(false),
- shutting_down(false), connection_eof(false) { }
- void reinit();
- void* data;
- struct http_parsing_state_s* http_parsing_state;
- struct ws_state_s* ws_state;
- std::vector<char> buffer;
- uv_tcp_t tcp;
- bool ws_mode;
- bool shutting_down;
- bool connection_eof;
+ class Delegate {
+ public:
+ virtual void OnHttpGet(const std::string& path) = 0;
+ virtual void OnSocketUpgrade(const std::string& path,
+ const std::string& accept_key) = 0;
+ virtual void OnWsFrame(const std::vector<char>& frame) = 0;
+ virtual ~Delegate() {}
+ };
- private:
- DISALLOW_COPY_AND_ASSIGN(InspectorSocket);
-};
+ using DelegatePointer = std::unique_ptr<Delegate>;
+ using Pointer = std::unique_ptr<InspectorSocket>;
+
+ static Pointer Accept(uv_stream_t* server, DelegatePointer delegate);
-int inspector_accept(uv_stream_t* server, InspectorSocket* inspector,
- handshake_cb callback);
+ ~InspectorSocket();
-void inspector_close(InspectorSocket* inspector,
- inspector_cb callback);
+ void AcceptUpgrade(const std::string& accept_key);
+ void CancelHandshake();
+ void Write(const char* data, size_t len);
+ void SwitchProtocol(ProtocolHandler* handler);
+ std::string GetHost();
-// Callbacks will receive stream handles. Use inspector_from_stream to get
-// InspectorSocket* from the stream handle.
-int inspector_read_start(InspectorSocket* inspector, uv_alloc_cb,
- uv_read_cb);
-void inspector_read_stop(InspectorSocket* inspector);
-void inspector_write(InspectorSocket* inspector,
- const char* data, size_t len);
-bool inspector_is_active(const InspectorSocket* inspector);
+ private:
+ InspectorSocket();
-inline InspectorSocket* inspector_from_stream(uv_tcp_t* stream) {
- return node::ContainerOf(&InspectorSocket::tcp, stream);
-}
+ std::unique_ptr<ProtocolHandler, void(*)(ProtocolHandler*)> protocol_handler_;
-inline InspectorSocket* inspector_from_stream(uv_stream_t* stream) {
- return inspector_from_stream(reinterpret_cast<uv_tcp_t*>(stream));
-}
+ DISALLOW_COPY_AND_ASSIGN(InspectorSocket);
+};
-inline InspectorSocket* inspector_from_stream(uv_handle_t* stream) {
- return inspector_from_stream(reinterpret_cast<uv_tcp_t*>(stream));
-}
} // namespace inspector
} // namespace node