Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http client Type of JDK gives "empty headers are not allowed" #3525

Open
wimdeblauwe-swmu opened this issue Sep 16, 2024 · 7 comments
Open

Comments

@wimdeblauwe-swmu
Copy link

Describe the bug
I started out with Spring Cloud Gateway today and I had trouble to even try the simplest example. It seems that the default http client type (which is jdk) is not working. I had to set it to autodetect to get it working.

Sample

  • Create a project on start.spring.io and select "Gateway". I also used Maven, Java 21 and Spring Boot 3.3.3.
  • Update the main class to have a simple router function:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.stripPrefix;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@SpringBootApplication
public class GatewayTestApplication {

	public static void main(String[] args) {
		SpringApplication.run(GatewayTestApplication.class, args);
	}

	@Bean
	public RouterFunction<ServerResponse> routerFunction() {
		return RouterFunctions.route()
				.GET("/httpbin/**", http("https://httpbin.org/"))
				.before(stripPrefix(1))
				.build();
	}
}

Try the request ( I used IntelliJ's http client):

GET http://localhost:8080/httpbin/get
Accept: application/json

It returns:

java.lang.IllegalArgumentException: empty headers are not allowed []

When I update application.properties with this property:

spring.cloud.gateway.mvc.http-client.type=autodetect

Things work and I get a proper response:

{
  "args": {},
  "headers": {
    "Accept": "application/json",
    "Accept-Encoding": "br, deflate, gzip, x-gzip",
    "Forwarded": "proto=http;host=\"localhost:8080\";for=\"127.0.0.1:58743\"",
    "Host": "httpbin.org",
    "User-Agent": "IntelliJ HTTP Client/IntelliJ IDEA 2024.2.1",
    "X-Amzn-Trace-Id": "Root=1-66e82034-6aa870117a60efaa246c19c5",
    "X-Forwarded-Host": "localhost:8080"
  },
  "origin": "127.0.0.1, 94.143.189.242",
  "url": "https://localhost:8080/get"
}
@spencergibb
Copy link
Member

If you'd like us to spend some time investigating, please take the time to provide a complete, minimal, verifiable sample (something that we can unzip attached to this issue or git clone, build, and deploy) that reproduces the problem.

@wimdeblauwe-swmu
Copy link
Author

I have created a project that shows the issue:

issue-3525.zip

@spencergibb
Copy link
Member

I downloaded and ran your project and

❯ http :8080/httpbin/get
HTTP/1.1 200 
Connection: keep-alive
Content-Length: 448
Content-Type: application/json
Keep-Alive: timeout=60
access-control-allow-credentials: true
access-control-allow-origin: *
date: Fri, 27 Sep 2024 15:33:25 GMT
server: gunicorn/19.9.0

{
    "args": {},
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Forwarded": "proto=http;host=\"localhost:8080\";for=\"[0:0:0:0:0:0:0:1]:56855\"",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/3.2.3",
        "X-Amzn-Trace-Id": "Root=1-66f6d044-5ffbb2a642ddfe241694d60e",
        "X-Forwarded-Host": "localhost:8080"
    },
    "origin": "0:0:0:0:0:0:0:1, 172.58.124.44",
    "url": "https://localhost:8080/get"
}

I'm using httpie. curl works as well. Maybe it's the client?

@wimdeblauwe-swmu
Copy link
Author

I can reproduce the behaviour with httpie if I remove the default headers:

http :8080/httpbin/get \
Accept: \
     Accept-Encoding: \
     Connection: \
     Host: \
     User-Agent:
HTTP/1.1 400
Connection: close
Content-Language: en
Content-Length: 435
Content-Type: text/html;charset=utf-8
Date: Mon, 30 Sep 2024 06:25:34 GMT

<!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad Request</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 400 – Bad Request</h1></body></html>

Not sure what IntelliJ's Http Client does exactly.

@wimdeblauwe-swmu
Copy link
Author

Seems with httpie, I get the same 400 Bad Request if I set spring.cloud.gateway.mvc.http-client.type to autodetect. So I don't know if the bad request is expected behaviour?

@spencergibb
Copy link
Member

If I remove gateway completely and just use webmvc.fn the error persists. So, I don't think this has anything to do with gateway.

@wimdeblauwe-swmu
Copy link
Author

I you can provide me with that project and let me know where I can file an issue for webmvc.fn, I can open a new issue for it if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants