Class: WebSocket::Driver::Hybi

Inherits:
WebSocket::Driver show all
Defined in:
lib/websocket/driver/hybi.rb,
lib/websocket/driver/hybi/stream_reader.rb

Direct Known Subclasses

Client

Defined Under Namespace

Classes: StreamReader

Constant Summary collapse

GUID =
'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
BYTE =
0b11111111
FIN =
MASK = 0b10000000
RSV1 =
0b01000000
RSV2 =
0b00100000
RSV3 =
0b00010000
OPCODE =
0b00001111
LENGTH =
0b01111111
OPCODES =
{
  :continuation => 0,
  :text         => 1,
  :binary       => 2,
  :close        => 8,
  :ping         => 9,
  :pong         => 10
}
OPCODE_CODES =
OPCODES.values
FRAGMENTED_OPCODES =
OPCODES.values_at(:continuation, :text, :binary)
OPENING_OPCODES =
OPCODES.values_at(:text, :binary)
ERRORS =
{
  :normal_closure       => 1000,
  :going_away           => 1001,
  :protocol_error       => 1002,
  :unacceptable         => 1003,
  :encoding_error       => 1007,
  :policy_violation     => 1008,
  :too_large            => 1009,
  :extension_error      => 1010,
  :unexpected_condition => 1011
}
ERROR_CODES =
ERRORS.values
MIN_RESERVED_ERROR =
3000
MAX_RESERVED_ERROR =
4999

Constants inherited from WebSocket::Driver

MAX_LENGTH, STATES

Instance Attribute Summary

Attributes inherited from WebSocket::Driver

#protocol, #ready_state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WebSocket::Driver

#set_header, #start, #state

Methods included from EventEmitter

#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener

Constructor Details

#initialize(socket, options = {}) ⇒ Hybi

Returns a new instance of Hybi.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/websocket/driver/hybi.rb', line 51

def initialize(socket, options = {})
  super
  reset

  @reader          = StreamReader.new
  @stage           = 0
  @masking         = options[:masking]
  @protocols       = options[:protocols] || []
  @protocols       = @protocols.strip.split(/\s*,\s*/) if String === @protocols
  @require_masking = options[:require_masking]
  @ping_callbacks  = {}

  return unless @socket.respond_to?(:env)

  if protos = @socket.env['HTTP_SEC_WEBSOCKET_PROTOCOL']
    protos = protos.split(/\s*,\s*/) if String === protos
    @protocol = protos.find { |p| @protocols.include?(p) }
  end
end

Class Method Details

.generate_accept(key) ⇒ Object



8
9
10
# File 'lib/websocket/driver/hybi.rb', line 8

def self.generate_accept(key)
  Base64.encode64(Digest::SHA1.digest(key + GUID)).strip
end

Instance Method Details

#binary(message) ⇒ Object



171
172
173
# File 'lib/websocket/driver/hybi.rb', line 171

def binary(message)
  frame(message, :binary)
end

#close(reason = nil, code = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/websocket/driver/hybi.rb', line 180

def close(reason = nil, code = nil)
  reason ||= ''
  code   ||= ERRORS[:normal_closure]

  if @ready_state <= 0
    @ready_state = 3
    emit(:close, CloseEvent.new(code, reason))
    true
  elsif @ready_state == 1
    frame(reason, :close, code)
    @ready_state = 2
    true
  else
    false
  end
end

#frame(data, type = nil, code = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/websocket/driver/hybi.rb', line 114

def frame(data, type = nil, code = nil)
  return queue([data, type, code]) if @ready_state <= 0
  return false unless @ready_state == 1

  data = data.to_s unless Array === data
  data = Driver.encode(data, :utf8) if String === data

  is_text = (String === data)
  opcode  = OPCODES[type || (is_text ? :text : :binary)]
  buffer  = data.respond_to?(:bytes) ? data.bytes.to_a : data
  insert  = code ? 2 : 0
  length  = buffer.size + insert
  header  = (length <= 125) ? 2 : (length <= 65535 ? 4 : 10)
  offset  = header + (@masking ? 4 : 0)
  masked  = @masking ? MASK : 0
  frame   = Array.new(offset)

  frame[0] = FIN | opcode

  if length <= 125
    frame[1] = masked | length
  elsif length <= 65535
    frame[1] = masked | 126
    frame[2] = (length >> 8) & BYTE
    frame[3] = length & BYTE
  else
    frame[1] = masked | 127
    frame[2] = (length >> 56) & BYTE
    frame[3] = (length >> 48) & BYTE
    frame[4] = (length >> 40) & BYTE
    frame[5] = (length >> 32) & BYTE
    frame[6] = (length >> 24) & BYTE
    frame[7] = (length >> 16) & BYTE
    frame[8] = (length >> 8)  & BYTE
    frame[9] = length & BYTE
  end

  if code
    buffer = [(code >> 8) & BYTE, code & BYTE] + buffer
  end

  if @masking
    mask = [rand(256), rand(256), rand(256), rand(256)]
    frame[header...offset] = mask
    buffer = Mask.mask(buffer, mask)
  end

  frame.concat(buffer)

  @socket.write(Driver.encode(frame, :binary))
  true
end

#parse(data) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/websocket/driver/hybi.rb', line 75

def parse(data)
  data = data.bytes.to_a if data.respond_to?(:bytes)
  @reader.put(data)
  buffer = true
  while buffer
    case @stage
      when 0 then
        buffer = @reader.read(1)
        parse_opcode(buffer[0]) if buffer

      when 1 then
        buffer = @reader.read(1)
        parse_length(buffer[0]) if buffer

      when 2 then
        buffer = @reader.read(@length_size)
        parse_extended_length(buffer) if buffer

      when 3 then
        buffer = @reader.read(4)
        if buffer
          @mask  = buffer
          @stage = 4
        end

      when 4 then
        buffer = @reader.read(@length)
        if buffer
          @payload = buffer
          emit_frame(buffer)
          @stage = 0
        end

      else
        buffer = nil
    end
  end
end

#ping(message = '', &callback) ⇒ Object



175
176
177
178
# File 'lib/websocket/driver/hybi.rb', line 175

def ping(message = '', &callback)
  @ping_callbacks[message] = callback if callback
  frame(message, :ping)
end

#text(message) ⇒ Object



167
168
169
# File 'lib/websocket/driver/hybi.rb', line 167

def text(message)
  frame(message, :text)
end

#versionObject



71
72
73
# File 'lib/websocket/driver/hybi.rb', line 71

def version
  "hybi-#{@socket.env['HTTP_SEC_WEBSOCKET_VERSION']}"
end