forked from DockYard/elixir-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Renderer content disposition encoding #7
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
Open
romsahel
wants to merge
2
commits into
parser-rfc-2822-memory-optimization
Choose a base branch
from
renderer-content-disposition-encoding
base: parser-rfc-2822-memory-optimization
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,15 @@ defmodule Mail.Renderers.RFC2822 do | |
| render_header_value(key, value) | ||
| end | ||
|
|
||
| @rfc2231_headers ["Content-Disposition", "Content-Type"] | ||
| defp render_header_value(key, value) when key in @rfc2231_headers and is_binary(value) do | ||
| value | ||
| end | ||
|
|
||
| defp render_header_value(key, [value | subtypes]) when key in @rfc2231_headers do | ||
| Enum.join([value | render_subtypes(subtypes, :rfc_2231)], "; ") | ||
| end | ||
|
|
||
| defp render_header_value(_key, [value | subtypes]), | ||
| do: | ||
| Enum.join([encode_header_value(value, :quoted_printable) | render_subtypes(subtypes)], "; ") | ||
|
|
@@ -143,27 +152,46 @@ defmodule Mail.Renderers.RFC2822 do | |
|
|
||
| defp render_address(email), do: validate_address(email) | ||
|
|
||
| defp render_subtypes([]), do: [] | ||
| defp render_subtypes(subtypes, encoding \\ :quoted_printable) | ||
|
|
||
| defp render_subtypes([], _encoding), do: [] | ||
|
|
||
| defp render_subtypes([{key, value} | subtypes]) when is_atom(key), | ||
| do: render_subtypes([{Atom.to_string(key), value} | subtypes]) | ||
| defp render_subtypes([{key, value} | subtypes], encoding) when is_atom(key), | ||
| do: render_subtypes([{Atom.to_string(key), value} | subtypes], encoding) | ||
|
|
||
| defp render_subtypes([{"boundary", value} | subtypes]) do | ||
| [~s(boundary="#{value}") | render_subtypes(subtypes)] | ||
| defp render_subtypes([{"boundary", value} | subtypes], encoding) do | ||
| [~s(boundary="#{value}") | render_subtypes(subtypes, encoding)] | ||
| end | ||
|
|
||
| defp render_subtypes([{key, value} | subtypes]) do | ||
| # RFC 2231 parameter encoding for Content-Type and Content-Disposition | ||
| defp render_subtypes([{key, value} | subtypes], :rfc_2231) do | ||
| key = String.replace(key, "_", "-") | ||
| value = encode_header_value(value, :quoted_printable) | ||
|
|
||
| value = | ||
| if value =~ ~r/[\s()<>@,;:\\<\/\[\]?=]/ do | ||
| "\"#{value}\"" | ||
| else | ||
| value | ||
| end | ||
| if contains_non_ascii?(value) do | ||
| value = encode_header_value(value, :rfc_2231) | ||
| ["#{key}*=UTF-8''#{value}" | render_subtypes(subtypes, :rfc_2231)] | ||
| else | ||
| value = maybe_wrap_in_quotes(value) | ||
| ["#{key}=#{value}" | render_subtypes(subtypes, :rfc_2231)] | ||
| end | ||
| end | ||
|
Comment on lines
+167
to
+177
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new handler is here. |
||
|
|
||
| ["#{key}=#{value}" | render_subtypes(subtypes)] | ||
| defp render_subtypes([{key, value} | subtypes], :quoted_printable) do | ||
| key = String.replace(key, "_", "-") | ||
| value = value |> encode_header_value(:quoted_printable) |> maybe_wrap_in_quotes() | ||
| ["#{key}=#{value}" | render_subtypes(subtypes, :quoted_printable)] | ||
| end | ||
|
|
||
| defp contains_non_ascii?(value) do | ||
| String.match?(value, ~r/[\x80-\xFF]/) | ||
| end | ||
|
|
||
| defp maybe_wrap_in_quotes(value) do | ||
| if value =~ ~r/[\s()<>@,;:\\<\/\[\]?=]/ do | ||
| "\"#{value}\"" | ||
| else | ||
| value | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
|
|
@@ -198,6 +226,11 @@ defmodule Mail.Renderers.RFC2822 do | |
| end | ||
| end | ||
|
|
||
| defp encode_header_value(header_value, :rfc_2231) do | ||
| # RFC 2231: parameter*=UTF-8''percent-encoded-value | ||
| URI.encode(header_value, &URI.char_unreserved?/1) | ||
| end | ||
|
|
||
| defp wrap_encoded_words(value) do | ||
| :binary.split(value, "=\r\n", [:global]) | ||
| |> Enum.map(fn chunk -> <<"=?UTF-8?Q?", chunk::binary, "?=">> end) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parser currently only supported Parameter Value Continuations which has the format
key*index="value"The non-continuation version is simply:
key*="value"