Skip to content

Commit

Permalink
fixup! KTOR-4379 Validate body size equals Content-Length
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Jun 24, 2022
1 parent a36b62c commit 2996cbd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Expand Up @@ -7,8 +7,7 @@ package io.ktor.http.cio
import io.ktor.http.*
import io.ktor.http.cio.internals.*
import io.ktor.utils.io.*
import java.io.IOException

import io.ktor.utils.io.errors.*
/**
* @return `true` if an http upgrade is expected accoding to request [method], [upgrade] header value and
* parsed [connectionOptions]
Expand Down
Expand Up @@ -20,12 +20,10 @@ internal fun CoroutineScope.servletReader(input: ServletInputStream, contentLeng
}
}

private class ServletReader(val input: ServletInputStream, contentLength: Int) : ReadListener {
private class ServletReader(val input: ServletInputStream, val contentLength: Int) : ReadListener {
val channel = ByteChannel()
private val events = Channel<Unit>(2)

private val contentLength: Int = if (contentLength < 0) Int.MAX_VALUE else contentLength

suspend fun run() {
val buffer = ArrayPool.borrow()
try {
Expand Down Expand Up @@ -72,6 +70,8 @@ private class ServletReader(val input: ServletInputStream, contentLength: Int) :

channel.writeFully(buffer, 0, readCount)

if (contentLength < 0) continue

if (bodySize == contentLength) {
channel.close()
events.close()
Expand Down
Expand Up @@ -569,21 +569,25 @@ abstract class ContentTestSuite<TEngine : ApplicationEngine, TConfiguration : Ap
post("/") {
val response = StringBuilder()

call.receiveMultipart().readAllParts().sortedBy { it.name }.forEach { part ->
when (part) {
is PartData.FormItem -> response.append("${part.name}=${part.value}\n")
is PartData.FileItem -> response.append(
"file:${part.name},${part.originalFileName},${part.provider().readText()}\n"
)
is PartData.BinaryItem -> {
try {
call.receiveMultipart().readAllParts().sortedBy { it.name }.forEach { part ->
when (part) {
is PartData.FormItem -> response.append("${part.name}=${part.value}\n")
is PartData.FileItem -> response.append(
"file:${part.name},${part.originalFileName},${part.provider().readText()}\n"
)
is PartData.BinaryItem -> {
}
is PartData.BinaryChannelItem -> {}
}
is PartData.BinaryChannelItem -> {}

part.dispose()
}

part.dispose()
call.respondText(response.toString())
} catch (cause: Throwable) {
throw cause
}

call.respondText(response.toString())
}
}

Expand Down Expand Up @@ -639,10 +643,10 @@ abstract class ContentTestSuite<TEngine : ApplicationEngine, TConfiguration : Ap
call.receiveMultipart().forEachPart { part ->
when (part) {
is PartData.FormItem -> response.append("${part.name}=${part.value}\n")
is PartData.FileItem -> response.append(
"file:${part.name},${part.originalFileName}," +
"${part.streamProvider().bufferedReader().lineSequence().count()}\n"
)
is PartData.FileItem -> {
val lineSequence = part.streamProvider().bufferedReader().lineSequence()
response.append("file:${part.name},${part.originalFileName},${lineSequence.count()}\n")
}
is PartData.BinaryItem -> {
}
is PartData.BinaryChannelItem -> {}
Expand Down

0 comments on commit 2996cbd

Please sign in to comment.