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

Remove intermediate string allocation when writing match details log #781

Merged
merged 1 commit into from
May 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions internal/corazarules/rule_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@
return mr.Rule_
}

func (mr MatchedRule) details(matchData types.MatchData) string {
log := &strings.Builder{}

func (mr MatchedRule) writeDetails(log *strings.Builder, matchData types.MatchData) {
msg := matchData.Message()
data := matchData.Data()
if len(msg) > 200 {
Expand All @@ -125,7 +123,6 @@
}
log.WriteString(fmt.Sprintf(" [hostname %q] [uri %q] [unique_id %q]",
mr.ServerIPAddress_, mr.URI_, mr.TransactionID_))
return log.String()
}

func (mr MatchedRule) matchData(matchData types.MatchData) string {
Expand Down Expand Up @@ -161,7 +158,7 @@
log.WriteString("Coraza: Warning. ")
}
log.WriteString(mr.matchData(matchData))
log.WriteString(mr.details(matchData))
mr.writeDetails(log, matchData)

Check warning on line 161 in internal/corazarules/rule_match.go

View check run for this annotation

Codecov / codecov/patch

internal/corazarules/rule_match.go#L161

Added line #L161 was not covered by tests
log.WriteString("\n")
}
return log.String()
Expand Down Expand Up @@ -192,7 +189,7 @@
}
log.WriteString(msg)
log.WriteString(" ")
log.WriteString(mr.details(matchData))
mr.writeDetails(log, matchData)
log.WriteString("\n")
}
return log.String()
Expand Down