This is an automated email from the ASF dual-hosted git repository.

chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-jspf.git

commit 3039751691c4f773bc0f6795b8501f9f023d2ad9
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Nov 19 17:29:00 2026 +0100

    [PERF] SPF1 parser: split the terms without regexes
    
    RFC4408SPF1Parser.parse removed "v=spf1" with String.replaceFirst,
    which compiles a Pattern and copies the record on every parse, then
    split the terms with Pattern.split("[ ]+"), a regex step at every
    character of a record the sender chooses. Together about 30% of the
    parse of a short record and 25% of the parse of a long hostile one.
    
    Cut the first "v=spf1" with indexOf and split on spaces by hand,
    dropping the empty terms as before. The version is still removed
    case-sensitively, as replaceFirst did.
    
    MacroExpand strips the leading zeros of a digit transformer with a
    loop instead of replaceFirst("^0+", ""), the last regex compiled per
    call there.
---
 .../org/apache/james/jspf/core/MacroExpand.java    |   6 +-
 .../james/jspf/parser/RFC4408SPF1Parser.java       | 114 ++++++++++++---------
 .../parser/RFC4408SPF1ParserSplitTermsTest.java    |  67 ++++++++++++
 3 files changed, 137 insertions(+), 50 deletions(-)

diff --git a/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java 
b/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java
index 2074ccd..569cb11 100644
--- a/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java
+++ b/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java
@@ -76,7 +76,11 @@ public class MacroExpand {
      *         them (RFC 7208 7.1)
      */
     private static int parseDigitTransformer(String digits) {
-        String value = digits.replaceFirst("^0+", "");
+        int zeros = 0;
+        while (zeros < digits.length() && digits.charAt(zeros) == '0') {
+            zeros++;
+        }
+        String value = digits.substring(zeros);
         if (value.length() > 9) {
             return Integer.MAX_VALUE;
         }
diff --git 
a/resolver/src/main/java/org/apache/james/jspf/parser/RFC4408SPF1Parser.java 
b/resolver/src/main/java/org/apache/james/jspf/parser/RFC4408SPF1Parser.java
index e573a2d..b6c9545 100644
--- a/resolver/src/main/java/org/apache/james/jspf/parser/RFC4408SPF1Parser.java
+++ b/resolver/src/main/java/org/apache/james/jspf/parser/RFC4408SPF1Parser.java
@@ -98,8 +98,6 @@ public class RFC4408SPF1Parser implements SPFRecordParser {
             + SPF1Constants.PASS + "\\" + SPF1Constants.FAIL + "\\"
             + SPF1Constants.NEUTRAL + "\\" + SPF1Constants.SOFTFAIL + "]";
 
-    private Pattern termsSeparatorPattern = null;
-
     private Pattern termPattern = null;
 
     private int TERM_STEP_REGEX_QUALIFIER_POS;
@@ -142,12 +140,6 @@ public class RFC4408SPF1Parser implements SPFRecordParser {
         String TERM_REGEX = "(?:" + MODIFIER_REGEX + "|" + DIRECTIVE_REGEX
                 + ")";
 
-        /**
-         * ABNF: 1*SP
-         */
-        String TERMS_SEPARATOR_REGEX = "[ ]+";
-
-        termsSeparatorPattern = Pattern.compile(TERMS_SEPARATOR_REGEX);
         termPattern = Pattern.compile(TERM_REGEX);
 
         initializePositions();
@@ -263,55 +255,48 @@ public class RFC4408SPF1Parser implements SPFRecordParser 
{
             throw new NoneException("No valid SPF Record: " + spfRecord);
         }
 
-        // extract terms
-        String[] terms = termsSeparatorPattern.split(spfRecord.replaceFirst(
-                SPF1Constants.SPF_VERSION1, ""));
-
         // cycle terms
-        for (int i = 0; i < terms.length; i++) {
-            if (terms[i].length() > 0) {
-                Matcher termMatcher = termPattern.matcher(terms[i]);
-                if (!termMatcher.matches()) {
-                    throw new PermErrorException("Term [" + terms[i]
-                            + "] is not syntactically valid: "
-                            + termPattern.pattern());
-                }
+        for (String term : splitTerms(spfRecord)) {
+            Matcher termMatcher = termPattern.matcher(term);
+            if (!termMatcher.matches()) {
+                throw new PermErrorException("Term [" + term
+                        + "] is not syntactically valid: "
+                        + termPattern.pattern());
+            }
 
-                // true if we matched a modifier, false if we matched a
-                // directive
-                String modifierString = termMatcher
-                        .group(TERM_STEP_REGEX_MODIFIER_POS);
-
-                if (modifierString != null) {
-                    // MODIFIER
-                    Modifier mod = (Modifier) lookupAndCreateTerm(termMatcher,
-                            TERM_STEP_REGEX_MODIFIER_POS);
-
-                    if (mod.enforceSingleInstance()) {
-                        Iterator<Modifier> it = 
result.getModifiers().iterator();
-                        while (it.hasNext()) {
-                            if (it.next().getClass().equals(mod.getClass())) {
-                                throw new PermErrorException("More than one "
-                                        + modifierString
-                                        + " found in SPF-Record");
-                            }
+            // true if we matched a modifier, false if we matched a
+            // directive
+            String modifierString = termMatcher
+                    .group(TERM_STEP_REGEX_MODIFIER_POS);
+
+            if (modifierString != null) {
+                // MODIFIER
+                Modifier mod = (Modifier) lookupAndCreateTerm(termMatcher,
+                        TERM_STEP_REGEX_MODIFIER_POS);
+
+                if (mod.enforceSingleInstance()) {
+                    Iterator<Modifier> it = result.getModifiers().iterator();
+                    while (it.hasNext()) {
+                        if (it.next().getClass().equals(mod.getClass())) {
+                            throw new PermErrorException("More than one "
+                                    + modifierString
+                                    + " found in SPF-Record");
                         }
                     }
+                }
 
-                    result.getModifiers().add(mod);
+                result.getModifiers().add(mod);
 
-                } else {
-                    // DIRECTIVE
-                    String qualifier = termMatcher
-                            .group(TERM_STEP_REGEX_QUALIFIER_POS);
+            } else {
+                // DIRECTIVE
+                String qualifier = termMatcher
+                        .group(TERM_STEP_REGEX_QUALIFIER_POS);
 
-                    Object mech = lookupAndCreateTerm(termMatcher,
-                            TERM_STEP_REGEX_MECHANISM_POS);
+                Object mech = lookupAndCreateTerm(termMatcher,
+                        TERM_STEP_REGEX_MECHANISM_POS);
 
-                    result.getDirectives().add(
-                            new Directive(qualifier, (Mechanism) mech));
-
-                }
+                result.getDirectives().add(
+                        new Directive(qualifier, (Mechanism) mech));
 
             }
         }
@@ -319,6 +304,37 @@ public class RFC4408SPF1Parser implements SPFRecordParser {
         return result;
     }
 
+    /**
+     * The terms of a record, without its first "v=spf1" (case-sensitive) and 
without the empty terms
+     * that several spaces in a row leave (ABNF: terms = *( 1*SP ( directive / 
modifier ) )).
+     *
+     * Neither a regex split nor String.replaceFirst: they cost a regex step 
per character of a
+     * record the sender chooses, and a Pattern compiled per call.
+     *
+     * @param spfRecord the record
+     * @return its terms
+     */
+    static List<String> splitTerms(String spfRecord) {
+        String rest = spfRecord;
+        int version = rest.indexOf(SPF1Constants.SPF_VERSION1);
+        if (version >= 0) {
+            rest = rest.substring(0, version) + rest.substring(version + 
SPF1Constants.SPF_VERSION1.length());
+        }
+        List<String> terms = new ArrayList<String>();
+        int start = 0;
+        while (start < rest.length()) {
+            int end = rest.indexOf(' ', start);
+            if (end < 0) {
+                end = rest.length();
+            }
+            if (end > start) {
+                terms.add(rest.substring(start, end));
+            }
+            start = end + 1;
+        }
+        return terms;
+    }
+
     /**
      * @param res
      *            the MatchResult
diff --git 
a/resolver/src/test/java/org/apache/james/jspf/parser/RFC4408SPF1ParserSplitTermsTest.java
 
b/resolver/src/test/java/org/apache/james/jspf/parser/RFC4408SPF1ParserSplitTermsTest.java
new file mode 100644
index 0000000..e37abf3
--- /dev/null
+++ 
b/resolver/src/test/java/org/apache/james/jspf/parser/RFC4408SPF1ParserSplitTermsTest.java
@@ -0,0 +1,67 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jspf.parser;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.regex.Pattern;
+
+import org.junit.Test;
+
+public class RFC4408SPF1ParserSplitTermsTest {
+
+    @Test
+    public void shouldDropTheVersionAndSplitOnSpaces() {
+        assertEquals(Arrays.asList("mx", "-all"), 
RFC4408SPF1Parser.splitTerms("v=spf1 mx -all"));
+    }
+
+    @Test
+    public void shouldSkipTheEmptyTermsOfSeveralSpaces() {
+        assertEquals(Arrays.asList("a", "mx", "-all"), 
RFC4408SPF1Parser.splitTerms("v=spf1   a  mx -all   "));
+    }
+
+    @Test
+    public void shouldGiveNoTermForASpaceOnly() {
+        assertEquals(Collections.emptyList(), 
RFC4408SPF1Parser.splitTerms("v=spf1 "));
+    }
+
+    @Test
+    public void shouldDropTheVersionCaseSensitivelyAsBefore() {
+        assertEquals(Arrays.asList("V=SPF1", "a"), 
RFC4408SPF1Parser.splitTerms("V=SPF1 a"));
+    }
+
+    @Test
+    public void shouldGiveTheTermsOfTheFormerRegexSplit() {
+        Pattern separator = Pattern.compile("[ ]+");
+        String[] records = {"v=spf1 a", "v=spf1  a  b", "v=spf1 a v=spf1 b", 
"V=SPF1 a v=spf1 b",
+            "v=spf1 redirect=v=spf1.example.com", "v=spf1     ", "v=spf1 
include:x  -all  "};
+        for (String record : records) {
+            java.util.List<String> expected = new java.util.ArrayList<>();
+            for (String term : separator.split(record.replaceFirst("v=spf1", 
""))) {
+                if (term.length() > 0) {
+                    expected.add(term);
+                }
+            }
+            assertEquals(record, expected, 
RFC4408SPF1Parser.splitTerms(record));
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to