Search in sources :

Example 21 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class HttpStaticFileServerHandler method sendRedirect.

private static void sendRedirect(ChannelHandlerContext ctx, String newUri) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND);
    response.headers().set(HttpHeaderNames.LOCATION, newUri);
    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 22 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class Http2ServerDowngraderTest method testUpgradeEmptyFullResponseWithTrailers.

@Test
public void testUpgradeEmptyFullResponseWithTrailers() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    HttpHeaders trailers = response.trailingHeaders();
    trailers.set("key", "value");
    assertTrue(ch.writeOutbound(response));
    Http2HeadersFrame headersFrame = ch.readOutbound();
    assertThat(headersFrame.headers().status().toString(), is("200"));
    assertFalse(headersFrame.isEndStream());
    Http2HeadersFrame trailersFrame = ch.readOutbound();
    assertThat(trailersFrame.headers().get("key").toString(), is("value"));
    assertTrue(trailersFrame.isEndStream());
    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Test(org.junit.Test)

Example 23 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class SpdyServerHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        if (is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        boolean keepAlive = isKeepAlive(req);
        ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
        response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {
            response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
            ctx.write(response);
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) Date(java.util.Date)

Example 24 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class SpdyHttpDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, SpdyFrame msg, List<Object> out) throws Exception {
    if (msg instanceof SpdySynStreamFrame) {
        // HTTP requests/responses are mapped one-to-one to SPDY streams.
        SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg;
        int streamId = spdySynStreamFrame.streamId();
        if (SpdyCodecUtil.isServerId(streamId)) {
            // SYN_STREAM frames initiated by the server are pushed resources
            int associatedToStreamId = spdySynStreamFrame.associatedStreamId();
            // it must reply with a RST_STREAM with error code INVALID_STREAM.
            if (associatedToStreamId == 0) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INVALID_STREAM);
                ctx.writeAndFlush(spdyRstStreamFrame);
                return;
            }
            // (we only support pushed resources divided into two header blocks).
            if (spdySynStreamFrame.isLast()) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
                ctx.writeAndFlush(spdyRstStreamFrame);
                return;
            }
            // reply with a RST_STREAM with error code INTERNAL_ERROR.
            if (spdySynStreamFrame.isTruncated()) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INTERNAL_ERROR);
                ctx.writeAndFlush(spdyRstStreamFrame);
                return;
            }
            try {
                FullHttpRequest httpRequestWithEntity = createHttpRequest(spdySynStreamFrame, ctx.alloc());
                // Set the Stream-ID, Associated-To-Stream-ID, iand Priority as headers
                httpRequestWithEntity.headers().setInt(Names.STREAM_ID, streamId);
                httpRequestWithEntity.headers().setInt(Names.ASSOCIATED_TO_STREAM_ID, associatedToStreamId);
                httpRequestWithEntity.headers().setInt(Names.PRIORITY, spdySynStreamFrame.priority());
                out.add(httpRequestWithEntity);
            } catch (Throwable ignored) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
                ctx.writeAndFlush(spdyRstStreamFrame);
            }
        } else {
            // reply with a HTTP 431 REQUEST HEADER FIELDS TOO LARGE reply.
            if (spdySynStreamFrame.isTruncated()) {
                SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamId);
                spdySynReplyFrame.setLast(true);
                SpdyHeaders frameHeaders = spdySynReplyFrame.headers();
                frameHeaders.setInt(STATUS, HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE.code());
                frameHeaders.setObject(VERSION, HttpVersion.HTTP_1_0);
                ctx.writeAndFlush(spdySynReplyFrame);
                return;
            }
            try {
                FullHttpRequest httpRequestWithEntity = createHttpRequest(spdySynStreamFrame, ctx.alloc());
                // Set the Stream-ID as a header
                httpRequestWithEntity.headers().setInt(Names.STREAM_ID, streamId);
                if (spdySynStreamFrame.isLast()) {
                    out.add(httpRequestWithEntity);
                } else {
                    // Request body will follow in a series of Data Frames
                    putMessage(streamId, httpRequestWithEntity);
                }
            } catch (Throwable t) {
                // If a client sends a SYN_STREAM without all of the getMethod, url (host and path),
                // scheme, and version headers the server must reply with a HTTP 400 BAD REQUEST reply.
                // Also sends HTTP 400 BAD REQUEST reply if header name/value pairs are invalid
                SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamId);
                spdySynReplyFrame.setLast(true);
                SpdyHeaders frameHeaders = spdySynReplyFrame.headers();
                frameHeaders.setInt(STATUS, HttpResponseStatus.BAD_REQUEST.code());
                frameHeaders.setObject(VERSION, HttpVersion.HTTP_1_0);
                ctx.writeAndFlush(spdySynReplyFrame);
            }
        }
    } else if (msg instanceof SpdySynReplyFrame) {
        SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg;
        int streamId = spdySynReplyFrame.streamId();
        // reply with a RST_STREAM frame with error code INTERNAL_ERROR.
        if (spdySynReplyFrame.isTruncated()) {
            SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INTERNAL_ERROR);
            ctx.writeAndFlush(spdyRstStreamFrame);
            return;
        }
        try {
            FullHttpResponse httpResponseWithEntity = createHttpResponse(spdySynReplyFrame, ctx.alloc(), validateHeaders);
            // Set the Stream-ID as a header
            httpResponseWithEntity.headers().setInt(Names.STREAM_ID, streamId);
            if (spdySynReplyFrame.isLast()) {
                HttpUtil.setContentLength(httpResponseWithEntity, 0);
                out.add(httpResponseWithEntity);
            } else {
                // Response body will follow in a series of Data Frames
                putMessage(streamId, httpResponseWithEntity);
            }
        } catch (Throwable t) {
            // If a client receives a SYN_REPLY without valid getStatus and version headers
            // the client must reply with a RST_STREAM frame indicating a PROTOCOL_ERROR
            SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
            ctx.writeAndFlush(spdyRstStreamFrame);
        }
    } else if (msg instanceof SpdyHeadersFrame) {
        SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg;
        int streamId = spdyHeadersFrame.streamId();
        FullHttpMessage fullHttpMessage = getMessage(streamId);
        if (fullHttpMessage == null) {
            // HEADERS frames may initiate a pushed response
            if (SpdyCodecUtil.isServerId(streamId)) {
                // reply with a RST_STREAM frame with error code INTERNAL_ERROR.
                if (spdyHeadersFrame.isTruncated()) {
                    SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INTERNAL_ERROR);
                    ctx.writeAndFlush(spdyRstStreamFrame);
                    return;
                }
                try {
                    fullHttpMessage = createHttpResponse(spdyHeadersFrame, ctx.alloc(), validateHeaders);
                    // Set the Stream-ID as a header
                    fullHttpMessage.headers().setInt(Names.STREAM_ID, streamId);
                    if (spdyHeadersFrame.isLast()) {
                        HttpUtil.setContentLength(fullHttpMessage, 0);
                        out.add(fullHttpMessage);
                    } else {
                        // Response body will follow in a series of Data Frames
                        putMessage(streamId, fullHttpMessage);
                    }
                } catch (Throwable t) {
                    // If a client receives a SYN_REPLY without valid getStatus and version headers
                    // the client must reply with a RST_STREAM frame indicating a PROTOCOL_ERROR
                    SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
                    ctx.writeAndFlush(spdyRstStreamFrame);
                }
            }
            return;
        }
        // Ignore trailers in a truncated HEADERS frame.
        if (!spdyHeadersFrame.isTruncated()) {
            for (Map.Entry<CharSequence, CharSequence> e : spdyHeadersFrame.headers()) {
                fullHttpMessage.headers().add(e.getKey(), e.getValue());
            }
        }
        if (spdyHeadersFrame.isLast()) {
            HttpUtil.setContentLength(fullHttpMessage, fullHttpMessage.content().readableBytes());
            removeMessage(streamId);
            out.add(fullHttpMessage);
        }
    } else if (msg instanceof SpdyDataFrame) {
        SpdyDataFrame spdyDataFrame = (SpdyDataFrame) msg;
        int streamId = spdyDataFrame.streamId();
        FullHttpMessage fullHttpMessage = getMessage(streamId);
        // If message is not in map discard Data Frame.
        if (fullHttpMessage == null) {
            return;
        }
        ByteBuf content = fullHttpMessage.content();
        if (content.readableBytes() > maxContentLength - spdyDataFrame.content().readableBytes()) {
            removeMessage(streamId);
            throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes.");
        }
        ByteBuf spdyDataFrameData = spdyDataFrame.content();
        int spdyDataFrameDataLen = spdyDataFrameData.readableBytes();
        content.writeBytes(spdyDataFrameData, spdyDataFrameData.readerIndex(), spdyDataFrameDataLen);
        if (spdyDataFrame.isLast()) {
            HttpUtil.setContentLength(fullHttpMessage, content.readableBytes());
            removeMessage(streamId);
            out.add(fullHttpMessage);
        }
    } else if (msg instanceof SpdyRstStreamFrame) {
        SpdyRstStreamFrame spdyRstStreamFrame = (SpdyRstStreamFrame) msg;
        int streamId = spdyRstStreamFrame.streamId();
        removeMessage(streamId);
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) ByteBuf(io.netty.buffer.ByteBuf) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class SpdyHttpDecoder method createHttpResponse.

private static FullHttpResponse createHttpResponse(SpdyHeadersFrame responseFrame, ByteBufAllocator alloc, boolean validateHeaders) throws Exception {
    // Create the first line of the response from the name/value pairs
    SpdyHeaders headers = responseFrame.headers();
    HttpResponseStatus status = HttpResponseStatus.parseLine(headers.get(STATUS));
    HttpVersion version = HttpVersion.valueOf(headers.getAsString(VERSION));
    headers.remove(STATUS);
    headers.remove(VERSION);
    boolean release = true;
    ByteBuf buffer = alloc.buffer();
    try {
        FullHttpResponse res = new DefaultFullHttpResponse(version, status, buffer, validateHeaders);
        for (Map.Entry<CharSequence, CharSequence> e : responseFrame.headers()) {
            res.headers().add(e.getKey(), e.getValue());
        }
        // The Connection and Keep-Alive headers are no longer valid
        HttpUtil.setKeepAlive(res, true);
        // Transfer-Encoding header is not valid
        res.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
        res.headers().remove(HttpHeaderNames.TRAILER);
        release = false;
        return res;
    } finally {
        if (release) {
            buffer.release();
        }
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)83 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)50 ByteBuf (io.netty.buffer.ByteBuf)24 Test (org.junit.Test)16 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 TransportAddress (org.elasticsearch.common.transport.TransportAddress)8 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)8 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)7 HttpRequest (io.netty.handler.codec.http.HttpRequest)6 ChannelFuture (io.netty.channel.ChannelFuture)5 ChannelPipeline (io.netty.channel.ChannelPipeline)5 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)5 HashMap (java.util.HashMap)5 Channel (com.weibo.api.motan.transport.Channel)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 Map (java.util.Map)4 Pair (com.nike.internal.util.Pair)3