Skip to content

Commit

Permalink
Merge pull request #421 from wneessen/bug/smime-fix-header-count
Browse files Browse the repository at this point in the history
Refactor header count logic for accurate line tracking
  • Loading branch information
wneessen authored Jan 13, 2025
2 parents 559046e + 077c438 commit f781a50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type Msg struct {

// headerCount is an indicate for how many headers have been written during the mail rendering process.
// This count can be helpful to identify where the mail header ends and the mail body starts
headerCount uint
headerCount int

// isDelivered indicates whether the Msg has been delivered.
isDelivered bool
Expand Down Expand Up @@ -3080,7 +3080,7 @@ func (m *Msg) signMessage() error {

// Since we only want to sign the message body, we need to find the position within
// the mail body from where we start reading.
var linecount uint = 0
linecount := 0
pos := 0
for linecount < m.headerCount {
nextIndex := bytes.Index(buf.Bytes()[pos:], []byte("\r\n"))
Expand Down
22 changes: 12 additions & 10 deletions msgwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func (mw *msgWriter) writeMsg(msg *Msg) {
}
}
if hasFrom && (len(from) > 0 && from[0] != nil) {
mw.writeHeader(Header(HeaderFrom), from[0].String())
msg.headerCount++
msg.headerCount += mw.writeHeader(Header(HeaderFrom), from[0].String())
}

// Set the rest of the address headers
Expand All @@ -125,8 +124,7 @@ func (mw *msgWriter) writeMsg(msg *Msg) {
for _, addr := range addresses {
val = append(val, addr.String())
}
mw.writeHeader(Header(to), val...)
msg.headerCount++
msg.headerCount += mw.writeHeader(Header(to), val...)
}
}

Expand Down Expand Up @@ -223,8 +221,7 @@ func (mw *msgWriter) writeGenHeader(msg *Msg) {

sort.Strings(keys)
for _, key := range keys {
mw.writeHeader(Header(key), msg.genHeader[Header(key)]...)
msg.headerCount++
msg.headerCount += mw.writeHeader(Header(key), msg.genHeader[Header(key)]...)
}
}

Expand All @@ -237,8 +234,9 @@ func (mw *msgWriter) writeGenHeader(msg *Msg) {
// - msg: The Msg object containing the preformatted headers to be written.
func (mw *msgWriter) writePreformattedGenHeader(msg *Msg) {
for key, val := range msg.preformHeader {
mw.writeString(fmt.Sprintf("%s: %s%s", key, val, SingleNewLine))
msg.headerCount++
line := fmt.Sprintf("%s: %s%s", key, val, SingleNewLine)
mw.writeString(line)
msg.headerCount += strings.Count(line, SingleNewLine)
}
}

Expand Down Expand Up @@ -459,14 +457,15 @@ func (mw *msgWriter) writeString(s string) {
// Parameters:
// - key: The Header key to be written.
// - values: A variadic parameter representing the values associated with the header.
func (mw *msgWriter) writeHeader(key Header, values ...string) {
func (mw *msgWriter) writeHeader(key Header, values ...string) int {
lines := 0
buffer := strings.Builder{}
charLength := MaxHeaderLength - 2
buffer.WriteString(string(key))
charLength -= len(key)
if len(values) == 0 {
buffer.WriteString(":\r\n")
return
return lines + 1
}
buffer.WriteString(": ")
charLength -= 2
Expand All @@ -491,6 +490,9 @@ func (mw *msgWriter) writeHeader(key Header, values ...string) {
SingleNewLine)
mw.writeString(bufferString)
mw.writeString("\r\n")

lines += strings.Count(bufferString, SingleNewLine) + 1
return lines
}

// writeBody writes an io.Reader into an io.Writer using the provided Encoding.
Expand Down

0 comments on commit f781a50

Please sign in to comment.