vert-x3 / vertx-web

HTTP web applications for Vert.x
Apache License 2.0
1.11k stars 535 forks source link

absoluteURI is null when host header is missing #2596

Closed rmartinc closed 6 months ago

rmartinc commented 7 months ago

Version

4.5.7 but it's in master too.

Context

There is very weird behavior in HttpServerrequest between vert.x and vertx-web. In vert.x when the host header is missing it returns null for authority() but returns an URI in absoluteURI(). Project vert.x executes some code to return the absolute URI based on the server origin. Nevertheless vertx-web returns null in both methods. Reading the javadoc for HttpServerRequest I think that authority can be null but absoluteURI not. I'm not happy with this because I don't understand that absoluteURI has an authority filled but the specific authority method returns null, but OK, I accept it. But then can vertx-web do the same?

I added this little diff to do the same in vertx-web too. I'm assigning initially the aboluteURI to the one returned by vert.x and modifying the testMissingHostHeader to assert is not null.

diff --git a/vertx-web/src/main/java/io/vertx/ext/web/impl/ForwardedParser.java b/vertx-web/src/main/java/io/vertx/ext/web/impl/ForwardedParser.java
index 557d6225e..53007ef51 100644
--- a/vertx-web/src/main/java/io/vertx/ext/web/impl/ForwardedParser.java
+++ b/vertx-web/src/main/java/io/vertx/ext/web/impl/ForwardedParser.java
@@ -106,6 +106,7 @@ class ForwardedParser {
     remoteAddress = delegate.remoteAddress();
     scheme = delegate.scheme();
     setHostAndPort(delegate.authority());
+    absoluteURI = delegate.absoluteURI();

     switch (allowForward) {
       case X_FORWARD:
diff --git a/vertx-web/src/test/java/io/vertx/ext/web/ForwardedTest.java b/vertx-web/src/test/java/io/vertx/ext/web/ForwardedTest.java
index 2ed5e479b..5bf378a4c 100644
--- a/vertx-web/src/test/java/io/vertx/ext/web/ForwardedTest.java
+++ b/vertx-web/src/test/java/io/vertx/ext/web/ForwardedTest.java
@@ -433,6 +433,7 @@ public class ForwardedTest extends WebTestBase {

     route.handler(rc -> {
       assertNull(rc.request().authority());
+      assertNotNull(rc.request().absoluteURI());
       rc.end();
     });

Do you have a reproducer?

You can execute your own test ForwardedTest#testMissingHostHeader you will see that authority() is null (as in vert.x) but absolutyURI() is also null (while in vert.x is http://localhost:8080/).

rmartinc commented 7 months ago

I have asked also about the authority and absoluteURI discrepancy in the vert.x google groups: https://groups.google.com/g/vertx/c/OBffuFH-WZU

vietj commented 6 months ago

in vertx 4.5.8 vertx-web will refuse HTTP requests without an host header since those are not valid according to HTTP