Skip to content

Add new middleware for x-forwarded-proto header support#13

Open
lukaszkorecki wants to merge 1 commit intoring-clojure:masterfrom
lukaszkorecki:master
Open

Add new middleware for x-forwarded-proto header support#13
lukaszkorecki wants to merge 1 commit intoring-clojure:masterfrom
lukaszkorecki:master

Conversation

@lukaszkorecki
Copy link
Copy Markdown

This follows existing pattern for x-forwarded-for header support - when x-forwarded-proto is detected and contains one of allowed values, it will override :scheme key in the request map.

Copy link
Copy Markdown
Member

@weavejester weavejester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be omitted; it doesn't provide any information that can't be already discerned.

Comment on lines +25 to +29
(def ^:private proto-lookup
{"http" :http
"https" :https
"ws" :ws
"wss" :wss})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary newline.

Comment on lines +42 to +43
"Middleware that changes the :scheme of the request map to the
last value present in the X-Forwarded-Proto header."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap at 80 characters:

  "Middleware that changes the :scheme of the request map to the last value
  present in the X-Forwarded-Proto header."

Comment on lines +32 to +33
"Change the :scheme key of the request map to the last value present in
the X-Forwarded-Proto header. See: wrap-forwarded-scheme."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

@lukaszkorecki
Copy link
Copy Markdown
Author

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?

@weavejester
Copy link
Copy Markdown
Member

Yep; this seems a simple enough addition that only one commit is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants