https://issues.apache.org/bugzilla/show_bug.cgi?id=49102
authortimw <timw@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 23 Sep 2010 10:38:56 +0000 (10:38 +0000)
committertimw <timw@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 23 Sep 2010 10:38:56 +0000 (10:38 +0000)
Protecting AJP code -> header/method lookup arrays with getters.
No measurable slowdown (especially after a profiling JIT gets done with it).

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1000400 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/coyote/ajp/AjpAprProcessor.java
java/org/apache/coyote/ajp/AjpProcessor.java
java/org/apache/coyote/ajp/Constants.java

index 48f92e1..c0c3c07 100644 (file)
@@ -713,7 +713,7 @@ public class AjpAprProcessor implements ActionHook {
         // Translate the HTTP method code to a String.
         byte methodCode = requestHeaderMessage.getByte();
         if (methodCode != Constants.SC_M_JK_STORED) {
-            String methodName = Constants.methodTransArray[methodCode - 1];
+            String methodName = Constants.getMethodForCode(methodCode - 1);
             request.method().setString(methodName);
         }
 
@@ -747,7 +747,7 @@ public class AjpAprProcessor implements ActionHook {
             isc &= 0xFF00;
             if(0xA000 == isc) {
                 requestHeaderMessage.getInt(); // To advance the read position
-                hName = Constants.headerTransArray[hId - 1];
+                hName = Constants.getHeaderForCode(hId - 1);
                 vMB = headers.addValue(hName);
             } else {
                 // reset hId -- if the header currently being read
index c7c45ba..f6097c0 100644 (file)
@@ -719,7 +719,7 @@ public class AjpProcessor implements ActionHook {
         // Translate the HTTP method code to a String.
         byte methodCode = requestHeaderMessage.getByte();
         if (methodCode != Constants.SC_M_JK_STORED) {
-            String methodName = Constants.methodTransArray[methodCode - 1];
+            String methodName = Constants.getMethodForCode(methodCode - 1);
             request.method().setString(methodName);
         }
 
@@ -753,7 +753,7 @@ public class AjpProcessor implements ActionHook {
             isc &= 0xFF00;
             if(0xA000 == isc) {
                 requestHeaderMessage.getInt(); // To advance the read position
-                hName = Constants.headerTransArray[hId - 1];
+                hName = Constants.getHeaderForCode(hId - 1);
                 vMB = headers.addValue(hName);
             } else {
                 // reset hId -- if the header currently being read
index 007fec9..b2e6b81 100644 (file)
@@ -120,7 +120,7 @@ public final class Constants {
     public static final int  MAX_SEND_SIZE = MAX_PACKET_SIZE - SEND_HEAD_LEN;
 
     // Translates integer codes to names of HTTP methods
-    public static final String []methodTransArray = {
+    private static final String [] methodTransArray = {
             "OPTIONS",
             "GET",
             "HEAD",
@@ -149,6 +149,16 @@ public final class Constants {
             "BASELINE-CONTROL",
             "MKACTIVITY"
     };
+    
+    /**
+     * Converts an AJP coded HTTP method to the method name.
+     * @param code the coded value
+     * @return the string value of the method
+     */
+       public static final String getMethodForCode(final int code) {
+               return methodTransArray[code];
+       }
+
     public static final int SC_M_JK_STORED = (byte) 0xFF;
 
     // id's for common request headers
@@ -170,7 +180,7 @@ public final class Constants {
     public static final byte SC_A_SSL_KEY_SIZE  = 11; // XXX ???
 
     // Translates integer codes to request header names
-    public static final String []headerTransArray = {
+    private static final String [] headerTransArray = {
             "accept",
             "accept-charset",
             "accept-encoding",
@@ -187,8 +197,17 @@ public final class Constants {
             "user-agent"
     };
 
+    /**
+     * Converts an AJP coded HTTP request header to the header name.
+     * @param code the coded value
+     * @return the string value of the header name
+     */
+       public static final String getHeaderForCode(final int code) {
+               return headerTransArray[code];
+       }
+
     // Translates integer codes to response header names
-    public static final String []responseTransArray = {
+    private static final String [] responseTransArray = {
             "Content-Type",
             "Content-Language",
             "Content-Length",
@@ -201,6 +220,15 @@ public final class Constants {
             "Status",
             "WWW-Authenticate"
     };
+    
+    /**
+     * Converts an AJP coded response header name to the HTTP response header name.
+     * @param code the coded value
+     * @return the string value of the header
+     */
+       public static final String getResponseHeaderForCode(final int code) {
+               return responseTransArray[code];
+       }
 
     private static final Hashtable<String,Integer>  responseTransHash =
         new Hashtable<String,Integer>(20);
@@ -209,7 +237,7 @@ public final class Constants {
         try {
             int i;
             for (i = 0; i < SC_RESP_AJP13_MAX; i++) {
-                responseTransHash.put(responseTransArray[i],
+                responseTransHash.put(getResponseHeaderForCode(i),
                                       new Integer(0xA001 + i));
             }
         }