|
284 | 284 |
|
285 | 285 | (defn coerce-json-body |
286 | 286 | [{:keys [coerce]} {:keys [body status] :as resp} keyword? strict? & [charset]] |
287 | | - (let [^String charset (or charset "UTF-8") |
| 287 | + (let [^String charset (or charset (-> resp :content-type-params :charset) "UTF-8") |
288 | 288 | body (util/force-byte-array body) |
289 | 289 | decode-func (if strict? json-decode-strict json-decode)] |
290 | 290 | (if json-enabled? |
|
302 | 302 | :else (assoc resp :body (String. ^"[B" body charset))) |
303 | 303 | (assoc resp :body (String. ^"[B" body charset))))) |
304 | 304 |
|
| 305 | +(defn coerce-clojure-body |
| 306 | + [request {:keys [body] :as resp}] |
| 307 | + (let [^String charset (or (-> resp :content-type-params :charset) "UTF-8") |
| 308 | + body (util/force-byte-array body)] |
| 309 | + (if edn-enabled? |
| 310 | + (assoc resp :body (parse-edn (String. ^"[B" body charset))) |
| 311 | + (binding [*read-eval* false] |
| 312 | + (assoc resp :body (read-string (String. ^"[B" body charset))))))) |
| 313 | + |
| 314 | +(defmulti coerce-content-type (fn [req resp] (:content-type resp))) |
| 315 | + |
| 316 | +(defmethod coerce-content-type :application/clojure [req resp] |
| 317 | + (coerce-clojure-body req resp)) |
| 318 | + |
| 319 | +(defmethod coerce-content-type :application/edn [req resp] |
| 320 | + (coerce-clojure-body req resp)) |
| 321 | + |
| 322 | +(defmethod coerce-content-type :application/json [req resp] |
| 323 | + (coerce-json-body req resp true false)) |
| 324 | + |
| 325 | +(defmethod coerce-content-type :default [req resp] |
| 326 | + (if-let [charset (-> resp :content-type-params :charset)] |
| 327 | + (coerce-response-body {:as charset} resp) |
| 328 | + (coerce-response-body {:as :default} resp))) |
| 329 | + |
| 330 | +(defmethod coerce-response-body :auto [request resp] |
| 331 | + (let [header (get-in resp [:headers "content-type"])] |
| 332 | + (->> (merge resp (util/parse-content-type header)) |
| 333 | + (coerce-content-type request)))) |
| 334 | + |
305 | 335 | (defmethod coerce-response-body :json [req resp] |
306 | 336 | (coerce-json-body req resp true false)) |
307 | 337 |
|
|
314 | 344 | (defmethod coerce-response-body :json-string-keys [req resp] |
315 | 345 | (coerce-json-body req resp false false)) |
316 | 346 |
|
317 | | -(defmethod coerce-response-body :clojure [_ {:keys [body] :as resp}] |
318 | | - (let [body (util/force-byte-array body)] |
319 | | - (if edn-enabled? |
320 | | - (assoc resp :body (parse-edn (String. ^"[B" body "UTF-8"))) |
321 | | - (binding [*read-eval* false] |
322 | | - (assoc resp :body (read-string (String. ^"[B" body "UTF-8"))))))) |
323 | | - |
324 | | -(defmethod coerce-response-body :auto |
325 | | - [req resp] |
326 | | - (let [typestring (get-in resp [:headers "content-type"])] |
327 | | - (cond |
328 | | - (.startsWith (str typestring) "text/") |
329 | | - (if-let [charset (second (re-find #"charset=(.*)" |
330 | | - (str typestring)))] |
331 | | - (coerce-response-body {:as charset} resp) |
332 | | - (coerce-response-body {:as :default} resp)) |
333 | | - |
334 | | - (or (.startsWith (str typestring) "application/clojure") |
335 | | - (.startsWith (str typestring) "application/edn")) |
336 | | - (let [charset (or (second (re-find #"charset=(.*)" (str typestring))) |
337 | | - "UTF-8")] |
338 | | - (coerce-response-body {:as :clojure} resp)) |
339 | | - |
340 | | - (and (.startsWith (str typestring) "application/json") |
341 | | - json-enabled?) |
342 | | - (do |
343 | | - (if-let [charset (second (re-find #"charset=(.*)" |
344 | | - (str typestring)))] |
345 | | - ;; Defaulting to lazy parsing w/ symbol keys. |
346 | | - (coerce-json-body req resp true false charset) |
347 | | - (coerce-json-body req resp true false "UTF-8"))) |
348 | | - |
349 | | - :else |
350 | | - (coerce-response-body {:as :default} resp)))) |
| 347 | +(defmethod coerce-response-body :clojure [req resp] |
| 348 | + (coerce-clojure-body req resp)) |
351 | 349 |
|
352 | 350 | (defmethod coerce-response-body :default |
353 | 351 | [{:keys [as]} {:keys [status body] :as resp}] |
|
0 commit comments