Search in sources :

Example 46 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class RequestInfoImpl method addContentChunk.

/**
     * {@inheritDoc}
     */
@Override
public int addContentChunk(HttpContent chunk) {
    if (isCompleteRequestWithAllChunks) {
        throw new IllegalStateException("Cannot add new content chunk - this RequestInfo is already marked as " + "representing the complete request with all chunks");
    }
    chunk.retain();
    rawContentLengthInBytes += chunk.content().readableBytes();
    // If content chunks will be released externally then there's no point in us holding on to them
    if (!contentChunksWillBeReleasedExternally)
        contentChunks.add(chunk);
    if (chunk instanceof LastHttpContent) {
        //      to be set to true if content chunks are released externally.
        if (!contentChunksWillBeReleasedExternally)
            isCompleteRequestWithAllChunks = true;
        HttpHeaders chunkTrailingHeaders = ((LastHttpContent) chunk).trailingHeaders();
        //noinspection StatementWithEmptyBody
        if (trailingHeaders == chunkTrailingHeaders) {
        // Can happen during the constructor. We've already set the trailing headers to the chunk's trailing headers, so nothing to do here.
        } else {
            if (!trailingHeaders.isEmpty()) {
                throw new IllegalStateException("Received the final chunk, but trailingHeaders was already " + "populated. This should not be possible.");
            }
            trailingHeaders.add(chunkTrailingHeaders);
        }
    }
    return rawContentLengthInBytes;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 47 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent 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 48 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project Glowstone by GlowstoneMC.

the class HttpHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        int responseCode = response.status().code();
        if (responseCode == HttpResponseStatus.NO_CONTENT.code()) {
            done(ctx);
            return;
        }
        if (responseCode != HttpResponseStatus.OK.code()) {
            throw new IllegalStateException("Expected HTTP response 200 OK, got " + responseCode);
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        content.append(httpContent.content().toString(Charset.forName("UTF-8")));
        if (msg instanceof LastHttpContent) {
            done(ctx);
        }
    }
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

LastHttpContent (io.netty.handler.codec.http.LastHttpContent)48 HttpContent (io.netty.handler.codec.http.HttpContent)21 ByteBuf (io.netty.buffer.ByteBuf)17 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)16 HttpResponse (io.netty.handler.codec.http.HttpResponse)15 Test (org.junit.Test)15 HttpRequest (io.netty.handler.codec.http.HttpRequest)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)10 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)6 Map (java.util.Map)6 ChannelFuture (io.netty.channel.ChannelFuture)5 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)5 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)5 List (java.util.List)5 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 HttpObject (io.netty.handler.codec.http.HttpObject)3