Merged branch 'jetty-9.3.x' into 'master'.
This commit is contained in:
commit
35758b6f87
|
@ -21,6 +21,8 @@ package org.eclipse.jetty.client;
|
||||||
import org.eclipse.jetty.client.api.Request;
|
import org.eclipse.jetty.client.api.Request;
|
||||||
import org.eclipse.jetty.client.api.Response;
|
import org.eclipse.jetty.client.api.Response;
|
||||||
import org.eclipse.jetty.client.api.Result;
|
import org.eclipse.jetty.client.api.Result;
|
||||||
|
import org.eclipse.jetty.http.HttpField;
|
||||||
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>A protocol handler that handles redirect status codes 301, 302, 303, 307 and 308.</p>
|
* <p>A protocol handler that handles redirect status codes 301, 302, 303, 307 and 308.</p>
|
||||||
|
@ -54,6 +56,14 @@ public class RedirectProtocolHandler extends Response.Listener.Adapter implement
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onHeader(Response response, HttpField field)
|
||||||
|
{
|
||||||
|
// Avoid that the content is decoded, which could generate
|
||||||
|
// errors, since we are discarding the content anyway.
|
||||||
|
return field.getHeader() != HttpHeader.CONTENT_ENCODING;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(Result result)
|
public void onComplete(Result result)
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.UnresolvedAddressException;
|
import java.nio.channels.UnresolvedAddressException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -44,7 +45,6 @@ import org.eclipse.jetty.toolchain.test.IO;
|
||||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -55,15 +55,11 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
super(sslContextFactory);
|
super(sslContextFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
|
||||||
public void prepare() throws Exception
|
|
||||||
{
|
|
||||||
start(new RedirectHandler());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test_303() throws Exception
|
public void test_303() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/localhost/done")
|
.path("/303/localhost/done")
|
||||||
|
@ -77,6 +73,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void test_303_302() throws Exception
|
public void test_303_302() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/localhost/302/localhost/done")
|
.path("/303/localhost/302/localhost/done")
|
||||||
|
@ -90,6 +88,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void test_303_302_OnDifferentDestinations() throws Exception
|
public void test_303_302_OnDifferentDestinations() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/127.0.0.1/302/localhost/done")
|
.path("/303/127.0.0.1/302/localhost/done")
|
||||||
|
@ -103,6 +103,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void test_301() throws Exception
|
public void test_301() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.method(HttpMethod.HEAD)
|
.method(HttpMethod.HEAD)
|
||||||
|
@ -117,6 +119,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void test_301_WithWrongMethod() throws Exception
|
public void test_301_WithWrongMethod() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.newRequest("localhost", connector.getLocalPort())
|
client.newRequest("localhost", connector.getLocalPort())
|
||||||
|
@ -140,6 +144,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void test_307_WithRequestContent() throws Exception
|
public void test_307_WithRequestContent() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7};
|
byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7};
|
||||||
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
|
@ -157,6 +163,7 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testMaxRedirections() throws Exception
|
public void testMaxRedirections() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
client.setMaxRedirects(1);
|
client.setMaxRedirects(1);
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -181,6 +188,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void test_303_WithConnectionClose_WithBigRequest() throws Exception
|
public void test_303_WithConnectionClose_WithBigRequest() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/localhost/done?close=true")
|
.path("/303/localhost/done?close=true")
|
||||||
|
@ -194,6 +203,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testDontFollowRedirects() throws Exception
|
public void testDontFollowRedirects() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.followRedirects(false)
|
.followRedirects(false)
|
||||||
|
@ -208,6 +219,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testRelativeLocation() throws Exception
|
public void testRelativeLocation() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/localhost/done?relative=true")
|
.path("/303/localhost/done?relative=true")
|
||||||
|
@ -221,6 +234,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testAbsoluteURIPathWithSpaces() throws Exception
|
public void testAbsoluteURIPathWithSpaces() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/localhost/a+space?decode=true")
|
.path("/303/localhost/a+space?decode=true")
|
||||||
|
@ -234,6 +249,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testRelativeURIPathWithSpaces() throws Exception
|
public void testRelativeURIPathWithSpaces() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/303/localhost/a+space?relative=true&decode=true")
|
.path("/303/localhost/a+space?relative=true&decode=true")
|
||||||
|
@ -247,7 +264,6 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testRedirectWithWrongScheme() throws Exception
|
public void testRedirectWithWrongScheme() throws Exception
|
||||||
{
|
{
|
||||||
dispose();
|
|
||||||
start(new AbstractHandler()
|
start(new AbstractHandler()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
|
@ -264,14 +280,10 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.path("/path")
|
.path("/path")
|
||||||
.timeout(5, TimeUnit.SECONDS)
|
.timeout(5, TimeUnit.SECONDS)
|
||||||
.send(new Response.CompleteListener()
|
.send(result ->
|
||||||
{
|
{
|
||||||
@Override
|
Assert.assertTrue(result.isFailed());
|
||||||
public void onComplete(Result result)
|
latch.countDown();
|
||||||
{
|
|
||||||
Assert.assertTrue(result.isFailed());
|
|
||||||
latch.countDown();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||||
}
|
}
|
||||||
|
@ -281,6 +293,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
public void testRedirectFailed() throws Exception
|
public void testRedirectFailed() throws Exception
|
||||||
{
|
{
|
||||||
// TODO this test is failing with timout after an ISP upgrade?? DNS dependent?
|
// TODO this test is failing with timout after an ISP upgrade?? DNS dependent?
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.newRequest("localhost", connector.getLocalPort())
|
client.newRequest("localhost", connector.getLocalPort())
|
||||||
|
@ -370,6 +384,7 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
@Test
|
@Test
|
||||||
public void testHttpRedirector() throws Exception
|
public void testHttpRedirector() throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
final HttpRedirector redirector = new HttpRedirector(client);
|
final HttpRedirector redirector = new HttpRedirector(client);
|
||||||
|
|
||||||
org.eclipse.jetty.client.api.Request request1 = client.newRequest("localhost", connector.getLocalPort())
|
org.eclipse.jetty.client.api.Request request1 = client.newRequest("localhost", connector.getLocalPort())
|
||||||
|
@ -390,20 +405,52 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
Assert.assertTrue(redirector.isRedirect(response2));
|
Assert.assertTrue(redirector.isRedirect(response2));
|
||||||
|
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
redirector.redirect(request2, response2, new Response.CompleteListener()
|
redirector.redirect(request2, response2, r ->
|
||||||
{
|
{
|
||||||
@Override
|
Response response3 = r.getResponse();
|
||||||
public void onComplete(Result result)
|
Assert.assertEquals(200, response3.getStatus());
|
||||||
{
|
Assert.assertFalse(redirector.isRedirect(response3));
|
||||||
Response response3 = result.getResponse();
|
latch.countDown();
|
||||||
Assert.assertEquals(200, response3.getStatus());
|
|
||||||
Assert.assertFalse(redirector.isRedirect(response3));
|
|
||||||
latch.countDown();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedirectWithCorruptedBody() throws Exception
|
||||||
|
{
|
||||||
|
byte[] bytes = "ok".getBytes(StandardCharsets.UTF_8);
|
||||||
|
start(new AbstractHandler()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||||
|
{
|
||||||
|
baseRequest.setHandled(true);
|
||||||
|
if (target.startsWith("/redirect"))
|
||||||
|
{
|
||||||
|
response.setStatus(HttpStatus.SEE_OTHER_303);
|
||||||
|
response.setHeader(HttpHeader.LOCATION.asString(), scheme + "://localhost:" + connector.getLocalPort() + "/ok");
|
||||||
|
// Say that we send gzipped content, but actually don't.
|
||||||
|
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
|
||||||
|
response.getOutputStream().write("redirect".getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response.setStatus(HttpStatus.OK_200);
|
||||||
|
response.getOutputStream().write(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
|
.scheme(scheme)
|
||||||
|
.path("/redirect")
|
||||||
|
.timeout(5, TimeUnit.SECONDS)
|
||||||
|
.send();
|
||||||
|
|
||||||
|
Assert.assertEquals(200, response.getStatus());
|
||||||
|
Assert.assertArrayEquals(bytes, response.getContent());
|
||||||
|
}
|
||||||
|
|
||||||
private void testSameMethodRedirect(final HttpMethod method, int redirectCode) throws Exception
|
private void testSameMethodRedirect(final HttpMethod method, int redirectCode) throws Exception
|
||||||
{
|
{
|
||||||
testMethodRedirect(method, method, redirectCode);
|
testMethodRedirect(method, method, redirectCode);
|
||||||
|
@ -416,6 +463,8 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||||
|
|
||||||
private void testMethodRedirect(final HttpMethod requestMethod, final HttpMethod redirectMethod, int redirectCode) throws Exception
|
private void testMethodRedirect(final HttpMethod requestMethod, final HttpMethod redirectMethod, int redirectCode) throws Exception
|
||||||
{
|
{
|
||||||
|
start(new RedirectHandler());
|
||||||
|
|
||||||
final AtomicInteger passes = new AtomicInteger();
|
final AtomicInteger passes = new AtomicInteger();
|
||||||
client.getRequestListeners().add(new org.eclipse.jetty.client.api.Request.Listener.Adapter()
|
client.getRequestListeners().add(new org.eclipse.jetty.client.api.Request.Listener.Adapter()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue