Skip to content

Commit d9e01d0

Browse files
committed
优化拉取远程配置的判断
1 parent e25613a commit d9e01d0

File tree

1 file changed

+16
-4
lines changed
  • app/src/main/java/me/teble/xposed/autodaily/utils

1 file changed

+16
-4
lines changed

app/src/main/java/me/teble/xposed/autodaily/utils/RepoUtil.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.teble.xposed.autodaily.utils
22

3+
import kotlinx.serialization.json.JsonArray
4+
import kotlinx.serialization.json.JsonElement
5+
import kotlinx.serialization.json.JsonObject
36
import okhttp3.Request
47

58
enum class FileEnum(val path: String) {
@@ -24,14 +27,23 @@ object RepoFileLoader {
2427

2528
private var repoUrl = originRepoUrl
2629

27-
private fun loadRepoFile(path: String): String? {
30+
private fun loadRepoFile(path: String, verifyJson: Boolean): String? {
2831
LogUtil.d("curr repo url: $repoUrl")
2932
val url = "$repoUrl/$path"
3033
val req = Request.Builder().url(url).build()
3134
try {
3235
val response = client.newCall(req).execute()
3336
if (response.isSuccessful) {
34-
return response.body!!.string()
37+
val content = response.body!!.string()
38+
if (verifyJson) {
39+
val cls = content.parse<JsonElement>()::class.java
40+
LogUtil.d("verify json content: $cls")
41+
if (cls != JsonObject::class.java
42+
&& cls != JsonArray::class.java) {
43+
throw IllegalArgumentException("invalid json: $content")
44+
}
45+
}
46+
return content
3547
}
3648
LogUtil.w("load repo file failed: $url, code: ${response.code}, response: ${response.body}")
3749
return null
@@ -40,15 +52,15 @@ object RepoFileLoader {
4052
val index = repoUrls.indexOf(repoUrl)
4153
if (index < repoUrls.size - 1) {
4254
repoUrl = repoUrls[index + 1]
43-
return loadRepoFile(path)
55+
return loadRepoFile(path, verifyJson)
4456
}
4557
throw e
4658
}
4759
}
4860

4961
fun load(fileEnum: FileEnum): String? {
5062
return runCatching {
51-
loadRepoFile(fileEnum.path)
63+
loadRepoFile(fileEnum.path, fileEnum.path.endsWith(".json"))
5264
}.getOrNull()
5365
}
5466
}

0 commit comments

Comments
 (0)