public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri) throws IOException, GadgetException { return fetch(proxyUri, null); }
public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri) throws IOException, GadgetException { HttpRequest rcr = buildHttpRequest(proxyUri, proxyUri.getResource()); if (rcr == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, HttpRequest fallbackRcr = buildHttpRequest(proxyUri, fallbackUri); results = requestPipeline.execute(fallbackRcr); if (!proxyUri.shouldReturnOrigOnErr() || !isRecoverable(results)) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); setResponseContentHeaders(response, results);
protected void setResponseContentHeaders(HttpResponseBuilder response, HttpResponse results) { // We're skipping the content disposition header for flash due to an issue with Flash player 10 // This does make some sites a higher value phishing target, but this can be mitigated by // additional referer checks. if (!isFlash(response.getHeader("Content-Type"), results.getHeader("Content-Type"))) { String contentDispositionValue = results.getHeader("Content-Disposition"); if (StringUtils.isBlank(contentDispositionValue) || contentDispositionValue.indexOf("attachment;") == -1 || contentDispositionValue.indexOf("filename") == -1) { response.setHeader("Content-Disposition", "attachment;filename=p.txt"); } else { response.setHeader("Content-Disposition", contentDispositionValue); } } if (results.getHeader("Content-Type") == null) { response.setHeader("Content-Type", "application/octet-stream"); } }
@Test public void testThrowExceptionIfReturnOriginalContentOnErrorNotSet() throws Exception { String url = "http://example.org/mypage.html"; String domain = "example.org"; setupProxyRequestMock(domain, url, true, -1, null, null); String contentType = "text/html; charset=UTF-8"; HttpResponse resp = new HttpResponseBuilder() .setResponseString("Hello") .addHeader("Content-Type", contentType) .create(); expect(pipeline.execute((HttpRequest) EasyMock.anyObject())).andReturn(resp); replay(); final StringBuilder stringBuilder = new StringBuilder(""); ResponseRewriter rewriter = getResponseRewriterThatThrowsExceptions(stringBuilder); ResponseRewriterRegistry rewriterRegistry = new DefaultResponseRewriterRegistry( Arrays.<ResponseRewriter>asList(rewriter), null); ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true); boolean exceptionCaught = false; try { proxyHandler.fetch(request); } catch (GadgetException e) { exceptionCaught = true; assertEquals(404, e.getHttpStatusCode()); } assertTrue(exceptionCaught); assertEquals("exceptionThrown", stringBuilder.toString()); }
new DefaultResponseRewriterRegistry( Arrays.<ResponseRewriter>asList(rewriter), null); ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true, gadgetAdminStore, LONG_LIVED_REFRESH); proxyHandler.fetch(request); } catch (GadgetException e) { exceptionCaught = true;
@Test public void testDoPostNormal() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(request.getInputStream()).andReturn(postContentStream); expect(request.getMethod()).andReturn(POST_METHOD); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }
new DefaultResponseRewriterRegistry( Arrays.<ResponseRewriter>asList(rewriter), null); ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true, gadgetAdminStore, LONG_LIVED_REFRESH); proxyHandler.fetch(request); } catch (GadgetException e) { exceptionCaught = true;
public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri) throws IOException, GadgetException { HttpRequest rcr = buildHttpRequest(proxyUri, proxyUri.getResource()); if (rcr == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, HttpRequest fallbackRcr = buildHttpRequest(proxyUri, fallbackUri); results = requestPipeline.execute(fallbackRcr); if (!proxyUri.shouldReturnOrigOnErr() || !isRecoverable(results)) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); setResponseContentHeaders(response, results);
protected void setResponseContentHeaders(HttpResponseBuilder response, HttpResponse results) { // We're skipping the content disposition header for flash due to an issue with Flash player 10 // This does make some sites a higher value phishing target, but this can be mitigated by // additional referer checks. if (!isFlash(response.getHeader("Content-Type"), results.getHeader("Content-Type"))) { response.setHeader("Content-Disposition", "attachment;filename=p.txt"); } if (results.getHeader("Content-Type") == null) { response.setHeader("Content-Type", "application/octet-stream"); } }
@Test public void testDoPostNormal() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(request.getInputStream()).andReturn(postContentStream); expect(request.getMethod()).andReturn(POST_METHOD); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }
@Test public void testRecoverableRewritingException() throws Exception { String url = "http://example.org/mypage.html"; String domain = "example.org"; setupProxyRequestMock(domain, url, true, -1, null, null); String contentType = "text/html; charset=UTF-8"; HttpResponse resp = new HttpResponseBuilder() .setResponseString("Hello") .addHeader("Content-Type", contentType) .create(); expect(pipeline.execute((HttpRequest) EasyMock.anyObject())).andReturn(resp); replay(); final StringBuilder stringBuilder = new StringBuilder(""); ResponseRewriter rewriter = getResponseRewriterThatThrowsExceptions(stringBuilder); ResponseRewriterRegistry rewriterRegistry = new DefaultResponseRewriterRegistry( Arrays.<ResponseRewriter>asList(rewriter), null); ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true); request.setReturnOriginalContentOnError(true); HttpResponse recorder = proxyHandler.fetch(request); verify(); // Ensure that original content is returned. assertEquals(recorder.getHeader("Content-Type"), contentType); assertEquals("Hello", recorder.getResponseAsString()); assertEquals("exceptionThrown", stringBuilder.toString()); }
public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri, @Nullable String postBody) throws IOException, GadgetException { HttpRequest rcr = buildHttpRequest(proxyUri, proxyUri.getResource(), postBody); if (rcr == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, HttpRequest fallbackRcr = buildHttpRequest(proxyUri, fallbackUri, null); results = requestPipeline.execute(fallbackRcr); if (!proxyUri.shouldReturnOrigOnErr() || !isRecoverable(results)) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); setResponseContentHeaders(response, results);
@Test public void testDoPostHttpError() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(HttpResponse.notFound()); expect(request.getMethod()).andReturn(POST_METHOD); expect(request.getInputStream()).andReturn(postContentStream); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_NOT_FOUND, ""); }
@Test public void testRecoverableRewritingException() throws Exception { String url = "http://example.org/mypage.html"; String domain = "example.org"; setupProxyRequestMock(domain, url, true, -1, null, null); setupGadgetAdminMock(true); String contentType = "text/html; charset=UTF-8"; HttpResponse resp = new HttpResponseBuilder() .setResponseString("Hello") .addHeader("Content-Type", contentType) .create(); expect(pipeline.execute((HttpRequest) EasyMock.anyObject())).andReturn(resp); replay(); final StringBuilder stringBuilder = new StringBuilder(""); ResponseRewriter rewriter = getResponseRewriterThatThrowsExceptions(stringBuilder); ResponseRewriterRegistry rewriterRegistry = new DefaultResponseRewriterRegistry( Arrays.<ResponseRewriter>asList(rewriter), null); ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true, gadgetAdminStore, LONG_LIVED_REFRESH); request.setReturnOriginalContentOnError(true); HttpResponse recorder = proxyHandler.fetch(request); verify(); // Ensure that original content is returned. assertEquals(recorder.getHeader("Content-Type"), contentType); assertEquals("Hello", recorder.getResponseAsString()); assertEquals("exceptionThrown", stringBuilder.toString()); }
@Test public void testDoPostHttpError() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(HttpResponse.notFound()); expect(request.getMethod()).andReturn(POST_METHOD); expect(request.getInputStream()).andReturn(postContentStream); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_NOT_FOUND, ""); }
@Test public void testRecoverableRewritingException() throws Exception { String url = "http://example.org/mypage.html"; String domain = "example.org"; setupProxyRequestMock(domain, url, true, -1, null, null); setupGadgetAdminMock(true); String contentType = "text/html; charset=UTF-8"; HttpResponse resp = new HttpResponseBuilder() .setResponseString("Hello") .addHeader("Content-Type", contentType) .create(); expect(pipeline.execute((HttpRequest) EasyMock.anyObject())).andReturn(resp); replay(); final StringBuilder stringBuilder = new StringBuilder(""); ResponseRewriter rewriter = getResponseRewriterThatThrowsExceptions(stringBuilder); ResponseRewriterRegistry rewriterRegistry = new DefaultResponseRewriterRegistry( Arrays.<ResponseRewriter>asList(rewriter), null); ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true, gadgetAdminStore, LONG_LIVED_REFRESH); request.setReturnOriginalContentOnError(true); HttpResponse recorder = proxyHandler.fetch(request); verify(); // Ensure that original content is returned. assertEquals(recorder.getHeader("Content-Type"), contentType); assertEquals("Hello", recorder.getResponseAsString()); assertEquals("exceptionThrown", stringBuilder.toString()); }
response = proxyHandler.fetch(proxyUri); } catch (GadgetException e) { response = ServletUtil.errorResponse(new GadgetException(e.getCode(), e.getMessage(),
response = proxyHandler.fetch(proxyUri); } catch (GadgetException e) { response = ServletUtil.errorResponse(new GadgetException(e.getCode(), e.getMessage(),
public GadgetsHandlerApi.ProxyResponse getProxy(GadgetsHandlerApi.ProxyRequest request) throws ProcessingException { verifyBaseParams(request, true); Set<String> fields = beanFilter.processBeanFields(request.getFields()); ProxyUri proxyUri = createProxyUri(request); List<Uri> uris = proxyUriManager.make(ImmutableList.of(proxyUri), null); HttpResponse httpResponse = null; try { if (isFieldIncluded(fields, "proxyContent")) { httpResponse = proxyHandler.fetch(proxyUri); } } catch (IOException e) { LOG.log(Level.INFO, "Failed to fetch resource " + proxyUri.getResource().toString(), e); throw new ProcessingException("Error getting response content", HttpResponse.SC_BAD_GATEWAY); } catch (GadgetException e) { // TODO: Clean this log if it is too spammy LOG.log(Level.INFO, "Failed to fetch resource " + proxyUri.getResource().toString(), e); throw new ProcessingException("Error getting response content", HttpResponse.SC_BAD_GATEWAY); } try { return createProxyResponse(uris.get(0), httpResponse, fields, getProxyExpireMs(proxyUri, httpResponse)); } catch (IOException e) { // Should never happen! LOG.log(Level.WARNING, "Error creating proxy response", e); throw new ProcessingException("Error getting response content", HttpResponse.SC_INTERNAL_SERVER_ERROR); } }
@Test public void testDoGetWithOAuth2() throws Exception { Map<String, String> options = new HashMap<String, String>(); options.put("OAUTH_SERVICE_NAME", "example"); ProxyUriManager.ProxyUri proxyUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); proxyUri.setAuthType(AuthType.OAUTH2); Uri uri = Uri.parse(BASIC_SYNTAX_URL + "&authz=oauth2&OAUTH_SERVICE_NAME=example&container=default&gadget=http://example.org/gadget.xml"); expect(proxyUriManager.process(uri)).andReturn(proxyUri); expect(request.getScheme()).andReturn(uri.getScheme()); expect(request.getServerName()).andReturn(uri.getAuthority()); expect(request.getServerPort()).andReturn(80); expect(request.getRequestURI()).andReturn(uri.getPath()); expect(request.getQueryString()).andReturn(uri.getQuery()); expect(request.getHeader("Host")).andReturn(uri.getAuthority()); expect(request.getParameter("OAUTH_SERVICE_NAME")).andReturn("example"); expect(request.getParameterNames()).andReturn(Collections.enumeration(options.keySet())); expect(lockedDomainService.isSafeForOpenProxy(uri.getAuthority())).andReturn(true); ProxyUriManager.ProxyUri pUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); pUri.setAuthType(AuthType.OAUTH2); pUri.setOAuth2Arguments(new OAuth2Arguments(AuthType.OAUTH2, options)); expect(proxyHandler.fetch(pUri)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doGet(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }