ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/valves/rewrite/RewriteCond.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/valves/rewrite/RewriteCond.java
Status
scanned
Type
file
Name
RewriteCond.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
7501 (7.3 KB)
MD5
15b7981bfafc21c2ed15c79e5493c14e
SHA1
532c0bc944349bd33d6adc62ea86f168bd18d735
SHA256
2da86d148464a96681659636212e5f3dc8e2bdcab941e7864803245598249810
SHA512

      
    
SHA1_git
f628a222149f6b538e6980d304128dc83dab30f4
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
RewriteCond.java | 7.3 KB |

/* * 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.catalina.valves.rewrite; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RewriteCond { public abstract static class Condition { public abstract boolean evaluate(String value, Resolver resolver); } public static class PatternCondition extends Condition { public Pattern pattern; private final ThreadLocal<Matcher> matcher = new ThreadLocal<>(); @Override public boolean evaluate(String value, Resolver resolver) { Matcher m = pattern.matcher(value); if (m.matches()) { matcher.set(m); return true; } else { return false; } } public Matcher getMatcher() { return matcher.get(); } } public static class LexicalCondition extends Condition { /** * <pre> * -1: &lt; * 0: = * 1: &gt; * </pre> */ public int type = 0; public String condition; @Override public boolean evaluate(String value, Resolver resolver) { int result = value.compareTo(condition); return switch (type) { case -1 -> (result < 0); case 0 -> (result == 0); case 1 -> (result > 0); default -> false; }; } } public static class ResourceCondition extends Condition { /** * <pre> * 0: -d (is directory ?) * 1: -f (is regular file ?) * 2: -s (is regular file with size ?) * </pre> */ public int type = 0; @Override public boolean evaluate(String value, Resolver resolver) { return resolver.resolveResource(type, value); } } protected String testString = null; protected String condPattern = null; protected String flagsString = null; public String getCondPattern() { return condPattern; } public void setCondPattern(String condPattern) { this.condPattern = condPattern; } public String getTestString() { return testString; } public void setTestString(String testString) { this.testString = testString; } public final String getFlagsString() { return flagsString; } public final void setFlagsString(String flagsString) { this.flagsString = flagsString; } public void parse(Map<String,RewriteMap> maps) { test = new Substitution(); test.setSub(testString); test.parse(maps); if (condPattern.startsWith("!")) { positive = false; condPattern = condPattern.substring(1); } if (condPattern.startsWith("<")) { LexicalCondition ncondition = new LexicalCondition(); ncondition.type = -1; ncondition.condition = condPattern.substring(1); this.condition = ncondition; } else if (condPattern.startsWith(">")) { LexicalCondition ncondition = new LexicalCondition(); ncondition.type = 1; ncondition.condition = condPattern.substring(1); this.condition = ncondition; } else if (condPattern.startsWith("=")) { LexicalCondition ncondition = new LexicalCondition(); ncondition.type = 0; ncondition.condition = condPattern.substring(1); this.condition = ncondition; } else if (condPattern.equals("-d")) { ResourceCondition ncondition = new ResourceCondition(); ncondition.type = 0; this.condition = ncondition; } else if (condPattern.equals("-f")) { ResourceCondition ncondition = new ResourceCondition(); ncondition.type = 1; this.condition = ncondition; } else if (condPattern.equals("-s")) { ResourceCondition ncondition = new ResourceCondition(); ncondition.type = 2; this.condition = ncondition; } else { PatternCondition ncondition = new PatternCondition(); int flags = Pattern.DOTALL; if (isNocase()) { flags |= Pattern.CASE_INSENSITIVE; } ncondition.pattern = Pattern.compile(condPattern, flags); this.condition = ncondition; } } public Matcher getMatcher() { if (condition instanceof PatternCondition) { return ((PatternCondition) condition).getMatcher(); } return null; } @Override public String toString() { return "RewriteCond " + testString + " " + condPattern + ((flagsString != null) ? (" " + flagsString) : ""); } protected boolean positive = true; protected Substitution test = null; protected Condition condition = null; /** * This makes the test case-insensitive, i.e., there is no difference between 'A-Z' and 'a-z' both in the expanded * TestString and the CondPattern. This flag is effective only for comparisons between TestString and CondPattern. * It has no effect on filesystem and subrequest checks. */ public boolean nocase = false; /** * Use this to combine rule conditions with a local OR instead of the implicit AND. */ public boolean ornext = false; /** * Evaluate the condition based on the context * * @param rule corresponding matched rule * @param cond last matched condition * @param resolver Property resolver * * @return <code>true</code> if the condition matches */ public boolean evaluate(Matcher rule, Matcher cond, Resolver resolver) { String value = test.evaluate(rule, cond, resolver); if (positive) { return condition.evaluate(value, resolver); } else { return !condition.evaluate(value, resolver); } } public boolean isNocase() { return nocase; } public void setNocase(boolean nocase) { this.nocase = nocase; } public boolean isOrnext() { return ornext; } public void setOrnext(boolean ornext) { this.ornext = ornext; } public boolean isPositive() { return positive; } public void setPositive(boolean positive) { this.positive = positive; } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
17.42
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9