diff options
author | Aleksei Koziatinskii <ak239spb@gmail.com> | 2019-05-17 01:33:09 +0300 |
---|---|---|
committer | Aleksei Koziatinskii <ak239spb@gmail.com> | 2019-06-03 18:15:04 +0300 |
commit | f0018a5152a9faaf2104d62f3152776a60f59390 (patch) | |
tree | 786fb50acdeb349a43cbad532670e4b41258d87a /src/inspector_socket_server.cc | |
parent | 7e18c650de419ae98511be3c7bc54b34efc6d3d4 (diff) |
inspector: added --inspect-publish-uid
This flag specifies how inspector websocket url should be reported.
Tthre options are supported:
- stderr - reports websocket as a message to stderr,
- http - exposes /json/list endpoint that contains inspector websocket
url,
- binding - require('inspector').url().
Related discussion: https://github.com/nodejs/diagnostics/issues/303
PR-URL: https://github.com/nodejs/node/pull/27741
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'src/inspector_socket_server.cc')
-rw-r--r-- | src/inspector_socket_server.cc | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/src/inspector_socket_server.cc b/src/inspector_socket_server.cc index 6bb04372226..092d5e7a786 100644 --- a/src/inspector_socket_server.cc +++ b/src/inspector_socket_server.cc @@ -94,14 +94,20 @@ const char* MatchPathSegment(const char* path, const char* expected) { return nullptr; } -void SendHttpResponse(InspectorSocket* socket, const std::string& response) { - const char HEADERS[] = "HTTP/1.0 200 OK\r\n" +void SendHttpResponse(InspectorSocket* socket, + const std::string& response, + int code) { + const char HEADERS[] = "HTTP/1.0 %d OK\r\n" "Content-Type: application/json; charset=UTF-8\r\n" "Cache-Control: no-cache\r\n" "Content-Length: %zu\r\n" "\r\n"; char header[sizeof(HEADERS) + 20]; - int header_len = snprintf(header, sizeof(header), HEADERS, response.size()); + int header_len = snprintf(header, + sizeof(header), + HEADERS, + code, + response.size()); socket->Write(header, header_len); socket->Write(response.data(), response.size()); } @@ -110,7 +116,11 @@ void SendVersionResponse(InspectorSocket* socket) { std::map<std::string, std::string> response; response["Browser"] = "node.js/" NODE_VERSION; response["Protocol-Version"] = "1.1"; - SendHttpResponse(socket, MapToString(response)); + SendHttpResponse(socket, MapToString(response), 200); +} + +void SendHttpNotFound(InspectorSocket* socket) { + SendHttpResponse(socket, "", 404); } void SendProtocolJson(InspectorSocket* socket) { @@ -131,7 +141,7 @@ void SendProtocolJson(InspectorSocket* socket) { CHECK_EQ(Z_STREAM_END, inflate(&strm, Z_FINISH)); CHECK_EQ(0, strm.avail_out); CHECK_EQ(Z_OK, inflateEnd(&strm)); - SendHttpResponse(socket, data); + SendHttpResponse(socket, data, 200); } } // namespace @@ -224,8 +234,9 @@ void PrintDebuggerReadyMessage( const std::string& host, const std::vector<InspectorSocketServer::ServerSocketPtr>& server_sockets, const std::vector<std::string>& ids, + bool publish_uid_stderr, FILE* out) { - if (out == nullptr) { + if (!publish_uid_stderr || out == nullptr) { return; } for (const auto& server_socket : server_sockets) { @@ -241,9 +252,15 @@ void PrintDebuggerReadyMessage( InspectorSocketServer::InspectorSocketServer( std::unique_ptr<SocketServerDelegate> delegate, uv_loop_t* loop, - const std::string& host, int port, FILE* out) - : loop_(loop), delegate_(std::move(delegate)), host_(host), port_(port), - next_session_id_(0), out_(out) { + const std::string& host, int port, + const InspectPublishUid& inspect_publish_uid, FILE* out) + : loop_(loop), + delegate_(std::move(delegate)), + host_(host), + port_(port), + inspect_publish_uid_(inspect_publish_uid), + next_session_id_(0), + out_(out) { delegate_->AssignServer(this); state_ = ServerState::kNew; } @@ -280,8 +297,11 @@ void InspectorSocketServer::SessionTerminated(int session_id) { if (connected_sessions_.empty()) { if (was_attached && state_ == ServerState::kRunning && !server_sockets_.empty()) { - PrintDebuggerReadyMessage(host_, server_sockets_, - delegate_->GetTargetIds(), out_); + PrintDebuggerReadyMessage(host_, + server_sockets_, + delegate_->GetTargetIds(), + inspect_publish_uid_.console, + out_); } if (state_ == ServerState::kStopped) { delegate_.reset(); @@ -294,6 +314,10 @@ bool InspectorSocketServer::HandleGetRequest(int session_id, const std::string& path) { SocketSession* session = Session(session_id); InspectorSocket* socket = session->ws_socket(); + if (!inspect_publish_uid_.http) { + SendHttpNotFound(socket); + return true; + } const char* command = MatchPathSegment(path.c_str(), "/json"); if (command == nullptr) return false; @@ -342,7 +366,7 @@ void InspectorSocketServer::SendListResponse(InspectorSocket* socket, formatted_address); target_map["webSocketDebuggerUrl"] = FormatAddress(detected_host, id, true); } - SendHttpResponse(socket, MapsToString(response)); + SendHttpResponse(socket, MapsToString(response), 200); } std::string InspectorSocketServer::GetFrontendURL(bool is_compat, @@ -397,8 +421,11 @@ bool InspectorSocketServer::Start() { } delegate_.swap(delegate_holder); state_ = ServerState::kRunning; - PrintDebuggerReadyMessage(host_, server_sockets_, - delegate_->GetTargetIds(), out_); + PrintDebuggerReadyMessage(host_, + server_sockets_, + delegate_->GetTargetIds(), + inspect_publish_uid_.console, + out_); return true; } |