@Test public void doFollowCrossSiteRedirects() throws Exception { when(response.getStatusLine()).thenReturn(statusLine); final RedirectStrategy underTest = new NexusRedirectStrategy(); request = new HttpGet("http: when(statusLine.getStatusCode()).thenReturn(SC_MOVED_TEMPORARILY); when(response.getFirstHeader(argThat(equalToIgnoringCase(LOCATION)))) .thenReturn(new BasicHeader(LOCATION, "http: assertThat(underTest.isRedirected(request, response, new BasicHttpContext()), is(true)); request = new HttpGet("http: when(statusLine.getStatusCode()).thenReturn(SC_MOVED_TEMPORARILY); when(response.getFirstHeader(argThat(equalToIgnoringCase(LOCATION)))) .thenReturn(new BasicHeader(LOCATION, "http: assertThat(underTest.isRedirected(request, response, new BasicHttpContext()), is(true)); }
@Override public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { if (super.isRedirected(request, response, context)) { final Header locationHeader = response.getFirstHeader(LOCATION); if (locationHeader == null) { throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location present"); } final URI sourceUri = ((HttpUriRequest) request).getURI(); final String sourceScheme = schemeOf(sourceUri); final String sourceHost = hostOf(sourceUri); final URI targetUri = createLocationURI(locationHeader.getValue()); final String targetScheme = schemeOf(targetUri); final String targetHost = hostOf(targetUri); if (!Objects.equals(sourceScheme, targetScheme)) { if (HTTP.equals(targetScheme)) { log.debug("Downgrade from HTTPS to HTTP during redirection {} -> {}", sourceUri, targetUri); } else if ("https".equals(targetScheme) && Objects.equals(sourceHost, targetHost)) { log.debug("Protocol upgrade during redirection on same host {} -> {}", sourceUri, targetUri); } } if (Boolean.TRUE == context.getAttribute(CONTENT_RETRIEVAL_MARKER_KEY)) { if (targetUri.getPath().endsWith("/")) { log.debug("Not following redirection to index {} -> {}", sourceUri, targetUri); return false; } } log.debug("Following redirection {} -> {}", sourceUri, targetUri); return true; } return false; }
NexusRedirectStrategy extends DefaultRedirectStrategy { @Override public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { if (super.isRedirected(request, response, context)) { final Header locationHeader = response.getFirstHeader(LOCATION); if (locationHeader == null) { throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location present"); } final URI sourceUri = ((HttpUriRequest) request).getURI(); final String sourceScheme = schemeOf(sourceUri); final String sourceHost = hostOf(sourceUri); final URI targetUri = createLocationURI(locationHeader.getValue()); final String targetScheme = schemeOf(targetUri); final String targetHost = hostOf(targetUri); if (!Objects.equals(sourceScheme, targetScheme)) { if (HTTP.equals(targetScheme)) { log.debug("Downgrade from HTTPS to HTTP during redirection {} -> {}", sourceUri, targetUri); } else if ("https".equals(targetScheme) && Objects.equals(sourceHost, targetHost)) { log.debug("Protocol upgrade during redirection on same host {} -> {}", sourceUri, targetUri); } } if (Boolean.TRUE == context.getAttribute(CONTENT_RETRIEVAL_MARKER_KEY)) { if (targetUri.getPath().endsWith("/")) { log.debug("Not following redirection to index {} -> {}", sourceUri, targetUri); return false; } } log.debug("Following redirection {} -> {}", sourceUri, targetUri); return true; } return false; } }
NexusRedirectStrategy extends DefaultRedirectStrategy { @Override public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { if (super.isRedirected(request, response, context)) { final Header locationHeader = response.getFirstHeader(LOCATION); if (locationHeader == null) { throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location present"); } final URI sourceUri = ((HttpUriRequest) request).getURI(); final String sourceScheme = schemeOf(sourceUri); final String sourceHost = hostOf(sourceUri); final URI targetUri = createLocationURI(locationHeader.getValue()); final String targetScheme = schemeOf(targetUri); final String targetHost = hostOf(targetUri); if (!Objects.equals(sourceScheme, targetScheme)) { if (HTTP.equals(targetScheme)) { log.debug("Downgrade from HTTPS to HTTP during redirection {} -> {}", sourceUri, targetUri); } else if ("https".equals(targetScheme) && Objects.equals(sourceHost, targetHost)) { log.debug("Protocol upgrade during redirection on same host {} -> {}", sourceUri, targetUri); } } if (Boolean.TRUE == context.getAttribute(CONTENT_RETRIEVAL_MARKER_KEY)) { if (targetUri.getPath().endsWith("/")) { log.debug("Not following redirection to index {} -> {}", sourceUri, targetUri); return false; } } log.debug("Following redirection {} -> {}", sourceUri, targetUri); return true; } return false; }  }
NexusRedirectStrategy extends DefaultRedirectStrategy { @Override public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { if (super.isRedirected(request, response, context)) { final Header locationHeader = response.getFirstHeader(LOCATION); if (locationHeader == null) { throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location present"); } final URI sourceUri = ((HttpUriRequest) request).getURI(); final String sourceScheme = schemeOf(sourceUri); final String sourceHost = hostOf(sourceUri); final URI targetUri = createLocationURI(locationHeader.getValue()); final String targetScheme = schemeOf(targetUri); final String targetHost = hostOf(targetUri); if (!Objects.equals(sourceScheme, targetScheme)) { if (HTTP.equals(targetScheme)) { log.debug("Downgrade from HTTPS to HTTP during redirection {} -> {}", sourceUri, targetUri); } else if ("https".equals(targetScheme) && Objects.equals(sourceHost, targetHost)) { log.debug("Protocol upgrade during redirection on same host {} -> {}", sourceUri, targetUri); } } if (Boolean.TRUE == context.getAttribute(CONTENT_RETRIEVAL_MARKER_KEY)) { if (targetUri.getPath().endsWith("/")) { log.debug("Not following redirection to index {} -> {}", sourceUri, targetUri); return false; } } log.debug("Following redirection {} -> {}", sourceUri, targetUri); return true; } return false; }  @Override boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context); }
NexusRedirectStrategy extends DefaultRedirectStrategy { @Override public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { if (super.isRedirected(request, response, context)) { final Header locationHeader = response.getFirstHeader(LOCATION); if (locationHeader == null) { throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location present"); } final URI sourceUri = ((HttpUriRequest) request).getURI(); final String sourceScheme = schemeOf(sourceUri); final String sourceHost = hostOf(sourceUri); final URI targetUri = createLocationURI(locationHeader.getValue()); final String targetScheme = schemeOf(targetUri); final String targetHost = hostOf(targetUri); if (!Objects.equals(sourceScheme, targetScheme)) { if (HTTP.equals(targetScheme)) { log.debug("Downgrade from HTTPS to HTTP during redirection {} -> {}", sourceUri, targetUri); } else if ("https".equals(targetScheme) && Objects.equals(sourceHost, targetHost)) { log.debug("Protocol upgrade during redirection on same host {} -> {}", sourceUri, targetUri); } } if (Boolean.TRUE == context.getAttribute(CONTENT_RETRIEVAL_MARKER_KEY)) { if (targetUri.getPath().endsWith("/")) { log.debug("Not following redirection to index {} -> {}", sourceUri, targetUri); return false; } } log.debug("Following redirection {} -> {}", sourceUri, targetUri); return true; } return false; }  @Override boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context); static final String CONTENT_RETRIEVAL_MARKER_KEY; }