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

remove duplicate in content type from bogus raw emails #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dstpierre
Copy link

I got and error when a content type contained a duplicate example:

"image/png; x-unix-mode=0644; name=image001.png; name=image001.png"

Go's mime.ParseMediaType returns an error if a duplicate like this is found causing the email parsing to stop, but this is only a bogus glitches that I think only some mail client would introduce.

I've added a removeDuplicate function that is call when the error: mime: duplicate parameter name is returned by mime.ParseMediaType.

It's basically simply removing duplicate and retry the function.


var cleaned string
for k := range m {
cleaned += k + ";"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a space after the ;?
Also, you can probably just return strings.Join(m, "; ") as that is both cleaner and faster

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I got ahead of myself, it is a map not a slice/array. I guess you could use a bytes.Buffer, though it won't really matter for a small number of entries.

// removeDuplicate removes duplicate from bogus content type preventing parsing of email
func removeDuplicate(s string) string {
m := make(map[string]bool)
parts := strings.Split(s, ";")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to re-lookup content types, but isn't the very first entry a special case?
Golang map keys are random, so that image/png entry might get put at the back of the list.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I'm reading this in the RFC 2046

In general, the top-level media type is used to declare the general
type of data, while the subtype specifies a specific format for that
type of data.

Not sure if "In General" means "required" though? I can have the first attribute saved, and remove duplicate than returning first attr + the cleaned up ones?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good, I think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just keep them all in order with a slice beside the map...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants