diff --git a/source/net/yacy/http/CrashProtectionHandler.java b/source/net/yacy/http/CrashProtectionHandler.java new file mode 100644 index 000000000..7f944affe --- /dev/null +++ b/source/net/yacy/http/CrashProtectionHandler.java @@ -0,0 +1,52 @@ +package net.yacy.http; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.HandlerContainer; +import org.eclipse.jetty.server.HttpConnection; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.HandlerWrapper; + +public class CrashProtectionHandler extends HandlerWrapper implements Handler, HandlerContainer { + + public CrashProtectionHandler() { + super(); + } + + public CrashProtectionHandler(Handler h) { + super(); + this.setHandler(h); + } + + + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, + HttpServletResponse response) throws IOException, ServletException { + try { + super.handle(target, baseRequest, request, response); + } catch (Exception e) { + // handle all we can + writeResponse(request, response, e); + } + } + + private void writeResponse(HttpServletRequest request, HttpServletResponse response, Exception exc) throws IOException { + PrintWriter out = response.getWriter(); + out.println("Ops!"); + out.println(); + out.println("Message: " + exc.getMessage()); + exc.printStackTrace(out); + response.setContentType("text/plain"); + response.setStatus(500); + + // we handled this request, break out of handler chain + Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); + base_request.setHandled(true); + } +} diff --git a/source/net/yacy/http/HttpServer.java b/source/net/yacy/http/HttpServer.java index c3491ed63..57ec1fee4 100644 --- a/source/net/yacy/http/HttpServer.java +++ b/source/net/yacy/http/HttpServer.java @@ -28,6 +28,7 @@ import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.server.nio.SelectChannelConnector; @@ -70,7 +71,7 @@ public class HttpServer { YaCySecurityHandler securityHandler = new YaCySecurityHandler(); securityHandler.setLoginService(new YaCyLoginService()); securityHandler.setRealmName("YaCy Admin Interface"); - securityHandler.setHandler(handlers); + securityHandler.setHandler(new CrashProtectionHandler(handlers)); // context handler for dispatcher and security ContextHandler context = new ContextHandler(); diff --git a/source/net/yacy/http/ProxyCacheHandler.java b/source/net/yacy/http/ProxyCacheHandler.java index e518d97df..c78a7101b 100644 --- a/source/net/yacy/http/ProxyCacheHandler.java +++ b/source/net/yacy/http/ProxyCacheHandler.java @@ -42,7 +42,6 @@ import org.eclipse.jetty.server.Request; import de.anomic.crawler.retrieval.Response; import de.anomic.http.client.Cache; -import de.anomic.search.Switchboard; /** * jetty http handler @@ -75,9 +74,21 @@ public class ProxyCacheHandler extends AbstractRemoteHandler implements Handler if(cachedResponseHeader != null) { RequestHeader proxyHeaders = ProxyHandler.convertHeaderFromJetty(request); + // TODO: this convertion is only necessary + final de.anomic.crawler.retrieval.Request yacyRequest = new de.anomic.crawler.retrieval.Request( + null, + url, + proxyHeaders.referer() == null ? null : new DigestURI(proxyHeaders.referer()).hash(), + "", + cachedResponseHeader.lastModified(), + sb.crawler.defaultProxyProfile.handle(), + 0, + 0, + 0, + 0); final Response cachedResponse = new Response( - null, + yacyRequest, proxyHeaders, cachedResponseHeader, "200 OK", diff --git a/source/net/yacy/http/ProxyHandler.java b/source/net/yacy/http/ProxyHandler.java index da1f1ee98..e7744b522 100644 --- a/source/net/yacy/http/ProxyHandler.java +++ b/source/net/yacy/http/ProxyHandler.java @@ -116,26 +116,28 @@ public class ProxyHandler extends AbstractRemoteHandler implements Handler { final ResponseHeader responseHeaderLegacy = new ResponseHeader(client.getHttpResponse().getAllHeaders()); cleanResponseHeader(responseHeader); + + // TODO: is this fast, if not, use value from ProxyCacheHandler + DigestURI digestURI = new DigestURI(url); + ResponseHeader cachedResponseHeader = Cache.getResponseHeader(digestURI); - - // TODO: wtf? // the cache does either not exist or is (supposed to be) stale long sizeBeforeDelete = -1; if (cachedResponseHeader != null) { // delete the cache - ResponseHeader rh = Cache.getResponseHeader(new DigestURI(url)); + ResponseHeader rh = Cache.getResponseHeader(digestURI); if (rh != null && (sizeBeforeDelete = rh.getContentLength()) == 0) { - byte[] b = Cache.getContent(url); + byte[] b = Cache.getContent(new DigestURI(url)); if (b != null) sizeBeforeDelete = b.length; } - Cache.delete(url); - conProp.setProperty(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_CODE, "TCP_REFRESH_MISS"); + Cache.delete(digestURI); + // log refresh miss } // reserver cache entry final de.anomic.crawler.retrieval.Request yacyRequest = new de.anomic.crawler.retrieval.Request( null, - new DigestURI(url), + digestURI, null, //requestHeader.referer() == null ? null : new DigestURI(requestHeader.referer()).hash(), "", responseHeaderLegacy.lastModified(), @@ -183,33 +185,34 @@ public class ProxyHandler extends AbstractRemoteHandler implements Handler { } else { cacheArray = null; } - if (log.isFine()) log.logFine(reqID +" writeContent of " + url + " produced cacheArray = " + ((cacheArray == null) ? "null" : ("size=" + cacheArray.length))); + //if (log.isFine()) log.logFine(reqID +" writeContent of " + url + " produced cacheArray = " + ((cacheArray == null) ? "null" : ("size=" + cacheArray.length))); if (sizeBeforeDelete == -1) { // totally fresh file - response.setContent(cacheArray); + yacyResponse.setContent(cacheArray); try { - Cache.store(response.url(), response.getResponseHeader(), cacheArray); - sb.toIndexer(response); + Cache.store(yacyResponse.url(), yacyResponse.getResponseHeader(), cacheArray); + sb.toIndexer(yacyResponse); } catch (IOException e) { - log.logWarning("cannot write " + response.url() + " to Cache (1): " + e.getMessage(), e); + //log.logWarning("cannot write " + response.url() + " to Cache (1): " + e.getMessage(), e); } - conProp.setProperty(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_CODE, "TCP_MISS"); + // log cache miss } else if (cacheArray != null && sizeBeforeDelete == cacheArray.length) { + // TODO: what should happen here? // before we came here we deleted a cache entry cacheArray = null; //cacheManager.push(cacheEntry); // unnecessary update - conProp.setProperty(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_CODE, "TCP_REF_FAIL_HIT"); + // log cache refresh fail miss } else { // before we came here we deleted a cache entry - response.setContent(cacheArray); + yacyResponse.setContent(cacheArray); try { - Cache.store(response.url(), response.getResponseHeader(), cacheArray); - sb.toIndexer(response); + Cache.store(yacyResponse.url(), yacyResponse.getResponseHeader(), cacheArray); + sb.toIndexer(yacyResponse); } catch (IOException e) { - log.logWarning("cannot write " + response.url() + " to Cache (2): " + e.getMessage(), e); + //log.logWarning("cannot write " + response.url() + " to Cache (2): " + e.getMessage(), e); } - conProp.setProperty(HeaderFramework.CONNECTION_PROP_PROXY_RESPOND_CODE, "TCP_REFRESH_MISS"); + // log refresh cache miss } } else { diff --git a/source/net/yacy/http/YaCyErrorHandler.java b/source/net/yacy/http/YaCyErrorHandler.java new file mode 100644 index 000000000..80714326f --- /dev/null +++ b/source/net/yacy/http/YaCyErrorHandler.java @@ -0,0 +1,8 @@ +package net.yacy.http; + +import org.eclipse.jetty.server.handler.ErrorHandler; + +public class YaCyErrorHandler extends ErrorHandler { + + +}