Class: Geoblacklight::FaradayMiddleware::FollowRedirects

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/geoblacklight/faraday_middleware/follow_redirects.rb

Overview

Public: Follow HTTP 301, 302, 303, 307, and 308 redirects.

For HTTP 301, 302, and 303, the original GET, POST, PUT, DELETE, or PATCH request gets converted into a GET. With ‘:standards_compliant => true`, however, the HTTP method after 301/302 remains unchanged. This allows you to opt into HTTP/1.1 compliance and act unlike the major web browsers.

This middleware currently only works with synchronous requests; i.e. it doesn’t support parallelism.

If you wish to persist cookies across redirects, you could use the faraday-cookie_jar gem:

Faraday.new(:url => url) do |faraday|
  faraday.use FaradayMiddleware::FollowRedirects
  faraday.use :cookie_jar
  faraday.adapter Faraday.default_adapter
end

Constant Summary collapse

ALLOWED_METHODS =

HTTP methods for which 30x redirects can be followed

Set.new [:head, :options, :get, :post, :put, :patch, :delete]
REDIRECT_CODES =

HTTP redirect status codes that this middleware implements

Set.new [301, 302, 303, 307, 308]
ENV_TO_CLEAR =

Keys in env hash which will get cleared between requests

Set.new [:status, :response, :response_headers]
FOLLOW_LIMIT =

Default value for max redirects followed

3
URI_UNSAFE =

Regex that matches characters that need to be escaped in URLs, sans the “%” character which we assume already represents an escaped sequence.

%r{[^\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]%]}
AUTH_HEADER =
"Authorization"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ FollowRedirects

Public: Initialize the middleware.

options - An options Hash (default: {}):

:limit                      - A Numeric redirect limit (default: 3)
:standards_compliant        - A Boolean indicating whether to respect
                             the HTTP spec when following 301/302
                             (default: false)
:callback                   - A callable used on redirects
                             with the old and new envs
:cookies                    - An Array of Strings (e.g.
                             ['cookie1', 'cookie2']) to choose
                             cookies to be kept, or :all to keep
                             all cookies (default: []).
:clear_authorization_header - A Boolean indicating whether the request
                             Authorization header should be cleared on
                             redirects (default: true)


78
79
80
81
82
83
84
# File 'lib/geoblacklight/faraday_middleware/follow_redirects.rb', line 78

def initialize(app, options = {})
  super(app)
  @options = options

  @convert_to_get = Set.new [303]
  @convert_to_get << 301 << 302 unless standards_compliant?
end

Instance Method Details

#call(env) ⇒ Object



86
87
88
# File 'lib/geoblacklight/faraday_middleware/follow_redirects.rb', line 86

def call(env)
  perform_with_redirection(env, follow_limit)
end