Skip to content

Commit

Permalink
Allow debugger to be attached from Inspector page in Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
ono-max committed Nov 6, 2022
1 parent edb4af8 commit 1f31fd5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
7 changes: 5 additions & 2 deletions lib/debug/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def greeting
@need_pause_at_first = false
dap_setup @sock.read($1.to_i)

when /^GET\s\/json\sHTTP\/1.1/, /^GET\s\/json\/version\sHTTP\/1.1/, /^GET\s\/#{@uuid}\sHTTP\/1.1/
when /^GET\s\/json\sHTTP\/1.1/, /^GET\s\/json\/version\sHTTP\/1.1/, /^GET\s\/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\sHTTP\/1.1/
# The reason for not using @uuid here is @uuid is nil if users run debugger without `--open=chrome`.

require_relative 'server_cdp'

self.extend(UI_CDP)
Expand Down Expand Up @@ -394,14 +396,15 @@ def initialize host: nil, port: nil
raise "Specify digits for port number"
end
end
@uuid = SecureRandom.uuid # for CDP
@uuid = nil # for CDP

super()
end

def chrome_setup
require_relative 'server_cdp'

@uuid = SecureRandom.uuid
unless @chrome_pid = UI_CDP.setup_chrome(@local_addr.inspect_sockaddr, @uuid)
DEBUGGER__.warn <<~EOS
With Chrome browser, type the following URL in the address-bar:
Expand Down
5 changes: 4 additions & 1 deletion lib/debug/server_cdp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def handle_request req
raise UI_ServerBase::RetryConnection

when /^GET\s\/json\sHTTP\/1.1/
@uuid = @uuid || SecureRandom.uuid
addr = @local_addr.inspect_sockaddr
body = [{
description: "ruby instance",
Expand All @@ -127,7 +128,9 @@ def handle_request req
send_http_res body
raise UI_ServerBase::RetryConnection

when /^GET\s\/#{@uuid}\sHTTP\/1.1/
when /^GET\s\/(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})\sHTTP\/1.1/
raise 'Incorrect uuid' unless $1 == @uuid

@need_pause_at_first = false
CONFIG.set_config no_color: true

Expand Down
7 changes: 5 additions & 2 deletions test/support/cdp_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ def setup_chrome_debuggee
def connect_to_cdp_server
ENV['RUBY_DEBUG_TEST_MODE'] = 'true'

body = get_request HOST, @remote_info.port, '/json'
sock = Socket.tcp HOST, @remote_info.port
uuid = body[0][:id]

Timeout.timeout(TIMEOUT_SEC) do
sleep 0.001 until @remote_info.debuggee_backlog.join.include? 'Connected'
sleep 0.001 until @remote_info.debuggee_backlog.join.match?(/Disconnected\.\R.*Connected/)
end
@web_sock = WebSocketClient.new sock
@web_sock.handshake @remote_info.port, '/'
@web_sock.handshake @remote_info.port, uuid
@reader_thread = Thread.new do
Thread.current.abort_on_exception = true
while res = @web_sock.extract_data
Expand Down
14 changes: 10 additions & 4 deletions test/support/protocol_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,20 @@ def attach_to_dap_server
HOST = '127.0.0.1'

def attach_to_cdp_server
body = get_request HOST, @remote_info.port, '/json'
Timeout.timeout(TIMEOUT_SEC) do
sleep 0.001 until @remote_info.debuggee_backlog.join.include? 'Disconnected.'
end

sock = Socket.tcp HOST, @remote_info.port
uuid = body[0][:id]

Timeout.timeout(TIMEOUT_SEC) do
sleep 0.001 until @remote_info.debuggee_backlog.join.include? 'Connected'
sleep 0.001 until @remote_info.debuggee_backlog.join.match?(/Disconnected\.\R.*Connected/)
end

@web_sock = WebSocketClient.new sock
@web_sock.handshake @remote_info.port, '/'
@web_sock.handshake @remote_info.port, uuid
@id = 1
@reader_thread = Thread.new do
while res = @web_sock.extract_data
Expand Down Expand Up @@ -797,9 +803,9 @@ def initialize s
@sock = s
end

def handshake port, path
def handshake port, uuid
key = SecureRandom.hex(11)
@sock.print "GET #{path} HTTP/1.1\r\nHost: 127.0.0.1:#{port}\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: #{key}==\r\n\r\n"
@sock.print "GET /#{uuid} HTTP/1.1\r\nHost: 127.0.0.1:#{port}\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: #{key}==\r\n\r\n"
server_key = get_server_key

correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
Expand Down
21 changes: 21 additions & 0 deletions test/support/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,26 @@ def setup_tcpip_remote_debuggee
remote_info.port = TCPIP_PORT
remote_info
end

# Debuggee sometimes sends msgs such as "out [1, 5] in ...".
# This http request method is for ignoring them.
def get_request host, port, path
Timeout.timeout(TIMEOUT_SEC) do
Socket.tcp(host, port){|sock|
sock.print "GET #{path} HTTP/1.1\r\n"
sock.close_write
loop do
case header = sock.gets
when /Content-Length: (\d+)/
b = sock.read(2)
raise b.inspect unless b == "\r\n"

l = sock.read $1.to_i
return JSON.parse l, symbolize_names: true
end
end
}
end
end
end
end

0 comments on commit 1f31fd5

Please sign in to comment.