niax / rust-email

Implementation of MIME Messages for Rust
MIT License
62 stars 34 forks source link

Encode header values according to RFC 2047 #44

Open milibopp opened 6 years ago

milibopp commented 6 years ago

I started this to address #3. This does not do much yet, it only checks whether the value is ASCII and, if not, uses base64 encoding.

It's better than nothing still and I'd be willing to do the rest of #3, but wanted to get some feedback, as the issue is quite old and I have not worked on this crate before.

Is changing the impl of Display for Header sufficient to make this work?

RalfJung commented 6 years ago

Headers containing email addresses are not fully encoded, just the "name" part is. Quoting from the RFC:

   From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
   To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
   CC: =?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>
   Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
    =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=

   From: =?ISO-8859-1?Q?Olle_J=E4rnefors?= <ojarnef@admin.kth.se>
   To: ietf-822@dimacs.rutgers.edu, ojarnef@admin.kth.se
   Subject: Time for ISO 10646?

   To: Dave Crocker <dcrocker@mordor.stanford.edu>
   Cc: ietf-822@dimacs.rutgers.edu, paf@comsol.se
   From: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <paf@nada.kth.se>
   Subject: Re: RFC-HDR care and feeding

   From: Nathaniel Borenstein <nsb@thumper.bellcore.com>
         (=?iso-8859-8?b?7eXs+SDv4SDp7Oj08A==?=)
   To: Greg Vaudreuil <gvaudre@NRI.Reston.VA.US>, Ned Freed
      <ned@innosoft.com>, Keith Moore <moore@cs.utk.edu>
   Subject: Test of new header generator

Does this code do that? It seems to just encode the full header, but I may be reading it incorrectly.

milibopp commented 6 years ago

No, you are right, it does encode the full header. Thank you for bringing this up, I guess this has to be handled more carefully.

milibopp commented 5 years ago

I finally started to pick this issue up again. Anyway, I am not entirely sure on the specification here. For simplicity, I decided to encode each part of the header separately.

However, reading the RFC more closely the specification requires much more. It almost seems like one would have to parse the string being put into the header value to encode only what is supposed to be encoded.