faraday_middleware 0.13.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3276b16e454e801286b52a8ba62f765b7517d63c2e224b336371e8a53facc41
4
- data.tar.gz: 1d7c8d737d91c247bc4bd3ae1326d5e1ce1096126eb7ed8c7bde8bf0b47f4260
3
+ metadata.gz: 759c9264c036da4dc2e7347541d09d1afee816769b06930f40517fde2a816ab4
4
+ data.tar.gz: b603e5501f0cab2e9d065cb86c71cc46b7c5260f7ef136bd13d791b19d650c3d
5
5
  SHA512:
6
- metadata.gz: 2d1b3441e3e62a474f6cc91d6b3b4d44214773b4036049b7b4f12bcdc827f0b8b8b7d5b0b0ed37fb8dda99271c8228eb4ffaeb82ee51ceb05a0e27c783d43a1e
7
- data.tar.gz: '08da04c8e67e334409a7df32374afb45576cf5bf960e007fcca684c384674e54eabcbd32a3849a3b57b6a6ff04604fa352baabb5beb619417830a1719c9b128b'
6
+ metadata.gz: 696314d36aa007d70c27e07c918878803c36778ce69d8192c73f32b04cbb28ac20fba0a64d7b2fbb87e2ad167cb99363a7648808f27d402d7130a5a36fd5e9b6
7
+ data.tar.gz: 5d479f6be7386844f4b699c06773fcfdc0ae22530904b95220c8433a1d2f6237072dc95dbaf4fd0929b6f098132c400bc5fbd11e3354f83ea23d7fccd0dc1d76
@@ -13,10 +13,25 @@ module FaradayMiddleware
13
13
  class Gzip < Faraday::Middleware
14
14
  dependency 'zlib'
15
15
 
16
+ def self.optional_dependency(lib = nil)
17
+ lib ? require(lib) : yield
18
+ true
19
+ rescue LoadError, NameError
20
+ false
21
+ end
22
+
23
+ BROTLI_SUPPORTED = optional_dependency 'brotli'
24
+
25
+ def self.supported_encodings
26
+ encodings = %w[gzip deflate]
27
+ encodings << 'br' if BROTLI_SUPPORTED
28
+ encodings
29
+ end
30
+
16
31
  ACCEPT_ENCODING = 'Accept-Encoding'.freeze
17
32
  CONTENT_ENCODING = 'Content-Encoding'.freeze
18
33
  CONTENT_LENGTH = 'Content-Length'.freeze
19
- SUPPORTED_ENCODINGS = 'gzip,deflate,br'.freeze
34
+ SUPPORTED_ENCODINGS = supported_encodings.join(',').freeze
20
35
  RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
21
36
 
22
37
  def call(env)
@@ -64,8 +79,6 @@ module FaradayMiddleware
64
79
  end
65
80
 
66
81
  def brotli_inflate(body)
67
- self.class.dependency 'brotli'
68
-
69
82
  Brotli.inflate(body)
70
83
  end
71
84
  end
@@ -45,15 +45,24 @@ module FaradayMiddleware
45
45
  # the "%" character which we assume already represents an escaped sequence.
46
46
  URI_UNSAFE = /[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]%]/
47
47
 
48
+ AUTH_HEADER = 'Authorization'.freeze
49
+
48
50
  # Public: Initialize the middleware.
49
51
  #
50
52
  # options - An options Hash (default: {}):
51
- # :limit - A Numeric redirect limit (default: 3)
52
- # :standards_compliant - A Boolean indicating whether to respect
53
+ # :limit - A Numeric redirect limit (default: 3)
54
+ # :standards_compliant - A Boolean indicating whether to respect
53
55
  # the HTTP spec when following 301/302
54
56
  # (default: false)
55
- # :callback - A callable that will be called on redirects
57
+ # :callback - A callable that will be called on redirects
56
58
  # with the old and new envs
59
+ # :cookies - An Array of Strings (e.g.
60
+ # ['cookie1', 'cookie2']) to choose
61
+ # cookies to be kept, or :all to keep
62
+ # all cookies (default: []).
63
+ # :clear_authorization_header - A Boolean indicating whether the request
64
+ # Authorization header should be cleared on
65
+ # redirects (default: true)
57
66
  def initialize(app, options = {})
58
67
  super(app)
59
68
  @options = options
@@ -89,7 +98,9 @@ module FaradayMiddleware
89
98
  end
90
99
 
91
100
  def update_env(env, request_body, response)
92
- env[:url] += safe_escape(response['location'] || '')
101
+ redirect_from_url = env[:url].to_s
102
+ redirect_to_url = safe_escape(response['location'] || '')
103
+ env[:url] += redirect_to_url
93
104
 
94
105
  if convert_to_get?(response)
95
106
  env[:method] = :get
@@ -98,6 +109,8 @@ module FaradayMiddleware
98
109
  env[:body] = request_body
99
110
  end
100
111
 
112
+ clear_authorization_header(env, redirect_from_url, redirect_to_url)
113
+
101
114
  ENV_TO_CLEAR.each {|key| env.delete key }
102
115
 
103
116
  env
@@ -130,5 +143,21 @@ module FaradayMiddleware
130
143
  '%' + match.unpack('H2' * match.bytesize).join('%').upcase
131
144
  }
132
145
  end
146
+
147
+ def clear_authorization_header(env, from_url, to_url)
148
+ return env if redirect_to_same_host?(from_url, to_url)
149
+ return env unless @options.fetch(:clear_authorization_header, true)
150
+
151
+ env[:request_headers].delete(AUTH_HEADER)
152
+ end
153
+
154
+ def redirect_to_same_host?(from_url, to_url)
155
+ return true if to_url.start_with?('/')
156
+
157
+ from_uri = URI.parse(from_url)
158
+ to_uri = URI.parse(to_url)
159
+
160
+ [from_uri.scheme, from_uri.host, from_uri.port] == [to_uri.scheme, to_uri.host, to_uri.port]
161
+ end
133
162
  end
134
163
  end
@@ -1,3 +1,3 @@
1
1
  module FaradayMiddleware
2
- VERSION = '0.13.0' unless defined?(FaradayMiddleware::VERSION)
2
+ VERSION = '0.13.1' unless defined?(FaradayMiddleware::VERSION)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Michaels-Ober
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-22 00:00:00.000000000 Z
12
+ date: 2019-02-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday