Search in sources :

Example 41 with FullHttpResponse

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

the class ResponseSender method sendError.

public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status, String reason) {
    byte[] content = reason != null ? BrStringUtils.toUtf8Bytes(reason) : new byte[0];
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.wrappedBuffer(content));
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
    sendResponse(ctx, response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 42 with FullHttpResponse

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

the class HelloWorldHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (!(msg instanceof HttpRequest))
        return;
    HttpRequest req = (HttpRequest) msg;
    if (HttpUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }
    boolean keepAlive = HttpUtil.isKeepAlive(req);
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(HELLO_WORLD));
    response.headers().set(CONTENT_TYPE, "text/plain");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    if (!keepAlive) {
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(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)

Example 43 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project wso2-synapse by wso2.

the class HttpResponseProcessor method writeResponse.

private boolean writeResponse(HttpRequestContext requestContext, OutgoingMessage outgoing, ChannelHandlerContext ctx) {
    boolean keepAlive = requestContext.isKeepAlive();
    HttpVersion httpVersion = consumerContext.getHttpVersion();
    HttpResponseStatus httpResponseStatus = outgoing.getStatusCode();
    FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, httpResponseStatus, Unpooled.copiedBuffer(outgoing.getBody(), CharsetUtil.UTF_8));
    populateHttpHeaders(response, outgoing);
    populateCookies(response, outgoing);
    if (!keepAlive) {
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        ctx.write(response);
    }
    return keepAlive;
}
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) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 44 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project wso2-synapse by wso2.

the class HttpResponseProcessor method write404NotFoundResponse.

private boolean write404NotFoundResponse(HttpRequestContext requestContext, ChannelHandlerContext ctx) {
    boolean keepAlive = requestContext.isKeepAlive();
    HttpVersion httpVersion = consumerContext.getHttpVersion();
    FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, NOT_FOUND);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (!keepAlive) {
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        ctx.write(response);
    }
    return keepAlive;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 45 with FullHttpResponse

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

the class HttpClientOperations method onInboundNext.

@Override
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        if (response.decoderResult().isFailure()) {
            onInboundError(response.decoderResult().cause());
            ReferenceCountUtil.release(msg);
            return;
        }
        if (HttpResponseStatus.CONTINUE.equals(response.status())) {
            is100Continue = true;
            ReferenceCountUtil.release(msg);
            return;
        }
        if (started) {
            if (log.isDebugEnabled()) {
                log.debug(format(channel(), "HttpClientOperations cannot proceed more than one response {}"), response.headers().toString());
            }
            ReferenceCountUtil.release(msg);
            return;
        }
        is100Continue = false;
        started = true;
        setNettyResponse(response);
        if (!isKeepAlive()) {
            markPersistent(false);
        }
        if (isInboundCancelled()) {
            ReferenceCountUtil.release(msg);
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "Received response (auto-read:{}) : {}"), channel().config().isAutoRead(), responseHeaders().entries().toString());
        }
        if (notRedirected(response)) {
            try {
                listener().onStateChange(this, HttpClientState.RESPONSE_RECEIVED);
            } catch (Exception e) {
                onInboundError(e);
                ReferenceCountUtil.release(msg);
                return;
            }
        } else {
            // when redirecting no need of manual reading
            channel().config().setAutoRead(true);
        }
        if (msg instanceof FullHttpResponse) {
            FullHttpResponse request = (FullHttpResponse) msg;
            if (request.content().readableBytes() > 0) {
                super.onInboundNext(ctx, msg);
            } else {
                request.release();
            }
            terminate();
        }
        return;
    }
    if (msg instanceof LastHttpContent) {
        if (is100Continue) {
            ReferenceCountUtil.release(msg);
            channel().read();
            return;
        }
        if (!started) {
            if (log.isDebugEnabled()) {
                log.debug(format(channel(), "HttpClientOperations received an incorrect end " + "delimiter (previously used connection?)"));
            }
            ReferenceCountUtil.release(msg);
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "Received last HTTP packet"));
        }
        if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
            if (redirecting != null) {
                ReferenceCountUtil.release(msg);
            } else {
                super.onInboundNext(ctx, msg);
            }
        }
        if (redirecting == null) {
            // EmitResult is ignored as it is guaranteed that there will be only one emission of LastHttpContent
            // Whether there are subscribers or the subscriber cancels is not of interest
            // Evaluated EmitResult: FAIL_TERMINATED, FAIL_OVERFLOW, FAIL_CANCELLED, FAIL_NON_SERIALIZED
            // FAIL_ZERO_SUBSCRIBER
            trailerHeaders.tryEmitValue(((LastHttpContent) msg).trailingHeaders());
        }
        // force auto read to enable more accurate close selection now inbound is done
        channel().config().setAutoRead(true);
        if (markSentBody()) {
            markPersistent(false);
        }
        terminate();
        return;
    }
    if (!started) {
        if (log.isDebugEnabled()) {
            if (msg instanceof ByteBufHolder) {
                msg = ((ByteBufHolder) msg).content();
            }
            log.debug(format(channel(), "HttpClientOperations received an incorrect chunk {} " + "(previously used connection?)"), msg);
        }
        ReferenceCountUtil.release(msg);
        return;
    }
    if (redirecting != null) {
        ReferenceCountUtil.release(msg);
        // when redirecting auto-read is set to true, no need of manual reading
        return;
    }
    super.onInboundNext(ctx, msg);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBufHolder(io.netty.buffer.ByteBufHolder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) AbortedException(reactor.netty.channel.AbortedException) URISyntaxException(java.net.URISyntaxException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)261 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19