Reading a HttpContent that has a PooledUnsafeDirectByteBuf

时间:2017-06-15 10:21:35

标签: netty reactor reactor-netty

I have a Reactor-Netty HttpServer, to which I send data that is more than 1024 bytes from an HttpClient.

In the client I get the different parts of the request in a Flux with the idea of concatenating the data into a String that I can ultimately parse. I was of the opinion that this could have been done by the HttpObjectAggregator, but alas no.

When the client receives the content it comes in as

DefaultHttpContent(data: PooledSlicedByteBuf(ridx: 1024, widx: 1024, cap: 1024/1024, unwrapped: PooledUnsafeDirectByteBuf(ridx: 1024, widx: 1024, cap: 1024)), decoderResult: success)

and

DefaultLastHttpContent(data: PooledSlicedByteBuf(ridx: 98, widx: 98, cap: 98/98, unwrapped: PooledUnsafeDirectByteBuf(ridx: 98, widx: 98, cap: 1024)), decoderResult: success)

If I attempt the 'extract' the bytes I find that they are not available.

Server

    private void startServer() {

    HttpServer server = HttpServer.create(opts -> opts.listen(8092).option(ChannelOption.SO_RCVBUF, 1024 * 1024)
            .option(ChannelOption.SO_SNDBUF, 1024 * 1024).option(ChannelOption.SO_KEEPALIVE, true)
            .afterChannelInit(channelInit -> {
                ChannelPipeline pipeline = channelInit.pipeline();
                pipeline.addLast("aggregator", new HttpObjectAggregator(64 * 1024));
            }));

    Mono<? extends NettyContext> context = server.newRouter(routes -> {
        routes.post("/test", postHandler());
        routes.put("/test", postHandler());
    });

    context.subscribe().block(Duration.ofSeconds(30));

}

BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> postHandler() {
    return (req, resp) -> {
        final String combinedContent = new String();
        req.requestHeaders().entries().forEach(entry -> {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key.equalsIgnoreCase("content-length")) {
                contentLength = Integer.parseInt(value);
            }
            log.debug(String.format("header [%s=>%s]", key, value));
        });

        req.receiveContent().doOnNext(request -> {
            ByteBuf bb = ByteBufUtil.readBytes(request.content().alloc(), request.content(), request.content().readableBytes());
            log.debug("HEX DUMP : " +  ByteBufUtil.prettyHexDump(request.content()));
            combinedContent.concat(new String(ByteBufUtil.getBytes(bb)));
        }).subscribe((what) -> {
            log.debug("Combined Request = " + combinedContent.toString());
            log.debug("what = " + what);
        });


        return Mono.empty();

    };
}

Client

@Test
public void testClientMessage() {
    HttpClient httpClient;
    try {
        httpClient = webClient.connect("localhost", 8092, false);
        webClient.send(httpClient, "/test", getTestRequest());

    } catch (SSLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private String getResponse() {
    return "Well Done";
}

private String getTestRequest() {
    return "moreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytesmoreThan1024bytes";
}

Any idea how I can get the data?

The data is logged to the console eg:

12:08:16.245 [reactor-http-nio-2] DEBUG r.ipc.netty.http.server.HttpServer - [id: 0xf0f21e64, L:/127.0.0.1:8092 - R:/127.0.0.1:52774] RECEIVED: 98B +-------------------------------------------------+ | 0 1 2 3 4 5 6 7 8 9 a b c d e f | +--------+-------------------------------------------------+----------------+ |00000000| 54 68 61 6e 31 30 32 34 62 79 74 65 73 6d 6f 72 |Than1024bytesmor| |00000010| 65 54 68 61 6e 31 30 32 34 62 79 74 65 73 6d 6f |eThan1024bytesmo| |00000020| 72 65 54 68 61 6e 31 30 32 34 62 79 74 65 73 6d |reThan1024bytesm| |00000030| 6f 72 65 54 68 61 6e 31 30 32 34 62 79 74 65 73 |oreThan1024bytes| |00000040| 6d 6f 72 65 54 68 61 6e 31 30 32 34 62 79 74 65 |moreThan1024byte| |00000050| 73 6d 6f 72 65 54 68 61 6e 31 30 32 34 62 79 74 |smoreThan1024byt| |00000060| 65 73 |es | +--------+-------------------------------------------------+----------------+

thanks

0 个答案:

没有答案