Skip to content

Fix ApacheHttpClient.getContentType() to use request charset as default - #566

Merged
codefromthecrypt merged 2 commits into
OpenFeign:masterfrom
loia:apache_charset_fix
Jun 12, 2017
Merged

codefromthecrypt merged 2 commits into
OpenFeign:masterfrom
loia:apache_charset_fix

Conversation

@loia

@loia loia commented Jun 10, 2017 •

Copy link
Copy Markdown
Contributor

This fix is to address an issue in ApacheHttpClient where there is a charset specified in request.charset() but not in the Content-Type header.

This causes a charset mismatch in the code below -

      if (request.charset() != null) {
        ContentType contentType = getContentType(request);
        String content = new String(request.body(), request.charset());
        entity = new StringEntity(content, contentType);

For example -

  • request.charset() = utf-8
  • Content-Type = application/json

The content string will be encoded in utf-8 but StringEntity will use ISO-8859-1 by default because there is no charset in the content type.

    public StringEntity(String string, ContentType contentType) throws UnsupportedCharsetException {
        Charset charset = contentType != null ? contentType.getCharset() : null;
        if (charset == null) {
            charset = HTTP.DEF_CONTENT_CHARSET;
        }

This bug appears to have been introduced in 9.4.0 by pull request #453 .

@codefromthecrypt

Copy link
Copy Markdown

seems testable? maybe add a mockwebserver test so it doesn't break again?

@loia

loia commented Jun 12, 2017

Copy link
Copy Markdown
Contributor Author

Added a test which fails on master and passes with this fix.

@codefromthecrypt
codefromthecrypt merged commit 8c54e3a into OpenFeign:master Jun 12, 2017
@codefromthecrypt

Copy link
Copy Markdown

nice job

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants