Skip to content

Commit

Permalink
Code clean-up - formatting. No functional change
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed May 20, 2024
1 parent c578fda commit 9512145
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 253 deletions.
48 changes: 26 additions & 22 deletions java/org/apache/catalina/valves/rewrite/QuotedStringTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public class QuotedStringTokenizer {
private int returnedTokens = 0;

enum WordMode {
SPACES, QUOTED, ESCAPED, SIMPLE, COMMENT
SPACES,
QUOTED,
ESCAPED,
SIMPLE,
COMMENT
}

public QuotedStringTokenizer(String text) {
Expand All @@ -55,27 +59,27 @@ private List<String> tokenizeText(String inputText) {
while (pos < length) {
char currentChar = inputText.charAt(pos);
switch (currentMode) {
case SPACES:
currentMode = handleSpaces(currentToken, currentChar);
break;
case QUOTED:
currentMode = handleQuoted(tokens, currentToken, currentChar);
break;
case ESCAPED:
currentToken.append(currentChar);
currentMode = WordMode.QUOTED;
break;
case SIMPLE:
currentMode = handleSimple(tokens, currentToken, currentChar);
break;
case COMMENT:
if (currentChar == '\r' || currentChar == '\n') {
currentMode = WordMode.SPACES;
}
break;
default:
throw new IllegalStateException(sm.getString("quotedStringTokenizer.tokenizeError",
inputText, Integer.valueOf(pos), currentMode));
case SPACES:
currentMode = handleSpaces(currentToken, currentChar);
break;
case QUOTED:
currentMode = handleQuoted(tokens, currentToken, currentChar);
break;
case ESCAPED:
currentToken.append(currentChar);
currentMode = WordMode.QUOTED;
break;
case SIMPLE:
currentMode = handleSimple(tokens, currentToken, currentChar);
break;
case COMMENT:
if (currentChar == '\r' || currentChar == '\n') {
currentMode = WordMode.SPACES;
}
break;
default:
throw new IllegalStateException(sm.getString("quotedStringTokenizer.tokenizeError", inputText,
Integer.valueOf(pos), currentMode));
}
pos++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,26 @@
/**
* Implement a map for the txt: and rnd: mod_rewrite capabilities.
*/
public class RandomizedTextRewriteMap implements RewriteMap{
public class RandomizedTextRewriteMap implements RewriteMap {

protected static final StringManager sm = StringManager.getManager(RandomizedTextRewriteMap.class);

private static final Random random = new Random();
private final Map<String, String[]> map = new HashMap<>();
private final Map<String,String[]> map = new HashMap<>();

/**
* Create a map from a text file according to the mod_rewrite syntax.
*
* @param txtFilePath the text file path
* @param useRandom if the map should produce random results
* @param useRandom if the map should produce random results
*/
public RandomizedTextRewriteMap(String txtFilePath, boolean useRandom) {
String line;
try (Resource txtResource = ConfigFileLoader.getSource().getResource(txtFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(txtResource.getInputStream()))) {
while ((line = reader.readLine()) != null) {
if (line.startsWith("#") || line.isEmpty()) {
//Ignore comment or empty lines
// Ignore comment or empty lines
continue;
}
String[] keyValuePair = line.split(" ", 2);
Expand Down
39 changes: 19 additions & 20 deletions java/org/apache/catalina/valves/rewrite/ResolverImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public ResolverImpl(Request request) {

/**
* The following are not implemented:
* - SERVER_ADMIN
* - API_VERSION
* - IS_SUBREQ
* <ul>
* <li>SERVER_ADMIN</li>
* <li>API_VERSION</li>
* <li>IS_SUBREQ</li>
* </ul>
*/
@Override
public String resolve(String key) {
Expand Down Expand Up @@ -113,8 +115,7 @@ public String resolve(String key) {
} else if (key.equals("SERVER_SOFTWARE")) {
return "tomcat";
} else if (key.equals("THE_REQUEST")) {
return request.getMethod() + " " + request.getRequestURI()
+ " " + request.getProtocol();
return request.getMethod() + " " + request.getRequestURI() + " " + request.getProtocol();
} else if (key.equals("REQUEST_URI")) {
return request.getRequestURI();
} else if (key.equals("REQUEST_FILENAME")) {
Expand Down Expand Up @@ -175,8 +176,8 @@ public String resolveSsl(String key) {
Set<Cipher> cipherList = OpenSSLCipherConfigurationParser.parse(cipherSuite);
if (cipherList.size() == 1) {
Cipher cipher = cipherList.iterator().next();
if (cipher.getLevel().equals(EncryptionLevel.EXP40)
|| cipher.getLevel().equals(EncryptionLevel.EXP56)) {
if (cipher.getLevel().equals(EncryptionLevel.EXP40) ||
cipher.getLevel().equals(EncryptionLevel.EXP56)) {
return "true";
} else {
return "false";
Expand Down Expand Up @@ -280,16 +281,15 @@ private String resolveSslCertificates(String key, X509Certificate[] certificates
key = key.substring("CERT_CHAIN_".length());
try {
return PEMFile.toPEM(certificates[Integer.parseInt(key)]);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException
| CertificateEncodingException e) {
} catch (NumberFormatException | ArrayIndexOutOfBoundsException | CertificateEncodingException e) {
// Ignore
}
}
return null;
}

private String resolveComponent(String fullDN, String component) {
HashMap<String, String> components = new HashMap<>();
HashMap<String,String> components = new HashMap<>();
StringTokenizer tokenizer = new StringTokenizer(fullDN, ",");
while (tokenizer.hasMoreElements()) {
String token = tokenizer.nextToken().trim();
Expand All @@ -316,8 +316,7 @@ private String resolveAlternateName(X509Certificate certificate, int type, int n
return elements.get(n);
}
}
} catch (NumberFormatException | ArrayIndexOutOfBoundsException
| CertificateParsingException e) {
} catch (NumberFormatException | ArrayIndexOutOfBoundsException | CertificateParsingException e) {
// Ignore
}
return null;
Expand All @@ -341,14 +340,14 @@ public boolean resolveResource(int type, String name) {
return false;
} else {
switch (type) {
case 0:
return resource.isDirectory();
case 1:
return resource.isFile();
case 2:
return resource.isFile() && resource.getContentLength() > 0;
default:
return false;
case 0:
return resource.isDirectory();
case 1:
return resource.isFile();
case 2:
return resource.isFile() && resource.getContentLength() > 0;
default:
return false;
}
}
}
Expand Down
39 changes: 21 additions & 18 deletions java/org/apache/catalina/valves/rewrite/RewriteCond.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public Matcher getMatcher() {

public static class LexicalCondition extends Condition {
/**
* <pre>
* -1: &lt;
* 0: =
* 1: &gt;
* 0: =
* 1: &gt;
* </pre>
*/
public int type = 0;
public String condition;
Expand All @@ -59,24 +61,26 @@ public static class LexicalCondition extends Condition {
public boolean evaluate(String value, Resolver resolver) {
int result = value.compareTo(condition);
switch (type) {
case -1:
return (result < 0);
case 0:
return (result == 0);
case 1:
return (result > 0);
default:
return false;
case -1:
return (result < 0);
case 0:
return (result == 0);
case 1:
return (result > 0);
default:
return 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;

Expand Down Expand Up @@ -114,7 +118,7 @@ public final void setFlagsString(String flagsString) {
this.flagsString = flagsString;
}

public void parse(Map<String, RewriteMap> maps) {
public void parse(Map<String,RewriteMap> maps) {
test = new Substitution();
test.setSub(testString);
test.parse(maps);
Expand Down Expand Up @@ -169,8 +173,7 @@ public Matcher getMatcher() {

@Override
public String toString() {
return "RewriteCond " + testString + " " + condPattern
+ ((flagsString != null) ? (" " + flagsString) : "");
return "RewriteCond " + testString + " " + condPattern + ((flagsString != null) ? (" " + flagsString) : "");
}


Expand All @@ -181,9 +184,8 @@ public String toString() {
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.
* 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;
Expand All @@ -196,9 +198,10 @@ public String toString() {
/**
* Evaluate the condition based on the context
*
* @param rule corresponding matched rule
* @param cond last matched condition
* @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) {
Expand Down
25 changes: 13 additions & 12 deletions java/org/apache/catalina/valves/rewrite/RewriteMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@
import org.apache.tomcat.util.res.StringManager;

/**
* Interface for user defined lookup/replacement logic that can be defined in
* a {@code rewrite.config} file by a {@code RewriteMap} directive. Such a map
* can then be used by a {@code RewriteRule} defined in the same file.
* Interface for user defined lookup/replacement logic that can be defined in a {@code rewrite.config} file by a
* {@code RewriteMap} directive. Such a map can then be used by a {@code RewriteRule} defined in the same file.
* <p>
* An example {@code rewrite.config} file could look like:
*
* <pre>
* RewriteMap uc example.UpperCaseMap
*
* RewriteRule ^/(.*)$ ${uc:$1}
* </pre>
*
* One parameter can be optionally appended to the {@code RewriteMap} directive.
* This could be used &ndash; for example &ndash; to specify a name of a file, that
* contains a lookup table used by the implementation of the map.
* One parameter can be optionally appended to the {@code RewriteMap} directive. This could be used &ndash; for example
* &ndash; to specify a name of a file, that contains a lookup table used by the implementation of the map.
*/
public interface RewriteMap {

/**
* Optional parameter that can be defined through the {@code RewriteMap}
* directive in the {@code rewrite.config} file.
* Optional parameter that can be defined through the {@code RewriteMap} directive in the {@code rewrite.config}
* file.
*
* @param params the optional parameter
*
* @return value is currently ignored
*/
String setParameters(String params);

/**
* Optional parameters that can be defined through the {@code RewriteMap}
* directive in the {@code rewrite.config} file.
* Optional parameters that can be defined through the {@code RewriteMap} directive in the {@code rewrite.config}
* file.
* <p>
* This method will be called, if there are more than one parameters defined.
*
Expand All @@ -66,10 +66,11 @@ default void setParameters(String... params) {

/**
* Maps a key to a replacement value.<br>
* The method is free to return {@code null} to indicate, that the default
* value from the {@code RewriteRule} directive should be used.
* The method is free to return {@code null} to indicate, that the default value from the {@code RewriteRule}
* directive should be used.
*
* @param key used by the actual implementation to generate a mapped value
*
* @return mapped value or {@code null}
*/
String lookup(String key);
Expand Down

0 comments on commit 9512145

Please sign in to comment.