module SslRoutes::ActionController

Constants

Constraints
Dispatcher

Public Class Methods

included(base) click to toggle source
# File lib/ssl_routes/rails.rb, line 5
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

determine_protocols(options) click to toggle source
# File lib/ssl_routes/rails.rb, line 22
def determine_protocols(options)
  current = self.request.ssl? ? 'https' : 'http'
  target  = case options[self.parameter]
    when String then options[self.parameter]
    when TrueClass then 'https'
    when FalseClass then 'http'
    else current
  end
  target = 'https' if self.secure_session && current_user
  target = options[:protocol] if options[:protocol]
  [ current, target.split(':').first ]
end

Private Instance Methods

ensure_protocol() click to toggle source
# File lib/ssl_routes/rails.rb, line 37
def ensure_protocol
  router = Rails.application.routes.router
  options = recognize_request(router, self.request)
  current, target = determine_protocols(options)
  if (current != target && !request.xhr? && request.get?)
    flash.keep
    response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
    response.headers['Pragma'] = 'no-cache'
    redirect_to "#{target}://#{request.host_with_port + request.fullpath}"
    return false
  end
end
recognize_request(router, req) click to toggle source
# File lib/ssl_routes/rails.rb, line 53
def recognize_request(router, req)
  router.recognize(req) do |route, matches, params|
    params.each do |key, value|
      if value.is_a?(String)
        value = value.dup.force_encoding(Encoding::BINARY) if value.encoding_aware?
        params[key] = URI.parser.unescape(value)
      end
    end

    dispatcher = route.app
    while dispatcher.is_a?(Constraints) && dispatcher.matches?(env) do
      dispatcher = dispatcher.app
    end

    if dispatcher.is_a?(Dispatcher) && dispatcher.controller(params, false)
      dispatcher.prepare_params!(params)
      return params
    end
  end

  raise ActionController::RoutingError, "No route matches #{path.inspect}"
end