websocket-driver 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.2.1 / 2013-07-05
2
+
3
+ * Queue sent messages if the client has not begun trying to connect
4
+ * Encode all strings sent to I/O as `ASCII-8BIT`
5
+
1
6
  ### 0.2.0 / 2013-05-12
2
7
 
3
8
  * Add API for setting and reading headers
@@ -78,7 +78,7 @@ module WebSocket
78
78
 
79
79
  def start
80
80
  return false unless @ready_state == 0
81
- @socket.write(handshake_response)
81
+ @socket.write(Driver.encode(handshake_response, :binary))
82
82
  open unless @stage == -1
83
83
  true
84
84
  end
@@ -116,14 +116,6 @@ module WebSocket
116
116
  true
117
117
  end
118
118
 
119
- def self.encode(string, validate_encoding = false)
120
- if Array === string
121
- string = utf8_string(string)
122
- return nil if validate_encoding and !valid_utf8?(string)
123
- end
124
- utf8_string(string)
125
- end
126
-
127
119
  def self.client(socket, options = {})
128
120
  Client.new(socket, options.merge(:masking => true))
129
121
  end
@@ -143,6 +135,23 @@ module WebSocket
143
135
  end
144
136
  end
145
137
 
138
+ def self.encode(string, encoding = nil)
139
+ if Array === string
140
+ string = string.pack('C*')
141
+ encoding ||= :binary
142
+ else
143
+ encoding ||= :utf8
144
+ end
145
+ case encoding
146
+ when :binary
147
+ string.force_encoding('ASCII-8BIT') if string.respond_to?(:force_encoding)
148
+ when :utf8
149
+ string.force_encoding('UTF-8') if string.respond_to?(:force_encoding)
150
+ return nil unless valid_utf8?(string)
151
+ end
152
+ string
153
+ end
154
+
146
155
  def self.utf8_string(string)
147
156
  string = string.pack('C*') if Array === string
148
157
  string.respond_to?(:force_encoding) ?
@@ -150,8 +159,7 @@ module WebSocket
150
159
  string
151
160
  end
152
161
 
153
- def self.valid_utf8?(byte_array)
154
- string = utf8_string(byte_array)
162
+ def self.valid_utf8?(string)
155
163
  if defined?(UTF8_MATCH)
156
164
  UTF8_MATCH =~ string ? true : false
157
165
  else
@@ -23,7 +23,7 @@ module WebSocket
23
23
 
24
24
  def start
25
25
  return false unless @ready_state == -1
26
- @socket.write(handshake_request)
26
+ @socket.write(Driver.encode(handshake_request, :binary))
27
27
  @ready_state = 0
28
28
  true
29
29
  end
@@ -41,7 +41,7 @@ module WebSocket
41
41
 
42
42
  when 2 then
43
43
  if data == 0xFF
44
- emit(:message, MessageEvent.new(Driver.encode(@buffer)))
44
+ emit(:message, MessageEvent.new(Driver.encode(@buffer, :utf8)))
45
45
  @stage = 0
46
46
  else
47
47
  if @length
@@ -57,9 +57,8 @@ module WebSocket
57
57
 
58
58
  def frame(data, type = nil, error_type = nil)
59
59
  return queue([data, type, error_type]) if @ready_state == 0
60
- data = Driver.encode(data)
61
- frame = ["\x00", data, "\xFF"].map(&Driver.method(:encode)) * ''
62
- @socket.write(frame)
60
+ frame = ["\x00", data, "\xFF"].map { |s| Driver.encode(s, :binary) } * ''
61
+ @socket.write(Driver.encode(frame, :binary))
63
62
  true
64
63
  end
65
64
 
@@ -23,7 +23,7 @@ module WebSocket
23
23
 
24
24
  def close(reason = nil, code = nil)
25
25
  return false if @ready_state == 3
26
- @socket.write("\xFF\x00")
26
+ @socket.write(Driver.encode("\xFF\x00", :binary))
27
27
  @ready_state = 3
28
28
  emit(:close, CloseEvent.new(nil, nil))
29
29
  true
@@ -63,7 +63,7 @@ module WebSocket
63
63
 
64
64
  def send_handshake_body
65
65
  return unless signature = handshake_signature
66
- @socket.write(signature)
66
+ @socket.write(Driver.encode(signature, :binary))
67
67
  @stage = 0
68
68
  open
69
69
  parse(@body[BODY_SIZE..-1]) if @body.size > BODY_SIZE
@@ -103,11 +103,11 @@ module WebSocket
103
103
  end
104
104
 
105
105
  def frame(data, type = nil, code = nil)
106
- return queue([data, type, code]) if @ready_state == 0
106
+ return queue([data, type, code]) if @ready_state <= 0
107
107
  return false unless @ready_state == 1
108
108
 
109
109
  data = data.to_s unless Array === data
110
- data = Driver.encode(data) if String === data
110
+ data = Driver.encode(data, :utf8) if String === data
111
111
 
112
112
  is_text = (String === data)
113
113
  opcode = OPCODES[type || (is_text ? :text : :binary)]
@@ -151,7 +151,7 @@ module WebSocket
151
151
 
152
152
  frame.concat(buffer)
153
153
 
154
- @socket.write(Driver.encode(frame))
154
+ @socket.write(Driver.encode(frame, :binary))
155
155
  true
156
156
  end
157
157
 
@@ -292,7 +292,7 @@ module WebSocket
292
292
  @buffer.concat(payload)
293
293
  if @final
294
294
  message = @buffer
295
- message = Driver.encode(message, true) if @mode == :text
295
+ message = Driver.encode(message, :utf8) if @mode == :text
296
296
  reset
297
297
  if message
298
298
  emit(:message, MessageEvent.new(message))
@@ -303,7 +303,7 @@ module WebSocket
303
303
 
304
304
  when OPCODES[:text] then
305
305
  if @final
306
- message = Driver.encode(payload, true)
306
+ message = Driver.encode(payload, :utf8)
307
307
  if message
308
308
  emit(:message, MessageEvent.new(message))
309
309
  else
@@ -331,18 +331,20 @@ module WebSocket
331
331
  code = ERRORS[:protocol_error]
332
332
  end
333
333
 
334
- if payload.size > 125 or not Driver.valid_utf8?(payload[2..-1] || [])
334
+ message = Driver.encode(payload[2..-1] || [], :utf8)
335
+
336
+ if payload.size > 125 or message.nil?
335
337
  code = ERRORS[:protocol_error]
336
338
  end
337
339
 
338
- reason = (payload.size > 2) ? Driver.encode(payload[2..-1], true) : ''
340
+ reason = (payload.size > 2) ? message : ''
339
341
  shutdown(code, reason || '')
340
342
 
341
343
  when OPCODES[:ping] then
342
344
  frame(payload, :pong)
343
345
 
344
346
  when OPCODES[:pong] then
345
- message = Driver.encode(payload, true)
347
+ message = Driver.encode(payload, :utf8)
346
348
  callback = @ping_callbacks[message]
347
349
  @ping_callbacks.delete(message)
348
350
  callback.call if callback
@@ -50,7 +50,7 @@ module WebSocket
50
50
  end
51
51
 
52
52
  def write(data)
53
- @socket.write(data)
53
+ @socket.write(Driver.encode(data, :binary))
54
54
  end
55
55
 
56
56
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-12 00:00:00.000000000 Z
12
+ date: 2013-07-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine