Search in sources :

Example 16 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project asterixdb by apache.

the class ChunkedResponse method fullResponse.

private void fullResponse(HttpVersion version, HttpResponseStatus status, ByteBuf buffer, HttpHeaders headers) {
    DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(version, status, buffer);
    fullResponse.headers().set(headers);
    fullResponse.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, buffer.readableBytes());
    future = ctx.writeAndFlush(fullResponse);
    headerSent = true;
    done = true;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 17 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.

the class HttpConversionUtil method toHttpResponse.

/**
     * Create a new object to contain the response data
     *
     * @param streamId The stream associated with the response
     * @param http2Headers The initial set of HTTP/2 headers to create the response with
     * @param alloc The {@link ByteBufAllocator} to use to generate the content of the message
     * @param validateHttpHeaders <ul>
     *        <li>{@code true} to validate HTTP headers in the http-codec</li>
     *        <li>{@code false} not to validate HTTP headers in the http-codec</li>
     *        </ul>
     * @return A new response object which represents headers/data
     * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)}
     */
public static FullHttpResponse toHttpResponse(int streamId, Http2Headers http2Headers, ByteBufAllocator alloc, boolean validateHttpHeaders) throws Http2Exception {
    HttpResponseStatus status = parseStatus(http2Headers.status());
    // HTTP/2 does not define a way to carry the version or reason phrase that is included in an
    // HTTP/1.1 status line.
    FullHttpResponse msg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, alloc.buffer(), validateHttpHeaders);
    try {
        addHttp2ToHttpHeaders(streamId, http2Headers, msg, false);
    } catch (Http2Exception e) {
        msg.release();
        throw e;
    } catch (Throwable t) {
        msg.release();
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }
    return msg;
}
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)

Example 18 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project rest.li by linkedin.

the class TestRAPClientCodec method testResponseDecoder.

@Test(dataProvider = "responseData")
public void testResponseDecoder(int status, String entity, HttpHeaders headers, String[] cookies) {
    final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec());
    ByteBuf content = Unpooled.copiedBuffer(entity, CHARSET);
    FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(status), content);
    nettyResponse.headers().set(headers);
    for (String cookie : cookies) {
        nettyResponse.headers().add(HttpHeaderNames.SET_COOKIE, cookie);
    }
    ch.writeInbound(nettyResponse);
    RestResponse response = (RestResponse) ch.readInbound();
    Assert.assertEquals(response.getStatus(), status);
    Assert.assertEquals(response.getEntity().asString(CHARSET), entity);
    assertList(response.getCookies(), nettyResponse.headers().getAll(HttpConstants.RESPONSE_COOKIE_HEADER_NAME));
    for (Map.Entry<String, String> header : nettyResponse.headers()) {
        if (!header.getKey().equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
            List<String> values = response.getHeaderValues(header.getKey());
            Assert.assertNotNull(values);
            Assert.assertTrue(values.contains(header.getValue()));
        }
    }
    // make sure the incoming ByteBuf is released
    Assert.assertEquals(content.refCnt(), 0);
    ch.finish();
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteString(com.linkedin.data.ByteString) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 19 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project BRFS by zhangnianli.

the class ResponseSender method sendError.

public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
    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 20 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project BRFS by zhangnianli.

the class ResponseSender method sendError.

public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
    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)

Aggregations

DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)223 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)145 ByteBuf (io.netty.buffer.ByteBuf)68 HttpResponse (io.netty.handler.codec.http.HttpResponse)39 Test (org.junit.Test)25 HttpRequest (io.netty.handler.codec.http.HttpRequest)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)23 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)22 ChannelFuture (io.netty.channel.ChannelFuture)19 IOException (java.io.IOException)19 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)16 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)15 HttpObject (io.netty.handler.codec.http.HttpObject)15 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 HttpVersion (io.netty.handler.codec.http.HttpVersion)12 InetSocketAddress (java.net.InetSocketAddress)12 SSLException (javax.net.ssl.SSLException)12 Test (org.junit.jupiter.api.Test)12 Map (java.util.Map)11