From 968fd5b864308d8d416487d9dcf1da735e667fba Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sun, 24 Sep 2023 14:10:06 +0200 Subject: [PATCH] Connection::Buffer: Fix recognition of incoming CLRF line endings Previously, a message received with CRLF line endings was (incorrectly) converted to CRCRLF line endings (i.e., from \r\n to \r\r\n). Then, the splitting in individual lines could generate an "empty" line, just containing the newly-added CR line ending \r. --- lib/runner/connection/buffer.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/runner/connection/buffer.rb b/lib/runner/connection/buffer.rb index e8f18e82..dcd01169 100644 --- a/lib/runner/connection/buffer.rb +++ b/lib/runner/connection/buffer.rb @@ -51,7 +51,7 @@ class Runner::Connection::Buffer # We split lines by `\n` and want to normalize them to be separated by `\r\n`. # This allows us to identify a former line end with `\r` (as the `\n` is not matched) # All results returned from this buffer are normalized to feature `\n` line endings. - message_parts.encode(crlf_newline: true).scan(SPLIT_INDIVIDUAL_LINES).each do |line| + normalized_line_endings(message_parts).scan(SPLIT_INDIVIDUAL_LINES).each do |line| # Same argumentation as above: We can always append (previous empty or invalid) buffer += line @@ -70,6 +70,13 @@ class Runner::Connection::Buffer buffer end + def normalized_line_endings(string) + # First, we ensure line endings are only represented by `\n`, regardless of the original line ending. + # Then, we convert all line endings to `\r\n` to ensure we can identify the `\r` at the end of a line. + # This "double conversion" is required to prevent line endings with \r\r\n. + string.encode(universal_newline: true).encode(crlf_newline: true) + end + def add_to_line_buffer(message) @buffering = false @global_buffer = +''