allthingssecurity opened a new pull request, #30526:
URL: https://github.com/apache/camel/pull/30526

   # Description
   
   [CAMEL-27265](https://issues.apache.org/jira/browse/CAMEL-27265)
   
   A request with a chunked body (`Transfer-Encoding: chunked`, as HTTP clients 
send a body of unknown length) gets no response in three setups of 
`camel-netty-http`:
   
   1. **Shared HTTP server** (`nettySharedHttpServer`, default `chunked=true`). 
The shared pipeline adds `HttpInboundStreamHandler`, which passes the request 
on with an `InputStream` of the body and writes the chunks into a 
`PipedOutputStream`. The routes of the shared server run on the I/O thread, so 
that thread both writes and reads the pipe: a route that reads the body waits 
for ever, and the other connections served by that I/O thread get no response 
either. A route that does not read the body hangs the same way (stream caching 
reads it first; with `noStreamCaching()`, as soon as the body is larger than 
the 1 KB pipe buffer). So does a request to a path that no route matches: the 
404 is sent, then the I/O thread waits to write the rest of a body larger than 
1 KB into the pipe.
   2. **Shared HTTP server with `chunked=false`.** The pipeline then has no 
`HttpObjectAggregator` (and no outbound streamer), so the handlers, which 
expect a full or a streamed request, get an `HttpRequest` followed by 
`HttpContent` messages and fail on them (for example `ClassCastException: 
DefaultHttpContent cannot be cast to InboundStreamHttpRequest`): the connection 
is closed without a response for every request, also a `GET`.
   3. **Server of an endpoint with `disableStreamCache=true`.** The same 
handler streams the body. The route runs on the executor service by default, so 
reading works, but: when the route does not read the body (or no route matches 
the path), the I/O thread waits for ever to write more than 1 KB into the pipe, 
so the response is never written and the other connections of that I/O thread 
are not served; with `usingExecutorService=false` a route that reads the body 
waits for ever; and the pipe is created once per connection and closed when the 
first body has been read, so a second chunked request on a kept-alive 
connection fails with `IOException: Pipe closed` (500).
   
   Fix:
   
   - `HttpServerSharedInitializerFactory` always adds the aggregator and the 
outbound streamer, and no longer adds the inbound streamer: each request, also 
a chunked one, is aggregated before the route is called, as on the server of an 
endpoint with the default options, and streamed responses still work. The 
`chunked` option is no longer used (javadoc and the component page say so); 
`chunked=false` now behaves like the default. The shared server is deprecated 
(CAMEL-27238); this keeps it working until it is removed.
   - `HttpServerInitializerFactory` adds the inbound streamer only when the 
route runs on the executor service (`disableStreamCache=true` and 
`usingExecutorService=true`, the default). With `usingExecutorService=false` a 
chunked request is aggregated, and the body is the Netty `ByteBuf`, as for a 
request with a `Content-Length`.
   - `HttpInboundStreamHandler` no longer uses 
`PipedInputStream`/`PipedOutputStream`. Each message gets its own stream, which 
keeps the received chunks (copied, so nothing is held from the Netty buffers). 
The I/O thread never waits for the reader: when 64 KB of the body wait to be 
read, the handler turns `autoRead` off for the connection, and the reader turns 
it on again once it has read down to 32 KB. Closing the stream discards what 
was received and what is still to come, and turns `autoRead` on, so the rest of 
the body is read and dropped and the connection serves the next request. A 
connection closed before the end of the body, or an invalid chunk, makes the 
stream fail with an `IOException` (before, the stream just ended). Reading the 
stream on the I/O thread of its own connection with no data received yet fails 
with an `IOException`, as that thread could not receive the data it waits for.
   - `HttpServerChannelHandler` closes the stream of a chunked request when the 
exchange is done (an on completion), as the `disableStreamCache` option doc 
already says Netty does. The exception is a streamed reply (an 
`OutboundStreamHttpResponse`, built when the body of the reply is an 
`InputStream`): the outbound streamer writes it after the exchange is done, and 
it may read the request stream, as a route that sends the body back does, as it 
is (CAMEL-26137) or in another stream (for example a `BufferedInputStream` 
around it). `getResponseBody` passes such a reply on in a small private 
subclass, `StreamedReply`, which takes over the request stream from the on 
completion and closes it when the reply is closed; the outbound streamer 
(`ChunkedWriteHandler`) closes the reply once written, when the write fails, or 
when the connection is closed. A route that does not read the body and replies 
with a stream of its own gets the rest of the body discarded once the reply is 
written. As that 
 rest is read only once the reply is written, a reply larger than what the 
connection buffers and a client that sends the whole body before it reads the 
response wait for each other; closing the body stream in the route, or a reply 
that is not a stream, avoids it, and the upgrade guide says so (checked by hand 
with a 4 MB body and an 8 MB streamed reply: on main that request, and the same 
one with a 1-byte streamed reply or an 8 MB reply that is not a stream, got no 
response at all; with the change only that one case waits, the others are 
answered and the next request on the connection too). When the route fails, the 
reply is the stack trace, not a stream, so the stream is closed when the 
exchange is done, also when the body is still the stream.
   - `HttpOutboundStreamHandler` closes the connection when the body of a 
streamed message fails to be read or written. The headers are sent before the 
body, so the peer otherwise waits for the rest of a message that never comes. A 
reply that sends the request stream back reads it on the I/O thread of the 
connection, so it can only send back a body that is received already; when a 
part of it is still to come, the stream fails with an `IOException` and the 
connection is now closed (on main the I/O thread waited for ever there).
   
   Not changed: with `disableStreamCache=true` the route still caches the 
stream first when stream caching is on for the route (the default), as before; 
the tests that need the raw stream use `noStreamCaching()`.
   
   The producer uses the same handler for a chunked response with 
`disableStreamCache=true`; it gets the same stream (one per response, no 
waiting on the I/O thread). A route that reads such a response on the I/O 
thread (right after an asynchronous producer) waited for ever before and now 
fails with an `IOException`; it works as before with `synchronous=true` or on 
another thread. Releasing a response stream that the route does not read on the 
producer side (when the channel goes back to the pool) is not changed here; I 
can file a follow-up JIRA for it.
   
   Docs: the stream note on the component page (with the streamed reply), and 
the shared server section. Upgrade guide: a 4.24 entry (`camel-netty-http - 
chunked requests on the shared HTTP server and with disableStreamCache`), as 
the body of a chunked request on the shared server is no longer an 
`InputStream`, `usingExecutorService=false` aggregates (both limited by 
`chunkedMaxContentLength`, 1 MB by default, 413 above it), and the stream is 
closed when the exchange is done, or once written for a streamed reply.
   
   Tests (the servers have one I/O thread, `workerCount=1`, so a request that 
is not answered also holds the next one; client sockets time out after 5 
seconds, and each class has a JUnit `@Timeout`):
   
   - `NettySharedHttpServerChunkedRequestTest`: a chunked request to a route 
that reads the body (the route gets "Hello World"), to a route with 
`noStreamCaching()` that does not read a 4 KB body, and to an unknown path 
(404), each followed by a `GET` on a new connection; plus two controls: a 
request with a `Content-Length`, and a streamed response (`InputStream` body 
with `disableStreamCache=true`, sent chunked).
   - `NettySharedHttpServerNotChunkedTest`: the same tests with `chunked=false`.
   - `NettyHttpChunkedRequestDisableStreamCacheTest` (server of an endpoint, 
`disableStreamCache=true`): two chunked requests of 200 KB on one connection to 
a route that reads the raw stream (not a stream cache) and answers its length; 
a 200 KB body that the route does not read, then a `GET` on the same connection 
and on a new one; a route that fails without reading a 200 KB body (500), then 
a `GET` on the same connection; two chunked requests on one connection to a 
route that sends the body back, and two to a route that sends it back in a 
`BufferedInputStream`; a streamed reply to a 200 KB body that the route does 
not read, then a `GET` on the same connection; a request to the route that 
sends the body back with only the first chunk sent, where the connection is 
closed; an unknown path (404); and a route with `usingExecutorService=false` 
that reads the body.
   - `HttpInboundStreamHandlerTest` (`EmbeddedChannel`): the body is streamed 
(control); `autoRead` is off above the high water mark and on again once read 
down to the low one; closing discards the rest and the next request gets its 
own stream; reading on the I/O thread with no data fails; a connection closed 
before the end fails the stream after the data received.
   
   On main 21 of the 24 tests fail (the three controls pass): the shared server 
and the endpoint server tests get no response within 5 seconds (or a 500 for 
the second request on a connection), `chunked=false` closes the connection 
without a response on every request, and three handler tests time out after 30 
seconds (the fourth gets `Pipe closed`). The first request sent back, as it is 
or in a `BufferedInputStream`, works on main, the second one on the same 
connection gets no complete response; a streamed reply to a body that the route 
does not read gets no response. With the change all 24 pass, and the 
camel-netty-http tests pass with `-Dio.netty.leakDetection.level=paranoid` (407 
run, 0 failures, 10 skipped, no leak detected; 383 before plus the 24 new), 
among them `NettyHttpReplyBodyTest` (CAMEL-26137) and the other tests of 
`HttpServerChannelHandler` and `HttpInboundStreamHandler`. The four new classes 
10 times in one JVM, also with paranoid leak detection: 240 run, 0 failures, 
 no leak detected.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27265](https://issues.apache.org/jira/browse/CAMEL-27265).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built the touched modules with the formatter and import-sort plugins; 
the component page changed, so its catalog copy is in the commit; no option 
metadata changed. I did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to