- Apache 2
- ModRewrite
- Ruby on Rails
When you have a form to upload a file, you may want to limit the file size. If you check for the size within the application - we have already lost the battle.
The better way (IMHO) is to use Apache ModRewrite to check for the size of a POST request and redirect with GET to error handling action
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{HTTP:Content-type} multipart/form-data
RewriteCond %{HTTP:Content-length} >500000
RewriteRule ^(.*?)$ %{REQUEST_URI}?form_data_too_big=on [R=303,L]
How it works
- Only POST requests
- Only mutipart/form-data POST
- Check HTTP Request header against limit. Add MIME envelop overhead (5Kb for example)
- Redirect with GET to the same URL with one parameter
Let say you have a controller with action process_upload, and the URL for the action is http://www.myserver.tld/file/process_upload.
def process_upload
if request.get? && params[:form_data_too_big]
return error_response_for_file_to_big
end
....
end
In that way you can work with respond_to_parent or json or javascript