How to limit upload size for a specific route and customize the error message?

30 views
Skip to first unread message

Drew Kutcharian

unread,
Jun 30, 2015, 8:14:28 PM6/30/15
to spray...@googlegroups.com
Hi Everyone,

Is there a way to limit the maximum upload size per route? We have multiple API end points with different upload limit sizes. Also, can I customize the response returned when the upload size exceeds the maximum size to be able to return a JSON message?

Thanks,

Drew

Mathias Doenitz

unread,
Jul 3, 2015, 5:31:20 PM7/3/15
to spray...@googlegroups.com
Drew,

> Is there a way to limit the maximum upload size per route? We have multiple API end points with different upload limit sizes. Also, can I customize the response returned when the upload size exceeds the maximum size to be able to return a JSON message?

spray has a clear separation of layers. The low-level spray-can layer is responsible for parsing the incoming HTTP messages and the high-level layers perform whatever high-level logic you define.
The `spray.can.server.parsing.max-content-length` defines what lengths the low-level message parser accepts or doesn’t accept. This parser is the first thing that an incoming request reaches when being received by your server. If the request content-length is too large the request will not be read from the network. Instead an error response is dispatched immediately and the connection closed.
If the content-length is ok the request is read from the network and dispatched to the high-level layer.

As such, by the time a request reaches your route structure it has already been accepted.
Of course you can still handle this request in any way you like in your routes. Treating requests differently depending on their content-length is not problem.

What you have to remember is that incoming requests can also come with `Transfer-Encoding: chunked`, which doesn’t specify a Content-Length.
In this case your application doesn’t know how long the request is going to be. You’ll have to keep track of the length yourself.

What you might find interesting is the `spray.can.server.parsing.incoming-auto-chunking-threshold-size` setting, which allows you to unify all requests to a single (streaming) model.
Sometimes this can help with keeping your high-level logic simpler.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/B72F65BB-068A-4F82-8D9F-33FB2B92ED6D%40venarc.com.
> For more options, visit https://groups.google.com/d/optout.

Drew Kutcharian

unread,
Jul 5, 2015, 2:28:27 AM7/5/15
to spray...@googlegroups.com
Thanks Mathias but I’m afraid I’m still not sure how to address the problem. The issue I’m having is two fold:

1. I would like to be able to return a custom error message when the uploaded file size is too large. We are uploading the files using an IFRAME in IE9 and you can’t access the headers of the response received from the server, so the only thing we can access is the actual content returned. It would be great if we could somehow return a custom JSON “file size exceeded message”

2. Say I set the spray can maximum upload size to 100 MB since there are scenarios where we allow large uploads. But we don’t want the user to upload a 100MB file for a profile photo! How can I stop them from uploading that large of a file for their photo?

It’d be great if you can point me to some examples, if there are any.

Best,

Drew
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/BC2EB890-E42D-49CD-9637-13F9B7EE85FE%40spray.io.
Reply all
Reply to author
Forward
0 new messages