Add new middleware for x-forwarded-proto header support#13
Add new middleware for x-forwarded-proto header support#13lukaszkorecki wants to merge 1 commit intoring-clojure:masterfrom
Conversation
weavejester
left a comment
There was a problem hiding this comment.
Thanks for the commit. There's some minor formatting/style changes that I've added as inline comments. Can you also change the commit message to:
Add middleware for x-forwarded-proto header
This ensures the PR adheres to the contributing guidelines.
| ([request respond raise] | ||
| (handler (forwarded-remote-addr-request request) respond raise)))) | ||
|
|
||
| ;; x-forwarded-proto header support |
There was a problem hiding this comment.
This comment can be omitted; it doesn't provide any information that can't be already discerned.
| (def ^:private proto-lookup | ||
| {"http" :http | ||
| "https" :https | ||
| "ws" :ws | ||
| "wss" :wss}) |
There was a problem hiding this comment.
I think it would be better in this case to define this as a set/predicate. This separates out validation from coercion.
(def ^:private valid-scheme? #{"http" "https" "ws" "wss"})And then in forwarded-scheme-request:
(let [forwarded-proto (get-in request [:headers "x-forwarded-proto"])
scheme (some-> forwarded-proto str/trim str/lower-case)]
(if (and scheme (valid-scheme? scheme))
(assoc request :scheme (keyword scheme))
request))| (handler req resp ex) | ||
| (is (not (realized? ex))) | ||
| (is (= (:body @resp) "1.2.3.4")))))) | ||
|
|
| "Middleware that changes the :scheme of the request map to the | ||
| last value present in the X-Forwarded-Proto header." |
There was a problem hiding this comment.
Wrap at 80 characters:
"Middleware that changes the :scheme of the request map to the last value
present in the X-Forwarded-Proto header."| "Change the :scheme key of the request map to the last value present in | ||
| the X-Forwarded-Proto header. See: wrap-forwarded-scheme." |
There was a problem hiding this comment.
Wrap at 80 characters:
"Change the :scheme key of the request map to the last value present in the
X-Forwarded-Proto header. See: wrap-forwarded-scheme."|
Happy to help - I'll pick this up on Monday and apply all suggestions. I assume you prefer this PR be a squashed commit, right? |
|
Yep; this seems a simple enough addition that only one commit is required. |
This follows existing pattern for
x-forwarded-forheader support - whenx-forwarded-protois detected and contains one of allowed values, it will override:schemekey in the request map.