Eugen Eistrach
Eugen Eistrach

Reputation: 23

How to limit allowed file size (multipart-file) in Ktor

I'm wondering how to change allowed file size for multipart-files in ktor? I can not find anything in the documentation about this (only how to receive multipart-files). I'm using embedded netty server. I will limit filesize on the client but it would be good to also have that restriction on the server.

Searched the web and the only two things I found related to the topic is this example (seems to be configuration for a WAR file): https://github.com/ktorio/ktor-samples/blob/master/other/maven-google-appengine-standard/webapp/WEB-INF/web.xml

and this example (how to receive multipart requests: https://github.com/ktorio/ktor-samples/blob/master/app/youkube/src/Upload.kt

Upvotes: 2

Views: 2611

Answers (1)

Alexey Soshin
Alexey Soshin

Reputation: 17731

There's nothing out-of-the-box, AFAIK.

But you can take inspiration from the official example: https://ktor.io/servers/uploads.html

Note this part:

val bytes = read(buffer).takeIf { it >= 0 } ?: break

And also the fact that they count bytesCopied:

bytesCopied += bytes

So, you could do something like

if (bytesCopied > limit) { throw RuntimeException("Limit reached") }

That's what Apache Tomcat implementation does.

Upvotes: 2

Related Questions