Skip to content

Commit 1b735db

Browse files
committed
feature: 增加网络连接可控制允许重定向的功能
1 parent 02b2000 commit 1b735db

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

android/JDCache/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
# https://developer.android.com/topic/libraries/support-library/androidx-rn
1717
groupId=com.jingdong.wireless.jdsdk
1818
artifactId=jdhybrid-cache
19-
version=1.0.0-beta-1
19+
version=1.0.0-beta-2
2020

android/JDCache/src/main/java/com/jd/jdcache/service/base/JDCacheFileRepoDelegate.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ open class FileRequestOption(
118118
var method: String = METHOD_GET,
119119
val header: MutableMap<String?, String>? = null,
120120
val userAgent: String? = null,
121-
val cookie: String? = null
121+
val cookie: String? = null,
122+
val allowRedirect: Boolean = true
122123
)
123124

124125
@Keep
@@ -127,11 +128,12 @@ open class FileSaveOption(
127128
header: MutableMap<String?, String>? = null,
128129
userAgent: String? = null,
129130
cookie: String? = null,
131+
allowRedirect: Boolean = true,
130132
val needUnzip: Boolean = false,
131133
val unzipDir: String? = null,
132134
val split: Boolean = false,
133135
val mergeWithFile: String? = null
134-
) : FileRequestOption(method, header, userAgent, cookie)
136+
) : FileRequestOption(method, header, userAgent, cookie, allowRedirect)
135137

136138
@Keep
137139
sealed class FileState {

android/JDCache/src/main/java/com/jd/jdcache/service/base/JDCacheNetDelegate.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ abstract class JDCacheNetDelegate : AbstractDelegate() {
8888
method: String = METHOD_GET,
8989
header: MutableMap<String?, String>? = null,
9090
userAgent: String? = null,
91-
cookie: String? = null
91+
cookie: String? = null,
92+
followRedirect: Boolean = true
9293
): Flow<NetState<File>>?
9394

9495
/**
@@ -100,10 +101,11 @@ abstract class JDCacheNetDelegate : AbstractDelegate() {
100101
method: String = METHOD_GET,
101102
header: MutableMap<String?, String>? = null,
102103
userAgent: String? = null,
103-
cookie: String? = null
104+
cookie: String? = null,
105+
followRedirect: Boolean = true
104106
): NetState<File>? {
105107
return try {
106-
downloadFlow(url, savePath, method, header, userAgent, cookie)?.toList()?.last()
108+
downloadFlow(url, savePath, method, header, userAgent, cookie, followRedirect)?.toList()?.last()
107109
} catch (e: Exception) {
108110
log { e(name, e) }
109111
null

android/JDCache/src/main/java/com/jd/jdcache/service/impl/FileRepo.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ open class FileRepo : JDCacheFileRepoDelegate() {
3434
option?.method ?: METHOD_GET,
3535
option?.header,
3636
option?.userAgent,
37-
option?.cookie
37+
option?.cookie,
38+
followRedirect = option?.allowRedirect?:true
3839
)?.map { netState ->
3940
when(netState) {
4041
is NetState.Complete -> {
@@ -71,7 +72,8 @@ open class FileRepo : JDCacheFileRepoDelegate() {
7172
option?.method ?: METHOD_GET,
7273
option?.header,
7374
option?.userAgent,
74-
option?.cookie
75+
option?.cookie,
76+
followRedirect = option?.allowRedirect?:true
7577
)?.map { netState ->
7678
when (netState) {
7779
is NetState.OnStart -> {

android/JDCache/src/main/java/com/jd/jdcache/service/impl/net/NetConnection.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ open class NetConnection : JDCacheNetDelegate() {
107107
method: String,
108108
header: MutableMap<String?, String>?,
109109
userAgent: String?,
110-
cookie: String?
110+
cookie: String?,
111+
followRedirect: Boolean
111112
): Flow<NetState<File>>? {
112113
if (url.isEmpty()) {
113114
log { e(name, "Cannot download file, because url is empty.") }
@@ -119,6 +120,10 @@ open class NetConnection : JDCacheNetDelegate() {
119120
}
120121
val request = FileRequest(url, savePath)
121122
request.method = method
123+
request.header = header
124+
request.userAgent = userAgent
125+
request.cookies = cookie
126+
request.allowRedirect = followRedirect
122127
return request.connectFlow()
123128
}
124129

0 commit comments

Comments
 (0)