lib/http/2/buffer.rb in http-2-0.8.0 vs lib/http/2/buffer.rb in http-2-0.8.1

- old
+ new

@@ -1,8 +1,13 @@ module HTTP2 # Simple binary buffer backed by string. # + # TODO: Refactor, it would be better if Buffer were not a String subclass, + # but rather wrap a string and only expose the mutating API needed so that + # the possible surface for things to go wrong stays controllable. + # - https://github.com/igrigorik/http-2/pull/46 + # class Buffer < String UINT32 = 'N'.freeze private_constant :UINT32 # Forces binary encoding on the string @@ -27,8 +32,17 @@ # Slice unsigned 32-bit integer from buffer. # @return [Integer] def read_uint32 read(4).unpack(UINT32).first + end + + # Ensures that data that is added is binary encoded as well, + # otherwise this could lead to the Buffer instance changing its encoding. + [:<<, :prepend].each do |mutating_method| + define_method(mutating_method) do |string| + string = string.dup if string.frozen? + super(string.force_encoding(Encoding::BINARY)) + end end end end