Search in sources :

Example 1 with NettyWebSocket

use of org.asynchttpclient.netty.ws.NettyWebSocket in project async-http-client by AsyncHttpClient.

the class WebSocketHandler method handleException.

@Override
public void handleException(NettyResponseFuture<?> future, Throwable e) {
    logger.warn("onError", e);
    try {
        WebSocketUpgradeHandler h = (WebSocketUpgradeHandler) future.getAsyncHandler();
        NettyWebSocket webSocket = h.onCompleted();
        if (webSocket != null) {
            webSocket.onError(e.getCause());
            webSocket.close();
        }
    } catch (Throwable t) {
        logger.error("onError", t);
    }
}
Also used : NettyWebSocket(org.asynchttpclient.netty.ws.NettyWebSocket) WebSocketUpgradeHandler(org.asynchttpclient.ws.WebSocketUpgradeHandler)

Example 2 with NettyWebSocket

use of org.asynchttpclient.netty.ws.NettyWebSocket in project async-http-client by AsyncHttpClient.

the class WebSocketHandler method handleRead.

@Override
public void handleRead(Channel channel, NettyResponseFuture<?> future, Object e) throws Exception {
    if (e instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) e;
        if (logger.isDebugEnabled()) {
            HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
            logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
        }
        WebSocketUpgradeHandler handler = (WebSocketUpgradeHandler) future.getAsyncHandler();
        HttpResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
        HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
        if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
            switch(handler.onStatusReceived(status)) {
                case CONTINUE:
                    upgrade(channel, future, handler, response, responseHeaders);
                    break;
                default:
                    abort(channel, future, handler, status);
            }
        }
    } else if (e instanceof WebSocketFrame) {
        final WebSocketFrame frame = (WebSocketFrame) e;
        WebSocketUpgradeHandler handler = (WebSocketUpgradeHandler) future.getAsyncHandler();
        NettyWebSocket webSocket = handler.onCompleted();
        // retain because we might buffer the frame
        if (webSocket.isReady()) {
            webSocket.handleFrame(frame);
        } else {
            // WebSocket hasn't been open yet, but upgrading the pipeline triggered a read and a frame was sent along the HTTP upgrade response
            // as we want to keep sequential order (but can't notify user of open before upgrading so he doesn't to try send immediately), we have to buffer
            webSocket.bufferFrame(frame);
        }
    } else if (!(e instanceof LastHttpContent)) {
        // ignore, end of handshake response
        logger.error("Invalid message {}", e);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) NettyResponseStatus(org.asynchttpclient.netty.NettyResponseStatus) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) NettyWebSocket(org.asynchttpclient.netty.ws.NettyWebSocket) HttpResponse(io.netty.handler.codec.http.HttpResponse) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) WebSocketUpgradeHandler(org.asynchttpclient.ws.WebSocketUpgradeHandler) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 3 with NettyWebSocket

use of org.asynchttpclient.netty.ws.NettyWebSocket in project async-http-client by AsyncHttpClient.

the class WebSocketHandler method upgrade.

private void upgrade(Channel channel, NettyResponseFuture<?> future, WebSocketUpgradeHandler handler, HttpResponse response, HttpResponseHeaders responseHeaders) throws Exception {
    boolean validStatus = response.status().equals(SWITCHING_PROTOCOLS);
    boolean validUpgrade = response.headers().get(UPGRADE) != null;
    String connection = response.headers().get(CONNECTION);
    boolean validConnection = HttpHeaderValues.UPGRADE.contentEqualsIgnoreCase(connection);
    final boolean headerOK = handler.onHeadersReceived(responseHeaders) == State.CONTINUE;
    if (!headerOK || !validStatus || !validUpgrade || !validConnection) {
        requestSender.abort(channel, future, new IOException("Invalid handshake response"));
        return;
    }
    String accept = response.headers().get(SEC_WEBSOCKET_ACCEPT);
    String key = getAcceptKey(future.getNettyRequest().getHttpRequest().headers().get(SEC_WEBSOCKET_KEY));
    if (accept == null || !accept.equals(key)) {
        requestSender.abort(channel, future, new IOException("Invalid challenge. Actual: " + accept + ". Expected: " + key));
    }
    // set back the future so the protocol gets notified of frames
    // removing the HttpClientCodec from the pipeline might trigger a read with a WebSocket message
    // if it comes in the same frame as the HTTP Upgrade response
    Channels.setAttribute(channel, future);
    handler.setWebSocket(new NettyWebSocket(channel, responseHeaders.getHeaders()));
    channelManager.upgradePipelineForWebSockets(channel.pipeline());
    // process using the same thread.
    try {
        handler.onOpen();
    } catch (Exception ex) {
        logger.warn("onSuccess unexpected exception", ex);
    }
    future.done();
}
Also used : NettyWebSocket(org.asynchttpclient.netty.ws.NettyWebSocket) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with NettyWebSocket

use of org.asynchttpclient.netty.ws.NettyWebSocket in project async-http-client by AsyncHttpClient.

the class WebSocketHandler method handleChannelInactive.

@Override
public void handleChannelInactive(NettyResponseFuture<?> future) {
    logger.trace("onClose");
    try {
        WebSocketUpgradeHandler h = (WebSocketUpgradeHandler) future.getAsyncHandler();
        NettyWebSocket webSocket = h.onCompleted();
        logger.trace("Connection was closed abnormally (that is, with no close frame being received).");
        if (webSocket != null)
            webSocket.close(1006, "Connection was closed abnormally (that is, with no close frame being received).");
    } catch (Throwable t) {
        logger.error("onError", t);
    }
}
Also used : NettyWebSocket(org.asynchttpclient.netty.ws.NettyWebSocket) WebSocketUpgradeHandler(org.asynchttpclient.ws.WebSocketUpgradeHandler)

Aggregations

NettyWebSocket (org.asynchttpclient.netty.ws.NettyWebSocket)4 WebSocketUpgradeHandler (org.asynchttpclient.ws.WebSocketUpgradeHandler)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)1 IOException (java.io.IOException)1 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)1 HttpResponseStatus (org.asynchttpclient.HttpResponseStatus)1 NettyResponseStatus (org.asynchttpclient.netty.NettyResponseStatus)1