use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.
the class DefaultSignalClient method assembleOutgoing.
private Outgoing assembleOutgoing(final HttpRequest request, final BodyForm body, final Attachment[] attachments) throws Exception {
if (0 == attachments.length) {
final LastHttpContent lastContent = buildLastContent(request, body);
return new Outgoing(Observable.<Object>just(request, lastContent), lastContent.content().readableBytes(), new Action0() {
@Override
public void call() {
ReferenceCountUtil.release(request);
ReferenceCountUtil.release(lastContent);
}
});
} else {
// Use the PostBody encoder
final HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(_DATA_FACTORY, request, true, CharsetUtil.UTF_8, // true => multipart
EncoderMode.HTML5);
final long signalSize = addSignalToMultipart(postRequestEncoder, body);
final long attachmentSize = addAttachmentsToMultipart(postRequestEncoder, attachments);
final long total = signalSize + attachmentSize;
// finalize request
final HttpRequest request4send = postRequestEncoder.finalizeRequest();
final Action0 toRelease = new Action0() {
@Override
public void call() {
ReferenceCountUtil.release(request4send);
RxNettys.releaseObjects(postRequestEncoder.getBodyListAttributes());
}
};
return postRequestEncoder.isChunked() ? new Outgoing(Observable.<Object>just(request4send, postRequestEncoder), total, toRelease) : new Outgoing(Observable.<Object>just(request4send), total, toRelease);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpOperationsTest method httpAndJsonDecoders.
@Test
public void httpAndJsonDecoders() {
EmbeddedChannel channel = new EmbeddedChannel();
NettyContext testContext = () -> channel;
ChannelHandler handler = new JsonObjectDecoder(true);
testContext.addHandlerLast("foo", handler);
HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
String json1 = "[{\"some\": 1} , {\"valu";
String json2 = "e\": true, \"test\": 1}]";
Object[] content = new Object[3];
content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
channel.writeInbound(content);
Object t = channel.readInbound();
assertThat(t, instanceOf(HttpResponse.class));
assertThat(t, not(instanceOf(HttpContent.class)));
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}"));
((ByteBuf) t).release();
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}"));
((ByteBuf) t).release();
t = channel.readInbound();
assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
((LastHttpContent) t).release();
t = channel.readInbound();
assertThat(t, nullValue());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addNamedDecoderReplaysLastHttp.
@Test
public void addNamedDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addNamedEncoderReplaysLastHttp.
@Test
public void addNamedEncoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpServerHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// modify message on way out to add headers if needed
if (msg instanceof HttpResponse) {
final HttpResponse response = (HttpResponse) msg;
trackResponse(response);
// Assume the response writer knows if they can persist or not and sets isKeepAlive on the response
if (!isKeepAlive(response) || !isSelfDefinedMessageLength(response)) {
// No longer keep alive as the client can't tell when the message is done unless we close connection
pendingResponses = 0;
persistentConnection = false;
}
// Server might think it can keep connection alive, but we should fix response header if we know better
if (!shouldKeepAlive()) {
setKeepAlive(response, false);
}
if (response.status().equals(HttpResponseStatus.CONTINUE)) {
ctx.write(msg, promise);
return;
}
}
if (msg instanceof LastHttpContent) {
if (!shouldKeepAlive()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug("Detected non persistent http " + "connection," + " " + "preparing to close", pendingResponses);
}
promise.addListener(ChannelFutureListener.CLOSE);
ctx.write(msg, promise);
return;
}
ctx.write(msg, promise).addListener(new TerminateHttpHandler(ctx.channel()));
if (!persistentConnection) {
return;
}
if (mustRecycleEncoder) {
mustRecycleEncoder = false;
pendingResponses -= 1;
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug("Decreasing pending responses, now " + "{}", pendingResponses);
}
}
if (pipelined != null && !pipelined.isEmpty()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug("draining next pipelined " + "request," + " pending response count: {}, queued: " + "{}", pendingResponses, pipelined.size());
}
ctx.executor().execute(this);
} else {
ctx.read();
}
return;
}
ctx.write(msg, promise);
}
Aggregations