Skip to content

HttpHeaders.setContentDispositionFormData() doesn't encode non-acsii characters correctly [SPR-14547] #19115

Closed

Description

@spring-projects-issues

Wenhao Ji opened SPR-14547 and commented

When passing name or filename containing non-ascii characters to the setContentDispositionFormData method, a browser like Chrome and IE may behave strangely, opening a download dialog where those characters are replaced by hyphens.

The parameters in http headers should be encoded according to [RFC5987] https://tools.ietf.org/html/rfc5987, so that we can get the correct string no matter what kind of characters are passed into.

For example,

@RequestMapping("downloadForm")
public HttpEntity<String> downloadFormData() throws UnsupportedEncodingException {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentDispositionFormData("form", "中文.txt");
    return new HttpEntity<String>("a=b", headers);
}

The Content-Disposition header values that returns from the controller method above should be like filename=UTF-8'zh-Hant-CN'%E4%B8%AD%E6%96%87.txt.


Affects: 4.3.2

Issue Links:

Activity

  1. spring-projects-issues commented on Aug 25, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    This is now fixed and backported.
    This will be shortly available in the 5.0 and 4.3 SNAPSHOTS if you'd like to try it.

    Note that I didn't change the default implementation of setContentDispositionFormData(String name, String filename), as the latest RFC7230 states that:

    In practice, most HTTP header
    field values use only a subset of the US-ASCII charset US-ASCII.
    Newly defined header fields SHOULD limit their field values to
    US-ASCII octets. A recipient SHOULD treat other octets in field
    content (obs-text) as opaque data.

    You can now use the dedicated method setContentDispositionFormData(String name, String filename, Charset charset), which requires the charset for the given field header.

    Let me know if you have any feedback about this.

    Thanks for the report!

  2. spring-projects-issues commented on Aug 26, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    The tests for this require consistent UTF-8 encoding in the test sources as well, following up on #16193 which switched enforced UTF-8 for the main sources.

  3. added
    type: enhancement
    in: web
    on Jan 11, 2019
  4. added this to the 4.3.3 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions