Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
78fbceff
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 搜索 >>
提交
78fbceff
编写于
11月 03, 2011
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added check for illegal characters when creating an encoded UriComponents object
上级
c290a4e6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
123 addition
and
22 deletion
+123
-22
org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java
...main/java/org/springframework/web/util/UriComponents.java
+111
-21
org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
...va/org/springframework/web/util/UriComponentsBuilder.java
+1
-1
org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsTests.java
...java/org/springframework/web/util/UriComponentsTests.java
+11
-0
未找到文件。
org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java
浏览文件 @
78fbceff
...
...
@@ -37,12 +37,8 @@ import org.springframework.util.StringUtils;
/**
* Represents an immutable collection of URI components, mapping component type to string values. Contains convenience
* getters for all components, as well as the regular {@link Map} implementation. Effectively similar to {@link URI},
* but with more powerful encoding options.
* <p/>
* <strong>Note</strong> that this {@code Map} does not contain entries for {@link Type#PATH_SEGMENT}
* nor {@link Type#QUERY_PARAM}, since those components can occur multiple
* times in the URI. Instead, one can use {@link #getPathSegments()} or {@link #getQueryParams()} respectively.
* getters for all components. Effectively similar to {@link URI}, but with more powerful encoding options and support
* for URI template variables.
*
* @author Arjen Poutsma
* @since 3.1
...
...
@@ -73,28 +69,44 @@ public final class UriComponents {
private
final
boolean
encoded
;
/**
* Package-friendly constructor that creates a new {@code UriComponents} instance from the given parameters. All
* parameters are optional, and can be {@code null}.
*
* @param scheme the scheme
* @param userInfo the user info
* @param host the host
* @param port the port
* @param path the path component
* @param queryParams the query parameters
* @param fragment the fragment
* @param encoded whether the components are encoded
* @param verify whether the components need to be verified to determine whether they contain illegal characters
*/
UriComponents
(
String
scheme
,
String
userInfo
,
String
host
,
int
port
,
PathComponent
path
,
MultiValueMap
<
String
,
String
>
queryParams
,
String
fragment
,
boolean
encoded
)
{
String
userInfo
,
String
host
,
int
port
,
PathComponent
path
,
MultiValueMap
<
String
,
String
>
queryParams
,
String
fragment
,
boolean
encoded
,
boolean
verify
)
{
this
.
scheme
=
scheme
;
this
.
userInfo
=
userInfo
;
this
.
host
=
host
;
this
.
port
=
port
;
this
.
path
=
path
!=
null
?
path
:
NULL_PATH_COMPONENT
;
if
(
queryParams
==
null
)
{
queryParams
=
new
LinkedMultiValueMap
<
String
,
String
>(
0
);
}
this
.
queryParams
=
CollectionUtils
.
unmodifiableMultiValueMap
(
queryParams
);
this
.
queryParams
=
CollectionUtils
.
unmodifiableMultiValueMap
(
queryParams
!=
null
?
queryParams
:
new
LinkedMultiValueMap
<
String
,
String
>(
0
));
this
.
fragment
=
fragment
;
this
.
encoded
=
encoded
;
if
(
verify
)
{
verify
();
}
}
// component getters
// component getters
/**
* Returns the scheme.
...
...
@@ -256,7 +268,7 @@ public final class UriComponents {
String
encodedFragment
=
encodeUriComponent
(
this
.
fragment
,
encoding
,
Type
.
FRAGMENT
);
return
new
UriComponents
(
encodedScheme
,
encodedUserInfo
,
encodedHost
,
this
.
port
,
encodedPath
,
encodedQueryParams
,
encodedFragment
,
true
);
encodedQueryParams
,
encodedFragment
,
true
,
false
);
}
/**
...
...
@@ -307,6 +319,63 @@ public final class UriComponents {
return
bos
.
toByteArray
();
}
// verifying
/**
* Verifies all URI components to determine whether they contain any illegal characters, throwing an
* {@code IllegalArgumentException} if so.
*
* @throws IllegalArgumentException if any of the components contain illegal characters
*/
private
void
verify
()
{
if
(!
encoded
)
{
return
;
}
verifyUriComponent
(
scheme
,
Type
.
SCHEME
);
verifyUriComponent
(
userInfo
,
Type
.
USER_INFO
);
verifyUriComponent
(
host
,
Type
.
HOST
);
path
.
verify
();
for
(
Map
.
Entry
<
String
,
List
<
String
>>
entry
:
queryParams
.
entrySet
())
{
verifyUriComponent
(
entry
.
getKey
(),
Type
.
QUERY_PARAM
);
for
(
String
value
:
entry
.
getValue
())
{
verifyUriComponent
(
value
,
Type
.
QUERY_PARAM
);
}
}
verifyUriComponent
(
fragment
,
Type
.
FRAGMENT
);
}
private
static
void
verifyUriComponent
(
String
source
,
Type
type
)
{
if
(
source
==
null
)
{
return
;
}
int
length
=
source
.
length
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
char
ch
=
source
.
charAt
(
i
);
if
(
ch
==
'%'
)
{
if
((
i
+
2
)
<
length
)
{
char
hex1
=
source
.
charAt
(
i
+
1
);
char
hex2
=
source
.
charAt
(
i
+
2
);
int
u
=
Character
.
digit
(
hex1
,
16
);
int
l
=
Character
.
digit
(
hex2
,
16
);
if
(
u
==
-
1
||
l
==
-
1
)
{
throw
new
IllegalArgumentException
(
"Invalid encoded sequence \""
+
source
.
substring
(
i
)
+
"\""
);
}
i
+=
2
;
}
else
{
throw
new
IllegalArgumentException
(
"Invalid encoded sequence \""
+
source
.
substring
(
i
)
+
"\""
);
}
}
else
if
(!
type
.
isAllowed
(
ch
))
{
throw
new
IllegalArgumentException
(
"Invalid character '"
+
ch
+
"' for "
+
type
.
name
()
+
" in \""
+
source
+
"\""
);
}
}
}
// expanding
/**
...
...
@@ -356,7 +425,7 @@ public final class UriComponents {
String
expandedFragment
=
expandUriComponent
(
this
.
fragment
,
uriVariables
);
return
new
UriComponents
(
expandedScheme
,
expandedUserInfo
,
expandedHost
,
this
.
port
,
expandedPath
,
expandedQueryParams
,
expandedFragment
,
false
);
expandedQueryParams
,
expandedFragment
,
false
,
false
);
}
private
static
String
expandUriComponent
(
String
source
,
UriTemplateVariables
uriVariables
)
{
...
...
@@ -523,7 +592,7 @@ public final class UriComponents {
/**
* Enumeration used to identify the parts of a URI.
* <p/>
*
<p>
Contains methods to indicate whether a given character is valid in a specific URI component.
* Contains methods to indicate whether a given character is valid in a specific URI component.
*
* @author Arjen Poutsma
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
...
...
@@ -681,6 +750,8 @@ public final class UriComponents {
PathComponent
encode
(
String
encoding
)
throws
UnsupportedEncodingException
;
void
verify
();
PathComponent
expand
(
UriTemplateVariables
uriVariables
);
}
...
...
@@ -711,6 +782,10 @@ public final class UriComponents {
return
new
FullPathComponent
(
encodedPath
);
}
public
void
verify
()
{
verifyUriComponent
(
path
,
Type
.
PATH
);
}
public
PathComponent
expand
(
UriTemplateVariables
uriVariables
)
{
String
expandedPath
=
expandUriComponent
(
getPath
(),
uriVariables
);
return
new
FullPathComponent
(
expandedPath
);
...
...
@@ -771,6 +846,12 @@ public final class UriComponents {
return
new
PathSegmentComponent
(
encodedPathSegments
);
}
public
void
verify
()
{
for
(
String
pathSegment
:
getPathSegments
())
{
verifyUriComponent
(
pathSegment
,
Type
.
PATH_SEGMENT
);
}
}
public
PathComponent
expand
(
UriTemplateVariables
uriVariables
)
{
List
<
String
>
pathSegments
=
getPathSegments
();
List
<
String
>
expandedPathSegments
=
new
ArrayList
<
String
>(
pathSegments
.
size
());
...
...
@@ -834,6 +915,12 @@ public final class UriComponents {
return
new
PathComponentComposite
(
encodedComponents
);
}
public
void
verify
()
{
for
(
PathComponent
pathComponent
:
pathComponents
)
{
pathComponent
.
verify
();
}
}
public
PathComponent
expand
(
UriTemplateVariables
uriVariables
)
{
List
<
PathComponent
>
expandedComponents
=
new
ArrayList
<
PathComponent
>(
pathComponents
.
size
());
for
(
PathComponent
pathComponent
:
pathComponents
)
{
...
...
@@ -862,6 +949,9 @@ public final class UriComponents {
return
this
;
}
public
void
verify
()
{
}
public
PathComponent
expand
(
UriTemplateVariables
uriVariables
)
{
return
this
;
}
...
...
org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
浏览文件 @
78fbceff
...
...
@@ -217,7 +217,7 @@ public class UriComponentsBuilder {
* @return the URI components
*/
public
UriComponents
build
(
boolean
encoded
)
{
return
new
UriComponents
(
scheme
,
userInfo
,
host
,
port
,
pathBuilder
.
build
(),
queryParams
,
fragment
,
encoded
);
return
new
UriComponents
(
scheme
,
userInfo
,
host
,
port
,
pathBuilder
.
build
(),
queryParams
,
fragment
,
encoded
,
true
);
}
// URI components methods
...
...
org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsTests.java
浏览文件 @
78fbceff
...
...
@@ -59,4 +59,15 @@ public class UriComponentsTests {
UriComponentsBuilder
.
fromPath
(
"/{foo}"
).
build
().
encode
().
expand
(
"bar"
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
invalidCharacters
()
{
UriComponentsBuilder
.
fromPath
(
"/{foo}"
).
build
(
true
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
invalidEncodedSequence
()
{
UriComponentsBuilder
.
fromPath
(
"/fo%2o"
).
build
(
true
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录