scoop:scoop.http.client:0.1.0
src/response/decoding.scoop
Selected production source · src/response/decoding.scoop
1
package scoop.http.client3
import scoop.compression.CompressionFormat4
import scoop.http.*6
/**7
* Whether response content codings are decoded before body delivery.8
*/9
public enum HttpContentDecoding {10
/**11
* Decodes supported gzip, zlib deflate, Brotli, and zstd layers in reverse coding order.12
*/13
Automatic,14
/**15
* Delivers encoded representation bytes unchanged.16
*/17
Raw18
}20
/**21
* Content-decoding mode, transfer buffer size, and maximum compression window.22
*/23
public class HttpDecodingOptions internal constructor(24
/**25
* Automatic decoding or raw encoded bytes.26
*/27
public val mode: HttpContentDecoding = HttpContentDecoding.Automatic,28
/**29
* The positive transfer-buffer size for decoding.30
*/31
public val bufferSize: Long = 8192L,32
/**33
* The maximum compression window accepted by the decoder.34
*/35
public val maxWindowBytes: Long? = None36
) {37
init {38
if (bufferSize <= 0L)39
throw IllegalArgumentException(Some("HTTP decoding buffer size must be positive"))40
if (maxWindowBytes != None && maxWindowBytes!! <= 0L)41
throw IllegalArgumentException(Some("HTTP decoding window limit must be positive"))42
}43
}45
/**46
* Automatic response decoding encountered an unsupported content coding.47
*/48
public class HttpUnsupportedContentCodingException public constructor(49
/**50
* The unsupported normalized content-coding token.51
*/52
public val coding: String53
)54
: Exception(Some("unsupported HTTP content coding: " + coding))56
internal class ClientResponseDecoding(57
val options: HttpDecodingOptions,58
val formats: Array<CompressionFormat>,59
val contentLength: Long?60
)62
internal fun clientDecodingOptions(63
config: HttpClientConfig,64
request: HttpRequest65
): HttpDecodingOptions = request.options.decoding ?: config.decoding67
internal fun clientResponseDecoding(68
call: ClientCall,69
head: HttpResponseHead,70
wireLength: Long?71
): ClientResponseDecoding {72
val options = clientDecodingOptions(call.client.config, call.request)73
val allows = httpResponseAllowsContent(head, call.request.methodBytes)74
val formats = ArrayList<CompressionFormat>()75
if (options.mode == HttpContentDecoding.Automatic && allows) {76
val codings = parseHttpTokenFields(head.fields, "Content-Encoding")77
var index = codings.size78
while (index > 0L) {79
index -= 1L80
val coding = codings[index]81
when (coding) {82
case "identity" -> {}83
case "gzip" -> formats.add(CompressionFormat.Gzip)84
case "x-gzip" -> formats.add(CompressionFormat.Gzip)85
case "deflate" -> formats.add(CompressionFormat.Zlib)86
case "br" -> formats.add(CompressionFormat.Brotli)87
case "zstd" -> formats.add(CompressionFormat.Zstd)88
else -> throw HttpUnsupportedContentCodingException(coding)89
}90
}91
}92
return ClientResponseDecoding(93
options,94
formats.toArray(),95
if (allows && formats.size == 0L)96
wireLength97
else98
None99
)100
}102
internal fun addClientAcceptEncoding(103
fields: ArrayList<HttpField>,104
request: HttpRequest,105
config: HttpClientConfig106
) {107
if (fields.findAll("Accept-Encoding".toByteArray()).size != 0L)108
return109
val range = fields.findAll("Range".toByteArray()).size != 0L110
if (range || clientDecodingOptions(config, request).mode == HttpContentDecoding.Automatic)111
fields.add(112
HttpField(113
"Accept-Encoding".toByteArray(),114
(115
if (range)116
"identity"117
else118
"gzip, deflate, br, zstd"119
).toByteArray()120
)121
)122
}