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

mime: cannot round-trip Word{En,De}coder #14608

Closed
nigeltao opened this issue Mar 2, 2016 · 5 comments
Closed

mime: cannot round-trip Word{En,De}coder #14608

nigeltao opened this issue Mar 2, 2016 · 5 comments
Milestone

Comments

@nigeltao
Copy link
Contributor

nigeltao commented Mar 2, 2016

WordEncoder sometimes passes text straight through, sometimes it does not. From the example_test.go:

fmt.Println(mime.QEncoding.Encode("utf-8", "¡Hola, señor!"))
fmt.Println(mime.QEncoding.Encode("utf-8", "Hello!"))

prints

// Output:
// =?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
// Hello!

The problem is that, given an arbitrary output of a WordEncoder, how should a user tell whether or not to pass it to a WordDecoder to recover the original? A WD returns an error if the input isn't actually escaped:

func main() {
    testCases := []string{
        "=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=",
        "Hello!",
    }
    for _, tc := range testCases {
        d := new(mime.WordDecoder)
        fmt.Println(d.Decode(tc))
    }
}

prints

¡Hola, señor! <nil>
 mime: invalid RFC 2047 encoded-word

RFC 2047 actually says that Generally, an "encoded-word" is a sequence of printable ASCII characters that begins with "=?", ends with "?=", and has two "?"s in between and, as a workaround, users of the mime package can check that before calling Decode.

Still, this should probably be provided by the library, instead of users of that library. I can think of two possible solutions:

  1. Add a WordDecoder.CanDecode(string) bool method. The caller should call that before calling Decode.
  2. Export the existing errInvalidWord as ErrInvalidWord (or as a better name). The caller should check err == mime.ErrInvalidWord after calling Decode.

@bradfitz, WDYT?

@nigeltao
Copy link
Contributor Author

nigeltao commented Mar 3, 2016

CC @alexcesaro.

@natefinch
Copy link
Contributor

Thanks for filing this. Honestly, my preference would be for a 3rd way - if the word is not encoded, just return it.. that would mirror what WordEncoder does, and would be what I would expect to work. But I think that may not be possible with the backwards compatibility guarantee.

Barring that, an IsEncoded function would be a nice alternate, so I could know if I need to try to Decode at all. The problem with calling it CanDecode is that if it returns false, I wonder if it actually is encoded, but incorrectly or something. IsEncoded is more clear, IMO. Obviously, it may still be encoded in some way that the Decoder doesn't understand, but as far as the Decoder is concerned, that's still "not encoded".

@bradfitz
Copy link
Contributor

bradfitz commented Mar 3, 2016

I'm fine with options 1, 2 or 3, actually.

@bradfitz bradfitz added this to the Go1.7 milestone Mar 3, 2016
@alexcesaro
Copy link
Contributor

I'm on holidays with a slow wifi but if I remember correctly
WordDecoder.DecodeHeader has the behavior you are looking for.

The documentation might need some clarification.
On Mar 3, 2016 11:05 AM, "Brad Fitzpatrick" notifications@github.com
wrote:

I'm fine with options 1, 2 or 3, actually.


Reply to this email directly or view it on GitHub
#14608 (comment).

@nigeltao
Copy link
Contributor Author

I'm closing this based on @alexcesaro's comment. @natefinch, please re-open if his suggestion doesn't work for you.

@golang golang locked and limited conversation to collaborators Mar 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants