Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Bugs processing "Content-Type: ...; charset=utf-8" #30

Open
dustingetz opened this issue Nov 13, 2017 · 1 comment
Open

Bugs processing "Content-Type: ...; charset=utf-8" #30

dustingetz opened this issue Nov 13, 2017 · 1 comment

Comments

@dustingetz
Copy link

dustingetz commented Nov 13, 2017

Expressjs appends charset to content-type, and afaict this confuses the KVLT from-content-type middleware.

My understanding is Express's behavior here isn't exactly standard, but it is allowed by browsers historically (or even required for legacy browsers to work expressjs/express#2238, or to mitigate a security vuln expressjs/express#2921. whatever.)

I couldn't find a way to work around from the Express side easily, so I had to do this super weird thing to make it work:

(defmethod kvlt.middleware/from-content-type
  (keyword "application/transit+json; charset=utf-8")   ; <======= weird
  [{:keys [form-params]}]
  (transit/encode form-params))

It caused all manner of issues because things like capitalization and whitespace start to matter when going out of band of the abstractions like this.

@dustingetz dustingetz changed the title Bugs processing "Content-Type: application/transit+json; charset=utf-8" Bugs processing "Content-Type: ...; charset=utf-8" Nov 13, 2017
@moea
Copy link
Member

moea commented Nov 13, 2017

kvlt should correctly handle that case, but doesn't. In the short-term, I think adding :as :application/transit+json to your request maps will allow you to just (defmethod from-content-type :application/transit+json ...), but that's awkward in other ways. the dispatch fn forfrom-content-type should strip any parameters off of the header value. I think the least impactful change would be something like this:

(defmulti from-content-type
  "Used by [[as]] to transform an incoming response.  Dispatches on
  `:content-type`' header, as a namespace-qualified
  keyword (e.g. `:application/edn`).  The input and output are the
  top-level response map, not only the response body.
  The default implementation (i.e. unrecognized content-type) returns
  the response map unmodified."
  (fn [resp]
    (some-> resp (header :content-type) (str/split #" ") first keyword)))

I'm not really actively working on this project, so it may get fixed faster if you submit a PR.

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

No branches or pull requests

2 participants