Author: toad
Date: 2006-12-12 13:14:23 +0000 (Tue, 12 Dec 2006)
New Revision: 11355
Modified:
trunk/freenet/src/freenet/clients/http/FProxyToadlet.java
trunk/freenet/src/freenet/clients/http/HTTPRequest.java
trunk/freenet/src/freenet/config/StringArrOption.java
trunk/freenet/src/freenet/keys/FreenetURI.java
trunk/freenet/src/freenet/support/URLDecoder.java
Log:
Be tolerant of %'s in URIs, if possible.
They really shouldn't be there...
Modified: trunk/freenet/src/freenet/clients/http/FProxyToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/FProxyToadlet.java 2006-12-12
03:12:15 UTC (rev 11354)
+++ trunk/freenet/src/freenet/clients/http/FProxyToadlet.java 2006-12-12
13:14:23 UTC (rev 11355)
@@ -264,7 +264,8 @@
try {
newURI = new FreenetURI(k);
} catch (MalformedURLException e) {
- sendErrorPage(ctx, 404, "Not found",
"Invalid key");
+ Logger.normal(this, "Invalid key: "+e+"
for "+k, e);
+ sendErrorPage(ctx, 404, "Not found",
"Invalid key: "+e);
return;
}
@@ -318,7 +319,7 @@
HTMLNode contentNode =
ctx.getPageMaker().getContentNode(pageNode);
HTMLNode errorInfobox = contentNode.addChild("div",
"class", "infobox infobox-error");
- errorInfobox.addChild("div", "class", "infobox-header",
"Invalid key");
+ errorInfobox.addChild("div", "class", "infobox-header",
"Invalid key: "+e);
HTMLNode errorContent = errorInfobox.addChild("div",
"class", "infobox-content");
errorContent.addChild("#", "Expected a freenet key, but
got ");
errorContent.addChild("code", ks);
Modified: trunk/freenet/src/freenet/clients/http/HTTPRequest.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/HTTPRequest.java 2006-12-12
03:12:15 UTC (rev 11354)
+++ trunk/freenet/src/freenet/clients/http/HTTPRequest.java 2006-12-12
13:14:23 UTC (rev 11355)
@@ -195,8 +195,8 @@
// url-decode the name and value
if (doUrlDecoding) {
try {
- name =
java.net.URLDecoder.decode(name, "UTF-8");
- value =
java.net.URLDecoder.decode(value, "UTF-8");
+ name = URLDecoder.decode(name,
"UTF-8");
+ value =
URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException
e) {
throw new Error(e);
}
Modified: trunk/freenet/src/freenet/config/StringArrOption.java
===================================================================
--- trunk/freenet/src/freenet/config/StringArrOption.java 2006-12-12
03:12:15 UTC (rev 11354)
+++ trunk/freenet/src/freenet/config/StringArrOption.java 2006-12-12
13:14:23 UTC (rev 11355)
@@ -64,7 +64,7 @@
public static String decode(String s) {
try {
- return URLDecoder.decode(s);
+ return URLDecoder.decode(s, false);
} catch (URLEncodedFormatException e) {
return null;
}
Modified: trunk/freenet/src/freenet/keys/FreenetURI.java
===================================================================
--- trunk/freenet/src/freenet/keys/FreenetURI.java 2006-12-12 03:12:15 UTC
(rev 11354)
+++ trunk/freenet/src/freenet/keys/FreenetURI.java 2006-12-12 13:14:23 UTC
(rev 11355)
@@ -12,7 +12,6 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
-import java.net.URLDecoder;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.StringTokenizer;
@@ -23,6 +22,8 @@
import freenet.support.HexUtil;
import freenet.support.IllegalBase64Exception;
import freenet.support.Logger;
+import freenet.support.URLDecoder;
+import freenet.support.URLEncodedFormatException;
import freenet.support.URLEncoder;
import freenet.client.InserterException;
@@ -222,9 +223,9 @@
if(URI.indexOf('@') < 0 || URI.indexOf('/') < 0) {
// Encoded URL?
try {
- URI=URLDecoder.decode(URI, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new Error(e);
+ URI=URLDecoder.decode(URI, false);
+ } catch (URLEncodedFormatException e) {
+ throw new MalformedURLException("Invalid URI:
no @ or /, or @ or / is escaped but there are invalid escapes");
}
}
@@ -251,9 +252,11 @@
while ((slash2 = URI.lastIndexOf("/")) != -1) {
String s;
try {
- s = URLDecoder.decode(URI.substring(slash2 +
"/".length()), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new Error(e);
+ s = URLDecoder.decode(URI.substring(slash2 +
"/".length()), true);
+ } catch (URLEncodedFormatException e) {
+ MalformedURLException ue = new
MalformedURLException(e.toString());
+ ue.initCause(e);
+ throw ue;
}
if (s != null)
sv.addElement(s);
Modified: trunk/freenet/src/freenet/support/URLDecoder.java
===================================================================
--- trunk/freenet/src/freenet/support/URLDecoder.java 2006-12-12 03:12:15 UTC
(rev 11354)
+++ trunk/freenet/src/freenet/support/URLDecoder.java 2006-12-12 13:14:23 UTC
(rev 11355)
@@ -28,22 +28,26 @@
// test harness
public static void main(String[] args) throws URLEncodedFormatException {
for (int i = 0; i < args.length; i++) {
- System.out.println(args[i] + " -> " + decode(args[i]));
+ System.out.println(args[i] + " -> " + decode(args[i], false));
}
}
/**
- * Translates a string out of x-www-form-urlencoded format.
+ * Decodes a URLEncoder format string.
*
* @param s String to be translated.
+ * @param tolerant If true, be tolerant of bogus escapes; bogus escapes
are treated as
+ * just plain characters. Not recommended; a hack to allow users to
paste in URLs
+ * containing %'s.
* @return the translated String.
*
**/
- public static String decode(String s) throws URLEncodedFormatException {
+ public static String decode(String s, boolean tolerant) throws
URLEncodedFormatException {
if (s.length() == 0)
return "";
int len = s.length();
ByteArrayOutputStream decodedBytes = new
ByteArrayOutputStream();
+ boolean hasDecodedSomething = false;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
@@ -64,8 +68,20 @@
if (read == 0)
throw new
URLEncodedFormatException("Can't encode" + " 00");
decodedBytes.write((int) read);
+ hasDecodedSomething = true;
} catch (NumberFormatException nfe) {
- throw new URLEncodedFormatException(s);
+ // Not encoded?
+ if(tolerant && !hasDecodedSomething) {
+ try {
+ byte[] buf =
('%'+hexval).getBytes("UTF-8");
+ decodedBytes.write(buf,
0, buf.length);
+ continue;
+ } catch
(UnsupportedEncodingException e) {
+ throw new Error(e);
+ }
+ }
+
+ throw new
URLEncodedFormatException("Not a two character hex % escape: "+hexval+" in "+s);
}
} else {
try {