@Test public void reconstructUrlFromFullUrlAndHostHeader() throws Exception { String[][] testCases = { new String[]{"https: new String[]{"https: new String[]{"https: new String[]{"http: new String[]{"http: new String[]{"http: new String[]{"http: }; List<String> failures = new ArrayList<>(); for (String[] testCase : testCases) { String fullUrl = testCase[0], hostHeader = testCase[1], expected = testCase[2]; boolean https = "https".equalsIgnoreCase(URI.create(fullUrl).getScheme()); HttpRequest mockRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, ""); ResponseManufacturingFilter filter = new ResponseManufacturingFilter(mockRequest, createChannelHandlerContext(https), EasyMock.createMock(BmpResponseManufacturer.WithState.class), EasyMock.createMock(BmpResponseListener.class)); String actual = filter.reconstructUrlFromFullUrlAndHostHeader(fullUrl, hostHeader); String msg = String.format("%s reconstructed from %s and Host: %s", actual, fullUrl, hostHeader); if (!Objects.equals(expected, actual)) { failures.add(String.format("FAIL: %s (expected %s)", msg, expected)); } else { System.out.println(msg); } } failures.forEach(System.out::println); assertEquals("failures", Collections.emptyList(), failures); }
protected String reconstructUrlFromFullUrlAndHostHeader(String fullUrl, @Nullable String hostHeader) { if (isHttps()) { if (hostHeader != null) { try { URI fullUrlUri = new URI(fullUrl); URIBuilder uriBuilder = new URIBuilder(fullUrlUri); cleanHostAndPort(uriBuilder, hostHeader, fullUrlUri.getScheme()); fullUrl = uriBuilder.build().toString(); } catch (URISyntaxException e) { log.info("failed to reconstruct URL with proper host", e); } } else { log.info("no host header in request {} {}", requestAccumulator.getMethod(), fullUrl); } } return fullUrl; }
ResponseManufacturingFilter extends HttpsAwareFiltersAdapter { protected String reconstructUrlFromFullUrlAndHostHeader(String fullUrl, @Nullable String hostHeader) { if (isHttps()) { if (hostHeader != null) { try { URI fullUrlUri = new URI(fullUrl); URIBuilder uriBuilder = new URIBuilder(fullUrlUri); cleanHostAndPort(uriBuilder, hostHeader, fullUrlUri.getScheme()); fullUrl = uriBuilder.build().toString(); } catch (URISyntaxException e) { log.info("failed to reconstruct URL with proper host", e); } } else { log.info("no host header in request {} {}", requestAccumulator.getMethod(), fullUrl); } } return fullUrl; } }
ResponseManufacturingFilter extends HttpsAwareFiltersAdapter { protected String reconstructUrlFromFullUrlAndHostHeader(String fullUrl, @Nullable String hostHeader) { if (isHttps()) { if (hostHeader != null) { try { URI fullUrlUri = new URI(fullUrl); URIBuilder uriBuilder = new URIBuilder(fullUrlUri); cleanHostAndPort(uriBuilder, hostHeader, fullUrlUri.getScheme()); fullUrl = uriBuilder.build().toString(); } catch (URISyntaxException e) { log.info("failed to reconstruct URL with proper host", e); } } else { log.info("no host header in request {} {}", requestAccumulator.getMethod(), fullUrl); } } return fullUrl; } <S> ResponseManufacturingFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, BmpResponseManufacturer.WithState<S> responseManufacturer, BmpResponseListener responseListener); }
ResponseManufacturingFilter extends HttpsAwareFiltersAdapter { protected String reconstructUrlFromFullUrlAndHostHeader(String fullUrl, @Nullable String hostHeader) { if (isHttps()) { if (hostHeader != null) { try { URI fullUrlUri = new URI(fullUrl); URIBuilder uriBuilder = new URIBuilder(fullUrlUri); cleanHostAndPort(uriBuilder, hostHeader, fullUrlUri.getScheme()); fullUrl = uriBuilder.build().toString(); } catch (URISyntaxException e) { log.info("failed to reconstruct URL with proper host", e); } } else { log.info("no host header in request {} {}", requestAccumulator.getMethod(), fullUrl); } } return fullUrl; } <S> ResponseManufacturingFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, BmpResponseManufacturer.WithState<S> responseManufacturer, BmpResponseListener responseListener); @Override HttpResponse clientToProxyRequest(HttpObject httpObject); @Override final HttpObject proxyToClientResponse(HttpObject httpObject); @Override HttpObject serverToProxyResponse(HttpObject httpObject); @Override void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress); @Override void proxyToServerRequestSending(); @Override void proxyToServerResolutionFailed(String hostAndPort); @Override void proxyToServerConnectionFailed(); @Override void serverToProxyResponseTimedOut(); @Override HttpResponse proxyToServerRequest(HttpObject httpObject); @Override void proxyToServerRequestSent(); @Override void serverToProxyResponseReceiving(); @Override void serverToProxyResponseReceived(); @Override void proxyToServerConnectionQueued(); @Override InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort); @Override void proxyToServerConnectionStarted(); @Override void proxyToServerConnectionSSLHandshakeStarted(); @Override void proxyToServerConnectionSucceeded(ChannelHandlerContext serverCtx); }
ResponseManufacturingFilter extends HttpsAwareFiltersAdapter { protected String reconstructUrlFromFullUrlAndHostHeader(String fullUrl, @Nullable String hostHeader) { if (isHttps()) { if (hostHeader != null) { try { URI fullUrlUri = new URI(fullUrl); URIBuilder uriBuilder = new URIBuilder(fullUrlUri); cleanHostAndPort(uriBuilder, hostHeader, fullUrlUri.getScheme()); fullUrl = uriBuilder.build().toString(); } catch (URISyntaxException e) { log.info("failed to reconstruct URL with proper host", e); } } else { log.info("no host header in request {} {}", requestAccumulator.getMethod(), fullUrl); } } return fullUrl; } <S> ResponseManufacturingFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, BmpResponseManufacturer.WithState<S> responseManufacturer, BmpResponseListener responseListener); @Override HttpResponse clientToProxyRequest(HttpObject httpObject); @Override final HttpObject proxyToClientResponse(HttpObject httpObject); @Override HttpObject serverToProxyResponse(HttpObject httpObject); @Override void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress); @Override void proxyToServerRequestSending(); @Override void proxyToServerResolutionFailed(String hostAndPort); @Override void proxyToServerConnectionFailed(); @Override void serverToProxyResponseTimedOut(); @Override HttpResponse proxyToServerRequest(HttpObject httpObject); @Override void proxyToServerRequestSent(); @Override void serverToProxyResponseReceiving(); @Override void serverToProxyResponseReceived(); @Override void proxyToServerConnectionQueued(); @Override InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort); @Override void proxyToServerConnectionStarted(); @Override void proxyToServerConnectionSSLHandshakeStarted(); @Override void proxyToServerConnectionSucceeded(ChannelHandlerContext serverCtx);  }