Polishing contribution

Closes gh-26463
This commit is contained in:
Rossen Stoyanchev
2021-02-05 12:15:50 +00:00
parent a264013d7a
commit 8fb9796d1d
2 changed files with 27 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,6 +48,9 @@ import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
*/
public final class ContentDisposition {
private final static Pattern BASE64_ENCODED_PATTERN =
Pattern.compile("=\\?([0-9a-zA-Z-_]+)\\?B\\?([+/0-9a-zA-Z]+=*)\\?=");
private static final String INVALID_HEADER_FIELD_PARAMETER_FORMAT =
"Invalid header field parameter format (as defined in RFC 5987)";
@@ -139,8 +142,9 @@ public final class ContentDisposition {
}
/**
* Return the value of the {@literal filename} parameter (or the value of the
* {@literal filename*} one decoded as defined in the RFC 5987), or {@code null} if not defined.
* Return the value of the {@literal filename} parameter, possibly decoded
* from BASE64 encoding based on RFC 2047, or of the {@literal filename*}
* parameter, possibly decoded as defined in the RFC 5987.
*/
@Nullable
public String getFilename() {
@@ -323,7 +327,6 @@ public final class ContentDisposition {
return new ContentDisposition("", null, null, null, null, null, null, null);
}
private final static Pattern BASE64_ENCODED_PATTERN = Pattern.compile("=\\?([0-9a-zA-Z-_]+)\\?B\\?([+/0-9a-zA-Z]+=*)\\?=");
/**
* Parse a {@literal Content-Disposition} header value as defined in RFC 2183.
* @param contentDisposition the {@literal Content-Disposition} header value
@@ -366,12 +369,18 @@ public final class ContentDisposition {
}
}
else if (attribute.equals("filename") && (filename == null)) {
final Matcher matcher = BASE64_ENCODED_PATTERN.matcher(value);
if (matcher.find()) {
String charsetName = matcher.group(1);
String encoded = matcher.group(2);
filename = new String(Base64.getDecoder().decode(encoded), Charset.forName(charsetName));
} else {
if (value.startsWith("=?") ) {
Matcher matcher = BASE64_ENCODED_PATTERN.matcher(value);
if (matcher.find()) {
String match1 = matcher.group(1);
String match2 = matcher.group(2);
filename = new String(Base64.getDecoder().decode(match2), Charset.forName(match1));
}
else {
filename = value;
}
}
else {
filename = value;
}
}