Author: nacktschneck
Date: 2006-03-08 22:41:02 +0000 (Wed, 08 Mar 2006)
New Revision: 8198
Modified:
trunk/freenet/src/freenet/pluginmanager/HTTPRequest.java
trunk/freenet/src/freenet/pluginmanager/PproxyToadlet.java
Log:
Fixed 'issues' with PproxyToadlet request parameter handling
- works again ;)
- no longer handles _all_ requests that have either ?remove=, ?load= or
?reload= or set
Modified: trunk/freenet/src/freenet/pluginmanager/HTTPRequest.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/HTTPRequest.java 2006-03-08
21:17:42 UTC (rev 8197)
+++ trunk/freenet/src/freenet/pluginmanager/HTTPRequest.java 2006-03-08
22:41:02 UTC (rev 8198)
@@ -1,6 +1,5 @@
package freenet.pluginmanager;
-import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
@@ -12,7 +11,6 @@
import freenet.support.URLDecoder;
import freenet.support.URLEncodedFormatException;
-import freenet.support.URLEncoder;
/**
* Used for passing all HTTP request information to the FredPlugin that handles
@@ -76,6 +74,14 @@
/**
+ *
+ * @return true if the query string was totally empty
+ */
+ public boolean hasParameters() {
+ return ! this.parameterNameValuesMap.isEmpty();
+ }
+
+ /**
* Parse the query string and populate {@link #parameterNameValuesMap}
with
* the lists of values for each parameter. If this method is not called
at
* all, all other methods would be useless. Because they rely on the
@@ -311,6 +317,7 @@
return values;
}
+
// TODO: add similar methods for multiple long, boolean etc.
}
Modified: trunk/freenet/src/freenet/pluginmanager/PproxyToadlet.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/PproxyToadlet.java 2006-03-08
21:17:42 UTC (rev 8197)
+++ trunk/freenet/src/freenet/pluginmanager/PproxyToadlet.java 2006-03-08
22:41:02 UTC (rev 8198)
@@ -7,18 +7,11 @@
import java.util.Date;
import java.util.Iterator;
-import freenet.client.FetchException;
-import freenet.client.FetchResult;
import freenet.client.HighLevelSimpleClient;
import freenet.clients.http.Toadlet;
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContextClosedException;
-import freenet.clients.http.PageMaker;
-import freenet.keys.FreenetURI;
-import freenet.support.Bucket;
import freenet.support.Logger;
-import freenet.support.URLDecoder;
-import freenet.support.URLEncodedFormatException;
import freenet.support.MultiValueTable;
public class PproxyToadlet extends Toadlet {
@@ -44,114 +37,35 @@
public void handleGet(URI uri, ToadletContext ctx)
throws ToadletContextClosedException, IOException {
//String basepath = "/plugins/";
- String ks = uri.getPath();
+ HTTPRequest request = new HTTPRequest(uri);
+ String path = request.getPath();
-
- if(ks.startsWith("/"))
- ks = ks.substring(1);
- ks = ks.substring("plugins/".length());
- Logger.minor(this, "Pproxy fetching "+ks);
+ // remove leading / and 'plugins/' from path
+ if(path.startsWith("/"))
+ path = path.substring(1);
+ path = path.substring("plugins/".length());
+ Logger.minor(this, "Pproxy fetching "+path);
try {
- if (ks.equals("")) {
- StringBuffer out = new StringBuffer();
- ctx.getPageMaker().makeHead(out, "Plugin List");
- out.append("<table style=\"border: 1pt solid
#c0c0c0;\">");
- out.append(" <tr>\n");
- out.append(" <th>Name</th>\n");
- out.append(" <th>ID</th>\n");
- out.append(" <th>Started</th>\n");
- out.append(" <th></th>\n");
- out.append(" </tr>\n");
- Iterator it = pm.getPlugins().iterator();
- while (it.hasNext()) {
- PluginInfoWrapper pi =
(PluginInfoWrapper) it.next();
- out.append(" <tr>\n");
- out.append(" <td style=\"border: 1pt
solid #c0c0c0;\">" + pi.getPluginClassName() + "</td>\n");
- out.append(" <td style=\"border: 1pt
solid #c0c0c0;\">" + pi.getThreadName() + "</td>\n");
- out.append(" <td style=\"border: 1pt
solid #c0c0c0;\">" + (new Date(pi.getStarted())) + "</td>\n");
- out.append(" <td style=\"border: 1pt
solid #c0c0c0;\">");
- if (pi.isPproxyPlugin())
- out.append(" <a
href=\""+pi.getPluginClassName()+"/\">[VISIT]</a> ");
- out.append(" <a
href=\"?remove="+pi.getThreadName()+"\">[UNLOAD]</a> ");
- out.append(" <a
href=\"?reload="+pi.getThreadName()+"\">[RELOAD]</a> ");
- out.append("</td>\n");
- out.append(" </tr>\n");
- }
-
- if (pm.getPlugins().isEmpty()) {
- out.append("<tr>\n");
- out.append("<td colspan=\"4\"\n");
- out.append("<i>No plugins
loaded</i>\n");
- out.append("</td>\n");
- out.append("</tr>\n");
- }
-
- out.append("</table>");
- //String ret = "<hr/>" + out.toString();
- //ret = pm.dumpPlugins().replaceAll(",",
"\n ").replaceAll("\"", " \" ");
- /*if (ret.length() < 6)
- ret += "<i>No plugins loaded</i>\n";
- ret += "<hr/>";*/
-
-
- // Obsolete
- //out.append("<form method=\"get\"><div>Remove
plugin: (enter ID) <input type=\"text\" name=\"remove\" size=40/><input
type=\"submit\" value=\"Remove\"/></div></form>\n");
- out.append("<form method=\"get\"
action=\".\"><div>Load plugin: <input type=\"text\" name=\"load\"
size=\"40\"/><input type=\"submit\" value=\"Load\" /></div></form>\n");
- ctx.getPageMaker().makeTail(out);
- writeReply(ctx, 200, "text/html", "OK",
out.toString());
- } else if (ks.startsWith("?remove=")) {
-
pm.killPlugin(ks.substring("?remove=".length()));
-
- MultiValueTable headers = new MultiValueTable();
-
- headers.put("Location", ".");
- ctx.sendReplyHeaders(302, "Found", headers,
null, 0);
- //writeReply(ctx, 200, "text/html", "OK",
mkForwardPage("Removing plugin", "Removing plugin...", ".", 5));
- } else if (ks.startsWith("?load=")) {
- pm.startPlugin(ks.substring("?load=".length()));
- //writeReply(ctx, 200, "text/html", "OK",
mkForwardPage("Loading plugin", "Loading plugin...", ".", 5));
- MultiValueTable headers = new MultiValueTable();
-
- headers.put("Location", ".");
- ctx.sendReplyHeaders(302, "Found", headers,
null, 0);
- } else if (ks.startsWith("?reload=")) {
- String fn = null;
- Iterator it = pm.getPlugins().iterator();
- while (it.hasNext()) {
- PluginInfoWrapper pi =
(PluginInfoWrapper) it.next();
- if
(pi.getThreadName().equals(ks.substring("?reload=".length()))) {
- fn = pi.getFilename();
- break;
- }
- }
-
- if (fn == null) {
- writeReply(ctx, 200, "text/html", "OK",
mkForwardPage("Error", "Plugin not found...", ".", 5));
- } else {
-
pm.killPlugin(ks.substring("?reload=".length()));
- pm.startPlugin(fn);
-
- MultiValueTable headers = new
MultiValueTable();
- headers.put("Location", ".");
- ctx.sendReplyHeaders(302, "Found",
headers, null, 0);
- }
+ if (path.equals("")) {
+ this.showPluginList(ctx, request);
} else {
- int to = ks.indexOf("/");
+ // split path into plugin class name and 'data'
path for plugin
+ int to = path.indexOf("/");
String plugin, data;
if (to == -1) {
- plugin = ks;
+ plugin = path;
data = "";
} else {
- plugin = ks.substring(0, to);
- data = ks.substring(to + 1);
+ plugin = path.substring(0, to);
+ data = path.substring(to + 1);
}
//pm.handleHTTPGet(plugin, data);
- HTTPRequest request = new HTTPRequest(data,
uri.getRawQuery());
+ // create a new request with the 'data' path
and pass it to the plugin
+ request = new HTTPRequest(data,
uri.getRawQuery());
//writeReply(ctx, 200, "text/html", "OK",
mkPage("plugin", pm.handleHTTPGet(plugin, data)));
writeReply(ctx, 200, "text/html", "OK",
pm.handleHTTPGet(plugin, request));
-
}
@@ -173,4 +87,91 @@
this.writeReply(ctx, 500, "text/html", "Internal
Error", msg);
}
}
+
+ private void showPluginList(ToadletContext ctx, HTTPRequest request)
throws ToadletContextClosedException, IOException {
+ if (!request.hasParameters()) {
+ StringBuffer out = new StringBuffer();
+ ctx.getPageMaker().makeHead(out, "Plugin List");
+ out.append("<table style=\"border: 1pt solid
#c0c0c0;\">");
+ out.append(" <tr>\n");
+ out.append(" <th>Name</th>\n");
+ out.append(" <th>ID</th>\n");
+ out.append(" <th>Started</th>\n");
+ out.append(" <th></th>\n");
+ out.append(" </tr>\n");
+ Iterator it = pm.getPlugins().iterator();
+ while (it.hasNext()) {
+ PluginInfoWrapper pi = (PluginInfoWrapper)
it.next();
+ out.append(" <tr>\n");
+ out.append(" <td style=\"border: 1pt solid
#c0c0c0;\">" + pi.getPluginClassName() + "</td>\n");
+ out.append(" <td style=\"border: 1pt solid
#c0c0c0;\">" + pi.getThreadName() + "</td>\n");
+ out.append(" <td style=\"border: 1pt solid
#c0c0c0;\">" + (new Date(pi.getStarted())) + "</td>\n");
+ out.append(" <td style=\"border: 1pt solid
#c0c0c0;\">");
+ if (pi.isPproxyPlugin())
+ out.append(" <a
href=\""+pi.getPluginClassName()+"/\">[VISIT]</a> ");
+ out.append(" <a
href=\"?remove="+pi.getThreadName()+"\">[UNLOAD]</a> ");
+ out.append(" <a
href=\"?reload="+pi.getThreadName()+"\">[RELOAD]</a> ");
+ out.append("</td>\n");
+ out.append(" </tr>\n");
+ }
+
+ if (pm.getPlugins().isEmpty()) {
+ out.append("<tr>\n");
+ out.append("<td colspan=\"4\"\n");
+ out.append("<i>No plugins loaded</i>\n");
+ out.append("</td>\n");
+ out.append("</tr>\n");
+ }
+
+ out.append("</table>");
+ //String ret = "<hr/>" + out.toString();
+ //ret = pm.dumpPlugins().replaceAll(",", "\n
").replaceAll("\"", " \" ");
+ /*if (ret.length() < 6)
+ ret += "<i>No plugins loaded</i>\n";
+ ret += "<hr/>";*/
+
+
+ // Obsolete
+ //out.append("<form method=\"get\"><div>Remove plugin:
(enter ID) <input type=\"text\" name=\"remove\" size=40/><input type=\"submit\"
value=\"Remove\"/></div></form>\n");
+ out.append("<form method=\"get\" action=\".\"><div>Load
plugin: <input type=\"text\" name=\"load\" size=\"40\"/><input type=\"submit\"
value=\"Load\" /></div></form>\n");
+ ctx.getPageMaker().makeTail(out);
+ writeReply(ctx, 200, "text/html", "OK", out.toString());
+ } else if (request.isParameterSet("remove")) {
+ pm.killPlugin(request.getParam("remove"));
+
+ MultiValueTable headers = new MultiValueTable();
+
+ headers.put("Location", ".");
+ ctx.sendReplyHeaders(302, "Found", headers, null, 0);
+ //writeReply(ctx, 200, "text/html", "OK",
mkForwardPage("Removing plugin", "Removing plugin...", ".", 5));
+ } else if (request.isParameterSet("load")) {
+ pm.startPlugin(request.getParam("load"));
+ //writeReply(ctx, 200, "text/html", "OK",
mkForwardPage("Loading plugin", "Loading plugin...", ".", 5));
+ MultiValueTable headers = new MultiValueTable();
+
+ headers.put("Location", ".");
+ ctx.sendReplyHeaders(302, "Found", headers, null, 0);
+ } else if (request.isParameterSet("reload")) {
+ String fn = null;
+ Iterator it = pm.getPlugins().iterator();
+ while (it.hasNext()) {
+ PluginInfoWrapper pi = (PluginInfoWrapper)
it.next();
+ if
(pi.getThreadName().equals(request.getParam("reload"))) {
+ fn = pi.getFilename();
+ break;
+ }
+ }
+
+ if (fn == null) {
+ writeReply(ctx, 200, "text/html", "OK",
mkForwardPage("Error", "Plugin not found...", ".", 5));
+ } else {
+ pm.killPlugin(request.getParam("reload"));
+ pm.startPlugin(fn);
+
+ MultiValueTable headers = new MultiValueTable();
+ headers.put("Location", ".");
+ ctx.sendReplyHeaders(302, "Found", headers,
null, 0);
+ }
+ }
+ }
}