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:
@ -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 = +''
|
||||
|
Reference in New Issue
Block a user