Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
6b082ebd
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6b082ebd
编写于
9月 13, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Escape unsafe characters in urls.
Fixes #133
上级
05106967
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
87 addition
and
15 deletion
+87
-15
integration/volley/src/androidTest/java/com/bumptech/glide/integration/volley/VolleyStreamFetcherServerTest.java
...ide/integration/volley/VolleyStreamFetcherServerTest.java
+2
-1
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyStreamFetcher.java
...umptech/glide/integration/volley/VolleyStreamFetcher.java
+10
-5
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyUrlLoader.java
...om/bumptech/glide/integration/volley/VolleyUrlLoader.java
+1
-1
library/src/androidTest/java/com/bumptech/glide/load/model/GlideUrlTest.java
...Test/java/com/bumptech/glide/load/model/GlideUrlTest.java
+22
-0
library/src/main/java/com/bumptech/glide/load/model/GlideUrl.java
...src/main/java/com/bumptech/glide/load/model/GlideUrl.java
+28
-6
library/src/main/java/com/bumptech/glide/load/model/stream/HttpUrlGlideUrlLoader.java
...mptech/glide/load/model/stream/HttpUrlGlideUrlLoader.java
+24
-2
未找到文件。
integration/volley/src/androidTest/java/com/bumptech/glide/integration/volley/VolleyStreamFetcherServerTest.java
浏览文件 @
6b082ebd
...
...
@@ -8,6 +8,7 @@ import com.android.volley.VolleyError;
import
com.android.volley.toolbox.Volley
;
import
com.bumptech.glide.Priority
;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
com.squareup.okhttp.mockwebserver.MockResponse
;
import
com.squareup.okhttp.mockwebserver.MockWebServer
;
import
org.junit.After
;
...
...
@@ -196,7 +197,7 @@ public class VolleyStreamFetcherServerTest {
return
super
.
get
();
}
};
return
new
VolleyStreamFetcher
(
requestQueue
,
url
.
toString
(
),
requestFuture
);
return
new
VolleyStreamFetcher
(
requestQueue
,
new
GlideUrl
(
url
.
toString
()
),
requestFuture
);
}
private
static
String
isToString
(
InputStream
is
)
throws
IOException
{
...
...
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyStreamFetcher.java
浏览文件 @
6b082ebd
...
...
@@ -7,24 +7,27 @@ import com.android.volley.Response;
import
com.android.volley.toolbox.HttpHeaderParser
;
import
com.bumptech.glide.Priority
;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
java.io.ByteArrayInputStream
;
import
java.io.InputStream
;
import
java.lang.reflect.Method
;
/**
* A DataFetcher backed by volley for fetching images via http.
*/
public
class
VolleyStreamFetcher
implements
DataFetcher
<
InputStream
>
{
private
final
RequestQueue
requestQueue
;
private
final
String
url
;
private
final
GlideUrl
url
;
private
VolleyRequestFuture
<
InputStream
>
requestFuture
;
@SuppressWarnings
(
"unused"
)
public
VolleyStreamFetcher
(
RequestQueue
requestQueue
,
String
url
)
{
public
VolleyStreamFetcher
(
RequestQueue
requestQueue
,
GlideUrl
url
)
{
this
(
requestQueue
,
url
,
null
);
}
public
VolleyStreamFetcher
(
RequestQueue
requestQueue
,
String
url
,
VolleyRequestFuture
<
InputStream
>
requestFuture
)
{
public
VolleyStreamFetcher
(
RequestQueue
requestQueue
,
GlideUrl
url
,
VolleyRequestFuture
<
InputStream
>
requestFuture
)
{
this
.
requestQueue
=
requestQueue
;
this
.
url
=
url
;
this
.
requestFuture
=
requestFuture
;
...
...
@@ -35,7 +38,9 @@ public class VolleyStreamFetcher implements DataFetcher<InputStream> {
@Override
public
InputStream
loadData
(
Priority
priority
)
throws
Exception
{
GlideRequest
request
=
new
GlideRequest
(
url
,
requestFuture
,
glideToVolleyPriority
(
priority
));
// Make sure the string url safely encodes non ascii characters.
String
stringUrl
=
url
.
toURL
().
toString
();
GlideRequest
request
=
new
GlideRequest
(
stringUrl
,
requestFuture
,
glideToVolleyPriority
(
priority
));
requestFuture
.
setRequest
(
requestQueue
.
add
(
request
));
...
...
@@ -49,7 +54,7 @@ public class VolleyStreamFetcher implements DataFetcher<InputStream> {
@Override
public
String
getId
()
{
return
url
;
return
url
.
toString
()
;
}
@Override
...
...
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyUrlLoader.java
浏览文件 @
6b082ebd
...
...
@@ -67,6 +67,6 @@ public class VolleyUrlLoader implements ModelLoader<GlideUrl, InputStream> {
@Override
public
DataFetcher
<
InputStream
>
getResourceFetcher
(
GlideUrl
url
,
int
width
,
int
height
)
{
return
new
VolleyStreamFetcher
(
requestQueue
,
url
.
toString
()
,
new
VolleyRequestFuture
<
InputStream
>());
return
new
VolleyStreamFetcher
(
requestQueue
,
url
,
new
VolleyRequestFuture
<
InputStream
>());
}
}
library/src/androidTest/java/com/bumptech/glide/load/model/GlideUrlTest.java
浏览文件 @
6b082ebd
...
...
@@ -60,4 +60,26 @@ public class GlideUrlTest {
assertEquals
(
expected
,
glideUrl
.
toString
());
}
@Test
public
void
testIssue133
()
throws
MalformedURLException
{
// u00e0=à
final
String
original
=
"http://www.commitstrip.com/wp-content/uploads/2014/07/"
+
"Excel-\u00E0-toutes-les-sauces-650-finalenglish.jpg"
;
final
String
escaped
=
"http://www.commitstrip.com/wp-content/uploads/2014/07/"
+
"Excel-%C3%A0-toutes-les-sauces-650-finalenglish.jpg"
;
GlideUrl
glideUrlFromString
=
new
GlideUrl
(
original
);
assertEquals
(
escaped
,
glideUrlFromString
.
toURL
().
toString
());
GlideUrl
glideUrlFromEscapedString
=
new
GlideUrl
(
escaped
);
assertEquals
(
escaped
,
glideUrlFromEscapedString
.
toURL
().
toString
());
GlideUrl
glideUrlFromUrl
=
new
GlideUrl
(
new
URL
(
original
));
assertEquals
(
escaped
,
glideUrlFromUrl
.
toURL
().
toString
());
GlideUrl
glideUrlFromEscapedUrl
=
new
GlideUrl
(
new
URL
(
escaped
));
assertEquals
(
escaped
,
glideUrlFromEscapedUrl
.
toURL
().
toString
());
}
}
\ No newline at end of file
library/src/main/java/com/bumptech/glide/load/model/GlideUrl.java
浏览文件 @
6b082ebd
package
com.bumptech.glide.load.model
;
import
android.net.Uri
;
import
android.text.TextUtils
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
/**
*
This is a simple wrapper for strings representing http/https URLs. new URL() is an excessively expensive operation
*
that may be unnecessary if the class loading the image from the URL doesn't actually require a URL object
.
*
A wrapper for strings representing http/https URLs responsible for ensuring URLs are properly escaped and avoiding
*
unnecessary URL instantiations for loaders that require only string urls rather than URL objects
.
*
* Users wishing to replace the class for handling URLs must register a factory using GlideUrl.
* <p>
* Users wishing to replace the class for handling URLs must register a factory using GlideUrl.
* </p>
*
* <p>
* To obtain a properly escaped URL, call {@link #toURL()}. To obtain a properly escaped string URL, call
* {@link #toURL()} and then {@link java.net.URL#toString()}.
* </p>
*/
public
class
GlideUrl
{
private
String
stringUrl
;
private
static
final
String
ALLOWED_URI_CHARS
=
"@#&=*+-_.,:!?()/~'%"
;
private
URL
url
;
private
URL
safeUrl
;
public
GlideUrl
(
URL
url
)
{
if
(
url
==
null
)
{
...
...
@@ -32,10 +43,21 @@ public class GlideUrl {
}
public
URL
toURL
()
throws
MalformedURLException
{
if
(
url
==
null
)
{
url
=
new
URL
(
stringUrl
);
return
getSafeUrl
();
}
// See http://stackoverflow.com/questions/3286067/url-encoding-in-android. Although the answer using URI would work,
// using it would require both decoding and encoding each string which is more complicated, slower and generates
// more objects than the solution below. See also issue #133.
private
URL
getSafeUrl
()
throws
MalformedURLException
{
if
(
safeUrl
!=
null
)
{
return
safeUrl
;
}
return
url
;
String
unsafe
=
toString
();
String
safe
=
Uri
.
encode
(
unsafe
,
ALLOWED_URI_CHARS
);
safeUrl
=
new
URL
(
safe
);
return
safeUrl
;
}
@Override
...
...
library/src/main/java/com/bumptech/glide/load/model/stream/HttpUrlGlideUrlLoader.java
浏览文件 @
6b082ebd
...
...
@@ -5,6 +5,7 @@ import com.bumptech.glide.load.data.DataFetcher;
import
com.bumptech.glide.load.data.HttpUrlFetcher
;
import
com.bumptech.glide.load.model.GenericLoaderFactory
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
com.bumptech.glide.load.model.ModelCache
;
import
com.bumptech.glide.load.model.ModelLoader
;
import
com.bumptech.glide.load.model.ModelLoaderFactory
;
...
...
@@ -16,13 +17,17 @@ import java.io.InputStream;
*/
public
class
HttpUrlGlideUrlLoader
implements
ModelLoader
<
GlideUrl
,
InputStream
>
{
private
ModelCache
<
GlideUrl
,
GlideUrl
>
modelCache
;
/**
* The default factory for {@link com.bumptech.glide.load.model.stream.HttpUrlGlideUrlLoader}s.
*/
public
static
class
Factory
implements
ModelLoaderFactory
<
GlideUrl
,
InputStream
>
{
private
ModelCache
<
GlideUrl
,
GlideUrl
>
modelCache
=
new
ModelCache
<
GlideUrl
,
GlideUrl
>(
500
);
@Override
public
ModelLoader
<
GlideUrl
,
InputStream
>
build
(
Context
context
,
GenericLoaderFactory
factories
)
{
return
new
HttpUrlGlideUrlLoader
();
return
new
HttpUrlGlideUrlLoader
(
modelCache
);
}
@Override
...
...
@@ -31,8 +36,25 @@ public class HttpUrlGlideUrlLoader implements ModelLoader<GlideUrl, InputStream>
}
}
public
HttpUrlGlideUrlLoader
()
{
// Empty.
}
public
HttpUrlGlideUrlLoader
(
ModelCache
<
GlideUrl
,
GlideUrl
>
modelCache
)
{
this
.
modelCache
=
modelCache
;
}
@Override
public
DataFetcher
<
InputStream
>
getResourceFetcher
(
GlideUrl
model
,
int
width
,
int
height
)
{
return
new
HttpUrlFetcher
(
model
);
// GlideUrls memoize parsed URLs so caching them saves a few object instantiations and time spent parsing urls.
GlideUrl
url
=
model
;
if
(
modelCache
!=
null
)
{
url
=
modelCache
.
get
(
model
,
0
,
0
);
if
(
url
==
null
)
{
modelCache
.
put
(
model
,
0
,
0
,
model
);
url
=
model
;
}
}
return
new
HttpUrlFetcher
(
url
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录