spring-cloud/spring-cloud-gateway

Compute forwarded: header correctly when HTTP2 is enabled

Open

#3106 opened on Oct 18, 2023

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Java (4,284 stars) (3,204 forks)batch import
enhancementhelp wanted

Description

Is your feature request related to a problem? Please describe. It is possible to enable HTTP2 support in Spring Boot application like so:

server.http2.enabled=true

with this enabled and with embedded Tomcat running in HTTPS mode (at port 8443), the browsers (like Chrome) use the HTTP2 (shown as h2 in protocol column of Chrome devtools network panel), and due to the specs related to HTTP2 do not send the Host: localhost:8443 header. Instead the browsers send :authority: localhost:8443.

The code here:

https://github.com/spring-cloud/spring-cloud-gateway/blob/51367a8668c686e972f621874ec8e40ae21b56b2/spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/ProxyExchange.java#L418

relies on the Host: header and if not found uses the host and scheme from URI. The port is not set at all.

However there is code in Tomcat that does process the :authority: header and sets the values of req.getServerName() and req.getServerPort().

Describe the solution you'd like The computation of forwarded: header should make use of values of req.getServerName() and req.getServerPort() because they are set from :authority: header, which is equivalent of Host: when HTTP2 is being used.

Describe alternatives you've considered None.

Additional context BTW libraries like HATEOAS rely on Host: header to return correct links in the response.

Contributor guide