Sha256: adc99aa46d8d6aec1006d84564297b69ac958bf5ca275994956cdd1b2550b7ea

Contents?: true

Size: 834 Bytes

Versions: 14

Compression:

Stored size: 834 Bytes

Contents

require 'time'

require 'rack'
require 'rack/utils'

module Murlsh

  # Rack middleware to set a far future expires header for urls that match
  # patterns.
  class FarFutureExpires

    def initialize(app, options={})
      @app = app
      @patterns = options[:patterns] ? [*options[:patterns]] : []
      # rfc2616 HTTP/1.1 servers SHOULD NOT send Expires dates more than one
      # year in the future.
      @future = options[:future] || (Time.now + 31536000).httpdate
    end

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

      req = Rack::Request.new(env)

      headers = Rack::Utils::HeaderHash.new(headers)

      @patterns.each do |pattern|
        if pattern.match(req.path)
          headers['Expires'] = @future
          break
        end
      end

      [status, headers, body]
    end

  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
murlsh-1.9.3 lib/murlsh/far_future_expires.rb
murlsh-1.9.2 lib/murlsh/far_future_expires.rb
murlsh-1.9.1 lib/murlsh/far_future_expires.rb
murlsh-1.9.0 lib/murlsh/far_future_expires.rb
murlsh-1.8.0 lib/murlsh/far_future_expires.rb
murlsh-1.7.1 lib/murlsh/far_future_expires.rb
murlsh-1.7.0 lib/murlsh/far_future_expires.rb
murlsh-1.6.1 lib/murlsh/far_future_expires.rb
murlsh-1.6.0 lib/murlsh/far_future_expires.rb
murlsh-1.5.0 lib/murlsh/far_future_expires.rb
murlsh-1.4.1 lib/murlsh/far_future_expires.rb
murlsh-1.4.0 lib/murlsh/far_future_expires.rb
murlsh-1.3.1 lib/murlsh/far_future_expires.rb
murlsh-1.3.0 lib/murlsh/far_future_expires.rb