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

KTOR-7876 Fix Sink.writeText corrupts long streams for Jetty server #4526

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions ktor-io/common/src/io/ktor/utils/io/core/Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,14 @@ public fun Source.readTextExactCharacters(charactersCount: Int, charset: Charset
}

/**
* Writes [text] characters in range \[[fromIndex] .. [toIndex]) with the specified [charset]
* Writes [text] characters in range [fromIndex] .. [toIndex]) with the specified [charset]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the KDoc and mention explicitly included and excluded part of the ranges?

*/
public fun Sink.writeText(
text: CharSequence,
fromIndex: Int = 0,
toIndex: Int = text.length,
charset: Charset = Charsets.UTF_8
) {
if (charset === Charsets.UTF_8) {
return writeString(text.toString(), fromIndex, toIndex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell me why whiteStrings corrupts non ASCII? Should we log this to the kotlinx-io instead?

}

charset.newEncoder().encodeToImpl(this, text, fromIndex, toIndex)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.servlet.jakarta.*
import io.ktor.server.testing.suites.*
import io.ktor.utils.io.*
import jakarta.servlet.http.*
import org.eclipse.jetty.server.*
import org.eclipse.jetty.server.handler.*
Expand Down Expand Up @@ -55,6 +56,23 @@ class JettyHttpServerJvmTest : HttpServerJvmTestSuite<JettyApplicationEngine, Je
}
}

@Test
fun testWriteNonASCIIChars() = runTest {
val input = (1..6000).map { if (it % 2 == 0) 'å' else (64 + it % 10).toChar() }.joinToString("")

createAndStartServer {
get {
call.respondBytesWriter {
writeStringUtf8(input)
}
}
}

withUrl("/", {}) {
assertEquals(input, call.response.bodyAsText())
}
}

private fun Server.addAttributesHandler() {
addEventListener(
object : LifeCycle.Listener {
Expand Down
Loading