Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Remove deprecated wrap-redirects
  • Loading branch information
rymndhng committed Nov 11, 2017
commit 80869ce461e22bb64cba583532b260a51d5cd860
5 changes: 0 additions & 5 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,6 @@ Redirect Options:

You may also pass in an instance of RedirectStrategy if you want a behaviour that's not implemented

NOTE: The options =:force-redirects= and =:follow-redirects= (present in
clj-http 2.x are no longer used). You can use =:graceful= to mostly emulate the
old redirect behaviour.


** Cookies
:PROPERTIES:
:CUSTOM_ID: h-3bb89b16-4be3-455e-98ec-c5ca5830ddb9
Expand Down
107 changes: 0 additions & 107 deletions src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -252,121 +252,14 @@
(response (exceptions-response req resp)))
raise))))

(declare wrap-redirects)
(declare reuse-pool)

(defn- follow-redirect-request
[req redirect trace-redirects resp]
(-> req
(merge (parse-url redirect))
(dissoc :query-params)
(assoc :url redirect)
(assoc :trace-redirects trace-redirects)
(reuse-pool resp)))

(defn follow-redirect
"Attempts to follow the redirects from the \"location\" header, if no such
header exists (bad server!), returns the response without following the
request."
[client {:keys [uri url scheme server-name server-port async? respond raise]
:as req}
{:keys [trace-redirects ^InputStream body] :as resp}]
(let [url (or url (str (name scheme) "://" server-name
(when server-port (str ":" server-port)) uri))]
(if-let [raw-redirect (get-in resp [:headers "location"])]
(let [redirect (str (URL. (URL. url) raw-redirect))]
(try (.close body) (catch Exception _))
(if-not async?
((wrap-redirects client)
(follow-redirect-request req redirect trace-redirects resp))
(if (some nil? [respond raise])
(raise
(IllegalArgumentException.
"If :async? is true, you must set :respond and :raise"))
((wrap-redirects client)
(follow-redirect-request req redirect trace-redirects resp)
respond raise))))
;; Oh well, we tried, but if no location is set, return the response
(if-not async?
resp
(respond resp)))))

(defn- respond*
[resp req]
(if (:async? req)
((:respond req) resp)
resp))

(defn- redirects-response
[client
{:keys [request-method max-redirects redirects-count trace-redirects url]
:or {redirects-count 1 trace-redirects []
;; max-redirects default taken from Firefox
max-redirects 20}
:as req} {:keys [status] :as resp}]
(let [resp-r (assoc resp :trace-redirects
(if url
(conj trace-redirects url)
trace-redirects))]
(cond
(false? (opt req :follow-redirects))
(respond* resp req)
(not (redirect? resp-r))
(respond* resp-r req)
(and max-redirects (> redirects-count max-redirects))
(if (opt req :throw-exceptions)
(throw (ex-info (format "Too many redirects: %s" redirects-count) resp-r))
(respond* resp-r req))
(= 303 status)
(follow-redirect client (assoc req :request-method :get
:redirects-count (inc redirects-count))
resp-r)
(#{301 302} status)
(cond
(#{:get :head} request-method)
(follow-redirect client (assoc req :redirects-count
(inc redirects-count)) resp-r)
(opt req :force-redirects)
(follow-redirect client (assoc req
:request-method :get
:redirects-count (inc redirects-count))
resp-r)
:else
(respond* resp-r req))
(= 307 status)
(if (or (#{:get :head} request-method)
(opt req :force-redirects))
(follow-redirect client (assoc req :redirects-count
(inc redirects-count)) resp-r)
(respond* resp-r req))
:else
(respond* resp-r req))))

(defn ^:deprecated wrap-redirects
"Middleware that follows redirects in the response. A ex-info exception is
thrown if too many redirects occur. Options

:follow-redirects - default:true, whether to follow redirects
:max-redirects - default:20, maximum number of redirects to follow
:force-redirects - default:false, force redirecting methods to GET requests

In the response:

:redirects-count - number of redirects
:trace-redirects - vector of sites the request was redirected from"
[client]
(fn
([req]
(redirects-response client req (client req)))
([req respond raise]
(client req
#(redirects-response client
(assoc req :async? true
:respond respond
:raise raise)
%)
raise))))

;; Multimethods for Content-Encoding dispatch automatically
;; decompressing response bodies
(defmulti decompress-body
Expand Down
Loading