@@ -14,9 +14,9 @@ internal class LoginTokenInterceptor(private val loginTokenManager: LoginTokenMa
1414
1515 private companion object {
1616 private const val LOGIN_TOKEN_HEADER = " proxer-api-token"
17- private const val MAX_PEEK_BYTE_COUNT = 512L
17+ private const val MAX_PEEK_BYTE_COUNT = 1_048_576L
1818
19- private val LOGIN_TOKEN_PATTERN = Regex (" \" token\" :.*?\" (.{255} )\" " , RegexOption .DOT_MATCHES_ALL )
19+ private val LOGIN_TOKEN_PATTERN = Regex (" \" token\" :.*?\" (.+? )\" " , RegexOption .DOT_MATCHES_ALL )
2020 private val ERROR_PATTERN = Regex (" \" code\" :.*?(\\ d+\b ?)" , RegexOption .DOT_MATCHES_ALL )
2121
2222 private val LOGIN_URL = ProxerUrls .apiBase.newBuilder()
@@ -48,31 +48,24 @@ internal class LoginTokenInterceptor(private val loginTokenManager: LoginTokenMa
4848 }
4949 }
5050
51- val response = chain.proceed(newRequestBuilder.build())
52-
53- if (response.isSuccessful) {
54- handleResponse(response)
55- }
56-
57- return response
51+ return handleResponse(chain.proceed(newRequestBuilder.build()))
5852 } else {
5953 return chain.proceed(oldRequest)
6054 }
6155 }
6256
63- private fun handleResponse (response : Response ) {
57+ private fun handleResponse (response : Response ): Response {
6458 val responseBody = peekResponseBody(response)
6559 val url = response.request.url
6660
6761 synchronized(lock) {
68- val existingLoginToken = loginTokenManager.provide()
6962 val potentialError = ERROR_PATTERN .find(responseBody)
7063
7164 if (potentialError != null ) {
7265 val errorCode = potentialError.groupValues[1 ].toInt()
7366 val errorType = ServerErrorType .fromErrorCode(errorCode)
7467
75- if (errorType != ServerErrorType . UNKNOWN && existingLoginToken != null && isLoginError(errorType) ) {
68+ if (errorType. isLoginError) {
7669 loginTokenManager.persist(null )
7770 }
7871 } else if (url.pathSegments == LOGIN_URL .pathSegments) {
@@ -87,32 +80,11 @@ internal class LoginTokenInterceptor(private val loginTokenManager: LoginTokenMa
8780 loginTokenManager.persist(null )
8881 }
8982 }
90- }
9183
92- private fun peekResponseBody (response : Response ): String {
93- return response.body?.source()
94- ?.let {
95- it.request(MAX_PEEK_BYTE_COUNT )
96-
97- it.buffer.snapshot().utf8()
98- }
99- ? : " "
84+ return response
10085 }
10186
102- private fun isLoginError (errorType : ServerErrorType ): Boolean {
103- return when (errorType) {
104- ServerErrorType .INVALID_TOKEN ,
105- ServerErrorType .NOTIFICATIONS_LOGIN_REQUIRED ,
106- ServerErrorType .UCP_LOGIN_REQUIRED ,
107- ServerErrorType .INFO_LOGIN_REQUIRED ,
108- ServerErrorType .LOGIN_ALREADY_LOGGED_IN ,
109- ServerErrorType .LOGIN_DIFFERENT_USER_ALREADY_LOGGED_IN ,
110- ServerErrorType .MESSAGES_LOGIN_REQUIRED ,
111- ServerErrorType .CHAT_LOGIN_REQUIRED ,
112- ServerErrorType .USER_2FA_SECRET_REQUIRED ,
113- ServerErrorType .ANIME_LOGIN_REQUIRED -> true
114-
115- else -> false
116- }
87+ private fun peekResponseBody (response : Response ): String {
88+ return if (response.body != null ) response.peekBody(MAX_PEEK_BYTE_COUNT ).string() else " "
11789 }
11890}
0 commit comments