Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions library/src/main/java/com/lagradost/nicehttp/NiceResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>
get() = this.headers.getCookies("set-cookie")
Expand All @@ -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)
}
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/lagradost/nicehttp/OkioHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down