From 6b32f7c1f603286907df57e33cdb34207dc284d3 Mon Sep 17 00:00:00 2001 From: cominch Date: Wed, 16 May 2012 15:05:17 +0200 Subject: [PATCH] re-enable augmented proxy --- .../anomic/http/server/HTTPDProxyHandler.java | 38 +++++++++++++++++-- .../yacy/interaction/AugmentHtmlStream.java | 7 ++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/source/de/anomic/http/server/HTTPDProxyHandler.java b/source/de/anomic/http/server/HTTPDProxyHandler.java index 5e2bc46c7..c0bb0768d 100644 --- a/source/de/anomic/http/server/HTTPDProxyHandler.java +++ b/source/de/anomic/http/server/HTTPDProxyHandler.java @@ -467,6 +467,14 @@ public final class HTTPDProxyHandler { port = sb.peers.myPort(); path = path.substring(16); } + + // point virtual directory to my peer + if (path.startsWith("/currentyacypeer/")) { + host = sb.peers.myIP(); + port = sb.peers.myPort(); + path = path.substring(16); + } + // resolve yacy and yacyh domains String yAddress = resolveYacyDomains(host); @@ -484,6 +492,8 @@ public final class HTTPDProxyHandler { final String connectHost = hostPart(host, port, yAddress); final String getUrl = "http://"+ connectHost + remotePath; + + requestHeader.remove(HeaderFramework.HOST); final HTTPClient client = setupHttpClient(requestHeader, connectHost); @@ -498,8 +508,15 @@ public final class HTTPDProxyHandler { if (responseHeader.isEmpty()) { throw new Exception(client.getHttpResponse().getStatusLine().toString()); } + + if(AugmentedHtmlStream.supportsMime(responseHeader.mime())) { + // enable chunk encoding, because we don't know the length after annotating + responseHeader.remove(HeaderFramework.CONTENT_LENGTH); + responseHeader.put(HeaderFramework.TRANSFER_ENCODING, "chunked"); + + } - final ChunkedOutputStream chunkedOut = setTransferEncoding(conProp, responseHeader, client.getHttpResponse().getStatusLine().getStatusCode(), respond); + ChunkedOutputStream chunkedOut = setTransferEncoding(conProp, responseHeader, client.getHttpResponse().getStatusLine().getStatusCode(), respond); // the cache does either not exist or is (supposed to be) stale long sizeBeforeDelete = -1; @@ -534,6 +551,11 @@ public final class HTTPDProxyHandler { // prepareResponseHeader(responseHeader, res.getHttpVer()); prepareResponseHeader(responseHeader, client.getHttpResponse().getProtocolVersion().toString()); + if(AugmentedHtmlStream.supportsMime(responseHeader.mime())) { + // chunked encoding disables somewhere, add it again + responseHeader.put(HeaderFramework.TRANSFER_ENCODING, "chunked"); + } + // sending the respond header back to the client if (chunkedOut != null) { responseHeader.put(HeaderFramework.TRANSFER_ENCODING, "chunked"); @@ -550,7 +572,7 @@ public final class HTTPDProxyHandler { if (hasBody(client.getHttpResponse().getStatusLine().getStatusCode())) { - final OutputStream outStream = chunkedOut != null ? chunkedOut : respond; + OutputStream outStream = chunkedOut != null ? chunkedOut : respond; final Response response = new Response( request, requestHeader, @@ -562,6 +584,10 @@ public final class HTTPDProxyHandler { final String storeError = response.shallStoreCacheForProxy(); final boolean storeHTCache = response.profile().storeHTCache(); final String supportError = TextParser.supports(response.url(), response.getMimeType()); + + if(AugmentedHtmlStream.supportsMime(responseHeader.mime())) { + outStream = new AugmentedHtmlStream(outStream, responseHeader.getCharSet(), url, url.hash(), requestHeader); + } if ( /* * Now we store the response into the htcache directory if @@ -629,6 +655,8 @@ public final class HTTPDProxyHandler { conProp.put(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_CODE,"TCP_MISS"); } + + outStream.close(); if (chunkedOut != null) { chunkedOut.finish(); @@ -679,7 +707,7 @@ public final class HTTPDProxyHandler { final RequestHeader requestHeader, final ResponseHeader cachedResponseHeader, final byte[] cacheEntry, - final OutputStream respond + OutputStream respond ) throws IOException { final String httpVer = (String) conProp.get(HeaderFramework.CONNECTION_PROP_HTTP_VER); @@ -716,6 +744,10 @@ public final class HTTPDProxyHandler { HTTPDemon.sendRespondHeader(conProp,respond,httpVer,203,cachedResponseHeader); //respondHeader(respond, "203 OK", cachedResponseHeader); // respond with 'non-authoritative' + if(AugmentedHtmlStream.supportsMime(cachedResponseHeader.mime())) { + respond = new AugmentedHtmlStream(respond, cachedResponseHeader.getCharSet(), url, url.hash(), requestHeader); + } + // send also the complete body now from the cache // simply read the file and transfer to out socket FileUtils.copy(cacheEntry, respond); diff --git a/source/net/yacy/interaction/AugmentHtmlStream.java b/source/net/yacy/interaction/AugmentHtmlStream.java index 6c5ed48d3..0ebfd0b67 100644 --- a/source/net/yacy/interaction/AugmentHtmlStream.java +++ b/source/net/yacy/interaction/AugmentHtmlStream.java @@ -216,6 +216,13 @@ public class AugmentHtmlStream { Switchboard sb = Switchboard.getSwitchboard(); boolean augmented = false; + + try { + Log.logInfo("AUGMENTATION", url.getName()); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } String Doc = data.toString();