@@ -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);
0 commit comments