profile
viewpoint

Ask questionsError decoding attachment file names in utf-8

Hi,

Any file name in the attachment is decoded incorrectly if it has at least one character with a length of more than one byte.

The fixUnescapedQuotes function in file header.go is not working properly.

This code

		param = param[startingQuote+1 : closingQuote]
		escaped := false
		for strIdx := range param {
			switch param[strIdx] {
			case '"':
				// We are inside of a quoted string, so lets escape this guy if it isn't already escaped.
				if !escaped {
					sb.WriteByte('\\')
					escaped = false
				}
				sb.WriteByte(param[strIdx])
			case '\\':
				// Something is getting escaped, a quote is the only char that needs
				// this, so lets assume the following char is a double-quote.
				escaped = true
				sb.WriteByte('\\')
			default:
				escaped = false
				sb.WriteByte(param[strIdx])
			}
		}

should be replaced with this one (WriteByte -> WriteRune).

		param = param[startingQuote+1 : closingQuote]
		escaped := false
		for _, c := range param {
			switch c {
			case '"':
				// We are inside of a quoted string, so lets escape this guy if it isn't already escaped.
				if !escaped {
					sb.WriteByte('\\')
					escaped = false
				}
				sb.WriteRune(c)
			case '\\':
				// Something is getting escaped, a quote is the only char that needs
				// this, so lets assume the following char is a double-quote.
				escaped = true
				sb.WriteByte('\\')
			default:
				escaped = false
				sb.WriteRune(c)
			}
		}

And one more value for header_test.go:

		{
			input: "application/rtf; charset=iso-8859-1; name=\"тест.rtf;\".rtf",
			want:  "application/rtf; charset=iso-8859-1; name=\"\\\"тест.rtf;\\\".rtf\"",
		},

Thanks.

jhillyerd/enmime

Answer questions jhillyerd

Please submit a pull request against the develop branch.

useful!

Related questions

No questions were found.
Github User Rank List