Class: RestCore::FollowRedirect
- Inherits:
-
Object
- Object
- RestCore::FollowRedirect
show all
- Includes:
- Middleware
- Defined in:
- lib/rest-core/middleware/follow_redirect.rb
Constant Summary
Constants included
from Middleware
Middleware::UNRESERVED
Constants included
from RestCore
ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Middleware
contain_binary?, #contain_binary?, escape, #escape, #fail, #id, included, #log, merge_hash, #merge_hash, #percent_encode, percent_encode, request_uri, #request_uri, #run, #string_keys, string_keys
Methods included from RestCore
eagerload, id
Class Method Details
.members ⇒ Object
5
|
# File 'lib/rest-core/middleware/follow_redirect.rb', line 5
def self.members; [:max_redirects]; end
|
Instance Method Details
#call(env, &k) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/rest-core/middleware/follow_redirect.rb', line 8
def call env, &k
e = env.merge('follow_redirect.max_redirects' =>
env['follow_redirect.max_redirects'] ||
max_redirects(env))
if e[DRY]
app.call(e, &k)
else
app.call(e){ |res| process(res, k) }
end
end
|
#process(res, k) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/rest-core/middleware/follow_redirect.rb', line 20
def process res, k
return k.call(res) if res['follow_redirect.max_redirects'] <= 0
return k.call(res) if ![301,302,303,307].include?(res[RESPONSE_STATUS])
return k.call(res) if [301,302 ,307].include?(res[RESPONSE_STATUS]) &&
![:get, :head ].include?(res[REQUEST_METHOD])
location = [res[RESPONSE_HEADERS]['LOCATION']].flatten.first
meth = if res[RESPONSE_STATUS] == 303
:get
else
res[REQUEST_METHOD]
end
call(res.merge(REQUEST_METHOD => meth ,
REQUEST_PATH => location,
REQUEST_QUERY => {} ,
'follow_redirect.max_redirects' =>
res['follow_redirect.max_redirects'] - 1), &k)
end
|