You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/11/22 08:37:30 UTC

[camel] branch master updated: CAMEL-14195: Fixed CS. If error decoding the request by netty server then return 400 status.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 08c193d  CAMEL-14195: Fixed CS. If error decoding the request by netty server then return 400 status.
08c193d is described below

commit 08c193d8bee5cdb7c95d133415bf96937bb20a2e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Nov 22 09:22:00 2019 +0100

    CAMEL-14195: Fixed CS. If error decoding the request by netty server then return 400 status.
---
 .../src/main/docs/netty-http-component.adoc        |  3 ++-
 .../netty/http/NettyHttpConfiguration.java         | 13 +++++++++++
 .../http/handlers/HttpServerChannelHandler.java    | 18 +++++++++++----
 .../dsl/NettyHttpEndpointBuilderFactory.java       | 26 ++++++++++++++++++++++
 .../NettyHttpComponentConfiguration.java           | 13 +++++++++++
 5 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/components/camel-netty-http/src/main/docs/netty-http-component.adoc b/components/camel-netty-http/src/main/docs/netty-http-component.adoc
index 89486c0..7a5f668 100644
--- a/components/camel-netty-http/src/main/docs/netty-http-component.adoc
+++ b/components/camel-netty-http/src/main/docs/netty-http-component.adoc
@@ -144,7 +144,7 @@ with the following path and query parameters:
 |===
 
 
-=== Query Parameters (79 parameters):
+=== Query Parameters (80 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -167,6 +167,7 @@ with the following path and query parameters:
 | *compression* (consumer) | Allow using gzip/deflate for compression on the Netty HTTP server if the client supports it from the HTTP headers. | false | boolean
 | *disconnectOnNoReply* (consumer) | If sync is enabled then this option dictates NettyConsumer if it should disconnect where there is no reply to send back. | true | boolean
 | *httpMethodRestrict* (consumer) | To disable HTTP methods on the Netty HTTP consumer. You can specify multiple separated by comma. |  | String
+| *logWarnOnBadRequest* (consumer) | Whether Netty HTTP server should log a WARN if decoding the HTTP request failed and a HTTP Status 400 (bad request) is returned. | true | boolean
 | *mapHeaders* (consumer) | If this option is enabled, then during binding from Netty to Camel Message then the headers will be mapped as well (eg added as header to the Camel Message as well). You can turn off this option to disable this. The headers can still be accessed from the org.apache.camel.component.netty.http.NettyHttpMessage message with the method getHttpRequest() that returns the Netty HTTP request io.netty.handler.codec.http.HttpRequest instance. | true | boolean
 | *maxHeaderSize* (consumer) | The maximum length of all headers. If the sum of the length of each header exceeds this value, a io.netty.handler.codec.TooLongFrameException will be raised. | 8192 | int
 | *nettyServerBootstrapFactory* (consumer) | To use a custom NettyServerBootstrapFactory |  | NettyServerBootstrapFactory
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
index 1c6f332..ca80250 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
@@ -55,6 +55,8 @@ public class NettyHttpConfiguration extends NettyConfiguration {
     private boolean muteException;
     @UriParam(label = "consumer")
     private boolean matchOnUriPrefix;
+    @UriParam(label = "consumer,advanced", defaultValue = "true")
+    private boolean logWarnOnBadRequest;
     @UriParam
     private boolean bridgeEndpoint;
     @UriParam(label = "advanced")
@@ -228,6 +230,17 @@ public class NettyHttpConfiguration extends NettyConfiguration {
         this.matchOnUriPrefix = matchOnUriPrefix;
     }
 
+    public boolean isLogWarnOnBadRequest() {
+        return logWarnOnBadRequest;
+    }
+
+    /**
+     * Whether Netty HTTP server should log a WARN if decoding the HTTP request failed and a HTTP Status 400 (bad request) is returned.
+     */
+    public void setLogWarnOnBadRequest(boolean logWarnOnBadRequest) {
+        this.logWarnOnBadRequest = logWarnOnBadRequest;
+    }
+
     public boolean isBridgeEndpoint() {
         return bridgeEndpoint;
     }
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
index f871c4b..1bc5d92 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
@@ -20,7 +20,6 @@ import java.net.URI;
 import java.nio.channels.ClosedChannelException;
 import java.nio.charset.Charset;
 import java.util.Locale;
-
 import javax.security.auth.Subject;
 import javax.security.auth.login.LoginException;
 
@@ -53,6 +52,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
+import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
 import static io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
 import static io.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE;
 import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;
@@ -89,9 +89,19 @@ public class HttpServerChannelHandler extends ServerChannelHandler {
         LOG.debug("Message received: {}", request);
 
         DecoderResult decoderResult = request.decoderResult();
-
-        if(decoderResult != null  && decoderResult.cause() != null) {
-            LOG.error("Netty Request Decoder Failure: {}", decoderResult.cause().getMessage());
+        if (decoderResult != null && decoderResult.cause() != null) {
+            if (getConsumer().getConfiguration().isLogWarnOnBadRequest()) {
+                LOG.warn("Netty request decoder failure due: {} returning HTTP Status 400 to client", decoderResult.cause().getMessage());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Netty request decoder failure (stacktrace)", decoderResult.cause());
+                }
+            }
+            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, BAD_REQUEST);
+            response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
+            response.headers().set(Exchange.CONTENT_LENGTH, 0);
+            ctx.writeAndFlush(response);
+            ctx.channel().close();
+            return;
         }
 
         if (consumer.isSuspended()) {
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/NettyHttpEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/NettyHttpEndpointBuilderFactory.java
index a0067f5..13cfe6b 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/NettyHttpEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/NettyHttpEndpointBuilderFactory.java
@@ -916,6 +916,32 @@ public interface NettyHttpEndpointBuilderFactory {
             return this;
         }
         /**
+         * Whether Netty HTTP server should log a WARN if decoding the HTTP
+         * request failed and a HTTP Status 400 (bad request) is returned.
+         * 
+         * The option is a: <code>boolean</code> type.
+         * 
+         * Group: consumer (advanced)
+         */
+        default AdvancedNettyHttpEndpointConsumerBuilder logWarnOnBadRequest(
+                boolean logWarnOnBadRequest) {
+            doSetProperty("logWarnOnBadRequest", logWarnOnBadRequest);
+            return this;
+        }
+        /**
+         * Whether Netty HTTP server should log a WARN if decoding the HTTP
+         * request failed and a HTTP Status 400 (bad request) is returned.
+         * 
+         * The option will be converted to a <code>boolean</code> type.
+         * 
+         * Group: consumer (advanced)
+         */
+        default AdvancedNettyHttpEndpointConsumerBuilder logWarnOnBadRequest(
+                String logWarnOnBadRequest) {
+            doSetProperty("logWarnOnBadRequest", logWarnOnBadRequest);
+            return this;
+        }
+        /**
          * If this option is enabled, then during binding from Netty to Camel
          * Message then the headers will be mapped as well (eg added as header
          * to the Camel Message as well). You can turn off this option to
diff --git a/platforms/spring-boot/components-starter/camel-netty-http-starter/src/main/java/org/apache/camel/component/netty/http/springboot/NettyHttpComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-netty-http-starter/src/main/java/org/apache/camel/component/netty/http/springboot/NettyHttpComponentConfiguration.java
index 5338bf9..c5f9743 100644
--- a/platforms/spring-boot/components-starter/camel-netty-http-starter/src/main/java/org/apache/camel/component/netty/http/springboot/NettyHttpComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-netty-http-starter/src/main/java/org/apache/camel/component/netty/http/springboot/NettyHttpComponentConfiguration.java
@@ -266,6 +266,11 @@ public class NettyHttpComponentConfiguration
          */
         private Boolean matchOnUriPrefix = false;
         /**
+         * Whether Netty HTTP server should log a WARN if decoding the HTTP
+         * request failed and a HTTP Status 400 (bad request) is returned.
+         */
+        private Boolean logWarnOnBadRequest = true;
+        /**
          * If the option is true, the producer will ignore the Exchange.HTTP_URI
          * header, and use the endpoint's URI for request. You may also set the
          * throwExceptionOnFailure to be false to let the producer send all the
@@ -408,6 +413,14 @@ public class NettyHttpComponentConfiguration
             this.matchOnUriPrefix = matchOnUriPrefix;
         }
 
+        public Boolean getLogWarnOnBadRequest() {
+            return logWarnOnBadRequest;
+        }
+
+        public void setLogWarnOnBadRequest(Boolean logWarnOnBadRequest) {
+            this.logWarnOnBadRequest = logWarnOnBadRequest;
+        }
+
         public Boolean getBridgeEndpoint() {
             return bridgeEndpoint;
         }