Class: DEBUGGER__::UI_CDP::WebSocketServer

Inherits:
Object
  • Object
show all
Includes:
WebSocketUtils
Defined in:
lib/debug/server_cdp.rb

Instance Method Summary collapse

Methods included from WebSocketUtils

#show_protocol

Constructor Details

#initialize(s) ⇒ WebSocketServer

Returns a new instance of WebSocketServer.



372
373
374
# File 'lib/debug/server_cdp.rb', line 372

def initialize s
  @sock = s
end

Instance Method Details

#extract_dataObject

Raises:



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/debug/server_cdp.rb', line 419

def extract_data
  first_group = @sock.getbyte
  fin = first_group & 0b10000000 != 128
  raise 'Unsupported' if fin

  opcode = first_group & 0b00001111
  raise Detach if opcode == 8
  raise "Unsupported: #{opcode}" unless opcode == 1

  second_group = @sock.getbyte
  mask = second_group & 0b10000000 == 128
  raise 'The client must mask all frames' unless mask
  payload_len = second_group & 0b01111111
  # TODO: Support other payload_lengths
  if payload_len == 126
    payload_len = @sock.gets(2).unpack('n*')[0]
  end

  masking_key = []
  4.times { masking_key << @sock.getbyte }
  unmasked = []
  payload_len.times do |n|
    masked = @sock.getbyte
    unmasked << (masked ^ masking_key[n % 4])
  end
  msg = unmasked.pack 'c*'
  show_protocol :>, msg
  JSON.parse msg
end

#handshakeObject



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/debug/server_cdp.rb', line 376

def handshake
  req = @sock.readpartial 4096
  show_protocol '>', req

  if req.match(/^Sec-WebSocket-Key: (.*)\r\n/)
    accept = Digest::SHA1.base64digest "#{$1}258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: #{accept}\r\n\r\n"
    @sock.print res
    show_protocol :<, res
  else
    "Unknown request: #{req}"
  end
end

#send(**msg) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/debug/server_cdp.rb', line 390

def send **msg
  msg = JSON.generate(msg)
  show_protocol :<, msg
  frame = Frame.new
  fin = 0b10000000
  opcode = 0b00000001
  frame.char fin + opcode

  mask = 0b00000000 # A server must not mask any frames in a WebSocket Protocol.
  bytesize = msg.bytesize
  if bytesize < 126
    payload_len = bytesize
    frame.char mask + payload_len
  elsif bytesize < 2 ** 16
    payload_len = 0b01111110
    frame.char mask + payload_len
    frame.uint16 bytesize
  elsif bytesize < 2 ** 64
    payload_len = 0b01111111
    frame.char mask + payload_len
    frame.ulonglong bytesize
  else
    raise 'Bytesize is too big.'
  end

  frame << msg
  @sock.print frame.b
end