/** {@inheritDoc} */ public HttpResponse fetch(HttpRequest request) { if (!whitelist.allows(request.getUri().toJavaUri())) { log.warn( "A request to " + request.getUri() + " has been denied. To allow requests to this URL add the application URL to your whitelist (http://confluence.atlassian.com/x/KQfCDQ )."); return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_FORBIDDEN) .setHeader("Content-Type", "text/plain") .setResponseString( "Requests to " + request.getUri() + " are not allowed. See your administrator about configuring a whitelist entry for this destination (http://confluence.atlassian.com/x/KQfCDQ ).") .create(); } HttpCacheKey cacheKey = new HttpCacheKey(request); HttpResponse response = cache.getResponse(cacheKey, request); if (response != null) { return response; } try { HttpUriRequest hcRequest = newHcRequest(request); if (request.getPostBodyLength() > 0) { ((HttpEntityEnclosingRequest) hcRequest) .setEntity(new InputStreamEntity(request.getPostBody(), request.getPostBodyLength())); } org.apache.http.HttpResponse hcResponse; try { // first try with TLS, the most common SSL protocol hcResponse = newHttpClient("TLSv1", request).execute(hcRequest); } catch (SSLException e) { log.debug( "SSL Exception establishing connection with TLSv1 protocol. Falling back to SSLv3.", e); // if TLS failed, try with SSLv3 hcResponse = newHttpClient("SSLv3", request).execute(hcRequest); } response = makeResponse(hcResponse); return cache.addResponse(cacheKey, request, response); } catch (IOException e) { if (e instanceof java.net.SocketTimeoutException || e instanceof java.net.SocketException) { return HttpResponse.timeout(); } else { log.error("Unable to retrieve response", e); } return HttpResponse.error(); } }
@Test public void testGetFallback() throws Exception { String url = "http://example.org/file.evil"; String domain = "example.org"; String fallback_url = "http://fallback.com/fallback.png"; setupProxyRequestMock(domain, url, true, -1, null, fallback_url); HttpRequest req = new HttpRequest(Uri.parse(url)).setIgnoreCache(true); HttpResponse resp = HttpResponse.error(); HttpResponse fallback_resp = new HttpResponse("Fallback"); expect(pipeline.execute(req)).andReturn(resp); expect(pipeline.execute(isA(HttpRequest.class))).andReturn(fallback_resp); replay(); proxyHandler.fetch(request); verify(); }