You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Dan Sandberg <x...@cs.stanford.edu> on 2002/05/09 08:07:49 UTC
[PATCH] SSI Updates - First Step
Hi Everyone. I finished up the SSI rewrite. I fixed many things that I
will detail in my next message.
I changed the DefaultServlet.java a little bit to factor out some code
that the SSI stuff needed as well. The CVS diff is below. The attached
zip has three new classes that are needed by my SSI stuff and by
DefaultServlet.java.
The three new classes are in org.apache.catalina.util and are:
URLEncoder.java - Has the code factored out of DefaultServlet.java
Strftime.java - Has code to do C-style strftime date formatting. Pulled
from one of the SSI classes.
IOTools.java - Has code to 'flow' data from an input stream to an
outputstream, or a reader to a writer.
I'll e-mail the rest of the files ( the SSI classes ) once these things
have been integrated into CVS.
Unzip the zip in the jakarta-tomcat-4.0.3-src/catalina/src/share
directory to put the files in the right place.
Let me know if there are any problems / suggestions / whatever.
Thanks,
-Dan
Index: catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
retrieving revision 1.54
diff -u -r1.54 DefaultServlet.java
---
catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
17 Apr 2002 05:49:59 -0000 1.54
+++
catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java 9
May 2002 05:52:02 -0000
@@ -80,7 +80,6 @@
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
-import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Enumeration;
@@ -89,7 +88,6 @@
import java.util.Locale;
import java.util.TimeZone;
import java.util.Hashtable;
-import java.util.BitSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.security.MessageDigest;
@@ -117,7 +115,7 @@
import org.apache.catalina.util.RequestUtil;
import org.apache.catalina.util.ServerInfo;
import org.apache.catalina.util.StringManager;
-
+import org.apache.catalina.util.URLEncoder;
/**
* The default resource-serving servlet for most web applications,
@@ -197,12 +195,25 @@
/**
+ * Array containing the safe characters set.
+ */
+ protected static URLEncoder urlEncoder;
+
+
+ /**
* GMT timezone - all HTTP dates are on GMT
*/
static {
formats[0].setTimeZone(gmtZone);
formats[1].setTimeZone(gmtZone);
formats[2].setTimeZone(gmtZone);
+
+ urlEncoder = new URLEncoder();
+ urlEncoder.addSafeCharacter('-');
+ urlEncoder.addSafeCharacter('_');
+ urlEncoder.addSafeCharacter('.');
+ urlEncoder.addSafeCharacter('*');
+ urlEncoder.addSafeCharacter('/');
}
@@ -226,45 +237,10 @@
/**
- * Array containing the safe characters set.
- */
- protected static BitSet safeCharacters;
-
-
- protected static final char[] hexadecimal =
- {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E', 'F'};
-
-
- /**
* Size of file transfer buffer in bytes.
*/
private static final int BUFFER_SIZE = 4096;
-
- // ----------------------------------------------------- Static
Initializer
-
-
- static {
- safeCharacters = new BitSet(256);
- int i;
- for (i = 'a'; i <= 'z'; i++) {
- safeCharacters.set(i);
- }
- for (i = 'A'; i <= 'Z'; i++) {
- safeCharacters.set(i);
- }
- for (i = '0'; i <= '9'; i++) {
- safeCharacters.set(i);
- }
- safeCharacters.set('-');
- safeCharacters.set('_');
- safeCharacters.set('.');
- safeCharacters.set('*');
- safeCharacters.set('/');
- }
-
-
// --------------------------------------------------------- Public
Methods
@@ -1016,55 +992,7 @@
* @param path Path which has to be rewiten
*/
protected String rewriteUrl(String path) {
-
- /**
- * Note: This code portion is very similar to URLEncoder.encode.
- * Unfortunately, there is no way to specify to the URLEncoder
which
- * characters should be encoded. Here, ' ' should be encoded as
"%20"
- * and '/' shouldn't be encoded.
- */
-
- int maxBytesPerChar = 10;
- int caseDiff = ('a' - 'A');
- StringBuffer rewrittenPath = new StringBuffer(path.length());
- ByteArrayOutputStream buf = new
ByteArrayOutputStream(maxBytesPerChar);
- OutputStreamWriter writer = null;
- try {
- writer = new OutputStreamWriter(buf, "UTF8");
- } catch (Exception e) {
- e.printStackTrace();
- writer = new OutputStreamWriter(buf);
- }
-
- for (int i = 0; i < path.length(); i++) {
- int c = (int) path.charAt(i);
- if (safeCharacters.get(c)) {
- rewrittenPath.append((char)c);
- } else {
- // convert to external encoding before hex conversion
- try {
- writer.write(c);
- writer.flush();
- } catch(IOException e) {
- buf.reset();
- continue;
- }
- byte[] ba = buf.toByteArray();
- for (int j = 0; j < ba.length; j++) {
- // Converting each byte in the buffer
- byte toEncode = ba[j];
- rewrittenPath.append('%');
- int low = (int) (toEncode & 0x0f);
- int high = (int) ((toEncode & 0xf0) >> 4);
- rewrittenPath.append(hexadecimal[high]);
- rewrittenPath.append(hexadecimal[low]);
- }
- buf.reset();
- }
- }
-
- return rewrittenPath.toString();
-
+ return urlEncoder.encode( path );
}