Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
5bbbc82e
S
spring-framework
项目概览
dk131072
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
5bbbc82e
编写于
12月 12, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistent handling of null header values in HttpHeaders
Issue: SPR-17588
上级
4b450308
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
69 addition
and
52 deletion
+69
-52
spring-web/src/main/java/org/springframework/http/HttpHeaders.java
...b/src/main/java/org/springframework/http/HttpHeaders.java
+51
-33
spring-web/src/main/java/org/springframework/http/ResponseEntity.java
...rc/main/java/org/springframework/http/ResponseEntity.java
+1
-4
spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java
.../test/java/org/springframework/http/HttpHeadersTests.java
+11
-4
spring-web/src/test/java/org/springframework/http/client/SimpleClientHttpRequestFactoryTests.java
...work/http/client/SimpleClientHttpRequestFactoryTests.java
+2
-2
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java
...eactive/function/server/DefaultServerResponseBuilder.java
+1
-4
spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java
...ngframework/web/reactive/resource/ResourceWebHandler.java
+3
-5
未找到文件。
spring-web/src/main/java/org/springframework/http/HttpHeaders.java
浏览文件 @
5bbbc82e
...
...
@@ -75,11 +75,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
private
static
final
long
serialVersionUID
=
-
8578554704772377436L
;
/**
* The empty {@code HttpHeaders} instance (immutable).
*/
public
static
final
HttpHeaders
EMPTY
=
new
ReadOnlyHttpHeaders
(
new
HttpHeaders
(
new
LinkedMultiValueMap
<>(
0
)));
/**
* The HTTP {@code Accept} header field name.
* @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.2">Section 5.3.2 of RFC 7231</a>
...
...
@@ -381,6 +377,12 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
*/
public
static
final
String
WWW_AUTHENTICATE
=
"WWW-Authenticate"
;
/**
* The empty {@code HttpHeaders} instance (immutable).
*/
public
static
final
HttpHeaders
EMPTY
=
new
ReadOnlyHttpHeaders
(
new
HttpHeaders
(
new
LinkedMultiValueMap
<>(
0
)));
/**
* Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match".
* @see <a href="https://tools.ietf.org/html/rfc7232#section-2.3">Section 2.3 of RFC 7232</a>
...
...
@@ -409,15 +411,15 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Construct a new, empty instance of the {@code HttpHeaders} object.
*/
public
HttpHeaders
()
{
this
(
CollectionUtils
.
toMultiValueMap
(
new
LinkedCaseInsensitiveMap
<>(
8
,
Locale
.
ENGLISH
)));
this
(
CollectionUtils
.
toMultiValueMap
(
new
LinkedCaseInsensitiveMap
<>(
8
,
Locale
.
ENGLISH
)));
}
/**
* Construct a new {@code HttpHeaders} instance backed by an existing map.
* @since 5.1
*/
public
HttpHeaders
(
MultiValueMap
<
String
,
String
>
headers
)
{
Assert
.
notNull
(
headers
,
"
headers
must not be null"
);
Assert
.
notNull
(
headers
,
"
MultiValueMap
must not be null"
);
this
.
headers
=
headers
;
}
...
...
@@ -445,7 +447,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @since 5.0
*/
public
void
setAcceptLanguage
(
List
<
Locale
.
LanguageRange
>
languages
)
{
Assert
.
notNull
(
languages
,
"
'languages'
must not be null"
);
Assert
.
notNull
(
languages
,
"
LanguageRange List
must not be null"
);
DecimalFormat
decimal
=
new
DecimalFormat
(
"0.0"
,
DECIMAL_FORMAT_SYMBOLS
);
List
<
String
>
values
=
languages
.
stream
()
.
map
(
range
->
...
...
@@ -555,7 +557,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Set the (new) value of the {@code Access-Control-Allow-Origin} response header.
*/
public
void
setAccessControlAllowOrigin
(
@Nullable
String
allowedOrigin
)
{
set
(
ACCESS_CONTROL_ALLOW_ORIGIN
,
allowedOrigin
);
set
OrRemove
(
ACCESS_CONTROL_ALLOW_ORIGIN
,
allowedOrigin
);
}
/**
...
...
@@ -614,7 +616,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Set the (new) value of the {@code Access-Control-Request-Method} request header.
*/
public
void
setAccessControlRequestMethod
(
@Nullable
HttpMethod
requestMethod
)
{
set
(
ACCESS_CONTROL_REQUEST_METHOD
,
(
requestMethod
!=
null
?
requestMethod
.
name
()
:
null
));
set
OrRemove
(
ACCESS_CONTROL_REQUEST_METHOD
,
(
requestMethod
!=
null
?
requestMethod
.
name
()
:
null
));
}
/**
...
...
@@ -766,14 +768,14 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @since 5.0.5
*/
public
void
setCacheControl
(
CacheControl
cacheControl
)
{
set
(
CACHE_CONTROL
,
cacheControl
.
getHeaderValue
());
set
OrRemove
(
CACHE_CONTROL
,
cacheControl
.
getHeaderValue
());
}
/**
* Set the (new) value of the {@code Cache-Control} header.
*/
public
void
setCacheControl
(
@Nullable
String
cacheControl
)
{
set
(
CACHE_CONTROL
,
cacheControl
);
set
OrRemove
(
CACHE_CONTROL
,
cacheControl
);
}
/**
...
...
@@ -817,7 +819,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @see #getContentDisposition()
*/
public
void
setContentDispositionFormData
(
String
name
,
@Nullable
String
filename
)
{
Assert
.
notNull
(
name
,
"
'name'
must not be null"
);
Assert
.
notNull
(
name
,
"
Name
must not be null"
);
ContentDisposition
.
Builder
disposition
=
ContentDisposition
.
builder
(
"form-data"
).
name
(
name
);
if
(
filename
!=
null
)
{
disposition
.
filename
(
filename
);
...
...
@@ -860,7 +862,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @since 5.0
*/
public
void
setContentLanguage
(
@Nullable
Locale
locale
)
{
set
(
CONTENT_LANGUAGE
,
(
locale
!=
null
?
locale
.
toLanguageTag
()
:
null
));
set
OrRemove
(
CONTENT_LANGUAGE
,
(
locale
!=
null
?
locale
.
toLanguageTag
()
:
null
));
}
/**
...
...
@@ -904,12 +906,12 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
*/
public
void
setContentType
(
@Nullable
MediaType
mediaType
)
{
if
(
mediaType
!=
null
)
{
Assert
.
isTrue
(!
mediaType
.
isWildcardType
(),
"
'Content-Type'
cannot contain wildcard type '*'"
);
Assert
.
isTrue
(!
mediaType
.
isWildcardSubtype
(),
"
'Content-Type'
cannot contain wildcard subtype '*'"
);
Assert
.
isTrue
(!
mediaType
.
isWildcardType
(),
"
Content-Type
cannot contain wildcard type '*'"
);
Assert
.
isTrue
(!
mediaType
.
isWildcardSubtype
(),
"
Content-Type
cannot contain wildcard subtype '*'"
);
set
(
CONTENT_TYPE
,
mediaType
.
toString
());
}
else
{
set
(
CONTENT_TYPE
,
null
);
remove
(
CONTENT_TYPE
);
}
}
...
...
@@ -953,9 +955,12 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
Assert
.
isTrue
(
etag
.
startsWith
(
"\""
)
||
etag
.
startsWith
(
"W/"
),
"Invalid ETag: does not start with W/ or \""
);
Assert
.
isTrue
(
etag
.
endsWith
(
"\""
),
"Invalid ETag: does not end with \""
);
}
set
(
ETAG
,
etag
);
}
else
{
remove
(
ETAG
);
}
}
/**
* Return the entity tag of the body, as specified by the {@code ETag} header.
...
...
@@ -1012,7 +1017,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
set
(
HOST
,
value
);
}
else
{
set
(
HOST
,
null
);
remove
(
HOST
,
null
);
}
}
...
...
@@ -1160,7 +1165,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @since 5.1.4
*/
public
void
setLastModified
(
ZonedDateTime
lastModified
)
{
setZonedDateTime
(
LAST_MODIFIED
,
lastModified
.
withZoneSameInstant
(
ZoneId
.
of
(
"GMT"
)
));
setZonedDateTime
(
LAST_MODIFIED
,
lastModified
.
withZoneSameInstant
(
GMT
));
}
/**
...
...
@@ -1179,7 +1184,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* as specified by the {@code Location} header.
*/
public
void
setLocation
(
@Nullable
URI
location
)
{
set
(
LOCATION
,
(
location
!=
null
?
location
.
toASCIIString
()
:
null
));
set
OrRemove
(
LOCATION
,
(
location
!=
null
?
location
.
toASCIIString
()
:
null
));
}
/**
...
...
@@ -1197,7 +1202,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Set the (new) value of the {@code Origin} header.
*/
public
void
setOrigin
(
@Nullable
String
origin
)
{
set
(
ORIGIN
,
origin
);
set
OrRemove
(
ORIGIN
,
origin
);
}
/**
...
...
@@ -1212,7 +1217,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Set the (new) value of the {@code Pragma} header.
*/
public
void
setPragma
(
@Nullable
String
pragma
)
{
set
(
PRAGMA
,
pragma
);
set
OrRemove
(
PRAGMA
,
pragma
);
}
/**
...
...
@@ -1244,7 +1249,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Set the (new) value of the {@code Upgrade} header.
*/
public
void
setUpgrade
(
@Nullable
String
upgrade
)
{
set
(
UPGRADE
,
upgrade
);
set
OrRemove
(
UPGRADE
,
upgrade
);
}
/**
...
...
@@ -1406,10 +1411,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
List
<
String
>
result
=
new
ArrayList
<>();
for
(
String
value
:
values
)
{
if
(
value
!=
null
)
{
String
[]
tokens
=
StringUtils
.
tokenizeToStringArray
(
value
,
","
);
for
(
String
token
:
tokens
)
{
result
.
add
(
token
);
}
Collections
.
addAll
(
result
,
StringUtils
.
tokenizeToStringArray
(
value
,
","
));
}
}
return
result
;
...
...
@@ -1468,16 +1470,32 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
*/
protected
String
toCommaDelimitedString
(
List
<
String
>
headerValues
)
{
StringBuilder
builder
=
new
StringBuilder
();
for
(
Iterator
<
String
>
it
=
headerValues
.
iterator
();
it
.
hasNext
();
)
{
for
(
Iterator
<
String
>
it
=
headerValues
.
iterator
();
it
.
hasNext
();)
{
String
val
=
it
.
next
();
if
(
val
!=
null
)
{
builder
.
append
(
val
);
if
(
it
.
hasNext
())
{
builder
.
append
(
", "
);
}
}
}
return
builder
.
toString
();
}
/**
* Set the given header value, or remove the header if {@code null}.
* @param headerName the header name
* @param headerValue the header value, or {@code null} for none
*/
private
void
setOrRemove
(
String
headerName
,
@Nullable
String
headerValue
)
{
if
(
headerValue
!=
null
)
{
set
(
headerName
,
headerValue
);
}
else
{
remove
(
headerName
);
}
}
// MultiValueMap implementation
...
...
spring-web/src/main/java/org/springframework/http/ResponseEntity.java
浏览文件 @
5bbbc82e
...
...
@@ -537,10 +537,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
@Override
public
BodyBuilder
cacheControl
(
CacheControl
cacheControl
)
{
String
ccValue
=
cacheControl
.
getHeaderValue
();
if
(
ccValue
!=
null
)
{
this
.
headers
.
setCacheControl
(
cacheControl
.
getHeaderValue
());
}
this
.
headers
.
setCacheControl
(
cacheControl
);
return
this
;
}
...
...
spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java
浏览文件 @
5bbbc82e
...
...
@@ -38,8 +38,8 @@ import java.util.TimeZone;
import
org.hamcrest.Matchers
;
import
org.junit.Test
;
import
static
java
.
time
.
format
.
DateTimeFormatter
.
RFC_1123_DATE_TIME
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
java
.
time
.
format
.
DateTimeFormatter
.
*
;
import
static
org
.
hamcrest
.
Matchers
.
*
;
import
static
org
.
junit
.
Assert
.*;
/**
...
...
@@ -355,11 +355,18 @@ public class HttpHeadersTests {
assertEquals
(
"Invalid Cache-Control header"
,
"no-cache"
,
headers
.
getFirst
(
"cache-control"
));
}
@Test
public
void
cacheControlEmpty
()
{
headers
.
setCacheControl
(
CacheControl
.
empty
());
assertNull
(
"Invalid Cache-Control header"
,
headers
.
getCacheControl
());
assertNull
(
"Invalid Cache-Control header"
,
headers
.
getFirst
(
"cache-control"
));
}
@Test
public
void
cacheControlAllValues
()
{
headers
.
add
(
HttpHeaders
.
CACHE_CONTROL
,
"max-age=1000, public"
);
headers
.
add
(
HttpHeaders
.
CACHE_CONTROL
,
"s-maxage=1000"
);
assert
That
(
headers
.
getCacheControl
(),
is
(
"max-age=1000, public, s-maxage=1000"
));
assert
Equals
(
"max-age=1000, public, s-maxage=1000"
,
headers
.
getCacheControl
(
));
}
@Test
...
...
@@ -375,7 +382,7 @@ public class HttpHeadersTests {
@Test
// SPR-11917
public
void
getAllowEmptySet
()
{
headers
.
setAllow
(
Collections
.
<
HttpMethod
>
emptySet
());
headers
.
setAllow
(
Collections
.
emptySet
());
assertThat
(
headers
.
getAllow
(),
Matchers
.
emptyCollectionOf
(
HttpMethod
.
class
));
}
...
...
spring-web/src/test/java/org/springframework/http/client/SimpleClientHttpRequestFactoryTests.java
浏览文件 @
5bbbc82e
/*
* Copyright 2002-201
5
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.
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java
浏览文件 @
5bbbc82e
...
...
@@ -177,10 +177,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
public
ServerResponse
.
BodyBuilder
cacheControl
(
CacheControl
cacheControl
)
{
String
ccValue
=
cacheControl
.
getHeaderValue
();
if
(
ccValue
!=
null
)
{
this
.
headers
.
setCacheControl
(
cacheControl
.
getHeaderValue
());
}
this
.
headers
.
setCacheControl
(
cacheControl
);
return
this
;
}
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java
浏览文件 @
5bbbc82e
...
...
@@ -345,11 +345,9 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
}
// Apply cache settings, if any
if
(
getCacheControl
()
!=
null
)
{
String
value
=
getCacheControl
().
getHeaderValue
();
if
(
value
!=
null
)
{
exchange
.
getResponse
().
getHeaders
().
setCacheControl
(
value
);
}
CacheControl
cacheControl
=
getCacheControl
();
if
(
cacheControl
!=
null
)
{
exchange
.
getResponse
().
getHeaders
().
setCacheControl
(
cacheControl
);
}
// Check the media type for the resource
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录