Hi sorry if question is too simple, but what would be proper middleware to decompres requests which have content encoding: gzip
? I thought it should be handled automatically but it looks like [muuntaja.format.json/decoder]
is called before it gets decompressed
It should happen automatically, but I think you need to tell reitit to expect gzip as a content encoding.
Let me see if I can find where thatโs specified
Hmm, I may be confusing that with something else, still digging
I can't even find mention of gzip in requests in reitit repo so I wonder if maybe this should be handled by web server
on which the handler is running
Are you using jetty as your web server engine?
yes
I found some article from 2015
about adding gzip handling so will try it
:thumbsup: It looks like thatโs the bit that needs to be configured to handle gzip encoding.
just need to check that danielsz/system jetty component allows passing of configurator flag ๐
it looks like it so will test the code
unfortunately it looks like it doesn't work, switched to http kit server and the same problem persists
but the request has proper content encoding set to gzip
Or that one looks to be only about compressing responses
but doesn't this gzip only responses?
and at the moment I am dealing with gziped body in request
Yeah. No idea how it works on the requests usually, never encountered problems.
Maybe a tangential question, but why are your incoming requests using gzip? Are they that large? A file upload would be, but file uploads usually use a different mechanism.
yeah unfortunately the request is rather large
Looks like clients normally don't send compressed data as they can't know if server supports it.
You could implment this in a middleware yourself. Just make sure it runs before Muuntaja, and wrap the req body inputstream in GZIPInputStream
.
thanks for help. I will probably need to settle on writting middleware as you mentioned albeit I would like to avoid it as custom code is always a higher chance of bugs ๐
for posterity, tested such simple middleware fn and it works:
(defn gzip-wrap [handler]
(fn [request]
(if (= (get-in request [:headers "content-encoding"]) "gzip")
(let [gzip-body (GZIPInputStream. (:body request))
response (handler (assoc request :body gzip-body))]
(.close gzip-body)
response)
(handler request))))
needs more work around error handling, but for simple cases seems to work
with-open saves a few lines: (with-open [gzip-body (GZIPInputStream. (:body request))] (handler (assoc request :body gzip-body)))
you're right ๐