Skip to content

Should CharacterEncodingFilter apply to all assets? #5459

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

Closed
codefromthecrypt opened this issue Mar 22, 2016 · 7 comments
Closed

Should CharacterEncodingFilter apply to all assets? #5459

codefromthecrypt opened this issue Mar 22, 2016 · 7 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@codefromthecrypt
Copy link
Contributor

CharacterEncodingFilter is auto-configured such that it appends a charset to all resources. The trouble is that this adds charset to resources such as png files. This causes confusion for people debugging their apps.

I've overridden it like below, but wondering if there's a smarter way to address this.

  /**
   * This opts out of adding charset to png resources.
   *
   * <p>By default, {@linkplain CharacterEncodingFilter} adds a charset qualifier to all resources,
   * which helps, as javascript assets include extended character sets. However, the filter also
   * adds charset to well-known binary ones like png. This creates confusing content types, such as
   * "image/png;charset=UTF-8".
   */
  @Bean
  @Order(Ordered.HIGHEST_PRECEDENCE)
  public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter() {
      @Override
      protected boolean shouldNotFilter(HttpServletRequest request) {
        return request.getServletPath().endsWith(".png");
      }
    };
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    return filter;
  }
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 22, 2016
@philwebb
Copy link
Member

Relates to #1182

@philwebb
Copy link
Member

I wonder if we can check if the mime type starts with IMAGE_? If not we could definitely provide a property for this.

@philwebb philwebb added type: bug A general bug for: team-attention An issue we'd like other members of the team to review and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 28, 2016
@philwebb philwebb added this to the 1.4.0.M2 milestone Mar 28, 2016
@philwebb
Copy link
Member

philwebb commented Apr 6, 2016

/cc @bclozel in case he has any suggestions

@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label Apr 6, 2016
@bclozel
Copy link
Member

bclozel commented Apr 6, 2016

I've created SPR-14126 to track this.

@snicoll snicoll modified the milestones: 1.4.0.M3, 1.4.0.M2 Apr 8, 2016
rrsivabalan pushed a commit to rrsivabalan/spring-boot that referenced this issue Apr 27, 2016
@rrsivabalan rrsivabalan mentioned this issue Apr 27, 2016
1 task
@bclozel
Copy link
Member

bclozel commented May 3, 2016

I've just resolved SPR-14126 as won't fix, as I can't find any way to address this issue (I couldn't even get to a workaround). The only possible solution I can think of involves wrapping/caching responses, just like the ShallowEtagHeaderFilter does - but it's a huge performance toll.

I know that the encoding situation has been discussed many times in this bug tracker - and that there is no "sane default" as it all depends on the application, the culture of the app users, etc. Setting the forceEncoding option is rather agressive.

Of course, we can discuss further options with the MVC team.

@bclozel
Copy link
Member

bclozel commented May 3, 2016

In the meantime, and as suggested by @wilkinsona - I've created and solved SPR-14240, adding options to selectively force encoding on the request only.

I believe changing the defaults in Boot to only use setForceRequestEncoding(true) would solve this issue.

@snicoll snicoll self-assigned this May 3, 2016
@snicoll snicoll closed this as completed in 428a10a May 3, 2016
wilkinsona added a commit that referenced this issue May 3, 2016
Following the encoding change made in 428a10a, the character encoding
of a response is no longer forced.

See gh-5459
@wilkinsona
Copy link
Member

We need to update the javadoc on HttpEncodingProperties to reflect the new behaviour. It's also no longer possible to use application.properties to enforce request and response encoding should a user wish to do so.

@wilkinsona wilkinsona reopened this May 12, 2016
uoa-noel added a commit to UoA-eResearch/research-hub-api that referenced this issue Jan 7, 2019
- Spring Boot used to offer the setting in application properties to specify a charset for all endpoints. However, they have now changed its behaviour (see spring-projects/spring-boot#5459)
- I have now added an annotation to each endpoint type so each endpoint uses UTF-8 charset.
- If we have an endpoint that returns images or other binary data, the produces value needs to be overridden. See https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#produces--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants