Updated Branches:
  refs/heads/master 59ff5faad -> 526222842

CAMEL-6599: camel-netty-http - Must include HOST header in client and server 
must validate HOST header exists


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/52622284
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/52622284
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/52622284

Branch: refs/heads/master
Commit: 526222842026a73a81eedc0cf6794be411adacb8
Parents: 59ff5fa
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Aug 1 14:57:21 2013 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Aug 1 14:57:21 2013 +0200

----------------------------------------------------------------------
 .../component/netty/http/DefaultNettyHttpBinding.java    |  8 ++++++++
 .../camel/component/netty/http/NettyHttpProducer.java    |  5 +++++
 .../netty/http/handlers/HttpServerChannelHandler.java    | 11 +++++++++++
 .../camel/component/netty/http/NettyHttpHeadersTest.java |  1 +
 4 files changed, 25 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/52622284/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
index 64f5b58..e464f0b 100644
--- 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
+++ 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
@@ -21,6 +21,7 @@ import java.io.ObjectOutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.net.URI;
+import java.net.URL;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
 import java.util.Iterator;
@@ -464,6 +465,13 @@ public class DefaultNettyHttpBinding implements 
NettyHttpBinding {
             LOG.trace("Content-Type: {}", contentType);
         }
 
+        // must include HOST header as required by HTTP 1.1
+        // use URI as its faster than URL (no DNS lookup)
+        URI u = new URI(uri);
+        String host = u.getHost();
+        request.setHeader(HttpHeaders.Names.HOST, host);
+        LOG.trace("Host: {}", host);
+
         // configure connection to accordingly to keep alive configuration
         // favor using the header from the message
         String connection = message.getHeader(HttpHeaders.Names.CONNECTION, 
String.class);

http://git-wip-us.apache.org/repos/asf/camel/blob/52622284/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
index 7a7ceda..261c6f9 100644
--- 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
+++ 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
@@ -62,6 +62,11 @@ public class NettyHttpProducer extends NettyProducer {
         String actualUri = request.getUri();
         exchange.getIn().setHeader(Exchange.HTTP_URL, actualUri);
 
+        if (getConfiguration().isBridgeEndpoint()) {
+            // Need to remove the Host key as it should be not used when 
bridging/proxying
+            exchange.getIn().removeHeader("host");
+        }
+
         return request;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/52622284/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
----------------------------------------------------------------------
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 37c60a1..12c1083 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
@@ -43,6 +43,7 @@ import org.jboss.netty.channel.ExceptionEvent;
 import org.jboss.netty.channel.MessageEvent;
 import org.jboss.netty.handler.codec.base64.Base64;
 import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
+import org.jboss.netty.handler.codec.http.HttpHeaders;
 import org.jboss.netty.handler.codec.http.HttpRequest;
 import org.jboss.netty.handler.codec.http.HttpResponse;
 import org.slf4j.Logger;
@@ -50,6 +51,7 @@ import org.slf4j.LoggerFactory;
 
 import static 
org.jboss.netty.handler.codec.http.HttpHeaders.is100ContinueExpected;
 import static org.jboss.netty.handler.codec.http.HttpHeaders.isKeepAlive;
+import static 
org.jboss.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
 import static org.jboss.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
 import static 
org.jboss.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
 import static 
org.jboss.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE;
@@ -113,6 +115,15 @@ public class HttpServerChannelHandler extends 
ServerChannelHandler {
             messageEvent.getChannel().write(response);
             return;
         }
+        // must include HOST header as required by HTTP 1.1
+        if (!request.getHeaderNames().contains(HttpHeaders.Names.HOST)) {
+            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, 
BAD_REQUEST);
+            response.setHeader(Exchange.CONTENT_TYPE, "text/plain");
+            response.setHeader(Exchange.CONTENT_LENGTH, 0);
+            response.setContent(ChannelBuffers.copiedBuffer(new byte[]{}));
+            messageEvent.getChannel().write(response);
+            return;
+        }
 
         // is basic auth configured
         NettyHttpSecurityConfiguration security = 
consumer.getEndpoint().getSecurityConfiguration();

http://git-wip-us.apache.org/repos/asf/camel/blob/52622284/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java
index e452346..27671b5 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java
@@ -26,6 +26,7 @@ public class NettyHttpHeadersTest extends BaseNettyTest {
     public void testHttpHeaders() throws Exception {
         getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
         getMockEndpoint("mock:input").expectedHeaderReceived("beer", "yes");
+        getMockEndpoint("mock:input").expectedHeaderReceived("host", 
"localhost");
         
getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, 
"POST");
         
getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URL, 
"http://localhost:"; + getPort() + "/foo");
         
getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URI, "/foo");

Reply via email to