Class: WebSocketClient::Protocol::Ietf00

Inherits:
Object
  • Object
show all
Defined in:
lib/websocket_client/protocol/ietf_00.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, source, sink) ⇒ Ietf00

Returns a new instance of Ietf00.



12
13
14
15
16
# File 'lib/websocket_client/protocol/ietf_00.rb', line 12

def initialize(uri, source, sink)
  @source = source
  @sink   = sink
  perform_http_prolog(uri)
end

Instance Attribute Details

#sinkObject (readonly)

Returns the value of attribute sink.



10
11
12
# File 'lib/websocket_client/protocol/ietf_00.rb', line 10

def sink
  @sink
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/websocket_client/protocol/ietf_00.rb', line 9

def source
  @source
end

Instance Method Details

#generate_content_keyObject



80
81
82
# File 'lib/websocket_client/protocol/ietf_00.rb', line 80

def generate_content_key
  'tacobob1'
end

#generate_header_keyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/websocket_client/protocol/ietf_00.rb', line 66

def generate_header_key
  key = '' 
  1.upto(32) do 
    key << rand(90) + 32
  end
  1.upto( rand(10) + 2 ) do
    key[rand(key.size-1)+1,1] = ' '
  end
  1.upto( rand(10) + 2 ) do
    key[rand(key.size-1)+1,1] = rand(9).to_s
  end
  key
end

#generate_keysObject



45
46
47
48
49
50
# File 'lib/websocket_client/protocol/ietf_00.rb', line 45

def generate_keys()
  key1 = generate_header_key
  key2 = generate_header_key
  key3 = generate_content_key
  [ key1, key2, key3, solve( key1, key2, key3 ) ]
end

#perform_http_prolog(uri) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/websocket_client/protocol/ietf_00.rb', line 18

def perform_http_prolog(uri)
  key1, key2, key3, solution = generate_keys()
  
  sink.write_line "GET #{uri.path} HTTP/1.1"
  sink.write_line "Host: #{uri.host}"
  sink.write_line "Connection: upgrade"
  sink.write_line "Upgrade: websocket"
  sink.write_line "Origin: http://#{uri.host}/"
  sink.write_line "Sec-WebSocket-Key1: #{key1}"
  sink.write_line "Sec-WebSocket-Key2: #{key2}"
  sink.write_line ""
  sink.write_line key3
  sink.flush
  
  while ( ! source.eof? ) 
    line = source.getline
    break if ( line.strip == '' ) 
  end
  
  challenge = source.getbytes( 16 ) 
  source.getline
  
  if ( challenge == solution )
    #
  end
end

#solve(key1, key2, key3) ⇒ Object



52
53
54
55
56
57
# File 'lib/websocket_client/protocol/ietf_00.rb', line 52

def solve(key1, key2, key3)
  int1 = solve_header_key( key1 )
  int2 = solve_header_key( key2 )
  input = int1.to_s + int2.to_s + key3
  Digest::MD5.digest( input ).bytes.to_a
end

#solve_header_key(key) ⇒ Object



59
60
61
62
63
64
# File 'lib/websocket_client/protocol/ietf_00.rb', line 59

def solve_header_key(key)
  key_digits = key.strip.gsub( /[^0-9]/, '').to_i
  key_spaces = key.strip.gsub( /[^ ]/, '').size
  solution = key_digits / key_spaces
  solution
end