khttp icon indicating copy to clipboard operation
khttp copied to clipboard

Khttp fails when reading from HEAD request with default "Accept-Encoding".

Open xobotun opened this issue 4 years ago • 0 comments

It looks like default _connection in GenericResponse here is initialized with GET method, even when underlying GenericRequest is configured correctly with HEAD method.

This, coupled with the default Accept-Encoding header, leads to khttp trying to read from an empty stream, but it first wraps this stream with a gzip wrapper here, which miserably fails.

I've found a temporary solution. Accept-Encoding can be set to nothing and the code will bypass wrapping and won't reach EOF.

This code works:

head("https://eddb.io/archive/v6/commodities.json", mapOf("Accept-Encoding" to "")).headers["Last-Modified"]

This code fails:

head("https://eddb.io/archive/v6/commodities.json").headers["Last-Modified"]

with the following stacktrace:

Exception in thread "main" java.io.EOFException
	at java.base/java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:269)
	at java.base/java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:259)
	at java.base/java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
	at java.base/java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:80)
	at java.base/java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:92)
	at khttp.responses.GenericResponse.getRealInputStream(GenericResponse.kt:197)
	at khttp.responses.GenericResponse.getRaw(GenericResponse.kt:207)
	at khttp.responses.GenericResponse.getContent(GenericResponse.kt:216)
	at khttp.responses.GenericResponse.init$khttp(GenericResponse.kt:377)
	at khttp.KHttp.request(KHttp.kt:61)
	at khttp.KHttp.head(KHttp.kt:35)
	at khttp.KHttp.head$default(KHttp.kt:34)
	at com.xobotun.elite_trade_loop_finder.service.Downloader.getWebLastModified(Downloader.kt:29)
	at com.xobotun.elite_trade_loop_finder.service.Downloader.shouldDownload(Downloader.kt:27)
	at com.xobotun.elite_trade_loop_finder.service.Downloader.access$shouldDownload(Downloader.kt:14)
	at com.xobotun.elite_trade_loop_finder.service.Downloader$renew$1.test(Downloader.kt:21)
	at com.xobotun.elite_trade_loop_finder.service.Downloader$renew$1.test(Downloader.kt:14)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
	at com.xobotun.elite_trade_loop_finder.service.Downloader.renew(Downloader.kt:22)
	at com.xobotun.elite_trade_loop_finder.ApplicationKt.main(Application.kt:16)

Here's what curl returns:

[xobotun@server~]$ curl -I https://eddb.io/archive/v6/commodities.json
HTTP/2 200
date: Sun, 29 Nov 2020 09:50:38 GMT
server: Apache
x-content-type-options: nosniff
last-modified: Sun, 29 Nov 2020 03:18:54 GMT
accept-ranges: bytes
content-length: 115293
cache-control: max-age=0
expires: Sun, 29 Nov 2020 09:50:38 GMT
vary: Accept-Encoding
strict-transport-security: max-age=315360000; includeSubDomains
content-type: application/json; charset=utf-8

I'd like to submit a fix, but I failed to follow where the needed data is not passed correctly. :(

I hope that will hope someone. :)

xobotun avatar Nov 29 '20 10:11 xobotun