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.
This commit is contained in:
Sebastian Serth
2023-09-24 14:10:06 +02:00
parent e5678483cc
commit 968fd5b864

View File

@ -51,7 +51,7 @@ class Runner::Connection::Buffer
# We split lines by `\n` and want to normalize them to be separated by `\r\n`. # 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) # 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. # 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) # Same argumentation as above: We can always append (previous empty or invalid)
buffer += line buffer += line
@ -70,6 +70,13 @@ class Runner::Connection::Buffer
buffer buffer
end 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) def add_to_line_buffer(message)
@buffering = false @buffering = false
@global_buffer = +'' @global_buffer = +''