476170 - Support servers that close connections without sending Connection: close header.

Adding support for validating the connection only in the HTTP/1.1
transport.
This commit is contained in:
Simone Bordet 2015-08-30 18:09:36 +02:00
parent 7211d415f9
commit 4ba2b22da7
5 changed files with 217 additions and 7 deletions

View File

@ -144,6 +144,7 @@ public class HttpClient extends ContainerLifeCycle
private volatile HttpField encodingField;
private volatile boolean removeIdleDestinations = false;
private volatile boolean connectBlocking = false;
private volatile boolean validateConnections = false;
/**
* Creates a {@link HttpClient} instance that can perform requests to non-TLS destinations only
@ -1025,6 +1026,32 @@ public class HttpClient extends ContainerLifeCycle
this.connectBlocking = connectBlocking;
}
/**
* @return whether connections are validated before sending a request
* @see #setValidateConnections(boolean)
*/
public boolean isValidateConnections()
{
return validateConnections;
}
/**
* <p>Whether connections are validated before sending a request.</p>
* <p>This is a best-effort attempt to validate that the connection
* onto which requests are sent is valid before sending the request.</p>
* <p>The validation is performed only if the underlying transport
* implementation supports validating connections before their use
* (some transport implementation may not be able to perform such
* check).</p>
*
* @param validateConnections whether connections are validated before sending a request.
* @see #isValidateConnections()
*/
public void setValidateConnections(boolean validateConnections)
{
this.validateConnections = validateConnections;
}
/**
* @return the forward proxy configuration
*/

View File

@ -76,10 +76,27 @@ public abstract class PoolingHttpDestination<C extends Connection> extends HttpD
process(connection);
}
@SuppressWarnings("unchecked")
public C acquire()
{
return (C)connectionPool.acquire();
while (true)
{
@SuppressWarnings("unchecked")
C c = (C)connectionPool.acquire();
if (isValid(c))
return c;
else
c.close();
}
}
private boolean isValid(Connection c)
{
if (getHttpClient().isValidateConnections())
{
if (c instanceof Validateable)
return ((Validateable)c).validate();
}
return true;
}
/**
@ -143,7 +160,10 @@ public abstract class PoolingHttpDestination<C extends Connection> extends HttpD
{
if (connectionPool.isActive(connection))
{
process(connection);
if (isValid(connection))
process(connection);
else
connection.close();
}
else
{

View File

@ -0,0 +1,24 @@
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.client;
public interface Validateable
{
boolean validate();
}

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.client.http;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousCloseException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
@ -26,17 +27,19 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.client.HttpConnection;
import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.client.Validateable;
import org.eclipse.jetty.client.api.Connection;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Sweeper;
public class HttpConnectionOverHTTP extends AbstractConnection implements Connection, Sweeper.Sweepable
public class HttpConnectionOverHTTP extends AbstractConnection implements Connection, Sweeper.Sweepable, Validateable
{
private static final Logger LOG = Log.getLogger(HttpConnectionOverHTTP.class);
@ -98,7 +101,7 @@ public class HttpConnectionOverHTTP extends AbstractConnection implements Connec
protected boolean onReadTimeout()
{
if (LOG.isDebugEnabled())
LOG.debug("{} idle timeout", this);
LOG.debug("Idle timeout {}", this);
close(new TimeoutException());
return false;
}
@ -166,13 +169,37 @@ public class HttpConnectionOverHTTP extends AbstractConnection implements Connec
{
if (!closed.get())
return false;
if (sweeps.incrementAndGet() < 4)
return false;
return true;
}
@Override
public boolean validate()
{
ByteBufferPool byteBufferPool = getHttpDestination().getHttpClient().getByteBufferPool();
ByteBuffer buffer = byteBufferPool.acquire(1, true);
try
{
EndPoint endPoint = getEndPoint();
int filled = endPoint.fill(buffer);
if (LOG.isDebugEnabled())
LOG.debug("Validated {} {}", filled, this);
// Invalid if we read -1 or garbage bytes.
return filled == 0;
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("Could not validate connection " + this, x);
return false;
}
finally
{
byteBufferPool.release(buffer);
}
}
@Override
public String toString()
{

View File

@ -74,7 +74,9 @@ import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.eclipse.jetty.toolchain.test.annotation.Slow;
@ -1539,6 +1541,116 @@ public class HttpClientTest extends AbstractHttpClientServerTest
}
}
@Test
public void testServerClosesConnectionAfterRedirect() throws Exception
{
start(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
if (target.endsWith("/redirect"))
{
response.setStatus(HttpStatus.TEMPORARY_REDIRECT_307);
response.setContentLength(0);
response.setHeader(HttpHeader.LOCATION.asString(), scheme + "://localhost:" + connector.getLocalPort() + "/");
response.flushBuffer();
baseRequest.getHttpChannel().getEndPoint().shutdownOutput();
}
else
{
response.setStatus(HttpStatus.OK_200);
response.setContentLength(0);
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
}
}
});
client.setValidateConnections(true);
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/redirect")
.send();
Assert.assertEquals(200, response.getStatus());
}
@Test
public void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnectionsWithConnectionCloseHeader() throws Exception
{
testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
response.setStatus(HttpStatus.OK_200);
response.setContentLength(0);
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
}
});
}
@Test
public void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnectionsWithoutConnectionCloseHeader() throws Exception
{
testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
response.setStatus(HttpStatus.OK_200);
response.setContentLength(0);
response.flushBuffer();
baseRequest.getHttpChannel().getEndPoint().shutdownOutput();
}
});
}
private void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections(Handler handler) throws Exception
{
start(handler);
client.setValidateConnections(true);
client.setMaxConnectionsPerDestination(1);
final CountDownLatch latch = new CountDownLatch(1);
Request request1 = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/one")
.onRequestBegin(r ->
{
try
{
latch.await();
}
catch (InterruptedException x)
{
r.abort(x);
}
});
FutureResponseListener listener1 = new FutureResponseListener(request1);
request1.send(listener1);
Request request2 = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/two");
FutureResponseListener listener2 = new FutureResponseListener(request2);
request2.send(listener2);
// Now we have one request about to be sent, and one queued.
latch.countDown();
ContentResponse response1 = listener1.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response1.getStatus());
ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response2.getStatus());
}
private void consume(InputStream input, boolean eof) throws IOException
{
int crlfs = 0;