reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
2021-01-08T13:29:51.025800Z

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

2021-01-08T13:34:04.026500Z

It should happen automatically, but I think you need to tell reitit to expect gzip as a content encoding.

2021-01-08T13:34:17.026800Z

Let me see if I can find where thatโ€™s specified

2021-01-08T13:35:01.027300Z

Hmm, I may be confusing that with something else, still digging

2021-01-08T13:36:54.027900Z

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

2021-01-08T13:37:01.028100Z

on which the handler is running

2021-01-08T13:39:11.028500Z

Are you using jetty as your web server engine?

2021-01-08T13:39:16.028700Z

yes

2021-01-08T13:39:22.028900Z

I found some article from 2015

2021-01-08T13:39:29.029300Z

about adding gzip handling so will try it

2021-01-08T13:39:50.029900Z

:thumbsup: It looks like thatโ€™s the bit that needs to be configured to handle gzip encoding.

2021-01-08T13:40:05.030200Z

just need to check that danielsz/system jetty component allows passing of configurator flag ๐Ÿ˜„

2021-01-08T13:40:28.030400Z

it looks like it so will test the code

2021-01-08T14:06:33.030800Z

unfortunately it looks like it doesn't work, switched to http kit server and the same problem persists

2021-01-08T14:06:44.031100Z

but the request has proper content encoding set to gzip

juhoteperi 2021-01-08T14:32:52.031800Z

Or that one looks to be only about compressing responses

2021-01-08T14:54:44.032200Z

but doesn't this gzip only responses?

2021-01-08T14:55:16.033Z

and at the moment I am dealing with gziped body in request

juhoteperi 2021-01-08T14:55:33.033400Z

Yeah. No idea how it works on the requests usually, never encountered problems.

2021-01-08T14:57:21.034500Z

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.

2021-01-08T14:59:45.035300Z

yeah unfortunately the request is rather large

juhoteperi 2021-01-08T15:05:03.036500Z

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.

2021-01-08T15:07:05.037500Z

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 ๐Ÿ˜„

2021-01-08T15:45:10.038200Z

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))))

2021-01-08T15:46:05.038900Z

needs more work around error handling, but for simple cases seems to work

juhoteperi 2021-01-08T15:51:06.039400Z

with-open saves a few lines: (with-open [gzip-body (GZIPInputStream. (:body request))] (handler (assoc request :body gzip-body)))

2021-01-08T15:54:23.039700Z

you're right ๐Ÿ‘