Search in sources :

Example 1 with HttpResponseException

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));
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpResponse(org.eclipse.jetty.client.HttpResponse) HttpResponseException(org.eclipse.jetty.client.HttpResponseException)

Example 2 with HttpResponseException

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;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpResponse(org.eclipse.jetty.client.HttpResponse) HttpResponseException(org.eclipse.jetty.client.HttpResponseException) Result(org.eclipse.jetty.client.api.Result)

Example 3 with HttpResponseException

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);
}
Also used : ArrayList(java.util.ArrayList) WebSocketClientConnection(org.eclipse.jetty.websocket.client.io.WebSocketClientConnection) HttpResponseException(org.eclipse.jetty.client.HttpResponseException) EndPoint(org.eclipse.jetty.io.EndPoint) URI(java.net.URI) ExtensionStack(org.eclipse.jetty.websocket.common.extensions.ExtensionStack) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) QuotedStringTokenizer(org.eclipse.jetty.util.QuotedStringTokenizer) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) HttpField(org.eclipse.jetty.http.HttpField)

Example 4 with HttpResponseException

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);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpResponseException(org.eclipse.jetty.client.HttpResponseException) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Aggregations

HttpResponseException (org.eclipse.jetty.client.HttpResponseException)4 HttpExchange (org.eclipse.jetty.client.HttpExchange)2 HttpResponse (org.eclipse.jetty.client.HttpResponse)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 HttpRequest (org.eclipse.jetty.client.HttpRequest)1 Result (org.eclipse.jetty.client.api.Result)1 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)1 HttpField (org.eclipse.jetty.http.HttpField)1 EndPoint (org.eclipse.jetty.io.EndPoint)1 QuotedStringTokenizer (org.eclipse.jetty.util.QuotedStringTokenizer)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1 WebSocketClientConnection (org.eclipse.jetty.websocket.client.io.WebSocketClientConnection)1 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)1 ExtensionStack (org.eclipse.jetty.websocket.common.extensions.ExtensionStack)1 Test (org.junit.Test)1