Skip to content

Commit

Permalink
Update MimeParser to decode body part headers with UTF-8 instead of I…
Browse files Browse the repository at this point in the history
…SO_8859_1 (#3969) (#3972)

Update ContentDisposition.parse to remove ASCII restriction for quoted tokens.
  • Loading branch information
romain-grecourt committed Mar 17, 2022
1 parent c30e5e2 commit 408ee01
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,8 +57,7 @@ public final class ContentDisposition {

private static final CharMatcher LINEAR_WHITE_SPACE = CharMatcher.anyOf(" \t\r\n");

private static final CharMatcher QUOTED_TEXT_MATCHER = CharMatcher.ascii()
.and(CharMatcher.noneOf("\"\\\r"));
private static final CharMatcher QUOTED_TEXT_MATCHER = CharMatcher.noneOf("\"\\\r");

private static final String NAME_PARAMETER = "name";
private static final String FILENAME_PARAMETER = "filename";
Expand Down
Expand Up @@ -250,7 +250,7 @@ private ParsingException(Throwable cause) {
}

private static final Logger LOGGER = Logger.getLogger(MimeParser.class.getName());
private static final Charset HEADER_ENCODING = StandardCharsets.ISO_8859_1;
private static final Charset HEADER_ENCODING = StandardCharsets.UTF_8;

/**
* All states.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -246,4 +246,16 @@ public void testDateQuotes() {
.build();
assertThat(cd.toString(), is(equalTo(template)));
}

@Test
public void testNonAsciiFilename() {
ContentDisposition cd = ContentDisposition.parse("form-data; name=\"file[]\"; filename=\"\u60A8\u597D.txt\"");
assertThat(cd.type(), is(equalTo("form-data")));
assertThat(cd.name().isPresent(), is(equalTo(true)));
assertThat(cd.name().get(), is(equalTo("file[]")));
assertThat(cd.filename().isPresent(), is(equalTo(true)));
assertThat(cd.filename().get(), is(equalTo("\u60A8\u597D.txt")));
assertThat(cd.parameters(), is(notNullValue()));
assertThat(cd.parameters().size(), is(equalTo(2)));
}
}
Expand Up @@ -638,6 +638,24 @@ public void testHeaderValueWithLeadingWhiteSpace() {
assertThat(new String(part1.content), is(equalTo("part1")));
}

@Test
public void testHeaderUTF8() {
String boundary = "boundary";
final byte[] chunk1 = ("--" + boundary + "\n"
+ "Content-Disposition: form-data; name=\"file[]\"; filename=\"\u60A8\u597D.txt\"\n"
+ "\n"
+ "part1\n"
+ "--" + boundary + "--").getBytes();
List<MimePart> parts = parse(boundary, chunk1).parts;
assertThat(parts.size(), is(equalTo(1)));
MimePart part1 = parts.get(0);
assertThat(part1.headers.size(), is(equalTo(1)));
assertThat(part1.headers.get("Content-Disposition"),
hasItems("form-data; name=\"file[]\"; filename=\"\u60A8\u597D.txt\""));
assertThat(part1.content, is(notNullValue()));
assertThat(new String(part1.content), is(equalTo("part1")));
}

@Test
public void testHeaderValueWithWhiteSpacesOnly() {
String boundary = "boundary";
Expand Down Expand Up @@ -764,9 +782,9 @@ static final class ParserResult {
final MimeParser.ParserEvent lastEvent;

ParserResult(List<MimePart> parts,
Map<String, List<String>> partHeaders,
byte[] partContent,
MimeParser.ParserEvent lastEvent) {
Map<String, List<String>> partHeaders,
byte[] partContent,
MimeParser.ParserEvent lastEvent) {
this.parts = parts;
this.partHeaders = partHeaders;
this.partContent = partContent;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package io.helidon.media.multipart;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package io.helidon.media.multipart;
Expand Down

0 comments on commit 408ee01

Please sign in to comment.