Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http-client-cio] Responses with more than 2048 characters in header result in IndexOutOfBoundsException #419

Closed
trathschlag opened this issue May 21, 2018 · 1 comment
Assignees
Labels

Comments

@trathschlag
Copy link

Hello everybody,
I ran into a problem with the CIO HTTP-Client when getting responses where the HTTP header exceeds 2048 characters:

java.lang.IndexOutOfBoundsException: null
	at java.nio.Buffer.checkIndex(Buffer.java:540)
	at java.nio.DirectCharBufferS.get(DirectCharBufferS.java:253)
	at io.ktor.http.cio.internals.CharBufferBuilder.getImpl(CharBufferBuilder.kt:21)
	at io.ktor.http.cio.internals.CharBufferBuilder.get(CharBufferBuilder.kt:18)
	at io.ktor.http.cio.internals.CharBufferBuilder.charAt(CharBufferBuilder.kt:6)
	at io.ktor.http.cio.internals.TokenizerKt.findColonOrSpace(Tokenizer.kt:68)
	at io.ktor.http.cio.HttpParserKt.parseHeaders(HttpParser.kt:81)
	at io.ktor.http.cio.HttpParserKt.parseResponse(HttpParser.kt:54)
	at io.ktor.http.cio.HttpParserKt$parseResponse$1.doResume(HttpParser.kt)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:53)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:53)
	at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
	at kotlinx.coroutines.experimental.io.internal.MutableDelegateContinuation.run(MutableDelegateContinuation.kt:14)
	at io.ktor.network.util.IOCoroutineDispatcher$IODispatchedTask.run(IOCoroutineDispatcher.kt)
	at io.ktor.network.util.IOCoroutineDispatcher$IOThread$run$1.doResume(IOCoroutineDispatcher.kt:78)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
	at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
	at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:25)
	at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
	at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:244)
	at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:185)
	at io.ktor.network.util.IOCoroutineDispatcher$IOThread.run(IOCoroutineDispatcher.kt:73)

The problem seems to be, that in CharBuffersBuilder the CHAR_BUFFER_SIZE from CharBufferPool is used mutliple times for offset calculation. As it turns out, this constant holds the size of the CharBuffer in bytes while the CharBuffer itself uses 16-bit characters. The code assumes the CharBuffer to be able to hold 4096 characters but this is acutally the size in bytes and the CharBuffer is only 2048 characters big. After reading 2048 characters from a response with a long header section, the code crashes.

@e5l e5l self-assigned this May 21, 2018
@e5l
Copy link
Member

e5l commented May 21, 2018

Thnx for the report, there is a bug here. WIP

@e5l e5l added the bug label Jun 8, 2018
e5l added a commit that referenced this issue Jun 26, 2018
@e5l e5l closed this as completed Jun 26, 2018
schleinzer pushed a commit to schleinzer/ktor that referenced this issue Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants