use of org.eclipse.jetty.client.HttpResponseException in project jetty.project by eclipse.
the class HttpReceiverOverHTTP method badMessage.
@Override
public void badMessage(int status, String reason) {
HttpExchange exchange = getHttpExchange();
if (exchange != null) {
HttpResponse response = exchange.getResponse();
response.status(status).reason(reason);
failAndClose(new HttpResponseException("HTTP protocol violation: bad response on " + getHttpConnection(), response));
}
}
use of org.eclipse.jetty.client.HttpResponseException in project jetty.project by eclipse.
the class HttpChannelOverHTTP method exchangeTerminating.
@Override
public Result exchangeTerminating(HttpExchange exchange, Result result) {
if (result.isFailed())
return result;
HttpResponse response = exchange.getResponse();
if ((response.getVersion() == HttpVersion.HTTP_1_1) && (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)) {
String connection = response.getHeaders().get(HttpHeader.CONNECTION);
if ((connection == null) || !connection.toLowerCase(Locale.US).contains("upgrade")) {
return new Result(result, new HttpResponseException("101 Switching Protocols without Connection: Upgrade not supported", response));
}
// Upgrade Response
HttpRequest request = exchange.getRequest();
if (request instanceof HttpConnectionUpgrader) {
HttpConnectionUpgrader listener = (HttpConnectionUpgrader) request;
try {
listener.upgrade(response, getHttpConnection());
} catch (Throwable x) {
return new Result(result, x);
}
}
}
return result;
}
use of org.eclipse.jetty.client.HttpResponseException in project jetty.project by eclipse.
the class WebSocketUpgradeRequest method upgrade.
@Override
public void upgrade(HttpResponse response, HttpConnectionOverHTTP oldConn) {
if (!this.getHeaders().get(HttpHeader.UPGRADE).equalsIgnoreCase("websocket")) {
// Not my upgrade
throw new HttpResponseException("Not WebSocket Upgrade", response);
}
// Check the Accept hash
String reqKey = this.getHeaders().get(HttpHeader.SEC_WEBSOCKET_KEY);
String expectedHash = AcceptHash.hashKey(reqKey);
String respHash = response.getHeaders().get(HttpHeader.SEC_WEBSOCKET_ACCEPT);
if (expectedHash.equalsIgnoreCase(respHash) == false) {
throw new HttpResponseException("Invalid Sec-WebSocket-Accept hash", response);
}
// We can upgrade
EndPoint endp = oldConn.getEndPoint();
WebSocketClientConnection connection = new WebSocketClientConnection(endp, wsClient.getExecutor(), wsClient.getScheduler(), localEndpoint.getPolicy(), wsClient.getBufferPool());
URI requestURI = this.getURI();
WebSocketSession session = getSessionFactory().createSession(requestURI, localEndpoint, connection);
session.setUpgradeRequest(new ClientUpgradeRequest(this));
session.setUpgradeResponse(new ClientUpgradeResponse(response));
connection.addListener(session);
ExtensionStack extensionStack = new ExtensionStack(getExtensionFactory());
List<ExtensionConfig> extensions = new ArrayList<>();
HttpField extField = response.getHeaders().getField(HttpHeader.SEC_WEBSOCKET_EXTENSIONS);
if (extField != null) {
String[] extValues = extField.getValues();
if (extValues != null) {
for (String extVal : extValues) {
QuotedStringTokenizer tok = new QuotedStringTokenizer(extVal, ",");
while (tok.hasMoreTokens()) {
extensions.add(ExtensionConfig.parse(tok.nextToken()));
}
}
}
}
extensionStack.negotiate(extensions);
extensionStack.configure(connection.getParser());
extensionStack.configure(connection.getGenerator());
// Setup Incoming Routing
connection.setNextIncomingFrames(extensionStack);
extensionStack.setNextIncoming(session);
// Setup Outgoing Routing
session.setOutgoingHandler(extensionStack);
extensionStack.setNextOutgoing(connection);
session.addManaged(extensionStack);
session.setFuture(fut);
wsClient.addManaged(session);
if (upgradeListener != null) {
upgradeListener.onHandshakeResponse(new ClientUpgradeResponse(response));
}
// Now swap out the connection
endp.upgrade(connection);
}
use of org.eclipse.jetty.client.HttpResponseException in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_BadResponse.
@Test
public void test_Receive_BadResponse() throws Exception {
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: A\r\n" + "\r\n");
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
try {
listener.get(5, TimeUnit.SECONDS);
Assert.fail();
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof HttpResponseException);
}
}
Aggregations