From 59399453c4fe230caa55aabb03358867dce11e36 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:56:40 +0800 Subject: [PATCH] Fix IP ban error message and parse error logging (#979) --- .../kotlin/com/hippo/ehviewer/client/EhEngine.kt | 16 +++++++++------- app/src/main/rust/src/lib.rs | 5 +++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt index 12a39a293..e23326571 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt @@ -60,7 +60,6 @@ import io.ktor.client.statement.bodyAsChannel import io.ktor.utils.io.pool.DirectByteBufferPool import io.ktor.utils.io.pool.useInstance import java.io.File -import java.io.RandomAccessFile import java.nio.ByteBuffer import kotlin.math.ceil import kotlinx.coroutines.CancellationException @@ -71,6 +70,8 @@ import kotlinx.serialization.json.addJsonArray import kotlinx.serialization.json.put import moe.tarsin.coroutines.removeAllSuspend import moe.tarsin.coroutines.runSuspendCatching +import okio.buffer +import okio.sink import org.jsoup.Jsoup import splitties.init.appCtx @@ -84,11 +85,11 @@ private const val U_CONFIG_TEXT = "Selected Profile" fun Either.saveParseError(e: Throwable) { val dir = AppConfig.externalParseErrorDir ?: return val file = File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt") - RandomAccessFile(file, "rw").use { randomAccessFile -> - with(randomAccessFile) { - writeUTF(e.message + "\n") - onLeft { writeUTF(it) } - onRight { channel.write(it) } + file.sink().buffer().use { sink -> + with(sink) { + writeUtf8(e.message + "\n") + onLeft { writeUtf8(it) } + onRight { write(it) } } } } @@ -100,7 +101,7 @@ fun rethrowExactly(code: Int, body: Either, e: Throwable): N // Check sad panda (without panda) val empty = body.fold( { it.isEmpty() }, - { it.limit() == 0 }, + { !it.hasRemaining() }, ) if (empty) { if (EhUtils.isExHentai) { @@ -159,6 +160,7 @@ suspend inline fun HttpStatement.fetchUsingAsByteBuffer(crossinline block: s runSuspendCatching { block(buffer) }.onFailure { + buffer.rewind() rethrowExactly(response.status.value, buffer.right(), it) }.getOrThrow() } diff --git a/app/src/main/rust/src/lib.rs b/app/src/main/rust/src/lib.rs index b69d41034..7bdee0bf8 100644 --- a/app/src/main/rust/src/lib.rs +++ b/app/src/main/rust/src/lib.rs @@ -15,7 +15,7 @@ extern crate sha1; extern crate tl; use android_logger::Config; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, ensure, Result}; use jni::objects::JByteBuffer; use jni::sys::{jint, jobject, JavaVM, JNI_VERSION_1_6}; use jni::JNIEnv; @@ -114,7 +114,8 @@ where let buffer = deref_mut_direct_bytebuffer(env, str)?; let value = { let html = unsafe { from_utf8_unchecked(&buffer[..limit as usize]) }; - let dom = tl::parse(html, ParserOptions::default()).map_err(|_| anyhow!("{html}"))?; + let dom = tl::parse(html, ParserOptions::default()).map_err(|e| anyhow!(e))?; + ensure!(dom.version().is_some(), "{html}"); f(&dom, html)? }; let mut cursor = Cursor::new(buffer);