Can't round trip through mime.WordEncoder / mime.WordDecoder?

81 views
Skip to first unread message

Nate Finch

unread,
Mar 2, 2016, 2:11:37 PM3/2/16
to golang-nuts
Am I missing something?  mime.WordEncoder says: 

Encode returns the encoded-word form of s. If s is ASCII without special characters, it is returned unchanged.

The problem is, that mime.WordDecoder can't handle non-encoded strings, it just returns an error if there's no format specified in the beginning of the string.... so how do I decode something that WordEncoder encoded?  For now, if Decode fails, I try re-encoding to see if it returns the same string as the input.  That's kind of horrible, but I don't really understand how else I should be using WordDecoder.

Nigel Tao

unread,
Mar 2, 2016, 6:43:36 PM3/2/16
to Nate Finch, Brad Fitzpatrick, golang-nuts
https://www.ietf.org/rfc/rfc2047.txt says that `Generally, an
"encoded-word" is a sequence of printable ASCII characters that begins
with "=?", ends with "?=", and has two "?"s in between.`

So you could check that before passing s to a WordDecoder. It is
indeed the first thing that WordDecoder.Decode does.

We can't change Go 1.6 now, but for Go 1.7, this should probably be
provided by the library, instead of users of that library. I filed
https://github.com/golang/go/issues/14608

Nate Finch

unread,
Mar 2, 2016, 9:41:19 PM3/2/16
to golang-nuts, nate....@gmail.com, brad...@golang.org
Thanks, Nigel.  
Reply all
Reply to author
Forward
0 new messages