Connection::Buffer: Restore buffering for lines ending with CR

With the turtle graphics, some very long lines might be returned by Poseidon just after the CR (\r). In this case, our new normalization will fix the mistake, but the next message received (just a LF `\n`) will be forwarded to the clients unchanged. This is not desired, so that we filter this case separately.
This commit is contained in:
Sebastian Serth
2023-09-24 16:29:08 +02:00
parent 4690dbee32
commit 077af74671

View File

@ -74,7 +74,16 @@ class Runner::Connection::Buffer
# 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)
normalized = string.encode(universal_newline: true).encode(crlf_newline: true)
# If the original input string ends with `\r`, it is incomplete and needs buffering.
# However, through our above normalization, the string would not end with `\r` anymore (but `\r\n`).
# Hence, in this case, we just remove the last character, so that all other line endings within the string remain unchanged.
if string.ends_with?("\r")
normalized[...-1]
else
normalized
end
end
def add_to_line_buffer(message)