Merge pull request #440 from AndyBernie/master

更新java.getCookie&&自定义cookie设置
pull/441/head
kunfei 4 years ago committed by GitHub
commit 04a4c076a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/src/main/assets/updateLog.md
  2. 10
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 4
      app/src/main/java/io/legado/app/help/http/CookieStore.kt
  4. 30
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -6,7 +6,11 @@
**2020/10/23** **2020/10/23**
* 修复选择错误的bug * 修复选择错误的bug
* 修复长图最后一张不能滚动的bug * 修复长图最后一张不能滚动的bug
* js添加java.getCookie(sourceUrl)来获取登录后的cookie by [AndyBernie](https://github.com/AndyBernie) * js添加java.getCookie(sourceUrl:String, key:String? = null)来获取登录后的cookie by [AndyBernie](https://github.com/AndyBernie)
```
java.getCookie("http://baidu.com", null) => userid=1234;pwd=adbcd
java.getCookie("http://baidu.com", "userid") => 1234
```
* 修复简繁转换没有处理标题 * 修复简繁转换没有处理标题
**2020/10/21** **2020/10/21**

@ -123,8 +123,14 @@ interface JsExtensions {
/** /**
*js实现读取cookie *js实现读取cookie
*/ */
fun getCookie(tag: String): String { fun getCookie(tag: String, key: String? = null): String {
return CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
val cookieMap = CookieStore.cookieToMap(cookie)
return if (key != null) {
cookieMap.get(key) ?: ""
} else {
cookie
}
} }
/** /**

@ -43,7 +43,7 @@ object CookieStore : CookiePersistor {
App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) App.db.cookieDao().delete(NetworkUtils.getSubDomain(url))
} }
private fun cookieToMap(cookie: String): MutableMap<String, String> { fun cookieToMap(cookie: String): MutableMap<String, String> {
val cookieMap = mutableMapOf<String, String>() val cookieMap = mutableMapOf<String, String>()
if (cookie.isBlank()) { if (cookie.isBlank()) {
return cookieMap return cookieMap
@ -63,7 +63,7 @@ object CookieStore : CookiePersistor {
return cookieMap return cookieMap
} }
private fun mapToCookie(cookieMap: Map<String, String>?): String? { fun mapToCookie(cookieMap: Map<String, String>?): String? {
if (cookieMap == null || cookieMap.isEmpty()) { if (cookieMap == null || cookieMap.isEmpty()) {
return null return null
} }

@ -281,7 +281,13 @@ class AnalyzeUrl(
fun getResponse(tag: String): Call<String> { fun getResponse(tag: String): Call<String> {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] += ";${cookie}" val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "")
cookieMap.putAll(customCookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap)
newCookie?.let {
headerMap.put("Cookie", it)
}
} }
return when { return when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
@ -312,6 +318,16 @@ class AnalyzeUrl(
if (type != null) { if (type != null) {
return Res(url, StringUtils.byteToHexString(getResponseBytes(tag))) return Res(url, StringUtils.byteToHexString(getResponseBytes(tag)))
} }
val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) {
val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "")
cookieMap.putAll(customCookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap)
newCookie?.let {
headerMap.put("Cookie", it)
}
}
if (useWebView) { if (useWebView) {
val params = AjaxWebView.AjaxParams(url) val params = AjaxWebView.AjaxParams(url)
params.headerMap = headerMap params.headerMap = headerMap
@ -322,10 +338,6 @@ class AnalyzeUrl(
params.tag = tag params.tag = tag
return HttpHelper.ajax(params) return HttpHelper.ajax(params)
} }
val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) {
headerMap["Cookie"] += ";${cookie}"
}
val res = when { val res = when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
@ -356,7 +368,13 @@ class AnalyzeUrl(
if (tag != null) { if (tag != null) {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] += ";${cookie}" val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "")
cookieMap.putAll(customCookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap)
newCookie?.let {
headerMap.put("Cookie", it)
}
} }
} }
val response = when { val response = when {

Loading…
Cancel
Save