File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -2,13 +2,19 @@ package mail
2
2
3
3
import (
4
4
"net/mail"
5
+ "regexp"
5
6
"time"
6
7
7
8
"github.com/emersion/go-message"
8
9
)
9
10
10
11
const dateLayout = "Mon, 02 Jan 2006 15:04:05 -0700"
11
12
13
+ // TODO: this is a blunt way to strip any trailing CFWS (comment). A sharper
14
+ // one would strip multiple CFWS, and only if really valid according to
15
+ // RFC5322.
16
+ var commentRE = regexp .MustCompile (`[ \t]+\(.*\)$` )
17
+
12
18
// A Header is a mail header.
13
19
type Header struct {
14
20
message.Header
@@ -31,7 +37,10 @@ func (h *Header) SetAddressList(key string, addrs []*Address) {
31
37
32
38
// Date parses the Date header field.
33
39
func (h * Header ) Date () (time.Time , error ) {
34
- return mail .ParseDate (h .Get ("Date" ))
40
+ //TODO: remove this once https://go-review.googlesource.com/c/go/+/117596/
41
+ // is merged
42
+ date := commentRE .ReplaceAllString (h .Get ("Date" ), "" )
43
+ return mail .ParseDate (date )
35
44
}
36
45
37
46
// SetDate formats the Date header field.
Original file line number Diff line number Diff line change @@ -42,3 +42,20 @@ func TestHeader(t *testing.T) {
42
42
t .Errorf ("Expected header subject to be %v, but got %v" , subject , got )
43
43
}
44
44
}
45
+
46
+ func TestCFWSDates (t * testing.T ) {
47
+ tc := []string {
48
+ "Mon, 22 Jul 2019 13:57:29 -0500 (GMT-05:00)" ,
49
+ "Mon, 22 Jul 2019 13:57:29 -0500" ,
50
+ "Mon, 2 Jan 06 15:04:05 MST (Some random stuff)" ,
51
+ "Mon, 2 Jan 06 15:04:05 MST" ,
52
+ }
53
+ var h mail.Header
54
+ for _ , tt := range tc {
55
+ h .Set ("Date" , tt )
56
+ _ , err := h .Date ()
57
+ if err != nil {
58
+ t .Errorf ("Failed to parse time %q: %v" , tt , err )
59
+ }
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments