/** * Build an HttpRequest object encapsulating the request details as requested * by the user. * @param request The http request. * @param uriToProxyOrRewrite The parsed uri to proxy or rewrite through * accel servlet. * @return Remote content request based on the parameters sent from the client. * @throws GadgetException In case the data could not be fetched. */ protected HttpRequest buildHttpRequest(HttpRequest request, ProxyUriManager.ProxyUri uriToProxyOrRewrite) throws GadgetException { Uri tgt = uriToProxyOrRewrite.getResource(); HttpRequest req = uriToProxyOrRewrite.makeHttpRequest(tgt); if (req == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, "No url parameter in request", HttpResponse.SC_BAD_REQUEST); } // Copy the post body if it exists. UriUtils.copyRequestData(request, req); // Set and copy headers. ServletUtil.setXForwardedForHeader(request, req); UriUtils.copyRequestHeaders( request, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES); req.setFollowRedirects(false); return req; }
UriUtils.copyResponseHeadersAndStatusCode( results, response, remapInternalServerError, false, UriUtils.DisallowedHeaders.OUTPUT_TRANSFER_DIRECTIVES, UriUtils.maybeRewriteContentType(req, response);
public VisitStatus visit(Gadget gadget, Node node) throws RewritingException { if (node.getNodeType() == Node.ELEMENT_NODE && META.equalsIgnoreCase(node.getNodeName())) { Element elem = (Element) node; String httpEquip = elem.getAttribute(HTTP_EQUIV); String content = elem.getAttribute(CONTENT); if (httpEquip != null && content != null && CONTENT_TYPE.equalsIgnoreCase(httpEquip)) { elem.setAttribute(CONTENT, UriUtils.getContentTypeWithoutCharset(content)); return VisitStatus.MODIFY; } } return VisitStatus.BYPASS; }
/** * Copies headers from HttpServletRequest object to HttpRequest object. * @param origRequest Servlet request to copy headers from. * @param req The HttpRequest object to copy headers to. * @param disallowedRequestHeaders Disallowed request headers to omit from * the servlet request */ public static void copyRequestHeaders(HttpRequest origRequest, HttpRequest req, DisallowedHeaders... disallowedRequestHeaders) { Set<String> allDisallowedHeaders = new HashSet<String>(); for (DisallowedHeaders h : disallowedRequestHeaders) { allDisallowedHeaders.addAll(h.getDisallowedHeaders()); } for (Map.Entry<String, List<String>> inHeader : origRequest.getHeaders().entrySet()) { String header = inHeader.getKey(); List<String> headerValues = inHeader.getValue(); if (headerValues != null && !headerValues.isEmpty() && isValidHeaderName(header) && !allDisallowedHeaders.contains(header.toLowerCase())) { // Remove existing values of this header. req.removeHeader(header); for (String headerVal : headerValues) { if (isValidHeaderValue(headerVal)) { req.addHeader(header, headerVal); } } } } }
@Test public void testCopyResponseHeadersAndStatusCode_AddHeader() throws Exception { HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(5000) .addHeader("hello", "world1") .addHeader("hello", "world2") .addHeader("hello\u2297", "bad header") .addHeader("Content-length", "10") .addHeader("vary", "1") .create(); HttpResponseBuilder builder = new HttpResponseBuilder(); UriUtils.copyResponseHeadersAndStatusCode(resp, builder, false, false, UriUtils.DisallowedHeaders.OUTPUT_TRANSFER_DIRECTIVES, UriUtils.DisallowedHeaders.CACHING_DIRECTIVES); HttpResponse response = builder.create(); // Date is added by HttpResponse. assertEquals(3, response.getHeaders().size()); Iterator<String> headers = response.getHeaders("hello").iterator(); assertEquals("world1", headers.next()); assertEquals("world2", headers.next()); assertEquals(5000, response.getHttpStatusCode()); }
private void verifyMime(String requestMime, String responseMime, String expectedMime) throws Exception { String url = "http://example.org/foo"; HttpRequest req = new HttpRequest(Uri.parse(url)) .setRewriteMimeType(requestMime); HttpResponseBuilder builder = new HttpResponseBuilder() .setHeader("Content-Type", responseMime); UriUtils.maybeRewriteContentType(req, builder); assertEquals(expectedMime, builder.getHeader("Content-Type")); }
@Test public void testCopyRequestData() throws Exception { HttpRequest origRequest = new HttpRequest(Uri.parse("http://www.example.com")); origRequest.setMethod("Post"); String data = "hello world"; origRequest.setPostBody(data.getBytes()); HttpRequest req = new HttpRequest(Uri.parse( "http://www.example.org/data.html")); UriUtils.copyRequestData(origRequest, req); assertEquals(data, req.getPostBodyAsString()); }
@Test public void testCopyRequestHeaders() throws Exception { HttpRequest origRequest = new HttpRequest(Uri.parse("http://www.example.org/data.html")); Map<String, List<String>> addedHeaders = ImmutableMap.<String, List<String>>builder() .put("h1", ImmutableList.of("v1", "v2")) .put("h2", ImmutableList.of("v3", "v4")) .put("hello\u2297", ImmutableList.of("v5", "v6")) .put("unchanged_header", ImmutableList.<String>of()) .put("Content-Length", ImmutableList.of("50", "100")) .build(); origRequest.addAllHeaders(addedHeaders); HttpRequest req = new HttpRequest(Uri.parse( "http://www.example.org/data.html")); req.removeHeader(HttpRequest.DOS_PREVENTION_HEADER); req.addHeader("h1", "hello"); req.addHeader("Content-Length", "10"); req.addHeader("unchanged_header", "original_value"); UriUtils.copyRequestHeaders(origRequest, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES); Map<String, List<String>> headers = ImmutableMap.<String, List<String>>builder() .put("h1", ImmutableList.of("v1", "v2")) .put("h2", ImmutableList.of("v3", "v4")) .put("unchanged_header", ImmutableList.of("original_value")) .put("Content-Length", ImmutableList.of("10")) .put(HttpRequest.DOS_PREVENTION_HEADER, ImmutableList.of("on")) .build(); assertEquals(headers, req.getHeaders()); }
@Test public void testCopyResponseHeadersAndStatusCode_AddHeader() throws Exception { HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(5000) .addHeader("hello", "world1") .addHeader("hello", "world2") .addHeader("hello\u2297", "bad header") .addHeader("Content-length", "10") .addHeader("vary", "1") .create(); HttpResponseBuilder builder = new HttpResponseBuilder(); UriUtils.copyResponseHeadersAndStatusCode(resp, builder, false, false, UriUtils.DisallowedHeaders.OUTPUT_TRANSFER_DIRECTIVES, UriUtils.DisallowedHeaders.CACHING_DIRECTIVES); HttpResponse response = builder.create(); // Date is added by HttpResponse. assertEquals(3, response.getHeaders().size()); Iterator<String> headers = response.getHeaders("hello").iterator(); assertEquals("world1", headers.next()); assertEquals("world2", headers.next()); assertEquals(5000, response.getHttpStatusCode()); }
isValidHeaderName(header) && !allDisallowedHeaders.contains(header.toLowerCase())) { if (isValidHeaderValue(headerVal)) { req.addHeader(header, headerVal);
private void verifyMime(String requestMime, String responseMime, String expectedMime) throws Exception { String url = "http://example.org/foo"; HttpRequest req = new HttpRequest(Uri.parse(url)) .setRewriteMimeType(requestMime); HttpResponseBuilder builder = new HttpResponseBuilder() .setHeader("Content-Type", responseMime); UriUtils.maybeRewriteContentType(req, builder); assertEquals(expectedMime, builder.getHeader("Content-Type")); }
@Test public void testCopyRequestData() throws Exception { HttpRequest origRequest = new HttpRequest(Uri.parse("http://www.example.com")); origRequest.setMethod("Post"); String data = "hello world"; origRequest.setPostBody(data.getBytes()); HttpRequest req = new HttpRequest(Uri.parse( "http://www.example.org/data.html")); UriUtils.copyRequestData(origRequest, req); assertEquals(data, req.getPostBodyAsString()); }
@Test public void testCopyRequestHeaders() throws Exception { HttpRequest origRequest = new HttpRequest(Uri.parse("http://www.example.org/data.html")); Map<String, List<String>> addedHeaders = ImmutableMap.<String, List<String>>builder() .put("h1", ImmutableList.of("v1", "v2")) .put("h2", ImmutableList.of("v3", "v4")) .put("hello\u2297", ImmutableList.of("v5", "v6")) .put("unchanged_header", ImmutableList.<String>of()) .put("Content-Length", ImmutableList.of("50", "100")) .build(); origRequest.addAllHeaders(addedHeaders); HttpRequest req = new HttpRequest(Uri.parse( "http://www.example.org/data.html")); req.removeHeader(HttpRequest.DOS_PREVENTION_HEADER); req.addHeader("h1", "hello"); req.addHeader("Content-Length", "10"); req.addHeader("unchanged_header", "original_value"); UriUtils.copyRequestHeaders(origRequest, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES); Map<String, List<String>> headers = ImmutableMap.<String, List<String>>builder() .put("h1", ImmutableList.of("v1", "v2")) .put("h2", ImmutableList.of("v3", "v4")) .put("unchanged_header", ImmutableList.of("original_value")) .put("Content-Length", ImmutableList.of("10")) .put(HttpRequest.DOS_PREVENTION_HEADER, ImmutableList.of("on")) .build(); assertEquals(headers, req.getHeaders()); }
UriUtils.copyResponseHeadersAndStatusCode( results, response, remapInternalServerError, true, UriUtils.DisallowedHeaders.OUTPUT_TRANSFER_DIRECTIVES, UriUtils.maybeRewriteContentType(req, response);
@Test public void testCopyResponseHeadersAndStatusCode_AddHeader() throws Exception { HttpResponse resp = new HttpResponseBuilder() .setHttpStatusCode(5000) .addHeader("hello", "world1") .addHeader("hello", "world2") .addHeader("hello\u2297", "bad header") .addHeader("Content-length", "10") .addHeader("vary", "1") .create(); HttpResponseBuilder builder = new HttpResponseBuilder(); UriUtils.copyResponseHeadersAndStatusCode(resp, builder, false, false, UriUtils.DisallowedHeaders.OUTPUT_TRANSFER_DIRECTIVES, UriUtils.DisallowedHeaders.CACHING_DIRECTIVES); HttpResponse response = builder.create(); // Date is added by HttpResponse. assertEquals(3, response.getHeaders().size()); Iterator<String> headers = response.getHeaders("hello").iterator(); assertEquals("world1", headers.next()); assertEquals("world2", headers.next()); assertEquals(5000, response.getHttpStatusCode()); }
/** * Build an HttpRequest object encapsulating the request details as requested * by the user. * @param request The http request. * @param uriToProxyOrRewrite The parsed uri to proxy or rewrite through * accel servlet. * @return Remote content request based on the parameters sent from the client. * @throws GadgetException In case the data could not be fetched. */ protected HttpRequest buildHttpRequest(HttpRequest request, ProxyUriManager.ProxyUri uriToProxyOrRewrite) throws GadgetException { Uri tgt = uriToProxyOrRewrite.getResource(); HttpRequest req = uriToProxyOrRewrite.makeHttpRequest(tgt); if (req == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, "No url parameter in request", HttpResponse.SC_BAD_REQUEST); } // Copy the post body if it exists. UriUtils.copyRequestData(request, req); // Set and copy headers. ServletUtil.setXForwardedForHeader(request, req); UriUtils.copyRequestHeaders( request, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES); req.setFollowRedirects(false); return req; }
public VisitStatus visit(Gadget gadget, Node node) throws RewritingException { if (node.getNodeType() == Node.ELEMENT_NODE && META.equalsIgnoreCase(node.getNodeName())) { Element elem = (Element) node; String httpEquip = elem.getAttribute(HTTP_EQUIV); String content = elem.getAttribute(CONTENT); if (httpEquip != null && content != null && CONTENT_TYPE.equalsIgnoreCase(httpEquip)) { elem.setAttribute(CONTENT, UriUtils.getContentTypeWithoutCharset(content)); return VisitStatus.MODIFY; } } return VisitStatus.BYPASS; }
isValidHeaderName(header) && !allDisallowedHeaders.contains(header.toLowerCase())) { if (isValidHeaderValue(headerVal)) { req.addHeader(header, headerVal);
private void verifyMime(String requestMime, String responseMime, String expectedMime) throws Exception { String url = "http://example.org/foo"; HttpRequest req = new HttpRequest(Uri.parse(url)) .setRewriteMimeType(requestMime); HttpResponseBuilder builder = new HttpResponseBuilder() .setHeader("Content-Type", responseMime); UriUtils.maybeRewriteContentType(req, builder); assertEquals(expectedMime, builder.getHeader("Content-Type")); }
@Test public void testCopyRequestData() throws Exception { HttpRequest origRequest = new HttpRequest(Uri.parse("http://www.example.com")); origRequest.setMethod("Post"); String data = "hello world"; origRequest.setPostBody(data.getBytes()); HttpRequest req = new HttpRequest(Uri.parse( "http://www.example.org/data.html")); UriUtils.copyRequestData(origRequest, req); assertEquals(data, req.getPostBodyAsString()); }