@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 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()); }
/** * 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; }
/** * 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.copyRequestHeaders( httpRequest, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES,
req.addHeader("unchanged_header", "original_value"); UriUtils.copyRequestHeaders(origRequest, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES);