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

Refactor header count logic for accurate line tracking #421

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading