From dc855d881b7e2c186bf5811d236f756f3f71b4b4 Mon Sep 17 00:00:00 2001 From: f1ori Date: Mon, 30 May 2011 21:25:20 +0000 Subject: [PATCH] * further improve proxyurl git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7762 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- .../anomic/http/server/HTTPDFileHandler.java | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/source/de/anomic/http/server/HTTPDFileHandler.java b/source/de/anomic/http/server/HTTPDFileHandler.java index bfcbf34f8..31a04a3f3 100644 --- a/source/de/anomic/http/server/HTTPDFileHandler.java +++ b/source/de/anomic/http/server/HTTPDFileHandler.java @@ -1297,16 +1297,24 @@ public final class HTTPDFileHandler { private static void doURLProxy(final serverObjects args, final HashMap conProp, final RequestHeader requestHeader, OutputStream out) throws IOException { final String httpVersion = (String) conProp.get(HeaderFramework.CONNECTION_PROP_HTTP_VER); URL proxyurl = null; - - if (args != null && args.containsKey("url")) { - String strUrl = args.get("url"); - proxyurl = new URL(strUrl); + + if(conProp != null && conProp.containsKey("ARGS")) { + String strARGS = (String) conProp.get("ARGS"); + if(strARGS.startsWith("url=")) { + String strUrl = strARGS.substring(4); // strip url= + proxyurl = new URL(strUrl); + } } + + if (proxyurl==null) { + throw new IOException("no url as argument supplied"); + } + // set properties for proxy connection final HashMap prop = new HashMap(); prop.put(HeaderFramework.CONNECTION_PROP_HTTP_VER, HeaderFramework.HTTP_VERSION_1_1); prop.put(HeaderFramework.CONNECTION_PROP_HOST, proxyurl.getHost()); - prop.put(HeaderFramework.CONNECTION_PROP_PATH, proxyurl.getPath().replaceAll(" ", "%20")); + prop.put(HeaderFramework.CONNECTION_PROP_PATH, proxyurl.getFile().replaceAll(" ", "%20")); prop.put(HeaderFramework.CONNECTION_PROP_REQUESTLINE, "PROXY"); prop.put("CLIENTIP", "0:0:0:0:0:0:0:1"); @@ -1338,9 +1346,25 @@ public final class HTTPDFileHandler { return; } - final int httpStatus = Integer.parseInt((String) prop.get(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_STATUS)); + final int httpStatus = Integer.parseInt((String) prop.get(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_STATUS)); + + String directory = ""; + if (proxyurl.getPath().lastIndexOf('/') > 0) + directory = proxyurl.getPath().substring(0, proxyurl.getPath().lastIndexOf('/')); + + if (outgoingHeader.containsKey("Location")) { + // rewrite location header + String location = outgoingHeader.get("Location"); + if (location.startsWith("http")) { + location = "/proxy.html?url=" + location; + } else { + location = "/proxy.html?url=http://" + proxyurl.getHost() + "/" + location; + } + outgoingHeader.put("Location", location); + } - if (outgoingHeader.getContentType().startsWith("text/html")) { + String mimeType = outgoingHeader.getContentType(); + if (mimeType.startsWith("text/html") || mimeType.startsWith("text")) { StringWriter buffer = new StringWriter(); if (outgoingHeader.containsKey(HeaderFramework.TRANSFER_ENCODING)) { @@ -1351,19 +1375,21 @@ public final class HTTPDFileHandler { String sbuffer = buffer.toString(); - String directory = ""; - if (proxyurl.getPath().lastIndexOf('/') > 0) - directory = proxyurl.getPath().substring(0, proxyurl.getPath().lastIndexOf('/')); // urls of form href="http://domain.com/path" sbuffer = sbuffer.replaceAll("(href|src)=\"http([^\"]+)\"", "$1=\"/proxy.html?url=http$2\""); sbuffer = sbuffer.replaceAll("(href|src)='http([^']+)'", "$1='/proxy.html?url=http$2'"); + sbuffer = sbuffer.replaceAll("url\\('http([^']+)'\\)", "url('/proxy.html?url=http$1')"); + sbuffer = sbuffer.replaceAll("url\\(http([^\\)]+)\\)'", "url(/proxy.html?url=http$1)"); // urls of form href="/absolute/path/to/linked/page" sbuffer = sbuffer.replaceAll("(href|src)=\"/([^:\"]+)\"", "$1=\"/proxy.html?url=http://"+proxyurl.getHost()+"/$2\""); sbuffer = sbuffer.replaceAll("(href|src)='/([^:']+)'", "$1='/proxy.html?url=http://"+proxyurl.getHost()+"/$2'"); + sbuffer = sbuffer.replaceAll("url\\('/([^:']+)'\\)", "url('/proxy.html?url=http://"+proxyurl.getHost()+"/$1')"); + sbuffer = sbuffer.replaceAll("url\\(/([^:\\)]+)\\)", "url(/proxy.html?url=http://"+proxyurl.getHost()+"/$1)"); // urls of form href="relative/path" sbuffer = sbuffer.replaceAll("(href|src)=\"([^:\"]+)\"", "$1=\"/proxy.html?url=http://"+proxyurl.getHost()+directory+"/$2\""); sbuffer = sbuffer.replaceAll("(href|src)='([^:']+)'", "$1='/proxy.html?url=http://"+proxyurl.getHost()+directory+"/$2'"); - sbuffer = sbuffer.replaceAll("url\\(", "url(/proxy.html?url=http://"+proxyurl.getHost()+proxyurl.getPath()); + sbuffer = sbuffer.replaceAll("url\\('([^:']+)'\\)", "url\\('/proxy.html?url=http://"+proxyurl.getHost()+directory+"/$1')"); + sbuffer = sbuffer.replaceAll("url\\(([^:\\)]+)\\)", "url\\(/proxy.html?url=http://"+proxyurl.getHost()+directory+"/$1)"); byte[] sbb = UTF8.getBytes(sbuffer);