Skip to content

Commit

Permalink
Refactor compressableMimeType
Browse files Browse the repository at this point in the history
Convert comma separated string to string array once and re-use it rather than converting for every new Processor

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1676477 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Apr 28, 2015
1 parent 909554c commit 364f7cf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 66 deletions.
30 changes: 22 additions & 8 deletions java/org/apache/coyote/http11/AbstractHttp11Protocol.java
Expand Up @@ -26,6 +26,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpUpgradeHandler;
Expand Down Expand Up @@ -131,16 +132,29 @@ public void setNoCompressionUserAgents(String valueS) {
}


private String compressableMimeTypes = "text/html,text/xml,text/plain";
public String getCompressableMimeType() { return compressableMimeTypes; }
private String compressableMimeType = "text/html,text/xml,text/plain";
private String[] compressableMimeTypes = null;
public String getCompressableMimeType() { return compressableMimeType; }
public void setCompressableMimeType(String valueS) {
compressableMimeTypes = valueS;
compressableMimeType = valueS;
compressableMimeTypes = null;
}
public String getCompressableMimeTypes() {
return getCompressableMimeType();
}
public void setCompressableMimeTypes(String valueS) {
setCompressableMimeType(valueS);
public String[] getCompressableMimeTypes() {
String[] result = compressableMimeTypes;
if (result != null) {
return result;
}
List<String> values = new ArrayList<>();
StringTokenizer tokens = new StringTokenizer(compressableMimeType, ",");
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken().trim();
if (token.length() > 0) {
values.add(token);
}
}
result = values.toArray(new String[values.size()]);
compressableMimeTypes = result;
return result;
}


Expand Down
60 changes: 3 additions & 57 deletions java/org/apache/coyote/http11/Http11Processor.java
Expand Up @@ -21,7 +21,6 @@
import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -201,11 +200,11 @@ public class Http11Processor extends AbstractProcessor {
*/
protected Pattern noCompressionUserAgents = null;


/**
* List of MIMES which could be gzipped
* List of MIMES for which compression may be enabled.
*/
protected String[] compressableMimeTypes =
{ "text/html", "text/xml", "text/plain" };
protected String[] compressableMimeTypes;


/**
Expand Down Expand Up @@ -320,18 +319,6 @@ public void setNoCompressionUserAgents(String noCompressionUserAgents) {
}
}

/**
* Add a mime-type which will be compressible
* The mime-type String will be exactly matched
* in the response mime-type header .
*
* @param mimeType mime-type string
*/
public void addCompressableMimeType(String mimeType) {
compressableMimeTypes =
addStringArray(compressableMimeTypes, mimeType);
}


/**
* Set compressible mime-type list (this method is best when used with
Expand All @@ -343,24 +330,6 @@ public void setCompressableMimeTypes(String[] compressableMimeTypes) {
}


/**
* Set compressable mime-type list
* List contains users agents separated by ',' :
*
* ie: "text/html,text/xml,text/plain"
*/
public void setCompressableMimeTypes(String compressableMimeTypes) {
if (compressableMimeTypes != null) {
this.compressableMimeTypes = null;
StringTokenizer st = new StringTokenizer(compressableMimeTypes, ",");

while (st.hasMoreTokens()) {
addCompressableMimeType(st.nextToken().trim());
}
}
}


/**
* Return compression level.
*/
Expand All @@ -377,29 +346,6 @@ public String getCompression() {
}


/**
* General use method
*
* @param sArray the StringArray
* @param value string
*/
private String[] addStringArray(String sArray[], String value) {
String[] result = null;
if (sArray == null) {
result = new String[1];
result[0] = value;
}
else {
result = new String[sArray.length + 1];
for (int i = 0; i < sArray.length; i++) {
result[i] = sArray[i];
}
result[sArray.length] = value;
}
return result;
}


/**
* Checks if any entry in the string array starts with the specified value
*
Expand Down
3 changes: 2 additions & 1 deletion webapps/docs/config/http.xml
Expand Up @@ -329,7 +329,8 @@
<attribute name="compressableMimeType" required="false">
<p>The value is a comma separated list of MIME types for which HTTP
compression may be used.
The default value is <code>text/html,text/xml,text/plain</code>.</p>
The default value is <code>text/html,text/xml,text/plain,text/css</code>.
</p>
</attribute>

<attribute name="compression" required="false">
Expand Down

0 comments on commit 364f7cf

Please sign in to comment.