| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c5469bb commit 0bcad33
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -495,12 +495,12 @@ class HttpHandler : public ProtocolHandler { | |||
| 495 | 495 | CancelHandshake(); | |
| 496 | 496 | return; | |
| 497 | 497 | } else if (!event.upgrade) { | |
| 498 | - delegate()->OnHttpGet(event.path); | ||
| 498 | + delegate()->OnHttpGet(event.host, event.path); | ||
| 499 | 499 | } else if (event.ws_key.empty()) { | |
| 500 | 500 | CancelHandshake(); | |
| 501 | 501 | return; | |
| 502 | 502 | } else { | |
| 503 | - delegate()->OnSocketUpgrade(event.path, event.ws_key); | ||
| 503 | + delegate()->OnSocketUpgrade(event.host, event.path, event.ws_key); | ||
| 504 | 504 | } | |
| 505 | 505 | } | |
| 506 | 506 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,8 +17,10 @@ class InspectorSocket { | |||
| 17 | 17 | public: | |
| 18 | 18 | class Delegate { | |
| 19 | 19 | public: | |
| 20 | - virtual void OnHttpGet(const std::string& path) = 0; | ||
| 21 | - virtual void OnSocketUpgrade(const std::string& path, | ||
| 20 | + virtual void OnHttpGet(const std::string& host, | ||
| 21 | + const std::string& path) = 0; | ||
| 22 | + virtual void OnSocketUpgrade(const std::string& host, | ||
| 23 | + const std::string& path, | ||
| 22 | 24 | const std::string& accept_key) = 0; | |
| 23 | 25 | virtual void OnWsFrame(const std::vector<char>& frame) = 0; | |
| 24 | 26 | virtual ~Delegate() {} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,33 +16,42 @@ namespace inspector { | |||
| 16 | 16 | // depend on inspector_socket_server.h | |
| 17 | 17 | std::string FormatWsAddress(const std::string& host, int port, | |
| 18 | 18 | const std::string& target_id, | |
| 19 | - bool include_protocol) { | ||
| 19 | + bool include_protocol); | ||
| 20 | + namespace { | ||
| 21 | + | ||
| 22 | + static const uint8_t PROTOCOL_JSON[] = { | ||
| 23 | + #include "v8_inspector_protocol_json.h" // NOLINT(build/include_order) | ||
| 24 | + }; | ||
| 25 | + | ||
| 26 | + void Escape(std::string* string) { | ||
| 27 | + for (char& c : *string) { | ||
| 28 | + c = (c == '\"' || c == '\\') ? '_' : c; | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + std::string FormatHostPort(const std::string& host, int port) { | ||
| 20 | 33 | // Host is valid (socket was bound) so colon means it's a v6 IP address | |
| 21 | 34 | bool v6 = host.find(':') != std::string::npos; | |
| 22 | 35 | std::ostringstream url; | |
| 23 | - if (include_protocol) | ||
| 24 | - url << "ws://"; | ||
| 25 | 36 | if (v6) { | |
| 26 | 37 | url << '['; | |
| 27 | 38 | } | |
| 28 | 39 | url << host; | |
| 29 | 40 | if (v6) { | |
| 30 | 41 | url << ']'; | |
| 31 | 42 | } | |
| 32 | - url << ':' << port << '/' << target_id; | ||
| 43 | + url << ':' << port; | ||
| 33 | 44 | return url.str(); | |
| 34 | 45 | } | |
| 35 | 46 | ||
| 36 | - namespace { | ||
| 37 | - | ||
| 38 | - static const uint8_t PROTOCOL_JSON[] = { | ||
| 39 | - #include "v8_inspector_protocol_json.h" // NOLINT(build/include_order) | ||
| 40 | - }; | ||
| 41 | - | ||
| 42 | - void Escape(std::string* string) { | ||
| 43 | - for (char& c : *string) { | ||
| 44 | - c = (c == '\"' || c == '\\') ? '_' : c; | ||
| 45 | - } | ||
| 47 | + std::string FormatAddress(const std::string& host, | ||
| 48 | + const std::string& target_id, | ||
| 49 | + bool include_protocol) { | ||
| 50 | + std::ostringstream url; | ||
| 51 | + if (include_protocol) | ||
| 52 | + url << "ws://"; | ||
| 53 | + url << host << '/' << target_id; | ||
| 54 | + return url.str(); | ||
| 46 | 55 | } | |
| 47 | 56 | ||
| 48 | 57 | std::string MapToString(const std::map<std::string, std::string>& object) { | |
@@ -141,6 +150,11 @@ void SendProtocolJson(InspectorSocket* socket) { | |||
| 141 | 150 | } | |
| 142 | 151 | } // namespace | |
| 143 | 152 | ||
| 153 | + std::string FormatWsAddress(const std::string& host, int port, | ||
| 154 | + const std::string& target_id, | ||
| 155 | + bool include_protocol) { | ||
| 156 | + return FormatAddress(FormatHostPort(host, port), target_id, include_protocol); | ||
| 157 | + } | ||
| 144 | 158 | ||
| 145 | 159 | class Closer { | |
| 146 | 160 | public: | |
@@ -213,8 +227,8 @@ class SocketSession { | |||
| 213 | 227 | ~Delegate() { | |
| 214 | 228 | server_->SessionTerminated(session_id_); | |
| 215 | 229 | } | |
| 216 | - void OnHttpGet(const std::string& path) override; | ||
| 217 | - void OnSocketUpgrade(const std::string& path, | ||
| 230 | + void OnHttpGet(const std::string& host, const std::string& path) override; | ||
| 231 | + void OnSocketUpgrade(const std::string& host, const std::string& path, | ||
| 218 | 232 | const std::string& ws_key) override; | |
| 219 | 233 | void OnWsFrame(const std::vector<char>& data) override; | |
| 220 | 234 | ||
@@ -320,6 +334,7 @@ void InspectorSocketServer::SessionTerminated(int session_id) { | |||
| 320 | 334 | } | |
| 321 | 335 | ||
| 322 | 336 | bool InspectorSocketServer::HandleGetRequest(int session_id, | |
| 337 | + const std::string& host, | ||
| 323 | 338 | const std::string& path) { | |
| 324 | 339 | SocketSession* session = Session(session_id); | |
| 325 | 340 | InspectorSocket* socket = session->ws_socket(); | |
@@ -328,25 +343,20 @@ bool InspectorSocketServer::HandleGetRequest(int session_id, | |||
| 328 | 343 | return false; | |
| 329 | 344 | ||
| 330 | 345 | if (MatchPathSegment(command, "list") || command[0] == '\0') { | |
| 331 | - SendListResponse(socket, session); | ||
| 346 | + SendListResponse(socket, host, session); | ||
| 332 | 347 | return true; | |
| 333 | 348 | } else if (MatchPathSegment(command, "protocol")) { | |
| 334 | 349 | SendProtocolJson(socket); | |
| 335 | 350 | return true; | |
| 336 | 351 | } else if (MatchPathSegment(command, "version")) { | |
| 337 | 352 | SendVersionResponse(socket); | |
| 338 | 353 | return true; | |
| 339 | - } else if (const char* target_id = MatchPathSegment(command, "activate")) { | ||
| 340 | - if (TargetExists(target_id)) { | ||
| 341 | - SendHttpResponse(socket, "Target activated"); | ||
| 342 | - return true; | ||
| 343 | - } | ||
| 344 | - return false; | ||
| 345 | 354 | } | |
| 346 | 355 | return false; | |
| 347 | 356 | } | |
| 348 | 357 | ||
| 349 | 358 | void InspectorSocketServer::SendListResponse(InspectorSocket* socket, | |
| 359 | + const std::string& host, | ||
| 350 | 360 | SocketSession* session) { | |
| 351 | 361 | std::vector<std::map<std::string, std::string>> response; | |
| 352 | 362 | for (const std::string& id : delegate_->GetTargetIds()) { | |
@@ -371,15 +381,18 @@ void InspectorSocketServer::SendListResponse(InspectorSocket* socket, | |||
| 371 | 381 | } | |
| 372 | 382 | } | |
| 373 | 383 | if (!connected) { | |
| 374 | - std::string host = socket->GetHost(); | ||
| 375 | - int port = session->server_port(); | ||
| 384 | + std::string detected_host = host; | ||
| 385 | + if (detected_host.empty()) { | ||
| 386 | + detected_host = FormatHostPort(socket->GetHost(), | ||
| 387 | + session->server_port()); | ||
| 388 | + } | ||
| 376 | 389 | std::ostringstream frontend_url; | |
| 377 | 390 | frontend_url << "chrome-devtools://devtools/bundled"; | |
| 378 | 391 | frontend_url << "/inspector.html?experiments=true&v8only=true&ws="; | |
| 379 | - frontend_url << FormatWsAddress(host, port, id, false); | ||
| 392 | + frontend_url << FormatAddress(detected_host, id, false); | ||
| 380 | 393 | target_map["devtoolsFrontendUrl"] += frontend_url.str(); | |
| 381 | 394 | target_map["webSocketDebuggerUrl"] = | |
| 382 | - FormatWsAddress(host, port, id, true); | ||
| 395 | + FormatAddress(detected_host, id, true); | ||
| 383 | 396 | } | |
| 384 | 397 | } | |
| 385 | 398 | SendHttpResponse(socket, MapsToString(response)); | |
@@ -531,12 +544,14 @@ void SocketSession::Send(const std::string& message) { | |||
| 531 | 544 | ws_socket_->Write(message.data(), message.length()); | |
| 532 | 545 | } | |
| 533 | 546 | ||
| 534 | - void SocketSession::Delegate::OnHttpGet(const std::string& path) { | ||
| 535 | - if (!server_->HandleGetRequest(session_id_, path)) | ||
| 547 | + void SocketSession::Delegate::OnHttpGet(const std::string& host, | ||
| 548 | + const std::string& path) { | ||
| 549 | + if (!server_->HandleGetRequest(session_id_, host, path)) | ||
| 536 | 550 | Session()->ws_socket()->CancelHandshake(); | |
| 537 | 551 | } | |
| 538 | 552 | ||
| 539 | - void SocketSession::Delegate::OnSocketUpgrade(const std::string& path, | ||
| 553 | + void SocketSession::Delegate::OnSocketUpgrade(const std::string& host, | ||
| 554 | + const std::string& path, | ||
| 540 | 555 | const std::string& ws_key) { | |
| 541 | 556 | std::string id = path.empty() ? path : path.substr(1); | |
| 542 | 557 | server_->SessionStarted(session_id_, id, ws_key); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,7 +67,8 @@ class InspectorSocketServer { | |||
| 67 | 67 | ||
| 68 | 68 | // Session connection lifecycle | |
| 69 | 69 | void Accept(int server_port, uv_stream_t* server_socket); | |
| 70 | - bool HandleGetRequest(int session_id, const std::string& path); | ||
| 70 | + bool HandleGetRequest(int session_id, const std::string& host, | ||
| 71 | + const std::string& path); | ||
| 71 | 72 | void SessionStarted(int session_id, const std::string& target_id, | |
| 72 | 73 | const std::string& ws_id); | |
| 73 | 74 | void SessionTerminated(int session_id); | |
@@ -77,7 +78,8 @@ class InspectorSocketServer { | |||
| 77 | 78 | SocketSession* Session(int session_id); | |
| 78 | 79 | ||
| 79 | 80 | private: | |
| 80 | - void SendListResponse(InspectorSocket* socket, SocketSession* session); | ||
| 81 | + void SendListResponse(InspectorSocket* socket, const std::string& host, | ||
| 82 | + SocketSession* session); | ||
| 81 | 83 | bool TargetExists(const std::string& id); | |
| 82 | 84 | ||
| 83 | 85 | enum class ServerState {kNew, kRunning, kStopping, kStopped}; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -112,11 +112,11 @@ class TestInspectorDelegate : public InspectorSocket::Delegate { | |||
| 112 | 112 | delegate = nullptr; | |
| 113 | 113 | } | |
| 114 | 114 | ||
| 115 | - void OnHttpGet(const std::string& path) override { | ||
| 115 | + void OnHttpGet(const std::string& host, const std::string& path) override { | ||
| 116 | 116 | process(kInspectorHandshakeHttpGet, path); | |
| 117 | 117 | } | |
| 118 | 118 | ||
| 119 | - void OnSocketUpgrade(const std::string& path, | ||
| 119 | + void OnSocketUpgrade(const std::string& host, const std::string& path, | ||
| 120 | 120 | const std::string& ws_key) override { | |
| 121 | 121 | ws_key_ = ws_key; | |
| 122 | 122 | process(kInspectorHandshakeUpgraded, path); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -365,10 +365,11 @@ class NodeInstance { | |||
| 365 | 365 | } | |
| 366 | 366 | } | |
| 367 | 367 | ||
| 368 | - httpGet(host, path) { | ||
| 368 | + httpGet(host, path, hostHeaderValue) { | ||
| 369 | 369 | console.log('[test]', `Testing ${path}`); | |
| 370 | + const headers = hostHeaderValue ? { 'Host': hostHeaderValue } : null; | ||
| 370 | 371 | return this.portPromise.then((port) => new Promise((resolve, reject) => { | |
| 371 | - const req = http.get({ host, port, path }, (res) => { | ||
| 372 | + const req = http.get({ host, port, path, headers }, (res) => { | ||
| 372 | 373 | let response = ''; | |
| 373 | 374 | res.setEncoding('utf8'); | |
| 374 | 375 | res | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,22 @@ | |||
| 1 | + // Flags: --expose-internals | ||
| 2 | + 'use strict'; | ||
| 3 | + const common = require('../common'); | ||
| 4 | + | ||
| 5 | + common.skipIfInspectorDisabled(); | ||
| 6 | + | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const { NodeInstance } = require('../common/inspector-helper.js'); | ||
| 9 | + | ||
| 10 | + common.crashOnUnhandledRejection(); | ||
| 11 | + | ||
| 12 | + async function test() { | ||
| 13 | + const madeUpHost = '111.111.111.111:11111'; | ||
| 14 | + const child = new NodeInstance(undefined, 'var a = 1'); | ||
| 15 | + const response = await child.httpGet(null, '/json', madeUpHost); | ||
| 16 | + assert.ok( | ||
| 17 | + response[0].webSocketDebuggerUrl.startsWith(`ws://${madeUpHost}`), | ||
| 18 | + response[0].webSocketDebuggerUrl); | ||
| 19 | + child.kill(); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + test(); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,9 @@ function checkListResponse(response) { | |||
| 11 | 11 | assert.strictEqual(1, response.length); | |
| 12 | 12 | assert.ok(response[0].devtoolsFrontendUrl); | |
| 13 | 13 | assert.ok( | |
| 14 | - /ws:\/\/127\.0\.0\.1:\d+\/[0-9A-Fa-f]{8}-/ | ||
| 15 | - .test(response[0].webSocketDebuggerUrl)); | ||
| 14 | + /ws:\/\/localhost:\d+\/[0-9A-Fa-f]{8}-/ | ||
| 15 | + .test(response[0].webSocketDebuggerUrl), | ||
| 16 | + response[0].webSocketDebuggerUrl); | ||
| 16 | 17 | } | |
| 17 | 18 | ||
| 18 | 19 | function checkVersion(response) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments