Implemented our own getPathInfo to use the decoding from java.net.URLDecoder.

Removed old slack since we now have some guarantees about req.path.
This commit is contained in:
hns 2002-10-17 16:00:58 +00:00
parent 9e0dae779c
commit 933a0ef7a3
5 changed files with 42 additions and 156 deletions

View file

@ -9,9 +9,9 @@ package helma.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.*;
import java.net.URLEncoder;
import java.net.URLDecoder;
import helma.framework.*;
import helma.util.*;
@ -57,7 +57,7 @@ public abstract class AbstractServletClient extends HttpServlet {
}
abstract ResponseTrans execute (RequestTrans req, String reqPath) throws Exception;
abstract ResponseTrans execute (RequestTrans req) throws Exception;
public void doGet (HttpServletRequest request, HttpServletResponse response)
@ -174,8 +174,8 @@ public abstract class AbstractServletClient extends HttpServlet {
if ( authorization != null )
reqtrans.set ("authorization", authorization );
String pathInfo = request.getPathInfo ();
ResponseTrans restrans = execute (reqtrans, pathInfo);
reqtrans.path = getPathInfo (request);
ResponseTrans restrans = execute (reqtrans);
writeResponse (request, response, restrans);
@ -475,6 +475,26 @@ public abstract class AbstractServletClient extends HttpServlet {
return false;
}
String getPathInfo (HttpServletRequest req) {
StringTokenizer t = new StringTokenizer (req.getContextPath(), "/");
int prefixTokens = t.countTokens();
t = new StringTokenizer (req.getServletPath(), "/");
prefixTokens += t.countTokens();
t = new StringTokenizer (req.getRequestURI(), "/");
int uriTokens = t.countTokens();
StringBuffer pathbuffer = new StringBuffer ();
for (int i=0; i<uriTokens; i++) {
String token = t.nextToken ();
if (i < prefixTokens)
continue;
if (i > prefixTokens)
pathbuffer.append ("/");
pathbuffer.append (URLDecoder.decode (token));
}
return pathbuffer.toString ();
}
public String getServletInfo(){
return new String("Helma Servlet Client");
}

View file

@ -39,36 +39,12 @@ public final class EmbeddedServletClient extends AbstractServletClient {
mountpoint = "/"+appName;
}
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
req.path = getRequestPath (reqPath);
ResponseTrans execute (RequestTrans req) throws Exception {
if (app == null)
app = Server.getServer().getApplication (appName);
return app.execute (req);
}
String getRequestPath (String path) {
if (path == null)
return "";
// We already get the correct request path
// from the servlet container.
return trim (path);
}
String trim (String str) {
char[] val = str.toCharArray ();
int len = val.length;
int st = 0;
while ((st < len) && (val[st] <= ' ' || val[st] == '/'))
st++;
while ((st < len) && (val[len - 1] <= ' ' || val[len - 1] == '/'))
len--;
return ((st > 0) || (len < val.length)) ? new String (val, st, len-st) : str;
}
}

View file

@ -40,10 +40,21 @@ public class MultiServletClient extends AbstractServletClient {
}
}
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
String appId = getAppID (reqPath);
ResponseTrans execute (RequestTrans req) throws Exception {
// the app-id is the first element in the request path
// so we have to first get than and than rewrite req.path.
int slash = req.path.indexOf ("/");
String appId = null;
if (slash == -1) {
// no slash found, path equals app-id
appId = req.path;
req.path = "";
} else {
// cut path into app id and rewritten path
appId = req.path.substring (0, slash);
req.path = req.path.substring (slash+1);
}
IRemoteApp app = getApp (appId);
req.path = getRequestPath (reqPath);
try {
return app.execute (req);
} catch (Exception x) {
@ -66,59 +77,6 @@ public class MultiServletClient extends AbstractServletClient {
apps.remove (appId);
}
String getAppID (String path) {
if (path == null)
throw new RuntimeException ("Invalid request path: "+path);
char[] val = path.toCharArray ();
int len = val.length;
int st = 0;
// advance to start of path
while ((st < len) && (val[st] <= ' ' || val[st] == '/'))
st++;
// eat characters of first path element
int end = st;
while (end < len && val[end] != '/' && val[end] > 20)
end++;
return new String (val, st, end -st);
}
String getRequestPath (String path) {
if (path == null)
return "";
char[] val = path.toCharArray ();
int len = val.length;
int st = 0;
// advance to start of path, eating up any slashes
while ((st < len) && (val[st] <= ' ' || val[st] == '/'))
st++;
// advance until slash ending the first path element
while (st < len && val[st] != '/')
st++;
if (st < len && val[st] == '/')
st++;
// eat away spaces and slashes at end of path
while ((st < len) && (val[len - 1] <= ' ' || val[len - 1] == '/'))
len--;
return ((st > 0) || (len < val.length)) ? new String (val, st, len-st) : path;
}
// for testing
public static void main (String args[]) {
MultiServletClient client = new MultiServletClient ();
// String path = "///appname/do/it/for/me///";
String path = "appname";
System.out.println (client.getAppID (path));
System.out.println (client.getRequestPath (path));
}
}

View file

@ -41,8 +41,7 @@ public class ServletClient extends AbstractServletClient {
}
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
req.path = getRequestPath (reqPath);
ResponseTrans execute (RequestTrans req) throws Exception {
if (app == null)
initApp ();
try {
@ -58,37 +57,6 @@ public class ServletClient extends AbstractServletClient {
}
String getRequestPath (String path) {
// get request path
if (path != null)
return trim (path);
else
return "";
}
String trim (String str) {
char[] val = str.toCharArray ();
int len = val.length;
int st = 0;
while ((st < len) && (val[st] <= ' ' || val[st] == '/'))
st++;
while ((st < len) && (val[len - 1] <= ' ' || val[len - 1] == '/'))
len--;
return ((st > 0) || (len < val.length)) ? new String (val, st, len-st) : str;
}
// for testing
public static void main (String args[]) {
ServletClient client = new ServletClient ();
String path = "///appname/some/random/path///";
// System.out.println (client.getAppID (path));
System.out.println (client.getRequestPath (path));
}
}

View file

@ -47,8 +47,7 @@ public final class StandaloneServletClient extends AbstractServletClient {
throw new ServletException ("dbdir parameter not specified");
}
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
req.path = getRequestPath (reqPath);
ResponseTrans execute (RequestTrans req) throws Exception {
if (app == null)
createApp ();
return app.execute (req);
@ -91,41 +90,6 @@ public final class StandaloneServletClient extends AbstractServletClient {
app = null;
}
String getAppID (String path) {
return appName;
}
String getRequestPath (String path) {
// get request path
if (path != null)
return trim (path);
else
return "";
}
String trim (String str) {
char[] val = str.toCharArray ();
int len = val.length;
int st = 0;
while ((st < len) && (val[st] <= ' ' || val[st] == '/'))
st++;
while ((st < len) && (val[len - 1] <= ' ' || val[len - 1] == '/'))
len--;
return ((st > 0) || (len < val.length)) ? new String (val, st, len-st) : str;
}
// for testing
public static void main (String args[]) {
StandaloneServletClient client = new StandaloneServletClient ();
String path = "///appname/some/random/path///";
System.out.println (client.getAppID (path));
System.out.println (client.getRequestPath (path));
}
}