Skip to content

Commit

Permalink
Optimise strings.lower()
Browse files Browse the repository at this point in the history
  • Loading branch information
soujanyanmbri committed Oct 9, 2024
1 parent 84521d3 commit aaed653
Show file tree
Hide file tree
Showing 24 changed files with 197 additions and 67 deletions.
4 changes: 3 additions & 1 deletion experimental/plugins/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/types/variables"

stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
)

type Macro interface {
Expand Down Expand Up @@ -134,7 +136,7 @@ func (m *macro) compile(input string) error {
m.tokens = append(m.tokens, macroToken{
text: currentToken.String(),
variable: v,
key: strings.ToLower(key),
key: stringsutil.AsciiToLower(key),
})
currentToken.Reset()
continue
Expand Down
6 changes: 3 additions & 3 deletions internal/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package actions
import (
"errors"
"fmt"
"strings"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
)

var (
Expand All @@ -28,7 +28,7 @@ var actionmap = map[string]ruleActionWrapper{}
// It can be used also for plugins.
// If you register an action with an existing name, it will be overwritten.
func Register(name string, a func() plugintypes.Action) {
name = strings.ToLower(name)
name = stringsutil.AsciiToLower(name)
actionmap[name] = a
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func init() {
// Get returns an unwrapped RuleAction from the actionmap based on the name
// If the action does not exist it returns an error
func Get(name string) (plugintypes.Action, error) {
name = strings.ToLower(name)
name = stringsutil.AsciiToLower(name)
if a, ok := actionmap[name]; ok {
return a(), nil
}
Expand Down
10 changes: 5 additions & 5 deletions internal/actions/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/collections"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
utils "github.com/corazawaf/coraza/v3/internal/strings"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -115,7 +115,7 @@ func (a *ctlFn) Init(_ plugintypes.RuleMetadata, data string) error {

// parseOnOff turns a string value into a boolean equivalent on/off into true/false
func parseOnOff(s string) (bool, bool) {
val := strings.ToLower(s)
val := stringsutil.AsciiToLower(s)
switch val {
case "on":
return true, true
Expand Down Expand Up @@ -144,7 +144,7 @@ func (a *ctlFn) Evaluate(_ plugintypes.RuleMetadata, txS plugintypes.Transaction
case ctlRuleRemoveTargetByTag:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if utils.InSlice(a.value, r.Tags_) {
if stringsutil.InSlice(a.value, r.Tags_) {
tx.RemoveRuleTargetByID(r.ID(), a.collection, a.colKey)
}
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (a *ctlFn) Evaluate(_ plugintypes.RuleMetadata, txS plugintypes.Transaction
case ctlRuleRemoveByTag:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if utils.InSlice(a.value, r.Tags_) {
if stringsutil.InSlice(a.value, r.Tags_) {
tx.RemoveRuleByID(r.ID_)
}
}
Expand Down Expand Up @@ -386,7 +386,7 @@ func parseCtl(data string) (ctlFunctionType, string, variables.RuleVariable, str
colname, colkey, _ = strings.Cut(col, ":")
}
collection, _ := variables.Parse(strings.TrimSpace(colname))
colkey = strings.ToLower(colkey)
colkey = stringsutil.AsciiToLower(colkey)
var act ctlFunctionType
switch action {
case "auditEngine":
Expand Down
3 changes: 2 additions & 1 deletion internal/actions/setvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/experimental/plugins/macro"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types/variables"
)

Expand Down Expand Up @@ -112,7 +113,7 @@ func (a *setvarFn) Evaluate(r plugintypes.RuleMetadata, tx plugintypes.Transacti
Str("var_value", value).
Int("rule_id", r.ID()).
Msg("Action evaluated")
a.evaluateTxCollection(r, tx, strings.ToLower(key), value)
a.evaluateTxCollection(r, tx, stringsutil.AsciiToLower(key), value)
}

func (a *setvarFn) Type() plugintypes.ActionType {
Expand Down
10 changes: 5 additions & 5 deletions internal/auditlog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package auditlog

import (
"fmt"
"strings"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
)

// NewConfig returns a Config with default values.
Expand All @@ -27,13 +27,13 @@ var formatters = map[string]plugintypes.AuditLogFormatter{}
// RegisterWriter registers a new logger
// it can be used for plugins
func RegisterWriter(name string, writer func() plugintypes.AuditLogWriter) {
writers[strings.ToLower(name)] = writer
writers[stringsutil.AsciiToLower(name)] = writer
}

// GetWriter returns a logger by name
// It returns an error if it doesn't exist
func GetWriter(name string) (plugintypes.AuditLogWriter, error) {
logger := writers[strings.ToLower(name)]
logger := writers[stringsutil.AsciiToLower(name)]
if logger == nil {
return nil, fmt.Errorf("invalid logger %q", name)
}
Expand All @@ -43,13 +43,13 @@ func GetWriter(name string) (plugintypes.AuditLogWriter, error) {
// RegisterFormatter registers a new logger format
// it can be used for plugins
func RegisterFormatter(name string, f plugintypes.AuditLogFormatter) {
formatters[strings.ToLower(name)] = f
formatters[stringsutil.AsciiToLower(name)] = f
}

// GetFormatter returns a formatter by name
// It returns an error if it doesn't exist
func GetFormatter(name string) (plugintypes.AuditLogFormatter, error) {
formatter := formatters[strings.ToLower(name)]
formatter := formatters[stringsutil.AsciiToLower(name)]
if formatter == nil {
return nil, fmt.Errorf("invalid formatter %q", name)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/bodyprocessors/bodyprocessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ package bodyprocessors

import (
"fmt"
"strings"

stringsutil "github.com/corazawaf/coraza/v3/internal/strings"

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
)
Expand All @@ -24,7 +25,7 @@ func RegisterBodyProcessor(name string, fn func() plugintypes.BodyProcessor) {
// GetBodyProcessor returns a body processor by name
// If the body processor is not found, it returns an error
func GetBodyProcessor(name string) (plugintypes.BodyProcessor, error) {
if fn, ok := processors[strings.ToLower(name)]; ok {
if fn, ok := processors[stringsutil.AsciiToLower(name)]; ok {
return fn(), nil
}
return nil, fmt.Errorf("invalid bodyprocessor %q", name)
Expand Down
4 changes: 2 additions & 2 deletions internal/collections/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package collections

import (
"regexp"
"strings"

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/internal/corazarules"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func NewConcatKeyed(variable variables.RuleVariable, data ...collection.Keyed) *
}

func (c *ConcatKeyed) Get(key string) []string {
keyL := strings.ToLower(key)
keyL := stringsutil.AsciiToLower(key)
var res []string
for _, c := range c.data {
res = append(res, c.Get(keyL)...)
Expand Down
13 changes: 7 additions & 6 deletions internal/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/corazawaf/coraza/v3/collection"
"github.com/corazawaf/coraza/v3/internal/corazarules"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func (c *Map) Get(key string) []string {
return nil
}
if !c.isCaseSensitive {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
var values []string
for _, a := range c.data[key] {
Expand Down Expand Up @@ -81,7 +82,7 @@ func (c *Map) FindString(key string) []types.MatchData {
return nil
}
if !c.isCaseSensitive {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
// if key is not empty
if e, ok := c.data[key]; ok {
Expand Down Expand Up @@ -115,7 +116,7 @@ func (c *Map) FindAll() []types.MatchData {
func (c *Map) Add(key string, value string) {
aVal := keyValue{key: key, value: value}
if !c.isCaseSensitive {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
c.data[key] = append(c.data[key], aVal)
}
Expand All @@ -124,7 +125,7 @@ func (c *Map) Add(key string, value string) {
func (c *Map) Set(key string, values []string) {
originalKey := key
if !c.isCaseSensitive {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
c.data[key] = make([]keyValue, 0, len(values))
for _, v := range values {
Expand All @@ -136,7 +137,7 @@ func (c *Map) Set(key string, values []string) {
func (c *Map) SetIndex(key string, index int, value string) {
originalKey := key
if !c.isCaseSensitive {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
values := c.data[key]
av := keyValue{key: originalKey, value: value}
Expand All @@ -154,7 +155,7 @@ func (c *Map) SetIndex(key string, index int, value string) {
// Remove removes a key/value from the map.
func (c *Map) Remove(key string) {
if !c.isCaseSensitive {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
if len(c.data) == 0 {
return
Expand Down
4 changes: 2 additions & 2 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package corazawaf
import (
"fmt"
"regexp"
"strings"
"sync"
"unsafe"

Expand All @@ -15,6 +14,7 @@ import (
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/internal/memoize"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
Expand Down Expand Up @@ -486,7 +486,7 @@ func caseSensitiveVariable(v variables.RuleVariable) bool {
// but the knowledge of the type of the Map it not here also, so let's start with this.
func newRuleVariableParams(v variables.RuleVariable, key string, re *regexp.Regexp, iscount bool) ruleVariableParams {
if !caseSensitiveVariable(v) {
key = strings.ToLower(key)
key = stringsutil.AsciiToLower(key)
}
return ruleVariableParams{
Count: iscount,
Expand Down
12 changes: 6 additions & 6 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ func (tx *Transaction) AddRequestHeader(key string, value string) {
if key == "" {
return
}
keyl := strings.ToLower(key)
keyl := stringsutil.AsciiToLower(key)
tx.variables.requestHeaders.Add(key, value)

switch keyl {
case "content-type":
val := strings.ToLower(value)
val := stringsutil.AsciiToLower(value)
if val == "application/x-www-form-urlencoded" {
tx.variables.reqbodyProcessor.Set("URLENCODED")
} else if strings.HasPrefix(val, "multipart/form-data") {
Expand Down Expand Up @@ -360,7 +360,7 @@ func (tx *Transaction) AddResponseHeader(key string, value string) {
if key == "" {
return
}
keyl := strings.ToLower(key)
keyl := stringsutil.AsciiToLower(key)
tx.variables.responseHeaders.Add(key, value)

// Most headers can be managed like that
Expand Down Expand Up @@ -592,9 +592,9 @@ func (tx *Transaction) GetField(rv ruleVariableParams) []types.MatchData {

for _, c := range matches {
isException := false
lkey := strings.ToLower(c.Key())
lkey := stringsutil.AsciiToLower(c.Key())
for _, ex := range rv.Exceptions {
if (ex.KeyRx != nil && ex.KeyRx.MatchString(lkey)) || strings.ToLower(ex.KeyStr) == lkey {
if (ex.KeyRx != nil && ex.KeyRx.MatchString(lkey)) || stringsutil.AsciiToLower(ex.KeyStr) == lkey {
isException = true
break
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ func (tx *Transaction) ProcessRequestBody() (*types.Interruption, error) {
}
tx.variables.reqbodyProcessor.Set(rbp)
}
rbp = strings.ToLower(rbp)
rbp = stringsutil.AsciiToLower(rbp)
if rbp == "" {
// so there is no bodyprocessor, we don't want to generate an error
tx.WAF.Rules.Eval(types.PhaseRequestBody, tx)
Expand Down
3 changes: 2 additions & 1 deletion internal/operators/pm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/memoize"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
)

type pm struct {
Expand All @@ -23,7 +24,7 @@ var _ plugintypes.Operator = (*pm)(nil)
func newPM(options plugintypes.OperatorOptions) (plugintypes.Operator, error) {
data := options.Arguments

data = strings.ToLower(data)
data = stringsutil.AsciiToLower(data)
dict := strings.Split(data, " ")
builder := ahocorasick.NewAhoCorasickBuilder(ahocorasick.Opts{
AsciiCaseInsensitive: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/operators/pm_from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/memoize"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
)

func newPMFromFile(options plugintypes.OperatorOptions) (plugintypes.Operator, error) {
Expand All @@ -35,7 +36,7 @@ func newPMFromFile(options plugintypes.OperatorOptions) (plugintypes.Operator, e
if l[0] == '#' {
continue
}
lines = append(lines, strings.ToLower(l))
lines = append(lines, stringsutil.AsciiToLower(l))
}

builder := ahocorasick.NewAhoCorasickBuilder(ahocorasick.Opts{
Expand Down
3 changes: 2 additions & 1 deletion internal/operators/validate_nid.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/memoize"
stringsutil "github.com/corazawaf/coraza/v3/internal/strings"
)

type validateNidFunction = func(input string) bool
Expand Down Expand Up @@ -72,7 +73,7 @@ func nidCl(nid string) bool {
if len(nid) < 8 {
return false
}
nid = strings.ToLower(nid)
nid = stringsutil.AsciiToLower(nid)
nid = nonDigitOrK.ReplaceAllString(nid, "")
rut, _ := strconv.Atoi(nid[:len(nid)-1])
dv := nid[len(nid)-1:]
Expand Down
Loading

0 comments on commit aaed653

Please sign in to comment.