Sha256: e2e62fee2684706ed1e85b316b017cc5cdecc80f31b80207dd588176e27544c1

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'rack'

module Rack

  module Rewritten

    class Canonical

      def initialize(app)
        @app = app
      end

      def call(env)
        req = Rack::Request.new(env)

        status, headers, response = @app.call(req.env)

        if status == 200 && headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
          body = ""
          response.each { |part| body << part }
          index = body.rindex("</head>")
          if index
            # go with a request duplicate since infinitive works on translations
	    target_req = req.dup
	    target_req.path_info = ::Rewritten.infinitive( ::Rewritten.get_current_translation(req.path) )
	    target_req.env['QUERY_STRING'] = ''
	    #target = ::Rewritten.get_current_translation(target_req.url)
	    target = target_req.url

            body.insert(index, %Q|<link rel="canonical" href="#{target}"/>| )
            headers["Content-Length"] = body.length.to_s
            response = [body]
          end

          [status, headers, response]
        end
      end

    end

  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rewritten-0.14.0 lib/rack/canonical.rb