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

Improve charset parsing in MockHttpServletRequest and Response [SPR-12677] #17276

Closed
spring-issuemaster opened this issue Jan 30, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Jan 30, 2015

Tor-Einar Jarnbjo opened SPR-12677 and commented

In MockHttpServletResponse, the parsing of the charset parameter in the content-type field is not correct. The implementation simply looks for the occurence of "charset=" anywhere in the contet-type header and treats the rest of the string as a character encoding name:

String CHARSET_PREFIX = "charset=";
...
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) {
	this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
	this.charset = true;
}

This simplified approach will fail for any of the following valid content-type headers:

text/plain;charset="utf-8" (the parameter value may be quoted)

text/plain;foocharset=bar (there may be other parameters ending with "charset")

text/plain;foo="charset=bar" ("charset=" may occur within the quoted value of a different parameter)

text/plain;charset=utf-8;foo=bar (the charset parameter may be followed by other parameters, here the character set would be parsed as "utf-8;foo=bar"


Affects: 4.1.2

Referenced from: commits f06dffb

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 30, 2015

Rossen Stoyanchev commented

Yes it's a little too simplistic and could be more robust.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 20, 2015

Rossen Stoyanchev commented

Modified title (was: "Charset parameter incorrectly parsed in MockHttpServletResponse").

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 20, 2015

Rossen Stoyanchev commented

This part of the code now uses the more advanced parsing capabilities of MediaType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.