Skip to content

Fix response body copy for non-standard charset - #4354

Open
hebulin wants to merge 1 commit into
spring-cloud:mainfrom
hebulin:fix/3695-non-standard-charset
Open

hebulin wants to merge 1 commit into
spring-cloud:mainfrom
hebulin:fix/3695-non-standard-charset

Conversation

@hebulin

@hebulin hebulin commented Nov 2, 2026

Copy link
Copy Markdown

Summary

Fixes #3695 by allowing Spring Cloud Gateway Server WebMVC to proxy responses whose Content-Type contains a non-standard charset such as x-user-defined.

Root Cause

AbstractProxyExchange.copyResponseBody and isStreamingResponse call HttpHeaders.getContentType(). In Spring Framework, that method parses the charset parameter and throws InvalidMediaTypeException for unsupported values. Clients may legitimately send Content-Type: text/plain; charset=x-user-defined when downloading binary data, so the gateway fails with a 500 before the response body is written.

Fix

  • Read the raw Content-Type header and parse only its type and subtype, ignoring parameters.
  • Fall back to null (non-streaming copy) when the type/subtype itself is invalid.
  • Apply the same logic in isStreamingResponse, which is used while closing a response after a copy error.
  • Add regression tests for a non-streaming and a streaming content type with a non-standard charset.

Before-After

Content-Type Before After
text/plain; charset=x-user-defined InvalidMediaTypeException body copied
text/event-stream; charset=x-user-defined InvalidMediaTypeException body streamed with flushing

Backward Compatibility

Valid content types behave as before, because parameters were already ignored by equalsTypeAndSubtype. Missing or malformed types are still treated as non-streaming. Response headers are forwarded unchanged.

Closes #3695

HttpHeaders#getContentType() validates the charset parameter and throws
InvalidMediaTypeException for values such as "x-user-defined" that clients
may send. Parse only the type and subtype when deciding whether a response
should be streamed, so the response body can still be proxied.

Fixes spring-cloudgh-3695

Signed-off-by: hebulin <hebulin@qq.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidMediaTypeException: Invalid mime type \"text/plain; charset=x-user-defined\": unsupported charset 'x-user-defined'

2 participants