class Hanami::Routing::ForceSsl

Force ssl

Redirect response to the secure equivalent resource (https)

@since 0.4.1 @api private

Constants

DEFAULT_HTTP_PORT

Default http port

@since 0.4.1 @api private

DEFAULT_SSL_PORT

Default ssl port

@since 0.4.1 @api private

EMPTY_BODY
HTTPS

@since 0.4.1 @api private

HTTP_X_FORWARDED_PROTO

@since 0.4.1 @api private

HTTP_X_FORWARDED_PROTO_SEPARATOR

@since 0.4.1 @api private

HTTP_X_FORWARDED_SCHEME

@since 0.4.1 @api private

HTTP_X_FORWARDED_SSL

@since 0.4.1 @api private

IDEMPOTENT_METHODS

@since 0.4.1 @api private

LOCATION_HEADER

Location header

@since 0.4.1 @api private

MOVED_PERMANENTLY_HTTP_CODE

Moved Permanently http code

@since 0.4.1 @api private

ON

@since 0.4.1 @api private

RACK_URL_SCHEME

@since 0.4.1 @api private

REQUEST_METHOD

@since 0.4.1 @api private

SSL_SCHEME

Https scheme

@since 0.4.1 @api private

TEMPORARY_REDIRECT_HTTP_CODE

Temporary Redirect http code

@since 0.4.1 @api private

Attributes

host[R]

@since 0.4.1 @api private

Public Class Methods

new(active, options = {}) click to toggle source

Initialize ForceSsl.

@param active [Boolean] activate redirection to SSL @param options [Hash] set of options @option options [String] :host @option options [Integer] :port

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 96
def initialize(active, options = {})
  @active = active
  @host   = options[:host]
  @port   = options[:port]

  _redefine_call
end

Public Instance Methods

call(env) click to toggle source

Set 301 status and Location header if this feature is activated.

@param env [Hash] a Rack env instance

@return [Array]

@see Hanami::Routing::HttpRouter#call

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 114
def call(env)
end
force?(env) click to toggle source

Check if router has to force the response with ssl

@return [Boolean]

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 123
def force?(env)
  !ssl?(env)
end

Private Instance Methods

_redefine_call() click to toggle source

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 177
def _redefine_call
  return unless @active

  Hanami::Utils::Deprecation.new('force_ssl option is deprecated, please delegate this behaviour to Nginx/Apache or use a Rack middleware like `rack-ssl`')

  define_singleton_method :call do |env|
    [redirect_code(env), { LOCATION_HEADER => full_url(env) }, EMPTY_BODY] if force?(env)
  end
end
full_url(env) click to toggle source

Return full url to redirect

@param env [Hash] Rack env

@return [String]

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 141
def full_url(env)
  "#{ SSL_SCHEME }://#{ host }:#{ port }#{ ::Rack::Request.new(env).fullpath }"
end
port() click to toggle source

Return correct default port for full url

@return [Integer]

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 167
def port
  if @port == DEFAULT_HTTP_PORT
    DEFAULT_SSL_PORT
  else
    @port
  end
end
redirect_code(env) click to toggle source

Return redirect code

@param env [Hash] Rack env

@return [Integer]

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 153
def redirect_code(env)
  if IDEMPOTENT_METHODS.include?(env[REQUEST_METHOD])
    MOVED_PERMANENTLY_HTTP_CODE
  else
    TEMPORARY_REDIRECT_HTTP_CODE
  end
end
scheme(env) click to toggle source

Adapted from Rack::Request#scheme

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 191
def scheme(env)
  if env[HTTPS] == ON
    SSL_SCHEME
  elsif env[HTTP_X_FORWARDED_SSL] == ON
    SSL_SCHEME
  elsif env[HTTP_X_FORWARDED_SCHEME]
    env[HTTP_X_FORWARDED_SCHEME]
  elsif env[HTTP_X_FORWARDED_PROTO]
    env[HTTP_X_FORWARDED_PROTO].split(HTTP_X_FORWARDED_PROTO_SEPARATOR)[0]
  else
    env[RACK_URL_SCHEME]
  end
end
ssl?(env) click to toggle source

@since 0.4.1 @api private

# File lib/hanami/routing/force_ssl.rb, line 207
def ssl?(env)
  scheme(env) == SSL_SCHEME
end