Skip to content
Browse files

Ensure we always use BINARY encoding of strings internally.

I was observing issues when using UTF-8 strings as inputs where
messages wouldn't roundtrip from Message.decode(msg.encode.to_s). These
changes ensures all strings are dealt with using BINARY encoding which
solves my problems.
  • Loading branch information...
1 parent 67f4894 commit 230c88227934b1486f81dc3e9c22acb4da6526bd @sundbp committed Mar 27, 2012
Showing with 10 additions and 2 deletions.
  1. +5 −1 lib/beefcake/buffer/base.rb
  2. +5 −1 lib/beefcake/buffer/encode.rb
View
6 lib/beefcake/buffer/base.rb
@@ -63,7 +63,11 @@ def initialize(s)
end
def initialize(buf="")
- self.buf = buf
+ if buf.respond_to?(:force_encoding)
+ self.buf = buf.force_encoding("BINARY")
+ else
+ self.buf = buf
+ end
end
if ''.respond_to?(:force_encoding)
View
6 lib/beefcake/buffer/encode.rb
@@ -106,7 +106,11 @@ def append_bool(n)
def append_string(s)
append_uint64(s.length)
- self << s
+ if s.respond_to?(:force_encoding)
+ self << s.force_encoding("BINARY")
+ else
+ self << s
+ end
end
alias :append_bytes :append_string

0 comments on commit 230c882

Please sign in to comment.
Something went wrong with that request. Please try again.