Ayush0612005 commented on code in PR #30509:
URL: https://github.com/apache/camel/pull/30509#discussion_r4644833441


##########
components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java:
##########
@@ -311,16 +311,18 @@ protected void readFormUrlEncodedBody(HttpServletRequest 
request, Message messag
                 String text = message.getBody(String.class);
                 if (org.apache.camel.util.ObjectHelper.isNotEmpty(text)) {
                     for (String param : text.split("&")) {
-                        String[] pair = param.split("=", 2);
-                        if (pair.length == 2) {
-                            String name = URLDecoder.decode(pair[0], charset);
-                            String value = URLDecoder.decode(pair[1], charset);
-                            if (headerFilterStrategy != null
-                                    && 
!headerFilterStrategy.applyFilterToExternalHeaders(name, value, 
message.getExchange())) {
-                                HttpHelper.appendHeader(headers, name, value);
-                            }
-                        } else {
-                            throw new IllegalArgumentException("Invalid 
parameter, expected to be a pair but was " + param);
+                        if (param.isEmpty()) {
+                            // an empty field, as in a=1&&b=2, has no name
+                            continue;
+                        }
+                        // a field without = is a name with an empty value, as 
in the URL-encoded form parser
+                        // of the WHATWG URL standard, which is what browsers 
and Vert.x do
+                        int pos = param.indexOf('=');
+                        String name = URLDecoder.decode(pos < 0 ? param : 
param.substring(0, pos), charset);

Review Comment:
   Thanks for the reproducer, you're right: the text this PR lets through still 
failed on a stray `%`, and the upgrade-guide sentence did not hold for it.
   
   Fixed in the latest commit: a new `HttpHelper.decodeFormComponent` 
(camel-http-base) decodes as the WHATWG form parser does, `+` as a space and 
`%XX` as a byte in the charset, and keeps a `%` that is not followed by two hex 
digits. `DefaultHttpBinding` now uses it instead of `URLDecoder`. 
`DefaultNettyHttpBinding` keeps calling the overridable `shouldUrlDecodeHeader` 
and falls back to it when that throws, which covers `urlDecodeHeaders=true`.
   
   Tests: `HttpHelperTest#testDecodeFormComponent` (`Save 50% now`, 
`{"discount":"50%"}`, `100%`, `%zz`, multi-byte escapes, ISO-8859-1), 
`DefaultHttpBindingFormUrlEncodedTest#testPercentNotFollowedByTwoHexDigitsIsKept`,
 and in `NettyHttpForwardFormTextTest` the two bodies from your table, through 
a consumer with `urlDecodeHeaders=true`. These fail on the previous head with 
the `Illegal hex characters` exception. The upgrade-guide entry now mentions 
the `%`.
   
   _Claude Code on behalf of Ayush0612005_
   



##########
components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java:
##########
@@ -568,6 +573,15 @@ public HttpResponse toNettyResponse(Message message, 
NettyHttpConfiguration conf
         // must use entrySet to ensure case of keys is preserved
         for (Map.Entry<String, Object> entry : 
message.getHeaders().entrySet()) {
             String key = entry.getKey();
+            if (!HttpHelper.isValidHeaderName(key)) {

Review Comment:
   Agreed, thanks: skipping the header only in the consumer's response moved 
the failure of a forwarding route to after the route had run.
   
   The netty-http producer (`toNettyRequest`) and the camel-http producer 
(`HttpProducer`) now skip a header whose name is not a token, with the same 
`HttpHelper.isValidHeaderName` and a DEBUG log, before the header filter 
strategy. Other headers are sent as before.
   
   Tests: your reproducer is in as `NettyHttpForwardFormTextTest` (netty-http 
consumer to `to("netty-http:...")`: 200 and the back route runs), and 
`HttpProducerInvalidHeaderNameTest` checks that camel-http sends a valid header 
and not the invalid one. Both fail on the previous head. The upgrade-guide 
entry now covers the producers too.
   
   Other producers that copy message headers (camel-vertx-http, camel-undertow, 
...) are not changed here; I can follow up on them if you'd like.
   
   _Claude Code on behalf of Ayush0612005_
   



-- 
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