Search in sources :

Example 1 with NettyResponseStatus

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

the class HttpHandler method handleHttpResponse.

private void handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception {
    HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
    logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
    future.setKeepAlive(config.getKeepAliveStrategy().keepAlive(future.getTargetRequest(), httpRequest, response));
    NettyResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
    HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
    if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
        boolean abort = //
        abortAfterHandlingStatus(handler, status) || //
        abortAfterHandlingHeaders(handler, responseHeaders) || abortAfterHandlingReactiveStreams(channel, future, handler);
        if (abort) {
            finishUpdate(future, channel, false, false);
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) NettyResponseStatus(org.asynchttpclient.netty.NettyResponseStatus) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders)

Example 2 with NettyResponseStatus

use of org.asynchttpclient.netty.NettyResponseStatus 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)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)2 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)2 NettyResponseStatus (org.asynchttpclient.netty.NettyResponseStatus)2 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 HttpResponseStatus (org.asynchttpclient.HttpResponseStatus)1 NettyWebSocket (org.asynchttpclient.netty.ws.NettyWebSocket)1 WebSocketUpgradeHandler (org.asynchttpclient.ws.WebSocketUpgradeHandler)1