Chunked request to undertow server causes hanging state

2k views Asked by At

I have an Undertow server and am running the defaults. I am attempting to send a chunked request and while the server to receive the chunks, the input stream server side never reaches a -1 state and hangs.

I have used the defaults for the Undertow server (I had tried increasing the number of threads but still no luck).

Undertow server = Undertow.builder()
    .addHttpListener(8080, "localhost")
    .setHandler(new HttpHandler() {
        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
            exchange.getResponseSender().send("Hello World");
        }
    })
    .setHandler(Handlers.routing().post("/xml", blockingHandler))
    .build();
server.start();

At first I was trying to use an ordinary httpHandler but that threw an error.

java.lang.IllegalStateException: UT000035: Cannot get stream as startBlocking has not been invoked

After some research I then used the BlockingHandler following advice found on stackoverflow. I no longer received the error above but now the handler hangs.

Here is the code for the handler (I have removed the exception handling for the sake of clarity):

HttpHandler httpHandler = new HttpHandler() {
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {

        StringBuilder builder = new StringBuilder( );

        try (InputStream inputStream = exchange.getInputStream()) {
            int line;
            while ((line = inputStream.read()) != -1) {
                builder.append(line);
                System.out.print((char)line);
            }
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
            exchange.getResponseSender().send("Chunks Posted");
        }
    }

I am the sending the request using postman as follows:

POST /xml HTTP/1.1
Host: localhost:8080
Transfer-Encoding: chunked
Cache-Control: no-cache
Postman-Token: c67e61c0-ce64-4490-9add-f8c37b128fe4

5<CR><LF>
aaaaa<CR><LF>
5<CR><LF>
bbbbb<CR><LF>
0<CR><LF><CR><LF>

When the request is sent with the above body, the data is read in fine but it appears that the inputStream is not closing, causing the hang. If I do not send with the chunked header then the stream behaves as expected.

The closest issue I could find was: https://issues.jboss.org/browse/WFLY-6671

but that could not be reproduced and the ticket was closed. I have tried to increase the number of worker io threads as the undertow documentation states it defaults to 1 which may cause a hang on other connections but still no luck.

I am new to Undertow and chunking but essentially what I am trying to create is a server that can handle a high volume of XML requests that are received in chunks and output them to a pipe, perhaps Kafka or DB like Mongo.

Any help would would be great as I cannot see why it would hang and do not know enough about the undertow wrapper streams. I thought that maybe the 0 read in may signify the end of the stream rather than the usual -1 but from what I can see that is not the case.

UPDATE: If I attempt to send the request via cURL with a -H "Transfer-Encoding: chunked" flag then there is no hanging, however the '' tags are being read. Attempted to escape the characters as well as removing them but the 0 that signifies end of chunk is being read as a char.

0

There are 0 answers