diff --git a/library/src/main/java/com/lagradost/nicehttp/NiceResponse.kt b/library/src/main/java/com/lagradost/nicehttp/NiceResponse.kt index 37925f8..c22a87d 100644 --- a/library/src/main/java/com/lagradost/nicehttp/NiceResponse.kt +++ b/library/src/main/java/com/lagradost/nicehttp/NiceResponse.kt @@ -5,7 +5,6 @@ import okhttp3.Response import okhttp3.internal.closeQuietly import org.jsoup.Jsoup import org.jsoup.nodes.Document -import java.io.StringWriter val Response.cookies: Map get() = this.headers.getCookies("set-cookie") @@ -32,6 +31,13 @@ class NiceResponse( // println("Warning: Using text after body is already consumed. Defaulting to textLarge.") return@lazy textLarge } + val size = size + if (size != null && size > MAX_TEXT_SIZE) { + throw IllegalStateException( + "Called .text on a text file with Content-Length header > $MAX_TEXT_SIZE bytes, this throws an exception to prevent OOM. " + + "If you know what you are doing, use .body/textLarge/documentLarge to download larger files" + ) + } OkioHelper.readLimited(body, MAX_TEXT_SIZE) } } @@ -48,8 +54,8 @@ class NiceResponse( /** Size, as reported by Content-Length */ val size by lazy { - (okhttpResponse.headers["Content-Length"] - ?: okhttpResponse.headers["content-length"])?.toLongOrNull() + // the get operator for headers is case insensitive + okhttpResponse.headers["content-length"]?.toLongOrNull() } val isSuccessful = okhttpResponse.isSuccessful diff --git a/library/src/main/java/com/lagradost/nicehttp/OkioHelper.kt b/library/src/main/java/com/lagradost/nicehttp/OkioHelper.kt index a78aa7f..390e97a 100644 --- a/library/src/main/java/com/lagradost/nicehttp/OkioHelper.kt +++ b/library/src/main/java/com/lagradost/nicehttp/OkioHelper.kt @@ -66,7 +66,7 @@ object OkioHelper { val size = source.buffer.writeMax(sourceField, max) if (size >= max) { throw IllegalStateException("Called .text on a text file with Content-Length > $max bytes, this throws an exception to prevent OOM. " + - "To avoid this use .body/textLarge/documentLarge") + "If you know what you are doing, use .body/textLarge/documentLarge to download larger files") } source.buffer.readString(charset) } catch (t: SecurityException) {