Sha256: 52cbc7d6a004bb85fa49ca4f0bfbfe121aafab7dd0e33fc6e73c3ac97df85be5

Contents?: true

Size: 969 Bytes

Versions: 2

Compression:

Stored size: 969 Bytes

Contents

# 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

2 entries across 2 versions & 1 rubygems

Version Path
crystal_ext-0.0.5 lib/crystal_ext/ensure_no_www.rb
crystal_ext-0.0.4 lib/crystal_ext/ensure_no_www.rb