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

Add :max-file-size option to wrap-multipart-params #189

Closed
wants to merge 1 commit into from

Conversation

ryfow
Copy link
Contributor

@ryfow ryfow commented Feb 13, 2015

I'd like to be able to limit the size of the files read/created by wrap-multipart params. This change accomplishes that by setting FileUpload.setFileSizeMax but I need some guidance on:

  1. Error handling. Right now it just propagates the commons-fileupload exception.

  2. Testing. I don't see an example of testing the actual MIME parsing. I'm wondering what you'd like to see.

Thanks!
Ryan

@weavejester
Copy link
Member

Please see PR #98.

@ryfow
Copy link
Contributor Author

ryfow commented Feb 13, 2015

Thanks. Sorry for not looking that up.

After reading that thread I decided what I really want is a limit on the request size. This is what I ended up with. If you think this would be generally useful, I'll submit another PR.

(defn- limited-stream [is max-size]
  (proxy [LimitedInputStream]
      [is max-size]
    (raiseError [max count]
      (throw (ex-info "Request too large."
                      {:max max
                       :count count
                       :type :request-too-large})))))

(defn wrap-request-size-limit [h max-size request-too-large-fn]
  (fn [r]
    (let [content-length (Long/valueOf (get-in r [:headers "content-length"] "0"))
          body (:body r)]
      (cond (< max-size content-length)
            (request-too-large-fn r max-size content-length)

            (instance? java.io.InputStream body)
            (try
              (h (assoc r :body (limited-stream body max-size)))
              (catch Exception e
                (let [d (ex-data e)]
                  (if (= (:type d) :request-too-large)
                    (request-too-large-fn r (:max d) (:count d))
                    (throw e)))))

            :default
            (h r)))))

@ryfow ryfow closed this Feb 13, 2015
@weavejester
Copy link
Member

I'm currently considering merging #98 into Ring 1.4, because the rewrite of the multipart namespace keeps getting delayed.

In general, new middleware like wrap-request-size-limit should be tried out as a third party library first. If it proves sufficiently popular, it can then be merged into Ring core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants