Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Bounce favicon expires #27

Closed
wants to merge 3 commits into from

1 participant

@cactus

add expirity headers to enable caching of the 404 for undesired favicons

For #26

@cactus

Added a couple of fixes (had the wrong name for the class), and a test.

I can't actually test the test, because ruby 1.9 doesn't want to work with specrb for some reason.
Anyway let me know if the test fails on a system that can run it.

@cactus

open for 2 years (wow. seems like not that long ago).
Closing because nobody cares about it.

@cactus cactus closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
31 lib/rack/contrib/bounce_favicon.rb
@@ -1,13 +1,38 @@
+require 'time'
module Rack
- # Bounce those annoying favicon.ico requests
+
+ # The Rack::BounceFavicon middleware intercepts requests for /favicon.ico
+ # and returns a 404 with a cache expirity (default 1 year).
+ #
+ # This middle accepts a hash with possible values as follows:
+ #
+ # :duration - This is the value in seconds that the cache headers will
+ # be set for. The default is 31536000 (1 year).
+ #
+ # Usage Examples:
+ #
+ # Basic usage
+ #
+ # use Rack::BounceFavicon
+ #
+ # Manually specify a cache duration of 5 years.
+ #
+ # use Rack::BounceFavicon, :duration => 365 * 24 * 60 * 60 * 5
+ #
class BounceFavicon
- def initialize(app)
+ def initialize(app, options={})
@app = app
+ @expire_duration = options[:duration] || 31536000 # =1year
end
def call(env)
if env["PATH_INFO"] == "/favicon.ico"
- [404, {"Content-Type" => "text/html", "Content-Length" => "0"}, []]
+ headers = {
+ "Content-Type" => "text/html",
+ "Content-Length" => "0",
+ "Cache-Control" => "max-age=#{@expire_duration}, public",
+ "Expires" => (Time.now + @expire_duration).httpdate }
+ [404, headers, []]
else
@app.call(env)
end
View
33 test/spec_rack_bounce_favicon.rb
@@ -0,0 +1,33 @@
+require 'test/spec'
+require 'rack/mock'
+require 'rack/contrib/bounce_icon'
+
+context "Rack::BounceFavicon" do
+ specify "should return 404 and cache headers for favicon requests" do
+ app = Rack::Builder.new do
+ use Rack::Lint
+ run Rack::BounceFavicon.new
+ end
+ response = Rack::MockRequest.new(app).get('/favicon.ico')
+ response.body.should.equal('')
+ response.status.should.equal(404)
+ response.headers['Content-Length'].should.equal 0
+ response.headers['Cache-Control'].should.equal 'max-age=31536000, public'
+ response.headers.should.contain 'Expires'
+ status.should.equal(404)
+ end
+
+ specify "should return 404 and cache headers for favicon requests when using custom expirity" do
+ app = Rack::Builder.new do
+ use Rack::Lint
+ run Rack::BounceFavicon.new :duration => 365 * 24 * 60 * 60 * 5
+ end
+ response = Rack::MockRequest.new(app).get('/favicon.ico')
+ response.body.should.equal('')
+ response.status.should.equal(404)
+ response.headers['Content-Length'].should.equal 0
+ response.headers['Cache-Control'].should.equal 'max-age=157680000, public'
+ response.headers.should.contain 'Expires'
+ status.should.equal(404)
+ end
+end
Something went wrong with that request. Please try again.