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);
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations