Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode period at start of line #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/mail/encoders/quoted_printable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Mail.Encoders.QuotedPrintable do

@new_line "=\r\n"
@max_length 76
@reserved_chars [?=, ??, ?_]
@reserved_chars [?=, ??, ?_, ?.]

@doc """
Encodes a string into a quoted-printable encoded string.
Expand Down Expand Up @@ -55,6 +55,21 @@ defmodule Mail.Encoders.QuotedPrintable do
end
end

# Encode ASCII period character
def encode(<<char, tail::binary>>, max_length, acc, line_length) when char in [?.] do
escaped = "=" <> Base.encode16(<<char>>)
line_length = line_length + byte_size(<<char>>)

case line_length do
1 ->
encode(tail, max_length, acc <> escaped, byte_size(escaped))
x when x < max_length ->
encode(tail, max_length, acc <> <<char>>, line_length)
_ ->
encode(tail, max_length, acc <> @new_line <> escaped, byte_size(escaped))
end
end

# Encode all other characters.
def encode(<<char, tail::binary>>, max_length, acc, line_length) do
escaped = "=" <> Base.encode16(<<char>>)
Expand Down
36 changes: 36 additions & 0 deletions test/mail/encoders/quoted_printable_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,42 @@ defmodule Mail.Encoders.QuotedPrintableTest do
assert Mail.Encoders.QuotedPrintable.encode(message) == encoding
end

test "encodes line beginning with a period" do
message = ".xxx"

encoding =
"=2Exxx"

assert Mail.Encoders.QuotedPrintable.encode(message) == encoding
end

test "encodes 75 chars ending with a period" do
message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."

encoding =
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."

assert Mail.Encoders.QuotedPrintable.encode(message) == encoding
end

test "encodes 76 chars ending with a period" do
message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."

encoding =
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\n=2E"

assert Mail.Encoders.QuotedPrintable.encode(message) == encoding
end

test "encodes 77 chars ending with a period" do
message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."

encoding =
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\nx."

assert Mail.Encoders.QuotedPrintable.encode(message) == encoding
end

test "decodes empty string" do
assert Mail.Encoders.QuotedPrintable.decode("") == ""
end
Expand Down