Issue #4603 - clarifications in comments and improve test

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-03-25 22:43:43 +11:00
parent 035c6d2639
commit 85ea14e33d
5 changed files with 24 additions and 4 deletions

View File

@ -314,6 +314,9 @@ public class HttpTransportOverHTTP2 implements HttpTransport
return transportCallback.onIdleTimeout(failure);
}
/**
* @return true if error sent, false if upgraded or aborted.
*/
boolean prepareUpgrade()
{
HttpChannelOverHTTP2 channel = (HttpChannelOverHTTP2)stream.getAttachment();

View File

@ -490,7 +490,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
break;
}
// If an upgrade is attempted and failed with sendError call, then do not close here.
// If send error is called we need to break.
if (checkAndPrepareUpgrade())
break;
@ -527,6 +527,10 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
return !suspended;
}
/**
* @param message the error message.
* @return true if we have sent an error, false if we have aborted.
*/
public boolean sendErrorOrAbort(String message)
{
try

View File

@ -479,7 +479,6 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
if (LOG.isDebugEnabled())
LOG.debug("Upgrade from {} to {}", getEndPoint().getConnection(), upgradeConnection);
getRequest().setAttribute(HttpTransport.UPGRADE_CONNECTION_ATTRIBUTE, upgradeConnection);
getResponse().setStatus(HttpStatus.SWITCHING_PROTOCOLS_101);
getHttpTransport().onCompleted();
return true;
}

View File

@ -160,12 +160,12 @@ public abstract class AbstractHandshaker implements Handshaker
coreSession.setWebSocketConnection(connection);
baseRequest.setHandled(true);
Response baseResponse = baseRequest.getResponse();
prepareResponse(baseResponse, negotiation);
if (httpConfig.getSendServerVersion())
baseResponse.getHttpFields().put(SERVER_VERSION);
baseResponse.flushBuffer();
baseRequest.setHandled(true);
baseRequest.setAttribute(HttpTransport.UPGRADE_CONNECTION_ATTRIBUTE, connection);

View File

@ -23,6 +23,7 @@ import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
@ -294,11 +295,21 @@ public class WebSocketOverHTTP2Test
startServer();
startClient(clientConnector -> new ClientConnectionFactoryOverHTTP2.H2(new HTTP2Client(clientConnector)));
CountDownLatch latch = new CountDownLatch(1);
connector.addBean(new HttpChannel.Listener()
{
@Override
public void onComplete(Request request)
{
latch.countDown();
}
});
EventSocket wsEndPoint = new EventSocket();
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/ws/throw");
ExecutionException failure;
try (StacklessLogging stacklessLogging = new StacklessLogging(HttpChannel.class))
try (StacklessLogging ignored = new StacklessLogging(HttpChannel.class))
{
failure = Assertions.assertThrows(ExecutionException.class, () ->
wsClient.connect(wsEndPoint, uri).get(5, TimeUnit.SECONDS));
@ -307,6 +318,9 @@ public class WebSocketOverHTTP2Test
Throwable cause = failure.getCause();
assertThat(cause, instanceOf(UpgradeException.class));
assertThat(cause.getMessage(), containsStringIgnoringCase("Unexpected HTTP Response Status Code: 500"));
// Wait for the request to complete on server before stopping.
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
@Test