lib/ftw/websocket/parser.rb in ftw-0.0.8 vs lib/ftw/websocket/parser.rb in ftw-0.0.9
- old
+ new
@@ -45,11 +45,11 @@
private
# A new WebSocket protocol parser.
def initialize
- @logger = Cabin::Channel.get($0)
+ @logger = Cabin::Channel.get
@opcode = 0
@masking_key = ""
@flag_final_payload = 0
@flag_mask = 0
@@ -169,13 +169,13 @@
# | |Masking-key, if MASK set to 1 |
# +-------------------------------+-------------------------------+
data = get
case @need
when 2
- @payload_length = data.unpack("S")
+ @payload_length = data.unpack("S").first
when 8
- @payload_length = data.unpack("Q")
+ @payload_length = data.unpack("Q").first
else
raise "Unknown payload_length byte length '#{@need}'"
end
if @masked
@@ -188,10 +188,12 @@
# This state yields no output.
return nil
end # def extended_payload_length
+ # State: mask
+ # Read the mask key
def mask
# + - - - - - - - - - - - - - - - +-------------------------------+
# | |Masking-key, if MASK set to 1 |
# +-------------------------------+-------------------------------+
# | Masking-key (continued) | Payload Data |
@@ -201,11 +203,10 @@
end # def mask
# State: payload
# Read the full payload and return it.
# See: http://tools.ietf.org/html/rfc6455#section-5.3
- #
def payload
# TODO(sissel): Handle massive payload lengths without exceeding memory.
# Perhaps if the payload is large (say, larger than 500KB by default),
# instead of returning the whole thing, simply return an Enumerable that
# yields chunks of the payload. There's no reason to buffer the entire
@@ -217,19 +218,23 @@
else
return data
end
end # def payload
+ # Unmask the message using the key.
+ #
+ # For implementation specification, see
+ # http://tools.ietf.org/html/rfc6455#section-5.3
def unmask(message, key)
masked = []
mask_bytes = key.unpack("C4")
i = 0
message.each_byte do |byte|
masked << (byte ^ mask_bytes[i % 4])
i += 1
end
p :unmasked => masked.pack("C*"), :original => message
return masked.pack("C*")
- end # def mask
+ end # def unmask
public(:feed)
end # class FTW::WebSocket::Parser