Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
5861e968
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,发现更多精彩内容 >>
提交
5861e968
编写于
3月 27, 2018
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Always specify charset for form data requests
Issue: SPR-16613
上级
e00384a6
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
33 addition
and
23 deletion
+33
-23
spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageWriter.java
...org/springframework/http/codec/FormHttpMessageWriter.java
+15
-9
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
...ingframework/http/converter/FormHttpMessageConverter.java
+9
-7
spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageWriterTests.java
...pringframework/http/codec/FormHttpMessageWriterTests.java
+5
-3
spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
...amework/http/converter/FormHttpMessageConverterTests.java
+3
-3
spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java
...ngframework/web/client/AbstractMockWebServerTestCase.java
+1
-1
未找到文件。
spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageWriter.java
浏览文件 @
5861e968
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -61,12 +61,15 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
public
static
final
Charset
DEFAULT_CHARSET
=
StandardCharsets
.
UTF_8
;
private
static
final
ResolvableType
MULTIVALUE
_TYPE
=
ResolvableType
.
forClassWithGenerics
(
MultiValueMap
.
class
,
String
.
class
,
String
.
class
);
private
static
final
MediaType
DEFAULT_FORM_DATA_MEDIA
_TYPE
=
new
MediaType
(
MediaType
.
APPLICATION_FORM_URLENCODED
,
DEFAULT_CHARSET
);
private
static
final
List
<
MediaType
>
MEDIA_TYPES
=
Collections
.
singletonList
(
MediaType
.
APPLICATION_FORM_URLENCODED
);
private
static
final
ResolvableType
MULTIVALUE_TYPE
=
ResolvableType
.
forClassWithGenerics
(
MultiValueMap
.
class
,
String
.
class
,
String
.
class
);
private
Charset
defaultCharset
=
DEFAULT_CHARSET
;
...
...
@@ -117,13 +120,16 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
ResolvableType
elementType
,
@Nullable
MediaType
mediaType
,
ReactiveHttpOutputMessage
message
,
Map
<
String
,
Object
>
hints
)
{
MediaType
contentType
=
message
.
getHeaders
().
getContentType
();
if
(
contentType
==
null
)
{
contentType
=
MediaType
.
APPLICATION_FORM_URLENCODED
;
message
.
getHeaders
().
setContentType
(
contentType
);
mediaType
=
(
mediaType
!=
null
?
mediaType
:
DEFAULT_FORM_DATA_MEDIA_TYPE
);
Charset
charset
;
if
(
mediaType
.
getCharset
()
==
null
)
{
charset
=
getDefaultCharset
();
mediaType
=
new
MediaType
(
mediaType
,
charset
);
}
Charset
charset
=
getMediaTypeCharset
(
contentType
);
else
{
charset
=
mediaType
.
getCharset
();
}
message
.
getHeaders
().
setContentType
(
mediaType
);
return
Mono
.
from
(
inputStream
).
flatMap
(
form
->
{
String
value
=
serializeForm
(
form
,
charset
);
...
...
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
浏览文件 @
5861e968
...
...
@@ -96,6 +96,9 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
public
static
final
Charset
DEFAULT_CHARSET
=
StandardCharsets
.
UTF_8
;
private
static
final
MediaType
DEFAULT_FORM_DATA_MEDIA_TYPE
=
new
MediaType
(
MediaType
.
APPLICATION_FORM_URLENCODED
,
DEFAULT_CHARSET
);
private
List
<
MediaType
>
supportedMediaTypes
=
new
ArrayList
<>();
...
...
@@ -290,15 +293,14 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
private
void
writeForm
(
MultiValueMap
<
String
,
String
>
form
,
@Nullable
MediaType
contentType
,
HttpOutputMessage
outputMessage
)
throws
IOException
{
Charset
charset
;
if
(
contentType
!=
null
)
{
outputMessage
.
getHeaders
().
setContentType
(
contentType
);
charset
=
(
contentType
.
getCharset
()
!=
null
?
contentType
.
getCharset
()
:
this
.
charset
);
}
else
{
outputMessage
.
getHeaders
().
setContentType
(
MediaType
.
APPLICATION_FORM_URLENCODED
);
contentType
=
(
contentType
!=
null
?
contentType
:
DEFAULT_FORM_DATA_MEDIA_TYPE
);
Charset
charset
=
contentType
.
getCharset
();
if
(
charset
==
null
)
{
charset
=
this
.
charset
;
contentType
=
new
MediaType
(
contentType
,
charset
);
}
outputMessage
.
getHeaders
().
setContentType
(
contentType
);
StringBuilder
builder
=
new
StringBuilder
();
for
(
Iterator
<
String
>
nameIterator
=
form
.
keySet
().
iterator
();
nameIterator
.
hasNext
();)
{
String
name
=
nameIterator
.
next
();
...
...
spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageWriterTests.java
浏览文件 @
5861e968
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -22,6 +22,7 @@ import org.junit.Test;
import
reactor.core.publisher.Mono
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
;
import
org.springframework.util.LinkedMultiValueMap
;
...
...
@@ -79,8 +80,9 @@ public class FormHttpMessageWriterTests {
String
responseBody
=
response
.
getBodyAsString
().
block
();
assertEquals
(
"name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"
,
responseBody
);
assertEquals
(
MediaType
.
APPLICATION_FORM_URLENCODED
,
response
.
getHeaders
().
getContentType
());
assertEquals
(
responseBody
.
getBytes
().
length
,
response
.
getHeaders
().
getContentLength
());
HttpHeaders
headers
=
response
.
getHeaders
();
assertEquals
(
"application/x-www-form-urlencoded;charset=UTF-8"
,
headers
.
getContentType
().
toString
());
assertEquals
(
responseBody
.
getBytes
().
length
,
headers
.
getContentLength
());
}
}
spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
浏览文件 @
5861e968
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -112,8 +112,8 @@ public class FormHttpMessageConverterTests {
assertEquals
(
"Invalid result"
,
"name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"
,
outputMessage
.
getBodyAsString
(
StandardCharsets
.
UTF_8
));
assertEquals
(
"Invalid content-type"
,
new
MediaType
(
"application"
,
"x-www-form-urlencoded"
)
,
outputMessage
.
getHeaders
().
getContentType
());
assertEquals
(
"Invalid content-type"
,
"application/x-www-form-urlencoded;charset=UTF-8"
,
outputMessage
.
getHeaders
().
getContentType
()
.
toString
()
);
assertEquals
(
"Invalid content-length"
,
outputMessage
.
getBodyAsBytes
().
length
,
outputMessage
.
getHeaders
().
getContentLength
());
}
...
...
spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java
浏览文件 @
5861e968
...
...
@@ -165,7 +165,7 @@ public class AbstractMockWebServerTestCase {
}
private
MockResponse
formRequest
(
RecordedRequest
request
)
{
assertEquals
(
"application/x-www-form-urlencoded"
,
request
.
getHeader
(
"Content-Type"
));
assertEquals
(
"application/x-www-form-urlencoded
;charset=UTF-8
"
,
request
.
getHeader
(
"Content-Type"
));
String
body
=
request
.
getBody
().
readUtf8
();
assertThat
(
body
,
Matchers
.
containsString
(
"name+1=value+1"
));
assertThat
(
body
,
Matchers
.
containsString
(
"name+2=value+2%2B1"
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录