[freenet-cvs] r11166 - trunk/freenet/src/freenet/clients/http

toad at freenetproject.org toad at freenetproject.org
Fri Dec 1 02:06:14 UTC 2006


Author: toad
Date: 2006-12-01 02:06:10 +0000 (Fri, 01 Dec 2006)
New Revision: 11166

Modified:
   trunk/freenet/src/freenet/clients/http/FProxyToadlet.java
   trunk/freenet/src/freenet/clients/http/QueueToadlet.java
Log:
Add a "go back to the referer" link.

Modified: trunk/freenet/src/freenet/clients/http/FProxyToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/FProxyToadlet.java	2006-12-01 01:36:18 UTC (rev 11165)
+++ trunk/freenet/src/freenet/clients/http/FProxyToadlet.java	2006-12-01 02:06:10 UTC (rev 11166)
@@ -83,7 +83,7 @@
 		
 	}
 
-	public static void handleDownload(ToadletContext context, Bucket data, BucketFactory bucketFactory, String mimeType, String requestedMimeType, String forceString, boolean forceDownload, String basePath, FreenetURI key, String extras) throws ToadletContextClosedException, IOException {
+	public static void handleDownload(ToadletContext context, Bucket data, BucketFactory bucketFactory, String mimeType, String requestedMimeType, String forceString, boolean forceDownload, String basePath, FreenetURI key, String extras, String referrer) throws ToadletContextClosedException, IOException {
 		if(requestedMimeType != null)
 			mimeType = requestedMimeType;
 		
@@ -130,6 +130,11 @@
 					option = optionList.addChild("li");
 					option.addChild("a", "href", basePath + key.toString(false) + "?type=application/xml+rss&force=" + getForceValue(key, now)+extras, "Click here");
 					option.addChild("%", " to open the file as RSS (<b>this is dangerous if the site author is malicious</b>).");
+					if(referrer != null) {
+						option = optionList.addChild("li");
+						option.addChild("a", "href", referrer, "Click here");
+						option.addChild("#", " to go back to the referring page.");
+					}
 					option = optionList.addChild("li");
 					option.addChild("a", "href", "/", "Click here");
 					option.addChild("#", " to go to the FProxy home page.");
@@ -175,6 +180,11 @@
 			option = optionList.addChild("li");
 			option.addChild("a", "href", basePath + key.toString(false) + "?force=" + getForceValue(key, now)+extras, "Click here");
 			option.addChild("#", " to open the file as " + mimeType + '.');
+			if(referrer != null) {
+				option = optionList.addChild("li");
+				option.addChild("a", "href", referrer, "Click here");
+				option.addChild("#", " to go back to the referring page.");
+			}
 			option = optionList.addChild("li");
 			option.addChild("a", "href", "/", "Click here");
 			option.addChild("#", " to go to the FProxy home page.");
@@ -326,8 +336,10 @@
 			Bucket data = result.asBucket();
 			String mimeType = result.getMimeType();
 			
-			handleDownload(ctx, data, ctx.getBucketFactory(), mimeType, requestedMimeType, httprequest.getParam("force", null), httprequest.isParameterSet("forcedownload"), "/", key, maxSize != MAX_LENGTH ? "&max-size="+maxSize : "");
+			String referer = sanitizeReferer(ctx);
 			
+			handleDownload(ctx, data, ctx.getBucketFactory(), mimeType, requestedMimeType, httprequest.getParam("force", null), httprequest.isParameterSet("forcedownload"), "/", key, maxSize != MAX_LENGTH ? "&max-size="+maxSize : "", referer);
+			
 		} catch (FetchException e) {
 			String msg = e.getMessage();
 			String extra = "";
@@ -424,6 +436,28 @@
 		}
 	}
 
+	private String sanitizeReferer(ToadletContext ctx) {
+		// FIXME we do something similar in the GenericFilterCallback thingy?
+		String referer = (String) ctx.getHeaders().get("referer");
+		if(referer != null) {
+			try {
+				URI refererURI = new URI(referer);
+				String path = refererURI.getPath();
+				while(path.startsWith("/")) path = path.substring(1);
+				FreenetURI furi = new FreenetURI(path);
+				HTTPRequest req = new HTTPRequest(refererURI);
+				String type = req.getParam("type");
+				referer = "/" + furi.toString(false);
+				if(type != null && type.length() > 0)
+					referer += "?type=" + type;
+			} catch (Throwable t) {
+				Logger.error(this, "Caught handling referrer: "+t+" for "+referer, t);
+				referer = null;
+			}
+		}
+		return referer;
+	}
+
 	private static String getForceValue(FreenetURI key, long time) {
 		MessageDigest md = SHA256.getMessageDigest();
 		ByteArrayOutputStream bos = new ByteArrayOutputStream();

Modified: trunk/freenet/src/freenet/clients/http/QueueToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/QueueToadlet.java	2006-12-01 01:36:18 UTC (rev 11165)
+++ trunk/freenet/src/freenet/clients/http/QueueToadlet.java	2006-12-01 02:06:10 UTC (rev 11166)
@@ -310,7 +310,7 @@
 							String mimeType = clientGet.getMIMEType();
 							String requestedMimeType = request.getParam("type", null);
 							String forceString = request.getParam("force");
-							FProxyToadlet.handleDownload(ctx, data, ctx.getBucketFactory(), mimeType, requestedMimeType, forceString, request.isParameterSet("forcedownload"), "/queue/", key, "");
+							FProxyToadlet.handleDownload(ctx, data, ctx.getBucketFactory(), mimeType, requestedMimeType, forceString, request.isParameterSet("forcedownload"), "/queue/", key, "", "/queue/");
 							return;
 						}
 					}




More information about the cvs mailing list