use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttps10ConnectionClose.
@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttps10ConnectionClose() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
try {
final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
cached.subscribe();
// server side recv req
final HttpTrade trade = trades.take();
// recv all request
trade.inbound().toCompletable().await();
final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
// for HTTP 1.0 Connection: Close response behavior
final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
// missing Content-Length
// response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
trade.outbound(Observable.just(fullrespfromsvr));
// wait for recv all resp at client side
cached.toCompletable().await();
svrRespContent.release();
assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
}
});
} finally {
assertEquals(0, allActiveAllocationsCount(allocator));
client.close();
server.unsubscribe();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project jocean-http by isdom.
the class DefaultSignalClientTestCase method buildResponse.
private static Observable<HttpObject> buildResponse(final Object responseBean, final Action1<Action0> onTerminate) {
final byte[] responseBytes = JSON.toJSONBytes(responseBean);
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(responseBytes));
onTerminate.call(new Action0() {
@Override
public void call() {
final boolean released = response.release();
if (LOG.isDebugEnabled()) {
LOG.debug("buildBytesResponse release {} released({})", response, released);
}
}
});
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return Observable.<HttpObject>just(response);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project jocean-http by isdom.
the class DefaultSignalClientTestCase method buildBytesResponse.
private static Observable<HttpObject> buildBytesResponse(final byte[] bodyAsBytes, final Action1<Action0> onTerminate) {
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(bodyAsBytes));
onTerminate.call(new Action0() {
@Override
public void call() {
final boolean released = response.release();
if (LOG.isDebugEnabled()) {
LOG.debug("buildBytesResponse release {} released({})", response, released);
}
}
});
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return Observable.<HttpObject>just(response);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project jocean-http by isdom.
the class NettysTestCase method test_httpobjs2fullresp_success2.
@Test
public final void test_httpobjs2fullresp_success2() throws Exception {
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Nettys4Test.buildByteBuf(REQ_CONTENT));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
assertEquals(1, response.refCnt());
final FullHttpResponse fullresp = Nettys.httpobjs2fullresp(Arrays.<HttpObject>asList(response));
assertNotNull(fullresp);
assertEquals(REQ_CONTENT, new String(Nettys.dumpByteBufAsBytes(fullresp.content()), Charsets.UTF_8));
assertEquals(2, response.refCnt());
fullresp.release();
assertEquals(1, response.refCnt());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project jocean-http by isdom.
the class HttpServerDemo method main.
public static void main(final String[] args) throws Exception {
SelfSignedCertificate ssc = new SelfSignedCertificate();
final // SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
// create for LocalChannel
@SuppressWarnings("resource") final HttpServerBuilder server = new DefaultHttpServerBuilder(new AbstractBootstrapCreator(new DefaultEventLoopGroup(1), new DefaultEventLoopGroup()) {
@Override
protected void initializeBootstrap(final ServerBootstrap bootstrap) {
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
bootstrap.channel(LocalServerChannel.class);
}
}, Feature.ENABLE_LOGGING, new Feature.ENABLE_SSL(sslCtx));
@SuppressWarnings("unused") final Subscription subscription = server.defineServer(new LocalAddress("test")).subscribe(new Action1<HttpTrade>() {
@Override
public void call(final HttpTrade trade) {
trade.outbound(trade.inbound().compose(RxNettys.message2fullreq(trade)).map(DisposableWrapperUtil.<FullHttpRequest>unwrap()).map(new Func1<FullHttpRequest, HttpObject>() {
@Override
public HttpObject call(final FullHttpRequest fullreq) {
try {
final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(Nettys.dumpByteBufAsBytes(fullreq.content())));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return response;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}));
}
});
@SuppressWarnings("resource") final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING, new Feature.ENABLE_SSL(SslContextBuilder.forClient().build()));
while (true) {
final ByteBuf content = Unpooled.buffer(0);
content.writeBytes("test content".getBytes("UTF-8"));
final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
HttpUtil.setContentLength(request, content.readableBytes());
/* // TODO using initiator
final Iterator<HttpObject> itr =
client.defineInteraction(
new LocalAddress("test"),
Observable.just(request))
.map(RxNettys.<HttpObject>retainer())
.toBlocking().toIterable().iterator();
final byte[] bytes = RxNettys.httpObjectsAsBytes(itr);
LOG.info("recv Response: {}", new String(bytes, "UTF-8"));
*/
Thread.sleep(1000);
}
}
Aggregations