Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A function for single request forwarding #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject tailrecursion/ring-proxy "2.0.0-SNAPSHOT"
(defproject tailrecursion/ring-proxy "2.0.1-SNAPSHOT"
:description "HTTP proxy ring middleware for Clojure web applications."
:url "https://github.com/tailrecursion/ring-proxy"
:license {:name "Eclipse Public License"
Expand Down
25 changes: 15 additions & 10 deletions src/tailrecursion/ring_proxy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
(.read rdr buf)
buf)))

(defn forward-request
"Forward request to the specified remote-uri"
[req remote-uri & [http-opts]]
(-> (merge {:method (:request-method req)
:url (str remote-uri "?" (:query-string req))
:headers (dissoc (:headers req) "host" "content-length")
:body (if-let [len (get-in req [:headers "content-length"])]
(slurp-binary (:body req) (Integer/parseInt len)))
:follow-redirects true
:throw-exceptions false
:as :stream} http-opts)
request
prepare-cookies))

(defn wrap-proxy
"Proxies requests to proxied-path, a local URI, to the remote URI at
remote-uri-base, also a string."
Expand All @@ -36,16 +50,7 @@
(subs (:uri req) (.length proxied-path)))
nil
nil)]
(-> (merge {:method (:request-method req)
:url (str remote-uri "?" (:query-string req))
:headers (dissoc (:headers req) "host" "content-length")
:body (if-let [len (get-in req [:headers "content-length"])]
(slurp-binary (:body req) (Integer/parseInt len)))
:follow-redirects true
:throw-exceptions false
:as :stream} http-opts)
request
prepare-cookies))
(forward-request req remote-uri [http-opts]))
(handler req)))))

(defn local-proxy-server
Expand Down