Skip to content

Commit

Permalink
Force UTF-8 encoding on incoming events
Browse files Browse the repository at this point in the history
If the "chunk" contains characters that aren't UTF-8, the library
will fail with "invalid byte sequence in UTF-8 (ArgumentError)".
This patch fixes this and ensures we work only with UTF-8.
  • Loading branch information
MusikAnimal committed Dec 30, 2018
1 parent dc15c19 commit 5151e1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/em-eventsource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def listen
@req.callback(&method(:handle_reconnect))
buffer = ""
@req.stream do |chunk|
unless chunk.valid_encoding?
chunk.scrub!('?')
end
buffer += chunk
while index = buffer.index(/\r\n\r\n|\n\n/)
stream = buffer.slice!(0..index)
Expand Down
11 changes: 11 additions & 0 deletions spec/em-eventsource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ def create_response_headers(status, content_type="", other={})
end
end

it "removes characters that are not UTF-8" do
start_source do |source, req|
source.message do |message|
message.must_equal "foo?bar"
source.close
EM.stop
end
req.stream_data("data: foo\xE4bar#{eol}#{eol}")
end
end

it "handle event name" do
start_source do |source, req|
source.on "plop" do |message|
Expand Down

0 comments on commit 5151e1f

Please sign in to comment.