use of org.eclipse.jetty.websocket.api.UpgradeException in project jetty.project by eclipse.
the class ClientConnectTest method testBadHandshake_SwitchingProtocols_NoConnectionHeader.
@Test
public void testBadHandshake_SwitchingProtocols_NoConnectionHeader() throws Exception {
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket, wsUri);
IBlockheadServerConnection connection = server.accept();
List<String> requestLines = connection.readRequestLines();
String key = connection.parseWebSocketKey(requestLines);
// Send Switching Protocols 101, but no 'Connection' header
StringBuilder resp = new StringBuilder();
resp.append("HTTP/1.1 101 Switching Protocols\r\n");
resp.append("Sec-WebSocket-Accept: ").append(AcceptHash.hashKey(key)).append("\r\n");
// Intentionally leave out Connection header
resp.append("\r\n");
connection.respond(resp.toString());
// The attempt to get upgrade response future should throw error
try {
future.get(30, TimeUnit.SECONDS);
Assert.fail("Expected ExecutionException -> UpgradeException");
} catch (ExecutionException e) {
// Expected Path
UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(101));
}
}
use of org.eclipse.jetty.websocket.api.UpgradeException in project elastic-core-maven by OrdinaryDude.
the class PeerWebSocket method startClient.
/**
* Start a client session
*
* @param uri Server URI
* @return TRUE if the WebSocket connection was completed
* @throws IOException I/O error occurred
*/
public boolean startClient(URI uri) throws IOException {
if (peerClient == null) {
return false;
}
String address = String.format("%s:%d", uri.getHost(), uri.getPort());
boolean useWebSocket = false;
//
// Create a WebSocket connection. We need to serialize the connection requests
// since the NRS server will issue multiple concurrent requests to the same peer.
// After a successful connection, the subsequent connection requests will return
// immediately. After an unsuccessful connection, a new connect attempt will not
// be done until 60 seconds have passed.
//
lock.lock();
try {
if (session != null) {
useWebSocket = true;
} else if (System.currentTimeMillis() > connectTime + 10 * 1000) {
connectTime = System.currentTimeMillis();
ClientUpgradeRequest req = new ClientUpgradeRequest();
Future<Session> conn = peerClient.connect(this, uri, req);
conn.get(Peers.connectTimeout + 100, TimeUnit.MILLISECONDS);
useWebSocket = true;
}
} catch (ExecutionException exc) {
if (exc.getCause() instanceof UpgradeException) {
// We will use HTTP
} else if (exc.getCause() instanceof IOException) {
// Report I/O exception
throw (IOException) exc.getCause();
} else {
// We will use HTTP
Logger.logDebugMessage(String.format("WebSocket connection to %s failed", address), exc);
}
} catch (TimeoutException exc) {
throw new SocketTimeoutException(String.format("WebSocket connection to %s timed out", address));
} catch (IllegalStateException exc) {
if (!peerClient.isStarted()) {
Logger.logDebugMessage("WebSocket client not started or shutting down");
throw exc;
}
Logger.logDebugMessage(String.format("WebSocket connection to %s failed", address), exc);
} catch (Exception exc) {
Logger.logDebugMessage(String.format("WebSocket connection to %s failed", address), exc);
} finally {
if (!useWebSocket) {
close();
}
lock.unlock();
}
return useWebSocket;
}
Aggregations