class Rack::PageSpeed

Constants

Filter

shortcut

Attributes

config[R]

Public Class Methods

new(app, options, &block) click to toggle source
# File lib/rack/pagespeed.rb, line 10
def initialize app, options, &block
  @app = app
  @config = Config.new options.merge(app: app), &block
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/pagespeed.rb, line 15
def call env
  if match = %r(^/rack-pagespeed-(.*)).match(env['PATH_INFO'])
    respond_with match[1]
  else
    status, headers, @response = @app.call(env)
    return [status, headers, @response] unless headers['Content-Type'] =~ /html/
    body = ""; @response.each do |part| body << part end
    @document = Nokogiri::HTML(body)
    @config.filters.each do |filter|
      filter.options[:env] = env
      filter.execute! @document
    end
    body = @document.to_html
    headers['Content-Length'] = body.length.to_s if headers['Content-Length'] # still UTF-8 unsafe
    [status, headers, [body]]
  end
end
respond_with(asset_id) click to toggle source
# File lib/rack/pagespeed.rb, line 33
def respond_with asset_id
  store = @config.store
  if asset = store[asset_id]
    [
      200,
      {
        'Last-Modified' => store.mtime(asset_id).httpdate,
        'Content-Length' => asset.length,
        'Content-Type' => (Rack::Mime.mime_type(::File.extname(asset_id))),
        'Cache-Control' => "public, max-age=#{(60*60*24*365.25*10).to_i}",
        'Expires' => (Time.now + 60*60*24*365.25*10).httpdate
      },
      [asset]
    ]
  else
    [404, {'Content-Type' => 'text/plain'}, ['Not found']]
  end
end