Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
a1c0905a
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 搜索 >>
提交
a1c0905a
编写于
10月 22, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistently accept empty Content-Type header and empty character encoding
Issue: SPR-12173 (cherry picked from commit
d501137f
)
上级
6f9d7da5
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
67 addition
and
32 deletion
+67
-32
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
.../org/springframework/mock/web/MockHttpServletRequest.java
+13
-10
spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java
...springframework/http/server/ServletServerHttpRequest.java
+24
-14
spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java
...gframework/http/server/ServletServerHttpRequestTests.java
+26
-3
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java
...springframework/mock/web/test/MockHttpServletRequest.java
+4
-5
未找到文件。
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
浏览文件 @
a1c0905a
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -36,7 +36,6 @@ import java.util.List;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.Set
;
import
javax.servlet.AsyncContext
;
import
javax.servlet.DispatcherType
;
import
javax.servlet.RequestDispatcher
;
...
...
@@ -58,7 +57,7 @@ import org.springframework.util.StringUtils;
/**
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
*
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
* <p>As of Spring
Framework
4.0, this set of mocks is designed on a Servlet 3.0 baseline.
*
* @author Juergen Hoeller
* @author Rod Johnson
...
...
@@ -70,10 +69,14 @@ import org.springframework.util.StringUtils;
*/
public
class
MockHttpServletRequest
implements
HttpServletRequest
{
private
static
final
String
HTTP
=
"http"
;
private
static
final
String
HTTPS
=
"https"
;
/**
* The default protocol: 'http'.
*/
public
static
final
String
DEFAULT_PROTOCOL
=
"http"
;
public
static
final
String
DEFAULT_PROTOCOL
=
HTTP
;
/**
* The default server address: '127.0.0.1'.
...
...
@@ -330,9 +333,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
private
void
updateContentTypeHeader
()
{
if
(
this
.
contentType
!=
null
)
{
if
(
StringUtils
.
hasLength
(
this
.
contentType
)
)
{
StringBuilder
sb
=
new
StringBuilder
(
this
.
contentType
);
if
(!
this
.
contentType
.
toLowerCase
().
contains
(
CHARSET_PREFIX
)
&&
this
.
characterEncoding
!=
null
)
{
if
(!
this
.
contentType
.
toLowerCase
().
contains
(
CHARSET_PREFIX
)
&&
StringUtils
.
hasLength
(
this
.
characterEncoding
))
{
sb
.
append
(
";"
).
append
(
CHARSET_PREFIX
).
append
(
this
.
characterEncoding
);
}
doAddHeaderValue
(
CONTENT_TYPE_HEADER
,
sb
.
toString
(),
true
);
...
...
@@ -357,8 +361,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
if
(
contentType
!=
null
)
{
int
charsetIndex
=
contentType
.
toLowerCase
().
indexOf
(
CHARSET_PREFIX
);
if
(
charsetIndex
!=
-
1
)
{
String
encoding
=
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
());
this
.
characterEncoding
=
encoding
;
this
.
characterEncoding
=
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
());
}
updateContentTypeHeader
();
}
...
...
@@ -955,8 +958,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public
StringBuffer
getRequestURL
()
{
StringBuffer
url
=
new
StringBuffer
(
this
.
scheme
).
append
(
"://"
).
append
(
this
.
serverName
);
if
(
this
.
serverPort
>
0
&&
((
"http"
.
equalsIgnoreCase
(
scheme
)
&&
this
.
serverPort
!=
80
)
||
(
"https"
.
equalsIgnoreCase
(
scheme
)
&&
this
.
serverPort
!=
443
)))
{
if
(
this
.
serverPort
>
0
&&
((
HTTP
.
equalsIgnoreCase
(
this
.
scheme
)
&&
this
.
serverPort
!=
80
)
||
(
HTTPS
.
equalsIgnoreCase
(
this
.
scheme
)
&&
this
.
serverPort
!=
443
)))
{
url
.
append
(
':'
).
append
(
this
.
serverPort
);
}
...
...
spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java
浏览文件 @
a1c0905a
...
...
@@ -30,7 +30,6 @@ import java.nio.charset.Charset;
import
java.security.Principal
;
import
java.util.Arrays
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -40,6 +39,8 @@ import org.springframework.http.HttpHeaders;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.MediaType
;
import
org.springframework.util.Assert
;
import
org.springframework.util.LinkedCaseInsensitiveMap
;
import
org.springframework.util.StringUtils
;
/**
* {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}.
...
...
@@ -111,21 +112,30 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
}
}
// HttpServletRequest exposes some headers as properties: we should include those if not already present
if
(
this
.
headers
.
getContentType
()
==
null
&&
this
.
servletRequest
.
getContentType
()
!=
null
)
{
MediaType
contentType
=
MediaType
.
parseMediaType
(
this
.
servletRequest
.
getContentType
());
this
.
headers
.
setContentType
(
contentType
);
MediaType
contentType
=
this
.
headers
.
getContentType
();
if
(
contentType
==
null
)
{
String
requestContentType
=
this
.
servletRequest
.
getContentType
();
if
(
StringUtils
.
hasLength
(
requestContentType
))
{
contentType
=
MediaType
.
parseMediaType
(
requestContentType
);
this
.
headers
.
setContentType
(
contentType
);
}
}
if
(
this
.
headers
.
getContentType
()
!=
null
&&
this
.
headers
.
getContentType
().
getCharSet
()
==
null
&&
this
.
servletRequest
.
getCharacterEncoding
()
!=
null
)
{
MediaType
oldContentType
=
this
.
headers
.
getContentType
();
Charset
charSet
=
Charset
.
forName
(
this
.
servletRequest
.
getCharacterEncoding
());
Map
<
String
,
String
>
params
=
new
HashMap
<
String
,
String
>(
oldContentType
.
getParameters
());
params
.
put
(
"charset"
,
charSet
.
toString
());
MediaType
newContentType
=
new
MediaType
(
oldContentType
.
getType
(),
oldContentType
.
getSubtype
(),
params
);
this
.
headers
.
setContentType
(
newContentType
);
if
(
contentType
!=
null
&&
contentType
.
getCharSet
()
==
null
)
{
String
requestEncoding
=
this
.
servletRequest
.
getCharacterEncoding
();
if
(
StringUtils
.
hasLength
(
requestEncoding
))
{
Charset
charSet
=
Charset
.
forName
(
requestEncoding
);
Map
<
String
,
String
>
params
=
new
LinkedCaseInsensitiveMap
<
String
>();
params
.
putAll
(
contentType
.
getParameters
());
params
.
put
(
"charset"
,
charSet
.
toString
());
MediaType
newContentType
=
new
MediaType
(
contentType
.
getType
(),
contentType
.
getSubtype
(),
params
);
this
.
headers
.
setContentType
(
newContentType
);
}
}
if
(
this
.
headers
.
getContentLength
()
==
-
1
&&
this
.
servletRequest
.
getContentLength
()
!=
-
1
)
{
this
.
headers
.
setContentLength
(
this
.
servletRequest
.
getContentLength
());
if
(
this
.
headers
.
getContentLength
()
==
-
1
)
{
int
requestContentLength
=
this
.
servletRequest
.
getContentLength
();
if
(
requestContentLength
!=
-
1
)
{
this
.
headers
.
setContentLength
(
requestContentLength
);
}
}
}
return
this
.
headers
;
...
...
spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java
浏览文件 @
a1c0905a
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
4
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 java.util.List;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.MediaType
;
...
...
@@ -39,12 +40,14 @@ public class ServletServerHttpRequestTests {
private
MockHttpServletRequest
mockRequest
;
@Before
public
void
create
()
throws
Exception
{
mockRequest
=
new
MockHttpServletRequest
();
request
=
new
ServletServerHttpRequest
(
mockRequest
);
}
@Test
public
void
getMethod
()
throws
Exception
{
mockRequest
.
setMethod
(
"POST"
);
...
...
@@ -65,8 +68,8 @@ public class ServletServerHttpRequestTests {
public
void
getHeaders
()
throws
Exception
{
String
headerName
=
"MyHeader"
;
String
headerValue1
=
"value1"
;
mockRequest
.
addHeader
(
headerName
,
headerValue1
);
String
headerValue2
=
"value2"
;
mockRequest
.
addHeader
(
headerName
,
headerValue1
);
mockRequest
.
addHeader
(
headerName
,
headerValue2
);
mockRequest
.
setContentType
(
"text/plain"
);
mockRequest
.
setCharacterEncoding
(
"UTF-8"
);
...
...
@@ -82,6 +85,26 @@ public class ServletServerHttpRequestTests {
headers
.
getContentType
());
}
@Test
public
void
getHeadersWithEmptyContentTypeAndEncoding
()
throws
Exception
{
String
headerName
=
"MyHeader"
;
String
headerValue1
=
"value1"
;
String
headerValue2
=
"value2"
;
mockRequest
.
addHeader
(
headerName
,
headerValue1
);
mockRequest
.
addHeader
(
headerName
,
headerValue2
);
mockRequest
.
setContentType
(
""
);
mockRequest
.
setCharacterEncoding
(
""
);
HttpHeaders
headers
=
request
.
getHeaders
();
assertNotNull
(
"No HttpHeaders returned"
,
headers
);
assertTrue
(
"Invalid headers returned"
,
headers
.
containsKey
(
headerName
));
List
<
String
>
headerValues
=
headers
.
get
(
headerName
);
assertEquals
(
"Invalid header values returned"
,
2
,
headerValues
.
size
());
assertTrue
(
"Invalid header values returned"
,
headerValues
.
contains
(
headerValue1
));
assertTrue
(
"Invalid header values returned"
,
headerValues
.
contains
(
headerValue2
));
assertNull
(
headers
.
getContentType
());
}
@Test
public
void
getBody
()
throws
Exception
{
byte
[]
content
=
"Hello World"
.
getBytes
(
"UTF-8"
);
...
...
@@ -105,4 +128,4 @@ public class ServletServerHttpRequestTests {
assertArrayEquals
(
"Invalid content returned"
,
content
,
result
);
}
}
\ No newline at end of file
}
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java
浏览文件 @
a1c0905a
...
...
@@ -36,7 +36,6 @@ import java.util.List;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.Set
;
import
javax.servlet.AsyncContext
;
import
javax.servlet.DispatcherType
;
import
javax.servlet.RequestDispatcher
;
...
...
@@ -333,9 +332,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
private
void
updateContentTypeHeader
()
{
if
(
this
.
contentType
!=
null
)
{
if
(
StringUtils
.
hasLength
(
this
.
contentType
)
)
{
StringBuilder
sb
=
new
StringBuilder
(
this
.
contentType
);
if
(!
this
.
contentType
.
toLowerCase
().
contains
(
CHARSET_PREFIX
)
&&
this
.
characterEncoding
!=
null
)
{
if
(!
this
.
contentType
.
toLowerCase
().
contains
(
CHARSET_PREFIX
)
&&
StringUtils
.
hasLength
(
this
.
characterEncoding
))
{
sb
.
append
(
";"
).
append
(
CHARSET_PREFIX
).
append
(
this
.
characterEncoding
);
}
doAddHeaderValue
(
CONTENT_TYPE_HEADER
,
sb
.
toString
(),
true
);
...
...
@@ -360,8 +360,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
if
(
contentType
!=
null
)
{
int
charsetIndex
=
contentType
.
toLowerCase
().
indexOf
(
CHARSET_PREFIX
);
if
(
charsetIndex
!=
-
1
)
{
String
encoding
=
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
());
this
.
characterEncoding
=
encoding
;
this
.
characterEncoding
=
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
());
}
updateContentTypeHeader
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录