Skip to content

Commit

Permalink
FollowRedirects middleware: copy opts from response env
Browse files Browse the repository at this point in the history
Middlewares that come after FollowRedirects might use `opts` for state
they need to keep if they are called multiple times.

The specific use case we have that requires this: Our pool for
gun requires explicit checkout/checkin of the connection. We have a
middleware after FollowRedirects that checks out a connection, passes
it to adapter opts, requests and then checks the connection back in.
However, if `body_as: :chunks` options is used, we, obviously,
can't checkin the connection in the middleware as it needs to be passed
to the client after leaving the middleware. But if there was a redirect
to a different host we want to checkin the old connection and checkout
a new one ourselves. So we want to check if adapter options already
have a connection in them and checkin it if present.
  • Loading branch information
rinpatch committed Sep 4, 2020
1 parent dbe88ce commit 3484e0f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/tesla/middleware/follow_redirects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ defmodule Tesla.Middleware.FollowRedirects do
prev_uri = URI.parse(env.url)
next_uri = parse_location(location, res)

# Copy opts and query params from the response env,
# these are not modified in the adapters, but may be
# in middlewares that come after, for example HTTP pooling
# middleware might need to modify adapter opts.
env = %{env | opts: res.opts, query: res.query}

env
|> filter_headers(prev_uri, next_uri)
|> new_request(status, URI.to_string(next_uri))
Expand Down

0 comments on commit 3484e0f

Please sign in to comment.