Skip to content

Commit

Permalink
Merge pull request #47 from naskoro/headers-with-canonical-mime-keys
Browse files Browse the repository at this point in the history
Update Headers and RawHeaders using canonical MIME keys.
  • Loading branch information
xdsxc authored Aug 12, 2024
2 parents 9326dd0 + a2bae4a commit 3eee0b7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
14 changes: 4 additions & 10 deletions gmime/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,13 @@ func (m *Envelope) Headers() textproto.MIMEHeader {
// TODO: this is not super efficient, but easier to read, may be optimize this?
headers := C.g_mime_object_get_header_list(m.asGMimeObject())
count := C.g_mime_header_list_get_count(headers)
goHeaders := make(textproto.MIMEHeader, int(count))
goHeaders := make(textproto.MIMEHeader)
var i C.int
for i = 0; i < count; i++ {
header := C.g_mime_header_list_get_header_at(headers, i)
name := C.GoString(C.g_mime_header_get_name(header))
value := C.GoString(C.g_mime_header_get_value(header))
if _, ok := goHeaders[name]; !ok {
goHeaders[name] = nil
}
goHeaders[name] = append(goHeaders[name], value)
goHeaders.Add(name, value)
}
return goHeaders
}
Expand All @@ -71,16 +68,13 @@ func (m *Envelope) RawHeaders() textproto.MIMEHeader {
// TODO: this is not super efficient, but easier to read, may be optimize this?
headers := C.g_mime_object_get_header_list(m.asGMimeObject())
count := C.g_mime_header_list_get_count(headers)
goHeaders := make(textproto.MIMEHeader, int(count))
goHeaders := make(textproto.MIMEHeader)
var i C.int
for i = 0; i < count; i++ {
header := C.g_mime_header_list_get_header_at(headers, i)
name := C.GoString(C.g_mime_header_get_name(header))
value := C.GoString(C.g_mime_header_get_raw_value(header))
if _, ok := goHeaders[name]; !ok {
goHeaders[name] = nil
}
goHeaders[name] = append(goHeaders[name], value)
goHeaders.Add(name, value)
}
return goHeaders
}
Expand Down
73 changes: 73 additions & 0 deletions gmime/gmime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/mail"
"os"
"runtime"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -172,6 +173,78 @@ func TestAddHTMLAlternativeToPlainText(t *testing.T) {
msg.Close()
}

func TestHeaders(t *testing.T) {
mimeBytes, err := ioutil.ReadFile("test_data/multipleHeaders.eml")
assert.NoError(t, err)
msg, err := Parse(string(mimeBytes))
assert.NoError(t, err)

assert.Equal(t, "<CAGPJ=uY91HEGoszHE9ELkB3wfcNJN4NGORM9q-vV8o_XJceBmg@mail.gmail.com>", msg.Headers().Get("Message-ID"))
assert.Equal(t, "<CAGPJ=uY91HEGoszHE9ELkB3wfcNJN4NGORM9q-vV8o_XJceBmg@mail.gmail.com>", msg.Headers().Get("Message-Id"))

headers := []string{}
for name, values := range msg.Headers() {
for _, value := range values {
headers = append(headers, fmt.Sprintf("%s: %s", name, value))
}
}
sort.Strings(headers)
assert.Equal(t, []string{
"Bcc: Tim <[email protected]>",
"Cc: trevor <[email protected]>",
"Date: Tue, 27 May 2014 13:46:04 -0700",
"Delivered-To: [email protected]",
"From: Kien Pham <[email protected]>",
"Message-Id: <CAGPJ=uY91HEGoszHE9ELkB3wfcNJN4NGORM9q-vV8o_XJceBmg@mail.gmail.com>",
"Mime-Version: 1.0",
"Received: by 10.220.115.205 with HTTP; Tue, 27 May 2014 13:46:04 -0700 (PDT)",
"Reply-To: isaac <[email protected]>",
"Sender: Kane Kim <[email protected]>",
"Subject: test inline image attachment",
"To: Kien Pham <[email protected]>",
"X-Header: 1",
"X-Header: 2",
"X-Header: 3",
"X-Header: 4",
}, headers)
}

func TestRawHeaders(t *testing.T) {
mimeBytes, err := ioutil.ReadFile("test_data/multipleHeaders.eml")
assert.NoError(t, err)
msg, err := Parse(string(mimeBytes))
assert.NoError(t, err)

assert.Equal(t, " <CAGPJ=uY91HEGoszHE9ELkB3wfcNJN4NGORM9q-vV8o_XJceBmg@mail.gmail.com>\n", msg.RawHeaders().Get("Message-ID"))
assert.Equal(t, " <CAGPJ=uY91HEGoszHE9ELkB3wfcNJN4NGORM9q-vV8o_XJceBmg@mail.gmail.com>\n", msg.RawHeaders().Get("Message-Id"))

headers := []string{}
for name, values := range msg.RawHeaders() {
for _, value := range values {
headers = append(headers, fmt.Sprintf("%s:%s", name, value))
}
}
sort.Strings(headers)
assert.Equal(t, []string{
"Bcc: Tim <[email protected]>\n",
"Cc: trevor <[email protected]>\n",
"Date: Tue, 27 May 2014 13:46:04 -0700\n",
"Delivered-To: [email protected]\n",
"From: Kien Pham <[email protected]>\n",
"Message-Id: <CAGPJ=uY91HEGoszHE9ELkB3wfcNJN4NGORM9q-vV8o_XJceBmg@mail.gmail.com>\n",
"Mime-Version: 1.0\n",
"Received: by 10.220.115.205 with HTTP; Tue, 27 May 2014 13:46:04 -0700 (PDT)\n",
"Reply-To: isaac <[email protected]>\n",
"Sender: Kane Kim <[email protected]>\n",
"Subject: test inline image attachment\n",
"To: Kien Pham <[email protected]>\n",
"X-Header: 1\n",
"X-Header: 2\n",
"X-Header: 3\n",
"X-Header: 4\n",
}, headers)
}

func TestRemoveAll(t *testing.T) {
mimeBytes, err := ioutil.ReadFile("test_data/multipleHeaders.eml")
assert.NoError(t, err)
Expand Down

0 comments on commit 3eee0b7

Please sign in to comment.