Skip to content

Commit

Permalink
fix(smtp_server): updated line split logic, normalize all linebreaks …
Browse files Browse the repository at this point in the history
…to \r\n (#897)

Co-authored-by: Vishnu S <[email protected]>
  • Loading branch information
vishnus and Vishnu S authored Jul 27, 2021
1 parent 670ea65 commit e8ba9ee
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/postal/smtp_server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ def run_event_loop
# Client went away
eof = true
end

# Normalize all \r\n and \n to \r\n
buffers[io] = buffers[io].encode(buffers[io].encoding, universal_newline: true).encode(buffers[io].encoding, crlf_newline: true)

# We line buffer, so look to see if we have received a newline
# and keep doing so until all buffered lines have been processed.
while buffers[io].index("\n")
while buffers[io].index("\r\n")
# Extract the line
if buffers[io].index("\r\n")
line, buffers[io] = buffers[io].split("\r\n", 2)
else
line, buffers[io] = buffers[io].split("\n", 2)
end
line, buffers[io] = buffers[io].split("\r\n", 2)

# Send the received line to the client object for processing
result = client.handle(line)
# If the client object returned some data, write it back to the client
Expand Down

0 comments on commit e8ba9ee

Please sign in to comment.