Sha256: a16ad1cbb534095cc87128004bae961842eecde39fb41ce0eb581b8c31b59b3b

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

require 'crystal_ext/gems'

# enshure domain has no www. except if there's custom subdomain
module Crystal
  # class UrlWithWww < StandardError
  # end
  
  module Processors        
    class EnsureNoWww < Processor
      def call
        workspace.params.must_be.defined
        if workspace.params.html? and url_with_www?
          redirect_without_www
        else
          next_processor.call
        end
      end            
      
      protected
        def uri
          @uri ||= Uri.parse workspace.request.url
        end
      
        def url_with_www?
          uri.host =~ /^www\./
        end
        
        def redirect_without_www
          uri.host = uri.host.sub(/^www\./, '')
          url = uri.to_s
          
          response = workspace.response
          response.status = 301
          response.headers['Location'] = url
          response.body = %(<html><body>You are being <a href="#{url.html_escape}">redirected</a>.</body></html>)
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crystal_ext-0.0.7 lib/crystal_ext/ensure_no_www.rb