@@ -16,33 +16,42 @@ namespace inspector {
1616// depend on inspector_socket_server.h
1717std::string FormatWsAddress (const std::string& host, int port,
1818 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) {
2033 // Host is valid (socket was bound) so colon means it's a v6 IP address
2134 bool v6 = host.find (' :' ) != std::string::npos;
2235 std::ostringstream url;
23- if (include_protocol)
24- url << " ws://" ;
2536 if (v6) {
2637 url << ' [' ;
2738 }
2839 url << host;
2940 if (v6) {
3041 url << ' ]' ;
3142 }
32- url << ' :' << port << ' / ' << target_id ;
43+ url << ' :' << port;
3344 return url.str ();
3445}
3546
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 ();
4655}
4756
4857std::string MapToString (const std::map<std::string, std::string>& object) {
@@ -141,6 +150,11 @@ void SendProtocolJson(InspectorSocket* socket) {
141150}
142151} // namespace
143152
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+ }
144158
145159class Closer {
146160 public:
@@ -213,8 +227,8 @@ class SocketSession {
213227 ~Delegate () {
214228 server_->SessionTerminated (session_id_);
215229 }
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,
218232 const std::string& ws_key) override ;
219233 void OnWsFrame (const std::vector<char >& data) override ;
220234
@@ -320,6 +334,7 @@ void InspectorSocketServer::SessionTerminated(int session_id) {
320334}
321335
322336bool InspectorSocketServer::HandleGetRequest (int session_id,
337+ const std::string& host,
323338 const std::string& path) {
324339 SocketSession* session = Session (session_id);
325340 InspectorSocket* socket = session->ws_socket ();
@@ -328,25 +343,20 @@ bool InspectorSocketServer::HandleGetRequest(int session_id,
328343 return false ;
329344
330345 if (MatchPathSegment (command, " list" ) || command[0 ] == ' \0 ' ) {
331- SendListResponse (socket, session);
346+ SendListResponse (socket, host, session);
332347 return true ;
333348 } else if (MatchPathSegment (command, " protocol" )) {
334349 SendProtocolJson (socket);
335350 return true ;
336351 } else if (MatchPathSegment (command, " version" )) {
337352 SendVersionResponse (socket);
338353 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 ;
345354 }
346355 return false ;
347356}
348357
349358void InspectorSocketServer::SendListResponse (InspectorSocket* socket,
359+ const std::string& host,
350360 SocketSession* session) {
351361 std::vector<std::map<std::string, std::string>> response;
352362 for (const std::string& id : delegate_->GetTargetIds ()) {
@@ -371,15 +381,18 @@ void InspectorSocketServer::SendListResponse(InspectorSocket* socket,
371381 }
372382 }
373383 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+ }
376389 std::ostringstream frontend_url;
377390 frontend_url << " chrome-devtools://devtools/bundled" ;
378391 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 );
380393 target_map[" devtoolsFrontendUrl" ] += frontend_url.str ();
381394 target_map[" webSocketDebuggerUrl" ] =
382- FormatWsAddress (host, port , id, true );
395+ FormatAddress (detected_host , id, true );
383396 }
384397 }
385398 SendHttpResponse (socket, MapsToString (response));
@@ -531,12 +544,14 @@ void SocketSession::Send(const std::string& message) {
531544 ws_socket_->Write (message.data (), message.length ());
532545}
533546
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))
536550 Session ()->ws_socket ()->CancelHandshake ();
537551}
538552
539- void SocketSession::Delegate::OnSocketUpgrade (const std::string& path,
553+ void SocketSession::Delegate::OnSocketUpgrade (const std::string& host,
554+ const std::string& path,
540555 const std::string& ws_key) {
541556 std::string id = path.empty () ? path : path.substr (1 );
542557 server_->SessionStarted (session_id_, id, ws_key);
0 commit comments