Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
84d8135c
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
84d8135c
编写于
12月 09, 2016
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MimeType parsing properly handles quoted semicolons
Issue: SPR-14986 (cherry picked from commit
7714eecc
)
上级
dd3c370b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
47 addition
and
21 deletion
+47
-21
spring-core/src/main/java/org/springframework/util/MimeType.java
...core/src/main/java/org/springframework/util/MimeType.java
+6
-6
spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java
...src/main/java/org/springframework/util/MimeTypeUtils.java
+31
-15
spring-core/src/test/java/org/springframework/util/MimeTypeTests.java
...src/test/java/org/springframework/util/MimeTypeTests.java
+10
-0
未找到文件。
spring-core/src/main/java/org/springframework/util/MimeType.java
浏览文件 @
84d8135c
...
...
@@ -165,8 +165,8 @@ public class MimeType implements Comparable<MimeType>, Serializable {
* @throws IllegalArgumentException if any of the parameters contains illegal characters
*/
public
MimeType
(
String
type
,
String
subtype
,
Map
<
String
,
String
>
parameters
)
{
Assert
.
hasLength
(
type
,
"
type
must not be empty"
);
Assert
.
hasLength
(
subtype
,
"
subtype
must not be empty"
);
Assert
.
hasLength
(
type
,
"
'type'
must not be empty"
);
Assert
.
hasLength
(
subtype
,
"
'subtype'
must not be empty"
);
checkToken
(
type
);
checkToken
(
subtype
);
this
.
type
=
type
.
toLowerCase
(
Locale
.
ENGLISH
);
...
...
@@ -202,8 +202,8 @@ public class MimeType implements Comparable<MimeType>, Serializable {
}
protected
void
checkParameters
(
String
attribute
,
String
value
)
{
Assert
.
hasLength
(
attribute
,
"
parameter attribute
must not be empty"
);
Assert
.
hasLength
(
value
,
"
parameter value
must not be empty"
);
Assert
.
hasLength
(
attribute
,
"
'attribute'
must not be empty"
);
Assert
.
hasLength
(
value
,
"
'value'
must not be empty"
);
checkToken
(
attribute
);
if
(
PARAM_CHARSET
.
equals
(
attribute
))
{
value
=
unquote
(
value
);
...
...
@@ -277,8 +277,8 @@ public class MimeType implements Comparable<MimeType>, Serializable {
* @since 4.3
*/
public
Charset
getCharset
()
{
String
char
S
et
=
getParameter
(
PARAM_CHARSET
);
return
(
char
Set
!=
null
?
Charset
.
forName
(
unquote
(
charS
et
))
:
null
);
String
char
s
et
=
getParameter
(
PARAM_CHARSET
);
return
(
char
set
!=
null
?
Charset
.
forName
(
unquote
(
chars
et
))
:
null
);
}
/**
...
...
spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java
浏览文件 @
84d8135c
...
...
@@ -49,6 +49,10 @@ public abstract class MimeTypeUtils {
private
static
Charset
US_ASCII
=
Charset
.
forName
(
"US-ASCII"
);
/**
* Comparator used by {@link #sortBySpecificity(List)}.
*/
public
static
final
Comparator
<
MimeType
>
SPECIFICITY_COMPARATOR
=
new
SpecificityComparator
<
MimeType
>();
/**
* Public constant mime type that includes all media ranges (i.e. "*/*").
...
...
@@ -219,12 +223,13 @@ public abstract class MimeTypeUtils {
if
(!
StringUtils
.
hasLength
(
mimeType
))
{
throw
new
InvalidMimeTypeException
(
mimeType
,
"'mimeType' must not be empty"
);
}
String
[]
parts
=
StringUtils
.
tokenizeToStringArray
(
mimeType
,
";"
);
if
(
parts
.
length
==
0
)
{
int
index
=
mimeType
.
indexOf
(
';'
);
String
fullType
=
(
index
>=
0
?
mimeType
.
substring
(
0
,
index
)
:
mimeType
).
trim
();
if
(
fullType
.
length
()
==
0
)
{
throw
new
InvalidMimeTypeException
(
mimeType
,
"'mimeType' must not be empty"
);
}
String
fullType
=
parts
[
0
].
trim
();
// java.net.HttpURLConnection returns a *; q=.2 Accept header
if
(
MimeType
.
WILDCARD_TYPE
.
equals
(
fullType
))
{
fullType
=
"*/*"
;
...
...
@@ -243,18 +248,36 @@ public abstract class MimeTypeUtils {
}
Map
<
String
,
String
>
parameters
=
null
;
if
(
parts
.
length
>
1
)
{
parameters
=
new
LinkedHashMap
<
String
,
String
>(
parts
.
length
-
1
);
for
(
int
i
=
1
;
i
<
parts
.
length
;
i
++)
{
String
parameter
=
parts
[
i
];
do
{
int
nextIndex
=
index
+
1
;
boolean
quoted
=
false
;
while
(
nextIndex
<
mimeType
.
length
())
{
char
ch
=
mimeType
.
charAt
(
nextIndex
);
if
(
ch
==
';'
)
{
if
(!
quoted
)
{
break
;
}
}
else
if
(
ch
==
'"'
)
{
quoted
=
!
quoted
;
}
nextIndex
++;
}
String
parameter
=
mimeType
.
substring
(
index
+
1
,
nextIndex
).
trim
();
if
(
parameter
.
length
()
>
0
)
{
if
(
parameters
==
null
)
{
parameters
=
new
LinkedHashMap
<
String
,
String
>(
4
);
}
int
eqIndex
=
parameter
.
indexOf
(
'='
);
if
(
eqIndex
!=
-
1
)
{
if
(
eqIndex
>=
0
)
{
String
attribute
=
parameter
.
substring
(
0
,
eqIndex
);
String
value
=
parameter
.
substring
(
eqIndex
+
1
,
parameter
.
length
());
parameters
.
put
(
attribute
,
value
);
}
}
index
=
nextIndex
;
}
while
(
index
<
mimeType
.
length
());
try
{
return
new
MimeType
(
type
,
subtype
,
parameters
);
...
...
@@ -353,11 +376,4 @@ public abstract class MimeTypeUtils {
return
new
String
(
generateMultipartBoundary
(),
US_ASCII
);
}
/**
* Comparator used by {@link #sortBySpecificity(List)}.
*/
public
static
final
Comparator
<
MimeType
>
SPECIFICITY_COMPARATOR
=
new
SpecificityComparator
<
MimeType
>();
}
spring-core/src/test/java/org/springframework/util/MimeTypeTests.java
浏览文件 @
84d8135c
...
...
@@ -87,6 +87,16 @@ public class MimeTypeTests {
assertEquals
(
"Invalid charset"
,
Charset
.
forName
(
"UTF-8"
),
mimeType
.
getCharset
());
}
@Test
public
void
parseQuotedSeparator
()
{
String
s
=
"application/xop+xml;charset=utf-8;type=\"application/soap+xml;action=\\\"http://x.y.z\\\"\""
;
MimeType
mimeType
=
MimeType
.
valueOf
(
s
);
assertEquals
(
"Invalid type"
,
"application"
,
mimeType
.
getType
());
assertEquals
(
"Invalid subtype"
,
"xop+xml"
,
mimeType
.
getSubtype
());
assertEquals
(
"Invalid charset"
,
Charset
.
forName
(
"UTF-8"
),
mimeType
.
getCharset
());
assertEquals
(
"\"application/soap+xml;action=\\\"http://x.y.z\\\"\""
,
mimeType
.
getParameter
(
"type"
));
}
@Test
public
void
withConversionService
()
{
ConversionService
conversionService
=
new
DefaultConversionService
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录