ring

ikitommi 2018-06-14T07:31:40.000497Z

@weavejester thanks. There doesn’t seem to get get-content-type available, the ring.util.response/get-header returns the raw string, which might have stuff like encoding, e.g.`"application/json; encoding=utf-8"`. My use case is that I need it multiple times in a request pipeline for different purposes, I guess the current good practise is to parse it once (in a content-negotiation middleware) and push it to request so it doesn’t need to be reparsed for all users.

ikitommi 2018-06-14T07:40:02.000137Z

But well, just wanted to know the cause, I’ll wire together the libs using Muuntaja keys. Only gets parsed once and the reads are fast (despite stored under a library-spesific key):

;; Muuntaja pushes the parsed results into a Record for fast access
(defrecord RequestAndCharset [format charset])

;; a Sample request
(def req {:headers {"origin" "<http://localhost:3000>",
                    "host" "localhost:3000",
                    "user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
                    "content-type" "application/json",
                    "cookie" "JSESSIONID=fo2TyZwq24rKinCsfcHI_DgKz611PO9Lpyh1mLqf; _ga=GA1.1.1363736022.1528784471",
                    "content-length" "22",
                    "referer" "<http://localhost:3000/index.html>",
                    "connection" "keep-alive",
                    "accept" "application/json",
                    "accept-language" "en-US,en;q=0.9,fi-FI;q=0.8,fi;q=0.7",
                    "accept-encoding" "gzip, deflate, br",
                    "dnt" "1"}
          :muuntaja/request (-&gt;RequestAndCharset "application/json" nil)
          :muuntaja/response (-&gt;RequestAndCharset "application/json" nil)})

;; 352ns
(cc/quick-bench
  (ring.util.response/get-header req "content-type"))

;; 45ns
(cc/quick-bench
  (-&gt; req :headers (get "content-type")))

;; 13ns
(cc/quick-bench
  (-&gt; req :muuntaja/request :format))

2018-06-14T15:39:55.000450Z

@ikitommi There’s a ring.util.request/content-type function.

2018-06-14T15:40:42.000576Z

Sorry, before I wrote ring.util.response/content-type when I meant ring.util.request/content-type.

ikitommi 2018-06-14T16:21:15.000779Z

oh, thanks again! For some reason I haven’t ever used the .request ns. It does the trick indeed.

ikitommi 2018-06-14T16:22:31.000690Z

;; 874ns
(cc/quick-bench
  (ring.util.request/content-type req))