Sha256: 7f94aa57578c93fbff03b5e5fc8f1a11cb740d1d77410a0cb63ac4e4aaac3a52

Contents?: true

Size: 888 Bytes

Versions: 187

Compression:

Stored size: 888 Bytes

Contents

class Lalala::ExtRack::CanonicalURL

  require 'uri'

  LINKS_HEADER = "Links"

  def initialize(app)
    @app = app
  end

  def call(env)
    dup._call(env)
  end

protected

  def _call(env)
    req_url = Rack::Request.new(env).url

    status, headers, body = @app.call(env)

    # prepare canonical urls
    urls = headers.delete('Canonical-URL')
    urls = [urls, req_url].flatten.compact
    urls = urls.map { |u| URI.parse(u) }

    # resolve canonical url
    url = urls.reverse.inject(urls.pop) do |acc, u|
      acc.merge(u)
    end
    url = url.to_s

    # if the canonical url is not the current url
    if url != req_url

      # set the canonical url
      links = [headers[LINKS_HEADER]].flatten.compact
      links << "<#{url}>; rel=\"canonical\""
      links = links.join(", ")
      headers[LINKS_HEADER] = links
    end

    return [status, headers, body]
  end

end

Version data entries

187 entries across 187 versions & 1 rubygems

Version Path
lalala-4.1.0.dev.425 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.420 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.419 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.418 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.417 lib/lalala/ext_rack/canonical_url.rb
lalala-4.0.0.dev.416 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.415 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.414 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.413 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.412 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.411 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.410 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.409 lib/lalala/ext_rack/canonical_url.rb
lalala-4.0.0.dev.407 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.408 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.398 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.397 lib/lalala/ext_rack/canonical_url.rb
lalala-4.0.0.dev.396 lib/lalala/ext_rack/canonical_url.rb
lalala-4.0.0.dev.395 lib/lalala/ext_rack/canonical_url.rb
lalala-4.1.0.dev.394 lib/lalala/ext_rack/canonical_url.rb